Skip to content

Commit

Permalink
Webrender update (border anti-aliasing, primitive caching).
Browse files Browse the repository at this point in the history
  • Loading branch information
gw3583 committed Aug 31, 2016
1 parent acb4700 commit 25f60a2
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 66 deletions.
4 changes: 2 additions & 2 deletions components/servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ports/cef/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions resources/shaders/prim_shared.glsl
Expand Up @@ -12,6 +12,9 @@
#define PST_BOTTOM uint(6)
#define PST_RIGHT uint(7)

#define UV_NORMALIZED uint(0)
#define UV_PIXEL uint(1)

// Border styles as defined in webrender_traits/types.rs
#define BORDER_STYLE_NONE uint(0)
#define BORDER_STYLE_SOLID uint(1)
Expand Down
78 changes: 29 additions & 49 deletions resources/shaders/ps_border.fs.glsl
Expand Up @@ -109,46 +109,6 @@ vec4 draw_dotted_edge() {
return mix(white, circleColor, circleColor.a);
}

// Our current edge calculation is based only on
// the size of the border-size, but we need to draw
// the dashes in the center of the segment we're drawing.
// This calculates how much to nudge and which axis to nudge on.
vec2 get_dashed_nudge_factor(vec2 dash_size, bool is_corner) {
if (is_corner) {
return vec2(0.0, 0.0);
}

bool xAxisFudge = vBorders.z > vBorders.w;
if (xAxisFudge) {
return vec2(dash_size.x / 2.0, 0);
}

return vec2(0.0, dash_size.y / 2.0);
}

vec4 draw_dashed_edge(bool is_corner) {
// Everything here should be in device pixels.
// We want the dot to be roughly the size of the whole border spacing
// 5.5 here isn't a magic number, it's just what mostly looks like FF/Chrome
// TODO: Investigate exactly what FF does.
float dash_interval = min(vBorders.w, vBorders.z) * 5.5;
vec2 edge_size = vec2(vBorders.z, vBorders.w);
vec2 dash_size = vec2(dash_interval / 2.0, dash_interval / 2.0);
vec2 position = vDevicePos - vBorders.xy;

vec2 dash_count = floor(edge_size/ dash_interval);
vec2 dist_between_dashes = edge_size / dash_count;

vec2 target_rect_index = floor(position / dist_between_dashes);
vec2 target_rect_loc = target_rect_index * dist_between_dashes;
target_rect_loc += get_dashed_nudge_factor(dash_size, is_corner);
vec4 target_rect = vec4(target_rect_loc, dash_size);

vec4 white = vec4(1.0, 1.0, 1.0, 1.0);
vec4 target_colored_rect = drawRect(position, target_rect, vVerticalColor.xyz);
return mix(white, target_colored_rect, target_colored_rect.a);
}

void draw_dotted_border(void) {
switch (vBorderPart) {
// These are the layer tile part PrimitivePart as uploaded by the tiling.rs
Expand All @@ -172,32 +132,51 @@ void draw_dotted_border(void) {
}
}

void draw_dashed_border(void) {
#endif

vec4 draw_dashed_edge(float position, float border_width) {
// TODO: Investigate exactly what FF does.
float size = border_width * 3;
float segment = floor(position / size) + 2;
return mix(vec4(0, 0, 0, 0), vHorizontalColor, mod(segment, 2));
}

void draw_dashed_border(vec2 local_pos, float distance_from_mix_line) {
// This is the conversion factor for transformations and device pixel scaling.
float pixels_per_fragment = length(fwidth(local_pos.xy));

switch (vBorderPart) {
// These are the layer tile part PrimitivePart as uploaded by the tiling.rs
case PST_TOP_LEFT:
case PST_TOP_RIGHT:
case PST_BOTTOM_LEFT:
case PST_BOTTOM_RIGHT:
{
// TODO: Fix for corners with a border-radius
bool is_corner = true;
oFragColor = draw_dashed_edge(is_corner);
oFragColor = get_fragment_color(distance_from_mix_line, pixels_per_fragment);
if (vRadii.x > 0.0) {
oFragColor *= vec4(1, 1, 1, alpha_for_solid_border_corner(local_pos,
vRadii.z,
vRadii.x,
pixels_per_fragment));
}

break;
}
case PST_BOTTOM:
case PST_TOP:
{
oFragColor = draw_dashed_edge(vLocalPos.x - vPieceRect.x, vPieceRect.w);
break;
}
case PST_LEFT:
case PST_RIGHT:
{
bool is_corner = false;
oFragColor = draw_dashed_edge(is_corner);
oFragColor = draw_dashed_edge(vLocalPos.y - vPieceRect.y, vPieceRect.z);
break;
}
}
}

#endif

vec4 draw_double_edge(float pos,
float len,
Expand Down Expand Up @@ -343,6 +322,8 @@ void main(void) {

switch (vBorderStyle) {
case BORDER_STYLE_DASHED:
draw_dashed_border(local_pos, distance_from_mix_line);
break;
case BORDER_STYLE_DOTTED:
case BORDER_STYLE_OUTSET:
case BORDER_STYLE_INSET:
Expand All @@ -362,8 +343,7 @@ void main(void) {
#else
switch (vBorderStyle) {
case BORDER_STYLE_DASHED:
discard_pixels_in_rounded_borders(local_pos);
draw_dashed_border();
draw_dashed_border(local_pos, vDistanceFromMixLine);
break;
case BORDER_STYLE_DOTTED:
discard_pixels_in_rounded_borders(local_pos);
Expand Down
3 changes: 2 additions & 1 deletion resources/shaders/ps_border.glsl
Expand Up @@ -16,10 +16,11 @@ flat varying vec2 vRefPoint;
flat varying uint vBorderStyle;
flat varying uint vBorderPart; // Which part of the border we're drawing.

flat varying vec4 vPieceRect;

// These are in device space
#ifdef WR_FEATURE_TRANSFORM
varying vec3 vLocalPos; // The clamped position in local space.
flat varying vec4 vPieceRect;
flat varying float vPieceRectHypotenuseLength;
#else
varying vec2 vLocalPos; // The clamped position in local space.
Expand Down
3 changes: 2 additions & 1 deletion resources/shaders/ps_border.vs.glsl
Expand Up @@ -111,12 +111,13 @@ void main(void) {
float width = x1 - x0;
float height = y1 - y0;

vPieceRect = vec4(x0, y0, width, height);

// The fragment shader needs to calculate the distance from the bisecting line
// to properly mix border colors. For transformed borders, we calculate this distance
// in the fragment shader itself. For non-transformed borders, we can use the
// interpolator.
#ifdef WR_FEATURE_TRANSFORM
vPieceRect = vec4(x0, y0, width, height);
vPieceRectHypotenuseLength = sqrt(pow(width, 2) + pow(height, 2));
#else
vDistanceFromMixLine = (vi.local_clamped_pos.x - x0) * height -
Expand Down
26 changes: 20 additions & 6 deletions resources/shaders/ps_image.vs.glsl
Expand Up @@ -5,8 +5,8 @@

struct Image {
PrimitiveInfo info;
vec4 st_rect; // Location of the image texture in the texture atlas.
vec4 stretch_size; // Size of the actual image.
vec4 st_rect; // Location of the image texture in the texture atlas.
vec4 stretch_size_uvkind; // Size of the actual image.
};

layout(std140) uniform Items {
Expand All @@ -20,13 +20,27 @@ void main(void) {
TransformVertexInfo vi = write_transform_vertex(image.info);
vLocalRect = vi.clipped_local_rect;
vLocalPos = vi.local_pos;
vStretchSize = image.stretch_size.xy;
vStretchSize = image.stretch_size_uvkind.xy;
#else
VertexInfo vi = write_vertex(image.info);
vUv = (vi.local_clamped_pos - vi.local_rect.p0) / image.stretch_size.xy;
vUv = (vi.local_clamped_pos - vi.local_rect.p0) / image.stretch_size_uvkind.xy;
#endif

// vUv will contain how many times this image has wrapped around the image size.
vTextureSize = image.st_rect.zw - image.st_rect.xy;
vTextureOffset = image.st_rect.xy;
vec2 st0 = image.st_rect.xy;
vec2 st1 = image.st_rect.zw;

switch (uint(image.stretch_size_uvkind.z)) {
case UV_NORMALIZED:
break;
case UV_PIXEL: {
vec2 texture_size = textureSize(sDiffuse, 0);
st0 /= texture_size;
st1 /= texture_size;
}
break;
}

vTextureSize = st1 - st0;
vTextureOffset = st0;
}
25 changes: 20 additions & 5 deletions resources/shaders/ps_image_clip.vs.glsl
Expand Up @@ -5,8 +5,8 @@

struct Image {
PrimitiveInfo info;
vec4 st_rect; // Location of the image texture in the texture atlas.
vec4 stretch_size; // Size of the actual image.
vec4 st_rect; // Location of the image texture in the texture atlas.
vec4 stretch_size_uvkind; // Size of the actual image.
Clip clip;
};

Expand All @@ -26,7 +26,22 @@ void main(void) {
vPos = vi.local_clamped_pos;

// vUv will contain how many times this image has wrapped around the image size.
vUv = (vi.local_clamped_pos - image.info.local_rect.xy) / image.stretch_size.xy;
vTextureSize = image.st_rect.zw - image.st_rect.xy;
vTextureOffset = image.st_rect.xy;
vUv = (vi.local_clamped_pos - image.info.local_rect.xy) / image.stretch_size_uvkind.xy;

vec2 st0 = image.st_rect.xy;
vec2 st1 = image.st_rect.zw;

switch (uint(image.stretch_size_uvkind.z)) {
case UV_NORMALIZED:
break;
case UV_PIXEL: {
vec2 texture_size = textureSize(sDiffuse, 0);
st0 /= texture_size;
st1 /= texture_size;
}
break;
}

vTextureSize = st1 - st0;
vTextureOffset = st0;
}

0 comments on commit 25f60a2

Please sign in to comment.