How to write data directly into video memory?
Asked Answered
W

2

6

Is there any way for a programmer to write data directly into video memory? I know OS's are very strict about this, but then how some types of applications (like videos players or computer games) can write their data directly into videos memory.

I know there are a bunch of well-known libraries for this (e.g. OpenGL); but they are just normal libraries after all and there's no fundamental difference between them and the programs that me and you write.

Wolfram answered 20/2, 2016 at 8:5 Comment(2)
On some ARM-Boards you can directly access the framebuffer via the file /dev/fb0. Modern Linux systems with open source drivers give you access to the video memory via libdrm & co.Casabonne
There is one fundamental difference indeed: They are shipped with their own drivers / kernel modules.Religion
Z
2

I know there are a bunch of well-known libraries for this (e.g. OpenGL); but they are just normal libraries after all and there's no fundamental difference between them and the programs that me and you write.

Oh, they are quite different from "normal libraries".

Take OpenGL for example. Part of that is a DLL or SO that you load. But part of that is something called a "driver" or "graphics driver". That is not a "normal library" at all; it is fundamentally a part of the operating system. It's basically a part of the OS that someone other than the OS maker wrote.

The system defines the interface for a driver, and hardware makers provide a driver library that fits into that interface. But that's not something that a normal user is able to provide, nor do most games run by installing their own graphics driver.

The purpose of the driver is to provide a uniform interface to graphics hardware for the OS and clients of the OS. Different hardware is different, but each driver for an OS exports the same interface (more or less), which allows client code to not have to know the details of the hardware.

This also prevents client code from breaking the system. If you crash a GPU (which, since it's a processor, is a thing you can do nowadays), someone has to deal with that. The OS handles CPU faults, and the graphics driver/OS nowadays handles GPU faults too. But in order to do that, it has to stand between you and the hardware.

On most operating systems, directly and arbitrarily accessing video memory is not allowed. Not for any program which is not itself a graphics driver. Many low-level APIs like OpenGL allow you to map certain memory allocations, so that you can access them. But that's very different from just giving someone a GPU memory pointer; after all, there's no guarantee that the mapped pointer actually points to GPU memory.

Oh, and they don't let you map textures or framebuffers, since they want to keep the details of hardware texture formats implementation-defined.

Zoophilous answered 20/2, 2016 at 15:26 Comment(2)
I believe you should put more emphasis on the final part of the answer: the problem is not accessing the memory (any driver can do that), the problem is that the hardware interface is complex. There are many kind of buffer (shaders, textures, frame buffer, ...), a GART, video modes and last but not least the fact that "the display" is a shared resource, as soon as the OS need a refresh, it will overwrite the frame bufffer.Religion
Thank you for your mini-comprehensive response ;-) But let me know if I'm right to say "if a graphics card manufacturer wants to actually support OpenGL, it has to provide an OpenGL-compatible driver (one which runs OpenGL commands)". And one last thing: I still don't know what APIs (how many of them) are there with which programmers can do magical graphic works. I guess each individual OS has its own way of accomplishing that task, right? For example, as far as I know, Windows has DirectX. What about other OSs?Wolfram
T
0

If you directly try writing to video memory on most operating systems you will just get a segmentation fault because the kernel mode and user mode have been separated.

Theodor answered 12/11, 2023 at 14:39 Comment(3)
Some OSes give you ways to mmap video RAM into a user-space process, e.g. Linux framebuffer console allows that if you're running as root. e.g. this page (wiki.c2.com/?FrameBuffer) from 2004 describes it. And Problem Mapping Framebuffer Device Memory using C#/Mono on Raspberry PI has practical code that does it on an RPi running Linux.Floro
The X server on modern Linux desktops using also memory-maps a lot of video RAM, not just the framebuffer. Of course it has more kernel help for sending commands to the GPU if using DRI (direct rendering infrastructure), so in that case it's not just a case of user-space writing the framebuffer directly to update the screen.Floro
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Verrucose

© 2022 - 2024 — McMap. All rights reserved.