From 7bcd1b05ab9aecb840febc3da7760e3bfa19cd40 Mon Sep 17 00:00:00 2001 From: coulsonwang Date: Tue, 31 Dec 2019 16:37:51 +0800 Subject: [PATCH] fix depth issue (#2037) * fix index buffer issue * fix depth issue --- native/cocos/renderer/gfx-gles2/GLES2Commands.cc | 6 +++--- native/cocos/renderer/gfx-gles2/GLES2Context.h | 1 + .../cocos/renderer/gfx-gles2/GLES2Framebuffer.cc | 15 +++++++++++---- native/cocos/renderer/gfx-gles2/GLES2Window.cc | 4 +++- native/cocos/renderer/gfx-gles3/GLES3Commands.cc | 6 +++--- native/cocos/renderer/gfx-gles3/GLES3Context.h | 1 + .../cocos/renderer/gfx-gles3/GLES3Framebuffer.cc | 16 ++++++++++++---- native/cocos/renderer/gfx-gles3/GLES3Window.cc | 4 +++- 8 files changed, 37 insertions(+), 16 deletions(-) diff --git a/native/cocos/renderer/gfx-gles2/GLES2Commands.cc b/native/cocos/renderer/gfx-gles2/GLES2Commands.cc index 51d97e6f80..0728f6f8cd 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Commands.cc +++ b/native/cocos/renderer/gfx-gles2/GLES2Commands.cc @@ -1157,7 +1157,7 @@ void GLES2CmdFuncExecuteCmds(GLES2Device* device, GLES2CmdPackage* cmd_package) for (uint i = 0; i < cmd_package->cmd_types.Size(); ++i) { GFXCmdType cmd_type = cmd_package->cmd_types[i]; - uint cmd_idx = cmd_indices[(int)cmd_type]; + uint& cmd_idx = cmd_indices[(int)cmd_type]; switch (cmd_type) { case GFXCmdType::BEGIN_RENDER_PASS: { @@ -1230,9 +1230,8 @@ void GLES2CmdFuncExecuteCmds(GLES2Device* device, GLES2CmdPackage* cmd_package) switch (gpu_render_pass->depth_stencil_attachment.depth_load_op) { case GFXLoadOp::LOAD: break; // GL default behaviour case GFXLoadOp::CLEAR: { - if (cache->dss.depth_write) { glDepthMask(true); - } + cache->dss.depth_write = true; glClearDepthf(cmd->clear_depth); gl_clears |= GL_DEPTH_BUFFER_BIT; break; @@ -1919,6 +1918,7 @@ void GLES2CmdFuncExecuteCmds(GLES2Device* device, GLES2CmdPackage* cmd_package) default: break; } + cmd_idx++; } } diff --git a/native/cocos/renderer/gfx-gles2/GLES2Context.h b/native/cocos/renderer/gfx-gles2/GLES2Context.h index 7fe0342cc7..aeed6a6d2a 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Context.h +++ b/native/cocos/renderer/gfx-gles2/GLES2Context.h @@ -46,6 +46,7 @@ class CC_GLES2_API GLES2Context : public GFXContext { #if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) CC_INLINE intptr_t eagl_context() const { return eagl_context_; } CC_INLINE intptr_t eagl_shared_ctx() const { return eagl_shared_ctx_; } + CC_INLINE uint getDefaultFramebuffer() const { return _defaultFBO; } #else CC_INLINE NativeDisplayType native_display() const { return native_display_; } CC_INLINE EGLDisplay egl_display() const { return egl_display_; } diff --git a/native/cocos/renderer/gfx-gles2/GLES2Framebuffer.cc b/native/cocos/renderer/gfx-gles2/GLES2Framebuffer.cc index d77a062fa1..96fc7d28d2 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Framebuffer.cc +++ b/native/cocos/renderer/gfx-gles2/GLES2Framebuffer.cc @@ -3,6 +3,7 @@ #include "GLES2RenderPass.h" #include "GLES2Commands.h" #include "GLES2TextureView.h" +#include "GLES2Context.h" NS_CC_BEGIN @@ -39,15 +40,21 @@ bool GLES2Framebuffer::Initialize(const GFXFramebufferInfo &info) { GLES2CmdFuncCreateFramebuffer((GLES2Device*)device_, gpu_fbo_); } - +#if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) + else + { + gpu_fbo_->gl_fbo = static_cast(device_->context())->getDefaultFramebuffer(); + } +#endif return true; } void GLES2Framebuffer::Destroy() { if (gpu_fbo_) { - CC_DELETE(gpu_fbo_); - GLES2CmdFuncDestroyFramebuffer((GLES2Device*)device_, gpu_fbo_); - gpu_fbo_ = nullptr; + if(is_offscreen()) + GLES2CmdFuncDestroyFramebuffer((GLES2Device*)device_, gpu_fbo_); + CC_DELETE(gpu_fbo_); + gpu_fbo_ = nullptr; } } diff --git a/native/cocos/renderer/gfx-gles2/GLES2Window.cc b/native/cocos/renderer/gfx-gles2/GLES2Window.cc index aa12b5427a..99505c4779 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Window.cc +++ b/native/cocos/renderer/gfx-gles2/GLES2Window.cc @@ -84,6 +84,7 @@ bool GLES2Window::Initialize(const GFXWindowInfo &info) { depth_stencil_texture_ = device_->CreateGFXTexture(depth_stecnil_tex_info); GFXTextureViewInfo depth_stecnil_tex_view_info; + depth_stecnil_tex_view_info.texture = depth_stencil_texture_; depth_stecnil_tex_view_info.type = GFXTextureViewType::TV2D; depth_stecnil_tex_view_info.format = color_fmt_; depth_stecnil_tex_view_info.base_level = 0; @@ -96,7 +97,8 @@ bool GLES2Window::Initialize(const GFXWindowInfo &info) { GFXFramebufferInfo fbo_info; fbo_info.render_pass = render_pass_; - fbo_info.color_views.push_back(color_tex_view_); + if(color_tex_view_) + fbo_info.color_views.push_back(color_tex_view_); fbo_info.depth_stencil_view = depth_stencil_tex_view_; fbo_info.is_offscreen = is_offscreen_; framebuffer_ = device_->CreateGFXFramebuffer(fbo_info); diff --git a/native/cocos/renderer/gfx-gles3/GLES3Commands.cc b/native/cocos/renderer/gfx-gles3/GLES3Commands.cc index a8d73b9fab..e897206a29 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Commands.cc +++ b/native/cocos/renderer/gfx-gles3/GLES3Commands.cc @@ -1278,7 +1278,7 @@ void GLES3CmdFuncExecuteCmds(GLES3Device* device, GLES3CmdPackage* cmd_package) for (uint i = 0; i < cmd_package->cmd_types.Size(); ++i) { GFXCmdType cmd_type = cmd_package->cmd_types[i]; - uint cmd_idx = cmd_indices[(int)cmd_type]; + uint& cmd_idx = cmd_indices[(int)cmd_type]; switch (cmd_type) { case GFXCmdType::BEGIN_RENDER_PASS: { @@ -1358,9 +1358,8 @@ void GLES3CmdFuncExecuteCmds(GLES3Device* device, GLES3CmdPackage* cmd_package) switch (gpu_render_pass->depth_stencil_attachment.depth_load_op) { case GFXLoadOp::LOAD: break; // GL default behaviour case GFXLoadOp::CLEAR: { - if (cache->dss.depth_write) { glDepthMask(true); - } + cache->dss.depth_write = true; glClearDepthf(cmd->clear_depth); gl_clears |= GL_DEPTH_BUFFER_BIT; break; @@ -1884,6 +1883,7 @@ void GLES3CmdFuncExecuteCmds(GLES3Device* device, GLES3CmdPackage* cmd_package) default: break; } + cmd_idx++; } } diff --git a/native/cocos/renderer/gfx-gles3/GLES3Context.h b/native/cocos/renderer/gfx-gles3/GLES3Context.h index 7875b05331..64c8b6316c 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Context.h +++ b/native/cocos/renderer/gfx-gles3/GLES3Context.h @@ -46,6 +46,7 @@ class CC_GLES3_API GLES3Context : public GFXContext { #if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) CC_INLINE intptr_t eagl_context() const { return eagl_context_; } CC_INLINE intptr_t eagl_shared_ctx() const { return eagl_shared_ctx_; } + CC_INLINE uint getDefaultFramebuffer() const { return _defaultFBO; } #else CC_INLINE NativeDisplayType native_display() const { return native_display_; } CC_INLINE EGLDisplay egl_display() const { return egl_display_; } diff --git a/native/cocos/renderer/gfx-gles3/GLES3Framebuffer.cc b/native/cocos/renderer/gfx-gles3/GLES3Framebuffer.cc index 34a76849c7..bc0b4823eb 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Framebuffer.cc +++ b/native/cocos/renderer/gfx-gles3/GLES3Framebuffer.cc @@ -3,6 +3,7 @@ #include "GLES3RenderPass.h" #include "GLES3Commands.h" #include "GLES3TextureView.h" +#include "GLES3Context.h" NS_CC_BEGIN @@ -39,15 +40,22 @@ bool GLES3Framebuffer::Initialize(const GFXFramebufferInfo &info) { GLES3CmdFuncCreateFramebuffer((GLES3Device*)device_, gpu_fbo_); } - +#if (CC_PLATFORM == CC_PLATFORM_MAC_IOS) + else + { + gpu_fbo_->gl_fbo = static_cast(device_->context())->getDefaultFramebuffer(); + } +#endif + return true; } void GLES3Framebuffer::Destroy() { if (gpu_fbo_) { - CC_DELETE(gpu_fbo_); - GLES3CmdFuncDestroyFramebuffer((GLES3Device*)device_, gpu_fbo_); - gpu_fbo_ = nullptr; + if(is_offscreen()) + GLES3CmdFuncDestroyFramebuffer((GLES3Device*)device_, gpu_fbo_); + CC_DELETE(gpu_fbo_); + gpu_fbo_ = nullptr; } } diff --git a/native/cocos/renderer/gfx-gles3/GLES3Window.cc b/native/cocos/renderer/gfx-gles3/GLES3Window.cc index 9a5af40998..b671aed10b 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Window.cc +++ b/native/cocos/renderer/gfx-gles3/GLES3Window.cc @@ -84,6 +84,7 @@ bool GLES3Window::Initialize(const GFXWindowInfo &info) { depth_stencil_texture_ = device_->CreateGFXTexture(depth_stecnil_tex_info); GFXTextureViewInfo depth_stecnil_tex_view_info; + depth_stecnil_tex_view_info.texture = depth_stencil_texture_; depth_stecnil_tex_view_info.type = GFXTextureViewType::TV2D; depth_stecnil_tex_view_info.format = color_fmt_; depth_stecnil_tex_view_info.base_level = 0; @@ -96,7 +97,8 @@ bool GLES3Window::Initialize(const GFXWindowInfo &info) { GFXFramebufferInfo fbo_info; fbo_info.render_pass = render_pass_; - fbo_info.color_views.push_back(color_tex_view_); + if(color_tex_view_) + fbo_info.color_views.push_back(color_tex_view_); fbo_info.depth_stencil_view = depth_stencil_tex_view_; fbo_info.is_offscreen = is_offscreen_; framebuffer_ = device_->CreateGFXFramebuffer(fbo_info);