ShaderSemantic.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/RHI.Reflect/ShaderSemantic.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/std/string/conversions.h>
  12. #include <AzCore/Utils/TypeHash.h>
  13. namespace AZ::RHI
  14. {
  15. void ShaderSemantic::Reflect(AZ::ReflectContext* context)
  16. {
  17. if (SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context))
  18. {
  19. serializeContext->Class<ShaderSemantic>()
  20. ->Version(1)
  21. ->Field("m_name", &ShaderSemantic::m_name)
  22. ->Field("m_index", &ShaderSemantic::m_index);
  23. }
  24. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  25. {
  26. behaviorContext->Class<ShaderSemantic>("ShaderSemantic")
  27. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  28. ->Attribute(AZ::Script::Attributes::Category, "render")
  29. ->Attribute(AZ::Script::Attributes::Module, "render")
  30. ->Constructor()
  31. ->Constructor<const ShaderSemantic&>()
  32. ->Constructor<const Name&, size_t>()
  33. ->Constructor<AZStd::string_view, size_t>()
  34. ->Method("ToString", &ShaderSemantic::ToString)
  35. ->Property("name", BehaviorValueProperty(&ShaderSemantic::m_name))
  36. ->Property("index", BehaviorValueProperty(&ShaderSemantic::m_index))
  37. ;
  38. }
  39. }
  40. ShaderSemantic ShaderSemantic::Parse(AZStd::string_view semantic)
  41. {
  42. AZStd::string semanticName = semantic;
  43. const size_t semanticIndexPos = semanticName.find_last_not_of("0123456789") + 1;
  44. const size_t semanticIndex = AZStd::stoi(semanticName.substr(semanticIndexPos));
  45. semanticName = semanticName.substr(0, semanticIndexPos);
  46. return ShaderSemantic(Name{ semanticName.data() }, static_cast<uint32_t>(semanticIndex));
  47. }
  48. ShaderSemantic::ShaderSemantic(const Name& name, size_t index)
  49. : m_name{ name }
  50. , m_index{ static_cast<uint32_t>(index) }
  51. {
  52. #ifdef AZ_ENABLE_TRACING
  53. if (!m_name.IsEmpty())
  54. {
  55. const char last = m_name.GetStringView()[m_name.GetStringView().size()-1];
  56. AZ_Assert(last < '0' || last > '9', "Name should not end with numeric characters. Use ShaderSemantic::Parse().");
  57. }
  58. #endif
  59. }
  60. ShaderSemantic::ShaderSemantic(AZStd::string_view name, size_t index)
  61. : ShaderSemantic{ Name{name}, index }
  62. {
  63. }
  64. bool ShaderSemantic::operator==(const ShaderSemantic& rhs) const
  65. {
  66. return this->m_index == rhs.m_index && this->m_name == rhs.m_name;
  67. }
  68. HashValue64 ShaderSemantic::GetHash(HashValue64 seed) const
  69. {
  70. seed = TypeHash64(m_name.GetHash(), seed);
  71. seed = TypeHash64(m_index, seed);
  72. return seed;
  73. }
  74. AZStd::string ShaderSemantic::ToString() const
  75. {
  76. return m_name.GetCStr() + AZStd::to_string(m_index);
  77. }
  78. }