From fa2c1146366557e5ce779d8b5c2575579dfa6389 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 17 Sep 2020 13:44:03 +0200 Subject: [PATCH] WARNING: struct RenderTexture2D: Removed `depthTexture` Not required anymore, attachment type is queried when required --- src/raylib.h | 5 ++--- src/rlgl.h | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index 5c2ae13c..73d75429 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -247,9 +247,8 @@ typedef Texture2D TextureCubemap; // RenderTexture2D type, for texture rendering typedef struct RenderTexture2D { unsigned int id; // OpenGL Framebuffer Object (FBO) id - Texture2D texture; // Color buffer attachment texture - Texture2D depth; // Depth buffer attachment texture - bool depthTexture; // Track if depth attachment is a texture or renderbuffer + Texture texture; // Color buffer attachment texture + Texture depth; // Depth buffer attachment texture } RenderTexture2D; // RenderTexture type, same as RenderTexture2D diff --git a/src/rlgl.h b/src/rlgl.h index b7596b0e..a90e1f4c 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -250,7 +250,6 @@ typedef unsigned char byte; unsigned int id; // OpenGL framebuffer (fbo) id Texture2D texture; // Color buffer attachment texture Texture2D depth; // Depth buffer attachment texture - bool depthTexture; // Track if depth attachment is a texture or renderbuffer } RenderTexture2D; // RenderTexture type, same as RenderTexture2D @@ -1453,9 +1452,18 @@ void rlDeleteTextures(unsigned int id) } // Unload render texture from GPU memory +// NOTE: All attached textures/cubemaps/renderbuffers are also deleted void rlDeleteRenderTextures(RenderTexture2D target) { #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(SUPPORT_RENDER_TEXTURES_HINT) + int depthType = 0; + glBindFramebuffer(GL_FRAMEBUFFER, target.id); // Bind framebuffer to query depth texture type + glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &depthType); + glBindFramebuffer(GL_FRAMEBUFFER, 0); // Unbind framebuffer to delete textures + + // NOTE: If a texture object is deleted while its image is attached to the *currently bound* framebuffer, + // the texture image is automatically detached from the currently bound framebuffer. + if (target.texture.id > 0) { glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); // Detach texture from FBO @@ -1463,12 +1471,12 @@ void rlDeleteRenderTextures(RenderTexture2D target) } if (target.depth.id > 0) { - if (target.depthTexture) + if (depthType == GL_TEXTURE) { glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0); glDeleteTextures(1, &target.depth.id); } - else + else if (depthType == GL_RENDERBUFFER) { glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0); glDeleteRenderbuffers(1, &target.depth.id); @@ -2276,7 +2284,7 @@ RenderTexture2D rlLoadRenderTexture(int width, int height, int format, int depth RenderTexture2D target = { 0 }; #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(SUPPORT_RENDER_TEXTURES_HINT) - if (useDepthTexture && RLGL.ExtSupported.texDepth) target.depthTexture = true; + if (useDepthTexture && !RLGL.ExtSupported.texDepth) useDepthTexture = false; // Create the framebuffer object glGenFramebuffers(1, &target.id); @@ -2334,8 +2342,11 @@ void rlRenderTextureAttach(RenderTexture2D target, unsigned int id, int attachTy if (attachType == 0) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, id, 0); else if (attachType == 1) { - if (target.depthTexture) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, id, 0); - else glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, id); + int depthType = 0; + glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &depthType); + + if (depthType == GL_TEXTURE) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, id, 0); + else if (depthType == GL_RENDERBUFFER) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, id); } glBindFramebuffer(GL_FRAMEBUFFER, 0);