r/GraphicsProgramming • • 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

29 comments sorted by

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.

1

u/TheNextJohnCarmack Dec 20 '21

Well when I said "x64 instructions for GPU stuff" I meant x64 instructions for sending stuff on the bus to the GPU. Do you have any idea where would be a good place to start? Scale is irrelevant, this is sort of a magnum opus project I fully expect to die before I properly complete.

8

u/smiler82 Dec 20 '21

I suggest getting your feet wet and just start writing the OS. Later you can add basic graphics (VGA) support. You will get an understanding of what is required (and the humongous scope of the work) during the journey. Even if you decide to give up on the way, writing an OS is in my opinion a rewarding experience.
https://wiki.osdev.org/Main_Page have a lot of good resources for hobbyist OS endeavors.

1

u/TheNextJohnCarmack Dec 20 '21

Hm, okay. It's unlikely I'll reach the scale required to get a full-blown realistic 3D engine working in a reasonable amount of time anyway

1

u/the_Demongod Dec 20 '21

The problem is that, to my knowledge, the graphics driver doesn't expose any bare metal API that you can use directly. The driver implements OpenGL, Vulkan, DirectX, etc. and you use that. It looks like you can get closer to Nvidia GPUs with NVAPI but this is still fairly abstract and as it mentions on the page, half the interesting features require you to sign an NDA. Based on that I'm going to go ahead and say that it's effectively impossible to do any direct communication with a modern GPU. As the other guys said, you'd be better off mucking around in OS dev or some other thing that will scratch this itch. GPUs are way too complicated to reasonably reverse engineer yourself. If you want to do something fun, I would suggest building your own GPU in an FPGA or in a hardware simulator and writing your own graphics driver.

0

u/TheNextJohnCarmack Dec 20 '21

I just don't understand how this is possible. This sounds like a hacker (FOSS enthusiast)'s nightmare if it is true. How is it possible that vendors have made a completely impenetrable software lock on their entire GPU? Surely there must be code that a CPU executes that somehow results in a response in the GPU directly? I mean, how else is it possible for the CPU and GPU to interact?

I am not strictly talking about human-readable APIs either. I'm talking any software that allows direct communication between CPU and GPU at a sub-operating system level. I am willing to painstakingly integrate pure binary into my OS from drivers.

3

u/the_Demongod Dec 20 '21

Obviously nothing is completely impenetrable, I'm sure Nvidia and AMD have teams of experts trying to reverse engineer each others' ISAs. It's just probably outside the capabilities of one person. Modern hardware is definitely a FOSS enthusiast's worst nightmare, it's absurdly complicated and the fine details of the architecture represent the company's entire value on the market. That's why if you work in e.g. an Intel fab, it's incredibly silo'd and you're not allowed to know anything beyond the immediate knowledge required to do your job. In principle you can sniff the packets being sent to the GPU, but I don't see how you could manage to make the GPU do anything useful with the information you'd be able to glean from that.

I don't know why you'd want to do this anyways, if you want to learn more about GPUs you should just build your own. I don't imagine you'd be able to eke any real capability out of a full-size GPU from the information you'd garner by sniffing binary on the PCIe bus, so by building your own you'd learn a whole lot more and wouldn't be sacrificing much hardware capability since you're not going to be able to make a real GPU do anything interesting this way anyways.

-9

u/TheNextJohnCarmack Dec 20 '21

That sucks. Fuck AMD and fuck NVIDIA, and most of all fuck their hardware. And fuck their software too. Sounds like it'll be easier to develop an entire computer entirely from scratch than to develop just a fucking driver.

Well anyway, thanks for the help. I appreciate it.

3

u/IQueryVisiC Dec 20 '21

There are FOSS drivers for Linux. AMD always opened their spec and NVIDIA has been reverse engineered at least the old models in my old notebooks which cannot run the new Windows.

At least Vulkan should be possible. I still believe that the drivers mostly cater to some games and guess what they want. With Vulkan you are much closer to the metal.

May I invite you to write an OS for r/AtariJaguar . They are completely open about the Spec. No NDA like on Japanese stuff. Also Intel integrated graphics is completely open I think. And it is quite good in its later iterations.

1

u/moon-chilled Dec 21 '21

where would be a good place to start?

PCI.

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.

https://nvidia.custhelp.com/app/answers/detail/a_id/1849/~/why-arent-the-nvidia-linux-drivers-open-source

But there is a reverse engineered open source alternative:

https://nouveau.freedesktop.org/

-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

u/TheNextJohnCarmack Dec 20 '21

but I guess I'll give it a shot.

1

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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

u/TheNextJohnCarmack Jan 04 '22

The goal is to make an OS best suited for me, not the “best”.

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

u/[deleted] Dec 20 '21

[deleted]

0

u/TheNextJohnCarmack Dec 20 '21

I outright hate Microcock and I heavily distrust Sony.

0

u/[deleted] Dec 20 '21

[deleted]

1

u/TheNextJohnCarmack Dec 20 '21

I am targeting exactly one GPU microarchitecture. Precisely one (mine).

0

u/[deleted] Dec 20 '21

[deleted]