Skip to content

Commit

Permalink
Generate the Vulkan shaders at build time
Browse files Browse the repository at this point in the history
Use `add_custom_command` and `add_custom_target` instead of
`execute_process` so the generation of the Vulkan shader is done at
build time and not configuration time.

Use `configure_file` instead of string replacement.

Signed-off-by: Ahmed Hesham <ahmed.hesham@arm.com>
  • Loading branch information
ahesham-arm committed Dec 19, 2024
1 parent 2316c63 commit 1f9ba06
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 70 deletions.
2 changes: 2 additions & 0 deletions test_conformance/vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ include_directories("../common/vulkan_wrapper")
add_subdirectory(shaders)

include(../CMakeCommon.txt)

add_dependencies(${${MODULE_NAME}_OUT} vulkan_shaders)
72 changes: 33 additions & 39 deletions test_conformance/vulkan/shaders/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# CMP0007:NEW - Don't ignore empty elements in a list
cmake_policy(SET CMP0007 NEW)
find_program(
Vulkan_glslang_binary
NAMES glslang
Expand All @@ -10,40 +8,36 @@ if(${Vulkan_glslang_binary} STREQUAL "Vulkan_glslang_binary-NOTFOUND")
else()
message(STATUS "Found glslang: ${Vulkan_glslang_binary}")

set(IMAGE2D_SHADER_IN_FILE "image2D.comp")
set(IMAGE2D_SHADER_TMP_OUT_FILE "tmp_image2D.comp")
set(BUFFER_SHADER_IN_FILE "buffer")
set(IMAGE2D_FORMATS_LIST_IN_FILE "image2D_test_formats.txt")

file(READ ${IMAGE2D_SHADER_IN_FILE} IMAGE2D_SHADER_UNFORMAT_CONTENT)
file(STRINGS ${IMAGE2D_FORMATS_LIST_IN_FILE} IMAGE2D_FORMATS_LIST)

if(NOT DEFINED VULKAN_TEST_RESOURCES)
set(VULKAN_TEST_RESOURCES ${CMAKE_CURRENT_BINARY_DIR})
endif()

foreach(IMAGE2D_FORMAT ${IMAGE2D_FORMATS_LIST})
list(GET IMAGE2D_FORMAT 1 GLSL_FORMAT)
list(GET IMAGE2D_FORMAT 2 GLSL_TYPE_PREFIX)
string(REPLACE "GLSL_FORMAT" "${GLSL_FORMAT}" IMAGE2D_SHADER_CONTENT "${IMAGE2D_SHADER_UNFORMAT_CONTENT}")
string(REPLACE "GLSL_TYPE_PREFIX" "${GLSL_TYPE_PREFIX}" IMAGE2D_SHADER_CONTENT "${IMAGE2D_SHADER_CONTENT}")
file(WRITE ${IMAGE2D_SHADER_TMP_OUT_FILE} "${IMAGE2D_SHADER_CONTENT}")
execute_process(
COMMAND ${Vulkan_glslang_binary} --target-env vulkan1.0 -o ${VULKAN_TEST_RESOURCES}/image2D_${GLSL_FORMAT}.spv ${IMAGE2D_SHADER_TMP_OUT_FILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE commandStatus
OUTPUT_QUIET)
if(commandStatus AND NOT commandStatus EQUAL 0)
message(FATAL_ERROR "shader -> spir-v compilation failed")
endif()
endforeach(IMAGE2D_FORMAT)
execute_process(
COMMAND ${Vulkan_glslang_binary} --target-env vulkan1.0 -o ${VULKAN_TEST_RESOURCES}/${BUFFER_SHADER_IN_FILE}.spv ${BUFFER_SHADER_IN_FILE}.comp
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE commandStatus
OUTPUT_QUIET)
if(commandStatus AND NOT commandStatus EQUAL 0)
message(FATAL_ERROR "shader -> spir-v compilation failed")
endif()
file(REMOVE ${IMAGE2D_SHADER_TMP_OUT_FILE})
endif()
set(vulkan_spirv_files "")

add_custom_command(
OUTPUT buffer.spv
COMMAND ${Vulkan_glslang_binary}
--target-env vulkan1.0
-o ${CMAKE_CURRENT_BINARY_DIR}/buffer.spv
${CMAKE_CURRENT_SOURCE_DIR}/buffer.comp
DEPENDS buffer.comp
VERBATIM)
list(APPEND vulkan_spirv_files ${CMAKE_CURRENT_BINARY_DIR}/buffer.spv)

file(STRINGS "image2D_test_formats.txt" image2D_formats)

foreach(format ${image2D_formats})
list(GET format 1 GLSL_FORMAT)
list(GET format 2 GLSL_TYPE_PREFIX)

configure_file(image2D.comp.in image2D_${GLSL_FORMAT}.comp)

add_custom_command(
OUTPUT image2D_${GLSL_FORMAT}.spv
COMMAND ${Vulkan_glslang_binary}
--target-env vulkan1.0
-o ${CMAKE_CURRENT_BINARY_DIR}/image2D_${GLSL_FORMAT}.spv
${CMAKE_CURRENT_BINARY_DIR}/image2D_${GLSL_FORMAT}.comp
DEPENDS image2D_${GLSL_FORMAT}.comp
VERBATIM)
list(APPEND vulkan_spirv_files ${CMAKE_CURRENT_BINARY_DIR}/image2D_${GLSL_FORMAT}.spv)
endforeach()

add_custom_target(vulkan_shaders DEPENDS ${vulkan_spirv_files})
endif()
31 changes: 0 additions & 31 deletions test_conformance/vulkan/shaders/image2D.comp

This file was deleted.

0 comments on commit 1f9ba06

Please sign in to comment.