diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index c5b9338842..ec5b82e601 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -842,6 +842,8 @@ cocos_source_files( cocos/renderer/pipeline/BatchedBuffer.h cocos/renderer/pipeline/Define.h cocos/renderer/pipeline/Define.cpp + cocos/renderer/pipeline/GlobalDescriptorSetManager.h + cocos/renderer/pipeline/GlobalDescriptorSetManager.cpp cocos/renderer/pipeline/InstancedBuffer.cpp cocos/renderer/pipeline/InstancedBuffer.h cocos/renderer/pipeline/PipelineStateManager.cpp diff --git a/native/cocos/physics/physx/PhysXSharedBody.cpp b/native/cocos/physics/physx/PhysXSharedBody.cpp index 1981350e0b..950d5c6002 100644 --- a/native/cocos/physics/physx/PhysXSharedBody.cpp +++ b/native/cocos/physics/physx/PhysXSharedBody.cpp @@ -99,7 +99,12 @@ void PhysXSharedBody::setType(ERigidBodyType v) { if (_mType == v) return; _mType = v; initActor(); - _mImpl.ptr = isStatic() ? reinterpret_cast(_mStaticActor) : reinterpret_cast(_mDynamicActor); + if (isStatic()) { + _mImpl.ptr = reinterpret_cast(_mStaticActor); + } else { + _mImpl.ptr = reinterpret_cast(_mDynamicActor); + _mImpl.rigidDynamic->setRigidBodyFlag(physx::PxRigidBodyFlag::eKINEMATIC, _mType == ERigidBodyType::KINEMATIC); + } } void PhysXSharedBody::reference(bool v) { @@ -213,14 +218,15 @@ void PhysXSharedBody::syncScale() { } void PhysXSharedBody::syncSceneToPhysics() { - if (getNode().getFlagsChanged()) { - if (getNode().getFlagsChanged() & static_cast(TransformBit::SCALE)) syncScale(); + uint32_t hasChangedFlags = getNode().getFlagsChanged(); + if (hasChangedFlags) { + if (hasChangedFlags & static_cast(TransformBit::SCALE)) syncScale(); auto wp = getImpl().rigidActor->getGlobalPose(); - if (getNode().getFlagsChanged() & static_cast(TransformBit::POSITION)) { - pxSetVec3Ext(wp.p, getNode().getWorldPosition()); + if (hasChangedFlags & static_cast(TransformBit::POSITION)) { + pxSetVec3Ext(wp.p, getNode().worldPosition); } - if (getNode().getFlagsChanged() & static_cast(TransformBit::ROTATION)) { - pxSetQuatExt(wp.q, getNode().getWorldRotation()); + if (hasChangedFlags & static_cast(TransformBit::ROTATION)) { + pxSetQuatExt(wp.q, getNode().worldRotation); } if (isKinematic()) { diff --git a/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.cpp b/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.cpp new file mode 100644 index 0000000000..b0556806a1 --- /dev/null +++ b/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.cpp @@ -0,0 +1,209 @@ +/**************************************************************************** + Copyright (c) 2020-2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + + +#include "GlobalDescriptorSetManager.h" + +#include "Define.h" +#include "RenderInstancedQueue.h" +#include "forward/ForwardPipeline.h" +#include "gfx-base/GFXBuffer.h" +#include "gfx-base/GFXDescriptorSet.h" +#include "gfx-base/GFXDevice.h" +#include "gfx-base/GFXSampler.h" +#include "gfx-base/GFXTexture.h" +#include "helper/SharedMemory.h" +#include "gfx-base/GFXDescriptorSetLayout.h" + + +namespace cc { +namespace pipeline { + +#define INIT_GLOBAL_DESCSET_LAYOUT(info) \ + do { \ + globalDescriptorSetLayout.samplers[info::NAME] = info::LAYOUT; \ + globalDescriptorSetLayout.bindings[info::BINDING] = info::DESCRIPTOR; \ + } while (0) + +void GlobalDSManager::activate(gfx::Device* device, RenderPipeline* pipeline) { + _device = device; + _pipeline = pipeline; + + const gfx::SamplerInfo info{ + gfx::Filter::LINEAR, + gfx::Filter::LINEAR, + gfx::Filter::NONE, + gfx::Address::CLAMP, + gfx::Address::CLAMP, + gfx::Address::CLAMP, + {}, + {}, + {}, + {}, + }; + const auto shadowMapSamplerHash = SamplerLib::genSamplerHash(info); + _sampler = SamplerLib::getSampler(shadowMapSamplerHash); + + setDescriptorSetLayout(); + if (_descriptorSetLayout) { + _descriptorSetLayout->destroy(); + CC_DELETE(_descriptorSetLayout); + } + _descriptorSetLayout = device->createDescriptorSetLayout({globalDescriptorSetLayout.bindings}); + + if (_globalDescriptorSet) { + _globalDescriptorSet->destroy(); + CC_DELETE(_globalDescriptorSet); + } + _globalDescriptorSet = device->createDescriptorSet({_descriptorSetLayout}); +} + +void GlobalDSManager::bindBuffer(uint binding, gfx::Buffer* buffer) { + if (_globalDescriptorSet) { + _globalDescriptorSet->bindBuffer(binding, buffer); + } + + for (auto &pair : _descriptorSetMap) { + if (pair.second) pair.second->bindBuffer(binding, buffer); + } +} + +void GlobalDSManager::bindSampler(uint binding, gfx::Sampler* sampler) { + if (_globalDescriptorSet) { + _globalDescriptorSet->bindSampler(binding, sampler); + } + + for (auto &pair : _descriptorSetMap) { + if (pair.second) pair.second->bindSampler(binding, sampler); + } +} + +void GlobalDSManager::bindTexture(uint binding, gfx::Texture* texture) { + if (_globalDescriptorSet) { + _globalDescriptorSet->bindTexture(binding, texture); + } + + for (auto &pair : _descriptorSetMap) { + if (pair.second) pair.second->bindTexture(binding, texture); + } +} + +void GlobalDSManager::update() { + if (_globalDescriptorSet) { + _globalDescriptorSet->update(); + } + + for (auto &pair : _descriptorSetMap) { + if (pair.second) pair.second->update(); + } +} + +gfx::DescriptorSet *GlobalDSManager::getOrCreateDescriptorSet(uint idx) { + // The global descriptorSet is managed by the pipeline and binds the buffer + if (_descriptorSetMap.count(idx) <= 0 || !_descriptorSetMap.at(idx)) { + auto *descriptorSet = _device->createDescriptorSet({_descriptorSetLayout}); + _descriptorSetMap.emplace(idx, descriptorSet); + + const auto begin = static_cast(PipelineGlobalBindings::UBO_GLOBAL); + const auto end = static_cast(PipelineGlobalBindings::COUNT); + for (uint i = begin; i < end; ++i) { + descriptorSet->bindBuffer(i, _globalDescriptorSet->getBuffer(i)); + descriptorSet->bindSampler(i, _globalDescriptorSet->getSampler(i)); + descriptorSet->bindTexture(i, _globalDescriptorSet->getTexture(i)); + } + + auto *shadowUBO = _device->createBuffer({ + gfx::BufferUsageBit::UNIFORM | gfx::BufferUsageBit::TRANSFER_DST, + gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE, + UBOShadow::SIZE, + UBOShadow::SIZE, + gfx::BufferFlagBit::NONE, + }); + descriptorSet->bindBuffer(UBOShadow::BINDING, shadowUBO); + + descriptorSet->update(); + } + + return _descriptorSetMap.at(idx); +} + +void GlobalDSManager::destroy() { + CC_SAFE_DESTROY(_descriptorSetLayout); + CC_SAFE_DESTROY(_sampler); +} + +void GlobalDSManager::setDescriptorSetLayout() { + globalDescriptorSetLayout.bindings.resize(static_cast(PipelineGlobalBindings::COUNT)); + + globalDescriptorSetLayout.blocks[UBOGlobal::NAME] = UBOGlobal::LAYOUT; + globalDescriptorSetLayout.bindings[UBOGlobal::BINDING] = UBOGlobal::DESCRIPTOR; + globalDescriptorSetLayout.blocks[UBOCamera::NAME] = UBOCamera::LAYOUT; + globalDescriptorSetLayout.bindings[UBOCamera::BINDING] = UBOCamera::DESCRIPTOR; + globalDescriptorSetLayout.blocks[UBOShadow::NAME] = UBOShadow::LAYOUT; + globalDescriptorSetLayout.bindings[UBOShadow::BINDING] = UBOShadow::DESCRIPTOR; + globalDescriptorSetLayout.samplers[SHADOWMAP::NAME] = SHADOWMAP::LAYOUT; + globalDescriptorSetLayout.bindings[SHADOWMAP::BINDING] = SHADOWMAP::DESCRIPTOR; + globalDescriptorSetLayout.samplers[ENVIRONMENT::NAME] = ENVIRONMENT::LAYOUT; + globalDescriptorSetLayout.bindings[ENVIRONMENT::BINDING] = ENVIRONMENT::DESCRIPTOR; + globalDescriptorSetLayout.samplers[SPOTLIGHTINGMAP::NAME] = SPOTLIGHTINGMAP::LAYOUT; + globalDescriptorSetLayout.bindings[SPOTLIGHTINGMAP::BINDING] = SPOTLIGHTINGMAP::DESCRIPTOR; + + INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERGBUFFERALBEDOMAP); + INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERGBUFFERPOSITIONMAP); + INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERGBUFFEREMISSIVEMAP); + INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERGBUFFERNORMALMAP); + INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERLIGHTINGRESULTMAP); + + localDescriptorSetLayout.bindings.resize(static_cast(ModelLocalBindings::COUNT)); + localDescriptorSetLayout.blocks[UBOLocalBatched::NAME] = UBOLocalBatched::LAYOUT; + localDescriptorSetLayout.bindings[UBOLocalBatched::BINDING] = UBOLocalBatched::DESCRIPTOR; + localDescriptorSetLayout.blocks[UBOLocal::NAME] = UBOLocal::LAYOUT; + localDescriptorSetLayout.bindings[UBOLocal::BINDING] = UBOLocal::DESCRIPTOR; + localDescriptorSetLayout.blocks[UBOForwardLight::NAME] = UBOForwardLight::LAYOUT; + localDescriptorSetLayout.bindings[UBOForwardLight::BINDING] = UBOForwardLight::DESCRIPTOR; + localDescriptorSetLayout.blocks[UBOSkinningTexture::NAME] = UBOSkinningTexture::LAYOUT; + localDescriptorSetLayout.bindings[UBOSkinningTexture::BINDING] = UBOSkinningTexture::DESCRIPTOR; + localDescriptorSetLayout.blocks[UBOSkinningAnimation::NAME] = UBOSkinningAnimation::LAYOUT; + localDescriptorSetLayout.bindings[UBOSkinningAnimation::BINDING] = UBOSkinningAnimation::DESCRIPTOR; + localDescriptorSetLayout.blocks[UBOSkinning::NAME] = UBOSkinning::LAYOUT; + localDescriptorSetLayout.bindings[UBOSkinning::BINDING] = UBOSkinning::DESCRIPTOR; + localDescriptorSetLayout.blocks[UBOMorph::NAME] = UBOMorph::LAYOUT; + localDescriptorSetLayout.bindings[UBOMorph::BINDING] = UBOMorph::DESCRIPTOR; + localDescriptorSetLayout.samplers[JOINTTEXTURE::NAME] = JOINTTEXTURE::LAYOUT; + localDescriptorSetLayout.bindings[JOINTTEXTURE::BINDING] = JOINTTEXTURE::DESCRIPTOR; + localDescriptorSetLayout.samplers[POSITIONMORPH::NAME] = POSITIONMORPH::LAYOUT; + localDescriptorSetLayout.bindings[POSITIONMORPH::BINDING] = POSITIONMORPH::DESCRIPTOR; + localDescriptorSetLayout.samplers[NORMALMORPH::NAME] = NORMALMORPH::LAYOUT; + localDescriptorSetLayout.bindings[NORMALMORPH::BINDING] = NORMALMORPH::DESCRIPTOR; + localDescriptorSetLayout.samplers[TANGENTMORPH::NAME] = TANGENTMORPH::LAYOUT; + localDescriptorSetLayout.bindings[TANGENTMORPH::BINDING] = TANGENTMORPH::DESCRIPTOR; + localDescriptorSetLayout.samplers[LIGHTMAPTEXTURE::NAME] = LIGHTMAPTEXTURE::LAYOUT; + localDescriptorSetLayout.bindings[LIGHTMAPTEXTURE::BINDING] = LIGHTMAPTEXTURE::DESCRIPTOR; + localDescriptorSetLayout.samplers[SPRITETEXTURE::NAME] = SPRITETEXTURE::LAYOUT; + localDescriptorSetLayout.bindings[SPRITETEXTURE::BINDING] = SPRITETEXTURE::DESCRIPTOR; +} + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.h b/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.h new file mode 100644 index 0000000000..b1f63692fb --- /dev/null +++ b/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.h @@ -0,0 +1,74 @@ +/**************************************************************************** + Copyright (c) 2020-2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "base/CoreStd.h" +#include "helper/SharedMemory.h" + +namespace cc { +namespace gfx { +class DescriptorSet; +class DescriptorSetLayout; +class Sampler; +class Buffer; +class Texture; +class Device; +} // namespace gfx +namespace pipeline { +class RenderPipeline; + +class GlobalDSManager : public Object { +public: + GlobalDSManager() = default; + ~GlobalDSManager() override = default; + + CC_INLINE std::unordered_map getDescriptorSetMap() const { return _descriptorSetMap; } + CC_INLINE gfx::Sampler *getShadowMapSampler() const { return _sampler; } + CC_INLINE gfx::DescriptorSetLayout *getDescriptorSetLayout() const { return _descriptorSetLayout; } + CC_INLINE gfx::DescriptorSet *getGlobalDescriptorSet() const { return _globalDescriptorSet; } + + void activate(gfx::Device *device, RenderPipeline *pipeline); + void bindBuffer(uint binding, gfx::Buffer *buffer); + void bindTexture(uint binding, gfx::Texture *texture); + void bindSampler(uint binding, gfx::Sampler *sampler); + void update(); + gfx::DescriptorSet *getOrCreateDescriptorSet(uint idx); + void destroy(); + +protected: + static void setDescriptorSetLayout(); + +private: + RenderPipeline * _pipeline = nullptr; + gfx::Device * _device = nullptr; + gfx::Sampler * _sampler = nullptr; + gfx::DescriptorSetLayout * _descriptorSetLayout = nullptr; + gfx::DescriptorSet * _globalDescriptorSet = nullptr; + std::unordered_map _descriptorSetMap{}; +}; + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/PipelineUBO.cpp b/native/cocos/renderer/pipeline/PipelineUBO.cpp index dad195e87f..d6f82afad4 100644 --- a/native/cocos/renderer/pipeline/PipelineUBO.cpp +++ b/native/cocos/renderer/pipeline/PipelineUBO.cpp @@ -359,18 +359,26 @@ void PipelineUBO::destroy() { } void PipelineUBO::updateGlobalUBO() { - auto *const ds = _pipeline->getDescriptorSet(); - auto *const cmdBuffer = _pipeline->getCommandBuffers()[0]; + auto *const globalDSManager = _pipeline->getGlobalDSManager(); + auto *const ds = _pipeline->getDescriptorSet(); + auto *const cmdBuffer = _pipeline->getCommandBuffers()[0]; ds->update(); PipelineUBO::updateGlobalUBOView(_pipeline, &_globalUBO); cmdBuffer->updateBuffer(ds->getBuffer(UBOGlobal::BINDING), _globalUBO.data(), UBOGlobal::SIZE); + + globalDSManager->bindBuffer(UBOGlobal::BINDING, ds->getBuffer(UBOGlobal::BINDING)); + globalDSManager->update(); } void PipelineUBO::updateCameraUBO(const scene::Camera *camera) { - auto *const ds = _pipeline->getDescriptorSet(); - auto *const cmdBuffer = _pipeline->getCommandBuffers()[0]; - PipelineUBO::updateCameraUBOView(_pipeline, &_cameraUBO, camera); + auto *const globalDSManager = _pipeline->getGlobalDSManager(); + auto *const ds = _pipeline->getDescriptorSet(); + auto *const cmdBuffer = _pipeline->getCommandBuffers()[0]; + PipelineUBO::updateCameraUBOView(_pipeline, _cameraUBO, camera); cmdBuffer->updateBuffer(ds->getBuffer(UBOCamera::BINDING), _cameraUBO.data(), UBOCamera::SIZE); + + globalDSManager->bindBuffer(UBOCamera::BINDING, ds->getBuffer(UBOCamera::BINDING)); + globalDSManager->update(); } void PipelineUBO::updateShadowUBO(const scene::Camera *camera) { diff --git a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp index 032e641d27..8c3d7ba826 100644 --- a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp +++ b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp @@ -48,7 +48,7 @@ namespace cc { namespace pipeline { -RenderAdditiveLightQueue::RenderAdditiveLightQueue(RenderPipeline *pipeline) : _pipeline(dynamic_cast(pipeline)), +RenderAdditiveLightQueue::RenderAdditiveLightQueue(RenderPipeline *pipeline) : _pipeline(pipeline), _instancedQueue(CC_NEW(RenderInstancedQueue)), _batchedQueue(CC_NEW(RenderBatchedQueue)) { auto * device = gfx::Device::getInstance(); @@ -64,23 +64,7 @@ RenderAdditiveLightQueue::RenderAdditiveLightQueue(RenderPipeline *pipeline) : _ _firstLightBufferView = device->createBuffer({_lightBuffer, 0, UBOForwardLight::SIZE}); _lightBufferData.resize(_lightBufferElementCount * _lightBufferCount); _dynamicOffsets.resize(1, 0); - - const gfx::SamplerInfo info{ - gfx::Filter::LINEAR, - gfx::Filter::LINEAR, - gfx::Filter::NONE, - gfx::Address::CLAMP, - gfx::Address::CLAMP, - gfx::Address::CLAMP, - {}, - {}, - {}, - {}, - }; - const auto shadowMapSamplerHash = SamplerLib::genSamplerHash(info); - _sampler = SamplerLib::getSampler(shadowMapSamplerHash); _phaseID = getPhaseID("forward-add"); - _shadowUBO.fill(0.F); } @@ -110,8 +94,8 @@ void RenderAdditiveLightQueue::recordCommandBuffer(gfx::Device *device, gfx::Ren cmdBuffer->bindInputAssembler(ia); for (size_t i = 0; i < dynamicOffsets.size(); ++i) { - const auto *light = lights[i]; - auto * globalDescriptorSet = getOrCreateDescriptorSet(light); + const auto light = lights[i]; + auto * globalDescriptorSet = _pipeline->getGlobalDSManager()->getOrCreateDescriptorSet(light); _dynamicOffsets[0] = dynamicOffsets[i]; cmdBuffer->bindDescriptorSet(globalSet, globalDescriptorSet); cmdBuffer->bindDescriptorSet(localSet, descriptorSet, _dynamicOffsets); @@ -161,16 +145,18 @@ void RenderAdditiveLightQueue::gatherLightPasses(const scene::Camera *camera, gf } void RenderAdditiveLightQueue::destroy() { - for (auto &pair : _descriptorSetMap) { + for (auto &pair : _pipeline->getGlobalDSManager()->getDescriptorSetMap()) { auto *descriptorSet = pair.second; - descriptorSet->getBuffer(UBOShadow::BINDING)->destroy(); - descriptorSet->getSampler(SHADOWMAP::BINDING)->destroy(); - descriptorSet->getTexture(SHADOWMAP::BINDING)->destroy(); - descriptorSet->getSampler(SPOTLIGHTINGMAP::BINDING)->destroy(); - descriptorSet->getTexture(SPOTLIGHTINGMAP::BINDING)->destroy(); - descriptorSet->destroy(); + if (descriptorSet) { + descriptorSet->getBuffer(UBOShadow::BINDING)->destroy(); + descriptorSet->getSampler(SHADOWMAP::BINDING)->destroy(); + descriptorSet->getTexture(SHADOWMAP::BINDING)->destroy(); + descriptorSet->getSampler(SPOTLIGHTINGMAP::BINDING)->destroy(); + descriptorSet->getTexture(SPOTLIGHTINGMAP::BINDING)->destroy(); + descriptorSet->destroy(); + } } - _descriptorSetMap.clear(); + _pipeline->getGlobalDSManager()->getDescriptorSetMap().clear(); } void RenderAdditiveLightQueue::clear() { @@ -194,7 +180,6 @@ void RenderAdditiveLightQueue::gatherValidLights(const scene::Camera *camera) { sphere.setRadius(light->getRange()); if (sphere.sphereFrustum(camera->frustum)) { _validLights.emplace_back(static_cast(light)); - getOrCreateDescriptorSet(light); } } @@ -203,7 +188,6 @@ void RenderAdditiveLightQueue::gatherValidLights(const scene::Camera *camera) { sphere.setRadius(light->getRange()); if (sphere.sphereFrustum(camera->frustum)) { _validLights.emplace_back(static_cast(light)); - getOrCreateDescriptorSet(light); } } } @@ -241,7 +225,7 @@ void RenderAdditiveLightQueue::addRenderQueue(const scene::Pass *pass, const sce lightPass.dynamicOffsets.resize(count); for (unsigned idx = 0; idx < count; idx++) { const auto lightIdx = _lightIndices[idx]; - lightPass.lights.emplace_back(_validLights[lightIdx]); + lightPass.lights.emplace_back(lightIdx); lightPass.dynamicOffsets[idx] = _lightBufferStride * lightIdx; } @@ -332,8 +316,9 @@ void RenderAdditiveLightQueue::updateLightDescriptorSet(const scene::Camera *cam const auto packing = static_cast(shadowInfo->packing) ? 1.0F : (isTextureHalfFloat ? 0.0F : 1.0F); const scene::Light *mainLight = scene->getMainLight(); - for (const auto *light : _validLights) { - auto *descriptorSet = getOrCreateDescriptorSet(light); + for (uint i = 0; i < _validLights.size(); ++i) { + const auto * light = _validLights[i]; + auto *descriptorSet = _pipeline->getGlobalDSManager()->getOrCreateDescriptorSet(i); if (!descriptorSet) { continue; } @@ -445,40 +430,5 @@ void RenderAdditiveLightQueue::lightCulling(const scene::Model *model) { } } -gfx::DescriptorSet *RenderAdditiveLightQueue::getOrCreateDescriptorSet(const scene::Light *light) { - if (!_descriptorSetMap.count(light)) { - auto * device = gfx::Device::getInstance(); - gfx::DescriptorSet *descriptorSet = device->createDescriptorSet({_pipeline->getDescriptorSetLayout()}); - - descriptorSet->bindBuffer(UBOGlobal::BINDING, _pipeline->getDescriptorSet()->getBuffer(UBOGlobal::BINDING)); - - descriptorSet->bindBuffer(UBOCamera::BINDING, _pipeline->getDescriptorSet()->getBuffer(UBOCamera::BINDING)); - - auto *shadowUBO = device->createBuffer({ - gfx::BufferUsageBit::UNIFORM | gfx::BufferUsageBit::TRANSFER_DST, - gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE, - UBOShadow::SIZE, - UBOShadow::SIZE, - gfx::BufferFlagBit::NONE, - }); - descriptorSet->bindBuffer(UBOShadow::BINDING, shadowUBO); - - // Main light sampler binding - descriptorSet->bindSampler(SHADOWMAP::BINDING, _sampler); - descriptorSet->bindTexture(SHADOWMAP::BINDING, _pipeline->getDefaultTexture()); - // Spot light sampler binding - descriptorSet->bindSampler(SPOTLIGHTINGMAP::BINDING, _sampler); - descriptorSet->bindTexture(SPOTLIGHTINGMAP::BINDING, _pipeline->getDefaultTexture()); - - descriptorSet->update(); - - _descriptorSetMap.emplace(map::value_type(light, descriptorSet)); - - return descriptorSet; - } - - return _descriptorSetMap.at(light); -} - } // namespace pipeline } // namespace cc diff --git a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h index 1c04311e27..2686c86f06 100644 --- a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h +++ b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h @@ -72,7 +72,7 @@ private: void lightCulling(const scene::Model *model); gfx::DescriptorSet *getOrCreateDescriptorSet(const scene::Light *light); - ForwardPipeline * _pipeline = nullptr; + RenderPipeline * _pipeline = nullptr; vector> _sortedSubModelsArray; vector> _sortedPSOCIArray; vector _validLights; @@ -84,9 +84,7 @@ private: RenderBatchedQueue * _batchedQueue = nullptr; gfx::Buffer * _lightBuffer = nullptr; gfx::Buffer * _firstLightBufferView = nullptr; - gfx::Sampler * _sampler = nullptr; - std::unordered_map _descriptorSetMap{}; std::array _shadowUBO{}; uint _lightBufferStride = 0; diff --git a/native/cocos/renderer/pipeline/RenderPipeline.cpp b/native/cocos/renderer/pipeline/RenderPipeline.cpp index 492182cf9a..de4b68f37c 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.cpp +++ b/native/cocos/renderer/pipeline/RenderPipeline.cpp @@ -45,9 +45,9 @@ RenderPipeline::RenderPipeline() : _device(gfx::Device::getInstance()) { RenderPipeline::instance = this; - setDescriptorSetLayout(); generateConstantMacros(); + _globalDSManager = new GlobalDSManager(); _pipelineUBO = new PipelineUBO(); _pipelineSceneData = new PipelineSceneData(); } @@ -56,71 +56,6 @@ RenderPipeline::~RenderPipeline() { RenderPipeline::instance = nullptr; } -#define INIT_GLOBAL_DESCSET_LAYOUT(info) \ - do { \ - globalDescriptorSetLayout.samplers[info::NAME] = info::LAYOUT; \ - globalDescriptorSetLayout.bindings[info::BINDING] = info::DESCRIPTOR; \ - } while (0) - -void RenderPipeline::setDescriptorSetLayout() { - globalDescriptorSetLayout.bindings.resize(static_cast(PipelineGlobalBindings::COUNT)); - - globalDescriptorSetLayout.blocks[UBOGlobal::NAME] = UBOGlobal::LAYOUT; - globalDescriptorSetLayout.bindings[UBOGlobal::BINDING] = UBOGlobal::DESCRIPTOR; - globalDescriptorSetLayout.blocks[UBOCamera::NAME] = UBOCamera::LAYOUT; - globalDescriptorSetLayout.bindings[UBOCamera::BINDING] = UBOCamera::DESCRIPTOR; - globalDescriptorSetLayout.blocks[UBOShadow::NAME] = UBOShadow::LAYOUT; - globalDescriptorSetLayout.bindings[UBOShadow::BINDING] = UBOShadow::DESCRIPTOR; - globalDescriptorSetLayout.samplers[SHADOWMAP::NAME] = SHADOWMAP::LAYOUT; - globalDescriptorSetLayout.bindings[SHADOWMAP::BINDING] = SHADOWMAP::DESCRIPTOR; - globalDescriptorSetLayout.samplers[ENVIRONMENT::NAME] = ENVIRONMENT::LAYOUT; - globalDescriptorSetLayout.bindings[ENVIRONMENT::BINDING] = ENVIRONMENT::DESCRIPTOR; - globalDescriptorSetLayout.samplers[SPOTLIGHTINGMAP::NAME] = SPOTLIGHTINGMAP::LAYOUT; - globalDescriptorSetLayout.bindings[SPOTLIGHTINGMAP::BINDING] = SPOTLIGHTINGMAP::DESCRIPTOR; - - INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERGBUFFERALBEDOMAP); - INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERGBUFFERPOSITIONMAP); - INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERGBUFFEREMISSIVEMAP); - INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERGBUFFERNORMALMAP); - INIT_GLOBAL_DESCSET_LAYOUT(SAMPLERLIGHTINGRESULTMAP); - - localDescriptorSetLayout.bindings.resize(static_cast(ModelLocalBindings::COUNT)); - localDescriptorSetLayout.blocks[UBOLocalBatched::NAME] = UBOLocalBatched::LAYOUT; - localDescriptorSetLayout.bindings[UBOLocalBatched::BINDING] = UBOLocalBatched::DESCRIPTOR; - localDescriptorSetLayout.blocks[UBOLocal::NAME] = UBOLocal::LAYOUT; - localDescriptorSetLayout.bindings[UBOLocal::BINDING] = UBOLocal::DESCRIPTOR; - localDescriptorSetLayout.blocks[UBOForwardLight::NAME] = UBOForwardLight::LAYOUT; - localDescriptorSetLayout.bindings[UBOForwardLight::BINDING] = UBOForwardLight::DESCRIPTOR; - localDescriptorSetLayout.blocks[UBOSkinningTexture::NAME] = UBOSkinningTexture::LAYOUT; - localDescriptorSetLayout.bindings[UBOSkinningTexture::BINDING] = UBOSkinningTexture::DESCRIPTOR; - localDescriptorSetLayout.blocks[UBOSkinningAnimation::NAME] = UBOSkinningAnimation::LAYOUT; - localDescriptorSetLayout.bindings[UBOSkinningAnimation::BINDING] = UBOSkinningAnimation::DESCRIPTOR; - localDescriptorSetLayout.blocks[UBOSkinning::NAME] = UBOSkinning::LAYOUT; - localDescriptorSetLayout.bindings[UBOSkinning::BINDING] = UBOSkinning::DESCRIPTOR; - localDescriptorSetLayout.blocks[UBOMorph::NAME] = UBOMorph::LAYOUT; - localDescriptorSetLayout.bindings[UBOMorph::BINDING] = UBOMorph::DESCRIPTOR; - localDescriptorSetLayout.samplers[JOINTTEXTURE::NAME] = JOINTTEXTURE::LAYOUT; - localDescriptorSetLayout.bindings[JOINTTEXTURE::BINDING] = JOINTTEXTURE::DESCRIPTOR; - localDescriptorSetLayout.samplers[POSITIONMORPH::NAME] = POSITIONMORPH::LAYOUT; - localDescriptorSetLayout.bindings[POSITIONMORPH::BINDING] = POSITIONMORPH::DESCRIPTOR; - localDescriptorSetLayout.samplers[NORMALMORPH::NAME] = NORMALMORPH::LAYOUT; - localDescriptorSetLayout.bindings[NORMALMORPH::BINDING] = NORMALMORPH::DESCRIPTOR; - localDescriptorSetLayout.samplers[TANGENTMORPH::NAME] = TANGENTMORPH::LAYOUT; - localDescriptorSetLayout.bindings[TANGENTMORPH::BINDING] = TANGENTMORPH::DESCRIPTOR; - localDescriptorSetLayout.samplers[LIGHTMAPTEXTURE::NAME] = LIGHTMAPTEXTURE::LAYOUT; - localDescriptorSetLayout.bindings[LIGHTMAPTEXTURE::BINDING] = LIGHTMAPTEXTURE::DESCRIPTOR; - localDescriptorSetLayout.samplers[SPRITETEXTURE::NAME] = SPRITETEXTURE::LAYOUT; - localDescriptorSetLayout.bindings[SPRITETEXTURE::BINDING] = SPRITETEXTURE::DESCRIPTOR; - localDescriptorSetLayout.samplers[REFLECTIONTEXTURE::NAME] = REFLECTIONTEXTURE::LAYOUT; - localDescriptorSetLayout.bindings[REFLECTIONTEXTURE::BINDING] = REFLECTIONTEXTURE::DESCRIPTOR; - localDescriptorSetLayout.storeImages[REFLECTIONSTORAGE::NAME] = REFLECTIONSTORAGE::LAYOUT; - localDescriptorSetLayout.bindings[REFLECTIONSTORAGE::BINDING] = REFLECTIONSTORAGE::DESCRIPTOR; - localDescriptorSetLayout.samplers[REFLECTIONTEXTURE::NAME] = REFLECTIONTEXTURE::LAYOUT; - localDescriptorSetLayout.bindings[REFLECTIONTEXTURE::BINDING] = REFLECTIONTEXTURE::DESCRIPTOR; - localDescriptorSetLayout.storeImages[REFLECTIONSTORAGE::NAME] = REFLECTIONSTORAGE::LAYOUT; - localDescriptorSetLayout.bindings[REFLECTIONSTORAGE::BINDING] = REFLECTIONSTORAGE::DESCRIPTOR; -} - bool RenderPipeline::initialize(const RenderPipelineInfo &info) { _flows = info.flows; _tag = info.tag; @@ -128,17 +63,8 @@ bool RenderPipeline::initialize(const RenderPipelineInfo &info) { } bool RenderPipeline::activate() { - if (_descriptorSetLayout) { - _descriptorSetLayout->destroy(); - CC_DELETE(_descriptorSetLayout); - } - _descriptorSetLayout = _device->createDescriptorSetLayout({globalDescriptorSetLayout.bindings}); - - if (_descriptorSet) { - _descriptorSet->destroy(); - CC_DELETE(_descriptorSet); - } - _descriptorSet = _device->createDescriptorSet({_descriptorSetLayout}); + _globalDSManager->activate(_device, this); + _descriptorSet = _globalDSManager->getGlobalDescriptorSet(); _pipelineUBO->activate(_device, this); _pipelineSceneData->activate(_device, this); @@ -175,8 +101,8 @@ void RenderPipeline::destroy() { } _flows.clear(); - CC_SAFE_DESTROY(_descriptorSetLayout); - CC_SAFE_DESTROY(_descriptorSet); + _globalDSManager->getGlobalDescriptorSet()->destroy(); + CC_SAFE_DELETE(_globalDSManager); CC_SAFE_DESTROY(_pipelineUBO); CC_SAFE_DESTROY(_pipelineSceneData); diff --git a/native/cocos/renderer/pipeline/RenderPipeline.h b/native/cocos/renderer/pipeline/RenderPipeline.h index ad78abf813..ffa6e84cd6 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.h +++ b/native/cocos/renderer/pipeline/RenderPipeline.h @@ -29,6 +29,7 @@ #include "PipelineSceneData.h" #include "PipelineUBO.h" #include "base/CoreStd.h" +#include "GlobalDescriptorSetManager.h" #include "helper/DefineMap.h" #include "scene/Camera.h" @@ -40,6 +41,7 @@ class DescriptorSetLayout; } // namespace gfx namespace pipeline { class DefineMap; +class GlobalDSManager; struct CC_DLL RenderPipelineInfo { uint tag = 0; @@ -61,25 +63,24 @@ public: void setPipelineSharedSceneData(scene::PipelineSharedSceneData *data); - inline const RenderFlowList & getFlows() const { return _flows; } - inline uint getTag() const { return _tag; } - inline const map &getGlobalBindings() const { return _globalBindings; } - inline const DefineMap & getMacros() const { return _macros; } - inline void setValue(const String &name, bool value) { _macros.setValue(name, value); } - inline gfx::DescriptorSet * getDescriptorSet() const { return _descriptorSet; } - inline gfx::DescriptorSetLayout * getDescriptorSetLayout() const { return _descriptorSetLayout; } - inline gfx::Texture * getDefaultTexture() const { return _defaultTexture; } - inline PipelineSceneData * getPipelineSceneData() const { return _pipelineSceneData; } - inline const gfx::CommandBufferList & getCommandBuffers() const { return _commandBuffers; } - inline PipelineUBO * getPipelineUBO() const { return _pipelineUBO; } - inline const String & getConstantMacros() { return _constantMacros; } - inline gfx::Device * getDevice() { return _device; } + CC_INLINE const RenderFlowList & getFlows() const { return _flows; } + CC_INLINE uint getTag() const { return _tag; } + CC_INLINE const map &getGlobalBindings() const { return _globalBindings; } + CC_INLINE const DefineMap & getMacros() const { return _macros; } + CC_INLINE void setValue(const String &name, bool value) { _macros.setValue(name, value); } + CC_INLINE GlobalDSManager * getGlobalDSManager() const { return _globalDSManager; } + CC_INLINE gfx::DescriptorSet * getDescriptorSet() const { return _descriptorSet; } + CC_INLINE gfx::DescriptorSetLayout * getDescriptorSetLayout() const { return _globalDSManager->getDescriptorSetLayout(); } + CC_INLINE gfx::Texture * getDefaultTexture() const { return _defaultTexture; } + CC_INLINE PipelineSceneData * getPipelineSceneData() const { return _pipelineSceneData; } + CC_INLINE const gfx::CommandBufferList & getCommandBuffers() const { return _commandBuffers; } + CC_INLINE PipelineUBO * getPipelineUBO() const { return _pipelineUBO; } + CC_INLINE const String & getConstantMacros() { return _constantMacros; } + CC_INLINE gfx::Device * getDevice() { return _device; } protected: static RenderPipeline *instance; - static void setDescriptorSetLayout(); - void generateConstantMacros(); gfx::CommandBufferList _commandBuffers; @@ -90,7 +91,7 @@ protected: String _constantMacros; gfx::Device * _device = nullptr; - gfx::DescriptorSetLayout *_descriptorSetLayout = nullptr; + GlobalDSManager * _globalDSManager = nullptr; gfx::DescriptorSet * _descriptorSet = nullptr; PipelineUBO * _pipelineUBO = nullptr; PipelineSceneData * _pipelineSceneData = nullptr;