summaryrefslogtreecommitdiffstats
path: root/glow/src/shader
diff options
context:
space:
mode:
authorLibravatar shan <shankern@protonmail.com>2022-10-05 16:07:43 -0700
committerLibravatar shan <shankern@protonmail.com>2022-10-05 16:07:43 -0700
commit1eb8d972ba60592da7bfc27fe7ec80138e64dd7b (patch)
tree1951255bcb8d2b87767da406eb87b80ab5a709cc /glow/src/shader
parentf7ce7244d017ec16545a3e52b6e7cf634bbffd4a (diff)
downloadiced-1eb8d972ba60592da7bfc27fe7ec80138e64dd7b.tar.gz
iced-1eb8d972ba60592da7bfc27fe7ec80138e64dd7b.tar.bz2
iced-1eb8d972ba60592da7bfc27fe7ec80138e64dd7b.zip
Reduced memory transfer of OpenGL gradient uniform upload. Rearranged gradient uniforms on OpenGL side to be more performant.
Diffstat (limited to 'glow/src/shader')
-rw-r--r--glow/src/shader/common/gradient.frag43
1 files changed, 22 insertions, 21 deletions
diff --git a/glow/src/shader/common/gradient.frag b/glow/src/shader/common/gradient.frag
index 1afb557d..89d2d398 100644
--- a/glow/src/shader/common/gradient.frag
+++ b/glow/src/shader/common/gradient.frag
@@ -1,6 +1,3 @@
-// GLSL does not support dynamically sized arrays without SSBOs
-#define MAX_STOPS 64
-
#ifdef GL_ES
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
@@ -16,34 +13,38 @@ layout (location = 0) out vec4 fragColor;
in vec2 raw_position;
-uniform vec2 gradient_start;
-uniform vec2 gradient_end;
-
+uniform vec4 gradient_direction;
uniform uint color_stops_size;
-uniform float color_stop_offsets[MAX_STOPS];
-uniform vec4 color_stop_colors[MAX_STOPS];
+// GLSL does not support dynamically sized arrays without SSBOs so this is capped to 16 stops
+//stored as color(vec4) -> offset(vec4) sequentially;
+uniform vec4 color_stops[32];
//TODO: rewrite without branching to make ALUs happy
void main() {
- vec2 gradient_vec = vec2(gradient_end - gradient_start);
- vec2 current_vec = vec2(raw_position.xy - gradient_start);
+ vec2 start = gradient_direction.xy;
+ vec2 end = gradient_direction.zw;
+ vec2 gradient_vec = vec2(end - start);
+ vec2 current_vec = vec2(raw_position.xy - start);
vec2 unit = normalize(gradient_vec);
float coord_offset = dot(unit, current_vec) / length(gradient_vec);
- for (uint i = 0; i < color_stops_size - 1; i++) {
- float stop_offset = color_stop_offsets[i];
- float next_stop_offset = color_stop_offsets[i + 1];
+ for (uint i = 0; i < color_stops_size - 2; i += 2) {
+ vec4 color = color_stops[i];
+ float offset = color_stops[i+1].x;
+
+ vec4 next_color = color_stops[i+2];
+ float next_offset = color_stops[i+3].x;
- if (stop_offset <= coord_offset && coord_offset <= next_stop_offset) {
- fragColor = mix(color_stop_colors[i], color_stop_colors[i+1], smoothstep(
- stop_offset,
- next_stop_offset,
+ if (offset <= coord_offset && coord_offset <= next_offset) {
+ fragColor = mix(color, next_color, smoothstep(
+ offset,
+ next_offset,
coord_offset
));
- } else if (coord_offset < color_stop_offsets[0]) {
- fragColor = color_stop_colors[0];
- } else if (coord_offset > color_stop_offsets[color_stops_size - 1]) {
- fragColor = color_stop_colors[color_stops_size - 1];
+ } else if (coord_offset < color_stops[1].x) {
+ fragColor = color_stops[0];
+ } else if (coord_offset > color_stops[color_stops_size - 1].x) {
+ fragColor = color_stops[color_stops_size - 2];
}
}
} \ No newline at end of file