Site Sections

Friday, October 26, 2007

Optimizations - Simple and Effective

I was working on my engine the other night, trying to get my new managers to work with a simple map parser that I am working on (I will be putting up the tutorials on the managers in a few days), and I noticed that I was starting to get some major slow down while rendering models, especially when I got any more than say 20 objects on the screen. Lag city I tell ya! It was really depressing.

Time to optimize...

C#, like many languages (especially C/C++) give you the option to pass values by reference. This means that instead of "passing by value" meaning copying the contents of a variable into another stack variable created for the called functions parameters, we instead simply create a reference (or pointer) to the location in memory where the passed variable is stored. This works great for larger objects as we do not want to have to create a separate copy of every object we want to pass as a parameter, especially if we are passing the object down a couple of calls.

This was my first optimization that I noticed I could perform. Now C# has two forms of pass by reference when dealing with function calls:
  1. Simply to pass a reference to a function by identifying it with the "ref" keyword, this must be placed in the function definition and the function call.
  2. Passing return variables by reference, this requires the use of the "out" keyword before the return variable INSIDE of the parameter list. We do not return these variables as normal, instead we can assign them a value and it acts as if we are assigning the value to the variable from the calling function scope. This is much faster than using standard returns when using them for assignment operations, also it allows for multiple return values. I have modified my managers to use this form of returning when doing searches for objects contained within.
This sped up the Hazy Mind engine quite a bit on its own. I did notice a major improvement when it came to the return speed on the shader manager. This got me thinking as to why the shader manager was giving me such poor performance. I started looking around and I noticed that in all the draw loops for objects, the Hazy Mind engine uses the shader manager to request the shader to draw with every time through the draw loop.

Hmmmm, I think to myself, why are we doing this? I am assuming it was in case someone wanted to change the shader of an object in the middle of the game? Well why not then move this expensive search cost to the setShader function and store a "reference" to these objects locally. Small memory hit, vast performance increase. We amortize the cost of searching to constant (actually practically nothing since we shouldn't need to change shaders on specific objects very often) and tada! almost instant improvement in speed, I can have many more objects on the screen with no lag. I have my render framerate fixed and it doesn't jump at all. I can imagine that my unfixed frame rate would now be through the roof.

So key things to remember:
  1. Use pass by ref, especially in functions that you are not going to change the state of the parameter objects. It really isn't all that much of a benefit on basic data types as their size is pretty much the same as the size of the reference.
  2. Use out return parameters for managers that return bulky objects such as models.
  3. keep search times low by not putting them in draw loops (this is very very important!)
Hope this helps!

Wednesday, October 17, 2007

Engine work again!

Well, it looks like I have found some time to do some more work on the engine. Last night I started writing a few more managers to handle static textures and models for my next goal of building a level editor. I noticed that the Hazy mind engine loads each texture and model at the time that the the load content function is called. It also loads the same texture multiple times if the developer creates multiple copies of the same object. I think that I can streamline this assuming that I know what textures and models are going to be loaded like a library.

This would make sense in a standard game as I can develop a map file that contains a list of all the assets that are going to be used in a level. These can be loaded into a manager and used like a lookup table when they are required.

When I get this functionality fully working I will post a tutorial here. Check back soon!

Tuesday, October 9, 2007

Car Troubles...

Man, sometimes I just feel like quitting driving. If only I could, I think I would. Not only would it save me the money in gas, but also from dealing with leaking radiators, bad service at Precision Auto, and all around car problems.

So here is my story:

My car was overheating, it would take radiator fluid while it was cool and it wouldn't leak. Turns out that when I ran it, it would still overheat no matter how much coolant I put in it. Also when I turned off the car, it would leak like a siv from the front corner of my driver side. I looked under the hood, and it didn't seem like any hose was leaking that I could see, so off to Precision Auto to get it checked out for radiator leaks. They look at it, never call me back with an estimate, figure out apparently that a clamp on a hose has come loose. I call them a few hours later right before they close and they say this to me and tell me that they have already fixed it, without my consent. I should have told them that, that was it and I wasn't going to pay them since they never called me back. Well, I was just happy to have my car fixed I paid it, whatever. Turns out it was 48 dollars in labor just to fix a clamp and run a pressure test. They tell me it turned out ok, radiator holds pressure, cost me a buck for a new clamp.

I drive it home, no overheating, so I'm happy right? Drive it to work and I didn't even pay attention to whether or not it was fixed, it seemed to be running fine. I get out of work just as Precision is about to close and notice that my heat is rising again... now I'm pissed right? They said it should be fixed. I get it home and look, sure enough, still leaking. I pull out my cell and give Precision a call back, and they say "Oh it might be your thermostat sticking, I can get you in on Thursday".

This is ridiculous, I shouldn't have to pay for service again, and this time I tell you I won't. Parts maybe but definitely not labor, it's their fault, they told me it was fixed. Moral of the story, walk or ride a bike, it's healthier, better for the environment, and a whole lot cheaper. If only I could, too bad I live in the U.P. of Michigan...

... needless to say, I have been dealing with either trying to find a new car to purchase or fix this one, not so much working on my game... Very sad :( hoping to get back to it soon, right after I beat Resident Evil for GC.

Monday, October 1, 2007

Woot for scanners

Yay! I finally got my new scanner on Friday, unfortunately I have had absolutely no time to play with it yet. I got the software installed just before I had to take off for the weekend to Marquette. Well, hopefully I will get the chance to play with it tonight and get some pictures that I have drawn up here.

As far as the scanner is concerned though, it is a Canoscan Lide 25 from Canon, it is a nice little scanner, smaller than a 17 inch widescreen laptop and fits in my laptop bag. The best part though is that it doesn't have a power supply, it is powered solely through the USB cable. This is nice for those crazy occasions where you are on the road and your life depends on scanning an image into your laptop on the go. (I don't know when that would be but just in case...) You can check it out here - (NewEgg) if you would like to get one yourself, they are cheap, only 60 bucks! Woot!