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:
authorRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 20:36:55 +0800
committerRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 20:49:20 +0800
commitb10e0e9d5a24c90bfc1a6d2370fcfcf641f3fbc7 (patch)
tree534a4a17b4facac4b248366d00bac6a0e72a5011 /runtime/src
parent51f31609236a952badeb9c9aba9efc2a6e4dc2ff (diff)
runtime: reuse the Xbox rectangle geometry generator from #682
Carry only runtime/src/rect_list_geometry.h, byte-for-byte from 6aa8eb11c. The stage generator and its original commits are by tedin7 (#207, carried forward in #682). This makes the draw-time change buildable against main while the explicit rectangle/quad work is in review.
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/rect_list_geometry.h268
1 files changed, 268 insertions, 0 deletions
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 @@
1#pragma once
2
3// Console rectangle lists on Vulkan, which has no such primitive.
4//
5// A rectangle is three corners and the hardware makes the fourth, a + b - p (p the right-angle corner, a and b its
6// neighbours), for the position and for every interpolant, after the vertex stage. Which vertex is p is not fixed:
7// the retail boot's full-screen rectangle has it at the second vertex. The stage picks the vertex opposite the longest
8// screen-space edge, and the first vertex on a tie. A list topology of rectangles (D3D_PRIMITIVE_TOPOLOGY 17) and a pipeline
9// whose topology type is 19 are console-only. The fact comes from xvalkyrie (GPL-3.0, commit 53af7df77c10, notes in
10// its backlog); no code is taken from it.
11//
12// The fourth vertex depends on what the vertex stage wrote, not on what it read, so the expansion has to sit after
13// the vertex stage: a pre-pass over the vertex buffers is wrong for any shader that is not linear in its inputs (and
14// the translated vertex shaders fetch by V# themselves, so there is no vertex buffer to expand). The pipeline gets a
15// small geometry stage instead, built here from the vertex shader's own SPIR-V: it takes the triangle (v1, v2, v3)
16// the list draws and emits the parallelogram as the two triangles (p, a, f) and (p, f, b), with the winding of
17// (v1, v2, v3). Both start at p, so a flat input (taken from a triangle's first vertex) is p's over the whole
18// rectangle. Every Location output of the vertex stage is a vec4 float in the translator's output (rdna2spirv.py),
19// and so is the stage's Position; the generated stage reads and writes exactly those.
20//
21// The edges are measured in normalized device coordinates, not pixels (the stage does not know the viewport). For the
22// usual axis-aligned rectangle that is exact; a rotated rectangle drawn into a very non-square viewport can pick a
23// different corner than the pixel-space right angle.
24//
25// Not covered, and refused with a reason: a vertex stage that writes ClipDistance, CullDistance, Layer or
26// ViewportIndex, or a Location output that is not a vec4 float.
27//
28// Plain word editing, standard library only.
29
30#include <algorithm>
31#include <array>
32#include <cstdint>
33#include <initializer_list>
34#include <map>
35#include <set>
36#include <string>
37#include <vector>
38
39namespace leonida {
40
41// D3D_PRIMITIVE_TOPOLOGY values and the PSO topology type of the console's rectangle and quad lists.
42constexpr std::uint32_t topology_rect_list = 17;
43constexpr std::uint32_t topology_quad_list = 19;
44constexpr std::uint32_t topology_type_rect_list = 19;
45constexpr std::array<std::uint32_t, 6> quad_triangle_indices{0, 1, 2, 0, 2, 3};
46
47// The geometry stage for `vertex_words`' outputs; empty, with `why` set, when the vertex stage cannot be expanded.
48inline std::vector<std::uint32_t> rect_list_geometry(const std::vector<std::uint32_t>& vertex_words, std::string& why) {
49 enum : std::uint32_t {
50 op_entry_point = 15, op_type_void = 19, op_type_int = 21, op_type_float = 22,
51 op_type_vector = 23, op_type_array = 28, op_type_pointer = 32, op_type_function = 33, op_constant = 43,
52 op_function = 54, op_function_end = 56, op_variable = 59, op_load = 61, op_store = 62, op_access_chain = 65,
53 op_decorate = 71, op_fadd = 129, op_fsub = 131, op_fmul = 133, op_fdiv = 136,
54 op_type_bool = 20, op_composite_construct = 80, op_composite_extract = 81, op_logical_and = 167,
55 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,
56 op_capability = 17, op_memory_model = 14, op_execution_mode = 16,
57 deco_builtin = 11, deco_location = 30, builtin_position = 0, builtin_point_size = 1,
58 storage_input = 1, storage_output = 3
59 };
60 why.clear();
61 if (vertex_words.size() < 6 || vertex_words[0] != 0x07230203u) {
62 why = "the vertex shader is not a SPIR-V module";
63 return {};
64 }
65 // What the vertex stage declares: types, Output variables, and the decorations that name them.
66 struct Type { std::uint32_t op = 0, component = 0, count = 0, width = 0; };
67 std::map<std::uint32_t, Type> types;
68 std::map<std::uint32_t, std::uint32_t> variable_type; // Output variable -> its pointer type
69 std::map<std::uint32_t, std::uint32_t> location, builtin; // variable id -> value
70 std::set<std::uint32_t> outputs;
71 for (std::size_t i = 5; i < vertex_words.size();) {
72 const std::uint32_t op = vertex_words[i] & 0xffffu, n = vertex_words[i] >> 16;
73 if (!n || i + n > vertex_words.size()) {
74 why = "the vertex shader's SPIR-V is cut short";
75 return {};
76 }
77 const std::uint32_t* w = &vertex_words[i];
78 if (op == op_type_float && n >= 3) types[w[1]] = {op, 0, 0, w[2]};
79 else if (op == op_type_vector && n >= 4) types[w[1]] = {op, w[2], w[3], 0};
80 else if (op == op_type_pointer && n >= 4) types[w[1]] = {op, w[3], w[2], 0};
81 else if (op == op_variable && n >= 4 && w[3] == storage_output) outputs.insert(w[2]), variable_type[w[2]] = w[1];
82 else if (op == op_decorate && n >= 4 && w[2] == deco_location) location[w[1]] = w[3];
83 else if (op == op_decorate && n >= 4 && w[2] == deco_builtin) builtin[w[1]] = w[3];
84 i += n;
85 }
86 std::uint32_t position = 0;
87 std::vector<std::uint32_t> params; // Location values, ascending
88 for (std::uint32_t id : outputs) {
89 const auto b = builtin.find(id);
90 if (b != builtin.end()) {
91 if (b->second == builtin_position) position = id;
92 else if (b->second != builtin_point_size) {
93 why = "the vertex shader writes ClipDistance, CullDistance, Layer or ViewportIndex";
94 return {};
95 }
96 continue;
97 }
98 const auto l = location.find(id);
99 if (l == location.end()) continue;
100 const Type& ptr = types[variable_type[id]];
101 const Type& vec = types[ptr.component];
102 const Type& scalar = types[vec.component];
103 // Known limit: every vec4 float output gets the plain a + b - p corner. The translator can export an attribute
104 // as the register's raw bits with two packed f16 halves (only the pixel shader knows, through per_vertex in
105 // tools/rdna2spirv.py), and nothing on the vertex side tells those apart, so such an output would get a wrong
106 // fourth corner. Fixing it needs the pixel shader's words here and a per-channel marker from the translator.
107 if (vec.op != op_type_vector || vec.count != 4 || scalar.op != op_type_float || scalar.width != 32) {
108 why = "a vertex output is not a vec4 float";
109 return {};
110 }
111 params.push_back(l->second);
112 }
113 if (!position) {
114 why = "the vertex shader writes no Position";
115 return {};
116 }
117 std::sort(params.begin(), params.end());
118 params.erase(std::unique(params.begin(), params.end()), params.end());
119
120 // The module: a fixed prelude, then per variable ids, then the body. Ids count up from 1.
121 std::uint32_t next = 1;
122 const auto id = [&] { return next++; };
123 std::vector<std::uint32_t> head, annotations, globals, body;
124 const auto emit = [](std::vector<std::uint32_t>& to, std::uint32_t op, std::initializer_list<std::uint32_t> args) {
125 to.push_back((std::uint32_t(args.size()) + 1) << 16 | op);
126 to.insert(to.end(), args.begin(), args.end());
127 };
128 const std::uint32_t t_void = id(), t_fn = id(), t_uint = id(), t_float = id(), t_v4 = id(),
129 c3 = id(), t_arr = id(), t_pin_arr = id(), t_pin_v4 = id(),
130 t_pout_v4 = id(), c0 = id(), c1 = id(), c2 = id(), t_bool = id(), t_bool4 = id();
131 emit(globals, op_type_void, {t_void});
132 emit(globals, op_type_function, {t_fn, t_void});
133 emit(globals, op_type_int, {t_uint, 32, 0});
134 emit(globals, op_type_float, {t_float, 32});
135 emit(globals, op_type_vector, {t_v4, t_float, 4});
136 emit(globals, op_type_bool, {t_bool});
137 emit(globals, op_type_vector, {t_bool4, t_bool, 4});
138 emit(globals, op_constant, {t_uint, c3, 3});
139 emit(globals, op_constant, {t_uint, c0, 0});
140 emit(globals, op_constant, {t_uint, c1, 1});
141 emit(globals, op_constant, {t_uint, c2, 2});
142 emit(globals, op_type_array, {t_arr, t_v4, c3});
143 emit(globals, op_type_pointer, {t_pin_arr, storage_input, t_arr});
144 emit(globals, op_type_pointer, {t_pin_v4, storage_input, t_v4});
145 emit(globals, op_type_pointer, {t_pout_v4, storage_output, t_v4});
146
147 struct Var { std::uint32_t in, out; bool is_position; };
148 std::vector<Var> vars;
149 vars.push_back({id(), id(), true});
150 for (std::size_t i = 0; i < params.size(); ++i) vars.push_back({id(), id(), false});
151 std::vector<std::uint32_t> io_vars;
152 emit(annotations, op_decorate, {vars[0].in, deco_builtin, builtin_position});
153 emit(annotations, op_decorate, {vars[0].out, deco_builtin, builtin_position});
154 for (std::size_t i = 0; i < params.size(); ++i) {
155 emit(annotations, op_decorate, {vars[i + 1].in, deco_location, params[i]});
156 emit(annotations, op_decorate, {vars[i + 1].out, deco_location, params[i]});
157 }
158 for (const Var& v : vars) {
159 emit(globals, op_variable, {t_pin_arr, v.in, storage_input});
160 emit(globals, op_variable, {t_pout_v4, v.out, storage_output});
161 io_vars.push_back(v.in);
162 io_vars.push_back(v.out);
163 }
164
165 const std::uint32_t main_fn = id(), entry_label = id();
166 emit(body, op_function, {t_void, main_fn, 0, t_fn});
167 emit(body, op_label, {entry_label});
168 // Per variable: the three vertices as drawn, loaded first.
169 struct Corners { std::uint32_t v[4]; }; // v[0] the right-angle corner, v[1], v[2] its neighbours, v[3] the fourth
170 std::vector<Corners> corners;
171 std::vector<std::array<std::uint32_t, 3>> loaded;
172 const std::uint32_t cs[3] = {c0, c1, c2};
173 for (const Var& v : vars) {
174 std::array<std::uint32_t, 3> three{};
175 for (int k = 0; k < 3; ++k) {
176 const std::uint32_t ptr = id();
177 three[k] = id();
178 emit(body, op_access_chain, {t_pin_v4, ptr, v.in, cs[k]});
179 emit(body, op_load, {t_v4, three[k], ptr});
180 }
181 loaded.push_back(three);
182 }
183 // Which vertex is the right angle: the one opposite the longest edge, measured on screen (position.xy / position.w;
184 // a right triangle's longest edge is its hypotenuse). Ties go to the first vertex, which keeps the plain
185 // "v1 is the corner" order for every rectangle whose right angle is there. A triangle that is not a right
186 // triangle gets the same rule, so the choice is at least consistent.
187 std::uint32_t screen[3][2];
188 for (int k = 0; k < 3; ++k) {
189 const std::uint32_t x = id(), y = id(), w = id();
190 emit(body, op_composite_extract, {t_float, x, loaded[0][k], 0});
191 emit(body, op_composite_extract, {t_float, y, loaded[0][k], 1});
192 emit(body, op_composite_extract, {t_float, w, loaded[0][k], 3});
193 screen[k][0] = id();
194 screen[k][1] = id();
195 emit(body, op_fdiv, {t_float, screen[k][0], x, w});
196 emit(body, op_fdiv, {t_float, screen[k][1], y, w});
197 }
198 const auto squared_edge = [&](int a, int b) {
199 const std::uint32_t dx = id(), dy = id(), xx = id(), yy = id(), sum = id();
200 emit(body, op_fsub, {t_float, dx, screen[a][0], screen[b][0]});
201 emit(body, op_fsub, {t_float, dy, screen[a][1], screen[b][1]});
202 emit(body, op_fmul, {t_float, xx, dx, dx});
203 emit(body, op_fmul, {t_float, yy, dy, dy});
204 emit(body, op_fadd, {t_float, sum, xx, yy});
205 return sum;
206 };
207 const std::uint32_t e01 = squared_edge(0, 1), e02 = squared_edge(0, 2), e12 = squared_edge(1, 2);
208 const std::uint32_t g12_01 = id(), g12_02 = id(), at_first = id(), g02_01 = id(), not_first = id(), at_second = id();
209 emit(body, op_ford_greater_equal, {t_bool, g12_01, e12, e01});
210 emit(body, op_ford_greater_equal, {t_bool, g12_02, e12, e02});
211 emit(body, op_logical_and, {t_bool, at_first, g12_01, g12_02}); // the edge opposite v1 is longest: v1 is the corner
212 emit(body, op_ford_greater_equal, {t_bool, g02_01, e02, e01});
213 emit(body, op_logical_not, {t_bool, not_first, at_first});
214 emit(body, op_logical_and, {t_bool, at_second, not_first, g02_01}); // else the edge opposite v2: v2; else v3
215 const std::uint32_t first4 = id(), second4 = id();
216 emit(body, op_composite_construct, {t_bool4, first4, at_first, at_first, at_first, at_first});
217 emit(body, op_composite_construct, {t_bool4, second4, at_second, at_second, at_second, at_second});
218 // The corner p, its neighbours a = next and b = the one after (the same cyclic order as v1 v2 v3, so the winding
219 // is the drawn triangle's), and the fourth corner a + b - p, for the position and for every interpolant alike.
220 for (const auto& three : loaded) {
221 Corners c{};
222 const auto pick = [&](std::uint32_t first, std::uint32_t second, std::uint32_t third) {
223 const std::uint32_t inner = id(), outer = id();
224 emit(body, op_select, {t_v4, inner, second4, second, third});
225 emit(body, op_select, {t_v4, outer, first4, first, inner});
226 return outer;
227 };
228 c.v[0] = pick(three[0], three[1], three[2]);
229 c.v[1] = pick(three[1], three[2], three[0]);
230 c.v[2] = pick(three[2], three[0], three[1]);
231 const std::uint32_t sum = id(), diff = id();
232 emit(body, op_fadd, {t_v4, sum, c.v[1], c.v[2]});
233 emit(body, op_fsub, {t_v4, diff, sum, c.v[0]});
234 c.v[3] = diff;
235 corners.push_back(c);
236 }
237 // Two triangles, (p, a, f) and (p, f, b): the diagonal is p to f, so both start at p and a flat input
238 // (which Vulkan takes from a triangle's first vertex) is p's over the whole rectangle. The attributes are
239 // affine over the parallelogram, so the split does not change any interpolated value.
240 const int triangles[2][3] = {{0, 1, 3}, {0, 3, 2}};
241 for (const auto& triangle : triangles) {
242 for (int k : triangle) {
243 for (std::size_t i = 0; i < vars.size(); ++i) emit(body, op_store, {vars[i].out, corners[i].v[k]});
244 emit(body, op_emit_vertex, {});
245 }
246 emit(body, op_end_primitive, {});
247 }
248 emit(body, op_return, {});
249 emit(body, op_function_end, {});
250
251 std::vector<std::uint32_t> out{0x07230203u, 0x00010000u, 0, next, 0};
252 emit(out, op_capability, {2}); // Geometry
253 emit(out, op_memory_model, {0, 1}); // Logical, GLSL450
254 std::vector<std::uint32_t> entry{3, main_fn, 0x6e69616du, 0}; // Geometry, %main, "main"
255 entry.insert(entry.end(), io_vars.begin(), io_vars.end());
256 out.push_back((std::uint32_t(entry.size()) + 1) << 16 | op_entry_point);
257 out.insert(out.end(), entry.begin(), entry.end());
258 emit(out, op_execution_mode, {main_fn, 22}); // Triangles
259 emit(out, op_execution_mode, {main_fn, 0, 1}); // Invocations 1
260 emit(out, op_execution_mode, {main_fn, 29}); // OutputTriangleStrip
261 emit(out, op_execution_mode, {main_fn, 26, 6}); // OutputVertices 6
262 out.insert(out.end(), annotations.begin(), annotations.end());
263 out.insert(out.end(), globals.begin(), globals.end());
264 out.insert(out.end(), body.begin(), body.end());
265 return out;
266}
267
268} // namespace leonida