ModelComponent.cpp 8.0 KB

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