use IntrusivePtr to share objects (#11674)

* use IntrusivePtr to share objects
This commit is contained in:
minggo
2022-06-24 09:33:52 +08:00
committed by GitHub
parent cd75a4b921
commit 69c2ff6a2a
2 changed files with 15 additions and 18 deletions
@@ -53,9 +53,7 @@ void GlobalDSManager::activate(gfx::Device *device) {
});
setDescriptorSetLayout();
CC_SAFE_DESTROY_NULL(_descriptorSetLayout)
_descriptorSetLayout = device->createDescriptorSetLayout({globalDescriptorSetLayout.bindings});
CC_SAFE_DESTROY_NULL(_globalDescriptorSet)
_globalDescriptorSet = device->createDescriptorSet({_descriptorSetLayout});
}
@@ -137,7 +135,7 @@ gfx::DescriptorSet *GlobalDSManager::getOrCreateDescriptorSet(uint32_t idx) {
UBOShadow::SIZE,
gfx::BufferFlagBit::NONE,
});
_shadowUBOs.push_back(shadowUBO);
_shadowUBOs.emplace_back(shadowUBO);
descriptorSet->bindBuffer(UBOShadow::BINDING, shadowUBO);
descriptorSet->update();
@@ -147,16 +145,12 @@ gfx::DescriptorSet *GlobalDSManager::getOrCreateDescriptorSet(uint32_t idx) {
}
void GlobalDSManager::destroy() {
for (auto *shadowUBO : _shadowUBOs) {
CC_SAFE_DELETE(shadowUBO)
}
for (auto &pair : _descriptorSetMap) {
CC_SAFE_DELETE(pair.second)
}
_shadowUBOs.clear();
_descriptorSetMap.clear();
CC_SAFE_DESTROY_NULL(_descriptorSetLayout)
CC_SAFE_DELETE(_globalDescriptorSet)
_descriptorSetLayout = nullptr;
_globalDescriptorSet = nullptr;
_linearSampler = nullptr;
_pointSampler = nullptr;
}
void GlobalDSManager::setDescriptorSetLayout() {
@@ -46,7 +46,6 @@ public:
GlobalDSManager() = default;
~GlobalDSManager() = default;
inline ccstd::unordered_map<uint32_t, gfx::DescriptorSet *> getDescriptorSetMap() const { return _descriptorSetMap; }
inline gfx::Sampler *getLinearSampler() const { return _linearSampler; }
inline gfx::Sampler *getPointSampler() const { return _pointSampler; }
inline gfx::DescriptorSetLayout *getDescriptorSetLayout() const { return _descriptorSetLayout; }
@@ -63,13 +62,17 @@ public:
static void setDescriptorSetLayout();
private:
// weak reference
gfx::Device *_device = nullptr;
gfx::Sampler *_linearSampler = nullptr;
gfx::Sampler *_pointSampler = nullptr;
// Samplers are hold by device
gfx::Sampler *_linearSampler{nullptr};
gfx::Sampler *_pointSampler{nullptr};
IntrusivePtr<gfx::DescriptorSetLayout> _descriptorSetLayout;
gfx::DescriptorSet *_globalDescriptorSet = nullptr;
ccstd::unordered_map<uint32_t, gfx::DescriptorSet *> _descriptorSetMap{};
ccstd::vector<gfx::Buffer *> _shadowUBOs;
IntrusivePtr<gfx::DescriptorSet> _globalDescriptorSet;
ccstd::vector<IntrusivePtr<gfx::Buffer>> _shadowUBOs;
ccstd::unordered_map<uint32_t, IntrusivePtr<gfx::DescriptorSet>> _descriptorSetMap;
};
} // namespace pipeline