From aa009ac26bb4790b9bd9866d5b96f57fccc0a224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E6=88=98?= <40414978+PatriceJiang@users.noreply.github.com> Date: Fri, 16 Jul 2021 15:20:23 +0800 Subject: [PATCH] 3.3 js message queue (#3731) --- native/CMakeLists.txt | 1 + native/cocos/bindings/jswrapper/v8/Object.cpp | 2 + .../bindings/manual/jsb_scene_manual.cpp | 6 + .../bindings/manual/jsb_scene_manual_ext.cpp | 170 ++++++++++++++++++ .../pipeline/deferred/DeferredPipeline.cpp | 4 +- .../pipeline/forward/ForwardPipeline.cpp | 21 ++- native/cocos/scene/RenderScene.cpp | 4 + native/tools/tojs/scene.ini | 3 +- 8 files changed, 197 insertions(+), 14 deletions(-) create mode 100644 native/cocos/bindings/manual/jsb_scene_manual_ext.cpp diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index fc1fb31f69..70166024b3 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -1646,6 +1646,7 @@ cocos_source_files( cocos/bindings/manual/jsb_module_register.h cocos/bindings/manual/jsb_platform.h NO_WERROR cocos/bindings/manual/jsb_scene_manual.cpp + NO_WERROR cocos/bindings/manual/jsb_scene_manual_ext.cpp cocos/bindings/manual/jsb_scene_manual.h NO_WERROR cocos/bindings/manual/jsb_network_manual.cpp cocos/bindings/manual/jsb_network_manual.h diff --git a/native/cocos/bindings/jswrapper/v8/Object.cpp b/native/cocos/bindings/jswrapper/v8/Object.cpp index 5cfbbc22e1..17cd2bb3e7 100644 --- a/native/cocos/bindings/jswrapper/v8/Object.cpp +++ b/native/cocos/bindings/jswrapper/v8/Object.cpp @@ -458,6 +458,7 @@ void Object::setPrivateData(void *data) { internal::setPrivate(__isolate, _obj, data, &_internalData); NativePtrToObjectMap::emplace(data, this); _privateData = data; + setProperty("__native_ptr__", se::Value(static_cast(reinterpret_cast(data)))); } void *Object::getPrivateData() const { @@ -473,6 +474,7 @@ void Object::clearPrivateData(bool clearMapping) { NativePtrToObjectMap::erase(_privateData); } internal::clearPrivate(__isolate, _obj); + setProperty("__native_ptr__", se::Value(static_cast(reinterpret_cast(nullptr)))); _privateData = nullptr; } } diff --git a/native/cocos/bindings/manual/jsb_scene_manual.cpp b/native/cocos/bindings/manual/jsb_scene_manual.cpp index bd28caea30..3bf50cc8ae 100644 --- a/native/cocos/bindings/manual/jsb_scene_manual.cpp +++ b/native/cocos/bindings/manual/jsb_scene_manual.cpp @@ -36,6 +36,8 @@ #define JSB_FREE(ptr) delete ptr #endif +extern bool register_all_drawbatch2d_ext_manual(se::Object* obj); //NOLINT + static bool js_scene_Pass_setRootBufferAndBlock(se::State& s) // NOLINT(readability-identifier-naming) { auto* cobj = SE_THIS_OBJECT(s); @@ -239,5 +241,9 @@ bool register_all_scene_manual(se::Object* obj) // NOLINT(readability-identifier __jsb_cc_scene_SubModel_proto->defineFunction("setSubMeshBuffers", _SE(js_scene_SubModel_setSubMeshBuffers)); __jsb_cc_scene_Pass_proto->defineFunction("setRootBufferAndBlock", _SE(js_scene_Pass_setRootBufferAndBlock)); __jsb_cc_scene_RenderScene_proto->defineFunction("updateBatches", _SE(js_scene_RenderScene_updateBatches)); + + // Impl MQ for DrawBatch2D + register_all_drawbatch2d_ext_manual(obj); + return true; } diff --git a/native/cocos/bindings/manual/jsb_scene_manual_ext.cpp b/native/cocos/bindings/manual/jsb_scene_manual_ext.cpp new file mode 100644 index 0000000000..2d30f9cf59 --- /dev/null +++ b/native/cocos/bindings/manual/jsb_scene_manual_ext.cpp @@ -0,0 +1,170 @@ + +#include "bindings/auto/jsb_scene_auto.h" +#include "jsb_scene_manual.h" +#include "scene/Model.h" +#include "scene/Node.h" + +namespace { + +template +struct alignas(uint32_t) AlignedPtr { + char _data[8]; + T * get() { + return reinterpret_cast(static_cast(*reinterpret_cast(this))); + } +}; + +void fastSetFlag(void *buffer) { + struct Heap { + AlignedPtr selfPtr; + uint32_t flags; + }; + Heap *heap = reinterpret_cast(buffer); + heap->selfPtr.get()->visFlags = heap->flags; +} + +void fastSetDescriptorSet(void *buffer) { + struct Heap { + AlignedPtr selfPtr; + AlignedPtr dsPtr; + }; + Heap *heap = reinterpret_cast(buffer); + heap->selfPtr.get()->descriptorSet = heap->dsPtr.get(); +} + +void fastSetInputAssembler(void *buffer) { + struct Heap { + AlignedPtr selfPtr; + AlignedPtr inputAssemblerPtr; + }; + Heap *heap = reinterpret_cast(buffer); + heap->selfPtr.get()->inputAssembler = heap->inputAssemblerPtr.get(); +} + +void fastSetPasses(void *buffer) { + struct Heap { + AlignedPtr selfPtr; + uint32_t passSize; + AlignedPtr passes[0]; + }; + static_assert(offsetof(Heap, passes) == 12); + + Heap *heap = reinterpret_cast(buffer); + auto &passes = heap->selfPtr.get()->passes; + passes.resize(heap->passSize); + for (auto i = 0; i < heap->passSize; i++) { + passes[i] = heap->passes[i].get(); + } +} + +void fastSetShaders(void *buffer) { + struct Heap { + AlignedPtr selfPtr; + uint32_t shaderSize; + AlignedPtr shaders[0]; + }; + Heap *heap = reinterpret_cast(buffer); + auto &shaders = heap->selfPtr.get()->shaders; + shaders.resize(heap->shaderSize); + for (auto i = 0; i < heap->shaderSize; i++) { + shaders[i] = heap->shaders[i].get(); + } +} + +template +uint64_t convertPtr(F *in) { + return static_cast(reinterpret_cast(in)); +} +bool mqInitialized{false}; +se::Object * msgQueue{nullptr}; +se::Object * globalThis{nullptr}; +constexpr size_t BUFFER_SIZE{1 * 1024 * 1024}; // 1MB + +} // namespace + +/** + * Run all commands in __fastMQ__. This function should be call before render logic, + * which sync data to native objects. + */ +void jsbFlushFastMQ() { + if (!mqInitialized) { + return; + } + se::AutoHandleScope scope; + se::Value maxIdx; + se::Value currentArrayBuffer; + se::Object * arrayBufferObj; + + globalThis->getProperty("__fastMQIdx__", &maxIdx); + auto maxSize = maxIdx.toUint32(); + uint8_t *mqPtr{nullptr}; + + for (auto i = 0; i <= maxSize; i++) { + msgQueue->getArrayElement(i, ¤tArrayBuffer); + arrayBufferObj = currentArrayBuffer.toObject(); + arrayBufferObj->getArrayBufferData(&mqPtr, nullptr); + auto *u32Ptr = reinterpret_cast(mqPtr); + auto offset = *u32Ptr; + auto commands = *(u32Ptr + 1); + assert(offset < BUFFER_SIZE); + if (commands == 0) return; + + using FastFunction = void (*)(void *); + + uint8_t *p = mqPtr + 8; + for (uint32_t i = 0; i < commands; i++) { + auto * base = reinterpret_cast(p); + uint32_t len = *base; + auto * fnAddr = reinterpret_cast(base + 1); + auto * fn = reinterpret_cast(fnAddr); + (*fn)(p + 12); + p += len; + } + // reset + *u32Ptr = 8; + *(u32Ptr + 1) = 0; + } + globalThis->setProperty("__fastMQIdx__", se::Value(0)); +} + +bool register_all_drawbatch2d_ext_manual(se::Object *obj) { //NOLINT + // allocate global message queue + + se::AutoHandleScope scope; + auto * msgArrayBuffer = se::Object::createArrayBufferObject(nullptr, BUFFER_SIZE); + globalThis = se::ScriptEngine::getInstance()->getGlobalObject(); + msgQueue = se::Object::createArrayObject(1); + { + uint8_t *data{nullptr}; + msgArrayBuffer->getArrayBufferData(&data, nullptr); + auto *int32Data = reinterpret_cast(data); + int32Data[0] = 8; + int32Data[1] = 0; + } + msgQueue->setArrayElement(0, se::Value(msgArrayBuffer)); + globalThis->setProperty("__fastMQ__", se::Value(msgQueue)); + globalThis->setProperty("__fastMQIdx__", se::Value(0)); + + mqInitialized = true; + + // register function table, serialize to queue + + se::Value dbJSValue; + se::Object *nsObj{nullptr}; + + se::Value nsValue; + obj->getProperty("ns", &nsValue); + nsObj = nsValue.toObject(); + + nsObj->getProperty("DrawBatch2D", &dbJSValue); + se::Object *dbJSObj = dbJSValue.toObject(); + se::Object *fnTable = se::Object::createPlainObject(); + fnTable->setProperty("visFlags", se::Value(convertPtr(fastSetFlag))); + fnTable->setProperty("descriptorSet", se::Value(convertPtr(fastSetDescriptorSet))); + fnTable->setProperty("inputAssembler", se::Value(convertPtr(fastSetInputAssembler))); + fnTable->setProperty("passes", se::Value(convertPtr(fastSetPasses))); + fnTable->setProperty("shaders", se::Value(convertPtr(fastSetShaders))); + dbJSObj->setProperty("fnTable", se::Value(fnTable)); + + return true; +} diff --git a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp index 3146d9a8c6..3e9f0983bd 100644 --- a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp +++ b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp @@ -221,7 +221,7 @@ bool DeferredPipeline::createQuadInputAssembler(gfx::Buffer **quadIB, gfx::Buffe gfx::Rect DeferredPipeline::getRenderArea(scene::Camera *camera) { gfx::Rect renderArea; - + uint w = camera->window->hasOnScreenAttachments && static_cast(_device->getSurfaceTransform()) % 2 ? camera->height : camera->width; uint h = camera->window->hasOnScreenAttachments && static_cast(_device->getSurfaceTransform()) % 2 ? camera->width : camera->height; @@ -266,7 +266,7 @@ bool DeferredPipeline::activeRenderer() { {}, {}, }; - const uint samplerHash = SamplerLib::genSamplerHash(info); + const uint samplerHash = SamplerLib::genSamplerHash(info); gfx::Sampler *const sampler = SamplerLib::getSampler(samplerHash); // Main light sampler binding diff --git a/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp b/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp index d1579adbed..3faa4c4313 100644 --- a/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp +++ b/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp @@ -24,9 +24,9 @@ ****************************************************************************/ #include "ForwardPipeline.h" +#include "../SceneCulling.h" #include "../shadow/ShadowFlow.h" #include "ForwardFlow.h" -#include "../SceneCulling.h" #include "gfx-base/GFXBuffer.h" #include "gfx-base/GFXCommandBuffer.h" #include "gfx-base/GFXDescriptorSet.h" @@ -57,19 +57,19 @@ gfx::RenderPass *ForwardPipeline::getOrCreateRenderPass(gfx::ClearFlags clearFla return _renderPasses[clearFlags]; } - auto *device = gfx::Device::getInstance(); - gfx::ColorAttachment colorAttachment; + auto * device = gfx::Device::getInstance(); + gfx::ColorAttachment colorAttachment; gfx::DepthStencilAttachment depthStencilAttachment; - colorAttachment.format = device->getColorFormat(); - depthStencilAttachment.format = device->getDepthStencilFormat(); + colorAttachment.format = device->getColorFormat(); + depthStencilAttachment.format = device->getDepthStencilFormat(); depthStencilAttachment.stencilStoreOp = gfx::StoreOp::STORE; - depthStencilAttachment.depthStoreOp = gfx::StoreOp::STORE; + depthStencilAttachment.depthStoreOp = gfx::StoreOp::STORE; if (!hasFlag(clearFlags, gfx::ClearFlagBit::COLOR)) { if (hasFlag(clearFlags, static_cast(skyboxFlag))) { colorAttachment.loadOp = gfx::LoadOp::DISCARD; } else { - colorAttachment.loadOp = gfx::LoadOp::LOAD; + colorAttachment.loadOp = gfx::LoadOp::LOAD; colorAttachment.beginAccesses = {gfx::AccessType::PRESENT}; } } @@ -80,10 +80,10 @@ gfx::RenderPass *ForwardPipeline::getOrCreateRenderPass(gfx::ClearFlags clearFla depthStencilAttachment.beginAccesses = {gfx::AccessType::DEPTH_STENCIL_ATTACHMENT_WRITE}; } - auto *renderPass = device->createRenderPass({ + auto *renderPass = device->createRenderPass({ {colorAttachment}, depthStencilAttachment, - {}, + {}, }); _renderPasses[clearFlags] = renderPass; @@ -121,7 +121,6 @@ bool ForwardPipeline::activate() { return true; } - void ForwardPipeline::render(const vector &cameras) { _commandBuffers[0]->begin(); _pipelineUBO->updateGlobalUBO(); @@ -154,7 +153,7 @@ bool ForwardPipeline::activeRenderer() { {}, {}, }; - const uint shadowMapSamplerHash = SamplerLib::genSamplerHash(info); + const uint shadowMapSamplerHash = SamplerLib::genSamplerHash(info); gfx::Sampler *const shadowMapSampler = SamplerLib::getSampler(shadowMapSamplerHash); // Main light sampler binding diff --git a/native/cocos/scene/RenderScene.cpp b/native/cocos/scene/RenderScene.cpp index b2daa1a395..bd3d54abc7 100644 --- a/native/cocos/scene/RenderScene.cpp +++ b/native/cocos/scene/RenderScene.cpp @@ -28,10 +28,14 @@ #include #include "base/Log.h" +extern void jsbFlushFastMQ(); + namespace cc { namespace scene { void RenderScene::update(uint32_t stamp) { + jsbFlushFastMQ(); + if (_directionalLight) { _directionalLight->update(); } diff --git a/native/tools/tojs/scene.ini b/native/tools/tojs/scene.ini index 3d0c3038e0..bc6c51e7f2 100644 --- a/native/tools/tojs/scene.ini +++ b/native/tools/tojs/scene.ini @@ -56,7 +56,8 @@ skip = Model::[setInstancedAttrBlock getType getLocalBuffer getModelBounds getWo RenderScene::[updateBatches], BakedAnimInfo::[buffer data dirty], BakedJointInfo::[boundsInfo jointTextureInfo animInfo buffer], - BakedSkinningModel::[updateTransform updateUBOs] + BakedSkinningModel::[updateTransform updateUBOs], + DrawBatch2D::[visFlags descriptorSet inputAssembler passes shaders] rename_functions =