ShaderInputNameIndex.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Serialization/SerializeContext.h>
  9. #include <Atom/RHI.Reflect/ShaderInputNameIndex.h>
  10. #include <Atom/RHI.Reflect/ShaderResourceGroupLayout.h>
  11. namespace AZ::RHI
  12. {
  13. // --- Constructors, assignment and reflection ---
  14. ShaderInputNameIndex::ShaderInputNameIndex(Name name)
  15. {
  16. *this = name;
  17. }
  18. ShaderInputNameIndex::ShaderInputNameIndex(const char* name)
  19. {
  20. *this = name;
  21. }
  22. void ShaderInputNameIndex::operator=(Name name)
  23. {
  24. Reset();
  25. m_name = name;
  26. }
  27. void ShaderInputNameIndex::operator=(const char* name)
  28. {
  29. Reset();
  30. m_name = Name(name);
  31. }
  32. void ShaderInputNameIndex::Reflect(AZ::ReflectContext* context)
  33. {
  34. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  35. {
  36. // Only serialize the Name field since the rest of the members are derived from the name at runtime
  37. serializeContext->Class<ShaderInputNameIndex>()
  38. ->Version(0)
  39. ->Field("Name", &ShaderInputNameIndex::m_name);
  40. }
  41. }
  42. // --- Functions for initializing the index ---
  43. void ShaderInputNameIndex::Initialize(IndexType indexType)
  44. {
  45. AssertHasName();
  46. m_initialized = true;
  47. m_inputType = indexType;
  48. }
  49. void ShaderInputNameIndex::FindBufferIndex(const ShaderResourceGroupLayout* srgLayout)
  50. {
  51. Initialize(IndexType::ShaderBuffer);
  52. m_index = Handle<>(srgLayout->FindShaderInputBufferIndex(m_name).GetIndex());
  53. }
  54. void ShaderInputNameIndex::FindImageIndex(const ShaderResourceGroupLayout* srgLayout)
  55. {
  56. Initialize(IndexType::ShaderImage);
  57. m_index = Handle<>(srgLayout->FindShaderInputImageIndex(m_name).GetIndex());
  58. }
  59. void ShaderInputNameIndex::FindSamplerIndex(const ShaderResourceGroupLayout* srgLayout)
  60. {
  61. Initialize(IndexType::ShaderSampler);
  62. m_index = Handle<>(srgLayout->FindShaderInputSamplerIndex(m_name).GetIndex());
  63. }
  64. void ShaderInputNameIndex::FindConstantIndex(const ShaderResourceGroupLayout* srgLayout)
  65. {
  66. Initialize(IndexType::ShaderConstant);
  67. m_index = Handle<>(srgLayout->FindShaderInputConstantIndex(m_name).GetIndex());
  68. }
  69. // --- Functions for checking if the index is initialized and retrieving it if not
  70. bool ShaderInputNameIndex::ValidateOrFindBufferIndex(const ShaderResourceGroupLayout* srgLayout)
  71. {
  72. if (IsValid()) // 99% use case, check this first for a quick early out
  73. {
  74. return true;
  75. }
  76. if (!m_initialized)
  77. {
  78. FindBufferIndex(srgLayout);
  79. return IsValid();
  80. }
  81. return false;
  82. }
  83. bool ShaderInputNameIndex::ValidateOrFindImageIndex(const ShaderResourceGroupLayout* srgLayout)
  84. {
  85. if (IsValid()) // 99% use case, check this first for a quick early out
  86. {
  87. return true;
  88. }
  89. if (!m_initialized)
  90. {
  91. FindImageIndex(srgLayout);
  92. return IsValid();
  93. }
  94. return false;
  95. }
  96. bool ShaderInputNameIndex::ValidateOrFindSamplerIndex(const ShaderResourceGroupLayout* srgLayout)
  97. {
  98. if (IsValid()) // 99% use case, check this first for a quick early out
  99. {
  100. return true;
  101. }
  102. if (!m_initialized)
  103. {
  104. FindSamplerIndex(srgLayout);
  105. return IsValid();
  106. }
  107. return false;
  108. }
  109. bool ShaderInputNameIndex::ValidateOrFindConstantIndex(const ShaderResourceGroupLayout* srgLayout)
  110. {
  111. if (IsValid()) // 99% use case, check this first for a quick early out
  112. {
  113. return true;
  114. }
  115. if (!m_initialized)
  116. {
  117. FindConstantIndex(srgLayout);
  118. return IsValid();
  119. }
  120. return false;
  121. }
  122. // --- Index getters with assertions ---
  123. u32 ShaderInputNameIndex::GetIndex() const
  124. {
  125. AssertValid();
  126. return m_index.GetIndex();
  127. }
  128. template<typename T>
  129. T ShaderInputNameIndex::GetIndexAs() const
  130. {
  131. return static_cast<T>(GetIndex());
  132. }
  133. ShaderInputBufferIndex ShaderInputNameIndex::GetBufferIndex() const
  134. {
  135. AZ_Assert(m_inputType == IndexType::ShaderBuffer, "ShaderInputNameIndex [%s] being cast as BufferIndex but is not of Buffer type!", m_name.GetCStr());
  136. return GetIndexAs<ShaderInputBufferIndex>();
  137. }
  138. ShaderInputImageIndex ShaderInputNameIndex::GetImageIndex() const
  139. {
  140. AZ_Assert(m_inputType == IndexType::ShaderImage, "ShaderInputNameIndex [%s] being cast as ImageIndex but is not of Image type!", m_name.GetCStr());
  141. return GetIndexAs<ShaderInputImageIndex>();
  142. }
  143. ShaderInputSamplerIndex ShaderInputNameIndex::GetSamplerIndex() const
  144. {
  145. AZ_Assert(m_inputType == IndexType::ShaderSampler, "ShaderInputNameIndex [%s] being cast as SamplerIndex but is not of Sampler type!", m_name.GetCStr());
  146. return GetIndexAs<ShaderInputSamplerIndex>();
  147. }
  148. ShaderInputConstantIndex ShaderInputNameIndex::GetConstantIndex() const
  149. {
  150. AZ_Assert(m_inputType == IndexType::ShaderConstant, "ShaderInputNameIndex [%s] being cast as ConstantIndex but is not of Constant type!", m_name.GetCStr());
  151. return GetIndexAs<ShaderInputConstantIndex>();
  152. }
  153. ShaderInputStaticSamplerIndex ShaderInputNameIndex::GetStaticSamplerIndex() const
  154. {
  155. AZ_Assert(m_inputType == IndexType::ShaderSampler, "ShaderInputNameIndex [%s] being cast as SamplerIndex but is not of Sampler type!", m_name.GetCStr());
  156. return GetIndexAs<ShaderInputStaticSamplerIndex>();
  157. }
  158. // --- Reset & Clear ---
  159. void ShaderInputNameIndex::Reset()
  160. {
  161. m_index.Reset();
  162. m_initialized = false;
  163. m_inputType = IndexType::InvalidIndex;
  164. }
  165. // --- Checks and asserts ---
  166. bool ShaderInputNameIndex::HasName() const
  167. {
  168. return !m_name.IsEmpty();
  169. }
  170. void ShaderInputNameIndex::AssertHasName() const
  171. {
  172. AZ_Assert(HasName(), "ShaderInputNameIndex does not have a valid Name. Please initialize with a valid Name.");
  173. }
  174. bool ShaderInputNameIndex::IsValid() const
  175. {
  176. return m_index.IsValid();
  177. }
  178. void ShaderInputNameIndex::AssertValid() const
  179. {
  180. AZ_Assert(IsValid(), "ShaderInputNameIndex [%s] does not have a valid index. Please initialize with the Shader Resource Group.", m_name.GetCStr());
  181. }
  182. bool ShaderInputNameIndex::IsInitialized() const
  183. {
  184. return m_initialized;
  185. }
  186. void ShaderInputNameIndex::AssetInialized() const
  187. {
  188. AZ_Assert(IsInitialized(), "ShaderInputNameIndex [%s] has not been initialized. Please initialize with the Shader Resource Group.", m_name.GetCStr());
  189. }
  190. const Name& ShaderInputNameIndex::GetNameForDebug() const
  191. {
  192. AZ_Assert(HasName(), "GetNameForDebug() called on ShaderInputNameIndex that doesn't have a name set. Please initialize it with a name.", m_name.GetCStr());
  193. return m_name;
  194. }
  195. }