r/C_Programming • • Jul 03 '26
From binary to pixels: How does the OS handle graphics at the kernel level?
I’m a beginner in low-level programming, and every time I boot up my computer, I find myself wondering: how did we go from simple 0s and 1s (electrical signals) to the complex graphical interfaces we use today?
I’m trying to understand the process from the very bottom. How does an OS actually handle rendering graphics at the kernel level?
If there is anyone who can explain how colors are rendered using C or x86 Assembly—how it works from the foundation—I would love to learn. Specifically, how does the hardware handle this at such a low level, and is the RGB model the standard way this is managed in hardware?
Any insight, resources, or explanations would be greatly appreciated,
Thanks in advance!.
29
u/litescript Jul 03 '26
hoo, boy, honestly if you are interested in the real nitty gritty stuff you’d be best served looking up the Ben Eater 6502 series on youtube. he does a fantastic job explaining how a computer works from electricity to clock cycles and he even builds it all on breadboards to see how the wiring works. it’s very thorough, and really well done.
39
u/Whorehammer Jul 03 '26
Watch Ben Eater's videos on YouTube about his World's Worst Video Card. It is a great introduction to the topic.
5
u/Rude-Professor-2485 Jul 03 '26 edited Jul 03 '26
In modern systems you need to communicate with the GPU. Generally, what is done is usong native APIs, such as OpenGl, Metal etc.. With that, the API communicates with the GPU s driver by translating the passed shaders on the API. You need to see how you can control the GPU driver. Or, to create a GPU driver or generating GPU' s machine code (really closed environment)
If you want to know how a GPU is made, you can delve in Patterson and Hennessy 's Quantitive Approach where they talk about GPU s and NPUs on paralelism topics
6
u/tellingyouhowitreall Jul 03 '26
Ooooo, buckle up kiddo.... we goin' on a ride.
First, a little history, because there are some incorrect things that have been said in this thread which we need to correct.
Back in the days of CRTs the screen worked by scanning an electron tube across a perforated phosphor plate in lines (hence the term scan line). When monitors, and TVs, moved from monochrome to color this was done by using three plates and scanning the screen three times for each frame, offset for each color. Since each color was scanned individually these are called color planes. EGA, CGA, and eventually VGA, used individual color planes in the system memory buffer and hardware memory map, and a digital-analog converter scanned through the graphics memory in sequence and presented each color plane to the monitor at a specific rate. The monitor would send a hardware interrupt to the graphics system (and possibly the CPU) during a gap between frames called the vertical refresh; and possibly between each scan line (the horizontal refresh).
Early hardware was basically the wild west, everyone had to program for specific hardware and specific interfaces. Early graphical acceleration in the raster era used hardware called a blitter which could accelerate block transfers with built in binary functions to mask in and out certain values. For instance to only write certain pixels in a block you could first mask those pixels with a pixel mask to white, then perform a second write that only wrote to the white pixels. These were functions supported by the blitter hardware, and how we got a lot of raster graphical effects. There are artifacts of modern graphics systems that date back to these blitter days (sp. hardware based alpha blending).
VGA based programming was incredibly complicated, and thankfully, isn't used anymore except by hobbyists and masochists.
Around the PS/2 (AT) days, along came a VESA. VESA is a graphical standards body (that still exists!) and standardized graphics interfaces for the hardware/OS boundary. Important things that VESA brought us were a standard BIOS interface for interacting with the underlying graphics hardware, discovering various output modes, standardized gamma, and other nice things, and SVGA. SVGA was originally designed as a standard model for 800 x 600 x 256 color graphics, using a single palatalized plane, with methods for 16 bit processors to map smaller regions of video memory and flip through them to render to different parts of video memory (this was very slow though). The development of 32 bit processors allowed the BIOS, and the OS, to map the entire region of graphics memory into the address space at once.
VESA 2.0 / 3.0 and the SVGA model are still what OSes use as the vendor neutral hardware interface for interacting with the graphics system.
Because the VESA interface is BIOS oriented, significant portions of it can only be accessed in real mode. At boot the BIOS talks to the hardware layer to get a set of video modes supported by the graphics card (an important note here is that not all modes supported by the graphics card are supported by the monitor). The BIOS interface in real mode is always accessed via interrupts, in this case INT 10h, with specific functions selected by setting the AX register (or EAX) to a specific value, and passing arguments in the other registers. The system uses these functions to get a pointer to a list of video mode IDs that it can scan, and then query each ID for the information it provides. Quick ref documentation @ https://www.ctyme.com/intr/int-10.htm (the functions we care about are AX = 4F00h through 4F05h. The code for interacting with the VESA subystem is actually pretty straight forward, and I'll put it in a reply to this comment. The real-mode requirement also means that OSes, even today, keep a real-mode subsystem around to help with changing graphics modes.
[ARM and mobile OSes work a little differently]
These software based graphics modes can support a number of underlying pixel formats, from palatalized 256 color, to 16 bit rgb (usually 5:6:5), to rgb 24 and rgb 32, to YUV. The pixel format is not standardized. However each video mode provides a set of conversion fields for pixel formats. For the RGB formats each field represents the bit position of the field and the bit width of the field. For instance on newer hardware the pixel format for 24 and 32 bit color depths is generally [blue-byte] [green-byte] [red-byte] ["reserved" byte], and the red field is specified as having a width of 8 and position of 16. Note that in memory this is a BGRA format. The actual masks for each color field can be built from the width and position, for instance to access the red color fields:
mask = ((1 << width) - 1) << position. Color conversion from the OS's 'native' color format to the underlying graphical format is then:(((color & ((1 << nativeWidth) << nativePosition)) >> nativePosition) << outputPosition) & (((1 << outputWidth) - 1) << outputPosition). Intentionally left unfactored--you can figure out how to reduce the number of shifts there if you want (you only need two).On modern hardware each graphics mode supports an LBA mode (linear buffer address). What is generally referred to as the frame buffer. It's a linear address for the memory mapped graphics buffer. There are some important details about 'linear addresses' and effective (mapped) addresses when the CPU is in protected mode or long mode, but it boils down the linear address being the real physical address in the address space. The most important detail will be that this can be remapped by the OS using paging, which we'll come back to. The LBA is a linear block mapping the entire output range to a single memory range.
For reasons of alignment, speed, and interacting with the hardware, the LBA is divided into scan lines. The scan lines are then divided by pixels. Each scan line in the buffer may be longer than the simple pixelSize * width calculation gives, the actual memory size of a scan line is its stride. And so when rendering into the memory buffer the graphics software needs to render each scan line independently of every other. Block transfers, the basic bread and butter of software rendering, for 24 and 32 bit modes then looks something like:
2
u/Ok_Chemistry_6387 Jul 03 '26
It doesn't. The days of a frame buffer are long behind us.
I would recommend looking at nand to tetris for a good introduction of the basics of this. However modern GPUs are very different from days of old. You can get a feel for how old systems handled graphics by using something like html canvas.
2
u/GenericFoodService Jul 05 '26
This varies wildly based on what OS and what hardware we are talking about.
On most modern operating systems, the conceptual model everything is using under the hood is "textures all the way down". Windows are textures that you ask a runtime library to manipulate, the desktop is a texture made by compositing the textures of all of your windows and window decorations and shells together, and those textures are serialized and given to dedicated hardware like a display adapter who knows how to drive your TV or Monitor.
Everything is just buffers, big serialized lists of numbers in the right order, moved into the right places. Sometimes that's by sending commands over a bus and asking another piece of hardware to construct the answer (like a graphics card), other times it's by literally sending the formatted data.
2
u/arihoenig Jul 03 '26
Graphics don't have to be done at the kernel level, and arguably shouldn't be done at the kernel level. In microkernel operating systems like QNX, the graphics drivers are user mode processes.
Having the assumption that graphics have some connection to ring 0 will not help in developing your understanding.
1
u/CounterSilly3999 Jul 03 '26
Yes, RGB. Bitmaps in the bits per pixel chunks. In case of 24 bpp -- a two dimensional array of 3 byte pixels. Just it is for the displays. Printing machines use CMYK. Conversion routines use sophisticated color profiles, not brought to consistency even by Adobe tools.
At hardware level display adapters use DAC's -- convert each color value to analog voltage and use it for lighting the corresponding LED of the display.
If you are in Windows, you can investigate the BITMAP structure of the API.
1
u/SavingsSpecialist896 Jul 03 '26
another interesting resource is the Nesdev wiki https://www.nesdev.org/wiki/Nesdev_Wiki. The Nintendo entertainment system used a version of the 6502 processor, so the resources related to graphics there may be helpful
1
u/meancoot Jul 04 '26
At the very bottom you want to look up the
scanoutprocess. This is the process used by a computer to encode aframebufferinto the signal needed for the output device. In its most basic form it is just an asynchronous process that reads from a 2D bitmap of pixels and encodes the video signal from it. Like the screen resolution, the format of the pixels isn't necessarily fixed, and may be RGB with varying bit depths for general graphics, or YUV for video playback.1
u/timrprobocom Jul 05 '26
Typically, e kennel isn't very much involved with graphics. It's a user mode problem, because you don't want a simple presentation bug to take down the whole system.
0
u/[deleted] Jul 03 '26
[deleted] — view deleted comment
46
u/[deleted] Jul 03 '26
[removed] — view removed comment