Skip to content

Commit ce9fbef

Browse files
committed
optimize using port names a bit
1 parent 43b653c commit ce9fbef

File tree

4 files changed

+51
-40
lines changed

4 files changed

+51
-40
lines changed

src/graphics/program-lib/chunks/end.frag

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
gl_FragColor.rgb = combineColor();
55
#endif
66

7-
#ifdef SHADERGRAPH_PIXELSHADER
7+
#ifdef SHADERGRAPH_PS
88
gl_FragColor.rgb += dEmission;
99
#else
1010
gl_FragColor.rgb += getEmission();

src/graphics/program-lib/programs/standard-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ standardnode.createShaderDefinition = function (device, options) {
2727

2828
graphCode.vsDecl = '';
2929
if (rootShaderGraph.getIoPortByName('sgVertOff')) {
30-
graphCode.vsDecl += "#define SHADERGRAPH_VERTEXSHADER\n";
30+
graphCode.vsDecl += "#define SHADERGRAPH_VS\n";
3131
graphCode.vsDecl += rootDeclGLSL;
3232
}
3333

@@ -40,7 +40,7 @@ standardnode.createShaderDefinition = function (device, options) {
4040

4141
graphCode.psDecl = '';
4242
if (rootShaderGraph.getIoPortByName('sgAlpha') || rootShaderGraph.getIoPortByName('sgNormalMap') || rootShaderGraph.getIoPortByName('sgGlossiness') || rootShaderGraph.getIoPortByName('sgSpecularity') || rootShaderGraph.getIoPortByName('sgAlbedo') || rootShaderGraph.getIoPortByName('sgFragOut') || rootShaderGraph.getIoPortByName('sgEmission')) {
43-
graphCode.psDecl += "#define SHADERGRAPH_PIXELSHADER\n";
43+
graphCode.psDecl += "#define SHADERGRAPH_PS\n";
4444
graphCode.psDecl += rootDeclGLSL;
4545
}
4646

src/scene/materials/shader-graph-node.js

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var counter = 0;
5252
* @class
5353
* @name ShaderGraphNode
5454
* @classdesc A Shader Graph Node class used by shader graphs
55-
* @param {string} funcCodeString - shader function used by this material
55+
* @param {string} funcCodeString - shader function used by this node
5656
* @param {string} declCodeString - shader declarations used by the shader function
5757
*/
5858
var ShaderGraphNode = function (funcCodeString, declCodeString) {
@@ -64,6 +64,11 @@ var ShaderGraphNode = function (funcCodeString, declCodeString) {
6464
this._codeStringAssetReferences = {};
6565
this._subGraphAssetReferences = [];
6666

67+
// port map and cache (optimization)
68+
this._portMap = {};
69+
this._portCache = {};
70+
71+
// graph
6772
this.graphData = {};
6873

6974
this.graphData.ioPorts = []; // input, output or constant variables
@@ -76,9 +81,6 @@ var ShaderGraphNode = function (funcCodeString, declCodeString) {
7681
this.graphData.subGraphs = [];
7782
this.graphData.connections = [];
7883
}
79-
80-
// cache for port access acceleration optimization
81-
this._portCache = {};
8284
};
8385

8486
ShaderGraphNode.prototype.constructor = ShaderGraphNode;
@@ -100,13 +102,20 @@ Object.assign(ShaderGraphNode.prototype, {
100102
return clone;
101103
},
102104

105+
_flushCachedPorts: function () {
106+
this._portCache = {};
107+
},
108+
109+
_getCachedPort: function (i) {
110+
var ioPort = this.graphData.ioPorts[i];
111+
if (!this._portCache[ioPort.name]) this._portCache[ioPort.name] = { isUniform: (ioPort.ptype === PORT_TYPE_IN || (ioPort.ptype === PORT_TYPE_CONST && ioPort.type === 'sampler2D')), nameId: ioPort.name + '_' + this.id, port: ioPort, index: i };
112+
return this._portCache[ioPort.name];
113+
},
114+
103115
updateUniforms: function (mat) {
104116
for (var n = 0; n < this.graphData.ioPorts.length; n++) {
105117
var ioPort = this.graphData.ioPorts[n];
106-
107-
// use port access acceleration optimization
108-
if (!this._portCache[ioPort.name]) this._portCache[ioPort.name] = { isUniform: (ioPort.ptype === PORT_TYPE_IN || (ioPort.ptype === PORT_TYPE_CONST && ioPort.type === 'sampler2D')), nameId: ioPort.name + '_' + this.id };
109-
var ioPortCached = this._portCache[ioPort.name];
118+
var ioPortCached = this._getCachedPort(n); // use port cache (minor optimization)
110119

111120
if (ioPortCached.isUniform) {
112121
switch (ioPort.type) {
@@ -225,6 +234,7 @@ Object.assign(ShaderGraphNode.prototype, {
225234
_addIoPort: function (type, name, value, ptype) {
226235
var ioPort = new Port(type, name, value, ptype);
227236

237+
this._portMap[name] = this.graphData.ioPorts.length;
228238
this.graphData.ioPorts.push(ioPort);
229239

230240
return ioPort;
@@ -330,8 +340,8 @@ Object.assign(ShaderGraphNode.prototype, {
330340
if (inNames && inNames[ioPort.name] !== undefined) {
331341
callString += inNames[ioPort.name] + ', ';
332342
} else {
333-
// this is tricky - use varname and unique (temp) id
334-
callString += ioPort.name + '_' + this.id + ', ';
343+
var ioPortCached = this._getCachedPort(i); // use port cache (minor optimization)
344+
callString += ioPortCached.nameId + ', ';
335345
}
336346
}
337347
}
@@ -356,9 +366,11 @@ Object.assign(ShaderGraphNode.prototype, {
356366
},
357367

358368
getIoPortByName: function (name) {
359-
return this.graphData.ioPorts.filter(function (ioPort) {
360-
return ioPort.name === name;
361-
})[0];
369+
return (this._portMap[name] === undefined) ? undefined : this.graphData.ioPorts[this._portMap[name]];
370+
},
371+
372+
getIoPortUniformName: function (name) {
373+
return (this._portMap[name] === undefined) ? undefined : this._getCachedPort(this._portMap[name]).nameId;
362374
},
363375

364376
_generateSubGraphFuncs: function (depGraphFuncs, depIoPortList) {
@@ -376,9 +388,9 @@ Object.assign(ShaderGraphNode.prototype, {
376388
if (subGraph.graphData.ioPorts) {
377389
for (var v = 0; v < subGraph.graphData.ioPorts.length; v++) {
378390
var ioPort = subGraph.graphData.ioPorts[v];
379-
if (ioPort.ptype === PORT_TYPE_IN || (ioPort.ptype === PORT_TYPE_CONST && ioPort.type === 'sampler2D') ) {
380-
var depIoPort = 'uniform ' + ioPort.type + ' ' + ioPort.name + '_' + subGraph.id + ';\n';
381-
391+
var ioPortCached = subGraph._getCachedPort(v); // use port cache (minor optimization)
392+
if (ioPortCached.isUniform) {
393+
var depIoPort = 'uniform ' + ioPort.type + ' ' + ioPortCached.nameId + ';\n';
382394
depIoPortList.push(depIoPort);
383395
}
384396
}
@@ -395,15 +407,17 @@ Object.assign(ShaderGraphNode.prototype, {
395407
var i;
396408
var ioPort;
397409
var generatedCodeString = '';
410+
398411
// run through inputs (and const sampler2Ds) to declare uniforms - (default) values are set elsewhere
399412
for (i = 0; i < this.graphData.ioPorts.length; i++) {
400-
var matId = '_' + this.id;
401413
ioPort = this.graphData.ioPorts[i];
402-
if (ioPort.ptype === PORT_TYPE_IN || (ioPort.ptype === PORT_TYPE_CONST && ioPort.type === 'sampler2D')) {
403-
generatedCodeString += 'uniform ' + ioPort.type + ' ' + ioPort.name + matId + ';\n';
414+
var ioPortCached = this._getCachedPort(i); // use port cache (minor optimization)
415+
416+
if (ioPortCached.isUniform) {
417+
generatedCodeString += 'uniform ' + ioPort.type + ' ' + ioPortCached.nameId + ';\n';
404418
}
405419
}
406-
// run through constants values are set here (except for textures - which have to be uniforms)
420+
// run through constants - values are set here (except for textures - which have to be uniforms)
407421
for (i = 0; i < this.graphData.ioPorts.length; i++) {
408422
ioPort = this.graphData.ioPorts[i];
409423
if (ioPort.ptype === PORT_TYPE_CONST && (ioPort.type !== 'sampler2D' )) {
@@ -435,17 +449,17 @@ Object.assign(ShaderGraphNode.prototype, {
435449
var funcString = '';
436450

437451
if ( func.endsWith('PS') ) {
438-
funcString += '#ifdef SHADERGRAPH_PIXELSHADER\n';
452+
funcString += '#ifdef SHADERGRAPH_PS\n';
439453
} else if ( func.endsWith('VS') ) {
440-
funcString += '#ifdef SHADERGRAPH_VERTEXSHADER\n';
454+
funcString += '#ifdef SHADERGRAPH_VS\n';
441455
}
442456

443457
funcString += depGraphFuncs[func] + '\n';
444458

445459
if ( func.endsWith('PS') ) {
446-
funcString += '#endif //SHADERGRAPH_PIXELSHADER\n';
460+
funcString += '#endif //SHADERGRAPH_PS\n';
447461
} else if ( func.endsWith('VS') ) {
448-
funcString += '#endif //SHADERGRAPH_VERTEXSHADER\n';
462+
funcString += '#endif //SHADERGRAPH_VS\n';
449463
}
450464

451465
depGraphList.push(funcString);
@@ -466,10 +480,10 @@ Object.assign(ShaderGraphNode.prototype, {
466480
var outNames = {};
467481

468482
for (var i = 0; i < this.graphData.ioPorts.length; i++) {
469-
var matId = '_' + this.id;
470483
var ioPort = this.graphData.ioPorts[i];
484+
var ioPortCached = this._getCachedPort(i); // use port cache (minor optimization)
471485
if (ioPort.ptype === PORT_TYPE_IN) {
472-
inNames[ioPort.name] = ioPort.name + matId;
486+
inNames[ioPort.name] = ioPortCached.nameId;
473487
}
474488
if (ioPort.ptype === PORT_TYPE_OUT || ioPort.ptype === PORT_TYPE_RET ) {
475489
generatedCodeString += ioPort.type + ' ' + ioPort.name + ';\n';
@@ -620,17 +634,17 @@ Object.assign(ShaderGraphNode.prototype, {
620634
var func = this.graphData.subGraphs[subGraphIndex].name;
621635

622636
if ( func.endsWith('PS') ) {
623-
generatedCodeString += '#ifdef SHADERGRAPH_PIXELSHADER\n';
637+
generatedCodeString += '#ifdef SHADERGRAPH_PS\n';
624638
} else if ( func.endsWith('VS') ) {
625-
generatedCodeString += '#ifdef SHADERGRAPH_VERTEXSHADER\n';
639+
generatedCodeString += '#ifdef SHADERGRAPH_VS\n';
626640
}
627641

628642
generatedCodeString += this.graphData.subGraphs[subGraphIndex]._generateSubGraphCall(dstTmpVarMap[subGraphIndex], srcTmpVarMap[subGraphIndex]);
629643

630644
if ( func.endsWith('PS') ) {
631-
generatedCodeString += '#endif //SHADERGRAPH_PIXELSHADER\n';
645+
generatedCodeString += '#endif //SHADERGRAPH_PS\n';
632646
} else if ( func.endsWith('VS') ) {
633-
generatedCodeString += '#endif //SHADERGRAPH_VERTEXSHADER\n';
647+
generatedCodeString += '#endif //SHADERGRAPH_VS\n';
634648
}
635649
}
636650
}

src/scene/materials/standard-node-material.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,10 @@ Object.assign(StandardNodeMaterial.prototype, {
6060

6161
if (this._shaderGraphChunk) {
6262
var rootShaderGraph = this._shaderGraphChunk;
63-
64-
if (!rootShaderGraph._portNameCache) rootShaderGraph._portNameCache = {};
65-
if (!rootShaderGraph._portNameCache[name]) rootShaderGraph._portNameCache[name] = name + '_' + rootShaderGraph.id;
66-
67-
var portName = rootShaderGraph._portNameCache[name];
68-
69-
StandardMaterial.prototype.setParameter.call(this, portName, data);
63+
var portName = rootShaderGraph.getIoPortUniformName(name);
64+
if (portName) {
65+
StandardMaterial.prototype.setParameter.call(this, portName, data);
66+
}
7067
}
7168
},
7269

0 commit comments

Comments
 (0)