From 743ade176541e36595d16dc657c99acd7c86d11c Mon Sep 17 00:00:00 2001 From: Jeremy Ong Date: Wed, 2 Feb 2022 00:23:55 -0700 Subject: [PATCH] Add "Definitions" field to shader asset Shaders can now specify a top-level field "Definitions" which accepts an array of string values. Each string will be appended to the set of preprocessor definitions defined globally and forwarded to the MCPP preprocessor on shader build. The shader-reload soak test was modified to accept a new shader to test this feature in the ASV. Signed-off-by: Jeremy Ong --- .../Code/Source/Editor/AzslShaderBuilderSystemComponent.cpp | 2 +- .../Asset/Shader/Code/Source/Editor/ShaderAssetBuilder.cpp | 3 +++ .../RPI/Code/Include/Atom/RPI.Edit/Shader/ShaderSourceData.h | 1 + Gems/Atom/RPI/Code/Source/RPI.Edit/Shader/ShaderSourceData.cpp | 3 ++- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Gems/Atom/Asset/Shader/Code/Source/Editor/AzslShaderBuilderSystemComponent.cpp b/Gems/Atom/Asset/Shader/Code/Source/Editor/AzslShaderBuilderSystemComponent.cpp index eae4e804e0..7e510e2e3a 100644 --- a/Gems/Atom/Asset/Shader/Code/Source/Editor/AzslShaderBuilderSystemComponent.cpp +++ b/Gems/Atom/Asset/Shader/Code/Source/Editor/AzslShaderBuilderSystemComponent.cpp @@ -82,7 +82,7 @@ namespace AZ // Register Shader Asset Builder AssetBuilderSDK::AssetBuilderDesc shaderAssetBuilderDescriptor; shaderAssetBuilderDescriptor.m_name = "Shader Asset Builder"; - shaderAssetBuilderDescriptor.m_version = 109; // Modify Metal shader platform to permit the precise keyword to fix depth bitwise mismatch between passes + shaderAssetBuilderDescriptor.m_version = 110; // Add "Definitions" field to shader asset to support convenient addition of preprocessor definitions shaderAssetBuilderDescriptor.m_patterns.push_back(AssetBuilderSDK::AssetBuilderPattern( AZStd::string::format("*.%s", RPI::ShaderSourceData::Extension), AssetBuilderSDK::AssetBuilderPattern::PatternType::Wildcard)); shaderAssetBuilderDescriptor.m_busId = azrtti_typeid(); shaderAssetBuilderDescriptor.m_createJobFunction = AZStd::bind(&ShaderAssetBuilder::CreateJobs, &m_shaderAssetBuilder, AZStd::placeholders::_1, AZStd::placeholders::_2); diff --git a/Gems/Atom/Asset/Shader/Code/Source/Editor/ShaderAssetBuilder.cpp b/Gems/Atom/Asset/Shader/Code/Source/Editor/ShaderAssetBuilder.cpp index e431b74282..341f737bd6 100644 --- a/Gems/Atom/Asset/Shader/Code/Source/Editor/ShaderAssetBuilder.cpp +++ b/Gems/Atom/Asset/Shader/Code/Source/Editor/ShaderAssetBuilder.cpp @@ -413,6 +413,9 @@ namespace AZ // At this moment We have global build options that should be merged with the build options that are common // to all the supervariants of this shader. buildOptions.m_compilerArguments.Merge(shaderSourceData.m_compiler); + buildOptions.m_preprocessorSettings.m_predefinedMacros.insert( + buildOptions.m_preprocessorSettings.m_predefinedMacros.end(), + shaderSourceData.m_definitions.begin(), shaderSourceData.m_definitions.end()); for (RHI::ShaderPlatformInterface* shaderPlatformInterface : platformInterfaces) { diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Edit/Shader/ShaderSourceData.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Edit/Shader/ShaderSourceData.h index 7742450bd6..a7d28eab14 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Edit/Shader/ShaderSourceData.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Edit/Shader/ShaderSourceData.h @@ -57,6 +57,7 @@ namespace AZ }; AZStd::string m_source; + AZStd::vector m_definitions; RHI::ShaderCompilerArguments m_compiler; AZStd::string m_drawListName; diff --git a/Gems/Atom/RPI/Code/Source/RPI.Edit/Shader/ShaderSourceData.cpp b/Gems/Atom/RPI/Code/Source/RPI.Edit/Shader/ShaderSourceData.cpp index 20bd36784f..d0abd89af0 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Edit/Shader/ShaderSourceData.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Edit/Shader/ShaderSourceData.cpp @@ -21,13 +21,14 @@ namespace AZ if (auto* serializeContext = azrtti_cast(context)) { serializeContext->Class() - ->Version(4) + ->Version(5) ->Field("Source", &ShaderSourceData::m_source) ->Field("DrawList", &ShaderSourceData::m_drawListName) ->Field("DepthStencilState", &ShaderSourceData::m_depthStencilState) ->Field("RasterState", &ShaderSourceData::m_rasterState) ->Field("BlendState", &ShaderSourceData::m_blendState) ->Field("ProgramSettings", &ShaderSourceData::m_programSettings) + ->Field("Definitions", &ShaderSourceData::m_definitions) ->Field("CompilerHints", &ShaderSourceData::m_compiler) ->Field("DisabledRHIBackends", &ShaderSourceData::m_disabledRhiBackends) ->Field("Supervariants", &ShaderSourceData::m_supervariants)