1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/* 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/. */
#include rect,render_task,gpu_cache,transform
#define EXTEND_MODE_CLAMP 0
#define EXTEND_MODE_REPEAT 1
#define SUBPX_DIR_NONE 0
#define SUBPX_DIR_HORIZONTAL 1
#define SUBPX_DIR_VERTICAL 2
#define SUBPX_DIR_MIXED 3
#define RASTER_LOCAL 0
#define RASTER_SCREEN 1
uniform sampler2D sClipMask;
vec2 clamp_rect(vec2 pt, RectWithSize rect) {
return clamp(pt, rect.p0, rect.p0 + rect.size);
}
#ifndef SWGL
// TODO: convert back to RectWithEndPoint if driver issues are resolved, if ever.
flat varying vec4 vClipMaskUvBounds;
// XY and W are homogeneous coordinates, Z is the layer index
varying vec4 vClipMaskUv;
#endif
#ifdef WR_VERTEX_SHADER
#define COLOR_MODE_FROM_PASS 0
#define COLOR_MODE_ALPHA 1
#define COLOR_MODE_SUBPX_CONST_COLOR 2
#define COLOR_MODE_SUBPX_BG_PASS0 3
#define COLOR_MODE_SUBPX_BG_PASS1 4
#define COLOR_MODE_SUBPX_BG_PASS2 5
#define COLOR_MODE_SUBPX_DUAL_SOURCE 6
#define COLOR_MODE_BITMAP 7
#define COLOR_MODE_COLOR_BITMAP 8
#define COLOR_MODE_IMAGE 9
uniform HIGHP_SAMPLER_FLOAT sampler2D sPrimitiveHeadersF;
uniform HIGHP_SAMPLER_FLOAT isampler2D sPrimitiveHeadersI;
// Instanced attributes
PER_INSTANCE in ivec4 aData;
#define VECS_PER_PRIM_HEADER_F 2U
#define VECS_PER_PRIM_HEADER_I 2U
struct Instance
{
int prim_header_address;
int picture_task_address;
int clip_address;
int segment_index;
int flags;
int resource_address;
int brush_kind;
};
Instance decode_instance_attributes() {
Instance instance;
instance.prim_header_address = aData.x;
instance.picture_task_address = aData.y >> 16;
instance.clip_address = aData.y & 0xffff;
instance.segment_index = aData.z & 0xffff;
instance.flags = aData.z >> 16;
instance.resource_address = aData.w & 0xffffff;
instance.brush_kind = aData.w >> 24;
return instance;
}
struct PrimitiveHeader {
RectWithSize local_rect;
RectWithSize local_clip_rect;
float z;
int specific_prim_address;
int transform_id;
ivec4 user_data;
};
PrimitiveHeader fetch_prim_header(int index) {
PrimitiveHeader ph;
ivec2 uv_f = get_fetch_uv(index, VECS_PER_PRIM_HEADER_F);
vec4 local_rect = TEXEL_FETCH(sPrimitiveHeadersF, uv_f, 0, ivec2(0, 0));
vec4 local_clip_rect = TEXEL_FETCH(sPrimitiveHeadersF, uv_f, 0, ivec2(1, 0));
ph.local_rect = RectWithSize(local_rect.xy, local_rect.zw);
ph.local_clip_rect = RectWithSize(local_clip_rect.xy, local_clip_rect.zw);
ivec2 uv_i = get_fetch_uv(index, VECS_PER_PRIM_HEADER_I);
ivec4 data0 = TEXEL_FETCH(sPrimitiveHeadersI, uv_i, 0, ivec2(0, 0));
ivec4 data1 = TEXEL_FETCH(sPrimitiveHeadersI, uv_i, 0, ivec2(1, 0));
ph.z = float(data0.x);
ph.specific_prim_address = data0.y;
ph.transform_id = data0.z;
ph.user_data = data1;
return ph;
}
struct VertexInfo {
vec2 local_pos;
vec4 world_pos;
};
VertexInfo write_vertex(vec2 local_pos,
RectWithSize local_clip_rect,
float z,
Transform transform,
PictureTask task) {
// Clamp to the two local clip rects.
vec2 clamped_local_pos = clamp_rect(local_pos, local_clip_rect);
// Transform the current vertex to world space.
vec4 world_pos = transform.m * vec4(clamped_local_pos, 0.0, 1.0);
// Convert the world positions to device pixel space.
vec2 device_pos = world_pos.xy * task.device_pixel_scale;
// Apply offsets for the render task to get correct screen location.
vec2 final_offset = -task.content_origin + task.common_data.task_rect.p0;
gl_Position = uTransform * vec4(device_pos + final_offset * world_pos.w, z * world_pos.w, world_pos.w);
VertexInfo vi = VertexInfo(
clamped_local_pos,
world_pos
);
return vi;
}
float cross2(vec2 v0, vec2 v1) {
return v0.x * v1.y - v0.y * v1.x;
}
// Return intersection of line (p0,p1) and line (p2,p3)
vec2 intersect_lines(vec2 p0, vec2 p1, vec2 p2, vec2 p3) {
vec2 d0 = p0 - p1;
vec2 d1 = p2 - p3;
float s0 = cross2(p0, p1);
float s1 = cross2(p2, p3);
float d = cross2(d0, d1);
float nx = s0 * d1.x - d0.x * s1;
float ny = s0 * d1.y - d0.y * s1;
return vec2(nx / d, ny / d);
}
VertexInfo write_transform_vertex(RectWithSize local_segment_rect,
RectWithSize local_prim_rect,
RectWithSize local_clip_rect,
vec4 clip_edge_mask,
float z,
Transform transform,
PictureTask task) {
// Calculate a clip rect from local_rect + local clip
RectWithEndpoint clip_rect = to_rect_with_endpoint(local_clip_rect);
RectWithEndpoint segment_rect = to_rect_with_endpoint(local_segment_rect);
segment_rect.p0 = clamp(segment_rect.p0, clip_rect.p0, clip_rect.p1);
segment_rect.p1 = clamp(segment_rect.p1, clip_rect.p0, clip_rect.p1);
// Calculate a clip rect from local_rect + local clip
RectWithEndpoint prim_rect = to_rect_with_endpoint(local_prim_rect);
prim_rect.p0 = clamp(prim_rect.p0, clip_rect.p0, clip_rect.p1);
prim_rect.p1 = clamp(prim_rect.p1, clip_rect.p0, clip_rect.p1);
// As this is a transform shader, extrude by 2 (local space) pixels
// in each direction. This gives enough space around the edge to
// apply distance anti-aliasing. Technically, it:
// (a) slightly over-estimates the number of required pixels in the simple case.
// (b) might not provide enough edge in edge case perspective projections.
// However, it's fast and simple. If / when we ever run into issues, we
// can do some math on the projection matrix to work out a variable
// amount to extrude.
// Only extrude along edges where we are going to apply AA.
float extrude_amount = 2.0;
vec4 extrude_distance = vec4(extrude_amount) * clip_edge_mask;
local_segment_rect.p0 -= extrude_distance.xy;
local_segment_rect.size += extrude_distance.xy + extrude_distance.zw;
// Select the corner of the local rect that we are processing.
vec2 local_pos = local_segment_rect.p0 + local_segment_rect.size * aPosition.xy;
// Convert the world positions to device pixel space.
vec2 task_offset = task.common_data.task_rect.p0 - task.content_origin;
// Transform the current vertex to the world cpace.
vec4 world_pos = transform.m * vec4(local_pos, 0.0, 1.0);
vec4 final_pos = vec4(
world_pos.xy * task.device_pixel_scale + task_offset * world_pos.w,
z * world_pos.w,
world_pos.w
);
gl_Position = uTransform * final_pos;
init_transform_vs(mix(
vec4(prim_rect.p0, prim_rect.p1),
vec4(segment_rect.p0, segment_rect.p1),
clip_edge_mask
));
VertexInfo vi = VertexInfo(
local_pos,
world_pos
);
return vi;
}
void write_clip(vec4 world_pos, ClipArea area, PictureTask task) {
#ifdef SWGL
swgl_clipMask(
sClipMask,
(task.common_data.task_rect.p0 - task.content_origin) - (area.common_data.task_rect.p0 - area.screen_origin),
area.common_data.task_rect.p0,
area.common_data.task_rect.size
);
#else
vec2 uv = world_pos.xy * area.device_pixel_scale +
world_pos.w * (area.common_data.task_rect.p0 - area.screen_origin);
vClipMaskUvBounds = vec4(
area.common_data.task_rect.p0,
area.common_data.task_rect.p0 + area.common_data.task_rect.size
);
vClipMaskUv = vec4(uv, area.common_data.texture_layer_index, world_pos.w);
#endif
}
// Read the exta image data containing the homogeneous screen space coordinates
// of the corners, interpolate between them, and return real screen space UV.
vec2 get_image_quad_uv(int address, vec2 f) {
ImageResourceExtra extra_data = fetch_image_resource_extra(address);
vec4 x = mix(extra_data.st_tl, extra_data.st_tr, f.x);
vec4 y = mix(extra_data.st_bl, extra_data.st_br, f.x);
vec4 z = mix(x, y, f.y);
return z.xy / z.w;
}
#endif //WR_VERTEX_SHADER
#ifdef WR_FRAGMENT_SHADER
struct Fragment {
vec4 color;
#ifdef WR_FEATURE_DUAL_SOURCE_BLENDING
vec4 blend;
#endif
};
float do_clip() {
#ifdef SWGL
// SWGL relies on builtin clip-mask support to do this more efficiently,
// so no clipping is required here.
return 1.0;
#else
// check for the dummy bounds, which are given to the opaque objects
if (vClipMaskUvBounds.xy == vClipMaskUvBounds.zw) {
return 1.0;
}
// anything outside of the mask is considered transparent
//Note: we assume gl_FragCoord.w == interpolated(1 / vClipMaskUv.w)
vec2 mask_uv = vClipMaskUv.xy * gl_FragCoord.w;
bvec2 left = lessThanEqual(vClipMaskUvBounds.xy, mask_uv); // inclusive
bvec2 right = greaterThan(vClipMaskUvBounds.zw, mask_uv); // non-inclusive
// bail out if the pixel is outside the valid bounds
if (!all(bvec4(left, right))) {
return 0.0;
}
// finally, the slow path - fetch the mask value from an image
return texelFetch(sClipMask, ivec2(mask_uv), 0).r;
#endif
}
#endif //WR_FRAGMENT_SHADER
|