Files
EVEngine/scripts/compile_gpu_driven_compute_shaders.py
T
西风逍遥游 eae8cfbc38 feat(graphics): stage 3 - VirtualGeometry hardware-raster integration
Virtual-geometry assets now render through the main GPU-driven frame instead of
the standalone software-raster experiment:

- Shared VG scene in vulkan::Graphics: pooled positions/triangles/clusters +
  per-cluster asset ids + per-slot visible list / indirect commands / asset
  models + materials. Meshes attach an asset via gpuDrivenVgAttachToMesh;
  GpuMeshRecord.vgAssetId routes them out of the instance chain.
- vg_main_cull.comp runs in the main frame's compute section (frustum + HZB
  against the per-asset model) and emits per-cluster VkDrawIndirectCommand
  (vertexCount = triCount*3, firstInstance = cluster id).
- Hardware raster: mesh3d_gbuffer_vgvis draws clusters with NO vertex input -
  gl_InstanceIndex = cluster, gl_VertexIndex = triangle corner, positions from
  the cluster stream; face normals computed from world edges; writes the same
  visID/visBary attachments (bit31 marks VG clusters).
- resolve_vis.frag decodes VG clusters: asset -> material, interpolates from
  the cluster stream, flat-tint shading via the shared PBR core, writes
  gl_FragDepth so later transparent draws depth-test correctly.
- gpu_emit.comp additionally writes non-indexed VkDrawIndirectCommand per
  bucket (the 20-byte indexed layout's firstInstance sits at offset 16, which
  vkCmdDrawIndirect (16-byte commands) ignores - this was the vis pass showing
  only instance 0). The vis pass now draws from that command stream.
- All per-frame bindless updates (vis samplers 15/16, VG bindings 26-29) moved
  BEFORE the set is bound to the recording command buffer (no UPDATE_AFTER_BIND;
  updating a bound set invalidates the CB - caught by validation).
- drawIndirectCount Vulkan 1.2 feature enabled at device creation; the VG
  count buffer carries VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT.
- RenderControl gains the "visResolve" feature flag (the stage-3 resolve path
  was previously enabled via an unknown flag that setFeature silently dropped).

Verified: GpuDriven.visResolveParity now genuinely exercises the resolve path
(maxDelta = 0.0 vs forward); GpuDriven.vgVisResolve renders a sphere's clusters
through the main frame (9 visible after frustum+HZB, tint-correct center,
frustum-culled to 0 behind the camera); 19 render tests + 3x suite repeat pass;
validation shows no new VUIDs.
2026-08-21 19:04:13 +08:00

79 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Compile the stage-2 GPU-driven compute shaders (HZB build / cull / emit).
Writes the same artifacts as scripts/compile_mesh3d_shaders.py:
- <name>.comp.spv (tracked binary)
- <name>_comp_spv.inc (embedded C++ array, consumed by Graphics.cpp)
"""
from __future__ import annotations
import struct
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SHADER_DIR = ROOT / "src" / "modules" / "graphics" / "shaders"
SHADERS = [
("hzb_build.comp", "comp"),
("gpu_cull.comp", "comp"),
("gpu_emit.comp", "comp"),
("vg_main_cull.comp", "comp"),
]
def spv_to_inc(spv_path: Path, array_name: str, out_path: Path) -> None:
data = spv_path.read_bytes()
if len(data) % 4 != 0:
raise SystemExit(f"SPIR-V size not multiple of 4: {spv_path}")
words = struct.unpack(f"<{len(data) // 4}I", data)
if not words or words[0] != 0x07230203:
raise SystemExit(f"bad SPIR-V magic in {spv_path}")
lines = [f"static const uint32_t {array_name}[] = {{"]
row: list[str] = []
for i, w in enumerate(words):
row.append(f"0x{w:08x}")
if len(row) == 8:
lines.append(" " + ", ".join(row) + ",")
row = []
if row:
lines.append(" " + ", ".join(row) + ",")
lines.append("};")
lines.append(
f"static const size_t {array_name}_count = sizeof({array_name})/sizeof({array_name}[0]);"
)
lines.append("")
out_path.write_text("\n".join(lines), encoding="utf-8")
print(f"wrote {out_path.name} ({len(words)} words)")
def compile_one(src: Path) -> Path:
out = Path(str(src) + ".spv")
glslc = shutil.which("glslc")
if not glslc:
raise SystemExit("need glslc on PATH (Vulkan SDK)")
cmd = [glslc, "-fshader-stage=comp", f"-I{SHADER_DIR}", str(src), "-o", str(out),
"--target-env=vulkan1.2"]
r = subprocess.run(cmd, capture_output=True, text=True)
if r.returncode != 0:
raise SystemExit(f"compile failed for {src.name}:\n{r.stderr or r.stdout or '(no output)'}")
return out
def main() -> int:
for name, stage in SHADERS:
src = SHADER_DIR / name
if not src.is_file():
raise SystemExit(f"missing {src}")
spv = compile_one(src)
array = f"{src.stem}_{stage}_spv"
spv_to_inc(spv, array, SHADER_DIR / f"{src.stem}_{stage}_spv.inc")
return 0
if __name__ == "__main__":
import shutil
sys.exit(main())