unifica shader compile script com a compile_spirv.cmake i regenera headers

This commit is contained in:
2026-05-14 17:40:01 +02:00
parent 583d901162
commit ce662609f3
7 changed files with 41 additions and 108 deletions
+31 -42
View File
@@ -158,56 +158,45 @@ endif()
if(NOT APPLE AND NOT EMSCRIPTEN)
find_program(GLSLC_EXE NAMES glslc)
set(SHADER_VERT_SRC "${CMAKE_SOURCE_DIR}/data/shaders/postfx.vert")
set(SHADER_FRAG_SRC "${CMAKE_SOURCE_DIR}/data/shaders/postfx.frag")
set(SHADER_CRTPI_SRC "${CMAKE_SOURCE_DIR}/data/shaders/crtpi_frag.glsl")
set(SHADER_UPSCALE_SRC "${CMAKE_SOURCE_DIR}/data/shaders/upscale.frag")
set(SHADER_DOWNSCALE_SRC "${CMAKE_SOURCE_DIR}/data/shaders/downscale.frag")
set(SHADERS_DIR "${CMAKE_SOURCE_DIR}/data/shaders")
set(HEADERS_DIR "${CMAKE_SOURCE_DIR}/source/core/rendering/sdl3gpu/spv")
set(SHADER_VERT_H "${CMAKE_SOURCE_DIR}/source/core/rendering/sdl3gpu/spv/postfx_vert_spv.h")
set(SHADER_FRAG_H "${CMAKE_SOURCE_DIR}/source/core/rendering/sdl3gpu/spv/postfx_frag_spv.h")
set(SHADER_CRTPI_H "${CMAKE_SOURCE_DIR}/source/core/rendering/sdl3gpu/spv/crtpi_frag_spv.h")
set(SHADER_UPSCALE_H "${CMAKE_SOURCE_DIR}/source/core/rendering/sdl3gpu/spv/upscale_frag_spv.h")
set(SHADER_DOWNSCALE_H "${CMAKE_SOURCE_DIR}/source/core/rendering/sdl3gpu/spv/downscale_frag_spv.h")
set(ALL_SHADER_SOURCES "${SHADER_VERT_SRC}" "${SHADER_FRAG_SRC}" "${SHADER_CRTPI_SRC}" "${SHADER_UPSCALE_SRC}" "${SHADER_DOWNSCALE_SRC}")
set(ALL_SHADER_HEADERS "${SHADER_VERT_H}" "${SHADER_FRAG_H}" "${SHADER_CRTPI_H}" "${SHADER_UPSCALE_H}" "${SHADER_DOWNSCALE_H}")
set(ALL_SHADER_HEADERS
"${HEADERS_DIR}/postfx_vert_spv.h"
"${HEADERS_DIR}/postfx_frag_spv.h"
"${HEADERS_DIR}/upscale_frag_spv.h"
"${HEADERS_DIR}/downscale_frag_spv.h"
"${HEADERS_DIR}/crtpi_frag_spv.h"
)
set(ALL_SHADER_SOURCES
"${SHADERS_DIR}/postfx.vert"
"${SHADERS_DIR}/postfx.frag"
"${SHADERS_DIR}/upscale.frag"
"${SHADERS_DIR}/downscale.frag"
"${SHADERS_DIR}/crtpi_frag.glsl"
)
if(GLSLC_EXE)
set(COMPILE_SHADER_SCRIPT "${CMAKE_SOURCE_DIR}/tools/shaders/compile_shader.cmake")
macro(add_shader SRC_FILE OUT_H VAR_NAME)
cmake_parse_arguments(S "" "STAGE" "" ${ARGN})
add_custom_command(
OUTPUT "${OUT_H}"
COMMAND ${CMAKE_COMMAND}
"-DGLSLC=${GLSLC_EXE}"
"-DSRC=${SRC_FILE}"
"-DOUT_H=${OUT_H}"
"-DVAR=${VAR_NAME}"
"-DSTAGE=${S_STAGE}"
-P "${COMPILE_SHADER_SCRIPT}"
DEPENDS "${SRC_FILE}" "${COMPILE_SHADER_SCRIPT}"
COMMENT "Compilando shader: ${VAR_NAME}"
)
endmacro()
add_shader("${SHADER_VERT_SRC}" "${SHADER_VERT_H}" "postfx_vert_spv")
add_shader("${SHADER_FRAG_SRC}" "${SHADER_FRAG_H}" "postfx_frag_spv")
add_shader("${SHADER_CRTPI_SRC}" "${SHADER_CRTPI_H}" "crtpi_frag_spv" STAGE fragment)
add_shader("${SHADER_UPSCALE_SRC}" "${SHADER_UPSCALE_H}" "upscale_frag_spv")
add_shader("${SHADER_DOWNSCALE_SRC}" "${SHADER_DOWNSCALE_H}" "downscale_frag_spv")
add_custom_command(
OUTPUT ${ALL_SHADER_HEADERS}
COMMAND ${CMAKE_COMMAND}
-D GLSLC=${GLSLC_EXE}
-D SHADERS_DIR=${SHADERS_DIR}
-D HEADERS_DIR=${HEADERS_DIR}
-P ${CMAKE_SOURCE_DIR}/tools/shaders/compile_spirv.cmake
DEPENDS ${ALL_SHADER_SOURCES}
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
COMMENT "Compilando shaders SPIR-V..."
)
add_custom_target(shaders DEPENDS ${ALL_SHADER_HEADERS})
message(STATUS "glslc encontrado: shaders se compilarán automáticamente")
else()
foreach(_h IN LISTS ALL_SHADER_HEADERS)
if(NOT EXISTS "${_h}")
foreach(HDR ${ALL_SHADER_HEADERS})
if(NOT EXISTS "${HDR}")
message(FATAL_ERROR
"glslc no encontrado y header SPIR-V no existe: ${_h}\n"
"glslc no encontrado y header SPIR-V no existe: ${HDR}\n"
" Instala glslc: sudo apt install glslang-tools (Linux)\n"
" choco install vulkan-sdk (Windows)\n"
" O genera los headers manualmente: tools/shaders/compile_spirv.sh"
" choco install vulkan-sdk (Windows)"
)
endif()
endforeach()
@@ -10357,5 +10357,6 @@ static const uint8_t kcrtpi_frag_spv[] = {
0x38,
0x00,
0x01,
0x00};
0x00,
};
static const size_t kcrtpi_frag_spv_size = 10356;
@@ -4249,5 +4249,6 @@ static const uint8_t kdownscale_frag_spv[] = {
0x38,
0x00,
0x01,
0x00};
0x00,
};
static const size_t kdownscale_frag_spv_size = 4248;
@@ -11241,5 +11241,6 @@ static const uint8_t kpostfx_frag_spv[] = {
0x38,
0x00,
0x01,
0x00};
0x00,
};
static const size_t kpostfx_frag_spv_size = 11240;
@@ -1445,5 +1445,6 @@ static const uint8_t kpostfx_vert_spv[] = {
0x38,
0x00,
0x01,
0x00};
0x00,
};
static const size_t kpostfx_vert_spv_size = 1444;
@@ -629,5 +629,6 @@ static const uint8_t kupscale_frag_spv[] = {
0x38,
0x00,
0x01,
0x00};
0x00,
};
static const size_t kupscale_frag_spv_size = 628;
-61
View File
@@ -1,61 +0,0 @@
# compile_shader.cmake
# Compila un shader GLSL a header C++ embebible con datos SPIR-V.
# Funciona en Windows, Linux y macOS sin bash ni herramientas Unix.
#
# Variables requeridas (pasar con -D al invocar cmake -P):
# GLSLC - ruta al ejecutable glslc
# SRC - archivo fuente GLSL
# OUT_H - archivo header de salida
# VAR - nombre base de la variable C++ (e.g. "postfx_vert_spv")
# STAGE - (opcional) stage explícito para glslc (e.g. "fragment")
cmake_minimum_required(VERSION 3.10)
# Nom intermedi únic per stage (vert/frag/comp) per evitar col·lisions en
# builds paral·lels: postfx.vert i postfx.frag generarien el mateix
# `postfx.spv` si féssim servir el nom del SRC. Usem el del OUT_H, que ja
# inclou el stage (p.ex. `postfx_frag_spv.h` → `postfx_frag_spv.spv`).
get_filename_component(OUT_DIR "${OUT_H}" DIRECTORY)
get_filename_component(OUT_NAME "${OUT_H}" NAME_WE)
set(OUT_SPV "${OUT_DIR}/${OUT_NAME}.spv")
# Compilar GLSL → SPIR-V
if(DEFINED STAGE AND NOT STAGE STREQUAL "")
execute_process(
COMMAND "${GLSLC}" "-fshader-stage=${STAGE}" "${SRC}" -o "${OUT_SPV}"
RESULT_VARIABLE RESULT
ERROR_VARIABLE ERROR_MSG
)
else()
execute_process(
COMMAND "${GLSLC}" "${SRC}" -o "${OUT_SPV}"
RESULT_VARIABLE RESULT
ERROR_VARIABLE ERROR_MSG
)
endif()
if(NOT RESULT EQUAL 0)
message(FATAL_ERROR "glslc falló compilando ${SRC}:\n${ERROR_MSG}")
endif()
# Leer binario SPIR-V como cadena hexadecimal
file(READ "${OUT_SPV}" SPV_HEX HEX)
file(REMOVE "${OUT_SPV}")
string(LENGTH "${SPV_HEX}" HEX_LEN)
math(EXPR BYTE_COUNT "${HEX_LEN} / 2")
# Convertir a array C++ con formato " 0xXX,\n" por byte
string(REGEX REPLACE "([0-9a-f][0-9a-f])" " 0x\\1,\n" SPV_BYTES "${SPV_HEX}")
# Eliminar la última coma y sustituir por el "}" de cierre
string(REGEX REPLACE ",\n$" "}" SPV_BYTES "${SPV_BYTES}")
# Escribir el header
file(WRITE "${OUT_H}"
"#pragma once\n"
"#include <cstddef>\n"
"#include <cstdint>\n"
"static const uint8_t k${VAR}[] = {\n"
"${SPV_BYTES};\n"
"static const size_t k${VAR}_size = ${BYTE_COUNT};\n"
)