ps_quad_radial_gradient.glsl (2674B)
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 /// This shader renders radial graidents in a color or alpha target. 6 7 #include ps_quad,gradient 8 9 // Start radius. Packed in to a vector to work around bug 1630356. 10 flat varying highp vec2 v_start_radius; 11 varying highp vec2 v_pos; 12 13 struct RadialGradient { 14 vec2 center; 15 vec2 scale; 16 float start_radius; 17 float end_radius; 18 float xy_ratio; 19 // 1.0 if the gradient should be repeated, 0.0 otherwise. 20 float repeat; 21 }; 22 23 RadialGradient fetch_radial_gradient(int address) { 24 vec4[2] data = fetch_from_gpu_buffer_2f(address); 25 26 return RadialGradient( 27 data[0].xy, 28 data[0].zw, 29 data[1].x, 30 data[1].y, 31 data[1].z, 32 data[1].w 33 ); 34 } 35 36 #ifdef WR_VERTEX_SHADER 37 void pattern_vertex(PrimitiveInfo info) { 38 RadialGradient gradient = fetch_radial_gradient(info.pattern_input.x); 39 v_gradient_address.x = info.pattern_input.y; 40 41 // Store 1/rd where rd = end_radius - start_radius 42 // If rd = 0, we can't get its reciprocal. Instead, just use a zero scale. 43 float rd = gradient.end_radius - gradient.start_radius; 44 float radius_scale = rd != 0.0 ? 1.0 / rd : 0.0; 45 46 v_start_radius.x = gradient.start_radius * radius_scale; 47 48 // Transform all coordinates by the y scale so the 49 // fragment shader can work with circles 50 51 // v_pos is in a coordinate space relative to the task rect 52 // (so it is independent of the task origin). 53 v_pos = ((info.local_pos - info.local_prim_rect.p0) * gradient.scale - gradient.center) * radius_scale; 54 v_pos.y *= gradient.xy_ratio; 55 56 v_gradient_repeat.x = gradient.repeat; 57 } 58 #endif 59 60 #ifdef WR_FRAGMENT_SHADER 61 vec4 pattern_fragment(vec4 color) { 62 // Solve for t in length(pd) = v_start_radius + t * rd 63 float offset = length(v_pos) - v_start_radius.x; 64 color *= sample_gradient(offset); 65 66 return color; 67 } 68 69 #if defined(SWGL_DRAW_SPAN) 70 void swgl_drawSpanRGBA8() { 71 int address = swgl_validateGradient(sGpuBufferF, get_gpu_buffer_uv(v_gradient_address.x), 72 int(GRADIENT_ENTRIES + 2.0)); 73 if (address < 0) { 74 return; 75 } 76 #ifdef WR_FEATURE_DITHERING 77 swgl_commitDitheredRadialGradientRGBA8(sGpuBufferF, address, GRADIENT_ENTRIES, v_gradient_repeat.x != 0.0, 78 v_pos, v_start_radius.x); 79 #else 80 swgl_commitRadialGradientRGBA8(sGpuBufferF, address, GRADIENT_ENTRIES, v_gradient_repeat.x != 0.0, v_pos, v_start_radius.x); 81 #endif 82 } 83 #endif 84 85 #endif