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?
2
u/deeper-blue Dec 20 '21
Modern GPUs are complex beasts. I would start one level lower. Try to get ahold of an old 3dfx Voodoo card. Understand how it works: https://fabiensanglard.net/3dfx_sst1/ and then look at how existing glide drivers talk to it. Then you can try to directly send the required commands over PCI to draw you first triangle.
2
u/TheNextJohnCarmack Dec 20 '21
Thanks. I think I'll go even lower than that and try Ben Eater's self-proclaimed "worst graphics card" to start
1
u/deeper-blue Dec 20 '21
Good plan. Alternatively look into Mister FPGA. FPGAs can be a good way to learn more about hardware and especially how to write hardware software interop stuff.
4
u/Andrispowq Dec 20 '21
If you have an Intel processor you might wanna start with an integrated GPU, since they have good documentation and they're MUCH easier than nVidia or AMD cards to program. To start, write some code to get the Root System Descriptor Table from the BIOS or UEFI, and find the contents of the PCI bus or the PCI Express bus (might need both). From there, you can access some of the variables or the GPU and start working with it, but it is definitely a huge task
1
u/Flannelot Dec 20 '21
You'd probably need to start with an open source driver to have any chance of success. Just learning to use a 3D engine is a long project, let alone writing your own.
NVidia still dont release the source code for 3D drivers.
But there is a reverse engineered open source alternative:
-2
u/TheNextJohnCarmack Dec 20 '21
Except aren't these built for Linux? I was hoping for something closer to the metal.
1
u/specialpatrol Dec 20 '21
A driver is designed to take specific kind of hardware and interface with a specific OS. There is no lower level. You would take the code here and change it's interface to work with whatever interface you come up with for your OS.
-1
1
Jan 03 '22
Its possible, but doing so would be astronomically difficult. If you wanna ditch windows, linux is your best bet.
1
u/TheNextJohnCarmack Jan 03 '22
I wanna ditch linux too
1
Jan 03 '22
If even FOSS isnt up to your expectations somehow, your screwed no matter what you do.
1
u/TheNextJohnCarmack Jan 03 '22
I’d just like every single line of code to be at my exact specifications, because even Linux has slight annoyances based on design decisions I might not have agreed with, and I also would like to completely understand at every level exactly how everything I use works.
1
Jan 04 '22
Programs are very hard to make, and shortcuts are made due to this. Though dumb design decisions are completely the designer's fault.
If you can somehow make something better than the currently available operating systems, I will definitely be another user if it happens to be publicly available, with support, However it is, in general, insanely hard to do, as there is a LOT that goes into an operating system with lots of people working on it.
Personally, if it works and the errors never end up getting in the way, i just live with them, and end up focusing on creating something new. However if its broken, it needs fixing.
1
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.
0
Dec 20 '21
[deleted]
0
u/TheNextJohnCarmack Dec 20 '21
I outright hate Microcock and I heavily distrust Sony.
0
Dec 20 '21
[deleted]
1
u/TheNextJohnCarmack Dec 20 '21
I am targeting exactly one GPU microarchitecture. Precisely one (mine).
0
13
u/Mason-B Dec 20 '21
Is it possible? Yes, in theory... with infinite time and resources. Is it likely or easy? Absolutely not. Even now the two major GPU suppliers (NVidia and AMD) require loading of proprietary microcode to perform 3d acceleration and function correctly. There are people who try to reverse engineer this stuff from scratch, and I think they've made some progress. But the reality is that it's very very hard to reverse engineer a chipset and assembly from scratch. I say this as someone who shoots the shit with multiple people who reverse engineer stuff for a day job. Even they have a starting point, a manual or documented prior art. For most parts of what you would want to do, that would not exist.
And to be clear, the GPU is a separate chipset, with it's own instruction set, hence "x64 instructions for GPU stuff" is a nonsensical statement, as x64 describes the CPU chipset.