Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
656cfa1
d3d12: mesh programs translate as compute on a host without mesh shad…
qhukz13 Nov 28, 2026
dc5818b
d3d12: mesh pipelines draw through compute and a triangle list withou…
qhukz13 Nov 28, 2026
bc0c72f
d3d12: mesh fallback review fixes: no mesh signature for it, the sink…
qhukz13 Nov 29, 2026
1b33fa2
d3d12: drew()'s doc comment back on drew(), not on drew_mesh_as_compute
qhukz13 Nov 29, 2026
1b6b223
d3d12: MeshOut::new is crate-private, as SigElement it takes
qhukz13 Nov 29, 2026
924b294
d3d12: the mesh fallback's recording in one place, tested in one list…
qhukz13 Nov 29, 2026
d5e9ecf
d3d12: pixel shaders read barycentrics stand-ins on a host without ba…
qhukz13 Nov 28, 2026
38ccdcc
d3d12: pipelines put the barycentrics stage in on a host without bary…
qhukz13 Nov 28, 2026
5674b8a
d3d12: mesh_vs writes the barycentrics stand-ins for a mesh program r…
qhukz13 Nov 28, 2026
7e691b6
d3d12: barycentrics stage review fixes: the vertex shader's interpola…
qhukz13 Nov 29, 2026
8bd30f5
d3d12: rectangles with the barycentrics stand-ins get one geometry stage
qhukz13 Nov 29, 2026
66afbce
d3d12: triangles with adjacency draw through a barycentrics stage tha…
qhukz13 Nov 29, 2026
58c3333
Merge main into d3d12-mesh-compute
qhukz13 Nov 29, 2026
eab9c1e
d3d12: the test programs in emit::test_programs, built on every platform
qhukz13 Nov 29, 2026
ae1ecd2
Merge d3d12-mesh-compute (with main) into d3d12-bary-fallback
qhukz13 Nov 29, 2026
d5f666a
Merge branch 'mesh-fix' into bary-fix
qhukz13 Nov 29, 2026
74ac98c
d3d12: the mesh fallback's shaders take main's sample-mask fields
qhukz13 Nov 29, 2026
a190045
Merge branch 'mesh-fix' into bary-fix
qhukz13 Nov 29, 2026
1e13fef
d3d12: main's sample-mask twins in other formats are no adjacency var…
qhukz13 Nov 29, 2026
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
2 changes: 2 additions & 0 deletions d3d12/crates/leonida-gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ blake3.workspace = true

[dev-dependencies]
tempfile.workspace = true
# The host checks' device and shaders (`emit::host`) for the mesh fallback's test.
leonida-shader = { workspace = true, features = ["host"] }

[features]
# Debugging switches (frame capture, the host debug layer, per-draw data).
Expand Down
270 changes: 256 additions & 14 deletions d3d12/crates/leonida-gpu/src/draw.rs

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions d3d12/crates/leonida-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,45 @@ fn create() -> windows::core::Result<Host> {
let amd = crate::indirect::amd(&device);
leonida_shader::emit::set_host_rdna(amd && o1.WaveLaneCountMin == 32 && o1.WaveLaneCountMax == 64);
}
// `D3D12-BARYCENTRICS-FALLBACK`: without D3D12 barycentrics (Pascal and older refuse every pixel
// shader that reads them: "Shader uses Barycentrics, but the device does not support this") the
// translator reads the weights and the other vertices' values from inputs a geometry stage of ours
// writes (`leonida_shader::emit::bary_gs`). `LEONIDA_BARY_FALLBACK=1` takes that path on any card.
let mut o3 = D3D12_FEATURE_DATA_D3D12_OPTIONS3::default();
let barycentrics = device
.CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS3, (&raw mut o3).cast(), size_of_val(&o3) as u32)
.is_ok()
&& o3.BarycentricsSupported.as_bool();
let bary_forced = crate::switch::on("LEONIDA_BARY_FALLBACK");
leonida_shader::emit::set_host_barycentrics(barycentrics && !bary_forced);
tracing::info!(
barycentrics,
forced = bary_forced,
"pixel shaders read barycentrics through a geometry stage: {}",
!barycentrics || bary_forced
);
// `D3D12-MESH-COMPUTE-FALLBACK`: without mesh shaders at shader model 6.6 (Pascal and older) the
// translator writes mesh programs as compute, drawn through a vertex shader of ours
// (`pipeline::host::MeshCompute`). `LEONIDA_MESH_COMPUTE=1` takes that path on any card.
let mut o7 = D3D12_FEATURE_DATA_D3D12_OPTIONS7::default();
let mut model = D3D12_FEATURE_DATA_SHADER_MODEL {
HighestShaderModel: D3D_SHADER_MODEL_6_6,
};
let mesh = device
.CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS7, (&raw mut o7).cast(), size_of_val(&o7) as u32)
.is_ok()
&& device
.CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, (&raw mut model).cast(), size_of_val(&model) as u32)
.is_ok()
&& o7.MeshShaderTier != D3D12_MESH_SHADER_TIER_NOT_SUPPORTED
&& model.HighestShaderModel.0 >= D3D_SHADER_MODEL_6_6.0;
let forced = std::env::var_os("LEONIDA_MESH_COMPUTE").is_some_and(|v| v == "1");
// With the barycentrics stand-ins (above) a pixel shader behind a mesh program reads inputs that
// only `mesh_vs` writes: a mesh pipeline has no stage after the mesh program to write them, so mesh
// programs run as compute then too.
let stand_ins = !barycentrics || bary_forced;
leonida_shader::emit::set_host_mesh_shaders(mesh && !forced && !stand_ins);
tracing::info!(mesh, forced, stand_ins, "mesh programs run as compute: {}", !mesh || forced || stand_ins);
let messages = debug
.then(|| device.cast::<ID3D12InfoQueue>().ok())
.flatten();
Expand Down
2 changes: 2 additions & 0 deletions d3d12/crates/leonida-gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub mod descriptor;
pub mod device;
#[cfg(windows)]
mod draw;
#[cfg(windows)]
mod mesh_fallback;
#[cfg(any(windows, test))]
mod drops;
#[cfg(windows)]
Expand Down
149 changes: 149 additions & 0 deletions d3d12/crates/leonida-gpu/src/mesh_fallback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//! `D3D12-MESH-COMPUTE-FALLBACK` as it is recorded on a command list: the
//! mesh program as compute writing each group's outputs to the mesh buffer
//! (`leonida_shader::emit::MESH_OUT_BYTES`), then a triangle list drawn
//! from them through the pass-through vertex shader
//! (`leonida_shader::emit::mesh_vs`). The draw path binds the pipelines and
//! the title's root arguments (`crate::draw`); what is recorded between and
//! after them is here, so a test records the same commands on a device of
//! its own.
//!
//! The buffer starts and ends each run in the common state, so every list,
//! and every run in one list, finds it there.

use windows::Win32::Graphics::Direct3D::{D3D_PRIMITIVE_TOPOLOGY, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST};
use windows::Win32::Graphics::Direct3D12::{
D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
D3D12_WRITEBUFFERIMMEDIATE_PARAMETER, ID3D12CommandSignature, ID3D12GraphicsCommandList,
ID3D12GraphicsCommandList2, ID3D12Resource,
};
use windows::core::Interface;

use crate::host::transition;

/// One run of a mesh pipeline as compute and a triangle list.
pub(crate) struct MeshRun<'a> {
pub(crate) list: &'a ID3D12GraphicsCommandList,
/// The mesh buffer, in the common state.
pub(crate) buffer: &'a ID3D12Resource,
/// The compute pipeline's root UAV the groups write to.
pub(crate) out_param: u32,
/// The graphics pipeline's root SRV the vertex shader reads.
pub(crate) in_param: u32,
pub(crate) max_primitives: u32,
/// Instances from this one on would read the sink slot
/// (`MeshOut::groups_cap`).
pub(crate) groups_cap: u32,
/// The topology the list has again after the draw (the title's, for its
/// next draw); `None` leaves the triangle list.
pub(crate) restore: Option<D3D_PRIMITIVE_TOPOLOGY>,
}

impl MeshRun<'_> {
/// `x` groups, the compute pipeline and its root arguments bound:
/// dispatches them, then `graphics` binds the graphics pipeline and its
/// root arguments (false: nothing is drawn) and the groups are drawn,
/// one instance a group up to the cap. Whether it drew.
///
/// # Safety
/// An open list with the compute pipeline bound; live resources.
pub(crate) unsafe fn direct(&self, x: u32, graphics: impl FnOnce() -> bool) -> bool {
let n = x.min(65_535);
if n == 0 {
return true;
}
let (l, buffer) = (self.list, self.buffer);
// SAFETY: as the caller promises.
unsafe {
l.ResourceBarrier(&[transition(buffer, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)]);
l.SetComputeRootUnorderedAccessView(self.out_param, buffer.GetGPUVirtualAddress());
l.Dispatch(n, 1, 1);
l.ResourceBarrier(&[transition(
buffer,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
)]);
}
let drawn = graphics();
// SAFETY: as above, the graphics pipeline bound when `drawn`.
unsafe {
if drawn {
self.draw_from_buffer(|l| l.DrawInstanced(self.max_primitives * 3, n.min(self.groups_cap), 0, 0));
}
l.ResourceBarrier(&[transition(
buffer,
D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
D3D12_RESOURCE_STATE_COMMON,
)]);
}
drawn
}

/// One indirect record, the compute pipeline and its root arguments
/// bound: the record at `arguments` (a dispatch's `(x, y, z)` with the
/// title's stride, in `dispatch`) runs as recorded, or not at all when
/// the count at `count` is 0. Each group raises the instance count in
/// the buffer's header, which is written first ([3 * max_primitives, 0,
/// 0, 0]), and the draw (`draw`, of 16-byte records) reads the header.
/// `graphics` as for [`Self::direct`]. Whether it drew; `None` on a
/// list without `WriteBufferImmediate`.
///
/// # Safety
/// As [`Self::direct`]; the argument and count buffers in the indirect
/// argument state.
pub(crate) unsafe fn indirect(
&self,
dispatch: &ID3D12CommandSignature,
draw: &ID3D12CommandSignature,
(arguments, at): (&ID3D12Resource, u64),
count: Option<(&ID3D12Resource, u64)>,
graphics: impl FnOnce() -> bool,
) -> Option<bool> {
let (l, buffer) = (self.list, self.buffer);
let l2 = l.cast::<ID3D12GraphicsCommandList2>().ok()?;
let read = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT;
// SAFETY: as the caller promises.
unsafe {
let va = buffer.GetGPUVirtualAddress();
l.ResourceBarrier(&[transition(buffer, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST)]);
let header = [self.max_primitives * 3, 0, 0, 0];
let params: Vec<D3D12_WRITEBUFFERIMMEDIATE_PARAMETER> = header
.iter()
.enumerate()
.map(|(k, &value)| D3D12_WRITEBUFFERIMMEDIATE_PARAMETER { Dest: va + 4 * k as u64, Value: value })
.collect();
l2.WriteBufferImmediate(params.len() as u32, params.as_ptr(), None);
l.ResourceBarrier(&[transition(buffer, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)]);
l.SetComputeRootUnorderedAccessView(self.out_param, va);
l.ExecuteIndirect(dispatch, 1, arguments, at, count.map(|c| c.0), count.map_or(0, |c| c.1));
l.ResourceBarrier(&[transition(buffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, read)]);
}
let drawn = graphics();
// SAFETY: as above, the graphics pipeline bound when `drawn`.
unsafe {
if drawn {
self.draw_from_buffer(|l| l.ExecuteIndirect(draw, 1, buffer, 0, None, 0));
}
l.ResourceBarrier(&[transition(buffer, read, D3D12_RESOURCE_STATE_COMMON)]);
}
Some(drawn)
}

/// The draw itself: the buffer as the vertex shader's root SRV, a
/// triangle list, then the topology to restore.
unsafe fn draw_from_buffer(&self, draw: impl FnOnce(&ID3D12GraphicsCommandList)) {
let l = self.list;
// SAFETY: an open list with the graphics pipeline bound.
unsafe {
l.SetGraphicsRootShaderResourceView(self.in_param, self.buffer.GetGPUVirtualAddress());
l.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
draw(l);
if let Some(t) = self.restore {
l.IASetPrimitiveTopology(t);
}
}
}
}

#[cfg(test)]
mod tests;
Loading
Loading