| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Scene/Components/ModelComponent.h>
- #include <AnKi/Scene/SceneNode.h>
- #include <AnKi/Scene/SceneGraph.h>
- #include <AnKi/Scene/Components/MoveComponent.h>
- #include <AnKi/Scene/Components/SkinComponent.h>
- #include <AnKi/Resource/ModelResource.h>
- #include <AnKi/Resource/ResourceManager.h>
- namespace anki {
- ModelComponent::ModelComponent(SceneNode* node)
- : SceneComponent(node, getStaticClassId())
- , m_node(node)
- , m_spatial(this)
- {
- m_gpuSceneTransformsIndex = U32(
- node->getSceneGraph().getAllGpuSceneContiguousArrays().allocate(GpuSceneContiguousArrayType::kTransformPairs));
- }
- ModelComponent::~ModelComponent()
- {
- GpuSceneMemoryPool::getSingleton().deferredFree(m_gpuSceneUniforms);
- for(const PatchInfo& patch : m_patchInfos)
- {
- if(patch.m_gpuSceneMeshLodsIndex != kMaxU32)
- {
- m_node->getSceneGraph().getAllGpuSceneContiguousArrays().deferredFree(
- GpuSceneContiguousArrayType::kMeshLods, patch.m_gpuSceneMeshLodsIndex);
- }
- }
- m_node->getSceneGraph().getAllGpuSceneContiguousArrays().deferredFree(GpuSceneContiguousArrayType::kTransformPairs,
- m_gpuSceneTransformsIndex);
- m_patchInfos.destroy(m_node->getMemoryPool());
- m_spatial.removeFromOctree(m_node->getSceneGraph().getOctree());
- }
- void ModelComponent::loadModelResource(CString filename)
- {
- ModelResourcePtr rsrc;
- const Error err = getExternalSubsystems(*m_node).m_resourceManager->loadResource(filename, rsrc);
- if(err)
- {
- ANKI_SCENE_LOGE("Failed to load model resource");
- return;
- }
- m_dirty = true;
- m_model = std::move(rsrc);
- const U32 modelPatchCount = m_model->getModelPatches().getSize();
- // GPU scene allocations
- for(const PatchInfo& patch : m_patchInfos)
- {
- if(patch.m_gpuSceneMeshLodsIndex != kMaxU32)
- {
- m_node->getSceneGraph().getAllGpuSceneContiguousArrays().deferredFree(
- GpuSceneContiguousArrayType::kMeshLods, patch.m_gpuSceneMeshLodsIndex);
- }
- }
- m_patchInfos.resize(m_node->getMemoryPool(), modelPatchCount);
- for(U32 i = 0; i < modelPatchCount; ++i)
- {
- m_patchInfos[i].m_gpuSceneMeshLodsIndex = U32(
- m_node->getSceneGraph().getAllGpuSceneContiguousArrays().allocate(GpuSceneContiguousArrayType::kMeshLods));
- }
- U32 uniformsSize = 0;
- for(U32 i = 0; i < modelPatchCount; ++i)
- {
- m_patchInfos[i].m_gpuSceneUniformsOffset = uniformsSize;
- const U32 size = U32(m_model->getModelPatches()[i].getMaterial()->getPrefilledLocalUniforms().getSizeInBytes());
- ANKI_ASSERT((size % 4) == 0);
- uniformsSize += size;
- }
- GpuSceneMemoryPool::getSingleton().deferredFree(m_gpuSceneUniforms);
- GpuSceneMemoryPool::getSingleton().allocate(uniformsSize, 4, m_gpuSceneUniforms);
- for(U32 i = 0; i < modelPatchCount; ++i)
- {
- m_patchInfos[i].m_gpuSceneUniformsOffset += U32(m_gpuSceneUniforms.m_offset);
- }
- // Some other per-patch init
- m_presentRenderingTechniques = RenderingTechniqueBit::kNone;
- m_castsShadow = false;
- for(U32 i = 0; i < modelPatchCount; ++i)
- {
- m_patchInfos[i].m_techniques = m_model->getModelPatches()[i].getMaterial()->getRenderingTechniques();
- m_castsShadow = m_castsShadow || m_model->getModelPatches()[i].getMaterial()->castsShadow();
- m_presentRenderingTechniques |= m_model->getModelPatches()[i].getMaterial()->getRenderingTechniques();
- }
- }
- Error ModelComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
- {
- if(!isEnabled()) [[unlikely]]
- {
- updated = false;
- return Error::kNone;
- }
- const Bool resourceUpdated = m_dirty;
- m_dirty = false;
- const Bool moved = info.m_node->movedThisFrame() || m_firstTimeUpdate;
- const Bool movedLastFrame = m_movedLastFrame || m_firstTimeUpdate;
- m_firstTimeUpdate = false;
- m_movedLastFrame = moved;
- updated = resourceUpdated || moved || movedLastFrame;
- // Upload mesh LODs and uniforms
- if(resourceUpdated) [[unlikely]]
- {
- // Upload the mesh views
- const U32 modelPatchCount = m_model->getModelPatches().getSize();
- for(U32 i = 0; i < modelPatchCount; ++i)
- {
- const ModelPatch& patch = m_model->getModelPatches()[i];
- const MeshResource& mesh = *patch.getMesh();
- Array<GpuSceneMeshLod, kMaxLodCount> meshLods;
- for(U32 l = 0; l < mesh.getLodCount(); ++l)
- {
- GpuSceneMeshLod& meshLod = meshLods[l];
- meshLod = {};
- meshLod.m_positionScale = mesh.getPositionsScale();
- meshLod.m_positionTranslation = mesh.getPositionsTranslation();
- for(VertexStreamId stream = VertexStreamId::kPosition; stream <= VertexStreamId::kBoneWeights; ++stream)
- {
- if(!mesh.isVertexStreamPresent(stream))
- {
- continue;
- }
- PtrSize offset;
- U32 vertCount;
- mesh.getVertexStreamInfo(l, stream, offset, vertCount);
- const PtrSize elementSize = getFormatInfo(kMeshRelatedVertexStreamFormats[stream]).m_texelSize;
- ANKI_ASSERT((offset % elementSize) == 0);
- meshLod.m_vertexOffsets[U32(stream)] = U32(offset / elementSize);
- }
- U32 firstIndex;
- U32 indexCount;
- Aabb aabb;
- mesh.getSubMeshInfo(l, i, firstIndex, indexCount, aabb);
- PtrSize offset;
- IndexType indexType;
- mesh.getIndexBufferInfo(l, offset, indexCount, indexType);
- meshLod.m_indexBufferOffset = U32(offset) / getIndexSize(indexType) + firstIndex;
- meshLod.m_indexCount = indexCount;
- }
- // Copy the last LOD to the rest just in case
- for(U32 l = mesh.getLodCount(); l < kMaxLodCount; ++l)
- {
- meshLods[l] = meshLods[l - 1];
- }
- const PtrSize offset = m_patchInfos[i].m_gpuSceneMeshLodsIndex * sizeof(meshLods)
- + info.m_node->getSceneGraph().getAllGpuSceneContiguousArrays().getArrayBase(
- GpuSceneContiguousArrayType::kMeshLods);
- GpuSceneMicroPatcher::getSingleton().newCopy(*info.m_framePool, offset, meshLods.getSizeInBytes(),
- &meshLods[0]);
- }
- // Upload the uniforms
- DynamicArrayRaii<U32> allUniforms(info.m_framePool, U32(m_gpuSceneUniforms.m_size / 4));
- U32 count = 0;
- for(U32 i = 0; i < modelPatchCount; ++i)
- {
- const ModelPatch& patch = m_model->getModelPatches()[i];
- const MaterialResource& mtl = *patch.getMaterial();
- memcpy(&allUniforms[count], mtl.getPrefilledLocalUniforms().getBegin(),
- mtl.getPrefilledLocalUniforms().getSizeInBytes());
- count += U32(mtl.getPrefilledLocalUniforms().getSizeInBytes() / 4);
- }
- ANKI_ASSERT(count * 4 == m_gpuSceneUniforms.m_size);
- GpuSceneMicroPatcher::getSingleton().newCopy(*info.m_framePool, m_gpuSceneUniforms.m_offset,
- m_gpuSceneUniforms.m_size, &allUniforms[0]);
- }
- // Upload transforms
- if(moved || movedLastFrame) [[unlikely]]
- {
- Array<Mat3x4, 2> trfs;
- trfs[0] = Mat3x4(info.m_node->getWorldTransform());
- trfs[1] = Mat3x4(info.m_node->getPreviousWorldTransform());
- const PtrSize offset = m_gpuSceneTransformsIndex * sizeof(trfs)
- + info.m_node->getSceneGraph().getAllGpuSceneContiguousArrays().getArrayBase(
- GpuSceneContiguousArrayType::kTransformPairs);
- GpuSceneMicroPatcher::getSingleton().newCopy(*info.m_framePool, offset, sizeof(trfs), &trfs[0]);
- }
- // Spatial update
- if(moved || resourceUpdated || m_skinComponent) [[unlikely]]
- {
- Aabb aabbLocal;
- if(m_skinComponent == nullptr) [[likely]]
- {
- aabbLocal = m_model->getBoundingVolume();
- }
- else
- {
- aabbLocal =
- m_skinComponent->getBoneBoundingVolumeLocalSpace().getCompoundShape(m_model->getBoundingVolume());
- }
- const Aabb aabbWorld = aabbLocal.getTransformed(info.m_node->getWorldTransform());
- m_spatial.setBoundingShape(aabbWorld);
- }
- const Bool spatialUpdated = m_spatial.update(info.m_node->getSceneGraph().getOctree());
- updated = updated || spatialUpdated;
- return Error::kNone;
- }
- void ModelComponent::setupRenderableQueueElements(U32 lod, RenderingTechnique technique, StackMemoryPool& tmpPool,
- WeakArray<RenderableQueueElement>& outRenderables) const
- {
- ANKI_ASSERT(isEnabled());
- outRenderables.setArray(nullptr, 0);
- const RenderingTechniqueBit requestedRenderingTechniqueMask = RenderingTechniqueBit(1 << technique);
- if(!(m_presentRenderingTechniques & requestedRenderingTechniqueMask))
- {
- return;
- }
- // Allocate renderables
- U32 renderableCount = 0;
- for(U32 i = 0; i < m_patchInfos.getSize(); ++i)
- {
- renderableCount += !!(m_patchInfos[i].m_techniques & requestedRenderingTechniqueMask);
- }
- if(renderableCount == 0)
- {
- return;
- }
- RenderableQueueElement* renderables = static_cast<RenderableQueueElement*>(
- tmpPool.allocate(sizeof(RenderableQueueElement) * renderableCount, alignof(RenderableQueueElement)));
- outRenderables.setArray(renderables, renderableCount);
- // Fill renderables
- const Bool moved = m_node->movedThisFrame() && technique == RenderingTechnique::kGBuffer;
- const Bool hasSkin = m_skinComponent != nullptr && m_skinComponent->isEnabled();
- RenderingKey key;
- key.setLod(lod);
- key.setRenderingTechnique(technique);
- key.setVelocity(moved);
- key.setSkinned(hasSkin);
- renderableCount = 0;
- for(U32 i = 0; i < m_patchInfos.getSize(); ++i)
- {
- if(!(m_patchInfos[i].m_techniques & requestedRenderingTechniqueMask))
- {
- continue;
- }
- RenderableQueueElement& queueElem = renderables[renderableCount];
- const ModelPatch& patch = m_model->getModelPatches()[i];
- ModelRenderingInfo modelInf;
- patch.getRenderingInfo(key, modelInf);
- AllGpuSceneContiguousArrays& gpuArrays = m_node->getSceneGraph().getAllGpuSceneContiguousArrays();
- queueElem.m_program = modelInf.m_program.get();
- queueElem.m_worldTransformsOffset = U32(m_gpuSceneTransformsIndex * sizeof(Mat3x4) * 2
- + gpuArrays.getArrayBase(GpuSceneContiguousArrayType::kTransformPairs));
- queueElem.m_uniformsOffset = m_patchInfos[i].m_gpuSceneUniformsOffset;
- queueElem.m_geometryOffset =
- U32(m_patchInfos[i].m_gpuSceneMeshLodsIndex * sizeof(GpuSceneMeshLod) * kMaxLodCount
- + lod * sizeof(GpuSceneMeshLod));
- queueElem.m_geometryOffset += U32(gpuArrays.getArrayBase(GpuSceneContiguousArrayType::kMeshLods));
- queueElem.m_boneTransformsOffset = (hasSkin) ? m_skinComponent->getBoneTransformsGpuSceneOffset() : 0;
- queueElem.m_indexCount = modelInf.m_indexCount;
- queueElem.m_firstIndex = U32(modelInf.m_indexBufferOffset / 2 + modelInf.m_firstIndex);
- queueElem.m_indexed = true;
- queueElem.m_primitiveTopology = PrimitiveTopology::kTriangles;
- queueElem.m_aabbMin = m_spatial.getAabbWorldSpace().getMin().xyz();
- queueElem.m_aabbMax = m_spatial.getAabbWorldSpace().getMax().xyz();
- queueElem.computeMergeKey();
- ++renderableCount;
- }
- }
- void ModelComponent::setupRayTracingInstanceQueueElements(U32 lod, RenderingTechnique technique,
- StackMemoryPool& tmpPool,
- WeakArray<RayTracingInstanceQueueElement>& outInstances) const
- {
- ANKI_ASSERT(isEnabled());
- outInstances.setArray(nullptr, 0);
- const RenderingTechniqueBit requestedRenderingTechniqueMask = RenderingTechniqueBit(1 << technique);
- if(!(m_presentRenderingTechniques & requestedRenderingTechniqueMask))
- {
- return;
- }
- // Allocate instances
- U32 instanceCount = 0;
- for(U32 i = 0; i < m_patchInfos.getSize(); ++i)
- {
- instanceCount += !!(m_patchInfos[i].m_techniques & requestedRenderingTechniqueMask);
- }
- if(instanceCount == 0)
- {
- return;
- }
- RayTracingInstanceQueueElement* instances = static_cast<RayTracingInstanceQueueElement*>(tmpPool.allocate(
- sizeof(RayTracingInstanceQueueElement) * instanceCount, alignof(RayTracingInstanceQueueElement)));
- outInstances.setArray(instances, instanceCount);
- RenderingKey key;
- key.setLod(lod);
- key.setRenderingTechnique(technique);
- instanceCount = 0;
- for(U32 i = 0; i < m_patchInfos.getSize(); ++i)
- {
- if(!(m_patchInfos[i].m_techniques & requestedRenderingTechniqueMask))
- {
- continue;
- }
- RayTracingInstanceQueueElement& queueElem = instances[instanceCount];
- const ModelPatch& patch = m_model->getModelPatches()[i];
- AllGpuSceneContiguousArrays& gpuArrays = m_node->getSceneGraph().getAllGpuSceneContiguousArrays();
- ModelRayTracingInfo modelInf;
- patch.getRayTracingInfo(key, modelInf);
- queueElem.m_bottomLevelAccelerationStructure = modelInf.m_bottomLevelAccelerationStructure.get();
- queueElem.m_shaderGroupHandleIndex = modelInf.m_shaderGroupHandleIndex;
- queueElem.m_worldTransformsOffset = U32(m_gpuSceneTransformsIndex * sizeof(Mat3x4) * 2
- + gpuArrays.getArrayBase(GpuSceneContiguousArrayType::kTransformPairs));
- queueElem.m_uniformsOffset = m_patchInfos[i].m_gpuSceneUniformsOffset;
- queueElem.m_geometryOffset =
- U32(m_patchInfos[i].m_gpuSceneMeshLodsIndex * sizeof(GpuSceneMeshLod) * kMaxLodCount
- + lod * sizeof(GpuSceneMeshLod));
- queueElem.m_geometryOffset += U32(gpuArrays.getArrayBase(GpuSceneContiguousArrayType::kMeshLods));
- queueElem.m_indexBufferOffset = U32(modelInf.m_indexBufferOffset);
- const Transform positionTransform(patch.getMesh()->getPositionsTranslation().xyz0(), Mat3x4::getIdentity(),
- patch.getMesh()->getPositionsScale());
- queueElem.m_transform = Mat3x4(m_node->getWorldTransform()).combineTransformations(Mat3x4(positionTransform));
- ++instanceCount;
- }
- }
- void ModelComponent::onOtherComponentRemovedOrAdded(SceneComponent* other, Bool added)
- {
- ANKI_ASSERT(other);
- if(other->getClassId() != SkinComponent::getStaticClassId())
- {
- return;
- }
- const Bool alreadyHasSkinComponent = m_skinComponent != nullptr;
- if(added && !alreadyHasSkinComponent)
- {
- m_skinComponent = static_cast<SkinComponent*>(other);
- m_dirty = true;
- }
- else if(!added && other == m_skinComponent)
- {
- m_skinComponent = nullptr;
- m_dirty = true;
- }
- }
- } // end namespace anki
|