From f78ade706e875dd0d14e9377c379fdd9b43b8587 Mon Sep 17 00:00:00 2001 From: timlyeee <43355299+timlyeee@users.noreply.github.com> Date: Fri, 7 Jan 2022 18:45:00 +0800 Subject: [PATCH 01/19] Update config.json (#4137) --- native/external/config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/native/external/config.json b/native/external/config.json index bb2249a89f..bd01c95b95 100644 --- a/native/external/config.json +++ b/native/external/config.json @@ -1,9 +1,9 @@ { - "version": "v3d-gfx-84", + "version": "v3d-gfx-88", "from": { "type": "github", "owner": "cocos-creator", "name": "engine-native-external", - "checkout": "v3d-gfx-83" + "checkout": "v3d-gfx-88" } } From 5321d81d95647a1885d1c79488feee03186b2ca8 Mon Sep 17 00:00:00 2001 From: DolaMay <158788224@qq.com> Date: Fri, 7 Jan 2022 22:56:18 +0800 Subject: [PATCH 02/19] Position Delete & Normal Compress: V3.4.1 native (#4135) * fix cluster check bugs * Position Delete & Normal Compress --- .../renderer/pipeline/ClusterLightCulling.cpp | 7 +++- .../cocos/renderer/pipeline/PipelineUBO.cpp | 8 ++-- .../pipeline/deferred/DeferredPipeline.cpp | 1 - .../pipeline/deferred/DeferredPipeline.h | 2 +- .../pipeline/deferred/GbufferStage.cpp | 8 ++-- .../pipeline/deferred/LightingStage.cpp | 38 ++++++++++++++----- 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/native/cocos/renderer/pipeline/ClusterLightCulling.cpp b/native/cocos/renderer/pipeline/ClusterLightCulling.cpp index a32bd80d17..70777d90b6 100644 --- a/native/cocos/renderer/pipeline/ClusterLightCulling.cpp +++ b/native/cocos/renderer/pipeline/ClusterLightCulling.cpp @@ -229,8 +229,10 @@ void ClusterLightCulling::updateLights() { break; } } - // the count of lights is set to cc_lightDir[0].w - _lightBufferData[3 * 4 + 3] = static_cast(validLightCount); + if (validLightCount > 0) { + // the count of lights is set to cc_lightDir[0].w + _lightBufferData[3 * 4 + 3] = static_cast(validLightCount); + } } void ClusterLightCulling::initBuildingSatge() { @@ -661,6 +663,7 @@ void ClusterLightCulling::clusterLightCulling(scene::Camera* camera) { if (!_initialized || _pipeline->getPipelineUBO()->getCurrentCameraUBOOffset() != 0) return; _camera = camera; update(); // update ubo and light data + if (_validLights.empty()) return; struct DataClusterBuild { framegraph::BufferHandle clusterBuffer; // cluster build storage buffer diff --git a/native/cocos/renderer/pipeline/PipelineUBO.cpp b/native/cocos/renderer/pipeline/PipelineUBO.cpp index 1edd46394a..d229e5d8c6 100644 --- a/native/cocos/renderer/pipeline/PipelineUBO.cpp +++ b/native/cocos/renderer/pipeline/PipelineUBO.cpp @@ -163,10 +163,10 @@ void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *out output[UBOCamera::GLOBAL_NEAR_FAR_OFFSET + 0] = static_cast(camera->nearClip); output[UBOCamera::GLOBAL_NEAR_FAR_OFFSET + 1] = static_cast(camera->farClip); - output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 0] = camera->viewPort.x; - output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 1] = camera->viewPort.y; - output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 2] = camera->viewPort.z; - output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 3] = camera->viewPort.w; + output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 0] = sharedData->shadingScale * camera->window->getWidth() * camera->viewPort.x; + output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 1] = sharedData->shadingScale * camera->window->getHeight() * camera->viewPort.y; + output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 2] = sharedData->shadingScale * camera->window->getWidth() * camera->viewPort.z; + output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 3] = sharedData->shadingScale * camera->window->getHeight() * camera->viewPort.w; } void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, std::array *bufferView, const scene::Camera *camera) { diff --git a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp index 491f248331..7708542943 100644 --- a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp +++ b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp @@ -51,7 +51,6 @@ namespace pipeline { framegraph::StringHandle DeferredPipeline::fgStrHandleGbufferTexture[GBUFFER_COUNT] = { framegraph::FrameGraph::stringToHandle("gbufferAlbedoTexture"), - framegraph::FrameGraph::stringToHandle("gbufferPositionTexture"), framegraph::FrameGraph::stringToHandle("gbufferNormalTexture"), framegraph::FrameGraph::stringToHandle("gbufferEmissiveTexture")}; diff --git a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h index d3170936f0..f406718f0c 100644 --- a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h +++ b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h @@ -69,7 +69,7 @@ private: ClusterLightCulling *_clusterComp{nullptr}; public: - static constexpr uint GBUFFER_COUNT = 4; + static constexpr uint GBUFFER_COUNT = 3; // deferred resource names static framegraph::StringHandle fgStrHandleGbufferTexture[GBUFFER_COUNT]; diff --git a/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp b/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp index b9f0812a6e..40beea573e 100644 --- a/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp +++ b/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp @@ -169,7 +169,7 @@ void GbufferStage::render(scene::Camera *camera) { static_cast(pipeline->getHeight() * shadingScale), }; for (int i = 0; i < DeferredPipeline::GBUFFER_COUNT - 1; ++i) { - if (i != 0) { // positions & normals need more precision + if (i != 0) { // normals need more precision data.gbuffer[i] = builder.create(DeferredPipeline::fgStrHandleGbufferTexture[i], gbufferInfoFloat); } else { data.gbuffer[i] = builder.create(DeferredPipeline::fgStrHandleGbufferTexture[i], gbufferInfo); @@ -178,10 +178,10 @@ void GbufferStage::render(scene::Camera *camera) { auto subpassEnabled = _device->hasFeature(gfx::Feature::INPUT_ATTACHMENT_BENEFIT); if (subpassEnabled) { - // when subpass enabled, the color result (gles2/gles3) will write to gbuffer[3] and the blit to color texture, so the format should be RGBA16F - data.gbuffer[3] = builder.create(DeferredPipeline::fgStrHandleGbufferTexture[3], gbufferInfoFloat); + // when subpass enabled, the color result (gles2/gles3) will write to gbuffer[2] and the blit to color texture, so the format should be RGBA16F + data.gbuffer[2] = builder.create(DeferredPipeline::fgStrHandleGbufferTexture[2], gbufferInfoFloat); } else { - data.gbuffer[3] = builder.create(DeferredPipeline::fgStrHandleGbufferTexture[3], gbufferInfo); + data.gbuffer[2] = builder.create(DeferredPipeline::fgStrHandleGbufferTexture[2], gbufferInfo); } gfx::Color clearColor{0.0, 0.0, 0.0, 0.0}; diff --git a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp index 93f227d3f0..b759d6bb33 100644 --- a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp +++ b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp @@ -319,7 +319,7 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { } struct RenderData { - framegraph::TextureHandle gbuffer[4]; // read from gbuffer stage + framegraph::TextureHandle gbuffer[3]; // read from gbuffer stage framegraph::TextureHandle outputTex; // output texture framegraph::TextureHandle depth; framegraph::BufferHandle lightBuffer; // light storage buffer @@ -337,7 +337,7 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { builder.subpass(true); // read gbuffer - for (int i = 0; i < 4; i++) { + for (int i = 0; i < 3; i++) { data.gbuffer[i] = builder.read(framegraph::TextureHandle(builder.readFromBlackboard(DeferredPipeline::fgStrHandleGbufferTexture[i]))); builder.writeToBlackboard(DeferredPipeline::fgStrHandleGbufferTexture[i], data.gbuffer[i]); } @@ -351,16 +351,23 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { data.depth = framegraph::TextureHandle(builder.readFromBlackboard(RenderPipeline::fgStrHandleOutDepthTexture)); data.depth = builder.write(data.depth, depthAttachmentInfo); + data.depth = builder.read(data.depth); builder.writeToBlackboard(RenderPipeline::fgStrHandleOutDepthTexture, data.depth); if (_pipeline->getClusterEnabled()) { // read cluster and light info data.lightBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterLightBuffer)); - builder.read(data.lightBuffer); + if (data.lightBuffer.isValid()) { + builder.read(data.lightBuffer); + } data.lightIndexBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterLightIndexBuffer)); - builder.read(data.lightIndexBuffer); + if (data.lightIndexBuffer.isValid()) { + builder.read(data.lightIndexBuffer); + } data.lightGridBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterLightGridBuffer)); - builder.read(data.lightGridBuffer); + if (data.lightGridBuffer.isValid()) { + builder.read(data.lightGridBuffer); + } } // write to lighting output @@ -406,12 +413,20 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { pass->getDescriptorSet()->bindTexture(i, table.getRead(data.gbuffer[i])); pass->getDescriptorSet()->bindSampler(i, _defaultSampler); } + pass->getDescriptorSet()->bindTexture(3, table.getRead(data.depth)); + pass->getDescriptorSet()->bindSampler(3, _defaultSampler); if (_pipeline->getClusterEnabled()) { // cluster buffer bind - pass->getDescriptorSet()->bindBuffer(CLUSTER_LIGHT_BINDING, table.getRead(data.lightBuffer)); - pass->getDescriptorSet()->bindBuffer(CLUSTER_LIGHT_INDEX_BINDING, table.getRead(data.lightIndexBuffer)); - pass->getDescriptorSet()->bindBuffer(CLUSTER_LIGHT_GRID_BINDING, table.getRead(data.lightGridBuffer)); + if (data.lightBuffer.isValid()) { + pass->getDescriptorSet()->bindBuffer(CLUSTER_LIGHT_BINDING, table.getRead(data.lightBuffer)); + } + if (data.lightIndexBuffer.isValid()) { + pass->getDescriptorSet()->bindBuffer(CLUSTER_LIGHT_INDEX_BINDING, table.getRead(data.lightIndexBuffer)); + } + if (data.lightGridBuffer.isValid()) { + pass->getDescriptorSet()->bindBuffer(CLUSTER_LIGHT_GRID_BINDING, table.getRead(data.lightGridBuffer)); + } } pass->getDescriptorSet()->update(); @@ -491,8 +506,11 @@ void LightingStage::fgTransparent(scene::Camera *camera) { auto *pipeline = static_cast(_pipeline); auto *cmdBuff = pipeline->getCommandBuffers()[0]; - vector dynamicOffsets = {0}; - cmdBuff->bindDescriptorSet(localSet, _descriptorSet, dynamicOffsets); + // no need to bind localSet in cluster + if (!_pipeline->getClusterEnabled()) { + vector dynamicOffsets = {0}; + cmdBuff->bindDescriptorSet(localSet, _descriptorSet, dynamicOffsets); + } const std::array globalOffsets = {pipeline->getPipelineUBO()->getCurrentCameraUBOOffset()}; cmdBuff->bindDescriptorSet(globalSet, pipeline->getDescriptorSet(), utils::toUint(globalOffsets.size()), globalOffsets.data()); From 08e9f59ce9cfe898605ed13caec8693aedd17400 Mon Sep 17 00:00:00 2001 From: timlyeee <43355299+timlyeee@users.noreply.github.com> Date: Sat, 8 Jan 2022 18:05:34 +0800 Subject: [PATCH 03/19] Fix callInfo leak (#4148) --- native/cocos/bindings/manual/JavaScriptJavaBridge.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/native/cocos/bindings/manual/JavaScriptJavaBridge.cpp b/native/cocos/bindings/manual/JavaScriptJavaBridge.cpp index 40bde75041..cf1e21c5dd 100644 --- a/native/cocos/bindings/manual/JavaScriptJavaBridge.cpp +++ b/native/cocos/bindings/manual/JavaScriptJavaBridge.cpp @@ -220,7 +220,9 @@ Java_com_cocos_lib_JsbBridge_nativeSendToScript(JNIEnv *env, jclass clazz, jstri ScriptNativeBridge* ScriptNativeBridge::bridgeCxxInstance{nullptr}; JavaScriptJavaBridge::CallInfo::~CallInfo() { + _mEnv->DeleteLocalRef(_mClassID); if (_mReturnType == ValueType::STRING && _mRet.stringValue) { + _mEnv->DeleteLocalRef(_mRetjstring); delete _mRet.stringValue; } } From 952f646bc1df358054d5683413b83d7cab993253 Mon Sep 17 00:00:00 2001 From: timlyeee <43355299+timlyeee@users.noreply.github.com> Date: Mon, 10 Jan 2022 17:26:21 +0800 Subject: [PATCH 04/19] remove unnecessary include (#4153) --- native/cocos/platform/apple/JsbBridgeWrapper.mm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/native/cocos/platform/apple/JsbBridgeWrapper.mm b/native/cocos/platform/apple/JsbBridgeWrapper.mm index acb074f69b..b857e8c90e 100644 --- a/native/cocos/platform/apple/JsbBridgeWrapper.mm +++ b/native/cocos/platform/apple/JsbBridgeWrapper.mm @@ -22,11 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#include "JsbBridgeWrapper.h" #include #include #include "JsbBridge.h" -#include "Application.h" +#include "JsbBridgeWrapper.h" @implementation JsbBridgeWrapper { JsbBridge* jb; From b9babc391c3066b39065feed7702c3863ecfb617 Mon Sep 17 00:00:00 2001 From: DolaMay <158788224@qq.com> Date: Wed, 12 Jan 2022 14:36:49 +0800 Subject: [PATCH 05/19] Fix Lighting stage feedback loop (#4157) * fix cluster check bugs * Position Delete & Normal Compress * Fix Lighting stage feedback loop --- native/cocos/renderer/gfx-vulkan/VKCommands.cpp | 1 - native/cocos/renderer/pipeline/deferred/LightingStage.cpp | 8 -------- 2 files changed, 9 deletions(-) diff --git a/native/cocos/renderer/gfx-vulkan/VKCommands.cpp b/native/cocos/renderer/gfx-vulkan/VKCommands.cpp index bf17f1ba6e..28aacb47f7 100644 --- a/native/cocos/renderer/gfx-vulkan/VKCommands.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKCommands.cpp @@ -609,7 +609,6 @@ void cmdFuncCCVKCreateRenderPass(CCVKDevice *device, CCVKGPURenderPass *gpuRende // first, gather necessary statistics for each attachment auto updateLifeCycle = [](AttachmentStatistics &statistics, uint32_t index, VkImageLayout layout, AttachmentStatistics::SubpassUsage usage) { if (statistics.records.count(index)) { - CC_ASSERT(statistics.records[index].layout == layout); statistics.records[index].usage |= usage; } else { statistics.records[index] = {layout, usage}; diff --git a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp index b759d6bb33..131c06025c 100644 --- a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp +++ b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp @@ -342,15 +342,7 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { builder.writeToBlackboard(DeferredPipeline::fgStrHandleGbufferTexture[i], data.gbuffer[i]); } - // read depth, as an attachment - framegraph::RenderTargetAttachment::Descriptor depthAttachmentInfo; - depthAttachmentInfo.usage = framegraph::RenderTargetAttachment::Usage::DEPTH_STENCIL; - depthAttachmentInfo.loadOp = gfx::LoadOp::LOAD; - depthAttachmentInfo.beginAccesses = {gfx::AccessType::DEPTH_STENCIL_ATTACHMENT_WRITE}; - depthAttachmentInfo.endAccesses = {gfx::AccessType::DEPTH_STENCIL_ATTACHMENT_WRITE}; - data.depth = framegraph::TextureHandle(builder.readFromBlackboard(RenderPipeline::fgStrHandleOutDepthTexture)); - data.depth = builder.write(data.depth, depthAttachmentInfo); data.depth = builder.read(data.depth); builder.writeToBlackboard(RenderPipeline::fgStrHandleOutDepthTexture, data.depth); From 9340b1e3cd3a0f48ce958353e8585f042bc77741 Mon Sep 17 00:00:00 2001 From: timlyeee <43355299+timlyeee@users.noreply.github.com> Date: Wed, 12 Jan 2022 15:27:14 +0800 Subject: [PATCH 06/19] remove local reference array (#4161) --- native/cocos/platform/java/jni/JniHelper.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/native/cocos/platform/java/jni/JniHelper.h b/native/cocos/platform/java/jni/JniHelper.h index 565f3ca305..0dd9ccee03 100644 --- a/native/cocos/platform/java/jni/JniHelper.h +++ b/native/cocos/platform/java/jni/JniHelper.h @@ -282,6 +282,7 @@ public: #ifndef __OHOS__ ccDeleteLocalRef(t.env, t.classID); #endif + ccDeleteLocalRef(t.env, array); deleteLocalRefs(t.env, &localRefs); return &ret[0]; } @@ -312,6 +313,7 @@ public: #ifndef __OHOS__ ccDeleteLocalRef(t.env, t.classID); #endif + ccDeleteLocalRef(t.env, array); deleteLocalRefs(t.env, &localRefs); } else { reportError(className, methodName, signature); From 675ecb2351065bae415c69b31dce093b157cf702 Mon Sep 17 00:00:00 2001 From: Zeqiang Li Date: Wed, 12 Jan 2022 15:37:39 +0800 Subject: [PATCH 07/19] camera render area (#4164) --- .../renderer/gfx-metal/MTLCommandBuffer.h | 3 ++ .../renderer/gfx-metal/MTLCommandBuffer.mm | 24 ++++++++-- .../cocos/renderer/gfx-metal/MTLGPUObjects.h | 2 + native/cocos/renderer/gfx-metal/MTLUtils.mm | 48 ++++++++++++++++--- 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/native/cocos/renderer/gfx-metal/MTLCommandBuffer.h b/native/cocos/renderer/gfx-metal/MTLCommandBuffer.h index c12b48e903..073c1d10af 100644 --- a/native/cocos/renderer/gfx-metal/MTLCommandBuffer.h +++ b/native/cocos/renderer/gfx-metal/MTLCommandBuffer.h @@ -32,6 +32,7 @@ #include "MTLComputeCommandEncoder.h" #include "MTLGPUObjects.h" #include "MTLRenderCommandEncoder.h" +#include namespace cc { namespace gfx { @@ -114,6 +115,8 @@ protected: CCMTLGPUCommandBufferObject *_gpuCommandBufferObj = nullptr; CCMTLSemaphore* _texCopySemaphore = nullptr; + + std::bitset _colorAppearedBefore; }; } // namespace gfx diff --git a/native/cocos/renderer/gfx-metal/MTLCommandBuffer.mm b/native/cocos/renderer/gfx-metal/MTLCommandBuffer.mm index 236d1ecee5..84437e462a 100644 --- a/native/cocos/renderer/gfx-metal/MTLCommandBuffer.mm +++ b/native/cocos/renderer/gfx-metal/MTLCommandBuffer.mm @@ -126,6 +126,8 @@ void CCMTLCommandBuffer::begin(RenderPass *renderPass, uint subpass, Framebuffer _firstDirtyDescriptorSet = UINT_MAX; _commandBufferBegan = true; _firstRenderPass = true; + + _colorAppearedBefore.reset(); } void CCMTLCommandBuffer::end() { @@ -162,6 +164,7 @@ void CCMTLCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *fb // if not rendering to full framebuffer(eg. left top area), draw a quad to pretend viewport clear. bool renderingFullFramebuffer = isRenderingEntireDrawable(renderArea, static_cast(fbo)); + bool needPartialClear = false; if (subpasses.empty()) { if (dsTexture) { auto *ccMtlTexture = static_cast(dsTexture); @@ -181,10 +184,16 @@ void CCMTLCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *fb ccMtlRenderPass->setColorAttachment(i, ccMtlTexture, 0); mtlRenderPassDescriptor.colorAttachments[i].clearColor = mu::toMTLClearColor(colors[i]); if(!renderingFullFramebuffer) { - mtlRenderPassDescriptor.colorAttachments[i].loadAction = _firstRenderPass ? MTLLoadActionClear : MTLLoadActionLoad; + if(!_colorAppearedBefore[i]) { + mtlRenderPassDescriptor.colorAttachments[i].loadAction = mu::toMTLLoadAction(colorAttachments[i].loadOp); + } else { + mtlRenderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionLoad; + needPartialClear = colorAttachments[i].loadOp == LoadOp::CLEAR; + } } else { mtlRenderPassDescriptor.colorAttachments[i].loadAction = mu::toMTLLoadAction(colorAttachments[i].loadOp); } + _colorAppearedBefore.set(i); mtlRenderPassDescriptor.colorAttachments[i].storeAction = mu::isFramebufferFetchSupported() ? mu::toMTLStoreAction(colorAttachments[i].storeOp) : MTLStoreActionStore; } } @@ -211,12 +220,18 @@ void CCMTLCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *fb ccMtlRenderPass->setColorAttachment(color, ccMtlTexture, 0); mtlRenderPassDescriptor.colorAttachments[color].clearColor = mu::toMTLClearColor(colors[color]); if(!renderingFullFramebuffer) { - mtlRenderPassDescriptor.colorAttachments[color].loadAction = _firstRenderPass ? MTLLoadActionClear : MTLLoadActionLoad; + if(!_colorAppearedBefore[color]) { + mtlRenderPassDescriptor.colorAttachments[color].loadAction = mu::toMTLLoadAction(colorAttachments[color].loadOp); + } else { + mtlRenderPassDescriptor.colorAttachments[color].loadAction = MTLLoadActionLoad; + needPartialClear = colorAttachments[color].loadOp == LoadOp::CLEAR; + } } else { mtlRenderPassDescriptor.colorAttachments[color].loadAction = mu::toMTLLoadAction(colorAttachments[color].loadOp); } mtlRenderPassDescriptor.colorAttachments[color].storeAction = mu::isFramebufferFetchSupported() ? mu::toMTLStoreAction(colorAttachments[color].storeOp) : MTLStoreActionStore; visited[color] = true; + _colorAppearedBefore.set(color); if (subpasses[i].resolves.size() > j) { uint32_t resolve = subpasses[i].resolves[j]; auto * resolveTex = static_cast(colorTextures[resolve]); @@ -259,7 +274,7 @@ void CCMTLCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *fb //[_renderEncoder.getMTLEncoder() memoryBarrierWithScope:MTLBarrierScopeTextures afterStages:MTLRenderStageFragment beforeStages:MTLRenderStageFragment]; } - if (!renderingFullFramebuffer) { + if (!renderingFullFramebuffer && needPartialClear) { //Metal doesn't apply the viewports and scissors to renderpass load-action clearing. mu::clearRenderArea(_mtlDevice, _renderEncoder.getMTLEncoder(), renderPass, renderArea, colors, depth, stencil); } @@ -475,8 +490,9 @@ void CCMTLCommandBuffer::nextSubpass() { const auto colorAttachmentCount = colorAttachments.size(); const SubpassInfoList &subpasses = curRenderPass->getSubpasses(); for (size_t slot = 0U; slot < colorAttachmentCount; slot++) { - mtlRenderPassDescriptor.colorAttachments[slot].loadAction = MTLLoadActionLoad; // mu::toMTLLoadAction(colorAttachments[slot].loadOp); + mtlRenderPassDescriptor.colorAttachments[slot].loadAction = _colorAppearedBefore[slot] ? MTLLoadActionLoad : mu::toMTLLoadAction(colorAttachments[slot].loadOp); mtlRenderPassDescriptor.colorAttachments[slot].storeAction = curSubpassIndex == subpasses.size() - 1 ? mu::toMTLStoreAction(colorAttachments[slot].storeOp) : MTLStoreActionStore; + _colorAppearedBefore.set(slot); } updateDepthStencilState(curSubpassIndex, mtlRenderPassDescriptor); _renderEncoder.initialize(getMTLCommandBuffer(), ccRenderpass->getMTLRenderPassDescriptor()); diff --git a/native/cocos/renderer/gfx-metal/MTLGPUObjects.h b/native/cocos/renderer/gfx-metal/MTLGPUObjects.h index 7d18dab249..104018e397 100644 --- a/native/cocos/renderer/gfx-metal/MTLGPUObjects.h +++ b/native/cocos/renderer/gfx-metal/MTLGPUObjects.h @@ -53,6 +53,8 @@ namespace { constexpr size_t MegaBytesToBytes = 1024 * 1024; } +constexpr size_t MAX_COLORATTACHMENTS = 16u; + class CCMTLGPUDescriptorSetLayout : public Object { public: DescriptorSetLayoutBindingList bindings; diff --git a/native/cocos/renderer/gfx-metal/MTLUtils.mm b/native/cocos/renderer/gfx-metal/MTLUtils.mm index 1b32ae9d66..772b0f6f45 100644 --- a/native/cocos/renderer/gfx-metal/MTLUtils.mm +++ b/native/cocos/renderer/gfx-metal/MTLUtils.mm @@ -314,24 +314,58 @@ bool isASTCFormat(Format format) { } } -gfx::Shader *createShader(CCMTLDevice *device) { +gfx::Shader *createShader(CCMTLDevice *device, CCMTLRenderPass* renderPass) { String vs = R"( layout(location = 0) in vec2 a_position; void main() { gl_Position = vec4(a_position, 1.0, 1.0); } )"; +// String fs = R"( +// precision mediump float; +// layout(set = 0, binding = 0) uniform Color { +// vec4 u_color; +// }; +// layout(location = 0) out vec4 o_color; +// +// void main() { +// o_color = u_color; +// } +// )"; + String fs = R"( precision mediump float; layout(set = 0, binding = 0) uniform Color { vec4 u_color; }; - layout(location = 0) out vec4 o_color; - - void main() { - o_color = u_color; - } )"; + + //TODO_Zeqiang: gather info in framegraph. + if(renderPass->getSubpasses().empty()) { + for (size_t i = 0; i < renderPass->getColorAttachments().size(); ++i) { + fs += "\n layout(location = " + std::to_string(i) + ") out vec4 o_color" + std::to_string(i) + ";"; + } + + fs += "\nvoid main() {\n o_color0 = u_color;\n"; + + for (size_t i = 1; i < renderPass->getColorAttachments().size(); ++i) { + fs += " o_color" + std::to_string(i) + " = vec4(0.0);\n"; + } + } else { + const auto& subpasses = renderPass->getSubpasses(); + for (size_t i = 0; i < subpasses[renderPass->getCurrentSubpassIndex()].colors.size(); ++i) { + fs += "\n layout(location = " + std::to_string(i) + ") out vec4 o_color" + std::to_string(i) + ";"; + } + + fs += "\nvoid main() {\n o_color0 = u_color;\n"; + + for (size_t i = 1; i < subpasses[renderPass->getCurrentSubpassIndex()].colors.size(); ++i) { + fs += " o_color" + std::to_string(i) + " = vec4(0.0);\n"; + } + } + + fs += "}"; + gfx::ShaderStageList shaderStageList; gfx::ShaderStage vertexShaderStage; vertexShaderStage.stage = gfx::ShaderStageFlagBit::VERTEX; @@ -367,7 +401,7 @@ CCMTLGPUPipelineState *getClearRenderPassPipelineState(CCMTLDevice *device, Rend gfx::Attribute position = {"a_position", gfx::Format::RG32F, false, 0, false}; gfx::PipelineStateInfo pipelineInfo; pipelineInfo.primitive = gfx::PrimitiveMode::TRIANGLE_LIST; - pipelineInfo.shader = createShader(device); + pipelineInfo.shader = createShader(device, static_cast(curPass)); pipelineInfo.inputState = {{position}}; pipelineInfo.renderPass = curPass; From 6d3d4830a37132ea3afd1edfbf14c9060c11afac Mon Sep 17 00:00:00 2001 From: troublemaker52025 Date: Wed, 12 Jan 2022 23:26:46 +0800 Subject: [PATCH 08/19] fix planar-shadow normal is zero for v3.4.1 (#4165) --- native/cocos/renderer/pipeline/PlanarShadowQueue.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/cocos/renderer/pipeline/PlanarShadowQueue.cpp b/native/cocos/renderer/pipeline/PlanarShadowQueue.cpp index 838d3b851c..f8d629f4de 100644 --- a/native/cocos/renderer/pipeline/PlanarShadowQueue.cpp +++ b/native/cocos/renderer/pipeline/PlanarShadowQueue.cpp @@ -109,7 +109,7 @@ void PlanarShadowQueue::recordCommandBuffer(gfx::Device *device, gfx::RenderPass const PipelineSceneData * sceneData = _pipeline->getPipelineSceneData(); const scene::PipelineSharedSceneData *sharedData = sceneData->getSharedData(); const scene::Shadow * shadowInfo = sharedData->shadow; - if (!shadowInfo->enabled || shadowInfo->shadowType != scene::ShadowType::PLANAR) { + if (!shadowInfo->enabled || shadowInfo->shadowType != scene::ShadowType::PLANAR || shadowInfo->normal.length() < 0.000001F) { return; } From 940891f274d8697ffb43042d1bf14ccd00900549 Mon Sep 17 00:00:00 2001 From: GengineJS <476393671@qq.com> Date: Fri, 14 Jan 2022 09:39:20 +0800 Subject: [PATCH 09/19] Fix the problem of deferred pipeline multi-execution transparent queue (#4168) --- native/cocos/renderer/pipeline/deferred/LightingStage.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp index 131c06025c..5b245a4174 100644 --- a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp +++ b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp @@ -542,8 +542,7 @@ void LightingStage::putTransparentObj2Queue() { // TODO(xwx): need to fallback unlit and gizmo material. if (pass->getPhase() != _phaseID) continue; for (k = 0; k < _renderQueues.size(); k++) { - _isTransparentQueueEmpty = false; - _renderQueues[k]->insertRenderPass(ro, m, p); + if (_renderQueues[k]->insertRenderPass(ro, m, p)) _isTransparentQueueEmpty = false; } p++; } From 8b0c68504c7a95bba5da8e3f5ae95cf675d4720d Mon Sep 17 00:00:00 2001 From: Zeqiang Li Date: Fri, 14 Jan 2022 15:57:55 +0800 Subject: [PATCH 10/19] m1 deferred rendering issue: fbf & write to same tex not fully support (#4174) Co-authored-by: lyeee --- native/cocos/renderer/gfx-metal/MTLUtils.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/native/cocos/renderer/gfx-metal/MTLUtils.mm b/native/cocos/renderer/gfx-metal/MTLUtils.mm index 772b0f6f45..bc3bae46e7 100644 --- a/native/cocos/renderer/gfx-metal/MTLUtils.mm +++ b/native/cocos/renderer/gfx-metal/MTLUtils.mm @@ -911,7 +911,7 @@ bool mu::isImageBlockSupported() { if(!mu::isFramebufferFetchSupported()) { return false; } -#if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) || TARGET_CPU_ARM64 +#if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) //|| TARGET_CPU_ARM64 return true; #else return false; @@ -919,7 +919,7 @@ bool mu::isImageBlockSupported() { } bool mu::isFramebufferFetchSupported() { -#if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) || TARGET_CPU_ARM64 +#if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) //|| TARGET_CPU_ARM64 return true; #else return false; From 9cd1fd911e699851aa597b7626b9140c7b60fec0 Mon Sep 17 00:00:00 2001 From: Zeqiang Li Date: Mon, 17 Jan 2022 09:38:41 +0800 Subject: [PATCH 11/19] fix mem status (#4179) --- native/cocos/renderer/gfx-metal/MTLTexture.mm | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/native/cocos/renderer/gfx-metal/MTLTexture.mm b/native/cocos/renderer/gfx-metal/MTLTexture.mm index eac5ebcc53..00696133fd 100644 --- a/native/cocos/renderer/gfx-metal/MTLTexture.mm +++ b/native/cocos/renderer/gfx-metal/MTLTexture.mm @@ -269,9 +269,14 @@ void CCMTLTexture::doResize(uint width, uint height, uint size) { CC_LOG_ERROR("CCMTLTexture: create MTLTexture failed when try to resize the texture."); return; } - - CCMTLDevice::getInstance()->getMemoryStatus().textureSize += size; - + + // texture is a wrapper of drawable when _swapchain is active, drawable is not a resource alloc by gfx, + // but the system so skip here. + if(!_swapchain) { + CCMTLDevice::getInstance()->getMemoryStatus().textureSize -= oldSize; + CCMTLDevice::getInstance()->getMemoryStatus().textureSize += size; + } + if (oldMTLTexture) { std::function destroyFunc = [=]() { if (oldMTLTexture) { @@ -282,7 +287,6 @@ void CCMTLTexture::doResize(uint width, uint height, uint size) { }; //gpu object only CCMTLGPUGarbageCollectionPool::getInstance()->collect(destroyFunc); - CCMTLDevice::getInstance()->getMemoryStatus().textureSize -= oldSize; } } @@ -310,4 +314,4 @@ void CCMTLTexture::deleteDefaultTexture(){ } // namespace gfx -} // namespace cc +} // namespace cc \ No newline at end of file From a608d90577bc4449d94423b0f2efbbd395572854 Mon Sep 17 00:00:00 2001 From: kenshin <420373550@qq.com> Date: Mon, 17 Jan 2022 19:37:11 +0800 Subject: [PATCH 12/19] copy from https://github.com/cocos-creator/engine-native/pull/4170 (#4185) --- native/cocos/ui/edit-box/EditBox-ios.mm | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/native/cocos/ui/edit-box/EditBox-ios.mm b/native/cocos/ui/edit-box/EditBox-ios.mm index 82668d5055..61f0628354 100644 --- a/native/cocos/ui/edit-box/EditBox-ios.mm +++ b/native/cocos/ui/edit-box/EditBox-ios.mm @@ -327,7 +327,15 @@ void removeKeyboardEventLisnters() { } - (void)keyboardWillHide:(NSNotification *)notification { - cc::EditBox::hide(); + NSDictionary *info = [notification userInfo]; + + CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; + CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; + CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y; + + if (yOffset <= 0) { + cc::EditBox::hide(); + } } @end From 815464849021dd26cfcede7defb66e850c7e8bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E6=88=98?= <40414978+PatriceJiang@users.noreply.github.com> Date: Wed, 19 Jan 2022 12:40:20 +0800 Subject: [PATCH 13/19] 3.4.1 Android: Java flush game thread tasks (#4193) * fix run on game thread * save * just flush java tasks, do not run gameTick * reduce timeout to 50 --- .../java/src/com/cocos/lib/CocosHelper.java | 100 +++++++++++------- .../platform/android/jni/JniCocosActivity.cpp | 10 +- .../main/java/com/cocos/lib/CocosHelper.java | 15 ++- 3 files changed, 75 insertions(+), 50 deletions(-) diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java index a2476020bd..f163fc317b 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java @@ -1,27 +1,27 @@ /**************************************************************************** -Copyright (c) 2010-2012 cocos2d-x.org -Copyright (c) 2013-2016 Chukong Technologies Inc. -Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2013-2016 Chukong Technologies Inc. + Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. -http://www.cocos2d-x.org + http://www.cocos2d-x.org -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. -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. + 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. ****************************************************************************/ package com.cocos.lib; @@ -77,7 +77,7 @@ public class CocosHelper { private static BatteryReceiver sBatteryReceiver = new BatteryReceiver(); public static final int NETWORK_TYPE_NONE = 0; - public static final int NETWORK_TYPE_LAN = 1; + public static final int NETWORK_TYPE_LAN = 1; public static final int NETWORK_TYPE_WWAN = 2; // The absolute path to the OBB if it exists. @@ -86,6 +86,7 @@ public class CocosHelper { // The OBB file private static ZipResourceFile sOBBFile = null; + private static Object sTaskMtx = new Object(); private static List sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>()); /** @@ -120,13 +121,19 @@ public class CocosHelper { } public static void runOnGameThread(final Runnable runnable) { - sTaskOnGameThread.add(runnable); + synchronized (sTaskMtx) { + sTaskOnGameThread.add(runnable); + } } static void flushTasksOnGameThread() { - while(sTaskOnGameThread.size() > 0) { - Runnable r = sTaskOnGameThread.remove(0); - if(r != null) { + List tmp = sTaskOnGameThread; + synchronized (sTaskMtx) { + sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>()); + } + while (tmp.size() > 0) { + Runnable r = tmp.remove(0); + if (r != null) { r.run(); } } @@ -159,10 +166,11 @@ public class CocosHelper { // =========================================================== private static boolean sInited = false; + public static void init(final Activity activity) { sActivity = activity; if (!sInited) { - CocosHelper.sVibrateService = (Vibrator)activity.getSystemService(Context.VIBRATOR_SERVICE); + CocosHelper.sVibrateService = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); CocosHelper.initObbFilePath(); CocosHelper.initializeOBBFile(); @@ -170,20 +178,30 @@ public class CocosHelper { } } - public static float getBatteryLevel() { return sBatteryReceiver.sBatteryLevel; } - public static String getObbFilePath() { return CocosHelper.sObbFilePath; } + public static float getBatteryLevel() { + return sBatteryReceiver.sBatteryLevel; + } + + public static String getObbFilePath() { + return CocosHelper.sObbFilePath; + } + public static String getWritablePath() { return sActivity.getFilesDir().getAbsolutePath(); } + public static String getCurrentLanguage() { return Locale.getDefault().getLanguage(); } + public static String getCurrentLanguageCode() { return Locale.getDefault().toString(); } - public static String getDeviceModel(){ + + public static String getDeviceModel() { return Build.MODEL; } + public static String getSystemVersion() { return Build.VERSION.RELEASE; } @@ -193,7 +211,7 @@ public class CocosHelper { if (sVibrateService != null && sVibrateService.hasVibrator()) { if (android.os.Build.VERSION.SDK_INT >= 26) { Class vibrationEffectClass = Class.forName("android.os.VibrationEffect"); - if(vibrationEffectClass != null) { + if (vibrationEffectClass != null) { final int DEFAULT_AMPLITUDE = CocosReflectionHelper.getConstantValue(vibrationEffectClass, "DEFAULT_AMPLITUDE"); //VibrationEffect.createOneShot(long milliseconds, int amplitude) @@ -201,10 +219,10 @@ public class CocosHelper { new Class[]{Long.TYPE, Integer.TYPE}); Class type = method.getReturnType(); - Object effect = method.invoke(vibrationEffectClass, + Object effect = method.invoke(vibrationEffectClass, new Object[]{(long) (duration * 1000), DEFAULT_AMPLITUDE}); //sVibrateService.vibrate(VibrationEffect effect); - CocosReflectionHelper.invokeInstanceMethod(sVibrateService,"vibrate", + CocosReflectionHelper.invokeInstanceMethod(sVibrateService, "vibrate", new Class[]{type}, new Object[]{(effect)}); } } else { @@ -228,11 +246,11 @@ public class CocosHelper { return ret; } - public static void copyTextToClipboard(final String text){ + public static void copyTextToClipboard(final String text) { sActivity.runOnUiThread(new Runnable() { @Override public void run() { - ClipboardManager myClipboard = (ClipboardManager)sActivity.getSystemService(Context.CLIPBOARD_SERVICE); + ClipboardManager myClipboard = (ClipboardManager) sActivity.getSystemService(Context.CLIPBOARD_SERVICE); ClipData myClip = ClipData.newPlainText("text", text); myClipboard.setPrimaryClip(myClip); } @@ -246,8 +264,8 @@ public class CocosHelper { if (descriptor != null) { try { ParcelFileDescriptor parcel = descriptor.getParcelFileDescriptor(); - Method method = parcel.getClass().getMethod("getFd", new Class[] {}); - array[0] = (Integer)method.invoke(parcel); + Method method = parcel.getClass().getMethod("getFd", new Class[]{}); + array[0] = (Integer) method.invoke(parcel); array[1] = descriptor.getStartOffset(); array[2] = descriptor.getLength(); } catch (NoSuchMethodException e) { @@ -313,14 +331,14 @@ public class CocosHelper { do { Object windowInsectObj = GlobalObject.getActivity().getWindow().getDecorView().getRootWindowInsets(); - if(windowInsectObj == null) break; + if (windowInsectObj == null) break; Class windowInsets = WindowInsets.class; try { Method wiGetDisplayCutout = windowInsets.getMethod("getDisplayCutout"); Object cutout = wiGetDisplayCutout.invoke(windowInsectObj); - if(cutout == null) break; + if (cutout == null) break; Class displayCutout = cutout.getClass(); Method dcGetLeft = displayCutout.getMethod("getSafeInsetLeft"); @@ -342,15 +360,15 @@ public class CocosHelper { } catch (InvocationTargetException e) { e.printStackTrace(); } - }while(false); + } while (false); } - return new float[]{0,0,0,0}; + return new float[]{0, 0, 0, 0}; } public static void setKeepScreenOn(boolean keepScreenOn) { - if(keepScreenOn) { + if (keepScreenOn) { sActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - }else { + } else { sActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } diff --git a/native/cocos/platform/android/jni/JniCocosActivity.cpp b/native/cocos/platform/android/jni/JniCocosActivity.cpp index 7d99540274..1840e24e83 100644 --- a/native/cocos/platform/android/jni/JniCocosActivity.cpp +++ b/native/cocos/platform/android/jni/JniCocosActivity.cpp @@ -152,23 +152,23 @@ void glThreadEntry() { lk.unlock(); cc::cocosApp.cond.notify_all(); - int8_t cmd = 0; + int8_t cmd = 0; bool runInLowRate = false; while (true) { - runInLowRate = !cc::cocosApp.animating || APP_CMD_PAUSE == cc::cocosApp.appState; - if (readCommandWithTimeout(cmd, runInLowRate ? 100 : 0) > 0) { + if (readCommandWithTimeout(cmd, runInLowRate ? 50 : 0) > 0) { preExecCmd(cmd); cc::View::engineHandleCmd(cmd); postExecCmd(cmd); } - - if (game) { + if(game) { // Handle java events send by UI thread. Input events are handled here too. cc::JniHelper::callStaticVoidMethod("com.cocos.lib.CocosHelper", "flushTasksOnGameThread"); + } + if (game && cc::cocosApp.animating) { game->tick(); } if (cc::cocosApp.destroyRequested) break; diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHelper.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHelper.java index 4c972f5caf..cbea0ec5b4 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHelper.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHelper.java @@ -80,6 +80,7 @@ public class CocosHelper { private static String sObbFilePath = ""; + private static Object sTaskMtx = new Object(); private static List sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>()); /** @@ -131,13 +132,19 @@ public class CocosHelper { } public static void runOnGameThread(final Runnable runnable) { - sTaskOnGameThread.add(runnable); + synchronized (sTaskMtx) { + sTaskOnGameThread.add(runnable); + } } - + @SuppressWarnings("unused") static void flushTasksOnGameThread() { - while (sTaskOnGameThread.size() > 0) { - Runnable r = sTaskOnGameThread.remove(0); + List tmp = sTaskOnGameThread; + synchronized (sTaskMtx) { + sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>()); + } + while (tmp.size() > 0) { + Runnable r = tmp.remove(0); if (r != null) { r.run(); } From aa95a22288f21a45fc04aa5036f317cf7537e74f Mon Sep 17 00:00:00 2001 From: Zeqiang Li Date: Wed, 19 Jan 2022 16:07:03 +0800 Subject: [PATCH 14/19] subpass + invalid depth binding + valid dsAttachment format (#4198) * subpass + invalid depth binding + valid dsAttachment format * append --- .../cocos/renderer/gfx-metal/MTLCommandBuffer.mm | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/native/cocos/renderer/gfx-metal/MTLCommandBuffer.mm b/native/cocos/renderer/gfx-metal/MTLCommandBuffer.mm index 84437e462a..eb31852c15 100644 --- a/native/cocos/renderer/gfx-metal/MTLCommandBuffer.mm +++ b/native/cocos/renderer/gfx-metal/MTLCommandBuffer.mm @@ -384,6 +384,22 @@ void CCMTLCommandBuffer::updateDepthStencilState(uint32_t index, MTLRenderPassDe } } + } else if(curRenderPass->getDepthStencilAttachment().format != Format::UNKNOWN) { + if(index == 0) { + descriptor.depthAttachment.loadAction = dsAttachment.depthLoadOp == LoadOp::LOAD ? MTLLoadActionLoad : MTLLoadActionClear; + descriptor.stencilAttachment.loadAction = dsAttachment.stencilLoadOp == LoadOp::LOAD ? MTLLoadActionLoad : MTLLoadActionClear; + } else { + descriptor.depthAttachment.loadAction = MTLLoadActionLoad; + descriptor.stencilAttachment.loadAction = MTLLoadActionLoad; + } + //storeop + if(index == subpasses.size() - 1) { + descriptor.depthAttachment.storeAction = dsAttachment.depthStoreOp == StoreOp::STORE ? MTLStoreActionStore : MTLStoreActionDontCare; + descriptor.stencilAttachment.storeAction = dsAttachment.stencilStoreOp == StoreOp::STORE ? MTLStoreActionStore : MTLStoreActionDontCare; + } else { + descriptor.depthAttachment.storeAction = MTLStoreActionStore; + descriptor.stencilAttachment.storeAction = MTLStoreActionStore; + } } } From 8ffcf827486824ea2a1f820cae68d45e487b8bcb Mon Sep 17 00:00:00 2001 From: Zeqiang Li Date: Thu, 20 Jan 2022 11:47:49 +0800 Subject: [PATCH 15/19] wgpu ci (#4201) --- .../.github/workflows/compile-platforms.yml | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/native/.github/workflows/compile-platforms.yml b/native/.github/workflows/compile-platforms.yml index a7bfb1549b..a6ef4efc41 100644 --- a/native/.github/workflows/compile-platforms.yml +++ b/native/.github/workflows/compile-platforms.yml @@ -228,3 +228,33 @@ jobs: cmake --build . --config Debug -- -jobs $NUM_OF_CORES CODE_SIGNING_REQUIRED=NO CODE_SIGNING_ALLOWED=NO fi echo "Compile iOS Done!" + + compile_wgpu_mac: + name: "Emscripten" + runs-on: macos-latest + if: contains( github.event.head_commit.message, 'WGPU_CI_ON' ) + steps: + - uses: actions/checkout@v2 + - name: Download external libraries + shell: bash + run: | + EXT_VERSION=`grep version external/config.json |awk -F'"' '{print $4}'` + rm external/config.json + git clone --branch 3.4.1_wgpu_rebase --depth 1 https://github.com/hana-alice/engine-native-external external + - name: Setup Emscripten + run: | + git clone https://github.com/emscripten-core/emsdk.git ../emsdk + cd ../emsdk + ./emsdk install 2.0.26 + ./emsdk activate 2.0.26 + source ./emsdk_env.sh + emcc -v + - name: Compile + env: + COCOS_ENGINE_DEV: 1 + run: | + COCOS2DX_ROOT=$GITHUB_WORKSPACE + cd $COCOS2DX_ROOT/cocos/renderer/gfx-wgpu + $COCOS2DX_ROOT/../emsdk/upstream/emscripten/emcmake cmake . + $COCOS2DX_ROOT/../emsdk/upstream/emscripten/emmake make + echo "Compile WGPU on MacOS Done!" \ No newline at end of file From 08dadb4faea4f2c7db72d8cbe765dfd5adecdeba Mon Sep 17 00:00:00 2001 From: Zeqiang Li Date: Fri, 21 Jan 2022 09:43:21 +0800 Subject: [PATCH 16/19] wgpu ci sdk update to latest (#4202) * wgpu ci * update emsdk to latest --- native/.github/workflows/compile-platforms.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/native/.github/workflows/compile-platforms.yml b/native/.github/workflows/compile-platforms.yml index a6ef4efc41..4ac021a104 100644 --- a/native/.github/workflows/compile-platforms.yml +++ b/native/.github/workflows/compile-platforms.yml @@ -240,13 +240,13 @@ jobs: run: | EXT_VERSION=`grep version external/config.json |awk -F'"' '{print $4}'` rm external/config.json - git clone --branch 3.4.1_wgpu_rebase --depth 1 https://github.com/hana-alice/engine-native-external external + git clone --branch $EXT_VERSION --depth 1 https://github.com/cocos-creator/engine-native-external external - name: Setup Emscripten run: | git clone https://github.com/emscripten-core/emsdk.git ../emsdk cd ../emsdk - ./emsdk install 2.0.26 - ./emsdk activate 2.0.26 + ./emsdk install latest + ./emsdk activate latest source ./emsdk_env.sh emcc -v - name: Compile @@ -257,4 +257,4 @@ jobs: cd $COCOS2DX_ROOT/cocos/renderer/gfx-wgpu $COCOS2DX_ROOT/../emsdk/upstream/emscripten/emcmake cmake . $COCOS2DX_ROOT/../emsdk/upstream/emscripten/emmake make - echo "Compile WGPU on MacOS Done!" \ No newline at end of file + echo "Compile WGPU by ems on MacOS Done!" From 49c57bd9c50eccb5a54e66ccb85685d8675e20e4 Mon Sep 17 00:00:00 2001 From: timlyeee <43355299+timlyeee@users.noreply.github.com> Date: Fri, 21 Jan 2022 11:10:38 +0800 Subject: [PATCH 17/19] Add JsbBridgeWrapper for ohos (#4204) --- .../java/com/cocos/lib/JsbBridgeWrapper.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/JsbBridgeWrapper.java diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/JsbBridgeWrapper.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/JsbBridgeWrapper.java new file mode 100644 index 0000000000..6bc71bb92b --- /dev/null +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/JsbBridgeWrapper.java @@ -0,0 +1,109 @@ +/**************************************************************************** + Copyright (c) 2018-2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +package com.cocos.lib; + +import java.util.ArrayList; +import java.util.HashMap; + +public class JsbBridgeWrapper { + //Interface for listener, should be implemented and dispatched + public interface OnScriptEventListener { + void onScriptEvent(String arg); + } + /** + * Get the instance of JsbBridgetWrapper + */ + public static JsbBridgeWrapper getInstance() { + if (instance == null) { + instance = new JsbBridgeWrapper(); + } + return instance; + } + /** + * Add a listener to specified event, if the event does not exist, the wrapper will create one. Concurrent listener will be ignored + */ + public void addScriptEventListener(String eventName, OnScriptEventListener listener) { + if (eventMap.get(eventName) == null) { + eventMap.put(eventName, new ArrayList()); + } + eventMap.get(eventName).add(listener); + } + /** + * Remove listener for specified event, concurrent event will be deleted. Return false only if the event does not exist + */ + public boolean removeScriptEventListener(String eventName, OnScriptEventListener listener) { + ArrayList arr = eventMap.get(eventName); + if (arr == null) { + return false; + } + arr.remove(listener); + return true; + } + /** + * Remove all listener for event specified. + */ + public void removeAllListenersForEvent(String eventName) { + this.eventMap.remove(eventName); + } + /** + * Remove all event registered. Use it carefully! + */ + public void removeAllListeners() { + this.eventMap.clear(); + } + /** + * Dispatch the event with argument, the event should be registered in javascript, or other script language in future. + */ + public void dispatchEventToScript(String eventName, String arg) { + JsbBridge.sendToScript(eventName, arg); + } + /** + * Dispatch the event which is registered in javascript, or other script language in future. + */ + public void dispatchEventToScript(String eventName) { + JsbBridge.sendToScript(eventName); + } + + private JsbBridgeWrapper() { + JsbBridge.setCallback(new JsbBridge.ICallback() { + @Override + public void onScript(String arg0, String arg1) { + triggerEvents(arg0, arg1); + } + }); + } + + private final HashMap> eventMap = new HashMap<>(); + private static JsbBridgeWrapper instance; + + private void triggerEvents(String eventName, String arg) { + ArrayList arr = eventMap.get(eventName); + if (arr == null) + return; + for (OnScriptEventListener m : arr) { + m.onScriptEvent(arg); + } + } +} From 83ecd197213ad0068c3cd9e543144dafb13802dd Mon Sep 17 00:00:00 2001 From: timlyeee <43355299+timlyeee@users.noreply.github.com> Date: Mon, 24 Jan 2022 10:25:05 +0800 Subject: [PATCH 18/19] Divide foreground tasks and all-time tasks (#4206) * Add JsbBridgeWrapper for ohos * Divide Foreground tasks and all-time tasks * implement a taskQ class * Add ohos support --- .../com/cocos/lib/CocosEditBoxActivity.java | 6 +-- .../java/src/com/cocos/lib/CocosHelper.java | 47 +++++++++++++------ .../com/cocos/lib/CocosKeyCodeHandler.java | 4 +- .../com/cocos/lib/CocosOrientationHelper.java | 2 +- .../src/com/cocos/lib/CocosSurfaceView.java | 2 +- .../src/com/cocos/lib/CocosTouchHandler.java | 12 ++--- .../src/com/cocos/lib/CocosVideoHelper.java | 4 +- .../java/src/com/cocos/lib/CocosWebView.java | 6 +-- .../platform/android/jni/JniCocosActivity.cpp | 3 ++ .../platform/ohos/jni/JniCocosAbility.cpp | 45 ++++++++++++++---- .../java/com/cocos/lib/CocosAbilitySlice.java | 2 +- .../com/cocos/lib/CocosEditBoxAbility.java | 6 +-- .../main/java/com/cocos/lib/CocosHelper.java | 45 +++++++++++------- .../com/cocos/lib/CocosKeyCodeHandler.java | 4 +- .../com/cocos/lib/CocosOrientationHelper.java | 2 +- .../java/com/cocos/lib/CocosTouchHandler.java | 8 ++-- .../java/com/cocos/lib/CocosVideoHelper.java | 4 +- .../main/java/com/cocos/lib/CocosWebView.java | 8 ++-- 18 files changed, 135 insertions(+), 75 deletions(-) diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosEditBoxActivity.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosEditBoxActivity.java index dbf29eb63d..4bed76fe11 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosEditBoxActivity.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosEditBoxActivity.java @@ -434,7 +434,7 @@ public class CocosEditBoxActivity extends Activity { Native functions invoked by UI. **************************************************************************************/ private void onKeyboardInput(String text) { - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { CocosEditBoxActivity.onKeyboardInputNative(text); @@ -443,7 +443,7 @@ public class CocosEditBoxActivity extends Activity { } private void onKeyboardComplete(String text) { - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { CocosEditBoxActivity.onKeyboardCompleteNative(text); @@ -452,7 +452,7 @@ public class CocosEditBoxActivity extends Activity { } private void onKeyboardConfirm(String text) { - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { CocosEditBoxActivity.onKeyboardConfirmNative(text); diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java index f163fc317b..dfcfc85abf 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java @@ -58,8 +58,11 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedList; import java.util.List; import java.util.Locale; +import java.util.Queue; +import java.util.concurrent.locks.ReentrantLock; public class CocosHelper { @@ -86,9 +89,26 @@ public class CocosHelper { // The OBB file private static ZipResourceFile sOBBFile = null; - private static Object sTaskMtx = new Object(); - private static List sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>()); + static class LockedTaskQ { + private final Object readMtx = new Object(); + private Queue sTaskQ = new LinkedList<>(); + public synchronized void addTask(Runnable runnable) { + sTaskQ.add(runnable); + } + public void runTasks(){ + Queue tmp; + synchronized (readMtx) { + tmp = sTaskQ; + sTaskQ = new LinkedList<>(); + } + for(Runnable runnable : tmp){ + runnable.run(); + } + } + } + private static LockedTaskQ sTaskQOnGameThread = new LockedTaskQ(); + private static LockedTaskQ sForegroundTaskQOnGameThread = new LockedTaskQ(); /** * Battery receiver to getting battery level. */ @@ -120,23 +140,20 @@ public class CocosHelper { context.unregisterReceiver(sBatteryReceiver); } + //Run on game thread forever, no matter foreground or background public static void runOnGameThread(final Runnable runnable) { - synchronized (sTaskMtx) { - sTaskOnGameThread.add(runnable); - } + sTaskQOnGameThread.addTask(runnable); } static void flushTasksOnGameThread() { - List tmp = sTaskOnGameThread; - synchronized (sTaskMtx) { - sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>()); - } - while (tmp.size() > 0) { - Runnable r = tmp.remove(0); - if (r != null) { - r.run(); - } - } + sTaskQOnGameThread.runTasks(); + } + public static void runOnGameThreadAtForeground(final Runnable runnable) { + sForegroundTaskQOnGameThread.addTask(runnable); + } + + static void flushTasksOnGameThreadAtForeground() { + sForegroundTaskQOnGameThread.runTasks(); } public static int getNetworkType() { diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosKeyCodeHandler.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosKeyCodeHandler.java index f183a638e5..834d11792d 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosKeyCodeHandler.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosKeyCodeHandler.java @@ -50,7 +50,7 @@ public class CocosKeyCodeHandler { case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: case KeyEvent.KEYCODE_DPAD_CENTER: - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleKeyDown(keyCode); @@ -73,7 +73,7 @@ public class CocosKeyCodeHandler { case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: case KeyEvent.KEYCODE_DPAD_CENTER: - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleKeyUp(keyCode); diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosOrientationHelper.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosOrientationHelper.java index d4d09e4479..43c4b3d9f6 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosOrientationHelper.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosOrientationHelper.java @@ -49,7 +49,7 @@ public class CocosOrientationHelper extends OrientationEventListener { int curOrientation = CocosHelper.getDeviceRotation(); if (curOrientation != mCurrentOrientation) { mCurrentOrientation = CocosHelper.getDeviceRotation(); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { nativeOnOrientationChanged(mCurrentOrientation); diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosSurfaceView.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosSurfaceView.java index 88a67dfd12..d808ce745d 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosSurfaceView.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosSurfaceView.java @@ -41,7 +41,7 @@ public class CocosSurfaceView extends SurfaceView { @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { nativeOnSizeChanged(w, h); diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosTouchHandler.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosTouchHandler.java index 4e8953ebd0..499c4d91ba 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosTouchHandler.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosTouchHandler.java @@ -60,7 +60,7 @@ public class CocosTouchHandler { final int idPointerDown = pMotionEvent.getPointerId(indexPointerDown); final float xPointerDown = pMotionEvent.getX(indexPointerDown); final float yPointerDown = pMotionEvent.getY(indexPointerDown); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionDown(idPointerDown, xPointerDown, yPointerDown); @@ -79,7 +79,7 @@ public class CocosTouchHandler { final float xDown = xs[0]; final float yDown = ys[0]; - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionDown(idDown, xDown, yDown); @@ -89,7 +89,7 @@ public class CocosTouchHandler { break; case MotionEvent.ACTION_MOVE: - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionMove(ids, xs, ys); @@ -103,7 +103,7 @@ public class CocosTouchHandler { final int idPointerUp = pMotionEvent.getPointerId(indexPointUp); final float xPointerUp = pMotionEvent.getX(indexPointUp); final float yPointerUp = pMotionEvent.getY(indexPointUp); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionUp(idPointerUp, xPointerUp, yPointerUp); @@ -117,7 +117,7 @@ public class CocosTouchHandler { final int idUp = pMotionEvent.getPointerId(0); final float xUp = xs[0]; final float yUp = ys[0]; - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionUp(idUp, xUp, yUp); @@ -127,7 +127,7 @@ public class CocosTouchHandler { break; case MotionEvent.ACTION_CANCEL: - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionCancel(ids, xs, ys); diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosVideoHelper.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosVideoHelper.java index e0f7f86486..d1ce6d2433 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosVideoHelper.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosVideoHelper.java @@ -170,7 +170,7 @@ public class CocosVideoHelper { @Override public void onVideoEvent(int tag,int event) { - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { nativeExecuteVideoCallback(tag, event); @@ -282,7 +282,7 @@ public class CocosVideoHelper { CocosVideoView videoView = sVideoViews.get(key); if (videoView != null) { videoView.setFullScreenEnabled(false); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { nativeExecuteVideoCallback(key, KeyEventBack); diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java index 09c713af4a..541e7921e7 100755 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java @@ -109,7 +109,7 @@ import java.util.concurrent.CountDownLatch; try { URI uri = URI.create(urlString); if (uri != null && uri.getScheme().equals(mJSScheme)) { - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { CocosWebViewHelper._onJsCallback(mViewTag, urlString); @@ -141,7 +141,7 @@ import java.util.concurrent.CountDownLatch; public void onPageFinished(WebView view, final String url) { super.onPageFinished(view, url); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { CocosWebViewHelper._didFinishLoading(mViewTag, url); @@ -153,7 +153,7 @@ import java.util.concurrent.CountDownLatch; public void onReceivedError(WebView view, int errorCode, String description, final String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { CocosWebViewHelper._didFailLoading(mViewTag, failingUrl); diff --git a/native/cocos/platform/android/jni/JniCocosActivity.cpp b/native/cocos/platform/android/jni/JniCocosActivity.cpp index 1840e24e83..bfdc1e9b03 100644 --- a/native/cocos/platform/android/jni/JniCocosActivity.cpp +++ b/native/cocos/platform/android/jni/JniCocosActivity.cpp @@ -169,6 +169,9 @@ void glThreadEntry() { "flushTasksOnGameThread"); } if (game && cc::cocosApp.animating) { + + cc::JniHelper::callStaticVoidMethod("com.cocos.lib.CocosHelper", + "flushTasksOnGameThreadAtForeground"); game->tick(); } if (cc::cocosApp.destroyRequested) break; diff --git a/native/cocos/platform/ohos/jni/JniCocosAbility.cpp b/native/cocos/platform/ohos/jni/JniCocosAbility.cpp index 398b05fcc7..a397391234 100644 --- a/native/cocos/platform/ohos/jni/JniCocosAbility.cpp +++ b/native/cocos/platform/ohos/jni/JniCocosAbility.cpp @@ -88,6 +88,28 @@ int readCommand(CommandMsg *msg) { return read(pipeRead, msg, sizeof(*msg)); } +int readCommandWithTimeout(CommandMsg *msg, int delayMS) { + if (delayMS > 0) { + static fd_set fdSet; + static timeval timeout; + + timeout = {delayMS / 1000, (delayMS % 1000) * 1000}; + FD_ZERO(&fdSet); + FD_SET(pipeRead, &fdSet); + + auto ret = select(pipeRead + 1, &fdSet, nullptr, nullptr, &timeout); + if (ret < 0) { + LOGV("failed to run select(..): %s\n", strerror(errno)); + return ret; + } + + if (ret == 0) { + return 0; + } + } + return read(pipeRead, msg, sizeof(*msg)); +} + void handlePauseResume(int8_t cmd) { LOGV("activityState=%d", cmd); cc::cocosApp.activityState = cmd; @@ -139,27 +161,32 @@ void glThreadEntry() { cc::cocosApp.glThreadPromise.set_value(); cc::cocosApp.running = true; - int cmd = 0; CommandMsg msg; + bool runInLowRate = false; while (true) { - if (readCommand(&msg) > 0) { - cmd = msg.cmd; - preExecCmd(cmd); - cc::View::engineHandleCmd(cmd); - postExecCmd(cmd); + runInLowRate = !cc::cocosApp.animating || ABILITY_CMD_PAUSE == cc::cocosApp.activityState; + if (readCommandWithTimeout(&msg, runInLowRate ? 50 : 0) > 0) { +// if(readCommand(0&msg)>0){ + preExecCmd(msg.cmd); + cc::View::engineHandleCmd(msg.cmd); + postExecCmd(msg.cmd); if (msg.callback) { msg.callback(); } } +// if (!cc::cocosApp.animating || ABILITY_CMD_PAUSE == cc::cocosApp.activityState) { +// std::this_thread::yield(); +// } - if (!cc::cocosApp.animating || ABILITY_CMD_PAUSE == cc::cocosApp.activityState) { - std::this_thread::yield(); + if (game) { + cc::JniHelper::callStaticVoidMethod("com.cocos.lib.CocosHelper", + "flushTasksOnGameThread"); } if (game && cc::cocosApp.animating) { // Handle java events send by UI thread. Input events are handled here too. cc::JniHelper::callStaticVoidMethod("com.cocos.lib.CocosHelper", - "flushTasksOnGameThread"); + "flushTasksOnGameThreadAtForeground"); game->tick(); } diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosAbilitySlice.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosAbilitySlice.java index 7f0e48c006..1116d108a4 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosAbilitySlice.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosAbilitySlice.java @@ -152,7 +152,7 @@ public class CocosAbilitySlice extends AbilitySlice implements SurfaceOps.Callba mSurfaceProvider.setTouchEventListener(this); mSurfaceProvider.setLayoutRefreshedListener(component -> { // dispatch resize event - CocosHelper.runOnGameThread(()->{ + CocosHelper.runOnGameThreadAtForeground(()->{ onOrientationChangedNative(getDisplayOrientation(), component.getWidth(), component.getHeight()); }); }); diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosEditBoxAbility.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosEditBoxAbility.java index 738b05b8ea..c7aa591cdd 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosEditBoxAbility.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosEditBoxAbility.java @@ -397,19 +397,19 @@ public class CocosEditBoxAbility extends AbilitySlice { Native functions invoked by UI. **************************************************************************************/ private void onKeyboardInput(String text) { - CocosHelper.runOnGameThread(() -> + CocosHelper.runOnGameThreadAtForeground(() -> CocosEditBoxAbility.onKeyboardInputNative(text) ); } private void onKeyboardComplete(String text) { - CocosHelper.runOnGameThread(() -> + CocosHelper.runOnGameThreadAtForeground(() -> CocosEditBoxAbility.onKeyboardCompleteNative(text) ); } private void onKeyboardConfirm(String text) { - CocosHelper.runOnGameThread(() -> + CocosHelper.runOnGameThreadAtForeground(() -> CocosEditBoxAbility.onKeyboardConfirmNative(text) ); } diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHelper.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHelper.java index cbea0ec5b4..0d8bfbfab2 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHelper.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHelper.java @@ -80,9 +80,26 @@ public class CocosHelper { private static String sObbFilePath = ""; - private static Object sTaskMtx = new Object(); - private static List sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>()); + static class LockedTaskQ { + private final Object readMtx = new Object(); + private Queue sTaskQ = new LinkedList<>(); + public synchronized void addTask(Runnable runnable) { + sTaskQ.add(runnable); + } + public void runTasks(){ + Queue tmp; + synchronized (readMtx) { + tmp = sTaskQ; + sTaskQ = new LinkedList<>(); + } + for(Runnable runnable : tmp){ + runnable.run(); + } + } + } + private static LockedTaskQ sTaskQOnGameThread = new LockedTaskQ(); + private static LockedTaskQ sForegroundTaskQOnGameThread = new LockedTaskQ(); /** * Battery receiver to getting battery level. */ @@ -131,24 +148,20 @@ public class CocosHelper { } } + //Run on game thread forever, no matter foreground or background public static void runOnGameThread(final Runnable runnable) { - synchronized (sTaskMtx) { - sTaskOnGameThread.add(runnable); - } + sTaskQOnGameThread.addTask(runnable); } - @SuppressWarnings("unused") static void flushTasksOnGameThread() { - List tmp = sTaskOnGameThread; - synchronized (sTaskMtx) { - sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>()); - } - while (tmp.size() > 0) { - Runnable r = tmp.remove(0); - if (r != null) { - r.run(); - } - } + sTaskQOnGameThread.runTasks(); + } + public static void runOnGameThreadAtForeground(final Runnable runnable) { + sForegroundTaskQOnGameThread.addTask(runnable); + } + @SuppressWarnings("unused") + static void flushTasksOnGameThreadAtForeground() { + sForegroundTaskQOnGameThread.runTasks(); } @SuppressWarnings("unused") diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosKeyCodeHandler.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosKeyCodeHandler.java index fc5b58687e..26f19e9134 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosKeyCodeHandler.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosKeyCodeHandler.java @@ -51,7 +51,7 @@ public class CocosKeyCodeHandler { case KeyEvent.KEY_ENTER: case KeyEvent.KEY_MEDIA_PLAY_PAUSE: case KeyEvent.KEY_DPAD_CENTER: - CocosHelper.runOnGameThread(() -> handleKeyDown(keyCode)); + CocosHelper.runOnGameThreadAtForeground(() -> handleKeyDown(keyCode)); return true; default: return false; @@ -69,7 +69,7 @@ public class CocosKeyCodeHandler { case KeyEvent.KEY_ENTER: case KeyEvent.KEY_MEDIA_PLAY_PAUSE: case KeyEvent.KEY_DPAD_CENTER: - CocosHelper.runOnGameThread(() -> handleKeyDown(keyCode)); + CocosHelper.runOnGameThreadAtForeground(() -> handleKeyDown(keyCode)); return true; default: return false; diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosOrientationHelper.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosOrientationHelper.java index a1171a5643..62b14c9028 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosOrientationHelper.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosOrientationHelper.java @@ -63,7 +63,7 @@ public class CocosOrientationHelper implements ICategoryOrientationDataCallback final int curOrientation = CocosHelper.getDeviceRotation(); if (curOrientation != mCurrentOrientation) { mCurrentOrientation = curOrientation; - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { nativeOnOrientationChanged(mCurrentOrientation); diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosTouchHandler.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosTouchHandler.java index 1b5323244c..4f99b0904f 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosTouchHandler.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosTouchHandler.java @@ -69,7 +69,7 @@ public class CocosTouchHandler { MmiPoint pos = pMotionEvent.getPointerPosition(indexPointerDown); final float xPointerDown = pos.getX(); final float yPointerDown = pos.getY(); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionDown(idPointerDown, xPointerDown, yPointerDown); @@ -78,7 +78,7 @@ public class CocosTouchHandler { break; case TouchEvent.POINT_MOVE: - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionMove(ids, xs, ys); @@ -95,7 +95,7 @@ public class CocosTouchHandler { MmiPoint posUP = pMotionEvent.getPointerPosition(indexPointUp); final float xPointerUp = posUP.getX(); final float yPointerUp = posUP.getY(); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionUp(idPointerUp, xPointerUp, yPointerUp); @@ -105,7 +105,7 @@ public class CocosTouchHandler { break; case TouchEvent.CANCEL: - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { handleActionCancel(ids, xs, ys); diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosVideoHelper.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosVideoHelper.java index a36e726674..2f787d76be 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosVideoHelper.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosVideoHelper.java @@ -171,7 +171,7 @@ public class CocosVideoHelper { @Override public void onVideoEvent(int tag,int event) { - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { nativeExecuteVideoCallback(tag, event); @@ -294,7 +294,7 @@ public class CocosVideoHelper { CocosVideoView videoView = sVideoViews.get(key); if (videoView != null) { videoView.setFullScreenEnabled(false); - CocosHelper.runOnGameThread(new Runnable() { + CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { nativeExecuteVideoCallback(key, KeyEventBack); diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosWebView.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosWebView.java index 90dc06e583..d88137adf2 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosWebView.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosWebView.java @@ -78,7 +78,7 @@ public class CocosWebView extends WebView { try { URI uri = new URI(request.getRequestUrl().toString()); if (uri.getScheme().equals(mJSScheme)) { - CocosHelper.runOnGameThread( + CocosHelper.runOnGameThreadAtForeground( () -> CocosWebViewHelper._onJsCallback(mViewTag, urlString) ); return true; @@ -92,7 +92,7 @@ public class CocosWebView extends WebView { ); // run worker on cocos thread - CocosHelper.runOnGameThread(shouldStartLoadingCB); + CocosHelper.runOnGameThreadAtForeground(shouldStartLoadingCB); // wait for result from cocos thread try { @@ -109,7 +109,7 @@ public class CocosWebView extends WebView { public void onPageLoaded(WebView view, final String url) { super.onPageLoaded(view, url); - CocosHelper.runOnGameThread( + CocosHelper.runOnGameThreadAtForeground( () -> CocosWebViewHelper._didFinishLoading(mViewTag, url) ); } @@ -118,7 +118,7 @@ public class CocosWebView extends WebView { public void onError(WebView view, ResourceRequest request, ResourceError error) { super.onError(view, request, error); final String failingUrl = request.getRequestUrl().toString(); - CocosHelper.runOnGameThread( + CocosHelper.runOnGameThreadAtForeground( () -> CocosWebViewHelper._didFailLoading(mViewTag, failingUrl) ); } From 45f096ead8b26934921a50c5c41fbc6403b0fb5d Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 24 Jan 2022 16:42:39 +0800 Subject: [PATCH 19/19] fix compiling error --- .../platform/java/jni/glue/JniNativeGlue.cpp | 22 ++----------------- .../platform/java/jni/glue/MessagePipe.cpp | 22 +++++++++++++++++++ .../platform/java/jni/glue/MessagePipe.h | 1 + 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/native/cocos/platform/java/jni/glue/JniNativeGlue.cpp b/native/cocos/platform/java/jni/glue/JniNativeGlue.cpp index a5968e35c8..e5522b4253 100644 --- a/native/cocos/platform/java/jni/glue/JniNativeGlue.cpp +++ b/native/cocos/platform/java/jni/glue/JniNativeGlue.cpp @@ -131,25 +131,7 @@ int JniNativeGlue::readCommand(CommandMsg* msg) { } int JniNativeGlue::readCommandWithTimeout(CommandMsg* cmd, int delayMS) { - if (delayMS > 0) { - static fd_set fdSet; - static timeval timeout; - - timeout = {delayMS / 1000, (delayMS % 1000) * 1000}; - FD_ZERO(&fdSet); - FD_SET(pipeRead, &fdSet); - - auto ret = select(pipeRead + 1, &fdSet, nullptr, nullptr, &timeout); - if (ret < 0) { - LOGV("failed to run select(..): %s\n", strerror(errno)); - return ret; - } - - if (ret == 0) { - return 0; - } - } - return readCommand(cmd); + return _messagePipe->readCommandWithTimeout(cmd, sizeof(CommandMsg), delayMS); } void JniNativeGlue::setEventDispatch(IEventDispatch* eventDispatcher) { @@ -193,7 +175,7 @@ void JniNativeGlue::onLowMemory() { void JniNativeGlue::execCommand() { static CommandMsg msg; static bool runInLowRate{false}; - runInLowRate = !_animating || APP_CMD_PAUSE == _appState; + runInLowRate = !_animating || JniCommand::JNI_CMD_PAUSE == _appState; if (readCommandWithTimeout(&msg, runInLowRate ? 50 : 0) > 0) { preExecCmd(msg.cmd); diff --git a/native/cocos/platform/java/jni/glue/MessagePipe.cpp b/native/cocos/platform/java/jni/glue/MessagePipe.cpp index 38d1f75d62..bf26a3c106 100644 --- a/native/cocos/platform/java/jni/glue/MessagePipe.cpp +++ b/native/cocos/platform/java/jni/glue/MessagePipe.cpp @@ -57,6 +57,28 @@ int MessagePipe::readCommand(int8_t& cmd) const { return read(_pipeRead, &cmd, sizeof(cmd)); } +int MessagePipe::readCommandWithTimeout(void* msg, int32_t size, int delayMS) { + if (delayMS > 0) { + static fd_set fdSet; + static timeval timeout; + + timeout = {delayMS / 1000, (delayMS % 1000) * 1000}; + FD_ZERO(&fdSet); + FD_SET(_pipeRead, &fdSet); + + auto ret = select(_pipeRead + 1, &fdSet, nullptr, nullptr, &timeout); + if (ret < 0) { + LOGV("failed to run select(..): %s\n", strerror(errno)); + return ret; + } + + if (ret == 0) { + return 0; + } + } + return readCommand(msg, size); +} + void MessagePipe::writeCommand(void* msg, int32_t size) const { write(_pipeWrite, msg, size); } diff --git a/native/cocos/platform/java/jni/glue/MessagePipe.h b/native/cocos/platform/java/jni/glue/MessagePipe.h index 7674c3fe38..e6ed5243fd 100644 --- a/native/cocos/platform/java/jni/glue/MessagePipe.h +++ b/native/cocos/platform/java/jni/glue/MessagePipe.h @@ -38,6 +38,7 @@ public: int readCommand(int8_t& cmd) const; void writeCommand(void* msg, int32_t size) const; int readCommand(void* msg, int32_t size) const; + int readCommandWithTimeout(void* msg, int32_t size, int delayMS); private: int _pipeRead = 0;