From dc9b5be1525541df0e48cef53741e00ab31e8859 Mon Sep 17 00:00:00 2001 From: Mathew Benson Date: Mon, 9 Feb 2026 00:53:09 +0300 Subject: [PATCH] ShaderC dependencies Remove and Shader Compilation Refactor - We remove the use of the shaderc download, which was only being used for compiling shaders. - We instead use the glslang compiler that we we're compiling anyway. - This way we can extract the glslang compiler that we have compiled and do the shader compilation as part of the CMake Scripting. - This should reduce the compilation times and/or storage requirements. - As Currently configured the Shaders will be compiled in each build, however, we can improve on this and ensure that compilation is only done as required at a later stage --- CMakeLists.txt | 4 + Obelisk/CMakeLists.txt | 3 - Resources/CMakeLists.txt | 33 ++++++++ Scripts/BuildEngine.ps1 | 8 -- Scripts/PostBuild.ps1 | 137 --------------------------------- Scripts/ShaderCompile.ps1 | 100 ------------------------ ZEngine/ZEngine/CMakeLists.txt | 2 - dependencies.cmake | 9 +-- 8 files changed, 40 insertions(+), 256 deletions(-) create mode 100644 Resources/CMakeLists.txt delete mode 100644 Scripts/PostBuild.ps1 delete mode 100644 Scripts/ShaderCompile.ps1 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b0420c8..2ef3e8f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,10 @@ if (NOT LAUNCHER_ONLY) ## Setup Dependencies include(dependencies.cmake) + ## Compile Shaders + + add_subdirectory (Resources) + # Core engine lib is here # add_subdirectory (ZEngine) diff --git a/Obelisk/CMakeLists.txt b/Obelisk/CMakeLists.txt index 2429ce50..b29f2753 100644 --- a/Obelisk/CMakeLists.txt +++ b/Obelisk/CMakeLists.txt @@ -53,9 +53,6 @@ install(TARGETS ${TARGET_NAME} DESTINATION bin ) -install(DIRECTORY ../Resources/Editor/Settings DESTINATION bin) - -install(DIRECTORY ../Resources/Shaders DESTINATION bin) diff --git a/Resources/CMakeLists.txt b/Resources/CMakeLists.txt new file mode 100644 index 00000000..80674828 --- /dev/null +++ b/Resources/CMakeLists.txt @@ -0,0 +1,33 @@ +set(ShaderPath ${CMAKE_CURRENT_SOURCE_DIR}/Shaders) +set(ShaderOutputPath "${ShaderPath}/Cache") + +file (MAKE_DIRECTORY "${ShaderOutputPath}") + +file (GLOB VertexShaders CONFIGURE_DEPENDS "${ShaderPath}/*.vert") +file (GLOB FragmentShaders CONFIGURE_DEPENDS "${ShaderPath}/*.frag") + +foreach(VertexShader ${VertexShaders}) + + cmake_path(GET VertexShader STEM shaderfilename) + cmake_path(APPEND_STRING shaderfilename "_vertex.spv" OUTPUT_VARIABLE VertexShaderOutputFileName ) + cmake_path(APPEND ShaderOutputPath ${VertexShaderOutputFileName} OUTPUT_VARIABLE VertexShaderOutputPath) + + add_custom_target("Compile-${shaderfilename}-vertex" ALL COMMAND glslang-standalone "-I${ShaderPath}" "-V" "-o" "${VertexShaderOutputPath}" "${VertexShader}") + + +endforeach() + +foreach(FragmentShader ${FragmentShaders}) + + cmake_path(GET FragmentShader STEM shaderfilename) + cmake_path(APPEND_STRING shaderfilename "_fragment.spv" OUTPUT_VARIABLE FragmentShaderOutputFileName ) + cmake_path(APPEND ShaderOutputPath ${FragmentShaderOutputFileName} OUTPUT_VARIABLE FragmentShaderOutputPath) + + + add_custom_target("Compile-${shaderfilename}-fragment" ALL COMMAND glslang-standalone "-V" "-I${ShaderPath}" "-o" "${FragmentShaderOutputPath}" "${FragmentShader}") + +endforeach() + +install(DIRECTORY Editor/Settings DESTINATION bin) + +install(DIRECTORY Shaders/Cache DESTINATION bin/Shaders) diff --git a/Scripts/BuildEngine.ps1 b/Scripts/BuildEngine.ps1 index b3c406a3..4a90b88c 100644 --- a/Scripts/BuildEngine.ps1 +++ b/Scripts/BuildEngine.ps1 @@ -174,7 +174,6 @@ function Build([string]$configuration, [int]$VsVersion , [bool]$runBuild) { } } - if(-Not $LauncherOnly) { # Run Clang format @@ -195,13 +194,6 @@ if(-Not $LauncherOnly) { } } - - # Run Shader Compilation - foreach ($config in $Configurations) { - $shaderCompileScript = Join-Path $PSScriptRoot -ChildPath "ShaderCompile.ps1" - & pwsh -File $shaderCompileScript -Configuration:$config -ForceRebuild:$true - } - if ($LASTEXITCODE -ne 0) { Write-Error "Stopped build process..." -ErrorAction Stop } diff --git a/Scripts/PostBuild.ps1 b/Scripts/PostBuild.ps1 deleted file mode 100644 index 6253d843..00000000 --- a/Scripts/PostBuild.ps1 +++ /dev/null @@ -1,137 +0,0 @@ -# MIT License - -# Copyright (c) 2020 Jean Philippe - -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -#Requires -PSEdition Core - -param ( - [Parameter(HelpMessage="SystemName type to build, default to Windows")] - [ValidateSet('Windows', 'Darwin', 'Linux')] - [string[]] $SystemName = 'Windows', - - [Parameter(HelpMessage = "Architecture to build")] - [ValidateSet('x64', 'arm64')] - [string] $Architecture = 'x64', - - [Parameter(HelpMessage="Configuration type to build, default to Debug")] - [ValidateSet('Debug', 'Release')] - [string[]] $Configurations = 'Debug', - - [Parameter(HelpMessage = "Build Launcher only")] - [switch] $LauncherOnly -) - -$ErrorActionPreference = "Stop" -$TargetFramework = 'net8.0' - -[string]$RepoRoot = [IO.Path]::Combine($PSScriptRoot, "..") -[string]$OuputBuildDirectory = If($IsWindows) { - [IO.Path]::Combine($RepoRoot, "Result.Windows.x64.MultiConfig") -} Else { - [IO.Path]::Combine($RepoRoot, "Result.$SystemName.x64.$Configurations") -} - -if($LauncherOnly) { - Write-Host "Skipping resources copy..." - return; -} - -$ContentsToProcess = @( - @{ - Name = "Resources" - IsDirectory = $true - Contents = @( - switch ($SystemName) { - "Windows" { - @{ From = "$RepoRoot\Resources\Shaders"; To = "$OuputBuildDirectory\Obelisk\$Configurations\Shaders"} - @{ From = "$RepoRoot\Resources\Editor\Settings"; To = "$OuputBuildDirectory\Obelisk\$Configurations\Settings"} - } - "Darwin" { - @{ From = "$RepoRoot\Resources\Shaders"; To = "$OuputBuildDirectory\Obelisk\$Configurations\Shaders"} - @{ From = "$RepoRoot\Resources\Editor\Settings"; To = "$OuputBuildDirectory\Obelisk\$Configurations\Settings"} - } - "Linux" { - @{ From = "$RepoRoot\Resources\Shaders"; To = "$OuputBuildDirectory\Obelisk\Shaders"} - @{ From = "$RepoRoot\Resources\Editor\Settings"; To = "$OuputBuildDirectory\Obelisk\Settings"} - } - Default { - throw 'This system is not supported' - } - } - ) - }, - @{ - Name = "Obelisk" - IsDirectory = $true - Contents = @( - switch ($SystemName) { - "Windows" { - @{ From = "$OuputBuildDirectory\Obelisk\$Configurations"; To = "$OuputBuildDirectory\Panzerfaust\$Configurations\$TargetFramework\win-$Architecture\Editor"} - @{ From = "$OuputBuildDirectory\Obelisk\$Configurations"; To = "$OuputBuildDirectory\Panzerfaust\$Configurations\$TargetFramework\win-$Architecture\publish\Editor"} - } - "Darwin" { - switch ($Architecture) { - "x64" { - @{ From = "$OuputBuildDirectory\Obelisk\$Configurations"; To = "$OuputBuildDirectory\Panzerfaust\$Configurations\$TargetFramework\osx-$Architecture\Editor"} - @{ From = "$OuputBuildDirectory\Obelisk\$Configurations"; To = "$OuputBuildDirectory\Panzerfaust\$Configurations\$TargetFramework\osx-$Architecture\publish\Editor"} - } - "arm64" { - @{ From = "$OuputBuildDirectory\Obelisk\$Configurations"; To = "$OuputBuildDirectory\Panzerfaust\$Configurations\$TargetFramework\osx-$Architecture\Editor"} - @{ From = "$OuputBuildDirectory\Obelisk\$Configurations"; To = "$OuputBuildDirectory\Panzerfaust\$Configurations\$TargetFramework\osx-$Architecture\publish\Editor"} - } - Default { - throw 'This architecture is not supported' - } - } - } - "Linux" { - @{ From = "$OuputBuildDirectory\Obelisk"; To = "$OuputBuildDirectory\Panzerfaust\$Configurations\$TargetFramework\Editor"} - @{ From = "$OuputBuildDirectory\Obelisk"; To = "$OuputBuildDirectory\Panzerfaust\$Configurations\$TargetFramework\Editor"} - } - Default { - throw 'This system is not supported' - } - } - ) - } -) - -foreach ($item in $ContentsToProcess) { - foreach($content in $item.Contents) { - - if($item.IsDirectory -eq $true) { - - # Delete if directories or files already exist - # - if(Test-Path $content.To) { - Remove-Item $content.To -Recurse -Force - } - Copy-Item -Path $content.From -Destination $content.To -Recurse -Force - } - else { - Copy-Item -Path $content.From -Destination $content.To -Force - } - - [string]$name = $item.Name - [string]$ToDirectory = $content.To - Write-Host "Copied $name --> $ToDirectory" - } -} diff --git a/Scripts/ShaderCompile.ps1 b/Scripts/ShaderCompile.ps1 deleted file mode 100644 index db82c286..00000000 --- a/Scripts/ShaderCompile.ps1 +++ /dev/null @@ -1,100 +0,0 @@ -# MIT License - -# Copyright (c) 2020 Jean Philippe - -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - - -#Requires -PSEdition Core - -param ( - [Parameter(HelpMessage = "Configuration type to build")] - [ValidateSet('Debug', 'Release')] - [string] $Configuration = 'Debug', - - [Parameter(HelpMessage = "Whether to force shader build, default to False")] - [bool] $ForceRebuild = $false -) - -$ErrorActionPreference = "Stop" - -. (Join-Path $PSScriptRoot Shared.ps1) - -$glslValidatorProgram = Find-GlslangValidator - -$shaderDirectory = Join-Path $repositoryRootPath -ChildPath "Resources\Shaders" -$shaderCacheDirectory = Join-Path $repositoryRootPath -ChildPath "Resources\Shaders\Cache" - -# Ensure the Shader Cache directory exists, or create it otherwise -if(-not (Test-Path $shaderCacheDirectory)) { - $Null = New-Item -ItemType Directory -Path $shaderCacheDirectory -ErrorAction SilentlyContinue -} - -$shaderSourceFiles = Get-ChildItem $shaderDirectory -Recurse -File | Where-Object {$_.Name -like "*.vert" -or $_.Name -like "*.frag"} -$cacheFilesCount = (Get-ChildItem $shaderCacheDirectory -Recurse | Measure-Object).Count; - -if (($cacheFilesCount -gt 0) -and (-not $ForceRebuild)) { - Write-Host "Shader files have already been compiled, skipping..." - return; -} - -if ($shaderSourceFiles.Count -eq 0) { - Write-Warning "No Shader files found..." - return; -} - -# Create include directories -$shaderIncludeDirectories = @(); -foreach ($shaderFile in $shaderSourceFiles) { - if(!$shaderIncludeDirectories.Contains($shaderFile.DirectoryName)) { - $shaderIncludeDirectories += $shaderFile.DirectoryName; - } -} - -[string] $includeDirArgs = ""; -foreach ($includeDir in $shaderIncludeDirectories) { - $includeDirArgs += " -I$includeDir" -} - -[string] $compilerArgs = ""; -if ($Configuration -eq "Debug") { - $compilerArgs = " -g" -} -else { - $compilerArgs = " -Os" -} - -foreach ($shaderFile in $shaderSourceFiles) { - $fileName = $shaderFile.BaseName - $suffixName = If($shaderFile.Extension -eq ".vert") { "_vertex" } Else { "_fragment" } - $outputFileFullName = Join-Path $shaderCacheDirectory -ChildPath "$fileName$suffixName.spv" - - $fileFullName = $shaderFile.FullName; - Write-Host "Compiling Shader file : $fileFullName" - $glslValidatorProcess = Start-Process $glslValidatorProgram -ArgumentList "$compilerArgs -V $fileFullName -o $outputFileFullName $includeDirArgs" -NoNewWindow -PassThru - $handle = $glslValidatorProcess.Handle; - $glslValidatorProcess.WaitForExit(); - $exitCode = $glslValidatorProcess.ExitCode - - if ($exitCode -ne 0) { - Write-Error "failed to compile $shaderFile ..." -ErrorAction Stop - } -} - -Write-Host "Shader files compilation successful..." diff --git a/ZEngine/ZEngine/CMakeLists.txt b/ZEngine/ZEngine/CMakeLists.txt index cb73f6fc..a7b35c4a 100644 --- a/ZEngine/ZEngine/CMakeLists.txt +++ b/ZEngine/ZEngine/CMakeLists.txt @@ -1,9 +1,7 @@ file (GLOB_RECURSE HEADER_FILES_LIST CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.h) file (GLOB_RECURSE CPP_FILES_LIST CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) -file (GLOB_RECURSE RESOURCE_FILES_LIST CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/../Resources/Shaders/*.*) source_group (TREE ${PROJECT_SOURCE_DIR}/ZEngine PREFIX "Source Files" FILES ${HEADER_FILES_LIST} ${CPP_FILES_LIST}) -source_group (TREE ${PROJECT_SOURCE_DIR}/../Resources PREFIX "Resources Files" FILES ${RESOURCE_FILES_LIST}) # ZEngine source files # diff --git a/dependencies.cmake b/dependencies.cmake index 694d3389..1ba957ef 100644 --- a/dependencies.cmake +++ b/dependencies.cmake @@ -102,7 +102,6 @@ FetchContent_Declare( ) - FetchContent_Declare( SPIRV-Tools GIT_REPOSITORY https://github.com/KhronosGroup/SPIRV-Tools.git @@ -179,14 +178,14 @@ FetchContent_MakeAvailable( yaml-cpp VulkanMemoryAllocator SPIRV-Headers - SPIRV-Tools - glslang spirv_cross_core - GTest nlohmann_json tlsf CLI11 rapidhash + SPIRV-Tools + glslang + GTest ) set(IMGUIDIR ${FETCHCONTENT_BASE_DIR}/imgui) @@ -240,7 +239,6 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") target_link_libraries(External_libs INTERFACE SPIRV-Tools-static) else() target_link_libraries(External_libs INTERFACE SPIRV-Tools) - endif() @@ -261,7 +259,6 @@ target_link_libraries(External_libs glslang::glslang glslang::glslang-default-resource-limits glslang::SPIRV - glslang::SPVRemapper GPUOpen::VulkanMemoryAllocator nlohmann_json::nlohmann_json )