3
0

MaterialNameContext.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <Atom/RPI.Reflect/Material/MaterialNameContext.h>
  9. #include <AzCore/RTTI/ReflectContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Name/Name.h>
  12. namespace AZ
  13. {
  14. namespace RPI
  15. {
  16. void MaterialNameContext::Reflect(ReflectContext* context)
  17. {
  18. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  19. {
  20. serializeContext->Class<MaterialNameContext>()
  21. ->Version(1)
  22. ->Field("propertyIdContext", &MaterialNameContext::m_propertyIdContext)
  23. ->Field("srgInputNameContext", &MaterialNameContext::m_srgInputNameContext)
  24. ->Field("shaderOptionNameContext", &MaterialNameContext::m_shaderOptionNameContext)
  25. ;
  26. }
  27. }
  28. bool MaterialNameContext::IsDefault() const
  29. {
  30. return m_propertyIdContext.empty() && m_srgInputNameContext.empty() && m_shaderOptionNameContext.empty();
  31. }
  32. void MaterialNameContext::ExtendPropertyIdContext(AZStd::string_view nameContext, bool insertDelimiter)
  33. {
  34. m_propertyIdContext += nameContext;
  35. if (insertDelimiter && !nameContext.empty() && !nameContext.ends_with("."))
  36. {
  37. m_propertyIdContext += ".";
  38. }
  39. }
  40. void MaterialNameContext::ExtendSrgInputContext(AZStd::string_view nameContext)
  41. {
  42. m_srgInputNameContext += nameContext;
  43. }
  44. void MaterialNameContext::ExtendShaderOptionContext(AZStd::string_view nameContext)
  45. {
  46. m_shaderOptionNameContext += nameContext;
  47. }
  48. bool MaterialNameContext::ContextualizeProperty(Name& propertyName) const
  49. {
  50. if (m_propertyIdContext.empty())
  51. {
  52. return false;
  53. }
  54. propertyName = m_propertyIdContext + propertyName.GetCStr();
  55. return true;
  56. }
  57. bool MaterialNameContext::ContextualizeSrgInput(Name& srgInputName) const
  58. {
  59. if (m_srgInputNameContext.empty())
  60. {
  61. return false;
  62. }
  63. srgInputName = m_srgInputNameContext + srgInputName.GetCStr();
  64. return true;
  65. }
  66. bool MaterialNameContext::ContextualizeShaderOption(Name& shaderOptionName) const
  67. {
  68. if (m_shaderOptionNameContext.empty())
  69. {
  70. return false;
  71. }
  72. shaderOptionName = m_shaderOptionNameContext + shaderOptionName.GetCStr();
  73. return true;
  74. }
  75. bool MaterialNameContext::ContextualizeProperty(AZStd::string& propertyName) const
  76. {
  77. if (m_propertyIdContext.empty())
  78. {
  79. return false;
  80. }
  81. propertyName = m_propertyIdContext + propertyName;
  82. return true;
  83. }
  84. bool MaterialNameContext::ContextualizeSrgInput(AZStd::string& srgInputName) const
  85. {
  86. if (m_srgInputNameContext.empty())
  87. {
  88. return false;
  89. }
  90. srgInputName = m_srgInputNameContext + srgInputName;
  91. return true;
  92. }
  93. bool MaterialNameContext::ContextualizeShaderOption(AZStd::string& shaderOptionName) const
  94. {
  95. if (m_shaderOptionNameContext.empty())
  96. {
  97. return false;
  98. }
  99. shaderOptionName = m_shaderOptionNameContext + shaderOptionName;
  100. return true;
  101. }
  102. Name MaterialNameContext::GetContextualizedProperty(Name& propertyName) const
  103. {
  104. Name contextualizedName = propertyName;
  105. ContextualizeProperty(contextualizedName);
  106. return contextualizedName;
  107. }
  108. Name MaterialNameContext::GetContextualizedSrgInput(Name& srgInputName) const
  109. {
  110. Name contextualizedName = srgInputName;
  111. ContextualizeSrgInput(contextualizedName);
  112. return contextualizedName;
  113. }
  114. Name MaterialNameContext::GetContextualizedShaderOption(Name& shaderOptionName) const
  115. {
  116. Name contextualizedName = shaderOptionName;
  117. ContextualizeShaderOption(contextualizedName);
  118. return contextualizedName;
  119. }
  120. AZStd::string MaterialNameContext::GetContextualizedProperty(const AZStd::string& propertyName) const
  121. {
  122. AZStd::string contextualizedName = propertyName;
  123. ContextualizeProperty(contextualizedName);
  124. return contextualizedName;
  125. }
  126. AZStd::string MaterialNameContext::GetContextualizedSrgInput(const AZStd::string& srgInputName) const
  127. {
  128. AZStd::string contextualizedName = srgInputName;
  129. ContextualizeSrgInput(contextualizedName);
  130. return contextualizedName;
  131. }
  132. AZStd::string MaterialNameContext::GetContextualizedShaderOption(const AZStd::string& shaderOptionName) const
  133. {
  134. AZStd::string contextualizedName = shaderOptionName;
  135. ContextualizeShaderOption(contextualizedName);
  136. return contextualizedName;
  137. }
  138. bool MaterialNameContext::HasContextForProperties() const
  139. {
  140. return !m_propertyIdContext.empty();
  141. }
  142. bool MaterialNameContext::HasContextForSrgInputs() const
  143. {
  144. return !m_srgInputNameContext.empty();
  145. }
  146. bool MaterialNameContext::HasContextForShaderOptions() const
  147. {
  148. return !m_shaderOptionNameContext.empty();
  149. }
  150. } // namespace RPI
  151. } // namespace AZ