Created
August 8, 2016 21:00
-
-
Save evilactually/a0d191701cb48f157b05be7f74d79396 to your computer and use it in GitHub Desktop.
Compiling GLSL to SPIR-V from CMake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) |
Nice!
if "$<TARGET_FILE_DIR:YourMainTarget>" is equal to "${PROJECT_BINARY_DIR}" the shaders folder is overwritten and the spv files become empty. You need a guard to that POST_BUILD custom command, but you can't use generator expressions on "if"s ( "$<TARGET_FILE_DIR:YourMainTarget>" )
Awesome!
Thanks!
thanks!
Wow, I didn't know this would be useful to anyone. At least something came out out of that mess-of-a-vulkan project of mine, that I spent a whole year working on.
Thanks man!
Thanks!
Nice job!
Thanks!
Thanks, work like a charm!!
Thanks!
I just wanted less stuff in my main CMakeLists.txt
set (SHADER_SOURCES
"${CMAKE_SOURCE_DIR}/shaders/test.vert")
APPEND_GLSL_TO_TARGET( MY_PROJECT ${SHADER_SOURCES})
So i moved it all into a cmake/compileGLSL.cmake as a macro
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()
macro(APPEND_GLSL_TO_TARGET target sources)
set(GLSL_SOURCE_FILES ${sources})
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 cd
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(${target} Shaders)
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:${target}>/shaders/"
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_BINARY_DIR}/shaders"
"$<TARGET_FILE_DIR:${target}>/shaders" )
endmacro()
Again, thank you very much
Thank you !
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
Thanks pal 👍