ModelComponent.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include <AnKi/Scene/Components/ModelComponent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. #include <AnKi/Scene/Components/MoveComponent.h>
  9. #include <AnKi/Scene/Components/SkinComponent.h>
  10. #include <AnKi/Resource/ModelResource.h>
  11. #include <AnKi/Resource/ResourceManager.h>
  12. namespace anki {
  13. ModelComponent::ModelComponent(SceneNode* node)
  14. : SceneComponent(node, getStaticClassId())
  15. , m_node(node)
  16. {
  17. }
  18. ModelComponent::~ModelComponent()
  19. {
  20. GpuSceneMemoryPool& gpuScene = *getExternalSubsystems(*m_node).m_gpuSceneMemoryPool;
  21. gpuScene.free(m_gpuSceneMeshLods);
  22. gpuScene.free(m_gpuSceneUniforms);
  23. m_patchInfos.destroy(m_node->getMemoryPool());
  24. }
  25. Error ModelComponent::loadModelResource(CString filename)
  26. {
  27. m_dirty = true;
  28. ModelResourcePtr rsrc;
  29. ANKI_CHECK(getExternalSubsystems(*m_node).m_resourceManager->loadResource(filename, rsrc));
  30. m_model = std::move(rsrc);
  31. const U32 modelPatchCount = m_model->getModelPatches().getSize();
  32. m_castsShadow = false;
  33. // GPU scene allocations
  34. GpuSceneMemoryPool& gpuScene = *getExternalSubsystems(*m_node).m_gpuSceneMemoryPool;
  35. gpuScene.free(m_gpuSceneMeshLods);
  36. gpuScene.allocate(sizeof(GpuSceneMeshLod) * kMaxLodCount * modelPatchCount, 4, m_gpuSceneMeshLods);
  37. U32 uniformsSize = 0;
  38. m_patchInfos.resize(m_node->getMemoryPool(), modelPatchCount);
  39. for(U32 i = 0; i < modelPatchCount; ++i)
  40. {
  41. m_patchInfos[i].m_gpuSceneUniformsOffset = uniformsSize;
  42. const U32 size = U32(m_model->getModelPatches()[i].getMaterial()->getPrefilledLocalUniforms().getSizeInBytes());
  43. ANKI_ASSERT((size % 4) == 0);
  44. uniformsSize += size;
  45. }
  46. gpuScene.free(m_gpuSceneUniforms);
  47. gpuScene.allocate(uniformsSize, 4, m_gpuSceneUniforms);
  48. for(U32 i = 0; i < modelPatchCount; ++i)
  49. {
  50. m_patchInfos[i].m_gpuSceneUniformsOffset += U32(m_gpuSceneUniforms.m_offset);
  51. }
  52. // Some other per-patch init
  53. m_presentRenderingTechniques = RenderingTechniqueBit::kNone;
  54. for(U32 i = 0; i < modelPatchCount; ++i)
  55. {
  56. m_patchInfos[i].m_techniques = m_model->getModelPatches()[i].getMaterial()->getRenderingTechniques();
  57. m_castsShadow = m_castsShadow || m_model->getModelPatches()[i].getMaterial()->castsShadow();
  58. m_presentRenderingTechniques |= m_model->getModelPatches()[i].getMaterial()->getRenderingTechniques();
  59. }
  60. return Error::kNone;
  61. }
  62. Error ModelComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  63. {
  64. if(ANKI_UNLIKELY(m_dirty && m_model.isCreated()))
  65. {
  66. GpuSceneMicroPatcher& gpuScenePatcher = *getExternalSubsystems(*info.m_node).m_gpuSceneMicroPatcher;
  67. // Upload the mesh views
  68. const U32 modelPatchCount = m_model->getModelPatches().getSize();
  69. DynamicArrayRaii<GpuSceneMeshLod> meshLods(info.m_framePool, modelPatchCount * kMaxLodCount);
  70. for(U32 i = 0; i < modelPatchCount; ++i)
  71. {
  72. const ModelPatch& patch = m_model->getModelPatches()[i];
  73. const MeshResource& mesh = *patch.getMesh();
  74. for(U32 l = 0; l < mesh.getLodCount(); ++l)
  75. {
  76. GpuSceneMeshLod& meshLod = meshLods[i * kMaxLodCount + l];
  77. meshLod = {};
  78. meshLod.m_positionScale = mesh.getPositionsScale();
  79. meshLod.m_positionTranslation = mesh.getPositionsTranslation();
  80. for(VertexStreamId stream = VertexStreamId::kPosition; stream <= VertexStreamId::kBoneWeights; ++stream)
  81. {
  82. if(!mesh.isVertexStreamPresent(stream))
  83. {
  84. continue;
  85. }
  86. PtrSize offset;
  87. U32 vertCount;
  88. mesh.getVertexStreamInfo(l, stream, offset, vertCount);
  89. const PtrSize elementSize = getFormatInfo(kMeshRelatedVertexStreamFormats[stream]).m_texelSize;
  90. ANKI_ASSERT((offset % elementSize) == 0);
  91. meshLod.m_vertexOffsets[U32(stream)] = U32(offset / elementSize);
  92. }
  93. PtrSize offset;
  94. U32 indexCount;
  95. IndexType indexType;
  96. mesh.getIndexBufferInfo(l, offset, indexCount, indexType);
  97. meshLod.m_indexOffset = U32(offset);
  98. meshLod.m_indexCount = indexCount;
  99. }
  100. // Copy the last LOD to the rest just in case
  101. for(U32 l = mesh.getLodCount(); l < kMaxLodCount; ++l)
  102. {
  103. meshLods[i * kMaxLodCount + l] = meshLods[i * kMaxLodCount + (l - 1)];
  104. }
  105. }
  106. gpuScenePatcher.newCopy(*info.m_framePool, m_gpuSceneMeshLods.m_offset, meshLods.getSizeInBytes(),
  107. &meshLods[0]);
  108. // Upload the uniforms
  109. DynamicArrayRaii<U32> allUniforms(info.m_framePool, U32(m_gpuSceneUniforms.m_size / 4));
  110. U32 count = 0;
  111. for(U32 i = 0; i < modelPatchCount; ++i)
  112. {
  113. const ModelPatch& patch = m_model->getModelPatches()[i];
  114. const MaterialResource& mtl = *patch.getMaterial();
  115. memcpy(&allUniforms[count], mtl.getPrefilledLocalUniforms().getBegin(),
  116. mtl.getPrefilledLocalUniforms().getSizeInBytes());
  117. count += U32(mtl.getPrefilledLocalUniforms().getSizeInBytes() / 4);
  118. }
  119. ANKI_ASSERT(count * 4 == m_gpuSceneUniforms.m_size);
  120. gpuScenePatcher.newCopy(*info.m_framePool, m_gpuSceneUniforms.m_offset, m_gpuSceneUniforms.m_size,
  121. &allUniforms[0]);
  122. }
  123. updated = m_dirty;
  124. m_dirty = false;
  125. return Error::kNone;
  126. }
  127. void ModelComponent::setupRenderableQueueElements(U32 lod, RenderingTechnique technique, StackMemoryPool& tmpPool,
  128. WeakArray<RenderableQueueElement>& outRenderables) const
  129. {
  130. ANKI_ASSERT(isEnabled());
  131. ANKI_ASSERT(m_moveComponent);
  132. outRenderables.setArray(nullptr, 0);
  133. const RenderingTechniqueBit requestedRenderingTechniqueMask = RenderingTechniqueBit(1 << technique);
  134. if(!(m_presentRenderingTechniques & requestedRenderingTechniqueMask))
  135. {
  136. return;
  137. }
  138. // Allocate renderables
  139. U32 renderableCount = 0;
  140. for(U32 i = 0; i < m_patchInfos.getSize(); ++i)
  141. {
  142. renderableCount += !!(m_patchInfos[i].m_techniques & requestedRenderingTechniqueMask);
  143. }
  144. if(renderableCount == 0)
  145. {
  146. return;
  147. }
  148. RenderableQueueElement* renderables = static_cast<RenderableQueueElement*>(
  149. tmpPool.allocate(sizeof(RenderableQueueElement) * renderableCount, alignof(RenderableQueueElement)));
  150. outRenderables.setArray(renderables, renderableCount);
  151. // Fill renderables
  152. const Bool moved = m_moveComponent->wasDirtyThisFrame() && technique == RenderingTechnique::kGBuffer;
  153. const Bool hasSkin = m_skinComponent != nullptr && m_skinComponent->isEnabled();
  154. RenderingKey key;
  155. key.setLod(lod);
  156. key.setRenderingTechnique(technique);
  157. key.setVelocity(moved);
  158. key.setSkinned(hasSkin);
  159. renderableCount = 0;
  160. for(U32 i = 0; i < m_patchInfos.getSize(); ++i)
  161. {
  162. if(!(m_patchInfos[i].m_techniques & requestedRenderingTechniqueMask))
  163. {
  164. continue;
  165. }
  166. RenderableQueueElement& queueElem = renderables[renderableCount];
  167. const ModelPatch& patch = m_model->getModelPatches()[i];
  168. ModelRenderingInfo modelInf;
  169. patch.getRenderingInfo(key, modelInf);
  170. queueElem.m_program = modelInf.m_program.get();
  171. queueElem.m_worldTransformsOffset = m_moveComponent->getTransformsGpuSceneOffset();
  172. queueElem.m_uniformsOffset = m_patchInfos[i].m_gpuSceneUniformsOffset;
  173. queueElem.m_geometryOffset =
  174. U32(m_gpuSceneMeshLods.m_offset + sizeof(GpuSceneMeshLod) * (kMaxLodCount * i + lod));
  175. queueElem.m_boneTransformsOffset = (hasSkin) ? m_skinComponent->getBoneTransformsGpuSceneOffset() : 0;
  176. queueElem.m_indexCount = modelInf.m_indexCount;
  177. queueElem.m_firstIndex = U32(modelInf.m_indexBufferOffset / 2 + modelInf.m_firstIndex);
  178. queueElem.m_indexed = true;
  179. queueElem.m_primitiveTopology = PrimitiveTopology::kTriangles;
  180. queueElem.computeMergeKey();
  181. ++renderableCount;
  182. }
  183. }
  184. void ModelComponent::onOtherComponentRemovedOrAdded(SceneComponent* other, Bool added)
  185. {
  186. ANKI_ASSERT(other);
  187. if(added)
  188. {
  189. if(other->getClassId() == MoveComponent::getStaticClassId())
  190. {
  191. m_moveComponent = static_cast<MoveComponent*>(other);
  192. }
  193. else if(other->getClassId() == SkinComponent::getStaticClassId())
  194. {
  195. m_skinComponent = static_cast<SkinComponent*>(other);
  196. }
  197. }
  198. else
  199. {
  200. if(other == m_moveComponent)
  201. {
  202. m_moveComponent = nullptr;
  203. }
  204. else if(other == m_skinComponent)
  205. {
  206. m_skinComponent = nullptr;
  207. }
  208. }
  209. }
  210. } // end namespace anki