3.3 js message queue (#3731)

This commit is contained in:
江战
2021-07-16 15:20:23 +08:00
committed by GitHub
parent cdfb58f907
commit aa009ac26b
8 changed files with 197 additions and 14 deletions
+1
View File
@@ -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
@@ -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<uint64_t>(reinterpret_cast<uintptr_t>(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<uint64_t>(reinterpret_cast<uintptr_t>(nullptr))));
_privateData = nullptr;
}
}
@@ -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<cc::scene::Pass>(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;
}
@@ -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 <class T>
struct alignas(uint32_t) AlignedPtr {
char _data[8];
T * get() {
return reinterpret_cast<T *>(static_cast<uintptr_t>(*reinterpret_cast<uint64_t *>(this)));
}
};
void fastSetFlag(void *buffer) {
struct Heap {
AlignedPtr<cc::scene::DrawBatch2D> selfPtr;
uint32_t flags;
};
Heap *heap = reinterpret_cast<Heap *>(buffer);
heap->selfPtr.get()->visFlags = heap->flags;
}
void fastSetDescriptorSet(void *buffer) {
struct Heap {
AlignedPtr<cc::scene::DrawBatch2D> selfPtr;
AlignedPtr<cc::gfx::DescriptorSet> dsPtr;
};
Heap *heap = reinterpret_cast<Heap *>(buffer);
heap->selfPtr.get()->descriptorSet = heap->dsPtr.get();
}
void fastSetInputAssembler(void *buffer) {
struct Heap {
AlignedPtr<cc::scene::DrawBatch2D> selfPtr;
AlignedPtr<cc::gfx::InputAssembler> inputAssemblerPtr;
};
Heap *heap = reinterpret_cast<Heap *>(buffer);
heap->selfPtr.get()->inputAssembler = heap->inputAssemblerPtr.get();
}
void fastSetPasses(void *buffer) {
struct Heap {
AlignedPtr<cc::scene::DrawBatch2D> selfPtr;
uint32_t passSize;
AlignedPtr<cc::scene::Pass> passes[0];
};
static_assert(offsetof(Heap, passes) == 12);
Heap *heap = reinterpret_cast<Heap *>(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<cc::scene::DrawBatch2D> selfPtr;
uint32_t shaderSize;
AlignedPtr<cc::gfx::Shader> shaders[0];
};
Heap *heap = reinterpret_cast<Heap *>(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 <typename F>
uint64_t convertPtr(F *in) {
return static_cast<uint64_t>(reinterpret_cast<intptr_t>(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, &currentArrayBuffer);
arrayBufferObj = currentArrayBuffer.toObject();
arrayBufferObj->getArrayBufferData(&mqPtr, nullptr);
auto *u32Ptr = reinterpret_cast<uint32_t *>(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<uint32_t *>(p);
uint32_t len = *base;
auto * fnAddr = reinterpret_cast<uint64_t *>(base + 1);
auto * fn = reinterpret_cast<FastFunction *>(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<uint32_t *>(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;
}
@@ -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<uint>(_device->getSurfaceTransform()) % 2 ? camera->height : camera->width;
uint h = camera->window->hasOnScreenAttachments && static_cast<uint>(_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
@@ -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<gfx::ClearFlagBit>(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<scene::Camera *> &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
+4
View File
@@ -28,10 +28,14 @@
#include <utility>
#include "base/Log.h"
extern void jsbFlushFastMQ();
namespace cc {
namespace scene {
void RenderScene::update(uint32_t stamp) {
jsbFlushFastMQ();
if (_directionalLight) {
_directionalLight->update();
}
+2 -1
View File
@@ -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 =