From 0a4d57580a0e96bae9b7cd88051e7921c9efa399 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 16 Aug 2017 19:51:13 -0700 Subject: [PATCH] Add the ECAA cover shader, untested as of yet --- demo/client/src/index.ts | 5 +++ shaders/gles2/common.inc.glsl | 17 +++++++-- shaders/gles2/ecaa-cover.fs.glsl | 13 +++++++ shaders/gles2/ecaa-cover.vs.glsl | 59 ++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 shaders/gles2/ecaa-cover.fs.glsl create mode 100644 shaders/gles2/ecaa-cover.vs.glsl diff --git a/demo/client/src/index.ts b/demo/client/src/index.ts index ed31adde..2ad2351f 100644 --- a/demo/client/src/index.ts +++ b/demo/client/src/index.ts @@ -47,6 +47,10 @@ const SHADER_URLS: ShaderMap = { vertex: "/glsl/gles2/ecaa-resolve.vs.glsl", fragment: "/glsl/gles2/ecaa-resolve.fs.glsl", }, + ecaaCover: { + vertex: "/glsl/gles2/ecaa-cover.vs.glsl", + fragment: "/glsl/gles2/ecaa-cover.fs.glsl", + }, }; interface UnlinkedShaderProgram { @@ -77,6 +81,7 @@ interface ShaderMap { directInterior: T; ecaaEdgeDetect: T; ecaaResolve: T; + ecaaCover: T; } interface UniformMap { diff --git a/shaders/gles2/common.inc.glsl b/shaders/gles2/common.inc.glsl index 2094637e..8eb7fb56 100644 --- a/shaders/gles2/common.inc.glsl +++ b/shaders/gles2/common.inc.glsl @@ -30,6 +30,11 @@ float convertPathIndexToDepthValue(int pathIndex) { return mix(-1.0, 1.0, float(pathIndex) / float(MAX_PATHS)); } +int unpackUInt16(vec2 packedValue) { + ivec2 valueBytes = ivec2(floor(packedValue * 255.0)); + return valueBytes.y * 256 + valueBytes.x; +} + vec4 fetchFloat4Data(sampler2D dataTexture, int index, ivec2 dimensions) { ivec2 pixelCoord = ivec2(imod(index, dimensions.x), index / dimensions.x); return texture2D(dataTexture, (vec2(pixelCoord) + 0.5) / vec2(dimensions)); @@ -39,11 +44,19 @@ vec4 fetchFloat4NormIndexedData(sampler2D dataTexture, float normIndex, ivec2 di return fetchFloat4Data(dataTexture, int(normIndex * float(dimensions.x)), dimensions); } +vec2 fetchFloat2Data(sampler2D dataTexture, int index, ivec2 dimensions) { + vec4 float4Data = fetchFloat4Data(dataTexture, index / 2, dimensions); + return index / 2 * 2 == index ? float4Data.xy : float4Data.zw; +} + +int fetchUInt16Data(sampler2D dataTexture, int index, ivec2 dimensions) { + return unpackUInt16(fetchFloat2Data(dataTexture, index, dimensions)); +} + vec2 packPathID(int pathID) { return vec2(imod(pathID, 256), pathID / 256) / 255.0; } int unpackPathID(vec2 packedPathID) { - ivec2 pathIDBytes = ivec2(floor(packedPathID * 255.0)); - return pathIDBytes.y * 256 + pathIDBytes.x; + return unpackUInt16(packedPathID); } diff --git a/shaders/gles2/ecaa-cover.fs.glsl b/shaders/gles2/ecaa-cover.fs.glsl new file mode 100644 index 00000000..edcbfaef --- /dev/null +++ b/shaders/gles2/ecaa-cover.fs.glsl @@ -0,0 +1,13 @@ +// pathfinder/shaders/gles2/ecaa-cover.fs.glsl +// +// Copyright (c) 2017 Mozilla Foundation + +precision highp float; + +varying vec2 vHorizontalExtents; + +void main() { + vec2 sides = gl_FragCoord.xx + vec2(-0.5, 0.5); + vec2 clampedSides = clamp(vHorizontalExtents, sides.x, sides.y); + gl_FragColor = vec4(vec3(clampedSides.y - clampedSides.x), 1.0); +} diff --git a/shaders/gles2/ecaa-cover.vs.glsl b/shaders/gles2/ecaa-cover.vs.glsl new file mode 100644 index 00000000..a18d9f06 --- /dev/null +++ b/shaders/gles2/ecaa-cover.vs.glsl @@ -0,0 +1,59 @@ +// pathfinder/shaders/gles2/ecaa-cover.vs.glsl +// +// Copyright (c) 2017 Mozilla Foundation + +precision highp float; + +uniform mat4 uTransform; +uniform ivec2 uFramebufferSize; +uniform ivec2 uBVertexPositionDimensions; +uniform ivec2 uBVertexInfoDimensions; +uniform sampler2D uBVertexPosition; +uniform sampler2D uBVertexInfo; + +attribute vec2 aQuadPosition; +attribute vec3 aUpperPointIndices; +attribute vec3 aLowerPointIndices; + +varying vec2 vHorizontalExtents; + +void main() { + // Fetch B-vertex positions. + // FIXME(pcwalton): This could be slightly optimized to fetch fewer positions. + ivec4 pointIndices = ivec4(ivec2(aUpperPointIndices.xy), ivec2(aLowerPointIndices.xy)); + vec2 upperLeftPosition = fetchFloat2Data(uBVertexPosition, + pointIndices.x, + uBVertexPositionDimensions); + vec2 upperRightPosition = fetchFloat2Data(uBVertexPosition, + pointIndices.y, + uBVertexPositionDimensions); + vec2 lowerLeftPosition = fetchFloat2Data(uBVertexPosition, + pointIndices.z, + uBVertexPositionDimensions); + vec2 lowerRightPosition = fetchFloat2Data(uBVertexPosition, + pointIndices.w, + uBVertexPositionDimensions); + + upperLeftPosition = transformVertexPosition(upperLeftPosition, uTransform); + upperRightPosition = transformVertexPosition(upperRightPosition, uTransform); + lowerLeftPosition = transformVertexPosition(lowerLeftPosition, uTransform); + lowerRightPosition = transformVertexPosition(lowerRightPosition, uTransform); + + vec4 extents = vec4(min(upperLeftPosition.x, lowerLeftPosition.x), + min(min(upperLeftPosition.y, upperRightPosition.y), + min(lowerLeftPosition.y, lowerRightPosition.y)), + max(upperRightPosition.x, lowerRightPosition.x), + max(max(upperLeftPosition.y, upperRightPosition.y), + max(lowerLeftPosition.y, lowerRightPosition.y))); + + vec4 roundedExtents = vec4(floor(extents.xy), ceil(extents.zw)); + + int pathID = fetchUInt16Data(uBVertexInfo, pointIndices.x, uBVertexInfoDimensions); + + vec2 position = mix(roundedExtents.xy, roundedExtents.zw, aQuadPosition); + position = convertScreenToClipSpace(position, uFramebufferSize); + float depth = convertPathIndexToDepthValue(pathID); + gl_Position = vec4(position, depth, 1.0); + + vHorizontalExtents = roundedExtents.xz; +}