upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorMumogg <104529023+Mumogg@users.noreply.github.com>2026-11-23 14:35:25 -0600
committerMumogg <104529023+Mumogg@users.noreply.github.com>2026-11-23 14:35:25 -0600
commite64653e58599b0372a3e3e4d8909bd652eb704e8 (patch)
tree5585bd2849f0d9ab58a7109c181aae60d82ecd3c /runtime/src
parentd8ba5ff389ba41478bd2733237a359c6ce070a42 (diff)
parent81caaa165312c32d5cb9fe7afd4b36ed4f8b6c9a (diff)
Merge branch 'pr7448' into local/ladder-frame-wine
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/rect_list_geometry.h207
-rw-r--r--runtime/src/vulkan_backend.h8
-rw-r--r--runtime/src/vulkan_replay.h38
3 files changed, 252 insertions, 1 deletions
diff --git a/runtime/src/rect_list_geometry.h b/runtime/src/rect_list_geometry.h
new file mode 100644
index 000000000..15d7368d3
--- /dev/null
+++ b/runtime/src/rect_list_geometry.h
@@ -0,0 +1,207 @@
1#pragma once
2
3// Console rectangle lists on Vulkan, which has no such primitive.
4//
5// A rectangle is three corners (v1, v2, v3) and the hardware makes the fourth, v2 + v3 - v1, for the position and for
6// every interpolant, after the vertex stage. A list topology of rectangles (D3D_PRIMITIVE_TOPOLOGY 17) and a pipeline
7// whose topology type is 19 are console-only. The fact comes from xvalkyrie (GPL-3.0, commit 53af7df77c10, notes in
8// its backlog); no code is taken from it.
9//
10// The fourth vertex depends on what the vertex stage wrote, not on what it read, so the expansion has to sit after
11// the vertex stage: a pre-pass over the vertex buffers is wrong for any shader that is not linear in its inputs (and
12// the translated vertex shaders fetch by V# themselves, so there is no vertex buffer to expand). The pipeline gets a
13// small geometry stage instead, built here from the vertex shader's own SPIR-V: it takes the triangle (v1, v2, v3)
14// the list draws and emits the parallelogram as the two triangles (v1, v2, v4) and (v1, v4, v3), with the winding of
15// (v1, v2, v3). Both start at v1, so a flat input (taken from a triangle's first vertex) is v1's over the whole
16// rectangle. Every Location output of the vertex stage is a vec4 float in the translator's output (rdna2spirv.py),
17// and so is the stage's Position; the generated stage reads and writes exactly those.
18//
19// Not covered, and refused with a reason: a vertex stage that writes ClipDistance, CullDistance, Layer or
20// ViewportIndex, or a Location output that is not a vec4 float.
21//
22// Plain word editing, standard library only.
23
24#include <algorithm>
25#include <array>
26#include <cstdint>
27#include <initializer_list>
28#include <map>
29#include <set>
30#include <string>
31#include <vector>
32
33namespace leonida {
34
35// D3D_PRIMITIVE_TOPOLOGY values and the PSO topology type of the console's rectangle and quad lists.
36constexpr std::uint32_t topology_rect_list = 17;
37constexpr std::uint32_t topology_quad_list = 19;
38constexpr std::uint32_t topology_type_rect_list = 19;
39constexpr std::array<std::uint32_t, 6> quad_triangle_indices{0, 1, 2, 0, 2, 3};
40
41// The geometry stage for `vertex_words`' outputs; empty, with `why` set, when the vertex stage cannot be expanded.
42inline std::vector<std::uint32_t> rect_list_geometry(const std::vector<std::uint32_t>& vertex_words, std::string& why) {
43 enum : std::uint32_t {
44 op_entry_point = 15, op_type_void = 19, op_type_int = 21, op_type_float = 22,
45 op_type_vector = 23, op_type_array = 28, op_type_pointer = 32, op_type_function = 33, op_constant = 43,
46 op_function = 54, op_function_end = 56, op_variable = 59, op_load = 61, op_store = 62, op_access_chain = 65,
47 op_decorate = 71, op_fadd = 129, op_fsub = 131, op_emit_vertex = 218, op_end_primitive = 219, op_label = 248, op_return = 253,
48 op_capability = 17, op_memory_model = 14, op_execution_mode = 16,
49 deco_builtin = 11, deco_location = 30, builtin_position = 0, builtin_point_size = 1,
50 storage_input = 1, storage_output = 3
51 };
52 why.clear();
53 if (vertex_words.size() < 6 || vertex_words[0] != 0x07230203u) {
54 why = "the vertex shader is not a SPIR-V module";
55 return {};
56 }
57 // What the vertex stage declares: types, Output variables, and the decorations that name them.
58 struct Type { std::uint32_t op = 0, component = 0, count = 0, width = 0; };
59 std::map<std::uint32_t, Type> types;
60 std::map<std::uint32_t, std::uint32_t> variable_type; // Output variable -> its pointer type
61 std::map<std::uint32_t, std::uint32_t> location, builtin; // variable id -> value
62 std::set<std::uint32_t> outputs;
63 for (std::size_t i = 5; i < vertex_words.size();) {
64 const std::uint32_t op = vertex_words[i] & 0xffffu, n = vertex_words[i] >> 16;
65 if (!n || i + n > vertex_words.size()) {
66 why = "the vertex shader's SPIR-V is cut short";
67 return {};
68 }
69 const std::uint32_t* w = &vertex_words[i];
70 if (op == op_type_float && n >= 3) types[w[1]] = {op, 0, 0, w[2]};
71 else if (op == op_type_vector && n >= 4) types[w[1]] = {op, w[2], w[3], 0};
72 else if (op == op_type_pointer && n >= 4) types[w[1]] = {op, w[3], w[2], 0};
73 else if (op == op_variable && n >= 4 && w[3] == storage_output) outputs.insert(w[2]), variable_type[w[2]] = w[1];
74 else if (op == op_decorate && n >= 4 && w[2] == deco_location) location[w[1]] = w[3];
75 else if (op == op_decorate && n >= 4 && w[2] == deco_builtin) builtin[w[1]] = w[3];
76 i += n;
77 }
78 std::uint32_t position = 0;
79 std::vector<std::uint32_t> params; // Location values, ascending
80 for (std::uint32_t id : outputs) {
81 const auto b = builtin.find(id);
82 if (b != builtin.end()) {
83 if (b->second == builtin_position) position = id;
84 else if (b->second != builtin_point_size) {
85 why = "the vertex shader writes ClipDistance, CullDistance, Layer or ViewportIndex";
86 return {};
87 }
88 continue;
89 }
90 const auto l = location.find(id);
91 if (l == location.end()) continue;
92 const Type& ptr = types[variable_type[id]];
93 const Type& vec = types[ptr.component];
94 const Type& scalar = types[vec.component];
95 // Known limit: every vec4 float output gets the plain v2 + v3 - v1 corner. The translator can export an attribute
96 // as the register's raw bits with two packed f16 halves (only the pixel shader knows, through per_vertex in
97 // tools/rdna2spirv.py), and nothing on the vertex side tells those apart, so such an output would get a wrong
98 // fourth corner. Fixing it needs the pixel shader's words here and a per-channel marker from the translator.
99 if (vec.op != op_type_vector || vec.count != 4 || scalar.op != op_type_float || scalar.width != 32) {
100 why = "a vertex output is not a vec4 float";
101 return {};
102 }
103 params.push_back(l->second);
104 }
105 if (!position) {
106 why = "the vertex shader writes no Position";
107 return {};
108 }
109 std::sort(params.begin(), params.end());
110 params.erase(std::unique(params.begin(), params.end()), params.end());
111
112 // The module: a fixed prelude, then per variable ids, then the body. Ids count up from 1.
113 std::uint32_t next = 1;
114 const auto id = [&] { return next++; };
115 std::vector<std::uint32_t> head, annotations, globals, body;
116 const auto emit = [](std::vector<std::uint32_t>& to, std::uint32_t op, std::initializer_list<std::uint32_t> args) {
117 to.push_back((std::uint32_t(args.size()) + 1) << 16 | op);
118 to.insert(to.end(), args.begin(), args.end());
119 };
120 const std::uint32_t t_void = id(), t_fn = id(), t_uint = id(), t_float = id(), t_v4 = id(),
121 c3 = id(), t_arr = id(), t_pin_arr = id(), t_pin_v4 = id(),
122 t_pout_v4 = id(), c0 = id(), c1 = id(), c2 = id();
123 emit(globals, op_type_void, {t_void});
124 emit(globals, op_type_function, {t_fn, t_void});
125 emit(globals, op_type_int, {t_uint, 32, 0});
126 emit(globals, op_type_float, {t_float, 32});
127 emit(globals, op_type_vector, {t_v4, t_float, 4});
128 emit(globals, op_constant, {t_uint, c3, 3});
129 emit(globals, op_constant, {t_uint, c0, 0});
130 emit(globals, op_constant, {t_uint, c1, 1});
131 emit(globals, op_constant, {t_uint, c2, 2});
132 emit(globals, op_type_array, {t_arr, t_v4, c3});
133 emit(globals, op_type_pointer, {t_pin_arr, storage_input, t_arr});
134 emit(globals, op_type_pointer, {t_pin_v4, storage_input, t_v4});
135 emit(globals, op_type_pointer, {t_pout_v4, storage_output, t_v4});
136
137 struct Var { std::uint32_t in, out; bool is_position; };
138 std::vector<Var> vars;
139 vars.push_back({id(), id(), true});
140 for (std::size_t i = 0; i < params.size(); ++i) vars.push_back({id(), id(), false});
141 std::vector<std::uint32_t> io_vars;
142 emit(annotations, op_decorate, {vars[0].in, deco_builtin, builtin_position});
143 emit(annotations, op_decorate, {vars[0].out, deco_builtin, builtin_position});
144 for (std::size_t i = 0; i < params.size(); ++i) {
145 emit(annotations, op_decorate, {vars[i + 1].in, deco_location, params[i]});
146 emit(annotations, op_decorate, {vars[i + 1].out, deco_location, params[i]});
147 }
148 for (const Var& v : vars) {
149 emit(globals, op_variable, {t_pin_arr, v.in, storage_input});
150 emit(globals, op_variable, {t_pout_v4, v.out, storage_output});
151 io_vars.push_back(v.in);
152 io_vars.push_back(v.out);
153 }
154
155 const std::uint32_t main_fn = id(), entry_label = id();
156 emit(body, op_function, {t_void, main_fn, 0, t_fn});
157 emit(body, op_label, {entry_label});
158 // Per variable: the three corners, and the fourth, v2 + v3 - v1.
159 struct Corners { std::uint32_t v[4]; };
160 std::vector<Corners> corners;
161 const std::uint32_t cs[3] = {c0, c1, c2};
162 for (const Var& v : vars) {
163 Corners c{};
164 for (int k = 0; k < 3; ++k) {
165 const std::uint32_t ptr = id();
166 c.v[k] = id();
167 emit(body, op_access_chain, {t_pin_v4, ptr, v.in, cs[k]});
168 emit(body, op_load, {t_v4, c.v[k], ptr});
169 }
170 const std::uint32_t sum = id(), diff = id();
171 emit(body, op_fadd, {t_v4, sum, c.v[1], c.v[2]});
172 emit(body, op_fsub, {t_v4, diff, sum, c.v[0]});
173 c.v[3] = diff;
174 corners.push_back(c);
175 }
176 // Two triangles, (v1, v2, v4) and (v1, v4, v3): the diagonal is v1 to v4, so both start at v1 and a flat input
177 // (which Vulkan takes from a triangle's first vertex) is v1's over the whole rectangle. The attributes are
178 // affine over the parallelogram, so the split does not change any interpolated value.
179 const int triangles[2][3] = {{0, 1, 3}, {0, 3, 2}};
180 for (const auto& triangle : triangles) {
181 for (int k : triangle) {
182 for (std::size_t i = 0; i < vars.size(); ++i) emit(body, op_store, {vars[i].out, corners[i].v[k]});
183 emit(body, op_emit_vertex, {});
184 }
185 emit(body, op_end_primitive, {});
186 }
187 emit(body, op_return, {});
188 emit(body, op_function_end, {});
189
190 std::vector<std::uint32_t> out{0x07230203u, 0x00010000u, 0, next, 0};
191 emit(out, op_capability, {2}); // Geometry
192 emit(out, op_memory_model, {0, 1}); // Logical, GLSL450
193 std::vector<std::uint32_t> entry{3, main_fn, 0x6e69616du, 0}; // Geometry, %main, "main"
194 entry.insert(entry.end(), io_vars.begin(), io_vars.end());
195 out.push_back((std::uint32_t(entry.size()) + 1) << 16 | op_entry_point);
196 out.insert(out.end(), entry.begin(), entry.end());
197 emit(out, op_execution_mode, {main_fn, 22}); // Triangles
198 emit(out, op_execution_mode, {main_fn, 0, 1}); // Invocations 1
199 emit(out, op_execution_mode, {main_fn, 29}); // OutputTriangleStrip
200 emit(out, op_execution_mode, {main_fn, 26, 6}); // OutputVertices 6
201 out.insert(out.end(), annotations.begin(), annotations.end());
202 out.insert(out.end(), globals.begin(), globals.end());
203 out.insert(out.end(), body.begin(), body.end());
204 return out;
205}
206
207} // namespace leonida
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 73d830ac1..77c8827b0 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -1247,6 +1247,14 @@ public:
1247 replay_.zero_size = size; 1247 replay_.zero_size = size;
1248 } 1248 }
1249 1249
1250 // Pre-expanded 32-bit triangle indices for non-indexed quad lists (ReplayFunctions::quad_indices).
1251 void set_quad_indices(VkBuffer buffer, VkDeviceSize offset, std::uint32_t count) {
1252 std::unique_lock lock(resource_mutex_);
1253 replay_.quad_indices = buffer;
1254 replay_.quad_indices_offset = offset;
1255 replay_.quad_index_count = count;
1256 }
1257
1250 // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears. 1258 // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears.
1251 void set_color_attachment_format(std::function<bool(VkFormat)> check) { 1259 void set_color_attachment_format(std::function<bool(VkFormat)> check) {
1252 std::unique_lock lock(resource_mutex_); 1260 std::unique_lock lock(resource_mutex_);
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index 37728ffc2..af58efbbd 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -32,6 +32,7 @@
32 32
33#include "command_list.h" 33#include "command_list.h"
34#include "depth24.h" 34#include "depth24.h"
35#include "rect_list_geometry.h"
35#include "uav_clear.h" 36#include "uav_clear.h"
36 37
37#include <vulkan/vulkan.h> 38#include <vulkan/vulkan.h>
@@ -43,6 +44,7 @@
43#include <array> 44#include <array>
44#include <cmath> 45#include <cmath>
45#include <cstdint> 46#include <cstdint>
47#include <cstdio>
46#include <functional> 48#include <functional>
47#include <limits> 49#include <limits>
48#include <map> 50#include <map>
@@ -873,6 +875,10 @@ struct ReplayFunctions {
873 VkBuffer zero_buffer = VK_NULL_HANDLE; 875 VkBuffer zero_buffer = VK_NULL_HANDLE;
874 VkDeviceSize zero_offset = 0; 876 VkDeviceSize zero_offset = 0;
875 VkDeviceSize zero_size = 0; 877 VkDeviceSize zero_size = 0;
878 // 32-bit triangle indices for non-indexed quad lists: 0, 1, 2, 0, 2, 3, then the same pattern for each quad.
879 VkBuffer quad_indices = VK_NULL_HANDLE;
880 VkDeviceSize quad_indices_offset = 0;
881 std::uint32_t quad_index_count = 0;
876 std::uint64_t heap_base = 0; // device address of the GPU address arena (push-constant dwords 32-33) 882 std::uint64_t heap_base = 0; // device address of the GPU address arena (push-constant dwords 32-33)
877 // Resolves available binary-occlusion queries to 64-bit 0/1 values, outside conditional rendering. 883 // Resolves available binary-occlusion queries to 64-bit 0/1 values, outside conditional rendering.
878 std::function<void(VkCommandBuffer, VkQueryPool, std::uint32_t first, std::uint32_t count, 884 std::function<void(VkCommandBuffer, VkQueryPool, std::uint32_t first, std::uint32_t count,
@@ -2187,6 +2193,9 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct
2187 p.vertex_sizes[slot] = size; 2193 p.vertex_sizes[slot] = size;
2188 p.vertex_strides[slot] = (pipeline->zero_step_slots & (1u << slot)) ? 0 : view.stride; 2194 p.vertex_strides[slot] = (pipeline->zero_step_slots & (1u << slot)) ? 0 : view.stride;
2189 } 2195 }
2196 // A non-indexed quad list is drawn from the shared triangle index buffer (emit).
2197 if (!draw.indexed && !draw.mesh && draw.topology == topology_quad_list && !vk.quad_indices)
2198 return ReplayResult::unresolved_resource;
2190 if (draw.indexed) { 2199 if (draw.indexed) {
2191 if (draw.index.format == 57) p.index_type = VK_INDEX_TYPE_UINT16; 2200 if (draw.index.format == 57) p.index_type = VK_INDEX_TYPE_UINT16;
2192 else if (draw.index.format == 42) p.index_type = VK_INDEX_TYPE_UINT32; 2201 else if (draw.index.format == 42) p.index_type = VK_INDEX_TYPE_UINT32;
@@ -2221,6 +2230,10 @@ inline VkPrimitiveTopology vulkan_topology(std::uint32_t topology) {
2221 case 11: return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY; 2230 case 11: return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY;
2222 case 12: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY; 2231 case 12: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY;
2223 case 13: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY; 2232 case 13: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY;
2233 // Rectangles draw as triangle lists through a geometry stage. Indexed quads draw as separate fans;
2234 // emit() overrides non-indexed quads to a triangle list backed by the shared indices.
2235 case topology_rect_list: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
2236 case topology_quad_list: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
2224 default: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; 2237 default: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
2225 } 2238 }
2226} 2239}
@@ -4103,7 +4116,8 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
4103 // A mesh pipeline has no input assembly: its topology comes from the shader, and neither the topology 4116 // A mesh pipeline has no input assembly: its topology comes from the shader, and neither the topology
4104 // nor primitive restart is dynamic state there (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065). 4117 // nor primitive restart is dynamic state there (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065).
4105 if (!draw->mesh) { 4118 if (!draw->mesh) {
4106 const VkPrimitiveTopology topology = detail::vulkan_topology(draw->topology); 4119 const VkPrimitiveTopology topology = draw->topology == topology_quad_list && !draw->indexed
4120 ? VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST : detail::vulkan_topology(draw->topology);
4107 vk.set_primitive_topology(command_buffer, topology); 4121 vk.set_primitive_topology(command_buffer, topology);
4108 const bool restart = draw->indexed && detail::strip_topology(topology) && 4122 const bool restart = draw->indexed && detail::strip_topology(topology) &&
4109 ((graphics.strip_cut == 1 && d.index_type == VK_INDEX_TYPE_UINT16) || 4123 ((graphics.strip_cut == 1 && d.index_type == VK_INDEX_TYPE_UINT16) ||
@@ -4120,6 +4134,28 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
4120 } 4134 }
4121 if (draw->mesh) { 4135 if (draw->mesh) {
4122 vk.draw_mesh_tasks(command_buffer, draw->count, draw->instances, draw->first); 4136 vk.draw_mesh_tasks(command_buffer, draw->count, draw->instances, draw->first);
4137 } else if (draw->topology == topology_quad_list) {
4138 if (draw->indexed) {
4139 // Indexed quads still read their vertices from the application's index buffer, one fan per quad.
4140 vk.bind_index_buffer(command_buffer, d.index_buffer, d.index_offset, d.index_type);
4141 for (std::uint32_t quad = 0; quad < draw->count / 4; ++quad)
4142 vk.draw_indexed(command_buffer, 4, draw->instances, draw->first + 4 * quad,
4143 draw->base_vertex, draw->first_instance);
4144 } else {
4145 // Both triangles start at the quad's first vertex for flat inputs. One draw also preserves
4146 // the original instance order, and VertexIndex - BaseVertex still gives 4q + k.
4147 constexpr std::uint32_t indices_per_quad = quad_triangle_indices.size();
4148 const std::uint32_t wanted = draw->count / 4;
4149 const std::uint32_t quads = std::min(wanted, vk.quad_index_count / indices_per_quad);
4150 if (quads < wanted)
4151 std::fprintf(stderr, "[rt] d3d12: a non-indexed quad list of %u quads draws only the first %u\n",
4152 wanted, quads);
4153 if (quads) {
4154 vk.bind_index_buffer(command_buffer, vk.quad_indices, vk.quad_indices_offset, VK_INDEX_TYPE_UINT32);
4155 vk.draw_indexed(command_buffer, quads * indices_per_quad, draw->instances, 0,
4156 std::int32_t(draw->first), draw->first_instance);
4157 }
4158 }
4123 } else if (draw->indexed) { 4159 } else if (draw->indexed) {
4124 vk.bind_index_buffer(command_buffer, d.index_buffer, d.index_offset, d.index_type); 4160 vk.bind_index_buffer(command_buffer, d.index_buffer, d.index_offset, d.index_type);
4125 vk.draw_indexed(command_buffer, draw->count, draw->instances, draw->first, draw->base_vertex, 4161 vk.draw_indexed(command_buffer, draw->count, draw->instances, draw->first, draw->base_vertex,