r/GraphicsProgramming • u/TheNextJohnCarmack • Dec 20 '21
Question x64 instructions for GPU stuff?
I am thinking about creating an OS, just for myself, but I wanna do 3D games on it. Can I just write my own driver for one specific GPU (nevermind the logistics, this is a hobby project. I just want to know if it is POSSIBLE), and how would I do that?
3
Upvotes
3
u/Hazematman Dec 20 '21
Do you have an idea of what kind of GPU you want to support? If you're able to get your hands on an older GPU it will probably be easier to support. Especially one from pre-programmable shaders era. That way you won't have to build your own shader programs from Isa.
I would look for "programmer reference manual" for the GPU you want to support. Unfortunately AMD doesn't publish these anymore for their newer GPUs, so you would have have to read the Linux driver source code to get an idea of how to control the GPU. I believe Intel still has programmer reference manuals for their GPUs. I wouldn't bother with Nvidia GPUs as they don't really have public facing documentation besides the reverse engineering effort in nouveau.
A high level overview of what you'll need to do from the driver side. Assuming you're using an external GPU over PCIe, you need to enumerate the PCIe bus and and get the BAR registers of the GPU you want to talk to. One of those BAR regions will be a MMIO map of the GPUs registers. You'll want to map that into your OS / application, and this will be the main way you'll interact with the GPU. As others have mentioned here, for many modern GPUs you'll need some microcode to get it to work properly. This is a binary blob you'll have to load on the GPU to get it to work. If you use an older gpu, (like 2002 or older) there's a good chance it won't require any microcode to work properly.
I would try to familiarize yourself with ring buffers and that is the way most GPUs are controlled. You'll end up created data packets in a ring buffer either in GPU accessible CPU ram or in VRAM. These packets do things like update the GPUs registers, load shaders, copy surfaces, perform DMA, and trigger drawing operations. you'll have to become familiar with the packet format of the GPU you're working with and write code to manage a ring buffer sending commands to the GPU.
If you have any more specific questions I would be happy to try and answer them.