From 4d8223731a2bb277fd590c07977b085b4560a4c8 Mon Sep 17 00:00:00 2001 From: nv-jdeligiannis <84378738+nv-jdeligiannis@users.noreply.github.com> Date: Tue, 21 Mar 2023 20:39:21 +0100 Subject: [PATCH] Switching shader compiler to NVIDIAGameWorks/ShaderMake (#35) * migrating to ShaderMake * bumping version to 1.0.1 --- .gitignore | 3 + .gitmodules | 3 + omm-sdk/CMakeLists.txt | 68 ++++---- omm-sdk/ShaderCompilation.cmake | 260 ++++++++++++++++--------------- omm-sdk/include/omm.h | 2 +- omm-sdk/scripts/omm_header_c.txt | 2 +- omm-sdk/shaders/platform.hlsli | 10 +- omm-sdk/src/version.h | 2 +- tests/CMakeLists.txt | 7 + tests/test_basic.cpp | 105 +++++++++++++ thirdparty/CMakeLists.txt | 7 +- thirdparty/ShaderMake | 1 + 12 files changed, 306 insertions(+), 164 deletions(-) create mode 160000 thirdparty/ShaderMake diff --git a/.gitignore b/.gitignore index 4dd7e33..b058d08 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,9 @@ cmake_install.cmake # make Makefile +# ShaderMake +shaders.cfg + # VS *.vcxproj* *.sln diff --git a/.gitmodules b/.gitmodules index 6beaeb4..8714bee 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "thirdparty/nvrhi"] path = thirdparty/nvrhi url = https://github.com/NVIDIAGameWorks/nvrhi.git +[submodule "thirdparty/ShaderMake"] + path = thirdparty/ShaderMake + url = https://github.com/NVIDIAGameWorks/ShaderMake.git diff --git a/omm-sdk/CMakeLists.txt b/omm-sdk/CMakeLists.txt index c92e72b..2ad98ba 100644 --- a/omm-sdk/CMakeLists.txt +++ b/omm-sdk/CMakeLists.txt @@ -1,9 +1,22 @@ +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. +# +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. cmake_minimum_required(VERSION 3.12) +if (WIN32) + set(DXIL_DEFAULT ON) +else() + set(DXIL_DEFAULT OFF) +endif() + option(OMM_DISABLE_INTERPROCEDURAL_OPTIMIZATION "disable interprocedural optimization" OFF) option(OMM_ENABLE_OPENMP "enable openmp" ON) -option(OMM_ENABLE_PRECOMPILED_SHADERS_DXIL "Embed precompiled dxil shaders. Require path to dxc.exe (normally located in WindowSDK)." ON) -option(OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV "Embed precompiled spirv shaders. Require path to VulkanSDK." ON) +option(OMM_ENABLE_PRECOMPILED_SHADERS_DXIL "Embedded precompiled DXIL shaders. Require path to dxc.exe (normally located in Window SDK)." ${DXIL_DEFAULT}) +option(OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV "Embedded precompiled SPIRV shaders. Require path to Vulkan SDK." ON) option(OMM_STATIC_LIBRARY "build static lib" OFF) option(OMM_CROSSCOMPILE_AARCH64 "cross compilation for aarch64" OFF) option(OMM_CROSSCOMPILE_X86_64 "cross compilation for x86_64" OFF) @@ -24,8 +37,6 @@ if (OMM_CROSSCOMPILE_X86_64) message(STATUS "CROSSCOMPILE_X86_64 enabled.") endif() -set(OMM_ENABLE_PRECOMPILED_SHADERS ${OMM_ENABLE_PRECOMPILED_SHADERS_DXIL} OR ${OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV} CACHE INTERNAL "") - set_property(GLOBAL PROPERTY USE_FOLDERS ON) set(CMAKE_CXX_STANDARD 20) @@ -39,40 +50,18 @@ if (OMM_DISABLE_INTERPROCEDURAL_OPTIMIZATION) endif() if ("${OMM_SHADER_OUTPUT_PATH}" STREQUAL "") - set(OMM_SHADER_OUTPUT_PATH "${PROJECT_BINARY_DIR}/shaders") + set(OMM_SHADER_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin/shaders") endif() # Vulkan resource offsets -set (OMM_VK_S_SHIFT 100) -set (OMM_VK_T_SHIFT 200) -set (OMM_VK_B_SHIFT 300) -set (OMM_VK_U_SHIFT 400) +set(OMM_VK_S_SHIFT 100) +set(OMM_VK_T_SHIFT 200) +set(OMM_VK_B_SHIFT 300) +set(OMM_VK_U_SHIFT 400) add_definitions(-DOMM_VK_S_SHIFT=${OMM_VK_S_SHIFT} -DOMM_VK_T_SHIFT=${OMM_VK_T_SHIFT} -DOMM_VK_B_SHIFT=${OMM_VK_B_SHIFT} -DOMM_VK_U_SHIFT=${OMM_VK_U_SHIFT}) -if (OMM_ENABLE_PRECOMPILED_SHADERS) - # include path for DXC and FXC (used in ShaderCompilation.cmake) - set(OMM_SHADER_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/shaders") - set(OMM_HEADER_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/shaders") - - # list HLSL shaders and headers - file(GLOB_RECURSE OMM_HLSL_FILES "shaders/*.hlsl") - file(GLOB_RECURSE OMM_HLSL_INCLUDE "shaders/*.hlsli") - - # Vulkan resource offsets +if (OMM_ENABLE_PRECOMPILED_SHADERS_DXIL OR OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) include(ShaderCompilation.cmake) - - # create "build/Shaders" - add_custom_target(CreateFolderForShaders ALL - COMMAND ${CMAKE_COMMAND} -E make_directory "${OMM_SHADER_OUTPUT_PATH}") - set_property(TARGET CreateFolderForShaders PROPERTY FOLDER "${OMM_PROJECT_FOLDER}/CustomScripts") - - # define build rules for the shaders - list_hlsl_headers("${OMM_HLSL_FILES}" OMM_HEADER_FILES) - list_hlsl_headers("${OMM_HLSL_INCLUDE}" OMM_HEADER_FILES) - list_hlsl_shaders("${OMM_HLSL_FILES}" "${OMM_HEADER_FILES}" OMM_SHADER_FILES) - add_custom_target(Shaders ALL DEPENDS ${OMM_SHADER_FILES} SOURCES "${OMM_HEADER_FILES}") - add_dependencies(Shaders CreateFolderForShaders) - set_property(TARGET Shaders PROPERTY FOLDER "${OMM_PROJECT_FOLDER}/Shaders") endif() if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) @@ -108,11 +97,16 @@ endif() target_link_libraries(${TARGET_NAME} omm-shared glm stb_lib xxHash::xxhash) set_target_properties(${TARGET_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) -target_include_directories(${TARGET_NAME} PUBLIC "include" "shaders") +target_include_directories(${TARGET_NAME} PUBLIC "include") +target_include_directories(${TARGET_NAME} PRIVATE "shaders") set_property(TARGET ${TARGET_NAME} PROPERTY FOLDER "${OMM_PROJECT_FOLDER}") -if (OMM_ENABLE_PRECOMPILED_SHADERS) - add_dependencies(${TARGET_NAME} Shaders) +if (OMM_ENABLE_PRECOMPILED_SHADERS_DXIL) + add_dependencies(${TARGET_NAME} omm-shaders-dxil) +endif() + +if (OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) + add_dependencies(${TARGET_NAME} omm-shaders-spirv) endif() if (OMM_ENABLE_INTERFACE_GEN) @@ -144,7 +138,7 @@ if (OMM_ENABLE_INTERFACE_GEN) endif() endif() -if (OMM_ENABLE_PRECOMPILED_SHADERS) +if (OMM_ENABLE_PRECOMPILED_SHADERS_DXIL OR OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) target_include_directories(${TARGET_NAME} PRIVATE "${OMM_SHADER_OUTPUT_PATH}") endif() if (OMM_ENABLE_PRECOMPILED_SHADERS_DXIL) @@ -190,7 +184,7 @@ endif() set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "${OMM_PROJECT_FOLDER}") if (NOT ${OMM_OUTPUT_BIN_PATH} STREQUAL "") - message(STATUS "OMMBake output path: '${OMM_OUTPUT_BIN_PATH}'") + message(STATUS "OMM-SDK output path: '${OMM_OUTPUT_BIN_PATH}'") set_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${OMM_OUTPUT_BIN_PATH}/$") set_target_properties(${TARGET_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${OMM_OUTPUT_BIN_PATH}/$") set_target_properties(${TARGET_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${OMM_OUTPUT_BIN_PATH}/$") diff --git a/omm-sdk/ShaderCompilation.cmake b/omm-sdk/ShaderCompilation.cmake index 28f8be4..d00b091 100644 --- a/omm-sdk/ShaderCompilation.cmake +++ b/omm-sdk/ShaderCompilation.cmake @@ -1,155 +1,171 @@ -# DXC on Windows does not like forward slashes -if (WIN32) - string(REPLACE "/" "\\" OMM_SHADER_INCLUDE_PATH "${OMM_SHADER_INCLUDE_PATH}") - string(REPLACE "/" "\\" OMM_HEADER_INCLUDE_PATH "${OMM_HEADER_INCLUDE_PATH}") -endif() +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. +# +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +########################################################################## +###################### Shader Compilation Util ########################### +########################################################################## + +function(compile_shaders) + + if (OMM_SHADER_DEBUG_INFO) + set (DXC_ADDITIONAL_OPTIONS -Qembed_debug -Zi) + endif() -# Find DXC -if (WIN32) + set(options "") + set(oneValueArgs TARGET CONFIG FOLDER DXIL SPIRV DXBC SPIRV_DXC CFLAGS INCLUDE) + set(multiValueArgs SOURCES) + cmake_parse_arguments(params "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) - # DXIL - if (OMM_ENABLE_PRECOMPILED_SHADERS_DXIL) - if (DEFINED CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION) - set (OMM_WINDOWS_SDK_VERSION ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}) - elseif (DEFINED ENV{WindowsSDKLibVersion}) - string (REGEX REPLACE "\\\\$" "" OMM_WINDOWS_SDK_VERSION "$ENV{WindowsSDKLibVersion}") - else() - message(FATAL_ERROR "WindowsSDK is not installed. (CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION is not defined; WindowsSDKLibVersion is '$ENV{WindowsSDKLibVersion}')") - endif() + if (NOT params_TARGET) + message(FATAL_ERROR "compile_shaders: TARGET argument missing") + endif() + if (NOT params_CONFIG) + message(FATAL_ERROR "compile_shaders: CONFIG argument missing") + endif() - get_filename_component(OMM_WINDOWS_SDK_ROOT - "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;KitsRoot10]" ABSOLUTE) + # just add the source files to the project as documents, they are built by the script + set_source_files_properties(${params_SOURCES} PROPERTIES VS_TOOL_OVERRIDE "None") - set(OMM_WINDOWS_SDK_BIN "${OMM_WINDOWS_SDK_ROOT}/bin/${OMM_WINDOWS_SDK_VERSION}/x64") + add_custom_target(${params_TARGET} + DEPENDS ShaderMake + SOURCES ${params_SOURCES}) - find_program(OMM_DXC_PATH "${OMM_WINDOWS_SDK_BIN}/dxc") + if (NOT params_INCLUDE) + set(INCLUDE_PATH "") + else() + set(INCLUDE_PATH -I ${params_INCLUDE}) + endif() - if (OMM_DXC_PATH) - message(STATUS "DXIL dxc.exe was found in ${OMM_DXC_PATH}" ) - else() - message(FATAL_ERROR "DXIL dxc.exe was not found on the system. Is the Windows SDK installed? Resolve this by either: - 1. Disable embedded DXIL shaders OMM_ENABLE_PRECOMPILED_SHADERS_DXIL OFF - 2. Provide a custom path to DXIL dxc.exe (OMM_DXC_PATH) - 3. Install Windows SDK on the system\n") + if (params_DXIL) + if (NOT DXC_PATH) + message(FATAL_ERROR "compile_shaders: DXC not found --- please set DXC_PATH to the full path to the DXC binary") endif() - message(STATUS "DXIL will be generated by: ${OMM_DXC_PATH}") + if (NOT params_CFLAGS) + set(CFLAGS "-Zi -Qembed_debug -O3 -WX") + else() + set(CFLAGS ${params_CFLAGS}) + endif() + add_custom_command(TARGET ${params_TARGET} PRE_BUILD + COMMAND ShaderMake + --useExe + --header + --config=${params_CONFIG} + --out ${params_DXIL} + --compiler ${DXC_PATH} + --platform=DXIL + ${INCLUDE_PATH} + ) endif() - # SPIRV - if (OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) - - find_program(OMM_VULKAN_DXC_SPIRV_PATH "$ENV{VULKAN_SDK}/Bin/dxc") + if (params_SPIRV) + if (NOT DXC_SPIRV_PATH) + message(FATAL_ERROR "compile_shaders: DXC for SPIR-V not found --- please set DXC_SPIRV_PATH to the full path to the DXC binary") + endif() - if (OMM_VULKAN_DXC_SPIRV_PATH) - message(STATUS "SPIRV dxc.exe was found in ${OMM_VULKAN_DXC_SPIRV_PATH}" ) + if (NOT params_CFLAGS) + set(CFLAGS "$,-Zi,> -fspv-target-env=vulkan1.2 -O3 -WX") else() - message(FATAL_ERROR "SPIRV dxc.exe was not found on the system. Is the Vulkan SDK installed? Resolve this by either: - 1. Disable embedded SPIRV shaders OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV OFF - 2. Provide a custom path to Vulkan dxc.exe (OMM_VULKAN_DXC_SPIRV_PATH) - 3. Install Vulkan SDK on the system and make sure environment variable VULKAN_SDK is set.\n") + set(CFLAGS ${params_CFLAGS}) endif() - message(STATUS "SPIRV will be generated by: ${OMM_VULKAN_DXC_SPIRV_PATH}") - endif() -else() - - set(OMM_ENABLE_PRECOMPILED_SHADERS_DXIL OFF CACHE BOOL "" FORCE) - - if (OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) - # On Linux, VulkanSDK does not set VULKAN_SDK, but DXC can be called directly assuming vulkan-sdk is installed - find_program(OMM_VULKAN_DXC_SPIRV_PATH "dxc") - if (OMM_VULKAN_DXC_SPIRV_PATH) - message(STATUS "SPIRV dxc was found in ${OMM_VULKAN_DXC_SPIRV_PATH}" ) + if (WIN32) + add_custom_command(TARGET ${params_TARGET} PRE_BUILD + COMMAND ShaderMake + --useExe + --header + --vulkanVersion=1.2 + --config=${params_CONFIG} + --out ${params_SPIRV} + --compiler ${DXC_SPIRV_PATH} + --platform=SPIRV + --sRegShift=${OMM_VK_S_SHIFT} + --tRegShift=${OMM_VK_T_SHIFT} + --bRegShift=${OMM_VK_B_SHIFT} + --uRegShift=${OMM_VK_U_SHIFT} + ${INCLUDE_PATH} + ) else() - message(FATAL_ERROR "SPIRV dxc was not found on the system. Is the Vulkan SDK installed? Resolve this by either: - 1. Disable embedded SPIRV shaders OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV OFF - 2. Provide a custom path to Vulkan dxc.exe (OMM_VULKAN_DXC_SPIRV_PATH) - 3. Install Vulkan SDK on the system and make sure environment variable VULKAN_SDK is set.\n") + add_custom_command(TARGET ${params_TARGET} PRE_BUILD + COMMAND ShaderMake + --header + --vulkanVersion=1.2 + --config=${params_CONFIG} + --out ${params_SPIRV} + --compiler ${DXC_SPIRV_PATH} + --platform=SPIRV + --sRegShift=${OMM_VK_S_SHIFT} + --tRegShift=${OMM_VK_T_SHIFT} + --bRegShift=${OMM_VK_B_SHIFT} + --uRegShift=${OMM_VK_U_SHIFT} + ${INCLUDE_PATH} + ) endif() - endif() -endif() + endif() + + if(params_FOLDER) + set_target_properties(${params_TARGET} PROPERTIES FOLDER ${params_FOLDER}) + endif() +endfunction() -function(get_shader_profile_from_name FILE_NAME DXC_PROFILE) +function(util_get_shader_profile_from_name FILE_NAME DXC_PROFILE) get_filename_component(EXTENSION ${FILE_NAME} EXT) if ("${EXTENSION}" STREQUAL ".cs.hlsl") - set(DXC_PROFILE "cs_6_0" PARENT_SCOPE) + set(DXC_PROFILE "cs" PARENT_SCOPE) endif() if ("${EXTENSION}" STREQUAL ".vs.hlsl") - set(DXC_PROFILE "vs_6_0" PARENT_SCOPE) + set(DXC_PROFILE "vs" PARENT_SCOPE) endif() if ("${EXTENSION}" STREQUAL ".gs.hlsl") - set(DXC_PROFILE "gs_6_0" PARENT_SCOPE) + set(DXC_PROFILE "gs" PARENT_SCOPE) endif() if ("${EXTENSION}" STREQUAL ".ps.hlsl") - set(DXC_PROFILE "ps_6_0" PARENT_SCOPE) + set(DXC_PROFILE "ps" PARENT_SCOPE) endif() endfunction() -macro(list_hlsl_headers OMM_HLSL_FILES OMM_HEADER_FILES) - foreach(FILE_NAME ${OMM_HLSL_FILES}) - set(DXC_PROFILE "") - get_shader_profile_from_name(${FILE_NAME} DXC_PROFILE) - if ("${DXC_PROFILE}" STREQUAL "") - list(APPEND OMM_HEADER_FILES ${FILE_NAME}) - set_source_files_properties(${FILE_NAME} PROPERTIES VS_TOOL_OVERRIDE "None") - endif() - endforeach() -endmacro() +function(util_generate_shader_config_file OUT_FILE_NAME DIR DEFINES) + file(GLOB_RECURSE HLSL_FILES "${CMAKE_CURRENT_SOURCE_DIR}/${DIR}/*.hlsl") + + set(out_content "") + foreach(FILE_NAME ${HLSL_FILES}) + get_filename_component(NAME_ONLY ${FILE_NAME} NAME) + util_get_shader_profile_from_name(${FILE_NAME} DXC_PROFILE) + set(out_content "${out_content}${DIR}/${NAME_ONLY} -T ${DXC_PROFILE} -E main ${DEFINES}\n") + endforeach() -set (OMM_DXC_VK_SHIFTS - -fvk-s-shift ${OMM_VK_S_SHIFT} 0 -fvk-s-shift ${OMM_VK_S_SHIFT} 1 -fvk-s-shift ${OMM_VK_S_SHIFT} 2 - -fvk-t-shift ${OMM_VK_T_SHIFT} 0 -fvk-t-shift ${OMM_VK_T_SHIFT} 1 -fvk-t-shift ${OMM_VK_T_SHIFT} 2 - -fvk-b-shift ${OMM_VK_B_SHIFT} 0 -fvk-b-shift ${OMM_VK_B_SHIFT} 1 -fvk-b-shift ${OMM_VK_B_SHIFT} 2 - -fvk-u-shift ${OMM_VK_U_SHIFT} 0 -fvk-u-shift ${OMM_VK_U_SHIFT} 1 -fvk-u-shift ${OMM_VK_U_SHIFT} 2) + file(WRITE ${OUT_FILE_NAME} ${out_content}) +endfunction() -if (OMM_SHADER_DEBUG_INFO) -set (DXC_ADDITIONAL_OPTIONS -Qembed_debug -Zi) +# Let CMake generate the shaders.cfg file +util_generate_shader_config_file( + "shaders.cfg" + "shaders" + "" +) + +if (OMM_ENABLE_PRECOMPILED_SHADERS_DXIL) + compile_shaders( + TARGET omm-shaders-dxil + CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/shaders.cfg + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/shaders + FOLDER "shaders" + DXIL ${PROJECT_BINARY_DIR}/bin + ) endif() -macro(list_hlsl_shaders OMM_HLSL_FILES OMM_HEADER_FILES OMM_SHADER_FILES) - foreach(FILE_NAME ${OMM_HLSL_FILES}) - get_filename_component(NAME_ONLY ${FILE_NAME} NAME) - string(REGEX REPLACE "\\.[^.]*$" "" NAME_ONLY ${NAME_ONLY}) - string(REPLACE "." "_" BYTECODE_ARRAY_NAME "${NAME_ONLY}") - set(DXC_PROFILE "") - set(OUTPUT_PATH_DXIL "${OMM_SHADER_OUTPUT_PATH}/${NAME_ONLY}.dxil") - set(OUTPUT_PATH_SPIRV "${OMM_SHADER_OUTPUT_PATH}/${NAME_ONLY}.spirv") - get_shader_profile_from_name(${FILE_NAME} DXC_PROFILE) - - # add DXC compilation step (DXIL) - if (OMM_ENABLE_PRECOMPILED_SHADERS_DXIL) - if (NOT "${DXC_PROFILE}" STREQUAL "" AND NOT "${OMM_DXC_PATH}" STREQUAL "") - add_custom_command( - OUTPUT ${OUTPUT_PATH_DXIL} ${OUTPUT_PATH_DXIL}.h - COMMAND ${OMM_DXC_PATH} -E main -DCOMPILER_DXC=1 -T ${DXC_PROFILE} -I "${OMM_HEADER_INCLUDE_PATH}" ${DXC_ADDITIONAL_OPTIONS} - -I "${OMM_SHADER_INCLUDE_PATH}" ${FILE_NAME} - -Vn g_${BYTECODE_ARRAY_NAME}_dxil -Fh ${OUTPUT_PATH_DXIL}.h -Fo ${OUTPUT_PATH_DXIL} - MAIN_DEPENDENCY ${FILE_NAME} - DEPENDS ${OMM_HEADER_FILES} - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/shaders" - VERBATIM - ) - list(APPEND OMM_SHADER_FILES ${OUTPUT_PATH_DXIL} ${OUTPUT_PATH_DXIL}.h) - endif() - endif() - # add one more DXC compilation step (SPIR-V) - if (OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) - if (NOT "${DXC_PROFILE}" STREQUAL "") - add_custom_command( - OUTPUT ${OUTPUT_PATH_SPIRV} ${OUTPUT_PATH_SPIRV}.h - COMMAND ${OMM_VULKAN_DXC_SPIRV_PATH} -E main -DCOMPILER_DXC=1 -T ${DXC_PROFILE} - -I "${OMM_HEADER_INCLUDE_PATH}" ${DXC_ADDITIONAL_OPTIONS} -I "${OMM_SHADER_INCLUDE_PATH}" - ${FILE_NAME} -spirv -Vn g_${BYTECODE_ARRAY_NAME}_spirv -Fh ${OUTPUT_PATH_SPIRV}.h - -Fo ${OUTPUT_PATH_SPIRV} ${OMM_DXC_VK_SHIFTS} -fspv-target-env=vulkan1.1 - MAIN_DEPENDENCY ${FILE_NAME} - DEPENDS ${OMM_HEADER_FILES} - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/shaders" - VERBATIM - ) - list(APPEND OMM_SHADER_FILES ${OUTPUT_PATH_SPIRV} ${OUTPUT_PATH_SPIRV}.h) - endif() - endif() - endforeach() -endmacro() +if (OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) + compile_shaders( + TARGET omm-shaders-spirv + CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/shaders.cfg + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/shaders + FOLDER "shaders" + SPIRV ${PROJECT_BINARY_DIR}/bin + ) +endif() diff --git a/omm-sdk/include/omm.h b/omm-sdk/include/omm.h index 04261cd..6a07bf1 100644 --- a/omm-sdk/include/omm.h +++ b/omm-sdk/include/omm.h @@ -16,7 +16,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #define OMM_VERSION_MAJOR 1 #define OMM_VERSION_MINOR 0 -#define OMM_VERSION_BUILD 0 +#define OMM_VERSION_BUILD 1 #define OMM_MAX_TRANSIENT_POOL_BUFFERS 8 diff --git a/omm-sdk/scripts/omm_header_c.txt b/omm-sdk/scripts/omm_header_c.txt index b37a394..3e8581b 100644 --- a/omm-sdk/scripts/omm_header_c.txt +++ b/omm-sdk/scripts/omm_header_c.txt @@ -16,7 +16,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #define OMM_VERSION_MAJOR 1 #define OMM_VERSION_MINOR 0 -#define OMM_VERSION_BUILD 0 +#define OMM_VERSION_BUILD 1 #define OMM_MAX_TRANSIENT_POOL_BUFFERS 8 diff --git a/omm-sdk/shaders/platform.hlsli b/omm-sdk/shaders/platform.hlsli index 9d01ad9..d410552 100644 --- a/omm-sdk/shaders/platform.hlsli +++ b/omm-sdk/shaders/platform.hlsli @@ -23,7 +23,15 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. // BINDINGS //================================================================================================================================= -#if( defined COMPILER_FXC || defined COMPILER_DXC ) +#ifndef COMPILER_DXC +#define COMPILER_DXC (1) +#endif + +#ifndef COMPILER_FXC +#define COMPILER_FXC (0) +#endif + +#if( COMPILER_FXC || COMPILER_DXC ) #define OMM_CONSTANTS_START(name) \ struct name { diff --git a/omm-sdk/src/version.h b/omm-sdk/src/version.h index 4ae3b1e..caaac3b 100644 --- a/omm-sdk/src/version.h +++ b/omm-sdk/src/version.h @@ -13,7 +13,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #define VERSION_MAJOR 1 #define VERSION_MINOR 0 -#define VERSION_BUILD 0 +#define VERSION_BUILD 1 #define VERSION_REVISION 0 #define VERSION_STRING STR(VERSION_MAJOR.VERSION_MINOR.VERSION_BUILD.VERSION_REVISION) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3eae569..05ce577 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -46,3 +46,10 @@ include_directories(tests ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) target_link_libraries(tests gtest gtest_main stb_lib omm-shared omm-sdk ${OMM_GPU_LIBS} ) set_target_properties(tests PROPERTIES FOLDER "${OMM_PROJECT_FOLDER}") + +if (OMM_ENABLE_PRECOMPILED_SHADERS_DXIL) + target_compile_definitions(tests PRIVATE OMM_ENABLE_PRECOMPILED_SHADERS_DXIL) +endif() +if (OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) + target_compile_definitions(tests PRIVATE OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) +endif() diff --git a/tests/test_basic.cpp b/tests/test_basic.cpp index 45083a0..cd6616b 100644 --- a/tests/test_basic.cpp +++ b/tests/test_basic.cpp @@ -82,6 +82,7 @@ namespace { void TearDown() override { EXPECT_EQ(omm::DestroyBaker(_baker), omm::Result::SUCCESS); } + void TestShaders(omm::Gpu::RenderAPI api, bool expectingShaders); omm::Baker _baker = 0; }; @@ -98,6 +99,110 @@ namespace { EXPECT_EQ(res, omm::Result::SUCCESS); } + void GpuTest::TestShaders(omm::Gpu::RenderAPI renderAPI, bool expectingShaders) + { + omm::Gpu::PipelineConfigDesc cfg; + cfg.renderAPI = renderAPI; + + omm::Gpu::Pipeline pipeline = 0; + omm::Result res = omm::Gpu::CreatePipeline(_baker, cfg, &pipeline); + ASSERT_EQ(res, omm::Result::SUCCESS); + + const omm::Gpu::PipelineInfoDesc* outPipelineDescs = nullptr; + res = omm::Gpu::GetPipelineDesc(pipeline, &outPipelineDescs); + ASSERT_EQ(res, omm::Result::SUCCESS); + + for (uint32_t i = 0; i < outPipelineDescs->pipelineNum; ++i) + { + const omm::Gpu::PipelineDesc& pipeline = outPipelineDescs->pipelines[i]; + ASSERT_LT(pipeline.type, omm::Gpu::PipelineType::MAX_NUM); + + switch (pipeline.type) + { + case omm::Gpu::PipelineType::Compute: + { + if (expectingShaders) + { + ASSERT_NE(pipeline.compute.computeShader.data, nullptr); + ASSERT_NE(pipeline.compute.computeShader.size, 0); + } + else + { + ASSERT_EQ(pipeline.compute.computeShader.data, nullptr); + ASSERT_EQ(pipeline.compute.computeShader.size, 0); + } + break; + } + case omm::Gpu::PipelineType::Graphics: + { + if (expectingShaders) + { + bool anyShader = false; + if (pipeline.graphics.pixelShader.data) + { + anyShader = true; + ASSERT_NE(pipeline.graphics.pixelShader.size, 0); + } + + if (pipeline.graphics.geometryShader.data) + { + anyShader = true; + ASSERT_NE(pipeline.graphics.geometryShader.size, 0); + } + + if (pipeline.graphics.vertexShader.data) + { + anyShader = true; + ASSERT_NE(pipeline.graphics.vertexShader.size, 0); + } + + ASSERT_EQ(anyShader, true); + } + else + { + ASSERT_EQ(pipeline.graphics.pixelShader.data, nullptr); + ASSERT_EQ(pipeline.graphics.pixelShader.size, 0); + + ASSERT_EQ(pipeline.graphics.geometryShader.data, nullptr); + ASSERT_EQ(pipeline.graphics.geometryShader.size, 0); + + ASSERT_EQ(pipeline.graphics.vertexShader.data, nullptr); + ASSERT_EQ(pipeline.graphics.vertexShader.size, 0); + } + break; + } + default: + { + ASSERT_EQ(false, true); + break; + } + } + } + + res = omm::Gpu::DestroyPipeline(_baker, pipeline); + EXPECT_EQ(res, omm::Result::SUCCESS); + } + +#if defined(OMM_ENABLE_PRECOMPILED_SHADERS_DXIL) + TEST_F(GpuTest, ShadersDXIL) { + TestShaders(omm::Gpu::RenderAPI::DX12, true /*expectingShaders*/); + } +#else + TEST_F(GpuTest, NoShadersDXIL) { + TestShaders(omm::Gpu::RenderAPI::DX12, false /*expectingShaders*/); + } +#endif + +#if defined(OMM_ENABLE_PRECOMPILED_SHADERS_SPIRV) + TEST_F(GpuTest, ShadersSPIRV) { + TestShaders(omm::Gpu::RenderAPI::Vulkan, true /*expectingShaders*/); + } +#else + TEST_F(GpuTest, NoShadersSPIRV) { + TestShaders(omm::Gpu::RenderAPI::Vulkan, false /*expectingShaders*/); + } +#endif + class TextureTest : public ::testing::Test { protected: void SetUp() override { diff --git a/thirdparty/CMakeLists.txt b/thirdparty/CMakeLists.txt index ff87766..4a471ad 100644 --- a/thirdparty/CMakeLists.txt +++ b/thirdparty/CMakeLists.txt @@ -46,4 +46,9 @@ set_target_properties("gmock" "gmock_main" "gtest_main" "gtest" PROPERTIES FOLDE set(BENCHMARK_ENABLE_INSTALL OFF) set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE) add_subdirectory("gbenchmark") -set_target_properties("benchmark" "benchmark_main" PROPERTIES FOLDER "${OMM_PROJECT_FOLDER}/Thirdparty") \ No newline at end of file +set_target_properties("benchmark" "benchmark_main" PROPERTIES FOLDER "${OMM_PROJECT_FOLDER}/Thirdparty") + +# ShaderMake +add_subdirectory("ShaderMake") +set_property(TARGET ShaderMake PROPERTY FOLDER "${OMM_PROJECT_FOLDER}/Thirdparty") +set_property(TARGET ShaderMakeBlob PROPERTY FOLDER "${OMM_PROJECT_FOLDER}/Thirdparty") \ No newline at end of file diff --git a/thirdparty/ShaderMake b/thirdparty/ShaderMake new file mode 160000 index 0000000..d6b1d48 --- /dev/null +++ b/thirdparty/ShaderMake @@ -0,0 +1 @@ +Subproject commit d6b1d48af8fa4d2075beababffc2cd2c0f03080f