Skip to content
Merged
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
56 changes: 49 additions & 7 deletions d3d12/crates/leonida-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint> 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<float> twin : register(t0);
[RootSignature(RS)]
float plane(float4 p : SV_Position) : SV_Depth {
return twin.Load(int4(int2(p.xy), 0, 0));
}
Expand Down Expand Up @@ -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, &parameter)?;
let desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC {
pRootSignature: std::mem::transmute_copy(&root),
VS: code(&vs[..]),
Expand Down Expand Up @@ -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<ID3D12RootSignature> {
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::<u8>(), 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
Expand Down
19 changes: 14 additions & 5 deletions d3d12/crates/leonida-gpu/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, &parameter).unwrap();
let desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC {
pRootSignature: std::mem::transmute_copy(&signature),
VS: code(&vs[..]),
Expand Down