mirror of
https://github.com/FMS-Cat/condition.git
synced 2025-08-17 19:06:42 +02:00
performance: improve PixelSorter performance
This commit is contained in:
@@ -39,17 +39,21 @@ export class PixelSorter {
|
|||||||
new BufferRenderTarget( {
|
new BufferRenderTarget( {
|
||||||
width: options.target.width,
|
width: options.target.width,
|
||||||
height: options.target.height,
|
height: options.target.height,
|
||||||
numBuffers: 2,
|
|
||||||
name: process.env.DEV && 'PixelSorter/swap0',
|
name: process.env.DEV && 'PixelSorter/swap0',
|
||||||
} ),
|
} ),
|
||||||
new BufferRenderTarget( {
|
new BufferRenderTarget( {
|
||||||
width: options.target.width,
|
width: options.target.width,
|
||||||
height: options.target.height,
|
height: options.target.height,
|
||||||
numBuffers: 2,
|
|
||||||
name: process.env.DEV && 'PixelSorter/swap1',
|
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 -----------------------------------------------------------------------------------
|
// -- bypass -----------------------------------------------------------------------------------
|
||||||
const materialReturn = new Material(
|
const materialReturn = new Material(
|
||||||
quadVert,
|
quadVert,
|
||||||
@@ -70,7 +74,7 @@ export class PixelSorter {
|
|||||||
const indexMaterials: Material[] = [];
|
const indexMaterials: Material[] = [];
|
||||||
|
|
||||||
while ( mul < options.target.width ) {
|
while ( mul < options.target.width ) {
|
||||||
const isFirst = mul === 1;
|
const isLast = ( mul * 8 > options.target.width );
|
||||||
|
|
||||||
const material = new Material(
|
const material = new Material(
|
||||||
quadVert,
|
quadVert,
|
||||||
@@ -79,16 +83,16 @@ export class PixelSorter {
|
|||||||
material.addUniform( 'mul', '1f', mul );
|
material.addUniform( 'mul', '1f', mul );
|
||||||
material.addUniformTexture(
|
material.addUniformTexture(
|
||||||
'sampler0',
|
'sampler0',
|
||||||
isFirst ? options.input : this.swapBuffer.o.getTexture( gl.COLOR_ATTACHMENT0 ),
|
options.input,
|
||||||
);
|
);
|
||||||
material.addUniformTexture(
|
material.addUniformTexture(
|
||||||
'sampler1',
|
'sampler1',
|
||||||
this.swapBuffer.o.getTexture( gl.COLOR_ATTACHMENT1 ),
|
this.swapBuffer.o.texture,
|
||||||
);
|
);
|
||||||
indexMaterials.push( material );
|
indexMaterials.push( material );
|
||||||
|
|
||||||
entityMain.components.push( new Quad( {
|
entityMain.components.push( new Quad( {
|
||||||
target: this.swapBuffer.i,
|
target: isLast ? bufferIndex : this.swapBuffer.i,
|
||||||
material,
|
material,
|
||||||
name: process.env.DEV && `PixelSorter/quadIndex-${ mul }`,
|
name: process.env.DEV && `PixelSorter/quadIndex-${ mul }`,
|
||||||
} ) );
|
} ) );
|
||||||
@@ -99,11 +103,12 @@ export class PixelSorter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// -- sort -------------------------------------------------------------------------------------
|
// -- sort -------------------------------------------------------------------------------------
|
||||||
let dir = 1.0 / 64.0;
|
let dir = 1.0 / 32.0;
|
||||||
let comp = 1.0 / 64.0;
|
let comp = 1.0 / 32.0;
|
||||||
|
|
||||||
while ( dir < 1.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(
|
const material = new Material(
|
||||||
quadVert,
|
quadVert,
|
||||||
@@ -113,11 +118,11 @@ export class PixelSorter {
|
|||||||
material.addUniform( 'comp', '1f', comp );
|
material.addUniform( 'comp', '1f', comp );
|
||||||
material.addUniformTexture(
|
material.addUniformTexture(
|
||||||
'sampler0',
|
'sampler0',
|
||||||
this.swapBuffer.o.getTexture( gl.COLOR_ATTACHMENT0 ),
|
isFirst ? options.input : this.swapBuffer.o.texture,
|
||||||
);
|
);
|
||||||
material.addUniformTexture(
|
material.addUniformTexture(
|
||||||
'sampler1',
|
'sampler1',
|
||||||
this.swapBuffer.o.getTexture( gl.COLOR_ATTACHMENT1 ),
|
bufferIndex.texture,
|
||||||
);
|
);
|
||||||
|
|
||||||
entityMain.components.push( new Quad( {
|
entityMain.components.push( new Quad( {
|
||||||
@@ -128,7 +133,7 @@ export class PixelSorter {
|
|||||||
|
|
||||||
this.swapBuffer.swap();
|
this.swapBuffer.swap();
|
||||||
|
|
||||||
if ( comp === 1.0 / 64.0 ) {
|
if ( comp === 1.0 / 32.0 ) {
|
||||||
dir *= 2.0;
|
dir *= 2.0;
|
||||||
comp = dir;
|
comp = dir;
|
||||||
} else {
|
} else {
|
||||||
|
@@ -6,8 +6,7 @@ const vec3 RGB = vec3( 0.299, 0.587, 0.114 );
|
|||||||
|
|
||||||
in vec2 vUv;
|
in vec2 vUv;
|
||||||
|
|
||||||
layout (location = 0) out vec4 fragColor;
|
out vec4 fragColor;
|
||||||
layout (location = 1) out vec4 fragClamp;
|
|
||||||
|
|
||||||
uniform float threshold;
|
uniform float threshold;
|
||||||
uniform float mul;
|
uniform float mul;
|
||||||
@@ -26,12 +25,12 @@ vec2 getValue( vec2 uv ) {
|
|||||||
void main() {
|
void main() {
|
||||||
vec2 uv = vUv;
|
vec2 uv = vUv;
|
||||||
|
|
||||||
fragColor = vec4( texture( sampler0, uv ).xyz, 1.0 );
|
vec4 tex = vec4( texture( sampler0, uv ).xyz, 1.0 );
|
||||||
fragClamp = vec4( getValue( uv ), 0.0, 1.0 );
|
fragColor = vec4( getValue( uv ), 0.0, 1.0 );
|
||||||
|
|
||||||
for ( int i = 1; i < 8; i ++ ) {
|
for ( int i = 1; i < 8; i ++ ) {
|
||||||
vec2 uvc = uv - vec2( i, 0 ) / resolution * mul;
|
vec2 uvc = uv - vec2( i, 0 ) / resolution * mul;
|
||||||
vec2 texc = getValue( uvc );
|
vec2 texc = getValue( uvc );
|
||||||
fragClamp.xy = min( fragClamp.xy, texc + mul * float( i ) );
|
fragColor.xy = min( fragColor.xy, texc + mul * float( i ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,8 +8,7 @@ const vec3 RGB = vec3( 0.299, 0.587, 0.114 );
|
|||||||
|
|
||||||
in vec2 vUv;
|
in vec2 vUv;
|
||||||
|
|
||||||
layout (location = 0) out vec4 fragColor;
|
out vec4 fragColor;
|
||||||
layout (location = 1) out vec4 fragClamp;
|
|
||||||
|
|
||||||
uniform float dir;
|
uniform float dir;
|
||||||
uniform float comp;
|
uniform float comp;
|
||||||
@@ -25,14 +24,14 @@ void main() {
|
|||||||
vec2 uv = vUv;
|
vec2 uv = vUv;
|
||||||
|
|
||||||
fragColor = texture( sampler0, uv );
|
fragColor = texture( sampler0, uv );
|
||||||
fragClamp = texture( sampler1, uv );
|
vec4 texIndex = texture( sampler1, uv );
|
||||||
|
|
||||||
if ( fragClamp.x < 0.5 ) {
|
if ( texIndex.x < 0.5 ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
float index = fragClamp.x - 1.0;
|
float index = texIndex.x - 1.0;
|
||||||
float width = fragClamp.x + fragClamp.y - 1.0;
|
float width = texIndex.x + texIndex.y - 1.0;
|
||||||
|
|
||||||
bool isCompRight = mod( index, 2.0 * comp * width ) < comp * width;
|
bool isCompRight = mod( index, 2.0 * comp * width ) < comp * width;
|
||||||
float offset = floor( ( isCompRight ? comp : -comp ) * width + 0.5 );
|
float offset = floor( ( isCompRight ? comp : -comp ) * width + 0.5 );
|
||||||
|
Reference in New Issue
Block a user