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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top