Files
mandy/runtime/test/rect_list_gs_tool.cpp
NotRexed 1e7b057ef2 runtime: rectangle lists (topology type 19) through a generated geometry stage
The title draws its full-screen passes and tiled lighting as D3D12.X rectangle
lists (pipeline topology type 19, list topology 17), which the Vulkan layer
refused, so those draws were lost. Ported from VIctory (GPL-3.0-or-later,
Marvisak and contributors, DexNom fork; victory-shader emit/rect.rs, 683ad7a99):

- src/rect_list_gs.h/.cpp read the vertex shader's SPIR-V output interface
  (Location outputs of 32-bit float/int/uint, Position, clip/cull distances when
  the shader declares their capability; separate built-ins or glslang's
  gl_PerVertex block, mirrored) and emit a SPIR-V 1.0 geometry shader, cached
  per output signature. VIctory's rule: the corner is the vertex sharing x with
  one other and y with the other; the fourth vertex is a + b - corner for every
  float output (exactly b or a where a or b has the corner's bits), integer
  outputs keep vertex 0's; unequal or non-positive w, coincident vertices or no
  corner stay a triangle. Two triangles, both led by vertex 0 (the provoking
  vertex for flat inputs) and turned as the title's; the primitive ID is passed.
- d3d12_vulkan_device.cpp: a type-19 PSO appends that stage and takes triangle
  lists; every other topology is unchanged. No geometryShader, or a VS the stage
  cannot be made for (no Position, Layer/ViewportIndex), is E_NOTIMPL with a log.
- tests/test_rect_list_gs.py runs the generator through rect_list_gs_tool on
  assembled vertex shaders and interprets the stage on the CPU (corner orders
  TL/BL/BR, TL/BL/TR and the third, unequal w, cache, refusals, spirv-val);
  d3d12_rect_list_test draws three rectangle orders plus a triangle control on
  the GPU (RTX 4060 Ti, 616.64, also under the validation layer).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 22:14:54 -05:00

62 lines
2.6 KiB
C++

// The rectangle-list geometry stage generator (src/rect_list_gs.h) as a command, for tests/test_rect_list_gs.py:
//
// rect_list_gs_tool <vs.spv> <gs.spv> [<vs.spv> <gs.spv> ...]
//
// Each vertex shader's stage is made through the same cached call the D3D12 layer uses (geometry_shader) and written
// to the file after it. One line per pair on stdout: "<pair> <first pair with the same words> <signature key>", so a
// caller sees which pairs the cache served the same words. A refused vertex shader prints
// "<pair> refused: <reason>" and ends the run with exit code 2; a file that cannot be read or written, 1.
// No GPU, no Vulkan loader, no game data.
#include "../src/rect_list_gs.h"
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iterator>
#include <memory>
#include <string>
#include <vector>
int main(int argc, char** argv) {
if (argc < 3 || (argc - 1) % 2) {
std::fprintf(stderr, "usage: rect_list_gs_tool <vs.spv> <gs.spv> [<vs.spv> <gs.spv> ...]\n");
return 1;
}
std::vector<std::shared_ptr<const std::vector<std::uint32_t>>> made;
for (int pair = 0; 2 * pair + 2 <= argc; ++pair) {
const char* vs_path = argv[1 + 2 * pair];
const char* gs_path = argv[2 + 2 * pair];
std::ifstream in(vs_path, std::ios::binary);
if (!in) {
std::fprintf(stderr, "%s: cannot be read\n", vs_path);
return 1;
}
const std::vector<char> bytes{std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()};
std::vector<std::uint32_t> vs(bytes.size() / 4);
if (!vs.empty()) std::memcpy(vs.data(), bytes.data(), vs.size() * 4);
std::string why;
leonida::rect_list::Signature signature;
const bool read = leonida::rect_list::read_signature(vs, signature, why);
const auto words = read ? leonida::rect_list::geometry_shader(vs, why) : nullptr;
if (!words) {
std::printf("%d refused: %s\n", pair, why.c_str());
return 2;
}
int first = int(made.size());
for (int i = 0; i < int(made.size()); ++i)
if (made[i] == words) {
first = i;
break;
}
made.push_back(words);
std::ofstream out(gs_path, std::ios::binary);
out.write(reinterpret_cast<const char*>(words->data()), std::streamsize(words->size() * 4));
if (!out) {
std::fprintf(stderr, "%s: cannot be written\n", gs_path);
return 1;
}
std::printf("%d %d %s\n", pair, first, signature.key().c_str());
}
return 0;
}