upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 20:40:44 +0800
committerRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 20:49:20 +0800
commitc55ecc9cbf4e7cde8b80b032b827e5943d451a9e (patch)
tree79ae52e0e666437e46d2dcf8d8f51bd6b762349c
parentb10e0e9d5a24c90bfc1a6d2370fcfcf641f3fbc7 (diff)
d3d12: expand rectangle draws through triangle-family pipelines
Select the geometry-stage variant when the input assembler requests an Xbox rectangle and cache it alongside the attachment-format variants. Keep a shared snapshot of the linked vertex program for that draw. Exercise alternating rectangle and triangle draws through the same PSO and compare the complete target after each switch.
-rw-r--r--docs/d3d12_vulkan.md4
-rw-r--r--runtime/shims/d3d12_vulkan_device.cpp67
-rw-r--r--runtime/src/vulkan_replay.h7
-rw-r--r--runtime/test/d3d12_translated_draw_test.cpp30
4 files changed, 106 insertions, 2 deletions
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:
832 `d3d12_state_sweep_test` checks the mapping against the host on the same adapter: 482 full-screen draws over 832 `d3d12_state_sweep_test` checks the mapping against the host on the same adapter: 482 full-screen draws over
833 every blend factor pair, blend operation, write mask, comparison function and stencil operation, with the stencil 833 every blend factor pair, blend operation, write mask, comparison function and stencil operation, with the stencil
834 plane read back, are byte-identical on an RTX 5080 and a Radeon iGPU. 834 plane read back, are byte-identical on an RTX 5080 and a Radeon iGPU.
835 - A triangle-family PSO can draw Xbox rectangle lists. When the input assembler selects topology 17, replay
836 selects a geometry-stage variant that emits the fourth corner after vertex shading. It requires
837 `geometryShader` and a vertex pipeline without an application geometry stage. A later triangle draw uses the
838 original PSO. The translated-draw GPU test switches between both primitives on one PSO and checks every pixel.
835 - **Logic ops** come from `RenderTarget[0]`, as in D3D12, and apply to every target (Vulkan has one per 839 - **Logic ops** come from `RenderTarget[0]`, as in D3D12, and apply to every target (Vulkan has one per
836 pipeline; it skips float formats). **`DepthClipEnable = FALSE`** is depth clamping: D3D12 clamps output depth 840 pipeline; it skips float formats). **`DepthClipEnable = FALSE`** is depth clamping: D3D12 clamps output depth
837 to the viewport's range anyway. 841 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 @@
18#include "../src/arena_size.h" 18#include "../src/arena_size.h"
19#include "../src/gfx10_descriptors.h" 19#include "../src/gfx10_descriptors.h"
20#include "../src/depth24_fragment.h" 20#include "../src/depth24_fragment.h"
21#include "../src/rect_list_geometry.h"
21#include "../src/uav_clear.h" 22#include "../src/uav_clear.h"
22#include "../src/vulkan_bindless.h" 23#include "../src/vulkan_bindless.h"
23#include "../src/vulkan_memory.h" 24#include "../src/vulkan_memory.h"
@@ -803,6 +804,23 @@ struct DeviceState {
803 return shader_library.loaded() ? &shader_library : nullptr; 804 return shader_library.loaded() ? &shader_library : nullptr;
804 } 805 }
805 806
807 // PSOs that differ only in fixed state can share the linked vertex program. The pixel entry distinguishes
808 // f16 interface variants. Compare the actual words as well, and let the last PSO release its snapshot.
809 using VertexWords = std::vector<std::uint32_t>;
810 std::mutex rectangle_vertices_mutex;
811 std::map<std::pair<const leonida::ShaderEntry*, const leonida::ShaderEntry*>,
812 std::weak_ptr<const VertexWords>> rectangle_vertices;
813
814 std::shared_ptr<const VertexWords> retain_rectangle_vertex(const leonida::ShaderEntry* vs,
815 const leonida::ShaderEntry* linked_ps, const VertexWords& words) {
816 std::lock_guard lock(rectangle_vertices_mutex);
817 auto& weak = rectangle_vertices[{vs, linked_ps}];
818 if (auto shared = weak.lock(); shared && *shared == words) return shared;
819 auto shared = std::make_shared<const VertexWords>(words);
820 weak = shared;
821 return shared;
822 }
823
806 bool depth_usable(VkFormat format) const { 824 bool depth_usable(VkFormat format) const {
807 VkFormatProperties properties{}; 825 VkFormatProperties properties{};
808 format_properties(info.physical, format, &properties); 826 format_properties(info.physical, format, &properties);
@@ -3613,6 +3631,15 @@ public:
3613 pixel ? "fragmentStoresAndAtomics" : "vertexPipelineStoresAndAtomics"); 3631 pixel ? "fragmentStoresAndAtomics" : "vertexPipelineStoresAndAtomics");
3614 return E_NOTIMPL; 3632 return E_NOTIMPL;
3615 } 3633 }
3634 const auto topology_bits = std::bit_cast<std::uint32_t>(desc.PrimitiveTopologyType);
3635 if (!mesh && topology_bits != leonida::topology_type_rect_list)
3636 graphics_.rectangle_variant_topology = leonida::topology_rect_list;
3637 if (!mesh && !geometry && device_.info.features.geometryShader &&
3638 topology_bits == D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE) {
3639 const bool linked =
3640 ps_stage.entry && (!ps_stage.entry->f16_halves.empty() || !vs_stage.entry->f16_halves.empty());
3641 rectangle_vertex_ = device_.retain_rectangle_vertex(vs_stage.entry, linked ? ps_stage.entry : nullptr, vs_stage.words);
3642 }
3616 for (Stage& stage : stages) { 3643 for (Stage& stage : stages) {
3617 if (!stage.entry) continue; 3644 if (!stage.entry) continue;
3618 // After the f16 link above, which can also edit the pixel shader's words. 3645 // After the f16 link above, which can also edit the pixel shader's words.
@@ -4179,6 +4206,17 @@ private:
4179 info.pColorBlendState = &blend; 4206 info.pColorBlendState = &blend;
4180 info.pDynamicState = &dynamic; 4207 info.pDynamicState = &dynamic;
4181 info.layout = graphics_.layout; 4208 info.layout = graphics_.layout;
4209 std::vector<VkPipelineShaderStageCreateInfo> rectangle_stages;
4210 if (unbound & leonida::GraphicsPipeline::rectangle_stage) {
4211 rectangle_stages.assign(info.pStages, info.pStages + info.stageCount);
4212 VkPipelineShaderStageCreateInfo geometry{VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
4213 geometry.stage = VK_SHADER_STAGE_GEOMETRY_BIT;
4214 geometry.module = rectangle_module_;
4215 geometry.pName = "main";
4216 rectangle_stages.push_back(geometry);
4217 info.stageCount = static_cast<std::uint32_t>(rectangle_stages.size());
4218 info.pStages = rectangle_stages.data();
4219 }
4182 VkPipeline pipeline = VK_NULL_HANDLE; 4220 VkPipeline pipeline = VK_NULL_HANDLE;
4183 if (device_.context->vk.vkCreateGraphicsPipelines(device_.info.device, device_.pipeline_cache(), 1, 4221 if (device_.context->vk.vkCreateGraphicsPipelines(device_.info.device, device_.pipeline_cache(), 1,
4184 &info, nullptr, &pipeline) != VK_SUCCESS) 4222 &info, nullptr, &pipeline) != VK_SUCCESS)
@@ -4186,10 +4224,37 @@ private:
4186 return pipeline; 4224 return pipeline;
4187 } 4225 }
4188 4226
4227 // Called under variants_mutex_. Attachment combinations reuse the same geometry module.
4228 VkShaderModule rectangle_module() {
4229 if (rectangle_module_ || !rectangle_vertex_) return rectangle_module_;
4230 std::string why;
4231 const auto words = leonida::rect_list_geometry(*rectangle_vertex_, why);
4232 if (words.empty()) {
4233 std::fprintf(stderr, "[rt] d3d12: cannot draw rectangle list: %s\n", why.c_str());
4234 rectangle_vertex_.reset();
4235 return VK_NULL_HANDLE;
4236 }
4237 VkShaderModuleCreateInfo module{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
4238 module.codeSize = words.size() * sizeof(words.front());
4239 module.pCode = words.data();
4240 VkShaderModule handle = VK_NULL_HANDLE;
4241 if (device_.context->vk.vkCreateShaderModule(device_.info.device, &module, nullptr, &handle) != VK_SUCCESS)
4242 return VK_NULL_HANDLE;
4243 modules_.push_back(handle);
4244 rectangle_module_ = handle;
4245 rectangle_vertex_.reset();
4246 return handle;
4247 }
4248
4189 // Built on first use and kept until the PSO is released (replay may call this from any queue). 4249 // Built on first use and kept until the PSO is released (replay may call this from any queue).
4190 VkPipeline variant(std::uint32_t unbound) { 4250 VkPipeline variant(std::uint32_t unbound) {
4191 std::lock_guard lock(variants_mutex_); 4251 std::lock_guard lock(variants_mutex_);
4192 if (const auto it = variants_.find(unbound); it != variants_.end()) return it->second; 4252 if (const auto it = variants_.find(unbound); it != variants_.end()) return it->second;
4253 if ((unbound & leonida::GraphicsPipeline::rectangle_stage) && !rectangle_module()) {
4254 log_once("a rectangle-list draw needs a compatible vertex pipeline and geometryShader; topology",
4255 leonida::topology_rect_list);
4256 return VK_NULL_HANDLE;
4257 }
4193 const VkPipeline pipeline = build(unbound); 4258 const VkPipeline pipeline = build(unbound);
4194 if (!pipeline) { 4259 if (!pipeline) {
4195 log_once("a pipeline variant for unbound attachment slots could not be built; slots", int(unbound)); 4260 log_once("a pipeline variant for unbound attachment slots could not be built; slots", int(unbound));
@@ -4207,6 +4272,8 @@ private:
4207 std::map<std::uint32_t, VkPipeline> variants_; 4272 std::map<std::uint32_t, VkPipeline> variants_;
4208 VkShaderModule module_ = VK_NULL_HANDLE; 4273 VkShaderModule module_ = VK_NULL_HANDLE;
4209 std::vector<VkShaderModule> modules_; 4274 std::vector<VkShaderModule> modules_;
4275 std::shared_ptr<const DeviceState::VertexWords> rectangle_vertex_;
4276 VkShaderModule rectangle_module_ = VK_NULL_HANDLE;
4210 VkDeviceSize code_base_ = 0; 4277 VkDeviceSize code_base_ = 0;
4211 VkDeviceSize code_bytes_ = 0; 4278 VkDeviceSize code_bytes_ = 0;
4212 bool registered_ = false; 4279 bool registered_ = false;
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 {
773 // unbound (D3D12 allows it; dynamic rendering needs the formats to agree). Bit i: colour slot i, bit 8: depth 773 // unbound (D3D12 allows it; dynamic rendering needs the formats to agree). Bit i: colour slot i, bit 8: depth
774 // and stencil. Null when it cannot be built. 774 // and stencil. Null when it cannot be built.
775 static constexpr std::uint32_t unbound_depth = 1u << 8; 775 static constexpr std::uint32_t unbound_depth = 1u << 8;
776 std::function<VkPipeline(std::uint32_t unbound)> variant; 776 static constexpr std::uint32_t rectangle_stage = 1u << 9;
777 // IA topology requiring a fourth-corner geometry stage. Zero when the PSO already contains the stage.
778 std::uint32_t rectangle_variant_topology = 0;
779 std::function<VkPipeline(std::uint32_t flags)> variant;
777 std::uint32_t samples = 1; // rasterizationSamples; every bound attachment must have this count 780 std::uint32_t samples = 1; // rasterizationSamples; every bound attachment must have this count
778}; 781};
779 782
@@ -2160,6 +2163,8 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct
2160 // Slots the pipeline has a format for but the draw leaves unbound take a variant whose format there is 2163 // Slots the pipeline has a format for but the draw leaves unbound take a variant whose format there is
2161 // VK_FORMAT_UNDEFINED: D3D12 discards those writes, Vulkan wants the formats to match the attachments. 2164 // VK_FORMAT_UNDEFINED: D3D12 discards those writes, Vulkan wants the formats to match the attachments.
2162 std::uint32_t unbound = 0; 2165 std::uint32_t unbound = 0;
2166 if (pipeline->rectangle_variant_topology && draw.topology == pipeline->rectangle_variant_topology)
2167 unbound |= GraphicsPipeline::rectangle_stage;
2163 for (std::size_t slot = 0; slot < p.colors.size(); ++slot) 2168 for (std::size_t slot = 0; slot < p.colors.size(); ++slot)
2164 if (pipeline->color_formats[slot] != VK_FORMAT_UNDEFINED && !p.colors[slot].id) unbound |= 1u << slot; 2169 if (pipeline->color_formats[slot] != VK_FORMAT_UNDEFINED && !p.colors[slot].id) unbound |= 1u << slot;
2165 if (pipeline->depth_format != VK_FORMAT_UNDEFINED && !p.depth.id) unbound |= GraphicsPipeline::unbound_depth; 2170 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() {
175 info.queue = d.queue; 175 info.queue = d.queue;
176 info.queue_family = d.family; 176 info.queue_family = d.family;
177 info.buffer_device_address = true; 177 info.buffer_device_address = true;
178 info.features = d.enabled;
178 info.shader_dir = (root_dir / "spirv").string(); 179 info.shader_dir = (root_dir / "spirv").string();
179 ID3D12Device* inner = nullptr; 180 ID3D12Device* inner = nullptr;
180 EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&inner)))); 181 EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&inner))));
@@ -293,7 +294,8 @@ int main() {
293 list->ResourceBarrier(2, barriers); 294 list->ResourceBarrier(2, barriers);
294 // The first command list already contains the vertex upload. Reuse it for subsequent frames 295 // The first command list already contains the vertex upload. Reuse it for subsequent frames
295 // after the preceding submission's fence has completed and its readback is available. 296 // after the preceding submission's fence has completed and its readback is available.
296 const auto render_frame = [&](UINT64 frame, float x_offset, float bias) -> int { 297 const auto render_frame = [&](UINT64 frame, float x_offset, float bias,
298 D3D_PRIMITIVE_TOPOLOGY last_topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST) -> int {
297 if (frame > 1) { 299 if (frame > 1) {
298 EXPECT(SUCCEEDED(allocator->Reset())); 300 EXPECT(SUCCEEDED(allocator->Reset()));
299 EXPECT(SUCCEEDED(list->Reset(allocator.p, pso.p))); 301 EXPECT(SUCCEEDED(list->Reset(allocator.p, pso.p)));
@@ -323,6 +325,7 @@ int main() {
323 {0, {1, -1, 0.125f + bias, 0}, 6}, // bottom right: same V#, other constants 325 {0, {1, -1, 0.125f + bias, 0}, 6}, // bottom right: same V#, other constants
324 {1, {0, 0, 0.25f + bias, 0}, 3}}; // bottom left: the second V# 326 {1, {0, 0, 0.25f + bias, 0}, 3}}; // bottom left: the second V#
325 for (const Draw& draw : draws) { 327 for (const Draw& draw : draws) {
328 if (draw.slot == 1) list->IASetPrimitiveTopology(last_topology);
326 list->SetGraphicsRootDescriptorTable(0, D3D12_GPU_DESCRIPTOR_HANDLE{gpu + std::uint64_t(draw.slot) * step}); 329 list->SetGraphicsRootDescriptorTable(0, D3D12_GPU_DESCRIPTOR_HANDLE{gpu + std::uint64_t(draw.slot) * step});
327 list->SetGraphicsRoot32BitConstants(1, 4, draw.constants, 0); 330 list->SetGraphicsRoot32BitConstants(1, 4, draw.constants, 0);
328 list->DrawInstanced(draw.vertices, 1, 0, 0); 331 list->DrawInstanced(draw.vertices, 1, 0, 0);
@@ -370,6 +373,31 @@ int main() {
370 } 373 }
371#endif 374#endif
372 EXPECT(render_frame(1, 0.0f, 0.0f) == 0); 375 EXPECT(render_frame(1, 0.0f, 0.0f) == 0);
376#ifndef LEONIDA_M15_GUEST
377 if (d.enabled.geometryShader) {
378 // The last draw covers half the lower-left quadrant as a triangle. The Xbox rectangle primitive
379 // fills the quadrant through the same PSO. The other three quadrants keep their original values.
380 const auto triangles = pixels;
381 constexpr auto rectangles = static_cast<D3D_PRIMITIVE_TOPOLOGY>(17);
382 const Pixel lower_left = {0.75f, 0.25f, 0.75f, 1.0f};
383 for (UINT64 frame : {2u, 4u}) {
384 EXPECT(render_frame(frame, 0.0f, 0.0f, rectangles) == 0);
385 for (UINT y = 0; y < target_extent; ++y) {
386 for (UINT x = 0; x < target_extent; ++x) {
387 const auto i = y * target_extent + x;
388 const auto& want = y >= target_extent / 2 && x < target_extent / 2 ? lower_left : triangles[i];
389 if (pixels[i] != want) {
390 std::fprintf(stderr, "rectangle draw mismatch at (%u, %u) in frame %llu\n", x, y,
391 static_cast<unsigned long long>(frame));
392 return 1;
393 }
394 }
395 }
396 EXPECT(render_frame(frame + 1, 0.0f, 0.0f) == 0);
397 EXPECT(pixels == triangles);
398 }
399 }
400#endif
373 for (auto* r : {&vertex_buffer, &upload, &readback, &target}) r->reset(); 401 for (auto* r : {&vertex_buffer, &upload, &readback, &target}) r->reset();
374 pso.reset(); 402 pso.reset();
375 root.reset(); 403 root.reset();