@@ -52,7 +52,7 @@ var counter = 0;
52
52
* @class
53
53
* @name ShaderGraphNode
54
54
* @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
56
56
* @param {string } declCodeString - shader declarations used by the shader function
57
57
*/
58
58
var ShaderGraphNode = function ( funcCodeString , declCodeString ) {
@@ -64,6 +64,11 @@ var ShaderGraphNode = function (funcCodeString, declCodeString) {
64
64
this . _codeStringAssetReferences = { } ;
65
65
this . _subGraphAssetReferences = [ ] ;
66
66
67
+ // port map and cache (optimization)
68
+ this . _portMap = { } ;
69
+ this . _portCache = { } ;
70
+
71
+ // graph
67
72
this . graphData = { } ;
68
73
69
74
this . graphData . ioPorts = [ ] ; // input, output or constant variables
@@ -76,9 +81,6 @@ var ShaderGraphNode = function (funcCodeString, declCodeString) {
76
81
this . graphData . subGraphs = [ ] ;
77
82
this . graphData . connections = [ ] ;
78
83
}
79
-
80
- // cache for port access acceleration optimization
81
- this . _portCache = { } ;
82
84
} ;
83
85
84
86
ShaderGraphNode . prototype . constructor = ShaderGraphNode ;
@@ -100,13 +102,20 @@ Object.assign(ShaderGraphNode.prototype, {
100
102
return clone ;
101
103
} ,
102
104
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
+
103
115
updateUniforms : function ( mat ) {
104
116
for ( var n = 0 ; n < this . graphData . ioPorts . length ; n ++ ) {
105
117
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)
110
119
111
120
if ( ioPortCached . isUniform ) {
112
121
switch ( ioPort . type ) {
@@ -225,6 +234,7 @@ Object.assign(ShaderGraphNode.prototype, {
225
234
_addIoPort : function ( type , name , value , ptype ) {
226
235
var ioPort = new Port ( type , name , value , ptype ) ;
227
236
237
+ this . _portMap [ name ] = this . graphData . ioPorts . length ;
228
238
this . graphData . ioPorts . push ( ioPort ) ;
229
239
230
240
return ioPort ;
@@ -330,8 +340,8 @@ Object.assign(ShaderGraphNode.prototype, {
330
340
if ( inNames && inNames [ ioPort . name ] !== undefined ) {
331
341
callString += inNames [ ioPort . name ] + ', ' ;
332
342
} 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 + ', ' ;
335
345
}
336
346
}
337
347
}
@@ -356,9 +366,11 @@ Object.assign(ShaderGraphNode.prototype, {
356
366
} ,
357
367
358
368
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 ;
362
374
} ,
363
375
364
376
_generateSubGraphFuncs : function ( depGraphFuncs , depIoPortList ) {
@@ -376,9 +388,9 @@ Object.assign(ShaderGraphNode.prototype, {
376
388
if ( subGraph . graphData . ioPorts ) {
377
389
for ( var v = 0 ; v < subGraph . graphData . ioPorts . length ; v ++ ) {
378
390
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' ;
382
394
depIoPortList . push ( depIoPort ) ;
383
395
}
384
396
}
@@ -395,15 +407,17 @@ Object.assign(ShaderGraphNode.prototype, {
395
407
var i ;
396
408
var ioPort ;
397
409
var generatedCodeString = '' ;
410
+
398
411
// run through inputs (and const sampler2Ds) to declare uniforms - (default) values are set elsewhere
399
412
for ( i = 0 ; i < this . graphData . ioPorts . length ; i ++ ) {
400
- var matId = '_' + this . id ;
401
413
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' ;
404
418
}
405
419
}
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)
407
421
for ( i = 0 ; i < this . graphData . ioPorts . length ; i ++ ) {
408
422
ioPort = this . graphData . ioPorts [ i ] ;
409
423
if ( ioPort . ptype === PORT_TYPE_CONST && ( ioPort . type !== 'sampler2D' ) ) {
@@ -435,17 +449,17 @@ Object.assign(ShaderGraphNode.prototype, {
435
449
var funcString = '' ;
436
450
437
451
if ( func . endsWith ( 'PS' ) ) {
438
- funcString += '#ifdef SHADERGRAPH_PIXELSHADER \n' ;
452
+ funcString += '#ifdef SHADERGRAPH_PS \n' ;
439
453
} else if ( func . endsWith ( 'VS' ) ) {
440
- funcString += '#ifdef SHADERGRAPH_VERTEXSHADER \n' ;
454
+ funcString += '#ifdef SHADERGRAPH_VS \n' ;
441
455
}
442
456
443
457
funcString += depGraphFuncs [ func ] + '\n' ;
444
458
445
459
if ( func . endsWith ( 'PS' ) ) {
446
- funcString += '#endif //SHADERGRAPH_PIXELSHADER \n' ;
460
+ funcString += '#endif //SHADERGRAPH_PS \n' ;
447
461
} else if ( func . endsWith ( 'VS' ) ) {
448
- funcString += '#endif //SHADERGRAPH_VERTEXSHADER \n' ;
462
+ funcString += '#endif //SHADERGRAPH_VS \n' ;
449
463
}
450
464
451
465
depGraphList . push ( funcString ) ;
@@ -466,10 +480,10 @@ Object.assign(ShaderGraphNode.prototype, {
466
480
var outNames = { } ;
467
481
468
482
for ( var i = 0 ; i < this . graphData . ioPorts . length ; i ++ ) {
469
- var matId = '_' + this . id ;
470
483
var ioPort = this . graphData . ioPorts [ i ] ;
484
+ var ioPortCached = this . _getCachedPort ( i ) ; // use port cache (minor optimization)
471
485
if ( ioPort . ptype === PORT_TYPE_IN ) {
472
- inNames [ ioPort . name ] = ioPort . name + matId ;
486
+ inNames [ ioPort . name ] = ioPortCached . nameId ;
473
487
}
474
488
if ( ioPort . ptype === PORT_TYPE_OUT || ioPort . ptype === PORT_TYPE_RET ) {
475
489
generatedCodeString += ioPort . type + ' ' + ioPort . name + ';\n' ;
@@ -620,17 +634,17 @@ Object.assign(ShaderGraphNode.prototype, {
620
634
var func = this . graphData . subGraphs [ subGraphIndex ] . name ;
621
635
622
636
if ( func . endsWith ( 'PS' ) ) {
623
- generatedCodeString += '#ifdef SHADERGRAPH_PIXELSHADER \n' ;
637
+ generatedCodeString += '#ifdef SHADERGRAPH_PS \n' ;
624
638
} else if ( func . endsWith ( 'VS' ) ) {
625
- generatedCodeString += '#ifdef SHADERGRAPH_VERTEXSHADER \n' ;
639
+ generatedCodeString += '#ifdef SHADERGRAPH_VS \n' ;
626
640
}
627
641
628
642
generatedCodeString += this . graphData . subGraphs [ subGraphIndex ] . _generateSubGraphCall ( dstTmpVarMap [ subGraphIndex ] , srcTmpVarMap [ subGraphIndex ] ) ;
629
643
630
644
if ( func . endsWith ( 'PS' ) ) {
631
- generatedCodeString += '#endif //SHADERGRAPH_PIXELSHADER \n' ;
645
+ generatedCodeString += '#endif //SHADERGRAPH_PS \n' ;
632
646
} else if ( func . endsWith ( 'VS' ) ) {
633
- generatedCodeString += '#endif //SHADERGRAPH_VERTEXSHADER \n' ;
647
+ generatedCodeString += '#endif //SHADERGRAPH_VS \n' ;
634
648
}
635
649
}
636
650
}
0 commit comments