uniform location caching
This commit is contained in:
parent
badd6e7f5f
commit
400ade6f22
@ -57,8 +57,10 @@ export function createIndexBuffer(gl:WebGL2RenderingContext, data:number[]){
|
|||||||
|
|
||||||
export class GLProgram{
|
export class GLProgram{
|
||||||
readonly program: WebGLProgram;
|
readonly program: WebGLProgram;
|
||||||
|
#uniformLoc : Map<string,WebGLUniformLocation>;
|
||||||
constructor(program: WebGLProgram){
|
constructor(program: WebGLProgram){
|
||||||
this.program = program;
|
this.program = program;
|
||||||
|
this.#uniformLoc = new Map<string,WebGLUniformLocation>();
|
||||||
}
|
}
|
||||||
getActiveUniforms(gl:WebGL2RenderingContext):WebGLActiveInfo[]{
|
getActiveUniforms(gl:WebGL2RenderingContext):WebGLActiveInfo[]{
|
||||||
const num = gl.getProgramParameter(this.program,gl.ACTIVE_UNIFORMS);
|
const num = gl.getProgramParameter(this.program,gl.ACTIVE_UNIFORMS);
|
||||||
@ -74,11 +76,22 @@ export class GLProgram{
|
|||||||
return gl.getAttribLocation(this.program,name);
|
return gl.getAttribLocation(this.program,name);
|
||||||
}
|
}
|
||||||
getUniformLocation(gl:WebGL2RenderingContext,name:string):WebGLUniformLocation{
|
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{
|
use(gl:WebGL2RenderingContext):void{
|
||||||
gl.useProgram(this.program);
|
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{
|
unuse(gl:WebGL2RenderingContext):void{
|
||||||
gl.useProgram(null);
|
gl.useProgram(null);
|
||||||
}
|
}
|
||||||
|
@ -72,8 +72,7 @@ export class Renderer2D implements RenderProgram{
|
|||||||
draw(gl:WebGL2RenderingContext){
|
draw(gl:WebGL2RenderingContext){
|
||||||
this.useProgram();
|
this.useProgram();
|
||||||
|
|
||||||
const location = this.program.getUniformLocation(gl,"u_color");//u_color 변수 위치를 참조
|
this.program.setUniform4fv(gl,"u_color",[this.uniforms.redcolor,0.3,0.8,1.0]);
|
||||||
gl.uniform4f(location, this.uniforms.redcolor, 0.3, 0.8, 1.0); //해당 위치에 0.2, 0.3, 0.8, 1.0 데이터를 전달
|
|
||||||
this.vao.bind(gl);
|
this.vao.bind(gl);
|
||||||
|
|
||||||
gl.drawElements(gl.TRIANGLES,this.indexBuffer.count,gl.UNSIGNED_SHORT,0);
|
gl.drawElements(gl.TRIANGLES,this.indexBuffer.count,gl.UNSIGNED_SHORT,0);
|
||||||
|
Loading…
Reference in New Issue
Block a user