Skip to content

Commit 3967c7d

Browse files
committed
Mask generation in the off-screen cache textures with support for multiple rounded cornered rectangles.
Removal of the _clip shader variants.
1 parent 3345d0b commit 3967c7d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+876
-679
lines changed

replay/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ fn main() {
7979
let resource_path = &args[1];
8080
let ref dir = args[2];
8181
let window = glutin::WindowBuilder::new()
82+
.with_title("WebRender Replay")
8283
.with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (3,2)))
8384
.build()
8485
.unwrap();

sample/src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ fn main() {
6363
let res_path = &args[1];
6464

6565
let window = glutin::WindowBuilder::new()
66+
.with_title("WebRender Sample")
6667
.with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (3, 2)))
6768
.build()
6869
.unwrap();
@@ -133,14 +134,15 @@ fn main() {
133134
&mut auxiliary_lists_builder));
134135

135136
let clip_region = {
136-
let rect = Rect::new(Point2D::new(100.0, 100.0), Size2D::new(100.0, 100.0));
137137
let mask = webrender_traits::ImageMask {
138138
image: api.add_image(2, 2, None, ImageFormat::A8, vec![0,80, 180, 255]),
139-
rect: rect,
139+
rect: Rect::new(Point2D::new(75.0, 75.0), Size2D::new(100.0, 100.0)),
140140
repeat: false,
141141
};
142142
let radius = webrender_traits::BorderRadius::uniform(20.0);
143-
let complex = webrender_traits::ComplexClipRegion::new(rect, radius);
143+
let complex = webrender_traits::ComplexClipRegion::new(
144+
Rect::new(Point2D::new(50.0, 50.0), Size2D::new(100.0, 100.0)),
145+
radius);
144146

145147
webrender_traits::ClipRegion::new(&bounds,
146148
vec![complex],

webrender/res/clip_shared.glsl

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,49 @@
33
* License, v. 2.0. If a copy of the MPL was not distributed with this
44
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
55

6-
flat varying vec4 vClipRect;
7-
flat varying vec4 vClipRadius;
8-
flat varying vec4 vClipMaskUvRect;
9-
flat varying vec4 vClipMaskLocalRect;
10-
116
#ifdef WR_VERTEX_SHADER
12-
void write_clip(ClipData clip) {
13-
vClipRect = vec4(clip.rect.rect.xy, clip.rect.rect.xy + clip.rect.rect.zw);
14-
vClipRadius = vec4(clip.top_left.outer_inner_radius.x,
15-
clip.top_right.outer_inner_radius.x,
16-
clip.bottom_right.outer_inner_radius.x,
17-
clip.bottom_left.outer_inner_radius.x);
18-
//TODO: interpolate the final mask UV
19-
vec2 texture_size = textureSize(sMask, 0);
20-
vClipMaskUvRect = clip.mask_data.uv_rect / texture_size.xyxy;
21-
vClipMaskLocalRect = clip.mask_data.local_rect; //TODO: transform
22-
}
23-
#endif
247

25-
#ifdef WR_FRAGMENT_SHADER
26-
float do_clip(vec2 pos) {
27-
vec2 ref_tl = vClipRect.xy + vec2( vClipRadius.x, vClipRadius.x);
28-
vec2 ref_tr = vClipRect.zy + vec2(-vClipRadius.y, vClipRadius.y);
29-
vec2 ref_br = vClipRect.zw + vec2(-vClipRadius.z, -vClipRadius.z);
30-
vec2 ref_bl = vClipRect.xw + vec2( vClipRadius.w, -vClipRadius.w);
8+
struct CacheClipInstance {
9+
int render_task_index;
10+
int layer_index;
11+
int data_index;
12+
};
3113

32-
float d_tl = distance(pos, ref_tl);
33-
float d_tr = distance(pos, ref_tr);
34-
float d_br = distance(pos, ref_br);
35-
float d_bl = distance(pos, ref_bl);
14+
CacheClipInstance fetch_clip_item(int index) {
15+
CacheClipInstance cci;
3616

37-
float pixels_per_fragment = length(fwidth(pos.xy));
38-
float nudge = 0.5 * pixels_per_fragment;
39-
vec4 distances = vec4(d_tl, d_tr, d_br, d_bl) - vClipRadius + nudge;
17+
int offset = index * 1;
4018

41-
bvec4 is_out = bvec4(pos.x < ref_tl.x && pos.y < ref_tl.y,
42-
pos.x > ref_tr.x && pos.y < ref_tr.y,
43-
pos.x > ref_br.x && pos.y > ref_br.y,
44-
pos.x < ref_bl.x && pos.y > ref_bl.y);
19+
ivec4 data0 = int_data[offset + 0];
4520

46-
float distance_from_border = dot(vec4(is_out),
47-
max(vec4(0.0, 0.0, 0.0, 0.0), distances));
21+
cci.render_task_index = data0.x;
22+
cci.layer_index = data0.y;
23+
cci.data_index = data0.z;
24+
25+
return cci;
26+
}
4827

49-
// Move the distance back into pixels.
50-
distance_from_border /= pixels_per_fragment;
51-
// Apply a more gradual fade out to transparent.
52-
//distance_from_border -= 0.5;
28+
// The transformed vertex function that always covers the whole whole clip area,
29+
// which is the intersection of all clip instances of a given primitive
30+
TransformVertexInfo write_clip_tile_vertex(vec4 local_clip_rect,
31+
Layer layer,
32+
ClipArea area) {
33+
vec2 lp0_base = local_clip_rect.xy;
34+
vec2 lp1_base = local_clip_rect.xy + local_clip_rect.zw;
5335

54-
float border_alpha = 1.0 - smoothstep(0.0, 1.0, distance_from_border);
36+
vec2 lp0 = clamp_rect(lp0_base, layer.local_clip_rect);
37+
vec2 lp1 = clamp_rect(lp1_base, layer.local_clip_rect);
38+
vec4 clipped_local_rect = vec4(lp0, lp1 - lp0);
5539

56-
bool repeat_mask = false; //TODO
57-
vec2 vMaskUv = (pos - vClipMaskLocalRect.xy) / vClipMaskLocalRect.zw;
58-
vec2 clamped_mask_uv = repeat_mask ? fract(vMaskUv) :
59-
clamp(vMaskUv, vec2(0.0, 0.0), vec2(1.0, 1.0));
60-
vec2 source_uv = clamped_mask_uv * vClipMaskUvRect.zw + vClipMaskUvRect.xy;
61-
float mask_alpha = texture(sMask, source_uv).r; //careful: texture has type A8
40+
vec2 final_pos = mix(area.task_bounds.xy, area.task_bounds.zw, aPosition.xy);
6241

63-
return border_alpha * mask_alpha;
42+
// compute the point position in side the layer, in CSS space
43+
vec2 clamped_pos = final_pos + area.screen_origin_target_index.xy - area.task_bounds.xy;
44+
vec4 layer_pos = get_layer_pos(clamped_pos / uDevicePixelRatio, layer);
45+
46+
gl_Position = uTransform * vec4(final_pos, 0.0, 1);
47+
48+
return TransformVertexInfo(layer_pos.xyw, clamped_pos, clipped_local_rect);
6449
}
65-
#endif
50+
51+
#endif //WR_VERTEX_SHADER

webrender/res/cs_clip_clear.fs.glsl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
void main(void) {
6+
oFragColor = vec4(1.0, 1.0, 1.0, 1.0);
7+
}

webrender/res/cs_clip_clear.vs.glsl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#line 1
2+
/* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
void main(void) {
7+
CacheClipInstance cci = fetch_clip_item(gl_InstanceID);
8+
ClipArea area = fetch_clip_area(cci.render_task_index);
9+
10+
vec2 final_pos = mix(area.task_bounds.xy, area.task_bounds.zw, aPosition.xy);
11+
12+
gl_Position = uTransform * vec4(final_pos, 0.0, 1.0);
13+
}

webrender/res/cs_clip_image.fs.glsl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
void main(void) {
6+
float alpha = 1.f;
7+
vec2 local_pos = init_transform_fs(vPos, vLocalRect, alpha);
8+
9+
bool repeat_mask = false; //TODO
10+
vec2 clamped_mask_uv = repeat_mask ? fract(vClipMaskUv.xy) :
11+
clamp(vClipMaskUv.xy, vec2(0.0, 0.0), vec2(1.0, 1.0));
12+
vec2 source_uv = clamped_mask_uv * vClipMaskUvRect.zw + vClipMaskUvRect.xy;
13+
float clip_alpha = texture(sMask, source_uv).r; //careful: texture has type A8
14+
15+
oFragColor = vec4(1.0, 1.0, 1.0, min(alpha, clip_alpha));
16+
}

webrender/res/ps_rectangle_clip.glsl renamed to webrender/res/cs_clip_image.glsl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
66

7-
varying vec4 vColor;
8-
9-
#ifdef WR_FEATURE_TRANSFORM
107
varying vec3 vPos;
118
flat varying vec4 vLocalRect;
12-
#else
13-
varying vec2 vPos;
14-
#endif
9+
flat varying vec4 vClipMaskUvRect;

webrender/res/cs_clip_image.vs.glsl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#line 1
2+
/* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
struct ImageMaskData {
7+
vec4 uv_rect;
8+
vec4 local_rect;
9+
};
10+
11+
ImageMaskData fetch_mask_data(int index) {
12+
ImageMaskData info;
13+
14+
ivec2 uv = get_fetch_uv_2(index);
15+
16+
info.uv_rect = texelFetchOffset(sData32, uv, 0, ivec2(0, 0));
17+
info.local_rect = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
18+
19+
return info;
20+
}
21+
22+
void main(void) {
23+
CacheClipInstance cci = fetch_clip_item(gl_InstanceID);
24+
ClipArea area = fetch_clip_area(cci.render_task_index);
25+
Layer layer = fetch_layer(cci.layer_index);
26+
ImageMaskData mask = fetch_mask_data(cci.data_index);
27+
vec4 local_rect = mask.local_rect;
28+
29+
TransformVertexInfo vi = write_clip_tile_vertex(local_rect,
30+
layer,
31+
area);
32+
vLocalRect = vi.clipped_local_rect;
33+
vPos = vi.local_pos;
34+
35+
vClipMaskUv = vec3((vPos.xy / vPos.z - local_rect.xy) / local_rect.zw, 0.0);
36+
vec2 texture_size = textureSize(sMask, 0);
37+
vClipMaskUvRect = mask.uv_rect / texture_size.xyxy;
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
float rounded_rect(vec2 pos) {
6+
vec2 ref_tl = vClipRect.xy + vec2( vClipRadius.x, vClipRadius.x);
7+
vec2 ref_tr = vClipRect.zy + vec2(-vClipRadius.y, vClipRadius.y);
8+
vec2 ref_br = vClipRect.zw + vec2(-vClipRadius.z, -vClipRadius.z);
9+
vec2 ref_bl = vClipRect.xw + vec2( vClipRadius.w, -vClipRadius.w);
10+
11+
float d_tl = distance(pos, ref_tl);
12+
float d_tr = distance(pos, ref_tr);
13+
float d_br = distance(pos, ref_br);
14+
float d_bl = distance(pos, ref_bl);
15+
16+
float pixels_per_fragment = length(fwidth(pos.xy));
17+
float nudge = 0.5 * pixels_per_fragment;
18+
vec4 distances = vec4(d_tl, d_tr, d_br, d_bl) - vClipRadius + nudge;
19+
20+
bvec4 is_out = bvec4(pos.x < ref_tl.x && pos.y < ref_tl.y,
21+
pos.x > ref_tr.x && pos.y < ref_tr.y,
22+
pos.x > ref_br.x && pos.y > ref_br.y,
23+
pos.x < ref_bl.x && pos.y > ref_bl.y);
24+
25+
float distance_from_border = dot(vec4(is_out),
26+
max(vec4(0.0, 0.0, 0.0, 0.0), distances));
27+
28+
// Move the distance back into pixels.
29+
distance_from_border /= pixels_per_fragment;
30+
// Apply a more gradual fade out to transparent.
31+
//distance_from_border -= 0.5;
32+
33+
return 1.0 - smoothstep(0.0, 1.0, distance_from_border);
34+
}
35+
36+
37+
void main(void) {
38+
float alpha = 1.f;
39+
vec2 local_pos = init_transform_fs(vPos, vLocalRect, alpha);
40+
41+
float clip_alpha = rounded_rect(local_pos);
42+
43+
oFragColor = vec4(1.0, 1.0, 1.0, min(alpha, clip_alpha));
44+
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1+
#line 1
2+
13
/* This Source Code Form is subject to the terms of the Mozilla Public
24
* License, v. 2.0. If a copy of the MPL was not distributed with this
35
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
46

5-
varying vec4 vColor;
6-
7-
#ifdef WR_FEATURE_TRANSFORM
8-
varying vec3 vLocalPos;
7+
varying vec3 vPos;
98
flat varying vec4 vLocalRect;
10-
#else
11-
varying vec2 vPos;
12-
#endif
9+
flat varying vec4 vClipRect;
10+
flat varying vec4 vClipRadius;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#line 1
2+
/* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
struct ClipRect {
7+
vec4 rect;
8+
vec4 dummy;
9+
};
10+
11+
ClipRect fetch_clip_rect(int index) {
12+
ClipRect rect;
13+
14+
ivec2 uv = get_fetch_uv_2(index);
15+
16+
rect.rect = texelFetchOffset(sData32, uv, 0, ivec2(0, 0));
17+
//rect.dummy = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
18+
rect.dummy = vec4(0.0, 0.0, 0.0, 0.0);
19+
20+
return rect;
21+
}
22+
23+
struct ClipCorner {
24+
vec4 rect;
25+
vec4 outer_inner_radius;
26+
};
27+
28+
ClipCorner fetch_clip_corner(int index) {
29+
ClipCorner corner;
30+
31+
ivec2 uv = get_fetch_uv_2(index);
32+
33+
corner.rect = texelFetchOffset(sData32, uv, 0, ivec2(0, 0));
34+
corner.outer_inner_radius = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
35+
36+
return corner;
37+
}
38+
39+
struct ClipData {
40+
ClipRect rect;
41+
ClipCorner top_left;
42+
ClipCorner top_right;
43+
ClipCorner bottom_left;
44+
ClipCorner bottom_right;
45+
};
46+
47+
ClipData fetch_clip(int index) {
48+
ClipData clip;
49+
50+
clip.rect = fetch_clip_rect(index + 0);
51+
clip.top_left = fetch_clip_corner(index + 1);
52+
clip.top_right = fetch_clip_corner(index + 2);
53+
clip.bottom_left = fetch_clip_corner(index + 3);
54+
clip.bottom_right = fetch_clip_corner(index + 4);
55+
56+
return clip;
57+
}
58+
59+
void main(void) {
60+
CacheClipInstance cci = fetch_clip_item(gl_InstanceID);
61+
ClipArea area = fetch_clip_area(cci.render_task_index);
62+
Layer layer = fetch_layer(cci.layer_index);
63+
ClipData clip = fetch_clip(cci.data_index);
64+
vec4 local_rect = clip.rect.rect;
65+
66+
TransformVertexInfo vi = write_clip_tile_vertex(local_rect,
67+
layer,
68+
area);
69+
vLocalRect = vi.clipped_local_rect;
70+
vPos = vi.local_pos;
71+
72+
vClipRect = vec4(local_rect.xy, local_rect.xy + local_rect.zw);
73+
vClipRadius = vec4(clip.top_left.outer_inner_radius.x,
74+
clip.top_right.outer_inner_radius.x,
75+
clip.bottom_right.outer_inner_radius.x,
76+
clip.bottom_left.outer_inner_radius.x);
77+
}

0 commit comments

Comments
 (0)