Skip to content

Commit

Permalink
Optimise full_screen_draw (#458)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
swiftcoder committed May 29, 2024
1 parent 2b1b15d commit a69b708
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a69b708

Please sign in to comment.