diff --git a/CMakeLists.txt b/CMakeLists.txt index b51c6ece6..0ed7f16e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,5 @@ # Copyright (c) 2020-2024, Arm Limited and Contributors +# Copyright (c) 2024, Mobica Limited # # SPDX-License-Identifier: Apache-2.0 # @@ -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") +endif() + # globally add VKB_DEBUG for the debug build add_compile_definitions($<$:VKB_DEBUG>) diff --git a/bldsys/cmake/sample_helper.cmake b/bldsys/cmake/sample_helper.cmake index e0e012c00..51ad16f22 100644 --- a/bldsys/cmake/sample_helper.cmake +++ b/bldsys/cmake/sample_helper.cmake @@ -1,5 +1,6 @@ #[[ Copyright (c) 2019-2024, Arm Limited and Contributors + Copyright (c) 2024, Mobica Limited SPDX-License-Identifier: Apache-2.0 @@ -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}) @@ -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}) @@ -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" @@ -84,7 +89,9 @@ function(add_sample_with_tags) LIBS ${TARGET_LIBS} SHADERS_GLSL - ${SHADERS_GLSL}) + ${SHADERS_GLSL} + SHADERS_HLSL + ${SHADERS_HLSL}) endfunction() @@ -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}) @@ -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) @@ -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) + 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()