From ed8cffbff8539bda975a8e55d7517ffd9693e8fa Mon Sep 17 00:00:00 2001 From: FMS-Cat Date: Sun, 14 Mar 2021 18:43:26 +0900 Subject: [PATCH] performance: improve PixelSorter performance --- src/entities/PixelSorter.ts | 29 +++++++++++++++++------------ src/shaders/pixel-sorter-index.frag | 9 ++++----- src/shaders/pixel-sorter.frag | 11 +++++------ 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/entities/PixelSorter.ts b/src/entities/PixelSorter.ts index b77c1e1..07233d1 100644 --- a/src/entities/PixelSorter.ts +++ b/src/entities/PixelSorter.ts @@ -39,17 +39,21 @@ export class PixelSorter { new BufferRenderTarget( { width: options.target.width, height: options.target.height, - numBuffers: 2, name: process.env.DEV && 'PixelSorter/swap0', } ), new BufferRenderTarget( { width: options.target.width, height: options.target.height, - numBuffers: 2, name: process.env.DEV && 'PixelSorter/swap1', } ), ); + const bufferIndex = new BufferRenderTarget( { + width: options.target.width, + height: options.target.height, + name: process.env.DEV && 'PixelSorter/index', + } ); + // -- bypass ----------------------------------------------------------------------------------- const materialReturn = new Material( quadVert, @@ -70,7 +74,7 @@ export class PixelSorter { const indexMaterials: Material[] = []; while ( mul < options.target.width ) { - const isFirst = mul === 1; + const isLast = ( mul * 8 > options.target.width ); const material = new Material( quadVert, @@ -79,16 +83,16 @@ export class PixelSorter { material.addUniform( 'mul', '1f', mul ); material.addUniformTexture( 'sampler0', - isFirst ? options.input : this.swapBuffer.o.getTexture( gl.COLOR_ATTACHMENT0 ), + options.input, ); material.addUniformTexture( 'sampler1', - this.swapBuffer.o.getTexture( gl.COLOR_ATTACHMENT1 ), + this.swapBuffer.o.texture, ); indexMaterials.push( material ); entityMain.components.push( new Quad( { - target: this.swapBuffer.i, + target: isLast ? bufferIndex : this.swapBuffer.i, material, name: process.env.DEV && `PixelSorter/quadIndex-${ mul }`, } ) ); @@ -99,11 +103,12 @@ export class PixelSorter { } // -- sort ------------------------------------------------------------------------------------- - let dir = 1.0 / 64.0; - let comp = 1.0 / 64.0; + let dir = 1.0 / 32.0; + let comp = 1.0 / 32.0; while ( dir < 1.0 ) { - const isLast = ( dir === 0.5 ) && ( comp === 1.0 / 64.0 ); + const isFirst = dir === 1.0 / 32.0; + const isLast = ( dir === 0.5 ) && ( comp === 1.0 / 32.0 ); const material = new Material( quadVert, @@ -113,11 +118,11 @@ export class PixelSorter { material.addUniform( 'comp', '1f', comp ); material.addUniformTexture( 'sampler0', - this.swapBuffer.o.getTexture( gl.COLOR_ATTACHMENT0 ), + isFirst ? options.input : this.swapBuffer.o.texture, ); material.addUniformTexture( 'sampler1', - this.swapBuffer.o.getTexture( gl.COLOR_ATTACHMENT1 ), + bufferIndex.texture, ); entityMain.components.push( new Quad( { @@ -128,7 +133,7 @@ export class PixelSorter { this.swapBuffer.swap(); - if ( comp === 1.0 / 64.0 ) { + if ( comp === 1.0 / 32.0 ) { dir *= 2.0; comp = dir; } else { diff --git a/src/shaders/pixel-sorter-index.frag b/src/shaders/pixel-sorter-index.frag index 49aab74..d4035a4 100644 --- a/src/shaders/pixel-sorter-index.frag +++ b/src/shaders/pixel-sorter-index.frag @@ -6,8 +6,7 @@ const vec3 RGB = vec3( 0.299, 0.587, 0.114 ); in vec2 vUv; -layout (location = 0) out vec4 fragColor; -layout (location = 1) out vec4 fragClamp; +out vec4 fragColor; uniform float threshold; uniform float mul; @@ -26,12 +25,12 @@ vec2 getValue( vec2 uv ) { void main() { vec2 uv = vUv; - fragColor = vec4( texture( sampler0, uv ).xyz, 1.0 ); - fragClamp = vec4( getValue( uv ), 0.0, 1.0 ); + vec4 tex = vec4( texture( sampler0, uv ).xyz, 1.0 ); + fragColor = vec4( getValue( uv ), 0.0, 1.0 ); for ( int i = 1; i < 8; i ++ ) { vec2 uvc = uv - vec2( i, 0 ) / resolution * mul; vec2 texc = getValue( uvc ); - fragClamp.xy = min( fragClamp.xy, texc + mul * float( i ) ); + fragColor.xy = min( fragColor.xy, texc + mul * float( i ) ); } } diff --git a/src/shaders/pixel-sorter.frag b/src/shaders/pixel-sorter.frag index 8015014..82493a1 100644 --- a/src/shaders/pixel-sorter.frag +++ b/src/shaders/pixel-sorter.frag @@ -8,8 +8,7 @@ const vec3 RGB = vec3( 0.299, 0.587, 0.114 ); in vec2 vUv; -layout (location = 0) out vec4 fragColor; -layout (location = 1) out vec4 fragClamp; +out vec4 fragColor; uniform float dir; uniform float comp; @@ -25,14 +24,14 @@ void main() { vec2 uv = vUv; fragColor = texture( sampler0, uv ); - fragClamp = texture( sampler1, uv ); + vec4 texIndex = texture( sampler1, uv ); - if ( fragClamp.x < 0.5 ) { + if ( texIndex.x < 0.5 ) { return; } - float index = fragClamp.x - 1.0; - float width = fragClamp.x + fragClamp.y - 1.0; + float index = texIndex.x - 1.0; + float width = texIndex.x + texIndex.y - 1.0; bool isCompRight = mod( index, 2.0 * comp * width ) < comp * width; float offset = floor( ( isCompRight ? comp : -comp ) * width + 0.5 );