AlphaBlend - why the high CPU usage?

Status
Not open for further replies.

ulillillia

Distinguished
Jul 10, 2011
551
0
19,010
I use AlphaBlend to do all of my game's drawing. Question is, why does it cause a lot of CPU usage? It seems to scale with the number of pixels drawn. For example, in the worlds that have precipitation, there are several layers of the scenery that cover the entire screen (which is 1024x768). Few total draws occur each frame, but there is a high number of pixels being drawn. Trouble is, the CPU usage goes up significantly. Likewise, a similar case applies with tons of draws of small images. My question revolves around the large, full screen images - why does this result in a lot of CPU usage (nearly maxing one core of my i7-2600K processor overclocked to 4 GHz)? High GPU usage I'd expect, but that only hovers around 60% (back when I had the 7600 GT - I haven't checked it with my GTX 460 yet).
 

theDanijel

Distinguished
May 4, 2011
150
0
18,710
http://msdn.microsoft.com/en-us/library/windows/desktop/dd370990(v=vs.85).aspx

Direct2D exists and is hardware accelerated, Windows 7 and Vista use it to render it's GUI (that is why you need at least a dx9 GPU with vista and 7) instead of the GDI/GDI+ used in Windows XP.

AlphaBlend is hardware accelerated, but can be done in software mode for compatibility (GDI/GDI+ and available in Direct2D). Since your CPU usage is high, looks like software mode is the case here or/and you have something stressing your CPU in the logic update loop.
Since there is a lot of GPU usage with the CPU usage I'm guessing this is not a 2D game. What libraries do you use for rendering and for everything else?
 

ulillillia

Distinguished
Jul 10, 2011
551
0
19,010
It is actually a purely 2D game. It looks highly 3D due to the way I use parallax scrolling and the fact I've taken it to an extreme. I don't know any 3D-related stuff and I don't have a modeling program to make 3D objects with. The only library I'm using for drawing is just the one for AlphaBlend. I use another for my timing system, the one that has the "timeGetTime" function.

If I comment out all of my drawing, CPU and GPU usage drops to next to nothing even though positions and stuff are still being calculated. If I uncomment the drawing, both return to high values. High values. My debug panel is showing about 1000ish draws per frame (that's 60,000 draws per second) for the most part and about 5 million pixels per frame (300 million per second). If I have a lot of text or small objects on screen (as from pause menus), I could very well get 3000ish draws per frame and the CPU and GPU usage is extremely high even though only 1 million more pixels are drawn per frame. Starting around 4000 draws per frame, I notice the frame rate dropping below 60 fps.

However, with only 300 draws of a larger-sized image, such as 256x256 (instead of 5x8), the CPU and GPU usage are still high, though with 30 million pixels per frame (1.8 billion per second), I still get high CPU and GPU usage. I would expect high GPU usage with that, but not CPU usage. I could post screenshots to explain the effects if you want.

Edit: from that link, it appears that Direct2D won't work for me because it doesn't support XP, an absolute must for me.
 


AlphaBlend MAY be accelerated, but you cannot guarantee hardware support on any given platform.

Also, you are thinking of DirectDraw. . .
 

theDanijel

Distinguished
May 4, 2011
150
0
18,710


Seems like there is a big problem when you handle your content.
Since you don't use a more standard game engine library, seems like you are not handling your resources well. To me it looks like you are trying to draw textures that are not even in the scene and that is causing you system to spend too much resouces. Try checking if something is even in the scene before you draw it.

May I sugest you use XNA to make it a lot easier. It is very good for 2D games (I use it for makeing windows phone and windows games) and it has it's own timing system and sound system. The only drawback is that it's limited to microsoft platforms and that includes XP ;)
 

ulillillia

Distinguished
Jul 10, 2011
551
0
19,010
If the object is not on screen, I don't draw it. When extremely high above everything, the draw count really drops as well as the pixels drawn per frame. I'm also cropping as well to keep the total drawn-pixel counts as low as possible. Here are 3 screenshots showing what I mean, with Windows Task Manager in the background:

AlphaBlendFrameRateProblems1.jpg


This shows a case of about 5600 draws per frame. This is because the very far buildings are 6x2 pixels, almost nothing. Here, I'm getting about 50 frames per second, not all that bad, but it results in the scene being jerky rather than smooth.

AlphaBlendFrameRateProblems2.jpg


With tons of height, way, way above everything, I could be running at about 140 fps with this scene.

AlphaBlendFrameRateProblems3.jpg


This is a case where the pixels drawn per frame is extremely high (due to all the snow from the snowstorm - 9 layers that cover the full 1024x768 display area) with relatively few draws per frame. When I had my GeForce 7600 GT, I was getting 100% CPU usage on one core and about 60% GPU usage despite about 35 to 40 fps. With my new GTX 460, it's no longer a problem as I can get about 100 fps like this.

In all 3 cases, note the two fields of data on my debug panel on the bottom left corner. The top of these is how many draws are being made each frame. Multiply this by 60 to get draws per second. The other is for the number of pixels drawn each frame. Again, multiply this by 60 to get how many pixels are drawn every second. The third screenshot has over a billion pixels drawn per frame. The first isn't even close to a half billion and does much worse.

I do intend on using SDL, though originally only for vsync and fullscreen. I was only wondering why AlphaBlend was causing so much CPU usage with just drawing.
 

theDanijel

Distinguished
May 4, 2011
150
0
18,710
Last scene in this case is far better optimised than the first scene. In the first scene you have lots of distance calculations with positioning of the buildings, size-distance, texture resize, etc... While in the last scene you just get everything there with no special calculations.
In short, if this is a 2D game, you are overdoing in trying to make it look like 3D and that is using your CPU. Something like this these days is done with a 3D engine because of better hardware optimisation and a way easier way of doing things (the game engine does all the caluculateing, cliping, LOD, rendering, etc., you just put the scene together). All you have to do is just limit character movement to a 2D plane.
In your case, you calculate what would simply be a LOD (level of detail) and distant object rendering, or more simply put this way you switch the calculation from your GPU to your CPU.
 
Status
Not open for further replies.