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"
)
@yuanotes
Copy link

Nice job!

@maelstrom0x8
Copy link

Thanks!

@psharm8
Copy link

psharm8 commented Dec 27, 2022

Thanks, work like a charm!!

@zixi-roy
Copy link

zixi-roy commented Aug 2, 2023

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

@VHSCODE
Copy link

VHSCODE commented Feb 11, 2024

Thank you !

@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