cs_linear_gradient.glsl (2102B)
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 shared,rect,render_task,gpu_buffer,gradient 6 7 varying highp vec2 v_pos; 8 9 flat varying mediump vec2 v_scale_dir; 10 11 // Start offset. Packed in to a vector to work around bug 1630356. 12 flat varying highp vec2 v_start_offset; 13 14 #ifdef WR_VERTEX_SHADER 15 16 #define EXTEND_MODE_REPEAT 1 17 18 PER_INSTANCE in vec4 aTaskRect; 19 PER_INSTANCE in vec2 aStartPoint; 20 PER_INSTANCE in vec2 aEndPoint; 21 PER_INSTANCE in vec2 aScale; 22 PER_INSTANCE in int aExtendMode; 23 PER_INSTANCE in int aGradientStopsAddress; 24 25 void main(void) { 26 vec2 pos = mix(aTaskRect.xy, aTaskRect.zw, aPosition.xy); 27 gl_Position = uTransform * vec4(pos, 0.0, 1.0); 28 29 v_pos = aPosition.xy * aScale; 30 31 vec2 dir = aEndPoint - aStartPoint; 32 33 // Normalize UV and offsets to 0..1 scale. 34 v_scale_dir = dir / dot(dir, dir); 35 v_start_offset.x = dot(aStartPoint, v_scale_dir); 36 37 v_scale_dir *= (aTaskRect.zw - aTaskRect.xy); 38 39 v_gradient_repeat.x = float(aExtendMode == EXTEND_MODE_REPEAT); 40 v_gradient_address.x = aGradientStopsAddress; 41 } 42 #endif 43 44 45 #ifdef WR_FRAGMENT_SHADER 46 47 void main(void) { 48 // Project position onto a direction vector to compute offset. 49 float offset = dot(v_pos, v_scale_dir) - v_start_offset.x; 50 51 oFragColor = sample_gradient(offset); 52 } 53 54 55 #ifdef SWGL_DRAW_SPAN 56 void swgl_drawSpanRGBA8() { 57 int address = swgl_validateGradient(sGpuBufferF, get_gpu_buffer_uv(v_gradient_address.x), int(GRADIENT_ENTRIES + 2.0)); 58 if (address < 0) { 59 return; 60 } 61 62 #ifdef WR_FEATURE_DITHERING 63 swgl_commitDitheredLinearGradientRGBA8(sGpuBufferF, address, GRADIENT_ENTRIES, false, v_gradient_repeat.x != 0.0, 64 v_pos, v_scale_dir, v_start_offset.x); 65 #else 66 swgl_commitLinearGradientRGBA8(sGpuBufferF, address, GRADIENT_ENTRIES, false, v_gradient_repeat.x != 0.0, 67 v_pos, v_scale_dir, v_start_offset.x); 68 #endif 69 } 70 #endif 71 72 73 #endif