Scaleing, SDL_DOUBLEBUF, SetVidMode, etc

i just want to clear some things up and make sure im right, if you want
to respond to one thing but are overwelmed at all im asking please
respond anyways, you can only explain and answer parts if you want.

1)could someone please explain to me how exactly SDL_Flip,
SDL_SetVidMode and SDL_UpdateRect work with and without SDL_DOUBLEBUFF,
SDL_HWSURFACE and SDL_SWSURFACE (in terms of where the pixel data is
being written and how it gets to the screan)

  1. and as for scaleing, i was looking at the Hermes source and at the
    scaleing code in it, and i was wondering if it would be
    posible/aceptable to integrate it to sdl and implement it in the
    following way:

-eather add a function SDL_SetVideoMode_Scale with the extra option of
the scale, or add flag’s for scaleing such as SDL_SCALE2X (which method
would be preferable?)
-set it up so when you do an SDL_Flip it would automaticly scale it so
the programs code doesnt change at all except in the seting of the video
mode, and the program doesnt have to wory about scaleing and it can be
added to prity much any SDL program really easily. im thinking this
would not work with some of the combinations of options i asked about in

  1. which is why im asking

if you can tell my why this wouldnt work, if it would work, what the
problems are, that im an idiot or whatever please feel free to do so

            Jess

I think I’ll add this to the FAQ.

1)could someone please explain to me how exactly SDL_Flip,
SDL_SetVidMode and SDL_UpdateRect work with and without SDL_DOUBLEBUFF,
SDL_HWSURFACE and SDL_SWSURFACE (in terms of where the pixel data is
being written and how it gets to the screan)

If you request SDL_SWSURFACE, then you get a video buffer allocated in
system memory, and you must call SDL_UpdateRects() or SDL_Flip() to update
the screen. SDL_Flip() calls SDL_UpdateRects(the-whole-screen) in this
case. All allocated surfaces will be in system memory for blit speed.

If you request SDL_HWSURFACE, then if possible SDL will give you access
to the actual video memory being displayed to the screen. If this is
successful, the returned surface will have the SDL_HWSURFACE flag set,
and you will be able to allocate other surfaces in video memory, which
presumably can be blitted very fast. The disadvantage is that video
memory tends to be much slower than system memory, so you don’t want
to write directly to it in most cases. In this case, SDL_UpdateRects()
and SDL_Flip() are inexpensive noops, as you are writing to memory
automatically being displayed.

If you request SDL_HWSURFACE, you may also request double-buffering
by adding the SDL_DOUBLEBUF flag. If possible, SDL will set up two
buffers in video memory for double-buffered page flipping. If this
is successfully set up, then you will be writing to the non-visible
back-buffer, and when you call SDL_Flip(), SDL will queue up a page
flip for the next vertical retrace, so that the current video surface
will then be displayed, and the front and back video buffers will be
swapped. The next display surface lock will block until the flip has
completed.

You should always check to see if you need to lock a surface before
accessing the pixels. A lock is necessary when the SDL_MUSTLOCK(surface)
macro evaluates to true. You should never assume that you can blindly
write to a video surface without locking it, otherwise your code will
work on some platforms and video drivers, but not others (and it’s tough
to debug in the fullscreen modes in which this often matters.)

  1. and as for scaleing, i was looking at the Hermes source and at the
    scaleing code in it, and i was wondering if it would be
    posible/aceptable to integrate it to sdl and implement it in the
    following way:

-eather add a function SDL_SetVideoMode_Scale with the extra option of
the scale, or add flag’s for scaleing such as SDL_SCALE2X (which method
would be preferable?)
-set it up so when you do an SDL_Flip it would automaticly scale it so
the programs code doesnt change at all except in the seting of the video
mode, and the program doesnt have to wory about scaleing and it can be
added to prity much any SDL program really easily. im thinking this
would not work with some of the combinations of options i asked about in

  1. which is why im asking

Is there any reason to explicitly ask for 2X scaling? Wouldn’t it make
sense just to switch the monitor to the requested mode?

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

This explanation was very nice. Thanks Sam. I do have some additional
questions though.

  1. What are the requirements to actually get a hw surface when you ask for
    it? I presume you cannot get it under X11? If so what should be used
    instead?

  2. Is there any other restrictions on dubblebuffering than the fact that
    there have to be enough free videomemory? If so, what are the restrictions?

Thanks in advance
Mattias Blomqvist> -----Original Message-----

From: EXT Sam Lantinga [mailto:slouken at devolution.com]
Sent: 14. April 2000 9:33
To: sdl at lokigames.com
Subject: Re: [SDL] Scaleing, SDL_DOUBLEBUF, SetVidMode, etc…

I think I’ll add this to the FAQ.

1)could someone please explain to me how exactly SDL_Flip,
SDL_SetVidMode and SDL_UpdateRect work with and without
SDL_DOUBLEBUFF,
SDL_HWSURFACE and SDL_SWSURFACE (in terms of where the pixel data is
being written and how it gets to the screan)

If you request SDL_SWSURFACE, then you get a video buffer allocated in
system memory, and you must call SDL_UpdateRects() or
SDL_Flip() to update
the screen. SDL_Flip() calls
SDL_UpdateRects(the-whole-screen) in this
case. All allocated surfaces will be in system memory for blit speed.

If you request SDL_HWSURFACE, then if possible SDL will give
you access
to the actual video memory being displayed to the screen. If this is
successful, the returned surface will have the SDL_HWSURFACE flag set,
and you will be able to allocate other surfaces in video memory, which
presumably can be blitted very fast. The disadvantage is that video
memory tends to be much slower than system memory, so you don’t want
to write directly to it in most cases. In this case,
SDL_UpdateRects()
and SDL_Flip() are inexpensive noops, as you are writing to memory
automatically being displayed.

If you request SDL_HWSURFACE, you may also request double-buffering
by adding the SDL_DOUBLEBUF flag. If possible, SDL will set up two
buffers in video memory for double-buffered page flipping. If this
is successfully set up, then you will be writing to the non-visible
back-buffer, and when you call SDL_Flip(), SDL will queue up a page
flip for the next vertical retrace, so that the current video surface
will then be displayed, and the front and back video buffers will be
swapped. The next display surface lock will block until the flip has
completed.

You should always check to see if you need to lock a surface before
accessing the pixels. A lock is necessary when the
SDL_MUSTLOCK(surface)
macro evaluates to true. You should never assume that you can blindly
write to a video surface without locking it, otherwise your code will
work on some platforms and video drivers, but not others (and
it’s tough
to debug in the fullscreen modes in which this often matters.)

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

This explanation was very nice. Thanks Sam. I do have some additional
questions though.

  1. What are the requirements to actually get a hw surface when you ask for
    it? I presume you cannot get it under X11? If so what should be used
    instead?

The underlying video driver must have access to the video memory.
You are correct that in X11 you do not have access to the video memory,
without using the DGA video driver. In this case your blits will all
be done in software, usually using highly optimized C or asm blitters.

  1. Is there any other restrictions on dubblebuffering than the fact that
    there have to be enough free videomemory? If so, what are the restrictions?

The underlying driver must be able to change the video display panning
during the vertical blank and report when the flip has completed.

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

This explanation was very nice. Thanks Sam. I do have some
additional
questions though.

  1. What are the requirements to actually get a hw surface
    when you ask for
    it? I presume you cannot get it under X11? If so what should be used
    instead?

The underlying video driver must have access to the video memory.
You are correct that in X11 you do not have access to the
video memory,
without using the DGA video driver. In this case your blits will all
be done in software, usually using highly optimized C or asm blitters.

  1. Is there any other restrictions on dubblebuffering than
    the fact that
    there have to be enough free videomemory? If so, what are
    the restrictions?

The underlying driver must be able to change the video display panning
during the vertical blank and report when the flip has completed.

Any example of a videodriver that can handle both these things?

Thanks
Mattias

Sam Lantinga wote:

If you request SDL_SWSURFACE, then you get a video
buffer allocated in system memory, and you must call
SDL_UpdateRects() or SDL_Flip() to update the screen.
SDL_Flip() calls SDL_UpdateRects(the-whole-screen) in
this case. All allocated surfaces will be in system memory
for blit speed.

[ snip ]

If you request SDL_HWSURFACE, you may also request
double-buffering by adding the SDL_DOUBLEBUF flag.
If possible, SDL will set up two buffers in video memory for
double-buffered page flipping. If this is successfully set up,
then you will be writing to the non-visible back-buffer, and
when you call SDL_Flip(), SDL will queue up a page flip for
the next vertical retrace, so that the current video surface
will then be displayed, and the front and back video buffers
will be swapped. The next display surface lock will block
until the flip has completed.

Does it make sense to have SDL_SWSURFACE | SDL_DOUBLEBUF support? If
something is written that makes use of the fact that the back buffer should
contain the contents of frame(n-2), it will fall apart if it can’t get both
hardware surfaces and double buffering. I guess this would require two
software surfaces where SDL_Flip() dumps the active one to the physical
screen and then alternates between the two.

This may be a moot point anyway. Are there any double buffered dirty
rectangle algorithms out there? Double buffering works great but I hate
starting from a blank slate after each flip. I would also hate to special
case a complicated dirty rectangle system depending on the available
hardware or the state of SDL_FULLSCREEN.

On a related note, has anyone come across the need for triple buffering
(SDL_TRIPLEBUF?) where you have frame(n-2) on the screen, frame(n-1) waiting
for the vertical retrace, and frame(n) being created (based on the contents
of frame(n-3) )? Some applications actually use quadruple buffering but
I have not seen any benchmarks on quad versus triple buffering and have seen
relatively little on the “real” advantages of going from double to triple
buffering.

  • Randi

Regimental Command
Generic Armored Combat System
http://www-users.cs.umn.edu/~relander/regcom/index.html

Sam Lantinga wrote:

You should always check to see if you need to lock a surface before
accessing the pixels. A lock is necessary when the SDL_MUSTLOCK(surface)
macro evaluates to true. You should never assume that you can blindly
write to a video surface without locking it, otherwise your code will
work on some platforms and video drivers, but not others (and it’s tough
to debug in the fullscreen modes in which this often matters.)

Locking? Is that new? And rather than have an SDL_MUSTLOCK macro, why
not require locking all the time and make it a no-op on surfaces that
don’t require it?–
Pierre Phaneuf
http://ludusdesign.com/

“Randi J. Relander” wrote:

On a related note, has anyone come across the need for triple buffering
(SDL_TRIPLEBUF?) where you have frame(n-2) on the screen, frame(n-1) waiting
for the vertical retrace, and frame(n) being created (based on the contents
of frame(n-3) )? Some applications actually use quadruple buffering but
I have not seen any benchmarks on quad versus triple buffering and have seen
relatively little on the “real” advantages of going from double to triple
buffering.

The advantage with triple buffering over double buffering is that you
can make use of asynchronous blitting. With async blits, usually you
cannot access the pixel data of the blitted surface until the blit is
finished. So that’s where the second backbuffer comes in play, you can
start building it without waiting for the first one to be finished
blitting.

Quadruple buffering (or N buffering) might be of some use if you
generate frames so quickly that you can build the second back buffer
before the first one is finished blitting. With triple buffering, you
have to wait for the first one to be ready before messing with it again.

I doubt this is really useful.–
Pierre Phaneuf
http://ludusdesign.com/

Does it make sense to have SDL_SWSURFACE | SDL_DOUBLEBUF support?

Internally, if you pass in SDL_DOUBLEBUF, SDL adds SDL_HWSURFACE. :slight_smile:

-Sam Lantinga, Lead Programmer, Loki Entertainment Software

The underlying driver must be able to change the video display panning
during the vertical blank and report when the flip has completed.

Any example of a videodriver that can handle both these things?

The fbcon driver is a great example. :slight_smile: Both the matrox and 3dfx
drivers do this.

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

Sam Lantinga wrote:

“Randi J. Relander” wrote:

Does it make sense to have SDL_SWSURFACE |
SDL_DOUBLEBUF support?

Internally, if you pass in SDL_DOUBLEBUF,
SDL adds SDL_HWSURFACE. :slight_smile:

But if the hardware doesn’t support it and/or you are going windowed (not
full screen) you end up back in software mode with just the single back
buffer to work with. The problem is that a “double buffered dirty rectangle"
algorithm would fall apart in this case. I guess what I need is an
"N-buffered dirty rectangle” algorithm where “N” is two if you actually get
double buffering and one otherwise. I really want to eliminate code
dependency on window/fullscreen mode, single/double buffer, screen size,
color depth, etc.

  • Randi

Regimental Command
Generic Armored Combat System
http://www-users.cs.umn.edu/~relander/regcom/index.html

Sam Lantinga wrote:

Is there any reason to explicitly ask for 2X scaling? Wouldn’t it make
sense just to switch the monitor to the requested mode.

well its really only useful in windowed mode, i thought about it a little
more and was thinking it would probly be beter to provide a more generic
stretch to where it could stretch it to any size, and if it was say 2x then
it could chose the apropriate optimized bliter to use for it. and this would
have some use in fullscreen mode to, stretching one size to a suported mode
or just to make it look funky :slight_smile: Would also be useful and easy to implement
a scaled bliter (SDL_BlitSurface_Scaled or whatever) which would also be
useful for other things, could also posibly be used to scale down say video
or images to display in a smaller resolution.

Jess