Skip to content

Instantly share code, notes, and snippets.

@evilactually
Created August 8, 2016 21:00
Show Gist options
  • Save evilactually/a0d191701cb48f157b05be7f74d79396 to your computer and use it in GitHub Desktop.
Save evilactually/a0d191701cb48f157b05be7f74d79396 to your computer and use it in GitHub Desktop.
Compiling GLSL to SPIR-V from CMake
if (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "AMD64")
set(GLSL_VALIDATOR "$ENV{VULKAN_SDK}/Bin/glslangValidator.exe")
else()
set(GLSL_VALIDATOR "$ENV{VULKAN_SDK}/Bin32/glslangValidator.exe")
endif()
file(GLOB_RECURSE GLSL_SOURCE_FILES
"shaders/*.frag"
"shaders/*.vert"
)
foreach(GLSL ${GLSL_SOURCE_FILES})
get_filename_component(FILE_NAME ${GLSL} NAME)
set(SPIRV "${PROJECT_BINARY_DIR}/shaders/${FILE_NAME}.spv")
add_custom_command(
OUTPUT ${SPIRV}
COMMAND ${CMAKE_COMMAND} -E make_directory "${PROJECT_BINARY_DIR}/shaders/"
COMMAND ${GLSL_VALIDATOR} -V ${GLSL} -o ${SPIRV}
DEPENDS ${GLSL})
list(APPEND SPIRV_BINARY_FILES ${SPIRV})
endforeach(GLSL)
add_custom_target(
Shaders
DEPENDS ${SPIRV_BINARY_FILES}
)
add_dependencies(YourMainTarget Shaders)
add_custom_command(TARGET YourMainTarget POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:YourMainTarget>/shaders/"
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_BINARY_DIR}/shaders"
"$<TARGET_FILE_DIR:YourMainTarget>/shaders"
)
@PHuuhn
Copy link

PHuuhn commented May 6, 2024

Thank you, I learned something new today 🐔👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment