From 9991e0674bf933215515c3f5632eed8ac879ecae Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Fri, 11 Nov 2016 05:57:01 +1000 Subject: [PATCH] Update shaders for sampler names, copy new text-shadow shaders. --- resources/shaders/cs_blur.fs.glsl | 40 +++++++++++++++++++++ resources/shaders/cs_blur.glsl | 10 ++++++ resources/shaders/cs_blur.vs.glsl | 45 ++++++++++++++++++++++++ resources/shaders/cs_text_run.fs.glsl | 9 +++++ resources/shaders/cs_text_run.glsl | 7 ++++ resources/shaders/cs_text_run.vs.glsl | 35 ++++++++++++++++++ resources/shaders/debug_font.fs.glsl | 4 +-- resources/shaders/prim_shared.glsl | 22 ++++++++++++ resources/shaders/ps_cache_image.fs.glsl | 7 ++++ resources/shaders/ps_cache_image.glsl | 5 +++ resources/shaders/ps_cache_image.vs.glsl | 27 ++++++++++++++ resources/shaders/ps_image.fs.glsl | 2 +- resources/shaders/ps_image.vs.glsl | 2 +- resources/shaders/ps_image_clip.fs.glsl | 2 +- resources/shaders/ps_image_clip.vs.glsl | 2 +- resources/shaders/ps_text_run.fs.glsl | 4 +-- resources/shaders/ps_text_run.vs.glsl | 2 +- resources/shaders/shared.glsl | 4 ++- 18 files changed, 219 insertions(+), 10 deletions(-) create mode 100644 resources/shaders/cs_blur.fs.glsl create mode 100644 resources/shaders/cs_blur.glsl create mode 100644 resources/shaders/cs_blur.vs.glsl create mode 100644 resources/shaders/cs_text_run.fs.glsl create mode 100644 resources/shaders/cs_text_run.glsl create mode 100644 resources/shaders/cs_text_run.vs.glsl create mode 100644 resources/shaders/ps_cache_image.fs.glsl create mode 100644 resources/shaders/ps_cache_image.glsl create mode 100644 resources/shaders/ps_cache_image.vs.glsl diff --git a/resources/shaders/cs_blur.fs.glsl b/resources/shaders/cs_blur.fs.glsl new file mode 100644 index 00000000000..4217b780f2e --- /dev/null +++ b/resources/shaders/cs_blur.fs.glsl @@ -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; +} diff --git a/resources/shaders/cs_blur.glsl b/resources/shaders/cs_blur.glsl new file mode 100644 index 00000000000..110b748b7dc --- /dev/null +++ b/resources/shaders/cs_blur.glsl @@ -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; diff --git a/resources/shaders/cs_blur.vs.glsl b/resources/shaders/cs_blur.vs.glsl new file mode 100644 index 00000000000..1e89b60e5e5 --- /dev/null +++ b/resources/shaders/cs_blur.vs.glsl @@ -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); +} diff --git a/resources/shaders/cs_text_run.fs.glsl b/resources/shaders/cs_text_run.fs.glsl new file mode 100644 index 00000000000..8e9b40db247 --- /dev/null +++ b/resources/shaders/cs_text_run.fs.glsl @@ -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(sColor0, vUv).a; + oFragColor = vec4(vColor.rgb, vColor.a * a); +} diff --git a/resources/shaders/cs_text_run.glsl b/resources/shaders/cs_text_run.glsl new file mode 100644 index 00000000000..39ed226b264 --- /dev/null +++ b/resources/shaders/cs_text_run.glsl @@ -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; diff --git a/resources/shaders/cs_text_run.vs.glsl b/resources/shaders/cs_text_run.vs.glsl new file mode 100644 index 00000000000..3ac5314bce3 --- /dev/null +++ b/resources/shaders/cs_text_run.vs.glsl @@ -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(sColor0, 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); +} diff --git a/resources/shaders/debug_font.fs.glsl b/resources/shaders/debug_font.fs.glsl index a10e0da4ceb..5550fa93447 100644 --- a/resources/shaders/debug_font.fs.glsl +++ b/resources/shaders/debug_font.fs.glsl @@ -5,9 +5,9 @@ void main(void) { #ifdef SERVO_ES2 - float alpha = texture(sDiffuse, vColorTexCoord.xy).a; + float alpha = texture(sColor0, vColorTexCoord.xy).a; #else - float alpha = texture(sDiffuse, vColorTexCoord.xy).r; + float alpha = texture(sColor0, vColorTexCoord.xy).r; #endif oFragColor = vec4(vColor.xyz, vColor.w * alpha); } diff --git a/resources/shaders/prim_shared.glsl b/resources/shaders/prim_shared.glsl index e4803c42a6f..b98d9d2b72f 100644 --- a/resources/shaders/prim_shared.glsl +++ b/resources/shaders/prim_shared.glsl @@ -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) { @@ -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; } diff --git a/resources/shaders/ps_cache_image.fs.glsl b/resources/shaders/ps_cache_image.fs.glsl new file mode 100644 index 00000000000..36ab69e3136 --- /dev/null +++ b/resources/shaders/ps_cache_image.fs.glsl @@ -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); +} diff --git a/resources/shaders/ps_cache_image.glsl b/resources/shaders/ps_cache_image.glsl new file mode 100644 index 00000000000..a5930ae066d --- /dev/null +++ b/resources/shaders/ps_cache_image.glsl @@ -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; diff --git a/resources/shaders/ps_cache_image.vs.glsl b/resources/shaders/ps_cache_image.vs.glsl new file mode 100644 index 00000000000..57e380bbbed --- /dev/null +++ b/resources/shaders/ps_cache_image.vs.glsl @@ -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); +} diff --git a/resources/shaders/ps_image.fs.glsl b/resources/shaders/ps_image.fs.glsl index b685e4772e1..f425fb33988 100644 --- a/resources/shaders/ps_image.fs.glsl +++ b/resources/shaders/ps_image.fs.glsl @@ -25,5 +25,5 @@ void main(void) { vec2 st = vTextureOffset + ((position_in_tile / vStretchSize) * vTextureSize); alpha = alpha * float(all(bvec2(step(position_in_tile, vStretchSize)))); - oFragColor = vec4(1, 1, 1, alpha) * texture(sDiffuse, st); + oFragColor = vec4(1, 1, 1, alpha) * texture(sColor0, st); } diff --git a/resources/shaders/ps_image.vs.glsl b/resources/shaders/ps_image.vs.glsl index b4e7a3e80af..a3eb7808e56 100644 --- a/resources/shaders/ps_image.vs.glsl +++ b/resources/shaders/ps_image.vs.glsl @@ -23,7 +23,7 @@ void main(void) { #endif // vUv will contain how many times this image has wrapped around the image size. - vec2 texture_size = vec2(textureSize(sDiffuse, 0)); + vec2 texture_size = vec2(textureSize(sColor0, 0)); vec2 st0 = image.st_rect.xy / texture_size; vec2 st1 = image.st_rect.zw / texture_size; diff --git a/resources/shaders/ps_image_clip.fs.glsl b/resources/shaders/ps_image_clip.fs.glsl index 45907a97ab2..5d48c94b838 100644 --- a/resources/shaders/ps_image_clip.fs.glsl +++ b/resources/shaders/ps_image_clip.fs.glsl @@ -28,5 +28,5 @@ void main(void) { vec2 st = vTextureOffset + ((position_in_tile / vStretchSize) * vTextureSize); alpha = alpha * float(all(bvec2(step(position_in_tile, vStretchSize)))); - oFragColor = texture(sDiffuse, st) * vec4(1, 1, 1, alpha); + oFragColor = texture(sColor0, st) * vec4(1, 1, 1, alpha); } diff --git a/resources/shaders/ps_image_clip.vs.glsl b/resources/shaders/ps_image_clip.vs.glsl index 5e9161008e5..bfbf54bc857 100644 --- a/resources/shaders/ps_image_clip.vs.glsl +++ b/resources/shaders/ps_image_clip.vs.glsl @@ -27,7 +27,7 @@ void main(void) { write_clip(clip); // vUv will contain how many times this image has wrapped around the image size. - vec2 texture_size = vec2(textureSize(sDiffuse, 0)); + vec2 texture_size = vec2(textureSize(sColor0, 0)); vec2 st0 = image.st_rect.xy / texture_size; vec2 st1 = image.st_rect.zw / texture_size; diff --git a/resources/shaders/ps_text_run.fs.glsl b/resources/shaders/ps_text_run.fs.glsl index f0948b207e3..a3dcacced46 100644 --- a/resources/shaders/ps_text_run.fs.glsl +++ b/resources/shaders/ps_text_run.fs.glsl @@ -4,9 +4,9 @@ void main(void) { #ifdef WR_FEATURE_SUBPIXEL_AA - oFragColor = texture(sDiffuse, vUv); + oFragColor = texture(sColor0, vUv); #else - float a = texture(sDiffuse, vUv).a; + float a = texture(sColor0, vUv).a; #ifdef WR_FEATURE_TRANSFORM float alpha = 0.0; init_transform_fs(vLocalPos, vLocalRect, alpha); diff --git a/resources/shaders/ps_text_run.vs.glsl b/resources/shaders/ps_text_run.vs.glsl index 3a6e009447e..9adfbffc76b 100644 --- a/resources/shaders/ps_text_run.vs.glsl +++ b/resources/shaders/ps_text_run.vs.glsl @@ -25,7 +25,7 @@ void main(void) { vec2 f = (vi.local_clamped_pos - vi.local_rect.p0) / (vi.local_rect.p1 - vi.local_rect.p0); #endif - vec2 texture_size = vec2(textureSize(sDiffuse, 0)); + vec2 texture_size = vec2(textureSize(sColor0, 0)); vec2 st0 = glyph.uv_rect.xy / texture_size; vec2 st1 = glyph.uv_rect.zw / texture_size; diff --git a/resources/shaders/shared.glsl b/resources/shaders/shared.glsl index ffa16a1d770..f250a709c6d 100644 --- a/resources/shaders/shared.glsl +++ b/resources/shaders/shared.glsl @@ -33,7 +33,9 @@ //====================================================================================== // Shared shader uniforms //====================================================================================== -uniform sampler2D sDiffuse; +uniform sampler2D sColor0; +uniform sampler2D sColor1; +uniform sampler2D sColor2; uniform sampler2D sMask; //======================================================================================