Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small shader initialization cleanup / optimization #6083

Merged
merged 2 commits into from Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/platform/graphics/webgl/webgl-graphics-device.js
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
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