forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlslShaderGenerator.cpp
799 lines (718 loc) · 39 KB
/
GlslShaderGenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXGenGlsl/GlslShaderGenerator.h>
#include <MaterialXGenGlsl/GlslSyntax.h>
#include <MaterialXGenGlsl/Nodes/PositionNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/NormalNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/TangentNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/BitangentNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/TexCoordNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/GeomColorNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/FrameNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/TimeNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/ViewDirectionNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/SurfaceNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/SurfaceShaderNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/LightNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/LightCompoundNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/LightShaderNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/HeightToNormalNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/LightSamplerNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/NumLightsNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/TransformVectorNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/TransformPointNodeGlsl.h>
#include <MaterialXGenGlsl/Nodes/TransformNormalNodeGlsl.h>
#include <MaterialXGenShader/Nodes/HwSourceCodeNode.h>
#include <MaterialXGenShader/Nodes/SwizzleNode.h>
#include <MaterialXGenShader/Nodes/ConvertNode.h>
#include <MaterialXGenShader/Nodes/CombineNode.h>
#include <MaterialXGenShader/Nodes/SwitchNode.h>
#include <MaterialXGenShader/Nodes/IfNode.h>
#include <MaterialXGenShader/Nodes/BlurNode.h>
#include <MaterialXGenShader/Nodes/HwImageNode.h>
namespace MaterialX
{
const string GlslShaderGenerator::LANGUAGE = "genglsl";
const string GlslShaderGenerator::TARGET = "glsl400";
const string GlslShaderGenerator::VERSION = "400";
//
// GlslShaderGenerator methods
//
GlslShaderGenerator::GlslShaderGenerator() :
HwShaderGenerator(GlslSyntax::create())
{
//
// Register all custom node implementation classes
//
// <!-- <if*> -->
static const string SEPARATOR = "_";
static const string INT_SEPARATOR = "I_";
static const string BOOL_SEPARATOR = "B_";
static const StringVec IMPL_PREFIXES = { "IM_ifgreater_", "IM_ifgreatereq_", "IM_ifequal_" };
static const vector<CreatorFunction<ShaderNodeImpl>> IMPL_CREATE_FUNCTIONS =
{ IfGreaterNode::create, IfGreaterEqNode::create, IfEqualNode::create };
static const vector<bool> IMPL_HAS_INTVERSION = { true, true, true };
static const vector<bool> IMPL_HAS_BOOLVERSION = { false, false, true };
static const StringVec IMPL_TYPES = { "float", "color2", "color3", "color4", "vector2", "vector3", "vector4" };
for (size_t i=0; i<IMPL_PREFIXES.size(); i++)
{
const string& implPrefix = IMPL_PREFIXES[i];
for (const string& implType : IMPL_TYPES)
{
const string implRoot = implPrefix + implType;
registerImplementation(implRoot + SEPARATOR + GlslShaderGenerator::LANGUAGE, IMPL_CREATE_FUNCTIONS[i]);
if (IMPL_HAS_INTVERSION[i])
{
registerImplementation(implRoot + INT_SEPARATOR + GlslShaderGenerator::LANGUAGE, IMPL_CREATE_FUNCTIONS[i]);
}
if (IMPL_HAS_BOOLVERSION[i])
{
registerImplementation(implRoot + BOOL_SEPARATOR + GlslShaderGenerator::LANGUAGE, IMPL_CREATE_FUNCTIONS[i]);
}
}
}
// <!-- <switch> -->
// <!-- 'which' type : float -->
registerImplementation("IM_switch_float_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_color2_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_color3_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_color4_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_vector2_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_vector3_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_vector4_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
// <!-- 'which' type : integer -->
registerImplementation("IM_switch_floatI_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_color2I_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_color3I_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_color4I_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_vector2I_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_vector3I_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_vector4I_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
// <!-- 'which' type : boolean -->
registerImplementation("IM_switch_floatB_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_color2B_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_color3B_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_color4B_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_vector2B_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_vector3B_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
registerImplementation("IM_switch_vector4B_" + GlslShaderGenerator::LANGUAGE, SwitchNode::create);
// <!-- <swizzle> -->
// <!-- from type : float -->
registerImplementation("IM_swizzle_float_color2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_float_color3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_float_color4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_float_vector2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_float_vector3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_float_vector4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
// <!-- from type : color2 -->
registerImplementation("IM_swizzle_color2_float_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color2_color2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color2_color3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color2_color4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color2_vector2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color2_vector3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color2_vector4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
// <!-- from type : color3 -->
registerImplementation("IM_swizzle_color3_float_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color3_color2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color3_color3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color3_color4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color3_vector2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color3_vector3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color3_vector4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
// <!-- from type : color4 -->
registerImplementation("IM_swizzle_color4_float_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color4_color2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color4_color3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color4_color4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color4_vector2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color4_vector3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_color4_vector4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
// <!-- from type : vector2 -->
registerImplementation("IM_swizzle_vector2_float_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector2_color2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector2_color3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector2_color4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector2_vector2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector2_vector3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector2_vector4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
// <!-- from type : vector3 -->
registerImplementation("IM_swizzle_vector3_float_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector3_color2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector3_color3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector3_color4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector3_vector2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector3_vector3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector3_vector4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
// <!-- from type : vector4 -->
registerImplementation("IM_swizzle_vector4_float_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector4_color2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector4_color3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector4_color4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector4_vector2_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector4_vector3_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
registerImplementation("IM_swizzle_vector4_vector4_" + GlslShaderGenerator::LANGUAGE, SwizzleNode::create);
// <!-- <convert> -->
registerImplementation("IM_convert_float_color2_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_float_color3_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_float_color4_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_float_vector2_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_float_vector3_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_float_vector4_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_vector2_color2_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_vector2_vector3_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_vector3_vector2_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_vector3_color3_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_vector3_vector4_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_vector4_vector3_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_vector4_color4_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_color2_vector2_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_color3_vector3_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_color4_vector4_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_color3_color4_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_color4_color3_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_boolean_float_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
registerImplementation("IM_convert_integer_float_" + GlslShaderGenerator::LANGUAGE, ConvertNode::create);
// <!-- <combine> -->
registerImplementation("IM_combine2_color2_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
registerImplementation("IM_combine2_vector2_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
registerImplementation("IM_combine2_color4CF_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
registerImplementation("IM_combine2_vector4VF_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
registerImplementation("IM_combine2_color4CC_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
registerImplementation("IM_combine2_vector4VV_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
registerImplementation("IM_combine3_color3_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
registerImplementation("IM_combine3_vector3_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
registerImplementation("IM_combine4_color4_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
registerImplementation("IM_combine4_vector4_" + GlslShaderGenerator::LANGUAGE, CombineNode::create);
// <!-- <position> -->
registerImplementation("IM_position_vector3_" + GlslShaderGenerator::LANGUAGE, PositionNodeGlsl::create);
// <!-- <normal> -->
registerImplementation("IM_normal_vector3_" + GlslShaderGenerator::LANGUAGE, NormalNodeGlsl::create);
// <!-- <tangent> -->
registerImplementation("IM_tangent_vector3_" + GlslShaderGenerator::LANGUAGE, TangentNodeGlsl::create);
// <!-- <bitangent> -->
registerImplementation("IM_bitangent_vector3_" + GlslShaderGenerator::LANGUAGE, BitangentNodeGlsl::create);
// <!-- <texcoord> -->
registerImplementation("IM_texcoord_vector2_" + GlslShaderGenerator::LANGUAGE, TexCoordNodeGlsl::create);
registerImplementation("IM_texcoord_vector3_" + GlslShaderGenerator::LANGUAGE, TexCoordNodeGlsl::create);
// <!-- <geomcolor> -->
registerImplementation("IM_geomcolor_float_" + GlslShaderGenerator::LANGUAGE, GeomColorNodeGlsl::create);
registerImplementation("IM_geomcolor_color2_" + GlslShaderGenerator::LANGUAGE, GeomColorNodeGlsl::create);
registerImplementation("IM_geomcolor_color3_" + GlslShaderGenerator::LANGUAGE, GeomColorNodeGlsl::create);
registerImplementation("IM_geomcolor_color4_" + GlslShaderGenerator::LANGUAGE, GeomColorNodeGlsl::create);
// <!-- <geompropvalue> -->
registerImplementation("IM_geompropvalue_integer_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
registerImplementation("IM_geompropvalue_boolean_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
registerImplementation("IM_geompropvalue_string_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
registerImplementation("IM_geompropvalue_float_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
registerImplementation("IM_geompropvalue_color2_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
registerImplementation("IM_geompropvalue_color3_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
registerImplementation("IM_geompropvalue_color4_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
registerImplementation("IM_geompropvalue_vector2_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
registerImplementation("IM_geompropvalue_vector3_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
registerImplementation("IM_geompropvalue_vector4_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create);
// <!-- <frame> -->
registerImplementation("IM_frame_float_" + GlslShaderGenerator::LANGUAGE, FrameNodeGlsl::create);
// <!-- <time> -->
registerImplementation("IM_time_float_" + GlslShaderGenerator::LANGUAGE, TimeNodeGlsl::create);
// <!-- <viewdirection> -->
registerImplementation("IM_viewdirection_vector3_" + GlslShaderGenerator::LANGUAGE, ViewDirectionNodeGlsl::create);
// <!-- <surface> -->
registerImplementation("IM_surface_" + GlslShaderGenerator::LANGUAGE, SurfaceNodeGlsl::create);
// <!-- <light> -->
registerImplementation("IM_light_" + GlslShaderGenerator::LANGUAGE, LightNodeGlsl::create);
// <!-- <point_light> -->
registerImplementation("IM_point_light_" + GlslShaderGenerator::LANGUAGE, LightShaderNodeGlsl::create);
// <!-- <directional_light> -->
registerImplementation("IM_directional_light_" + GlslShaderGenerator::LANGUAGE, LightShaderNodeGlsl::create);
// <!-- <spot_light> -->
registerImplementation("IM_spot_light_" + GlslShaderGenerator::LANGUAGE, LightShaderNodeGlsl::create);
// <!-- <heighttonormal> -->
registerImplementation("IM_heighttonormal_vector3_" + GlslShaderGenerator::LANGUAGE, HeightToNormalNodeGlsl::create);
// <!-- <blur> -->
registerImplementation("IM_blur_float_" + GlslShaderGenerator::LANGUAGE, BlurNode::create);
registerImplementation("IM_blur_color2_" + GlslShaderGenerator::LANGUAGE, BlurNode::create);
registerImplementation("IM_blur_color3_" + GlslShaderGenerator::LANGUAGE, BlurNode::create);
registerImplementation("IM_blur_color4_" + GlslShaderGenerator::LANGUAGE, BlurNode::create);
registerImplementation("IM_blur_vector2_" + GlslShaderGenerator::LANGUAGE, BlurNode::create);
registerImplementation("IM_blur_vector3_" + GlslShaderGenerator::LANGUAGE, BlurNode::create);
registerImplementation("IM_blur_vector4_" + GlslShaderGenerator::LANGUAGE, BlurNode::create);
// <!-- <ND_transformpoint> ->
registerImplementation("IM_transformpoint_vector3_" + GlslShaderGenerator::LANGUAGE, TransformPointNodeGlsl::create);
// <!-- <ND_transformvector> ->
registerImplementation("IM_transformvector_vector3_" + GlslShaderGenerator::LANGUAGE, TransformVectorNodeGlsl::create);
// <!-- <ND_transformnormal> ->
registerImplementation("IM_transformnormal_vector3_" + GlslShaderGenerator::LANGUAGE, TransformNormalNodeGlsl::create);
// <!-- <image> -->
registerImplementation("IM_image_float_" + GlslShaderGenerator::LANGUAGE, HwImageNode::create);
registerImplementation("IM_image_color2_" + GlslShaderGenerator::LANGUAGE, HwImageNode::create);
registerImplementation("IM_image_color3_" + GlslShaderGenerator::LANGUAGE, HwImageNode::create);
registerImplementation("IM_image_color4_" + GlslShaderGenerator::LANGUAGE, HwImageNode::create);
registerImplementation("IM_image_vector2_" + GlslShaderGenerator::LANGUAGE, HwImageNode::create);
registerImplementation("IM_image_vector3_" + GlslShaderGenerator::LANGUAGE, HwImageNode::create);
registerImplementation("IM_image_vector4_" + GlslShaderGenerator::LANGUAGE, HwImageNode::create);
_lightSamplingNodes.push_back(ShaderNode::create(nullptr, "numActiveLightSources", NumLightsNodeGlsl::create()));
_lightSamplingNodes.push_back(ShaderNode::create(nullptr, "sampleLightSource", LightSamplerNodeGlsl::create()));
}
ShaderPtr GlslShaderGenerator::generate(const string& name, ElementPtr element, GenContext& context) const
{
ShaderPtr shader = createShader(name, element, context);
// Turn on fixed float formatting to make sure float values are
// emitted with a decimal point and not as integers, and to avoid
// any scientific notation which isn't supported by all OpenGL targets.
ScopedFloatFormatting fmt(Value::FloatFormatFixed);
// Emit code for vertex shader stage
ShaderStage& vs = shader->getStage(Stage::VERTEX);
emitVertexStage(shader->getGraph(), context, vs);
replaceTokens(_tokenSubstitutions, vs);
// Emit code for pixel shader stage
ShaderStage& ps = shader->getStage(Stage::PIXEL);
emitPixelStage(shader->getGraph(), context, ps);
replaceTokens(_tokenSubstitutions, ps);
return shader;
}
void GlslShaderGenerator::emitVertexStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const
{
// Add version directive
emitLine("#version " + getVersion(), stage, false);
emitLineBreak(stage);
// Add all constants
const VariableBlock& constants = stage.getConstantBlock();
if (!constants.empty())
{
emitVariableDeclarations(constants, _syntax->getConstantQualifier(), SEMICOLON, context, stage);
emitLineBreak(stage);
}
// Add all uniforms
for (const auto& it : stage.getUniformBlocks())
{
const VariableBlock& uniforms = *it.second;
if (!uniforms.empty())
{
emitComment("Uniform block: " + uniforms.getName(), stage);
emitVariableDeclarations(uniforms, _syntax->getUniformQualifier(), SEMICOLON, context, stage);
emitLineBreak(stage);
}
}
// Add vertex inputs
const VariableBlock& vertexInputs = stage.getInputBlock(HW::VERTEX_INPUTS);
if (!vertexInputs.empty())
{
emitComment("Inputs block: " + vertexInputs.getName(), stage);
emitVariableDeclarations(vertexInputs, _syntax->getInputQualifier(), SEMICOLON, context, stage, false);
emitLineBreak(stage);
}
// Add vertex data outputs block
const VariableBlock& vertexData = stage.getOutputBlock(HW::VERTEX_DATA);
if (!vertexData.empty())
{
emitLine("out " + vertexData.getName(), stage, false);
emitScopeBegin(stage);
emitVariableDeclarations(vertexData, EMPTY_STRING, SEMICOLON, context, stage, false);
emitScopeEnd(stage, false, false);
emitString(" " + vertexData.getInstance() + SEMICOLON, stage);
emitLineBreak(stage);
emitLineBreak(stage);
}
emitFunctionDefinitions(graph, context, stage);
// Add main function
setFunctionName("main", stage);
emitLine("void main()", stage, false);
emitScopeBegin(stage);
emitLine("vec4 hPositionWorld = " + HW::T_WORLD_MATRIX + " * vec4(" + HW::T_IN_POSITION + ", 1.0)", stage);
emitLine("gl_Position = " + HW::T_VIEW_PROJECTION_MATRIX + " * hPositionWorld", stage);
emitFunctionCalls(graph, context, stage);
emitScopeEnd(stage);
emitLineBreak(stage);
}
void GlslShaderGenerator::emitSpecularEnvironment(GenContext& context, ShaderStage& stage) const
{
int specularMethod = context.getOptions().hwSpecularEnvironmentMethod;
if (specularMethod == SPECULAR_ENVIRONMENT_FIS)
{
emitInclude("pbrlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_environment_fis.glsl", context, stage);
}
else if (specularMethod == SPECULAR_ENVIRONMENT_PREFILTER)
{
emitInclude("pbrlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_environment_prefilter.glsl", context, stage);
}
else if (specularMethod == SPECULAR_ENVIRONMENT_NONE)
{
emitInclude("pbrlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_environment_none.glsl", context, stage);
}
else
{
throw ExceptionShaderGenError("Invalid hardware specular environment method specified: '" + std::to_string(specularMethod) + "'");
}
emitLineBreak(stage);
}
void GlslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const
{
// Add version directive
emitLine("#version " + getVersion(), stage, false);
emitLineBreak(stage);
// Add global constants and type definitions
emitInclude("pbrlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_defines.glsl", context, stage);
const unsigned int maxLights = std::max(1u, context.getOptions().hwMaxActiveLightSources);
emitLine("#define MAX_LIGHT_SOURCES " + std::to_string(maxLights), stage, false);
emitLineBreak(stage);
emitTypeDefinitions(context, stage);
// Add all constants
const VariableBlock& constants = stage.getConstantBlock();
if (!constants.empty())
{
emitVariableDeclarations(constants, _syntax->getConstantQualifier(), SEMICOLON, context, stage);
emitLineBreak(stage);
}
// Add all uniforms
for (const auto& it : stage.getUniformBlocks())
{
const VariableBlock& uniforms = *it.second;
// Skip light uniforms as they are handled separately
if (!uniforms.empty() && uniforms.getName() != HW::LIGHT_DATA)
{
emitComment("Uniform block: " + uniforms.getName(), stage);
emitVariableDeclarations(uniforms, _syntax->getUniformQualifier(), SEMICOLON, context, stage);
emitLineBreak(stage);
}
}
bool lighting = graph.hasClassification(ShaderNode::Classification::SHADER|ShaderNode::Classification::SURFACE) ||
graph.hasClassification(ShaderNode::Classification::BSDF);
// Add light data block if needed
if (lighting)
{
const VariableBlock& lightData = stage.getUniformBlock(HW::LIGHT_DATA);
emitLine("struct " + lightData.getName(), stage, false);
emitScopeBegin(stage);
emitVariableDeclarations(lightData, EMPTY_STRING, SEMICOLON, context, stage, false);
emitScopeEnd(stage, true);
emitLineBreak(stage);
emitLine("uniform " + lightData.getName() + " " + lightData.getInstance() + "[MAX_LIGHT_SOURCES]", stage);
emitLineBreak(stage);
}
// Add vertex data inputs block
const VariableBlock& vertexData = stage.getInputBlock(HW::VERTEX_DATA);
if (!vertexData.empty())
{
emitLine("in " + vertexData.getName(), stage, false);
emitScopeBegin(stage);
emitVariableDeclarations(vertexData, EMPTY_STRING, SEMICOLON, context, stage, false);
emitScopeEnd(stage, false, false);
emitString(" " + vertexData.getInstance() + SEMICOLON, stage);
emitLineBreak(stage);
emitLineBreak(stage);
}
// Add the pixel shader output. This needs to be a vec4 for rendering
// and upstream connection will be converted to vec4 if needed in emitFinalOutput()
emitComment("Pixel shader outputs", stage);
const VariableBlock& outputs = stage.getOutputBlock(HW::PIXEL_OUTPUTS);
emitVariableDeclarations(outputs, _syntax->getOutputQualifier(), SEMICOLON, context, stage, false);
emitLineBreak(stage);
// Emit common math functions
emitInclude("pbrlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_math.glsl", context, stage);
emitLineBreak(stage);
// Emit lighting and shadowing functions
if (lighting)
{
emitSpecularEnvironment(context, stage);
if (context.getOptions().hwShadowMap)
{
emitInclude("pbrlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_shadow.glsl", context, stage);
}
}
if (context.getOptions().hwWriteDepthMoments)
{
emitInclude("pbrlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_shadow.glsl", context, stage);
}
// Emit sampling code if needed
if (graph.hasClassification(ShaderNode::Classification::CONVOLUTION2D))
{
// Emit sampling functions
emitInclude("stdlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_sampling.glsl", context, stage);
emitLineBreak(stage);
}
// Set the include file to use for uv transformations,
// depending on the vertical flip flag.
if (context.getOptions().fileTextureVerticalFlip)
{
_tokenSubstitutions[ShaderGenerator::T_FILE_TRANSFORM_UV] = "stdlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_transform_uv_vflip.glsl";
}
else
{
_tokenSubstitutions[ShaderGenerator::T_FILE_TRANSFORM_UV] = "stdlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_transform_uv.glsl";
}
// Emit uv transform code globally if needed.
if (context.getOptions().hwAmbientOcclusion)
{
emitInclude(ShaderGenerator::T_FILE_TRANSFORM_UV, context, stage);
}
// Add all functions for node implementations
emitFunctionDefinitions(graph, context, stage);
const ShaderGraphOutputSocket* outputSocket = graph.getOutputSocket();
// Add main function
setFunctionName("main", stage);
emitLine("void main()", stage, false);
emitScopeBegin(stage);
if (graph.hasClassification(ShaderNode::Classification::CLOSURE))
{
// Handle the case where the graph is a direct closure.
// We don't support rendering closures without attaching
// to a surface shader, so just output black.
emitLine(outputSocket->getVariable() + " = vec4(0.0, 0.0, 0.0, 1.0)", stage);
}
else if (context.getOptions().hwWriteDepthMoments)
{
emitLine(outputSocket->getVariable() + " = vec4(mx_compute_depth_moments(), 0.0, 1.0)", stage);
}
else
{
// Add all function calls
emitFunctionCalls(graph, context, stage);
// Emit final output
const ShaderOutput* outputConnection = outputSocket->getConnection();
if (outputConnection)
{
string finalOutput = outputConnection->getVariable();
const string& channels = outputSocket->getChannels();
if (!channels.empty())
{
finalOutput = _syntax->getSwizzledVariable(finalOutput, outputConnection->getType(), channels, outputSocket->getType());
}
if (graph.hasClassification(ShaderNode::Classification::SURFACE))
{
if (context.getOptions().hwTransparency)
{
emitLine("float outAlpha = clamp(1.0 - dot(" + finalOutput + ".transparency, vec3(0.3333)), 0.0, 1.0)", stage);
emitLine(outputSocket->getVariable() + " = vec4(" + finalOutput + ".color, outAlpha)", stage);
}
else
{
emitLine(outputSocket->getVariable() + " = vec4(" + finalOutput + ".color, 1.0)", stage);
}
}
else
{
if (!outputSocket->getType()->isFloat4())
{
toVec4(outputSocket->getType(), finalOutput);
}
emitLine(outputSocket->getVariable() + " = " + finalOutput, stage);
}
}
else
{
string outputValue = outputSocket->getValue() ? _syntax->getValue(outputSocket->getType(), *outputSocket->getValue()) : _syntax->getDefaultValue(outputSocket->getType());
if (!outputSocket->getType()->isFloat4())
{
string finalOutput = outputSocket->getVariable() + "_tmp";
emitLine(_syntax->getTypeName(outputSocket->getType()) + " " + finalOutput + " = " + outputValue, stage);
toVec4(outputSocket->getType(), finalOutput);
emitLine(outputSocket->getVariable() + " = " + finalOutput, stage);
}
else
{
emitLine(outputSocket->getVariable() + " = " + outputValue, stage);
}
}
}
// End main function
emitScopeEnd(stage);
emitLineBreak(stage);
}
void GlslShaderGenerator::emitFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const
{
BEGIN_SHADER_STAGE(stage, Stage::PIXEL)
// For surface shaders we need light shaders
if (graph.hasClassification(ShaderNode::Classification::SHADER | ShaderNode::Classification::SURFACE))
{
// Emit functions for all bound light shaders
HwLightShadersPtr lightShaders = context.getUserData<HwLightShaders>(HW::USER_DATA_LIGHT_SHADERS);
if (lightShaders)
{
for (const auto& it : lightShaders->get())
{
emitFunctionDefinition(*it.second, context, stage);
}
}
// Emit functions for light sampling
for (const auto& it : _lightSamplingNodes)
{
emitFunctionDefinition(*it, context, stage);
}
}
END_SHADER_STAGE(stage, Stage::PIXEL)
// Call parent to emit all other functions
HwShaderGenerator::emitFunctionDefinitions(graph, context, stage);
}
void GlslShaderGenerator::emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const
{
BEGIN_SHADER_STAGE(stage, Stage::VERTEX)
// For vertex stage just emit all function calls in order
// and ignore conditional scope.
for (const ShaderNode* node : graph.getNodes())
{
emitFunctionCall(*node, context, stage, false);
}
END_SHADER_STAGE(stage, Stage::VERTEX)
BEGIN_SHADER_STAGE(stage, Stage::PIXEL)
// For pixel stage surface shaders need special handling
if (graph.hasClassification(ShaderNode::Classification::SHADER | ShaderNode::Classification::SURFACE))
{
// Handle all texturing nodes. These are inputs to any
// closure/shader nodes and need to be emitted first.
emitTextureNodes(graph, context, stage);
// Emit function calls for all surface shader nodes
for (const ShaderNode* node : graph.getNodes())
{
if (node->hasClassification(ShaderNode::Classification::SHADER | ShaderNode::Classification::SURFACE))
{
emitFunctionCall(*node, context, stage, false);
}
}
}
else
{
// No surface shader or closure graph,
// so generate a normel function call.
HwShaderGenerator::emitFunctionCalls(graph, context, stage);
}
END_SHADER_STAGE(stage, Stage::PIXEL)
}
void GlslShaderGenerator::toVec4(const TypeDesc* type, string& variable)
{
if (type->isFloat3())
{
variable = "vec4(" + variable + ", 1.0)";
}
else if (type->isFloat2())
{
variable = "vec4(" + variable + ", 0.0, 1.0)";
}
else if (type == Type::FLOAT || type == Type::INTEGER)
{
variable = "vec4(" + variable + ", " + variable + ", " + variable + ", 1.0)";
}
else if (type == Type::BSDF || type == Type::EDF)
{
variable = "vec4(" + variable + ", 1.0)";
}
else
{
// Can't understand other types. Just return black.
variable = "vec4(0.0, 0.0, 0.0, 1.0)";
}
}
void GlslShaderGenerator::emitVariableDeclaration(const ShaderPort* variable, const string& qualifier,
GenContext&, ShaderStage& stage,
bool assignValue) const
{
// A file texture input needs special handling on GLSL
if (variable->getType() == Type::FILENAME)
{
// Samplers must always be uniforms
emitString("uniform sampler2D " + variable->getVariable(), stage);
}
else
{
string str = qualifier.empty() ? EMPTY_STRING : qualifier + " ";
str += _syntax->getTypeName(variable->getType()) + " " + variable->getVariable();
// If an array we need an array qualifier (suffix) for the variable name
if (variable->getType()->isArray() && variable->getValue())
{
str += _syntax->getArraySuffix(variable->getType(), *variable->getValue());
}
if (!variable->getSemantic().empty())
{
str += " : " + variable->getSemantic();
}
if (assignValue)
{
const string valueStr = (variable->getValue() ?
_syntax->getValue(variable->getType(), *variable->getValue(), true) :
_syntax->getDefaultValue(variable->getType(), true));
str += valueStr.empty() ? EMPTY_STRING : " = " + valueStr;
}
emitString(str, stage);
}
}
ShaderNodeImplPtr GlslShaderGenerator::createCompoundImplementation(const NodeGraph& impl) const
{
NodeDefPtr nodeDef = impl.getNodeDef();
if (!nodeDef)
{
throw ExceptionShaderGenError("Error creating compound implementation. Given nodegraph '" + impl.getName() + "' has no nodedef set");
}
if (TypeDesc::get(nodeDef->getType()) == Type::LIGHTSHADER)
{
return LightCompoundNodeGlsl::create();
}
return HwShaderGenerator::createCompoundImplementation(impl);
}
bool GlslShaderGenerator::remapEnumeration(const ValueElement& input, const string& value, std::pair<const TypeDesc*, ValuePtr>& result) const
{
// Early out if not an enum input.
const string& enumNames = input.getAttribute(ValueElement::ENUM_ATTRIBUTE);
if (enumNames.empty())
{
return false;
}
// Don't convert already supported types
// or filenames and arrays.
const TypeDesc* type = TypeDesc::get(input.getType());
if (_syntax->typeSupported(type) ||
type == Type::FILENAME || type->isArray())
{
return false;
}
// For GLSL we always convert to integer,
// with the integer value being an index into the enumeration.
result.first = Type::INTEGER;
result.second = nullptr;
// Try remapping to an enum value.
if (!value.empty())
{
StringVec valueElemEnumsVec = splitString(enumNames, ",");
auto pos = std::find(valueElemEnumsVec.begin(), valueElemEnumsVec.end(), value);
if (pos == valueElemEnumsVec.end())
{
throw ExceptionShaderGenError("Given value '" + value + "' is not a valid enum value for input '" + input.getNamePath() + "'");
}
const int index = static_cast<int>(std::distance(valueElemEnumsVec.begin(), pos));
result.second = Value::createValue<int>(index);
}
return true;
}
const string GlslImplementation::SPACE = "space";
const string GlslImplementation::TO_SPACE = "tospace";
const string GlslImplementation::FROM_SPACE = "fromspace";
const string GlslImplementation::WORLD = "world";
const string GlslImplementation::OBJECT = "object";
const string GlslImplementation::MODEL = "model";
const string GlslImplementation::INDEX = "index";
const string GlslImplementation::GEOMPROP = "geomprop";
namespace
{
// List name of inputs that are not to be editable and
// published as shader uniforms in GLSL.
const std::set<string> IMMUTABLE_INPUTS =
{
// Geometric node inputs are immutable since a shader needs regeneration if they change.
"index", "space", "attrname"
};
}
const string& GlslImplementation::getLanguage() const
{
return GlslShaderGenerator::LANGUAGE;
}
const string& GlslImplementation::getTarget() const
{
return GlslShaderGenerator::TARGET;
}
bool GlslImplementation::isEditable(const ShaderInput& input) const
{
return IMMUTABLE_INPUTS.count(input.getName()) == 0;
}
}