common/vulkan, core: pre-bake screen-quad VS, no glslang at runtime

This is the probe patch in a series that eliminates dep/glslang from the
libretro core build. It establishes the infrastructure - the GLSL source
tree, the embedded-SPIR-V loader, the regeneration tool, and the build
wiring - by converting exactly one call site so the pattern can be
smoke-tested in isolation before the rest of Bucket A and then Bucket B
follow in subsequent patches. glslang stays in the tree until the final
patch in the series, which removes dep/glslang and the surrounding
shader_compiler.cpp.

What this patch does:

- Adds data/shaders/vulkan/screen_quad.vert.glsl - the GLSL source for
  the screen-quad VS, kept in the tree for human readability. The build
  does NOT see this file; it is consumed only by the offline regen tool.

- Adds src/common/vulkan/embedded_spirv/screen_quad_vs.inc - the
  pre-compiled SPIR-V blob as a 'const uint32_t[]' (367 words, 1.4 KiB),
  generated offline from the .glsl by glslangValidator targeting
  vulkan1.0. The .inc is checked in and #include'd by embedded_shaders.cpp
  inside namespace Vulkan::EmbeddedShaders. spirv-val confirms the blob
  is valid Vulkan 1.0 SPIR-V (magic 0x07230203, version 1.0).

- Adds src/common/vulkan/embedded_shaders.{h,cpp}. The header exposes a
  pair of extern declarations per blob and a single
  CreateShaderModule(const uint32_t*, size_t) loader. The loader
  intentionally bypasses Vulkan::ShaderCache - pre-baked SPIR-V is
  already final, so there is nothing to compile and nothing worth
  caching on disk. Just a sized-and-checked vkCreateShaderModule call.

- Adds tools/regen_vulkan_spirv.py - the offline workflow for
  contributors who edit shader .glsl sources. It invokes
  glslangValidator on each .glsl under data/shaders/vulkan/ and rewrites
  the matching .inc. CRITICAL: this tool is NEVER invoked by Make or
  CMake. The build sees only .h/.cpp/.inc files and has no shader-
  compiler dependency. Contributors run the script manually after
  editing .glsl, commit both files together.

- Adds data/shaders/vulkan/README.md documenting the workflow and the
  invariant that the build must never invoke an external shader
  compiler.

- Switches GPU_HW_Vulkan::GetFullscreenQuadVertexShader() (in
  src/core/gpu_hw_vulkan.cpp) from
      g_vulkan_shader_cache->GetVertexShader(m_shadergen->Generate...)
  to
      Vulkan::EmbeddedShaders::CreateShaderModule(k_screen_quad_vs, ...).
  The m_shadergen null-check is removed since the embedded path has no
  such ordering dependency. The function's contract (lazy creation,
  stored in m_fullscreen_quad_vertex_shader, returned to multiple
  pipeline builders) is unchanged.

- Registers src/common/vulkan/embedded_shaders.cpp in both
  Makefile.libretro (alphabetical order in the vulkan/ SOURCES_CXX
  block) and src/common/CMakeLists.txt (alphabetical order in the
  common target's source list). Neither change adds any build-tool
  dependency; the new .cpp compiles like any other.

What this patch does NOT do:

- It does not remove dep/glslang or the SOURCES_CXX entries that pull
  in its 99 translation units. glslang is still needed for every shader
  generator that has not yet been converted - in particular all of
  GPU_HW_ShaderGen, plus the remaining utility shaders (UV quad VS,
  VRAM read/copy/write/fill/update-depth FS, display FS, adaptive
  downsample mip/blur/composite FS, box-sample downsample FS).
  Subsequent patches in this series convert those one bucket at a time.
  The final patch removes dep/glslang, src/common/vulkan/shader_compiler.{h,cpp},
  the glslang link line in CMake, and the Makefile entries.

- It does not touch the disk-backed Vulkan::ShaderCache. That cache
  still serves the runtime-glslang path for remaining shaders.

Smoke tests performed in isolation:

- glslangValidator --target-env vulkan1.0 succeeds on the .glsl source,
  producing 367 32-bit words of SPIR-V.
- spirv-val --target-env vulkan1.0 reports VALID on the blob.
- spirv-dis confirms the entry point, VertexData block, and Position/
  VertexIndex built-ins are correctly emitted.
- A standalone harness including the .inc inside the
  Vulkan::EmbeddedShaders namespace and calling CreateShaderModule
  through a stubbed vkCreateShaderModule reads back the correct magic
  word and size.
- g++ -fsyntax-only on embedded_shaders.cpp against the real headers
  in the tree (excluding only the platform-specific X11 surface define
  not used by this TU) is clean.

Smoke tests that require the user's setup:

- Real build on Windows (RTX 5090) and macOS.
- Vulkan core boots, RetroArch's pipeline cache populates, and at
  least one game renders past the first frame using the screen-quad
  pipeline (VRAM ops, display, downsample - all of which call
  GetFullscreenQuadVertexShader internally).
This commit is contained in:
LibretroAdmin 2026-05-17 17:49:19 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit 2cbcefcff6
9 changed files with 388 additions and 8 deletions

View file

@ -306,6 +306,7 @@ SOURCES_CXX += \
$(COMMON_DIR)/gl/texture.cpp \
$(COMMON_DIR)/vulkan/builders.cpp \
$(COMMON_DIR)/vulkan/context.cpp \
$(COMMON_DIR)/vulkan/embedded_shaders.cpp \
$(COMMON_DIR)/vulkan/shader_cache.cpp \
$(COMMON_DIR)/vulkan/shader_compiler.cpp \
$(COMMON_DIR)/vulkan/staging_buffer.cpp \

View file

@ -0,0 +1,46 @@
# Vulkan shader sources (pre-baked SPIR-V)
This directory holds the GLSL sources for shaders used by the Vulkan backend
that have been moved off the runtime `ShaderGen` path. Each `.glsl` file here
has a matching pre-compiled SPIR-V blob checked in to
`src/common/vulkan/embedded_spirv/`.
## Why this exists
Historically the Vulkan backend generated GLSL at runtime via
`src/core/shadergen.cpp` and `src/core/gpu_hw_shadergen.cpp`, then handed the
text to glslang inside `src/common/vulkan/shader_compiler.cpp` to produce
SPIR-V. Both `dep/glslang/` (4.4 MB of vendored source, 99 translation units)
and the disk-backed runtime shader cache exist solely to support that path.
Pre-baking the SPIR-V removes:
* the glslang dependency from the libretro core build,
* the per-launch shader compilation stall, and
* a class of "stale cache after glslang version bump" runtime failures.
The OpenGL / OpenGL ES / D3D11 / D3D12 backends still use the runtime
`ShaderGen` path; only the Vulkan backend reads from the embedded SPIR-V.
## Build-time constraint
The core build (`Makefile.libretro` and `src/common/CMakeLists.txt`) must
never invoke an external shader compiler. The `.inc` files under
`src/common/vulkan/embedded_spirv/` are checked into the repository and
compiled like any other C++ source. The `tools/regen_vulkan_spirv.py` helper
exists for contributors who edit the `.glsl` sources in this directory; it is
not part of the build.
## Regenerating the SPIR-V blobs
After editing a `.glsl` file here, run:
python3 tools/regen_vulkan_spirv.py
This invokes `glslangValidator --target-env vulkan1.0` on each `.glsl` file
and rewrites the matching `.inc` under `src/common/vulkan/embedded_spirv/`.
Both the `.glsl` and the regenerated `.inc` must be committed together so the
two stay in sync.
`glslangValidator` is part of the Vulkan SDK (or the `glslang-tools` package
on Debian / Ubuntu). It is **not** invoked by the core build.

View file

@ -0,0 +1,24 @@
// Pre-baked Vulkan source for ShaderGen::GenerateScreenQuadVertexShader().
//
// This file is the source of truth for the screen quad vertex shader used by
// the Vulkan backend. The runtime equivalent of this shader is generated by
// ShaderGen::GenerateScreenQuadVertexShader() in src/core/shadergen.cpp; that
// function is still used by the OpenGL, OpenGL ES, D3D11 and D3D12 backends.
// For Vulkan we ship a pre-compiled SPIR-V blob in
// src/common/vulkan/embedded_spirv/screen_quad_vs.inc
// generated from this file by tools/regen_vulkan_spirv.py. The build itself
// has no glslangValidator dependency.
#version 450 core
layout(location = 0) out VertexData {
vec2 v_tex0;
};
void main()
{
v_tex0 = vec2(float((uint(gl_VertexIndex) << 1) & 2u), float(uint(gl_VertexIndex) & 2u));
gl_Position = vec4(v_tex0 * vec2(2.0, -2.0) + vec2(-1.0, 1.0), 0.0, 1.0);
// Flip Y to match the convention of ShaderGen output for Vulkan.
gl_Position.y = -gl_Position.y;
}

View file

@ -73,6 +73,8 @@ add_library(common
vulkan/builders.h
vulkan/context.cpp
vulkan/context.h
vulkan/embedded_shaders.cpp
vulkan/embedded_shaders.h
vulkan/shader_cache.cpp
vulkan/shader_cache.h
vulkan/shader_compiler.cpp

View file

@ -0,0 +1,50 @@
// Copyright 2026 LibretroAdmin
// Licensed under GPLv2+
// Refer to the LICENSE file included.
#include "embedded_shaders.h"
#include "../log.h"
#include "context.h"
#include "util.h"
Log_SetChannel(Vulkan::EmbeddedShaders);
// Storage for the embedded SPIR-V blobs. Each .inc supplies a definition of
// the array and the matching size constant declared in embedded_shaders.h.
// The .inc files MUST be included inside the namespace block below so the
// names resolve to the header's extern declarations.
namespace Vulkan::EmbeddedShaders {
#include "embedded_spirv/screen_quad_vs.inc"
VkShaderModule CreateShaderModule(const uint32_t* spv, size_t spv_size_bytes)
{
if (!spv || spv_size_bytes == 0)
{
Log_ErrorPrint("CreateShaderModule called with empty SPIR-V blob");
return VK_NULL_HANDLE;
}
if ((spv_size_bytes % sizeof(uint32_t)) != 0)
{
Log_ErrorPrintf("CreateShaderModule SPIR-V size %zu is not a multiple of 4", spv_size_bytes);
return VK_NULL_HANDLE;
}
const VkShaderModuleCreateInfo ci = {
VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
nullptr,
0,
spv_size_bytes,
spv,
};
VkShaderModule mod = VK_NULL_HANDLE;
VkResult res = vkCreateShaderModule(g_vulkan_context->GetDevice(), &ci, nullptr, &mod);
if (res != VK_SUCCESS)
{
LOG_VULKAN_ERROR(res, "vkCreateShaderModule() failed for embedded SPIR-V: ");
return VK_NULL_HANDLE;
}
return mod;
}
} // namespace Vulkan::EmbeddedShaders

View file

@ -0,0 +1,42 @@
// Copyright 2026 LibretroAdmin
// Licensed under GPLv2+
// Refer to the LICENSE file included.
#pragma once
#include "../types.h"
#include "vulkan_loader.h"
#include <cstddef>
#include <cstdint>
// Pre-compiled SPIR-V blobs for the Vulkan backend.
//
// Each blob is generated offline by tools/regen_vulkan_spirv.py from the
// matching GLSL source under data/shaders/vulkan/, and checked into
// src/common/vulkan/embedded_spirv/. Nothing in the build system invokes
// glslangValidator - the .inc files are consumed as plain C++ arrays. See
// data/shaders/vulkan/README.md for the editing workflow.
//
// This path replaces the runtime glslang -> SPIR-V step done in
// Vulkan::ShaderCompiler::CompileShader for the shaders that have been
// pre-baked. Until every shader has been pre-baked, the runtime path and
// glslang still cover the remainder.
namespace Vulkan::EmbeddedShaders {
// Screen-quad vertex shader (fullscreen-triangle helper used by VRAM ops,
// display, downsample, etc.). Equivalent to
// ShaderGen::GenerateScreenQuadVertexShader() in Vulkan mode.
//
// The array is sized in the .cpp; consumers see only the bare extern.
extern const uint32_t k_screen_quad_vs[];
extern const size_t k_screen_quad_vs_size_bytes;
// Create a VkShaderModule directly from a pre-compiled SPIR-V blob.
//
// This intentionally bypasses Vulkan::ShaderCache: pre-baked SPIR-V is already
// final, so there is nothing to compile and nothing worth caching on disk.
// The caller owns the returned module and must vkDestroyShaderModule it.
// Returns VK_NULL_HANDLE on failure.
VkShaderModule CreateShaderModule(const uint32_t* spv, size_t spv_size_bytes);
} // namespace Vulkan::EmbeddedShaders

View file

@ -0,0 +1,57 @@
// Autogenerated from data/shaders/vulkan/screen_quad.vert.glsl.
// DO NOT EDIT. Regenerate with tools/regen_vulkan_spirv.py.
// glslangValidator --target-env vulkan1.0
//
// Included inside namespace Vulkan::EmbeddedShaders in
// embedded_shaders.cpp. The matching extern declaration lives in
// embedded_shaders.h.
const uint32_t k_screen_quad_vs[] = {
0x07230203u, 0x00010000u, 0x0008000bu, 0x00000039u, 0x00000000u, 0x00020011u, 0x00000001u, 0x0006000bu,
0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu, 0x00000000u, 0x00000001u,
0x0008000fu, 0x00000000u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x0000000au, 0x0000000eu, 0x00000023u,
0x00030003u, 0x00000002u, 0x000001c2u, 0x00040005u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x00050005u,
0x00000008u, 0x74726556u, 0x61447865u, 0x00006174u, 0x00050006u, 0x00000008u, 0x00000000u, 0x65745f76u,
0x00003078u, 0x00030005u, 0x0000000au, 0x00000000u, 0x00060005u, 0x0000000eu, 0x565f6c67u, 0x65747265u,
0x646e4978u, 0x00007865u, 0x00060005u, 0x00000021u, 0x505f6c67u, 0x65567265u, 0x78657472u, 0x00000000u,
0x00060006u, 0x00000021u, 0x00000000u, 0x505f6c67u, 0x7469736fu, 0x006e6f69u, 0x00070006u, 0x00000021u,
0x00000001u, 0x505f6c67u, 0x746e696fu, 0x657a6953u, 0x00000000u, 0x00070006u, 0x00000021u, 0x00000002u,
0x435f6c67u, 0x4470696cu, 0x61747369u, 0x0065636eu, 0x00070006u, 0x00000021u, 0x00000003u, 0x435f6c67u,
0x446c6c75u, 0x61747369u, 0x0065636eu, 0x00030005u, 0x00000023u, 0x00000000u, 0x00030047u, 0x00000008u,
0x00000002u, 0x00040047u, 0x0000000au, 0x0000001eu, 0x00000000u, 0x00040047u, 0x0000000eu, 0x0000000bu,
0x0000002au, 0x00030047u, 0x00000021u, 0x00000002u, 0x00050048u, 0x00000021u, 0x00000000u, 0x0000000bu,
0x00000000u, 0x00050048u, 0x00000021u, 0x00000001u, 0x0000000bu, 0x00000001u, 0x00050048u, 0x00000021u,
0x00000002u, 0x0000000bu, 0x00000003u, 0x00050048u, 0x00000021u, 0x00000003u, 0x0000000bu, 0x00000004u,
0x00020013u, 0x00000002u, 0x00030021u, 0x00000003u, 0x00000002u, 0x00030016u, 0x00000006u, 0x00000020u,
0x00040017u, 0x00000007u, 0x00000006u, 0x00000002u, 0x0003001eu, 0x00000008u, 0x00000007u, 0x00040020u,
0x00000009u, 0x00000003u, 0x00000008u, 0x0004003bu, 0x00000009u, 0x0000000au, 0x00000003u, 0x00040015u,
0x0000000bu, 0x00000020u, 0x00000001u, 0x0004002bu, 0x0000000bu, 0x0000000cu, 0x00000000u, 0x00040020u,
0x0000000du, 0x00000001u, 0x0000000bu, 0x0004003bu, 0x0000000du, 0x0000000eu, 0x00000001u, 0x00040015u,
0x00000010u, 0x00000020u, 0x00000000u, 0x0004002bu, 0x0000000bu, 0x00000012u, 0x00000001u, 0x0004002bu,
0x00000010u, 0x00000014u, 0x00000002u, 0x00040020u, 0x0000001cu, 0x00000003u, 0x00000007u, 0x00040017u,
0x0000001eu, 0x00000006u, 0x00000004u, 0x0004002bu, 0x00000010u, 0x0000001fu, 0x00000001u, 0x0004001cu,
0x00000020u, 0x00000006u, 0x0000001fu, 0x0006001eu, 0x00000021u, 0x0000001eu, 0x00000006u, 0x00000020u,
0x00000020u, 0x00040020u, 0x00000022u, 0x00000003u, 0x00000021u, 0x0004003bu, 0x00000022u, 0x00000023u,
0x00000003u, 0x0004002bu, 0x00000006u, 0x00000026u, 0x40000000u, 0x0004002bu, 0x00000006u, 0x00000027u,
0xc0000000u, 0x0005002cu, 0x00000007u, 0x00000028u, 0x00000026u, 0x00000027u, 0x0004002bu, 0x00000006u,
0x0000002au, 0xbf800000u, 0x0004002bu, 0x00000006u, 0x0000002bu, 0x3f800000u, 0x0005002cu, 0x00000007u,
0x0000002cu, 0x0000002au, 0x0000002bu, 0x0004002bu, 0x00000006u, 0x0000002eu, 0x00000000u, 0x00040020u,
0x00000032u, 0x00000003u, 0x0000001eu, 0x00040020u, 0x00000034u, 0x00000003u, 0x00000006u, 0x00050036u,
0x00000002u, 0x00000004u, 0x00000000u, 0x00000003u, 0x000200f8u, 0x00000005u, 0x0004003du, 0x0000000bu,
0x0000000fu, 0x0000000eu, 0x0004007cu, 0x00000010u, 0x00000011u, 0x0000000fu, 0x000500c4u, 0x00000010u,
0x00000013u, 0x00000011u, 0x00000012u, 0x000500c7u, 0x00000010u, 0x00000015u, 0x00000013u, 0x00000014u,
0x00040070u, 0x00000006u, 0x00000016u, 0x00000015u, 0x0004003du, 0x0000000bu, 0x00000017u, 0x0000000eu,
0x0004007cu, 0x00000010u, 0x00000018u, 0x00000017u, 0x000500c7u, 0x00000010u, 0x00000019u, 0x00000018u,
0x00000014u, 0x00040070u, 0x00000006u, 0x0000001au, 0x00000019u, 0x00050050u, 0x00000007u, 0x0000001bu,
0x00000016u, 0x0000001au, 0x00050041u, 0x0000001cu, 0x0000001du, 0x0000000au, 0x0000000cu, 0x0003003eu,
0x0000001du, 0x0000001bu, 0x00050041u, 0x0000001cu, 0x00000024u, 0x0000000au, 0x0000000cu, 0x0004003du,
0x00000007u, 0x00000025u, 0x00000024u, 0x00050085u, 0x00000007u, 0x00000029u, 0x00000025u, 0x00000028u,
0x00050081u, 0x00000007u, 0x0000002du, 0x00000029u, 0x0000002cu, 0x00050051u, 0x00000006u, 0x0000002fu,
0x0000002du, 0x00000000u, 0x00050051u, 0x00000006u, 0x00000030u, 0x0000002du, 0x00000001u, 0x00070050u,
0x0000001eu, 0x00000031u, 0x0000002fu, 0x00000030u, 0x0000002eu, 0x0000002bu, 0x00050041u, 0x00000032u,
0x00000033u, 0x00000023u, 0x0000000cu, 0x0003003eu, 0x00000033u, 0x00000031u, 0x00060041u, 0x00000034u,
0x00000035u, 0x00000023u, 0x0000000cu, 0x0000001fu, 0x0004003du, 0x00000006u, 0x00000036u, 0x00000035u,
0x0004007fu, 0x00000006u, 0x00000037u, 0x00000036u, 0x00060041u, 0x00000034u, 0x00000038u, 0x00000023u,
0x0000000cu, 0x0000001fu, 0x0003003eu, 0x00000038u, 0x00000037u, 0x000100fdu, 0x00010038u,
};
const size_t k_screen_quad_vs_size_bytes = sizeof(k_screen_quad_vs);

View file

@ -5,6 +5,7 @@
#include "common/timer.h"
#include "common/vulkan/builders.h"
#include "common/vulkan/context.h"
#include "common/vulkan/embedded_shaders.h"
#include "common/vulkan/shader_cache.h"
#include "common/vulkan/staging_texture.h"
#include "common/vulkan/util.h"
@ -2019,15 +2020,15 @@ VkShaderModule GPU_HW_Vulkan::GetFullscreenQuadVertexShader()
if (m_fullscreen_quad_vertex_shader != VK_NULL_HANDLE)
return m_fullscreen_quad_vertex_shader;
if (!m_shadergen)
{
Log_ErrorPrint("GetFullscreenQuadVertexShader called before CompilePipelines constructed the shadergen");
return VK_NULL_HANDLE;
}
m_fullscreen_quad_vertex_shader =
g_vulkan_shader_cache->GetVertexShader(m_shadergen->GenerateScreenQuadVertexShader());
// Pre-baked SPIR-V: no glslang invocation, no shader_cache lookup, no
// dependency on m_shadergen existing yet. See
// data/shaders/vulkan/screen_quad.vert.glsl for the source of the blob,
// and src/common/vulkan/embedded_shaders.h for the loader.
m_fullscreen_quad_vertex_shader = Vulkan::EmbeddedShaders::CreateShaderModule(
Vulkan::EmbeddedShaders::k_screen_quad_vs,
Vulkan::EmbeddedShaders::k_screen_quad_vs_size_bytes);
if (m_fullscreen_quad_vertex_shader == VK_NULL_HANDLE)
Log_ErrorPrint("Lazy fullscreen-quad vertex shader compile failed");
Log_ErrorPrint("Embedded fullscreen-quad vertex shader load failed");
return m_fullscreen_quad_vertex_shader;
}

157
tools/regen_vulkan_spirv.py Executable file
View file

@ -0,0 +1,157 @@
#!/usr/bin/env python3
# Regenerate embedded Vulkan SPIR-V blobs from GLSL sources.
#
# This script is NOT invoked by the core build. It is run by contributors who
# edit shader sources under data/shaders/vulkan/. The build itself only sees
# the resulting .inc files (checked into src/common/vulkan/embedded_spirv/),
# so the libretro core has zero glslangValidator dependency.
#
# Usage:
# python3 tools/regen_vulkan_spirv.py [--glslang PATH]
#
# Requires glslangValidator from the Vulkan SDK or the `glslang-tools`
# package on Debian / Ubuntu.
import argparse
import os
import re
import struct
import subprocess
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
GLSL_DIR = REPO_ROOT / "data" / "shaders" / "vulkan"
INC_DIR = REPO_ROOT / "src" / "common" / "vulkan" / "embedded_spirv"
# Map glsl file suffix to glslangValidator stage flag. Anything not in this
# table is skipped with a warning.
STAGE_FROM_SUFFIX = {
".vert.glsl": ("vert", "vs"),
".frag.glsl": ("frag", "fs"),
".comp.glsl": ("comp", "cs"),
".geom.glsl": ("geom", "gs"),
}
def find_glslang(explicit):
if explicit:
return explicit
for name in ("glslangValidator", "glslangValidator.exe"):
from shutil import which
path = which(name)
if path:
return path
sys.stderr.write(
"error: glslangValidator not found on PATH. Install the Vulkan SDK\n"
" or the glslang-tools package, or pass --glslang PATH.\n")
sys.exit(1)
def sanitize_identifier(stem):
# Map "screen_quad.vert" -> "screen_quad_vs"; only [A-Za-z0-9_] survive.
name = re.sub(r"[^A-Za-z0-9_]", "_", stem)
return name
def compile_one(glslang, glsl_path, stage_flag):
# Write SPIR-V to a temp file under INC_DIR so we don't pollute /tmp on
# restricted-filesystem hosts.
spv_path = INC_DIR / (glsl_path.stem + ".spv.tmp")
cmd = [
glslang,
"--target-env", "vulkan1.0",
"-S", stage_flag,
"-V", str(glsl_path),
"-o", str(spv_path),
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
sys.stderr.write(f"glslangValidator failed for {glsl_path.name}:\n")
sys.stderr.write(result.stdout)
sys.stderr.write(result.stderr)
if spv_path.exists():
spv_path.unlink()
sys.exit(1)
data = spv_path.read_bytes()
spv_path.unlink()
if len(data) % 4 != 0:
sys.stderr.write(
f"error: {glsl_path.name} produced {len(data)} bytes "
f"of SPIR-V (not a multiple of 4)\n")
sys.exit(1)
words = struct.unpack(f"<{len(data) // 4}I", data)
if not words or words[0] != 0x07230203:
sys.stderr.write(
f"error: {glsl_path.name} produced invalid SPIR-V (bad magic)\n")
sys.exit(1)
return words
def emit_inc(identifier, glsl_name, words, out_path):
# 8 words per line keeps the file diff-friendly without being too tall.
PER_LINE = 8
lines = []
lines.append(
f"// Autogenerated from data/shaders/vulkan/{glsl_name}.\n"
f"// DO NOT EDIT. Regenerate with tools/regen_vulkan_spirv.py.\n"
f"// glslangValidator --target-env vulkan1.0\n"
f"//\n"
f"// Included inside namespace Vulkan::EmbeddedShaders in\n"
f"// embedded_shaders.cpp. The matching extern declaration lives in\n"
f"// embedded_shaders.h.\n"
f"\n"
f"const uint32_t k_{identifier}[] = {{\n")
for i in range(0, len(words), PER_LINE):
chunk = ", ".join(f"0x{w:08x}u" for w in words[i:i + PER_LINE])
lines.append(f" {chunk},\n")
lines.append(f"}};\n"
f"const size_t k_{identifier}_size_bytes = "
f"sizeof(k_{identifier});\n")
out_path.write_text("".join(lines))
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--glslang", help="path to glslangValidator binary")
args = ap.parse_args()
glslang = find_glslang(args.glslang)
INC_DIR.mkdir(parents=True, exist_ok=True)
glsl_files = sorted(GLSL_DIR.glob("*.glsl"))
if not glsl_files:
sys.stderr.write(f"warning: no .glsl files under {GLSL_DIR}\n")
return
total = 0
for glsl_path in glsl_files:
# glsl_path.name looks like "screen_quad.vert.glsl".
suffix = None
for k in STAGE_FROM_SUFFIX:
if glsl_path.name.endswith(k):
suffix = k
break
if suffix is None:
sys.stderr.write(
f"warning: {glsl_path.name} has no recognised stage suffix; "
f"skipping\n")
continue
stage_flag, ident_suffix = STAGE_FROM_SUFFIX[suffix]
# "screen_quad.vert.glsl" -> stem "screen_quad.vert" -> "screen_quad"
base = glsl_path.name[:-len(suffix)]
identifier = sanitize_identifier(base) + "_" + ident_suffix
out_path = INC_DIR / (identifier + ".inc")
words = compile_one(glslang, glsl_path, stage_flag)
emit_inc(identifier, glsl_path.name, words, out_path)
size_kb = (len(words) * 4) / 1024.0
print(f" {glsl_path.name:<40} -> {out_path.name} "
f"({len(words)} words, {size_kb:.1f} KiB)")
total += 1
print(f"regenerated {total} SPIR-V blob(s) in {INC_DIR}")
if __name__ == "__main__":
main()