Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions d3d12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ This is Leonida's Direct3D 12 runtime: it builds as `leonida.exe`, takes `LEONID
still work) and is launched by Leonida's `scripts/play-d3d12.ps1` on Windows and `scripts/play-linux.sh` on Linux
(with VKD3D-Proton bundled, `docs/linux.md`).

**Runtime shaders on Wine.** Put the matching x64 `dxcompiler.dll` and `dxil.dll` from one
[Microsoft DirectX Shader Compiler release](https://github.com/microsoft/DirectXShaderCompiler/releases)
next to the Windows executable actually launched (`leonida.exe`, or the file selected by `LEONIDA_RUNTIME`;
for Rust tests, next to their `.exe` in `target/.../deps/`). The Linux launcher installs VKD3D-Proton into
its prefix separately; do not put the DXC pair in the prefix's System32 directory. If the prefix overrides
either DLL to builtin-only, prefer the native pair (`WINEDLLOVERRIDES='dxcompiler,dxil=n'`).
Wine's built-in FXC can compile a shader without preserving its embedded root signature. When the app-local
DXC pair is missing or cannot load, the runtime warns once and uses FXC; affected passes may still fail.
Native Windows continues to use the system FXC and does not require either DLL.

It grew from the VIctory runtime: Marvisak/VIctory, through trustno1-coder/VIctory-NVIDIA (merged into Leonida at
`bafb0e25b`) and the forks credited below. Like them it is GPL-3.0-or-later (`LICENSE`). Leonida renamed it and adds
its own work on top; everything below this section is trustno1-coder/VIctory-NVIDIA's README as it came, with the
Expand Down
1 change: 1 addition & 0 deletions d3d12/crates/leonida-gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ workspace = true
windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Security", "Win32_System_Diagnostics_Debug", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Kernel", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Performance", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_Threading", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_WindowsAndMessaging"] }
windows = { workspace = true, features = [
"Win32_Graphics_Direct3D",
"Win32_Graphics_Direct3D_Dxc",
"Win32_Graphics_Direct3D_Fxc",
"Win32_Graphics_Direct3D12",
"Win32_Graphics_Dxgi",
Expand Down
97 changes: 97 additions & 0 deletions d3d12/crates/leonida-gpu/src/ambient_readback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//! An opt-in, synthetic hardware readback through the actual radiance and fog passes.

use crate::ambient::{self, Role, Views};
use crate::host::{host, transition};
use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R11G11B10_FLOAT, DXGI_SAMPLE_DESC};
use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory2, DXGI_ADAPTER_FLAG_SOFTWARE, IDXGIAdapter1, IDXGIFactory4};

// R11G11B10_FLOAT uses an exponent bias of 15 and 6/6/5 mantissa bits.
fn grey(exponent: u32, half: bool) -> u32 {
let r = exponent << 6 | u32::from(half) << 5;
let b = exponent << 5 | u32::from(half) << 4;
r | r << 11 | b << 22
}

#[test]
#[ignore = "run alone on hardware with LEONIDA_AMBIENT=1 and LEONIDA_AMBIENT_AIR=1"]
fn radiance_changes_the_fog_readback() {
for var in ["LEONIDA_AMBIENT", "LEONIDA_AMBIENT_AIR"] {
assert_eq!(std::env::var(var).as_deref(), Ok("1"), "{var} must be 1");
}
let host = host().expect("physical D3D12 device");
let dev = &host.device;
let run = host.new_list().expect("command list");
let list = &run.list;
// SAFETY: local D3D12 objects are kept through the synchronous submission. Copy locations
// borrow their resource; all rows start at aligned offsets and fit the staging allocations.
unsafe {
let dxgi: IDXGIFactory4 = CreateDXGIFactory2(Default::default()).unwrap();
let adapter: IDXGIAdapter1 = dxgi.EnumAdapterByLuid(dev.GetAdapterLuid()).unwrap();
let info = adapter.GetDesc1().unwrap();
assert_eq!(info.Flags & DXGI_ADAPTER_FLAG_SOFTWARE.0 as u32, 0, "software adapter");
let mut support = D3D12_FEATURE_DATA_FORMAT_SUPPORT { Format: DXGI_FORMAT_R11G11B10_FLOAT, ..Default::default() };
dev.CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, (&raw mut support).cast(), std::mem::size_of_val(&support) as u32).unwrap();
assert!(support.Support2.contains(D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD | D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE));
let heap: ID3D12DescriptorHeap = dev.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, NumDescriptors: 2,
Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, ..Default::default()
}).unwrap();
let step = dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
let (up, data) = host.upload_buffer(1024).unwrap();
let back = host.read_back_buffer(256).unwrap();
let tex = |dimension, width: u64, slot: usize| {
let desc = D3D12_RESOURCE_DESC {
Dimension: dimension, Width: width, Height: 1, DepthOrArraySize: 1, MipLevels: 1,
Format: DXGI_FORMAT_R11G11B10_FLOAT, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Flags: D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, ..Default::default()
};
let mut out: Option<ID3D12Resource> = None;
let mem = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() };
dev.CreateCommittedResource(&mem, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COPY_DEST, None, &mut out).unwrap();
let out = out.unwrap();
let mut cpu = heap.GetCPUDescriptorHandleForHeapStart();
cpu.ptr += slot * step as usize;
dev.CreateUnorderedAccessView(&out, None::<&ID3D12Resource>, None, cpu);
out
};
let image = tex(D3D12_RESOURCE_DIMENSION_TEXTURE2D, 2, 0);
let fog = tex(D3D12_RESOURCE_DIMENSION_TEXTURE3D, 4, 1);
let sub = |res: &ID3D12Resource| D3D12_TEXTURE_COPY_LOCATION {
pResource: std::mem::transmute_copy(res), Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 0 },
};
let row = |res: &ID3D12Resource, offset, width| D3D12_TEXTURE_COPY_LOCATION {
pResource: std::mem::transmute_copy(res), Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: D3D12_PLACED_SUBRESOURCE_FOOTPRINT {
Offset: offset, Footprint: D3D12_SUBRESOURCE_FOOTPRINT { Format: DXGI_FORMAT_R11G11B10_FLOAT,
Width: width, Height: 1, Depth: 1, RowPitch: 256 },
} },
};
let light = [grey(15, false), grey(16, true)]; // Half the cells see 1; the other half see 3.
let input = [grey(18, false), grey(14, false), grey(31, false), grey(15, false)]; // 8, 0.5, +inf, 1
data.cast::<u32>().copy_from_nonoverlapping(light.as_ptr(), light.len());
data.add(512).cast::<u32>().copy_from_nonoverlapping(input.as_ptr(), input.len());
list.CopyTextureRegion(&sub(&image), 0, 0, 0, &row(&up, 0, 2), None);
list.CopyTextureRegion(&sub(&fog), 0, 0, 0, &row(&up, 512, 4), None);
list.ResourceBarrier(&[transition(&image, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
transition(&fog, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)]);
list.SetDescriptorHeaps(&[Some(heap.clone())]);
for (slot, role, width) in [(0, Role::Radiance, 2), (1, Role::Air, 4)] {
let mut handle = heap.GetGPUDescriptorHandleForHeapStart();
handle.ptr += slot * u64::from(step);
assert!(ambient::after(&run, role, &Views { image: handle, depth: None, size: [width, 1], slices: 1 }), "{role:?} was not recorded");
}
list.ResourceBarrier(&[transition(&fog, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE)]);
list.CopyTextureRegion(&row(&back, 0, 4), 0, 0, 0, &sub(&fog), None);
run.close();
host.execute_sync_internal(&run);
dev.GetDeviceRemovedReason().unwrap();
let mut mapped = std::ptr::null_mut();
back.Map(0, Some(&D3D12_RANGE { Begin: 0, End: 16 }), Some(&mut mapped)).unwrap();
let got = mapped.cast::<[u32; 4]>().read();
back.Unmap(0, Some(&D3D12_RANGE { Begin: 0, End: 0 }));
assert_eq!(got, [grey(16, false), input[1], 0, input[3]], "radiance mean caps 8 at 2; fog clears infinity");
eprintln!("physical {:04x}:{:04x}, packed fog readback: {got:08x?}", info.VendorId, info.DeviceId);
}
}
Loading
Loading