SDL_FillRect question

Hi, could someone explain to me how SDL_FillRect works? and how it goes so fast?
it goes faster than a loop through the screen’s pixels setting each one to a color.

for(unsigned int coord = 0;coord<position_max;coord++)
{
screen_pixels[coord] = 61;
}

that loop takes 1-2ms on my machine and SDL_FillRect runs faster than a millisecond.
I’m just preplexed at how the “fast fill” could actually be achieved. I have one guess, but
it would involve a seperate data structure being created for all the “occupied” pixels on the screen.

It goes faster by letting the GPU optimize it.? Communication between the CPU and the GPU is slow, and
if you set each pixel individually, that’s a whole bunch of slow calls.? But if you only send a single slow
"color thisentire rect" call, and then let the GPU worry about the details, it cuts out a lot of communication
overhead.________________________________
From: Omar Eshmawi <neo_storm at live.com>
Subject: [SDL] SDL_FillRect question

Hi, could someone explain to me how SDL_FillRect works? and how it goes so fast?
it goes faster than a loop through the screen’s pixels setting each one to a color.

?for(unsigned int coord = 0;coord<position_max;coord++)
?{
??? screen_pixels[coord] = 61;
?}

that loop takes 1-2ms on my machine and SDL_FillRect runs faster than a millisecond.
I’m just preplexed at how the “fast fill” could actually be achieved. I have one guess, but
it would involve a seperate data structure being created for all the “occupied” pixels on the screen.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I don’t know all the details, but I do know that it uses memset and
possibly MMX/assembly behind the scenes, which can be much faster than
manually ‘touching’ each pixel.

SAOn December 25, 2011 3:43:14 PM Omar Eshmawi wrote:

Hi, could someone explain to me how SDL_FillRect works? and how it goes
so fast? it goes faster than a loop through the screen’s pixels
setting each one to a color.

for(unsigned int coord = 0;coord<position_max;coord++)
{
screen_pixels[coord] = 61;
}

that loop takes 1-2ms on my machine and SDL_FillRect runs faster than a
millisecond. I’m just preplexed at how the “fast fill” could actually
be achieved. I have one guess, but it would involve a seperate data
structure being created for all the “occupied” pixels on the screen.

Yeah, memset() is awfully fast if you’re talking about the software
SDL_FillRect().

Jonny DOn Sun, Dec 25, 2011 at 2:22 PM, Stephen Anthony wrote:

On December 25, 2011 3:43:14 PM Omar Eshmawi wrote:

Hi, could someone explain to me how SDL_FillRect works? and how it goes
so fast? it goes faster than a loop through the screen’s pixels
setting each one to a color.

for(unsigned int coord = 0;coord<position_max;coord++)
{
screen_pixels[coord] = 61;
}

that loop takes 1-2ms on my machine and SDL_FillRect runs faster than a
millisecond. I’m just preplexed at how the “fast fill” could actually
be achieved. I have one guess, but it would involve a seperate data
structure being created for all the “occupied” pixels on the screen.

I don’t know all the details, but I do know that it uses memset and
possibly MMX/assembly behind the scenes, which can be much faster than
manually ‘touching’ each pixel.

SA


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

wow, well then I have 2 follow-up questions, is a loop still the fastest method for something purely computational? and is SDL_Flip the fastest function for drawing the screen surface?

Plain statements are the fastest without getting into vector operations.
Imagine you want to change each of ‘n’ pixels in a diagonal line. You
could write a loop and change each x==y pixel or you could write 'n’
setpixel calls (plain statements). This is one form of loop-unrolling that
can speed up some operations. Obviously, there are drawbacks to writing it
out explicitly, like flexibility, that are worth more than the speed.
Beside that, there are some functions which work on regions of memory
(like memset) and special SIMD (vector) instructions like MMX and SSE which
work on multiple pieces of data at once, whereas a pixel loop works on a
single piece of data at a time.

SDL_Flip() doesn’t really draw anything. Once you’ve changed the display
surface, SDL_Flip() updates the surface in video memory, so you could get
more speed by updating just the regions that change with SDL_UpdateRects().
Some games with mostly static screens use a dirty rect system to get a
little more performance.

Jonny DOn Mon, Dec 26, 2011 at 3:29 AM, retardedtroglodyte <neo_storm at live.com>wrote:

**
wow, well then I have 2 follow-up questions, is a loop still the fastest
method for something purely computational? and is SDL_Flip the fastest
function for drawing the screen surface?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org