forked from GarageGames/Torque3D
/
shaderFeatureGLSL.cpp
2861 lines (2386 loc) · 98 KB
/
shaderFeatureGLSL.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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "shaderGen/GLSL/shaderFeatureGLSL.h"
#include "shaderGen/langElement.h"
#include "shaderGen/shaderOp.h"
#include "shaderGen/shaderGenVars.h"
#include "gfx/gfxDevice.h"
#include "materials/matInstance.h"
#include "materials/processedMaterial.h"
#include "materials/materialFeatureTypes.h"
#include "core/util/autoPtr.h"
#include "lighting/advanced/advancedLightBinManager.h"
#include "ts/tsShape.h"
LangElement * ShaderFeatureGLSL::setupTexSpaceMat( Vector<ShaderComponent*> &, // componentList
Var **texSpaceMat )
{
Var *N = (Var*) LangElement::find( "normal" );
Var *B = (Var*) LangElement::find( "B" );
Var *T = (Var*) LangElement::find( "T" );
Var *tangentW = (Var*) LangElement::find( "tangentW" );
// setup matrix var
*texSpaceMat = new Var;
(*texSpaceMat)->setType( "float3x3" );
(*texSpaceMat)->setName( "objToTangentSpace" );
MultiLine * meta = new MultiLine;
meta->addStatement( new GenOp( " @ = float3x3(1,0,0, 0,1,0, 0,0,1);\r\n", new DecOp( *texSpaceMat ) ) );
// Protect against missing normal and tangent.
if ( !N || !T )
{
meta->addStatement( new GenOp( " tSetMatrixRow(@, 0, float3( 1, 0, 0 )); tSetMatrixRow(@, 1,float3( 0, 1, 0 )); tSetMatrixRow(@,2, float3( 0, 0, 1 ));\r\n",
*texSpaceMat, *texSpaceMat, *texSpaceMat ) );
return meta;
}
meta->addStatement( new GenOp( " tSetMatrixRow(@, 0, @);\r\n", *texSpaceMat, T ) );
if ( B )
meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, @);\r\n", *texSpaceMat, B ) );
else
{
if(dStricmp((char*)T->type, "vec4") == 0)
meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, cross( @, normalize(@) ) * @.w);\r\n", *texSpaceMat, T, N, T ) );
else
meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, cross( @, normalize(@) ));\r\n", *texSpaceMat, T, N ) );
}
meta->addStatement( new GenOp( " tSetMatrixRow(@, 2, normalize(@));\r\n", *texSpaceMat, N ) );
return meta;
}
LangElement* ShaderFeatureGLSL::assignColor( LangElement *elem,
Material::BlendOp blend,
LangElement *lerpElem,
ShaderFeature::OutputTarget outputTarget )
{
// search for color var
Var *color = (Var*) LangElement::find( getOutputTargetVarName(outputTarget) );
if ( !color )
{
// create color var
color = new Var;
color->setType( "vec4" );
color->setName( getOutputTargetVarName( outputTarget ) );
color->setStructName( "OUT" );
return new GenOp( "@ = @", color, elem );
}
LangElement *assign;
switch ( blend )
{
case Material::Add:
assign = new GenOp( "@ += @", color, elem );
break;
case Material::Sub:
assign = new GenOp( "@ -= @", color, elem );
break;
case Material::Mul:
assign = new GenOp( "@ *= @", color, elem );
break;
case Material::AddAlpha:
assign = new GenOp( "@ += @ * @.a", color, elem, elem );
break;
case Material::LerpAlpha:
if ( !lerpElem )
lerpElem = elem;
assign = new GenOp( "@.rgb = lerp( @.rgb, (@).rgb, (@).a )", color, color, elem, lerpElem );
break;
case Material::ToneMap:
assign = new GenOp( "@ = 1.0 - exp(-1.0 * @ * @)", color, color, elem );
break;
default:
AssertFatal(false, "Unrecognized color blendOp");
// Fallthru
case Material::None:
assign = new GenOp( "@ = @", color, elem );
break;
}
return assign;
}
LangElement *ShaderFeatureGLSL::expandNormalMap( LangElement *sampleNormalOp,
LangElement *normalDecl,
LangElement *normalVar,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
if ( fd.features.hasFeature( MFT_IsDXTnm, getProcessIndex() ) )
{
if ( fd.features[MFT_ImposterVert] )
{
// The imposter system uses object space normals and
// encodes them with the z axis in the alpha component.
meta->addStatement( new GenOp( " @ = float4( normalize( @.xyw * 2.0 - 1.0 ), 0.0 ); // Obj DXTnm\r\n", normalDecl, sampleNormalOp ) );
}
else
{
// DXT Swizzle trick
meta->addStatement( new GenOp( " @ = float4( @.ag * 2.0 - 1.0, 0.0, 0.0 ); // DXTnm\r\n", normalDecl, sampleNormalOp ) );
meta->addStatement( new GenOp( " @.z = sqrt( 1.0 - dot( @.xy, @.xy ) ); // DXTnm\r\n", normalVar, normalVar, normalVar ) );
}
}
else
{
meta->addStatement( new GenOp( " @ = @;\r\n", normalDecl, sampleNormalOp ) );
meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 1.0;\r\n", normalVar, normalVar ) );
}
return meta;
}
ShaderFeatureGLSL::ShaderFeatureGLSL()
{
output = NULL;
}
Var * ShaderFeatureGLSL::getVertTexCoord( const String &name )
{
Var *inTex = NULL;
for( U32 i=0; i<LangElement::elementList.size(); i++ )
{
if( !dStrcmp( (char*)LangElement::elementList[i]->name, name.c_str() ) )
{
inTex = dynamic_cast<Var*>( LangElement::elementList[i] );
if ( inTex )
{
// NOTE: This used to do this check...
//
// dStrcmp( (char*)inTex->structName, "IN" )
//
// ... to ensure that the var was from the input
// vertex structure, but this kept some features
// ( ie. imposter vert ) from decoding their own
// coords for other features to use.
//
// If we run into issues with collisions between
// IN vars and local vars we may need to revise.
break;
}
}
}
return inTex;
}
Var* ShaderFeatureGLSL::getOutObjToTangentSpace( Vector<ShaderComponent*> &componentList,
MultiLine *meta,
const MaterialFeatureData &fd )
{
Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" );
if ( !outObjToTangentSpace )
meta->addStatement( setupTexSpaceMat( componentList, &outObjToTangentSpace ) );
return outObjToTangentSpace;
}
Var* ShaderFeatureGLSL::getOutWorldToTangent( Vector<ShaderComponent*> &componentList,
MultiLine *meta,
const MaterialFeatureData &fd )
{
Var *outWorldToTangent = (Var*)LangElement::find( "outWorldToTangent" );
if ( outWorldToTangent )
return outWorldToTangent;
Var *worldToTangent = (Var*)LangElement::find( "worldToTangent" );
if ( !worldToTangent )
{
Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
if(!fd.features[MFT_ParticleNormal])
{
// turn obj->tangent into world->tangent
worldToTangent = new Var;
worldToTangent->setType( "float3x3" );
worldToTangent->setName( "worldToTangent" );
LangElement *worldToTangentDecl = new DecOp( worldToTangent );
// Get the world->obj transform
Var *worldToObj = (Var*)LangElement::find( "worldToObj" );
if ( !worldToObj )
{
worldToObj = new Var;
worldToObj->setName( "worldToObj" );
if ( fd.features[MFT_UseInstancing] )
{
// We just use transpose to convert the 3x3 portion of
// the object transform to its inverse.
worldToObj->setType( "float3x3" );
Var *objTrans = getObjTrans( componentList, true, meta );
meta->addStatement( new GenOp( " @ = transpose( float3x3(@) ); // Instancing!\r\n", new DecOp( worldToObj ), objTrans ) );
}
else
{
worldToObj->setType( "float4x4" );
worldToObj->uniform = true;
worldToObj->constSortPos = cspPrimitive;
}
}
// assign world->tangent transform
meta->addStatement( new GenOp( " @ = tMul( @, float3x3(@) );\r\n", worldToTangentDecl, texSpaceMat, worldToObj ) );
}
else
{
// Assume particle normal generation has set this up in the proper space
worldToTangent = texSpaceMat;
}
}
// send transform to pixel shader
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
outWorldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
outWorldToTangent->setName( "outWorldToTangent" );
outWorldToTangent->setStructName( "OUT" );
outWorldToTangent->setType( "float3x3" );
meta->addStatement( new GenOp( " @ = @;\r\n", outWorldToTangent, worldToTangent ) );
return outWorldToTangent;
}
Var* ShaderFeatureGLSL::getOutViewToTangent( Vector<ShaderComponent*> &componentList,
MultiLine *meta,
const MaterialFeatureData &fd )
{
Var *outViewToTangent = (Var*)LangElement::find( "outViewToTangent" );
if ( outViewToTangent )
return outViewToTangent;
Var *viewToTangent = (Var*)LangElement::find( "viewToTangent" );
if ( !viewToTangent )
{
Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
if(!fd.features[MFT_ParticleNormal])
{
// turn obj->tangent into world->tangent
viewToTangent = new Var;
viewToTangent->setType( "float3x3" );
viewToTangent->setName( "viewToTangent" );
LangElement *viewToTangentDecl = new DecOp( viewToTangent );
// Get the view->obj transform
Var *viewToObj = getInvWorldView( componentList, fd.features[MFT_UseInstancing], meta );
// assign world->tangent transform
meta->addStatement( new GenOp( " @ = tMul( (@), float3x3(@) );\r\n", viewToTangentDecl, texSpaceMat, viewToObj ) );
}
else
{
// Assume particle normal generation has set this up in the proper space
viewToTangent = texSpaceMat;
}
}
// send transform to pixel shader
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
outViewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
outViewToTangent->setName( "outViewToTangent" );
outViewToTangent->setStructName( "OUT" );
outViewToTangent->setType( "float3x3" );
meta->addStatement( new GenOp( " @ = @;\r\n", outViewToTangent, viewToTangent ) );
return outViewToTangent;
}
Var* ShaderFeatureGLSL::getOutTexCoord( const char *name,
const char *type,
bool mapsToSampler,
bool useTexAnim,
MultiLine *meta,
Vector<ShaderComponent*> &componentList )
{
String outTexName = String::ToString( "out_%s", name );
Var *texCoord = (Var*)LangElement::find( outTexName );
if ( !texCoord )
{
Var *inTex = getVertTexCoord( name );
AssertFatal( inTex, "ShaderFeatureGLSL::getOutTexCoord - Unknown vertex input coord!" );
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
texCoord = connectComp->getElement( RT_TEXCOORD );
texCoord->setName( outTexName );
texCoord->setStructName( "OUT" );
texCoord->setType( type );
texCoord->mapsToSampler = mapsToSampler;
if( useTexAnim )
{
inTex->setType( "vec4" );
// create texture mat var
Var *texMat = new Var;
texMat->setType( "float4x4" );
texMat->setName( "texMat" );
texMat->uniform = true;
texMat->constSortPos = cspPass;
// Statement allows for casting of different types which
// eliminates vector truncation problems.
String statement = String::ToString( " @ = %s(tMul(@, @).xy);\r\n", type );
meta->addStatement( new GenOp( statement , texCoord, texMat, inTex ) );
}
else
{
// Statement allows for casting of different types which
// eliminates vector truncation problems.
String statement = String::ToString( " @ = %s(@);\r\n", type );
meta->addStatement( new GenOp( statement, texCoord, inTex ) );
}
}
AssertFatal( dStrcmp( type, (const char*)texCoord->type ) == 0,
"ShaderFeatureGLSL::getOutTexCoord - Type mismatch!" );
return texCoord;
}
Var* ShaderFeatureGLSL::getInTexCoord( const char *name,
const char *type,
bool mapsToSampler,
Vector<ShaderComponent*> &componentList )
{
Var* texCoord = (Var*)LangElement::find( name );
if ( !texCoord )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
texCoord = connectComp->getElement( RT_TEXCOORD );
texCoord->setName( name );
texCoord->setStructName( "IN" );
texCoord->setType( type );
texCoord->mapsToSampler = mapsToSampler;
}
AssertFatal( dStrcmp( type, (const char*)texCoord->type ) == 0,
"ShaderFeatureGLSL::getInTexCoord - Type mismatch!" );
return texCoord;
}
Var* ShaderFeatureGLSL::getInColor( const char *name,
const char *type,
Vector<ShaderComponent*> &componentList )
{
Var *inColor = (Var*)LangElement::find( name );
if ( !inColor )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
inColor = connectComp->getElement( RT_COLOR );
inColor->setName( name );
inColor->setStructName( "IN" );
inColor->setType( type );
}
AssertFatal( dStrcmp( type, (const char*)inColor->type ) == 0,
"ShaderFeatureGLSL::getInColor - Type mismatch!" );
return inColor;
}
Var* ShaderFeatureGLSL::addOutVpos( MultiLine *meta,
Vector<ShaderComponent*> &componentList )
{
/*
// Nothing to do if we're on SM 3.0... we use the real vpos.
if ( GFX->getPixelShaderVersion() >= 3.0f )
return NULL;
*/
// For SM 2.x we need to generate the vpos in the vertex shader
// and pass it as a texture coord to the pixel shader.
Var *outVpos = (Var*)LangElement::find( "outVpos" );
if ( !outVpos )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
outVpos = connectComp->getElement( RT_TEXCOORD );
outVpos->setName( "outVpos" );
outVpos->setStructName( "OUT" );
outVpos->setType( "vec4" );
outVpos->mapsToSampler = false;
Var *outPosition = (Var*) LangElement::find( "gl_Position" );
AssertFatal( outPosition, "ShaderFeatureGLSL::addOutVpos - Didn't find the output position." );
meta->addStatement( new GenOp( " @ = @;\r\n", outVpos, outPosition ) );
}
return outVpos;
}
Var* ShaderFeatureGLSL::getInVpos( MultiLine *meta,
Vector<ShaderComponent*> &componentList )
{
Var *inVpos = (Var*)LangElement::find( "vpos" );
if ( inVpos )
return inVpos;
ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
/*
if ( GFX->getPixelShaderVersion() >= 3.0f )
{
inVpos = connectComp->getElement( RT_VPOS );
inVpos->setName( "vpos" );
inVpos->setStructName( "IN" );
inVpos->setType( "vec2" );
return inVpos;
}
*/
inVpos = connectComp->getElement( RT_TEXCOORD );
inVpos->setName( "inVpos" );
inVpos->setStructName( "IN" );
inVpos->setType( "vec4" );
Var *vpos = new Var( "vpos", "vec2" );
meta->addStatement( new GenOp( " @ = @.xy / @.w;\r\n", new DecOp( vpos ), inVpos, inVpos ) );
return vpos;
}
Var* ShaderFeatureGLSL::getInWorldToTangent( Vector<ShaderComponent*> &componentList )
{
Var *worldToTangent = (Var*)LangElement::find( "worldToTangent" );
if ( !worldToTangent )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
worldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
worldToTangent->setName( "worldToTangent" );
worldToTangent->setStructName( "IN" );
worldToTangent->setType( "float3x3" );
}
return worldToTangent;
}
Var* ShaderFeatureGLSL::getInViewToTangent( Vector<ShaderComponent*> &componentList )
{
Var *viewToTangent = (Var*)LangElement::find( "viewToTangent" );
if ( !viewToTangent )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
viewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
viewToTangent->setName( "viewToTangent" );
viewToTangent->setStructName( "IN" );
viewToTangent->setType( "float3x3" );
}
return viewToTangent;
}
Var* ShaderFeatureGLSL::getNormalMapTex()
{
Var *normalMap = (Var*)LangElement::find( "bumpMap" );
if ( !normalMap )
{
normalMap = new Var;
normalMap->setType( "sampler2D" );
normalMap->setName( "bumpMap" );
normalMap->uniform = true;
normalMap->sampler = true;
normalMap->constNum = Var::getTexUnitNum();
}
return normalMap;
}
Var* ShaderFeatureGLSL::getObjTrans( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta )
{
Var *objTrans = (Var*)LangElement::find( "objTrans" );
if ( objTrans )
return objTrans;
if ( useInstancing )
{
ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
Var *instObjTrans = vertStruct->getElement( RT_TEXCOORD, 4, 4 );
instObjTrans->setStructName( "IN" );
instObjTrans->setName( "inst_objectTrans" );
mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+0 );
mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+1 );
mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+2 );
mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+3 );
objTrans = new Var;
objTrans->setType( "mat4x4" );
objTrans->setName( "objTrans" );
meta->addStatement( new GenOp( " @ = mat4x4( // Instancing!\r\n", new DecOp( objTrans ), instObjTrans ) );
meta->addStatement( new GenOp( " @[0],\r\n", instObjTrans ) );
meta->addStatement( new GenOp( " @[1],\r\n", instObjTrans ) );
meta->addStatement( new GenOp( " @[2],\r\n",instObjTrans ) );
meta->addStatement( new GenOp( " @[3] );\r\n", instObjTrans ) );
}
else
{
objTrans = new Var;
objTrans->setType( "float4x4" );
objTrans->setName( "objTrans" );
objTrans->uniform = true;
objTrans->constSortPos = cspPrimitive;
}
return objTrans;
}
Var* ShaderFeatureGLSL::getModelView( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta )
{
Var *modelview = (Var*)LangElement::find( "modelview" );
if ( modelview )
return modelview;
if ( useInstancing )
{
Var *objTrans = getObjTrans( componentList, useInstancing, meta );
Var *viewProj = (Var*)LangElement::find( "viewProj" );
if ( !viewProj )
{
viewProj = new Var;
viewProj->setType( "float4x4" );
viewProj->setName( "viewProj" );
viewProj->uniform = true;
viewProj->constSortPos = cspPass;
}
modelview = new Var;
modelview->setType( "float4x4" );
modelview->setName( "modelview" );
meta->addStatement( new GenOp( " @ = tMul( @, @ ); // Instancing!\r\n", new DecOp( modelview ), viewProj, objTrans ) );
}
else
{
modelview = new Var;
modelview->setType( "float4x4" );
modelview->setName( "modelview" );
modelview->uniform = true;
modelview->constSortPos = cspPrimitive;
}
return modelview;
}
Var* ShaderFeatureGLSL::getWorldView( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta )
{
Var *worldView = (Var*)LangElement::find( "worldViewOnly" );
if ( worldView )
return worldView;
if ( useInstancing )
{
Var *objTrans = getObjTrans( componentList, useInstancing, meta );
Var *worldToCamera = (Var*)LangElement::find( "worldToCamera" );
if ( !worldToCamera )
{
worldToCamera = new Var;
worldToCamera->setType( "float4x4" );
worldToCamera->setName( "worldToCamera" );
worldToCamera->uniform = true;
worldToCamera->constSortPos = cspPass;
}
worldView = new Var;
worldView->setType( "float4x4" );
worldView->setName( "worldViewOnly" );
meta->addStatement( new GenOp( " @ = tMul( @, @ ); // Instancing!\r\n", new DecOp( worldView ), worldToCamera, objTrans ) );
}
else
{
worldView = new Var;
worldView->setType( "float4x4" );
worldView->setName( "worldViewOnly" );
worldView->uniform = true;
worldView->constSortPos = cspPrimitive;
}
return worldView;
}
Var* ShaderFeatureGLSL::getInvWorldView( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta )
{
Var *viewToObj = (Var*)LangElement::find( "viewToObj" );
if ( viewToObj )
return viewToObj;
if ( useInstancing )
{
Var *worldView = getWorldView( componentList, useInstancing, meta );
viewToObj = new Var;
viewToObj->setType( "float3x3" );
viewToObj->setName( "viewToObj" );
// We just use transpose to convert the 3x3 portion
// of the world view transform into its inverse.
meta->addStatement( new GenOp( " @ = transpose( float3x3(@) ); // Instancing!\r\n", new DecOp( viewToObj ), worldView ) );
}
else
{
viewToObj = new Var;
viewToObj->setType( "float4x4" );
viewToObj->setName( "viewToObj" );
viewToObj->uniform = true;
viewToObj->constSortPos = cspPrimitive;
}
return viewToObj;
}
void ShaderFeatureGLSL::getWsPosition( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta,
LangElement *wsPosition )
{
Var *inPosition = (Var*)LangElement::find( "wsPosition" );
if ( inPosition )
{
meta->addStatement( new GenOp( " @ = @.xyz;\r\n",
wsPosition, inPosition ) );
return;
}
// Get the input position.
inPosition = (Var*)LangElement::find( "inPosition" );
if ( !inPosition )
inPosition = (Var*)LangElement::find( "position" );
AssertFatal( inPosition, "ShaderFeatureGLSL::getWsPosition - The vertex position was not found!" );
Var *objTrans = getObjTrans( componentList, useInstancing, meta );
meta->addStatement( new GenOp( " @ = tMul( @, vec4( @.xyz, 1 ) ).xyz;\r\n",
wsPosition, objTrans, inPosition ) );
}
Var* ShaderFeatureGLSL::addOutWsPosition( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta )
{
Var *outWsPosition = (Var*)LangElement::find( "outWsPosition" );
if ( !outWsPosition )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
outWsPosition = connectComp->getElement( RT_TEXCOORD );
outWsPosition->setName( "outWsPosition" );
outWsPosition->setStructName( "OUT" );
outWsPosition->setType( "vec3" );
outWsPosition->mapsToSampler = false;
getWsPosition( componentList, useInstancing, meta, outWsPosition );
}
return outWsPosition;
}
Var* ShaderFeatureGLSL::getInWsPosition( Vector<ShaderComponent*> &componentList )
{
Var *wsPosition = (Var*)LangElement::find( "wsPosition" );
if ( !wsPosition )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
wsPosition = connectComp->getElement( RT_TEXCOORD );
wsPosition->setName( "wsPosition" );
wsPosition->setStructName( "IN" );
wsPosition->setType( "vec3" );
}
return wsPosition;
}
Var* ShaderFeatureGLSL::getWsView( Var *wsPosition, MultiLine *meta )
{
Var *wsView = (Var*)LangElement::find( "wsView" );
if ( !wsView )
{
wsView = new Var( "wsView", "vec3" );
Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
if ( !eyePos )
{
eyePos = new Var;
eyePos->setType( "vec3" );
eyePos->setName( "eyePosWorld" );
eyePos->uniform = true;
eyePos->constSortPos = cspPass;
}
meta->addStatement( new GenOp( " @ = normalize( @ - @ );\r\n",
new DecOp( wsView ), eyePos, wsPosition ) );
}
return wsView;
}
Var* ShaderFeatureGLSL::addOutDetailTexCoord( Vector<ShaderComponent*> &componentList,
MultiLine *meta,
bool useTexAnim )
{
// Check if its already added.
Var *outTex = (Var*)LangElement::find( "detCoord" );
if ( outTex )
return outTex;
// Grab incoming texture coords.
Var *inTex = getVertTexCoord( "texCoord" );
// create detail variable
Var *detScale = new Var;
detScale->setType( "vec2" );
detScale->setName( "detailScale" );
detScale->uniform = true;
detScale->constSortPos = cspPotentialPrimitive;
// grab connector texcoord register
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
outTex = connectComp->getElement( RT_TEXCOORD );
outTex->setName( "detCoord" );
outTex->setStructName( "OUT" );
outTex->setType( "vec2" );
outTex->mapsToSampler = true;
if ( useTexAnim )
{
inTex->setType( "vec4" );
// Find or create the texture matrix.
Var *texMat = (Var*)LangElement::find( "texMat" );
if ( !texMat )
{
texMat = new Var;
texMat->setType( "float4x4" );
texMat->setName( "texMat" );
texMat->uniform = true;
texMat->constSortPos = cspPass;
}
meta->addStatement( new GenOp( " @ = tMul(@, @).xy * @;\r\n", outTex, texMat, inTex, detScale ) );
}
else
{
// setup output to mul texCoord by detail scale
meta->addStatement( new GenOp( " @ = @ * @;\r\n", outTex, inTex, detScale ) );
}
return outTex;
}
//****************************************************************************
// Base Texture
//****************************************************************************
DiffuseMapFeatGLSL::DiffuseMapFeatGLSL()
: mTorqueDep("shaders/common/gl/torque.glsl")
{
addDependency(&mTorqueDep);
}
void DiffuseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
getOutTexCoord( "texCoord",
"vec2",
true,
fd.features[MFT_TexAnim],
meta,
componentList );
output = meta;
}
U32 DiffuseMapFeatGLSL::getOutputTargets(const MaterialFeatureData &fd) const
{
return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
}
void DiffuseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
// grab connector texcoord register
Var *inTex = getInTexCoord( "texCoord", "vec2", true, componentList );
//determine output target
ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
if (fd.features[MFT_isDeferred])
targ = ShaderFeature::RenderTarget1;
// create texture var
Var *diffuseMap = new Var;
diffuseMap->setType( "sampler2D" );
diffuseMap->setName( "diffuseMap" );
diffuseMap->uniform = true;
diffuseMap->sampler = true;
diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
// create sample color var
Var *diffColor = new Var;
diffColor->setType("vec4");
diffColor->setName("diffuseColor");
LangElement *colorDecl = new DecOp( diffColor );
MultiLine * meta = new MultiLine;
output = meta;
if ( fd.features[MFT_CubeMap] )
{
meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n",
colorDecl,
diffuseMap,
inTex ) );
if (!fd.features[MFT_Imposter])
meta->addStatement( new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor) );
meta->addStatement( new GenOp( " @;\r\n", assignColor( diffColor, Material::Mul, NULL, targ) ) );
}
else if(fd.features[MFT_DiffuseMapAtlas])
{
// Handle atlased textures
// http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
Var *atlasedTex = new Var;
atlasedTex->setName("atlasedTexCoord");
atlasedTex->setType("vec2");
LangElement *atDecl = new DecOp(atlasedTex);
// Parameters of the texture atlas
Var *atParams = new Var;
atParams->setType("vec4");
atParams->setName("diffuseAtlasParams");
atParams->uniform = true;
atParams->constSortPos = cspPotentialPrimitive;
// Parameters of the texture (tile) this object is using in the atlas
Var *tileParams = new Var;
tileParams->setType("vec4");
tileParams->setName("diffuseAtlasTileParams");
tileParams->uniform = true;
tileParams->constSortPos = cspPotentialPrimitive;
const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
if(is_sm3)
{
// Figure out the mip level
meta->addStatement(new GenOp(" float2 _dx = ddx(@ * @.z);\r\n", inTex, atParams));
meta->addStatement(new GenOp(" float2 _dy = ddy(@ * @.z);\r\n", inTex, atParams));
meta->addStatement(new GenOp(" float mipLod = 0.5 * log2(max(dot(_dx, _dx), dot(_dy, _dy)));\r\n"));
meta->addStatement(new GenOp(" mipLod = clamp(mipLod, 0.0, @.w);\r\n", atParams));
// And the size of the mip level
meta->addStatement(new GenOp(" float mipPixSz = pow(2.0, @.w - mipLod);\r\n", atParams));
meta->addStatement(new GenOp(" float2 mipSz = mipPixSz / @.xy;\r\n", atParams));
}
else
{
meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
}
// Tiling mode
// TODO: Select wrap or clamp somehow
if( true ) // Wrap
meta->addStatement(new GenOp(" @ = frac(@);\r\n", atDecl, inTex));
else // Clamp
meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, inTex));
// Finally scale/offset, and correct for filtering
meta->addStatement(new GenOp(" @ = @ * ((mipSz * @.xy - 1.0) / mipSz) + 0.5 / mipSz + @.xy * @.xy;\r\n",
atlasedTex, atlasedTex, atParams, atParams, tileParams));
// Add a newline
meta->addStatement(new GenOp( "\r\n"));
// For the rest of the feature...
inTex = atlasedTex;
// To dump out UV coords...
//#define DEBUG_ATLASED_UV_COORDS
#ifdef DEBUG_ATLASED_UV_COORDS
if(!fd.features[MFT_PrePassConditioner])
{
meta->addStatement(new GenOp(" @ = vec4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
return;
}
#endif
if(is_sm3)
{
meta->addStatement(new GenOp( " @ = tex2Dlod(@, float4(@, 0.0, mipLod));\r\n",
new DecOp(diffColor), diffuseMap, inTex));
if (!fd.features[MFT_Imposter])
meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor));
}
else
{
meta->addStatement(new GenOp( " @ = tex2D(@, @);\r\n",
new DecOp(diffColor), diffuseMap, inTex));
if (!fd.features[MFT_Imposter])
meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor));
}
meta->addStatement(new GenOp( " @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
}
else
{
meta->addStatement(new GenOp("@ = tex2D(@, @);\r\n", colorDecl, diffuseMap, inTex));
if (!fd.features[MFT_Imposter])
meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor));
meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ)));
}
}
ShaderFeature::Resources DiffuseMapFeatGLSL::getResources( const MaterialFeatureData &fd )
{
Resources res;
res.numTex = 1;
res.numTexReg = 1;
return res;
}
void DiffuseMapFeatGLSL::setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex )