Sign in with
Sign up | Sign in
Your question

AlphaBlend - why the high CPU usage?

Tags:
  • Programming
  • Games
  • CPUs
  • Apps
Last response: in Apps General Discussion
Share
December 26, 2011 3:40:13 AM

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).

More about : alphablend high cpu usage

a b L Programming
December 29, 2011 8:13:15 PM

That is a Windows GDI function and is not hardware accelerated. If you want hardware acceleration then you need to use Direct2D, Direct3D, or OpenGL.
m
0
l
December 30, 2011 4:23:32 AM

I've been told that AlphaBlend is hardware accelerated. Direct2D doesn't exist any more (after DirectX 7 if I recall) - it's now under Direct3D.
m
0
l
Related resources
December 30, 2011 8:46:34 AM

http://msdn.microsoft.com/en-us/library/windows/desktop...(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?
m
0
l
December 30, 2011 1:07:41 PM

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.
m
0
l
a b L Programming
December 30, 2011 5:52:08 PM

ulillillia said:
I've been told that AlphaBlend is hardware accelerated. Direct2D doesn't exist any more (after DirectX 7 if I recall) - it's now under Direct3D.


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

Also, you are thinking of DirectDraw. . .
m
0
l
a b L Programming
December 30, 2011 6:00:38 PM

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

Then to insure hardware acceleration of your alpha blended blts you can use an Orthographic Projection with either Direct3D or OpenGL. Many Linux 2D games use OpenGL.
m
0
l
January 1, 2012 12:42:57 AM

ulillillia said:
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.


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 ;) 
m
0
l
January 1, 2012 4:04:17 AM

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:



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.



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



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.
m
0
l
January 1, 2012 10:26:28 AM

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.
m
0
l
!