Skip to content

Commit fcab1e9

Browse files
nicalweb-flow
authored andcommitted
Bug 1888628 - Extract the texture sampling logic out of ps_quad.glsl. r=gw
This simplifies the common infrastructure, removes two varyings from the common set and will allow other patterns to handle sampling differently if they need it (for example an upcoming repeating pattern). In addition: - the color parameter is always passed to the fragment shader (it used to be only when no uv_rect was passed). - v_flags was reorganized a bit so that w is used by the common infrastructure and xyz are available for patterns to use. Differential Revision: https://phabricator.services.mozilla.com/D206098
1 parent db2f109 commit fcab1e9

File tree

5 files changed

+87
-51
lines changed

5 files changed

+87
-51
lines changed

webrender/res/ps_quad.glsl

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@
3737
#include shared,rect,transform,render_task,gpu_buffer
3838

3939
flat varying mediump vec4 v_color;
40-
flat varying mediump vec4 v_uv_sample_bounds;
41-
// x: (in ps_quad_textured) has edge flags
42-
// y: has uv rect
43-
// z: (in ps_quad_textured) sample as mask
40+
// w: has edge flags
41+
// x,y,z are avaible for patterns to use.
4442
flat varying lowp ivec4 v_flags;
45-
varying highp vec2 v_uv;
4643

4744
#ifndef SWGL_ANTIALIAS
4845
varying highp vec2 vLocalPos;
@@ -74,22 +71,24 @@ varying highp vec2 vLocalPos;
7471

7572
PER_INSTANCE in ivec4 aData;
7673

74+
struct QuadSegment {
75+
RectWithEndpoint rect;
76+
RectWithEndpoint uv_rect;
77+
};
78+
7779
struct PrimitiveInfo {
7880
vec2 local_pos;
7981

8082
RectWithEndpoint local_prim_rect;
8183
RectWithEndpoint local_clip_rect;
8284

85+
QuadSegment segment;
86+
8387
int edge_flags;
8488
int quad_flags;
8589
ivec2 pattern_input;
8690
};
8791

88-
struct QuadSegment {
89-
RectWithEndpoint rect;
90-
vec4 uv_rect;
91-
};
92-
9392
struct QuadPrimitive {
9493
RectWithEndpoint bounds;
9594
RectWithEndpoint clip;
@@ -102,7 +101,7 @@ QuadSegment fetch_segment(int base, int index) {
102101
vec4 texels[2] = fetch_from_gpu_buffer_2f(base + 3 + index * 2);
103102

104103
seg.rect = RectWithEndpoint(texels[0].xy, texels[0].zw);
105-
seg.uv_rect = texels[1];
104+
seg.uv_rect = RectWithEndpoint(texels[1].xy, texels[1].zw);
106105

107106
return seg;
108107
}
@@ -232,7 +231,7 @@ PrimitiveInfo quad_primive_info(void) {
232231
QuadSegment seg;
233232
if (qi.segment_index == INVALID_SEGMENT_INDEX) {
234233
seg.rect = prim.bounds;
235-
seg.uv_rect = vec4(0.0);
234+
seg.uv_rect = RectWithEndpoint(vec2(0.0), vec2(0.0));
236235
} else {
237236
seg = fetch_segment(qi.prim_address_f, qi.segment_index);
238237
}
@@ -325,35 +324,13 @@ PrimitiveInfo quad_primive_info(void) {
325324
qi.quad_flags
326325
);
327326

328-
if (seg.uv_rect.xy == seg.uv_rect.zw) {
329-
v_color = prim.color;
330-
v_flags.y = 0;
331-
} else {
332-
v_color = vec4(1.0);
333-
v_flags.y = 1;
334-
335-
vec2 f = (vi.local_pos - seg.rect.p0) / (seg.rect.p1 - seg.rect.p0);
336-
337-
vec2 uv = mix(
338-
seg.uv_rect.xy,
339-
seg.uv_rect.zw,
340-
f
341-
);
342-
343-
vec2 texture_size = vec2(TEX_SIZE(sColor0));
344-
345-
v_uv = uv / texture_size;
346-
347-
v_uv_sample_bounds = vec4(
348-
seg.uv_rect.xy + vec2(0.5),
349-
seg.uv_rect.zw - vec2(0.5)
350-
) / texture_size.xyxy;
351-
}
327+
v_color = prim.color;
352328

353329
return PrimitiveInfo(
354330
vi.local_pos,
355331
prim.bounds,
356332
prim.clip,
333+
seg,
357334
qi.edge_flags,
358335
qi.quad_flags,
359336
qh.pattern_input
@@ -372,9 +349,9 @@ void antialiasing_vertex(PrimitiveInfo prim) {
372349
vLocalPos = prim.local_pos;
373350

374351
if (prim.edge_flags == 0) {
375-
v_flags.x = 0;
352+
v_flags.w = 0;
376353
} else {
377-
v_flags.x = 1;
354+
v_flags.w = 1;
378355
}
379356
#endif
380357
}
@@ -392,7 +369,7 @@ vec4 pattern_fragment(vec4 base_color);
392369
float antialiasing_fragment() {
393370
float alpha = 1.0;
394371
#ifndef SWGL_ANTIALIAS
395-
if (v_flags.x != 0) {
372+
if (v_flags.w != 0) {
396373
alpha = init_transform_fs(vLocalPos);
397374
}
398375
#endif

webrender/res/ps_quad_textured.glsl

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,42 @@
44

55
/// This shader renders solid colors or simple images in a color or alpha target.
66

7-
#include ps_quad
7+
#include ps_quad,sample_color0
8+
9+
#define v_flags_textured v_flags.x
10+
#define v_flags_sample_as_mask v_flags.y
811

912
#ifdef WR_VERTEX_SHADER
13+
1014
void pattern_vertex(PrimitiveInfo info) {
15+
// Note: Since the uv rect is passed via segments, This shader cannot sample from a
16+
// texture if no segments are provided
17+
if (info.segment.uv_rect.p0 != info.segment.uv_rect.p1) {
18+
// Textured
19+
v_flags_textured = 1;
20+
21+
vec2 f = (info.local_pos - info.segment.rect.p0) / rect_size(info.segment.rect);
22+
vs_init_sample_color0(f, info.segment.uv_rect);
23+
} else {
24+
// Solid color
25+
v_flags_textured = 0;
26+
}
27+
1128
if ((info.quad_flags & QF_SAMPLE_AS_MASK) != 0) {
12-
v_flags.z = 1;
29+
v_flags_sample_as_mask = 1;
1330
} else {
14-
v_flags.z = 0;
31+
v_flags_sample_as_mask = 0;
1532
}
1633
}
34+
1735
#endif
1836

1937
#ifdef WR_FRAGMENT_SHADER
38+
2039
vec4 pattern_fragment(vec4 color) {
21-
if (v_flags.y != 0) {
22-
vec2 uv = clamp(v_uv, v_uv_sample_bounds.xy, v_uv_sample_bounds.zw);
23-
vec4 texel = TEX_SAMPLE(sColor0, uv);
24-
if (v_flags.z != 0) {
40+
if (v_flags_textured != 0) {
41+
vec4 texel = fs_sample_color0();
42+
if (v_flags_sample_as_mask != 0) {
2543
texel = texel.rrrr;
2644
}
2745
color *= texel;
@@ -32,12 +50,12 @@ vec4 pattern_fragment(vec4 color) {
3250

3351
#if defined(SWGL_DRAW_SPAN)
3452
void swgl_drawSpanRGBA8() {
35-
if (v_flags.y != 0) {
36-
if (v_flags.z != 0) {
53+
if (v_flags_textured != 0) {
54+
if (v_flags_sample_as_mask != 0) {
3755
// Fall back to fragment shader as we don't specialize for mask yet. Perhaps
3856
// we can use an existing swgl commit or add a new one though?
3957
} else {
40-
swgl_commitTextureLinearColorRGBA8(sColor0, v_uv, v_uv_sample_bounds, v_color);
58+
swgl_commitTextureLinearColorRGBA8(sColor0, v_uv0, v_uv0_sample_bounds, v_color);
4159
}
4260
} else {
4361
swgl_commitSolidRGBA8(v_color);

webrender/res/sample_color0.glsl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
/// This file provides the boilerplate for sampling from sColor0 with strict sample bounds.
6+
7+
#include shared
8+
9+
flat varying mediump vec4 v_uv0_sample_bounds;
10+
varying highp vec2 v_uv0;
11+
12+
#ifdef WR_VERTEX_SHADER
13+
14+
/// sample_pos is in 0..1 normalized coordinates
15+
/// uv_rect is in pixel.
16+
void vs_init_sample_color0(vec2 sample_pos, RectWithEndpoint uv_rect) {
17+
vec2 uv = mix(uv_rect.p0, uv_rect.p1, sample_pos);
18+
19+
vec2 texture_size = vec2(TEX_SIZE(sColor0));
20+
21+
v_uv0 = uv / texture_size;
22+
23+
v_uv0_sample_bounds = vec4(
24+
uv_rect.p0 + vec2(0.5),
25+
uv_rect.p1 - vec2(0.5)
26+
) / texture_size.xyxy;
27+
}
28+
29+
#endif
30+
31+
#ifdef WR_FRAGMENT_SHADER
32+
33+
/// The vertex shader must have called vs_init_sample_color0
34+
vec4 fs_sample_color0() {
35+
vec2 uv = clamp(v_uv0, v_uv0_sample_bounds.xy, v_uv0_sample_bounds.zw);
36+
vec4 texel = TEX_SAMPLE(sColor0, uv);
37+
38+
return texel;
39+
}
40+
41+
#endif

webrender/src/pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Pattern {
6262
Pattern {
6363
kind: PatternKind::ColorOrTexture,
6464
shader_input: PatternShaderInput::default(),
65-
base_color: PremultipliedColorF::BLACK,
65+
base_color: PremultipliedColorF::WHITE,
6666
is_opaque: false,
6767
}
6868
}

webrender/src/quad.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ fn add_composite_prim(
616616
&mut frame_state.frame_gpu_data.f32,
617617
rect,
618618
rect,
619-
pattern.base_color,
619+
PremultipliedColorF::WHITE,
620620
segments,
621621
);
622622

0 commit comments

Comments
 (0)