diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index 405bac5826..7e26674cef 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -620,6 +620,18 @@ cocos_source_files( cocos/renderer/pipeline/SceneCulling.h cocos/renderer/pipeline/forward/UIPhase.cpp cocos/renderer/pipeline/forward/UIPhase.h + cocos/renderer/pipeline/deferred/DeferredPipeline.cpp + cocos/renderer/pipeline/deferred/DeferredPipeline.h + cocos/renderer/pipeline/deferred/GbufferFlow.cpp + cocos/renderer/pipeline/deferred/GbufferFlow.h + cocos/renderer/pipeline/deferred/GbufferStage.cpp + cocos/renderer/pipeline/deferred/GbufferStage.h + cocos/renderer/pipeline/deferred/LightingFlow.cpp + cocos/renderer/pipeline/deferred/LightingFlow.h + cocos/renderer/pipeline/deferred/LightingStage.cpp + cocos/renderer/pipeline/deferred/LightingStage.h + cocos/renderer/pipeline/deferred/PostprocessStage.cpp + cocos/renderer/pipeline/deferred/PostprocessStage.h cocos/renderer/pipeline/shadow/ShadowFlow.cpp cocos/renderer/pipeline/shadow/ShadowFlow.h cocos/renderer/pipeline/shadow/ShadowStage.cpp diff --git a/native/cocos/bindings/auto/jsb_pipeline_auto.cpp b/native/cocos/bindings/auto/jsb_pipeline_auto.cpp index 40cf1dff70..beccbd8dbd 100644 --- a/native/cocos/bindings/auto/jsb_pipeline_auto.cpp +++ b/native/cocos/bindings/auto/jsb_pipeline_auto.cpp @@ -12,6 +12,12 @@ #include "renderer/pipeline/Define.h" #include "renderer/pipeline/helper/SharedMemory.h" #include "renderer/pipeline/InstancedBuffer.h" +#include "renderer/pipeline/deferred/DeferredPipeline.h" +#include "renderer/pipeline/deferred/GbufferFlow.h" +#include "renderer/pipeline/deferred/GbufferStage.h" +#include "renderer/pipeline/deferred/LightingFlow.h" +#include "renderer/pipeline/deferred/LightingStage.h" +#include "renderer/pipeline/deferred/PostprocessStage.h" #include "cocos/renderer/core/Core.h" #include "cocos/renderer/core/gfx/GFXDescriptorSet.h" @@ -2057,6 +2063,25 @@ static bool js_pipeline_RenderPipeline_getDescriptorSetLayout(se::State& s) } SE_BIND_PROP_GET(js_pipeline_RenderPipeline_getDescriptorSetLayout) +static bool js_pipeline_RenderPipeline_getDevice(se::State& s) +{ + cc::pipeline::RenderPipeline* cobj = SE_THIS_OBJECT(s); + SE_PRECONDITION2(cobj, false, "js_pipeline_RenderPipeline_getDevice : Invalid Native Object"); + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc == 0) { + cc::gfx::Device* result = cobj->getDevice(); + ok &= nativevalue_to_se(result, s.rval(), nullptr /*ctx*/); + SE_PRECONDITION2(ok, false, "js_pipeline_RenderPipeline_getDevice : Error processing arguments"); + SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); + return true; + } + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0); + return false; +} +SE_BIND_FUNC(js_pipeline_RenderPipeline_getDevice) + static bool js_pipeline_RenderPipeline_initialize(se::State& s) { cc::pipeline::RenderPipeline* cobj = SE_THIS_OBJECT(s); @@ -2098,6 +2123,25 @@ static bool js_pipeline_RenderPipeline_render(se::State& s) } SE_BIND_FUNC(js_pipeline_RenderPipeline_render) +static bool js_pipeline_RenderPipeline_setPipelineSharedSceneData(se::State& s) +{ + cc::pipeline::RenderPipeline* cobj = SE_THIS_OBJECT(s); + SE_PRECONDITION2(cobj, false, "js_pipeline_RenderPipeline_setPipelineSharedSceneData : Invalid Native Object"); + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc == 1) { + HolderType arg0 = {}; + ok &= sevalue_to_native(args[0], &arg0, s.thisObject()); + SE_PRECONDITION2(ok, false, "js_pipeline_RenderPipeline_setPipelineSharedSceneData : Error processing arguments"); + cobj->setPipelineSharedSceneData(arg0.value()); + return true; + } + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1); + return false; +} +SE_BIND_FUNC(js_pipeline_RenderPipeline_setPipelineSharedSceneData) + static bool js_pipeline_RenderPipeline_setValue(se::State& s) { cc::pipeline::RenderPipeline* cobj = SE_THIS_OBJECT(s); @@ -2147,8 +2191,10 @@ bool js_register_pipeline_RenderPipeline(se::Object* obj) cls->defineProperty("descriptorSetLayout", _SE(js_pipeline_RenderPipeline_getDescriptorSetLayout), nullptr); cls->defineFunction("activate", _SE(js_pipeline_RenderPipeline_activate)); cls->defineFunction("destroy", _SE(js_pipeline_RenderPipeline_destroy)); + cls->defineFunction("getDevice", _SE(js_pipeline_RenderPipeline_getDevice)); cls->defineFunction("initialize", _SE(js_pipeline_RenderPipeline_initialize)); cls->defineFunction("render", _SE(js_pipeline_RenderPipeline_render)); + cls->defineFunction("setPipelineSharedSceneData", _SE(js_pipeline_RenderPipeline_setPipelineSharedSceneData)); cls->defineFunction("setValue", _SE(js_pipeline_RenderPipeline_setValue)); cls->defineStaticFunction("getInstance", _SE(js_pipeline_RenderPipeline_getInstance)); cls->install(); @@ -2164,101 +2210,6 @@ bool js_register_pipeline_RenderPipeline(se::Object* obj) se::Object* __jsb_cc_pipeline_ForwardPipeline_proto = nullptr; se::Class* __jsb_cc_pipeline_ForwardPipeline_class = nullptr; -static bool js_pipeline_ForwardPipeline_getSphere(se::State& s) -{ - cc::pipeline::ForwardPipeline* cobj = SE_THIS_OBJECT(s); - SE_PRECONDITION2(cobj, false, "js_pipeline_ForwardPipeline_getSphere : Invalid Native Object"); - const auto& args = s.args(); - size_t argc = args.size(); - CC_UNUSED bool ok = true; - if (argc == 0) { - cc::pipeline::Sphere* result = cobj->getSphere(); - ok &= nativevalue_to_se(result, s.rval(), nullptr /*ctx*/); - SE_PRECONDITION2(ok, false, "js_pipeline_ForwardPipeline_getSphere : Error processing arguments"); - SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); - return true; - } - SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0); - return false; -} -SE_BIND_FUNC(js_pipeline_ForwardPipeline_getSphere) - -static bool js_pipeline_ForwardPipeline_setAmbient(se::State& s) -{ - cc::pipeline::ForwardPipeline* cobj = SE_THIS_OBJECT(s); - SE_PRECONDITION2(cobj, false, "js_pipeline_ForwardPipeline_setAmbient : Invalid Native Object"); - const auto& args = s.args(); - size_t argc = args.size(); - CC_UNUSED bool ok = true; - if (argc == 1) { - HolderType arg0 = {}; - ok &= sevalue_to_native(args[0], &arg0, s.thisObject()); - SE_PRECONDITION2(ok, false, "js_pipeline_ForwardPipeline_setAmbient : Error processing arguments"); - cobj->setAmbient(arg0.value()); - return true; - } - SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1); - return false; -} -SE_BIND_FUNC(js_pipeline_ForwardPipeline_setAmbient) - -static bool js_pipeline_ForwardPipeline_setFog(se::State& s) -{ - cc::pipeline::ForwardPipeline* cobj = SE_THIS_OBJECT(s); - SE_PRECONDITION2(cobj, false, "js_pipeline_ForwardPipeline_setFog : Invalid Native Object"); - const auto& args = s.args(); - size_t argc = args.size(); - CC_UNUSED bool ok = true; - if (argc == 1) { - HolderType arg0 = {}; - ok &= sevalue_to_native(args[0], &arg0, s.thisObject()); - SE_PRECONDITION2(ok, false, "js_pipeline_ForwardPipeline_setFog : Error processing arguments"); - cobj->setFog(arg0.value()); - return true; - } - SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1); - return false; -} -SE_BIND_FUNC(js_pipeline_ForwardPipeline_setFog) - -static bool js_pipeline_ForwardPipeline_setShadows(se::State& s) -{ - cc::pipeline::ForwardPipeline* cobj = SE_THIS_OBJECT(s); - SE_PRECONDITION2(cobj, false, "js_pipeline_ForwardPipeline_setShadows : Invalid Native Object"); - const auto& args = s.args(); - size_t argc = args.size(); - CC_UNUSED bool ok = true; - if (argc == 1) { - HolderType arg0 = {}; - ok &= sevalue_to_native(args[0], &arg0, s.thisObject()); - SE_PRECONDITION2(ok, false, "js_pipeline_ForwardPipeline_setShadows : Error processing arguments"); - cobj->setShadows(arg0.value()); - return true; - } - SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1); - return false; -} -SE_BIND_FUNC(js_pipeline_ForwardPipeline_setShadows) - -static bool js_pipeline_ForwardPipeline_setSkybox(se::State& s) -{ - cc::pipeline::ForwardPipeline* cobj = SE_THIS_OBJECT(s); - SE_PRECONDITION2(cobj, false, "js_pipeline_ForwardPipeline_setSkybox : Invalid Native Object"); - const auto& args = s.args(); - size_t argc = args.size(); - CC_UNUSED bool ok = true; - if (argc == 1) { - HolderType arg0 = {}; - ok &= sevalue_to_native(args[0], &arg0, s.thisObject()); - SE_PRECONDITION2(ok, false, "js_pipeline_ForwardPipeline_setSkybox : Error processing arguments"); - cobj->setSkybox(arg0.value()); - return true; - } - SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1); - return false; -} -SE_BIND_FUNC(js_pipeline_ForwardPipeline_setSkybox) - SE_DECLARE_FINALIZE_FUNC(js_cc_pipeline_ForwardPipeline_finalize) static bool js_pipeline_ForwardPipeline_constructor(se::State& s) // constructor.c @@ -2291,11 +2242,6 @@ bool js_register_pipeline_ForwardPipeline(se::Object* obj) { auto cls = se::Class::create("ForwardPipeline", obj, __jsb_cc_pipeline_RenderPipeline_proto, _SE(js_pipeline_ForwardPipeline_constructor)); - cls->defineFunction("getSphere", _SE(js_pipeline_ForwardPipeline_getSphere)); - cls->defineFunction("setAmbient", _SE(js_pipeline_ForwardPipeline_setAmbient)); - cls->defineFunction("setFog", _SE(js_pipeline_ForwardPipeline_setFog)); - cls->defineFunction("setShadows", _SE(js_pipeline_ForwardPipeline_setShadows)); - cls->defineFunction("setSkybox", _SE(js_pipeline_ForwardPipeline_setSkybox)); cls->defineFinalizeFunction(_SE(js_cc_pipeline_ForwardPipeline_finalize)); cls->install(); JSBClassType::registerClass(cls); @@ -2962,6 +2908,25 @@ static bool js_pipeline_RenderStage_activate(se::State& s) } SE_BIND_FUNC(js_pipeline_RenderStage_activate) +static bool js_pipeline_RenderStage_getFlow(se::State& s) +{ + cc::pipeline::RenderStage* cobj = SE_THIS_OBJECT(s); + SE_PRECONDITION2(cobj, false, "js_pipeline_RenderStage_getFlow : Invalid Native Object"); + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc == 0) { + cc::pipeline::RenderFlow* result = cobj->getFlow(); + ok &= nativevalue_to_se(result, s.rval(), nullptr /*ctx*/); + SE_PRECONDITION2(ok, false, "js_pipeline_RenderStage_getFlow : Error processing arguments"); + SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); + return true; + } + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0); + return false; +} +SE_BIND_FUNC(js_pipeline_RenderStage_getFlow) + static bool js_pipeline_RenderStage_getTag(se::State& s) { cc::pipeline::RenderStage* cobj = SE_THIS_OBJECT(s); @@ -3011,6 +2976,7 @@ bool js_register_pipeline_RenderStage(se::Object* obj) auto cls = se::Class::create("RenderStage", obj, nullptr, nullptr); cls->defineFunction("activate", _SE(js_pipeline_RenderStage_activate)); + cls->defineFunction("getFlow", _SE(js_pipeline_RenderStage_getFlow)); cls->defineFunction("getTag", _SE(js_pipeline_RenderStage_getTag)); cls->defineFunction("initialize", _SE(js_pipeline_RenderStage_initialize)); cls->install(); @@ -3383,6 +3349,354 @@ bool js_register_pipeline_InstancedBuffer(se::Object* obj) return true; } +se::Object* __jsb_cc_pipeline_DeferredPipeline_proto = nullptr; +se::Class* __jsb_cc_pipeline_DeferredPipeline_class = nullptr; + +SE_DECLARE_FINALIZE_FUNC(js_cc_pipeline_DeferredPipeline_finalize) + +static bool js_pipeline_DeferredPipeline_constructor(se::State& s) // constructor.c +{ + cc::pipeline::DeferredPipeline* cobj = JSB_ALLOC(cc::pipeline::DeferredPipeline); + s.thisObject()->setPrivateData(cobj); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); + return true; +} +SE_BIND_CTOR(js_pipeline_DeferredPipeline_constructor, __jsb_cc_pipeline_DeferredPipeline_class, js_cc_pipeline_DeferredPipeline_finalize) + + + +extern se::Object* __jsb_cc_pipeline_RenderPipeline_proto; + +static bool js_cc_pipeline_DeferredPipeline_finalize(se::State& s) +{ + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(SE_THIS_OBJECT(s)); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) + { + se::NonRefNativePtrCreatedByCtorMap::erase(iter); + cc::pipeline::DeferredPipeline* cobj = SE_THIS_OBJECT(s); + JSB_FREE(cobj); + } + return true; +} +SE_BIND_FINALIZE_FUNC(js_cc_pipeline_DeferredPipeline_finalize) + +bool js_register_pipeline_DeferredPipeline(se::Object* obj) +{ + auto cls = se::Class::create("DeferredPipeline", obj, __jsb_cc_pipeline_RenderPipeline_proto, _SE(js_pipeline_DeferredPipeline_constructor)); + + cls->defineFinalizeFunction(_SE(js_cc_pipeline_DeferredPipeline_finalize)); + cls->install(); + JSBClassType::registerClass(cls); + + __jsb_cc_pipeline_DeferredPipeline_proto = cls->getProto(); + __jsb_cc_pipeline_DeferredPipeline_class = cls; + + se::ScriptEngine::getInstance()->clearException(); + return true; +} + +se::Object* __jsb_cc_pipeline_GbufferFlow_proto = nullptr; +se::Class* __jsb_cc_pipeline_GbufferFlow_class = nullptr; + +static bool js_pipeline_GbufferFlow_getInitializeInfo(se::State& s) +{ + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc == 0) { + const cc::pipeline::RenderFlowInfo& result = cc::pipeline::GbufferFlow::getInitializeInfo(); + ok &= nativevalue_to_se(result, s.rval(), nullptr /*ctx*/); + SE_PRECONDITION2(ok, false, "js_pipeline_GbufferFlow_getInitializeInfo : Error processing arguments"); + SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); + return true; + } + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0); + return false; +} +SE_BIND_FUNC(js_pipeline_GbufferFlow_getInitializeInfo) + +SE_DECLARE_FINALIZE_FUNC(js_cc_pipeline_GbufferFlow_finalize) + +static bool js_pipeline_GbufferFlow_constructor(se::State& s) // constructor.c +{ + cc::pipeline::GbufferFlow* cobj = JSB_ALLOC(cc::pipeline::GbufferFlow); + s.thisObject()->setPrivateData(cobj); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); + return true; +} +SE_BIND_CTOR(js_pipeline_GbufferFlow_constructor, __jsb_cc_pipeline_GbufferFlow_class, js_cc_pipeline_GbufferFlow_finalize) + + + +extern se::Object* __jsb_cc_pipeline_RenderFlow_proto; + +static bool js_cc_pipeline_GbufferFlow_finalize(se::State& s) +{ + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(SE_THIS_OBJECT(s)); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) + { + se::NonRefNativePtrCreatedByCtorMap::erase(iter); + cc::pipeline::GbufferFlow* cobj = SE_THIS_OBJECT(s); + JSB_FREE(cobj); + } + return true; +} +SE_BIND_FINALIZE_FUNC(js_cc_pipeline_GbufferFlow_finalize) + +bool js_register_pipeline_GbufferFlow(se::Object* obj) +{ + auto cls = se::Class::create("GbufferFlow", obj, __jsb_cc_pipeline_RenderFlow_proto, _SE(js_pipeline_GbufferFlow_constructor)); + + cls->defineStaticFunction("getInitializeInfo", _SE(js_pipeline_GbufferFlow_getInitializeInfo)); + cls->defineFinalizeFunction(_SE(js_cc_pipeline_GbufferFlow_finalize)); + cls->install(); + JSBClassType::registerClass(cls); + + __jsb_cc_pipeline_GbufferFlow_proto = cls->getProto(); + __jsb_cc_pipeline_GbufferFlow_class = cls; + + se::ScriptEngine::getInstance()->clearException(); + return true; +} + +se::Object* __jsb_cc_pipeline_GbufferStage_proto = nullptr; +se::Class* __jsb_cc_pipeline_GbufferStage_class = nullptr; + +static bool js_pipeline_GbufferStage_getInitializeInfo(se::State& s) +{ + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc == 0) { + const cc::pipeline::RenderStageInfo& result = cc::pipeline::GbufferStage::getInitializeInfo(); + ok &= nativevalue_to_se(result, s.rval(), nullptr /*ctx*/); + SE_PRECONDITION2(ok, false, "js_pipeline_GbufferStage_getInitializeInfo : Error processing arguments"); + SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); + return true; + } + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0); + return false; +} +SE_BIND_FUNC(js_pipeline_GbufferStage_getInitializeInfo) + +SE_DECLARE_FINALIZE_FUNC(js_cc_pipeline_GbufferStage_finalize) + +static bool js_pipeline_GbufferStage_constructor(se::State& s) // constructor.c +{ + cc::pipeline::GbufferStage* cobj = JSB_ALLOC(cc::pipeline::GbufferStage); + s.thisObject()->setPrivateData(cobj); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); + return true; +} +SE_BIND_CTOR(js_pipeline_GbufferStage_constructor, __jsb_cc_pipeline_GbufferStage_class, js_cc_pipeline_GbufferStage_finalize) + + + +extern se::Object* __jsb_cc_pipeline_RenderStage_proto; + +static bool js_cc_pipeline_GbufferStage_finalize(se::State& s) +{ + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(SE_THIS_OBJECT(s)); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) + { + se::NonRefNativePtrCreatedByCtorMap::erase(iter); + cc::pipeline::GbufferStage* cobj = SE_THIS_OBJECT(s); + JSB_FREE(cobj); + } + return true; +} +SE_BIND_FINALIZE_FUNC(js_cc_pipeline_GbufferStage_finalize) + +bool js_register_pipeline_GbufferStage(se::Object* obj) +{ + auto cls = se::Class::create("GbufferStage", obj, __jsb_cc_pipeline_RenderStage_proto, _SE(js_pipeline_GbufferStage_constructor)); + + cls->defineStaticFunction("getInitializeInfo", _SE(js_pipeline_GbufferStage_getInitializeInfo)); + cls->defineFinalizeFunction(_SE(js_cc_pipeline_GbufferStage_finalize)); + cls->install(); + JSBClassType::registerClass(cls); + + __jsb_cc_pipeline_GbufferStage_proto = cls->getProto(); + __jsb_cc_pipeline_GbufferStage_class = cls; + + se::ScriptEngine::getInstance()->clearException(); + return true; +} + +se::Object* __jsb_cc_pipeline_LightingFlow_proto = nullptr; +se::Class* __jsb_cc_pipeline_LightingFlow_class = nullptr; + +static bool js_pipeline_LightingFlow_getInitializeInfo(se::State& s) +{ + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc == 0) { + const cc::pipeline::RenderFlowInfo& result = cc::pipeline::LightingFlow::getInitializeInfo(); + ok &= nativevalue_to_se(result, s.rval(), nullptr /*ctx*/); + SE_PRECONDITION2(ok, false, "js_pipeline_LightingFlow_getInitializeInfo : Error processing arguments"); + SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); + return true; + } + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0); + return false; +} +SE_BIND_FUNC(js_pipeline_LightingFlow_getInitializeInfo) + +SE_DECLARE_FINALIZE_FUNC(js_cc_pipeline_LightingFlow_finalize) + +static bool js_pipeline_LightingFlow_constructor(se::State& s) // constructor.c +{ + cc::pipeline::LightingFlow* cobj = JSB_ALLOC(cc::pipeline::LightingFlow); + s.thisObject()->setPrivateData(cobj); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); + return true; +} +SE_BIND_CTOR(js_pipeline_LightingFlow_constructor, __jsb_cc_pipeline_LightingFlow_class, js_cc_pipeline_LightingFlow_finalize) + + + +extern se::Object* __jsb_cc_pipeline_RenderFlow_proto; + +static bool js_cc_pipeline_LightingFlow_finalize(se::State& s) +{ + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(SE_THIS_OBJECT(s)); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) + { + se::NonRefNativePtrCreatedByCtorMap::erase(iter); + cc::pipeline::LightingFlow* cobj = SE_THIS_OBJECT(s); + JSB_FREE(cobj); + } + return true; +} +SE_BIND_FINALIZE_FUNC(js_cc_pipeline_LightingFlow_finalize) + +bool js_register_pipeline_LightingFlow(se::Object* obj) +{ + auto cls = se::Class::create("LightingFlow", obj, __jsb_cc_pipeline_RenderFlow_proto, _SE(js_pipeline_LightingFlow_constructor)); + + cls->defineStaticFunction("getInitializeInfo", _SE(js_pipeline_LightingFlow_getInitializeInfo)); + cls->defineFinalizeFunction(_SE(js_cc_pipeline_LightingFlow_finalize)); + cls->install(); + JSBClassType::registerClass(cls); + + __jsb_cc_pipeline_LightingFlow_proto = cls->getProto(); + __jsb_cc_pipeline_LightingFlow_class = cls; + + se::ScriptEngine::getInstance()->clearException(); + return true; +} + +se::Object* __jsb_cc_pipeline_LightingStage_proto = nullptr; +se::Class* __jsb_cc_pipeline_LightingStage_class = nullptr; + +static bool js_pipeline_LightingStage_getInitializeInfo(se::State& s) +{ + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc == 0) { + const cc::pipeline::RenderStageInfo& result = cc::pipeline::LightingStage::getInitializeInfo(); + ok &= nativevalue_to_se(result, s.rval(), nullptr /*ctx*/); + SE_PRECONDITION2(ok, false, "js_pipeline_LightingStage_getInitializeInfo : Error processing arguments"); + SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); + return true; + } + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0); + return false; +} +SE_BIND_FUNC(js_pipeline_LightingStage_getInitializeInfo) + +SE_DECLARE_FINALIZE_FUNC(js_cc_pipeline_LightingStage_finalize) + +static bool js_pipeline_LightingStage_constructor(se::State& s) // constructor.c +{ + cc::pipeline::LightingStage* cobj = JSB_ALLOC(cc::pipeline::LightingStage); + s.thisObject()->setPrivateData(cobj); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); + return true; +} +SE_BIND_CTOR(js_pipeline_LightingStage_constructor, __jsb_cc_pipeline_LightingStage_class, js_cc_pipeline_LightingStage_finalize) + + + +extern se::Object* __jsb_cc_pipeline_RenderStage_proto; + +static bool js_cc_pipeline_LightingStage_finalize(se::State& s) +{ + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(SE_THIS_OBJECT(s)); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) + { + se::NonRefNativePtrCreatedByCtorMap::erase(iter); + cc::pipeline::LightingStage* cobj = SE_THIS_OBJECT(s); + JSB_FREE(cobj); + } + return true; +} +SE_BIND_FINALIZE_FUNC(js_cc_pipeline_LightingStage_finalize) + +bool js_register_pipeline_LightingStage(se::Object* obj) +{ + auto cls = se::Class::create("LightingStage", obj, __jsb_cc_pipeline_RenderStage_proto, _SE(js_pipeline_LightingStage_constructor)); + + cls->defineStaticFunction("getInitializeInfo", _SE(js_pipeline_LightingStage_getInitializeInfo)); + cls->defineFinalizeFunction(_SE(js_cc_pipeline_LightingStage_finalize)); + cls->install(); + JSBClassType::registerClass(cls); + + __jsb_cc_pipeline_LightingStage_proto = cls->getProto(); + __jsb_cc_pipeline_LightingStage_class = cls; + + se::ScriptEngine::getInstance()->clearException(); + return true; +} + +se::Object* __jsb_cc_pipeline_PostprocessStage_proto = nullptr; +se::Class* __jsb_cc_pipeline_PostprocessStage_class = nullptr; + +SE_DECLARE_FINALIZE_FUNC(js_cc_pipeline_PostprocessStage_finalize) + +static bool js_pipeline_PostprocessStage_constructor(se::State& s) // constructor.c +{ + cc::pipeline::PostprocessStage* cobj = JSB_ALLOC(cc::pipeline::PostprocessStage); + s.thisObject()->setPrivateData(cobj); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); + return true; +} +SE_BIND_CTOR(js_pipeline_PostprocessStage_constructor, __jsb_cc_pipeline_PostprocessStage_class, js_cc_pipeline_PostprocessStage_finalize) + + + +extern se::Object* __jsb_cc_pipeline_RenderStage_proto; + +static bool js_cc_pipeline_PostprocessStage_finalize(se::State& s) +{ + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(SE_THIS_OBJECT(s)); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) + { + se::NonRefNativePtrCreatedByCtorMap::erase(iter); + cc::pipeline::PostprocessStage* cobj = SE_THIS_OBJECT(s); + JSB_FREE(cobj); + } + return true; +} +SE_BIND_FINALIZE_FUNC(js_cc_pipeline_PostprocessStage_finalize) + +bool js_register_pipeline_PostprocessStage(se::Object* obj) +{ + auto cls = se::Class::create("PostprocessStage", obj, __jsb_cc_pipeline_RenderStage_proto, _SE(js_pipeline_PostprocessStage_constructor)); + + cls->defineFinalizeFunction(_SE(js_cc_pipeline_PostprocessStage_finalize)); + cls->install(); + JSBClassType::registerClass(cls); + + __jsb_cc_pipeline_PostprocessStage_proto = cls->getProto(); + __jsb_cc_pipeline_PostprocessStage_class = cls; + + se::ScriptEngine::getInstance()->clearException(); + return true; +} + bool register_all_pipeline(se::Object* obj) { // Get the ns @@ -3395,21 +3709,27 @@ bool register_all_pipeline(se::Object* obj) } se::Object* ns = nsVal.toObject(); - js_register_pipeline_RenderQueueDesc(ns); - js_register_pipeline_RenderFlow(ns); - js_register_pipeline_PassView(ns); - js_register_pipeline_RenderStageInfo(ns); - js_register_pipeline_RenderPipeline(ns); - js_register_pipeline_ForwardPipeline(ns); - js_register_pipeline_RenderPipelineInfo(ns); - js_register_pipeline_Light(ns); js_register_pipeline_RenderStage(ns); + js_register_pipeline_PostprocessStage(ns); js_register_pipeline_ForwardStage(ns); - js_register_pipeline_ShadowStage(ns); - js_register_pipeline_RenderFlowInfo(ns); - js_register_pipeline_ForwardFlow(ns); + js_register_pipeline_GbufferStage(ns); js_register_pipeline_InstancedBuffer(ns); + js_register_pipeline_RenderStageInfo(ns); + js_register_pipeline_LightingStage(ns); + js_register_pipeline_ShadowStage(ns); + js_register_pipeline_RenderFlow(ns); + js_register_pipeline_ForwardFlow(ns); + js_register_pipeline_RenderPipeline(ns); + js_register_pipeline_DeferredPipeline(ns); + js_register_pipeline_GbufferFlow(ns); + js_register_pipeline_PassView(ns); + js_register_pipeline_ForwardPipeline(ns); + js_register_pipeline_Light(ns); + js_register_pipeline_RenderFlowInfo(ns); js_register_pipeline_RenderWindow(ns); + js_register_pipeline_RenderQueueDesc(ns); + js_register_pipeline_RenderPipelineInfo(ns); + js_register_pipeline_LightingFlow(ns); js_register_pipeline_ShadowFlow(ns); return true; } diff --git a/native/cocos/bindings/auto/jsb_pipeline_auto.h b/native/cocos/bindings/auto/jsb_pipeline_auto.h index ffdff6555d..9404b1884b 100644 --- a/native/cocos/bindings/auto/jsb_pipeline_auto.h +++ b/native/cocos/bindings/auto/jsb_pipeline_auto.h @@ -15,6 +15,12 @@ #include "cocos/renderer/pipeline/Define.h" #include "cocos/renderer/pipeline/helper/SharedMemory.h" #include "cocos/renderer/pipeline/InstancedBuffer.h" +#include "cocos/renderer/pipeline/deferred/DeferredPipeline.h" +#include "cocos/renderer/pipeline/deferred/GbufferFlow.h" +#include "cocos/renderer/pipeline/deferred/GbufferStage.h" +#include "cocos/renderer/pipeline/deferred/LightingFlow.h" +#include "cocos/renderer/pipeline/deferred/LightingStage.h" +#include "cocos/renderer/pipeline/deferred/PostprocessStage.h" extern se::Object* __jsb_cc_pipeline_RenderQueueDesc_proto; extern se::Class* __jsb_cc_pipeline_RenderQueueDesc_class; @@ -88,8 +94,10 @@ bool register_all_pipeline(se::Object* obj); JSB_REGISTER_OBJECT_TYPE(cc::pipeline::RenderPipeline); SE_DECLARE_FUNC(js_pipeline_RenderPipeline_activate); SE_DECLARE_FUNC(js_pipeline_RenderPipeline_destroy); +SE_DECLARE_FUNC(js_pipeline_RenderPipeline_getDevice); SE_DECLARE_FUNC(js_pipeline_RenderPipeline_initialize); SE_DECLARE_FUNC(js_pipeline_RenderPipeline_render); +SE_DECLARE_FUNC(js_pipeline_RenderPipeline_setPipelineSharedSceneData); SE_DECLARE_FUNC(js_pipeline_RenderPipeline_setValue); SE_DECLARE_FUNC(js_pipeline_RenderPipeline_getInstance); @@ -100,11 +108,6 @@ bool js_register_cc_pipeline_ForwardPipeline(se::Object* obj); bool register_all_pipeline(se::Object* obj); JSB_REGISTER_OBJECT_TYPE(cc::pipeline::ForwardPipeline); -SE_DECLARE_FUNC(js_pipeline_ForwardPipeline_getSphere); -SE_DECLARE_FUNC(js_pipeline_ForwardPipeline_setAmbient); -SE_DECLARE_FUNC(js_pipeline_ForwardPipeline_setFog); -SE_DECLARE_FUNC(js_pipeline_ForwardPipeline_setShadows); -SE_DECLARE_FUNC(js_pipeline_ForwardPipeline_setSkybox); SE_DECLARE_FUNC(js_pipeline_ForwardPipeline_ForwardPipeline); extern se::Object* __jsb_cc_pipeline_RenderFlowInfo_proto; @@ -156,6 +159,7 @@ bool register_all_pipeline(se::Object* obj); JSB_REGISTER_OBJECT_TYPE(cc::pipeline::RenderStage); SE_DECLARE_FUNC(js_pipeline_RenderStage_activate); +SE_DECLARE_FUNC(js_pipeline_RenderStage_getFlow); SE_DECLARE_FUNC(js_pipeline_RenderStage_getTag); SE_DECLARE_FUNC(js_pipeline_RenderStage_initialize); @@ -203,3 +207,61 @@ SE_DECLARE_FUNC(js_pipeline_InstancedBuffer_setDynamicOffset); SE_DECLARE_FUNC(js_pipeline_InstancedBuffer_get); SE_DECLARE_FUNC(js_pipeline_InstancedBuffer_InstancedBuffer); +extern se::Object* __jsb_cc_pipeline_DeferredPipeline_proto; +extern se::Class* __jsb_cc_pipeline_DeferredPipeline_class; + +bool js_register_cc_pipeline_DeferredPipeline(se::Object* obj); +bool register_all_pipeline(se::Object* obj); + +JSB_REGISTER_OBJECT_TYPE(cc::pipeline::DeferredPipeline); +SE_DECLARE_FUNC(js_pipeline_DeferredPipeline_DeferredPipeline); + +extern se::Object* __jsb_cc_pipeline_GbufferFlow_proto; +extern se::Class* __jsb_cc_pipeline_GbufferFlow_class; + +bool js_register_cc_pipeline_GbufferFlow(se::Object* obj); +bool register_all_pipeline(se::Object* obj); + +JSB_REGISTER_OBJECT_TYPE(cc::pipeline::GbufferFlow); +SE_DECLARE_FUNC(js_pipeline_GbufferFlow_getInitializeInfo); +SE_DECLARE_FUNC(js_pipeline_GbufferFlow_GbufferFlow); + +extern se::Object* __jsb_cc_pipeline_GbufferStage_proto; +extern se::Class* __jsb_cc_pipeline_GbufferStage_class; + +bool js_register_cc_pipeline_GbufferStage(se::Object* obj); +bool register_all_pipeline(se::Object* obj); + +JSB_REGISTER_OBJECT_TYPE(cc::pipeline::GbufferStage); +SE_DECLARE_FUNC(js_pipeline_GbufferStage_getInitializeInfo); +SE_DECLARE_FUNC(js_pipeline_GbufferStage_GbufferStage); + +extern se::Object* __jsb_cc_pipeline_LightingFlow_proto; +extern se::Class* __jsb_cc_pipeline_LightingFlow_class; + +bool js_register_cc_pipeline_LightingFlow(se::Object* obj); +bool register_all_pipeline(se::Object* obj); + +JSB_REGISTER_OBJECT_TYPE(cc::pipeline::LightingFlow); +SE_DECLARE_FUNC(js_pipeline_LightingFlow_getInitializeInfo); +SE_DECLARE_FUNC(js_pipeline_LightingFlow_LightingFlow); + +extern se::Object* __jsb_cc_pipeline_LightingStage_proto; +extern se::Class* __jsb_cc_pipeline_LightingStage_class; + +bool js_register_cc_pipeline_LightingStage(se::Object* obj); +bool register_all_pipeline(se::Object* obj); + +JSB_REGISTER_OBJECT_TYPE(cc::pipeline::LightingStage); +SE_DECLARE_FUNC(js_pipeline_LightingStage_getInitializeInfo); +SE_DECLARE_FUNC(js_pipeline_LightingStage_LightingStage); + +extern se::Object* __jsb_cc_pipeline_PostprocessStage_proto; +extern se::Class* __jsb_cc_pipeline_PostprocessStage_class; + +bool js_register_cc_pipeline_PostprocessStage(se::Object* obj); +bool register_all_pipeline(se::Object* obj); + +JSB_REGISTER_OBJECT_TYPE(cc::pipeline::PostprocessStage); +SE_DECLARE_FUNC(js_pipeline_PostprocessStage_PostprocessStage); + diff --git a/native/cocos/renderer/pipeline/Define.cpp b/native/cocos/renderer/pipeline/Define.cpp index 2a35acb2e1..1d658f3443 100644 --- a/native/cocos/renderer/pipeline/Define.cpp +++ b/native/cocos/renderer/pipeline/Define.cpp @@ -247,6 +247,81 @@ const gfx::UniformSampler SHADOWMAP::LAYOUT = { 1, }; +const String SAMPLERGBUFFERALBEDOMAP::NAME = "cc_gbuffer_albedoMap"; +const gfx::DescriptorSetLayoutBinding SAMPLERGBUFFERALBEDOMAP::DESCRIPTOR = { + SAMPLERGBUFFERALBEDOMAP::BINDING, + gfx::DescriptorType::SAMPLER, + 1, + gfx::ShaderStageFlagBit::FRAGMENT, +}; +const gfx::UniformSampler SAMPLERGBUFFERALBEDOMAP::LAYOUT = { + GLOBAL_SET, + SAMPLERGBUFFERALBEDOMAP::BINDING, + SAMPLERGBUFFERALBEDOMAP::NAME, + gfx::Type::SAMPLER2D, + 1, +}; + +const String SAMPLERGBUFFERPOSITIONMAP::NAME = "cc_gbuffer_positionMap"; +const gfx::DescriptorSetLayoutBinding SAMPLERGBUFFERPOSITIONMAP::DESCRIPTOR = { + SAMPLERGBUFFERPOSITIONMAP::BINDING, + gfx::DescriptorType::SAMPLER, + 1, + gfx::ShaderStageFlagBit::FRAGMENT, +}; +const gfx::UniformSampler SAMPLERGBUFFERPOSITIONMAP::LAYOUT = { + GLOBAL_SET, + SAMPLERGBUFFERPOSITIONMAP::BINDING, + SAMPLERGBUFFERPOSITIONMAP::NAME, + gfx::Type::SAMPLER2D, + 1, +}; + +const String SAMPLERGBUFFERNORMALMAP::NAME = "cc_gbuffer_normalMap"; +const gfx::DescriptorSetLayoutBinding SAMPLERGBUFFERNORMALMAP::DESCRIPTOR = { + SAMPLERGBUFFERNORMALMAP::BINDING, + gfx::DescriptorType::SAMPLER, + 1, + gfx::ShaderStageFlagBit::FRAGMENT, +}; +const gfx::UniformSampler SAMPLERGBUFFERNORMALMAP::LAYOUT = { + GLOBAL_SET, + SAMPLERGBUFFERNORMALMAP::BINDING, + SAMPLERGBUFFERNORMALMAP::NAME, + gfx::Type::SAMPLER2D, + 1, +}; + +const String SAMPLERGBUFFEREMISSIVEMAP::NAME = "cc_gbuffer_emissiveMap"; +const gfx::DescriptorSetLayoutBinding SAMPLERGBUFFEREMISSIVEMAP::DESCRIPTOR = { + SAMPLERGBUFFEREMISSIVEMAP::BINDING, + gfx::DescriptorType::SAMPLER, + 1, + gfx::ShaderStageFlagBit::FRAGMENT, +}; +const gfx::UniformSampler SAMPLERGBUFFEREMISSIVEMAP::LAYOUT = { + GLOBAL_SET, + SAMPLERGBUFFEREMISSIVEMAP::BINDING, + SAMPLERGBUFFEREMISSIVEMAP::NAME, + gfx::Type::SAMPLER2D, + 1, +}; + +const String SAMPLERLIGHTINGRESULTMAP::NAME = "cc_lighting_resultMap"; +const gfx::DescriptorSetLayoutBinding SAMPLERLIGHTINGRESULTMAP::DESCRIPTOR = { + SAMPLERLIGHTINGRESULTMAP::BINDING, + gfx::DescriptorType::SAMPLER, + 1, + gfx::ShaderStageFlagBit::FRAGMENT, +}; +const gfx::UniformSampler SAMPLERLIGHTINGRESULTMAP::LAYOUT = { + GLOBAL_SET, + SAMPLERLIGHTINGRESULTMAP::BINDING, + SAMPLERLIGHTINGRESULTMAP::NAME, + gfx::Type::SAMPLER2D, + 1, +}; + const String ENVIRONMENT::NAME = "cc_environment"; const gfx::DescriptorSetLayoutBinding ENVIRONMENT::DESCRIPTOR = { ENVIRONMENT::BINDING, diff --git a/native/cocos/renderer/pipeline/Define.h b/native/cocos/renderer/pipeline/Define.h index 6cf10c4394..db107937b0 100644 --- a/native/cocos/renderer/pipeline/Define.h +++ b/native/cocos/renderer/pipeline/Define.h @@ -175,6 +175,11 @@ enum class CC_DLL PipelineGlobalBindings { SAMPLER_SHADOWMAP, SAMPLER_ENVIRONMENT, // don't put this as the first sampler binding due to Mac GL driver issues: cubemap at texture unit 0 causes rendering issues SAMPLER_SPOT_LIGHTING_MAP, + SAMPLER_GBUFFER_ALBEDOMAP, + SAMPLER_GBUFFER_POSITIONMAP, + SAMPLER_GBUFFER_NORMALMAP, + SAMPLER_GBUFFER_EMISSIVEMAP, + SAMPLER_LIGHTING_RESULTMAP, COUNT, }; @@ -233,6 +238,10 @@ struct CC_DLL UBOForwardLight { static const String NAME; }; +struct CC_DLL UBODeferredLight { + static constexpr uint LIGHTS_PER_PASS = 10; +}; + struct CC_DLL UBOSkinningTexture { static constexpr uint JOINTS_TEXTURE_INFO_OFFSET = 0; static constexpr uint COUNT = UBOSkinningTexture::JOINTS_TEXTURE_INFO_OFFSET + 4; @@ -294,6 +303,21 @@ enum class CC_DLL RenderFlowTag { UI, }; +enum class CC_DLL DeferredStagePriority { + GBUFFER = 10, + LIGHTING = 15, + TRANSPARANT = 18, + POSTPROCESS = 19, + UI = 20 +}; + +enum class CC_DLL DeferredFlowPriority { + SHADOW = 0, + GBUFFER = 1, + LIGHTING = 5, + UI = 10 +}; + struct CC_DLL UBOGlobal : public Object { static constexpr uint TIME_OFFSET = 0; static constexpr uint SCREEN_SIZE_OFFSET = UBOGlobal::TIME_OFFSET + 4; @@ -400,6 +424,41 @@ struct CC_DLL SHADOWMAP : public Object { static const String NAME; }; +struct CC_DLL SAMPLERGBUFFERALBEDOMAP : public Object { + static constexpr uint BINDING = static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_ALBEDOMAP); + static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; + static const gfx::UniformSampler LAYOUT; + static const String NAME; +}; + +struct CC_DLL SAMPLERGBUFFERPOSITIONMAP : public Object { + static constexpr uint BINDING = static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_POSITIONMAP); + static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; + static const gfx::UniformSampler LAYOUT; + static const String NAME; +}; + +struct CC_DLL SAMPLERGBUFFERNORMALMAP : public Object { + static constexpr uint BINDING = static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_NORMALMAP); + static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; + static const gfx::UniformSampler LAYOUT; + static const String NAME; +}; + +struct CC_DLL SAMPLERGBUFFEREMISSIVEMAP : public Object { + static constexpr uint BINDING = static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_EMISSIVEMAP); + static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; + static const gfx::UniformSampler LAYOUT; + static const String NAME; +}; + +struct CC_DLL SAMPLERLIGHTINGRESULTMAP : public Object { + static constexpr uint BINDING = static_cast(PipelineGlobalBindings::SAMPLER_LIGHTING_RESULTMAP); + static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; + static const gfx::UniformSampler LAYOUT; + static const String NAME; +}; + struct CC_DLL ENVIRONMENT : public Object { static constexpr uint BINDING = static_cast(PipelineGlobalBindings::SAMPLER_ENVIRONMENT); static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; diff --git a/native/cocos/renderer/pipeline/PipelineUBO.cpp b/native/cocos/renderer/pipeline/PipelineUBO.cpp index 137f25e62e..bda398e3f8 100644 --- a/native/cocos/renderer/pipeline/PipelineUBO.cpp +++ b/native/cocos/renderer/pipeline/PipelineUBO.cpp @@ -69,7 +69,8 @@ void PipelineUBO::updateGlobalUBOView(const RenderPipeline *pipeline, std::array uboGlobalView[UBOGlobal::NATIVE_SIZE_OFFSET + 3] = 1.0f / uboGlobalView[UBOGlobal::NATIVE_SIZE_OFFSET + 1]; } -void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, std::array &bufferView, const Camera *camera) +void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, std::array &bufferView, + const Camera *camera, bool hasOffScreenAttachments) { const auto scene = camera->getScene(); const Light *mainLight = nullptr; @@ -136,17 +137,22 @@ void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, std::array memcpy(uboCameraView.data() + UBOCamera::MAT_VIEW_OFFSET, camera->matView.m, sizeof(cc::Mat4)); memcpy(uboCameraView.data() + UBOCamera::MAT_VIEW_INV_OFFSET, camera->getNode()->worldMatrix.m, sizeof(cc::Mat4)); - memcpy(uboCameraView.data() + UBOCamera::MAT_PROJ_OFFSET, camera->matProj.m, sizeof(cc::Mat4)); - memcpy(uboCameraView.data() + UBOCamera::MAT_PROJ_INV_OFFSET, camera->matProjInv.m, sizeof(cc::Mat4)); - memcpy(uboCameraView.data() + UBOCamera::MAT_VIEW_PROJ_OFFSET, camera->matViewProj.m, sizeof(cc::Mat4)); - memcpy(uboCameraView.data() + UBOCamera::MAT_VIEW_PROJ_INV_OFFSET, camera->matViewProjInv.m, sizeof(cc::Mat4)); TO_VEC3(uboCameraView, camera->position, UBOCamera::CAMERA_POS_OFFSET); - auto projectionSignY = device->getScreenSpaceSignY(); - if (camera->getWindow()->hasOffScreenAttachments) { - projectionSignY *= device->getUVSpaceSignY(); // need flipping if drawing on render targets + if (hasOffScreenAttachments) { + memcpy(uboCameraView.data() + UBOCamera::MAT_PROJ_OFFSET, camera->matProjOffscreen.m, sizeof(cc::Mat4)); + memcpy(uboCameraView.data() + UBOCamera::MAT_PROJ_INV_OFFSET, camera->matProjInvOffscreen.m, sizeof(cc::Mat4)); + memcpy(uboCameraView.data() + UBOCamera::MAT_VIEW_PROJ_OFFSET, camera->matViewProjOffscreen.m, sizeof(cc::Mat4)); + memcpy(uboCameraView.data() + UBOCamera::MAT_VIEW_PROJ_INV_OFFSET, camera->matViewProjInvOffscreen.m, sizeof(cc::Mat4)); + uboCameraView[UBOCamera::CAMERA_POS_OFFSET + 3] = device->getScreenSpaceSignY() * device->getUVSpaceSignY(); + } + else { + memcpy(uboCameraView.data() + UBOCamera::MAT_PROJ_OFFSET, camera->matProj.m, sizeof(cc::Mat4)); + memcpy(uboCameraView.data() + UBOCamera::MAT_PROJ_INV_OFFSET, camera->matProjInv.m, sizeof(cc::Mat4)); + memcpy(uboCameraView.data() + UBOCamera::MAT_VIEW_PROJ_OFFSET, camera->matViewProj.m, sizeof(cc::Mat4)); + memcpy(uboCameraView.data() + UBOCamera::MAT_VIEW_PROJ_INV_OFFSET, camera->matViewProjInv.m, sizeof(cc::Mat4)); + uboCameraView[UBOCamera::CAMERA_POS_OFFSET + 3] = device->getScreenSpaceSignY(); } - uboCameraView[UBOCamera::CAMERA_POS_OFFSET + 3] = projectionSignY; if (fog->enabled) { TO_VEC4(uboCameraView, fog->fogColor, UBOCamera::GLOBAL_FOG_COLOR_OFFSET); @@ -317,11 +323,11 @@ void PipelineUBO::updateGlobalUBO() cmdBuffer->updateBuffer(ds->getBuffer(UBOGlobal::BINDING), _globalUBO.data(), UBOGlobal::SIZE); } -void PipelineUBO::updateCameraUBO(const Camera *camera) +void PipelineUBO::updateCameraUBO(const Camera *camera, bool hasOffScreenAttachments) { const auto ds = _pipeline->getDescriptorSet(); const auto cmdBuffer = _pipeline->getCommandBuffers()[0]; - PipelineUBO::updateCameraUBOView(_pipeline, _cameraUBO, camera); + PipelineUBO::updateCameraUBOView(_pipeline, _cameraUBO, camera, hasOffScreenAttachments); cmdBuffer->updateBuffer(ds->getBuffer(UBOCamera::BINDING), _cameraUBO.data(), UBOCamera::SIZE); } diff --git a/native/cocos/renderer/pipeline/PipelineUBO.h b/native/cocos/renderer/pipeline/PipelineUBO.h index a19ab6f326..8d34b1b0c9 100644 --- a/native/cocos/renderer/pipeline/PipelineUBO.h +++ b/native/cocos/renderer/pipeline/PipelineUBO.h @@ -41,7 +41,7 @@ class Device; class CC_DLL PipelineUBO : public Object { public: static void updateGlobalUBOView(const RenderPipeline *pipeline, std::array &bufferView); - static void updateCameraUBOView(const RenderPipeline *pipeline, std::array &bufferView, const Camera *camera); + static void updateCameraUBOView(const RenderPipeline *pipeline, std::array &bufferView, const Camera *camera, bool hasOffScreenAttachments); static void updateShadowUBOView(const RenderPipeline *pipeline, std::array &bufferView, const Camera *camera); static void updateShadowUBOLightView(const RenderPipeline *pipeline, std::array &bufferView, const Light *light); @@ -51,7 +51,7 @@ public: void activate(gfx::Device *device, RenderPipeline *pipeline); void destroy(); void updateGlobalUBO(); - void updateCameraUBO(const Camera *camera); + void updateCameraUBO(const Camera *camera, bool hasOffScreenAttachments); void updateShadowUBO(const Camera *camera); void updateShadowUBOLight(const Light *light); void updateShadowUBORange(uint offset, const Mat4* data); diff --git a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp index 19ee2a483d..c40bb56935 100644 --- a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp +++ b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp @@ -323,7 +323,7 @@ void RenderAdditiveLightQueue::updateLightDescriptorSet(const Camera *camera, gf _shadowUBO.fill(0.0f); PipelineUBO::updateGlobalUBOView(_pipeline, _globalUBO); - PipelineUBO::updateCameraUBOView(_pipeline, _cameraUBO, camera); + PipelineUBO::updateCameraUBOView(_pipeline, _cameraUBO, camera, camera->getWindow()->hasOffScreenAttachments); switch (light->getType()) { case LightType::SPOT: { diff --git a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h index 9aacc92f88..24ce6feb87 100644 --- a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h +++ b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h @@ -62,7 +62,7 @@ public: private: void clear(); void updateUBOs(const Camera *camera, gfx::CommandBuffer *cmdBuffer); - void updateCameraUBO(const Camera *camera, gfx::CommandBuffer *cmdBuffer); + void updateCameraUBO(const Camera *camera, gfx::CommandBuffer *cmdBuffer, bool hasOffScreenAttachments); void updateLightDescriptorSet(const Camera *camera, gfx::CommandBuffer *cmdBuffer); void updateGlobalDescriptorSet(const Camera *camera, gfx::CommandBuffer *cmdBuffer); bool getLightPassIndex(const ModelView *model, vector &lightPassIndices) const; diff --git a/native/cocos/renderer/pipeline/RenderPipeline.cpp b/native/cocos/renderer/pipeline/RenderPipeline.cpp index 38a31d734b..b883e4c630 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.cpp +++ b/native/cocos/renderer/pipeline/RenderPipeline.cpp @@ -49,6 +49,12 @@ RenderPipeline::RenderPipeline() RenderPipeline::~RenderPipeline() { } +#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)); @@ -65,6 +71,12 @@ void RenderPipeline::setDescriptorSetLayout() { globalDescriptorSetLayout.samplers[SPOT_LIGHTING_MAP::NAME] = SPOT_LIGHTING_MAP::LAYOUT; globalDescriptorSetLayout.bindings[SPOT_LIGHTING_MAP::BINDING] = SPOT_LIGHTING_MAP::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; diff --git a/native/cocos/renderer/pipeline/RenderPipeline.h b/native/cocos/renderer/pipeline/RenderPipeline.h index 05f6b21f85..5dffd4f0d5 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.h +++ b/native/cocos/renderer/pipeline/RenderPipeline.h @@ -70,6 +70,7 @@ public: 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 gfx::Device *getDevice() { return _device; } protected: static RenderPipeline *_instance; diff --git a/native/cocos/renderer/pipeline/RenderStage.h b/native/cocos/renderer/pipeline/RenderStage.h index 1a1c90686d..9e2255d86b 100644 --- a/native/cocos/renderer/pipeline/RenderStage.h +++ b/native/cocos/renderer/pipeline/RenderStage.h @@ -59,6 +59,7 @@ public: CC_INLINE const String &getName() const { return _name; } CC_INLINE uint getPriority() const { return _priority; } CC_INLINE uint getTag() const { return _tag; } + CC_INLINE RenderFlow *getFlow() const {return _flow;} protected: RenderQueueDescList _renderQueueDescriptors; @@ -69,7 +70,7 @@ protected: String _name; uint _priority = 0; uint _tag = 0; - gfx::ColorList _clearColors = {{0, 0, 0, 1.0f}}; + gfx::ColorList _clearColors = {{0, 0, 0, 0.0f}, {0, 0, 0, 0.0f}, {0, 0, 0, 0.0f}, {0, 0, 0, 0.0f}}; }; } // namespace pipeline diff --git a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp new file mode 100644 index 0000000000..257e44352d --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp @@ -0,0 +1,326 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "DeferredPipeline.h" +#include "../helper/SharedMemory.h" +#include "GbufferFlow.h" +#include "LightingFlow.h" +#include "../shadow/ShadowFlow.h" +#include "../SceneCulling.h" +#include "gfx/GFXBuffer.h" +#include "gfx/GFXCommandBuffer.h" +#include "gfx/GFXDescriptorSet.h" +#include "gfx/GFXDevice.h" +#include "gfx/GFXQueue.h" +#include "gfx/GFXRenderPass.h" +#include "gfx/GFXTexture.h" +#include "gfx/GFXFramebuffer.h" +#include "gfx/GFXSampler.h" +#include "platform/Application.h" + +namespace cc { +namespace pipeline { +namespace { +#define TO_VEC3(dst, src, offset) \ + dst[offset] = src.x; \ + dst[offset + 1] = src.y; \ + dst[offset + 2] = src.z; +#define TO_VEC4(dst, src, offset) \ + dst[offset] = src.x; \ + dst[offset + 1] = src.y; \ + dst[offset + 2] = src.z; \ + dst[offset + 3] = src.w; +} // namespace + +gfx::RenderPass *DeferredPipeline::getOrCreateRenderPass(gfx::ClearFlags clearFlags) { + if (_renderPasses.find(clearFlags) != _renderPasses.end()) { + return _renderPasses[clearFlags]; + } + + auto device = gfx::Device::getInstance(); + gfx::ColorAttachment colorAttachment; + gfx::DepthStencilAttachment depthStencilAttachment; + colorAttachment.format = device->getColorFormat(); + depthStencilAttachment.format = device->getDepthStencilFormat(); + depthStencilAttachment.stencilStoreOp = gfx::StoreOp::DISCARD; + depthStencilAttachment.depthStoreOp = gfx::StoreOp::DISCARD; + + if (!(clearFlags & gfx::ClearFlagBit::COLOR)) { + if (clearFlags & static_cast(SKYBOX_FLAG)) { + colorAttachment.loadOp = gfx::LoadOp::DISCARD; + } else { + colorAttachment.loadOp = gfx::LoadOp::LOAD; + colorAttachment.beginLayout = gfx::TextureLayout::PRESENT_SRC; + } + } + + if (static_cast(clearFlags & gfx::ClearFlagBit::DEPTH_STENCIL) != gfx::ClearFlagBit::DEPTH_STENCIL) { + if (!(clearFlags & gfx::ClearFlagBit::DEPTH)) depthStencilAttachment.depthLoadOp = gfx::LoadOp::LOAD; + if (!(clearFlags & gfx::ClearFlagBit::STENCIL)) depthStencilAttachment.stencilLoadOp = gfx::LoadOp::LOAD; + depthStencilAttachment.beginLayout = gfx::TextureLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + } + + auto renderPass = device->createRenderPass({ + {colorAttachment}, + depthStencilAttachment, + }); + _renderPasses[clearFlags] = renderPass; + + return renderPass; +} + +bool DeferredPipeline::initialize(const RenderPipelineInfo &info) { + RenderPipeline::initialize(info); + + if (_flows.size() == 0) { + auto shadowFlow = CC_NEW(ShadowFlow); + shadowFlow->initialize(ShadowFlow::getInitializeInfo()); + _flows.emplace_back(shadowFlow); + + auto gbufferFlow = CC_NEW(GbufferFlow); + gbufferFlow->initialize(GbufferFlow::getInitializeInfo()); + _flows.emplace_back(gbufferFlow); + + auto lightingFlow = CC_NEW(LightingFlow); + lightingFlow->initialize(LightingFlow::getInitializeInfo()); + _flows.emplace_back(lightingFlow); + } + + return true; +} + +bool DeferredPipeline::activate() { + if (!RenderPipeline::activate()) { + CC_LOG_ERROR("RenderPipeline active failed."); + return false; + } + + if (!activeRenderer()) { + CC_LOG_ERROR("DeferredPipeline startup failed!"); + return false; + } + + return true; +} + +void DeferredPipeline::render(const vector &cameras) { + if (cameras.size() == 0) return; + _commandBuffers[0]->begin(); + _pipelineUBO->updateGlobalUBO(); + Camera *camera = GET_CAMERA(cameras[0]); + _pipelineUBO->updateCameraUBO(camera, true); + for (const auto flow : _flows) { + flow->render(camera); + } + _commandBuffers[0]->end(); + _device->getQueue()->submit(_commandBuffers); +} + +bool DeferredPipeline::createQuadInputAssembler(gfx::Buffer* &quadIB, gfx::Buffer* &quadVB, gfx::InputAssembler* &quadIA, gfx::SurfaceTransform surfaceTransform) { + // step 1 create vertex buffer + uint vbStride = sizeof(float) * 4; + uint vbSize = vbStride * 4; + quadVB = _device->createBuffer({gfx::BufferUsageBit::VERTEX | gfx::BufferUsageBit::TRANSFER_DST, + gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE, vbSize, vbStride}); + if (!quadVB) { + return false; + } + + float vbData[16] = {0}; + int n = 0; + switch (surfaceTransform) { + case (gfx::SurfaceTransform::IDENTITY): + n = 0; + vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = 0.0; vbData[n++] = 1.0; + vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = 1.0; + vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = 0.0; vbData[n++] = 0.0; + vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = 0.0; + break; + case (gfx::SurfaceTransform::ROTATE_90): + n = 0; + vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = 1.0; + vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = 0.0; + vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = 0.0; vbData[n++] = 1.0; + vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = 0.0; vbData[n++] = 0.0; + break; + case (gfx::SurfaceTransform::ROTATE_180): + n = 0; + vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = 0.0; vbData[n++] = 0.0; + vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = 0.0; + vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = 0.0; vbData[n++] = 1.0; + vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = 1.0; + break; + case (gfx::SurfaceTransform::ROTATE_270): + n = 0; + vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = 0.0; vbData[n++] = 0.0; + vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = 0.0; vbData[n++] = 1.0; + vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = 0.0; + vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = 1.0; + break; + default: + break; + } + + quadVB->update(vbData, sizeof(vbData)); + + // step 2 create index buffer + uint ibStride = 4; + uint ibSize = ibStride * 6; + + quadIB =_device->createBuffer({gfx::BufferUsageBit::INDEX | gfx::BufferUsageBit::TRANSFER_DST, + gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE, ibSize, ibStride}); + + if (!quadIB) { + return false; + } + + unsigned int ibData[] = {0, 1, 2, 1, 3, 2}; + quadIB->update(ibData, sizeof(ibData)); + + // step 3 create input assembler + gfx::InputAssemblerInfo info; + info.attributes.push_back({"a_position", gfx::Format::RG32F}); + info.attributes.push_back({"a_texCoord", gfx::Format::RG32F}); + info.vertexBuffers.push_back(quadVB); + info.indexBuffer = quadIB; + quadIA = _device->createInputAssembler(info); + if (!quadIA) { + return false; + } + + return true; +} + +gfx::Rect DeferredPipeline::getRenderArea(Camera *camera, bool onScreen) { + gfx::Rect renderArea; + uint w, h; + if (onScreen) { + w = camera->getWindow()->hasOnScreenAttachments && (uint)_device->getSurfaceTransform() % 2 ? camera->height : camera->width; + h = camera->getWindow()->hasOnScreenAttachments && (uint)_device->getSurfaceTransform() % 2 ? camera->width : camera->height; + } + else { + w = camera->width; + h = camera->height; + } + + renderArea.x = camera->viewportX * w; + renderArea.y = camera->viewportY * h; + renderArea.width = camera->viewportWidth * w * _pipelineSceneData->getSharedData()->shadingScale; + renderArea.height = camera->viewportHeight * h * _pipelineSceneData->getSharedData()->shadingScale; + return renderArea; +} + +void DeferredPipeline::destroyQuadInputAssembler() { + if (_quadIB) { + _quadIB->destroy(); + _quadIB = nullptr; + } + + if (_quadVBOnscreen) { + _quadVBOnscreen->destroy(); + _quadVBOnscreen = nullptr; + } + + if (_quadVBOffscreen) { + _quadVBOffscreen->destroy(); + _quadVBOffscreen = nullptr; + } + + if (_quadIAOnscreen) { + _quadIAOnscreen->destroy(); + _quadIAOnscreen = nullptr; + } + + if (_quadIAOffscreen) { + _quadIAOffscreen->destroy(); + _quadIAOffscreen = nullptr; + } +} + +bool DeferredPipeline::activeRenderer() { + _commandBuffers.push_back(_device->getCommandBuffer()); + const auto sharedData = _pipelineSceneData->getSharedData(); + + gfx::SamplerInfo info{ + gfx::Filter::LINEAR, + gfx::Filter::LINEAR, + gfx::Filter::NONE, + gfx::Address::CLAMP, + gfx::Address::CLAMP, + gfx::Address::CLAMP, + }; + const auto shadowMapSamplerHash = genSamplerHash(std::move(info)); + const auto shadowMapSampler = getSampler(shadowMapSamplerHash); + + // Main light sampler binding + this->_descriptorSet->bindSampler(SHADOWMAP::BINDING, shadowMapSampler); + this->_descriptorSet->bindTexture(SHADOWMAP::BINDING, getDefaultTexture()); + + // Spot light sampler binding + this->_descriptorSet->bindSampler(SPOT_LIGHTING_MAP::BINDING, shadowMapSampler); + this->_descriptorSet->bindTexture(SPOT_LIGHTING_MAP::BINDING, getDefaultTexture()); + + _descriptorSet->update(); + + // update global defines when all states initialized. + _macros.setValue("CC_USE_HDR", static_cast(sharedData->isHDR)); + _macros.setValue("CC_SUPPORT_FLOAT_TEXTURE", _device->hasFeature(gfx::Feature::TEXTURE_FLOAT)); + + if (!createQuadInputAssembler(_quadIB, _quadVBOffscreen, _quadIAOffscreen, gfx::SurfaceTransform::IDENTITY)) { + return false; + } + + if (!createQuadInputAssembler(_quadIB, _quadVBOnscreen, _quadIAOnscreen, _device->getSurfaceTransform())) { + return false; + } + + return true; +} + +void DeferredPipeline::destroy() { + destroyQuadInputAssembler(); + + if (_descriptorSet) { + _descriptorSet->getBuffer(UBOGlobal::BINDING)->destroy(); + _descriptorSet->getBuffer(UBOCamera::BINDING)->destroy(); + _descriptorSet->getBuffer(UBOShadow::BINDING)->destroy(); + _descriptorSet->getSampler(SHADOWMAP::BINDING)->destroy(); + _descriptorSet->getTexture(SHADOWMAP::BINDING)->destroy(); + _descriptorSet->getSampler(SPOT_LIGHTING_MAP::BINDING)->destroy(); + _descriptorSet->getTexture(SPOT_LIGHTING_MAP::BINDING)->destroy(); + } + + for (auto &it : _renderPasses) { + it.second->destroy(); + } + _renderPasses.clear(); + + _commandBuffers.clear(); + + RenderPipeline::destroy(); +} + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h new file mode 100644 index 0000000000..a2d1661322 --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h @@ -0,0 +1,96 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 + +#include "../RenderPipeline.h" +#include "../helper/SharedMemory.h" +#include "../../core/gfx/GFXBuffer.h" +#include "../../core/gfx/GFXInputAssembler.h" + +namespace cc { +namespace pipeline { +struct UBOGlobal; +struct UBOCamera; +struct UBOShadow; +struct Fog; +struct Ambient; +struct Skybox; +struct Shadows; +struct Sphere; +class Framebuffer; +class Camera; + +class CC_DLL DeferredPipeline : public RenderPipeline { +public: + DeferredPipeline() = default; + ~DeferredPipeline() = default; + + virtual bool initialize(const RenderPipelineInfo &info) override; + virtual void destroy() override; + virtual bool activate() override; + virtual void render(const vector &cameras) override; + + gfx::RenderPass *getOrCreateRenderPass(gfx::ClearFlags clearFlags); + + CC_INLINE gfx::Buffer *getLightsUBO() const { return _lightsUBO; } + CC_INLINE const LightList &getValidLights() const { return _validLights; } + CC_INLINE const gfx::BufferList &getLightBuffers() const { return _lightBuffers; } + CC_INLINE const UintList &getLightIndexOffsets() const { return _lightIndexOffsets; } + CC_INLINE const UintList &getLightIndices() const { return _lightIndices; } + gfx::InputAssembler *getQuadIAOnScreen(){return _quadIAOnscreen;} + gfx::InputAssembler *getQuadIAOffScreen(){return _quadIAOffscreen;} + void setDepth(gfx::Texture *tex) {_depth = tex;} + gfx::Texture *getDepth(){return _depth;} + gfx::Rect getRenderArea(Camera *view, bool onScreen); + +private: + bool activeRenderer(); + bool createQuadInputAssembler(gfx::Buffer* &quadIB, gfx::Buffer* &quadVB, gfx::InputAssembler* &quadIA, + gfx::SurfaceTransform surfaceTransform); + void destroyQuadInputAssembler(); + +private: + gfx::Buffer *_lightsUBO = nullptr; + LightList _validLights; + gfx::BufferList _lightBuffers; + UintList _lightIndexOffsets; + UintList _lightIndices; + map _renderPasses; + + // light stage + gfx::Buffer *_quadIB = nullptr; + gfx::Buffer *_quadVBOnscreen = nullptr; + gfx::Buffer *_quadVBOffscreen = nullptr; + gfx::InputAssembler *_quadIAOnscreen = nullptr; + gfx::InputAssembler *_quadIAOffscreen = nullptr; + + gfx::Texture *_depth = nullptr; +}; + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/deferred/GbufferFlow.cpp b/native/cocos/renderer/pipeline/deferred/GbufferFlow.cpp new file mode 100644 index 0000000000..072f7795ba --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/GbufferFlow.cpp @@ -0,0 +1,189 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "GbufferFlow.h" +#include "DeferredPipeline.h" +#include "GbufferStage.h" +#include "../SceneCulling.h" +#include "../../core/gfx/GFXDevice.h" +#include "../../core/gfx/GFXDescriptorSet.h" +#include "../../core/gfx/GFXSampler.h" +#include "gfx/GFXRenderPass.h" + +namespace cc { +namespace pipeline { +RenderFlowInfo GbufferFlow::_initInfo = { + "GbufferFlow", + static_cast(DeferredFlowPriority::GBUFFER), + static_cast(RenderFlowTag::SCENE), + {}, +}; +const RenderFlowInfo &GbufferFlow::getInitializeInfo() { return GbufferFlow::_initInfo; } + +GbufferFlow::~GbufferFlow() { +} + +bool GbufferFlow::initialize(const RenderFlowInfo &info) { + RenderFlow::initialize(info); + + if (_stages.size() == 0) { + GbufferStage *gbufferStage = CC_NEW(GbufferStage); + gbufferStage->initialize(GbufferStage::getInitializeInfo()); + _stages.emplace_back(gbufferStage); + } + + return true; +} + +void GbufferFlow::createRenderPass(gfx::Device *device) { + if (_gbufferRenderPass != nullptr) { + return; + } + + gfx::RenderPassInfo info; + + gfx::ColorAttachment color = { + gfx::Format::RGBA16F, + 1, + gfx::LoadOp::CLEAR, + gfx::StoreOp::STORE, + gfx::TextureLayout::UNDEFINED, + gfx::TextureLayout::COLOR_ATTACHMENT_OPTIMAL + }; + + for (int i = 0; i < 4; i++) { + info.colorAttachments.push_back(color); + } + + gfx::DepthStencilAttachment depth = { + device->getDepthStencilFormat(), + 1, + gfx::LoadOp::CLEAR, + gfx::StoreOp::STORE, + gfx::LoadOp::CLEAR, + gfx::StoreOp::STORE, + gfx::TextureLayout::UNDEFINED, + gfx::TextureLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL + }; + + info.depthStencilAttachment = depth; + _gbufferRenderPass = device->createRenderPass(info); +} + +void GbufferFlow::createRenderTargets(gfx::Device *device) { + gfx::TextureInfo info = { + gfx::TextureType::TEX2D, + gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED, + gfx::Format::RGBA16F, + _width, + _height, + }; + + if (_gbufferRenderTargets.empty()) { + for (int i = 0; i < 4; i++) { + gfx::Texture *tex = device->createTexture(info); + _gbufferRenderTargets.push_back(tex); + } + } + + if (!_depth) { + info.usage = gfx::TextureUsageBit::DEPTH_STENCIL_ATTACHMENT; + info.format = device->getDepthStencilFormat(); + _depth = device->createTexture(info); + + DeferredPipeline *pp = dynamic_cast(_pipeline); + pp->setDepth(_depth); + } + + if (!_gbufferFrameBuffer) { + gfx::FramebufferInfo fbInfo = { + _gbufferRenderPass, + _gbufferRenderTargets, + _depth, + }; + + _gbufferFrameBuffer = device->createFramebuffer(fbInfo); + } +} + +void GbufferFlow::activate(RenderPipeline *pipeline) { + RenderFlow::activate(pipeline); + + gfx::Device *device = pipeline->getDevice(); + if (device->getSurfaceTransform() == gfx::SurfaceTransform::IDENTITY || + device->getSurfaceTransform() == gfx::SurfaceTransform::ROTATE_180) { + _width = device->getWidth(); + _height = device->getHeight(); + } + else { + _width = device->getHeight(); + _height = device->getWidth(); + } + + createRenderPass(device); + createRenderTargets(device); + + pipeline->getDescriptorSet()->bindTexture( + static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_ALBEDOMAP), _gbufferFrameBuffer->getColorTextures()[0]); + pipeline->getDescriptorSet()->bindTexture( + static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_POSITIONMAP), _gbufferFrameBuffer->getColorTextures()[1]); + pipeline->getDescriptorSet()->bindTexture( + static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_NORMALMAP), _gbufferFrameBuffer->getColorTextures()[2]); + pipeline->getDescriptorSet()->bindTexture( + static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_EMISSIVEMAP), _gbufferFrameBuffer->getColorTextures()[3]); + + gfx::SamplerInfo sInfo = { + gfx::Filter::LINEAR, + gfx::Filter::LINEAR, + gfx::Filter::NONE, + gfx::Address::CLAMP, + gfx::Address::CLAMP, + gfx::Address::CLAMP, + }; + + uint hash = genSamplerHash(sInfo); + gfx::Sampler *gbufferSampler = getSampler(hash); + pipeline->getDescriptorSet()->bindSampler( + static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_ALBEDOMAP), gbufferSampler); + pipeline->getDescriptorSet()->bindSampler( + static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_POSITIONMAP), gbufferSampler); + pipeline->getDescriptorSet()->bindSampler( + static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_NORMALMAP), gbufferSampler); + pipeline->getDescriptorSet()->bindSampler( + static_cast(PipelineGlobalBindings::SAMPLER_GBUFFER_EMISSIVEMAP), gbufferSampler); +} + +void GbufferFlow::render(Camera *camera) { + auto pipeline = static_cast(_pipeline); + sceneCulling(pipeline, camera); + RenderFlow::render(camera); +} + +void GbufferFlow::destroy() { + RenderFlow::destroy(); +} + +} // namespace pipeline +} // namespace cc \ No newline at end of file diff --git a/native/cocos/renderer/pipeline/deferred/GbufferFlow.h b/native/cocos/renderer/pipeline/deferred/GbufferFlow.h new file mode 100644 index 0000000000..8321d0d06f --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/GbufferFlow.h @@ -0,0 +1,67 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "../RenderFlow.h" + +namespace cc { +namespace pipeline { + +class Camera; +class GbufferStage; + +class CC_DLL GbufferFlow : public RenderFlow { +public: + static const RenderFlowInfo &getInitializeInfo(); + + GbufferFlow() = default; + virtual ~GbufferFlow(); + + virtual bool initialize(const RenderFlowInfo &info) override; + virtual void activate(RenderPipeline *pipeline) override; + virtual void destroy() override; + virtual void render(Camera *camera) override; + gfx::Framebuffer *getFrameBuffer() {return _gbufferFrameBuffer;} + +private: + void createRenderPass(gfx::Device *device); + void createRenderTargets(gfx::Device *device); + +private: + static RenderFlowInfo _initInfo; + + gfx::RenderPass *_gbufferRenderPass = nullptr; + gfx::TextureList _gbufferRenderTargets; + gfx::Framebuffer *_gbufferFrameBuffer = nullptr; + gfx::Texture *_depth = nullptr; + uint _width; + uint _height; + + GbufferStage *_GbufferStage = nullptr; +}; + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp b/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp new file mode 100644 index 0000000000..c0a8979d45 --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp @@ -0,0 +1,184 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "GbufferStage.h" +#include "GbufferFlow.h" +#include "../BatchedBuffer.h" +#include "../InstancedBuffer.h" +#include "../PlanarShadowQueue.h" +#include "../RenderBatchedQueue.h" +#include "../RenderInstancedQueue.h" +#include "../RenderQueue.h" +#include "../helper/SharedMemory.h" +#include "DeferredPipeline.h" +#include "gfx/GFXCommandBuffer.h" +#include "gfx/GFXDevice.h" +#include "gfx/GFXFramebuffer.h" +#include "gfx/GFXQueue.h" + +namespace cc { +namespace pipeline { +namespace { +void SRGBToLinear(gfx::Color &out, const gfx::Color &gamma) { + out.x = gamma.x * gamma.x; + out.y = gamma.y * gamma.y; + out.z = gamma.z * gamma.z; +} + +void LinearToSRGB(gfx::Color &out, const gfx::Color &linear) { + out.x = std::sqrt(linear.x); + out.y = std::sqrt(linear.y); + out.z = std::sqrt(linear.z); +} +} // namespace + +RenderStageInfo GbufferStage::_initInfo = { + "GbufferStage", + static_cast(DeferredStagePriority::GBUFFER), + static_cast(RenderFlowTag::SCENE), + {{false, RenderQueueSortMode::FRONT_TO_BACK, {"default"}}, + {true, RenderQueueSortMode::BACK_TO_FRONT, {"default", "planarShadow"}}}}; +const RenderStageInfo &GbufferStage::getInitializeInfo() { return GbufferStage::_initInfo; } + +GbufferStage::GbufferStage() : RenderStage() { + _batchedQueue = CC_NEW(RenderBatchedQueue); + _instancedQueue = CC_NEW(RenderInstancedQueue); +} + +GbufferStage::~GbufferStage() { +} + +bool GbufferStage::initialize(const RenderStageInfo &info) { + RenderStage::initialize(info); + _renderQueueDescriptors = info.renderQueues; + _phaseID = getPhaseID("deferred-gbuffer"); + return true; +} + +void GbufferStage::activate(RenderPipeline *pipeline, RenderFlow *flow) { + RenderStage::activate(pipeline, flow); + for (const auto &descriptor : _renderQueueDescriptors) { + uint phase = 0; + for (const auto &stage : descriptor.stages) { + phase |= getPhaseID(stage); + } + + std::function sortFunc = opaqueCompareFn; + switch (descriptor.sortMode) { + case RenderQueueSortMode::BACK_TO_FRONT: + sortFunc = transparentCompareFn; + break; + case RenderQueueSortMode::FRONT_TO_BACK: + sortFunc = opaqueCompareFn; + default: + break; + } + + RenderQueueCreateInfo info = {descriptor.isTransparent, phase, sortFunc}; + _renderQueues.emplace_back(CC_NEW(RenderQueue(std::move(info)))); + } + + _planarShadowQueue = CC_NEW(PlanarShadowQueue(_pipeline)); +} + +void GbufferStage::destroy() { + CC_SAFE_DELETE(_batchedQueue); + CC_SAFE_DELETE(_instancedQueue); + CC_SAFE_DELETE(_planarShadowQueue); + RenderStage::destroy(); +} + +void GbufferStage::render(Camera *camera) { + _instancedQueue->clear(); + _batchedQueue->clear(); + auto pipeline = static_cast(_pipeline); + const auto &renderObjects = _pipeline->getPipelineSceneData()->getRenderObjects(); + if (renderObjects.empty()) { + return; + } + + for (auto queue : _renderQueues) { + queue->clear(); + } + + uint m = 0, p = 0; + size_t k = 0; + for (size_t i = 0; i < renderObjects.size(); ++i) { + const auto &ro = renderObjects[i]; + const auto model = ro.model; + const auto subModelID = model->getSubModelID(); + const auto subModelCount = subModelID[0]; + for (m = 1; m <= subModelCount; ++m) { + auto subModel = model->getSubModelView(subModelID[m]); + for (p = 0; p < subModel->passCount; ++p) { + const PassView *pass = subModel->getPassView(p); + + if (pass->phase != _phaseID) continue; + if (pass->getBatchingScheme() == BatchingSchemes::INSTANCING) { + auto instancedBuffer = InstancedBuffer::get(subModel->passID[p]); + instancedBuffer->merge(model, subModel, p); + _instancedQueue->add(instancedBuffer); + } else if (pass->getBatchingScheme() == BatchingSchemes::VB_MERGING) { + auto batchedBuffer = BatchedBuffer::get(subModel->passID[p]); + batchedBuffer->merge(subModel, p, model); + _batchedQueue->add(batchedBuffer); + } else { + for (k = 0; k < _renderQueues.size(); k++) { + _renderQueues[k]->insertRenderPass(ro, m, p); + } + } + } + } + } + for (auto queue : _renderQueues) { + queue->sort(); + } + + auto cmdBuff = pipeline->getCommandBuffers()[0]; + + _instancedQueue->uploadBuffers(cmdBuff); + _batchedQueue->uploadBuffers(cmdBuff); + + // render area is not oriented + _renderArea = pipeline->getRenderArea(camera, false); + + GbufferFlow *flow = dynamic_cast(getFlow()); + assert(flow != nullptr); + auto framebuffer = flow->getFrameBuffer(); + auto renderPass = framebuffer->getRenderPass(); + + cmdBuff->beginRenderPass(renderPass, framebuffer, _renderArea, _clearColors, camera->clearDepth, camera->clearStencil); + cmdBuff->bindDescriptorSet(GLOBAL_SET, _pipeline->getDescriptorSet()); + + _renderQueues[0]->recordCommandBuffer(_device, renderPass, cmdBuff); + _instancedQueue->recordCommandBuffer(_device, renderPass, cmdBuff); + _batchedQueue->recordCommandBuffer(_device, renderPass, cmdBuff); + //_renderQueues[1]->recordCommandBuffer(_device, renderPass, cmdBuff); + + cmdBuff->endRenderPass(); +} + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/deferred/GbufferStage.h b/native/cocos/renderer/pipeline/deferred/GbufferStage.h new file mode 100644 index 0000000000..4afd61c75a --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/GbufferStage.h @@ -0,0 +1,63 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "../RenderStage.h" +#include "../../core/gfx/GFXFramebuffer.h" + +namespace cc { +namespace pipeline { + +class RenderFlow; +class Camera; +class RenderBatchedQueue; +class RenderInstancedQueue; +class RenderAdditiveLightQueue; +class PlanarShadowQueue; + +class CC_DLL GbufferStage : public RenderStage { +public: + static const RenderStageInfo &getInitializeInfo(); + + GbufferStage(); + ~GbufferStage(); + + virtual bool initialize(const RenderStageInfo &info) override; + virtual void activate(RenderPipeline *pipeline, RenderFlow *flow) override; + virtual void destroy() override; + virtual void render(Camera *camera) override; + +private: + static RenderStageInfo _initInfo; + PlanarShadowQueue *_planarShadowQueue = nullptr; + RenderBatchedQueue *_batchedQueue = nullptr; + RenderInstancedQueue *_instancedQueue = nullptr; + gfx::Rect _renderArea; + uint _phaseID = 0; +}; + +} // namespace pipeline +} // namespace cc \ No newline at end of file diff --git a/native/cocos/renderer/pipeline/deferred/LightingFlow.cpp b/native/cocos/renderer/pipeline/deferred/LightingFlow.cpp new file mode 100644 index 0000000000..37f24cfbbe --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/LightingFlow.cpp @@ -0,0 +1,168 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "LightingFlow.h" +#include "DeferredPipeline.h" +#include "LightingStage.h" +#include "../SceneCulling.h" +#include "gfx/GFXDevice.h" +#include "gfx/GFXDescriptorSet.h" +#include "gfx/GFXFramebuffer.h" +#include "gfx/GFXDevice.h" + +namespace cc { +namespace pipeline { +RenderFlowInfo LightingFlow::_initInfo = { + "LightingFlow", + static_cast(DeferredFlowPriority::LIGHTING), + static_cast(RenderFlowTag::SCENE), + {}, +}; +const RenderFlowInfo &LightingFlow::getInitializeInfo() { return LightingFlow::_initInfo; } + +LightingFlow::~LightingFlow() { +} + +bool LightingFlow::initialize(const RenderFlowInfo &info) { + RenderFlow::initialize(info); + + if (_stages.size() == 0) { + auto stage = CC_NEW(LightingStage); + stage->initialize(LightingStage::getInitializeInfo()); + _stages.emplace_back(stage); + } + + return true; +} + +void LightingFlow::createRenderPass(gfx::Device *device) { + if (_lightingRenderPass == nullptr) { + gfx::ColorAttachment cAttch = { + gfx::Format::RGBA16F, + 1, + gfx::LoadOp::CLEAR, + gfx::StoreOp::STORE, + gfx::TextureLayout::UNDEFINED, + gfx::TextureLayout::COLOR_ATTACHMENT_OPTIMAL + }; + gfx::RenderPassInfo info; + info.colorAttachments.push_back(cAttch); + info.depthStencilAttachment = { + device->getDepthStencilFormat(), + 1, + gfx::LoadOp::LOAD, + gfx::StoreOp::DISCARD, + gfx::LoadOp::DISCARD, + gfx::StoreOp::DISCARD, + gfx::TextureLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + gfx::TextureLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL + }; + + _lightingRenderPass = device->createRenderPass(info); + assert(_lightingRenderPass != nullptr); + } +} + +void LightingFlow::createFrameBuffer(gfx::Device *device) { + if (_lightingRenderTarget == nullptr) { + gfx::TextureInfo rtInfo = { + gfx::TextureType::TEX2D, + gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED, + gfx::Format::RGBA16F, + _width, + _height, + }; + _lightingRenderTarget = device->createTexture(rtInfo); + } + + if (!_lightingFrameBuff) { + gfx::FramebufferInfo fbInfo; + fbInfo.renderPass = _lightingRenderPass; + fbInfo.colorTextures.push_back(_lightingRenderTarget); + fbInfo.depthStencilTexture = _depth; + _lightingFrameBuff = device->createFramebuffer(fbInfo); + assert(_lightingFrameBuff != nullptr); + } +} + +void LightingFlow::activate(RenderPipeline *pipeline) { + RenderFlow::activate(pipeline); + if (_lightingRenderPass != nullptr) { + return; + } + + const auto device = pipeline->getDevice(); + if (device->getSurfaceTransform() == gfx::SurfaceTransform::IDENTITY || + device->getSurfaceTransform() == gfx::SurfaceTransform::ROTATE_180) { + _width = device->getWidth(); + _height = device->getHeight(); + } + else { + _width = device->getHeight(); + _height = device->getWidth(); + } + + DeferredPipeline *pp = dynamic_cast(pipeline); + assert(pp != nullptr); + _depth = pp->getDepth(); + + // create renderpass + createRenderPass(device); + + // create framebuffer + createFrameBuffer(device); + + // bind sampler and texture, used in postprocess + pipeline->getDescriptorSet()->bindTexture( + static_cast(PipelineGlobalBindings::SAMPLER_LIGHTING_RESULTMAP), + _lightingFrameBuff->getColorTextures()[0]); + + gfx::SamplerInfo spInfo = { + gfx::Filter::LINEAR, + gfx::Filter::LINEAR, + gfx::Filter::NONE, + gfx::Address::CLAMP, + gfx::Address::CLAMP, + gfx::Address::CLAMP + }; + + auto spHash = genSamplerHash(spInfo); + gfx::Sampler *copySampler = getSampler(spHash); + pipeline->getDescriptorSet()->bindSampler( + static_cast(PipelineGlobalBindings::SAMPLER_LIGHTING_RESULTMAP), + copySampler); +} + +void LightingFlow::render(Camera *camera) { + auto pipeline = dynamic_cast(_pipeline); + RenderFlow::render(camera); +} + +void LightingFlow::destroy() { + RenderFlow::destroy(); +} + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/deferred/LightingFlow.h b/native/cocos/renderer/pipeline/deferred/LightingFlow.h new file mode 100644 index 0000000000..b5cc6d217e --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/LightingFlow.h @@ -0,0 +1,63 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "../RenderFlow.h" + +namespace cc { +namespace pipeline { + +class RenderView; + +class CC_DLL LightingFlow : public RenderFlow { +public: + static const RenderFlowInfo &getInitializeInfo(); + + LightingFlow() = default; + virtual ~LightingFlow(); + + virtual bool initialize(const RenderFlowInfo &info) override; + virtual void activate(RenderPipeline *pipeline) override; + virtual void destroy() override; + virtual void render(Camera *camera) override; + + void createRenderPass(gfx::Device *device); + void createFrameBuffer(gfx::Device *device); + gfx::Framebuffer *getLightingFrameBuffer(){return _lightingFrameBuff;} + +private: + static RenderFlowInfo _initInfo; + + gfx::RenderPass *_lightingRenderPass = nullptr; + gfx::Texture *_lightingRenderTarget = nullptr; + gfx::Texture *_depth = nullptr; + gfx::Framebuffer *_lightingFrameBuff = nullptr; + uint _width; + uint _height; +}; + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp new file mode 100644 index 0000000000..5b88b93a5e --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp @@ -0,0 +1,369 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "LightingStage.h" +#include "LightingFlow.h" +#include "../BatchedBuffer.h" +#include "../InstancedBuffer.h" +#include "../PlanarShadowQueue.h" +#include "../RenderBatchedQueue.h" +#include "../RenderInstancedQueue.h" +#include "../RenderQueue.h" +#include "../helper/SharedMemory.h" +#include "DeferredPipeline.h" +#include "gfx/GFXCommandBuffer.h" +#include "gfx/GFXDevice.h" +#include "gfx/GFXFramebuffer.h" +#include "gfx/GFXQueue.h" +#include "gfx/GFXDescriptorSet.h" +#include "../PipelineStateManager.h" + +namespace cc { +namespace pipeline { +namespace { +void SRGBToLinear(gfx::Color &out, const gfx::Color &gamma) { + out.x = gamma.x * gamma.x; + out.y = gamma.y * gamma.y; + out.z = gamma.z * gamma.z; +} + +void LinearToSRGB(gfx::Color &out, const gfx::Color &linear) { + out.x = std::sqrt(linear.x); + out.y = std::sqrt(linear.y); + out.z = std::sqrt(linear.z); +} +} // namespace + +RenderStageInfo LightingStage::_initInfo = { + "LightingStage", + static_cast(DeferredStagePriority::LIGHTING), + static_cast(RenderFlowTag::SCENE), + {{false, RenderQueueSortMode::FRONT_TO_BACK, {"default"}}, + {true, RenderQueueSortMode::BACK_TO_FRONT, {"default", "planarShadow"}}}}; + +const RenderStageInfo &LightingStage::getInitializeInfo() { return LightingStage::_initInfo; } + +LightingStage::LightingStage() : RenderStage() { +} + +LightingStage::~LightingStage() { + _deferredLitsBufs->destroy(); + _deferredLitsBufs = nullptr; + _deferredLitsBufView->destroy(); + _deferredLitsBufView = nullptr; +} + +bool LightingStage::initialize(const RenderStageInfo &info) { + RenderStage::initialize(info); + _renderQueueDescriptors = info.renderQueues; + _phaseID = getPhaseID("deferred-lighting"); + _transparentPhaseID = getPhaseID("deferred-transparent"); + return true; +} + +void LightingStage::gatherLights(Camera *camera) { + DeferredPipeline *pipeline = dynamic_cast(_pipeline); + if (!pipeline) { + return; + } + + const auto sceneData = _pipeline->getPipelineSceneData(); + const auto sharedData = sceneData->getSharedData(); + + gfx::CommandBuffer *cmdBuf = pipeline->getCommandBuffers()[0]; + auto scene = camera->getScene(); + const auto sphereLightArrayID = scene->getSphereLightArrayID(); + auto sphereCount = sphereLightArrayID ? sphereLightArrayID[0] : 0; + const auto spotLightArrayID = scene->getSpotLightArrayID(); + auto spotCount = spotLightArrayID ? spotLightArrayID[0] : 0; + + Sphere sphere; + auto exposure = camera->exposure; + int idx = 0; + int elementLen = sizeof(cc::Vec4) / sizeof(float); + int fieldLen = elementLen * _maxDeferredLights; + uint offset = 0; + cc::Vec4 tmpArray; + + for (int i = 1; i <= sphereCount && idx < _maxDeferredLights; i++, idx++) { + const auto light = scene->getSphereLight(sphereLightArrayID[i]); + sphere.setCenter(light->position); + sphere.setRadius(light->range); + if (!sphere_frustum(&sphere, camera->getFrustum())) { + continue; + } + // position + offset = idx * elementLen; + _lightBufferData[offset] = light->position.x; + _lightBufferData[offset + 1] = light->position.y; + _lightBufferData[offset + 2] = light->position.z; + _lightBufferData[offset + 3] = 0; + + // color + offset = idx * elementLen + fieldLen; + tmpArray.set(light->color.x, light->color.y, light->color.z, 0); + if (light->useColorTemperature) { + tmpArray.x *= light->colorTemperatureRGB.x; + tmpArray.y *= light->colorTemperatureRGB.y; + tmpArray.z *= light->colorTemperatureRGB.z; + } + + if (sharedData->isHDR) { + tmpArray.w = light->luminance * sharedData->fpScale * _lightMeterScale; + } else { + tmpArray.w = light->luminance * exposure * _lightMeterScale; + } + + _lightBufferData[offset + 0] = tmpArray.x; + _lightBufferData[offset + 1] = tmpArray.y; + _lightBufferData[offset + 2] = tmpArray.z; + _lightBufferData[offset + 3] = tmpArray.w; + + // size range angle + offset = idx * elementLen + fieldLen * 2; + _lightBufferData[offset] = light->size; + _lightBufferData[offset + 1] = light->range; + _lightBufferData[offset + 2] = 0; + } + + for (int i = 1; i <= spotCount && idx < _maxDeferredLights; i++, idx++) { + const auto light = scene->getSpotLight(spotLightArrayID[i]); + sphere.setCenter(light->position); + sphere.setRadius(light->range); + if (!sphere_frustum(&sphere, camera->getFrustum())) { + continue; + } + // position + offset = idx * elementLen; + _lightBufferData[offset] = light->position.x; + _lightBufferData[offset + 1] = light->position.y; + _lightBufferData[offset + 2] = light->position.z; + _lightBufferData[offset + 3] = 1; + + // color + offset = idx * elementLen + fieldLen; + tmpArray.set(light->color.x, light->color.y, light->color.z, 0); + if (light->useColorTemperature) { + tmpArray.x *= light->colorTemperatureRGB.x; + tmpArray.y *= light->colorTemperatureRGB.y; + tmpArray.z *= light->colorTemperatureRGB.z; + } + + if (sharedData->isHDR) { + tmpArray.w = light->luminance * sharedData->fpScale * _lightMeterScale; + } else { + tmpArray.w = light->luminance * exposure * _lightMeterScale; + } + + _lightBufferData[offset + 0] = tmpArray.x; + _lightBufferData[offset + 1] = tmpArray.y; + _lightBufferData[offset + 2] = tmpArray.z; + _lightBufferData[offset + 3] = tmpArray.w; + + // size range angle + offset = idx * elementLen + fieldLen * 2; + _lightBufferData[offset] = light->size; + _lightBufferData[offset + 1] = light->range; + _lightBufferData[offset + 2] = light->spotAngle; + + // dir + offset = idx * elementLen + fieldLen * 3; + _lightBufferData[offset] = light->direction.x; + _lightBufferData[offset + 1] = light->direction.y; + _lightBufferData[offset + 2] = light->direction.z; + } + + // the count of lights is set to cc_lightDir[0].w + _lightBufferData[fieldLen * 3 + 3] = idx; + cmdBuf->updateBuffer(_deferredLitsBufs, _lightBufferData.data()); +} + +void LightingStage::initLightingBuffer() { + const auto device = _pipeline->getDevice(); + + // color/pos/dir/angle 都是vec4存储, 最后一个vec4只要x存储光源个数 + uint totalSize = sizeof(Vec4) * 4 * _maxDeferredLights; + totalSize = std::ceil((float)totalSize / device->getUboOffsetAlignment()) * device->getUboOffsetAlignment(); + + // create lighting buffer and view + if (_deferredLitsBufs == nullptr) { + gfx::BufferInfo bfInfo = { + gfx::BufferUsageBit::UNIFORM | gfx::BufferUsageBit::TRANSFER_DST, + gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE, + totalSize, + static_cast(device->getUboOffsetAlignment()), + }; + _deferredLitsBufs = device->createBuffer(bfInfo); + assert(_deferredLitsBufs != nullptr); + } + + if (_deferredLitsBufView == nullptr) { + gfx::BufferViewInfo bvInfo = {_deferredLitsBufs, 0, totalSize}; + _deferredLitsBufView = device->createBuffer(bvInfo); + assert(_deferredLitsBufView != nullptr); + _descriptorSet->bindBuffer(static_cast(ModelLocalBindings::UBO_FORWARD_LIGHTS), _deferredLitsBufView); + } + + _lightBufferData.resize(totalSize / sizeof(float)); +} + +void LightingStage::activate(RenderPipeline *pipeline, RenderFlow *flow) { + RenderStage::activate(pipeline, flow); + + const auto device = pipeline->getDevice(); + + // create descriptorset/layout + gfx::DescriptorSetLayoutInfo layoutInfo = {localDescriptorSetLayout.bindings}; + _descLayout = device->createDescriptorSetLayout(layoutInfo); + + gfx::DescriptorSetInfo setInfo = {_descLayout}; + _descriptorSet = device->createDescriptorSet(setInfo); + + // create lighting buffer and view + initLightingBuffer(); + + for (const auto &descriptor : _renderQueueDescriptors) { + uint phase = 0; + for (const auto &stage : descriptor.stages) { + phase |= getPhaseID(stage); + } + + std::function sortFunc = opaqueCompareFn; + switch (descriptor.sortMode) { + case RenderQueueSortMode::BACK_TO_FRONT: + sortFunc = transparentCompareFn; + break; + case RenderQueueSortMode::FRONT_TO_BACK: + sortFunc = opaqueCompareFn; + default: + break; + } + + RenderQueueCreateInfo info = {descriptor.isTransparent, phase, sortFunc}; + _renderQueues.emplace_back(CC_NEW(RenderQueue(std::move(info)))); + } + + _planarShadowQueue = CC_NEW(PlanarShadowQueue(_pipeline)); +} + +void LightingStage::destroy() { + CC_SAFE_DELETE(_planarShadowQueue); + RenderStage::destroy(); +} + +void LightingStage::render(Camera *camera) { + auto pipeline = static_cast(_pipeline); + const auto sceneData = _pipeline->getPipelineSceneData(); + const auto sharedData = sceneData->getSharedData(); + + auto cmdBuff = pipeline->getCommandBuffers()[0]; + + // lighting info + gatherLights(camera); + _descriptorSet->update(); + + vector dynamicOffsets = {0}; + cmdBuff->bindDescriptorSet(static_cast(SetIndex::LOCAL), _descriptorSet, dynamicOffsets); + + // draw quad + gfx::Rect renderArea = pipeline->getRenderArea(camera, false); + + gfx::Color clearColor = {0.0, 0.0, 0.0, 1.0}; + if (camera->clearFlag | static_cast( gfx::ClearFlagBit::COLOR)) { + if (sharedData->isHDR) { + SRGBToLinear(clearColor, camera->clearColor); + const auto scale = sharedData->fpScale / camera->exposure; + clearColor.x *= scale; + clearColor.y *= scale; + clearColor.z *= scale; + } else { + clearColor = camera->clearColor; + } + } + + clearColor.w = camera->clearColor.w; + + LightingFlow *flow = dynamic_cast(_flow); + assert(flow != nullptr); + + auto frameBuffer = flow->getLightingFrameBuffer(); + auto renderPass = frameBuffer->getRenderPass(); + + cmdBuff->beginRenderPass(renderPass, frameBuffer, renderArea, &clearColor, + camera->clearDepth, camera->clearStencil); + + cmdBuff->bindDescriptorSet(static_cast(SetIndex::GLOBAL), pipeline->getDescriptorSet()); + + // get pso and draw quad + PassView *pass = sceneData->getSharedData()->getDeferredLightPass(); + gfx::Shader *shader = sceneData->getSharedData()->getDeferredLightPassShader(); + + gfx::InputAssembler* inputAssembler = pipeline->getQuadIAOffScreen(); + gfx::PipelineState *pState =PipelineStateManager::getOrCreatePipelineState( + pass, shader, inputAssembler, renderPass); + assert(pState != nullptr); + + cmdBuff->bindPipelineState(pState); + cmdBuff->bindInputAssembler(inputAssembler); + cmdBuff->draw(inputAssembler); + + // planerQueue + _planarShadowQueue->recordCommandBuffer(_device, renderPass, cmdBuff); + + // transparent + for (auto queue : _renderQueues) { + queue->clear(); + } + const auto &renderObjects = sceneData->getRenderObjects(); + + uint m = 0, p = 0; + size_t k = 0; + for (size_t i = 0; i < renderObjects.size(); ++i) { + const auto &ro = renderObjects[i]; + const auto model = ro.model; + const auto subModelID = model->getSubModelID(); + const auto subModelCount = subModelID[0]; + for (m = 1; m <= subModelCount; ++m) { + auto subModel = model->getSubModelView(subModelID[m]); + for (p = 0; p < subModel->passCount; ++p) { + const PassView *pass = subModel->getPassView(p); + + if (pass->phase != _transparentPhaseID) continue; + for (k = 0; k < _renderQueues.size(); k++) { + _renderQueues[k]->insertRenderPass(ro, m, p); + } + } + } + } + for (auto queue : _renderQueues) { + queue->sort(); + queue->recordCommandBuffer(_device, renderPass, cmdBuff); + } + + cmdBuff->endRenderPass(); +} + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/deferred/LightingStage.h b/native/cocos/renderer/pipeline/deferred/LightingStage.h new file mode 100644 index 0000000000..cadbc2c02f --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/LightingStage.h @@ -0,0 +1,76 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "../RenderStage.h" + +namespace cc { +namespace pipeline { + +class RenderFlow; +class Camera; +class RenderBatchedQueue; +class RenderInstancedQueue; +class RenderAdditiveLightQueue; +class PlanarShadowQueue; + +class CC_DLL LightingStage : public RenderStage { +public: + static const RenderStageInfo &getInitializeInfo(); + + LightingStage(); + ~LightingStage(); + + virtual bool initialize(const RenderStageInfo &info) override; + virtual void activate(RenderPipeline *pipeline, RenderFlow *flow) override; + virtual void destroy() override; + virtual void render(Camera *camera) override; + + void initLightingBuffer(); + +private: + void gatherLights(Camera *camera); + +private: + static RenderStageInfo _initInfo; + PlanarShadowQueue *_planarShadowQueue = nullptr; + gfx::Rect _renderArea; + uint _phaseID = 0; + uint _transparentPhaseID = 0; + + gfx::Buffer *_deferredLitsBufs = nullptr; + gfx::Buffer *_deferredLitsBufView = nullptr; + std::vector _lightBufferData; + uint _lightBufferStride = 0; + uint _lightBufferElementCount = 0; + float _lightMeterScale = 10000.0; + gfx::DescriptorSet *_descriptorSet = nullptr; + gfx::DescriptorSetLayout *_descLayout = nullptr; + uint _maxDeferredLights = UBODeferredLight::LIGHTS_PER_PASS; +}; + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/deferred/PostprocessStage.cpp b/native/cocos/renderer/pipeline/deferred/PostprocessStage.cpp new file mode 100644 index 0000000000..168efdb69e --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/PostprocessStage.cpp @@ -0,0 +1,90 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "PostprocessStage.h" +#include "DeferredPipeline.h" +#include "gfx/GFXFramebuffer.h" +#include "gfx/GFXCommandBuffer.h" +#include "../helper/SharedMemory.h" +#include "../PipelineStateManager.h" +#include "gfx/GFXDevice.h" + +namespace cc { +namespace pipeline { +RenderStageInfo PostprocessStage::_initInfo = { + "PostprocessStage", + static_cast(DeferredStagePriority::POSTPROCESS), + 0, +}; + +bool PostprocessStage::initialize(const RenderStageInfo &info) { + RenderStage::initialize(info); + return true; +} + +void PostprocessStage::activate(RenderPipeline *pipeline, RenderFlow *flow) { + RenderStage::activate(pipeline, flow); +} + +void PostprocessStage::destroy() { +} + +void PostprocessStage::render(Camera *camera) { + DeferredPipeline *pp = dynamic_cast(_pipeline); + assert(pp != nullptr); + gfx::Device *device = pp->getDevice(); + gfx::CommandBuffer *cmdBf = pp->getCommandBuffers()[0]; + + _pipeline->getPipelineUBO()->updateCameraUBO(camera, camera->getWindow()->hasOffScreenAttachments); + gfx::Rect renderArea = pp->getRenderArea(camera, !camera->getWindow()->hasOffScreenAttachments); + + gfx::Color color = {0, 0, 0, 1}; + gfx::ColorList clst; + clst.push_back(color); + + gfx::Framebuffer *fb = camera->getWindow()->getFramebuffer(); + gfx::RenderPass *rp = fb->getRenderPass() ? + fb->getRenderPass() : pp->getOrCreateRenderPass(static_cast(camera->clearFlag)); + + cmdBf->beginRenderPass(rp, fb, renderArea, clst, camera->clearDepth, camera->clearStencil); + cmdBf->bindDescriptorSet(static_cast(SetIndex::GLOBAL), pp->getDescriptorSet()); + + // post proces + const auto sceneData = _pipeline->getPipelineSceneData(); + PassView *pv = sceneData->getSharedData()->getDeferredPostPass(); + gfx::Shader *sd = sceneData->getSharedData()->getDeferredPostPassShader(); + + gfx::InputAssembler *ia = camera->getWindow()->hasOffScreenAttachments ? pp->getQuadIAOffScreen() : pp->getQuadIAOnScreen(); + + gfx::PipelineState *pso = PipelineStateManager::getOrCreatePipelineState(pv, sd, ia, rp); + assert(pso != nullptr); + + cmdBf->bindPipelineState(pso); + cmdBf->bindInputAssembler(ia); + cmdBf->draw(ia); + cmdBf->endRenderPass(); +} +} // namespace pipeline +} // namespace cc \ No newline at end of file diff --git a/native/cocos/renderer/pipeline/deferred/PostprocessStage.h b/native/cocos/renderer/pipeline/deferred/PostprocessStage.h new file mode 100644 index 0000000000..89aadc2fc3 --- /dev/null +++ b/native/cocos/renderer/pipeline/deferred/PostprocessStage.h @@ -0,0 +1,50 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. + + https://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 "../RenderStage.h" + +namespace cc { +namespace pipeline { + +class Camera; + +class CC_DLL PostprocessStage : public RenderStage { +public: + PostprocessStage() {}; + ~PostprocessStage() {}; + + virtual bool initialize(const RenderStageInfo &info) override; + virtual void activate(RenderPipeline *pipeline, RenderFlow *flow) override; + virtual void destroy() override; + virtual void render(Camera *camera) override; + +private: + gfx::Rect _renderArea; + static RenderStageInfo _initInfo; +}; +} // namespace pipeline +} // namespace cc \ No newline at end of file diff --git a/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp b/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp index 97bfd227e4..8aa30e3a32 100644 --- a/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp +++ b/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp @@ -122,7 +122,7 @@ void ForwardPipeline::render(const vector &cameras) { _pipelineUBO->updateGlobalUBO(); for (const auto cameraId : cameras) { Camera *camera = GET_CAMERA(cameraId); - _pipelineUBO->updateCameraUBO(camera); + _pipelineUBO->updateCameraUBO(camera, camera->getWindow()->hasOffScreenAttachments); for (const auto flow : _flows) { flow->render(camera); } diff --git a/native/cocos/renderer/pipeline/helper/SharedMemory.h b/native/cocos/renderer/pipeline/helper/SharedMemory.h index d8445c6b9b..72ef3c64c7 100644 --- a/native/cocos/renderer/pipeline/helper/SharedMemory.h +++ b/native/cocos/renderer/pipeline/helper/SharedMemory.h @@ -379,6 +379,10 @@ struct CC_DLL Camera { cc::Mat4 matViewProjInv; cc::Mat4 matProj; cc::Mat4 matProjInv; + cc::Mat4 matViewProjOffscreen; + cc::Mat4 matViewProjInvOffscreen; + cc::Mat4 matProjOffscreen; + cc::Mat4 matProjInvOffscreen; CC_INLINE const Node *getNode() const { return GET_NODE(nodeID); } CC_INLINE const Scene *getScene() const { return GET_SCENE(sceneID); } @@ -481,12 +485,20 @@ struct CC_DLL PipelineSharedSceneData { uint32_t isHDR = 0; uint32_t shadingScale = 0; uint32_t fpScale = 0; + uint32_t deferredLightPass = 0; + uint32_t deferredLightPassShader = 0; + uint32_t deferredPostPass = 0; + uint32_t deferredPostPassShader = 0; CC_INLINE Shadows* getShadows() const {return GET_SHADOWS(shadow);} CC_INLINE Skybox* getSkybox() const {return GET_SKYBOX(skybox);} CC_INLINE Ambient* getAmbient() const {return GET_AMBIENT(ambient);} CC_INLINE Fog* getFog() const {return GET_FOG(fog);} - + CC_INLINE PassView* getDeferredLightPass() const {return GET_PASS(deferredLightPass);} + CC_INLINE gfx::Shader* getDeferredLightPassShader() const {return GET_SHADER(deferredLightPassShader);} + CC_INLINE PassView* getDeferredPostPass() const {return GET_PASS(deferredPostPass);} + CC_INLINE gfx::Shader* getDeferredPostPassShader() const {return GET_SHADER(deferredPostPassShader);} + const static se::PoolType type; }; diff --git a/native/tools/tojs/pipeline.ini b/native/tools/tojs/pipeline.ini index 05ee174d80..39d87a6fc6 100644 --- a/native/tools/tojs/pipeline.ini +++ b/native/tools/tojs/pipeline.ini @@ -26,13 +26,13 @@ cxxgenerator_headers = extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s # what headers to parse -headers = %(cocosdir)s/cocos/renderer/pipeline/forward/ForwardPipeline.h %(cocosdir)s/cocos/renderer/pipeline/forward/ForwardFlow.h %(cocosdir)s/cocos/renderer/pipeline/forward/ForwardStage.h %(cocosdir)s/cocos/renderer/pipeline/shadow/ShadowFlow.h %(cocosdir)s/cocos/renderer/pipeline/shadow/ShadowStage.h %(cocosdir)s/cocos/renderer/pipeline/RenderPipeline.h %(cocosdir)s/cocos/renderer/pipeline/RenderFlow.h %(cocosdir)s/cocos/renderer/pipeline/RenderStage.h %(cocosdir)s/cocos/renderer/pipeline/Define.h %(cocosdir)s/cocos/renderer/pipeline/helper/SharedMemory.h %(cocosdir)s/cocos/renderer/pipeline/InstancedBuffer.h +headers = %(cocosdir)s/cocos/renderer/pipeline/forward/ForwardPipeline.h %(cocosdir)s/cocos/renderer/pipeline/forward/ForwardFlow.h %(cocosdir)s/cocos/renderer/pipeline/forward/ForwardStage.h %(cocosdir)s/cocos/renderer/pipeline/shadow/ShadowFlow.h %(cocosdir)s/cocos/renderer/pipeline/shadow/ShadowStage.h %(cocosdir)s/cocos/renderer/pipeline/RenderPipeline.h %(cocosdir)s/cocos/renderer/pipeline/RenderFlow.h %(cocosdir)s/cocos/renderer/pipeline/RenderStage.h %(cocosdir)s/cocos/renderer/pipeline/Define.h %(cocosdir)s/cocos/renderer/pipeline/helper/SharedMemory.h %(cocosdir)s/cocos/renderer/pipeline/InstancedBuffer.h %(cocosdir)s/cocos/renderer/pipeline/deferred/DeferredPipeline.h %(cocosdir)s/cocos/renderer/pipeline/deferred/GbufferFlow.h %(cocosdir)s/cocos/renderer/pipeline/deferred/GbufferStage.h %(cocosdir)s/cocos/renderer/pipeline/deferred/LightingFlow.h %(cocosdir)s/cocos/renderer/pipeline/deferred/LightingStage.h %(cocosdir)s/cocos/renderer/pipeline/deferred/PostprocessStage.h hpp_headers = cocos/bindings/auto/jsb_gfx_auto.h # what classes to produce code for. You can use regular expressions here. When testing the regular # expression, it will be enclosed in "^$", like this: "^Menu*$". -classes = RenderPipeline ForwardPipeline ForwardFlow ForwardStage ShadowFlow ShadowStage RenderPipelineInfo RenderFlowInfo RenderStageInfo RenderQueueDesc RenderFlow RenderStage RenderWindow InstancedBuffer Light PassView +classes = RenderPipeline ForwardPipeline ForwardFlow ForwardStage ShadowFlow ShadowStage RenderPipelineInfo RenderFlowInfo RenderStageInfo RenderQueueDesc RenderFlow RenderStage RenderWindow InstancedBuffer Light PassView DeferredPipeline GbufferStage GbufferFlow LightingStage LightingFlow PostprocessStage # what should we skip? in the format ClassName::[function function] # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also @@ -48,9 +48,15 @@ skip = ForwardPipeline::[getOrCreateRenderPass getLightsUBO getValidLights getLi ForwardStage::[initialize activate destroy render], ShadowFlow::[initialize activate destroy render], ShadowStage::[initialize activate destroy render], - InstancedBuffer::[merge uploadBuffers clear getInstances getPass hasPendingModels dynamicOffsets] + InstancedBuffer::[merge uploadBuffers clear getInstances getPass hasPendingModels dynamicOffsets], + DeferredPipeline::[getOrCreateRenderPass getLightsUBO getValidLights getLightBuffers getLightIndexOffsets getLightIndices getQuadIAOnScreen getQuadIAOffScreen setDepth getDepth getRenderArea createQuadInputAssembler destroyQuadInputAssembler], + GbufferFlow::[initialize activate destroy render getFrameBuffer createRenderPass createRenderTargets], + GbufferStage::[initialize activate destroy render], + LightingFlow::[initialize activate destroy render createRenderPass createFrameBuffer getLightingFrameBuffer], + LightingStage::[initialize activate destroy render initLightingBuffer gatherLights], + PostprocessStage::[initialize activate destroy render] -rename_functions = +rename_functions = getter_setter= RenderPipeline::[descriptorSet descriptorSetLayout] @@ -60,7 +66,7 @@ rename_classes = remove_prefix = # classes for which there will be no "parent" lookup -classes_have_no_parents = +classes_have_no_parents = # base classes which will be skipped when their sub-classes found them. base_classes_to_skip = Clonable Object