*`vk_common` - for common vulkan functions that are not device specific, like `vkFlushMemoryRanges`
* Some game-specific names, such as `metro_exodus`. We'll come back to those.
Seeing this makes you think, "wait a second, what are some instance-level functions doing there?" Let's explain:
1. You call a Vulkan function, say - `vkCreateInstance`.
2. The Vulkan loader is actually what gets called, and it sends your call down the chain (through any layers, if needed) and eventually to the ICD.
3. The ICD then does what it needs to do (See: https://github.com/Mesa3D/mesa/blob/main/src/vulkan/runtime/vk_instance.c#L44)
4. Whatever that is returned is passed up
But, I chose `vkCreateInstance` for a very good reason. This happens _way_ before you even get to creating devices, so how
does it choose which driver to go with if it could be any of them? Well this is what is called the "loader terminator", and it
specifically deals with this issue. TODO WRITE
Now let's move out of Vulkan and into Mesa, arguably the most important piece of the Linux graphics puzzle.
## Mesa
Mesa is the piece of software that is probably doing most of the heavy lifting here, and is an important piece of the
Linux graphics stack. Taken from their website:
```
Open source implementations of OpenGL, OpenGL ES, Vulkan, OpenCL, and more!
```
Oh... alright - well that doesn't really explain much. Let's look at their documentation which explains it better:
```shell
The Mesa project began as an open-source implementation of the OpenGL specification - a system for rendering interactive 3D graphics.
Over the years the project has grown to implement more graphics APIs, including OpenGL ES, OpenCL, OpenMAX, VDPAU, VA-API, XvMC, Vulkan and EGL.
A variety of device drivers allows the Mesa libraries to be used in many different environments ranging from software emulation to complete hardware acceleration for modern GPUs.
Mesa ties into several other open-source projects: the Direct Rendering Infrastructure, X.org, and Wayland to provide OpenGL support on Linux, FreeBSD, and other operating systems.
```
What's important to note that Mesa runs in _userspace_. As it says in the last line, it interfaces with kernel APIs such as
DRM (not to be confused with _digital rights management_, and don't worry the acronyms get more confusing).
Modern graphics drivers (and this applies to most desktop operating systems such as Windows and macOS) are split into two parts:
the kernel-space driver that interfaces directly with the hardware, and there is typically one per vendor, and then the userspace driver
that sits on top of the aforementioned layer and interacts with applications. The AMDGPU kernel driver is what we'll be covering today,
and the Mesa stack is our userspace driver. Specifically, we care about RADV.
* RADV is their AMD driver _for_ Vulkan. It has nothing to do with OpenGL support for this hardware (unless you use Zink ;-))
* ANV is their Intel driver _for_ Vulkan. As usual, it has nothing to do with OpenGL support for Intel chipsets.
* And so on...
Mesa not interfaces with the kernel DRI, which in turn calls a bunch of AMD-specific stuff
(in the case of RADV) in order to accomplish its goals. Again, this happens in _userspace_ as the kernel
driver only exists to facilitate I/O, load firmware and other really low level things.
So _what_ is DRI? Well, it's actually comprised of DRM and DRM KMS. So wait, how does this fit into OpenGL again? Well there's
another term, "Gallium".
### Gallium
This is so incredibly fascinating because for the longest time - whenever I think of "Gallium", I think
of "Gallium Nine" which is the way to run DX9 almost "natively" on your system via Mesa. But this has nothing to do with
that really, and is just another frontend to this system.
It's just a framework to reduce the amount of driver code needed to write OpenGL-compliant drivers. Unlike the
Vulkan device drivers, these drivers are located under `src/gallium/drivers`.
* radeonsi - RADV equivalent for OpenGL
* iris - Intel OpenGL support
* etc
### Overview
So after going through all of that, I think we should take a step back and see what we have learned here.
1. You call `vkCreateInstance`
2. The Vulkan loader terminator eventually calls `vk_common_create_instance`, which exists in the Radeon ICD,
or the `/usr/lib/libvulkan_radeon.so` shared object. This object contains instance-level functions as well as device-specific
functions.
3. If the function is device-specific, the Mesa Vulkan driver knows how to interface with the driver
using the kernel APIs (DRI, which includes DRM KMS for modesetting and DRM for device interfacing).
4. The I/O leaves userspace (Mesa) and heads into the kernel (AMDGPU).
5. ...
6. The I/O returns to Mesa, and goes back up to the Vulkan Loader.
7. If needed (for example, enumerating all physical devices) the Vulkan loader terminator will combine the sources from multiple ICDs, otherwise
just leaves it alone.
Wow, that is a lot! And we learned with our brief OpenGL tangent that the process is very similar with Gallium. Now
of course this is nice and all, but what about WSI, swapchains and how those interact with Wayland?
Before we get into that, I would like to revisit the DRM subsystem in the kernel before we head back up to the userspace
portion.
## DRM
To remind us, DRM stands for the "Device Rendering Manager" and handles device-specific graphics guff. For us, the part
that's important is that it exposes device-specific APIs to control them from userspace. To find it in the kernel tree, see
`drivers/gpu/drm`.
Since we are just worrying about AMD gpus, you can see the AMD-specific bits in `drivers/gpu/drm/amd/amdgpu`. Inside
you'll see a metric ton of source and header files related to the amdgpu drm interface. Sweet!
Now I'm curious about how does Mesa then interface with these device-specific stuff? Does it just include the relevant kernel header? Let's find out...
If we take a look at the Mesa RADV `meson.build` we get our answer:
`dependencies : [ ..., dep_libdrm_amdgpu, ... ]`
Jackpot! This `dep_libdrm_amdgpu` bit is specifically referring to the libdrm project under the Mesa umbrella (https://gitlab.freedesktop.org/mesa/drm)
To get more confusing, this is _not_ DRM (the Linux subsystem) but rather a library sitting on top of it. Yes, really. To get even more