Files
mandy/tools/bary_fallback_gs.py
Andrey ca8a627c5c bary_fallback_gs: restore write mode before unlink on replace failure and publish mode (BARY-FALLBACK-GS-STAGE-CLEANUP)
Apply published_mode(asm/spv) to temporary files before atomic replacement,
and restore write permission (0o600) before directory cleanup in exception block
when replace fails, preventing Windows PermissionError [WinError 5] and
stranded temporary files.

agent: yes
AI-assisted: yes
2026-11-29 18:24:17 +03:00

295 lines
13 KiB
Python

#!/usr/bin/env python3
"""Pass-through geometry shaders that feed rdna2spirv.py --bary-fallback pixel shaders.
On a device without VK_KHR_fragment_shader_barycentric, `rdna2spirv.py --bary-fallback` declares the raw barycentric
pair a pixel shader reads as an ordinary vec3 input, at the location its index.json entry gives as
"bary_fallback_input". This module writes the stage that fills it: a geometry shader that copies Position and the
pixel shader's vec4 inputs from each vertex unchanged and adds (1,0,0), (0,1,0), (0,0,1) at the triangle's three
vertices. Default (perspective) interpolation of those gives the perspective-correct barycentrics, the value
BaryCoordKHR has, so the pixel shader's .y/.z are AMD's i/j.
python -I tools/bary_fallback_gs.py <spirv dir> # <name>.bary_gs.spv per such PS in index.json
python -I tools/bary_fallback_gs.py <spirv dir> --pair VS PS
The paired helper forwards a VS ClipDistance or CullDistance array. The suffix identifies the kind and length.
Mixed clip/cull layouts are rejected. Standard library only; build_for_index assembles and validates with spirv-as / spirv-val from PATH.
Usage: `python -I tools/bary_fallback_gs.py --help` lists the options.
"""
import argparse
from collections.abc import Iterator
from contextlib import suppress
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
_TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
if _TOOLS_DIR not in sys.path:
sys.path.insert(0, _TOOLS_DIR)
from file_mode import published_mode # noqa: E402
SUFFIX = ".bary_gs" # <ps name>.bary_gs.spvasm / .spv next to the pixel shader
def _parse_location(value) -> int:
if type(value) is int:
n = value
elif isinstance(value, str):
try:
n = int(value)
except ValueError:
raise ValueError(f"input location must be a non-negative integer, got {value!r}") from None
else:
raise ValueError(f"input location must be a non-negative integer, got {value!r}")
if n < 0:
raise ValueError(f"input location must be a non-negative integer, got {value!r}")
return n
def _validate_shader_name(name: str, index_path: str) -> None:
if (
not isinstance(name, str)
or not name
or name in (".", "..")
or "/" in name
or "\\" in name
or os.path.basename(name) != name
):
raise ValueError(f"{index_path}: invalid shader name {name!r}")
def gs_spvasm(locations, bary_location, clip_count: int = 0, cull_count: int = 0):
"""SPIR-V assembly for a triangle pass-through GS: Position and the vec4 inputs at `locations`, copied per vertex,
plus the one-hot barycentric vec3 at `bary_location`."""
locations = sorted(set(_parse_location(n) for n in locations))
if type(bary_location) is not int or bary_location < 0:
raise ValueError(f"bary location must be a non-negative integer, got {bary_location!r}")
if bary_location in locations:
raise ValueError(f"bary location {bary_location} collides with a pass-through location")
ins = [f"%in{n}" for n in locations]
outs = [f"%out{n}" for n in locations]
if type(clip_count) is not int or not 0 <= clip_count <= 8:
raise ValueError("clip distance count must be an integer from 0 to 8")
if type(cull_count) is not int or not 0 <= cull_count <= 8:
raise ValueError("cull distance count must be an integer from 0 to 8")
if clip_count and cull_count:
raise ValueError("paired helper cannot forward mixed ClipDistance and CullDistance")
count = clip_count or cull_count
kind = "cull" if cull_count else "clip"
builtin = kind.title() + "Distance"
distance_iface = [f"%in_{kind}", f"%out_{kind}"] if count else []
iface = " ".join(["%in_pos", "%out_pos", *ins, *outs, "%out_bary", *distance_iface])
lines = [
"OpCapability Geometry",
*([f"OpCapability {builtin}"] if count else []),
"OpMemoryModel Logical GLSL450",
f'OpEntryPoint Geometry %main "main" {iface}',
"OpExecutionMode %main Triangles",
"OpExecutionMode %main Invocations 1",
"OpExecutionMode %main OutputTriangleStrip",
"OpExecutionMode %main OutputVertices 3",
"OpDecorate %in_pos BuiltIn Position",
"OpDecorate %out_pos BuiltIn Position",
*([f"OpDecorate %in_{kind} BuiltIn {builtin}", f"OpDecorate %out_{kind} BuiltIn {builtin}"]
if count else []),
*[f"OpDecorate %in{n} Location {n}" for n in locations],
*[f"OpDecorate %out{n} Location {n}" for n in locations],
f"OpDecorate %out_bary Location {int(bary_location)}",
"%void = OpTypeVoid",
"%fn = OpTypeFunction %void",
"%uint = OpTypeInt 32 0",
"%int = OpTypeInt 32 1",
"%float = OpTypeFloat 32",
"%v3float = OpTypeVector %float 3",
"%v4float = OpTypeVector %float 4",
"%uint_3 = OpConstant %uint 3",
"%arr = OpTypeArray %v4float %uint_3",
"%ptr_in_arr = OpTypePointer Input %arr",
"%ptr_in_v4 = OpTypePointer Input %v4float",
"%ptr_out_v4 = OpTypePointer Output %v4float",
"%ptr_out_v3 = OpTypePointer Output %v3float",
*([f"%{kind}_count = OpConstant %uint {count}",
f"%{kind} = OpTypeArray %float %{kind}_count",
f"%triangle_{kind} = OpTypeArray %{kind} %uint_3",
f"%ptr_in_triangle_{kind} = OpTypePointer Input %triangle_{kind}",
f"%ptr_in_{kind} = OpTypePointer Input %{kind}",
f"%ptr_out_{kind} = OpTypePointer Output %{kind}"] if count else []),
"%f0 = OpConstant %float 0",
"%f1 = OpConstant %float 1",
"%bary0 = OpConstantComposite %v3float %f1 %f0 %f0",
"%bary1 = OpConstantComposite %v3float %f0 %f1 %f0",
"%bary2 = OpConstantComposite %v3float %f0 %f0 %f1",
*[f"%int_{v} = OpConstant %int {v}" for v in range(3)],
"%in_pos = OpVariable %ptr_in_arr Input",
"%out_pos = OpVariable %ptr_out_v4 Output",
*[f"{v} = OpVariable %ptr_in_arr Input" for v in ins],
*[f"{v} = OpVariable %ptr_out_v4 Output" for v in outs],
"%out_bary = OpVariable %ptr_out_v3 Output",
*([f"%in_{kind} = OpVariable %ptr_in_triangle_{kind} Input", f"%out_{kind} = OpVariable %ptr_out_{kind} Output"]
if count else []),
"%main = OpFunction %void None %fn",
"%entry = OpLabel",
]
for v in range(3):
for src, dst in (("%in_pos", "%out_pos"), *zip(ins, outs)):
p, x = f"%p{v}_{src[1:]}", f"%x{v}_{src[1:]}"
lines += [f"{p} = OpAccessChain %ptr_in_v4 {src} %int_{v}", f"{x} = OpLoad %v4float {p}",
f"OpStore {dst} {x}"]
if count:
lines += [f"%{kind}_ptr{v} = OpAccessChain %ptr_in_{kind} %in_{kind} %int_{v}",
f"%distances{v} = OpLoad %{kind} %{kind}_ptr{v}", f"OpStore %out_{kind} %distances{v}"]
lines += [f"OpStore %out_bary %bary{v}", "OpEmitVertex"]
lines += ["OpEndPrimitive", "OpReturn", "OpFunctionEnd"]
return "\n".join(lines) + "\n"
def _helpers_for_index(spirv_dir: str | os.PathLike[str], pair: tuple[str, str] | None) -> Iterator[tuple[str, str]]:
index_path = os.path.join(spirv_dir, "index.json")
if pair is None and not os.path.isfile(index_path):
return
with open(index_path, encoding="utf-8-sig") as f:
index = json.load(f)
if not isinstance(index, dict):
raise ValueError(f"{index_path}: translation index must be a JSON object")
clip_count = cull_count = 0
names = sorted(index)
suffix = ""
if pair is not None:
vertex, pixel = pair
producer = index.get(vertex, {})
consumer = index.get(pixel, {})
for name, entry in ((vertex, producer), (pixel, consumer)):
if not isinstance(entry, dict):
raise ValueError(f"{index_path}: index entry {name!r} must be a JSON object")
if producer.get("stage") != "vs" or consumer.get("stage") != "ps" or not consumer.get("bary_fallback_input"):
raise ValueError("--pair requires a translated VS and a barycentric-fallback PS from the index")
raw_caps = producer.get("capabilities", [])
if not isinstance(raw_caps, list) or not all(isinstance(c, str) for c in raw_caps):
raise ValueError(f"{index_path}: index entry {vertex!r} capabilities must be a list of strings")
caps = set(raw_caps)
unsupported = caps & {"ShaderLayer", "ShaderViewportIndex"}
if unsupported:
raise ValueError("paired helper cannot forward " + ", ".join(sorted(unsupported)))
def distance_count(kind):
if kind.title() + "Distance" not in caps:
return 0
field = f"{kind}_distance_count"
count = producer.get(field)
if type(count) is not int or not 1 <= count <= 8:
raise ValueError(f"missing or invalid VS {field}; regenerate the translation index")
return count
clip_count = distance_count("clip")
cull_count = distance_count("cull")
suffix = f".cull{cull_count}" if cull_count else f".clip{clip_count}"
names = [pixel]
for name in names:
meta = index[name]
fb = meta.get("bary_fallback_input") if isinstance(meta, dict) else None
if not fb:
continue
_validate_shader_name(name, index_path)
if not isinstance(fb, dict) or "location" not in fb:
raise ValueError(
f"{index_path}: index entry {name!r} bary_fallback_input must be an object with 'location'"
)
inputs = meta.get("inputs", [])
if not isinstance(inputs, list):
raise ValueError(f"{index_path}: index entry {name!r} inputs must be a list")
path = os.path.join(spirv_dir, name + SUFFIX + suffix + ".spvasm")
shader = gs_spvasm(inputs, fb["location"], clip_count, cull_count)
yield path, shader
def _write_text_atomic(path: str, text: str) -> None:
parent = os.path.dirname(os.path.abspath(path)) or "."
staged = None
try:
with tempfile.NamedTemporaryFile(
mode="w",
encoding="utf-8",
newline="\n",
dir=parent,
prefix=".bary-gs-",
suffix=".tmp",
delete=False,
) as f:
staged = f.name
f.write(text)
os.chmod(staged, published_mode(path))
os.replace(staged, path)
staged = None
finally:
if staged is not None:
with suppress(OSError):
os.chmod(staged, 0o600)
os.unlink(staged)
def write_for_index(spirv_dir: str | os.PathLike[str], pair: tuple[str, str] | None = None) -> list[str]:
"""Write helpers for all fallback PS entries, or one distance-layout-specific helper for an explicit (VS, PS) pair."""
written = []
for path, shader in _helpers_for_index(spirv_dir, pair):
_write_text_atomic(path, shader)
written.append(path)
return written
def build_for_index(spirv_dir: str | os.PathLike[str], pair: tuple[str, str] | None = None) -> list[str]:
"""Assemble and validate each helper with SPIRV-Tools for Vulkan 1.2 before publishing its text and binary.
Returns the .spv paths; raises RuntimeError with the tool output on failure."""
built = []
for asm, shader in _helpers_for_index(spirv_dir, pair):
spv = asm[: -len(".spvasm")] + ".spv"
with tempfile.TemporaryDirectory(prefix=".bary-gs-", dir=os.path.dirname(spv)) as staging:
staged_asm = os.path.join(staging, os.path.basename(asm))
staged_spv = os.path.join(staging, os.path.basename(spv))
Path(staged_asm).write_text(shader, encoding="utf-8", newline="\n")
for cmd in (["spirv-as", "--target-env", "vulkan1.2", staged_asm, "-o", staged_spv],
["spirv-val", "--target-env", "vulkan1.2", staged_spv]):
r = subprocess.run(cmd, capture_output=True, text=True)
if r.returncode:
raise RuntimeError(f"{cmd[0]} failed for {asm}:\n{r.stdout}{r.stderr}")
os.chmod(staged_asm, published_mode(asm))
os.chmod(staged_spv, published_mode(spv))
try:
os.replace(staged_asm, asm)
os.replace(staged_spv, spv)
except BaseException:
for s in (staged_asm, staged_spv):
try:
os.chmod(s, 0o600)
except OSError:
pass
raise
built.append(spv)
return built
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("spirv_dir")
parser.add_argument("--pair", nargs=2, metavar=("VS", "PS"), help="build for these index entries")
args = parser.parse_args(argv[1:])
if not os.path.exists(args.spirv_dir):
parser.exit(2, f"ERROR: {args.spirv_dir}: does not exist\n")
if not os.path.isdir(args.spirv_dir):
parser.exit(1, f"{args.spirv_dir}: not a directory\n")
try:
paths = build_for_index(args.spirv_dir, tuple(args.pair) if args.pair else None)
except (OSError, ValueError, RuntimeError) as err:
parser.exit(1, f"{err}\n")
for path in paths:
print(path)
if __name__ == "__main__":
try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode
sys.stdout.reconfigure(errors="replace")
except (AttributeError, ValueError, OSError):
pass
main(sys.argv)