From 3a5f866782d561b9a75b49a3fb434d30b9a6c7c6 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sat, 26 Jun 2021 20:19:51 +0100 Subject: [PATCH] Downgrade OpenGL and GLSL for glow renderer --- glow/src/quad.rs | 58 ++++++++++++++++++++++++++--------- glow/src/shader/quad.frag | 18 +++++------ glow/src/shader/quad.vert | 35 +++++++++------------ glow/src/shader/triangle.frag | 8 ++--- glow/src/shader/triangle.vert | 8 ++--- 5 files changed, 72 insertions(+), 55 deletions(-) diff --git a/glow/src/quad.rs b/glow/src/quad.rs index a8fbb9e5..16bfb25b 100644 --- a/glow/src/quad.rs +++ b/glow/src/quad.rs @@ -6,6 +6,13 @@ use iced_native::Rectangle; const MAX_INSTANCES: usize = 100_000; +#[repr(C)] +#[derive(Copy, Clone, Debug)] +pub struct QuadWithQPos(layer::Quad, [f32; 2]); + +unsafe impl bytemuck::Zeroable for QuadWithQPos {} +unsafe impl bytemuck::Pod for QuadWithQPos {} + #[derive(Debug)] pub struct Pipeline { program: ::Program, @@ -133,23 +140,39 @@ impl Pipeline { let mut i = 0; let total = instances.len(); + let mut tagged_instances: Vec = Vec::new(); + + let pos_map = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]; + while i < total { let end = (i + MAX_INSTANCES).min(total); let amount = end - i; unsafe { + tagged_instances.clear(); + tagged_instances.extend( + instances[i..end] + .iter() + .map(|v| { + [ + QuadWithQPos(*v, pos_map[0]), + QuadWithQPos(*v, pos_map[1]), + QuadWithQPos(*v, pos_map[2]), + QuadWithQPos(*v, pos_map[3]), + ] + }) + .flatten(), + ); + gl.buffer_sub_data_u8_slice( glow::ARRAY_BUFFER, 0, - bytemuck::cast_slice(&instances[i..end]), + bytemuck::cast_slice(&tagged_instances), ); - gl.draw_arrays_instanced( - glow::TRIANGLE_STRIP, - 0, - 4, - amount as i32, - ); + for j in 0..amount as i32 { + gl.draw_arrays(glow::TRIANGLE_STRIP, j * 4, 4); + } } i += MAX_INSTANCES; @@ -177,23 +200,20 @@ unsafe fn create_instance_buffer( gl.bind_buffer(glow::ARRAY_BUFFER, Some(buffer)); gl.buffer_data_size( glow::ARRAY_BUFFER, - (size * std::mem::size_of::()) as i32, + (size * std::mem::size_of::()) as i32, glow::DYNAMIC_DRAW, ); - let stride = std::mem::size_of::() as i32; + let stride = std::mem::size_of::() as i32; gl.enable_vertex_attrib_array(0); gl.vertex_attrib_pointer_f32(0, 2, glow::FLOAT, false, stride, 0); - gl.vertex_attrib_divisor(0, 1); gl.enable_vertex_attrib_array(1); gl.vertex_attrib_pointer_f32(1, 2, glow::FLOAT, false, stride, 4 * 2); - gl.vertex_attrib_divisor(1, 1); gl.enable_vertex_attrib_array(2); gl.vertex_attrib_pointer_f32(2, 4, glow::FLOAT, false, stride, 4 * (2 + 2)); - gl.vertex_attrib_divisor(2, 1); gl.enable_vertex_attrib_array(3); gl.vertex_attrib_pointer_f32( @@ -204,7 +224,6 @@ unsafe fn create_instance_buffer( stride, 4 * (2 + 2 + 4), ); - gl.vertex_attrib_divisor(3, 1); gl.enable_vertex_attrib_array(4); gl.vertex_attrib_pointer_f32( @@ -215,7 +234,6 @@ unsafe fn create_instance_buffer( stride, 4 * (2 + 2 + 4 + 4), ); - gl.vertex_attrib_divisor(4, 1); gl.enable_vertex_attrib_array(5); gl.vertex_attrib_pointer_f32( @@ -226,7 +244,17 @@ unsafe fn create_instance_buffer( stride, 4 * (2 + 2 + 4 + 4 + 1), ); - gl.vertex_attrib_divisor(5, 1); + + // q_Pos + gl.enable_vertex_attrib_array(6); + gl.vertex_attrib_pointer_f32( + 6, + 2, + glow::FLOAT, + false, + stride, + 4 * (2 + 2 + 4 + 4 + 1 + 1), + ); gl.bind_vertex_array(None); gl.bind_buffer(glow::ARRAY_BUFFER, None); diff --git a/glow/src/shader/quad.frag b/glow/src/shader/quad.frag index cea36bdc..8f4a516c 100644 --- a/glow/src/shader/quad.frag +++ b/glow/src/shader/quad.frag @@ -1,15 +1,13 @@ -#version 330 +#version 120 uniform float u_ScreenHeight; -in vec4 v_Color; -in vec4 v_BorderColor; -in vec2 v_Pos; -in vec2 v_Scale; -in float v_BorderRadius; -in float v_BorderWidth; - -out vec4 o_Color; +varying vec4 v_Color; +varying vec4 v_BorderColor; +varying vec2 v_Pos; +varying vec2 v_Scale; +varying float v_BorderRadius; +varying float v_BorderWidth; float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius) { @@ -66,5 +64,5 @@ void main() { float radius_alpha = 1.0 - smoothstep(max(v_BorderRadius - 0.5, 0.0), v_BorderRadius + 0.5, d); - o_Color = vec4(mixed_color.xyz, mixed_color.w * radius_alpha); + gl_FragColor = vec4(mixed_color.xyz, mixed_color.w * radius_alpha); } diff --git a/glow/src/shader/quad.vert b/glow/src/shader/quad.vert index 82417856..8838483a 100644 --- a/glow/src/shader/quad.vert +++ b/glow/src/shader/quad.vert @@ -1,31 +1,24 @@ -#version 330 +#version 120 uniform mat4 u_Transform; uniform float u_Scale; -layout(location = 0) in vec2 i_Pos; -layout(location = 1) in vec2 i_Scale; -layout(location = 2) in vec4 i_Color; -layout(location = 3) in vec4 i_BorderColor; -layout(location = 4) in float i_BorderRadius; -layout(location = 5) in float i_BorderWidth; +attribute vec2 i_Pos; +attribute vec2 i_Scale; +attribute vec4 i_Color; +attribute vec4 i_BorderColor; +attribute float i_BorderRadius; +attribute float i_BorderWidth; +attribute vec2 q_Pos; -out vec4 v_Color; -out vec4 v_BorderColor; -out vec2 v_Pos; -out vec2 v_Scale; -out float v_BorderRadius; -out float v_BorderWidth; - -const vec2 positions[4] = vec2[]( - vec2(0.0, 0.0), - vec2(0.0, 1.0), - vec2(1.0, 0.0), - vec2(1.0, 1.0) -); +varying vec4 v_Color; +varying vec4 v_BorderColor; +varying vec2 v_Pos; +varying vec2 v_Scale; +varying float v_BorderRadius; +varying float v_BorderWidth; void main() { - vec2 q_Pos = positions[gl_VertexID]; vec2 p_Pos = i_Pos * u_Scale; vec2 p_Scale = i_Scale * u_Scale; diff --git a/glow/src/shader/triangle.frag b/glow/src/shader/triangle.frag index d186784a..0d109dfc 100644 --- a/glow/src/shader/triangle.frag +++ b/glow/src/shader/triangle.frag @@ -1,9 +1,7 @@ -#version 330 +#version 120 -in vec4 v_Color; - -out vec4 o_Color; +varying vec4 v_Color; void main() { - o_Color = v_Color; + gl_FragColor = v_Color; } diff --git a/glow/src/shader/triangle.vert b/glow/src/shader/triangle.vert index 5723436a..e5707016 100644 --- a/glow/src/shader/triangle.vert +++ b/glow/src/shader/triangle.vert @@ -1,11 +1,11 @@ -#version 330 +#version 120 uniform mat4 u_Transform; -layout(location = 0) in vec2 i_Position; -layout(location = 1) in vec4 i_Color; +attribute vec2 i_Position; +attribute vec4 i_Color; -out vec4 v_Color; +varying vec4 v_Color; void main() { gl_Position = u_Transform * vec4(i_Position, 0.0, 1.0);