Files
cocos4/native/cocos/core/Root.cpp
T
hyde zhou 02407dbc70 V3.6.0 pipeline (#11277)
* decouple GlobalDescriptorSetManager from RenderPipeline

* formatting

* move interface onGlobalPipelineStateChanged from PipelineSceneData to Pipeline

* add layouts

* fix eslint error

* rendergraph creates device resources

* restore resourceGraph

* resource graph updated

* add scene interface

* layout graph updated

* stop use IntrusivePtr<gfx::Swapchain>

* add setViewport setScissor

* scene transversal modified

* accidentally omitted

* remove pipeline event from pipeline, decouple terrain from render pipeline

* shadingScale moved from PipelineSceneData to Pipeline

* Pipeline removed from PipelineSceneData

* pipelineEvent refactored

* fix typo

* rendergraph executes

* interface changed

* fix submit

* Update the draw function

* pipeline runtime added to pipeline

* automatically barrier

* typo

* comment

* forgot sth important

* add classic/custom pipeline switch

* web pipeline can be enabled

* relax pipeline interface

* pipeline use gfx::Color

* decouple native RenderPipeline

* fix clang tidy

* add get pipelineSceneData

* present refactored

* added executor example

* half way done

* find slot

* merge & sort layout

* layout graph updated

* enable native usesCustomPipeline

* change mergedbs

* fix comment

* fix eslint error

* fix

* draft

* update the rendergraph compiler

* update the compiler

* update executor

* update to latest

* revert mtlcmdbuffer manually

* valid added

* external node

* add framebuffer

* fix linter

* fix linter

* fix implicit narrow cast

* fix linter

* use native root.pipeline

* use Root to get pipeline instance

* fix ccstd::string

* turn off validation

* cleanup

* ccstd::pmr::string changed

* update the pipeline ubo

* use ccstd::pmr::string

* uses ccstd::pmr::vector

* uses ccstd::pmr::map

* reconstruction

* external res

* compile issue

* clang tidy

* fix ccstd::pmr::string hash

* fix potential issue

* Fixed some rendering errors of webpipeline in the editor

* Optimize attachment load/storeOp logic

* update executor

* WebPipeline bug fixed in editor

* Fix the problem of resource cache error when camera is deleted or added

* webpipeline performs logic adjustments and bug fixes

* render graph updated

* descriptor updated

* descriptor updated

* fix member traits

* libstdc++ does not support hashing std::pmr::string on linux

* refactor descriptor

* jsb allocator walkaround

* rename

* descriptor type refactored

* fix default

* adding layout graph builder

* transfer env

* switch env

* switch env

* adding native layout builder

* adding native builder

* adding layout graph

* adding descriptor

* adding descriptor layout

* create layout graph builder added

* fix root

* format

* add web layout graph builder

* refactor web layout graph

* fix intrusive ptr

* descriptor set data updated

* before applying another idea

* debug string added

* adding render graph

* adding resource graph

* adding native pipeline

* graph remove edge temporary fix

* remove_edge simplified

* remove edge simplified

* outIter renamed

* remove edges simplified

* fix reference graph impl

* run test case

* adding native impl

* remove unused native

* queue added

* pass reorder v1.0

* adding native pipeline

* adding native pipeline

* fix bug

* render graph setup added

* render graph updated

* fix ccstd::string

* getFirstChildLayout, num of children must be 1

* remove examples

* structure refactor

* pass reorder & framegraphDispatcher interface

* remove test code

* remove debug code

* ts added object detection

* ts get name added

* RenderGraph setup moved

* add ts layout graph implement

* fix lint

* fix ci: implicit type conversion

* ci

* adding warmup

* turn off custom

* pipeline updated

* format

* try disable clang 64 to 32 narrow warning

Co-authored-by: zxx43 <zxx43@hotmail.com>
Co-authored-by: GengineJS <476393671@qq.com>
Co-authored-by: Zeqiang Li <zeqiang-li@outlook.com>
2022-06-01 19:06:59 +08:00

408 lines
12 KiB
C++

/****************************************************************************
Copyright (c) 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 "core/Root.h"
#include "core/event/CallbacksInvoker.h"
#include "core/event/EventTypesToJS.h"
#include "profiler/Profiler.h"
#include "renderer/gfx-base/GFXDef.h"
#include "renderer/gfx-base/GFXDevice.h"
#include "renderer/gfx-base/GFXSwapchain.h"
#include "renderer/pipeline/PipelineSceneData.h"
#include "renderer/pipeline/custom/NativePipelineTypes.h"
#include "renderer/pipeline/custom/RenderInterfaceTypes.h"
#include "renderer/pipeline/deferred/DeferredPipeline.h"
#include "renderer/pipeline/forward/ForwardPipeline.h"
#include "scene/Camera.h"
#include "scene/DirectionalLight.h"
#include "scene/DrawBatch2D.h"
#include "scene/SpotLight.h"
namespace cc {
namespace {
Root *instance = nullptr;
}
Root *Root::getInstance() {
return instance;
}
Root::Root(gfx::Device *device)
: _device(device) {
instance = this;
_eventProcessor = new CallbacksInvoker();
// TODO(minggo):
// this._dataPoolMgr = legacyCC.internal.DataPoolManager && new legacyCC.internal.DataPoolManager(device) as DataPoolManager;
_cameraList.reserve(6);
_swapchains.reserve(2);
}
Root::~Root() {
destroy();
CC_SAFE_DELETE(_eventProcessor);
instance = nullptr;
}
void Root::initialize(gfx::Swapchain *swapchain) {
_swapchain = swapchain;
gfx::RenderPassInfo renderPassInfo;
gfx::ColorAttachment colorAttachment;
colorAttachment.format = swapchain->getColorTexture()->getFormat();
renderPassInfo.colorAttachments.emplace_back(colorAttachment);
auto &depthStencilAttachment = renderPassInfo.depthStencilAttachment;
depthStencilAttachment.format = swapchain->getDepthStencilTexture()->getFormat();
depthStencilAttachment.depthStoreOp = gfx::StoreOp::DISCARD;
depthStencilAttachment.stencilStoreOp = gfx::StoreOp::DISCARD;
scene::IRenderWindowInfo info;
info.title = ccstd::string{"rootMainWindow"};
info.width = swapchain->getWidth();
info.height = swapchain->getHeight();
info.renderPassInfo = renderPassInfo;
info.swapchain = swapchain;
_mainWindow = createWindow(info);
_curWindow = _mainWindow;
// TODO(minggo):
// return Promise.resolve(builtinResMgr.initBuiltinRes(this._device));
}
render::Pipeline *Root::getCustomPipeline() const {
return dynamic_cast<render::Pipeline *>(_pipelineRuntime.get());
}
void Root::destroy() {
destroyScenes();
if (_usesCustomPipeline) {
_pipelineRuntime->destroy();
}
_pipelineRuntime.reset();
CC_SAFE_DESTROY_NULL(_pipeline);
// TODO(minggo):
// CC_SAFE_DESTROY(_batcher2D);
// TODO(minggo):
// this.dataPoolManager.clear();
}
void Root::resize(uint32_t width, uint32_t height) {
for (const auto &window : _windows) {
if (window->getSwapchain()) {
window->resize(width, height);
}
}
}
namespace {
class RenderPipelineBridge final : public render::PipelineRuntime {
public:
explicit RenderPipelineBridge(pipeline::RenderPipeline *pipelineIn)
: pipeline(pipelineIn) {}
bool activate(gfx::Swapchain *swapchain) override {
return pipeline->activate(swapchain);
}
bool destroy() noexcept override {
return pipeline->destroy();
}
void render(const ccstd::vector<scene::Camera *> &cameras) override {
pipeline->render(cameras);
}
const MacroRecord &getMacros() const override {
return pipeline->getMacros();
}
pipeline::GlobalDSManager *getGlobalDSManager() const override {
return pipeline->getGlobalDSManager();
}
gfx::DescriptorSetLayout *getDescriptorSetLayout() const override {
return pipeline->getDescriptorSetLayout();
}
pipeline::PipelineSceneData *getPipelineSceneData() const override {
return pipeline->getPipelineSceneData();
}
const ccstd::string &getConstantMacros() const override {
return pipeline->getConstantMacros();
}
scene::Model *getProfiler() const override {
return pipeline->getProfiler();
}
void setProfiler(scene::Model *profiler) override {
pipeline->setProfiler(profiler);
}
float getShadingScale() const override {
return pipeline->getShadingScale();
}
void setShadingScale(float scale) override {
pipeline->setShadingScale(scale);
}
void onGlobalPipelineStateChanged() override {
pipeline->onGlobalPipelineStateChanged();
}
void setValue(const ccstd::string &name, int32_t value) override {
pipeline->setValue(name, value);
}
void setValue(const ccstd::string &name, bool value) override {
pipeline->setValue(name, value);
}
bool isOcclusionQueryEnabled() const override {
return pipeline->isOcclusionQueryEnabled();
}
pipeline::RenderPipeline *pipeline = nullptr;
};
} // namespace
bool Root::setRenderPipeline(pipeline::RenderPipeline *rppl /* = nullptr*/) {
if (!_usesCustomPipeline) {
if (rppl != nullptr && dynamic_cast<pipeline::DeferredPipeline *>(rppl) != nullptr) {
_useDeferredPipeline = true;
}
bool isCreateDefaultPipeline{false};
if (!rppl) {
rppl = ccnew pipeline::ForwardPipeline();
rppl->initialize({});
isCreateDefaultPipeline = true;
}
_pipeline = rppl;
_pipelineRuntime = std::make_unique<RenderPipelineBridge>(rppl);
// now cluster just enabled in deferred pipeline
if (!_useDeferredPipeline || !_device->hasFeature(gfx::Feature::COMPUTE_SHADER)) {
// disable cluster
_pipeline->setClusterEnabled(false);
}
_pipeline->setBloomEnabled(false);
if (!_pipeline->activate(_mainWindow->getSwapchain())) {
if (isCreateDefaultPipeline) {
CC_SAFE_DESTROY_AND_DELETE(_pipeline);
}
_pipeline = nullptr;
return false;
}
} else {
_pipelineRuntime = std::make_unique<render::NativePipeline>(
boost::container::pmr::get_default_resource());
if (!_pipelineRuntime->activate(_mainWindow->getSwapchain())) {
_pipelineRuntime->destroy();
_pipelineRuntime.reset();
return false;
}
}
// TODO(minggo):
// auto *scene = Director::getInstance()->getScene();
// if (scene) {
// scene->getSceneGlobals()->activate();
// }
onGlobalPipelineStateChanged();
_eventProcessor->emit(EventTypesToJS::ROOT_BATCH2D_INIT, this);
// TODO(minggo):
// if (!_batcher) {
// _batcher = ccnew Batcher2D(this);
// if (!this._batcher.initialize()) {
// this.destroy();
// return false;
// }
// }
return true;
}
void Root::onGlobalPipelineStateChanged() {
for (const auto &scene : _scenes) {
scene->onGlobalPipelineStateChanged();
}
_pipelineRuntime->onGlobalPipelineStateChanged();
}
void Root::activeWindow(scene::RenderWindow *window) {
_curWindow = window;
}
void Root::resetCumulativeTime() {
_cumulativeTime = 0;
}
void Root::frameMove(float deltaTime, int32_t totalFrames) {
if (!cc::gfx::Device::getInstance()->isRendererAvailable()) {
return;
}
CCObject::deferredDestroy();
_frameTime = deltaTime;
++_frameCount;
_cumulativeTime += deltaTime;
_fpsTime += deltaTime;
if (_fpsTime > 1.0F) {
_fps = _frameCount;
_frameCount = 0;
_fpsTime = 0.0;
}
for (const auto &scene : _scenes) {
scene->removeBatches();
}
_eventProcessor->emit(EventTypesToJS::ROOT_BATCH2D_UPDATE, this); // cjh added for sync logic in ts.
// TODO(minggo):
// if (_batcher) {
// _batcher.update();
// }
//
_cameraList.clear();
for (const auto &window : _windows) {
window->extractRenderCameras(_cameraList);
}
if (_pipelineRuntime != nullptr && !_cameraList.empty()) {
_swapchains.clear();
_swapchains.emplace_back(_swapchain);
_device->acquire(_swapchains);
// NOTE: c++ doesn't have a Director, so totalFrames need to be set from JS
uint32_t stamp = totalFrames;
_eventProcessor->emit(EventTypesToJS::ROOT_BATCH2D_UPLOAD_BUFFERS, this);
// if (_batcher != nullptr) {
// _batcher->uploadBuffers();
// }
for (const auto &scene : _scenes) {
scene->update(stamp);
}
CC_PROFILER_UPDATE;
_eventProcessor->emit(EventTypesToJS::DIRECTOR_BEFORE_COMMIT, this);
std::stable_sort(_cameraList.begin(), _cameraList.end(), [](const auto *a, const auto *b) {
return a->getPriority() < b->getPriority();
});
_pipelineRuntime->render(_cameraList);
_device->present();
}
_eventProcessor->emit(EventTypesToJS::ROOT_BATCH2D_RESET, this);
// cjh TODO: if (this._batcher) this._batcher.reset();
}
scene::RenderWindow *Root::createWindow(scene::IRenderWindowInfo &info) {
IntrusivePtr<scene::RenderWindow> window = ccnew scene::RenderWindow();
window->initialize(_device, info);
_windows.emplace_back(window);
return window;
}
void Root::destroyWindow(scene::RenderWindow *window) {
auto it = std::find(_windows.begin(), _windows.end(), window);
if (it != _windows.end()) {
CC_SAFE_DESTROY(*it);
_windows.erase(it);
}
}
void Root::destroyWindows() {
for (const auto &window : _windows) {
CC_SAFE_DESTROY(window);
}
_windows.clear();
}
scene::RenderScene *Root::createScene(const scene::IRenderSceneInfo &info) {
IntrusivePtr<scene::RenderScene> scene = ccnew scene::RenderScene();
scene->initialize(info);
_scenes.emplace_back(scene);
return scene.get();
}
void Root::destroyScene(scene::RenderScene *scene) {
auto it = std::find(_scenes.begin(), _scenes.end(), scene);
if (it != _scenes.end()) {
CC_SAFE_DESTROY(*it);
_scenes.erase(it);
}
}
void Root::destroyModel(scene::Model *model) { // NOLINT(readability-convert-member-functions-to-static)
if (model == nullptr) {
return;
}
if (model->getScene() != nullptr) {
model->getScene()->removeModel(model);
}
model->destroy();
}
void Root::destroyLight(scene::Light *light) { // NOLINT(readability-convert-member-functions-to-static)
if (light == nullptr) {
return;
}
if (light->getScene() != nullptr) {
if (light->getType() == scene::LightType::DIRECTIONAL) {
light->getScene()->removeDirectionalLight(static_cast<scene::DirectionalLight *>(light));
} else if (light->getType() == scene::LightType::SPHERE) {
light->getScene()->removeSphereLight(static_cast<scene::SphereLight *>(light));
} else if (light->getType() == scene::LightType::SPOT) {
light->getScene()->removeSpotLight(static_cast<scene::SpotLight *>(light));
}
}
light->destroy();
}
scene::Camera *Root::createCamera() const {
return ccnew scene::Camera(_device);
}
void Root::destroyScenes() {
for (const auto &scene : _scenes) {
CC_SAFE_DESTROY(scene);
}
_scenes.clear();
}
} // namespace cc