-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathterrFeatureHLSL.cpp
More file actions
1191 lines (977 loc) · 40.8 KB
/
Copy pathterrFeatureHLSL.cpp
File metadata and controls
1191 lines (977 loc) · 40.8 KB
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 "terrain/hlsl/terrFeatureHLSL.h"
#include "terrain/terrFeatureTypes.h"
#include "materials/materialFeatureTypes.h"
#include "materials/materialFeatureData.h"
#include "materials/processedMaterial.h"
#include "gfx/gfxDevice.h"
#include "shaderGen/langElement.h"
#include "shaderGen/shaderOp.h"
#include "shaderGen/featureMgr.h"
#include "shaderGen/shaderGen.h"
#include "core/module.h"
namespace
{
void register_hlsl_shader_features_for_terrain(GFXAdapterType type)
{
if (type != Direct3D11)
return;
FEATUREMGR->registerFeature( MFT_TerrainBaseMap, new TerrainBaseMapFeatHLSL );
FEATUREMGR->registerFeature( MFT_TerrainParallaxMap, new NamedFeatureHLSL( "Terrain Parallax Texture" ) );
FEATUREMGR->registerFeature( MFT_TerrainDetailMap, new TerrainDetailMapFeatHLSL );
FEATUREMGR->registerFeature( MFT_TerrainNormalMap, new TerrainNormalMapFeatHLSL );
FEATUREMGR->registerFeature( MFT_TerrainMacroMap, new TerrainMacroMapFeatHLSL );
FEATUREMGR->registerFeature( MFT_TerrainLightMap, new TerrainLightMapFeatHLSL );
FEATUREMGR->registerFeature( MFT_TerrainSideProject, new NamedFeatureHLSL( "Terrain Side Projection" ) );
FEATUREMGR->registerFeature( MFT_TerrainAdditive, new TerrainAdditiveFeatHLSL );
FEATUREMGR->registerFeature( MFT_DeferredTerrainBaseMap, new TerrainBaseMapFeatHLSL );
FEATUREMGR->registerFeature( MFT_DeferredTerrainMacroMap, new TerrainMacroMapFeatHLSL );
FEATUREMGR->registerFeature( MFT_DeferredTerrainDetailMap, new TerrainDetailMapFeatHLSL );
FEATUREMGR->registerFeature( MFT_DeferredTerrainBlankInfoMap, new TerrainBlankInfoMapFeatHLSL );
}
};
MODULE_BEGIN( TerrainFeatHLSL )
MODULE_INIT_AFTER( ShaderGen )
MODULE_INIT
{
SHADERGEN->getFeatureInitSignal().notify(®ister_hlsl_shader_features_for_terrain);
}
MODULE_END;
TerrainFeatHLSL::TerrainFeatHLSL()
: mTorqueDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/torque.hlsl" ))
{
addDependency( &mTorqueDep );
}
Var* TerrainFeatHLSL::_getUniformVar( const char *name, const char *type, ConstantSortPosition csp )
{
Var *theVar = (Var*)LangElement::find( name );
if ( !theVar )
{
theVar = new Var;
theVar->setType( type );
theVar->setName( name );
theVar->uniform = true;
theVar->constSortPos = csp;
}
return theVar;
}
Var* TerrainFeatHLSL::_computeAndGetInDetailCoord( MultiLine* meta, Vector<ShaderComponent*> &componentList )
{
String name("detCoord");
Var *inDet = (Var*)LangElement::find( name );
if (!inDet)
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
inDet = connectComp->getElement(RT_TEXCOORD);
inDet->setName(name);
inDet->setStructName("IN");
inDet->setType("float4");
}
S32 index = getProcessIndex();
// Get the detail scale and fade info.
Var *detScaleAndFade = (Var*)LangElement::find(String::ToString("detailScaleAndFade%d", index));
if (!detScaleAndFade)
{
detScaleAndFade = new Var;
detScaleAndFade->setType("float4");
detScaleAndFade->setName(String::ToString("detailScaleAndFade%d", index));
detScaleAndFade->uniform = true;
detScaleAndFade->constSortPos = cspPotentialPrimitive;
}
Var *detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", index));
if (!detCoord)
{
detCoord = new Var;
detCoord->setType("float4");
detCoord->setName(String::ToString("detCoord%d", index));
meta->addStatement(new GenOp(" @;\r\n", new DecOp(detCoord)));
meta->addStatement(new GenOp(" @.xyz = @.xyz * @.xyx;\r\n", detCoord, inDet, detScaleAndFade));
meta->addStatement(new GenOp(" @.w = clamp((@.z - @.w) * @.w, 0.0, 1.0);\r\n", detCoord, detScaleAndFade, inDet, detScaleAndFade));
}
return detCoord;
}
Var* TerrainFeatHLSL::_computeAndGetInMacroCoord(MultiLine* meta, Vector<ShaderComponent*> &componentList )
{
String name( "macroCoord" );
Var *inDet = (Var*)LangElement::find( name );
if ( !inDet )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
inDet = connectComp->getElement( RT_TEXCOORD );
inDet->setName( name );
inDet->setStructName( "IN" );
inDet->setType( "float4" );
}
S32 index = getProcessIndex();
// Get the macro scale and fade info.
Var *macroScaleAndFade = (Var*)LangElement::find(String::ToString("macroScaleAndFade%d", index));
if (!macroScaleAndFade)
{
macroScaleAndFade = new Var;
macroScaleAndFade->setType("float4");
macroScaleAndFade->setName(String::ToString("macroScaleAndFade%d", index));
macroScaleAndFade->uniform = true;
macroScaleAndFade->constSortPos = cspPotentialPrimitive;
}
Var *macroCoord = (Var*)LangElement::find(String::ToString("macroCoord%d", index));
if (!macroCoord)
{
macroCoord = new Var;
macroCoord->setType("float4");
macroCoord->setName(String::ToString("macroCoord%d", index));
meta->addStatement(new GenOp(" @;\r\n", new DecOp(macroCoord)));
meta->addStatement(new GenOp(" @.xyz = @.xyz * @.xyx;\r\n", macroCoord, inDet, macroScaleAndFade));
meta->addStatement(new GenOp(" @.w = clamp((@.z - @.w) * @.w, 0.0, 1.0);\r\n", macroCoord, macroScaleAndFade, inDet, macroScaleAndFade));
}
return macroCoord;
}
Var* TerrainFeatHLSL::_getNormalMapTex()
{
String name("normalMap");
Var *normalMap = (Var*)LangElement::find(name);
if (!normalMap)
{
normalMap = new Var;
normalMap->setType("SamplerState");
normalMap->setName(name);
normalMap->uniform = true;
normalMap->sampler = true;
normalMap->constNum = Var::getTexUnitNum();
}
return normalMap;
}
Var* TerrainFeatHLSL::_getDetailIdStrengthParallax()
{
String name( String::ToString( "detailIdStrengthParallax%d", getProcessIndex() ) );
Var *detailInfo = (Var*)LangElement::find( name );
if ( !detailInfo )
{
detailInfo = new Var;
detailInfo->setType( "float3" );
detailInfo->setName( name );
detailInfo->uniform = true;
detailInfo->constSortPos = cspPotentialPrimitive;
}
return detailInfo;
}
Var* TerrainFeatHLSL::_getMacroIdStrengthParallax()
{
String name( String::ToString( "macroIdStrengthParallax%d", getProcessIndex() ) );
Var *detailInfo = (Var*)LangElement::find( name );
if ( !detailInfo )
{
detailInfo = new Var;
detailInfo->setType( "float3" );
detailInfo->setName( name );
detailInfo->uniform = true;
detailInfo->constSortPos = cspPotentialPrimitive;
}
return detailInfo;
}
void TerrainBaseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
output = meta;
// Generate the incoming texture var.
Var *inTex;
{
Var *inPos = (Var*)LangElement::find( "inPosition" );
if ( !inPos )
inPos = (Var*)LangElement::find( "position" );
inTex = new Var( "texCoord", "float3" );
Var *oneOverTerrainSize = _getUniformVar( "oneOverTerrainSize", "float", cspPass );
// NOTE: The y coord here should be negative to have
// the texture maps not end up flipped which also caused
// normal and parallax mapping to be incorrect.
//
// This mistake early in development means that the layer
// id bilinear blend depends on it being that way.
//
// So instead i fixed this by flipping the base and detail
// coord y scale to compensate when rendering.
//
meta->addStatement( new GenOp( " @ = @.xyz * float3( @, @, -@ );\r\n",
new DecOp( inTex ), inPos, oneOverTerrainSize, oneOverTerrainSize, oneOverTerrainSize ) );
}
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
// Pass the texture coord to the pixel shader.
Var *outTex = connectComp->getElement( RT_TEXCOORD );
outTex->setName( "outTexCoord" );
outTex->setStructName( "OUT" );
outTex->setType( "float3" );
meta->addStatement( new GenOp( " @.xy = @.xy;\r\n", outTex, inTex ) );
// If this shader has a side projected layer then we
// pass the dot product between the +Y and the normal
// thru outTexCoord.z for use in blending the textures.
if ( fd.features.hasFeature( MFT_TerrainSideProject ) )
{
Var *inNormal = (Var*)LangElement::find( "normal" );
meta->addStatement(
new GenOp( " @.z = pow( abs( dot( normalize( float3( @.x, @.y, 0 ) ), float3( 0, 1, 0 ) ) ), 10.0 );\r\n",
outTex, inNormal, inNormal ) );
}
else
meta->addStatement( new GenOp( " @.z = 0;\r\n", outTex ) );
// HACK: This is sort of lazy... we generate the tanget
// vector here so that we're sure it exists in the parallax
// and normal features which will expect "T" to exist.
//
// If this shader doesn't use it the shader compiler will
// optimize away this code.
//
Var *inTangentZ = getVertTexCoord( "tcTangentZ" );
Var *inTanget = new Var( "T", "float3" );
Var *squareSize = _getUniformVar( "squareSize", "float", cspPass );
meta->addStatement( new GenOp( " @ = normalize( float3( @, 0, @ ) );\r\n",
new DecOp( inTanget ), squareSize, inTangentZ ) );
}
void TerrainBaseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
// grab connector texcoord register
Var *texCoord = getInTexCoord( "texCoord", "float3", componentList );
// create texture var
Var *diffuseMap = new Var;
diffuseMap->setType( "SamplerState" );
diffuseMap->setName( "baseTexMap" );
diffuseMap->uniform = true;
diffuseMap->sampler = true;
diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
MultiLine *meta = new MultiLine;
Var *baseColor = new Var;
baseColor->setType( "float4" );
baseColor->setName( "baseColor" );
Var *diffuseTex = new Var;
diffuseTex->setType("Texture2D");
diffuseTex->setName("baseTexture");
diffuseTex->uniform = true;
diffuseTex->texture = true;
diffuseTex->constNum = diffuseMap->constNum;
meta->addStatement(new GenOp(" @ = @.Sample( @, @.xy );\r\n", new DecOp(baseColor), diffuseTex, diffuseMap, texCoord));
ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
if (fd.features.hasFeature(MFT_isDeferred))
{
target= ShaderFeature::RenderTarget1;
}
meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul,NULL,target ) ) );
output = meta;
}
ShaderFeature::Resources TerrainBaseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
{
Resources res;
res.numTexReg = 1;
res.numTex = 1;
return res;
}
U32 TerrainBaseMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
{
return fd.features[MFT_DeferredTerrainBaseMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
}
TerrainDetailMapFeatHLSL::TerrainDetailMapFeatHLSL()
: mTorqueDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/torque.hlsl" )),
mTerrainDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/terrain/terrain.hlsl" ))
{
addDependency( &mTorqueDep );
addDependency( &mTerrainDep );
}
void TerrainDetailMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
const S32 detailIndex = getProcessIndex();
// Grab incoming texture coords... the base map feature
// made sure this was created.
Var *inTex = (Var*)LangElement::find( "texCoord" );
AssertFatal( inTex, "The texture coord is missing!" );
// Grab the input position.
Var *inPos = (Var*)LangElement::find( "inPosition" );
if ( !inPos )
inPos = (Var*)LangElement::find( "position" );
// Get the object space eye position.
Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
MultiLine *meta = new MultiLine;
// If we have parallax mapping then make sure we've sent
// the negative view vector to the pixel shader.
if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) &&
!LangElement::find( "outNegViewTS" ) )
{
// Get the object to tangent transform which
// will consume 3 output registers.
Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd );
// Now use a single output register to send the negative
// view vector in tangent space to the pixel shader.
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
Var *outNegViewTS = connectComp->getElement( RT_TEXCOORD );
outNegViewTS->setName( "outNegViewTS" );
outNegViewTS->setStructName( "OUT" );
outNegViewTS->setType( "float3" );
meta->addStatement( new GenOp( " @ = mul( @, float3( @ - @.xyz ) );\r\n",
outNegViewTS, objToTangentSpace, eyePos, inPos ) );
}
// Get the distance from the eye to this vertex.
Var *dist = (Var*)LangElement::find( "dist" );
if ( !dist )
{
dist = new Var;
dist->setType( "float" );
dist->setName( "dist" );
meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
new DecOp( dist ), inPos, eyePos ) );
}
// Get the detail scale and fade info.
Var *detScaleAndFade = new Var;
detScaleAndFade->setType( "float4" );
detScaleAndFade->setName( String::ToString( "detailScaleAndFade%d", detailIndex ) );
detScaleAndFade->uniform = true;
detScaleAndFade->constSortPos = cspPotentialPrimitive;
// Setup the detail coord.
//
// NOTE: You see here we scale the texture coord by 'xyx'
// to generate the detail coord. This y is here because
// its scale is flipped to correct for the non negative y
// in texCoord.
//
// See TerrainBaseMapFeatHLSL::processVert().
//
// grab connector texcoord register
Var *outTex = (Var*)LangElement::find("detCoord");
if (!outTex)
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
outTex = connectComp->getElement(RT_TEXCOORD);
outTex->setName("detCoord");
outTex->setStructName("OUT");
outTex->setType("float4");
}
meta->addStatement(new GenOp(" @.xyz = @.xyx;\r\n", outTex, inTex));
meta->addStatement(new GenOp(" @.w = @;\r\n", outTex, dist));
output = meta;
}
void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
const S32 detailIndex = getProcessIndex();
Var *inTex = getVertTexCoord( "texCoord" );
MultiLine *meta = new MultiLine;
// We need the negative tangent space view vector
// as in parallax mapping we step towards the camera.
Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
if ( !negViewTS &&
fd.features.hasFeature( MFT_TerrainParallaxMap ) )
{
Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
if ( !inNegViewTS )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
inNegViewTS = connectComp->getElement( RT_TEXCOORD );
inNegViewTS->setName( "outNegViewTS" );
inNegViewTS->setStructName( "IN" );
inNegViewTS->setType( "float3" );
}
negViewTS = new Var( "negViewTS", "float3" );
meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
}
// Get the layer samples.
Var *layerSample = (Var*)LangElement::find( "layerSample" );
if ( !layerSample )
{
layerSample = new Var;
layerSample->setType( "float4" );
layerSample->setName( "layerSample" );
// Get the layer texture var
Var *layerTex = new Var;
layerTex->setType( "SamplerState" );
layerTex->setName( "layerTex" );
layerTex->uniform = true;
layerTex->sampler = true;
layerTex->constNum = Var::getTexUnitNum();
Var* layerTexObj = new Var;
layerTexObj->setName("layerTexObj");
layerTexObj->setType("Texture2D");
layerTexObj->uniform = true;
layerTexObj->texture = true;
layerTexObj->constNum = layerTex->constNum;
// Read the layer texture to get the samples.
meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
new DecOp(layerSample), layerTexObj, layerTex, inTex));
}
Var *layerSize = (Var*)LangElement::find( "layerSize" );
if ( !layerSize )
{
layerSize = new Var;
layerSize->setType( "float" );
layerSize->setName( "layerSize" );
layerSize->uniform = true;
layerSize->constSortPos = cspPass;
}
// Grab the incoming detail coord.
Var *inDet = _computeAndGetInDetailCoord( meta, componentList );
// Get the detail id.
Var *detailInfo = _getDetailIdStrengthParallax();
// Create the detail blend var.
Var *detailBlend = new Var;
detailBlend->setType( "float" );
detailBlend->setName( String::ToString( "detailBlend%d", detailIndex ) );
// Calculate the blend for this detail texture.
meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) );
// Get a var and accumulate the blend amount.
Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
if ( !blendTotal )
{
blendTotal = new Var;
blendTotal->setName( "blendTotal" );
blendTotal->setType( "float" );
meta->addStatement( new GenOp( " @ = 0;\r\n", new DecOp( blendTotal ) ) );
}
// Add to the blend total.
meta->addStatement(new GenOp(" @ += @;\r\n", blendTotal, detailBlend));
Var *detailColor = (Var*)LangElement::find("detailColor");
if (!detailColor)
{
detailColor = new Var;
detailColor->setType("float4");
detailColor->setName("detailColor");
meta->addStatement(new GenOp(" @;\r\n", new DecOp(detailColor)));
}
// Get the detail texture.
Var *detailMap = (Var*)LangElement::find("detailMap");
if (!detailMap)
{
detailMap = new Var;
detailMap->setType("SamplerState");
detailMap->setName("detailMap");
detailMap->uniform = true;
detailMap->sampler = true;
detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
}
S32 detailTexIndex = Var::getTexUnitNum();
// If we had a parallax feature... then factor in the parallax
// amount so that it fades out with the layer blending.
if (fd.features.hasFeature(MFT_TerrainParallaxMap, detailIndex))
{
// Get the rest of our inputs.
Var *normalMap = _getNormalMapTex();
normalMap->constNum--;
detailTexIndex++;
String name(String::ToString("normalMapTex%d", getProcessIndex()));
Var *normalMapTex = (Var*)LangElement::find(name);
if (!normalMapTex)
{
normalMapTex = new Var;
normalMapTex->setName(String::ToString("normalMapTex%d", getProcessIndex()));
normalMapTex->setType("Texture2D");
normalMapTex->uniform = true;
normalMapTex->texture = true;
normalMapTex->constNum = Var::getTexUnitNum();
}
// Call the library function to do the rest.
if (fd.features.hasFeature(MFT_IsBC3nm, detailIndex))
{
meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @, @.xy, @, @.z * @ );\r\n",
inDet, normalMapTex, normalMap, inDet, negViewTS, detailInfo, detailBlend));
}
else
{
meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @, @.xy, @, @.z * @ );\r\n",
inDet, normalMapTex, normalMap, inDet, negViewTS, detailInfo, detailBlend));
}
}
// Note that we're doing the standard greyscale detail
// map technique here which can darken and lighten the
// diffuse texture.
//
// We take two color samples and lerp between them for
// side projection layers... else a single sample.
//
//Sampled detail texture that is not expanded
Var* detailTex = new Var;
detailTex->setName(String::ToString("detailTex%d", detailIndex));
detailTex->setType("Texture2D");
detailTex->uniform = true;
detailTex->texture = true;
detailTex->constNum = detailTexIndex;
// If we're using SM 3.0 then take advantage of
// dynamic branching to skip layers per-pixel.
if ( GFX->getPixelShaderVersion() >= 3.0f )
meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
meta->addStatement( new GenOp( " {\r\n" ) );
if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
{
meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
detailColor, detailTex, detailMap, inDet, detailTex, detailMap, inDet, inTex));
}
else
{
meta->addStatement(new GenOp(" @ = ( @.Sample( @, @.xy ) * 2.0 ) - 1.0;\r\n",
detailColor, detailTex, detailMap, inDet));
}
meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
detailColor, detailInfo, inDet ) );
ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
if(fd.features.hasFeature( MFT_DeferredTerrainDetailMap ))
target= ShaderFeature::RenderTarget1;
Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
meta->addStatement( new GenOp( " @ += @ * @;\r\n",
outColor, detailColor, detailBlend));
meta->addStatement( new GenOp( " }\r\n" ) );
output = meta;
}
ShaderFeature::Resources TerrainDetailMapFeatHLSL::getResources( const MaterialFeatureData &fd )
{
Resources res;
if ( getProcessIndex() == 0 )
{
// If this is the first detail pass then we
// samples from the layer tex.
res.numTex += 1;
// If this material also does parallax then it
// will generate the negative view vector and the
// worldToTanget transform.
if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) )
res.numTexReg += 4;
// We always send the detail texture
// coord information to the pixel shader.
res.numTexReg += 1;
}
// sample from the detail texture for diffuse coloring.
res.numTex += 1;
// If we have parallax for this layer then we'll also
// be sampling the normal map for the parallax heightmap.
if ( fd.features.hasFeature( MFT_TerrainParallaxMap, getProcessIndex() ) )
res.numTex += 1;
// Finally we always send the detail texture
// coord to the pixel shader.
//res.numTexReg += 1;
return res;
}
U32 TerrainDetailMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
{
return fd.features[MFT_DeferredTerrainDetailMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
}
TerrainMacroMapFeatHLSL::TerrainMacroMapFeatHLSL()
: mTorqueDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/torque.hlsl" )),
mTerrainDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/terrain/terrain.hlsl" ))
{
addDependency( &mTorqueDep );
addDependency( &mTerrainDep );
}
void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
const S32 detailIndex = getProcessIndex();
// Grab incoming texture coords... the base map feature
// made sure this was created.
Var *inTex = (Var*)LangElement::find( "texCoord" );
AssertFatal( inTex, "The texture coord is missing!" );
// Grab the input position.
Var *inPos = (Var*)LangElement::find( "inPosition" );
if ( !inPos )
inPos = (Var*)LangElement::find( "position" );
// Get the object space eye position.
Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
MultiLine *meta = new MultiLine;
// Get the distance from the eye to this vertex.
Var *dist = (Var*)LangElement::find( "macroDist" );
if ( !dist )
{
dist = new Var;
dist->setType( "float" );
dist->setName( "macroDist" );
meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
new DecOp( dist ), inPos, eyePos ) );
}
// grab connector texcoord register
Var *outTex = (Var*)LangElement::find("macroCoord");
if (!outTex)
{
// grab connector texcoord register
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
outTex = connectComp->getElement(RT_TEXCOORD);
outTex->setName("macroCoord");
outTex->setStructName("OUT");
outTex->setType("float4");
}
// Get the detail scale and fade info.
Var *detScaleAndFade = new Var;
detScaleAndFade->setType( "float4" );
detScaleAndFade->setName( String::ToString( "macroScaleAndFade%d", detailIndex ) );
detScaleAndFade->uniform = true;
detScaleAndFade->constSortPos = cspPotentialPrimitive;
// Setup the detail coord.
meta->addStatement(new GenOp(" @.xyz = @.xyx;\r\n", outTex, inTex));
// And sneak the detail fade thru the w detailCoord.
meta->addStatement(new GenOp(" @.w = @;\r\n", outTex, dist));
output = meta;
}
void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
const S32 detailIndex = getProcessIndex();
Var *inTex = getVertTexCoord( "texCoord" );
MultiLine *meta = new MultiLine;
// We need the negative tangent space view vector
// as in parallax mapping we step towards the camera.
Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
if ( !negViewTS &&
fd.features.hasFeature( MFT_TerrainParallaxMap ) )
{
Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
if ( !inNegViewTS )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
inNegViewTS = connectComp->getElement( RT_TEXCOORD );
inNegViewTS->setName( "outNegViewTS" );
inNegViewTS->setStructName( "IN" );
inNegViewTS->setType( "float3" );
}
negViewTS = new Var( "negViewTS", "float3" );
meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
}
// Get the layer samples.
Var *layerSample = (Var*)LangElement::find( "layerSample" );
if ( !layerSample )
{
layerSample = new Var;
layerSample->setType( "float4" );
layerSample->setName( "layerSample" );
// Get the layer texture var
Var *layerTex = new Var;
layerTex->setType( "SamplerState" );
layerTex->setName( "macrolayerTex" );
layerTex->uniform = true;
layerTex->sampler = true;
layerTex->constNum = Var::getTexUnitNum();
// Read the layer texture to get the samples.
Var *layerTexObj = new Var;
layerTexObj->setType("Texture2D");
layerTexObj->setName("macroLayerTexObj");
layerTexObj->uniform = true;
layerTexObj->texture = true;
layerTexObj->constNum = layerTex->constNum;
meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
new DecOp(layerSample), layerTexObj, layerTex, inTex));
}
Var *layerSize = (Var*)LangElement::find( "layerSize" );
if ( !layerSize )
{
layerSize = new Var;
layerSize->setType( "float" );
layerSize->setName( "layerSize" );
layerSize->uniform = true;
layerSize->constSortPos = cspPass;
}
// Grab the incoming detail coord.
Var *inDet = _computeAndGetInMacroCoord( meta, componentList );
// Get the detail id.
Var *detailInfo = _getMacroIdStrengthParallax();
// Create the detail blend var.
Var *detailBlend = new Var;
detailBlend->setType( "float" );
detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) );
// Calculate the blend for this detail texture.
meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) );
// Get a var and accumulate the blend amount.
Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
if ( !blendTotal )
{
blendTotal = new Var;
blendTotal->setName( "blendTotal" );
blendTotal->setType( "float" );
meta->addStatement( new GenOp( " @ = 0;\r\n", new DecOp( blendTotal ) ) );
}
// Add to the blend total.
meta->addStatement(new GenOp(" @ += @;\r\n", blendTotal, detailBlend));
Var *detailColor = (Var*)LangElement::find( "macroColor" );
if ( !detailColor )
{
detailColor = new Var;
detailColor->setType( "float4" );
detailColor->setName( "macroColor" );
meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
}
// Get the detail texture.
Var *detailMap = (Var*)LangElement::find("macroMap");
if (!detailMap)
{
detailMap = new Var;
detailMap->setType("SamplerState");
detailMap->setName("macroMap");
detailMap->uniform = true;
detailMap->sampler = true;
detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
}
//Create texture object for directx 11
Var *detailTex = new Var;
detailTex->setName(String::ToString("macroMapTex%d", detailIndex));
detailTex->setType("Texture2D");
detailTex->uniform = true;
detailTex->texture = true;
detailTex->constNum = Var::getTexUnitNum();
// If we're using SM 3.0 then take advantage of
// dynamic branching to skip layers per-pixel.
if ( GFX->getPixelShaderVersion() >= 3.0f )
meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
meta->addStatement( new GenOp( " {\r\n" ) );
// Note that we're doing the standard greyscale detail
// map technique here which can darken and lighten the
// diffuse texture.
//
// We take two color samples and lerp between them for
// side projection layers... else a single sample.
//
if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
{
meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
detailColor, detailTex, detailMap, inDet, detailTex, detailMap, inDet, inTex));
}
else
{
meta->addStatement(new GenOp(" @ = ( @.Sample( @, @.xy ) * 2.0 ) - 1.0;\r\n",
detailColor, detailTex, detailMap, inDet));
}
meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
detailColor, detailInfo, inDet ) );
ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
if(fd.features.hasFeature(MFT_DeferredTerrainMacroMap))
target= ShaderFeature::RenderTarget1;
Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
meta->addStatement(new GenOp(" @ += @ * @;\r\n",
outColor, detailColor, detailBlend));
meta->addStatement( new GenOp( " }\r\n" ) );
output = meta;
}
ShaderFeature::Resources TerrainMacroMapFeatHLSL::getResources( const MaterialFeatureData &fd )
{
Resources res;
if ( getProcessIndex() == 0 )
{
// If this is the first detail pass then we
// samples from the layer tex.
res.numTex += 1;
}
res.numTex += 1;
// Finally we always send the detail texture
// coord to the pixel shader.
res.numTexReg += 1;
return res;
}
U32 TerrainMacroMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
{
return fd.features[MFT_DeferredTerrainMacroMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
}
void TerrainNormalMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
// We only need to process normals during the deferred.
if ( !fd.features.hasFeature( MFT_DeferredConditioner ) )
return;
MultiLine *meta = new MultiLine;
// Make sure the world to tangent transform
// is created and available for the pixel shader.
getOutViewToTangent( componentList, meta, fd );
output = meta;
}
void TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
Var *viewToTangent = getInViewToTangent( componentList );
// This var is read from GBufferConditionerHLSL and
// used in the deferred output.
Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
if ( !gbNormal )
{
gbNormal = new Var;
gbNormal->setName( "gbNormal" );
gbNormal->setType( "float3" );
meta->addStatement( new GenOp( " @ = @[2];\r\n", new DecOp( gbNormal ), viewToTangent ) );
}
const S32 normalIndex = getProcessIndex();
Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) );
AssertFatal( detailBlend, "The detail blend is missing!" );
// If we're using SM 3.0 then take advantage of
// dynamic branching to skip layers per-pixel.
if ( GFX->getPixelShaderVersion() >= 3.0f )
meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
meta->addStatement( new GenOp( " {\r\n" ) );