Skip to content

Commit

Permalink
Use -HV 2021 for DXC on PC when available (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
walbourn authored Aug 31, 2024
1 parent d43b5d3 commit b7680be
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
18 changes: 14 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ elseif(WINDOWS_STORE)
endif()

include(GNUInstallDirs)
include(build/CompilerAndLinker.cmake)

#--- Library
set(LIBRARY_HEADERS
Expand Down Expand Up @@ -252,19 +253,30 @@ else()
endif()

if(NOT USE_PREBUILT_SHADERS)
if(BUILD_DXIL_SHADERS AND VCPKG_TOOLCHAIN AND (NOT BUILD_SCARLETT_SHADERS) AND (NOT BUILD_XBOXONE_SHADERS))
if(BUILD_SCARLETT_SHADERS OR BUILD_XBOXONE_SHADERS)
elseif(BUILD_DXIL_SHADERS AND VCPKG_TOOLCHAIN)
message(STATUS "Using VCPKG for DirectXShaderCompiler (${VCPKG_HOST_TRIPLET}).")
find_program(DIRECTX_DXC_TOOL DXC.EXE
REQUIRED NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH NO_DEFAULT_PATH
HINTS ${DIRECTX_DXC_PATH}
"${CMAKE_CURRENT_LIST_DIR}/build/vcpkg_installed/${VCPKG_HOST_TRIPLET}/tools/directx-dxc")
elseif(BUILD_DXIL_SHADERS)
find_program(DIRECTX_DXC_TOOL DXC.EXE
HINTS "C:/Program Files (x86)/Windows Kits/10/bin/${CMAKE_SYSTEM_VERSION}/${DIRECTX_HOST_ARCH}"
"C:/Program Files (x86)/Windows Kits/10/bin/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}/${DIRECTX_HOST_ARCH}")
message(STATUS "Using DirectXShaderCompiler found in ${DIRECTX_DXC_TOOL}")
else()
find_program(DIRECTX_FXC_TOOL FXC.EXE
HINTS "C:/Program Files (x86)/Windows Kits/10/bin/${CMAKE_SYSTEM_VERSION}/${DIRECTX_HOST_ARCH}"
"C:/Program Files (x86)/Windows Kits/10/bin/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}/${DIRECTX_HOST_ARCH}")
message(STATUS "Using LegacyShaderCompiler found in ${DIRECTX_FXC_TOOL}")
endif()
add_custom_command(
OUTPUT "${COMPILED_SHADERS}/SpriteEffect_SpriteVertexShader.inc"
MAIN_DEPENDENCY "${PROJECT_SOURCE_DIR}/Src/Shaders/CompileShaders.cmd"
DEPENDS ${SHADER_SOURCES}
COMMENT "Generating HLSL shaders..."
COMMAND ${CMAKE_COMMAND} -E env CompileShadersOutput="${COMPILED_SHADERS}" $<$<BOOL:${DIRECTX_DXC_TOOL}>:DirectXShaderCompiler=${DIRECTX_DXC_TOOL}> CompileShaders.cmd ARGS ${ShaderOpts} > "${COMPILED_SHADERS}/compileshaders.log"
COMMAND ${CMAKE_COMMAND} -E env CompileShadersOutput="${COMPILED_SHADERS}" $<$<BOOL:${DIRECTX_DXC_TOOL}>:DirectXShaderCompiler=${DIRECTX_DXC_TOOL}> $<$<BOOL:${DIRECTX_FXC_TOOL}>:LegacyShaderCompiler=${DIRECTX_FXC_TOOL}> CompileShaders.cmd ARGS ${ShaderOpts} > "${COMPILED_SHADERS}/compileshaders.log"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/Src/Shaders"
USES_TERMINAL)
endif()
Expand Down Expand Up @@ -407,8 +419,6 @@ if(MSVC)
endif()
endif()

include(build/CompilerAndLinker.cmake)

target_compile_definitions(${PROJECT_NAME} PRIVATE ${COMPILER_DEFINES})
target_compile_options(${PROJECT_NAME} PRIVATE ${COMPILER_SWITCHES})
target_link_options(${PROJECT_NAME} PRIVATE ${LINKER_SWITCHES})
Expand Down
47 changes: 35 additions & 12 deletions Src/Shaders/CompileShaders.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if not exist %XBOXFXC% goto needxdk
goto continue

:continuegxdk
set XBOXOPTS=/D__XBOX_PER_THREAD_SCRATCH_SIZE_LIMIT_IN_BYTES=0
set XBOXOPTS=-HV 2021 /D__XBOX_PER_THREAD_SCRATCH_SIZE_LIMIT_IN_BYTES=0
if %2.==scarlett. (
set XBOXPREFIX=XboxGamingScarlett
set XBOXDXC="%GameDKLatest%\GXDK\bin\Scarlett\DXC.exe"
Expand All @@ -49,23 +49,36 @@ if not exist %XBOXDXC% goto needgxdk
goto continue

:continuedxil
set DXILOPTS=-HV 2021
if defined DirectXShaderCompiler goto dxcviaenv
set PCDXC="%WindowsSdkVerBinPath%%FXCARCH%\dxc.exe"
if exist %PCDXC% goto continue
if exist %PCDXC% goto dxilver
set PCDXC="%WindowsSdkBinPath%%WindowsSDKVersion%\%FXCARCH%\dxc.exe"
if exist %PCDXC% goto continue
if exist %PCDXC% goto dxilver

set PCDXC=dxc.exe
goto continue

:dxilver
if not defined WindowsSDKVersion goto continue
REM known DXC.EXE versions that don't support -HV 2021
if not "x%WindowsSDKVersion:10.0.19041.0=%"=="x%WindowsSDKVersion%" set DXILOPTS=%DXILOPTS:-HV 2021=%
if not "x%WindowsSDKVersion:10.0.20348.0=%"=="x%WindowsSDKVersion%" set DXILOPTS=%DXILOPTS:-HV 2021=%
if not "x%WindowsSDKVersion:10.0.22000.0=%"=="x%WindowsSDKVersion%" set DXILOPTS=%DXILOPTS:-HV 2021=%
goto continue

:dxcviaenv
set PCDXC="%DirectXShaderCompiler%"
if exist %PCDXC% goto continue
goto needdxil
if not exist %PCDXC% goto needdxil

if not "x%DirectXShaderCompiler:10.0.19041.0=%"=="x%DirectXShaderCompiler%" set DXILOPTS=%DXILOPTS:-HV 2021=%
if not "x%DirectXShaderCompiler:10.0.20348.0=%"=="x%DirectXShaderCompiler%" set DXILOPTS=%DXILOPTS:-HV 2021=%
if not "x%DirectXShaderCompiler:10.0.22000.0=%"=="x%DirectXShaderCompiler%" set DXILOPTS=%DXILOPTS:-HV 2021=%
goto continue

:continuepc
set PCOPTS=

if defined LegacyShaderCompiler goto fxcviaenv
set PCFXC="%WindowsSdkVerBinPath%%FXCARCH%\fxc.exe"
if exist %PCFXC% goto continue
set PCFXC="%WindowsSdkBinPath%%WindowsSDKVersion%\%FXCARCH%\fxc.exe"
Expand All @@ -74,6 +87,12 @@ set PCFXC="%WindowsSdkDir%bin\%WindowsSDKVersion%\%FXCARCH%\fxc.exe"
if exist %PCFXC% goto continue

set PCFXC=fxc.exe
goto continue

:fxcviaenv
set PCFXC="%LegacyShaderCompiler%"
if not exist %PCFXC% goto needfxc
goto continue

:continue
if not defined CompileShadersOutput set CompileShadersOutput=Compiled
Expand Down Expand Up @@ -293,28 +312,28 @@ endlocal
exit /b 0

:CompileShader
set fxc=%PCFXC% "%1.fx" %FXCOPTS% /T%2_5_1 %PCOPTS% /E%3 "/Fh%CompileShadersOutput%\%1_%3.inc" "/Fd%CompileShadersOutput%\%1_%3.pdb" /Vn%1_%3
set fxc=%PCFXC% "%1.fx" %FXCOPTS% /T%2_5_1 /E%3 "/Fh%CompileShadersOutput%\%1_%3.inc" "/Fd%CompileShadersOutput%\%1_%3.pdb" /Vn%1_%3
echo.
echo %fxc%
%fxc% || set error=1
exit /b

:CompileComputeShader
set fxc=%PCFXC% "%1.hlsl" %FXCOPTS% /Tcs_5_1 %PCOPTS% /E%2 "/Fh%CompileShadersOutput%\%1_%2.inc" "/Fd%CompileShadersOutput%\%1_%2.pdb" /Vn%1_%2
set fxc=%PCFXC% "%1.hlsl" %FXCOPTS% /Tcs_5_1 /E%2 "/Fh%CompileShadersOutput%\%1_%2.inc" "/Fd%CompileShadersOutput%\%1_%2.pdb" /Vn%1_%2
echo.
echo %fxc%
%fxc% || set error=1
exit /b

:CompileShaderdxil
set dxc=%PCDXC% "%1.fx" %FXCOPTS% /T%2_6_0 /E%3 "/Fh%CompileShadersOutput%\%1_%3.inc" "/Fd%CompileShadersOutput%\%1_%3.pdb" /Vn%1_%3
set dxc=%PCDXC% "%1.fx" %FXCOPTS% /T%2_6_0 %DXILOPTS% /E%3 "/Fh%CompileShadersOutput%\%1_%3.inc" "/Fd%CompileShadersOutput%\%1_%3.pdb" /Vn%1_%3
echo.
echo %dxc%
%dxc% || set error=1
exit /b

:CompileComputeShaderdxil
set dxc=%PCDXC% "%1.hlsl" %FXCOPTS% /Tcs_6_0 /E%2 "/Fh%CompileShadersOutput%\%1_%2.inc" "/Fd%CompileShadersOutput%\%1_%2.pdb" /Vn%1_%2
set dxc=%PCDXC% "%1.hlsl" %FXCOPTS% /Tcs_6_0 %DXILOPTS% /E%2 "/Fh%CompileShadersOutput%\%1_%2.inc" "/Fd%CompileShadersOutput%\%1_%2.pdb" /Vn%1_%2
echo.
echo %dxc%
%dxc% || set error=1
Expand All @@ -335,14 +354,14 @@ echo %fxc%
exit /b

:CompileShadergxdk
set dxc=%XBOXDXC% "%1.fx" %FXCOPTS% -HV 2021 /T%2_6_0 %XBOXOPTS% /E%3 "/Fh%CompileShadersOutput%\%XBOXPREFIX%%1_%3.inc" "/Fd%CompileShadersOutput%\%XBOXPREFIX%%1_%3.pdb" /Vn%1_%3
set dxc=%XBOXDXC% "%1.fx" %FXCOPTS% /T%2_6_0 %XBOXOPTS% /E%3 "/Fh%CompileShadersOutput%\%XBOXPREFIX%%1_%3.inc" "/Fd%CompileShadersOutput%\%XBOXPREFIX%%1_%3.pdb" /Vn%1_%3
echo.
echo %dxc%
%dxc% || set error=1
exit /b

:CompileComputeShadergxdk
set dxc=%XBOXDXC% "%1.hlsl" %FXCOPTS% -HV 2021 /Tcs_6_0 %XBOXOPTS% /E%2 "/Fh%CompileShadersOutput%\%XBOXPREFIX%%1_%2.inc" "/Fd%CompileShadersOutput%\%XBOXPREFIX%%1_%2.pdb" /Vn%1_%2
set dxc=%XBOXDXC% "%1.hlsl" %FXCOPTS% /Tcs_6_0 %XBOXOPTS% /E%2 "/Fh%CompileShadersOutput%\%XBOXPREFIX%%1_%2.inc" "/Fd%CompileShadersOutput%\%XBOXPREFIX%%1_%2.pdb" /Vn%1_%2
echo.
echo %dxc%
%dxc% || set error=1
Expand All @@ -358,6 +377,10 @@ echo ERROR: CompileShaders gxdk requires the Microsoft Gaming SDK
echo (try re-running from the Microsoft GDKX Gaming Command Prompt)
exit /b 1

:needfxc
echo ERROR: CompileShaders requires FXC.EXE
exit /b 1

:needdxil
echo ERROR: CompileShaders dxil requires DXC.EXE
exit /b 1
7 changes: 7 additions & 0 deletions build/CompilerAndLinker.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ elseif(CMAKE_VS_PLATFORM_NAME_DEFAULT MATCHES "^[Aa][Rr][Mm]64EC$")
set(DIRECTX_ARCH arm64ec)
endif()

#--- Determines host architecture
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "[Aa][Rr][Mm]64|aarch64|arm64")
set(DIRECTX_HOST_ARCH arm64)
else()
set(DIRECTX_HOST_ARCH x64)
endif()

#--- Build with Unicode Win32 APIs per "UTF-8 Everywhere"
if(WIN32)
list(APPEND COMPILER_DEFINES _UNICODE UNICODE)
Expand Down
1 change: 1 addition & 0 deletions build/DirectXTK12-GitHub-Dev17.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pr:
paths:
include:
- build/DirectXTK12-GitHub-Dev17.yml
- Src/Shaders/CompileShaders.cmd

resources:
repositories:
Expand Down
1 change: 1 addition & 0 deletions build/DirectXTK12-GitHub-GDK-Dev17.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pr:
- build/*.in
- build/*.cmake
- build/SetupBWOI.*
- Src/Shaders/CompileShaders.cmd

resources:
repositories:
Expand Down
1 change: 1 addition & 0 deletions build/DirectXTK12-GitHub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pr:
paths:
include:
- build/DirectXTK12-GitHub.yml
- Src/Shaders/CompileShaders.cmd

resources:
repositories:
Expand Down

0 comments on commit b7680be

Please sign in to comment.