diff --git a/filament/include/filament/InstanceBuffer.h b/filament/include/filament/InstanceBuffer.h index 4493dab61c..2a5742b724 100644 --- a/filament/include/filament/InstanceBuffer.h +++ b/filament/include/filament/InstanceBuffer.h @@ -45,7 +45,7 @@ public: public: /** - * @param instanceCount the number of instances this InstanceBuffer will support, must be + * @param instanceCount The number of instances this InstanceBuffer will support, must be * >= 1 and <= \c Engine::getMaxAutomaticInstances() * @see Engine::getMaxAutomaticInstances */ diff --git a/filament/src/RenderPass.cpp b/filament/src/RenderPass.cpp index 2efbfaccd7..942259a4ec 100644 --- a/filament/src/RenderPass.cpp +++ b/filament/src/RenderPass.cpp @@ -671,7 +671,7 @@ RenderPass::Command* RenderPass::generateCommandsImpl(CommandTypeFlags extraFlag cmd.key |= makeField(soaVisibility[i].priority, PRIORITY_MASK, PRIORITY_SHIFT); cmd.key |= makeField(soaVisibility[i].channel, CHANNEL_MASK, CHANNEL_SHIFT); cmd.info.index = i; - cmd.info.hasHybridInstancing = bool(soaInstanceInfo[i].handle); + cmd.info.hasHybridInstancing = bool(soaInstanceInfo[i].buffer); cmd.info.instanceCount = soaInstanceInfo[i].count; cmd.info.hasMorphing = bool(morphing.handle); cmd.info.hasSkinning = bool(skinning.handle); diff --git a/filament/src/components/RenderableManager.cpp b/filament/src/components/RenderableManager.cpp index e6300b5a11..28e1d2edaf 100644 --- a/filament/src/components/RenderableManager.cpp +++ b/filament/src/components/RenderableManager.cpp @@ -585,17 +585,6 @@ void FRenderableManager::create( InstancesInfo& instances = manager[ci].instances; instances.count = builder->mInstanceCount; instances.buffer = builder->mInstanceBuffer; - if (instances.buffer) { - // Allocate our instance buffer for this Renderable. We always allocate a size to match - // PerRenderableUib, regardless of the number of instances. This is because the buffer - // will get bound to the PER_RENDERABLE UBO, and we can't bind a buffer smaller than the - // full size of the UBO. - instances.handle = driver.createBufferObject(sizeof(PerRenderableUib), - BufferObjectBinding::UNIFORM, BufferUsage::DYNAMIC); - if (auto name = instances.buffer->getName(); !name.empty()) { - driver.setDebugTag(instances.handle.getId(), std::move(name)); - } - } const uint32_t boneCount = builder->mSkinningBoneCount; const uint32_t targetCount = builder->mMorphTargetCount; @@ -774,11 +763,6 @@ void FRenderableManager::destroyComponent(Instance const ci) noexcept { if (morphWeights.handle) { driver.destroyBufferObject(morphWeights.handle); } - - InstancesInfo const& instances = manager[ci].instances; - if (instances.handle) { - driver.destroyBufferObject(instances.handle); - } } void FRenderableManager::destroyComponentPrimitives( diff --git a/filament/src/components/RenderableManager.h b/filament/src/components/RenderableManager.h index 45846bba77..3ea94dd3af 100644 --- a/filament/src/components/RenderableManager.h +++ b/filament/src/components/RenderableManager.h @@ -165,7 +165,7 @@ public: inline Box const& getAABB(Instance instance) const noexcept; - inline Box const& getAxisAlignedBoundingBox(Instance const instance) const noexcept { return getAABB(instance); } + Box const& getAxisAlignedBoundingBox(Instance const instance) const noexcept { return getAABB(instance); } inline Visibility getVisibility(Instance instance) const noexcept; inline uint8_t getLayerMask(Instance instance) const noexcept; inline uint8_t getPriority(Instance instance) const noexcept; @@ -189,18 +189,15 @@ public: inline MorphingBindingInfo getMorphingBufferInfo(Instance instance) const noexcept; struct InstancesInfo { - union { - FInstanceBuffer* buffer; - uint64_t padding; // ensures the pointer is 64 bits on all archs - }; - backend::Handle handle; - uint16_t count; - char padding0[2]; + FInstanceBuffer* buffer = nullptr; + alignas(8) // ensures the pointer is 64 bits on all archs + uint16_t count = 0; + char padding0[6] = {}; }; static_assert(sizeof(InstancesInfo) == 16); inline InstancesInfo getInstancesInfo(Instance instance) const noexcept; - inline size_t getLevelCount(Instance) const noexcept { return 1u; } + size_t getLevelCount(Instance) const noexcept { return 1u; } size_t getPrimitiveCount(Instance instance, uint8_t level) const noexcept; void setMaterialInstanceAt(Instance instance, uint8_t level, size_t primitiveIndex, FMaterialInstance const* materialInstance); diff --git a/filament/src/details/InstanceBuffer.cpp b/filament/src/details/InstanceBuffer.cpp index d4f2157e48..f70bc7e7a6 100644 --- a/filament/src/details/InstanceBuffer.cpp +++ b/filament/src/details/InstanceBuffer.cpp @@ -87,7 +87,7 @@ InstanceBuffer* InstanceBuffer::Builder::build(Engine& engine) const { // ------------------------------------------------------------------------------------------------ -FInstanceBuffer::FInstanceBuffer(FEngine&, const Builder& builder) +FInstanceBuffer::FInstanceBuffer(FEngine& engine, const Builder& builder) : mName(builder.getName()) { mInstanceCount = builder->mInstanceCount; @@ -98,8 +98,26 @@ FInstanceBuffer::FInstanceBuffer(FEngine&, const Builder& builder) memcpy(mLocalTransforms.data(), builder->mLocalTransforms, sizeof(math::mat4f) * mInstanceCount); } + + // Allocate our instance buffer. We always allocate a size to match + // PerRenderableUib, regardless of the number of instances. This is because the buffer + // will get bound to the PER_RENDERABLE UBO, and we can't bind a buffer smaller than the + // full size of the UBO. + DriverApi& driver = engine.getDriverApi(); + mHandle = driver.createBufferObject(sizeof(PerRenderableUib), + BufferObjectBinding::UNIFORM, BufferUsage::DYNAMIC); + if (auto name = mName; !name.empty()) { + driver.setDebugTag(mHandle.getId(), std::move(name)); + } } +void FInstanceBuffer::terminate(FEngine& engine) { + DriverApi& driver = engine.getDriverApi(); + driver.destroyBufferObject(std::move(mHandle)); +} + +FInstanceBuffer::~FInstanceBuffer() noexcept = default; + void FInstanceBuffer::setLocalTransforms( math::mat4f const* localTransforms, size_t const count, size_t const offset) { FILAMENT_CHECK_PRECONDITION(offset + count <= mInstanceCount) @@ -110,7 +128,7 @@ void FInstanceBuffer::setLocalTransforms( } void FInstanceBuffer::prepare(FEngine& engine, math::mat4f const& rootTransform, - const PerRenderableData& ubo, Handle handle) { + const PerRenderableData& ubo) { DriverApi& driver = engine.getDriverApi(); // TODO: allocate this staging buffer from a pool. @@ -125,7 +143,7 @@ void FInstanceBuffer::prepare(FEngine& engine, math::mat4f const& rootTransform, math::mat3f const m = math::mat3f::getTransformForNormals(model.upperLeft()); stagingBuffer[i].worldFromModelNormalMatrix = math::prescaleForNormals(m); } - driver.updateBufferObject(handle, { + driver.updateBufferObject(mHandle, { stagingBuffer, stagingBufferSize, +[](void* buffer, size_t, void*) { free(buffer); @@ -133,8 +151,5 @@ void FInstanceBuffer::prepare(FEngine& engine, math::mat4f const& rootTransform, }, 0); } -void FInstanceBuffer::terminate(FEngine&) { -} - } // namespace filament diff --git a/filament/src/details/InstanceBuffer.h b/filament/src/details/InstanceBuffer.h index 131235ead2..eb346fcd51 100644 --- a/filament/src/details/InstanceBuffer.h +++ b/filament/src/details/InstanceBuffer.h @@ -40,24 +40,27 @@ struct PerRenderableData; class FInstanceBuffer : public InstanceBuffer { public: FInstanceBuffer(FEngine& engine, const Builder& builder); + ~FInstanceBuffer() noexcept; void terminate(FEngine& engine); - inline size_t getInstanceCount() const noexcept { return mInstanceCount; } + size_t getInstanceCount() const noexcept { return mInstanceCount; } void setLocalTransforms(math::mat4f const* localTransforms, size_t count, size_t offset); - void prepare(FEngine& engine, math::mat4f const& rootTransform, const PerRenderableData& ubo, - backend::Handle handle); + void prepare(FEngine& engine, math::mat4f const& rootTransform, const PerRenderableData& ubo); utils::CString const& getName() const noexcept { return mName; } + backend::BufferObjectHandle getHandle() const noexcept { return mHandle; } + private: friend class RenderableManager; utils::FixedCapacityVector mLocalTransforms; utils::CString mName; size_t mInstanceCount; + backend::BufferObjectHandle mHandle; }; FILAMENT_DOWNCAST(InstanceBuffer) diff --git a/filament/src/details/Scene.cpp b/filament/src/details/Scene.cpp index 4a7b82e63b..ff4a8f24e3 100644 --- a/filament/src/details/Scene.cpp +++ b/filament/src/details/Scene.cpp @@ -435,7 +435,7 @@ void FScene::updateUBOs( auto& instancesInfo = instancesData[i]; if (UTILS_UNLIKELY(instancesInfo.buffer)) { instancesInfo.buffer->prepare( - mEngine, worldTransformData[i], uboData[i], instancesInfo.handle); + mEngine, worldTransformData[i], uboData[i]); } } diff --git a/filament/src/details/View.cpp b/filament/src/details/View.cpp index 8d6b3c9074..31cd89c024 100644 --- a/filament/src/details/View.cpp +++ b/filament/src/details/View.cpp @@ -786,7 +786,7 @@ void FView::prepare(FEngine& engine, DriverApi& driver, RootArenaScope& rootAren // FIXME: when only one is active the UBO handle of the other is null // (probably a problem on vulkan) - if (UTILS_UNLIKELY(skinning.handle || morphing.handle || instance.handle)) { + if (UTILS_UNLIKELY(skinning.handle || morphing.handle || instance.buffer)) { auto const ci = sceneData.elementAt(i); FRenderableManager& rcm = engine.getRenderableManager(); auto& descriptorSet = rcm.getDescriptorSet(ci); @@ -800,7 +800,7 @@ void FView::prepare(FEngine& engine, DriverApi& driver, RootArenaScope& rootAren descriptorSet.setBuffer(layout, +PerRenderableBindingPoints::OBJECT_UNIFORMS, - instance.handle ? instance.handle : mRenderableUbh, + instance.buffer ? instance.buffer->getHandle() : mRenderableUbh, 0, sizeof(PerRenderableUib)); descriptorSet.setBuffer(layout,