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

Program manager fixes #1222

Merged
merged 3 commits into from Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -84,6 +84,7 @@ Get a program that fits the parameters provided. If one is already cached, retur
* `defines`: Object indicating `#define` constants to include in the shaders.
* `modules`: Array of module objects to include in the shaders.
* `inject`: Object of hook injections to include in the shaders.
* `program`: A `Program` object to compare the new one two. If they're the same, use count won't be modified. If not, `program` will be released.

### `addDefaultModule(module: Object)`

Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/lib/base-model.js
Expand Up @@ -121,7 +121,7 @@ export default class BaseModel {
}

setProgram(props) {
this.programProps = Object.assign({}, props, {});
this.programProps = Object.assign({}, props);
this._programDirty = true;
}

Expand Down Expand Up @@ -276,7 +276,7 @@ export default class BaseModel {
this.getModuleUniforms = () => {};
this._programDirty = false;
} else if (this.programManager) {
program = this.programManager.get({vs, fs, modules, inject, defines});
program = this.programManager.get({vs, fs, modules, inject, defines, program: this.program});
this.getModuleUniforms = this.programManager.getUniforms(program);
// Program always dirty if there's a program manager
} else {
Expand Down
24 changes: 21 additions & 3 deletions modules/core/src/resource-management/program-manager.js
Expand Up @@ -35,7 +35,7 @@ export default class ProgramManager {
}

addModuleInjection(module, opts) {
const moduleName = module.name;
const moduleName = typeof module === 'string' ? module : module.name;
const {hook, injection, order = 0} = opts;
const shaderStage = hook.slice(0, 2);

Expand All @@ -60,8 +60,17 @@ export default class ProgramManager {
}

get(props = {}) {
const {vs = '', fs = '', defines = {}, inject = {}, varyings = [], bufferMode = 0x8c8d} = props; // varyings/bufferMode for xform feedback, 0x8c8d = SEPARATE_ATTRIBS

const {
vs = '',
fs = '',
defines = {},
inject = {},
varyings = [],
bufferMode = 0x8c8d,
program = null
} = props; // varyings/bufferMode for xform feedback, 0x8c8d = SEPARATE_ATTRIBS

const oldProgramHash = program ? program.hash : '';
const modules = this._getModuleList(props.modules); // Combine with default modules

const vsHash = this._getHash(vs);
Expand Down Expand Up @@ -90,6 +99,10 @@ export default class ProgramManager {
this._hookStateCounter
}B${bufferMode}`;

if (oldProgramHash === hash) {
return program;
}

if (!this._programCache[hash]) {
const assembled = assembleShaders(this.gl, {
vs,
Expand All @@ -114,6 +127,11 @@ export default class ProgramManager {
}

this._useCounts[hash]++;

if (program) {
this.release(program);
}

return this._programCache[hash];
}

Expand Down
29 changes: 29 additions & 0 deletions modules/core/test/lib/model.spec.js
Expand Up @@ -171,6 +171,35 @@ test('Model#program management', t => {
t.ok(model1.program !== model2.program, 'Program updated after draw.');
t.deepEqual(model2.getUniforms(), model2.program.uniforms, 'Program uniforms set');

// This part is checking that the use counts
// don't get bloated by multiple checks.
const model3 = new Model(gl, {
programManager: pm,
vs,
fs,
defines: {
MODEL3_DEFINE1: true
}
});

const oldProgram = model3.program;

// Check for program updates a few times
model3.draw();
model3.draw();

model3.setProgram({
vs,
fs,
defines: {
MODEL3_DEFINE2: true
}
});

model3.draw();
t.ok(model3.program !== oldProgram, 'Program updated after draw.');
t.ok(oldProgram.handle === null, 'Old program released after update');

t.end();
});

Expand Down
2 changes: 1 addition & 1 deletion modules/webgl/src/classes/program.js
Expand Up @@ -56,7 +56,7 @@ export default class Program extends Resource {
initialize(props = {}) {
const {hash, vs, fs, varyings, bufferMode = GL_SEPARATE_ATTRIBS} = props;

this.hash = hash || null; // Used by ProgramManager
this.hash = hash || ''; // Used by ProgramManager

// Create shaders if needed
this.vs =
Expand Down