From 1846ebd861eb8697cc2814adb028bb2c636d097d Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 6 Oct 2017 19:18:34 -0700 Subject: [PATCH] Use sRGB color textures where available --- demo/client/src/ecaa-strategy.ts | 9 ++++----- demo/client/src/gl-utils.ts | 20 ++++++++++---------- demo/client/src/ssaa-strategy.ts | 4 ++-- demo/client/src/text-demo.ts | 32 ++++++++++++++++---------------- demo/client/src/view.ts | 6 ++++++ 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/demo/client/src/ecaa-strategy.ts b/demo/client/src/ecaa-strategy.ts index 71ae43a3..f8a4658a 100644 --- a/demo/client/src/ecaa-strategy.ts +++ b/demo/client/src/ecaa-strategy.ts @@ -138,9 +138,8 @@ export abstract class ECAAStrategy extends AntialiasingStrategy { } protected initDirectFramebuffer(view: MonochromeDemoView) { - this.directColorTexture = createFramebufferColorTexture(view.gl, this.destFramebufferSize); - this.directPathIDTexture = createFramebufferColorTexture(view.gl, - this.destFramebufferSize); + this.directColorTexture = createFramebufferColorTexture(view, this.destFramebufferSize); + this.directPathIDTexture = createFramebufferColorTexture(view, this.destFramebufferSize); this.directFramebuffer = createFramebuffer(view.gl, view.drawBuffersExt, @@ -574,9 +573,9 @@ export class ECAAMulticolorStrategy extends ECAAStrategy { } protected initEdgeDetectFramebuffer(view: MonochromeDemoView) { - this.bgColorTexture = createFramebufferColorTexture(view.gl, + this.bgColorTexture = createFramebufferColorTexture(view, this.supersampledFramebufferSize); - this.fgColorTexture = createFramebufferColorTexture(view.gl, + this.fgColorTexture = createFramebufferColorTexture(view, this.supersampledFramebufferSize); this.edgeDetectFramebuffer = createFramebuffer(view.gl, view.drawBuffersExt, diff --git a/demo/client/src/gl-utils.ts b/demo/client/src/gl-utils.ts index 25c47c0d..42d1baa6 100644 --- a/demo/client/src/gl-utils.ts +++ b/demo/client/src/gl-utils.ts @@ -11,6 +11,7 @@ import * as glmatrix from 'gl-matrix'; import {assert, UINT32_SIZE, unwrapNull} from './utils'; +import { DemoView } from './view'; export type WebGLVertexArrayObject = any; @@ -24,25 +25,24 @@ export interface UniformMap { export const QUAD_ELEMENTS: Uint8Array = new Uint8Array([2, 0, 1, 1, 3, 2]); -export function createFramebufferColorTexture(gl: WebGLRenderingContext, size: glmatrix.vec2): - WebGLTexture { +export function createFramebufferColorTexture(view: DemoView, size: glmatrix.vec2): WebGLTexture { // Firefox seems to have a bug whereby textures don't get marked as initialized when cleared // if they're anything other than the first attachment of an FBO. To work around this, supply // zero data explicitly when initializing the texture. const zeroes = new Uint8Array(size[0] * size[1] * UINT32_SIZE); - const texture = unwrapNull(gl.createTexture()); - gl.activeTexture(gl.TEXTURE0); - gl.bindTexture(gl.TEXTURE_2D, texture); - gl.texImage2D(gl.TEXTURE_2D, + const texture = unwrapNull(view.gl.createTexture()); + view.gl.activeTexture(view.gl.TEXTURE0); + view.gl.bindTexture(view.gl.TEXTURE_2D, texture); + view.gl.texImage2D(view.gl.TEXTURE_2D, 0, - gl.RGBA, + view.colorAlphaFormat, size[0], size[1], 0, - gl.RGBA, - gl.UNSIGNED_BYTE, + view.colorAlphaFormat, + view.gl.UNSIGNED_BYTE, zeroes); - setTextureParameters(gl, gl.NEAREST); + setTextureParameters(view.gl, view.gl.NEAREST); return texture; } diff --git a/demo/client/src/ssaa-strategy.ts b/demo/client/src/ssaa-strategy.ts index 35e07955..cf9a151d 100644 --- a/demo/client/src/ssaa-strategy.ts +++ b/demo/client/src/ssaa-strategy.ts @@ -48,11 +48,11 @@ export default class SSAAStrategy extends AntialiasingStrategy { view.gl.bindTexture(view.gl.TEXTURE_2D, this.supersampledColorTexture); view.gl.texImage2D(view.gl.TEXTURE_2D, 0, - view.gl.RGBA, + view.colorAlphaFormat, this.supersampledFramebufferSize[0], this.supersampledFramebufferSize[1], 0, - view.gl.RGBA, + view.colorAlphaFormat, view.gl.UNSIGNED_BYTE, null); setTextureParameters(view.gl, view.gl.LINEAR); diff --git a/demo/client/src/text-demo.ts b/demo/client/src/text-demo.ts index aa0ddb2b..e545b187 100644 --- a/demo/client/src/text-demo.ts +++ b/demo/client/src/text-demo.ts @@ -30,7 +30,7 @@ import {calculatePixelDescent, calculatePixelRectForGlyph, PathfinderFont} from import {BUILTIN_FONT_URI, calculatePixelXMin, GlyphStore, Hint, SimpleTextLayout} from "./text"; import {assert, expectNotNull, panic, PathfinderError, scaleRect, UINT32_SIZE} from './utils'; import {unwrapNull} from './utils'; -import {MonochromeDemoView, Timings, TIMINGS} from './view'; +import {DemoView, MonochromeDemoView, Timings, TIMINGS} from './view'; const DEFAULT_TEXT: string = `’Twas brillig, and the slithy toves @@ -441,7 +441,7 @@ class TextDemoView extends MonochromeDemoView { // Blit. this.gl.uniformMatrix4fv(blitProgram.uniforms.uTransform, false, transform); this.gl.activeTexture(this.gl.TEXTURE0); - this.gl.bindTexture(this.gl.TEXTURE_2D, this.appController.atlas.ensureTexture(this.gl)); + this.gl.bindTexture(this.gl.TEXTURE_2D, this.appController.atlas.ensureTexture(this)); this.gl.uniform1i(blitProgram.uniforms.uSource, 0); this.setIdentityTexScaleUniform(blitProgram.uniforms); this.gl.drawElements(this.gl.TRIANGLES, @@ -582,7 +582,7 @@ class TextDemoView extends MonochromeDemoView { } private createAtlasFramebuffer() { - const atlasColorTexture = this.appController.atlas.ensureTexture(this.gl); + const atlasColorTexture = this.appController.atlas.ensureTexture(this); this.atlasDepthTexture = createFramebufferDepthTexture(this.gl, ATLAS_SIZE); this.atlasFramebuffer = createFramebuffer(this.gl, this.drawBuffersExt, @@ -770,23 +770,23 @@ class Atlas { this._usedSize = glmatrix.vec2.clone([ATLAS_SIZE[0], shelfBottom]); } - ensureTexture(gl: WebGLRenderingContext): WebGLTexture { + ensureTexture(view: DemoView): WebGLTexture { if (this._texture != null) return this._texture; - const texture = unwrapNull(gl.createTexture()); + const texture = unwrapNull(view.gl.createTexture()); this._texture = texture; - gl.bindTexture(gl.TEXTURE_2D, texture); - gl.texImage2D(gl.TEXTURE_2D, - 0, - gl.RGBA, - ATLAS_SIZE[0], - ATLAS_SIZE[1], - 0, - gl.RGBA, - gl.UNSIGNED_BYTE, - null); - setTextureParameters(gl, gl.NEAREST); + view.gl.bindTexture(view.gl.TEXTURE_2D, texture); + view.gl.texImage2D(view.gl.TEXTURE_2D, + 0, + view.colorAlphaFormat, + ATLAS_SIZE[0], + ATLAS_SIZE[1], + 0, + view.colorAlphaFormat, + view.gl.UNSIGNED_BYTE, + null); + setTextureParameters(view.gl, view.gl.NEAREST); return texture; } diff --git a/demo/client/src/view.ts b/demo/client/src/view.ts index 7631942b..eb2254be 100644 --- a/demo/client/src/view.ts +++ b/demo/client/src/view.ts @@ -140,6 +140,11 @@ export abstract class DemoView extends PathfinderView { pathTransformBufferTextures: PathfinderBufferTexture[]; + get colorAlphaFormat(): number { + return this.sRGBExt == null ? this.gl.RGBA : this.sRGBExt.SRGB_ALPHA_EXT; + } + + protected sRGBExt: any; protected timerQueryExt: any; protected antialiasingStrategy: AntialiasingStrategy | null; @@ -295,6 +300,7 @@ export abstract class DemoView extends PathfinderView { this.drawBuffersExt = this.gl.getExtension('WEBGL_draw_buffers'); this.colorBufferHalfFloatExt = this.gl.getExtension('EXT_color_buffer_half_float'); this.instancedArraysExt = this.gl.getExtension('ANGLE_instanced_arrays'); + this.sRGBExt = this.gl.getExtension('EXT_sRGB'); this.textureHalfFloatExt = this.gl.getExtension('OES_texture_half_float'); this.timerQueryExt = this.gl.getExtension('EXT_disjoint_timer_query'); this.vertexArrayObjectExt = this.gl.getExtension('OES_vertex_array_object');