Copy as Markdown

Other Tools

#define SWGL 1
#define __VERSION__ 150
#define WR_MAX_VERTEX_TEXTURE_WIDTH 1024U
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/// This shader applies a (rounded) rectangle mask to the content of the framebuffer.
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/// The common infrastructure for ps_quad_* shaders.
///
/// # Memory layout
///
/// The diagram below shows the the various pieces of data fectched in the vertex shader:
///
///```ascii
/// (int gpu buffer)
/// +---------------+ (sGpuCache)
/// (instance-step vertex attr) | Int header | +-----------+
/// +-----------------------------+ | | | Transform |
/// | Quad instance (uvec4) | +--> | transform id +--> +-----------+
/// | | | | z id |
/// | x: int prim address +---+ +---------------+ (float gpu buffer)
/// | y: float prim address +--------------------------> +-----------+--------------+-+-+
/// | z: quad flags | (sGpuCache) | Quad Prim | Quad Segment | | |
/// | edge flags | +--------------------+ | | | | |
/// | part index | | Picture task | | bounds | rect | | |
/// | segment index | | | | clip | uv rect | | |
/// | w: picture task address +--> | task rect | | color | | | |
/// +-----------------------------+ | device pixel scale | +-----------+--------------+-+-+
/// | content origin |
/// +--------------------+
///
/// To use the quad infrastructure, a shader must define the following entry
/// points in the corresponding shader stages:
/// - void pattern_vertex(PrimitiveInfo prim)
/// - vec4 pattern_fragment(vec4 base_color)
///```
#define WR_FEATURE_TEXTURE_2D
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef WR_FEATURE_TEXTURE_EXTERNAL
// for this extension.
#endif
#ifdef WR_FEATURE_TEXTURE_EXTERNAL_ESSL1
// Some GLES 3 devices do not support GL_OES_EGL_image_external_essl3, so we
// must use GL_OES_EGL_image_external instead and make the shader ESSL1
// compatible.
#endif
#ifdef WR_FEATURE_TEXTURE_EXTERNAL_BT709
#endif
#ifdef WR_FEATURE_ADVANCED_BLEND
#endif
#ifdef WR_FEATURE_DUAL_SOURCE_BLENDING
#ifdef GL_ES
#else
#endif
#endif
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#if defined(GL_ES)
#if GL_ES == 1
// Sampler default precision is lowp on mobile GPUs.
// This causes RGBA32F texture data to be clamped to 16 bit floats on some GPUs (e.g. Mali-T880).
// Define highp precision macro to allow lossless FLOAT texture sampling.
#define HIGHP_SAMPLER_FLOAT highp
// Default int precision in GLES 3 is highp (32 bits) in vertex shaders
// and mediump (16 bits) in fragment shaders. If an int is being used as
// a texel address in a fragment shader it, and therefore requires > 16
// bits, it must be qualified with this.
#define HIGHP_FS_ADDRESS highp
// texelFetchOffset is buggy on some Android GPUs (see issue #1694).
// Fallback to texelFetch on mobile GPUs.
#define TEXEL_FETCH(sampler, position, lod, offset) texelFetch(sampler, position + offset, lod)
#else
#define HIGHP_SAMPLER_FLOAT
#define HIGHP_FS_ADDRESS
#define TEXEL_FETCH(sampler, position, lod, offset) texelFetchOffset(sampler, position, lod, offset)
#endif
#else
#define HIGHP_SAMPLER_FLOAT
#define HIGHP_FS_ADDRESS
#if defined(PLATFORM_MACOS) && !defined(SWGL)
// texelFetchOffset introduces a variety of shader compilation bugs on macOS Intel so avoid it.
#define TEXEL_FETCH(sampler, position, lod, offset) texelFetch(sampler, position + offset, lod)
#else
#define TEXEL_FETCH(sampler, position, lod, offset) texelFetchOffset(sampler, position, lod, offset)
#endif
#endif
#ifdef SWGL
#define SWGL_DRAW_SPAN
#define SWGL_CLIP_MASK
#define SWGL_ANTIALIAS
#define SWGL_BLEND
#define SWGL_CLIP_DIST
#endif
#ifdef WR_VERTEX_SHADER
#ifdef SWGL
// Annotate a vertex attribute as being flat per each drawn primitive instance.
// SWGL can use this information to avoid redundantly loading the attribute in all SIMD lanes.
#define PER_INSTANCE flat
#else
#define PER_INSTANCE
#endif
#if __VERSION__ != 100
#define varying out
#define attribute in
#endif
#endif
#ifdef WR_FRAGMENT_SHADER
precision highp float;
#if __VERSION__ != 100
#define varying in
#endif
#endif
// Flat interpolation is not supported on ESSL 1
#if __VERSION__ == 100
#define flat
#endif
#if defined(WR_FEATURE_TEXTURE_EXTERNAL_ESSL1)
#define TEX_SAMPLE(sampler, tex_coord) texture2D(sampler, tex_coord.xy)
#elif defined(WR_FEATURE_TEXTURE_EXTERNAL_BT709)
// Force conversion from yuv to rgb using BT709 colorspace
#define TEX_SAMPLE(sampler, tex_coord) vec4(yuv_2_rgb(texture(sampler, tex_coord.xy).xyz, itu_709), 1.0)
#else
#define TEX_SAMPLE(sampler, tex_coord) texture(sampler, tex_coord.xy)
#endif
#if defined(WR_FEATURE_TEXTURE_EXTERNAL) && defined(PLATFORM_ANDROID)
// On some Mali GPUs we have encountered crashes in glDrawElements when using
// textureSize(samplerExternalOES) in a vertex shader without potentially
// sampling from the texture. This tricks the driver in to thinking the texture
// may be sampled from, avoiding the crash. See bug 1692848.
uniform bool u_mali_workaround_dummy;
#define TEX_SIZE(sampler) (u_mali_workaround_dummy ? ivec2(texture(sampler, vec2(0.0, 0.0)).rr) : textureSize(sampler, 0))
#else
#define TEX_SIZE(sampler) textureSize(sampler, 0)
#endif
//======================================================================================
// Vertex shader attributes and uniforms
//======================================================================================
#ifdef WR_VERTEX_SHADER
// Uniform inputs
uniform mat4 uTransform; // Orthographic projection
// Attribute inputs
attribute vec2 aPosition;
// get_fetch_uv is a macro to work around a macOS Intel driver parsing bug.
// TODO: convert back to a function once the driver issues are resolved, if ever.
// Do the division with unsigned ints because that's more efficient with D3D
#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)))
#endif
//======================================================================================
// Fragment shader attributes and uniforms
//======================================================================================
#ifdef WR_FRAGMENT_SHADER
// Uniform inputs
// Fragment shader outputs
#ifdef WR_FEATURE_ADVANCED_BLEND
layout(blend_support_all_equations) out;
#endif
#if __VERSION__ == 100
#define oFragColor gl_FragColor
#elif defined(WR_FEATURE_DUAL_SOURCE_BLENDING)
layout(location = 0, index = 0) out vec4 oFragColor;
layout(location = 0, index = 1) out vec4 oFragBlend;
#else
out vec4 oFragColor;
#endif
// Write an output color in normal shaders.
void write_output(vec4 color) {
oFragColor = color;
}
#define EPSILON 0.0001
// "Show Overdraw" color. Premultiplied.
#define WR_DEBUG_OVERDRAW_COLOR vec4(0.110, 0.077, 0.027, 0.125)
float distance_to_line(vec2 p0, vec2 perp_dir, vec2 p) {
vec2 dir_to_p0 = p0 - p;
return dot(normalize(perp_dir), dir_to_p0);
}
// fwidth is not defined in ESSL 1, but that's okay because we don't need
// it for any ESSL 1 shader variants.
#if __VERSION__ != 100
/// Find the appropriate half range to apply the AA approximation over.
/// This range represents a coefficient to go from one CSS pixel to half a device pixel.
vec2 compute_aa_range_xy(vec2 position) {
return fwidth(position);
}
float compute_aa_range(vec2 position) {
// The constant factor is chosen to compensate for the fact that length(fw) is equal
// to sqrt(2) times the device pixel ratio in the typical case.
//
// This coefficient is chosen to ensure that any sample 0.5 pixels or more inside of
// the shape has no anti-aliasing applied to it (since pixels are sampled at their center,
// such a pixel (axis aligned) is fully inside the border). We need this so that antialiased
// curves properly connect with non-antialiased vertical or horizontal lines, among other things.
//
// Lines over a half-pixel away from the pixel center *can* intersect with the pixel square;
// indeed, unless they are horizontal or vertical, they are guaranteed to. However, choosing
// a nonzero area for such pixels causes noticeable artifacts at the junction between an anti-
// aliased corner and a straight edge.
//
// We may want to adjust this constant in specific scenarios (for example keep the principled
// value for straight edges where we want pixel-perfect equivalence with non antialiased lines
// when axis aligned, while selecting a larger and smoother aa range on curves).
//
// As a further optimization, we compute the reciprocal of this range, such that we
// can then use the cheaper inversesqrt() instead of length(). This also elides a
// division that would otherwise be necessary inside distance_aa.
#ifdef SWGL
// SWGL uses an approximation for fwidth() such that it returns equal x and y.
// Thus, sqrt(2)/length(w) = sqrt(2)/sqrt(x*x + x*x) = recip(x).
return recip(fwidth(position).x);
#else
// sqrt(2)/length(w) = inversesqrt(0.5 * dot(w, w))
vec2 w = fwidth(position);
return inversesqrt(0.5 * dot(w, w));
#endif
}
#endif
/// Return the blending coefficient for distance antialiasing.
///
/// 0.0 means inside the shape, 1.0 means outside.
///
/// This makes the simplifying assumption that the area of a 1x1 pixel square
/// under a line is reasonably similar to just the signed Euclidian distance
/// from the center of the square to that line. This diverges slightly from
/// better approximations of the exact area, but the difference between the
/// methods is not perceptibly noticeable, while this approximation is much
/// faster to compute.
///
/// See the comments in `compute_aa_range()` for more information on the
/// cutoff values of -0.5 and 0.5.
float distance_aa_xy(vec2 aa_range, vec2 signed_distance) {
// The aa_range is the raw per-axis filter width, so we need to divide
// the local signed distance by the filter width to get an approximation
// of screen distance.
#ifdef SWGL
// The SWGL fwidth() approximation returns uniform X and Y ranges.
vec2 dist = signed_distance * recip(aa_range.x);
#else
vec2 dist = signed_distance / aa_range;
#endif
// Choose whichever axis is further outside the rectangle for AA.
return clamp(0.5 - max(dist.x, dist.y), 0.0, 1.0);
}
float distance_aa(float aa_range, float signed_distance) {
// The aa_range is already stored as a reciprocal with uniform scale,
// so just multiply it, then use that for AA.
float dist = signed_distance * aa_range;
return clamp(0.5 - dist, 0.0, 1.0);
}
/// Component-wise selection.
///
/// The idea of using this is to ensure both potential branches are executed before
/// selecting the result, to avoid observable timing differences based on the condition.
///
/// Example usage: color = if_then_else(LessThanEqual(color, vec3(0.5)), vec3(0.0), vec3(1.0));
///
/// The above example sets each component to 0.0 or 1.0 independently depending on whether
/// their values are below or above 0.5.
///
/// This is written as a macro in order to work with vectors of any dimension.
///
/// Note: Some older android devices don't support mix with bvec. If we ever run into them
/// the only option we have is to polyfill it with a branch per component.
#define if_then_else(cond, then_branch, else_branch) mix(else_branch, then_branch, cond)
#endif
//======================================================================================
// Shared shader uniforms
//======================================================================================
#ifdef WR_FEATURE_TEXTURE_2D
uniform sampler2D sColor0;
uniform sampler2D sColor1;
uniform sampler2D sColor2;
#elif defined WR_FEATURE_TEXTURE_RECT
uniform sampler2DRect sColor0;
uniform sampler2DRect sColor1;
uniform sampler2DRect sColor2;
#elif defined(WR_FEATURE_TEXTURE_EXTERNAL) || defined(WR_FEATURE_TEXTURE_EXTERNAL_ESSL1)
uniform samplerExternalOES sColor0;
uniform samplerExternalOES sColor1;
uniform samplerExternalOES sColor2;
#elif defined(WR_FEATURE_TEXTURE_EXTERNAL_BT709)
uniform __samplerExternal2DY2YEXT sColor0;
uniform __samplerExternal2DY2YEXT sColor1;
uniform __samplerExternal2DY2YEXT sColor2;
#endif
#ifdef WR_FEATURE_DITHERING
uniform sampler2D sDither;
#endif
//======================================================================================
// Interpolator definitions
//======================================================================================
//======================================================================================
// VS only types and UBOs
//======================================================================================
//======================================================================================
// VS only functions
//======================================================================================
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
struct RectWithSize {
vec2 p0;
vec2 size;
};
struct RectWithEndpoint {
vec2 p0;
vec2 p1;
};
float point_inside_rect(vec2 p, vec2 p0, vec2 p1) {
vec2 s = step(p0, p) - step(p1, p);
return s.x * s.y;
}
vec2 signed_distance_rect_xy(vec2 pos, vec2 p0, vec2 p1) {
// Instead of using a true signed distance to rect here, we just use the
// simpler approximation of the maximum distance on either axis from the
// outside of the rectangle. This avoids expensive use of length() and only
// causes mostly imperceptible differences at corner pixels.
return max(p0 - pos, pos - p1);
}
float signed_distance_rect(vec2 pos, vec2 p0, vec2 p1) {
// Collapse the per-axis distances to edges to a single approximate value.
vec2 d = signed_distance_rect_xy(pos, p0, p1);
return max(d.x, d.y);
}
vec2 rect_clamp(RectWithEndpoint rect, vec2 pt) {
return clamp(pt, rect.p0, rect.p1);
}
vec2 rect_size(RectWithEndpoint rect) {
return rect.p1 - rect.p0;
}
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
flat varying highp vec4 vTransformBounds;
#ifdef WR_VERTEX_SHADER
#define VECS_PER_TRANSFORM 8U
uniform HIGHP_SAMPLER_FLOAT sampler2D sTransformPalette;
void init_transform_vs(vec4 local_bounds) {
vTransformBounds = local_bounds;
}
struct Transform {
mat4 m;
mat4 inv_m;
bool is_axis_aligned;
};
Transform fetch_transform(int id) {
Transform transform;
transform.is_axis_aligned = (id >> 23) == 0;
int index = id & 0x007fffff;
// Create a UV base coord for each 8 texels.
// This is required because trying to use an offset
// of more than 8 texels doesn't work on some versions
// of macOS.
ivec2 uv = get_fetch_uv(index, VECS_PER_TRANSFORM);
ivec2 uv0 = ivec2(uv.x + 0, uv.y);
transform.m[0] = TEXEL_FETCH(sTransformPalette, uv0, 0, ivec2(0, 0));
transform.m[1] = TEXEL_FETCH(sTransformPalette, uv0, 0, ivec2(1, 0));
transform.m[2] = TEXEL_FETCH(sTransformPalette, uv0, 0, ivec2(2, 0));
transform.m[3] = TEXEL_FETCH(sTransformPalette, uv0, 0, ivec2(3, 0));
transform.inv_m[0] = TEXEL_FETCH(sTransformPalette, uv0, 0, ivec2(4, 0));
transform.inv_m[1] = TEXEL_FETCH(sTransformPalette, uv0, 0, ivec2(5, 0));
transform.inv_m[2] = TEXEL_FETCH(sTransformPalette, uv0, 0, ivec2(6, 0));
transform.inv_m[3] = TEXEL_FETCH(sTransformPalette, uv0, 0, ivec2(7, 0));
return transform;
}
// Return the intersection of the plane (set up by "normal" and "point")
// with the ray (set up by "ray_origin" and "ray_dir"),
// writing the resulting scaler into "t".
bool ray_plane(vec3 normal, vec3 pt, vec3 ray_origin, vec3 ray_dir, out float t)
{
float denom = dot(normal, ray_dir);
if (abs(denom) > 1e-6) {
vec3 d = pt - ray_origin;
t = dot(d, normal) / denom;
return t >= 0.0;
}
return false;
}
// Apply the inverse transform "inv_transform"
// to the reference point "ref" in CSS space,
// producing a local point on a Transform plane,
// set by a base point "a" and a normal "n".
vec4 untransform(vec2 ref, vec3 n, vec3 a, mat4 inv_transform) {
vec3 p = vec3(ref, -10000.0);
vec3 d = vec3(0, 0, 1.0);
float t = 0.0;
// get an intersection of the Transform plane with Z axis vector,
// originated from the "ref" point
ray_plane(n, a, p, d, t);
float z = p.z + d.z * t; // Z of the visible point on the Transform
vec4 r = inv_transform * vec4(ref, z, 1.0);
return r;
}
// Given a CSS space position, transform it back into the Transform space.
vec4 get_node_pos(vec2 pos, Transform transform) {
// get a point on the scroll node plane
vec4 ah = transform.m * vec4(0.0, 0.0, 0.0, 1.0);
vec3 a = ah.xyz / ah.w;
// get the normal to the scroll node plane
vec3 n = transpose(mat3(transform.inv_m)) * vec3(0.0, 0.0, 1.0);
return untransform(pos, n, a, transform.inv_m);
}
#endif //WR_VERTEX_SHADER
#ifdef WR_FRAGMENT_SHADER
// Assume transform bounds are set to a large scale to signal they are invalid.
bool has_valid_transform_bounds() {
return vTransformBounds.w < 1.0e15;
}
float init_transform_fs(vec2 local_pos) {
// Ideally we want to track distances in screen space after transformation
// as signed distance calculations lose context about the direction vector
// to exit the geometry, merely remembering the minimum distance to the
// exit. However, we can't always sanely track distances in screen space
// due to perspective transforms, clipping, and other concerns, so we do
// this in local space. However, this causes problems tracking distances
// in local space when attempting to scale by a uniform AA range later in
// the presence of a transform which actually has non-uniform scaling.
//
// To work around this, we independently track the distances on the local
// space X and Y axes and then scale them by the independent AA ranges (as
// computed from fwidth derivatives) for the X and Y axes. This can break
// down at certain angles (45 degrees or close to it), but still gives a
// better approximation of screen-space distances in the presence of non-
// uniform scaling for other rotations.
//
// Get signed distance from local rect bounds.
vec2 d = signed_distance_rect_xy(
local_pos,
vTransformBounds.xy,
vTransformBounds.zw
);
// Find the appropriate distance to apply the AA smoothstep over.
vec2 aa_range = compute_aa_range_xy(local_pos);
// Only apply AA to fragments outside the signed distance field.
return distance_aa_xy(aa_range, d);
}
float init_transform_rough_fs(vec2 local_pos) {
return point_inside_rect(
local_pos,
vTransformBounds.xy,
vTransformBounds.zw
);
}
#endif //WR_FRAGMENT_SHADER
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef WR_VERTEX_SHADER
#define VECS_PER_RENDER_TASK 2U
uniform HIGHP_SAMPLER_FLOAT sampler2D sRenderTasks;
struct RenderTaskData {
RectWithEndpoint task_rect;
vec4 user_data;
};
// See RenderTaskData in render_task.rs
RenderTaskData fetch_render_task_data(int index) {
ivec2 uv = get_fetch_uv(index, VECS_PER_RENDER_TASK);
vec4 texel0 = TEXEL_FETCH(sRenderTasks, uv, 0, ivec2(0, 0));
vec4 texel1 = TEXEL_FETCH(sRenderTasks, uv, 0, ivec2(1, 0));
RectWithEndpoint task_rect = RectWithEndpoint(
texel0.xy,
texel0.zw
);
RenderTaskData data = RenderTaskData(
task_rect,
texel1
);
return data;
}
RectWithEndpoint fetch_render_task_rect(int index) {
ivec2 uv = get_fetch_uv(index, VECS_PER_RENDER_TASK);
vec4 texel0 = TEXEL_FETCH(sRenderTasks, uv, 0, ivec2(0, 0));
vec4 texel1 = TEXEL_FETCH(sRenderTasks, uv, 0, ivec2(1, 0));
RectWithEndpoint task_rect = RectWithEndpoint(
texel0.xy,
texel0.zw
);
return task_rect;
}
#define PIC_TYPE_IMAGE 1
#define PIC_TYPE_TEXT_SHADOW 2
/*
The dynamic picture that this brush exists on. Right now, it
contains minimal information. In the future, it will describe
the transform mode of primitives on this picture, among other things.
*/
struct PictureTask {
RectWithEndpoint task_rect;
float device_pixel_scale;
vec2 content_origin;
};
PictureTask fetch_picture_task(int address) {
RenderTaskData task_data = fetch_render_task_data(address);
PictureTask task = PictureTask(
task_data.task_rect,
task_data.user_data.x,
task_data.user_data.yz
);
return task;
}
#define CLIP_TASK_EMPTY 0x7FFFFFFF
struct ClipArea {
RectWithEndpoint task_rect;
float device_pixel_scale;
vec2 screen_origin;
};
ClipArea fetch_clip_area(int index) {
RenderTaskData task_data;
if (index >= CLIP_TASK_EMPTY) {
// We deliberately create a dummy RenderTaskData here then convert to a
// ClipArea after this if-else statement, rather than initialize the
// ClipArea in separate branches, to avoid a miscompile in some Adreno
// drivers. See bug 1884791. Unfortunately the specific details of the bug
// are unknown, so please take extra care not to regress this when
// refactoring.
task_data = RenderTaskData(RectWithEndpoint(vec2(0.0), vec2(0.0)),
vec4(0.0));
} else {
task_data = fetch_render_task_data(index);
}
return ClipArea(task_data.task_rect, task_data.user_data.x,
task_data.user_data.yz);
}
#endif //WR_VERTEX_SHADER
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
uniform HIGHP_SAMPLER_FLOAT sampler2D sGpuBufferF;
uniform HIGHP_SAMPLER_FLOAT isampler2D sGpuBufferI;
ivec2 get_gpu_buffer_uv(HIGHP_FS_ADDRESS int address) {
return ivec2(uint(address) % WR_MAX_VERTEX_TEXTURE_WIDTH,
uint(address) / WR_MAX_VERTEX_TEXTURE_WIDTH);
}
vec4 fetch_from_gpu_buffer_1f(HIGHP_FS_ADDRESS int address) {
ivec2 uv = get_gpu_buffer_uv(address);
return texelFetch(sGpuBufferF, uv, 0);
}
vec4[2] fetch_from_gpu_buffer_2f(HIGHP_FS_ADDRESS int address) {
ivec2 uv = get_gpu_buffer_uv(address);
return vec4[2](
TEXEL_FETCH(sGpuBufferF, uv, 0, ivec2(0, 0)),
TEXEL_FETCH(sGpuBufferF, uv, 0, ivec2(1, 0))
);
}
vec4[3] fetch_from_gpu_buffer_3f(HIGHP_FS_ADDRESS int address) {
ivec2 uv = get_gpu_buffer_uv(address);
return vec4[3](
TEXEL_FETCH(sGpuBufferF, uv, 0, ivec2(0, 0)),
TEXEL_FETCH(sGpuBufferF, uv, 0, ivec2(1, 0)),
TEXEL_FETCH(sGpuBufferF, uv, 0, ivec2(2, 0))
);
}
vec4[4] fetch_from_gpu_buffer_4f(HIGHP_FS_ADDRESS int address) {
ivec2 uv = get_gpu_buffer_uv(address);
return vec4[4](
TEXEL_FETCH(sGpuBufferF, uv, 0, ivec2(0, 0)),
TEXEL_FETCH(sGpuBufferF, uv, 0, ivec2(1, 0)),
TEXEL_FETCH(sGpuBufferF, uv, 0, ivec2(2, 0)),
TEXEL_FETCH(sGpuBufferF, uv, 0, ivec2(3, 0))
);
}
ivec4 fetch_from_gpu_buffer_1i(HIGHP_FS_ADDRESS int address) {
ivec2 uv = get_gpu_buffer_uv(address);
return texelFetch(sGpuBufferI, uv, 0);
}
flat varying mediump vec4 v_color;
// w: has edge flags
// x,y,z are avaible for patterns to use.
flat varying lowp ivec4 v_flags;
#ifndef SWGL_ANTIALIAS
varying highp vec2 vLocalPos;
#endif
#ifdef WR_VERTEX_SHADER
#define EDGE_AA_LEFT 1
#define EDGE_AA_TOP 2
#define EDGE_AA_RIGHT 4
#define EDGE_AA_BOTTOM 8
#define PART_CENTER 0
#define PART_LEFT 1
#define PART_TOP 2
#define PART_RIGHT 3
#define PART_BOTTOM 4
#define PART_ALL 5
#define QF_IS_OPAQUE 1
#define QF_APPLY_DEVICE_CLIP 2
#define QF_IGNORE_DEVICE_SCALE 4
#define QF_USE_AA_SEGMENTS 8
#define QF_SAMPLE_AS_MASK 16
#define INVALID_SEGMENT_INDEX 0xff
#define AA_PIXEL_RADIUS 2.0
PER_INSTANCE in ivec4 aData;
struct QuadSegment {
RectWithEndpoint rect;
RectWithEndpoint uv_rect;
};
struct PrimitiveInfo {
vec2 local_pos;
RectWithEndpoint local_prim_rect;
RectWithEndpoint local_clip_rect;
QuadSegment segment;
int edge_flags;
int quad_flags;
ivec2 pattern_input;
};
struct QuadPrimitive {
RectWithEndpoint bounds;
RectWithEndpoint clip;
vec4 color;
};
QuadSegment fetch_segment(int base, int index) {
QuadSegment seg;
vec4 texels[2] = fetch_from_gpu_buffer_2f(base + 3 + index * 2);
seg.rect = RectWithEndpoint(texels[0].xy, texels[0].zw);
seg.uv_rect = RectWithEndpoint(texels[1].xy, texels[1].zw);
return seg;
}
QuadPrimitive fetch_primitive(int index) {
QuadPrimitive prim;
vec4 texels[3] = fetch_from_gpu_buffer_3f(index);
prim.bounds = RectWithEndpoint(texels[0].xy, texels[0].zw);
prim.clip = RectWithEndpoint(texels[1].xy, texels[1].zw);
prim.color = texels[2];
return prim;
}
struct QuadHeader {
int transform_id;
int z_id;
ivec2 pattern_input;
};
QuadHeader fetch_header(int address) {
ivec4 header = fetch_from_gpu_buffer_1i(address);
QuadHeader qh = QuadHeader(
header.x,
header.y,
header.zw
);
return qh;
}
struct QuadInstance {
// x
int prim_address_i;
// y
int prim_address_f;
// z
int quad_flags;
int edge_flags;
int part_index;
int segment_index;
// w
int picture_task_address;
};
QuadInstance decode_instance() {
QuadInstance qi = QuadInstance(
aData.x,
aData.y,
(aData.z >> 24) & 0xff,
(aData.z >> 16) & 0xff,
(aData.z >> 8) & 0xff,
(aData.z >> 0) & 0xff,
aData.w
);
return qi;
}
struct VertexInfo {
vec2 local_pos;
};
VertexInfo write_vertex(vec2 local_pos,
float z,
Transform transform,
vec2 content_origin,
RectWithEndpoint task_rect,
float device_pixel_scale,
int quad_flags) {
VertexInfo vi;
// Transform the current vertex to world space.
vec4 world_pos = transform.m * vec4(local_pos, 0.0, 1.0);
// Convert the world positions to device pixel space.
vec2 device_pos = world_pos.xy * device_pixel_scale;
if ((quad_flags & QF_APPLY_DEVICE_CLIP) != 0) {
RectWithEndpoint device_clip_rect = RectWithEndpoint(
content_origin,
content_origin + task_rect.p1 - task_rect.p0
);
// Clip to task rect
device_pos = rect_clamp(device_clip_rect, device_pos);
vi.local_pos = (transform.inv_m * vec4(device_pos / device_pixel_scale, 0.0, 1.0)).xy;
} else {
vi.local_pos = local_pos;
}
// Apply offsets for the render task to get correct screen location.
vec2 final_offset = -content_origin + task_rect.p0;
gl_Position = uTransform * vec4(device_pos + final_offset * world_pos.w, z * world_pos.w, world_pos.w);
return vi;
}
float edge_aa_offset(int edge, int flags) {
return ((flags & edge) != 0) ? AA_PIXEL_RADIUS : 0.0;
}
#ifdef WR_VERTEX_SHADER
void pattern_vertex(PrimitiveInfo prim);
#endif
PrimitiveInfo quad_primive_info(void) {
QuadInstance qi = decode_instance();
QuadHeader qh = fetch_header(qi.prim_address_i);
Transform transform = fetch_transform(qh.transform_id);
PictureTask task = fetch_picture_task(qi.picture_task_address);
QuadPrimitive prim = fetch_primitive(qi.prim_address_f);
float z = float(qh.z_id);
QuadSegment seg;
if (qi.segment_index == INVALID_SEGMENT_INDEX) {
seg.rect = prim.bounds;
seg.uv_rect = RectWithEndpoint(vec2(0.0), vec2(0.0));
} else {
seg = fetch_segment(qi.prim_address_f, qi.segment_index);
}
// The local space rect that we will draw, which is effectively:
// - The tile within the primitive we will draw
// - Intersected with any local-space clip rect(s)
// - Expanded for AA edges where appropriate
RectWithEndpoint local_coverage_rect = seg.rect;
// Apply local clip rect
local_coverage_rect.p0 = max(local_coverage_rect.p0, prim.clip.p0);
local_coverage_rect.p1 = min(local_coverage_rect.p1, prim.clip.p1);
local_coverage_rect.p1 = max(local_coverage_rect.p0, local_coverage_rect.p1);
switch (qi.part_index) {
case PART_LEFT:
local_coverage_rect.p1.x = local_coverage_rect.p0.x + AA_PIXEL_RADIUS;
#ifdef SWGL_ANTIALIAS
swgl_antiAlias(EDGE_AA_LEFT);
#else
local_coverage_rect.p0.x -= AA_PIXEL_RADIUS;
local_coverage_rect.p0.y -= AA_PIXEL_RADIUS;
local_coverage_rect.p1.y += AA_PIXEL_RADIUS;
#endif
break;
case PART_TOP:
local_coverage_rect.p0.x = local_coverage_rect.p0.x + AA_PIXEL_RADIUS;
local_coverage_rect.p1.x = local_coverage_rect.p1.x - AA_PIXEL_RADIUS;
local_coverage_rect.p1.y = local_coverage_rect.p0.y + AA_PIXEL_RADIUS;
#ifdef SWGL_ANTIALIAS
swgl_antiAlias(EDGE_AA_TOP);
#else
local_coverage_rect.p0.y -= AA_PIXEL_RADIUS;
#endif
break;
case PART_RIGHT:
local_coverage_rect.p0.x = local_coverage_rect.p1.x - AA_PIXEL_RADIUS;
#ifdef SWGL_ANTIALIAS
swgl_antiAlias(EDGE_AA_RIGHT);
#else
local_coverage_rect.p1.x += AA_PIXEL_RADIUS;
local_coverage_rect.p0.y -= AA_PIXEL_RADIUS;
local_coverage_rect.p1.y += AA_PIXEL_RADIUS;
#endif
break;
case PART_BOTTOM:
local_coverage_rect.p0.x = local_coverage_rect.p0.x + AA_PIXEL_RADIUS;
local_coverage_rect.p1.x = local_coverage_rect.p1.x - AA_PIXEL_RADIUS;
local_coverage_rect.p0.y = local_coverage_rect.p1.y - AA_PIXEL_RADIUS;
#ifdef SWGL_ANTIALIAS
swgl_antiAlias(EDGE_AA_BOTTOM);
#else
local_coverage_rect.p1.y += AA_PIXEL_RADIUS;
#endif
break;
case PART_CENTER:
local_coverage_rect.p0.x += edge_aa_offset(EDGE_AA_LEFT, qi.edge_flags);
local_coverage_rect.p1.x -= edge_aa_offset(EDGE_AA_RIGHT, qi.edge_flags);
local_coverage_rect.p0.y += edge_aa_offset(EDGE_AA_TOP, qi.edge_flags);
local_coverage_rect.p1.y -= edge_aa_offset(EDGE_AA_BOTTOM, qi.edge_flags);
break;
case PART_ALL:
default:
#ifdef SWGL_ANTIALIAS
swgl_antiAlias(qi.edge_flags);
#else
local_coverage_rect.p0.x -= edge_aa_offset(EDGE_AA_LEFT, qi.edge_flags);
local_coverage_rect.p1.x += edge_aa_offset(EDGE_AA_RIGHT, qi.edge_flags);
local_coverage_rect.p0.y -= edge_aa_offset(EDGE_AA_TOP, qi.edge_flags);
local_coverage_rect.p1.y += edge_aa_offset(EDGE_AA_BOTTOM, qi.edge_flags);
#endif
break;
}
vec2 local_pos = mix(local_coverage_rect.p0, local_coverage_rect.p1, aPosition);
float device_pixel_scale = task.device_pixel_scale;
if ((qi.quad_flags & QF_IGNORE_DEVICE_SCALE) != 0) {
device_pixel_scale = 1.0f;
}
VertexInfo vi = write_vertex(
local_pos,
z,
transform,
task.content_origin,
task.task_rect,
device_pixel_scale,
qi.quad_flags
);
v_color = prim.color;
return PrimitiveInfo(
vi.local_pos,
prim.bounds,
prim.clip,
seg,
qi.edge_flags,
qi.quad_flags,
qh.pattern_input
);
}
void antialiasing_vertex(PrimitiveInfo prim) {
#ifndef SWGL_ANTIALIAS
// This does the setup that is required for init_tranform_vs.
RectWithEndpoint xf_bounds = RectWithEndpoint(
max(prim.local_prim_rect.p0, prim.local_clip_rect.p0),
min(prim.local_prim_rect.p1, prim.local_clip_rect.p1)
);
vTransformBounds = vec4(xf_bounds.p0, xf_bounds.p1);
vLocalPos = prim.local_pos;
if (prim.edge_flags == 0) {
v_flags.w = 0;
} else {
v_flags.w = 1;
}
#endif
}
void main() {
PrimitiveInfo prim = quad_primive_info();
antialiasing_vertex(prim);
pattern_vertex(prim);
}
#endif
#ifdef WR_FRAGMENT_SHADER
vec4 pattern_fragment(vec4 base_color);
float antialiasing_fragment() {
float alpha = 1.0;
#ifndef SWGL_ANTIALIAS
if (v_flags.w != 0) {
alpha = init_transform_fs(vLocalPos);
}
#endif
return alpha;
}
void main() {
vec4 base_color = v_color;
base_color *= antialiasing_fragment();
oFragColor = pattern_fragment(base_color);
}
#endif
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Preprocess the radii for computing the distance approximation. This should
// be used in the vertex shader if possible to avoid doing expensive division
// in the fragment shader. When dealing with a point (zero radii), approximate
// it as an ellipse with very small radii so that we don't need to branch.
vec2 inverse_radii_squared(vec2 radii) {
return 1.0 / max(radii * radii, 1.0e-6);
}
#ifdef WR_FRAGMENT_SHADER
// One iteration of Newton's method on the 2D equation of an ellipse:
//
// E(x, y) = x^2/a^2 + y^2/b^2 - 1
//
// The Jacobian of this equation is:
//
// J(E(x, y)) = [ 2*x/a^2 2*y/b^2 ]
//
// We approximate the distance with:
//
// E(x, y) / ||J(E(x, y))||
//
// See G. Taubin, "Distance Approximations for Rasterizing Implicit
// Curves", section 3.
//
// A scale relative to the unit scale of the ellipse may be passed in to cause
// the math to degenerate to length(p) when scale is 0, or otherwise give the
// normal distance approximation if scale is 1.
float distance_to_ellipse_approx(vec2 p, vec2 inv_radii_sq, float scale) {
vec2 p_r = p * inv_radii_sq;
float g = dot(p, p_r) - scale;
vec2 dG = (1.0 + scale) * p_r;
return g * inversesqrt(dot(dG, dG));
}
// Slower but more accurate version that uses the exact distance when dealing
// with a 0-radius point distance and otherwise uses the faster approximation
// when dealing with non-zero radii.
float distance_to_ellipse(vec2 p, vec2 radii) {
return distance_to_ellipse_approx(p, inverse_radii_squared(radii),
float(all(greaterThan(radii, vec2(0.0)))));
}
float distance_to_rounded_rect(
vec2 pos,
vec3 plane_tl,
vec4 center_radius_tl,
vec3 plane_tr,
vec4 center_radius_tr,
vec3 plane_br,
vec4 center_radius_br,
vec3 plane_bl,
vec4 center_radius_bl,
vec4 rect_bounds
) {
// Clip against each ellipse. If the fragment is in a corner, one of the
// branches below will select it as the corner to calculate the distance
// to. We use half-space planes to detect which corner's ellipse the
// fragment is inside, where the plane is defined by a normal and offset.
// If outside any ellipse, default to a small offset so a negative distance
// is returned for it.
vec4 corner = vec4(vec2(1.0e-6), vec2(1.0));
// Calculate the ellipse parameters for each corner.
center_radius_tl.xy = center_radius_tl.xy - pos;
center_radius_tr.xy = (center_radius_tr.xy - pos) * vec2(-1.0, 1.0);
center_radius_br.xy = pos - center_radius_br.xy;
center_radius_bl.xy = (center_radius_bl.xy - pos) * vec2(1.0, -1.0);
// Evaluate each half-space plane in turn to select a corner.
if (dot(pos, plane_tl.xy) > plane_tl.z) {
corner = center_radius_tl;
}
if (dot(pos, plane_tr.xy) > plane_tr.z) {
corner = center_radius_tr;
}
if (dot(pos, plane_br.xy) > plane_br.z) {
corner = center_radius_br;
}
if (dot(pos, plane_bl.xy) > plane_bl.z) {
corner = center_radius_bl;
}
// Calculate the distance of the selected corner and the rectangle bounds,
// whichever is greater.
return max(distance_to_ellipse_approx(corner.xy, corner.zw, 1.0),
signed_distance_rect(pos, rect_bounds.xy, rect_bounds.zw));
}
#endif
varying highp vec4 vClipLocalPos;
#ifdef WR_FEATURE_FAST_PATH
flat varying highp vec3 v_clip_params; // xy = box size, z = radius
#else
flat varying highp vec4 vClipCenter_Radius_TL;
flat varying highp vec4 vClipCenter_Radius_TR;
flat varying highp vec4 vClipCenter_Radius_BR;
flat varying highp vec4 vClipCenter_Radius_BL;
// We pack 4 vec3 clip planes into 3 vec4 to save a varying slot.
flat varying highp vec4 vClipPlane_A;
flat varying highp vec4 vClipPlane_B;
flat varying highp vec4 vClipPlane_C;
#endif
flat varying highp vec2 vClipMode;
#ifdef WR_VERTEX_SHADER
PER_INSTANCE in ivec4 aClipData;
#define CLIP_SPACE_RASTER 0
#define CLIP_SPACE_PRIMITIVE 1
struct Clip {
RectWithEndpoint rect;
#ifdef WR_FEATURE_FAST_PATH
vec4 radii;
#else
vec4 radii_top;
vec4 radii_bottom;
#endif
float mode;
int space;
};
Clip fetch_clip(int index) {
Clip clip;
clip.space = aClipData.z;
#ifdef WR_FEATURE_FAST_PATH
vec4 texels[3] = fetch_from_gpu_buffer_3f(index);
clip.rect = RectWithEndpoint(texels[0].xy, texels[0].zw);
clip.radii = texels[1];
clip.mode = texels[2].x;
#else
vec4 texels[4] = fetch_from_gpu_buffer_4f(index);
clip.rect = RectWithEndpoint(texels[0].xy, texels[0].zw);
clip.radii_top = texels[1];
clip.radii_bottom = texels[2];
clip.mode = texels[3].x;
#endif
return clip;
}
void pattern_vertex(PrimitiveInfo prim_info) {
Clip clip = fetch_clip(aClipData.y);
Transform clip_transform = fetch_transform(aClipData.x);
vClipLocalPos = clip_transform.m * vec4(prim_info.local_pos, 0.0, 1.0);
#ifndef WR_FEATURE_FAST_PATH
if (clip.space == CLIP_SPACE_RASTER) {
vTransformBounds = vec4(clip.rect.p0, clip.rect.p1);
} else {
RectWithEndpoint xf_bounds = RectWithEndpoint(
max(clip.rect.p0, prim_info.local_clip_rect.p0),
min(clip.rect.p1, prim_info.local_clip_rect.p1)
);
vTransformBounds = vec4(xf_bounds.p0, xf_bounds.p1);
}
#endif
vClipMode.x = clip.mode;
#ifdef WR_FEATURE_FAST_PATH
// If the radii are all uniform, we can use a much simpler 2d
// signed distance function to get a rounded rect clip.
vec2 half_size = 0.5 * (clip.rect.p1 - clip.rect.p0);
float radius = clip.radii.x;
vClipLocalPos.xy -= (half_size + clip.rect.p0) * vClipLocalPos.w;
v_clip_params = vec3(half_size - vec2(radius), radius);
#else
vec2 r_tl = clip.radii_top.xy;
vec2 r_tr = clip.radii_top.zw;
vec2 r_br = clip.radii_bottom.zw;
vec2 r_bl = clip.radii_bottom.xy;
vClipCenter_Radius_TL = vec4(clip.rect.p0 + r_tl,
inverse_radii_squared(r_tl));
vClipCenter_Radius_TR = vec4(clip.rect.p1.x - r_tr.x,
clip.rect.p0.y + r_tr.y,
inverse_radii_squared(r_tr));
vClipCenter_Radius_BR = vec4(clip.rect.p1 - r_br,
inverse_radii_squared(r_br));
vClipCenter_Radius_BL = vec4(clip.rect.p0.x + r_bl.x,
clip.rect.p1.y - r_bl.y,
inverse_radii_squared(r_bl));
// We need to know the half-spaces of the corners separate from the center
// and radius. We compute a point that falls on the diagonal (which is just
// an inner vertex pushed out along one axis, but not on both) to get the
// plane offset of the half-space. We also compute the direction vector of
// the half-space, which is a perpendicular vertex (-y,x) of the vector of
// the diagonal. We leave the scales of the vectors unchanged.
vec2 n_tl = -r_tl.yx;
vec2 n_tr = vec2(r_tr.y, -r_tr.x);
vec2 n_br = r_br.yx;
vec2 n_bl = vec2(-r_bl.y, r_bl.x);
vec3 tl = vec3(n_tl,
dot(n_tl, vec2(clip.rect.p0.x, clip.rect.p0.y + r_tl.y)));
vec3 tr = vec3(n_tr,
dot(n_tr, vec2(clip.rect.p1.x - r_tr.x, clip.rect.p0.y)));
vec3 br = vec3(n_br,
dot(n_br, vec2(clip.rect.p1.x, clip.rect.p1.y - r_br.y)));
vec3 bl = vec3(n_bl,
dot(n_bl, vec2(clip.rect.p0.x + r_bl.x, clip.rect.p1.y)));
vClipPlane_A = vec4(tl.x, tl.y, tl.z, tr.x);
vClipPlane_B = vec4(tr.y, tr.z, br.x, br.y);
vClipPlane_C = vec4(br.z, bl.x, bl.y, bl.z);
#endif
}
#endif
#ifdef WR_FRAGMENT_SHADER
#ifdef WR_FEATURE_FAST_PATH
float sd_box(in vec2 pos, in vec2 box_size) {
vec2 d = abs(pos) - box_size;
return length(max(d, vec2(0.0))) + min(max(d.x,d.y), 0.0);
}
float sd_rounded_box(in vec2 pos, in vec2 box_size, in float radius) {
return sd_box(pos, box_size) - radius;
}
#endif
vec4 pattern_fragment(vec4 _base_color) {
vec2 clip_local_pos = vClipLocalPos.xy / vClipLocalPos.w;
float aa_range = compute_aa_range(clip_local_pos);
#ifdef WR_FEATURE_FAST_PATH
float dist = sd_rounded_box(clip_local_pos, v_clip_params.xy, v_clip_params.z);
#else
vec3 plane_tl = vec3(vClipPlane_A.x, vClipPlane_A.y, vClipPlane_A.z);
vec3 plane_tr = vec3(vClipPlane_A.w, vClipPlane_B.x, vClipPlane_B.y);
vec3 plane_br = vec3(vClipPlane_B.z, vClipPlane_B.w, vClipPlane_C.x);
vec3 plane_bl = vec3(vClipPlane_C.y, vClipPlane_C.z, vClipPlane_C.w);
float dist = distance_to_rounded_rect(
clip_local_pos,
plane_tl,
vClipCenter_Radius_TL,
plane_tr,
vClipCenter_Radius_TR,
plane_br,
vClipCenter_Radius_BR,
plane_bl,
vClipCenter_Radius_BL,
vTransformBounds
);
#endif
// Compute AA for the given dist and range.
float alpha = distance_aa(aa_range, dist);
// Select alpha or inverse alpha depending on clip in/out.
float final_alpha = mix(alpha, 1.0 - alpha, vClipMode.x);
return vec4(final_alpha);
}
#endif