Skip to content

Commit

Permalink
Generate the Vulkan shaders at build time (#2199)
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.

Fixes #2179

Signed-off-by: Ahmed Hesham <[email protected]>
  • Loading branch information
ahesham-arm authored Jan 7, 2025
1 parent 28da017 commit 4486241
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 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()
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}
}
}
}

0 comments on commit 4486241

Please sign in to comment.