brush_blend.glsl (3973B)
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,blend,image_source 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 // x: Flag to allow perspective interpolation of UV. 18 // y: Filter-dependent "amount" parameter. 19 // Packed in to a vector to work around bug 1630356. 20 flat varying mediump vec2 v_perspective_amount; 21 #define v_perspective v_perspective_amount.x 22 #define v_amount v_perspective_amount.y 23 24 // x: Blend op, y: Lookup table GPU cache address. 25 // Packed in to a vector to work around bug 1630356. 26 // Must be explicitly marked as highp, as the default integer precision in 27 // fragment shaders is mediump which may only be 16 bits in ESSL 3, and GPU 28 // cache address can exceed that maximum representable value. 29 flat varying highp ivec2 v_op_table_address_vec; 30 #define v_op v_op_table_address_vec.x 31 #define v_table_address v_op_table_address_vec.y 32 33 // We must keep this as highp as we encoutered shader compilation crashes on 34 // Mali-T devices when mediump. 35 flat varying highp mat4 v_color_mat; 36 // The function to use for each component of a component transfer filter. Using a int[4] 37 // or ivec4 (with each element or component containing the function for each component) has 38 // ran in to bugs 1695912 and 1731758, so instead use a vec4 and cast the values to/from floats. 39 flat varying mediump vec4 v_funcs; 40 flat varying mediump vec4 v_color_offset; 41 42 #ifdef WR_VERTEX_SHADER 43 void brush_vs( 44 VertexInfo vi, 45 int prim_address, 46 RectWithEndpoint local_rect, 47 RectWithEndpoint segment_rect, 48 ivec4 prim_user_data, 49 int specific_resource_address, 50 mat4 transform, 51 PictureTask pic_task, 52 int brush_flags, 53 vec4 unused 54 ) { 55 ImageSource res = fetch_image_source(prim_user_data.x); 56 vec2 uv0 = res.uv_rect.p0; 57 vec2 uv1 = res.uv_rect.p1; 58 59 vec2 inv_texture_size = vec2(1.0) / vec2(TEX_SIZE(sColor0).xy); 60 vec2 f = (vi.local_pos - local_rect.p0) / rect_size(local_rect); 61 f = get_image_quad_uv(prim_user_data.x, f); 62 vec2 uv = mix(uv0, uv1, f); 63 float perspective_interpolate = (brush_flags & BRUSH_FLAG_PERSPECTIVE_INTERPOLATION) != 0 ? 1.0 : 0.0; 64 65 v_uv = uv * inv_texture_size * mix(vi.world_pos.w, 1.0, perspective_interpolate); 66 v_perspective = perspective_interpolate; 67 68 v_uv_sample_bounds = vec4(uv0 + vec2(0.5), uv1 - vec2(0.5)) * inv_texture_size.xyxy; 69 70 float amount = float(prim_user_data.z) / 65536.0; 71 72 v_op = prim_user_data.y & 0xffff; 73 v_amount = amount; 74 75 v_funcs.r = float((prim_user_data.y >> 28) & 0xf); 76 v_funcs.g = float((prim_user_data.y >> 24) & 0xf); 77 v_funcs.b = float((prim_user_data.y >> 20) & 0xf); 78 v_funcs.a = float((prim_user_data.y >> 16) & 0xf); 79 80 SetupFilterParams( 81 v_op, 82 amount, 83 prim_user_data.z, 84 v_color_offset, 85 v_color_mat, 86 v_table_address 87 ); 88 } 89 #endif 90 91 #ifdef WR_FRAGMENT_SHADER 92 Fragment brush_fs() { 93 float perspective_divisor = mix(gl_FragCoord.w, 1.0, v_perspective); 94 vec2 uv = v_uv * perspective_divisor; 95 // Clamp the uvs to avoid sampling artifacts. 96 uv = clamp(uv, v_uv_sample_bounds.xy, v_uv_sample_bounds.zw); 97 98 vec4 Cs = texture(sColor0, uv); 99 100 float alpha; 101 vec3 color; 102 CalculateFilter( 103 Cs, 104 v_op, 105 v_amount, 106 v_table_address, 107 v_color_offset, 108 v_color_mat, 109 v_funcs, 110 color, 111 alpha 112 ); 113 114 #ifdef WR_FEATURE_ALPHA_PASS 115 alpha *= antialias_brush(); 116 #endif 117 118 // Pre-multiply the alpha into the output value. 119 return Fragment(alpha * vec4(color, 1.0)); 120 } 121 #endif