Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate the Vulkan shaders at build time #2199

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
}
Loading