From bc009d6e64d5255a89d98f554357e957bd7080e0 Mon Sep 17 00:00:00 2001 From: Ahmed Hesham Date: Thu, 19 Dec 2024 12:45:06 +0000 Subject: [PATCH] Generate the Vulkan shaders at build time 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 --- test_conformance/vulkan/CMakeLists.txt | 2 + .../vulkan/shaders/CMakeLists.txt | 72 +++++++++---------- .../shaders/{image2D.comp => image2D.comp.in} | 24 ++++--- 3 files changed, 50 insertions(+), 48 deletions(-) rename test_conformance/vulkan/shaders/{image2D.comp => image2D.comp.in} (67%) diff --git a/test_conformance/vulkan/CMakeLists.txt b/test_conformance/vulkan/CMakeLists.txt index 61a6014923..7b9bcc1801 100644 --- a/test_conformance/vulkan/CMakeLists.txt +++ b/test_conformance/vulkan/CMakeLists.txt @@ -32,3 +32,5 @@ include_directories("../common/vulkan_wrapper") add_subdirectory(shaders) include(../CMakeCommon.txt) + +add_dependencies(${${MODULE_NAME}_OUT} vulkan_shaders) diff --git a/test_conformance/vulkan/shaders/CMakeLists.txt b/test_conformance/vulkan/shaders/CMakeLists.txt index 258075504b..002ab9bf5c 100644 --- a/test_conformance/vulkan/shaders/CMakeLists.txt +++ b/test_conformance/vulkan/shaders/CMakeLists.txt @@ -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 @@ -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() \ No newline at end of file + 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() diff --git a/test_conformance/vulkan/shaders/image2D.comp b/test_conformance/vulkan/shaders/image2D.comp.in similarity index 67% rename from test_conformance/vulkan/shaders/image2D.comp rename to test_conformance/vulkan/shaders/image2D.comp.in index 6c0f6f26b3..0ad70a63d2 100644 --- a/test_conformance/vulkan/shaders/image2D.comp +++ b/test_conformance/vulkan/shaders/image2D.comp.in @@ -1,6 +1,6 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -#extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable +#extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable #define MAX_2D_IMAGES 5 #define MAX_2D_IMAGE_MIP_LEVELS 11 @@ -10,22 +10,28 @@ layout(binding = 0) buffer Params { uint32_t numImage2DDescriptors; }; -layout(binding = 1, GLSL_FORMAT ) uniform GLSL_TYPE_PREFIXimage2D image2DList[ MAX_2D_IMAGE_DESCRIPTORS ]; + +layout(binding = 1, ${GLSL_FORMAT}) uniform ${GLSL_TYPE_PREFIX}image2D image2DList[MAX_2D_IMAGE_DESCRIPTORS]; layout(local_size_x = 32, local_size_y = 32) in; -void main() { + +void main() +{ uvec3 numThreads = gl_NumWorkGroups * gl_WorkGroupSize; - for (uint32_t image2DIdx = 0; image2DIdx < numImage2DDescriptors; image2DIdx++) { + for (uint32_t image2DIdx = 0; image2DIdx < numImage2DDescriptors; image2DIdx++) + { ivec2 imageDim = imageSize(image2DList[image2DIdx]); uint32_t heightBy2 = imageDim.y / 2; - for (uint32_t row = gl_GlobalInvocationID.y; row < heightBy2; row += numThreads.y) { - for (uint32_t col = gl_GlobalInvocationID.x; col < imageDim.x; col += numThreads.x) { + for (uint32_t row = gl_GlobalInvocationID.y; row < heightBy2; row += numThreads.y) + { + for (uint32_t col = gl_GlobalInvocationID.x; col < imageDim.x; col += numThreads.x) + { ivec2 coordsA = ivec2(col, row); ivec2 coordsB = ivec2(col, imageDim.y - row - 1); - GLSL_TYPE_PREFIXvec4 dataA = imageLoad(image2DList[image2DIdx], coordsA); - GLSL_TYPE_PREFIXvec4 dataB = imageLoad(image2DList[image2DIdx], coordsB); + ${GLSL_TYPE_PREFIX}vec4 dataA = imageLoad(image2DList[image2DIdx], coordsA); + ${GLSL_TYPE_PREFIX}vec4 dataB = imageLoad(image2DList[image2DIdx], coordsB); imageStore(image2DList[image2DIdx], coordsA, dataB); imageStore(image2DList[image2DIdx], coordsB, dataA); } } } -} \ No newline at end of file +}