From a69b70874d93da231f5e4f8c35adb4d38134aeee Mon Sep 17 00:00:00 2001 From: Tristam MacDonald Date: Wed, 29 May 2024 12:16:39 +0200 Subject: [PATCH] Optimise full_screen_draw (#458) * Optimise full_screen_draw Get rid of the vertex buffer entirely, in favour of indexing vertex positions from an array within the vertex shader. Before this patch full_screen_draw is taking 18% of my frametime (mostly in creating and destroying the vertex buffer many times per from). After the patch it is reduced to 5%. Note that my app is very heavy on post-process effects - I expect the majority of apps will not see quite such a major difference. * move the empty VAO bind into full_screen_draw --- src/core.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/core.rs b/src/core.rs index 48f0de10..a8a91db8 100644 --- a/src/core.rs +++ b/src/core.rs @@ -67,25 +67,24 @@ pub(crate) fn full_screen_draw( render_states: RenderStates, viewport: Viewport, ) { - let position_buffer = VertexBuffer::new_with_data( - context, - &[ - vec3(-3.0, -1.0, 0.0), - vec3(3.0, -1.0, 0.0), - vec3(0.0, 2.0, 0.0), - ], - ); - program.use_vertex_attribute("position", &position_buffer); + unsafe { context.bind_vertex_array(Some(context.vao)) }; program.draw_arrays(render_states, viewport, 3); } pub(crate) fn full_screen_vertex_shader_source() -> &'static str { " - in vec3 position; out vec2 uvs; out vec4 col; void main() { + vec3 vertices[3] = vec3[3]( + vec3(-3.0, -1.0, 0.0), + vec3(3.0, -1.0, 0.0), + vec3(0.0, 2.0, 0.0) + ); + + vec3 position = vertices[gl_VertexID]; + uvs = 0.5 * position.xy + 0.5; col = vec4(1.0); gl_Position = vec4(position, 1.0);