From b7be6a6f865f00ed47414a9a57845e47b68489f2 Mon Sep 17 00:00:00 2001 From: Andre Weissflog Date: Thu, 27 Nov 2025 18:26:30 +0100 Subject: sokol_gfx.h gl: fix glDrawBuffers for the case that no color attachments are used --- sokol_gfx.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'sokol_gfx.h') diff --git a/sokol_gfx.h b/sokol_gfx.h index a6fa985b..c3fe87b9 100644 --- a/sokol_gfx.h +++ b/sokol_gfx.h @@ -5518,10 +5518,8 @@ inline int sg_append_buffer(sg_buffer buf_id, const sg_range& data) { return sg_ #define GL_PROGRAM_POINT_SIZE 0x8642 #define GL_DEPTH_ATTACHMENT 0x8D00 #define GL_DEPTH_STENCIL_ATTACHMENT 0x821A - #define GL_COLOR_ATTACHMENT2 0x8CE2 #define GL_COLOR_ATTACHMENT0 0x8CE0 #define GL_R16F 0x822D - #define GL_COLOR_ATTACHMENT22 0x8CF6 #define GL_DRAW_FRAMEBUFFER 0x8CA9 #define GL_FRAMEBUFFER_COMPLETE 0x8CD5 #define GL_NUM_EXTENSIONS 0x821D @@ -5563,7 +5561,6 @@ inline int sg_append_buffer(sg_buffer buf_id, const sg_range& data) { return sg_ #define GL_RGB10_A2 0x8059 #define GL_RGBA8 0x8058 #define GL_SRGB8_ALPHA8 0x8C43 - #define GL_COLOR_ATTACHMENT1 0x8CE1 #define GL_RGBA4 0x8056 #define GL_RGB8 0x8051 #define GL_ARRAY_BUFFER 0x8892 @@ -5646,7 +5643,6 @@ inline int sg_append_buffer(sg_buffer buf_id, const sg_range& data) { return sg_ #define GL_DST_COLOR 0x0306 #define GL_COMPILE_STATUS 0x8B81 #define GL_RED 0x1903 - #define GL_COLOR_ATTACHMENT3 0x8CE3 #define GL_DST_ALPHA 0x0304 #define GL_RGB5_A1 0x8057 #define GL_GREATER 0x0204 @@ -10927,13 +10923,16 @@ _SOKOL_PRIVATE void _sg_gl_begin_pass(const sg_pass* pass, const _sg_attachments _sg.cur_pass.valid = false; return; } - static const GLenum gl_draw_bufs[SG_MAX_COLOR_ATTACHMENTS] = { - GL_COLOR_ATTACHMENT0, - GL_COLOR_ATTACHMENT1, - GL_COLOR_ATTACHMENT2, - GL_COLOR_ATTACHMENT3 - }; - glDrawBuffers(atts->num_color_views, gl_draw_bufs); + GLenum gl_draw_bufs[SG_MAX_COLOR_ATTACHMENTS]; + SOKOL_ASSERT(_sg.limits.max_color_attachments <= SG_MAX_COLOR_ATTACHMENTS); + for (int i = 0; i < _sg.limits.max_color_attachments; i++) { + if (i < atts->num_color_views) { + gl_draw_bufs[i] = GL_COLOR_ATTACHMENT0 + i; + } else { + gl_draw_bufs[i] = GL_NONE; + } + } + glDrawBuffers(_sg.limits.max_color_attachments, gl_draw_bufs); #if defined(_SOKOL_GL_HAS_COMPUTE) _sg_gl_handle_memory_barriers(0, 0, atts); -- cgit v1.2.3 From 18dc988ae893efefe0f8e3000777cc97164ad220 Mon Sep 17 00:00:00 2001 From: Andre Weissflog Date: Thu, 27 Nov 2025 19:30:36 +0100 Subject: sokol_gfx.h gl: fix signed/unsigned warning on clang/gcc --- sokol_gfx.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sokol_gfx.h') diff --git a/sokol_gfx.h b/sokol_gfx.h index c3fe87b9..11d387e2 100644 --- a/sokol_gfx.h +++ b/sokol_gfx.h @@ -10927,7 +10927,7 @@ _SOKOL_PRIVATE void _sg_gl_begin_pass(const sg_pass* pass, const _sg_attachments SOKOL_ASSERT(_sg.limits.max_color_attachments <= SG_MAX_COLOR_ATTACHMENTS); for (int i = 0; i < _sg.limits.max_color_attachments; i++) { if (i < atts->num_color_views) { - gl_draw_bufs[i] = GL_COLOR_ATTACHMENT0 + i; + gl_draw_bufs[i] = (GLenum)(GL_COLOR_ATTACHMENT0 + i); } else { gl_draw_bufs[i] = GL_NONE; } -- cgit v1.2.3 From 6579577b9de45406c92a5bae9e1d64842e1bcccc Mon Sep 17 00:00:00 2001 From: etherbound-dev Date: Mon, 1 Dec 2025 14:06:56 -0800 Subject: fix(gl): detach unused FBO attachments in _sg_gl_begin_pass The GL backend uses a single shared FBO for all offscreen passes. When switching from a pass with depth-stencil to one without, the old depth-stencil attachment remained bound, causing rendering to be clipped to the smaller attachment's dimensions. This fix explicitly detaches: - Unused color attachment slots (beyond num_color_views) - Depth-stencil attachment when not used in the current pass Fixes rendering clipping when mixing render targets with and without depth-stencil attachments in the same frame. Reproduction: https://github.com/etherbound-dev/sokol-depth-stencil-bug --- sokol_gfx.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'sokol_gfx.h') diff --git a/sokol_gfx.h b/sokol_gfx.h index 11d387e2..1d08c14e 100644 --- a/sokol_gfx.h +++ b/sokol_gfx.h @@ -10909,6 +10909,11 @@ _SOKOL_PRIVATE void _sg_gl_begin_pass(const sg_pass* pass, const _sg_attachments _sg_gl_fb_attach_texture(view, gl_att_type); } } + // explicitly detach unused color attachments + for (int i = atts->num_color_views; i < SG_MAX_COLOR_ATTACHMENTS; i++) { + const GLenum gl_att_type = (GLenum)(GL_COLOR_ATTACHMENT0 + i); + glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att_type, GL_TEXTURE_2D, 0, 0); + } if (atts->ds_view) { const _sg_view_t* view = atts->ds_view; const _sg_image_t* img = _sg_image_ref_ptr(&view->cmn.img.ref); @@ -10918,6 +10923,9 @@ _SOKOL_PRIVATE void _sg_gl_begin_pass(const sg_pass* pass, const _sg_attachments } else { _sg_gl_fb_attach_texture(view, gl_att_type); } + } else { + // explicitly detach depth-stencil attachment if not used in this pass + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0); } if (!_sg_gl_check_framebuffer_status()) { _sg.cur_pass.valid = false; -- cgit v1.2.3 From 1a400a26a5dae74938d13df35755427bcbd82dde Mon Sep 17 00:00:00 2001 From: Andre Weissflog Date: Tue, 2 Dec 2025 14:12:03 +0100 Subject: sokol_gfx.h gl: when clearing framebuffer slots in begin-pass, also clear the renderbuffer attachments --- sokol_gfx.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'sokol_gfx.h') diff --git a/sokol_gfx.h b/sokol_gfx.h index 1d08c14e..c53702b3 100644 --- a/sokol_gfx.h +++ b/sokol_gfx.h @@ -10910,8 +10910,9 @@ _SOKOL_PRIVATE void _sg_gl_begin_pass(const sg_pass* pass, const _sg_attachments } } // explicitly detach unused color attachments - for (int i = atts->num_color_views; i < SG_MAX_COLOR_ATTACHMENTS; i++) { + for (int i = atts->num_color_views; i < _sg.limits.max_color_attachments; i++) { const GLenum gl_att_type = (GLenum)(GL_COLOR_ATTACHMENT0 + i); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, gl_att_type, GL_RENDERBUFFER, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att_type, GL_TEXTURE_2D, 0, 0); } if (atts->ds_view) { @@ -10925,6 +10926,7 @@ _SOKOL_PRIVATE void _sg_gl_begin_pass(const sg_pass* pass, const _sg_attachments } } else { // explicitly detach depth-stencil attachment if not used in this pass + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0); } if (!_sg_gl_check_framebuffer_status()) { -- cgit v1.2.3