From ed3c76c0b0d91f65e71749b00f79620316237247 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Sat, 30 Nov 2024 05:06:08 -0600 Subject: [PATCH] Use shader compilation from command line This avoid dependency compilation and linking --- CMakeLists.txt | 18 +++++++++++++----- bootstrap/shaders/.gitignore | 1 + bootstrap/std/shader_compilation.c | 22 +++++++++++++++++++++- project.sh | 1 + 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 bootstrap/shaders/.gitignore diff --git a/CMakeLists.txt b/CMakeLists.txt index ef8d115..89b4849 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ cmake_print_variables("CXX flags Release: ${CMAKE_CXX_FLAGS_RELEASE}") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_C_STANDARD 23) set(CMAKE_CXX_STANDARD 23) +set(SHADER_COMPILATION_USE_SOURCE OFF CACHE BOOL "Variable to control if to invoke command line for shader compilation or not") set(LIBRARY_NAME "std") set(RUNNER_NAME "runner") @@ -34,6 +35,11 @@ else() endif() set (USE_LLVM ${BB_IS_CI} CACHE BOOL "Variable that determines if LLVM should be linked to the target") +if (SHADER_COMPILATION_USE_SOURCE) + set(SHADER_COMPILATION_USE_SOURCE_C_FLAG "-DSHADER_COMPILATION_USE_SOURCE=1") +else() + set(SHADER_COMPILATION_USE_SOURCE_C_FLAG "-DSHADER_COMPILATION_USE_SOURCE=0") +endif() add_compile_options( -pedantic @@ -44,7 +50,7 @@ add_compile_options( -fansi-escape-codes -ferror-limit=1 -march=native - ${BB_IS_CI_C_FLAG} -DCOMPILER_NAME=\"${COMPILER_NAME}\" -DBUILD_DIR=\"${CMAKE_BINARY_DIR}\" -DBB_DIR=\"${BB_DIR}\" + ${BB_IS_CI_C_FLAG} -DCOMPILER_NAME=\"${COMPILER_NAME}\" -DBUILD_DIR=\"${CMAKE_BINARY_DIR}\" -DBB_DIR=\"${BB_DIR}\" ${SHADER_COMPILATION_USE_SOURCE_C_FLAG} ) include_directories("bootstrap/include") @@ -185,10 +191,12 @@ if (USE_LLVM) endif() if (NOT BB_IS_CI) - add_subdirectory(dependencies/SPIRV-Headers-vulkan-sdk-1.3.296.0) - add_subdirectory(dependencies/SPIRV-Tools-vulkan-sdk-1.3.296.0) - add_subdirectory(dependencies/glslang-15.0.0) - target_link_libraries(${COMPILER_NAME} PRIVATE glslang::glslang glslang::SPIRV glslang::glslang-default-resource-limits) + if (SHADER_COMPILATION_USE_SOURCE) + add_subdirectory(dependencies/SPIRV-Headers-vulkan-sdk-1.3.296.0) + add_subdirectory(dependencies/SPIRV-Tools-vulkan-sdk-1.3.296.0) + add_subdirectory(dependencies/glslang-15.0.0) + target_link_libraries(${COMPILER_NAME} PRIVATE glslang::glslang glslang::SPIRV glslang::glslang-default-resource-limits) + endif() set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) diff --git a/bootstrap/shaders/.gitignore b/bootstrap/shaders/.gitignore new file mode 100644 index 0000000..27ca244 --- /dev/null +++ b/bootstrap/shaders/.gitignore @@ -0,0 +1 @@ +*.spv diff --git a/bootstrap/std/shader_compilation.c b/bootstrap/std/shader_compilation.c index fb3b576..a0938e2 100644 --- a/bootstrap/std/shader_compilation.c +++ b/bootstrap/std/shader_compilation.c @@ -1,7 +1,7 @@ #include +#if SHADER_COMPILATION_USE_SOURCE #include - // Required for use of glslang_default_resource #include @@ -85,9 +85,11 @@ fn SpirVBinary compileShaderToSPIRV_Vulkan(Arena* arena, glslang_stage_t stage, return bin; } +#endif String compile_shader(Arena* arena, String path, ShaderStage shader_stage) { +#if SHADER_COMPILATION_USE_SOURCE auto file = file_read(arena, path); glslang_stage_t stage; @@ -107,4 +109,22 @@ String compile_shader(Arena* arena, String path, ShaderStage shader_stage) .pointer = (u8*)result.words, .length = result.size * sizeof(*result.words), }; +#else + unused(shader_stage); + auto output_path = arena_join_string(arena, (Slice(String))array_to_slice(((String[]) { + path, + strlit(".spv"), + }))); + char* arguments[] = { + "/usr/bin/glslangValidator", + "-V", + string_to_c(path), + "-o", + string_to_c(output_path), + 0, + }; + run_command(arena, (CStringSlice)array_to_slice(arguments), 0); + auto file = file_read(arena, output_path); + return file; +#endif } diff --git a/project.sh b/project.sh index 2e7ac3c..713bfc8 100755 --- a/project.sh +++ b/project.sh @@ -84,6 +84,7 @@ case $BIRTH_OS in ;; esac +SHADER_DIR=$ BUILD_DIR=$ORIGINAL_DIR/build/$CMAKE_BUILD_TYPE mkdir -p $BUILD_DIR cmake . \