From 6a7c013e617cc0287f17630a6c30aeeccb0f9192 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 5 Oct 2017 19:14:52 -0700 Subject: [PATCH] Make the hints a uniform instead of a buffer texture. Saves a good bit of memory traffic during vertex shading. --- demo/client/src/3d-demo.ts | 5 +++++ demo/client/src/benchmark.ts | 11 +++-------- demo/client/src/ecaa-strategy.ts | 3 +-- demo/client/src/svg-demo.ts | 5 +++++ demo/client/src/text-demo.ts | 22 +++++++++------------- demo/client/src/view.ts | 9 ++++----- shaders/gles2/direct-curve.vs.glsl | 6 ++---- shaders/gles2/direct-interior.vs.glsl | 6 ++---- shaders/gles2/ecaa-cover.vs.glsl | 8 +++----- shaders/gles2/ecaa-curve.vs.glsl | 8 +++----- shaders/gles2/ecaa-line.vs.glsl | 6 ++---- 11 files changed, 39 insertions(+), 50 deletions(-) diff --git a/demo/client/src/3d-demo.ts b/demo/client/src/3d-demo.ts index 65d062c7..8bae9ddc 100644 --- a/demo/client/src/3d-demo.ts +++ b/demo/client/src/3d-demo.ts @@ -25,6 +25,7 @@ import {BUILTIN_FONT_URI, ExpandedMeshData} from "./text"; import {GlyphStore, Hint, PathfinderFont, TextFrame, TextRun} from "./text"; import {assert, panic, PathfinderError, unwrapNull} from "./utils"; import {DemoView, Timings} from "./view"; +import { UniformMap } from './gl-utils'; const TEXT_AVAILABLE_WIDTH: number = 150000; const TEXT_PADDING: number = 2000; @@ -304,6 +305,10 @@ class ThreeDView extends DemoView { this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, CUBE_INDICES, this.gl.STATIC_DRAW); } + setHintsUniform(uniforms: UniformMap): void { + this.gl.uniform4f(uniforms.uHints, 0, 0, 0, 0); + } + protected pathColorsForObject(objectIndex: number): Uint8Array { return TEXT_COLOR; } diff --git a/demo/client/src/benchmark.ts b/demo/client/src/benchmark.ts index 07fcbec4..66605106 100644 --- a/demo/client/src/benchmark.ts +++ b/demo/client/src/benchmark.ts @@ -25,6 +25,7 @@ import {BUILTIN_FONT_URI, ExpandedMeshData, GlyphStore, PathfinderFont, TextFram import {TextRun} from "./text"; import {assert, PathfinderError, unwrapNull, unwrapUndef} from "./utils"; import {DemoView, MonochromeDemoView, Timings } from "./view"; +import { UniformMap } from './gl-utils'; const STRING: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; @@ -125,7 +126,6 @@ class BenchmarkAppController extends DemoAppController { this.view.then(view => { view.uploadPathColors(1); view.uploadPathTransforms(1); - view.uploadHints(); view.attachMeshes([expandedMeshes.meshes]); }); }); @@ -246,13 +246,8 @@ class BenchmarkTestView extends MonochromeDemoView { this.camera.onZoom = () => this.setDirty(); } - uploadHints(): void { - const glyphCount = unwrapNull(this.appController.textRun).glyphIDs.length; - const pathHints = new Float32Array((glyphCount + 1) * 4); - - const pathHintsBufferTexture = new PathfinderBufferTexture(this.gl, 'uPathHints'); - pathHintsBufferTexture.upload(this.gl, pathHints); - this.pathHintsBufferTexture = pathHintsBufferTexture; + setHintsUniform(uniforms: UniformMap): void { + this.gl.uniform4f(uniforms.uHints, 0, 0, 0, 0); } protected createAAStrategy(aaType: AntialiasingStrategyName, diff --git a/demo/client/src/ecaa-strategy.ts b/demo/client/src/ecaa-strategy.ts index 7fedc6ab..b834b4a9 100644 --- a/demo/client/src/ecaa-strategy.ts +++ b/demo/client/src/ecaa-strategy.ts @@ -379,8 +379,7 @@ export abstract class ECAAStrategy extends AntialiasingStrategy { this.bVertexPositionBufferTexture.bind(view.gl, uniforms, 0); this.bVertexPathIDBufferTexture.bind(view.gl, uniforms, 1); view.pathTransformBufferTextures[0].bind(view.gl, uniforms, 2); - if (view.pathHintsBufferTexture !== null) - view.pathHintsBufferTexture.bind(view.gl, uniforms, 3); + view.setHintsUniform(uniforms); } private antialiasLines(view: MonochromeDemoView) { diff --git a/demo/client/src/svg-demo.ts b/demo/client/src/svg-demo.ts index cdb79eb1..b2679db0 100644 --- a/demo/client/src/svg-demo.ts +++ b/demo/client/src/svg-demo.ts @@ -23,6 +23,7 @@ import SSAAStrategy from "./ssaa-strategy"; import {BUILTIN_SVG_URI, SVGLoader} from './svg-loader'; import {panic, unwrapNull} from './utils'; import {DemoView, Timings} from './view'; +import { UniformMap } from './gl-utils'; const parseColor = require('parse-color'); @@ -120,6 +121,10 @@ class SVGDemoView extends DemoView { return this.destAllocatedSize; } + setHintsUniform(uniforms: UniformMap): void { + this.gl.uniform4f(uniforms.uHints, 0, 0, 0, 0); + } + protected pathColorsForObject(objectIndex: number): Uint8Array { const instances = this.appController.loader.pathInstances; const pathColors = new Uint8Array(4 * (instances.length + 1)); diff --git a/demo/client/src/text-demo.ts b/demo/client/src/text-demo.ts index 422bff69..aa0ddb2b 100644 --- a/demo/client/src/text-demo.ts +++ b/demo/client/src/text-demo.ts @@ -342,6 +342,15 @@ class TextDemoView extends MonochromeDemoView { this.setDirty(); } + setHintsUniform(uniforms: UniformMap): void { + const hint = this.createHint(); + this.gl.uniform4f(uniforms.uHints, + hint.xHeight, + hint.hintedXHeight, + hint.stemHeight, + hint.hintedStemHeight); + } + protected initContext() { super.initContext(); } @@ -568,19 +577,6 @@ class TextDemoView extends MonochromeDemoView { this.uploadPathTransforms(1); // TODO(pcwalton): Regenerate the IBOs to include only the glyphs we care about. - const pathCount = this.appController.pathCount; - const pathHints = new Float32Array((pathCount + 1) * 4); - - for (let pathID = 0; pathID < pathCount; pathID++) { - pathHints[pathID * 4 + 0] = hint.xHeight; - pathHints[pathID * 4 + 1] = hint.hintedXHeight; - pathHints[pathID * 4 + 2] = hint.stemHeight; - pathHints[pathID * 4 + 3] = hint.hintedStemHeight; - } - - const pathHintsBufferTexture = new PathfinderBufferTexture(this.gl, 'uPathHints'); - pathHintsBufferTexture.upload(this.gl, pathHints); - this.pathHintsBufferTexture = pathHintsBufferTexture; this.setGlyphTexCoords(); } diff --git a/demo/client/src/view.ts b/demo/client/src/view.ts index c24bf73d..7631942b 100644 --- a/demo/client/src/view.ts +++ b/demo/client/src/view.ts @@ -139,7 +139,6 @@ export abstract class DemoView extends PathfinderView { meshData: PathfinderMeshData[]; pathTransformBufferTextures: PathfinderBufferTexture[]; - pathHintsBufferTexture: PathfinderBufferTexture | null; protected timerQueryExt: any; @@ -280,6 +279,8 @@ export abstract class DemoView extends PathfinderView { } } + abstract setHintsUniform(uniforms: UniformMap): void; + protected resized(): void { super.resized(); @@ -500,14 +501,13 @@ export abstract class DemoView extends PathfinderView { // Draw direct interior parts. this.setTransformUniform(directInteriorProgram.uniforms, objectIndex); this.setFramebufferSizeUniform(directInteriorProgram.uniforms); + this.setHintsUniform(directInteriorProgram.uniforms); this.pathColorsBufferTextures[objectIndex].bind(this.gl, directInteriorProgram.uniforms, 0); this.pathTransformBufferTextures[objectIndex].bind(this.gl, directInteriorProgram.uniforms, 1); - if (this.pathHintsBufferTexture != null) - this.pathHintsBufferTexture.bind(this.gl, directInteriorProgram.uniforms, 2); let indexCount = this.gl.getBufferParameter(this.gl.ELEMENT_ARRAY_BUFFER, this.gl.BUFFER_SIZE) / UINT32_SIZE; if (instanceCount == null) { @@ -538,14 +538,13 @@ export abstract class DemoView extends PathfinderView { // Draw direct curve parts. this.setTransformUniform(directCurveProgram.uniforms, objectIndex); this.setFramebufferSizeUniform(directCurveProgram.uniforms); + this.setHintsUniform(directInteriorProgram.uniforms); this.pathColorsBufferTextures[objectIndex].bind(this.gl, directCurveProgram.uniforms, 0); this.pathTransformBufferTextures[objectIndex].bind(this.gl, directCurveProgram.uniforms, 1); - if (this.pathHintsBufferTexture != null) - this.pathHintsBufferTexture.bind(this.gl, directCurveProgram.uniforms, 2); indexCount = this.gl.getBufferParameter(this.gl.ELEMENT_ARRAY_BUFFER, this.gl.BUFFER_SIZE) / UINT32_SIZE; if (instanceCount == null) { diff --git a/shaders/gles2/direct-curve.vs.glsl b/shaders/gles2/direct-curve.vs.glsl index d44aeef3..95f6709e 100644 --- a/shaders/gles2/direct-curve.vs.glsl +++ b/shaders/gles2/direct-curve.vs.glsl @@ -11,12 +11,11 @@ precision highp float; uniform mat4 uTransform; +uniform vec4 uHints; uniform ivec2 uPathColorsDimensions; uniform ivec2 uPathTransformDimensions; -uniform ivec2 uPathHintsDimensions; uniform sampler2D uPathColors; uniform sampler2D uPathTransform; -uniform sampler2D uPathHints; attribute vec2 aPosition; attribute vec2 aTexCoord; @@ -31,10 +30,9 @@ varying float vSign; void main() { int pathID = int(aPathID); - vec4 pathHints = fetchFloat4Data(uPathHints, pathID, uPathHintsDimensions); vec4 pathTransform = fetchFloat4Data(uPathTransform, pathID, uPathTransformDimensions); - vec2 position = hintPosition(aPosition, pathHints); + vec2 position = hintPosition(aPosition, uHints); position = transformVertexPositionST(position, pathTransform); position = transformVertexPosition(position, uTransform); diff --git a/shaders/gles2/direct-interior.vs.glsl b/shaders/gles2/direct-interior.vs.glsl index ee4a5f5b..1eecc5b2 100644 --- a/shaders/gles2/direct-interior.vs.glsl +++ b/shaders/gles2/direct-interior.vs.glsl @@ -11,12 +11,11 @@ precision highp float; uniform mat4 uTransform; +uniform vec4 uHints; uniform ivec2 uPathColorsDimensions; uniform ivec2 uPathTransformDimensions; -uniform ivec2 uPathHintsDimensions; uniform sampler2D uPathColors; uniform sampler2D uPathTransform; -uniform sampler2D uPathHints; attribute vec2 aPosition; attribute float aPathID; @@ -27,10 +26,9 @@ varying vec2 vPathID; void main() { int pathID = int(aPathID); - vec4 pathHints = fetchFloat4Data(uPathHints, pathID, uPathHintsDimensions); vec4 pathTransform = fetchFloat4Data(uPathTransform, pathID, uPathTransformDimensions); - vec2 position = hintPosition(aPosition, pathHints); + vec2 position = hintPosition(aPosition, uHints); position = transformVertexPositionST(position, pathTransform); position = transformVertexPosition(position, uTransform); diff --git a/shaders/gles2/ecaa-cover.vs.glsl b/shaders/gles2/ecaa-cover.vs.glsl index 0e7fdd31..9f81f330 100644 --- a/shaders/gles2/ecaa-cover.vs.glsl +++ b/shaders/gles2/ecaa-cover.vs.glsl @@ -11,15 +11,14 @@ precision highp float; uniform vec4 uTransformST; +uniform vec4 uHints; uniform ivec2 uFramebufferSize; uniform ivec2 uBVertexPositionDimensions; uniform ivec2 uBVertexPathIDDimensions; uniform ivec2 uPathTransformDimensions; -uniform ivec2 uPathHintsDimensions; uniform sampler2D uBVertexPosition; uniform sampler2D uBVertexPathID; uniform sampler2D uPathTransform; -uniform sampler2D uPathHints; attribute vec2 aQuadPosition; attribute vec4 aUpperPointIndices; @@ -51,11 +50,10 @@ void main() { int pathID = fetchUInt16Data(uBVertexPathID, pointIndices.x, uBVertexPathIDDimensions); - vec4 hints = fetchFloat4Data(uPathHints, pathID, uPathHintsDimensions); vec4 transform = fetchFloat4Data(uPathTransform, pathID, uPathTransformDimensions); - upperLeftPosition = hintPosition(upperLeftPosition, hints); - lowerRightPosition = hintPosition(lowerRightPosition, hints); + upperLeftPosition = hintPosition(upperLeftPosition, uHints); + lowerRightPosition = hintPosition(lowerRightPosition, uHints); upperLeftPosition = transformVertexPositionST(upperLeftPosition, transform); lowerRightPosition = transformVertexPositionST(lowerRightPosition, transform); diff --git a/shaders/gles2/ecaa-curve.vs.glsl b/shaders/gles2/ecaa-curve.vs.glsl index 07e2e868..44bcce32 100644 --- a/shaders/gles2/ecaa-curve.vs.glsl +++ b/shaders/gles2/ecaa-curve.vs.glsl @@ -11,15 +11,14 @@ precision highp float; uniform vec4 uTransformST; +uniform vec4 uHints; uniform ivec2 uFramebufferSize; uniform ivec2 uBVertexPositionDimensions; uniform ivec2 uBVertexPathIDDimensions; uniform ivec2 uPathTransformDimensions; -uniform ivec2 uPathHintsDimensions; uniform sampler2D uBVertexPosition; uniform sampler2D uBVertexPathID; uniform sampler2D uPathTransform; -uniform sampler2D uPathHints; uniform bool uLowerPart; attribute vec2 aQuadPosition; @@ -46,7 +45,6 @@ void main() { int pathID = fetchUInt16Data(uBVertexPathID, pointIndices.x, uBVertexPathIDDimensions); - vec4 hints = fetchFloat4Data(uPathHints, pathID, uPathHintsDimensions); vec4 transform = fetchFloat4Data(uPathTransform, pathID, uPathTransformDimensions); // Transform the points, and compute the position of this vertex. @@ -58,8 +56,8 @@ void main() { uFramebufferSize, transform, uTransformST, - hints)) { - controlPointPosition = hintPosition(controlPointPosition, hints); + uHints)) { + controlPointPosition = hintPosition(controlPointPosition, uHints); controlPointPosition = transformVertexPositionST(controlPointPosition, transform); controlPointPosition = transformVertexPositionST(controlPointPosition, uTransformST); controlPointPosition = convertClipToScreenSpace(controlPointPosition, uFramebufferSize); diff --git a/shaders/gles2/ecaa-line.vs.glsl b/shaders/gles2/ecaa-line.vs.glsl index 8721a1c9..687d2253 100644 --- a/shaders/gles2/ecaa-line.vs.glsl +++ b/shaders/gles2/ecaa-line.vs.glsl @@ -11,15 +11,14 @@ precision highp float; uniform vec4 uTransformST; +uniform vec4 uHints; uniform ivec2 uFramebufferSize; uniform ivec2 uBVertexPositionDimensions; uniform ivec2 uBVertexPathIDDimensions; uniform ivec2 uPathTransformDimensions; -uniform ivec2 uPathHintsDimensions; uniform sampler2D uBVertexPosition; uniform sampler2D uBVertexPathID; uniform sampler2D uPathTransform; -uniform sampler2D uPathHints; uniform bool uLowerPart; attribute vec2 aQuadPosition; @@ -40,7 +39,6 @@ void main() { int pathID = fetchUInt16Data(uBVertexPathID, pointIndices.x, uBVertexPathIDDimensions); - vec4 hints = fetchFloat4Data(uPathHints, pathID, uPathHintsDimensions); vec4 transform = fetchFloat4Data(uPathTransform, pathID, uPathTransformDimensions); // Transform the points, and compute the position of this vertex. @@ -52,7 +50,7 @@ void main() { uFramebufferSize, transform, uTransformST, - hints); + uHints); float depth = convertPathIndexToViewportDepthValue(pathID);