ModelResource.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright (C) 2009-2021, 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. U32 m_boneTransformsBinding;
  65. U32 m_prevFrameBoneTransformsBinding;
  66. };
  67. /// Part of the information required to create a TLAS and a SBT.
  68. /// @memberof ModelResource
  69. class ModelRayTracingInfo
  70. {
  71. public:
  72. ModelGpuDescriptor m_descriptor;
  73. AccelerationStructurePtr m_bottomLevelAccelerationStructure;
  74. Array<U32, U(RayType::COUNT)> m_shaderGroupHandleIndices;
  75. /// Get some pointers that the m_descriptor is pointing to. Use these pointers for life tracking.
  76. Array<GrObjectPtr, U(TextureChannelId::COUNT) + 2> m_grObjectReferences;
  77. U32 m_grObjectReferenceCount;
  78. };
  79. /// Model patch class. Its very important class and it binds a material with a few mesh (one for each LOD).
  80. class ModelPatch
  81. {
  82. friend class ModelResource;
  83. public:
  84. const MaterialResourcePtr& getMaterial() const
  85. {
  86. return m_mtl;
  87. }
  88. const MeshResourcePtr& getMesh(U32 lod) const
  89. {
  90. return m_meshes[lod];
  91. }
  92. const Aabb& getBoundingShape() const
  93. {
  94. return m_meshes[0]->getBoundingShape();
  95. }
  96. /// Get information for rendering.
  97. void getRenderingInfo(const RenderingKey& key, ModelRenderingInfo& inf) const;
  98. /// Get the ray tracing info.
  99. void getRayTracingInfo(U32 lod, ModelRayTracingInfo& info) const;
  100. RayTypeBit getSupportedRayTracingTypes() const
  101. {
  102. return m_mtl->getSupportedRayTracingTypes();
  103. }
  104. private:
  105. #if ANKI_ENABLE_ASSERTIONS
  106. ModelResource* m_model = nullptr;
  107. #endif
  108. MaterialResourcePtr m_mtl;
  109. Array<MeshResourcePtr, MAX_LOD_COUNT> m_meshes; ///< Just keep the references.
  110. // Begin cached data
  111. class VertexAttributeInfo
  112. {
  113. public:
  114. U32 m_bufferBinding : 8;
  115. U32 m_relativeOffset : 24;
  116. Format m_format = Format::NONE;
  117. };
  118. Array<VertexAttributeInfo, U(VertexAttributeId::COUNT)> m_vertexAttributeInfos;
  119. class VertexBufferInfo
  120. {
  121. public:
  122. BufferPtr m_buffer;
  123. PtrSize m_stride : 16;
  124. PtrSize m_offset : 48;
  125. };
  126. Array2d<VertexBufferInfo, MAX_LOD_COUNT, U(VertexAttributeBufferId::COUNT)> m_vertexBufferInfos;
  127. class IndexBufferInfo
  128. {
  129. public:
  130. BufferPtr m_buffer;
  131. U32 m_firstIndex = MAX_U32;
  132. U32 m_indexCount = MAX_U32;
  133. };
  134. Array<IndexBufferInfo, MAX_LOD_COUNT> m_indexBufferInfos;
  135. BitSet<U(VertexAttributeId::COUNT)> m_presentVertexAttributes = {false};
  136. IndexType m_indexType : 2;
  137. // End cached data
  138. U8 m_meshLodCount : 6;
  139. ANKI_USE_RESULT Error init(ModelResource* model, ConstWeakArray<CString> meshFNames, const CString& mtlFName,
  140. U32 subMeshIndex, Bool async, ResourceManager* resources);
  141. ANKI_USE_RESULT Bool supportsSkinning() const
  142. {
  143. return m_meshes[0]->hasBoneWeights() && m_mtl->supportsSkinning();
  144. }
  145. };
  146. /// Model is an entity that acts as a container for other resources. Models are all the non static objects in a map.
  147. ///
  148. /// XML file format:
  149. /// @code
  150. /// <model>
  151. /// <modelPatches>
  152. /// <modelPatch [subMeshIndex=int]>
  153. /// <mesh>path/to/mesh.mesh</mesh>
  154. /// [<mesh1>path/to/mesh_lod_1.ankimesh</mesh1>]
  155. /// [<mesh2>path/to/mesh_lod_2.ankimesh</mesh2>]
  156. /// <material>path/to/material.ankimtl</material>
  157. /// </modelPatch>
  158. /// ...
  159. /// <modelPatch>...</modelPatch>
  160. /// </modelPatches>
  161. /// </model>
  162. /// @endcode
  163. ///
  164. /// Notes:
  165. /// - If the materials need texture coords then mesh should have them
  166. /// - If the subMeshIndex is not present then assume the whole mesh
  167. class ModelResource : public ResourceObject
  168. {
  169. public:
  170. ModelResource(ResourceManager* manager);
  171. ~ModelResource();
  172. ConstWeakArray<ModelPatch> getModelPatches() const
  173. {
  174. return m_modelPatches;
  175. }
  176. /// The volume that includes all the geometry of all model patches.
  177. const Aabb& getBoundingVolume() const
  178. {
  179. return m_boundingVolume;
  180. }
  181. Bool supportsSkinning() const
  182. {
  183. return m_skinning;
  184. }
  185. ANKI_USE_RESULT Error load(const ResourceFilename& filename, Bool async);
  186. private:
  187. DynamicArray<ModelPatch> m_modelPatches;
  188. Aabb m_boundingVolume;
  189. Bool m_skinning = false;
  190. };
  191. /// @}
  192. } // end namespace anki