ModelResource.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Resource/ResourceObject.h>
  7. #include <AnKi/Gr.h>
  8. #include <AnKi/Collision/Aabb.h>
  9. #include <AnKi/Resource/RenderingKey.h>
  10. #include <AnKi/Resource/MeshResource.h>
  11. #include <AnKi/Resource/MaterialResource.h>
  12. namespace anki {
  13. /// @addtogroup resource
  14. /// @{
  15. /// @memberof ModelResource
  16. class ModelVertexBufferBinding
  17. {
  18. public:
  19. BufferPtr m_buffer;
  20. PtrSize m_offset;
  21. PtrSize m_stride;
  22. Bool operator==(const ModelVertexBufferBinding& b) const
  23. {
  24. return m_buffer == b.m_buffer && m_offset == b.m_offset && m_stride == b.m_stride;
  25. }
  26. Bool operator!=(const ModelVertexBufferBinding& b) const
  27. {
  28. return !(*this == b);
  29. }
  30. };
  31. /// @memberof ModelResource
  32. class ModelVertexAttribute
  33. {
  34. public:
  35. VertexAttributeId m_location;
  36. Format m_format;
  37. U32 m_bufferBinding;
  38. U32 m_relativeOffset;
  39. Bool operator==(const ModelVertexAttribute& b) const
  40. {
  41. return m_bufferBinding == b.m_bufferBinding && m_format == b.m_format && m_relativeOffset == b.m_relativeOffset
  42. && m_location == b.m_location;
  43. }
  44. Bool operator!=(const ModelVertexAttribute& b) const
  45. {
  46. return !(*this == b);
  47. }
  48. };
  49. /// @memberof ModelResource
  50. /// Part of the information required render the model.
  51. class ModelRenderingInfo
  52. {
  53. public:
  54. ShaderProgramPtr m_program;
  55. Array<ModelVertexBufferBinding, MAX_VERTEX_ATTRIBUTES> m_vertexBufferBindings;
  56. U32 m_vertexBufferBindingCount;
  57. Array<ModelVertexAttribute, MAX_VERTEX_ATTRIBUTES> m_vertexAttributes;
  58. U32 m_vertexAttributeCount;
  59. BufferPtr m_indexBuffer;
  60. PtrSize m_indexBufferOffset;
  61. IndexType m_indexType;
  62. U32 m_firstIndex;
  63. U32 m_indexCount;
  64. };
  65. /// Part of the information required to create a TLAS and a SBT.
  66. /// @memberof ModelResource
  67. class ModelRayTracingInfo
  68. {
  69. public:
  70. AccelerationStructurePtr m_bottomLevelAccelerationStructure;
  71. U32 m_shaderGroupHandleIndex;
  72. /// Get some pointers to pass to the command buffer for refcounting.
  73. ConstWeakArray<GrObjectPtr> m_grObjectReferences;
  74. };
  75. /// Model patch class. Its very important class and it binds a material with a few mesh (one for each LOD).
  76. class ModelPatch
  77. {
  78. friend class ModelResource;
  79. public:
  80. const MaterialResourcePtr& getMaterial() const
  81. {
  82. return m_mtl;
  83. }
  84. const MeshResourcePtr& getMesh(U32 lod) const
  85. {
  86. return m_meshes[lod];
  87. }
  88. const Aabb& getBoundingShape() const
  89. {
  90. return m_meshes[0]->getBoundingShape();
  91. }
  92. /// Get information for rendering.
  93. void getRenderingInfo(const RenderingKey& key, ModelRenderingInfo& inf) const;
  94. /// Get the ray tracing info.
  95. void getRayTracingInfo(const RenderingKey& key, ModelRayTracingInfo& info) const;
  96. private:
  97. #if ANKI_ENABLE_ASSERTIONS
  98. ModelResource* m_model = nullptr;
  99. #endif
  100. MaterialResourcePtr m_mtl;
  101. Array<MeshResourcePtr, MAX_LOD_COUNT> m_meshes; ///< Just keep the references.
  102. DynamicArray<GrObjectPtr> m_grObjectRefs;
  103. // Begin cached data
  104. class VertexAttributeInfo
  105. {
  106. public:
  107. U32 m_bufferBinding : 8;
  108. U32 m_relativeOffset : 24;
  109. Format m_format = Format::NONE;
  110. };
  111. Array<VertexAttributeInfo, U(VertexAttributeId::COUNT)> m_vertexAttributeInfos;
  112. class VertexBufferInfo
  113. {
  114. public:
  115. BufferPtr m_buffer;
  116. PtrSize m_stride : 16;
  117. PtrSize m_offset : 48;
  118. };
  119. Array2d<VertexBufferInfo, MAX_LOD_COUNT, U(VertexAttributeBufferId::COUNT)> m_vertexBufferInfos;
  120. class IndexBufferInfo
  121. {
  122. public:
  123. BufferPtr m_buffer;
  124. PtrSize m_offset;
  125. U32 m_firstIndex = MAX_U32;
  126. U32 m_indexCount = MAX_U32;
  127. };
  128. Array<IndexBufferInfo, MAX_LOD_COUNT> m_indexBufferInfos;
  129. BitSet<U(VertexAttributeId::COUNT)> m_presentVertexAttributes = {false};
  130. IndexType m_indexType : 2;
  131. // End cached data
  132. U8 m_meshLodCount : 6;
  133. Error init(ModelResource* model, ConstWeakArray<CString> meshFNames, const CString& mtlFName, U32 subMeshIndex,
  134. Bool async, ResourceManager* resources);
  135. [[nodiscard]] Bool supportsSkinning() const
  136. {
  137. return m_meshes[0]->hasBoneWeights() && m_mtl->supportsSkinning();
  138. }
  139. };
  140. /// Model is an entity that acts as a container for other resources. Models are all the non static objects in a map.
  141. ///
  142. /// XML file format:
  143. /// @code
  144. /// <model>
  145. /// <modelPatches>
  146. /// <modelPatch [subMeshIndex=int]>
  147. /// <mesh>path/to/mesh.mesh</mesh>
  148. /// [<mesh1>path/to/mesh_lod_1.ankimesh</mesh1>]
  149. /// [<mesh2>path/to/mesh_lod_2.ankimesh</mesh2>]
  150. /// <material>path/to/material.ankimtl</material>
  151. /// </modelPatch>
  152. /// ...
  153. /// <modelPatch>...</modelPatch>
  154. /// </modelPatches>
  155. /// </model>
  156. /// @endcode
  157. ///
  158. /// Notes:
  159. /// - If the materials need texture coords then mesh should have them
  160. /// - If the subMeshIndex is not present then assume the whole mesh
  161. class ModelResource : public ResourceObject
  162. {
  163. public:
  164. ModelResource(ResourceManager* manager);
  165. ~ModelResource();
  166. ConstWeakArray<ModelPatch> getModelPatches() const
  167. {
  168. return m_modelPatches;
  169. }
  170. /// The volume that includes all the geometry of all model patches.
  171. const Aabb& getBoundingVolume() const
  172. {
  173. return m_boundingVolume;
  174. }
  175. Error load(const ResourceFilename& filename, Bool async);
  176. private:
  177. DynamicArray<ModelPatch> m_modelPatches;
  178. Aabb m_boundingVolume;
  179. };
  180. /// @}
  181. } // end namespace anki