tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

brush_opacity.glsl (2682B)


      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 3
      6 #define WR_FEATURE_TEXTURE_2D
      7 
      8 #include shared,prim_shared,brush
      9 
     10 // Interpolated UV coordinates to sample.
     11 varying highp vec2 v_uv;
     12 
     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;
     16 
     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
     21 
     22 #ifdef WR_VERTEX_SHADER
     23 void brush_vs(
     24     VertexInfo vi,
     25     int prim_address,
     26     RectWithEndpoint local_rect,
     27     RectWithEndpoint segment_rect,
     28     ivec4 prim_user_data,
     29     int specific_resource_address,
     30     mat4 transform,
     31     PictureTask pic_task,
     32     int brush_flags,
     33     vec4 unused
     34 ) {
     35     ImageSource res = fetch_image_source(prim_user_data.x);
     36     vec2 uv0 = res.uv_rect.p0;
     37     vec2 uv1 = res.uv_rect.p1;
     38 
     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;
     44 
     45     v_uv = uv / texture_size * mix(vi.world_pos.w, 1.0, perspective_interpolate);
     46     v_perspective = perspective_interpolate;
     47 
     48     v_uv_sample_bounds = vec4(uv0 + vec2(0.5), uv1 - vec2(0.5)) / texture_size.xyxy;
     49 
     50     v_opacity = clamp(float(prim_user_data.y) / 65536.0, 0.0, 1.0);
     51 }
     52 #endif
     53 
     54 #ifdef WR_FRAGMENT_SHADER
     55 Fragment brush_fs() {
     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);
     60 
     61     // No need to un-premultiply since we'll only apply a factor to the alpha.
     62     vec4 color = texture(sColor0, uv);
     63 
     64     float alpha = v_opacity;
     65 
     66     #ifdef WR_FEATURE_ALPHA_PASS
     67         alpha *= antialias_brush();
     68     #endif
     69 
     70     // Pre-multiply the contribution of the opacity factor.
     71     return Fragment(alpha * color);
     72 }
     73 
     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;
     78 
     79     swgl_commitTextureLinearColorRGBA8(sColor0, uv, v_uv_sample_bounds, v_opacity);
     80 }
     81 #endif
     82 
     83 #endif