Merge pull request #1518 from bungoboingo/fix/old_gl

Fixed issues with old GL versions ( <= 2.1 )
This commit is contained in:
Héctor Ramón 2022-11-08 23:23:58 +01:00 committed by GitHub
commit 8102f96f12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 19 deletions

View file

@ -8,3 +8,4 @@ publish = false
[dependencies] [dependencies]
iced = { path = "../..", features = ["canvas", "tokio", "debug"] } iced = { path = "../..", features = ["canvas", "tokio", "debug"] }
rand = "0.8.5" rand = "0.8.5"
env_logger = "0.9"

View file

@ -9,6 +9,8 @@ use iced::{
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
fn main() -> iced::Result { fn main() -> iced::Result {
env_logger::builder().format_timestamp(None).init();
ModernArt::run(Settings { ModernArt::run(Settings {
antialiasing: true, antialiasing: true,
..Settings::default() ..Settings::default()

View file

@ -1,20 +1,20 @@
#ifdef GL_ES #ifdef GL_ES
#ifdef GL_FRAGMENT_PRECISION_HIGH #ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float; precision highp float;
#else #else
precision mediump float; precision mediump float;
#endif #endif
#endif #endif
#ifdef HIGHER_THAN_300 #ifdef HIGHER_THAN_300
layout (location = 0) out vec4 fragColor; layout (location = 0) out vec4 fragColor;
#define gl_FragColor fragColor #define gl_FragColor fragColor
#endif #endif
in vec2 raw_position; in vec2 raw_position;
uniform vec4 gradient_direction; uniform vec4 gradient_direction;
uniform uint color_stops_size; uniform int color_stops_size;
// GLSL does not support dynamically sized arrays without SSBOs so this is capped to 16 stops // GLSL does not support dynamically sized arrays without SSBOs so this is capped to 16 stops
//stored as color(vec4) -> offset(vec4) sequentially; //stored as color(vec4) -> offset(vec4) sequentially;
uniform vec4 color_stops[32]; uniform vec4 color_stops[32];
@ -28,23 +28,23 @@ void main() {
vec2 unit = normalize(gradient_vec); vec2 unit = normalize(gradient_vec);
float coord_offset = dot(unit, current_vec) / length(gradient_vec); float coord_offset = dot(unit, current_vec) / length(gradient_vec);
//if a gradient has a start/end stop that is identical, the mesh will have a transparent fill //if a gradient has a start/end stop that is identical, the mesh will have a transparent fill
fragColor = vec4(0.0, 0.0, 0.0, 0.0); gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
float min_offset = color_stops[1].x; float min_offset = color_stops[1].x;
float max_offset = color_stops[color_stops_size - 1u].x; float max_offset = color_stops[color_stops_size - 1].x;
for (uint i = 0u; i < color_stops_size - 2u; i += 2u) { for (int i = 0; i < color_stops_size - 2; i += 2) {
float curr_offset = color_stops[i+1u].x; float curr_offset = color_stops[i+1].x;
float next_offset = color_stops[i+3u].x; float next_offset = color_stops[i+3].x;
if (coord_offset <= min_offset) { if (coord_offset <= min_offset) {
//current coordinate is before the first defined offset, set it to the start color //current coordinate is before the first defined offset, set it to the start color
fragColor = color_stops[0]; gl_FragColor = color_stops[0];
} }
if (curr_offset <= coord_offset && coord_offset <= next_offset) { if (curr_offset <= coord_offset && coord_offset <= next_offset) {
//current fragment is between the current offset processing & the next one, interpolate colors //current fragment is between the current offset processing & the next one, interpolate colors
fragColor = mix(color_stops[i], color_stops[i+2u], smoothstep( gl_FragColor = mix(color_stops[i], color_stops[i+2], smoothstep(
curr_offset, curr_offset,
next_offset, next_offset,
coord_offset coord_offset
@ -53,7 +53,7 @@ void main() {
if (coord_offset >= max_offset) { if (coord_offset >= max_offset) {
//current coordinate is before the last defined offset, set it to the last color //current coordinate is before the last defined offset, set it to the last color
fragColor = color_stops[color_stops_size - 2u]; gl_FragColor = color_stops[color_stops_size - 2];
} }
} }
} }

View file

@ -14,5 +14,5 @@ out vec4 fragColor;
uniform vec4 color; uniform vec4 color;
void main() { void main() {
fragColor = color; gl_FragColor = color;
} }

View file

@ -71,14 +71,14 @@ impl Program {
linear.end.y, linear.end.y,
); );
gl.uniform_1_u32( gl.uniform_1_i32(
Some( Some(
&self &self
.uniform_data .uniform_data
.uniform_locations .uniform_locations
.color_stops_size_location, .color_stops_size_location,
), ),
(linear.color_stops.len() * 2) as u32, (linear.color_stops.len() * 2) as i32,
); );
let mut stops = [0.0; 128]; let mut stops = [0.0; 128];