shared.glsl (10666B)
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 #ifdef WR_FEATURE_TEXTURE_EXTERNAL 6 // Please check https://www.khronos.org/registry/OpenGL/extensions/OES/OES_EGL_image_external_essl3.txt 7 // for this extension. 8 #extension GL_OES_EGL_image_external_essl3 : require 9 #endif 10 11 #ifdef WR_FEATURE_TEXTURE_EXTERNAL_ESSL1 12 // Some GLES 3 devices do not support GL_OES_EGL_image_external_essl3, so we 13 // must use GL_OES_EGL_image_external instead and make the shader ESSL1 14 // compatible. 15 #extension GL_OES_EGL_image_external : require 16 #endif 17 18 #ifdef WR_FEATURE_TEXTURE_EXTERNAL_BT709 19 #extension GL_EXT_YUV_target : require 20 #endif 21 22 #ifdef WR_FEATURE_ADVANCED_BLEND 23 #extension GL_KHR_blend_equation_advanced : require 24 #endif 25 26 #ifdef WR_FEATURE_DUAL_SOURCE_BLENDING 27 #ifdef GL_ES 28 #extension GL_EXT_blend_func_extended : require 29 #else 30 #extension GL_ARB_explicit_attrib_location : require 31 #endif 32 #endif 33 34 #include base 35 36 #if defined(WR_FEATURE_TEXTURE_EXTERNAL_ESSL1) 37 #define TEX_SAMPLE(sampler, tex_coord) texture2D(sampler, tex_coord.xy) 38 #elif defined(WR_FEATURE_TEXTURE_EXTERNAL_BT709) 39 // Force conversion from yuv to rgb using BT709 colorspace 40 #define TEX_SAMPLE(sampler, tex_coord) vec4(yuv_2_rgb(texture(sampler, tex_coord.xy).xyz, itu_709), 1.0) 41 #else 42 #define TEX_SAMPLE(sampler, tex_coord) texture(sampler, tex_coord.xy) 43 #endif 44 45 #if defined(WR_FEATURE_TEXTURE_EXTERNAL) && defined(PLATFORM_ANDROID) 46 // On some Mali GPUs we have encountered crashes in glDrawElements when using 47 // textureSize(samplerExternalOES) in a vertex shader without potentially 48 // sampling from the texture. This tricks the driver in to thinking the texture 49 // may be sampled from, avoiding the crash. See bug 1692848. 50 uniform bool u_mali_workaround_dummy; 51 #define TEX_SIZE(sampler) (u_mali_workaround_dummy ? ivec2(texture(sampler, vec2(0.0, 0.0)).rr) : textureSize(sampler, 0)) 52 #else 53 #define TEX_SIZE(sampler) textureSize(sampler, 0) 54 #endif 55 56 // Keep these in sync with the corresponding constants in gpu_types.rs 57 // Specifies that the UV coordinates supplied to certain shaders are normalized. 58 #define UV_TYPE_NORMALIZED 0 59 // Specifies that the UV coordinates supplied to certain shaders are not normalized. 60 #define UV_TYPE_UNNORMALIZED 1 61 62 //====================================================================================== 63 // Vertex shader attributes and uniforms 64 //====================================================================================== 65 #ifdef WR_VERTEX_SHADER 66 // Uniform inputs 67 uniform mat4 uTransform; // Orthographic projection 68 69 // Attribute inputs 70 attribute vec2 aPosition; 71 72 // get_fetch_uv is a macro to work around a macOS Intel driver parsing bug. 73 // TODO: convert back to a function once the driver issues are resolved, if ever. 74 // https://github.com/servo/webrender/pull/623 75 // https://github.com/servo/servo/issues/13953 76 // Do the division with unsigned ints because that's more efficient with D3D 77 #define get_fetch_uv(i, vpi) ivec2(int(vpi * (uint(i) % (WR_MAX_VERTEX_TEXTURE_WIDTH/vpi))), int(uint(i) / (WR_MAX_VERTEX_TEXTURE_WIDTH/vpi))) 78 #endif 79 80 //====================================================================================== 81 // Fragment shader attributes and uniforms 82 //====================================================================================== 83 #ifdef WR_FRAGMENT_SHADER 84 // Uniform inputs 85 86 // Fragment shader outputs 87 #ifdef WR_FEATURE_ADVANCED_BLEND 88 layout(blend_support_all_equations) out; 89 #endif 90 91 #if __VERSION__ == 100 92 #define oFragColor gl_FragColor 93 #elif defined(WR_FEATURE_DUAL_SOURCE_BLENDING) 94 layout(location = 0, index = 0) out vec4 oFragColor; 95 layout(location = 0, index = 1) out vec4 oFragBlend; 96 #else 97 out vec4 oFragColor; 98 #endif 99 100 // Write an output color in normal shaders. 101 void write_output(vec4 color) { 102 oFragColor = color; 103 } 104 105 #define EPSILON 0.0001 106 107 // "Show Overdraw" color. Premultiplied. 108 #define WR_DEBUG_OVERDRAW_COLOR vec4(0.110, 0.077, 0.027, 0.125) 109 110 float distance_to_line(vec2 p0, vec2 perp_dir, vec2 p) { 111 vec2 dir_to_p0 = p0 - p; 112 return dot(normalize(perp_dir), dir_to_p0); 113 } 114 115 // fwidth is not defined in ESSL 1, but that's okay because we don't need 116 // it for any ESSL 1 shader variants. 117 #if __VERSION__ != 100 118 /// Find the appropriate half range to apply the AA approximation over. 119 /// This range represents a coefficient to go from one CSS pixel to half a device pixel. 120 vec2 compute_aa_range_xy(vec2 position) { 121 return fwidth(position); 122 } 123 124 float compute_aa_range(vec2 position) { 125 // The constant factor is chosen to compensate for the fact that length(fw) is equal 126 // to sqrt(2) times the device pixel ratio in the typical case. 127 // 128 // This coefficient is chosen to ensure that any sample 0.5 pixels or more inside of 129 // the shape has no anti-aliasing applied to it (since pixels are sampled at their center, 130 // such a pixel (axis aligned) is fully inside the border). We need this so that antialiased 131 // curves properly connect with non-antialiased vertical or horizontal lines, among other things. 132 // 133 // Lines over a half-pixel away from the pixel center *can* intersect with the pixel square; 134 // indeed, unless they are horizontal or vertical, they are guaranteed to. However, choosing 135 // a nonzero area for such pixels causes noticeable artifacts at the junction between an anti- 136 // aliased corner and a straight edge. 137 // 138 // We may want to adjust this constant in specific scenarios (for example keep the principled 139 // value for straight edges where we want pixel-perfect equivalence with non antialiased lines 140 // when axis aligned, while selecting a larger and smoother aa range on curves). 141 // 142 // As a further optimization, we compute the reciprocal of this range, such that we 143 // can then use the cheaper inversesqrt() instead of length(). This also elides a 144 // division that would otherwise be necessary inside distance_aa. 145 #ifdef SWGL 146 // SWGL uses an approximation for fwidth() such that it returns equal x and y. 147 // Thus, sqrt(2)/length(w) = sqrt(2)/sqrt(x*x + x*x) = recip(x). 148 return recip(fwidth(position).x); 149 #else 150 // sqrt(2)/length(w) = inversesqrt(0.5 * dot(w, w)) 151 vec2 w = fwidth(position); 152 return inversesqrt(0.5 * dot(w, w)); 153 #endif 154 } 155 #endif 156 157 /// Return the blending coefficient for distance antialiasing. 158 /// 159 /// 0.0 means inside the shape, 1.0 means outside. 160 /// 161 /// This makes the simplifying assumption that the area of a 1x1 pixel square 162 /// under a line is reasonably similar to just the signed Euclidian distance 163 /// from the center of the square to that line. This diverges slightly from 164 /// better approximations of the exact area, but the difference between the 165 /// methods is not perceptibly noticeable, while this approximation is much 166 /// faster to compute. 167 /// 168 /// See the comments in `compute_aa_range()` for more information on the 169 /// cutoff values of -0.5 and 0.5. 170 float distance_aa_xy(vec2 aa_range, vec2 signed_distance) { 171 // The aa_range is the raw per-axis filter width, so we need to divide 172 // the local signed distance by the filter width to get an approximation 173 // of screen distance. 174 #ifdef SWGL 175 // The SWGL fwidth() approximation returns uniform X and Y ranges. 176 vec2 dist = signed_distance * recip(aa_range.x); 177 #else 178 vec2 dist = signed_distance / aa_range; 179 #endif 180 // Choose whichever axis is further outside the rectangle for AA. 181 return clamp(0.5 - max(dist.x, dist.y), 0.0, 1.0); 182 } 183 184 float distance_aa(float aa_range, float signed_distance) { 185 // The aa_range is already stored as a reciprocal with uniform scale, 186 // so just multiply it, then use that for AA. 187 float dist = signed_distance * aa_range; 188 return clamp(0.5 - dist, 0.0, 1.0); 189 } 190 191 /// Component-wise selection. 192 /// 193 /// The idea of using this is to ensure both potential branches are executed before 194 /// selecting the result, to avoid observable timing differences based on the condition. 195 /// 196 /// Example usage: color = if_then_else(LessThanEqual(color, vec3(0.5)), vec3(0.0), vec3(1.0)); 197 /// 198 /// The above example sets each component to 0.0 or 1.0 independently depending on whether 199 /// their values are below or above 0.5. 200 /// 201 /// This is written as a macro in order to work with vectors of any dimension. 202 /// 203 /// Note: Some older android devices don't support mix with bvec. If we ever run into them 204 /// the only option we have is to polyfill it with a branch per component. 205 #define if_then_else(cond, then_branch, else_branch) mix(else_branch, then_branch, cond) 206 #endif 207 208 //====================================================================================== 209 // Shared shader uniforms 210 //====================================================================================== 211 #ifdef WR_FEATURE_TEXTURE_2D 212 uniform sampler2D sColor0; 213 uniform sampler2D sColor1; 214 uniform sampler2D sColor2; 215 #elif defined WR_FEATURE_TEXTURE_RECT 216 uniform sampler2DRect sColor0; 217 uniform sampler2DRect sColor1; 218 uniform sampler2DRect sColor2; 219 #elif defined(WR_FEATURE_TEXTURE_EXTERNAL) || defined(WR_FEATURE_TEXTURE_EXTERNAL_ESSL1) 220 uniform samplerExternalOES sColor0; 221 uniform samplerExternalOES sColor1; 222 uniform samplerExternalOES sColor2; 223 #elif defined(WR_FEATURE_TEXTURE_EXTERNAL_BT709) 224 uniform __samplerExternal2DY2YEXT sColor0; 225 uniform __samplerExternal2DY2YEXT sColor1; 226 uniform __samplerExternal2DY2YEXT sColor2; 227 #endif 228 229 //====================================================================================== 230 // Interpolator definitions 231 //====================================================================================== 232 233 //====================================================================================== 234 // VS only types and UBOs 235 //====================================================================================== 236 237 //====================================================================================== 238 // VS only functions 239 //======================================================================================