Lots of SetPixel's are slow, so how do games do it?

D.V.D

Make a wish
Reaction score
73
I made a simple function to see how fast my cpu can draw a decent chunk of the screen a certian color. The thing I found out is, using the SetPixel function to do it is extremely slow. So how do games do it? How is drawing a bitmap any faster than setting the pixels?

Code:
void drawlots(HDC hdc) {
	COLORREF col=RGB(0,0,0);
	for (short i=0; i<1000;i++){
		for (short y=0;y<500;y++) {
			SetPixel(hdc,i,y,col);
		}
	}
}
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
There's a ton of reasons why this is slow:

First of all, this little piece of code means you're running SetPixel() 500,000 times! Calling a function and passing all the parameters to the callee take a little bit of time in itself. The program needs to allocate some stack memory, copy over all data, jump to the point in memory where your function is at (which wastes CPU cycles by cache trashing because of the jump), do all it's work and jump back to the caller's memory point again.

Second, SetPixel does more than just flip a few bits, it'll need to get the screen data linked to that device context and check if the given coordinates are within it's bounds, then release the data again. Everytime you call this function it'll have to do this over and over again.
SetPixel also returns a value (the color that's been drawn) and even though you don't do anything with the value it'll still slow down your program.

The actual "drawing" (meaning the modification of the memory bits where the screen pixels are stored in) is very fast.

If you draw a bitmap you're passing the entire picture in one swoop instead of doing all this ugly labor again and again.

It might even be worse. I don't know how SetPixel() works internally, but if it directly communicates with your graphics card that's another bottleneck. CPU is very slow in comparison to a GPU. That means every time the CPU sends data the GPU is stalled.

In games, one of the most important things to make stuff fast is to streamline data into your GPU smoothly. You try to let the GPU work a lot because it's so friggin fast and to only send data to it when necessary.
If you want certain changes to images then there's often shaders (which are used by the GPU - so it's fast) or other trickeries.

But there are ways to change each pixels without SetPixel.
I found this link for example
 

D.V.D

Make a wish
Reaction score
73
Interesting Ill check the link out. Oh I see why its slow. And I realize its 500 000 calls but loading a desktop background thats full 1080p would result in over 2 million calls to modify every single bit on the computer screen to the image. Yet its done fairly quickly. I also found some info on programming on the GPU. Working with shaders doesn't give you full control over your GPU the same way programming on your CPU gives you full control. Using opencl or in my case CUDA, you can create programs that run across hundreds of cores and even more threads. I was planning to try that out but Im having trouble getting it to work with visual studio C++.
 

D.V.D

Make a wish
Reaction score
73
In your Gpu. Look at the specs for a GTX 460 or 570, they have 448 cores but they're not clocked as high as CPU cores.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
And I realize its 500 000 calls but loading a desktop background thats full 1080p would result in over 2 million calls to modify every single bit on the computer screen to the image. Yet its done fairly quickly.

That's because you don't load each pixel individually, but in bunches. Displaying a 40x40 image on your screen doesn't invoke the SetPixel function 1600 times. Instead the entire pixel data is taken and drawn in one fell swoop.

Code:
void print(int i){
    cout << i;
}

void main(){
    int[] array = {1,6,4,2,7}

    for(int i=0; i<array.length; ++i){
        print( array[i] );
    }
}


Code:
void print(int[] arr){
    for(int i=0; i<arr.length; ++i){
        cout << arr[i];
    }
}

void main(){
    int[] array = {1,6,4,2,7}

    print( array );
}

Look at those two pieces of pseudo code. They're both doing the same, but the first one invokes print 5 times. The second piece of code would be slightly faster because it saves 4 out of these 5 calls. Calling functions takes time too.

Of course the difference in this example would be negligible, it won't even be measurable. But when the difference is 1 to 500,000 it suddenly grows. And as I said, that's only one part where SetPixel would be slow.

You're right that the GPU isn't as programmable as the CPU, but that comes with it's power. The reason our CPUs aren't as powerful as our GPUs is because they need to be much more flexible. Flexibility comes with a price. Otherwise there wouldn't be a reason to replace your CPU with a GPU :3

When working with anything to display you generally try not to use things like SetPixel but you try to process as much data at once as you can. I brought shaders up because they can be used to modify your scene in ways where you might want SetPixel for (e.g. recoloring). If you really need to set a lot of single pixels you generally look for a trick around that.
E.g. put all the single pixels into a transparent image and display the entire image (or parts of it).
 

D.V.D

Make a wish
Reaction score
73
Oh I see, so what if I set pixel data of a bitmap and then just draw the bitmap. Would that technically be slightly faster or would it still carry the problems that using mass SetPixel's would because you would be pretty much calling mass SetPixel's on a bitmap instead of the HDC. The reason why I want to modify each pixel separately is for a attempt later on making voxels :p
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Drawing bitmap data would probably be a good chunk faster, yes.

A bitmap isn't much more than a chunk of rawr RGB values, so their modification isn't very complex. But as explained in the link in my first post there might need to be some padding depending on the image size. Also it might store pixels in RBG and not RGB..
 

D.V.D

Make a wish
Reaction score
73
Ill use the windows bitmap, it should use COLORREF values so RGB ill check however and see how fast it turns out.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top