Skip to content

Commit a5d39ff

Browse files
committed
Image mask support - shader side
1 parent 44eaff9 commit a5d39ff

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

webrender/res/clip_shared.glsl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
flat varying vec4 vClipRect;
77
flat varying vec4 vClipRadius;
8+
flat varying vec4 vClipMaskUvRect;
9+
flat varying vec4 vClipMaskScreenRect;
810

911
#ifdef WR_VERTEX_SHADER
1012
void write_clip(Clip clip) {
@@ -13,6 +15,9 @@ void write_clip(Clip clip) {
1315
clip.top_right.outer_inner_radius.x,
1416
clip.bottom_right.outer_inner_radius.x,
1517
clip.bottom_left.outer_inner_radius.x);
18+
//TODO: interpolate the final mask UV
19+
vClipMaskUvRect = clip.mask_rect.uv;
20+
vClipMaskScreenRect = clip.mask_rect.screen;
1621
}
1722
#endif
1823

@@ -46,6 +51,13 @@ float do_clip(vec2 pos) {
4651
// Apply a more gradual fade out to transparent.
4752
//distance_from_border -= 0.5;
4853

49-
return 1.0 - smoothstep(0.0, 1.0, distance_from_border);
54+
float border_alpha = 1.0 - smoothstep(0.0, 1.0, distance_from_border);
55+
56+
vec2 vMaskUv = (pos - vClipMaskScreenRect.xy) / vClipMaskScreenRect.zw;
57+
vec2 clamped_mask_uv = clamp(vMaskUv, vec2(0.0, 0.0), vec2(1.0, 1.0));
58+
vec2 source_uv = clamped_mask_uv * vClipMaskUvRect.zw + vClipMaskUvRect.xy;
59+
float mask_alpha = texture(sMask, source_uv).r; //careful: texture has type A8
60+
61+
return border_alpha * mask_alpha;
5062
}
5163
#endif

webrender/res/prim_shared.glsl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ PrimitiveInstance fetch_instance(int index) {
257257

258258
return pi;
259259
}
260-
261260
struct Primitive {
262261
Layer layer;
263262
Tile tile;
@@ -298,7 +297,24 @@ ClipRect fetch_clip_rect(int index) {
298297
ivec2 uv = get_fetch_uv_2(index);
299298

300299
rect.rect = texelFetchOffset(sData32, uv, 0, ivec2(0, 0));
301-
rect.dummy = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
300+
//rect.dummy = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
301+
rect.dummy = vec4(0.0, 0.0, 0.0, 0.0);
302+
303+
return rect;
304+
}
305+
306+
struct MaskRect {
307+
vec4 uv;
308+
vec4 screen;
309+
};
310+
311+
MaskRect fetch_mask_rect(int index) {
312+
MaskRect rect;
313+
314+
ivec2 uv = get_fetch_uv_2(index);
315+
316+
rect.uv = texelFetchOffset(sData32, uv, 0, ivec2(0, 0));
317+
rect.screen = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
302318

303319
return rect;
304320
}
@@ -325,6 +341,7 @@ struct Clip {
325341
ClipCorner top_right;
326342
ClipCorner bottom_left;
327343
ClipCorner bottom_right;
344+
MaskRect mask_rect;
328345
};
329346

330347
Clip fetch_clip(int index) {
@@ -335,6 +352,7 @@ Clip fetch_clip(int index) {
335352
clip.top_right = fetch_clip_corner(index + 2);
336353
clip.bottom_left = fetch_clip_corner(index + 3);
337354
clip.bottom_right = fetch_clip_corner(index + 4);
355+
clip.mask_rect = fetch_mask_rect(index+5);
338356

339357
return clip;
340358
}

0 commit comments

Comments
 (0)