Skip to content

Commit

Permalink
Small shader initialization cleanup / optimization (#6083)
Browse files Browse the repository at this point in the history
* Small shader initialization cleanup / optimization

* moved few lines down

---------

Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Feb 22, 2024
1 parent 2886a90 commit d807226
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
21 changes: 21 additions & 0 deletions src/platform/graphics/webgl/webgl-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ class WebglGraphicsDevice extends GraphicsDevice {
}
}

/** @type {WebGL2RenderingContext} */
let gl = null;

// we always allocate the default framebuffer without antialiasing, so remove that option
Expand Down Expand Up @@ -427,6 +428,26 @@ class WebglGraphicsDevice extends GraphicsDevice {
// only enable ImageBitmap on chrome
this.supportsImageBitmap = !isSafari && typeof ImageBitmap !== 'undefined';

// supported sampler types
this._samplerTypes = new Set([
...[
gl.SAMPLER_2D,
gl.SAMPLER_CUBE
],
...(this.isWebGL2 ? [
gl.UNSIGNED_INT_SAMPLER_2D,
gl.INT_SAMPLER_2D,
gl.SAMPLER_2D_SHADOW,
gl.SAMPLER_CUBE_SHADOW,
gl.SAMPLER_3D,
gl.INT_SAMPLER_3D,
gl.UNSIGNED_INT_SAMPLER_3D,
gl.SAMPLER_2D_ARRAY,
gl.INT_SAMPLER_2D_ARRAY,
gl.UNSIGNED_INT_SAMPLER_2D_ARRAY
] : [])
]);

this.glAddress = [
gl.REPEAT,
gl.CLAMP_TO_EDGE,
Expand Down
36 changes: 9 additions & 27 deletions src/platform/graphics/webgl/webgl-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { DebugGraphics } from '../debug-graphics.js';

let _totalCompileTime = 0;

const _vertexShaderBuiltins = [
const _vertexShaderBuiltins = new Set([
'gl_VertexID',
'gl_InstanceID',
'gl_DrawID',
'gl_BaseVertex',
'gl_BaseInstance'
];
]);

// class used to hold compiled WebGL vertex or fragment shaders in the device cache
class CompiledShaderCache {
Expand Down Expand Up @@ -304,14 +304,13 @@ class WebglShader {
}

// Query the program for each vertex buffer input (GLSL 'attribute')
let i = 0;
const numAttributes = gl.getProgramParameter(glProgram, gl.ACTIVE_ATTRIBUTES);
while (i < numAttributes) {
const info = gl.getActiveAttrib(glProgram, i++);
for (let i = 0; i < numAttributes; i++) {
const info = gl.getActiveAttrib(glProgram, i);
const location = gl.getAttribLocation(glProgram, info.name);

// a built-in attributes for which we do not need to provide any data
if (_vertexShaderBuiltins.indexOf(info.name) !== -1)
if (_vertexShaderBuiltins.has(info.name))
continue;

// Check attributes are correctly linked up
Expand All @@ -325,32 +324,15 @@ class WebglShader {
}

// Query the program for each shader state (GLSL 'uniform')
i = 0;
const samplerTypes = device._samplerTypes;
const numUniforms = gl.getProgramParameter(glProgram, gl.ACTIVE_UNIFORMS);
while (i < numUniforms) {
const info = gl.getActiveUniform(glProgram, i++);
for (let i = 0; i < numUniforms; i++) {
const info = gl.getActiveUniform(glProgram, i);
const location = gl.getUniformLocation(glProgram, info.name);

const shaderInput = new WebglShaderInput(device, info.name, device.pcUniformType[info.type], location);

if (
info.type === gl.SAMPLER_2D ||
info.type === gl.SAMPLER_CUBE ||
(
device.isWebGL2 && (
info.type === gl.UNSIGNED_INT_SAMPLER_2D ||
info.type === gl.INT_SAMPLER_2D ||
info.type === gl.SAMPLER_2D_SHADOW ||
info.type === gl.SAMPLER_CUBE_SHADOW ||
info.type === gl.SAMPLER_3D ||
info.type === gl.INT_SAMPLER_3D ||
info.type === gl.UNSIGNED_INT_SAMPLER_3D ||
info.type === gl.SAMPLER_2D_ARRAY ||
info.type === gl.INT_SAMPLER_2D_ARRAY ||
info.type === gl.UNSIGNED_INT_SAMPLER_2D_ARRAY
)
)
) {
if (samplerTypes.has(info.type)) {
this.samplers.push(shaderInput);
} else {
this.uniforms.push(shaderInput);
Expand Down

0 comments on commit d807226

Please sign in to comment.