diff --git a/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveColor.pass b/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveColor.pass index 94e42f9202..baffbef6f3 100644 --- a/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveColor.pass +++ b/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveColor.pass @@ -50,6 +50,13 @@ "Attachment": "ResolvedOutput" } } + ], + "FallbackConnections": [ + // used in non-MSAA pipelines when the resolve is disabled + { + "Input" : "Input", + "Output" : "Output" + } ] } } diff --git a/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveCustom.pass b/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveCustom.pass index c9f93b433d..b7e6040cec 100644 --- a/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveCustom.pass +++ b/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveCustom.pass @@ -48,6 +48,13 @@ } } ], + "FallbackConnections": [ + // used in non-MSAA pipelines when the resolve is disabled + { + "Input" : "Input", + "Output" : "Output" + } + ], "PassData": { "$type": "FullscreenTrianglePassData", "ShaderAsset": { diff --git a/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveDepth.pass b/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveDepth.pass index 738f83c168..b335c5460e 100644 --- a/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveDepth.pass +++ b/Gems/Atom/Feature/Common/Assets/Passes/MSAAResolveDepth.pass @@ -54,6 +54,13 @@ } } ], + "FallbackConnections": [ + // used in non-MSAA pipelines when the resolve is disabled + { + "Input" : "Input", + "Output" : "Output" + } + ], "PassData": { "$type": "FullscreenTrianglePassData", "ShaderAsset": { diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseComposite.shader b/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseComposite.shader index f074dcb0dd..1593f0bb66 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseComposite.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseComposite.shader @@ -52,5 +52,14 @@ "type": "Fragment" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseGlobalFullscreen.shader b/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseGlobalFullscreen.shader index 06967548bb..3d4b711f63 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseGlobalFullscreen.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseGlobalFullscreen.shader @@ -52,5 +52,14 @@ "type": "Fragment" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseProbeGridDownsample.shader b/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseProbeGridDownsample.shader index 372ba59637..dff0f755d0 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseProbeGridDownsample.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/DiffuseGlobalIllumination/DiffuseProbeGridDownsample.shader @@ -30,5 +30,14 @@ "type": "Fragment" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/LightCulling/LightCullingTilePrepare.shader b/Gems/Atom/Feature/Common/Assets/Shaders/LightCulling/LightCullingTilePrepare.shader index d101424e65..070bc8e2bc 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/LightCulling/LightCullingTilePrepare.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/LightCulling/LightCullingTilePrepare.shader @@ -15,5 +15,14 @@ "type" : "Compute" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionComposite.shader b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionComposite.shader index c1d272e857..847bf5e300 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionComposite.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionComposite.shader @@ -49,5 +49,14 @@ "type": "Fragment" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionGlobalFullscreen.shader b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionGlobalFullscreen.shader index 069700191a..445a29c74f 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionGlobalFullscreen.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionGlobalFullscreen.shader @@ -40,5 +40,14 @@ "type": "Fragment" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeBlendWeight.shader b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeBlendWeight.shader index 645f635743..1da653f1e7 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeBlendWeight.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeBlendWeight.shader @@ -49,5 +49,14 @@ "type": "Fragment" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeRenderInner.shader b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeRenderInner.shader index 24ba5c250b..76cab60b3c 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeRenderInner.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeRenderInner.shader @@ -42,5 +42,14 @@ "type": "Fragment" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeRenderOuter.shader b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeRenderOuter.shader index 68a2344fb5..9e60a5faf9 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeRenderOuter.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeRenderOuter.shader @@ -49,5 +49,14 @@ "type": "Fragment" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeStencil.shader b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeStencil.shader index c4a0bb3c8b..538d746fba 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeStencil.shader +++ b/Gems/Atom/Feature/Common/Assets/Shaders/Reflections/ReflectionProbeStencil.shader @@ -47,5 +47,14 @@ "type": "Vertex" } ] - } + }, + + "Supervariants": + [ + { + "Name": "NoMSAA", + "PlusArguments": "--no-ms", + "MinusArguments": "" + } + ] } diff --git a/Gems/Atom/Feature/Common/Code/Source/Mesh/MeshFeatureProcessor.cpp b/Gems/Atom/Feature/Common/Code/Source/Mesh/MeshFeatureProcessor.cpp index b087269089..cbee40616e 100644 --- a/Gems/Atom/Feature/Common/Code/Source/Mesh/MeshFeatureProcessor.cpp +++ b/Gems/Atom/Feature/Common/Code/Source/Mesh/MeshFeatureProcessor.cpp @@ -658,7 +658,7 @@ namespace AZ if (!m_shaderResourceGroup) { auto& shaderAsset = material->GetAsset()->GetMaterialTypeAsset()->GetShaderAssetForObjectSrg(); - m_shaderResourceGroup = RPI::ShaderResourceGroup::Create(shaderAsset, RPI::DefaultSupervariantIndex, objectSrgLayout->GetName()); + m_shaderResourceGroup = RPI::ShaderResourceGroup::Create(shaderAsset, objectSrgLayout->GetName()); if (!m_shaderResourceGroup) { AZ_Warning("MeshFeatureProcessor", false, "Failed to create a new shader resource group, skipping."); diff --git a/Gems/Atom/Feature/Common/Code/Source/RayTracing/RayTracingFeatureProcessor.cpp b/Gems/Atom/Feature/Common/Code/Source/RayTracing/RayTracingFeatureProcessor.cpp index 3023e06b50..f27c9d4e0d 100644 --- a/Gems/Atom/Feature/Common/Code/Source/RayTracing/RayTracingFeatureProcessor.cpp +++ b/Gems/Atom/Feature/Common/Code/Source/RayTracing/RayTracingFeatureProcessor.cpp @@ -74,12 +74,12 @@ namespace AZ } // create the RayTracingSceneSrg - m_rayTracingSceneSrg = RPI::ShaderResourceGroup::Create(m_rayTracingSrgAsset, RPI::DefaultSupervariantIndex, Name("RayTracingSceneSrg")); + m_rayTracingSceneSrg = RPI::ShaderResourceGroup::Create(m_rayTracingSrgAsset, Name("RayTracingSceneSrg")); AZ_Assert(m_rayTracingSceneSrg, "Failed to create RayTracingSceneSrg"); // create the RayTracingMaterialSrg const AZ::Name rayTracingMaterialSrgName("RayTracingMaterialSrg"); - m_rayTracingMaterialSrg = RPI::ShaderResourceGroup::Create(m_rayTracingSrgAsset, RPI::DefaultSupervariantIndex, Name("RayTracingMaterialSrg")); + m_rayTracingMaterialSrg = RPI::ShaderResourceGroup::Create(m_rayTracingSrgAsset, Name("RayTracingMaterialSrg")); AZ_Assert(m_rayTracingMaterialSrg, "Failed to create RayTracingMaterialSrg"); } diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/Shader.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/Shader.h index 938c8c06b1..e8bd94af75 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/Shader.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/Shader.h @@ -139,6 +139,11 @@ namespace AZ //! This tag corresponds to the ShaderAsset object's DrawListName. RHI::DrawListTag GetDrawListTag() const; + //! Changes the supervariant of the shader to the specified supervariantIndex. + //! [GFX TODO][ATOM-15813]: this can be removed when the shader InstanceDatabase can support multiple shader + //! instances with different supervariants. + void ChangeSupervariant(SupervariantIndex supervariantIndex); + private: explicit Shader(const SupervariantIndex& supervariantIndex) : m_supervariantIndex(supervariantIndex){}; Shader() = delete; diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderResourceGroup.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderResourceGroup.h index fb2b443ca5..1e7cadec4b 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderResourceGroup.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderResourceGroup.h @@ -61,6 +61,12 @@ namespace AZ AZ_CLASS_ALLOCATOR(ShaderResourceGroup, AZ::SystemAllocator, 0); /// Instantiates a unique shader resource group instance using its paired asset but with a random InstanceId. + /// This version uses the appropriate default supervariant. + static Data::Instance Create( + const Data::Asset& shaderAsset, const AZ::Name& srgName); + + /// Instantiates a unique shader resource group instance using its paired asset but with a random InstanceId. + /// This version uses the supervariant specified by the caller. static Data::Instance Create( const Data::Asset& shaderAsset, const SupervariantIndex& supervariantIndex, const AZ::Name& srgName); diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderSystem.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderSystem.h index 362e5fec72..51c776212d 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderSystem.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderSystem.h @@ -45,15 +45,19 @@ namespace AZ ShaderOptionValue GetGlobalShaderOption(const AZ::Name& shaderOptionName) override; const GlobalShaderOptionMap& GetGlobalShaderOptions() const override; void Connect(GlobalShaderOptionUpdatedEvent::Handler& handler) override; + void SetSupervariantName(const AZ::Name& supervariantName) override; + const AZ::Name& GetSupervariantName() const override; /////////////////////////////////////////////////////////////////// private: AZStd::unordered_map m_globalShaderOptionValues; GlobalShaderOptionUpdatedEvent m_globalShaderOptionUpdatedEvent; ShaderVariantAsyncLoader m_shaderVariantAsyncLoader; - }; - - + //! The ShaderSystem supervariantName is used by the ShaderAsset to search for an additional supervariant permutation. + //! This is done by appending the supervariantName set here to the user-specified supervariant name. + //! Currently this is used for NoMSAA supervariant support. + AZ::Name m_supervariantName; + }; } // namespace RPI } // namespace AZ diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderSystemInterface.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderSystemInterface.h index 5d2bf97578..9b0cce85bf 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderSystemInterface.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderSystemInterface.h @@ -47,6 +47,12 @@ namespace AZ //! Connect a handler for GlobalShaderOptionUpdatedEvent's virtual void Connect(GlobalShaderOptionUpdatedEvent::Handler& handler) = 0; + + //! The ShaderSystem supervariantName is used by the ShaderAsset to search for an additional supervariant permutation. + //! This is done by appending the supervariantName set here to the user-specified supervariant name. + //! Currently this is used for NoMSAA supervariant support. + virtual void SetSupervariantName(const AZ::Name& supervariantName) = 0; + virtual const AZ::Name& GetSupervariantName() const = 0; }; } // namespace RPI diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Shader/ShaderAsset.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Shader/ShaderAsset.h index a09f866bde..bc89c04768 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Shader/ShaderAsset.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Shader/ShaderAsset.h @@ -103,6 +103,8 @@ namespace AZ //! Returns the shader option group layout. const ShaderOptionGroupLayout* GetShaderOptionGroupLayout() const; + //! Returns the supervariant index from the specified name. + //! Note that this will append the system supervariant name from RPI::ShaderSystem when searching. SupervariantIndex GetSupervariantIndex(const AZ::Name& supervariantName) const; //! This function should be your one stop shop to get a ShaderVariantAsset. @@ -139,22 +141,15 @@ namespace AZ Data::Asset GetRootVariant() const { return GetRootVariant(DefaultSupervariantIndex); } - //! Finds and returns the shader resource group asset with the requested name. Returns an empty handle if no matching group was - //! found. + //! Finds and returns the shader resource group asset with the requested name. Returns an empty handle if no matching group was found. const RHI::Ptr& FindShaderResourceGroupLayout( const Name& shaderResourceGroupName, SupervariantIndex supervariantIndex) const; - const RHI::Ptr& FindShaderResourceGroupLayout(const Name& shaderResourceGroupName) const - { - return FindShaderResourceGroupLayout(shaderResourceGroupName, DefaultSupervariantIndex); - } + const RHI::Ptr& FindShaderResourceGroupLayout(const Name& shaderResourceGroupName) const; //! Finds and returns the shader resource group layout associated with the requested binding slot. Returns an empty handle if no matching srg was found. const RHI::Ptr& FindShaderResourceGroupLayout( uint32_t bindingSlot, SupervariantIndex supervariantIndex) const; - const RHI::Ptr& FindShaderResourceGroupLayout(uint32_t bindingSlot) const - { - return FindShaderResourceGroupLayout(bindingSlot, DefaultSupervariantIndex); - } + const RHI::Ptr& FindShaderResourceGroupLayout(uint32_t bindingSlot) const; //! Finds and returns the shader resource group layout designated as a ShaderVariantKey fallback. const RHI::Ptr& FindFallbackShaderResourceGroupLayout( SupervariantIndex supervariantIndex) const; @@ -277,6 +272,8 @@ namespace AZ Supervariant* GetSupervariant(SupervariantIndex supervariantIndex); const Supervariant* GetSupervariant(SupervariantIndex supervariantIndex) const; + //! Search for a supervariant index by name. + SupervariantIndex GetSupervariantIndexInternal(AZ::Name supervariantName) const; //! The name is the stem of the source .shader file. Name m_name; diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Shader/ShaderCommonTypes.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Shader/ShaderCommonTypes.h index 6d42ac386e..d5d1d0bddc 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Shader/ShaderCommonTypes.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Shader/ShaderCommonTypes.h @@ -37,6 +37,9 @@ namespace AZ //! in the list of supervariants owned by the ShaderAsset. static const SupervariantIndex DefaultSupervariantIndex(0); + //! The NoMSAA supervariant is auto-generated by AZSLc. + static const char* NoMsaaSupervariantName = "NoMSAA"; + static const SupervariantIndex InvalidSupervariantIndex; enum class ShaderStageType : uint32_t diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/Material/Material.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/Material/Material.cpp index 4e65d68d6c..431cb62684 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/Material/Material.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/Material/Material.cpp @@ -69,7 +69,7 @@ namespace AZ if (srgLayout) { auto shaderAsset = m_materialAsset->GetMaterialTypeAsset()->GetShaderAssetForMaterialSrg(); - m_shaderResourceGroup = ShaderResourceGroup::Create(shaderAsset, DefaultSupervariantIndex, srgLayout->GetName()); + m_shaderResourceGroup = ShaderResourceGroup::Create(shaderAsset, srgLayout->GetName()); if (m_shaderResourceGroup) { diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/Pass/RasterPass.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/Pass/RasterPass.cpp index 13f2784a2d..59e7db427a 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/Pass/RasterPass.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/Pass/RasterPass.cpp @@ -64,11 +64,10 @@ namespace AZ if (shaderAsset) { - auto supervariantIndex = DefaultSupervariantIndex; - const auto srgLayout = shaderAsset->FindShaderResourceGroupLayout(SrgBindingSlot::Pass, supervariantIndex); + const auto srgLayout = shaderAsset->FindShaderResourceGroupLayout(SrgBindingSlot::Pass); if (srgLayout) { - m_shaderResourceGroup = ShaderResourceGroup::Create(shaderAsset, supervariantIndex, srgLayout->GetName()); + m_shaderResourceGroup = ShaderResourceGroup::Create(shaderAsset, srgLayout->GetName()); AZ_Assert( m_shaderResourceGroup, "[RasterPass '%s']: Failed to create SRG from shader asset '%s'", GetPathName().GetCStr(), diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/Scene.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/Scene.cpp index d87f119a7a..59c375f1d3 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/Scene.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/Scene.cpp @@ -46,7 +46,7 @@ namespace AZ if (sceneSrgLayout) { auto shaderAsset = RPISystemInterface::Get()->GetCommonShaderAssetForSrgs(); - scene->m_srg = ShaderResourceGroup::Create(shaderAsset, DefaultSupervariantIndex, sceneSrgLayout->GetName()); + scene->m_srg = ShaderResourceGroup::Create(shaderAsset, sceneSrgLayout->GetName()); } return ScenePtr(scene); diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/Shader.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/Shader.cpp index d74ffbfca2..ab6e35e923 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/Shader.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/Shader.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -33,6 +34,19 @@ namespace AZ auto anySupervariantName = AZStd::any(supervariantName); Data::Instance shaderInstance = Data::InstanceDatabase::Instance().FindOrCreate( Data::InstanceId::CreateFromAssetId(shaderAsset.GetId()), shaderAsset, &anySupervariantName); + + if (shaderInstance) + { + // [GFX TODO][ATOM-15813] Change InstanceDatabase to support multiple instances with different supervariants. + // At this time we do not support multiple supervariants loaded for a shader asset simultaneously, so if this shader + // is referring to the wrong supervariant we need to change it to the correct one. + SupervariantIndex supervariantIndex = shaderAsset->GetSupervariantIndex(supervariantName); + if (supervariantIndex.IsValid() && shaderInstance->GetSupervariantIndex() != supervariantIndex) + { + shaderInstance->ChangeSupervariant(supervariantIndex); + } + } + return shaderInstance; } @@ -68,8 +82,6 @@ namespace AZ RHI::ResultCode Shader::Init(ShaderAsset& shaderAsset) { - AZ_Assert(m_supervariantIndex != InvalidSupervariantIndex, "Invalid supervariant index"); - ShaderVariantFinderNotificationBus::Handler::BusDisconnect(); ShaderVariantFinderNotificationBus::Handler::BusConnect(shaderAsset.GetId()); @@ -395,5 +407,15 @@ namespace AZ { return m_drawListTag; } + + void Shader::ChangeSupervariant(SupervariantIndex supervariantIndex) + { + if (supervariantIndex != m_supervariantIndex) + { + m_supervariantIndex = supervariantIndex; + Init(*m_asset); + } + } + } // namespace RPI } // namespace AZ diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderResourceGroup.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderResourceGroup.cpp index f63165495f..b52fffbdb0 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderResourceGroup.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderResourceGroup.cpp @@ -40,6 +40,19 @@ namespace AZ return Data::InstanceId::CreateData(idString.data(), idString.size()); } + Data::Instance ShaderResourceGroup::Create( + const Data::Asset& shaderAsset, const AZ::Name& srgName) + { + // retrieve the supervariantIndex by searching for the default supervariant name, this will + // allow the shader asset to properly handle the RPI::ShaderSystem supervariant + SupervariantIndex supervariantIndex = shaderAsset->GetSupervariantIndex(AZ::Name("")); + + SrgInitParams initParams{ supervariantIndex, srgName }; + auto anyInitParams = AZStd::any(initParams); + return Data::InstanceDatabase::Instance().FindOrCreate( + Data::InstanceId::CreateRandom(), shaderAsset, &anyInitParams); + } + Data::Instance ShaderResourceGroup::Create( const Data::Asset& shaderAsset, const SupervariantIndex& supervariantIndex, const AZ::Name& srgName) { diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderSystem.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderSystem.cpp index d94760d243..1058a7efc2 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderSystem.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderSystem.cpp @@ -148,6 +148,16 @@ namespace AZ { handler.Connect(m_globalShaderOptionUpdatedEvent); } + + void ShaderSystem::SetSupervariantName(const AZ::Name& supervariantName) + { + m_supervariantName = supervariantName; + } + + const AZ::Name& ShaderSystem::GetSupervariantName() const + { + return m_supervariantName; + } /////////////////////////////////////////////////////////////////// } // namespace RPI diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/View.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/View.cpp index f86a51124b..fcc46565fa 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/View.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/View.cpp @@ -59,7 +59,7 @@ namespace AZ if (viewSrgShaderAsset.IsReady()) { - m_shaderResourceGroup = ShaderResourceGroup::Create(viewSrgShaderAsset, DefaultSupervariantIndex, RPISystemInterface::Get()->GetViewSrgLayout()->GetName()); + m_shaderResourceGroup = ShaderResourceGroup::Create(viewSrgShaderAsset, RPISystemInterface::Get()->GetViewSrgLayout()->GetName()); } #if AZ_TRAIT_MASKED_OCCLUSION_CULLING_SUPPORTED m_maskedOcclusionCulling = MaskedOcclusionCulling::Create(); diff --git a/Gems/Atom/RPI/Code/Source/RPI.Reflect/Shader/ShaderAsset.cpp b/Gems/Atom/RPI/Code/Source/RPI.Reflect/Shader/ShaderAsset.cpp index 08d0cab935..c89217460c 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Reflect/Shader/ShaderAsset.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Reflect/Shader/ShaderAsset.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -148,18 +149,28 @@ namespace AZ SupervariantIndex ShaderAsset::GetSupervariantIndex(const AZ::Name& supervariantName) const { - const auto& supervariants = GetCurrentShaderApiData().m_supervariants; - const uint32_t supervariantCount = supervariants.size(); - for (uint32_t index = 0; index < supervariantCount; ++index) + SupervariantIndex supervariantIndex = InvalidSupervariantIndex; + + // check for an RPI ShaderSystem supervariant + RPI::ShaderSystemInterface* shaderSystemInterface = ShaderSystemInterface::Get(); + if (shaderSystemInterface && !shaderSystemInterface->GetSupervariantName().IsEmpty()) { - if (supervariants[index].m_name == supervariantName) - { - return SupervariantIndex{index}; - } + // search for the combined requested name and system supervariant name + // Note: the shader may not support this supervariant, if it doesn't we will + // fallback to the requested name below + AZStd::string combinedName = supervariantName.GetCStr(); + combinedName.append(shaderSystemInterface->GetSupervariantName().GetCStr()); + supervariantIndex = GetSupervariantIndexInternal(AZ::Name(combinedName)); + } + + if (supervariantIndex == InvalidSupervariantIndex) + { + // search for the requested name + supervariantIndex = GetSupervariantIndexInternal(supervariantName); } - return InvalidSupervariantIndex; - } + return supervariantIndex; + } Data::Asset ShaderAsset::GetVariant( const ShaderVariantId& shaderVariantId, SupervariantIndex supervariantIndex) @@ -296,6 +307,24 @@ namespace AZ return RHI::NullSrgLayout; } + const RHI::Ptr& ShaderAsset::FindShaderResourceGroupLayout(const Name& shaderResourceGroupName) const + { + SupervariantIndex supervariantIndex = DefaultSupervariantIndex; + + // check for an RPI ShaderSystem specified supervariant + RPI::ShaderSystemInterface* shaderSystemInterface = ShaderSystemInterface::Get(); + if (shaderSystemInterface && !shaderSystemInterface->GetSupervariantName().IsEmpty()) + { + SupervariantIndex systemSupervariantIndex = GetSupervariantIndexInternal(shaderSystemInterface->GetSupervariantName()); + if (systemSupervariantIndex.IsValid()) + { + supervariantIndex = systemSupervariantIndex; + } + } + + return FindShaderResourceGroupLayout(shaderResourceGroupName, supervariantIndex); + } + const RHI::Ptr& ShaderAsset::FindShaderResourceGroupLayout( uint32_t bindingSlot, SupervariantIndex supervariantIndex) const { @@ -319,6 +348,24 @@ namespace AZ return RHI::NullSrgLayout; } + const RHI::Ptr& ShaderAsset::FindShaderResourceGroupLayout(uint32_t bindingSlot) const + { + SupervariantIndex supervariantIndex = DefaultSupervariantIndex; + + // check for an RPI ShaderSystem specified supervariant + RPI::ShaderSystemInterface* shaderSystemInterface = ShaderSystemInterface::Get(); + if (shaderSystemInterface && !shaderSystemInterface->GetSupervariantName().IsEmpty()) + { + SupervariantIndex systemSupervariantIndex = GetSupervariantIndexInternal(shaderSystemInterface->GetSupervariantName()); + if (systemSupervariantIndex.IsValid()) + { + supervariantIndex = systemSupervariantIndex; + } + } + + return FindShaderResourceGroupLayout(bindingSlot, supervariantIndex); + } + const RHI::Ptr& ShaderAsset::FindFallbackShaderResourceGroupLayout( SupervariantIndex supervariantIndex) const { @@ -467,6 +514,20 @@ namespace AZ return &supervariants[index]; } + SupervariantIndex ShaderAsset::GetSupervariantIndexInternal(AZ::Name supervariantName) const + { + const auto& supervariants = GetCurrentShaderApiData().m_supervariants; + const uint32_t supervariantCount = supervariants.size(); + for (uint32_t index = 0; index < supervariantCount; ++index) + { + if (supervariants[index].m_name == supervariantName) + { + return SupervariantIndex{ index }; + } + } + return InvalidSupervariantIndex; + } + bool ShaderAsset::FinalizeAfterLoad() { // Use the current RHI that is active to select which shader data to use.