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
3 changes: 2 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2357,7 +2357,8 @@ The trace will enumerate them.
- `DEPTH_STENCIL2`'s per-face read and write masks go to each face's `VkStencilOpState` (the front face's in the
description, the back face's beside it), so `OPTIONS14.IndependentFrontAndBackStencilRefMaskSupported` is TRUE:
with `OMSetFrontAndBackStencilRef`'s per-face references, both of that cap's parts.
- `RASTERIZER1`/`2`'s float depth bias is truncated to the integer the PSO path takes, with a log.
- `RASTERIZER1`/`2`'s `FLOAT DepthBias` is preserved verbatim in both
`VkPipelineRasterizationStateCreateInfo::depthBiasConstantFactor` and the D24-as-D32 fragment-bias path.
- **Mesh PSO:** the MS (`ms` index entry by FSSM hash) and PS on the mesh stage. It has no vertex input or input
assembly; the input layout, topology type and strip cut are ignored, as D3D12 ignores them for mesh PSOs. Topology
and strides are not dynamic state there (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065/07066). Creation
Expand Down
28 changes: 20 additions & 8 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6733,13 +6733,19 @@ struct StencilFaceMasks {
UINT8 write = D3D12_DEFAULT_STENCIL_WRITE_MASK;
};

struct GraphicsStreamExtras {
bool has_float_depth_bias = false;
float depth_bias = 0.0f;
};

struct PipelineStream {
D3D12_GRAPHICS_PIPELINE_STATE_DESC graphics{};
bool back_masks = false; // DEPTH_STENCIL2: back_stencil holds the back face's masks
StencilFaceMasks back_stencil;
D3D12_COMPUTE_PIPELINE_STATE_DESC compute{};
D3D12_SHADER_BYTECODE ms{};
D3D12_SHADER_BYTECODE as{};
GraphicsStreamExtras extras{};
bool has_cs = false;
bool has_graphics_stage = false; // VS, PS, DS, HS, GS, AS or MS
};
Expand Down Expand Up @@ -6926,7 +6932,8 @@ HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, Pipe
bias = d.DepthBias;
}
r.DepthBias = INT(bias);
if (ok && FLOAT(r.DepthBias) != bias) log_once("a fractional depth bias is truncated; bias x1000", int(bias * 1000));
out.extras.has_float_depth_bias = ok;
out.extras.depth_bias = bias;
break;
}
case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VIEW_INSTANCING: {
Expand Down Expand Up @@ -7116,7 +7123,9 @@ class PipelineState final
if (seen.insert(line).second) std::fprintf(stderr, "%s\n", line);
}
HRESULT create_graphics(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc, const D3D12_SHADER_BYTECODE* ms = nullptr,
const D3D12_SHADER_BYTECODE* as = nullptr, const StencilFaceMasks* back_masks = nullptr) {
const D3D12_SHADER_BYTECODE* as = nullptr,
const StencilFaceMasks* back_masks = nullptr,
const GraphicsStreamExtras* extras = nullptr) {
auto& vk = device_.context->vk;
const VkDevice dev = device_.info.device;
const bool mesh = ms && ms->pShaderBytecode && ms->BytecodeLength;
Expand Down Expand Up @@ -7259,9 +7268,11 @@ class PipelineState final
int(samples));
depth24_fragment = false;
}
const leonida::Depth24Bias depth24_bias{desc.RasterizerState.DepthBias,
desc.RasterizerState.SlopeScaledDepthBias,
desc.RasterizerState.DepthBiasClamp,packed16_fallback?65535.0f:16777215.0f};
const float depth_bias =
(extras && extras->has_float_depth_bias) ? extras->depth_bias : float(desc.RasterizerState.DepthBias);
const leonida::Depth24Bias depth24_bias{depth_bias, desc.RasterizerState.SlopeScaledDepthBias,
desc.RasterizerState.DepthBiasClamp,
packed16_fallback ? 65535.0f : 16777215.0f};
for (Stage& stage : stages) {
if (!stage.code.pShaderBytecode || !stage.code.BytecodeLength) continue; // no PS: depth only
const std::span<const std::uint8_t> raw_bytes{
Expand Down Expand Up @@ -7572,9 +7583,9 @@ class PipelineState final
raster.frontFace = r.FrontCounterClockwise ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE;
// The D24-as-D32 pixel stage applies the bias itself, in D24 units (depth24_fragment above): the
// rasterizer's bias stays off and zero, so its clamp needs no depthBiasClamp feature either.
raster.depthBiasEnable = !depth24_fragment && (r.DepthBias || r.SlopeScaledDepthBias != 0.0f);
raster.depthBiasEnable = !depth24_fragment && (depth_bias != 0.0f || r.SlopeScaledDepthBias != 0.0f);
if (raster.depthBiasEnable) {
raster.depthBiasConstantFactor = float(r.DepthBias);
raster.depthBiasConstantFactor = depth_bias;
raster.depthBiasClamp = r.DepthBiasClamp;
raster.depthBiasSlopeFactor = r.SlopeScaledDepthBias;
}
Expand Down Expand Up @@ -12094,7 +12105,8 @@ class Device final
pipeline->attach_device(identity());
const HRESULT hr = stream.has_cs ? pipeline->create_compute(stream.compute)
: pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as,
stream.back_masks ? &stream.back_stencil : nullptr);
stream.back_masks ? &stream.back_stencil : nullptr,
&stream.extras);
if (FAILED(hr)) {
pipeline->Release();
return finish(hr);
Expand Down
14 changes: 7 additions & 7 deletions runtime/src/depth24_fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
namespace leonida {

struct Depth24Bias {
int constant = 0; // D3D12_RASTERIZER_DESC::DepthBias
float slope = 0.0f; // SlopeScaledDepthBias
float clamp = 0.0f; // DepthBiasClamp: > 0 caps the bias, < 0 floors it, 0 leaves it
float constant = 0.0f; // D3D12_RASTERIZER_DESC::DepthBias / RASTERIZER1/2 FLOAT DepthBias
float slope = 0.0f; // SlopeScaledDepthBias
float clamp = 0.0f; // DepthBiasClamp: > 0 caps the bias, < 0 floors it, 0 leaves it
float levels = 16777215.0f;
bool any() const { return constant != 0 || slope != 0.0f; }
bool any() const { return constant != 0.0f || slope != 0.0f; }
};

// D3D's constant term in float: (float)DepthBias * r, with r the float nearest 1 / (2^24 - 1).
inline float depth24_bias_constant(int bias, float levels=16777215.0f) {
return float(bias) * float(1.0 / double(levels));
// D3D's constant term in float: DepthBias * r, with r the float nearest 1 / (2^24 - 1) (or the packed16 level count).
inline float depth24_bias_constant(float bias, float levels = 16777215.0f) {
return bias * float(1.0 / double(levels));
}

namespace spirv24 {
Expand Down
153 changes: 153 additions & 0 deletions runtime/test/d3d12_bundle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,159 @@ bool layer_checks(ID3D12Device* device, const std::vector<std::uint8_t>& vs, con
return false;
}
}

// RASTERIZER1 and RASTERIZER2 stream subobjects preserve FLOAT DepthBias (checked with a D24 depth target
// cleared to 0.0f and GREATER depth test: 0.0f bias fails against 0.0f, while 1.5f bias pushes above 0.0f).
{
Rig r;
CHECK(r.init(device, vs, ps));
Ref<ID3D12Device2> dev2;
CHECK(SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&dev2.p))));
struct alignas(void*) SubRootSig {
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE;
ID3D12RootSignature* v = nullptr;
};
struct alignas(void*) SubShader {
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t{};
D3D12_SHADER_BYTECODE v{};
};
struct alignas(void*) SubInput {
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT;
D3D12_INPUT_LAYOUT_DESC v{};
};
struct alignas(void*) SubTopology {
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY;
D3D12_PRIMITIVE_TOPOLOGY_TYPE v = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
};
struct alignas(void*) SubRtv {
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS;
D3D12_RT_FORMAT_ARRAY v{};
};
struct alignas(void*) SubDsvFormat {
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT;
DXGI_FORMAT v = DXGI_FORMAT_D24_UNORM_S8_UINT;
};
struct alignas(void*) SubDepth {
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL;
D3D12_DEPTH_STENCIL_DESC v{};
};
struct alignas(void*) SubRast1 {
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER1;
D3D12_RASTERIZER_DESC1 v{};
};
struct alignas(void*) SubRast2 {
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER2;
D3D12_RASTERIZER_DESC2 v{};
};
static const D3D12_INPUT_ELEMENT_DESC elems[2] = {
{"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D12_APPEND_ALIGNED_ELEMENT,
D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
};
const D3D12_DEPTH_STENCIL_DESC depth_greater{TRUE, D3D12_DEPTH_WRITE_MASK_ZERO, D3D12_COMPARISON_FUNC_GREATER};

D3D12_RESOURCE_DESC dsv_tex{};
dsv_tex.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
dsv_tex.Width = dsv_tex.Height = target_px;
dsv_tex.DepthOrArraySize = dsv_tex.MipLevels = 1;
dsv_tex.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dsv_tex.SampleDesc.Count = 1;
dsv_tex.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
D3D12_CLEAR_VALUE dsv_clear{};
dsv_clear.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
Ref<ID3D12Resource> depth_target;
CHECK(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &dsv_tex,
D3D12_RESOURCE_STATE_DEPTH_WRITE, &dsv_clear,
IID_PPV_ARGS(&depth_target.p))));
Ref<ID3D12DescriptorHeap> dsv_heap;
const D3D12_DESCRIPTOR_HEAP_DESC dsv_heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
CHECK(SUCCEEDED(device->CreateDescriptorHeap(&dsv_heap_desc, IID_PPV_ARGS(&dsv_heap.p))));
const D3D12_CPU_DESCRIPTOR_HANDLE dsv = dsv_heap->GetCPUDescriptorHandleForHeapStart();
device->CreateDepthStencilView(depth_target.p, nullptr, dsv);

const auto make_r1 = [&](FLOAT bias, Ref<ID3D12PipelineState>& out) {
struct Stream {
SubRootSig rs;
SubShader vs;
SubShader ps;
SubInput ia;
SubTopology top;
SubRtv rtv;
SubDsvFormat dsv_fmt;
SubDepth ds;
SubRast1 r1;
} s{};
s.rs.v = r.root1.p;
s.vs = {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, {vs.data(), vs.size()}};
s.ps = {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS, {ps.data(), ps.size()}};
s.ia.v = {elems, 2};
s.rtv.v.NumRenderTargets = 1;
s.rtv.v.RTFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
s.ds.v = depth_greater;
s.r1.v.FillMode = D3D12_FILL_MODE_SOLID;
s.r1.v.CullMode = D3D12_CULL_MODE_NONE;
s.r1.v.DepthBias = bias;
s.r1.v.DepthClipEnable = TRUE;
const D3D12_PIPELINE_STATE_STREAM_DESC stream_desc{sizeof(s), &s};
return SUCCEEDED(dev2->CreatePipelineState(&stream_desc, IID_PPV_ARGS(&out.p)));
};
const auto make_r2 = [&](FLOAT bias, Ref<ID3D12PipelineState>& out) {
struct Stream {
SubRootSig rs;
SubShader vs;
SubShader ps;
SubInput ia;
SubTopology top;
SubRtv rtv;
SubDsvFormat dsv_fmt;
SubDepth ds;
SubRast2 r2;
} s{};
s.rs.v = r.root1.p;
s.vs = {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, {vs.data(), vs.size()}};
s.ps = {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS, {ps.data(), ps.size()}};
s.ia.v = {elems, 2};
s.rtv.v.NumRenderTargets = 1;
s.rtv.v.RTFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
s.ds.v = depth_greater;
s.r2.v.FillMode = D3D12_FILL_MODE_SOLID;
s.r2.v.CullMode = D3D12_CULL_MODE_NONE;
s.r2.v.DepthBias = bias;
s.r2.v.DepthClipEnable = TRUE;
const D3D12_PIPELINE_STATE_STREAM_DESC stream_desc{sizeof(s), &s};
return SUCCEEDED(dev2->CreatePipelineState(&stream_desc, IID_PPV_ARGS(&out.p)));
};
Ref<ID3D12PipelineState> r1_zero, r1_bias, r2_bias;
CHECK(make_r1(0.0f, r1_zero));
CHECK(make_r1(1.5f, r1_bias));
CHECK(make_r2(1.5f, r2_bias));

const auto draw_quad = [&](ID3D12PipelineState* pso, const char* expected) {
std::string grid_text;
HRESULT close = S_OK;
if (!r.render([&](Rig& rig, ID3D12GraphicsCommandList* l) {
const D3D12_VIEWPORT viewport{0, 0, target_px, target_px, 0, 1};
const D3D12_RECT scissor{0, 0, LONG(target_px), LONG(target_px)};
l->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, 0.0f, 0, 0, nullptr);
l->OMSetRenderTargets(1, &rig.rtv, FALSE, &dsv);
l->RSSetViewports(1, &viewport);
l->RSSetScissorRects(1, &scissor);
l->SetPipelineState(pso);
l->SetGraphicsRootSignature(rig.root1.p);
rig.vertices(l, vb_quad);
rig.offset(l, 0, 0);
rig.tint(l, tint_red);
l->IASetPrimitiveTopology(strip);
l->DrawInstanced(4, 1, 0, 0);
}, grid_text, close))
return false;
return close == S_OK && grid_text == expected;
};
CHECK(draw_quad(r1_zero.p, BLANK4 " " BLANK4));
CHECK(draw_quad(r1_bias.p, ROWS4("RRRRBBBB") " " BLANK4));
CHECK(draw_quad(r2_bias.p, ROWS4("RRRRBBBB") " " BLANK4));
}
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions runtime/test/d3d12_d24_support_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// Synthetic resources only; no game files.
#include "vk_test_device.h"
#include "../shims/d3d12_com.h"
#include "../src/depth24_fragment.h"

#include <cstdio>

Expand Down Expand Up @@ -106,6 +107,10 @@ int main() {
}
device->Release();

check(leonida::depth24_bias_constant(0.5f) > 0.0f &&
leonida::depth24_bias_constant(1.5f) > leonida::depth24_bias_constant(1.0f),
"depth24_bias_constant preserves fractional DepthBias", DXGI_FORMAT_D24_UNORM_S8_UINT);

if (g_failures) {
std::printf("d3d12_d24_support_test: %d checks failed\n", g_failures);
return 1;
Expand Down
Loading