Skip to content

Commit

Permalink
Auto merge of #2623 - kvark:shader-module, r=glennw
Browse files Browse the repository at this point in the history
Separate primitive shaders from clip shaders

Includes #2622

Note: this is not done because I'm bored.
The problem Szeged team has bumped into is that `aDataX` attributes end up being used in the shaders they don't relate to (e.g. clip shaders). This isn't affecting GLSL but it does affect GLSL -> SPIRV -> XXX shader pipelines.

The solution I came up with involves moving parts of `prim_shared` into separate shared modules, and turning `clip_shared` into a catch-all header of clip shades.

Downsides: more shader modules (but not programs) to navigate between.
Any suggestions on alternative solutions are welcome ;)

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/webrender/2623)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Apr 6, 2018
2 parents a05186d + 0b1808a commit f685027
Show file tree
Hide file tree
Showing 15 changed files with 522 additions and 453 deletions.
1 change: 0 additions & 1 deletion webrender/res/base.glsl
Expand Up @@ -33,6 +33,5 @@

#ifdef WR_FRAGMENT_SHADER
precision highp float;

#define varying in
#endif
4 changes: 2 additions & 2 deletions webrender/res/brush.glsl
Expand Up @@ -103,14 +103,14 @@ void main(void) {
brush_prim.local_rect
);

// TODO(gw): vLocalBounds may be referenced by
// TODO(gw): transform bounds may be referenced by
// the fragment shader when running in
// the alpha pass, even on non-transformed
// items. For now, just ensure it has no
// effect. We can tidy this up as we move
// more items to be brush shaders.
#ifdef WR_FEATURE_ALPHA_PASS
vLocalBounds = vec4(vec2(-1000000.0), vec2(1000000.0));
init_transform_vs(vec4(vec2(-1000000.0), vec2(1000000.0)));
#endif
} else {
bvec4 edge_mask = notEqual(brush.edge_mask & ivec4(1, 2, 4, 8), ivec4(0));
Expand Down
88 changes: 88 additions & 0 deletions webrender/res/clip_scroll.glsl
@@ -0,0 +1,88 @@
/* 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_CLIP_SCROLL_NODE 9

uniform HIGHP_SAMPLER_FLOAT sampler2D sClipScrollNodes;

struct ClipScrollNode {
mat4 transform;
mat4 inv_transform;
bool is_axis_aligned;
};

ClipScrollNode fetch_clip_scroll_node(int index) {
ClipScrollNode node;

// 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 OSX.
ivec2 uv = get_fetch_uv(index, VECS_PER_CLIP_SCROLL_NODE);
ivec2 uv0 = ivec2(uv.x + 0, uv.y);
ivec2 uv1 = ivec2(uv.x + 8, uv.y);

node.transform[0] = TEXEL_FETCH(sClipScrollNodes, uv0, 0, ivec2(0, 0));
node.transform[1] = TEXEL_FETCH(sClipScrollNodes, uv0, 0, ivec2(1, 0));
node.transform[2] = TEXEL_FETCH(sClipScrollNodes, uv0, 0, ivec2(2, 0));
node.transform[3] = TEXEL_FETCH(sClipScrollNodes, uv0, 0, ivec2(3, 0));

node.inv_transform[0] = TEXEL_FETCH(sClipScrollNodes, uv0, 0, ivec2(4, 0));
node.inv_transform[1] = TEXEL_FETCH(sClipScrollNodes, uv0, 0, ivec2(5, 0));
node.inv_transform[2] = TEXEL_FETCH(sClipScrollNodes, uv0, 0, ivec2(6, 0));
node.inv_transform[3] = TEXEL_FETCH(sClipScrollNodes, uv0, 0, ivec2(7, 0));

vec4 misc = TEXEL_FETCH(sClipScrollNodes, uv1, 0, ivec2(0, 0));
node.is_axis_aligned = misc.x == 0.0;

return node;
}

// 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 ClipScrollNode 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 ClipScrollNode 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 ClipScrollNode

vec4 r = inv_transform * vec4(ref, z, 1.0);
return r;
}

// Given a CSS space position, transform it back into the ClipScrollNode space.
vec4 get_node_pos(vec2 pos, ClipScrollNode node) {
// get a point on the scroll node plane
vec4 ah = node.transform * 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(node.inv_transform)) * vec3(0.0, 0.0, 1.0);
return untransform(pos, n, a, node.inv_transform);
}

#endif //WR_VERTEX_SHADER

14 changes: 13 additions & 1 deletion webrender/res/clip_shared.glsl
Expand Up @@ -2,6 +2,8 @@
* 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/. */

#include rect,clip_scroll,render_task,resource_cache,snap,transform

#ifdef WR_VERTEX_SHADER

#define SEGMENT_ALL 0
Expand Down Expand Up @@ -88,10 +90,20 @@ ClipVertexInfo write_clip_tile_vertex(RectWithSize local_clip_rect,

gl_Position = uTransform * vec4(vertex_pos, 0.0, 1);

vLocalBounds = vec4(local_clip_rect.p0, local_clip_rect.p0 + local_clip_rect.size);
init_transform_vs(vec4(local_clip_rect.p0, local_clip_rect.p0 + local_clip_rect.size));

ClipVertexInfo vi = ClipVertexInfo(node_pos.xyw, actual_pos, local_clip_rect);
return vi;
}

#endif //WR_VERTEX_SHADER

#ifdef WR_FRAGMENT_SHADER

//Note: identical to prim_shared
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);
}

#endif //WR_FRAGMENT_SHADER
2 changes: 1 addition & 1 deletion webrender/res/cs_clip_border.glsl
Expand Up @@ -2,7 +2,7 @@
* 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/. */

#include shared,prim_shared,clip_shared
#include shared,clip_shared

varying vec3 vPos;

Expand Down
8 changes: 2 additions & 6 deletions webrender/res/cs_clip_box_shadow.glsl
Expand Up @@ -2,7 +2,7 @@
* 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/. */

#include shared,prim_shared,clip_shared
#include shared,clip_shared

varying vec3 vPos;
varying vec2 vUv;
Expand Down Expand Up @@ -103,11 +103,7 @@ void main(void) {
uv = mix(vUvBounds_NoClamp.xy, vUvBounds_NoClamp.zw, uv);
uv = clamp(uv, vUvBounds.xy, vUvBounds.zw);

float in_shadow_rect = point_inside_rect(
local_pos,
vLocalBounds.xy,
vLocalBounds.zw
);
float in_shadow_rect = init_transform_rough_fs(local_pos);

float texel = TEX_SAMPLE(sColor0, vec3(uv, vLayer)).r;

Expand Down
10 changes: 6 additions & 4 deletions webrender/res/cs_clip_image.glsl
Expand Up @@ -2,9 +2,11 @@
* 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/. */

#include shared,prim_shared,clip_shared
#include shared,clip_shared

varying vec3 vPos;
varying vec3 vClipMaskImageUv;

flat varying vec4 vClipMaskUvRect;
flat varying vec4 vClipMaskUvInnerRect;
flat varying float vLayer;
Expand Down Expand Up @@ -36,7 +38,7 @@ void main(void) {
vPos = vi.local_pos;
vLayer = res.layer;

vClipMaskUv = vec3((vPos.xy / vPos.z - local_rect.p0) / local_rect.size, 0.0);
vClipMaskImageUv = vec3((vPos.xy / vPos.z - local_rect.p0) / local_rect.size, 0.0);
vec2 texture_size = vec2(textureSize(sColor0, 0));
vClipMaskUvRect = vec4(res.uv_rect.p0, res.uv_rect.p1 - res.uv_rect.p0) / texture_size.xyxy;
// applying a half-texel offset to the UV boundaries to prevent linear samples from the outside
Expand All @@ -50,8 +52,8 @@ void main(void) {
float alpha = init_transform_fs(vPos.xy / vPos.z);

bool repeat_mask = false; //TODO
vec2 clamped_mask_uv = repeat_mask ? fract(vClipMaskUv.xy) :
clamp(vClipMaskUv.xy, vec2(0.0, 0.0), vec2(1.0, 1.0));
vec2 clamped_mask_uv = repeat_mask ? fract(vClipMaskImageUv.xy) :
clamp(vClipMaskImageUv.xy, vec2(0.0, 0.0), vec2(1.0, 1.0));
vec2 source_uv = clamp(clamped_mask_uv * vClipMaskUvRect.zw + vClipMaskUvRect.xy,
vClipMaskUvInnerRect.xy, vClipMaskUvInnerRect.zw);
float clip_alpha = texture(sColor0, vec3(source_uv, vLayer)).r; //careful: texture has type A8
Expand Down
7 changes: 6 additions & 1 deletion webrender/res/cs_clip_line.glsl
Expand Up @@ -2,7 +2,12 @@
* 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/. */

#include shared,prim_shared,clip_shared
#include shared,clip_shared

#define LINE_STYLE_SOLID 0
#define LINE_STYLE_DOTTED 1
#define LINE_STYLE_DASHED 2
#define LINE_STYLE_WAVY 3

varying vec3 vLocalPos;

Expand Down
8 changes: 5 additions & 3 deletions webrender/res/cs_clip_rectangle.glsl
Expand Up @@ -2,7 +2,7 @@
* 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/. */

#include shared,prim_shared,clip_shared,ellipse
#include shared,clip_shared,ellipse

varying vec3 vPos;
flat varying float vClipMode;
Expand All @@ -19,7 +19,8 @@ struct ClipRect {

ClipRect fetch_clip_rect(ivec2 address) {
vec4 data[2] = fetch_from_resource_cache_2_direct(address);
return ClipRect(RectWithSize(data[0].xy, data[0].zw), data[1]);
ClipRect rect = ClipRect(RectWithSize(data[0].xy, data[0].zw), data[1]);
return rect;
}

struct ClipCorner {
Expand All @@ -32,7 +33,8 @@ struct ClipCorner {
ClipCorner fetch_clip_corner(ivec2 address, float index) {
address += ivec2(2 + 2 * int(index), 0);
vec4 data[2] = fetch_from_resource_cache_2_direct(address);
return ClipCorner(RectWithSize(data[0].xy, data[0].zw), data[1]);
ClipCorner corner = ClipCorner(RectWithSize(data[0].xy, data[0].zw), data[1]);
return corner;
}

struct ClipData {
Expand Down

0 comments on commit f685027

Please sign in to comment.