Skip to content

Commit

Permalink
Fix #2: Statically Allocate for Texture
Browse files Browse the repository at this point in the history
  • Loading branch information
monyone committed Jun 4, 2024
1 parent 325ceac commit 7c2bfce
Show file tree
Hide file tree
Showing 54 changed files with 2,079 additions and 2,731 deletions.
2 changes: 1 addition & 1 deletion docs/dist/anime4k.js

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions generate_shader.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ def prepareTextures(program: Program):
if (!{bind}) {{ return; }}
""".strip() for bind in sorted(list(set(program.get_bind() + [program.get_hook(), 'OUTPUT', 'NATIVE'])))])

def prepareOutputTexture(program: Program):
def prepareOutputTexture(program: Program, program_index: int):
return f"""
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_{program_index}_intermediate_texture;
fillEmptyTexture(gl, output, {program.get_width()}, {program.get_height()});
gl.viewport(0, 0, {program.get_width()}, {program.get_height()});
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand All @@ -223,9 +223,6 @@ def unbindTextures(program: Program):

def pushOutputTexture(program: Program):
return f"""
if (textures.has('{program.get_save()}')) {{
gl.deleteTexture(textures.get('{program.get_save()}')!.texture);
}}
textures.set('{program.get_save()}', {{ texture: output, width: {program.get_width()}, height: {program.get_height()}}});
""".strip()

Expand All @@ -234,7 +231,7 @@ def generateHook(programs: list[Program], hook: str):
{{
{ indent(prepareTextures(program), ' ') }
{f' if ({program.get_when_cond()})' if program.get_when_cond() else ''} {{
{ indent(prepareOutputTexture(program), ' ') }
{ indent(prepareOutputTexture(program, index), ' ') }
gl.useProgram(this.program_{index});
Expand Down Expand Up @@ -335,13 +332,15 @@ def generateHook(programs: list[Program], hook: str):
for index, program in enumerate(programs):
out.write(f'const fragment_{index}_shader = `\n{program}`;\n')
webgl_programs_declare = '\n'.join([ f'private program_{index}: WebGLProgram;' for index in range(len(programs)) ])
webgl_program_intermediate_texture_declare = '\n'.join([ f'private program_{index}_intermediate_texture: WebGLProgram;' for index in range(len(programs)) ])
webgl_program_a_position_location_declare = '\n'.join([ f'private program_{index}_a_position_location: number;' for index in range(len(programs)) ])
webgl_program_a_texture_coord_location_declare = '\n'.join([ f'private program_{index}_a_texture_coord_location: number;' for index in range(len(programs)) ])
webgl_program_u_resolution_location_declare = '\n'.join([ f'private program_{index}_u_resolution_location: WebGLUniformLocation | null;' for index in range(len(programs)) ])
webgl_program_u_texture_size_location_declare = '\n'.join([ f'private program_{index}_u_texture_size_location: WebGLUniformLocation | null;' for index in range(len(programs)) ])
webgl_program_u_texture_location_declare = '\n'.join(['\n'.join([ f'private program_{index}_{bind}_TextureLocation: WebGLUniformLocation | null' for bind in program.get_bind()]) for index, program in enumerate(programs)])

webgl_programs_assign = '\n'.join([ f'this.program_{index} = createProgram(gl, createVertexShader(gl, vertex_shader)!, createFragmentShader(gl, fragment_{index}_shader)!)!;' for index in range(len(programs)) ])
webgl_program_intermediate_texture_assign = '\n'.join([ f'this.program_{index}_intermediate_texture = createTexture(gl, gl.NEAREST)!;' for index in range(len(programs)) ])
webgl_program_a_position_location_assign = '\n'.join([ f'this.program_{index}_a_position_location = gl.getAttribLocation(this.program_{index}, "a_position");\ngl.enableVertexAttribArray(this.program_{index}_a_position_location);' for index in range(len(programs))])
webgl_program_a_texture_coord_location_assign = '\n'.join([ f'this.program_{index}_a_texture_coord_location = gl.getAttribLocation(this.program_{index}, "a_texture_coord");\ngl.enableVertexAttribArray(this.program_{index}_a_texture_coord_location);' for index in range(len(programs)) ])
webgl_program_u_resolution_location_assign = '\n'.join([ f'this.program_{index}_u_resolution_location = gl.getUniformLocation(this.program_{index}, "u_resolution");' for index in range(len(programs)) ])
Expand All @@ -356,6 +355,7 @@ def generateHook(programs: list[Program], hook: str):
export default class {outfile.stem.replace('+', '_')} extends Anime4KShader {{
private gl: WebGLRenderingContext;
{indent(webgl_programs_declare, ' ')}
{indent(webgl_program_intermediate_texture_declare, ' ')}
{indent(webgl_program_a_position_location_declare, ' ')}
{indent(webgl_program_a_texture_coord_location_declare, ' ')}
{indent(webgl_program_u_resolution_location_declare, ' ')}
Expand All @@ -367,6 +367,7 @@ def generateHook(programs: list[Program], hook: str):
super();
this.gl = gl;
{indent(webgl_programs_assign, ' ')}
{indent(webgl_program_intermediate_texture_assign, ' ')}
{indent(webgl_program_a_position_location_assign, ' ')}
{indent(webgl_program_a_texture_coord_location_assign, ' ')}
{indent(webgl_program_u_resolution_location_assign, ' ')}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anime4k.js",
"version": "0.0.5",
"version": "0.0.6",
"description": "Anime4K WebGL port/implementation",
"keywords": [
"anime4k",
Expand Down
28 changes: 12 additions & 16 deletions src/glsl/Deblur/Anime4K_Deblur_DoG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
private program_1: WebGLProgram;
private program_2: WebGLProgram;
private program_3: WebGLProgram;
private program_0_intermediate_texture: WebGLProgram;
private program_1_intermediate_texture: WebGLProgram;
private program_2_intermediate_texture: WebGLProgram;
private program_3_intermediate_texture: WebGLProgram;
private program_0_a_position_location: number;
private program_1_a_position_location: number;
private program_2_a_position_location: number;
Expand Down Expand Up @@ -243,6 +247,10 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
this.program_1 = createProgram(gl, createVertexShader(gl, vertex_shader)!, createFragmentShader(gl, fragment_1_shader)!)!;
this.program_2 = createProgram(gl, createVertexShader(gl, vertex_shader)!, createFragmentShader(gl, fragment_2_shader)!)!;
this.program_3 = createProgram(gl, createVertexShader(gl, vertex_shader)!, createFragmentShader(gl, fragment_3_shader)!)!;
this.program_0_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_1_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_2_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_3_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_0_a_position_location = gl.getAttribLocation(this.program_0, "a_position");
gl.enableVertexAttribArray(this.program_0_a_position_location);
this.program_1_a_position_location = gl.getAttribLocation(this.program_1, "a_position");
Expand Down Expand Up @@ -289,7 +297,7 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_0_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width), (MAIN.height));
gl.viewport(0, 0, (MAIN.width), (MAIN.height));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand All @@ -314,9 +322,6 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('LINELUMA')) {
gl.deleteTexture(textures.get('LINELUMA')!.texture);
}
textures.set('LINELUMA', { texture: output, width: (MAIN.width), height: (MAIN.height)});
}
}
Expand All @@ -332,7 +337,7 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_1_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width), (MAIN.height));
gl.viewport(0, 0, (MAIN.width), (MAIN.height));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand Down Expand Up @@ -362,9 +367,6 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('MMKERNEL')) {
gl.deleteTexture(textures.get('MMKERNEL')!.texture);
}
textures.set('MMKERNEL', { texture: output, width: (MAIN.width), height: (MAIN.height)});
}
}
Expand All @@ -380,7 +382,7 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_2_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width), (MAIN.height));
gl.viewport(0, 0, (MAIN.width), (MAIN.height));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand Down Expand Up @@ -410,9 +412,6 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('MMKERNEL')) {
gl.deleteTexture(textures.get('MMKERNEL')!.texture);
}
textures.set('MMKERNEL', { texture: output, width: (MAIN.width), height: (MAIN.height)});
}
}
Expand All @@ -430,7 +429,7 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_3_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width), (MAIN.height));
gl.viewport(0, 0, (MAIN.width), (MAIN.height));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand Down Expand Up @@ -465,9 +464,6 @@ export default class Anime4K_Deblur_DoG extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('MAIN')) {
gl.deleteTexture(textures.get('MAIN')!.texture);
}
textures.set('MAIN', { texture: output, width: (MAIN.width), height: (MAIN.height)});
}
}
Expand Down
49 changes: 21 additions & 28 deletions src/glsl/Deblur/Anime4K_Deblur_Original.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
private program_4: WebGLProgram;
private program_5: WebGLProgram;
private program_6: WebGLProgram;
private program_0_intermediate_texture: WebGLProgram;
private program_1_intermediate_texture: WebGLProgram;
private program_2_intermediate_texture: WebGLProgram;
private program_3_intermediate_texture: WebGLProgram;
private program_4_intermediate_texture: WebGLProgram;
private program_5_intermediate_texture: WebGLProgram;
private program_6_intermediate_texture: WebGLProgram;
private program_0_a_position_location: number;
private program_1_a_position_location: number;
private program_2_a_position_location: number;
Expand Down Expand Up @@ -414,6 +421,13 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
this.program_4 = createProgram(gl, createVertexShader(gl, vertex_shader)!, createFragmentShader(gl, fragment_4_shader)!)!;
this.program_5 = createProgram(gl, createVertexShader(gl, vertex_shader)!, createFragmentShader(gl, fragment_5_shader)!)!;
this.program_6 = createProgram(gl, createVertexShader(gl, vertex_shader)!, createFragmentShader(gl, fragment_6_shader)!)!;
this.program_0_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_1_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_2_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_3_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_4_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_5_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_6_intermediate_texture = createTexture(gl, gl.NEAREST)!;
this.program_0_a_position_location = gl.getAttribLocation(this.program_0, "a_position");
gl.enableVertexAttribArray(this.program_0_a_position_location);
this.program_1_a_position_location = gl.getAttribLocation(this.program_1, "a_position");
Expand Down Expand Up @@ -485,7 +499,7 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_0_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width), (MAIN.height));
gl.viewport(0, 0, (MAIN.width), (MAIN.height));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand All @@ -510,9 +524,6 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('LINELUMA')) {
gl.deleteTexture(textures.get('LINELUMA')!.texture);
}
textures.set('LINELUMA', { texture: output, width: (MAIN.width), height: (MAIN.height)});
}
}
Expand All @@ -528,7 +539,7 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_1_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width * 2), (MAIN.height * 2));
gl.viewport(0, 0, (MAIN.width * 2), (MAIN.height * 2));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand Down Expand Up @@ -558,9 +569,6 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('LUMAD')) {
gl.deleteTexture(textures.get('LUMAD')!.texture);
}
textures.set('LUMAD', { texture: output, width: (MAIN.width * 2), height: (MAIN.height * 2)});
}
}
Expand All @@ -576,7 +584,7 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_2_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width * 2), (MAIN.height * 2));
gl.viewport(0, 0, (MAIN.width * 2), (MAIN.height * 2));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand Down Expand Up @@ -606,9 +614,6 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('LUMAD')) {
gl.deleteTexture(textures.get('LUMAD')!.texture);
}
textures.set('LUMAD', { texture: output, width: (MAIN.width * 2), height: (MAIN.height * 2)});
}
}
Expand All @@ -624,7 +629,7 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_3_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width * 2), (MAIN.height * 2));
gl.viewport(0, 0, (MAIN.width * 2), (MAIN.height * 2));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand Down Expand Up @@ -654,9 +659,6 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('LUMAMM')) {
gl.deleteTexture(textures.get('LUMAMM')!.texture);
}
textures.set('LUMAMM', { texture: output, width: (MAIN.width * 2), height: (MAIN.height * 2)});
}
}
Expand All @@ -674,7 +676,7 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_4_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width * 2), (MAIN.height * 2));
gl.viewport(0, 0, (MAIN.width * 2), (MAIN.height * 2));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand Down Expand Up @@ -709,9 +711,6 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('LUMAMM')) {
gl.deleteTexture(textures.get('LUMAMM')!.texture);
}
textures.set('LUMAMM', { texture: output, width: (MAIN.width * 2), height: (MAIN.height * 2)});
}
}
Expand All @@ -729,7 +728,7 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
const OUTPUT = textures.get('OUTPUT');
if (!OUTPUT) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_5_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width * 2), (MAIN.height * 2));
gl.viewport(0, 0, (MAIN.width * 2), (MAIN.height * 2));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand Down Expand Up @@ -764,9 +763,6 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('RESAMPLED')) {
gl.deleteTexture(textures.get('RESAMPLED')!.texture);
}
textures.set('RESAMPLED', { texture: output, width: (MAIN.width * 2), height: (MAIN.height * 2)});
}
}
Expand All @@ -782,7 +778,7 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
const RESAMPLED = textures.get('RESAMPLED');
if (!RESAMPLED) { return; }
{
const output = createTexture(gl, gl.NEAREST)!;
const output = this.program_6_intermediate_texture;
fillEmptyTexture(gl, output, (MAIN.width), (MAIN.height));
gl.viewport(0, 0, (MAIN.width), (MAIN.height));
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
Expand Down Expand Up @@ -812,9 +808,6 @@ export default class Anime4K_Deblur_Original extends Anime4KShader {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(texcoordBuffer);
if (textures.has('MAIN')) {
gl.deleteTexture(textures.get('MAIN')!.texture);
}
textures.set('MAIN', { texture: output, width: (MAIN.width), height: (MAIN.height)});
}
}
Expand Down
Loading

0 comments on commit 7c2bfce

Please sign in to comment.