WhiteBoxAttributeBuffer.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #pragma once
  9. #include "WhiteBoxBuffer.h"
  10. #include <Atom/RHI.Reflect/ShaderSemantic.h>
  11. #include <Atom/RPI.Reflect/Model/ModelLodAssetCreator.h>
  12. namespace WhiteBox
  13. {
  14. //! Attributes for white box mesh vertices.
  15. enum class AttributeType
  16. {
  17. Position,
  18. Normal,
  19. Tangent,
  20. Bitangent,
  21. UV,
  22. Color
  23. };
  24. //! The number of attributes required by the white box mesh.
  25. inline constexpr uint32_t NumAttributes = 6;
  26. //! Trait to describe white box mesh vertex attribute format.
  27. template<AttributeType AttributeTypeT>
  28. struct AttributeTrait
  29. {
  30. };
  31. //! Attribute trait specialization for vertex position attribute.
  32. template<>
  33. struct AttributeTrait<AttributeType::Position>
  34. {
  35. static constexpr const char* ShaderSemantic = "POSITION";
  36. using BufferType = Vector3Buffer;
  37. };
  38. //! Attribute trait specialization for vertex normal attribute
  39. template<>
  40. struct AttributeTrait<AttributeType::Normal>
  41. {
  42. static constexpr const char* ShaderSemantic = "NORMAL";
  43. using BufferType = Vector3Buffer;
  44. };
  45. //! Attribute trait specialization for vertex tangent attribute.
  46. template<>
  47. struct AttributeTrait<AttributeType::Tangent>
  48. {
  49. static constexpr const char* ShaderSemantic = "TANGENT";
  50. using BufferType = Vector4Buffer;
  51. };
  52. //! Attribute trait specialization for vertex bitangent attribute.
  53. template<>
  54. struct AttributeTrait<AttributeType::Bitangent>
  55. {
  56. static constexpr const char* ShaderSemantic = "BITANGENT";
  57. using BufferType = Vector3Buffer;
  58. };
  59. //! Attribute trait specialization for vertex uv attribute.
  60. template<>
  61. struct AttributeTrait<AttributeType::UV>
  62. {
  63. static constexpr const char* ShaderSemantic = "UV";
  64. using BufferType = Vector2Buffer;
  65. };
  66. //! Attribute trait specialization for vertex color attribute.
  67. template<>
  68. struct AttributeTrait<AttributeType::Color>
  69. {
  70. static constexpr const char* ShaderSemantic = "COLOR";
  71. using BufferType = Vector4Buffer;
  72. };
  73. //! Buffer to hold white box mesh vertex attribute data.
  74. template<AttributeType AttributeTypeT>
  75. class AttributeBuffer
  76. {
  77. public:
  78. using Trait = AttributeTrait<AttributeTypeT>;
  79. //! Construct a new Attribute Buffer object from the specified data.
  80. template<typename VertexStreamDataType>
  81. AttributeBuffer(const AZStd::vector<VertexStreamDataType>& data);
  82. //! Retrieves the buffer asset.
  83. const AZ::Data::Asset<AZ::RPI::BufferAsset>& GetBuffer() const;
  84. //! Retrieves the buffer view descriptor.
  85. const AZ::RHI::BufferViewDescriptor& GetBufferViewDescriptor() const;
  86. //! Retrieves the buffer asset view.
  87. const AZ::RPI::BufferAssetView& GetBufferAssetView() const;
  88. //! Retrieves the attribute's shader semantic.
  89. const AZ::RHI::ShaderSemantic& GetShaderSemantic() const;
  90. //! Adds this attribute buffer to the Lod.
  91. void AddLodStreamBuffer(AZ::RPI::ModelLodAssetCreator& modelLodCreator) const;
  92. //! Adds this attribute buffer to the mesh.
  93. void AddMeshStreamBuffer(AZ::RPI::ModelLodAssetCreator& modelLodCreator) const;
  94. //! Returns true of the attribute buffer is valid, otherwise false.
  95. bool IsValid() const;
  96. //! Update the attribute buffer contents with the new data.
  97. template<typename VertexStreamDataType>
  98. bool UpdateData(const AZStd::vector<VertexStreamDataType>& data);
  99. private:
  100. typename Trait::BufferType m_buffer;
  101. AZ::RHI::ShaderSemantic m_shaderSemantic;
  102. };
  103. template<AttributeType AttributeTypeT>
  104. template<typename VertexStreamDataType>
  105. AttributeBuffer<AttributeTypeT>::AttributeBuffer(const AZStd::vector<VertexStreamDataType>& data)
  106. : m_buffer(data)
  107. , m_shaderSemantic(AZ::Name(Trait::ShaderSemantic))
  108. {
  109. if (!IsValid())
  110. {
  111. AZ_Error(
  112. "AttributeBuffer", false, "Couldn't create buffer for attribute %s",
  113. m_shaderSemantic.ToString().c_str());
  114. }
  115. }
  116. template<AttributeType AttributeTypeT>
  117. const AZ::Data::Asset<AZ::RPI::BufferAsset>& AttributeBuffer<AttributeTypeT>::GetBuffer() const
  118. {
  119. return m_buffer.GetBuffer();
  120. }
  121. template<AttributeType AttributeTypeT>
  122. const AZ::RHI::BufferViewDescriptor& AttributeBuffer<AttributeTypeT>::GetBufferViewDescriptor() const
  123. {
  124. return m_buffer.GetBufferViewDescriptor();
  125. }
  126. template<AttributeType AttributeTypeT>
  127. const AZ::RPI::BufferAssetView& AttributeBuffer<AttributeTypeT>::GetBufferAssetView() const
  128. {
  129. return m_buffer.GetBufferAssetView();
  130. }
  131. template<AttributeType AttributeTypeT>
  132. const AZ::RHI::ShaderSemantic& AttributeBuffer<AttributeTypeT>::GetShaderSemantic() const
  133. {
  134. return m_shaderSemantic;
  135. }
  136. template<AttributeType AttributeTypeT>
  137. void AttributeBuffer<AttributeTypeT>::AddLodStreamBuffer(AZ::RPI::ModelLodAssetCreator& modelLodCreator) const
  138. {
  139. modelLodCreator.AddLodStreamBuffer(GetBuffer());
  140. }
  141. template<AttributeType AttributeTypeT>
  142. void AttributeBuffer<AttributeTypeT>::AddMeshStreamBuffer(AZ::RPI::ModelLodAssetCreator& modelLodCreator) const
  143. {
  144. modelLodCreator.AddMeshStreamBuffer(GetShaderSemantic(), AZ::Name(), GetBufferAssetView());
  145. }
  146. template<AttributeType AttributeTypeT>
  147. bool AttributeBuffer<AttributeTypeT>::IsValid() const
  148. {
  149. return m_buffer.IsValid();
  150. }
  151. template<AttributeType AttributeTypeT>
  152. template<typename VertexStreamDataType>
  153. bool AttributeBuffer<AttributeTypeT>::UpdateData(const AZStd::vector<VertexStreamDataType>& data)
  154. {
  155. if (!m_buffer.UpdateData(data))
  156. {
  157. AZ_Error(
  158. "AttributeBuffer", false, "Couldn't update buffer for attribute %s",
  159. m_shaderSemantic.ToString().c_str());
  160. return false;
  161. }
  162. return true;
  163. }
  164. //! Attribute buffer alias for position attributes.
  165. using PositionAttribute = AttributeBuffer<AttributeType::Position>;
  166. //! Attribute buffer alias for normal attributes.
  167. using NormalAttribute = AttributeBuffer<AttributeType::Normal>;
  168. //! Attribute buffer alias for tangent attributes.
  169. using TangentAttribute = AttributeBuffer<AttributeType::Tangent>;
  170. //! Attribute buffer alias for bitangent attributes.
  171. using BitangentAttribute = AttributeBuffer<AttributeType::Bitangent>;
  172. //! Attribute buffer alias for uv attributes.
  173. using UVAttribute = AttributeBuffer<AttributeType::UV>;
  174. //! Attribute buffer alias for color attributes.
  175. using ColorAttribute = AttributeBuffer<AttributeType::Color>;
  176. } // namespace WhiteBox