Skip to content

Change text-shadow to blur entire text run, and clean up texture cache. #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions webrender/res/cs_blur.fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#line 1
/* 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/. */

// TODO(gw): Write a fast path blur that handles smaller blur radii
// with a offset / weight uniform table and a constant
// loop iteration count!

// TODO(gw): Make use of the bilinear sampling trick to reduce
// the number of texture fetches needed for a gaussian blur.

float gauss(float x, float sigma) {
return (1.0 / sqrt(6.283185307179586 * sigma * sigma)) * exp(-(x * x) / (2.0 * sigma * sigma));
}

void main(void) {
vec4 sample = texture(sCache, vUv);
vec4 color = vec4(sample.rgb * sample.a, sample.a) * gauss(0.0, vSigma);

for (int i=1 ; i < vBlurRadius ; ++i) {
vec2 offset = vec2(float(i)) * vOffsetScale;

vec2 st0 = clamp(vUv.xy + offset, vUvRect.xy, vUvRect.zw);
vec4 color0 = texture(sCache, vec3(st0, vUv.z));

vec2 st1 = clamp(vUv.xy - offset, vUvRect.xy, vUvRect.zw);
vec4 color1 = texture(sCache, vec3(st1, vUv.z));

// Alpha must be premultiplied in order to properly blur the alpha channel.
float weight = gauss(float(i), vSigma);
color += vec4(color0.rgb * color0.a, color0.a) * weight;
color += vec4(color1.rgb * color1.a, color1.a) * weight;
}

// Unpremultiply the alpha.
color.rgb /= color.a;

oFragColor = color;
}
10 changes: 10 additions & 0 deletions webrender/res/cs_blur.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#line 1
/* 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/. */

varying vec3 vUv;
flat varying vec4 vUvRect;
flat varying vec2 vOffsetScale;
flat varying float vSigma;
flat varying int vBlurRadius;
45 changes: 45 additions & 0 deletions webrender/res/cs_blur.vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#line 1
/* 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/. */

// Applies a separable gaussian blur in one direction, as specified
// by the dir field in the blur command.

#define DIR_HORIZONTAL 0
#define DIR_VERTICAL 1

void main(void) {
BlurCommand cmd = fetch_blur(gl_InstanceID);
RenderTaskData task = fetch_render_task(cmd.task_id);
RenderTaskData src_task = fetch_render_task(cmd.src_task_id);

vec4 local_rect = task.data0;

vec2 pos = mix(local_rect.xy,
local_rect.xy + local_rect.zw,
aPosition.xy);

vec2 texture_size = textureSize(sCache, 0).xy;
vUv.z = src_task.data1.x;
vBlurRadius = int(task.data1.y);
vSigma = task.data1.y * 0.5;

switch (cmd.dir) {
case DIR_HORIZONTAL:
vOffsetScale = vec2(1.0 / texture_size.x, 0.0);
break;
case DIR_VERTICAL:
vOffsetScale = vec2(0.0, 1.0 / texture_size.y);
break;
}

vUvRect = vec4(src_task.data0.xy, src_task.data0.xy + src_task.data0.zw);
vUvRect /= texture_size.xyxy;

vec2 uv0 = src_task.data0.xy / texture_size;
vec2 uv1 = (src_task.data0.xy + src_task.data0.zw) / texture_size;
vUv.xy = mix(uv0, uv1, aPosition.xy);

gl_Position = uTransform * vec4(pos, 0.0, 1.0);
}
9 changes: 9 additions & 0 deletions webrender/res/cs_text_run.fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#line 1
/* 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/. */

void main(void) {
float a = texture(sDiffuse, vUv).a;
oFragColor = vec4(vColor.rgb, vColor.a * a);
}
7 changes: 7 additions & 0 deletions webrender/res/cs_text_run.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#line 1
/* 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/. */

varying vec2 vUv;
flat varying vec4 vColor;
35 changes: 35 additions & 0 deletions webrender/res/cs_text_run.vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#line 1
/* 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/. */

// Draw a text run to a cache target. These are always
// drawn un-transformed. These are used for effects such
// as text-shadow.

void main(void) {
CachePrimitiveInstance cpi = fetch_cache_instance(gl_InstanceID);
RenderTaskData task = fetch_render_task(cpi.render_task_index);
TextRun text = fetch_text_run(cpi.specific_prim_index);
Glyph glyph = fetch_glyph(cpi.sub_index);
PrimitiveGeometry pg = fetch_prim_geometry(cpi.global_prim_index);

// Glyphs size is already in device-pixels.
// The render task origin is in device-pixels. Offset that by
// the glyph offset, relative to its primitive bounding rect.
vec2 size = glyph.uv_rect.zw - glyph.uv_rect.xy;
vec2 origin = task.data0.xy + uDevicePixelRatio * (glyph.offset.xy - pg.local_rect.xy);
vec4 local_rect = vec4(origin, size);

vec2 texture_size = vec2(textureSize(sDiffuse, 0));
vec2 st0 = glyph.uv_rect.xy / texture_size;
vec2 st1 = glyph.uv_rect.zw / texture_size;

vec2 pos = mix(local_rect.xy,
local_rect.xy + local_rect.zw,
aPosition.xy);
vUv = mix(st0, st1, aPosition.xy);
vColor = text.color;

gl_Position = uTransform * vec4(pos, 0.0, 1.0);
}
22 changes: 22 additions & 0 deletions webrender/res/prim_shared.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,31 @@ PrimitiveInstance fetch_instance(int index) {
return pi;
}

struct BlurCommand {
int task_id;
int src_task_id;
int dir;
};

BlurCommand fetch_blur(int index) {
BlurCommand blur;

int offset = index * 1;

ivec4 data0 = int_data[offset + 0];

blur.task_id = data0.x;
blur.src_task_id = data0.y;
blur.dir = data0.z;

return blur;
}

struct CachePrimitiveInstance {
int global_prim_index;
int specific_prim_index;
int render_task_index;
int sub_index;
};

CachePrimitiveInstance fetch_cache_instance(int index) {
Expand All @@ -293,6 +314,7 @@ CachePrimitiveInstance fetch_cache_instance(int index) {
cpi.global_prim_index = data0.x;
cpi.specific_prim_index = data0.y;
cpi.render_task_index = data0.z;
cpi.sub_index = data0.w;

return cpi;
}
Expand Down
7 changes: 7 additions & 0 deletions webrender/res/ps_cache_image.fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* 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/. */

void main(void) {
oFragColor = texture(sCache, vUv);
}
5 changes: 5 additions & 0 deletions webrender/res/ps_cache_image.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* 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/. */

varying vec3 vUv;
27 changes: 27 additions & 0 deletions webrender/res/ps_cache_image.vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#line 1
/* 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/. */

// Draw a cached primitive (e.g. a blurred text run) from the
// target cache to the framebuffer, applying tile clip boundaries.

void main(void) {
Primitive prim = load_primitive(gl_InstanceID);

VertexInfo vi = write_vertex(prim.local_rect,
prim.local_clip_rect,
prim.layer,
prim.tile);

RenderTaskData child_task = fetch_render_task(prim.user_data.x);
vUv.z = child_task.data1.x;

vec2 texture_size = vec2(textureSize(sCache, 0));
vec2 uv0 = child_task.data0.xy / texture_size;
vec2 uv1 = (child_task.data0.xy + child_task.data0.zw) / texture_size;

vec2 f = (vi.local_clamped_pos - prim.local_rect.xy) / prim.local_rect.zw;

vUv.xy = mix(uv0, uv1, f);
}
75 changes: 1 addition & 74 deletions webrender/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use euclid::Matrix4D;
use fnv::FnvHasher;
use gleam::gl;
use internal_types::{PackedVertex, PackedVertexForQuad};
use internal_types::{PackedVertexForTextureCacheUpdate, RenderTargetMode, TextureSampler};
use internal_types::{RenderTargetMode, TextureSampler};
use internal_types::{VertexAttribute, DebugFontVertex, DebugColorVertex};
//use notify::{self, Watcher};
use std::collections::HashMap;
Expand Down Expand Up @@ -65,7 +65,6 @@ pub enum VertexFormat {
Rectangles,
DebugFont,
DebugColor,
RasterOp,
}

pub trait FileWatcherHandler : Send {
Expand Down Expand Up @@ -234,78 +233,6 @@ impl VertexFormat {
vertex_stride as gl::GLint,
0 + vertex_stride * offset);
}
VertexFormat::RasterOp => {
gl::enable_vertex_attrib_array(VertexAttribute::Position as gl::GLuint);
gl::enable_vertex_attrib_array(VertexAttribute::ColorRectTL as gl::GLuint);
gl::enable_vertex_attrib_array(VertexAttribute::ColorTexCoordRectTop as
gl::GLuint);
gl::enable_vertex_attrib_array(VertexAttribute::BorderRadii as gl::GLuint);
gl::enable_vertex_attrib_array(VertexAttribute::BorderPosition as gl::GLuint);
gl::enable_vertex_attrib_array(VertexAttribute::BlurRadius as gl::GLuint);
gl::enable_vertex_attrib_array(VertexAttribute::DestTextureSize as gl::GLuint);
gl::enable_vertex_attrib_array(VertexAttribute::SourceTextureSize as gl::GLuint);
gl::enable_vertex_attrib_array(VertexAttribute::Misc as gl::GLuint);

self.set_divisors(0);

let vertex_stride = mem::size_of::<PackedVertexForTextureCacheUpdate>() as
gl::GLuint;

gl::vertex_attrib_pointer(VertexAttribute::Position as gl::GLuint,
2,
gl::FLOAT,
false,
vertex_stride as gl::GLint,
0);
gl::vertex_attrib_pointer(VertexAttribute::ColorRectTL as gl::GLuint,
4,
gl::UNSIGNED_BYTE,
true,
vertex_stride as gl::GLint,
8);
gl::vertex_attrib_pointer(VertexAttribute::ColorTexCoordRectTop as gl::GLuint,
2,
gl::UNSIGNED_SHORT,
true,
vertex_stride as gl::GLint,
12);
gl::vertex_attrib_pointer(VertexAttribute::BorderRadii as gl::GLuint,
4,
gl::FLOAT,
false,
vertex_stride as gl::GLint,
16);
gl::vertex_attrib_pointer(VertexAttribute::BorderPosition as gl::GLuint,
4,
gl::FLOAT,
false,
vertex_stride as gl::GLint,
32);
gl::vertex_attrib_pointer(VertexAttribute::DestTextureSize as gl::GLuint,
2,
gl::FLOAT,
false,
vertex_stride as gl::GLint,
48);
gl::vertex_attrib_pointer(VertexAttribute::SourceTextureSize as gl::GLuint,
2,
gl::FLOAT,
false,
vertex_stride as gl::GLint,
56);
gl::vertex_attrib_pointer(VertexAttribute::BlurRadius as gl::GLuint,
1,
gl::FLOAT,
false,
vertex_stride as gl::GLint,
64);
gl::vertex_attrib_pointer(VertexAttribute::Misc as gl::GLuint,
4,
gl::UNSIGNED_BYTE,
false,
vertex_stride as gl::GLint,
68);
}
}
}

Expand Down
Loading