D3D12: device creation¶
What it does¶
D3D12 has no feature-enable step at device creation: once the device exists, everything
the GPU supports is available. A few operations must still happen before
D3D12CreateDevice, such as calling D3D12EnableExperimentalFeatures for preview shader
models. For those, the D3D12 backend provides RI::D3D12::createDevice alongside
RI::createDevice. It accepts a
NativeDeviceAdditions struct with a beforeDeviceCreation callback that RI invokes just
before creating the device. All of these declarations live in
<RI/D3D12/Native.hpp>.
Step by step:
- Write a callback with the signature
void hook(IDXGIAdapter1 *adapter, void *user). - Pass it in
NativeDeviceAdditions::beforeDeviceCreation, and pass any context pointer it needs inNativeDeviceAdditions::user. - Call
RI::D3D12::createDevice(desc, additions): RI selects theIDXGIAdapter1fordesc.gpu, callsbeforeDeviceCreation(adapter, user)if non-null, and then callsD3D12CreateDevice.
Signature¶
// <RI/D3D12/Native.hpp>, namespace RI::D3D12
struct NativeDeviceAdditions {
void (*beforeDeviceCreation)(IDXGIAdapter1 *adapter, void *user) = nullptr;
void *user = nullptr;
};
From the header: Native additions at device creation (P60 (3)). D3D12 has no feature enable step, so the addition is a hook that runs just before D3D12CreateDevice, e.g. to call D3D12EnableExperimentalFeatures. listGpus also creates short-lived devices; the hook does not run for those.
| Field | Type | Default | Description |
|---|---|---|---|
beforeDeviceCreation |
void (*)(IDXGIAdapter1 *adapter, void *user) |
nullptr |
|
user |
void * |
nullptr |
// <RI/D3D12/Native.hpp>, namespace RI::D3D12
Device createDevice(const DeviceDesc &desc, const NativeDeviceAdditions &additions);
Example¶
// Runs on the adapter RI picked, just before RI calls D3D12CreateDevice.
void enableExperimentalShaderModels(IDXGIAdapter1 * /*adapter*/, void *user) {
UUID features[] = {D3D12ExperimentalShaderModels};
const HRESULT result = D3D12EnableExperimentalFeatures(1, features, nullptr, nullptr);
*static_cast<bool *>(user) = SUCCEEDED(result); // fails without Windows developer mode
}
bool experimental = false;
RI::Device device = RI::D3D12::createDevice(
{.gpu = gpus[0]}, {.beforeDeviceCreation = &enableExperimentalShaderModels, .user = &experimental});
The same in native code¶
Vulkan needs no hook: what must be decided before creation is the feature chain and the extension list, which RI exposes directly. See Vulkan: device creation.
// Optional. Experimental features (preview shader models, ...) are switched
// on for the whole process, before D3D12CreateDevice. Fails without
// Windows developer mode.
constexpr bool wantExperimentalShaderModels = false;
if (wantExperimentalShaderModels) {
UUID experimental[] = {D3D12ExperimentalShaderModels};
if (FAILED(D3D12EnableExperimentalFeatures(1, experimental, nullptr, nullptr))) {
std::printf("experimental shader models unavailable\n");
}
}
What to watch for: experimental features apply to the whole process and need Windows developer mode. Enable them before any device is created on the adapter.
The Agility SDK exports¶
A D3D12 executable must export the Agility SDK version and path, or Windows loads its own,
older D3D12 runtime. Exporting those symbols from a library has no effect. Include
<D3D12AgilityExports.hpp> and invoke EQUINE_D3D12_AGILITY_SDK_EXPORTS() at namespace
scope in exactly one .cpp file of each D3D12 executable:
// Once per executable, in one .cpp file (P55). CMake: equine_deploy_agility_sdk(<target>).
EQUINE_D3D12_AGILITY_SDK_EXPORTS()
In CMake, call equine_deploy_agility_sdk(<target>) to copy the Agility SDK runtime
(D3D12Core.dll and d3d12SDKLayers.dll) into a D3D12/ folder next to the executable.
In plain D3D12, the same two exports look like this:
// The Agility SDK: D3D12 loads the newer runtime shipped next to your program
// (in .\D3D12\) only if the *executable* exports these two symbols.
extern "C" {
__declspec(dllexport) extern const UINT D3D12SDKVersion = D3D12_SDK_VERSION;
__declspec(dllexport) extern const char *D3D12SDKPath = ".\\D3D12\\";
}
Rules¶
- The
beforeDeviceCreationcallback runs only inRI::D3D12::createDevice.RI::listGpusalso creates short-lived devices to check each adapter, and the callback does not run for those. - In validation builds (
EQUINE_VALIDATION), the callback runs after RI enables the D3D12 debug layer, on theIDXGIAdapter1that matchesdesc.gpu. - D3D12 returns the same
ID3D12Devicefor every device created on one adapter. Two RI devices on one GPU share thatID3D12Device, each holding a reference, so a feature enabled by one callback affects both.
Native handles¶
// <RI/D3D12/Native.hpp>, namespace RI::D3D12
struct NativeDevice {
IDXGIFactory6 *factory = nullptr;
IDXGIAdapter1 *adapter = nullptr;
ID3D12Device *device = nullptr;
ID3D12CommandQueue *graphicsQueue = nullptr; // a DIRECT queue
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_12_0;
};
From the header: Borrowed pointers: RI keeps its references; AddRef to keep one longer.
| Field | Type | Default | Description |
|---|---|---|---|
factory |
IDXGIFactory6 * |
nullptr |
|
adapter |
IDXGIAdapter1 * |
nullptr |
|
device |
ID3D12Device * |
nullptr |
|
graphicsQueue |
ID3D12CommandQueue * |
nullptr |
a DIRECT queue |
featureLevel |
D3D_FEATURE_LEVEL |
D3D_FEATURE_LEVEL_12_0 |
// <RI/D3D12/Native.hpp>, namespace RI::D3D12
NativeDevice native(const Device &device);
ID3D12Heap *native(const Memory &memory);
ID3D12Resource *native(const Buffer &buffer);
ID3D12GraphicsCommandList7 *native(const CommandList &cmd);
NativeTexture native(const Texture &texture);
ID3D12QueryHeap *native(const QueryPool &pool);
NativeDescriptorHeap native(const DescriptorHeap &heap);
NativePipeline native(const Pipeline &pipeline);
From the header: Native objects behind RI objects (P84). Borrowed pointers.
// Borrowed pointers: RI keeps its references. AddRef to keep one longer.
const RI::D3D12::NativeDevice n = RI::D3D12::native(device);
D3D12_FEATURE_DATA_D3D12_OPTIONS7 options7{};
n.device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS7, &options7, sizeof(options7));
std::printf("mesh shaders: %s\n", options7.MeshShaderTier != D3D12_MESH_SHADER_TIER_NOT_SUPPORTED ? "yes" : "no");
The returned pointers are borrowed: RI keeps its references and releases them in
RI::destroyDevice. Call AddRef if you need a pointer to outlive the device.