diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3ae29163e..42a886b81 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -832,6 +832,10 @@ The compute path, end to end: `d3d12_state_sweep_test` checks the mapping against the host on the same adapter: 482 full-screen draws over every blend factor pair, blend operation, write mask, comparison function and stencil operation, with the stencil plane read back, are byte-identical on an RTX 5080 and a Radeon iGPU. + - A triangle-family PSO can draw Xbox rectangle lists. When the input assembler selects topology 17, replay + selects a geometry-stage variant that emits the fourth corner after vertex shading. It requires + `geometryShader` and a vertex pipeline without an application geometry stage. A later triangle draw uses the + original PSO. The translated-draw GPU test switches between both primitives on one PSO and checks every pixel. - **Logic ops** come from `RenderTarget[0]`, as in D3D12, and apply to every target (Vulkan has one per pipeline; it skips float formats). **`DepthClipEnable = FALSE`** is depth clamping: D3D12 clamps output depth to the viewport's range anyway. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 0273f9339..868f01f54 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -18,6 +18,7 @@ #include "../src/arena_size.h" #include "../src/gfx10_descriptors.h" #include "../src/depth24_fragment.h" +#include "../src/rect_list_geometry.h" #include "../src/uav_clear.h" #include "../src/vulkan_bindless.h" #include "../src/vulkan_memory.h" @@ -803,6 +804,23 @@ struct DeviceState { return shader_library.loaded() ? &shader_library : nullptr; } + // PSOs that differ only in fixed state can share the linked vertex program. The pixel entry distinguishes + // f16 interface variants. Compare the actual words as well, and let the last PSO release its snapshot. + using VertexWords = std::vector; + std::mutex rectangle_vertices_mutex; + std::map, + std::weak_ptr> rectangle_vertices; + + std::shared_ptr retain_rectangle_vertex(const leonida::ShaderEntry* vs, + const leonida::ShaderEntry* linked_ps, const VertexWords& words) { + std::lock_guard lock(rectangle_vertices_mutex); + auto& weak = rectangle_vertices[{vs, linked_ps}]; + if (auto shared = weak.lock(); shared && *shared == words) return shared; + auto shared = std::make_shared(words); + weak = shared; + return shared; + } + bool depth_usable(VkFormat format) const { VkFormatProperties properties{}; format_properties(info.physical, format, &properties); @@ -3613,6 +3631,15 @@ class PipelineState final pixel ? "fragmentStoresAndAtomics" : "vertexPipelineStoresAndAtomics"); return E_NOTIMPL; } + const auto topology_bits = std::bit_cast(desc.PrimitiveTopologyType); + if (!mesh && topology_bits != leonida::topology_type_rect_list) + graphics_.rectangle_variant_topology = leonida::topology_rect_list; + if (!mesh && !geometry && device_.info.features.geometryShader && + topology_bits == D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE) { + const bool linked = + ps_stage.entry && (!ps_stage.entry->f16_halves.empty() || !vs_stage.entry->f16_halves.empty()); + rectangle_vertex_ = device_.retain_rectangle_vertex(vs_stage.entry, linked ? ps_stage.entry : nullptr, vs_stage.words); + } for (Stage& stage : stages) { if (!stage.entry) continue; // After the f16 link above, which can also edit the pixel shader's words. @@ -4179,6 +4206,17 @@ class PipelineState final info.pColorBlendState = &blend; info.pDynamicState = &dynamic; info.layout = graphics_.layout; + std::vector rectangle_stages; + if (unbound & leonida::GraphicsPipeline::rectangle_stage) { + rectangle_stages.assign(info.pStages, info.pStages + info.stageCount); + VkPipelineShaderStageCreateInfo geometry{VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}; + geometry.stage = VK_SHADER_STAGE_GEOMETRY_BIT; + geometry.module = rectangle_module_; + geometry.pName = "main"; + rectangle_stages.push_back(geometry); + info.stageCount = static_cast(rectangle_stages.size()); + info.pStages = rectangle_stages.data(); + } VkPipeline pipeline = VK_NULL_HANDLE; if (device_.context->vk.vkCreateGraphicsPipelines(device_.info.device, device_.pipeline_cache(), 1, &info, nullptr, &pipeline) != VK_SUCCESS) @@ -4186,10 +4224,37 @@ class PipelineState final return pipeline; } + // Called under variants_mutex_. Attachment combinations reuse the same geometry module. + VkShaderModule rectangle_module() { + if (rectangle_module_ || !rectangle_vertex_) return rectangle_module_; + std::string why; + const auto words = leonida::rect_list_geometry(*rectangle_vertex_, why); + if (words.empty()) { + std::fprintf(stderr, "[rt] d3d12: cannot draw rectangle list: %s\n", why.c_str()); + rectangle_vertex_.reset(); + return VK_NULL_HANDLE; + } + VkShaderModuleCreateInfo module{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; + module.codeSize = words.size() * sizeof(words.front()); + module.pCode = words.data(); + VkShaderModule handle = VK_NULL_HANDLE; + if (device_.context->vk.vkCreateShaderModule(device_.info.device, &module, nullptr, &handle) != VK_SUCCESS) + return VK_NULL_HANDLE; + modules_.push_back(handle); + rectangle_module_ = handle; + rectangle_vertex_.reset(); + return handle; + } + // Built on first use and kept until the PSO is released (replay may call this from any queue). VkPipeline variant(std::uint32_t unbound) { std::lock_guard lock(variants_mutex_); if (const auto it = variants_.find(unbound); it != variants_.end()) return it->second; + if ((unbound & leonida::GraphicsPipeline::rectangle_stage) && !rectangle_module()) { + log_once("a rectangle-list draw needs a compatible vertex pipeline and geometryShader; topology", + leonida::topology_rect_list); + return VK_NULL_HANDLE; + } const VkPipeline pipeline = build(unbound); if (!pipeline) { log_once("a pipeline variant for unbound attachment slots could not be built; slots", int(unbound)); @@ -4207,6 +4272,8 @@ class PipelineState final std::map variants_; VkShaderModule module_ = VK_NULL_HANDLE; std::vector modules_; + std::shared_ptr rectangle_vertex_; + VkShaderModule rectangle_module_ = VK_NULL_HANDLE; VkDeviceSize code_base_ = 0; VkDeviceSize code_bytes_ = 0; bool registered_ = false; diff --git a/runtime/src/rect_list_geometry.h b/runtime/src/rect_list_geometry.h new file mode 100644 index 000000000..e90173095 --- /dev/null +++ b/runtime/src/rect_list_geometry.h @@ -0,0 +1,268 @@ +#pragma once + +// Console rectangle lists on Vulkan, which has no such primitive. +// +// A rectangle is three corners and the hardware makes the fourth, a + b - p (p the right-angle corner, a and b its +// neighbours), for the position and for every interpolant, after the vertex stage. Which vertex is p is not fixed: +// the retail boot's full-screen rectangle has it at the second vertex. The stage picks the vertex opposite the longest +// screen-space edge, and the first vertex on a tie. A list topology of rectangles (D3D_PRIMITIVE_TOPOLOGY 17) and a pipeline +// whose topology type is 19 are console-only. The fact comes from xvalkyrie (GPL-3.0, commit 53af7df77c10, notes in +// its backlog); no code is taken from it. +// +// The fourth vertex depends on what the vertex stage wrote, not on what it read, so the expansion has to sit after +// the vertex stage: a pre-pass over the vertex buffers is wrong for any shader that is not linear in its inputs (and +// the translated vertex shaders fetch by V# themselves, so there is no vertex buffer to expand). The pipeline gets a +// small geometry stage instead, built here from the vertex shader's own SPIR-V: it takes the triangle (v1, v2, v3) +// the list draws and emits the parallelogram as the two triangles (p, a, f) and (p, f, b), with the winding of +// (v1, v2, v3). Both start at p, so a flat input (taken from a triangle's first vertex) is p's over the whole +// rectangle. Every Location output of the vertex stage is a vec4 float in the translator's output (rdna2spirv.py), +// and so is the stage's Position; the generated stage reads and writes exactly those. +// +// The edges are measured in normalized device coordinates, not pixels (the stage does not know the viewport). For the +// usual axis-aligned rectangle that is exact; a rotated rectangle drawn into a very non-square viewport can pick a +// different corner than the pixel-space right angle. +// +// Not covered, and refused with a reason: a vertex stage that writes ClipDistance, CullDistance, Layer or +// ViewportIndex, or a Location output that is not a vec4 float. +// +// Plain word editing, standard library only. + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace leonida { + +// D3D_PRIMITIVE_TOPOLOGY values and the PSO topology type of the console's rectangle and quad lists. +constexpr std::uint32_t topology_rect_list = 17; +constexpr std::uint32_t topology_quad_list = 19; +constexpr std::uint32_t topology_type_rect_list = 19; +constexpr std::array quad_triangle_indices{0, 1, 2, 0, 2, 3}; + +// The geometry stage for `vertex_words`' outputs; empty, with `why` set, when the vertex stage cannot be expanded. +inline std::vector rect_list_geometry(const std::vector& vertex_words, std::string& why) { + enum : std::uint32_t { + op_entry_point = 15, op_type_void = 19, op_type_int = 21, op_type_float = 22, + op_type_vector = 23, op_type_array = 28, op_type_pointer = 32, op_type_function = 33, op_constant = 43, + op_function = 54, op_function_end = 56, op_variable = 59, op_load = 61, op_store = 62, op_access_chain = 65, + op_decorate = 71, op_fadd = 129, op_fsub = 131, op_fmul = 133, op_fdiv = 136, + op_type_bool = 20, op_composite_construct = 80, op_composite_extract = 81, op_logical_and = 167, + op_logical_not = 168, op_select = 169, op_ford_greater_equal = 190, op_emit_vertex = 218, op_end_primitive = 219, op_label = 248, op_return = 253, + op_capability = 17, op_memory_model = 14, op_execution_mode = 16, + deco_builtin = 11, deco_location = 30, builtin_position = 0, builtin_point_size = 1, + storage_input = 1, storage_output = 3 + }; + why.clear(); + if (vertex_words.size() < 6 || vertex_words[0] != 0x07230203u) { + why = "the vertex shader is not a SPIR-V module"; + return {}; + } + // What the vertex stage declares: types, Output variables, and the decorations that name them. + struct Type { std::uint32_t op = 0, component = 0, count = 0, width = 0; }; + std::map types; + std::map variable_type; // Output variable -> its pointer type + std::map location, builtin; // variable id -> value + std::set outputs; + for (std::size_t i = 5; i < vertex_words.size();) { + const std::uint32_t op = vertex_words[i] & 0xffffu, n = vertex_words[i] >> 16; + if (!n || i + n > vertex_words.size()) { + why = "the vertex shader's SPIR-V is cut short"; + return {}; + } + const std::uint32_t* w = &vertex_words[i]; + if (op == op_type_float && n >= 3) types[w[1]] = {op, 0, 0, w[2]}; + else if (op == op_type_vector && n >= 4) types[w[1]] = {op, w[2], w[3], 0}; + else if (op == op_type_pointer && n >= 4) types[w[1]] = {op, w[3], w[2], 0}; + else if (op == op_variable && n >= 4 && w[3] == storage_output) outputs.insert(w[2]), variable_type[w[2]] = w[1]; + else if (op == op_decorate && n >= 4 && w[2] == deco_location) location[w[1]] = w[3]; + else if (op == op_decorate && n >= 4 && w[2] == deco_builtin) builtin[w[1]] = w[3]; + i += n; + } + std::uint32_t position = 0; + std::vector params; // Location values, ascending + for (std::uint32_t id : outputs) { + const auto b = builtin.find(id); + if (b != builtin.end()) { + if (b->second == builtin_position) position = id; + else if (b->second != builtin_point_size) { + why = "the vertex shader writes ClipDistance, CullDistance, Layer or ViewportIndex"; + return {}; + } + continue; + } + const auto l = location.find(id); + if (l == location.end()) continue; + const Type& ptr = types[variable_type[id]]; + const Type& vec = types[ptr.component]; + const Type& scalar = types[vec.component]; + // Known limit: every vec4 float output gets the plain a + b - p corner. The translator can export an attribute + // as the register's raw bits with two packed f16 halves (only the pixel shader knows, through per_vertex in + // tools/rdna2spirv.py), and nothing on the vertex side tells those apart, so such an output would get a wrong + // fourth corner. Fixing it needs the pixel shader's words here and a per-channel marker from the translator. + if (vec.op != op_type_vector || vec.count != 4 || scalar.op != op_type_float || scalar.width != 32) { + why = "a vertex output is not a vec4 float"; + return {}; + } + params.push_back(l->second); + } + if (!position) { + why = "the vertex shader writes no Position"; + return {}; + } + std::sort(params.begin(), params.end()); + params.erase(std::unique(params.begin(), params.end()), params.end()); + + // The module: a fixed prelude, then per variable ids, then the body. Ids count up from 1. + std::uint32_t next = 1; + const auto id = [&] { return next++; }; + std::vector head, annotations, globals, body; + const auto emit = [](std::vector& to, std::uint32_t op, std::initializer_list args) { + to.push_back((std::uint32_t(args.size()) + 1) << 16 | op); + to.insert(to.end(), args.begin(), args.end()); + }; + const std::uint32_t t_void = id(), t_fn = id(), t_uint = id(), t_float = id(), t_v4 = id(), + c3 = id(), t_arr = id(), t_pin_arr = id(), t_pin_v4 = id(), + t_pout_v4 = id(), c0 = id(), c1 = id(), c2 = id(), t_bool = id(), t_bool4 = id(); + emit(globals, op_type_void, {t_void}); + emit(globals, op_type_function, {t_fn, t_void}); + emit(globals, op_type_int, {t_uint, 32, 0}); + emit(globals, op_type_float, {t_float, 32}); + emit(globals, op_type_vector, {t_v4, t_float, 4}); + emit(globals, op_type_bool, {t_bool}); + emit(globals, op_type_vector, {t_bool4, t_bool, 4}); + emit(globals, op_constant, {t_uint, c3, 3}); + emit(globals, op_constant, {t_uint, c0, 0}); + emit(globals, op_constant, {t_uint, c1, 1}); + emit(globals, op_constant, {t_uint, c2, 2}); + emit(globals, op_type_array, {t_arr, t_v4, c3}); + emit(globals, op_type_pointer, {t_pin_arr, storage_input, t_arr}); + emit(globals, op_type_pointer, {t_pin_v4, storage_input, t_v4}); + emit(globals, op_type_pointer, {t_pout_v4, storage_output, t_v4}); + + struct Var { std::uint32_t in, out; bool is_position; }; + std::vector vars; + vars.push_back({id(), id(), true}); + for (std::size_t i = 0; i < params.size(); ++i) vars.push_back({id(), id(), false}); + std::vector io_vars; + emit(annotations, op_decorate, {vars[0].in, deco_builtin, builtin_position}); + emit(annotations, op_decorate, {vars[0].out, deco_builtin, builtin_position}); + for (std::size_t i = 0; i < params.size(); ++i) { + emit(annotations, op_decorate, {vars[i + 1].in, deco_location, params[i]}); + emit(annotations, op_decorate, {vars[i + 1].out, deco_location, params[i]}); + } + for (const Var& v : vars) { + emit(globals, op_variable, {t_pin_arr, v.in, storage_input}); + emit(globals, op_variable, {t_pout_v4, v.out, storage_output}); + io_vars.push_back(v.in); + io_vars.push_back(v.out); + } + + const std::uint32_t main_fn = id(), entry_label = id(); + emit(body, op_function, {t_void, main_fn, 0, t_fn}); + emit(body, op_label, {entry_label}); + // Per variable: the three vertices as drawn, loaded first. + struct Corners { std::uint32_t v[4]; }; // v[0] the right-angle corner, v[1], v[2] its neighbours, v[3] the fourth + std::vector corners; + std::vector> loaded; + const std::uint32_t cs[3] = {c0, c1, c2}; + for (const Var& v : vars) { + std::array three{}; + for (int k = 0; k < 3; ++k) { + const std::uint32_t ptr = id(); + three[k] = id(); + emit(body, op_access_chain, {t_pin_v4, ptr, v.in, cs[k]}); + emit(body, op_load, {t_v4, three[k], ptr}); + } + loaded.push_back(three); + } + // Which vertex is the right angle: the one opposite the longest edge, measured on screen (position.xy / position.w; + // a right triangle's longest edge is its hypotenuse). Ties go to the first vertex, which keeps the plain + // "v1 is the corner" order for every rectangle whose right angle is there. A triangle that is not a right + // triangle gets the same rule, so the choice is at least consistent. + std::uint32_t screen[3][2]; + for (int k = 0; k < 3; ++k) { + const std::uint32_t x = id(), y = id(), w = id(); + emit(body, op_composite_extract, {t_float, x, loaded[0][k], 0}); + emit(body, op_composite_extract, {t_float, y, loaded[0][k], 1}); + emit(body, op_composite_extract, {t_float, w, loaded[0][k], 3}); + screen[k][0] = id(); + screen[k][1] = id(); + emit(body, op_fdiv, {t_float, screen[k][0], x, w}); + emit(body, op_fdiv, {t_float, screen[k][1], y, w}); + } + const auto squared_edge = [&](int a, int b) { + const std::uint32_t dx = id(), dy = id(), xx = id(), yy = id(), sum = id(); + emit(body, op_fsub, {t_float, dx, screen[a][0], screen[b][0]}); + emit(body, op_fsub, {t_float, dy, screen[a][1], screen[b][1]}); + emit(body, op_fmul, {t_float, xx, dx, dx}); + emit(body, op_fmul, {t_float, yy, dy, dy}); + emit(body, op_fadd, {t_float, sum, xx, yy}); + return sum; + }; + const std::uint32_t e01 = squared_edge(0, 1), e02 = squared_edge(0, 2), e12 = squared_edge(1, 2); + const std::uint32_t g12_01 = id(), g12_02 = id(), at_first = id(), g02_01 = id(), not_first = id(), at_second = id(); + emit(body, op_ford_greater_equal, {t_bool, g12_01, e12, e01}); + emit(body, op_ford_greater_equal, {t_bool, g12_02, e12, e02}); + emit(body, op_logical_and, {t_bool, at_first, g12_01, g12_02}); // the edge opposite v1 is longest: v1 is the corner + emit(body, op_ford_greater_equal, {t_bool, g02_01, e02, e01}); + emit(body, op_logical_not, {t_bool, not_first, at_first}); + emit(body, op_logical_and, {t_bool, at_second, not_first, g02_01}); // else the edge opposite v2: v2; else v3 + const std::uint32_t first4 = id(), second4 = id(); + emit(body, op_composite_construct, {t_bool4, first4, at_first, at_first, at_first, at_first}); + emit(body, op_composite_construct, {t_bool4, second4, at_second, at_second, at_second, at_second}); + // The corner p, its neighbours a = next and b = the one after (the same cyclic order as v1 v2 v3, so the winding + // is the drawn triangle's), and the fourth corner a + b - p, for the position and for every interpolant alike. + for (const auto& three : loaded) { + Corners c{}; + const auto pick = [&](std::uint32_t first, std::uint32_t second, std::uint32_t third) { + const std::uint32_t inner = id(), outer = id(); + emit(body, op_select, {t_v4, inner, second4, second, third}); + emit(body, op_select, {t_v4, outer, first4, first, inner}); + return outer; + }; + c.v[0] = pick(three[0], three[1], three[2]); + c.v[1] = pick(three[1], three[2], three[0]); + c.v[2] = pick(three[2], three[0], three[1]); + const std::uint32_t sum = id(), diff = id(); + emit(body, op_fadd, {t_v4, sum, c.v[1], c.v[2]}); + emit(body, op_fsub, {t_v4, diff, sum, c.v[0]}); + c.v[3] = diff; + corners.push_back(c); + } + // Two triangles, (p, a, f) and (p, f, b): the diagonal is p to f, so both start at p and a flat input + // (which Vulkan takes from a triangle's first vertex) is p's over the whole rectangle. The attributes are + // affine over the parallelogram, so the split does not change any interpolated value. + const int triangles[2][3] = {{0, 1, 3}, {0, 3, 2}}; + for (const auto& triangle : triangles) { + for (int k : triangle) { + for (std::size_t i = 0; i < vars.size(); ++i) emit(body, op_store, {vars[i].out, corners[i].v[k]}); + emit(body, op_emit_vertex, {}); + } + emit(body, op_end_primitive, {}); + } + emit(body, op_return, {}); + emit(body, op_function_end, {}); + + std::vector out{0x07230203u, 0x00010000u, 0, next, 0}; + emit(out, op_capability, {2}); // Geometry + emit(out, op_memory_model, {0, 1}); // Logical, GLSL450 + std::vector entry{3, main_fn, 0x6e69616du, 0}; // Geometry, %main, "main" + entry.insert(entry.end(), io_vars.begin(), io_vars.end()); + out.push_back((std::uint32_t(entry.size()) + 1) << 16 | op_entry_point); + out.insert(out.end(), entry.begin(), entry.end()); + emit(out, op_execution_mode, {main_fn, 22}); // Triangles + emit(out, op_execution_mode, {main_fn, 0, 1}); // Invocations 1 + emit(out, op_execution_mode, {main_fn, 29}); // OutputTriangleStrip + emit(out, op_execution_mode, {main_fn, 26, 6}); // OutputVertices 6 + out.insert(out.end(), annotations.begin(), annotations.end()); + out.insert(out.end(), globals.begin(), globals.end()); + out.insert(out.end(), body.begin(), body.end()); + return out; +} + +} // namespace leonida diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 889bb71f7..f7702f128 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -773,7 +773,10 @@ struct GraphicsPipeline { // unbound (D3D12 allows it; dynamic rendering needs the formats to agree). Bit i: colour slot i, bit 8: depth // and stencil. Null when it cannot be built. static constexpr std::uint32_t unbound_depth = 1u << 8; - std::function variant; + static constexpr std::uint32_t rectangle_stage = 1u << 9; + // IA topology requiring a fourth-corner geometry stage. Zero when the PSO already contains the stage. + std::uint32_t rectangle_variant_topology = 0; + std::function variant; std::uint32_t samples = 1; // rasterizationSamples; every bound attachment must have this count }; @@ -2160,6 +2163,8 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct // Slots the pipeline has a format for but the draw leaves unbound take a variant whose format there is // VK_FORMAT_UNDEFINED: D3D12 discards those writes, Vulkan wants the formats to match the attachments. std::uint32_t unbound = 0; + if (pipeline->rectangle_variant_topology && draw.topology == pipeline->rectangle_variant_topology) + unbound |= GraphicsPipeline::rectangle_stage; for (std::size_t slot = 0; slot < p.colors.size(); ++slot) if (pipeline->color_formats[slot] != VK_FORMAT_UNDEFINED && !p.colors[slot].id) unbound |= 1u << slot; if (pipeline->depth_format != VK_FORMAT_UNDEFINED && !p.depth.id) unbound |= GraphicsPipeline::unbound_depth; diff --git a/runtime/test/d3d12_translated_draw_test.cpp b/runtime/test/d3d12_translated_draw_test.cpp index 62ce7b65c..a489c660a 100644 --- a/runtime/test/d3d12_translated_draw_test.cpp +++ b/runtime/test/d3d12_translated_draw_test.cpp @@ -175,6 +175,7 @@ int main() { info.queue = d.queue; info.queue_family = d.family; info.buffer_device_address = true; + info.features = d.enabled; info.shader_dir = (root_dir / "spirv").string(); ID3D12Device* inner = nullptr; EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&inner)))); @@ -293,7 +294,8 @@ int main() { list->ResourceBarrier(2, barriers); // The first command list already contains the vertex upload. Reuse it for subsequent frames // after the preceding submission's fence has completed and its readback is available. - const auto render_frame = [&](UINT64 frame, float x_offset, float bias) -> int { + const auto render_frame = [&](UINT64 frame, float x_offset, float bias, + D3D_PRIMITIVE_TOPOLOGY last_topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST) -> int { if (frame > 1) { EXPECT(SUCCEEDED(allocator->Reset())); EXPECT(SUCCEEDED(list->Reset(allocator.p, pso.p))); @@ -323,6 +325,7 @@ int main() { {0, {1, -1, 0.125f + bias, 0}, 6}, // bottom right: same V#, other constants {1, {0, 0, 0.25f + bias, 0}, 3}}; // bottom left: the second V# for (const Draw& draw : draws) { + if (draw.slot == 1) list->IASetPrimitiveTopology(last_topology); list->SetGraphicsRootDescriptorTable(0, D3D12_GPU_DESCRIPTOR_HANDLE{gpu + std::uint64_t(draw.slot) * step}); list->SetGraphicsRoot32BitConstants(1, 4, draw.constants, 0); list->DrawInstanced(draw.vertices, 1, 0, 0); @@ -370,6 +373,31 @@ int main() { } #endif EXPECT(render_frame(1, 0.0f, 0.0f) == 0); +#ifndef LEONIDA_M15_GUEST + if (d.enabled.geometryShader) { + // The last draw covers half the lower-left quadrant as a triangle. The Xbox rectangle primitive + // fills the quadrant through the same PSO. The other three quadrants keep their original values. + const auto triangles = pixels; + constexpr auto rectangles = static_cast(17); + const Pixel lower_left = {0.75f, 0.25f, 0.75f, 1.0f}; + for (UINT64 frame : {2u, 4u}) { + EXPECT(render_frame(frame, 0.0f, 0.0f, rectangles) == 0); + for (UINT y = 0; y < target_extent; ++y) { + for (UINT x = 0; x < target_extent; ++x) { + const auto i = y * target_extent + x; + const auto& want = y >= target_extent / 2 && x < target_extent / 2 ? lower_left : triangles[i]; + if (pixels[i] != want) { + std::fprintf(stderr, "rectangle draw mismatch at (%u, %u) in frame %llu\n", x, y, + static_cast(frame)); + return 1; + } + } + } + EXPECT(render_frame(frame + 1, 0.0f, 0.0f) == 0); + EXPECT(pixels == triangles); + } + } +#endif for (auto* r : {&vertex_buffer, &upload, &readback, &target}) r->reset(); pso.reset(); root.reset();