uniform location caching

This commit is contained in:
monoid 2021-10-03 23:34:22 +09:00
parent badd6e7f5f
commit 400ade6f22
2 changed files with 15 additions and 3 deletions

View File

@ -57,8 +57,10 @@ export function createIndexBuffer(gl:WebGL2RenderingContext, data:number[]){
export class GLProgram{
readonly program: WebGLProgram;
#uniformLoc : Map<string,WebGLUniformLocation>;
constructor(program: WebGLProgram){
this.program = program;
this.#uniformLoc = new Map<string,WebGLUniformLocation>();
}
getActiveUniforms(gl:WebGL2RenderingContext):WebGLActiveInfo[]{
const num = gl.getProgramParameter(this.program,gl.ACTIVE_UNIFORMS);
@ -74,11 +76,22 @@ export class GLProgram{
return gl.getAttribLocation(this.program,name);
}
getUniformLocation(gl:WebGL2RenderingContext,name:string):WebGLUniformLocation{
return gl.getUniformLocation(this.program,name);
if(this.#uniformLoc.has(name)){
return this.#uniformLoc.get(name);
}
else{
const location = gl.getUniformLocation(this.program,name);
this.#uniformLoc.set(name,location);
return location;
}
}
use(gl:WebGL2RenderingContext):void{
gl.useProgram(this.program);
}
setUniform4fv(gl:WebGL2RenderingContext,name:string,v:number[]){
const loc = this.getUniformLocation(gl,name);
gl.uniform4fv(loc,v);
}
unuse(gl:WebGL2RenderingContext):void{
gl.useProgram(null);
}

View File

@ -72,8 +72,7 @@ export class Renderer2D implements RenderProgram{
draw(gl:WebGL2RenderingContext){
this.useProgram();
const location = this.program.getUniformLocation(gl,"u_color");//u_color 변수 위치를 참조
gl.uniform4f(location, this.uniforms.redcolor, 0.3, 0.8, 1.0); //해당 위치에 0.2, 0.3, 0.8, 1.0 데이터를 전달
this.program.setUniform4fv(gl,"u_color",[this.uniforms.redcolor,0.3,0.8,1.0]);
this.vao.bind(gl);
gl.drawElements(gl.TRIANGLES,this.indexBuffer.count,gl.UNSIGNED_SHORT,0);