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

Add offline compilation for HLSL shaders with usage of DXC #990

Merged
Merged
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
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) 2020-2024, Arm Limited and Contributors
# Copyright (c) 2024, Mobica Limited
#
# SPDX-License-Identifier: Apache-2.0
#
Expand Down Expand Up @@ -30,6 +31,27 @@ endif()

cmake_minimum_required(VERSION 3.16)

# search for Vulkan SDK
find_package(Vulkan)

if(VULKAN_FOUND)
if(NOT Vulkan_dxc_EXECUTABLE)
find_program(Vulkan_dxc_EXECUTABLE
NAMES dxc
HINTS
"$ENV{VULKAN_SDK}/Bin"
"$ENV{VULKAN_SDK}/bin"
)
endif()
if(DEFINED Vulkan_dxc_EXECUTABLE)
message("Found DirectX Shader Compiler under ${Vulkan_dxc_EXECUTABLE}")
elseif()
message("Couldn't find DirectX Shader Compiler executable, make sure it is present in Vulkan SDK or add it manually via Vulkan_dxc_EXECUTABLE env variable")
endif()
else()
message("VULKAN_SDK path not found, HLSL shaders won't be compiled")
SaschaWillems marked this conversation as resolved.
Show resolved Hide resolved
endif()

# globally add VKB_DEBUG for the debug build
add_compile_definitions($<$<CONFIG:DEBUG>:VKB_DEBUG>)

Expand Down
82 changes: 67 additions & 15 deletions bldsys/cmake/sample_helper.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[[
Copyright (c) 2019-2024, Arm Limited and Contributors
Copyright (c) 2024, Mobica Limited

SPDX-License-Identifier: Apache-2.0

Expand All @@ -22,7 +23,7 @@ set(SCRIPT_DIR ${CMAKE_CURRENT_LIST_DIR})
function(add_sample)
set(options)
set(oneValueArgs ID CATEGORY AUTHOR NAME DESCRIPTION)
set(multiValueArgs FILES LIBS SHADER_FILES_GLSL)
set(multiValueArgs FILES LIBS SHADER_FILES_GLSL SHADER_FILES_HLSL)

cmake_parse_arguments(TARGET "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

Expand All @@ -40,13 +41,15 @@ function(add_sample)
LIBS
${TARGET_LIBS}
SHADER_FILES_GLSL
${TARGET_SHADER_FILES_GLSL})
${TARGET_SHADER_FILES_GLSL}
SHADER_FILES_HLSL
${TARGET_SHADER_FILES_HLSL})
endfunction()

function(add_sample_with_tags)
set(options)
set(oneValueArgs ID CATEGORY AUTHOR NAME DESCRIPTION)
set(multiValueArgs TAGS FILES LIBS SHADER_FILES_GLSL)
set(multiValueArgs TAGS FILES LIBS SHADER_FILES_GLSL SHADER_FILES_HLSL)

cmake_parse_arguments(TARGET "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

Expand All @@ -63,12 +66,14 @@ function(add_sample_with_tags)
endif()

# Add GLSL shader files for this sample
if (TARGET_SHADER_FILES_GLSL)
list(APPEND SHADER_FILES_GLSL ${TARGET_SHADER_FILES_GLSL})
foreach(SHADER_FILE_GLSL ${SHADER_FILES_GLSL})
list(APPEND SHADERS_GLSL "${PROJECT_SOURCE_DIR}/shaders/${SHADER_FILE_GLSL}")
endforeach()
endif()
foreach(SHADER_FILE_GLSL ${TARGET_SHADER_FILES_GLSL})
list(APPEND SHADERS_GLSL "${PROJECT_SOURCE_DIR}/shaders/${SHADER_FILE_GLSL}")
endforeach()

# Add HLSL shader files for this sample
foreach(SHADER_FILE_HLSL ${TARGET_SHADER_FILES_HLSL})
list(APPEND SHADERS_HLSL "${PROJECT_SOURCE_DIR}/shaders/${SHADER_FILE_HLSL}")
endforeach()

add_project(
TYPE "Sample"
Expand All @@ -84,7 +89,9 @@ function(add_sample_with_tags)
LIBS
${TARGET_LIBS}
SHADERS_GLSL
${SHADERS_GLSL})
${SHADERS_GLSL}
SHADERS_HLSL
${SHADERS_HLSL})

endfunction()

Expand Down Expand Up @@ -115,7 +122,7 @@ endfunction()
function(add_project)
set(options)
set(oneValueArgs TYPE ID CATEGORY AUTHOR NAME DESCRIPTION)
set(multiValueArgs TAGS FILES LIBS SHADERS_GLSL)
set(multiValueArgs TAGS FILES LIBS SHADERS_GLSL SHADERS_HLSL)

cmake_parse_arguments(TARGET "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

Expand All @@ -135,15 +142,20 @@ function(add_project)

source_group("\\" FILES ${TARGET_FILES})

# Add shaders to project group
if (SHADERS_GLSL)
# Add GLSL shaders to project group
if (TARGET_SHADERS_GLSL)
source_group("\\Shaders" FILES ${SHADERS_GLSL})
endif()

#Add HLSL shaders to project group
if (TARGET_SHADERS_HLSL)
source_group("\\Shaders" FILES ${SHADERS_HLSL})
endif()

if(${TARGET_TYPE} STREQUAL "Sample")
add_library(${PROJECT_NAME} OBJECT ${TARGET_FILES} ${SHADERS_GLSL})
add_library(${PROJECT_NAME} OBJECT ${TARGET_FILES} ${SHADERS_GLSL} ${SHADERS_HLSL})
elseif(${TARGET_TYPE} STREQUAL "Test")
add_library(${PROJECT_NAME} STATIC ${TARGET_FILES} ${SHADERS_GLSL})
add_library(${PROJECT_NAME} STATIC ${TARGET_FILES} ${SHADERS_GLSL} ${SHADERS_HLSL})
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)

Expand Down Expand Up @@ -181,4 +193,44 @@ endif()
if(VKB_DO_CLANG_TIDY)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_CLANG_TIDY "${VKB_DO_CLANG_TIDY}")
endif()

if(DEFINED Vulkan_dxc_EXECUTABLE AND DEFINED SHADERS_HLSL)
compile_hlsl_shaders(
SHADERS_HLSL ${TARGET_SHADERS_HLSL}
)
endif()
endfunction()

function(compile_hlsl_shaders)
asuessenbach marked this conversation as resolved.
Show resolved Hide resolved
set(options)
set(oneValueArgs)
set(multiValueArgs SHADERS_HLSL)

cmake_parse_arguments(TARGET "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

foreach(SHADER_FILE_HLSL ${TARGET_SHADERS_HLSL})
set(HLSL_SPV_FILE ${SHADER_FILE_HLSL}.spv)

if(${SHADER_FILE_HLSL} MATCHES "[^-]+.vert.hlsl")
set(DXC_PROFILE "vs_6_1")
elseif(${SHADER_FILE_HLSL} MATCHES "[^-]+.frag.hlsl")
set(DXC_PROFILE "ps_6_4")
elseif(${SHADER_FILE_HLSL} MATCHES "[^-]+.rgen..hlsl" OR ${SHADER_FILE_HLSL} MATCHES "[^-]+.rmiss.hlsl" OR ${SHADER_FILE_HLSL} MATCHES "[^-]+.rchit.hlsl")
set(DXC_PROFILE "lib_6_3")
set(DXC_TARGET "-fspv-target-env=vulkan1.1spirv1.4")
elseif(${SHADER_FILE_HLSL} MATCHES "[^-]+.comp.hlsl")
set(DXC_PROFILE "cs_6_1")
elseif(${SHADER_FILE_HLSL} MATCHES "[^-]+.geom.hlsl")
set(DXC_PROFILE "gs_6_1")
elseif(${SHADER_FILE_HLSL} MATCHES "[^-]+.tesc.hlsl")
set(DXC_PROFILE "hs_6_1")
elseif(${SHADER_FILE_HLSL} MATCHES "[^-]+.tese.hlsl")
set(DXC_PROFILE "ds_6_1")
elseif(${SHADER_FILE_HLSL} MATCHES "[^-]+.mesh.hlsl")
set(DXC_PROFILE "ms_6_1")
set(DXC_TARGET "-fspv-target-env=vulkan1.2")
endif()

execute_process(COMMAND ${Vulkan_dxc_EXECUTABLE} -spirv -T ${DXC_PROFILE} -E main -fspv-extension=SPV_KHR_ray_tracing ${DXC_TARGET} ${SHADER_FILE_HLSL} -Fo ${HLSL_SPV_FILE})
endforeach()
endfunction()
Loading