brush_linear_gradient.glsl (2726B)
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 #define VECS_PER_SPECIFIC_BRUSH 2 6 7 #include shared,prim_shared,brush,gpu_buffer,gradient_shared 8 9 // Start offset. Packed in to vector to work around bug 1630356. 10 flat varying mediump vec2 v_start_offset; 11 12 flat varying mediump vec2 v_scale_dir; 13 14 #ifdef WR_VERTEX_SHADER 15 16 struct LinearGradientBrushData { 17 vec4 start_end_point; 18 int extend_mode; 19 vec2 stretch_size; 20 }; 21 22 LinearGradientBrushData fetch_gradient(int address) { 23 vec4 data[2] = fetch_from_gpu_buffer_2f(address); 24 return LinearGradientBrushData( 25 data[0], 26 int(data[1].x), 27 data[1].yz 28 ); 29 } 30 31 void brush_vs( 32 VertexInfo vi, 33 int prim_address, 34 RectWithEndpoint local_rect, 35 RectWithEndpoint segment_rect, 36 ivec4 prim_user_data, 37 int specific_resource_address, 38 mat4 transform, 39 PictureTask pic_task, 40 int brush_flags, 41 vec4 texel_rect 42 ) { 43 LinearGradientBrushData gradient = fetch_gradient(prim_address); 44 45 write_gradient_vertex( 46 vi, 47 local_rect, 48 segment_rect, 49 prim_user_data, 50 brush_flags, 51 texel_rect, 52 gradient.extend_mode, 53 gradient.stretch_size 54 ); 55 56 vec2 start_point = gradient.start_end_point.xy; 57 vec2 end_point = gradient.start_end_point.zw; 58 vec2 dir = end_point - start_point; 59 60 // Normalize UV and offsets to 0..1 scale. 61 v_scale_dir = dir / dot(dir, dir); 62 v_start_offset.x = dot(start_point, v_scale_dir); 63 v_scale_dir *= v_repeated_size; 64 } 65 #endif 66 67 #ifdef WR_FRAGMENT_SHADER 68 float get_gradient_offset(vec2 pos) { 69 // Project position onto a direction vector to compute offset. 70 return dot(pos, v_scale_dir) - v_start_offset.x; 71 } 72 73 Fragment brush_fs() { 74 vec4 color = sample_gradient(get_gradient_offset(compute_repeated_pos())); 75 76 #ifdef WR_FEATURE_ALPHA_PASS 77 color *= antialias_brush(); 78 #endif 79 80 return Fragment(color); 81 } 82 83 #ifdef SWGL_DRAW_SPAN 84 void swgl_drawSpanRGBA8() { 85 int address = swgl_validateGradient(sGpuBufferF, get_gpu_buffer_uv(v_gradient_address.x), int(GRADIENT_ENTRIES + 2.0)); 86 if (address < 0) { 87 return; 88 } 89 #ifdef WR_FEATURE_DITHERING 90 swgl_commitDitheredLinearGradientRGBA8(sGpuBufferF, address, GRADIENT_ENTRIES, true, v_gradient_repeat.x != 0.0, 91 v_pos, v_scale_dir, v_start_offset.x); 92 #else 93 swgl_commitLinearGradientRGBA8(sGpuBufferF, address, GRADIENT_ENTRIES, true, v_gradient_repeat.x != 0.0, 94 v_pos, v_scale_dir, v_start_offset.x); 95 #endif 96 } 97 #endif 98 99 #endif