Bug 1571974: Simplify orientation handling in line decoration shaders. r=kvark

We want to use the same line decoration (dashed, dotted, wavy) shader code for
both horizontal and vertical lines, so it makes sense for them to use a
coordinate system that has been rotated (transposed, actually) so that .x always
runs parallel to the line being decorated, and .y is always perpendicular.

Before this patch, we passed the orientation enum as a vertex attribute, used a
switch to swap coordinates in the vertex shader, and then swapped them again in
the fragment shader.

This patch trades the orientation for a f32 'axis select' vertex attribute, and
uses `mix` to swap them in the vertex shader. Then no consideration is necessary
in the fragment shader: the vLocalPos varying is already in the appropriate form.

Since get_line_decoration_sizes is already thinking in terms of line-parallel
coordinates, it might seem like a good idea for decoration jobs to simply use
line-parallel coordinates throughout. However, this actually results in more
swapping and opportunities for confusion: much of the CPU work is concerned with
the rectangle the decoration's mask occupies in the texture cache, which is
axis-aligned.

Differential Revision: https://phabricator.services.mozilla.com/D60926

--HG--
extra : rebase_source : 8dcd8455c664067dd25f583c944d611a35c25e79
extra : source : dfb21632ea198c1acdc6a34ee08113d516f666d5
This commit is contained in:
Jim Blandy 2020-01-24 21:46:56 +00:00
parent 3e1f7b939b
commit 2436afd008
3 changed files with 28 additions and 32 deletions

View File

@ -9,41 +9,37 @@
#define LINE_STYLE_DASHED 2
#define LINE_STYLE_WAVY 3
// Local space position
// Fragment position in the coordinate system used for positioning decorations.
// To keep the code independent of whether the line is horizontal or vertical,
// vLocalPos.x is always parallel, and .y always perpendicular, to the line
// being decorated.
varying vec2 vLocalPos;
flat varying float vAxisSelect;
flat varying int vStyle;
flat varying vec4 vParams;
#ifdef WR_VERTEX_SHADER
#define LINE_ORIENTATION_VERTICAL 0
#define LINE_ORIENTATION_HORIZONTAL 1
// The size of the mask tile we're rendering, in pixels.
in vec4 aTaskRect;
// The size of the mask tile. aLocalSize.x is always horizontal and .y vertical,
// regardless of the line's orientation. The size is chosen by
// prim_store::get_line_decoration_sizes.
in vec2 aLocalSize;
// A LINE_STYLE_* value, indicating what sort of line to draw.
in int aStyle;
in int aOrientation;
// 0.0 for a horizontal line, 1.0 for a vertical line.
in float aAxisSelect;
// The thickness of the wavy line itself, not the amplitude of the waves (i.e.,
// the thickness of the final decorated line).
in float aWavyLineThickness;
void main(void) {
vec2 size;
switch (aOrientation) {
case LINE_ORIENTATION_HORIZONTAL:
vAxisSelect = 0.0;
size = aLocalSize;
break;
case LINE_ORIENTATION_VERTICAL:
vAxisSelect = 1.0;
size = aLocalSize.yx;
break;
default:
vAxisSelect = 0.0;
size = vec2(0.0);
}
vec2 size = mix(aLocalSize, aLocalSize.yx, aAxisSelect);
vStyle = aStyle;
switch (vStyle) {
@ -86,7 +82,7 @@ void main(void) {
vParams = vec4(0.0);
}
vLocalPos = aPosition.xy * aLocalSize;
vLocalPos = mix(aPosition.xy, aPosition.yx, aAxisSelect) * size;
gl_Position = uTransform * vec4(aTaskRect.xy + aTaskRect.zw * aPosition.xy, 0.0, 1.0);
}
@ -98,13 +94,10 @@ void main(void) {
void main(void) {
// Find the appropriate distance to apply the step over.
vec2 local_pos = vLocalPos;
float aa_range = compute_aa_range(local_pos);
vec2 pos = vLocalPos;
float aa_range = compute_aa_range(pos);
float alpha = 1.0;
// Select the x/y coord, depending on which axis this edge is.
vec2 pos = mix(local_pos.xy, local_pos.yx, vAxisSelect);
switch (vStyle) {
case LINE_STYLE_SOLID: {
break;

View File

@ -773,7 +773,10 @@ impl TextureCacheRenderTarget {
task_rect: target_rect.0.to_f32(),
local_size: info.local_size,
style: info.style as i32,
orientation: info.orientation as i32,
axis_select: match info.orientation {
LineOrientation::Horizontal => 0.0,
LineOrientation::Vertical => 1.0,
},
wavy_line_thickness: info.wavy_line_thickness,
});
}
@ -1056,7 +1059,7 @@ pub struct LineDecorationJob {
pub local_size: LayoutSize,
pub wavy_line_thickness: f32,
pub style: i32,
pub orientation: i32,
pub axis_select: f32,
}
#[cfg_attr(feature = "capture", derive(Serialize))]

View File

@ -454,9 +454,9 @@ pub(crate) mod desc {
kind: VertexAttributeKind::I32,
},
VertexAttribute {
name: "aOrientation",
name: "aAxisSelect",
count: 1,
kind: VertexAttributeKind::I32,
kind: VertexAttributeKind::F32,
},
],
};