| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Resource/ResourceObject.h>
- #include <AnKi/Gr.h>
- #include <AnKi/Collision/Aabb.h>
- #include <AnKi/Resource/RenderingKey.h>
- #include <AnKi/Resource/MeshResource.h>
- #include <AnKi/Resource/MaterialResource.h>
- namespace anki {
- /// @addtogroup resource
- /// @{
- /// @memberof ModelResource
- class ModelVertexBufferBinding
- {
- public:
- BufferPtr m_buffer;
- PtrSize m_offset;
- PtrSize m_stride;
- Bool operator==(const ModelVertexBufferBinding& b) const
- {
- return m_buffer == b.m_buffer && m_offset == b.m_offset && m_stride == b.m_stride;
- }
- Bool operator!=(const ModelVertexBufferBinding& b) const
- {
- return !(*this == b);
- }
- };
- /// @memberof ModelResource
- class ModelVertexAttribute
- {
- public:
- VertexAttributeId m_location;
- Format m_format;
- U32 m_bufferBinding;
- U32 m_relativeOffset;
- Bool operator==(const ModelVertexAttribute& b) const
- {
- return m_bufferBinding == b.m_bufferBinding && m_format == b.m_format && m_relativeOffset == b.m_relativeOffset
- && m_location == b.m_location;
- }
- Bool operator!=(const ModelVertexAttribute& b) const
- {
- return !(*this == b);
- }
- };
- /// @memberof ModelResource
- /// Part of the information required render the model.
- class ModelRenderingInfo
- {
- public:
- ShaderProgramPtr m_program;
- Array<ModelVertexBufferBinding, MAX_VERTEX_ATTRIBUTES> m_vertexBufferBindings;
- U32 m_vertexBufferBindingCount;
- Array<ModelVertexAttribute, MAX_VERTEX_ATTRIBUTES> m_vertexAttributes;
- U32 m_vertexAttributeCount;
- BufferPtr m_indexBuffer;
- PtrSize m_indexBufferOffset;
- IndexType m_indexType;
- U32 m_firstIndex;
- U32 m_indexCount;
- U32 m_boneTransformsBinding;
- U32 m_prevFrameBoneTransformsBinding;
- };
- /// Part of the information required to create a TLAS and a SBT.
- /// @memberof ModelResource
- class ModelRayTracingInfo
- {
- public:
- ModelGpuDescriptor m_descriptor;
- AccelerationStructurePtr m_bottomLevelAccelerationStructure;
- Array<U32, U(RayType::COUNT)> m_shaderGroupHandleIndices;
- /// Get some pointers that the m_descriptor is pointing to. Use these pointers for life tracking.
- Array<GrObjectPtr, U(TextureChannelId::COUNT) + 2> m_grObjectReferences;
- U32 m_grObjectReferenceCount;
- };
- /// Model patch class. Its very important class and it binds a material with a few mesh (one for each LOD).
- class ModelPatch
- {
- friend class ModelResource;
- public:
- const MaterialResourcePtr& getMaterial() const
- {
- return m_mtl;
- }
- const MeshResourcePtr& getMesh(U32 lod) const
- {
- return m_meshes[lod];
- }
- const Aabb& getBoundingShape() const
- {
- return m_meshes[0]->getBoundingShape();
- }
- /// Get information for rendering.
- void getRenderingInfo(const RenderingKey& key, ModelRenderingInfo& inf) const;
- /// Get the ray tracing info.
- void getRayTracingInfo(U32 lod, ModelRayTracingInfo& info) const;
- RayTypeBit getSupportedRayTracingTypes() const
- {
- return m_mtl->getSupportedRayTracingTypes();
- }
- private:
- #if ANKI_ENABLE_ASSERTIONS
- ModelResource* m_model = nullptr;
- #endif
- MaterialResourcePtr m_mtl;
- Array<MeshResourcePtr, MAX_LOD_COUNT> m_meshes; ///< Just keep the references.
- // Begin cached data
- class VertexAttributeInfo
- {
- public:
- U32 m_bufferBinding : 8;
- U32 m_relativeOffset : 24;
- Format m_format = Format::NONE;
- };
- Array<VertexAttributeInfo, U(VertexAttributeId::COUNT)> m_vertexAttributeInfos;
- class VertexBufferInfo
- {
- public:
- BufferPtr m_buffer;
- PtrSize m_stride : 16;
- PtrSize m_offset : 48;
- };
- Array2d<VertexBufferInfo, MAX_LOD_COUNT, U(VertexAttributeBufferId::COUNT)> m_vertexBufferInfos;
- class IndexBufferInfo
- {
- public:
- BufferPtr m_buffer;
- U32 m_firstIndex = MAX_U32;
- U32 m_indexCount = MAX_U32;
- };
- Array<IndexBufferInfo, MAX_LOD_COUNT> m_indexBufferInfos;
- BitSet<U(VertexAttributeId::COUNT)> m_presentVertexAttributes = {false};
- IndexType m_indexType : 2;
- // End cached data
- U8 m_meshLodCount : 6;
- ANKI_USE_RESULT Error init(ModelResource* model, ConstWeakArray<CString> meshFNames, const CString& mtlFName,
- U32 subMeshIndex, Bool async, ResourceManager* resources);
- ANKI_USE_RESULT Bool supportsSkinning() const
- {
- return m_meshes[0]->hasBoneWeights() && m_mtl->supportsSkinning();
- }
- };
- /// Model is an entity that acts as a container for other resources. Models are all the non static objects in a map.
- ///
- /// XML file format:
- /// @code
- /// <model>
- /// <modelPatches>
- /// <modelPatch [subMeshIndex=int]>
- /// <mesh>path/to/mesh.mesh</mesh>
- /// [<mesh1>path/to/mesh_lod_1.ankimesh</mesh1>]
- /// [<mesh2>path/to/mesh_lod_2.ankimesh</mesh2>]
- /// <material>path/to/material.ankimtl</material>
- /// </modelPatch>
- /// ...
- /// <modelPatch>...</modelPatch>
- /// </modelPatches>
- /// </model>
- /// @endcode
- ///
- /// Notes:
- /// - If the materials need texture coords then mesh should have them
- /// - If the subMeshIndex is not present then assume the whole mesh
- class ModelResource : public ResourceObject
- {
- public:
- ModelResource(ResourceManager* manager);
- ~ModelResource();
- ConstWeakArray<ModelPatch> getModelPatches() const
- {
- return m_modelPatches;
- }
- /// The volume that includes all the geometry of all model patches.
- const Aabb& getBoundingVolume() const
- {
- return m_boundingVolume;
- }
- Bool supportsSkinning() const
- {
- return m_skinning;
- }
- ANKI_USE_RESULT Error load(const ResourceFilename& filename, Bool async);
- private:
- DynamicArray<ModelPatch> m_modelPatches;
- Aabb m_boundingVolume;
- Bool m_skinning = false;
- };
- /// @}
- } // end namespace anki
|