gradient.glsl (1962B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include dithering,gpu_buffer 6 7 // Gradient GPU cache address. 8 // Packed in to a vector to work around bug 1630356. 9 flat varying highp ivec2 v_gradient_address; 10 // Repetition along the gradient stops. 11 // Packed in to a vector to work around bug 1630356. 12 flat varying mediump vec2 v_gradient_repeat; 13 14 #ifdef WR_FRAGMENT_SHADER 15 16 #define GRADIENT_ENTRIES 128.0 17 18 float clamp_gradient_entry(float offset) { 19 // Calculate the color entry index to use for this offset: 20 // offsets < 0 use the first color entry, 0 21 // offsets from [0, 1) use the color entries in the range of [1, N-1) 22 // offsets >= 1 use the last color entry, N-1 23 // so transform the range [0, 1) -> [1, N-1) 24 25 // TODO(gw): In the future we might consider making the size of the 26 // LUT vary based on number / distribution of stops in the gradient. 27 // Ensure we don't fetch outside the valid range of the LUT. 28 return clamp(1.0 + offset * GRADIENT_ENTRIES, 0.0, 1.0 + GRADIENT_ENTRIES); 29 } 30 31 vec4 sample_gradient(float offset) { 32 // Modulo the offset if the gradient repeats. 33 offset -= floor(offset) * v_gradient_repeat.x; 34 35 // Calculate the texel to index into the gradient color entries: 36 // floor(x) is the gradient color entry index 37 // fract(x) is the linear filtering factor between start and end 38 float x = clamp_gradient_entry(offset); 39 float entry_index = floor(x); 40 float entry_fract = x - entry_index; 41 42 // Fetch the start and end color. There is a [start, end] color per entry. 43 vec4 texels[2] = fetch_from_gpu_buffer_2f(v_gradient_address.x + 2 * int(entry_index)); 44 45 // Finally interpolate and apply dithering 46 return dither(texels[0] + texels[1] * entry_fract); 47 } 48 49 #endif //WR_FRAGMENT_SHADER