Skip to content
Closed
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
43 changes: 41 additions & 2 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3113,7 +3113,25 @@ struct PipelineStream {
bool has_graphics_stage = false; // VS, PS, DS, HS, GS, AS or MS
};

HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, PipelineStream& out) {
// LEONIDA_XS_STREAM_ENUM=pc: never read a stream with the console's numbering (below).
bool console_stream_enum_enabled() {
static const bool enabled = [] {
char value[8] = {};
const DWORD n = GetEnvironmentVariableA("LEONIDA_XS_STREAM_ENUM", value, sizeof(value));
return !(n == 2 && (value[0] == 'p' || value[0] == 'P') && (value[1] == 'c' || value[1] == 'C'));
}();
return enabled;
}

// console_enum: read the types with the console's numbering, which has no gap at 23 where the PC's has one (PC:
// VIEW_INSTANCING 22, AS 24, MS 25, DEPTH_STENCIL2 26, ...; console: AS 23, MS 24, ...), so a type from 23 on is the PC
// type one above it. Evidence (docs/retail_boot_intel_host.md section 3): in a retail boot, the 430 streams whose
// subobject 24 the PC reading took for an AS each carried a payload the size of one of the title's mesh-shader
// containers (430 of 430; 280 of those sizes match no vertex shader), and none of them had a 25.
// CreatePipelineState uses it only for a stream the PC reading leaves with an AS and no MS, which the PC runtime
// refuses (an AS needs an MS) and this layer refuses too, so no stream that works today is read differently.
HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, PipelineStream& out,
bool console_enum = false) {
if (!desc.pPipelineStateSubobjectStream || !desc.SizeInBytes) return E_INVALIDARG;
D3D12_GRAPHICS_PIPELINE_STATE_DESC& g = out.graphics;
for (auto& target : g.BlendState.RenderTarget) {
Expand Down Expand Up @@ -3145,6 +3163,8 @@ HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, Pipe
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type;
if (size - at < sizeof(type)) return E_INVALIDARG;
std::memcpy(&type, base + at, sizeof(type));
if (console_enum && unsigned(type) >= 23 && unsigned(type) < 63)
type = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(unsigned(type) + 1);
// The versioned depth-stencil and rasterizer subobjects describe the same state as their base type, so the
// stream contract counts any two of one family as duplicates; the payload below still follows `type`.
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE family = type;
Expand Down Expand Up @@ -6180,7 +6200,26 @@ class Device final
*out = nullptr;
if (!desc) return d3d12_trace::result(method, E_INVALIDARG);
PipelineStream stream;
if (const HRESULT hr = parse_pipeline_stream(*desc, stream); FAILED(hr)) return d3d12_trace::result(method, hr);
HRESULT parsed = parse_pipeline_stream(*desc, stream);
// A stream the PC reading refuses, or leaves with an AS and no MS (no valid PC stream), is read again with the
// console's numbering (parse_pipeline_stream), and that reading is taken when it holds a mesh shader (its 24)
// and no AS. A stream the PC reading accepts as valid is never read differently. LEONIDA_XS_STREAM_ENUM=pc
// keeps the PC reading alone (such PSOs are then refused, as before).
if ((FAILED(parsed) || (stream.as.pShaderBytecode && !stream.ms.pShaderBytecode)) &&
console_stream_enum_enabled()) {
PipelineStream console;
if (SUCCEEDED(parse_pipeline_stream(*desc, console, true)) && console.ms.pShaderBytecode &&
!console.as.pShaderBytecode) {
static std::atomic<unsigned> renumbered{0};
if (renumbered.fetch_add(1, std::memory_order_relaxed) == 0)
std::fprintf(stderr, "[rt] d3d12: a pipeline stream the PC numbering cannot read (an AS and no MS, "
"or refused) is read with the console's subobject numbering (its 24 is the "
"mesh shader); LEONIDA_XS_STREAM_ENUM=pc turns this off\n");
stream = console;
parsed = S_OK;
}
}
if (FAILED(parsed)) return d3d12_trace::result(method, parsed);
auto* pipeline = new PipelineState(state_);
pipeline->attach_device(identity());
const HRESULT hr = stream.has_cs ? pipeline->create_compute(stream.compute)
Expand Down
28 changes: 28 additions & 0 deletions runtime/test/d3d12_mesh_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,34 @@ int main() {
bad.add(DepthStencil1Subobject{.inner = bounds});
EXPECT(create(device.p, bad, refused) == E_NOTIMPL && !refused.p);

// A console stream: its numbering has no gap at 23, so its mesh shader is 24 (the PC's AS) and its
// RASTERIZER1 26 (the PC's DEPTH_STENCIL2). An AS without an MS is no PC stream, so the layer reads it with the
// console's numbering and makes the mesh PSO. The PC reading of the same bytes alone would refuse it.
D3D12_RASTERIZER_DESC1 raster1{};
raster1.FillMode = D3D12_FILL_MODE_SOLID;
raster1.CullMode = D3D12_CULL_MODE_NONE;
raster1.DepthClipEnable = TRUE;
unsigned console_psos = 0;
for (int with_raster1 = 0; with_raster1 < 2; ++with_raster1) {
Stream console;
console.add(RootSubobject{.inner = root.p});
console.add(Subobject<D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(24), D3D12_SHADER_BYTECODE>{.inner = ms});
console.add(PsSubobject{.inner = ps}).add(TargetsSubobject{.inner = one_target()});
if (with_raster1)
console.add(Subobject<D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(26), D3D12_RASTERIZER_DESC1>{.inner = raster1});
Ref<ID3D12PipelineState> console_pso;
const HRESULT console_hr = create(device.p, console, console_pso);
if (FAILED(console_hr))
std::fprintf(stderr, "console stream %d: 0x%08lx\n", with_raster1, static_cast<unsigned long>(console_hr));
EXPECT(SUCCEEDED(console_hr) && console_pso.p);
++console_psos;
}
// An MS and an AS in PC numbering stay the PC's (and refused, the layer builds no AS).
bad = mesh_stream;
bad.add(Subobject<D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(24), D3D12_SHADER_BYTECODE>{.inner = ms});
EXPECT(create(device.p, bad, refused) == E_NOTIMPL && !refused.p);
std::printf("console-numbered streams: %u mesh PSOs (MS as 24, RASTERIZER1 as 26)\n", console_psos);

const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
const D3D12_HEAP_PROPERTIES upload_heap{D3D12_HEAP_TYPE_UPLOAD};
const D3D12_HEAP_PROPERTIES readback_heap{D3D12_HEAP_TYPE_READBACK};
Expand Down