From da1be433f1eb411d91ff846b239a8f54e6ed3011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoi=CC=82t=20Rouleau?= Date: Fri, 27 Nov 2026 11:36:57 -0500 Subject: [PATCH] gl: skip state that's already set Each draw call set state that hadn't changed: - `apply_bindings` enabled every vertex attribute and set its divisor again whenever its buffer changed, which is every draw call when each has buffers of its own, as macroquad's batches do. An attribute in the cache is enabled with its divisor set, so now only its pointer is set again. - `apply_pipeline` called `glUseProgram`, set the depth test and the front face every time. They're cached now, as blending, stencil, cull face and color mask already were. - `end_render_pass` bound no buffers in place of the pass's, so the next pass bound them again. They stay bound now. - `new_buffer` and `buffer_update` bound the buffer they upload into and then bound back the one before. Draws don't read the vertex buffer binding, so it stays as the upload left it, and the next draw binds back the index buffer the bindings applied if an upload bound another since. In the web build, with 200 draw calls a frame, a draw call now makes 19.8 WebGL calls rather than 32.5: `bindBuffer` falls from 4 a draw call to 2, `enableVertexAttribArray` and `vertexAttribDivisor` from 3.9 to 0.01, `useProgram` from 1 to 0.04, and `frontFace` from 1 to none. Co-Authored-By: Claude Opus 5.5 Co-authored-by: multica-agent --- src/graphics/gl.rs | 111 +++++++++++++++++++++------------------ src/graphics/gl/cache.rs | 55 +++++++++++++------ 2 files changed, 97 insertions(+), 69 deletions(-) diff --git a/src/graphics/gl.rs b/src/graphics/gl.rs index 5fb34c096..6aa890435 100644 --- a/src/graphics/gl.rs +++ b/src/graphics/gl.rs @@ -1,4 +1,4 @@ -use std::ffi::CString; +use std::{cell::Cell, ffi::CString}; use crate::{window, ResourceManager}; @@ -549,11 +549,9 @@ impl GlContext { textures: Textures(vec![]), info, cache: GlCache { - stored_index_buffer: 0, - stored_index_type: None, - stored_vertex_buffer: 0, index_buffer: 0, index_type: None, + index_buffer_uploading: Cell::new(None), vertex_buffer: 0, cur_pipeline: None, cur_pass: None, @@ -562,6 +560,9 @@ impl GlContext { stencil: None, color_write: (true, true, true, true), cull_face: CullFace::Nothing, + program: 0, + depth_test: None, + front_face: FrontFaceOrder::CounterClockwise, stored_texture: 0, stored_target: 0, textures: [CachedTexture { @@ -896,6 +897,8 @@ impl RenderingBackend for GlContext { _ => panic!("Metal source on OpenGl context"), }; let shader = load_shader_internal(vertex, fragment, meta)?; + // Loading it left its program in use. + self.cache.program = shader.program; Ok(ShaderId(self.shaders.add(shader))) } @@ -925,8 +928,11 @@ impl RenderingBackend for GlContext { } fn delete_shader(&mut self, program: ShaderId) { - unsafe { glDeleteProgram(self.shaders[program.0].program) }; - self.shaders.remove(program.0); + let program = self.shaders.remove(program.0).program; + unsafe { glDeleteProgram(program) }; + if self.cache.program == program { + self.cache.program = 0; + } self.cache.cur_pipeline = None; } @@ -1295,46 +1301,44 @@ impl RenderingBackend for GlContext { fn apply_pipeline(&mut self, pipeline: &Pipeline) { self.cache.cur_pipeline = Some(*pipeline); - { - let pipeline = &self.pipelines[pipeline.0]; - let shader = &self.shaders[pipeline.shader.0]; - unsafe { - glUseProgram(shader.program); + let pipeline = &self.pipelines[pipeline.0]; + let program = self.shaders[pipeline.shader.0].program; + let params = pipeline.params; + unsafe { + if self.cache.program != program { + glUseProgram(program); + self.cache.program = program; } - unsafe { - glEnable(GL_SCISSOR_TEST); - } + glEnable(GL_SCISSOR_TEST); - if pipeline.params.depth_write { - unsafe { - glEnable(GL_DEPTH_TEST); - glDepthFunc(pipeline.params.depth_test.into()) - } - } else { - unsafe { - glDisable(GL_DEPTH_TEST); + let depth_test = params.depth_write.then_some(params.depth_test); + if self.cache.depth_test != depth_test { + match depth_test { + Some(depth_test) => { + if self.cache.depth_test.is_none() { + glEnable(GL_DEPTH_TEST); + } + glDepthFunc(depth_test.into()); + } + None => glDisable(GL_DEPTH_TEST), } + self.cache.depth_test = depth_test; } - match pipeline.params.front_face_order { - FrontFaceOrder::Clockwise => unsafe { - glFrontFace(GL_CW); - }, - FrontFaceOrder::CounterClockwise => unsafe { - glFrontFace(GL_CCW); - }, + if self.cache.front_face != params.front_face_order { + match params.front_face_order { + FrontFaceOrder::Clockwise => glFrontFace(GL_CW), + FrontFaceOrder::CounterClockwise => glFrontFace(GL_CCW), + } + self.cache.front_face = params.front_face_order; } } - self.set_cull_face(self.pipelines[pipeline.0].params.cull_face); - self.set_blend( - self.pipelines[pipeline.0].params.color_blend, - self.pipelines[pipeline.0].params.alpha_blend, - ); - - self.set_stencil(self.pipelines[pipeline.0].params.stencil_test); - self.set_color_write(self.pipelines[pipeline.0].params.color_write); + self.set_cull_face(params.cull_face); + self.set_blend(params.color_blend, params.alpha_blend); + self.set_stencil(params.stencil_test); + self.set_color_write(params.color_write); } fn new_buffer( @@ -1362,15 +1366,13 @@ impl RenderingBackend for GlContext { unsafe { glGenBuffers(1, &mut gl_buf as *mut _); - self.cache.store_buffer_binding(gl_target); - self.cache.bind_buffer(gl_target, gl_buf, index_type); + self.cache.bind_buffer_to_upload(gl_target, gl_buf); glBufferData(gl_target, size as _, std::ptr::null() as *const _, gl_usage); if let BufferSource::Slice(data) = data { debug_assert!(data.is_slice); glBufferSubData(gl_target, 0, size as _, data.ptr as _); } - self.cache.restore_buffer_binding(gl_target); } let buffer = Buffer { @@ -1401,11 +1403,8 @@ impl RenderingBackend for GlContext { assert!(size <= buffer.size); let gl_target = gl_buffer_target(&buffer.buffer_type); - self.cache.store_buffer_binding(gl_target); - self.cache - .bind_buffer(gl_target, buffer.gl_buf, buffer.index_type); + self.cache.bind_buffer_to_upload(gl_target, buffer.gl_buf); unsafe { glBufferSubData(gl_target, 0, size as _, data.ptr as _) }; - self.cache.restore_buffer_binding(gl_target); } /// Size of buffer in bytes @@ -1478,7 +1477,7 @@ impl RenderingBackend for GlContext { let pip = &self.pipelines[self.cache.cur_pipeline.unwrap().0]; for attr_index in 0..MAX_VERTEX_ATTRIBUTES { - let cached_attr = &mut self.cache.attributes[attr_index]; + let cached_attr = self.cache.attributes[attr_index]; let pip_attribute = pip.layout.get(attr_index).copied(); @@ -1519,14 +1518,21 @@ impl RenderingBackend for GlContext { attribute.offset as *mut _, ), } - if self.info.features.instancing { + // An attribute in the cache is enabled, with its + // divisor set. + if self.info.features.instancing + && cached_attr.map_or(true, |cached_attr| { + cached_attr.attribute.divisor != attribute.divisor + }) + { glVertexAttribDivisor(attr_index as GLuint, attribute.divisor as u32); } - glEnableVertexAttribArray(attr_index as GLuint); + if cached_attr.is_none() { + glEnableVertexAttribArray(attr_index as GLuint); + } }; - let cached_attr = &mut self.cache.attributes[attr_index]; - *cached_attr = Some(CachedAttribute { + self.cache.attributes[attr_index] = Some(CachedAttribute { attribute, gl_vbuf: vb.gl_buf, }); @@ -1535,7 +1541,7 @@ impl RenderingBackend for GlContext { unsafe { glDisableVertexAttribArray(attr_index as GLuint); } - *cached_attr = None; + self.cache.attributes[attr_index] = None; } } } @@ -1707,8 +1713,8 @@ impl RenderingBackend for GlContext { } } glBindFramebuffer(GL_FRAMEBUFFER, self.default_framebuffer); - self.cache.bind_buffer(GL_ARRAY_BUFFER, 0, None); - self.cache.bind_buffer(GL_ELEMENT_ARRAY_BUFFER, 0, None); + // The buffers stay bound, for the next pass's bindings to skip + // binding them again. // On Android when resizing the screen, visual tearing may occur // on some devices since clearing the screen is clipped by scissor. @@ -1737,6 +1743,7 @@ impl RenderingBackend for GlContext { let pip = &self.pipelines[self.cache.cur_pipeline.unwrap().0]; let primitive_type = pip.params.primitive_type.into(); let index_type = self.cache.index_type.expect("Unset index buffer type"); + self.cache.bind_index_buffer_to_draw(); unsafe { glDrawElementsInstanced( diff --git a/src/graphics/gl/cache.rs b/src/graphics/gl/cache.rs index 2a3c1dcc7..d980fb092 100644 --- a/src/graphics/gl/cache.rs +++ b/src/graphics/gl/cache.rs @@ -1,3 +1,5 @@ +use std::cell::Cell; + use crate::graphics::*; #[derive(Clone, Copy, Debug, PartialEq, Default)] @@ -26,13 +28,14 @@ pub struct CachedTexture { } pub struct GlCache { - pub stored_index_buffer: GLuint, - pub stored_index_type: Option, - pub stored_vertex_buffer: GLuint, pub stored_target: GLuint, pub stored_texture: GLuint, + /// The index buffer draws read, as the bindings last applied it. pub index_buffer: GLuint, pub index_type: Option, + /// Another index buffer, bound since to upload into it, which the next + /// draw binds `index_buffer` back over. + pub index_buffer_uploading: Cell>, pub vertex_buffer: GLuint, pub textures: [CachedTexture; MAX_SHADERSTAGE_IMAGES], pub cur_pipeline: Option, @@ -42,6 +45,10 @@ pub struct GlCache { pub stencil: Option, pub color_write: ColorMask, pub cull_face: CullFace, + pub program: GLuint, + /// `None` while the depth test is off. + pub depth_test: Option, + pub front_face: FrontFaceOrder, pub attributes: [Option; MAX_VERTEX_ATTRIBUTES], } @@ -55,34 +62,48 @@ impl GlCache { } } } else { - if self.index_buffer != buffer { - self.index_buffer = buffer; + let bound = self + .index_buffer_uploading + .take() + .unwrap_or(self.index_buffer); + if bound != buffer { unsafe { glBindBuffer(target, buffer); } } + self.index_buffer = buffer; self.index_type = index_type; } } - pub fn store_buffer_binding(&mut self, target: GLenum) { + /// Binds `buffer` to upload into it, without binding back what was + /// bound: draws don't read the vertex buffer binding, and the next draw + /// binds back the index buffer the bindings applied. + pub fn bind_buffer_to_upload(&mut self, target: GLenum, buffer: GLuint) { if target == GL_ARRAY_BUFFER { - self.stored_vertex_buffer = self.vertex_buffer; + self.bind_buffer(target, buffer, None); } else { - self.stored_index_buffer = self.index_buffer; - self.stored_index_type = self.index_type; + let bound = self + .index_buffer_uploading + .get() + .unwrap_or(self.index_buffer); + if bound != buffer { + unsafe { + glBindBuffer(target, buffer); + } + } + self.index_buffer_uploading + .set((buffer != self.index_buffer).then_some(buffer)); } } - pub fn restore_buffer_binding(&mut self, target: GLenum) { - if target == GL_ARRAY_BUFFER { - if self.stored_vertex_buffer != 0 { - self.bind_buffer(target, self.stored_vertex_buffer, None); - self.stored_vertex_buffer = 0; + /// Binds back the index buffer the bindings applied, if an upload has + /// bound another since. + pub fn bind_index_buffer_to_draw(&self) { + if self.index_buffer_uploading.take().is_some() { + unsafe { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.index_buffer); } - } else if self.stored_index_buffer != 0 { - self.bind_buffer(target, self.stored_index_buffer, self.stored_index_type); - self.stored_index_buffer = 0; } }