Use get_uniform_location for wider compatibility

This commit is contained in:
Héctor Ramón Jiménez 2020-05-22 05:52:11 +02:00
parent 1b287cddaf
commit 6f71a8e3d5
7 changed files with 93 additions and 48 deletions

View File

@ -34,4 +34,4 @@ features = ["font-source", "font-fallback", "font-icons", "opengl"]
[dependencies.glow_glyph]
git = "https://github.com/hecrj/glow_glyph"
rev = "f027ffa49962d78ac85e282c635e848bef785ee9"
rev = "8ec7982d9e0ce828769d4ba7abe73b0b0ec22db6"

View File

@ -11,6 +11,9 @@ pub struct Pipeline {
program: <glow::Context as HasContext>::Program,
vertex_array: <glow::Context as HasContext>::VertexArray,
instances: <glow::Context as HasContext>::Buffer,
transform_location: <glow::Context as HasContext>::UniformLocation,
scale_location: <glow::Context as HasContext>::UniformLocation,
screen_height_location: <glow::Context as HasContext>::UniformLocation,
current_transform: Transformation,
current_scale: f32,
current_target_height: u32,
@ -28,14 +31,30 @@ impl Pipeline {
)
};
let transform_location =
unsafe { gl.get_uniform_location(program, "u_Transform") }
.expect("Get transform location");
let scale_location =
unsafe { gl.get_uniform_location(program, "u_Scale") }
.expect("Get scale location");
let screen_height_location =
unsafe { gl.get_uniform_location(program, "u_ScreenHeight") }
.expect("Get target height location");
unsafe {
gl.use_program(Some(program));
let matrix: [f32; 16] = Transformation::identity().into();
gl.uniform_matrix_4_f32_slice(Some(&0), false, &matrix);
gl.uniform_matrix_4_f32_slice(
Some(&transform_location),
false,
&matrix,
);
gl.uniform_1_f32(Some(&1), 1.0);
gl.uniform_1_f32(Some(&2), 0.0);
gl.uniform_1_f32(Some(&scale_location), 1.0);
gl.uniform_1_f32(Some(&screen_height_location), 0.0);
gl.use_program(None);
}
@ -47,6 +66,9 @@ impl Pipeline {
program,
vertex_array,
instances,
transform_location,
scale_location,
screen_height_location,
current_transform: Transformation::identity(),
current_scale: 1.0,
current_target_height: 0,
@ -79,7 +101,11 @@ impl Pipeline {
if transformation != self.current_transform {
unsafe {
let matrix: [f32; 16] = transformation.into();
gl.uniform_matrix_4_f32_slice(Some(&0), false, &matrix);
gl.uniform_matrix_4_f32_slice(
Some(&self.transform_location),
false,
&matrix,
);
self.current_transform = transformation;
}
@ -87,7 +113,7 @@ impl Pipeline {
if scale != self.current_scale {
unsafe {
gl.uniform_1_f32(Some(&1), scale);
gl.uniform_1_f32(Some(&self.scale_location), scale);
}
self.current_scale = scale;
@ -95,7 +121,10 @@ impl Pipeline {
if target_height != self.current_target_height {
unsafe {
gl.uniform_1_f32(Some(&2), target_height as f32);
gl.uniform_1_f32(
Some(&self.screen_height_location),
target_height as f32,
);
}
self.current_target_height = target_height;

View File

@ -1,15 +1,15 @@
#version 450
#version 330
layout(location = 2) uniform float u_Screen_Height;
uniform float u_ScreenHeight;
layout(location = 0) in vec4 v_Color;
layout(location = 1) in vec4 v_BorderColor;
layout(location = 2) in vec2 v_Pos;
layout(location = 3) in vec2 v_Scale;
layout(location = 4) in float v_BorderRadius;
layout(location = 5) in float v_BorderWidth;
in vec4 v_Color;
in vec4 v_BorderColor;
in vec2 v_Pos;
in vec2 v_Scale;
in float v_BorderRadius;
in float v_BorderWidth;
layout(location = 0) out vec4 o_Color;
out vec4 o_Color;
float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius)
{
@ -22,8 +22,8 @@ float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius)
vec2 bottom_right_distance = frag_coord - bottom_right;
vec2 distance = vec2(
max(max(top_left_distance.x, bottom_right_distance.x), 0),
max(max(top_left_distance.y, bottom_right_distance.y), 0)
max(max(top_left_distance.x, bottom_right_distance.x), 0.0),
max(max(top_left_distance.y, bottom_right_distance.y), 0.0)
);
return sqrt(distance.x * distance.x + distance.y * distance.y);
@ -32,11 +32,11 @@ float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius)
void main() {
vec4 mixed_color;
vec2 fragCoord = vec2(gl_FragCoord.x, u_Screen_Height - gl_FragCoord.y);
vec2 fragCoord = vec2(gl_FragCoord.x, u_ScreenHeight - gl_FragCoord.y);
// TODO: Remove branching (?)
if(v_BorderWidth > 0) {
float internal_border = max(v_BorderRadius - v_BorderWidth, 0);
float internal_border = max(v_BorderRadius - v_BorderWidth, 0.0);
float internal_distance = distance(
fragCoord,
@ -64,7 +64,7 @@ void main() {
);
float radius_alpha =
1.0 - smoothstep(max(v_BorderRadius - 0.5, 0), v_BorderRadius + 0.5, d);
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);
}

View File

@ -1,7 +1,7 @@
#version 450
#version 330
layout(location = 0) uniform mat4 u_Transform;
layout(location = 1) uniform float u_Scale;
uniform mat4 u_Transform;
uniform float u_Scale;
layout(location = 0) in vec2 i_Pos;
layout(location = 1) in vec2 i_Scale;
@ -10,12 +10,12 @@ layout(location = 3) in vec4 i_BorderColor;
layout(location = 4) in float i_BorderRadius;
layout(location = 5) in float i_BorderWidth;
layout(location = 0) out vec4 o_Color;
layout(location = 1) out vec4 o_BorderColor;
layout(location = 2) out vec2 o_Pos;
layout(location = 3) out vec2 o_Scale;
layout(location = 4) out float o_BorderRadius;
layout(location = 5) out float o_BorderWidth;
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),
@ -25,7 +25,7 @@ const vec2 positions[4] = vec2[](
);
void main() {
vec2 v_Pos = positions[gl_VertexID];
vec2 q_Pos = positions[gl_VertexID];
vec2 p_Pos = i_Pos * u_Scale;
vec2 p_Scale = i_Scale * u_Scale;
@ -36,12 +36,12 @@ void main() {
vec4(p_Pos - vec2(0.5, 0.5), 0.0, 1.0)
);
o_Color = i_Color;
o_BorderColor = i_BorderColor;
o_Pos = p_Pos;
o_Scale = p_Scale;
o_BorderRadius = i_BorderRadius * u_Scale;
o_BorderWidth = i_BorderWidth * u_Scale;
v_Color = i_Color;
v_BorderColor = i_BorderColor;
v_Pos = p_Pos;
v_Scale = p_Scale;
v_BorderRadius = i_BorderRadius * u_Scale;
v_BorderWidth = i_BorderWidth * u_Scale;
gl_Position = u_Transform * i_Transform * vec4(v_Pos, 0.0, 1.0);
gl_Position = u_Transform * i_Transform * vec4(q_Pos, 0.0, 1.0);
}

View File

@ -1,8 +1,9 @@
#version 450
#version 330
layout(location = 0) in vec4 i_Color;
layout(location = 0) out vec4 o_Color;
in vec4 v_Color;
out vec4 o_Color;
void main() {
o_Color = i_Color;
o_Color = v_Color;
}

View File

@ -1,13 +1,13 @@
#version 450
#version 330
layout(location = 0) uniform mat4 u_Transform;
uniform mat4 u_Transform;
layout(location = 0) in vec2 i_Position;
layout(location = 1) in vec4 i_Color;
layout(location = 0) out vec4 o_Color;
out vec4 v_Color;
void main() {
gl_Position = u_Transform * vec4(i_Position, 0.0, 1.0);
o_Color = i_Color;
v_Color = i_Color;
}

View File

@ -18,6 +18,7 @@ pub(crate) struct Pipeline {
vertex_array: <glow::Context as HasContext>::VertexArray,
vertices: Buffer<Vertex2D>,
indices: Buffer<u32>,
transform_location: <glow::Context as HasContext>::UniformLocation,
current_transform: Transformation,
antialias: Antialias,
}
@ -40,11 +41,19 @@ impl Pipeline {
)
};
let transform_location =
unsafe { gl.get_uniform_location(program, "u_Transform") }
.expect("Get transform location");
unsafe {
gl.use_program(Some(program));
let transform: [f32; 16] = Transformation::identity().into();
gl.uniform_matrix_4_f32_slice(Some(&0), false, &transform);
gl.uniform_matrix_4_f32_slice(
Some(&transform_location),
false,
&transform,
);
gl.use_program(None);
}
@ -98,6 +107,7 @@ impl Pipeline {
vertex_array,
vertices,
indices,
transform_location,
current_transform: Transformation::identity(),
antialias: Antialias::new(antialiasing),
}
@ -163,6 +173,7 @@ impl Pipeline {
let Self {
antialias,
current_transform,
transform_location,
..
} = self;
@ -185,7 +196,11 @@ impl Pipeline {
unsafe {
if *current_transform != transform {
let matrix: [f32; 16] = transform.into();
gl.uniform_matrix_4_f32_slice(Some(&0), false, &matrix);
gl.uniform_matrix_4_f32_slice(
Some(transform_location),
false,
&matrix,
);
*current_transform = transform;
}