Need to write directly to video memory, can I do it with SDL?

Hi,

can SDL solve this problem ?

I have to implement some kind of app which basically must analyze audio and
produce realtime FFT spectograms.

I need a bit of GUI (a few buttons, filerequests, sliders etc) , thus I’m
developing it using Qt.

But I want to to have a sort of FFT-spectrum analyzer which must be
as FAST as possible AND must run in a window.
Going through the X server either using Qt’s Pixmaps or the SDL calls (in
windowed mode) is WAY too slow.

My question: is there a way to figure out the layout of the videoram of the
gfx card , mmap the videoram and the write to it direcly ?

I’m more that happy if I’m able to support let’s say only a few formats like
8bit colormapped or 24/32bit RGB color modes.
Portability is not a big concern.

The target system will problably be a Redhat 6.2 box with XFree 3.x.

So my questions:

does SDL allow me to figure out the layout of the videosurface (depth,
pixelformat etc) and mmap the videoram into userspace mem ?

Or do I need to use the X11 APIs for this ?
(if yes, any hints where I can get more infos how to implement this ?)

Or do I need XFree 4.0 for this ?

I do not care if some garbage will be left on the screen when moving the
FFT-analyzer window (which will basically be an empty Qt window where I draw
into , but addressing direcly the screen surface giving the illusion that I’m
drawing into the real window.

As said absolute precedence is speed , and I want the Xserver totally out of
the way since going through it is way too slow for my app.

Using the linux framebuffer or SDL fullscreen is not an option because I need
some GUI elements to control my app and I’m not willing to write a custom GUI
for this.

Thanks for infos.

Benno.

Using the linux framebuffer or SDL fullscreen is not an option because I need
some GUI elements to control my app and I’m not willing to write a custom GUI
for this.

Would it be option to be in either fullscreen fast analyzer mode or window
system gui mode, taking turns. When you see one, you don’t see the other,
but you can flip screen? I think that might be possible with SDL frame
buffer.

– Timo Suoranta – @Timo_K_Suoranta

Using the linux framebuffer or SDL fullscreen is not an option because I need
some GUI elements to control my app and I’m not willing to write a custom GUI
for this.

It’s not possible to directly access the screen in windowed mode,
using any Linux API.

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

It’s not possible to directly access the screen in windowed mode,
using any Linux API.

Not even though a crude hack ?
I mean if the X server is able to access the videoram, why shoudn’t an
application be able to do so ?
Or does the X server lock down the access to this memory areas so that no
other apps can access it.

I’m not a video expert, but my commonsense says that this should be possible.
(thus I cannot estimate how hard it would be and how much amount of hacking
it would need).

I remember that someone mentioned this memory mapping of video ram in some
discussion board, thus my questions.
(if anyone can confirm that one cannot mmap the video ram into userspace while
running under an X server , then I will shut my mouth)

thanks for the infos,

cheers,
Benno.On Friday 06 April 2001 22:47, you wrote:

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

It’s not possible to directly access the screen in windowed mode,
using any Linux API.

Not even though a crude hack ?
I mean if the X server is able to access the videoram, why shoudn’t an
application be able to do so ?
Or does the X server lock down the access to this memory areas so that no
other apps can access it.

I’m not a video expert, but my commonsense says that this should be possible.
(thus I cannot estimate how hard it would be and how much amount of hacking
it would need).

I remember that someone mentioned this memory mapping of video ram in some
discussion board, thus my questions.
(if anyone can confirm that one cannot mmap the video ram into userspace while
running under an X server , then I will shut my mouth)

It is quite possible to write directly to video memory; that’s what the XFree86
DGA extension is used for. You need write access to /dev/mem to do this, however.

By the way, I don’t believe there is any way to “lock down” memory areas. You can
access /dev/mem and change any part of it, though most attempts to do so would
probably crash the machine, or worse…

Regards,

Alex.On Sat, 7 Apr 2001 00:42:14 +0200 Benno Senoner wrote:

On Friday 06 April 2001 22:47, you wrote:

It is quite possible to write directly to video memory; that’s what the
XFree86 DGA extension is used for. You need write access to /dev/mem to do
this, however.

Yes, as said I can run the app as root, this is not a problem.

The problem is to figure out the startaddress (offsets between row etc),
pixel layout etc etc. (eg if it is RGB, 8bit colormapped etc)

Can SDL used for this stuff, or do I need some XFree calls ?

I’d would be grateful for any hint in that direction

cheers,
Benno.On Saturday 07 April 2001 04:09, you wrote:

By the way, I don’t believe there is any way to “lock down” memory areas.
You can access /dev/mem and change any part of it, though most attempts to
do so would probably crash the machine, or worse…

Regards,

Alex.

No, the real problem is that the X server uses the hardware’s accelerator,
and if you try to access the video memory at the same time, you can lock
up many modern video cards. So, direct pixel access while the X server
is still active handling windows is not practical. You mentioned that a
fullscreen application wasn’t practical, so no, there’s no way to do what
you want.

See ya,
-Sam Lantinga, Lead Programmer, Loki Entertainment Software> On Saturday 07 April 2001 04:09, you wrote:

It is quite possible to write directly to video memory; that’s what the
XFree86 DGA extension is used for. You need write access to /dev/mem to do
this, however.

Yes, as said I can run the app as root, this is not a problem.

The problem is to figure out the startaddress (offsets between row etc),
pixel layout etc etc. (eg if it is RGB, 8bit colormapped etc)

It is quite possible to write directly to video memory; that’s what the
XFree86 DGA extension is used for. You need write access to /dev/mem to
do this, however.

Yes, as said I can run the app as root, this is not a problem.

The problem is to figure out the startaddress (offsets between row etc),
pixel layout etc etc. (eg if it is RGB, 8bit colormapped etc)

No, the real problem is that the X server uses the hardware’s accelerator,
and if you try to access the video memory at the same time, you can lock
up many modern video cards. So, direct pixel access while the X server
is still active handling windows is not practical. You mentioned that a
fullscreen application wasn’t practical, so no, there’s no way to do what
you want.

Omk, thanks for the explanation, I haven’t thought about the lockup issues,
you are right. (modern gfx cards do have command queues and similar magic
stuff).

So meanwhile I will use standard APIs (SDL & co),and I’m looking forward to
support for hw-accelerated surfaces in windowed mode within SDL.

Sam, is this planned for the upcoming versions ?

AFAIK, the DRI kernel stuff allows one to share a gfx card (using low-level
access methods) in a lockup-safe fashion among several apps, including the X
server.
But I’m not informed what the actual status of the DRI APIs looks like.

Anyway, thank you again guys for all your infos.

cheers,
Benno.On Saturday 07 April 2001 21:00, you wrote:

On Saturday 07 April 2001 04:09, you wrote:

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

So meanwhile I will use standard APIs (SDL & co),and I’m looking forward to
support for hw-accelerated surfaces in windowed mode within SDL.

Sam, is this planned for the upcoming versions ?

Yes, but for the reasons I mentioned you won’t be able to directly
touch the hardware, just cache surfaces in video memory and have the
X server act on your behalf.

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

Hi there, Benno! :-)On Saturday 07 April 2001 11:37, Benno Senoner wrote:

On Saturday 07 April 2001 04:09, you wrote:

It is quite possible to write directly to video memory; that’s what the
XFree86 DGA extension is used for. You need write access to /dev/mem to
do this, however.

Yes, as said I can run the app as root, this is not a problem.

The problem is to figure out the startaddress (offsets between row etc),
pixel layout etc etc. (eg if it is RGB, 8bit colormapped etc)

Can SDL used for this stuff, or do I need some XFree calls ?

I’d would be grateful for any hint in that direction

Depending on what you want to do, getting direct access to VRAM may not
improve things; it may even slow things down. The problem is that modern
video cards aren’t really designed for direct access, but for accelerated
rendering. This becomes very noticable when you try to do full frame rate
animation at high resolution on Linux; the bandwidth just doesn’t cut it, as
long as the CPU does the blitting. (And retrace sync is missing as well, but
that’s another story…)

The only way I know of to get real performance on Linux with current driver
archs is to use a 3D API (OpenGL) and rely on hardware 3D acceleration (Mesa,
Utah-GLX or DRI.) I can see basically 3 ways of doing what you want, provided
I’m understanding your problem (lower numbers are generally faster):

  1. Draw the display using OpenGL “primitives”, with or without textures.

  2. Render in software (perhaps using SDL) into a “small” texture, and then
    render that to screen using OpenGL, scaling it up to whatever size you
    need, depending on the screen resolution. (You can do this with
    interpolation without significant slowdown on most cards these days, if
    you prefer a blurred image to big simple [aliazing distorted] scaling.)

  3. Do 2, but in 1:1 texture:screen resolution; ie full size. Why not
    render directly to VRAM then? Well, software rendering to system RAM is
    very fast in relation to doing it to VRAM, while at least some OpenGL
    drivers seem to be capable of using busmaster DMA - which is also very
    fast on modern cards (AGP in particular), and doesn’t burn CPU cycles.
    The net result is that you can use the full busmaster DMA bandwidth
    without messing with direct VRAM access. (And you get blending,
    transformations and stuff as an extra bonus, BTW. :slight_smile:

Now, I have yet to see some proof of the busmaster DMA actually being used
for texture uploading on a Linux driver… It seems like the code is there
(and BM DMA is used for fetching commands as well), but I’ve failed to get
anywhere near the expected performance so far. Might be me, or the driver -
haven’t tried very hard yet.

Anyway, it’s important to use the exact right format as most cards don’t do
pixel format conversions on the fly when downloading. (Or at least, the
drivers don’t seem to support it.) If you don’t, the driver will silently
chew away quite a few CPU cycles converting your texture before downloading
it. (Could still be faster than direct VRAM access though, at least with a
carefully optimized driver on a fast CPU.)

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -’