Skip to content

Commit

Permalink
Small optimizations (#1294)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsherif committed Nov 21, 2019
1 parent 4070147 commit f1f7119
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
camelcase: 0,
'max-statements': 0,
'max-depth': 0,
complexity: 0,
'luma-gl-custom-rules/check-log-call': 1,
'import/no-unresolved': ['error'],
'import/no-extraneous-dependencies': [
Expand Down
14 changes: 14 additions & 0 deletions modules/gltools/src/state-tracker/track-context-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ function installSetterSpy(gl, functionName, setter) {
});
}

function installProgramSpy(gl) {
const originalUseProgram = gl.useProgram.bind(gl);

gl.useProgram = function useProgramLuma(handle) {
if (gl.state.program !== handle) {
originalUseProgram(handle);
gl.state.program = handle;
}
};
}

// HELPER CLASS - GLState

/* eslint-disable no-shadow */
Expand All @@ -83,6 +94,7 @@ class GLState {
} = {}
) {
this.gl = gl;
this.program = null;
this.stateStack = [];
this.enable = true;
this.cache = copyState ? getParameters(gl) : Object.assign({}, GL_PARAMETER_DEFAULTS);
Expand Down Expand Up @@ -160,6 +172,8 @@ export default function trackContextState(gl, {enable = true, copyState} = {}) {
// Create a state cache
gl.state = new GLState(gl, {copyState, enable});

installProgramSpy(gl);

// intercept all setter functions in the table
for (const key in GL_HOOKED_SETTERS) {
const setter = GL_HOOKED_SETTERS[key];
Expand Down
4 changes: 4 additions & 0 deletions modules/gltools/src/state-tracker/unified-parameter-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {isObjectEmpty} from './utils';
// Note: requires a `cache` object to be set on the context (gl.state.cache)
// This object is used to fill in any missing values for composite setter functions
export function setParameters(gl, values) {
if (isObjectEmpty(values)) {
return;
}

const compositeSetters = {};

// HANDLE PRIMITIVE SETTERS (and make note of any composite setters)
Expand Down
24 changes: 9 additions & 15 deletions modules/webgl/src/classes/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default class Program extends Resource {
// uniforms
this.uniforms = {};

this._textureUniforms = {};
this._texturesRenderable = true;

// Setup varyings if supplied
Expand Down Expand Up @@ -202,6 +203,7 @@ export default class Program extends Resource {
if (uniformSetter) {
let value = uniform;
let textureUpdate = false;

if (value instanceof Framebuffer) {
value = value.texture;
}
Expand All @@ -224,9 +226,13 @@ export default class Program extends Resource {
if (!texture.loaded) {
this._texturesRenderable = false;
}

this._textureUniforms[uniformName] = texture;
} else {
value = uniformSetter.textureIndex;
}
} else if (this._textureUniforms[uniformName]) {
delete this._textureUniforms[uniformName];
}

// NOTE(Tarek): uniformSetter returns whether
Expand Down Expand Up @@ -276,21 +282,9 @@ export default class Program extends Resource {
// Binds textures
// Note: This is currently done before every draw call
_bindTextures() {
for (const uniformName in this.uniforms) {
const uniformSetter = this._uniformSetters[uniformName];

if (uniformSetter && uniformSetter.textureIndex !== undefined) {
let uniform = this.uniforms[uniformName];

if (uniform instanceof Framebuffer) {
uniform = uniform.texture;
}
if (uniform instanceof Texture) {
const texture = uniform;
// Bind texture to index
texture.bind(uniformSetter.textureIndex);
}
}
for (const uniformName in this._textureUniforms) {
const textureIndex = this._uniformSetters[uniformName].textureIndex;
this._textureUniforms[uniformName].bind(textureIndex);
}
}

Expand Down

0 comments on commit f1f7119

Please sign in to comment.