Bug 1705433 - Pack more varyings in vectors to work around Adreno 3xx bug. r=kvark
[gecko.git] / gfx / wr / webrender / res / gradient.glsl
blob6b374acd530a8c2290d99f92cdefe114689667e5
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/. */
5 flat varying HIGHP_FS_ADDRESS int v_gradient_address;
7 #if defined(PLATFORM_ANDROID) && !defined(SWGL)
8 // Work around Adreno 3xx driver bug. See the v_perspective comment in
9 // brush_image or bugs 1630356 and  for details.
10 flat varying vec2 v_gradient_repeat_vec;
11 #define v_gradient_repeat v_gradient_repeat_vec.x
12 #else
13 // Repetition along the gradient stops.
14 flat varying float v_gradient_repeat;
15 #endif
17 #ifdef WR_FRAGMENT_SHADER
19 #ifdef WR_FEATURE_DITHERING
20 vec4 dither(vec4 color) {
21     const int matrix_mask = 7;
23     ivec2 pos = ivec2(gl_FragCoord.xy) & ivec2(matrix_mask);
24     float noise_normalized = (texelFetch(sDither, pos, 0).r * 255.0 + 0.5) / 64.0;
25     float noise = (noise_normalized - 0.5) / 256.0; // scale down to the unit length
27     return color + vec4(noise, noise, noise, 0);
29 #else
30 vec4 dither(vec4 color) {
31     return color;
33 #endif //WR_FEATURE_DITHERING
35 #define GRADIENT_ENTRIES 128.0
37 float clamp_gradient_entry(float offset) {
38     // Calculate the color entry index to use for this offset:
39     //     offsets < 0 use the first color entry, 0
40     //     offsets from [0, 1) use the color entries in the range of [1, N-1)
41     //     offsets >= 1 use the last color entry, N-1
42     //     so transform the range [0, 1) -> [1, N-1)
44     // TODO(gw): In the future we might consider making the size of the
45     // LUT vary based on number / distribution of stops in the gradient.
46     // Ensure we don't fetch outside the valid range of the LUT.
47     return clamp(1.0 + offset * GRADIENT_ENTRIES, 0.0, 1.0 + GRADIENT_ENTRIES);
50 vec4 sample_gradient(float offset) {
51     // Modulo the offset if the gradient repeats.
52     offset -= floor(offset) * v_gradient_repeat;
54     // Calculate the texel to index into the gradient color entries:
55     //     floor(x) is the gradient color entry index
56     //     fract(x) is the linear filtering factor between start and end
57     float x = clamp_gradient_entry(offset);
58     float entry_index = floor(x);
59     float entry_fract = x - entry_index;
61     // Fetch the start and end color. There is a [start, end] color per entry.
62     vec4 texels[2] = fetch_from_gpu_cache_2(v_gradient_address + 2 * int(entry_index));
64     // Finally interpolate and apply dithering
65     return dither(texels[0] + texels[1] * entry_fract);
68 #endif //WR_FRAGMENT_SHADER