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 3
6 #define WR_FEATURE_TEXTURE_2D
8 #include shared,prim_shared,brush
10 // Interpolated UV coordinates to sample.
11 varying highp vec2 v_uv;
13 // Normalized bounds of the source image in the texture, adjusted to avoid
14 // sampling artifacts.
15 flat varying highp vec4 v_uv_sample_bounds;
17 flat varying mediump vec2 v_opacity_perspective_vec;
18 #define v_opacity v_opacity_perspective_vec.x
19 // Flag to allow perspective interpolation of UV.
20 #define v_perspective v_opacity_perspective_vec.y
22 #ifdef WR_VERTEX_SHADER
26 RectWithEndpoint local_rect,
27 RectWithEndpoint segment_rect,
29 int specific_resource_address,
35 ImageSource res = fetch_image_source(prim_user_data.x);
36 vec2 uv0 = res.uv_rect.p0;
37 vec2 uv1 = res.uv_rect.p1;
39 vec2 texture_size = vec2(TEX_SIZE(sColor0).xy);
40 vec2 f = (vi.local_pos - local_rect.p0) / rect_size(local_rect);
41 f = get_image_quad_uv(prim_user_data.x, f);
42 vec2 uv = mix(uv0, uv1, f);
43 float perspective_interpolate = (brush_flags & BRUSH_FLAG_PERSPECTIVE_INTERPOLATION) != 0 ? 1.0 : 0.0;
45 v_uv = uv / texture_size * mix(vi.world_pos.w, 1.0, perspective_interpolate);
46 v_perspective = perspective_interpolate;
48 v_uv_sample_bounds = vec4(uv0 + vec2(0.5), uv1 - vec2(0.5)) / texture_size.xyxy;
50 v_opacity = clamp(float(prim_user_data.y) / 65536.0, 0.0, 1.0);
54 #ifdef WR_FRAGMENT_SHADER
56 float perspective_divisor = mix(gl_FragCoord.w, 1.0, v_perspective);
57 vec2 uv = v_uv * perspective_divisor;
58 // Clamp the uvs to avoid sampling artifacts.
59 uv = clamp(uv, v_uv_sample_bounds.xy, v_uv_sample_bounds.zw);
61 // No need to un-premultiply since we'll only apply a factor to the alpha.
62 vec4 color = texture(sColor0, uv);
64 float alpha = v_opacity;
66 #ifdef WR_FEATURE_ALPHA_PASS
67 alpha *= antialias_brush();
70 // Pre-multiply the contribution of the opacity factor.
71 return Fragment(alpha * color);
74 #if defined(SWGL_DRAW_SPAN) && !defined(WR_FEATURE_DUAL_SOURCE_BLENDING)
75 void swgl_drawSpanRGBA8() {
76 float perspective_divisor = mix(swgl_forceScalar(gl_FragCoord.w), 1.0, v_perspective);
77 vec2 uv = v_uv * perspective_divisor;
79 swgl_commitTextureLinearColorRGBA8(sColor0, uv, v_uv_sample_bounds, v_opacity);