From 5b3477768cafdbf0f3dfe3867e51089a6ae15642 Mon Sep 17 00:00:00 2001 From: merzleckeier <362449027+merzleckeier@users.noreply.github.com> Date: Sat, 28 Nov 2026 21:30:34 +0100 Subject: [PATCH] d3d12: serialize depth-plane root signatures explicitly Wine can compile the depth-plane shader without embedding its RootSignature attribute. Serialize the matching v1.1 signature from the host parameter instead, and give the existing depth-plane GPU test an explicit signature for its readback draw. --- d3d12/crates/leonida-gpu/src/host.rs | 56 +++++++++++++++++++++--- d3d12/crates/leonida-gpu/src/resource.rs | 19 +++++--- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/host.rs b/d3d12/crates/leonida-gpu/src/host.rs index 4566e7d231..0f12273301 100644 --- a/d3d12/crates/leonida-gpu/src/host.rs +++ b/d3d12/crates/leonida-gpu/src/host.rs @@ -1600,17 +1600,13 @@ float4 corner(uint i : SV_VertexID) : SV_Position { } "#; const PLANE_STEPS: &str = r#" -#define RS "DescriptorTable(SRV(t0), visibility = SHADER_VISIBILITY_PIXEL)" Texture2DArray twin : register(t0); -[RootSignature(RS)] float plane(float4 p : SV_Position) : SV_Depth { return float(twin.Load(int4(int2(p.xy), 0, 0))) * asfloat(0x37800080); } "#; const PLANE_FLOATS: &str = r#" -#define RS "DescriptorTable(SRV(t0), visibility = SHADER_VISIBILITY_PIXEL)" Texture2DArray twin : register(t0); -[RootSignature(RS)] float plane(float4 p : SV_Position) : SV_Depth { return twin.Load(int4(int2(p.xy), 0, 0)); } @@ -1720,10 +1716,28 @@ impl PlanePass { pShaderBytecode: bytes.as_ptr().cast(), BytecodeLength: bytes.len(), }; - // SAFETY: the root signature is inside the pixel shader; the - // description points at live data and never releases its pointer. + // Wine's HLSL compiler leaves RootSignature attributes out of the bytecode. + let range = D3D12_DESCRIPTOR_RANGE1 { + RangeType: D3D12_DESCRIPTOR_RANGE_TYPE_SRV, + NumDescriptors: 1, + BaseShaderRegister: 0, + RegisterSpace: 0, + Flags: D3D12_DESCRIPTOR_RANGE_FLAG_DATA_STATIC_WHILE_SET_AT_EXECUTE, + OffsetInDescriptorsFromTableStart: D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, + }; + let parameter = D3D12_ROOT_PARAMETER1 { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, + Anonymous: D3D12_ROOT_PARAMETER1_0 { + DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE1 { + NumDescriptorRanges: 1, + pDescriptorRanges: &range, + }, + }, + ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL, + }; + // SAFETY: the range, root signature and bytecode live through the calls. unsafe { - let root: ID3D12RootSignature = device.CreateRootSignature(0, &ps[..])?; + let root = plane_root_signature(device, ¶meter)?; let desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC { pRootSignature: std::mem::transmute_copy(&root), VS: code(&vs[..]), @@ -1756,6 +1770,34 @@ impl PlanePass { } } +/// Serializes the single root parameter used by the plane pass or its test draw. +/// +/// # Safety +/// Any descriptor ranges referenced by the parameter must live through this call. +pub(crate) unsafe fn plane_root_signature( + device: &ID3D12Device, + parameter: &D3D12_ROOT_PARAMETER1, +) -> windows::core::Result { + let desc = D3D12_VERSIONED_ROOT_SIGNATURE_DESC { + Version: D3D_ROOT_SIGNATURE_VERSION_1_1, + Anonymous: D3D12_VERSIONED_ROOT_SIGNATURE_DESC_0 { + Desc_1_1: D3D12_ROOT_SIGNATURE_DESC1 { + NumParameters: 1, + pParameters: parameter, + ..Default::default() + }, + }, + }; + let mut blob = None; + // SAFETY: the description borrows the live parameter and its ranges. + unsafe { + D3D12SerializeVersionedRootSignature(&desc, &mut blob, None)?; + let blob = blob.ok_or_else(windows::core::Error::empty)?; + let bytes = std::slice::from_raw_parts(blob.GetBufferPointer().cast::(), blob.GetBufferSize()); + device.CreateRootSignature(0, bytes) + } +} + /// Records into `list` the pass that draws `plane`, the twin of the depth /// plane of `depth`, into that plane: `depth` in the depth-write state, /// `plane` readable by pixel shaders. The pass sets the list's descriptor diff --git a/d3d12/crates/leonida-gpu/src/resource.rs b/d3d12/crates/leonida-gpu/src/resource.rs index 31fbcfe1a0..a5b570f707 100644 --- a/d3d12/crates/leonida-gpu/src/resource.rs +++ b/d3d12/crates/leonida-gpu/src/resource.rs @@ -4949,9 +4949,7 @@ mod tests { // The draws: a triangle over the target at the depth of a class, // as the title's lighting places its rectangles. const SOURCE: &str = r#" -#define RS "RootConstants(num32BitConstants = 1, b0)" cbuffer Drawn : register(b0) { uint number; }; -[RootSignature(RS)] float4 corner(uint i : SV_VertexID) : SV_Position { float z = float(number) * asfloat(0x37800080); return float4(float(i & 1) * 4.0 - 1.0, float(i >> 1) * 4.0 - 1.0, z, 1.0); @@ -4968,10 +4966,21 @@ float4 lit() : SV_Target { return 1.0; } blend.RenderTarget[0].RenderTargetWriteMask = 0xf; let mut formats = [DXGI_FORMAT(0); 8]; formats[0] = DXGI_FORMAT(61); - // SAFETY: plain creation; the description points at live data. + let parameter = D3D12_ROOT_PARAMETER1 { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, + Anonymous: D3D12_ROOT_PARAMETER1_0 { + Constants: D3D12_ROOT_CONSTANTS { + ShaderRegister: 0, + RegisterSpace: 0, + Num32BitValues: 1, + }, + }, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + }; + // SAFETY: the parameter has no descriptor ranges and the pipeline description + // points at live data. let (signature, pipeline) = unsafe { - let signature: ID3D12RootSignature = - host.device.CreateRootSignature(0, &vs[..]).unwrap(); + let signature = crate::host::plane_root_signature(&host.device, ¶meter).unwrap(); let desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC { pRootSignature: std::mem::transmute_copy(&signature), VS: code(&vs[..]),