123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AtomCore/std/containers/vector_set.h>
- #include <AzCore/Asset/AssetSerializer.h>
- #include <Atom/RPI.Reflect/Material/ShaderCollection.h>
- #include <Atom/RHI/RHISystemInterface.h>
- #include <Atom/RHI/DrawListTagRegistry.h>
- namespace AZ
- {
- namespace RPI
- {
- //! This allows ShaderCollection::Item to serialize only a ShaderVariantId rather than the ShaderOptionsGroup object,
- //! but still provide the corresponding ShaderOptionsGroup for use at runtime.
- //! RenderStates will be modified at runtime as well. It will be merged into the RenderStates stored in the corresponding ShaderVariant.
- class ShaderVariantReferenceSerializationEvents
- : public SerializeContext::IEventHandler
- {
- //! Called right before we start reading from the instance pointed by classPtr.
- void OnReadBegin(void* classPtr) override
- {
- ShaderCollection::Item* shaderVariantReference = reinterpret_cast<ShaderCollection::Item*>(classPtr);
- shaderVariantReference->m_shaderVariantId = shaderVariantReference->m_shaderOptionGroup.GetShaderVariantId();
- }
- //! Called right after we finish writing data to the instance pointed at by classPtr.
- void OnWriteEnd(void* classPtr) override
- {
- ShaderCollection::Item* shaderVariantReference = reinterpret_cast<ShaderCollection::Item*>(classPtr);
- if (shaderVariantReference->m_shaderAsset.IsReady())
- {
- shaderVariantReference->m_shaderOptionGroup = ShaderOptionGroup{
- shaderVariantReference->m_shaderAsset->GetShaderOptionGroupLayout(),
- shaderVariantReference->m_shaderVariantId
- };
- }
- else
- {
- // No worries, eventually the Material::Init will end up
- // calling InitializeShaderOptionGroup() and @m_shaderOptionGroup
- // will get proper data.
- shaderVariantReference->m_shaderOptionGroup = {};
- shaderVariantReference->m_shaderAsset.QueueLoad(); // Not necessary to call QueueLoad, but doesn't hurt either.
- }
- }
- };
- void ShaderCollection::Reflect(AZ::ReflectContext* context)
- {
- ShaderCollection::Item::Reflect(context);
- NameReflectionMapForIndex::Reflect(context);
- if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
- {
- serializeContext->Class<ShaderCollection>()
- ->Version(5)
- ->Field("ShaderItems", &ShaderCollection::m_shaderItems)
- ->Field("ShaderTagIndexMap", &ShaderCollection::m_shaderTagIndexMap)
- ;
- }
- }
- void ShaderCollection::Item::Reflect(AZ::ReflectContext* context)
- {
- if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
- {
- serializeContext->Class<ShaderCollection::Item>()
- ->Version(6)
- ->EventHandler<ShaderVariantReferenceSerializationEvents>()
- ->Field("ShaderAsset", &ShaderCollection::Item::m_shaderAsset)
- ->Field("ShaderVariantId", &ShaderCollection::Item::m_shaderVariantId)
- ->Field("Enabled", &ShaderCollection::Item::m_enabled)
- ->Field("OwnedShaderOptionIndices", &ShaderCollection::Item::m_ownedShaderOptionIndices)
- ->Field("ShaderTag", &ShaderCollection::Item::m_shaderTag)
- ;
- }
- if (BehaviorContext* behaviorContext = azrtti_cast<BehaviorContext*>(context))
- {
- behaviorContext->Class<Item>("ShaderCollectionItem")
- ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
- ->Attribute(AZ::Script::Attributes::Category, "Shader")
- ->Attribute(AZ::Script::Attributes::Module, "shader")
- ->Method("GetShaderAsset", &Item::GetShaderAsset)
- ->Method("GetShaderAssetId", &Item::GetShaderAssetId)
- ->Method("GetShaderVariantId", &Item::GetShaderVariantId)
- ->Method("GetShaderOptionGroup", &Item::GetShaderOptionGroup)
- ->Method("MaterialOwnsShaderOption", static_cast<bool (Item::*)(const Name&) const>(&Item::MaterialOwnsShaderOption));
- }
- }
- size_t ShaderCollection::size() const
- {
- return m_shaderItems.size();
- }
- ShaderCollection::iterator ShaderCollection::begin()
- {
- return m_shaderItems.begin();
- }
- ShaderCollection::const_iterator ShaderCollection::begin() const
- {
- return m_shaderItems.begin();
- }
- ShaderCollection::iterator ShaderCollection::end()
- {
- return m_shaderItems.end();
- }
- ShaderCollection::const_iterator ShaderCollection::end() const
- {
- return m_shaderItems.end();
- }
- ShaderCollection::Item::Item()
- : m_renderStatesOverlay(RHI::GetInvalidRenderStates())
- {
- }
- ShaderCollection::Item& ShaderCollection::operator[](size_t i)
- {
- return m_shaderItems[i];
- }
- const ShaderCollection::Item& ShaderCollection::operator[](size_t i) const
- {
- return m_shaderItems[i];
- }
- ShaderCollection::Item& ShaderCollection::operator[](const AZ::Name& shaderTag)
- {
- return m_shaderItems[m_shaderTagIndexMap.Find(shaderTag).GetIndex()];
- }
- const ShaderCollection::Item& ShaderCollection::operator[](const AZ::Name& shaderTag) const
- {
- return m_shaderItems[m_shaderTagIndexMap.Find(shaderTag).GetIndex()];
- }
- bool ShaderCollection::HasShaderTag(const AZ::Name& shaderTag) const
- {
- return (m_shaderTagIndexMap.Find(shaderTag).IsValid());
- }
- void ShaderCollection::TryReplaceShaderAsset(const Data::Asset<ShaderAsset>& newShaderAsset)
- {
- for (auto& shaderItem : m_shaderItems)
- {
- shaderItem.TryReplaceShaderAsset(newShaderAsset);
- }
- }
- bool ShaderCollection::InitializeShaderOptionGroups()
- {
- for (auto& shaderItem : m_shaderItems)
- {
- if (!shaderItem.InitializeShaderOptionGroup())
- {
- return false;
- }
- }
- return true;
- }
- ShaderCollection::Item::Item(const Data::Asset<ShaderAsset>& shaderAsset, const AZ::Name& shaderTag, ShaderVariantId variantId)
- : m_renderStatesOverlay(RHI::GetInvalidRenderStates())
- , m_shaderAsset(shaderAsset)
- , m_shaderVariantId(variantId)
- , m_shaderTag(shaderTag)
- , m_shaderOptionGroup(shaderAsset->GetShaderOptionGroupLayout(), variantId)
- {
- }
- ShaderCollection::Item::Item(Data::Asset<ShaderAsset>&& shaderAsset, const AZ::Name& shaderTag, ShaderVariantId variantId)
- : m_renderStatesOverlay(RHI::GetInvalidRenderStates())
- , m_shaderAsset(AZStd::move(shaderAsset))
- , m_shaderVariantId(variantId)
- , m_shaderTag(shaderTag)
- , m_shaderOptionGroup(shaderAsset->GetShaderOptionGroupLayout(), variantId)
- {
- }
- const Data::Asset<ShaderAsset>& ShaderCollection::Item::GetShaderAsset() const
- {
- return m_shaderAsset;
- }
- const ShaderVariantId& ShaderCollection::Item::GetShaderVariantId() const
- {
- return m_shaderOptionGroup.GetShaderVariantId();
- }
- const ShaderOptionGroup* ShaderCollection::Item::GetShaderOptions() const
- {
- return &m_shaderOptionGroup;
- }
- ShaderOptionGroup* ShaderCollection::Item::GetShaderOptions()
- {
- return &m_shaderOptionGroup;
- }
- bool ShaderCollection::Item::MaterialOwnsShaderOption(const AZ::Name& shaderOptionName) const
- {
- return m_ownedShaderOptionIndices.contains(m_shaderOptionGroup.FindShaderOptionIndex(shaderOptionName));
- }
- bool ShaderCollection::Item::MaterialOwnsShaderOption(ShaderOptionIndex shaderOptionIndex) const
- {
- return m_ownedShaderOptionIndices.contains(shaderOptionIndex);
- }
- const RHI::RenderStates* ShaderCollection::Item::GetRenderStatesOverlay() const
- {
- return &m_renderStatesOverlay;
- }
- RHI::RenderStates* ShaderCollection::Item::GetRenderStatesOverlay()
- {
- return &m_renderStatesOverlay;
- }
- RHI::DrawListTag ShaderCollection::Item::GetDrawListTagOverride() const
- {
- return m_drawListTagOverride;
- }
- void ShaderCollection::Item::SetDrawListTagOverride(RHI::DrawListTag drawList)
- {
- m_drawListTagOverride = drawList;
- }
- void ShaderCollection::Item::SetDrawListTagOverride(const AZ::Name& drawListName)
- {
- if (drawListName.IsEmpty())
- {
- m_drawListTagOverride.Reset();
- return;
- }
- RHI::DrawListTagRegistry* drawListTagRegistry = RHI::RHISystemInterface::Get()->GetDrawListTagRegistry();
- // Note: we should use FindTag instead of AcquireTag to avoid occupy DrawListTag entries.
- RHI::DrawListTag newTag = drawListTagRegistry->FindTag(drawListName);
- if (newTag.IsNull())
- {
- AZ_Error("ShaderCollection", false, "Failed to set draw list with name: %s.", drawListName.GetCStr());
- return;
- }
- m_drawListTagOverride = newTag;
- }
- void ShaderCollection::Item::SetEnabled(bool enabled)
- {
- m_enabled = enabled;
- }
- bool ShaderCollection::Item::IsEnabled() const
- {
- return m_enabled;
- }
- const AZ::Name& ShaderCollection::Item::GetShaderTag() const
- {
- return m_shaderTag;
- }
- const Data::AssetId& ShaderCollection::Item::GetShaderAssetId() const
- {
- return m_shaderAsset->GetId();
- }
- const AZ::RPI::ShaderOptionGroup& ShaderCollection::Item::GetShaderOptionGroup() const
- {
- return m_shaderOptionGroup;
- }
- bool ShaderCollection::Item::InitializeShaderOptionGroup()
- {
- if (!m_shaderAsset.IsReady())
- {
- return false;
- }
- m_shaderOptionGroup = ShaderOptionGroup{
- m_shaderAsset->GetShaderOptionGroupLayout(),
- m_shaderVariantId };
- return true;
- }
- void ShaderCollection::Item::TryReplaceShaderAsset(const Data::Asset<ShaderAsset>& newShaderAsset)
- {
- if (newShaderAsset.GetId() != m_shaderAsset.GetId())
- {
- return;
- }
- m_shaderAsset = newShaderAsset;
- [[maybe_unused]] bool success = InitializeShaderOptionGroup();
- AZ_Assert(success, "Failed to InitializeShaderOptionGroup using shaderAsset with uuid=%s and hint=%s"
- , newShaderAsset.GetId().ToFixedString().c_str(), newShaderAsset.GetHint().c_str());
- }
- } // namespace RPI
- } // namespace AZ
|