Bug 1857841 - pt 3. Add a new page kind named "fresh" r=glandium
[gecko.git] / gfx / wr / webrender / res / brush_linear_gradient.glsl
blob235be4b24be82755a4638baacfcebc0671d3336e
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 #define VECS_PER_SPECIFIC_BRUSH 2
7 #include shared,prim_shared,brush,gpu_buffer,gradient_shared
9 // Start offset. Packed in to vector to work around bug 1630356.
10 flat varying mediump vec2 v_start_offset;
12 flat varying mediump vec2 v_scale_dir;
14 #ifdef WR_VERTEX_SHADER
16 struct Gradient {
17     vec4 start_end_point;
18     int extend_mode;
19     vec2 stretch_size;
22 Gradient fetch_gradient(int address) {
23     vec4 data[2] = fetch_from_gpu_cache_2(address);
24     return Gradient(
25         data[0],
26         int(data[1].x),
27         data[1].yz
28     );
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     Gradient gradient = fetch_gradient(prim_address);
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     );
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;
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;
65 #endif
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;
73 Fragment brush_fs() {
74     vec4 color = sample_gradient(get_gradient_offset(compute_repeated_pos()));
76 #ifdef WR_FEATURE_ALPHA_PASS
77     color *= antialias_brush();
78 #endif
80     return Fragment(color);
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     }
90     swgl_commitLinearGradientRGBA8(sGpuBufferF, address, GRADIENT_ENTRIES, true, v_gradient_repeat.x != 0.0,
91                                    v_pos, v_scale_dir, v_start_offset.x);
93 #endif
95 #endif