move the management of the InstanceBuffer's UBO into InstanceBuffer

For some reason the UBO was managed by RenderableManager, but it's a
lot cleaner and easier to have this done by InstanceBuffer itself.
This commit is contained in:
Mathias Agopian
2025-07-25 00:36:05 -07:00
committed by Mathias Agopian
parent 2c60740edd
commit de6f7b56da
8 changed files with 38 additions and 39 deletions
+1 -1
View File
@@ -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
*/
+1 -1
View File
@@ -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);
@@ -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(
+6 -9
View File
@@ -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<backend::HwBufferObject> 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);
+21 -6
View File
@@ -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<HwBufferObject> 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
+6 -3
View File
@@ -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<backend::HwBufferObject> 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<math::mat4f> mLocalTransforms;
utils::CString mName;
size_t mInstanceCount;
backend::BufferObjectHandle mHandle;
};
FILAMENT_DOWNCAST(InstanceBuffer)
+1 -1
View File
@@ -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]);
}
}
+2 -2
View File
@@ -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<FScene::RENDERABLE_INSTANCE>(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,