shaderFeatureHLSL.cpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "shaderGen/HLSL/shaderFeatureHLSL.h"
  24. #include "shaderGen/langElement.h"
  25. #include "shaderGen/shaderOp.h"
  26. #include "shaderGen/shaderGenVars.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "materials/matInstance.h"
  29. #include "materials/processedMaterial.h"
  30. #include "materials/materialFeatureTypes.h"
  31. #include "core/util/autoPtr.h"
  32. #include "lighting/advanced/advancedLightBinManager.h"
  33. #include "ts/tsShape.h"
  34. #include "shaderGen/shaderGen.h"
  35. LangElement * ShaderFeatureHLSL::setupTexSpaceMat( Vector<ShaderComponent*> &, // componentList
  36. Var **texSpaceMat )
  37. {
  38. Var *N = (Var*) LangElement::find( "normal" );
  39. Var *B = (Var*) LangElement::find( "B" );
  40. Var *T = (Var*) LangElement::find( "T" );
  41. Var *tangentW = (Var*) LangElement::find( "tangentW" );
  42. // setup matrix var
  43. *texSpaceMat = new Var;
  44. (*texSpaceMat)->setType( "float3x3" );
  45. (*texSpaceMat)->setName( "objToTangentSpace" );
  46. MultiLine *meta = new MultiLine;
  47. meta->addStatement( new GenOp( " @;\r\n", new DecOp( *texSpaceMat ) ) );
  48. // Protect against missing normal and tangent.
  49. if ( !N || !T )
  50. {
  51. meta->addStatement( new GenOp( " @[0] = float3( 1, 0, 0 ); @[1] = float3( 0, 1, 0 ); @[2] = float3( 0, 0, 1 );\r\n",
  52. *texSpaceMat, *texSpaceMat, *texSpaceMat ) );
  53. return meta;
  54. }
  55. meta->addStatement( new GenOp( " @[0] = @;\r\n", *texSpaceMat, T ) );
  56. if ( B )
  57. meta->addStatement( new GenOp( " @[1] = @;\r\n", *texSpaceMat, B ) );
  58. else
  59. {
  60. if(dStricmp((char*)T->type, "float4") == 0)
  61. meta->addStatement( new GenOp( " @[1] = cross( @, normalize(@) ) * @.w;\r\n", *texSpaceMat, T, N, T ) );
  62. else if (tangentW)
  63. meta->addStatement(new GenOp(" @[1] = cross( @, normalize(@) ) * @;\r\n", *texSpaceMat, T, N, tangentW));
  64. else
  65. meta->addStatement( new GenOp( " @[1] = cross( @, normalize(@) );\r\n", *texSpaceMat, T, N ) );
  66. }
  67. meta->addStatement( new GenOp( " @[2] = normalize(@);\r\n", *texSpaceMat, N ) );
  68. return meta;
  69. }
  70. LangElement* ShaderFeatureHLSL::assignColor( LangElement *elem,
  71. Material::BlendOp blend,
  72. LangElement *lerpElem,
  73. ShaderFeature::OutputTarget outputTarget )
  74. {
  75. // search for color var
  76. Var *color = (Var*) LangElement::find( getOutputTargetVarName(outputTarget) );
  77. if ( !color )
  78. {
  79. // create color var
  80. color = new Var;
  81. color->setType( "fragout" );
  82. color->setName( getOutputTargetVarName(outputTarget) );
  83. color->setStructName( "OUT" );
  84. return new GenOp( "@ = @", color, elem );
  85. }
  86. LangElement *assign;
  87. switch ( blend )
  88. {
  89. case Material::Add:
  90. assign = new GenOp( "@ += @", color, elem );
  91. break;
  92. case Material::Sub:
  93. assign = new GenOp( "@ -= @", color, elem );
  94. break;
  95. case Material::Mul:
  96. assign = new GenOp( "@ *= @", color, elem );
  97. break;
  98. case Material::AddAlpha:
  99. assign = new GenOp( "@ += @ * @.a", color, elem, elem );
  100. break;
  101. case Material::LerpAlpha:
  102. if ( !lerpElem )
  103. lerpElem = elem;
  104. assign = new GenOp( "@.rgb = lerp( @.rgb, (@).rgb, (@).a )", color, color, elem, lerpElem );
  105. break;
  106. case Material::ToneMap:
  107. assign = new GenOp( "@ = 1.0 - exp(-1.0 * @ * @)", color, color, elem );
  108. break;
  109. default:
  110. AssertFatal(false, "Unrecognized color blendOp");
  111. // Fallthru
  112. case Material::None:
  113. assign = new GenOp( "@ = @", color, elem );
  114. break;
  115. }
  116. return assign;
  117. }
  118. LangElement *ShaderFeatureHLSL::expandNormalMap( LangElement *sampleNormalOp,
  119. LangElement *normalDecl,
  120. LangElement *normalVar,
  121. const MaterialFeatureData &fd )
  122. {
  123. MultiLine *meta = new MultiLine;
  124. const bool hasBc3 = fd.features.hasFeature(MFT_IsBC3nm, getProcessIndex() );
  125. const bool hasBc5 = fd.features.hasFeature(MFT_IsBC5nm, getProcessIndex() );
  126. if ( hasBc3 || hasBc5 )
  127. {
  128. if ( fd.features[MFT_ImposterVert] )
  129. {
  130. // The imposter system uses object space normals and
  131. // encodes them with the z axis in the alpha component.
  132. meta->addStatement( new GenOp( " @ = float4( normalize( @.xyw * 2.0 - 1.0 ), 0.0 ); // Obj DXTnm\r\n", normalDecl, sampleNormalOp ) );
  133. }
  134. else if( hasBc3 )
  135. {
  136. // BC3 Swizzle trick
  137. meta->addStatement( new GenOp( " @ = float4( @.ag * 2.0 - 1.0, 0.0, 0.0 ); // DXTnm\r\n", normalDecl, sampleNormalOp ) );
  138. meta->addStatement( new GenOp( " @.z = sqrt( 1.0 - dot( @.xy, @.xy ) ); // DXTnm\r\n", normalVar, normalVar, normalVar ) );
  139. }
  140. else if (hasBc5)
  141. {
  142. // BC5
  143. meta->addStatement(new GenOp(" @ = float4( @.gr * 2.0 - 1.0, 0.0, 0.0 ); // bc5nm\r\n", normalDecl, sampleNormalOp ) );
  144. meta->addStatement(new GenOp(" @.z = sqrt( 1.0 - dot( @.xy, @.xy ) ); // bc5nm\r\n", normalVar, normalVar, normalVar )) ;
  145. }
  146. }
  147. else
  148. {
  149. meta->addStatement( new GenOp( " @ = @;\r\n", normalDecl, sampleNormalOp ) );
  150. meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 1.0;\r\n", normalVar, normalVar ) );
  151. }
  152. return meta;
  153. }
  154. ShaderFeatureHLSL::ShaderFeatureHLSL()
  155. {
  156. output = NULL;
  157. }
  158. Var * ShaderFeatureHLSL::getVertTexCoord( const String &name )
  159. {
  160. Var *inTex = NULL;
  161. for( U32 i=0; i<LangElement::elementList.size(); i++ )
  162. {
  163. if( !dStrcmp( (char*)LangElement::elementList[i]->name, name.c_str() ) )
  164. {
  165. inTex = dynamic_cast<Var*>( LangElement::elementList[i] );
  166. if ( inTex )
  167. {
  168. // NOTE: This used to do this check...
  169. //
  170. // dStrcmp( (char*)inTex->structName, "IN" )
  171. //
  172. // ... to ensure that the var was from the input
  173. // vertex structure, but this kept some features
  174. // ( ie. imposter vert ) from decoding their own
  175. // coords for other features to use.
  176. //
  177. // If we run into issues with collisions between
  178. // IN vars and local vars we may need to revise.
  179. break;
  180. }
  181. }
  182. }
  183. return inTex;
  184. }
  185. Var* ShaderFeatureHLSL::getOutObjToTangentSpace( Vector<ShaderComponent*> &componentList,
  186. MultiLine *meta,
  187. const MaterialFeatureData &fd )
  188. {
  189. Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" );
  190. if ( !outObjToTangentSpace )
  191. meta->addStatement( setupTexSpaceMat( componentList, &outObjToTangentSpace ) );
  192. return outObjToTangentSpace;
  193. }
  194. Var* ShaderFeatureHLSL::getOutWorldToTangent( Vector<ShaderComponent*> &componentList,
  195. MultiLine *meta,
  196. const MaterialFeatureData &fd )
  197. {
  198. Var *outWorldToTangent = (Var*)LangElement::find( "outWorldToTangent" );
  199. if ( outWorldToTangent )
  200. return outWorldToTangent;
  201. Var *worldToTangent = (Var*)LangElement::find( "worldToTangent" );
  202. if ( !worldToTangent )
  203. {
  204. Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
  205. if (!fd.features[MFT_ParticleNormal] )
  206. {
  207. // turn obj->tangent into world->tangent
  208. worldToTangent = new Var;
  209. worldToTangent->setType( "float3x3" );
  210. worldToTangent->setName( "worldToTangent" );
  211. LangElement *worldToTangentDecl = new DecOp( worldToTangent );
  212. // Get the world->obj transform
  213. Var *worldToObj = (Var*)LangElement::find( "worldToObj" );
  214. if ( !worldToObj )
  215. {
  216. worldToObj = new Var;
  217. worldToObj->setName( "worldToObj" );
  218. if ( fd.features[MFT_UseInstancing] )
  219. {
  220. // We just use transpose to convert the 3x3 portion of
  221. // the object transform to its inverse.
  222. worldToObj->setType( "float3x3" );
  223. Var *objTrans = getObjTrans( componentList, true, meta );
  224. meta->addStatement( new GenOp( " @ = transpose( (float3x3)@ ); // Instancing!\r\n", new DecOp( worldToObj ), objTrans ) );
  225. }
  226. else
  227. {
  228. worldToObj->setType( "float4x4" );
  229. worldToObj->uniform = true;
  230. worldToObj->constSortPos = cspPrimitive;
  231. }
  232. }
  233. // assign world->tangent transform
  234. meta->addStatement( new GenOp( " @ = mul( @, (float3x3)@ );\r\n", worldToTangentDecl, texSpaceMat, worldToObj ) );
  235. }
  236. else
  237. {
  238. // Assume particle normal generation has set this up in the proper space
  239. worldToTangent = texSpaceMat;
  240. }
  241. }
  242. // send transform to pixel shader
  243. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  244. outWorldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  245. outWorldToTangent->setName( "outWorldToTangent" );
  246. outWorldToTangent->setStructName( "OUT" );
  247. outWorldToTangent->setType( "float3x3" );
  248. meta->addStatement( new GenOp( " @ = @;\r\n", outWorldToTangent, worldToTangent ) );
  249. return outWorldToTangent;
  250. }
  251. Var* ShaderFeatureHLSL::getOutViewToTangent( Vector<ShaderComponent*> &componentList,
  252. MultiLine *meta,
  253. const MaterialFeatureData &fd )
  254. {
  255. Var *outViewToTangent = (Var*)LangElement::find( "outViewToTangent" );
  256. if ( outViewToTangent )
  257. return outViewToTangent;
  258. Var *viewToTangent = (Var*)LangElement::find( "viewToTangent" );
  259. if ( !viewToTangent )
  260. {
  261. Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
  262. if ( !fd.features[MFT_ParticleNormal] )
  263. {
  264. // turn obj->tangent into world->tangent
  265. viewToTangent = new Var;
  266. viewToTangent->setType( "float3x3" );
  267. viewToTangent->setName( "viewToTangent" );
  268. LangElement *viewToTangentDecl = new DecOp( viewToTangent );
  269. // Get the view->obj transform
  270. Var *viewToObj = getInvWorldView( componentList, fd.features[MFT_UseInstancing], meta );
  271. // assign world->tangent transform
  272. meta->addStatement( new GenOp( " @ = mul( @, (float3x3)@ );\r\n", viewToTangentDecl, texSpaceMat, viewToObj ) );
  273. }
  274. else
  275. {
  276. // Assume particle normal generation has set this up in the proper space
  277. viewToTangent = texSpaceMat;
  278. }
  279. }
  280. // send transform to pixel shader
  281. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  282. outViewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  283. outViewToTangent->setName( "outViewToTangent" );
  284. outViewToTangent->setStructName( "OUT" );
  285. outViewToTangent->setType( "float3x3" );
  286. meta->addStatement( new GenOp( " @ = @;\r\n", outViewToTangent, viewToTangent ) );
  287. return outViewToTangent;
  288. }
  289. Var* ShaderFeatureHLSL::getOutTexCoord( const char *name,
  290. const char *type,
  291. bool useTexAnim,
  292. MultiLine *meta,
  293. Vector<ShaderComponent*> &componentList )
  294. {
  295. String outTexName = String::ToString( "out_%s", name );
  296. Var *texCoord = (Var*)LangElement::find( outTexName );
  297. if ( !texCoord )
  298. {
  299. Var *inTex = getVertTexCoord( name );
  300. AssertFatal( inTex, "ShaderFeatureHLSL::getOutTexCoord - Unknown vertex input coord!" );
  301. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  302. texCoord = connectComp->getElement( RT_TEXCOORD );
  303. texCoord->setName( outTexName );
  304. texCoord->setStructName( "OUT" );
  305. texCoord->setType( type );
  306. if ( useTexAnim )
  307. {
  308. inTex->setType( "float2" );
  309. // create texture mat var
  310. Var *texMat = new Var;
  311. texMat->setType( "float4x4" );
  312. texMat->setName( "texMat" );
  313. texMat->uniform = true;
  314. texMat->constSortPos = cspPass;
  315. // Statement allows for casting of different types which
  316. // eliminates vector truncation problems.
  317. String statement = String::ToString(" @ = (%s)mul(@, float4(@,1,1));\r\n", type);
  318. meta->addStatement( new GenOp( statement, texCoord, texMat, inTex ) );
  319. }
  320. else
  321. {
  322. // Statement allows for casting of different types which
  323. // eliminates vector truncation problems.
  324. String statement = String::ToString( " @ = (%s)@;\r\n", type );
  325. meta->addStatement( new GenOp( statement, texCoord, inTex ) );
  326. }
  327. }
  328. AssertFatal( dStrcmp( type, (const char*)texCoord->type ) == 0,
  329. "ShaderFeatureHLSL::getOutTexCoord - Type mismatch!" );
  330. return texCoord;
  331. }
  332. Var* ShaderFeatureHLSL::getInTexCoord( const char *name,
  333. const char *type,
  334. Vector<ShaderComponent*> &componentList )
  335. {
  336. Var* texCoord = (Var*)LangElement::find( name );
  337. if ( !texCoord )
  338. {
  339. ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
  340. texCoord = connectComp->getElement( RT_TEXCOORD );
  341. texCoord->setName( name );
  342. texCoord->setStructName( "IN" );
  343. texCoord->setType( type );
  344. }
  345. AssertFatal( dStrcmp( type, (const char*)texCoord->type ) == 0,
  346. "ShaderFeatureHLSL::getInTexCoord - Type mismatch!" );
  347. return texCoord;
  348. }
  349. Var* ShaderFeatureHLSL::getInColor( const char *name,
  350. const char *type,
  351. Vector<ShaderComponent*> &componentList )
  352. {
  353. Var *inColor = (Var*)LangElement::find( name );
  354. if ( !inColor )
  355. {
  356. ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
  357. inColor = connectComp->getElement( RT_COLOR );
  358. inColor->setName( name );
  359. inColor->setStructName( "IN" );
  360. inColor->setType( type );
  361. }
  362. AssertFatal( dStrcmp( type, (const char*)inColor->type ) == 0,
  363. "ShaderFeatureHLSL::getInColor - Type mismatch!" );
  364. return inColor;
  365. }
  366. Var* ShaderFeatureHLSL::addOutVpos( MultiLine *meta,
  367. Vector<ShaderComponent*> &componentList )
  368. {
  369. // Nothing to do if we're on SM 3.0... we use the real vpos.
  370. if ( GFX->getPixelShaderVersion() >= 3.0f )
  371. return NULL;
  372. // For SM 2.x we need to generate the vpos in the vertex shader
  373. // and pass it as a texture coord to the pixel shader.
  374. Var *outVpos = (Var*)LangElement::find( "outVpos" );
  375. if ( !outVpos )
  376. {
  377. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  378. outVpos = connectComp->getElement( RT_TEXCOORD );
  379. outVpos->setName( "outVpos" );
  380. outVpos->setStructName( "OUT" );
  381. outVpos->setType( "float4" );
  382. Var *outPosition = (Var*) LangElement::find( "hpos" );
  383. AssertFatal( outPosition, "ShaderFeatureHLSL::addOutVpos - Didn't find the output position." );
  384. meta->addStatement( new GenOp( " @ = @;\r\n", outVpos, outPosition ) );
  385. }
  386. return outVpos;
  387. }
  388. Var* ShaderFeatureHLSL::getInVpos( MultiLine *meta,
  389. Vector<ShaderComponent*> &componentList )
  390. {
  391. Var *inVpos = (Var*)LangElement::find( "vpos" );
  392. if ( inVpos )
  393. return inVpos;
  394. ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>(componentList[C_CONNECTOR]);
  395. inVpos = connectComp->getElement( RT_SVPOSITION );
  396. inVpos->setName( "vpos" );
  397. inVpos->setStructName( "IN" );
  398. inVpos->setType( "float4" );
  399. return inVpos;
  400. }
  401. Var* ShaderFeatureHLSL::getInWorldToTangent( Vector<ShaderComponent*> &componentList )
  402. {
  403. Var *worldToTangent = (Var*)LangElement::find( "worldToTangent" );
  404. if ( !worldToTangent )
  405. {
  406. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  407. worldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  408. worldToTangent->setName( "worldToTangent" );
  409. worldToTangent->setStructName( "IN" );
  410. worldToTangent->setType( "float3x3" );
  411. }
  412. return worldToTangent;
  413. }
  414. Var* ShaderFeatureHLSL::getInViewToTangent( Vector<ShaderComponent*> &componentList )
  415. {
  416. Var *viewToTangent = (Var*)LangElement::find( "viewToTangent" );
  417. if ( !viewToTangent )
  418. {
  419. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  420. viewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  421. viewToTangent->setName( "viewToTangent" );
  422. viewToTangent->setStructName( "IN" );
  423. viewToTangent->setType( "float3x3" );
  424. }
  425. return viewToTangent;
  426. }
  427. Var* ShaderFeatureHLSL::getNormalMapTex()
  428. {
  429. Var *normalMap = (Var*)LangElement::find("bumpMap");
  430. if (!normalMap)
  431. {
  432. normalMap = new Var;
  433. normalMap->setType("SamplerState");
  434. normalMap->setName("bumpMap");
  435. normalMap->uniform = true;
  436. normalMap->sampler = true;
  437. normalMap->constNum = Var::getTexUnitNum();
  438. Var* normalMapTex = new Var;
  439. normalMapTex->setName("bumpMapTex");
  440. normalMapTex->setType("Texture2D");
  441. normalMapTex->uniform = true;
  442. normalMapTex->texture = true;
  443. normalMapTex->constNum = normalMap->constNum;
  444. }
  445. return normalMap;
  446. }
  447. Var* ShaderFeatureHLSL::getObjTrans( Vector<ShaderComponent*> &componentList,
  448. bool useInstancing,
  449. MultiLine *meta )
  450. {
  451. Var *objTrans = (Var*)LangElement::find( "objTrans" );
  452. if ( objTrans )
  453. return objTrans;
  454. if ( useInstancing )
  455. {
  456. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  457. Var *instObjTrans = vertStruct->getElement( RT_TEXCOORD, 4, 4 );
  458. instObjTrans->setStructName( "IN" );
  459. instObjTrans->setName( "inst_objectTrans" );
  460. mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+0 );
  461. mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+1 );
  462. mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+2 );
  463. mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+3 );
  464. objTrans = new Var;
  465. objTrans->setType( "float4x4" );
  466. objTrans->setName( "objTrans" );
  467. meta->addStatement( new GenOp( " @ = { // Instancing!\r\n", new DecOp( objTrans ), instObjTrans ) );
  468. meta->addStatement( new GenOp( " @[0],\r\n", instObjTrans ) );
  469. meta->addStatement( new GenOp( " @[1],\r\n", instObjTrans ) );
  470. meta->addStatement( new GenOp( " @[2],\r\n",instObjTrans ) );
  471. meta->addStatement( new GenOp( " @[3] };\r\n", instObjTrans ) );
  472. }
  473. else
  474. {
  475. objTrans = new Var;
  476. objTrans->setType( "float4x4" );
  477. objTrans->setName( "objTrans" );
  478. objTrans->uniform = true;
  479. objTrans->constSortPos = cspPrimitive;
  480. }
  481. return objTrans;
  482. }
  483. Var* ShaderFeatureHLSL::getModelView( Vector<ShaderComponent*> &componentList,
  484. bool useInstancing,
  485. MultiLine *meta )
  486. {
  487. Var *modelview = (Var*)LangElement::find( "modelview" );
  488. if ( modelview )
  489. return modelview;
  490. if ( useInstancing )
  491. {
  492. Var *objTrans = getObjTrans( componentList, useInstancing, meta );
  493. Var *viewProj = (Var*)LangElement::find( "viewProj" );
  494. if ( !viewProj )
  495. {
  496. viewProj = new Var;
  497. viewProj->setType( "float4x4" );
  498. viewProj->setName( "viewProj" );
  499. viewProj->uniform = true;
  500. viewProj->constSortPos = cspPass;
  501. }
  502. modelview = new Var;
  503. modelview->setType( "float4x4" );
  504. modelview->setName( "modelview" );
  505. meta->addStatement( new GenOp( " @ = mul( @, @ ); // Instancing!\r\n", new DecOp( modelview ), viewProj, objTrans ) );
  506. }
  507. else
  508. {
  509. modelview = new Var;
  510. modelview->setType( "float4x4" );
  511. modelview->setName( "modelview" );
  512. modelview->uniform = true;
  513. modelview->constSortPos = cspPrimitive;
  514. }
  515. return modelview;
  516. }
  517. Var* ShaderFeatureHLSL::getWorldView( Vector<ShaderComponent*> &componentList,
  518. bool useInstancing,
  519. MultiLine *meta )
  520. {
  521. Var *worldView = (Var*)LangElement::find( "worldViewOnly" );
  522. if ( worldView )
  523. return worldView;
  524. if ( useInstancing )
  525. {
  526. Var *objTrans = getObjTrans( componentList, useInstancing, meta );
  527. Var *worldToCamera = (Var*)LangElement::find( "worldToCamera" );
  528. if ( !worldToCamera )
  529. {
  530. worldToCamera = new Var;
  531. worldToCamera->setType( "float4x4" );
  532. worldToCamera->setName( "worldToCamera" );
  533. worldToCamera->uniform = true;
  534. worldToCamera->constSortPos = cspPass;
  535. }
  536. worldView = new Var;
  537. worldView->setType( "float4x4" );
  538. worldView->setName( "worldViewOnly" );
  539. meta->addStatement( new GenOp( " @ = mul( @, @ ); // Instancing!\r\n", new DecOp( worldView ), worldToCamera, objTrans ) );
  540. }
  541. else
  542. {
  543. worldView = new Var;
  544. worldView->setType( "float4x4" );
  545. worldView->setName( "worldViewOnly" );
  546. worldView->uniform = true;
  547. worldView->constSortPos = cspPrimitive;
  548. }
  549. return worldView;
  550. }
  551. Var* ShaderFeatureHLSL::getInvWorldView( Vector<ShaderComponent*> &componentList,
  552. bool useInstancing,
  553. MultiLine *meta )
  554. {
  555. Var *viewToObj = (Var*)LangElement::find( "viewToObj" );
  556. if ( viewToObj )
  557. return viewToObj;
  558. if ( useInstancing )
  559. {
  560. Var *worldView = getWorldView( componentList, useInstancing, meta );
  561. viewToObj = new Var;
  562. viewToObj->setType( "float3x3" );
  563. viewToObj->setName( "viewToObj" );
  564. // We just use transpose to convert the 3x3 portion
  565. // of the world view transform into its inverse.
  566. meta->addStatement( new GenOp( " @ = transpose( (float3x3)@ ); // Instancing!\r\n", new DecOp( viewToObj ), worldView ) );
  567. }
  568. else
  569. {
  570. viewToObj = new Var;
  571. viewToObj->setType( "float4x4" );
  572. viewToObj->setName( "viewToObj" );
  573. viewToObj->uniform = true;
  574. viewToObj->constSortPos = cspPrimitive;
  575. }
  576. return viewToObj;
  577. }
  578. void ShaderFeatureHLSL::getWsPosition( Vector<ShaderComponent*> &componentList,
  579. bool useInstancing,
  580. MultiLine *meta,
  581. LangElement *wsPosition )
  582. {
  583. Var *inPosition = (Var*)LangElement::find( "wsPosition" );
  584. if ( inPosition )
  585. {
  586. meta->addStatement( new GenOp( " @ = @.xyz;\r\n",
  587. wsPosition, inPosition ) );
  588. return;
  589. }
  590. // Get the input position.
  591. inPosition = (Var*)LangElement::find( "inPosition" );
  592. if ( !inPosition )
  593. inPosition = (Var*)LangElement::find( "position" );
  594. AssertFatal( inPosition, "ShaderFeatureHLSL::getWsPosition - The vertex position was not found!" );
  595. Var *objTrans = getObjTrans( componentList, useInstancing, meta );
  596. meta->addStatement( new GenOp( " @ = mul( @, float4( @.xyz, 1 ) ).xyz;\r\n",
  597. wsPosition, objTrans, inPosition ) );
  598. }
  599. Var* ShaderFeatureHLSL::addOutWsPosition( Vector<ShaderComponent*> &componentList,
  600. bool useInstancing,
  601. MultiLine *meta )
  602. {
  603. Var *outWsPosition = (Var*)LangElement::find( "outWsPosition" );
  604. if ( !outWsPosition )
  605. {
  606. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  607. outWsPosition = connectComp->getElement( RT_TEXCOORD );
  608. outWsPosition->setName( "outWsPosition" );
  609. outWsPosition->setStructName( "OUT" );
  610. outWsPosition->setType( "float3" );
  611. getWsPosition( componentList, useInstancing, meta, outWsPosition );
  612. }
  613. return outWsPosition;
  614. }
  615. Var* ShaderFeatureHLSL::getInWsPosition( Vector<ShaderComponent*> &componentList )
  616. {
  617. Var *wsPosition = (Var*)LangElement::find( "wsPosition" );
  618. if ( !wsPosition )
  619. {
  620. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  621. wsPosition = connectComp->getElement( RT_TEXCOORD );
  622. wsPosition->setName( "wsPosition" );
  623. wsPosition->setStructName( "IN" );
  624. wsPosition->setType( "float3" );
  625. }
  626. return wsPosition;
  627. }
  628. Var* ShaderFeatureHLSL::getWsView( Var *wsPosition, MultiLine *meta )
  629. {
  630. Var *wsView = (Var*)LangElement::find( "wsView" );
  631. if ( !wsView )
  632. {
  633. wsView = new Var( "wsView", "float3" );
  634. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  635. if ( !eyePos )
  636. {
  637. eyePos = new Var;
  638. eyePos->setType( "float3" );
  639. eyePos->setName( "eyePosWorld" );
  640. eyePos->uniform = true;
  641. eyePos->constSortPos = cspPass;
  642. }
  643. meta->addStatement( new GenOp( " @ = normalize( @ - @ );\r\n",
  644. new DecOp( wsView ), eyePos, wsPosition ) );
  645. }
  646. return wsView;
  647. }
  648. Var* ShaderFeatureHLSL::addOutDetailTexCoord( Vector<ShaderComponent*> &componentList,
  649. MultiLine *meta,
  650. bool useTexAnim )
  651. {
  652. // Check if its already added.
  653. Var *outTex = (Var*)LangElement::find( "detCoord" );
  654. if ( outTex )
  655. return outTex;
  656. // Grab incoming texture coords.
  657. Var *inTex = getVertTexCoord( "texCoord" );
  658. inTex->setType("float2");
  659. // create detail variable
  660. Var *detScale = new Var;
  661. detScale->setType( "float2" );
  662. detScale->setName( "detailScale" );
  663. detScale->uniform = true;
  664. detScale->constSortPos = cspPotentialPrimitive;
  665. // grab connector texcoord register
  666. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  667. outTex = connectComp->getElement( RT_TEXCOORD );
  668. outTex->setName( "detCoord" );
  669. outTex->setStructName( "OUT" );
  670. outTex->setType( "float2" );
  671. if ( useTexAnim )
  672. {
  673. // Find or create the texture matrix.
  674. Var *texMat = (Var*)LangElement::find( "texMat" );
  675. if ( !texMat )
  676. {
  677. texMat = new Var;
  678. texMat->setType( "float4x4" );
  679. texMat->setName( "texMat" );
  680. texMat->uniform = true;
  681. texMat->constSortPos = cspPass;
  682. }
  683. meta->addStatement(new GenOp(" @ = mul(@, float4(@,1,1)).xy * @;\r\n", outTex, texMat, inTex, detScale));
  684. }
  685. else
  686. {
  687. // setup output to mul texCoord by detail scale
  688. meta->addStatement( new GenOp( " @ = @ * @;\r\n", outTex, inTex, detScale ) );
  689. }
  690. return outTex;
  691. }
  692. Var* ShaderFeatureHLSL::getSurface(Vector<ShaderComponent*>& componentList, MultiLine* meta, const MaterialFeatureData& fd)
  693. {
  694. ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(componentList[C_CONNECTOR]);
  695. Var* diffuseColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  696. Var* matinfo = (Var*)LangElement::find("PBRConfig");
  697. if (!matinfo)
  698. {
  699. Var* metalness = (Var*)LangElement::find("metalness");
  700. if (!metalness)
  701. {
  702. metalness = new Var("metalness", "float");
  703. metalness->uniform = true;
  704. metalness->constSortPos = cspPotentialPrimitive;
  705. }
  706. Var* smoothness = (Var*)LangElement::find("smoothness");
  707. if (!smoothness)
  708. {
  709. smoothness = new Var("smoothness", "float");
  710. smoothness->uniform = true;
  711. smoothness->constSortPos = cspPotentialPrimitive;
  712. }
  713. matinfo = new Var("PBRConfig", "float4");
  714. LangElement* colorDecl = new DecOp(matinfo);
  715. meta->addStatement(new GenOp(" @ = float4(0.0,1.0,@,@);\r\n", colorDecl, smoothness, metalness)); //reconstruct matinfo, no ao darkening
  716. }
  717. Var* inTex = getInTexCoord("texCoord", "float2", componentList);
  718. if (!inTex)
  719. return nullptr;
  720. Var* wsNormal = (Var*)LangElement::find("wsNormal");
  721. Var* normal = (Var*)LangElement::find("normal");
  722. if (!normal)
  723. {
  724. normal = new Var("normal", "float3");
  725. meta->addStatement(new GenOp(" @;\r\n\n", new DecOp(normal)));
  726. if (!fd.features[MFT_NormalMap])
  727. {
  728. Var* worldToTangent = getInWorldToTangent(componentList);
  729. meta->addStatement(new GenOp(" @ = normalize(mul(@,float3(0,0,1.0f)));\r\n\n", normal, worldToTangent));
  730. }
  731. else
  732. {
  733. meta->addStatement(new GenOp(" @ = normalize( half3( @ ) );\r\n", normal, wsNormal));
  734. }
  735. }
  736. Var* wsEyePos = (Var*)LangElement::find("eyePosWorld");
  737. Var* wsPosition = getInWsPosition(componentList);
  738. Var* wsView = getWsView(wsPosition, meta);
  739. Var* surface = (Var*)LangElement::find("surface");
  740. if (!surface)
  741. {
  742. surface = new Var("surface", "Surface");
  743. meta->addStatement(new GenOp(" @ = createForwardSurface(@,@,@,@,@,@,@);\r\n\n", new DecOp(surface), diffuseColor, normal, matinfo,
  744. inTex, wsPosition, wsEyePos, wsView));
  745. }
  746. return surface;
  747. }
  748. //****************************************************************************
  749. // Base Texture
  750. //****************************************************************************
  751. DiffuseMapFeatHLSL::DiffuseMapFeatHLSL()
  752. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl"))
  753. {
  754. addDependency(&mTorqueDep);
  755. }
  756. void DiffuseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  757. const MaterialFeatureData &fd )
  758. {
  759. MultiLine *meta = new MultiLine;
  760. getOutTexCoord( "texCoord",
  761. "float2",
  762. fd.features[MFT_TexAnim],
  763. meta,
  764. componentList );
  765. output = meta;
  766. }
  767. U32 DiffuseMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  768. {
  769. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  770. }
  771. void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  772. const MaterialFeatureData &fd )
  773. {
  774. // grab connector texcoord register
  775. Var *inTex = getInTexCoord( "texCoord", "float2", componentList );
  776. //determine output target
  777. ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
  778. if (fd.features[MFT_isDeferred])
  779. targ = ShaderFeature::RenderTarget1;
  780. // create texture var
  781. Var *diffuseMap = new Var;
  782. diffuseMap->setType( "SamplerState" );
  783. diffuseMap->setName( "diffuseMap" );
  784. diffuseMap->uniform = true;
  785. diffuseMap->sampler = true;
  786. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  787. Var* diffuseMapTex = new Var;
  788. diffuseMapTex->setName("diffuseMapTex");
  789. diffuseMapTex->setType("Texture2D");
  790. diffuseMapTex->uniform = true;
  791. diffuseMapTex->texture = true;
  792. diffuseMapTex->constNum = diffuseMap->constNum;
  793. // create sample color
  794. Var *diffColor = new Var;
  795. diffColor->setType("float4");
  796. diffColor->setName("diffuseColor");
  797. LangElement *colorDecl = new DecOp(diffColor);
  798. MultiLine * meta = new MultiLine;
  799. output = meta;
  800. if ( fd.features[MFT_CubeMap] )
  801. {
  802. meta->addStatement(new GenOp(" @ = @.Sample(@, @);\r\n", colorDecl, diffuseMapTex, diffuseMap, inTex));
  803. meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ)));
  804. }
  805. else if(fd.features[MFT_DiffuseMapAtlas])
  806. {
  807. // Handle atlased textures
  808. // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
  809. Var *atlasedTex = new Var;
  810. atlasedTex->setName("atlasedTexCoord");
  811. atlasedTex->setType("float2");
  812. LangElement *atDecl = new DecOp(atlasedTex);
  813. // Parameters of the texture atlas
  814. Var *atParams = new Var;
  815. atParams->setType("float4");
  816. atParams->setName("diffuseAtlasParams");
  817. atParams->uniform = true;
  818. atParams->constSortPos = cspPotentialPrimitive;
  819. // Parameters of the texture (tile) this object is using in the atlas
  820. Var *tileParams = new Var;
  821. tileParams->setType("float4");
  822. tileParams->setName("diffuseAtlasTileParams");
  823. tileParams->uniform = true;
  824. tileParams->constSortPos = cspPotentialPrimitive;
  825. const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
  826. if(is_sm3)
  827. {
  828. // Figure out the mip level
  829. meta->addStatement(new GenOp(" float2 _dx = ddx(@ * @.z);\r\n", inTex, atParams));
  830. meta->addStatement(new GenOp(" float2 _dy = ddy(@ * @.z);\r\n", inTex, atParams));
  831. meta->addStatement(new GenOp(" float mipLod = 0.5 * log2(max(dot(_dx, _dx), dot(_dy, _dy)));\r\n"));
  832. meta->addStatement(new GenOp(" mipLod = clamp(mipLod, 0.0, @.w);\r\n", atParams));
  833. // And the size of the mip level
  834. meta->addStatement(new GenOp(" float mipPixSz = pow(2.0, @.w - mipLod);\r\n", atParams));
  835. meta->addStatement(new GenOp(" float2 mipSz = mipPixSz / @.xy;\r\n", atParams));
  836. }
  837. else
  838. {
  839. meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
  840. }
  841. // Tiling mode
  842. // TODO: Select wrap or clamp somehow
  843. if( true ) // Wrap
  844. meta->addStatement(new GenOp(" @ = frac(@);\r\n", atDecl, inTex));
  845. else // Clamp
  846. meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, inTex));
  847. // Finally scale/offset, and correct for filtering
  848. meta->addStatement(new GenOp(" @ = @ * ((mipSz * @.xy - 1.0) / mipSz) + 0.5 / mipSz + @.xy * @.xy;\r\n",
  849. atlasedTex, atlasedTex, atParams, atParams, tileParams));
  850. // Add a newline
  851. meta->addStatement(new GenOp( "\r\n"));
  852. // For the rest of the feature...
  853. inTex = atlasedTex;
  854. // To dump out UV coords...
  855. //#define DEBUG_ATLASED_UV_COORDS
  856. #ifdef DEBUG_ATLASED_UV_COORDS
  857. if(!fd.features[MFT_DeferredConditioner])
  858. {
  859. meta->addStatement(new GenOp(" @ = float4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
  860. meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
  861. return;
  862. }
  863. #endif
  864. meta->addStatement(new GenOp(" @ = @.SampleLevel(@,@,mipLod);\r\n",
  865. new DecOp(diffColor), diffuseMapTex, diffuseMap, inTex));
  866. meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
  867. }
  868. else
  869. {
  870. meta->addStatement(new GenOp("@ = @.Sample(@, @);\r\n", colorDecl, diffuseMapTex, diffuseMap, inTex));
  871. meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ)));
  872. }
  873. }
  874. ShaderFeature::Resources DiffuseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  875. {
  876. Resources res;
  877. res.numTex = 1;
  878. res.numTexReg = 1;
  879. return res;
  880. }
  881. void DiffuseMapFeatHLSL::setTexData( Material::StageData &stageDat,
  882. const MaterialFeatureData &fd,
  883. RenderPassData &passData,
  884. U32 &texIndex )
  885. {
  886. GFXTextureObject *tex = stageDat.getTex( MFT_DiffuseMap );
  887. if ( tex )
  888. {
  889. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  890. passData.mTexSlot[ texIndex++ ].texObject = tex;
  891. }
  892. }
  893. //****************************************************************************
  894. // Overlay Texture
  895. //****************************************************************************
  896. void OverlayTexFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  897. const MaterialFeatureData &fd )
  898. {
  899. Var *inTex = getVertTexCoord( "texCoord2" );
  900. AssertFatal( inTex, "OverlayTexFeatHLSL::processVert() - The second UV set was not found!" );
  901. // grab connector texcoord register
  902. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  903. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  904. outTex->setName( "outTexCoord2" );
  905. outTex->setStructName( "OUT" );
  906. outTex->setType( "float2" );
  907. if( fd.features[MFT_TexAnim] )
  908. {
  909. inTex->setType( "float4" );
  910. // Find or create the texture matrix.
  911. Var *texMat = (Var*)LangElement::find( "texMat" );
  912. if ( !texMat )
  913. {
  914. texMat = new Var;
  915. texMat->setType( "float4x4" );
  916. texMat->setName( "texMat" );
  917. texMat->uniform = true;
  918. texMat->constSortPos = cspPass;
  919. }
  920. output = new GenOp( " @ = mul(@, @);\r\n", outTex, texMat, inTex );
  921. return;
  922. }
  923. // setup language elements to output incoming tex coords to output
  924. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  925. }
  926. void OverlayTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  927. const MaterialFeatureData &fd )
  928. {
  929. // grab connector texcoord register
  930. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  931. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  932. inTex->setName( "texCoord2" );
  933. inTex->setStructName( "IN" );
  934. inTex->setType( "float2" );
  935. // create texture var
  936. Var *diffuseMap = new Var;
  937. diffuseMap->setType( "SamplerState" );
  938. diffuseMap->setName( "overlayMap" );
  939. diffuseMap->uniform = true;
  940. diffuseMap->sampler = true;
  941. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  942. Var* diffuseMapTex = new Var;
  943. diffuseMapTex->setName("overlayMapTex");
  944. diffuseMapTex->setType("Texture2D");
  945. diffuseMapTex->uniform = true;
  946. diffuseMapTex->texture = true;
  947. diffuseMapTex->constNum = diffuseMap->constNum;
  948. LangElement *statement = new GenOp("@.Sample(@, @)", diffuseMapTex, diffuseMap, inTex);
  949. output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
  950. }
  951. ShaderFeature::Resources OverlayTexFeatHLSL::getResources( const MaterialFeatureData &fd )
  952. {
  953. Resources res;
  954. res.numTex = 1;
  955. res.numTexReg = 1;
  956. return res;
  957. }
  958. void OverlayTexFeatHLSL::setTexData( Material::StageData &stageDat,
  959. const MaterialFeatureData &fd,
  960. RenderPassData &passData,
  961. U32 &texIndex )
  962. {
  963. GFXTextureObject *tex = stageDat.getTex( MFT_OverlayMap );
  964. if ( tex )
  965. {
  966. passData.mSamplerNames[texIndex] = "overlayMap";
  967. passData.mTexSlot[ texIndex++ ].texObject = tex;
  968. }
  969. }
  970. //****************************************************************************
  971. // Diffuse color
  972. //****************************************************************************
  973. U32 DiffuseFeatureHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  974. {
  975. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  976. }
  977. void DiffuseFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  978. const MaterialFeatureData &fd )
  979. {
  980. Var *diffuseMaterialColor = new Var;
  981. diffuseMaterialColor->setType( "float4" );
  982. diffuseMaterialColor->setName( "diffuseMaterialColor" );
  983. diffuseMaterialColor->uniform = true;
  984. diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
  985. MultiLine* meta = new MultiLine;
  986. Var *col = (Var*)LangElement::find("col");
  987. ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
  988. if (fd.features[MFT_isDeferred])
  989. {
  990. targ = ShaderFeature::RenderTarget1;
  991. col = (Var*)LangElement::find("col1");
  992. if (!col)
  993. {
  994. // create color var
  995. col = new Var;
  996. col->setType("fragout");
  997. col->setName(getOutputTargetVarName(targ));
  998. col->setStructName("OUT");
  999. meta->addStatement(new GenOp(" @ = float4(1.0,1.0,1.0,1.0);\r\n", col));
  1000. }
  1001. }
  1002. Material::BlendOp op;
  1003. if (fd.features[MFT_DiffuseMap])
  1004. op = Material::Mul;
  1005. else
  1006. op = Material::None;
  1007. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffuseMaterialColor, op, NULL, targ ) ) );
  1008. output = meta;
  1009. }
  1010. //****************************************************************************
  1011. // Diffuse vertex color
  1012. //****************************************************************************
  1013. void DiffuseVertColorFeatureHLSL::processVert( Vector< ShaderComponent* >& componentList,
  1014. const MaterialFeatureData& fd )
  1015. {
  1016. // Create vertex color connector if it doesn't exist.
  1017. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1018. if( !outColor )
  1019. {
  1020. // Search for vert color.
  1021. Var* inColor = dynamic_cast< Var* >( LangElement::find( "diffuse" ) );
  1022. if( !inColor )
  1023. {
  1024. output = NULL;
  1025. return;
  1026. }
  1027. // Create connector.
  1028. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1029. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1030. outColor = connectComp->getElement( RT_COLOR );
  1031. outColor->setName( "vertColor" );
  1032. outColor->setStructName( "OUT" );
  1033. outColor->setType( "float4" );
  1034. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1035. }
  1036. else
  1037. output = NULL; // Nothing we need to do.
  1038. }
  1039. void DiffuseVertColorFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1040. const MaterialFeatureData &fd )
  1041. {
  1042. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1043. if( !vertColor )
  1044. {
  1045. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1046. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1047. vertColor = connectComp->getElement( RT_COLOR );
  1048. vertColor->setName( "vertColor" );
  1049. vertColor->setStructName( "IN" );
  1050. vertColor->setType( "float4" );
  1051. }
  1052. MultiLine* meta = new MultiLine;
  1053. if (fd.features[MFT_isDeferred])
  1054. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul, NULL, ShaderFeature::RenderTarget1)));
  1055. else
  1056. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul)));
  1057. output = meta;
  1058. }
  1059. //****************************************************************************
  1060. // Lightmap
  1061. //****************************************************************************
  1062. void LightmapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1063. const MaterialFeatureData &fd )
  1064. {
  1065. // grab tex register from incoming vert
  1066. Var *inTex = getVertTexCoord( "texCoord2" );
  1067. // grab connector texcoord register
  1068. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1069. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1070. outTex->setName( "texCoord2" );
  1071. outTex->setStructName( "OUT" );
  1072. outTex->setType( "float2" );
  1073. // setup language elements to output incoming tex coords to output
  1074. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  1075. }
  1076. void LightmapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1077. const MaterialFeatureData &fd )
  1078. {
  1079. // grab connector texcoord register
  1080. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1081. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  1082. inTex->setName( "texCoord2" );
  1083. inTex->setStructName( "IN" );
  1084. inTex->setType( "float2" );
  1085. // create texture var
  1086. Var *lightMap = new Var;
  1087. lightMap->setType( "SamplerState" );
  1088. lightMap->setName( "lightMap" );
  1089. lightMap->uniform = true;
  1090. lightMap->sampler = true;
  1091. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1092. Var *lightMapTex = new Var;
  1093. lightMapTex->setName("lightMapTex");
  1094. lightMapTex->setType("Texture2D");
  1095. lightMapTex->uniform = true;
  1096. lightMapTex->texture = true;
  1097. lightMapTex->constNum = lightMap->constNum;
  1098. // argh, pixel specular should prob use this too
  1099. if( fd.features[MFT_NormalMap] )
  1100. {
  1101. Var *lmColor = new Var;
  1102. lmColor->setName( "lmColor" );
  1103. lmColor->setType( "float4" );
  1104. LangElement *lmColorDecl = new DecOp( lmColor );
  1105. output = new GenOp(" @ = @.Sample(@, @);\r\n", lmColorDecl, lightMapTex, lightMap, inTex);
  1106. return;
  1107. }
  1108. // Add realtime lighting, if it is available
  1109. LangElement *statement = NULL;
  1110. if( fd.features[MFT_RTLighting] )
  1111. {
  1112. // Advanced lighting is the only dynamic lighting supported right now
  1113. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1114. if(inColor != NULL)
  1115. {
  1116. // Find out if RTLighting should be added or substituted
  1117. bool bPreProcessedLighting = false;
  1118. AdvancedLightBinManager *lightBin;
  1119. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1120. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1121. // Lightmap has already been included in the advanced light bin, so
  1122. // no need to do any sampling or anything
  1123. if (bPreProcessedLighting)
  1124. statement = new GenOp("float4(@, 1.0)", inColor);
  1125. else
  1126. {
  1127. statement = new GenOp("@.Sample(@, @) + float4(@.rgb, 0.0)", lightMapTex, lightMap, inTex, inColor);
  1128. }
  1129. }
  1130. }
  1131. // If we still don't have it... then just sample the lightmap.
  1132. if (!statement)
  1133. {
  1134. statement = new GenOp("@.Sample(@, @)", lightMapTex, lightMap, inTex);
  1135. }
  1136. // Assign to proper render target
  1137. MultiLine *meta = new MultiLine;
  1138. if( fd.features[MFT_LightbufferMRT] )
  1139. {
  1140. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1141. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1142. }
  1143. else
  1144. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
  1145. output = meta;
  1146. }
  1147. ShaderFeature::Resources LightmapFeatHLSL::getResources( const MaterialFeatureData &fd )
  1148. {
  1149. Resources res;
  1150. res.numTex = 1;
  1151. res.numTexReg = 1;
  1152. return res;
  1153. }
  1154. void LightmapFeatHLSL::setTexData( Material::StageData &stageDat,
  1155. const MaterialFeatureData &fd,
  1156. RenderPassData &passData,
  1157. U32 &texIndex )
  1158. {
  1159. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  1160. passData.mSamplerNames[ texIndex ] = "lightMap";
  1161. if ( tex )
  1162. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1163. else
  1164. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  1165. }
  1166. U32 LightmapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1167. {
  1168. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1169. }
  1170. //****************************************************************************
  1171. // Tonemap
  1172. //****************************************************************************
  1173. void TonemapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1174. const MaterialFeatureData &fd )
  1175. {
  1176. // Grab the connector
  1177. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1178. // Set up the second set of texCoords
  1179. Var *inTex2 = getVertTexCoord( "texCoord2" );
  1180. if ( inTex2 )
  1181. {
  1182. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  1183. outTex2->setName( "texCoord2" );
  1184. outTex2->setStructName( "OUT" );
  1185. outTex2->setType( "float2" );
  1186. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  1187. }
  1188. }
  1189. void TonemapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1190. const MaterialFeatureData &fd )
  1191. {
  1192. // Grab connector
  1193. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1194. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  1195. inTex2->setName( "texCoord2" );
  1196. inTex2->setStructName( "IN" );
  1197. inTex2->setType( "float2" );
  1198. // create texture var
  1199. Var *toneMap = new Var;
  1200. toneMap->setType( "SamplerState" );
  1201. toneMap->setName( "toneMap" );
  1202. toneMap->uniform = true;
  1203. toneMap->sampler = true;
  1204. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1205. Var *toneMapTex = new Var;
  1206. toneMapTex->setName("toneMapTex");
  1207. toneMapTex->setType("Texture2D");
  1208. toneMapTex->uniform = true;
  1209. toneMapTex->texture = true;
  1210. toneMapTex->constNum = toneMap->constNum;
  1211. MultiLine * meta = new MultiLine;
  1212. // First get the toneMap color
  1213. Var *toneMapColor = new Var;
  1214. toneMapColor->setType( "float4" );
  1215. toneMapColor->setName( "toneMapColor" );
  1216. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  1217. meta->addStatement(new GenOp(" @ = @.Sample(@, @);\r\n", toneMapColorDecl, toneMapTex, toneMap, inTex2));
  1218. // We do a different calculation if there is a diffuse map or not
  1219. Material::BlendOp blendOp = Material::Mul;
  1220. if ( fd.features[MFT_DiffuseMap] )
  1221. {
  1222. // Reverse the tonemap
  1223. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
  1224. // Re-tonemap with the current color factored in
  1225. blendOp = Material::ToneMap;
  1226. }
  1227. // Find out if RTLighting should be added
  1228. bool bPreProcessedLighting = false;
  1229. AdvancedLightBinManager *lightBin;
  1230. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1231. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1232. // Add in the realtime lighting contribution
  1233. if ( fd.features[MFT_RTLighting] )
  1234. {
  1235. // Right now, only Advanced Lighting is supported
  1236. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1237. if(inColor != NULL)
  1238. {
  1239. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1240. // the dynamic light buffer, and it already has the tonemap included
  1241. if(bPreProcessedLighting)
  1242. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  1243. else
  1244. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  1245. }
  1246. }
  1247. // Assign to proper render target
  1248. if( fd.features[MFT_LightbufferMRT] )
  1249. {
  1250. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1251. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1252. }
  1253. else
  1254. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  1255. output = meta;
  1256. }
  1257. ShaderFeature::Resources TonemapFeatHLSL::getResources( const MaterialFeatureData &fd )
  1258. {
  1259. Resources res;
  1260. res.numTex = 1;
  1261. res.numTexReg = 1;
  1262. return res;
  1263. }
  1264. void TonemapFeatHLSL::setTexData( Material::StageData &stageDat,
  1265. const MaterialFeatureData &fd,
  1266. RenderPassData &passData,
  1267. U32 &texIndex )
  1268. {
  1269. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  1270. if ( tex )
  1271. {
  1272. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  1273. passData.mSamplerNames[ texIndex ] = "toneMap";
  1274. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1275. }
  1276. }
  1277. U32 TonemapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1278. {
  1279. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1280. }
  1281. //****************************************************************************
  1282. // pureLIGHT Lighting
  1283. //****************************************************************************
  1284. void VertLitHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1285. const MaterialFeatureData &fd )
  1286. {
  1287. // If we have a lightMap or toneMap then our lighting will be
  1288. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1289. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1290. {
  1291. output = NULL;
  1292. return;
  1293. }
  1294. // Create vertex color connector if it doesn't exist.
  1295. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1296. if( !outColor )
  1297. {
  1298. // Search for vert color
  1299. Var *inColor = (Var*) LangElement::find( "diffuse" );
  1300. // If there isn't a vertex color then we can't do anything
  1301. if( !inColor )
  1302. {
  1303. output = NULL;
  1304. return;
  1305. }
  1306. // Grab the connector color
  1307. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1308. outColor = connectComp->getElement( RT_COLOR );
  1309. outColor->setName( "vertColor" );
  1310. outColor->setStructName( "OUT" );
  1311. outColor->setType( "float4" );
  1312. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1313. }
  1314. else
  1315. output = NULL; // Nothing we need to do.
  1316. }
  1317. void VertLitHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1318. const MaterialFeatureData &fd )
  1319. {
  1320. // If we have a lightMap or toneMap then our lighting will be
  1321. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1322. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1323. {
  1324. output = NULL;
  1325. return;
  1326. }
  1327. // Grab the connector color register
  1328. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1329. if( !vertColor )
  1330. {
  1331. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1332. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1333. vertColor = connectComp->getElement( RT_COLOR );
  1334. vertColor->setName( "vertColor" );
  1335. vertColor->setStructName( "IN" );
  1336. vertColor->setType( "float4" );
  1337. }
  1338. MultiLine * meta = new MultiLine;
  1339. // Defaults (no diffuse map)
  1340. Material::BlendOp blendOp = Material::Mul;
  1341. LangElement *outColor = vertColor;
  1342. // We do a different calculation if there is a diffuse map or not
  1343. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1344. {
  1345. Var * finalVertColor = new Var;
  1346. finalVertColor->setName( "finalVertColor" );
  1347. finalVertColor->setType( "float4" );
  1348. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1349. // Reverse the tonemap
  1350. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
  1351. // Set the blend op to tonemap
  1352. blendOp = Material::ToneMap;
  1353. outColor = finalVertColor;
  1354. }
  1355. // Add in the realtime lighting contribution, if applicable
  1356. if ( fd.features[MFT_RTLighting] )
  1357. {
  1358. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1359. if(rtLightingColor != NULL)
  1360. {
  1361. bool bPreProcessedLighting = false;
  1362. AdvancedLightBinManager *lightBin;
  1363. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1364. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1365. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1366. // the dynamic light buffer, and it already has the baked-vertex-color
  1367. // included in it
  1368. if(bPreProcessedLighting)
  1369. outColor = new GenOp( "float4(@.rgb, 1.0)", rtLightingColor );
  1370. else
  1371. outColor = new GenOp( "float4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
  1372. }
  1373. }
  1374. // Output the color
  1375. if ( fd.features[MFT_LightbufferMRT] )
  1376. {
  1377. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1378. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1379. }
  1380. else
  1381. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1382. output = meta;
  1383. }
  1384. U32 VertLitHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1385. {
  1386. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1387. }
  1388. //****************************************************************************
  1389. // Detail map
  1390. //****************************************************************************
  1391. void DetailFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1392. const MaterialFeatureData &fd )
  1393. {
  1394. MultiLine *meta = new MultiLine;
  1395. addOutDetailTexCoord( componentList,
  1396. meta,
  1397. fd.features[MFT_TexAnim] );
  1398. output = meta;
  1399. }
  1400. void DetailFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1401. const MaterialFeatureData &fd )
  1402. {
  1403. // Get the detail texture coord.
  1404. Var *inTex = getInTexCoord( "detCoord", "float2", componentList );
  1405. // create texture var
  1406. Var *detailMap = new Var;
  1407. detailMap->setType( "SamplerState" );
  1408. detailMap->setName( "detailMap" );
  1409. detailMap->uniform = true;
  1410. detailMap->sampler = true;
  1411. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1412. Var* detailMapTex = new Var;
  1413. detailMapTex->setName("detailMapTex");
  1414. detailMapTex->setType("Texture2D");
  1415. detailMapTex->uniform = true;
  1416. detailMapTex->texture = true;
  1417. detailMapTex->constNum = detailMap->constNum;
  1418. // We're doing the standard greyscale detail map
  1419. // technique which can darken and lighten the
  1420. // diffuse texture.
  1421. // TODO: We could add a feature to toggle between this
  1422. // and a simple multiplication with the detail map.
  1423. LangElement *statement = new GenOp("( @.Sample(@, @) * 2.0 ) - 1.0", detailMapTex, detailMap, inTex);
  1424. if ( fd.features[MFT_isDeferred])
  1425. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add, NULL, ShaderFeature::RenderTarget1 ) );
  1426. else
  1427. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1428. }
  1429. ShaderFeature::Resources DetailFeatHLSL::getResources( const MaterialFeatureData &fd )
  1430. {
  1431. Resources res;
  1432. res.numTex = 1;
  1433. res.numTexReg = 1;
  1434. return res;
  1435. }
  1436. void DetailFeatHLSL::setTexData( Material::StageData &stageDat,
  1437. const MaterialFeatureData &fd,
  1438. RenderPassData &passData,
  1439. U32 &texIndex )
  1440. {
  1441. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1442. if ( tex )
  1443. {
  1444. passData.mSamplerNames[texIndex] = "detailMap";
  1445. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1446. }
  1447. }
  1448. //****************************************************************************
  1449. // Vertex position
  1450. //****************************************************************************
  1451. void VertPositionHLSL::determineFeature( Material *material,
  1452. const GFXVertexFormat *vertexFormat,
  1453. U32 stageNum,
  1454. const FeatureType &type,
  1455. const FeatureSet &features,
  1456. MaterialFeatureData *outFeatureData )
  1457. {
  1458. // This feature is always on!
  1459. outFeatureData->features.addFeature( type );
  1460. }
  1461. void VertPositionHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1462. const MaterialFeatureData &fd )
  1463. {
  1464. // First check for an input position from a previous feature
  1465. // then look for the default vertex position.
  1466. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1467. if ( !inPosition )
  1468. inPosition = (Var*)LangElement::find( "position" );
  1469. // grab connector position
  1470. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1471. Var *outPosition = connectComp->getElement(RT_SVPOSITION);
  1472. outPosition->setName( "hpos" );
  1473. outPosition->setStructName( "OUT" );
  1474. MultiLine *meta = new MultiLine;
  1475. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1476. meta->addStatement( new GenOp( " @ = mul(@, float4(@.xyz,1));\r\n",
  1477. outPosition, modelview, inPosition ) );
  1478. if (fd.materialFeatures[MFT_SkyBox])
  1479. {
  1480. meta->addStatement(new GenOp(" @ = @.xyww;\r\n", outPosition, outPosition));
  1481. }
  1482. output = meta;
  1483. }
  1484. void VertPositionHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1485. const MaterialFeatureData &fd)
  1486. {
  1487. // grab connector position
  1488. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  1489. Var *outPosition = connectComp->getElement(RT_SVPOSITION);
  1490. outPosition->setName("vpos");
  1491. outPosition->setStructName("IN");
  1492. }
  1493. //****************************************************************************
  1494. // Reflect Cubemap
  1495. //****************************************************************************
  1496. void ReflectCubeFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1497. const MaterialFeatureData &fd )
  1498. {
  1499. // search for vert normal
  1500. Var *inNormal = (Var*) LangElement::find( "normal" );
  1501. if ( !inNormal )
  1502. return;
  1503. MultiLine * meta = new MultiLine;
  1504. // If a base or bump tex is present in the material, but not in the
  1505. // current pass - we need to add one to the current pass to use
  1506. // its alpha channel as a gloss map. Here we just need the tex coords.
  1507. if( !fd.features[MFT_DiffuseMap] &&
  1508. !fd.features[MFT_NormalMap] )
  1509. {
  1510. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1511. fd.materialFeatures[MFT_NormalMap] )
  1512. {
  1513. // find incoming texture var
  1514. Var *inTex = getVertTexCoord( "texCoord" );
  1515. // grab connector texcoord register
  1516. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1517. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1518. outTex->setName( "texCoord" );
  1519. outTex->setStructName( "OUT" );
  1520. outTex->setType( "float2" );
  1521. // setup language elements to output incoming tex coords to output
  1522. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1523. }
  1524. }
  1525. // create cubeTrans
  1526. bool useInstancing = fd.features[MFT_UseInstancing];
  1527. Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
  1528. // cube vert position
  1529. Var * cubeVertPos = new Var;
  1530. cubeVertPos->setName( "cubeVertPos" );
  1531. cubeVertPos->setType( "float3" );
  1532. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1533. meta->addStatement( new GenOp( " @ = mul(@, float4(@,1)).xyz;\r\n",
  1534. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1535. // cube normal
  1536. Var * cubeNormal = new Var;
  1537. cubeNormal->setName( "cubeNormal" );
  1538. cubeNormal->setType( "float3" );
  1539. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1540. meta->addStatement(new GenOp(" @ = ( mul( (@), float4(@, 0) ) ).xyz;\r\n",
  1541. cubeNormDecl, cubeTrans, inNormal));
  1542. meta->addStatement(new GenOp(" @ = bool(length(@)) ? normalize(@) : @;\r\n",
  1543. cubeNormal, cubeNormal, cubeNormal, cubeNormal));
  1544. // grab the eye position
  1545. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1546. if ( !eyePos )
  1547. {
  1548. eyePos = new Var( "eyePosWorld", "float3" );
  1549. eyePos->uniform = true;
  1550. eyePos->constSortPos = cspPass;
  1551. }
  1552. // eye to vert
  1553. Var * eyeToVert = new Var;
  1554. eyeToVert->setName( "eyeToVert" );
  1555. eyeToVert->setType( "float3" );
  1556. LangElement *e2vDecl = new DecOp( eyeToVert );
  1557. meta->addStatement( new GenOp( " @ = @ - @;\r\n",
  1558. e2vDecl, cubeVertPos, eyePos ) );
  1559. // grab connector texcoord register
  1560. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1561. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1562. reflectVec->setName( "reflectVec" );
  1563. reflectVec->setStructName( "OUT" );
  1564. reflectVec->setType( "float3" );
  1565. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1566. output = meta;
  1567. }
  1568. void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1569. const MaterialFeatureData &fd )
  1570. {
  1571. MultiLine * meta = new MultiLine;
  1572. Var *glossColor = NULL;
  1573. // If a base or bump tex is present in the material, but not in the
  1574. // current pass - we need to add one to the current pass to use
  1575. // its alpha channel as a gloss map.
  1576. if( !fd.features[MFT_DiffuseMap] &&
  1577. !fd.features[MFT_NormalMap])
  1578. {
  1579. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1580. fd.materialFeatures[MFT_NormalMap])
  1581. {
  1582. // grab connector texcoord register
  1583. Var *inTex = getInTexCoord("texCoord", "float2", componentList);
  1584. // create texture var
  1585. Var *newMap = new Var;
  1586. newMap->setType( "SamplerState" );
  1587. newMap->setName( "glossMap" );
  1588. newMap->uniform = true;
  1589. newMap->sampler = true;
  1590. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1591. Var* glowMapTex = new Var;
  1592. glowMapTex->setName("glowMapTex");
  1593. glowMapTex->setType("Texture2D");
  1594. glowMapTex->uniform = true;
  1595. glowMapTex->texture = true;
  1596. glowMapTex->constNum = newMap->constNum;
  1597. // create sample color
  1598. Var *color = new Var;
  1599. color->setType( "float4" );
  1600. color->setName( "diffuseColor" );
  1601. LangElement *colorDecl = new DecOp( color );
  1602. glossColor = color;
  1603. meta->addStatement(new GenOp(" @ = @.Sample( @, @ );\r\n", colorDecl, glowMapTex, newMap, inTex));
  1604. }
  1605. }
  1606. if (!glossColor)
  1607. {
  1608. if (fd.features[MFT_isDeferred])
  1609. glossColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1610. if (!glossColor)
  1611. glossColor = (Var*)LangElement::find("diffuseColor");
  1612. if (!glossColor)
  1613. glossColor = (Var*)LangElement::find("bumpNormal");
  1614. }
  1615. // grab connector texcoord register
  1616. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1617. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1618. reflectVec->setName( "reflectVec" );
  1619. reflectVec->setStructName( "IN" );
  1620. reflectVec->setType( "float3" );
  1621. // create cubemap var
  1622. Var *cubeMap = new Var;
  1623. cubeMap->setType( "SamplerState" );
  1624. cubeMap->setName( "cubeMap" );
  1625. cubeMap->uniform = true;
  1626. cubeMap->sampler = true;
  1627. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1628. Var* cubeMapTex = new Var;
  1629. cubeMapTex->setName("cubeMapTex");
  1630. cubeMapTex->setType("TextureCube"); // cubeMapTex->setType("TextureCube");
  1631. cubeMapTex->uniform = true;
  1632. cubeMapTex->texture = true;
  1633. cubeMapTex->constNum = cubeMap->constNum;
  1634. Var *cubeMips = new Var;
  1635. cubeMips->setType("float");
  1636. cubeMips->setName("cubeMips");
  1637. cubeMips->uniform = true;
  1638. cubeMips->constSortPos = cspPotentialPrimitive;
  1639. // TODO: Restore the lighting attenuation here!
  1640. Var *attn = NULL;
  1641. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1642. //attn = (Var*)LangElement::find("attn");
  1643. //else
  1644. if ( fd.materialFeatures[MFT_RTLighting] )
  1645. attn =(Var*)LangElement::find("d_NL_Att");
  1646. LangElement *texCube = NULL;
  1647. Var* matinfo = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  1648. Var *smoothness = (Var*)LangElement::find("smoothness");
  1649. if (smoothness) //try to grab smoothness directly
  1650. {
  1651. texCube = new GenOp("@.SampleLevel( @, float3(@).rgb, min((1.0 - @)*@ + 1.0, @))", cubeMapTex, cubeMap, reflectVec, smoothness, cubeMips, cubeMips);
  1652. }
  1653. else if (glossColor)//failing that, try and find color data
  1654. {
  1655. texCube = new GenOp("@.SampleLevel( @, float3(@).rgb, min((1.0 - @.b)*@ + 1.0, @))", cubeMapTex, cubeMap, reflectVec, glossColor, cubeMips, cubeMips);
  1656. }
  1657. else //failing *that*, just draw the cubemap
  1658. {
  1659. texCube = new GenOp("@.Sample( @, @ )", cubeMapTex, cubeMap, reflectVec);
  1660. }
  1661. LangElement *lerpVal = NULL;
  1662. Material::BlendOp blendOp = Material::LerpAlpha;
  1663. // Note that the lerpVal needs to be a float4 so that
  1664. // it will work with the LerpAlpha blend.
  1665. if (matinfo)
  1666. {
  1667. if (attn)
  1668. lerpVal = new GenOp("@ * saturate( @ )", matinfo, attn);
  1669. else
  1670. lerpVal = new GenOp("@", matinfo);
  1671. }
  1672. else if ( glossColor )
  1673. {
  1674. if ( attn )
  1675. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1676. else
  1677. lerpVal = glossColor;
  1678. }
  1679. else
  1680. {
  1681. if ( attn )
  1682. lerpVal = new GenOp( "saturate( @ ).xxxx", attn );
  1683. else
  1684. blendOp = Material::Mul;
  1685. }
  1686. Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  1687. if (fd.features[MFT_isDeferred])
  1688. {
  1689. //metalness: black(0) = color, white(1) = reflection
  1690. if (fd.features[MFT_ToneMap])
  1691. meta->addStatement(new GenOp(" @ *= @;\r\n", targ, texCube));
  1692. else
  1693. meta->addStatement(new GenOp(" @ = @;\r\n", targ, texCube));
  1694. }
  1695. else
  1696. {
  1697. meta->addStatement(new GenOp(" //forward lit cubemapping\r\n"));
  1698. targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  1699. Var *metalness = (Var*)LangElement::find("metalness");
  1700. if (metalness)
  1701. {
  1702. Var *dColor = new Var("dColor", "float3");
  1703. Var *reflectColor = new Var("reflectColor", "float3");
  1704. meta->addStatement(new GenOp(" @ = @.rgb - (@.rgb * @);\r\n", new DecOp(dColor), targ, targ, metalness));
  1705. meta->addStatement(new GenOp(" @ = @.rgb*(@).rgb*@;\r\n", new DecOp(reflectColor), targ, texCube, metalness));
  1706. meta->addStatement(new GenOp(" @.rgb = @+@;\r\n", targ, dColor, reflectColor));
  1707. }
  1708. else if (lerpVal)
  1709. meta->addStatement(new GenOp(" @ *= float4(@.rgb*@.a, @.a);\r\n", targ, texCube, lerpVal, targ));
  1710. else
  1711. meta->addStatement(new GenOp(" @.rgb *= @.rgb;\r\n", targ, texCube));
  1712. }
  1713. output = meta;
  1714. }
  1715. ShaderFeature::Resources ReflectCubeFeatHLSL::getResources( const MaterialFeatureData &fd )
  1716. {
  1717. Resources res;
  1718. if( fd.features[MFT_DiffuseMap] ||
  1719. fd.features[MFT_NormalMap] )
  1720. {
  1721. res.numTex = 1;
  1722. res.numTexReg = 1;
  1723. }
  1724. else
  1725. {
  1726. res.numTex = 2;
  1727. res.numTexReg = 2;
  1728. }
  1729. return res;
  1730. }
  1731. void ReflectCubeFeatHLSL::setTexData( Material::StageData &stageDat,
  1732. const MaterialFeatureData &stageFeatures,
  1733. RenderPassData &passData,
  1734. U32 &texIndex )
  1735. {
  1736. // set up a gloss map if one is not present in the current pass
  1737. // but is present in the current material stage
  1738. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1739. !passData.mFeatureData.features[MFT_NormalMap] )
  1740. {
  1741. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1742. if ( tex && stageFeatures.features[MFT_DiffuseMap] )
  1743. {
  1744. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  1745. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1746. }
  1747. else
  1748. {
  1749. tex = stageDat.getTex( MFT_NormalMap );
  1750. if ( tex && stageFeatures.features[ MFT_NormalMap ] )
  1751. {
  1752. passData.mSamplerNames[ texIndex ] = "bumpMap";
  1753. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1754. }
  1755. }
  1756. }
  1757. if( stageDat.getCubemap() )
  1758. {
  1759. passData.mCubeMap = stageDat.getCubemap();
  1760. passData.mSamplerNames[texIndex] = "cubeMap";
  1761. passData.mTexType[texIndex++] = Material::Cube;
  1762. }
  1763. else
  1764. {
  1765. if( stageFeatures.features[MFT_CubeMap] )
  1766. {
  1767. // assuming here that it is a scenegraph cubemap
  1768. passData.mSamplerNames[texIndex] = "cubeMap";
  1769. passData.mTexType[texIndex++] = Material::SGCube;
  1770. }
  1771. }
  1772. }
  1773. //****************************************************************************
  1774. // RTLighting
  1775. //****************************************************************************
  1776. RTLightingFeatHLSL::RTLightingFeatHLSL()
  1777. : mDep(ShaderGen::smCommonShaderPath + String("/lighting.hlsl" ))
  1778. {
  1779. addDependency( &mDep );
  1780. }
  1781. void RTLightingFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1782. const MaterialFeatureData &fd )
  1783. {
  1784. MultiLine *meta = new MultiLine;
  1785. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1786. getOutWorldToTangent(componentList, meta, fd);
  1787. // Special case for lighting imposters. We dont have a vert normal and may not
  1788. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1789. if ( fd.features[MFT_ImposterVert] )
  1790. {
  1791. if ( !fd.features[MFT_NormalMap] )
  1792. {
  1793. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1794. if ( !eyePos )
  1795. {
  1796. eyePos = new Var( "eyePosWorld", "float3" );
  1797. eyePos->uniform = true;
  1798. eyePos->constSortPos = cspPass;
  1799. }
  1800. Var *inPosition = (Var*)LangElement::find( "position" );
  1801. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1802. outNormal->setName( "wsNormal" );
  1803. outNormal->setStructName( "OUT" );
  1804. outNormal->setType( "float3" );
  1805. // Transform the normal to world space.
  1806. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );
  1807. }
  1808. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1809. output = meta;
  1810. return;
  1811. }
  1812. // Find the incoming vertex normal.
  1813. Var *inNormal = (Var*)LangElement::find( "normal" );
  1814. // Skip out on realtime lighting if we don't have a normal
  1815. // or we're doing some sort of baked lighting.
  1816. if ( !inNormal ||
  1817. fd.features[MFT_LightMap] ||
  1818. fd.features[MFT_ToneMap] ||
  1819. fd.features[MFT_VertLit] )
  1820. return;
  1821. // If there isn't a normal map then we need to pass
  1822. // the world space normal to the pixel shader ourselves.
  1823. if ( !fd.features[MFT_NormalMap] )
  1824. {
  1825. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1826. outNormal->setName( "wsNormal" );
  1827. outNormal->setStructName( "OUT" );
  1828. outNormal->setType( "float3" );
  1829. // Get the transform to world space.
  1830. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1831. // Transform the normal to world space.
  1832. meta->addStatement( new GenOp( " @ = mul( @, float4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1833. }
  1834. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1835. output = meta;
  1836. }
  1837. void RTLightingFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1838. const MaterialFeatureData &fd )
  1839. {
  1840. // Skip out on realtime lighting if we don't have a normal
  1841. // or we're doing some sort of baked lighting.
  1842. //
  1843. // TODO: We can totally detect for this in the material
  1844. // feature setup... we should move it out of here!
  1845. //
  1846. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1847. return;
  1848. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1849. MultiLine *meta = new MultiLine;
  1850. // Now the wsPosition and wsView.
  1851. Var *wsPosition = getInWsPosition( componentList );
  1852. Var *wsView = getWsView( wsPosition, meta );
  1853. // Look for a light mask generated from a previous
  1854. // feature (this is done for BL terrain lightmaps).
  1855. LangElement *lightMask = LangElement::find( "lightMask" );
  1856. if ( !lightMask )
  1857. lightMask = new GenOp( "float4( 1, 1, 1, 1 )" );
  1858. // Get all the light constants.
  1859. Var *inLightPos = new Var( "inLightPos", "float4" );
  1860. inLightPos->uniform = true;
  1861. inLightPos->arraySize = 4;
  1862. inLightPos->constSortPos = cspPotentialPrimitive;
  1863. Var * inLightConfigData = new Var( "inLightConfigData", "float4" );
  1864. inLightConfigData->uniform = true;
  1865. inLightConfigData->arraySize = 4;
  1866. inLightConfigData->constSortPos = cspPotentialPrimitive;
  1867. Var *inLightColor = new Var( "inLightColor", "float4" );
  1868. inLightColor->uniform = true;
  1869. inLightColor->arraySize = 4;
  1870. inLightColor->constSortPos = cspPotentialPrimitive;
  1871. Var *inLightSpotDir = new Var( "inLightSpotDir", "float4" );
  1872. inLightSpotDir->uniform = true;
  1873. inLightSpotDir->arraySize = 4;
  1874. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1875. Var * lightSpotParams = new Var( "lightSpotParams", "float4" );
  1876. lightSpotParams->uniform = true;
  1877. lightSpotParams->arraySize = 4;
  1878. lightSpotParams->constSortPos = cspPotentialPrimitive;
  1879. Var* hasVectorLight = new Var("hasVectorLight", "int");
  1880. hasVectorLight->uniform = true;
  1881. hasVectorLight->constSortPos = cspPotentialPrimitive;
  1882. Var* vectorLightDirection = new Var("vectorLightDirection", "float4");
  1883. vectorLightDirection->uniform = true;
  1884. vectorLightDirection->constSortPos = cspPotentialPrimitive;
  1885. Var* vectorLightColor = new Var("vectorLightColor", "float4");
  1886. vectorLightColor->uniform = true;
  1887. vectorLightColor->constSortPos = cspPotentialPrimitive;
  1888. Var* vectorLightBrightness = new Var("vectorLightBrightness", "float");
  1889. vectorLightBrightness->uniform = true;
  1890. vectorLightBrightness->constSortPos = cspPotentialPrimitive;
  1891. Var* surface = getSurface(componentList, meta, fd);
  1892. if (!surface)
  1893. {
  1894. Con::errorf("ShaderGen::RTLightingFeatHLSL() - failed to generate surface!");
  1895. return;
  1896. }
  1897. Var *smoothness = (Var*)LangElement::find("smoothness");
  1898. Var *metalness = (Var*)LangElement::find("metalness");
  1899. Var *curColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  1900. Var *ambient = new Var( "ambient", "float4" );
  1901. ambient->uniform = true;
  1902. ambient->constSortPos = cspPass;
  1903. Var* lighting = new Var("lighting", "float4");
  1904. meta->addStatement(new GenOp(" @ = compute4Lights( @, @, @, @,\r\n"
  1905. " @, @, @, @, @, @, @);\r\n",
  1906. new DecOp(lighting), surface, lightMask, inLightPos, inLightConfigData, inLightColor, inLightSpotDir, lightSpotParams,
  1907. hasVectorLight, vectorLightDirection, vectorLightColor, vectorLightBrightness));
  1908. meta->addStatement(new GenOp(" @.rgb += @.rgb;\r\n", curColor, lighting));
  1909. output = meta;
  1910. }
  1911. ShaderFeature::Resources RTLightingFeatHLSL::getResources( const MaterialFeatureData &fd )
  1912. {
  1913. Resources res;
  1914. // These features disable realtime lighting.
  1915. if ( !fd.features[MFT_LightMap] &&
  1916. !fd.features[MFT_ToneMap] &&
  1917. !fd.features[MFT_VertLit] )
  1918. {
  1919. // If enabled we pass the position.
  1920. res.numTexReg = 1;
  1921. // If there isn't a bump map then we pass the
  1922. // world space normal as well.
  1923. if ( !fd.features[MFT_NormalMap] )
  1924. res.numTexReg++;
  1925. }
  1926. return res;
  1927. }
  1928. //****************************************************************************
  1929. // Fog
  1930. //****************************************************************************
  1931. FogFeatHLSL::FogFeatHLSL()
  1932. : mFogDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" ))
  1933. {
  1934. addDependency( &mFogDep );
  1935. }
  1936. void FogFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1937. const MaterialFeatureData &fd )
  1938. {
  1939. MultiLine *meta = new MultiLine;
  1940. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1941. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1942. {
  1943. // Grab the eye position.
  1944. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1945. if ( !eyePos )
  1946. {
  1947. eyePos = new Var( "eyePosWorld", "float3" );
  1948. eyePos->uniform = true;
  1949. eyePos->constSortPos = cspPass;
  1950. }
  1951. Var *fogData = new Var( "fogData", "float3" );
  1952. fogData->uniform = true;
  1953. fogData->constSortPos = cspPass;
  1954. Var *wsPosition = new Var( "fogPos", "float3" );
  1955. getWsPosition( componentList,
  1956. fd.features[MFT_UseInstancing],
  1957. meta,
  1958. new DecOp( wsPosition ) );
  1959. // We pass the fog amount to the pixel shader.
  1960. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1961. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1962. fogAmount->setName( "fogAmount" );
  1963. fogAmount->setStructName( "OUT" );
  1964. fogAmount->setType( "float" );
  1965. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1966. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1967. }
  1968. else
  1969. {
  1970. // We fog in world space... make sure the world space
  1971. // position is passed to the pixel shader. This is
  1972. // often already passed for lighting, so it takes up
  1973. // no extra output registers.
  1974. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1975. }
  1976. output = meta;
  1977. }
  1978. void FogFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1979. const MaterialFeatureData &fd )
  1980. {
  1981. MultiLine *meta = new MultiLine;
  1982. Var *fogColor = new Var;
  1983. fogColor->setType( "float4" );
  1984. fogColor->setName( "fogColor" );
  1985. fogColor->uniform = true;
  1986. fogColor->constSortPos = cspPass;
  1987. // Get the out color.
  1988. Var *color = (Var*) LangElement::find( "col" );
  1989. if ( !color )
  1990. {
  1991. color = new Var;
  1992. color->setType( "fragout" );
  1993. color->setName( "col" );
  1994. color->setStructName( "OUT" );
  1995. }
  1996. Var *fogAmount;
  1997. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1998. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1999. {
  2000. // Per-vertex.... just get the fog amount.
  2001. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2002. fogAmount = connectComp->getElement( RT_TEXCOORD );
  2003. fogAmount->setName( "fogAmount" );
  2004. fogAmount->setStructName( "IN" );
  2005. fogAmount->setType( "float" );
  2006. }
  2007. else
  2008. {
  2009. Var *wsPosition = getInWsPosition( componentList );
  2010. // grab the eye position
  2011. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2012. if ( !eyePos )
  2013. {
  2014. eyePos = new Var( "eyePosWorld", "float3" );
  2015. eyePos->uniform = true;
  2016. eyePos->constSortPos = cspPass;
  2017. }
  2018. Var *fogData = new Var( "fogData", "float3" );
  2019. fogData->uniform = true;
  2020. fogData->constSortPos = cspPass;
  2021. /// Get the fog amount.
  2022. fogAmount = new Var( "fogAmount", "float" );
  2023. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  2024. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  2025. }
  2026. // Lerp between the fog color and diffuse color.
  2027. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  2028. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  2029. output = meta;
  2030. }
  2031. ShaderFeature::Resources FogFeatHLSL::getResources( const MaterialFeatureData &fd )
  2032. {
  2033. Resources res;
  2034. res.numTexReg = 1;
  2035. return res;
  2036. }
  2037. //****************************************************************************
  2038. // Visibility
  2039. //****************************************************************************
  2040. VisibilityFeatHLSL::VisibilityFeatHLSL()
  2041. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" ))
  2042. {
  2043. addDependency( &mTorqueDep );
  2044. }
  2045. void VisibilityFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2046. const MaterialFeatureData &fd )
  2047. {
  2048. MultiLine *meta = new MultiLine;
  2049. output = meta;
  2050. if ( fd.features[ MFT_UseInstancing ] )
  2051. {
  2052. // We pass the visibility to the pixel shader via
  2053. // another output register.
  2054. //
  2055. // TODO: We should see if we can share this register
  2056. // with some other common instanced data.
  2057. //
  2058. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2059. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  2060. outVisibility->setStructName( "OUT" );
  2061. outVisibility->setName( "visibility" );
  2062. outVisibility->setType( "float" );
  2063. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  2064. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  2065. instVisibility->setStructName( "IN" );
  2066. instVisibility->setName( "inst_visibility" );
  2067. instVisibility->setType( "float" );
  2068. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  2069. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  2070. }
  2071. if ( fd.features[ MFT_IsTranslucent ] )
  2072. return;
  2073. addOutVpos( meta, componentList );
  2074. }
  2075. void VisibilityFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2076. const MaterialFeatureData &fd )
  2077. {
  2078. // Get the visibility constant.
  2079. Var *visibility = NULL;
  2080. if ( fd.features[ MFT_UseInstancing ] )
  2081. visibility = getInTexCoord( "visibility", "float", componentList );
  2082. else
  2083. {
  2084. visibility = (Var*)LangElement::find( "visibility" );
  2085. if ( !visibility )
  2086. {
  2087. visibility = new Var();
  2088. visibility->setType( "float" );
  2089. visibility->setName( "visibility" );
  2090. visibility->uniform = true;
  2091. visibility->constSortPos = cspPotentialPrimitive;
  2092. }
  2093. }
  2094. MultiLine *meta = new MultiLine;
  2095. output = meta;
  2096. // Translucent objects do a simple alpha fade.
  2097. if ( fd.features[ MFT_IsTranslucent ] )
  2098. {
  2099. Var *color = (Var*)LangElement::find( "col" );
  2100. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  2101. return;
  2102. }
  2103. // Everything else does a fizzle.
  2104. Var *vPos = getInVpos( meta, componentList );
  2105. // vpos is a float4 in d3d11
  2106. meta->addStatement( new GenOp( " fizzle( @.xy, @ );\r\n", vPos, visibility ) );
  2107. }
  2108. ShaderFeature::Resources VisibilityFeatHLSL::getResources( const MaterialFeatureData &fd )
  2109. {
  2110. Resources res;
  2111. // TODO: Fix for instancing.
  2112. if ( !fd.features[ MFT_IsTranslucent ] )
  2113. res.numTexReg = 1;
  2114. return res;
  2115. }
  2116. //****************************************************************************
  2117. // AlphaTest
  2118. //****************************************************************************
  2119. void AlphaTestHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2120. const MaterialFeatureData &fd )
  2121. {
  2122. // If we're below SM3 and don't have a depth output
  2123. // feature then don't waste an instruction here.
  2124. if ( GFX->getPixelShaderVersion() < 3.0 &&
  2125. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  2126. !fd.features[ MFT_DepthOut ] )
  2127. {
  2128. output = NULL;
  2129. return;
  2130. }
  2131. // If we don't have a color var then we cannot do an alpha test.
  2132. Var *color = (Var*)LangElement::find( "col1" );
  2133. if (!color)
  2134. color = (Var*)LangElement::find("col");
  2135. if ( !color )
  2136. {
  2137. output = NULL;
  2138. return;
  2139. }
  2140. // Now grab the alpha test value.
  2141. Var *alphaTestVal = new Var;
  2142. alphaTestVal->setType( "float" );
  2143. alphaTestVal->setName( "alphaTestValue" );
  2144. alphaTestVal->uniform = true;
  2145. alphaTestVal->constSortPos = cspPotentialPrimitive;
  2146. // Do the clip.
  2147. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  2148. }
  2149. //****************************************************************************
  2150. // GlowMask
  2151. //****************************************************************************
  2152. void GlowMaskHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2153. const MaterialFeatureData &fd )
  2154. {
  2155. output = NULL;
  2156. // Get the output color... and make it black to mask out
  2157. // glow passes rendered before us.
  2158. //
  2159. // The shader compiler will optimize out all the other
  2160. // code above that doesn't contribute to the alpha mask.
  2161. Var *color = (Var*)LangElement::find( "col" );
  2162. if ( color )
  2163. output = new GenOp( " @.rgb = 0;\r\n", color );
  2164. }
  2165. //****************************************************************************
  2166. // RenderTargetZero
  2167. //****************************************************************************
  2168. void RenderTargetZeroHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  2169. {
  2170. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  2171. // This will prevent a divide by zero causing an FP special on float render targets
  2172. output = new GenOp( " @;\r\n", assignColor( new GenOp( "0.00001" ), Material::None, NULL, mOutputTargetMask ) );
  2173. }
  2174. //****************************************************************************
  2175. // HDR Output
  2176. //****************************************************************************
  2177. HDROutHLSL::HDROutHLSL()
  2178. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" ))
  2179. {
  2180. addDependency( &mTorqueDep );
  2181. }
  2182. void HDROutHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2183. const MaterialFeatureData &fd )
  2184. {
  2185. // Let the helper function do the work.
  2186. Var *color = (Var*)LangElement::find( "col" );
  2187. if ( color )
  2188. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2189. }
  2190. //****************************************************************************
  2191. // FoliageFeatureHLSL
  2192. //****************************************************************************
  2193. #include "T3D/fx/groundCover.h"
  2194. FoliageFeatureHLSL::FoliageFeatureHLSL()
  2195. : mDep(ShaderGen::smCommonShaderPath + String("/foliage.hlsl" ))
  2196. {
  2197. addDependency( &mDep );
  2198. }
  2199. void FoliageFeatureHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2200. const MaterialFeatureData &fd )
  2201. {
  2202. // Get the input variables we need.
  2203. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2204. if ( !inPosition )
  2205. inPosition = (Var*)LangElement::find( "position" );
  2206. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2207. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2208. MultiLine *meta = new MultiLine;
  2209. // Declare the normal and tangent variables since they do not exist
  2210. // in this vert type, but we do need to set them up for others.
  2211. Var *normal = (Var*)LangElement::find( "normal" );
  2212. AssertFatal( normal, "FoliageFeatureHLSL requires vert normal!" );
  2213. Var *tangent = new Var;
  2214. tangent->setType( "float3" );
  2215. tangent->setName( "T" );
  2216. LangElement *tangentDec = new DecOp( tangent );
  2217. meta->addStatement( new GenOp( " @;\n", tangentDec ) );
  2218. // We add a float foliageFade to the OUT structure.
  2219. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2220. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2221. fade->setName( "foliageFade" );
  2222. fade->setStructName( "OUT" );
  2223. fade->setType( "float" );
  2224. // grab the eye position
  2225. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2226. if ( !eyePos )
  2227. {
  2228. eyePos = new Var( "eyePosWorld", "float3" );
  2229. eyePos->uniform = true;
  2230. eyePos->constSortPos = cspPass;
  2231. }
  2232. // All actual work is offloaded to this method.
  2233. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2234. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2235. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2236. output = meta;
  2237. }
  2238. void FoliageFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2239. const MaterialFeatureData &fd )
  2240. {
  2241. // Find / create IN.foliageFade
  2242. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2243. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2244. fade->setName( "foliageFade" );
  2245. fade->setStructName( "IN" );
  2246. fade->setType( "float" );
  2247. // Find / create visibility
  2248. Var *visibility = (Var*) LangElement::find( "visibility" );
  2249. if ( !visibility )
  2250. {
  2251. visibility = new Var();
  2252. visibility->setType( "float" );
  2253. visibility->setName( "visibility" );
  2254. visibility->uniform = true;
  2255. visibility->constSortPos = cspPotentialPrimitive;
  2256. }
  2257. MultiLine *meta = new MultiLine;
  2258. // Multiply foliageFade into visibility.
  2259. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2260. output = meta;
  2261. }
  2262. void FoliageFeatureHLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2263. {
  2264. // This isn't really necessary since the outFeatureData will be filtered after
  2265. // this call.
  2266. if ( features.hasFeature( MFT_Foliage ) )
  2267. outFeatureData->features.addFeature( type );
  2268. }
  2269. ShaderFeatureConstHandles* FoliageFeatureHLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2270. {
  2271. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2272. AssertFatal( gcover != NULL, "FoliageFeatureHLSL::createConstHandles - userObject was not valid!" );
  2273. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2274. handles->mGroundCover = gcover;
  2275. handles->init( shader );
  2276. return handles;
  2277. }
  2278. void ParticleNormalFeatureHLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2279. {
  2280. MultiLine *meta = new MultiLine;
  2281. output = meta;
  2282. // Calculate normal and tangent values since we want to keep particle verts
  2283. // as light-weight as possible
  2284. Var *normal = (Var*) LangElement::find("normal");
  2285. if(normal == NULL)
  2286. {
  2287. normal = new Var;
  2288. normal->setType( "float3" );
  2289. normal->setName( "normal" );
  2290. // These values are not accidental. It is slightly adjusted from facing straight into the
  2291. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2292. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2293. // [11/23/2009 Pat]
  2294. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2295. }
  2296. Var *T = (Var*) LangElement::find( "T" );
  2297. if(T == NULL)
  2298. {
  2299. T = new Var;
  2300. T->setType( "float3" );
  2301. T->setName( "T" );
  2302. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2303. }
  2304. }
  2305. //****************************************************************************
  2306. // ImposterVertFeatureHLSL
  2307. //****************************************************************************
  2308. ImposterVertFeatureHLSL::ImposterVertFeatureHLSL()
  2309. : mDep(ShaderGen::smCommonShaderPath + String("/imposter.hlsl" ))
  2310. {
  2311. addDependency( &mDep );
  2312. }
  2313. void ImposterVertFeatureHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2314. const MaterialFeatureData &fd )
  2315. {
  2316. MultiLine *meta = new MultiLine;
  2317. output = meta;
  2318. // Get the input vertex variables.
  2319. Var *inPosition = (Var*)LangElement::find( "position" );
  2320. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2321. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2322. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2323. // Get the input shader constants.
  2324. Var *imposterLimits = new Var;
  2325. imposterLimits->setType( "float4" );
  2326. imposterLimits->setName( "imposterLimits" );
  2327. imposterLimits->uniform = true;
  2328. imposterLimits->constSortPos = cspPotentialPrimitive;
  2329. Var *imposterUVs = new Var;
  2330. imposterUVs->setType( "float4" );
  2331. imposterUVs->setName( "imposterUVs" );
  2332. imposterUVs->arraySize = 64; // See imposter.hlsl
  2333. imposterUVs->uniform = true;
  2334. imposterUVs->constSortPos = cspPotentialPrimitive;
  2335. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2336. if ( !eyePos )
  2337. {
  2338. eyePos = new Var( "eyePosWorld", "float3" );
  2339. eyePos->uniform = true;
  2340. eyePos->constSortPos = cspPass;
  2341. }
  2342. // Declare the outputs from this feature.
  2343. Var *outInPosition = new Var;
  2344. outInPosition->setType( "float3" );
  2345. outInPosition->setName( "inPosition" );
  2346. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2347. Var *outTexCoord = new Var;
  2348. outTexCoord->setType( "float2" );
  2349. outTexCoord->setName( "texCoord" );
  2350. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2351. Var *outWorldToTangent = new Var;
  2352. outWorldToTangent->setType( "float3x3" );
  2353. outWorldToTangent->setName( "worldToTangent" );
  2354. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2355. // Add imposterFade to the OUT structure.
  2356. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2357. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2358. outFade->setName( "imposterFade" );
  2359. outFade->setStructName( "OUT" );
  2360. outFade->setType( "float" );
  2361. // Assign OUT.imposterFade
  2362. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2363. // All actual work is done in this method.
  2364. meta->addStatement( new GenOp( " imposter_v( @.xyz, @.w, @.x * length(@), normalize(@), normalize(@), @.y, @.x, @.z, @.w, @, @, @, @, @ );\r\n",
  2365. inPosition,
  2366. inPosition,
  2367. inMiscParams,
  2368. inRightVec,
  2369. inUpVec,
  2370. inRightVec,
  2371. imposterLimits,
  2372. imposterLimits,
  2373. imposterLimits,
  2374. imposterLimits,
  2375. eyePos,
  2376. imposterUVs,
  2377. outInPosition,
  2378. outTexCoord,
  2379. outWorldToTangent ) );
  2380. // Copy the position to wsPosition for use in shaders
  2381. // down stream instead of looking for objTrans.
  2382. Var *wsPosition = new Var;
  2383. wsPosition->setType( "float3" );
  2384. wsPosition->setName( "wsPosition" );
  2385. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2386. // If we new viewToTangent... its the same as the
  2387. // world to tangent for an imposter.
  2388. Var *viewToTangent = new Var;
  2389. viewToTangent->setType( "float3x3" );
  2390. viewToTangent->setName( "viewToTangent" );
  2391. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2392. }
  2393. void ImposterVertFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2394. const MaterialFeatureData &fd )
  2395. {
  2396. // Find / create IN.imposterFade
  2397. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2398. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2399. fade->setName( "imposterFade" );
  2400. fade->setStructName( "IN" );
  2401. fade->setType( "float" );
  2402. // Find / create visibility
  2403. Var *visibility = (Var*) LangElement::find( "visibility" );
  2404. if ( !visibility )
  2405. {
  2406. visibility = new Var();
  2407. visibility->setType( "float" );
  2408. visibility->setName( "visibility" );
  2409. visibility->uniform = true;
  2410. visibility->constSortPos = cspPotentialPrimitive;
  2411. }
  2412. MultiLine *meta = new MultiLine;
  2413. // Multiply foliageFade into visibility.
  2414. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2415. output = meta;
  2416. }
  2417. void ImposterVertFeatureHLSL::determineFeature( Material *material,
  2418. const GFXVertexFormat *vertexFormat,
  2419. U32 stageNum,
  2420. const FeatureType &type,
  2421. const FeatureSet &features,
  2422. MaterialFeatureData *outFeatureData )
  2423. {
  2424. if ( features.hasFeature( MFT_ImposterVert ) )
  2425. outFeatureData->features.addFeature( MFT_ImposterVert );
  2426. }
  2427. //****************************************************************************
  2428. // HardwareSkinningFeatureHLSL
  2429. //****************************************************************************
  2430. void HardwareSkinningFeatureHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2431. const MaterialFeatureData &fd )
  2432. {
  2433. MultiLine *meta = new MultiLine;
  2434. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2435. Var *inNormal = (Var*)LangElement::find( "inNormal" );
  2436. if ( !inPosition )
  2437. inPosition = (Var*)LangElement::find( "position" );
  2438. if ( !inNormal )
  2439. inNormal = (Var*)LangElement::find( "normal" );
  2440. Var* posePos = new Var("posePos", "float3");
  2441. Var* poseNormal = new Var("poseNormal", "float3");
  2442. Var* poseMat = new Var("poseMat", "float4x3");
  2443. Var* poseRotMat = new Var("poseRotMat", "float3x3");
  2444. Var* nodeTransforms = (Var*)LangElement::find("nodeTransforms");
  2445. if (!nodeTransforms)
  2446. {
  2447. nodeTransforms = new Var("nodeTransforms", "float4x3");
  2448. nodeTransforms->uniform = true;
  2449. nodeTransforms->arraySize = TSShape::smMaxSkinBones;
  2450. nodeTransforms->constSortPos = cspPotentialPrimitive;
  2451. }
  2452. U32 numIndices = mVertexFormat->getNumBlendIndices();
  2453. meta->addStatement( new GenOp( " @ = 0.0;\r\n", new DecOp( posePos ) ) );
  2454. meta->addStatement( new GenOp( " @ = 0.0;\r\n", new DecOp( poseNormal ) ) );
  2455. meta->addStatement( new GenOp( " @;\r\n", new DecOp( poseMat ) ) );
  2456. meta->addStatement(new GenOp(" @;\r\n int i;\r\n", new DecOp(poseRotMat)));
  2457. for (U32 i=0; i<numIndices; i++)
  2458. {
  2459. // NOTE: To keep things simple, we assume all 4 bone indices are used in each element chunk.
  2460. LangElement* inIndices = (Var*)LangElement::find(String::ToString( "blendIndices%d", i ));
  2461. LangElement* inWeights = (Var*)LangElement::find(String::ToString( "blendWeight%d", i ));
  2462. AssertFatal(inIndices && inWeights, "Something went wrong here");
  2463. AssertFatal(poseMat && nodeTransforms && posePos && inPosition && inWeights && poseNormal && inNormal && poseRotMat, "Something went REALLY wrong here");
  2464. meta->addStatement( new GenOp( " for (i=0; i<4; i++) {\r\n" ) );
  2465. meta->addStatement( new GenOp( " int poseIdx = int(@[i]);\r\n", inIndices ) );
  2466. meta->addStatement( new GenOp( " float poseWeight = @[i];\r\n", inWeights) );
  2467. meta->addStatement( new GenOp( " @ = @[poseIdx];\r\n", poseMat, nodeTransforms) );
  2468. meta->addStatement( new GenOp( " @ = (float3x3)@;\r\n", poseRotMat, poseMat) );
  2469. meta->addStatement( new GenOp( " @ += (mul(float4(@, 1), @)).xyz * poseWeight;\r\n", posePos, inPosition, poseMat) );
  2470. meta->addStatement( new GenOp( " @ += (mul(@,@) * poseWeight);\r\n", poseNormal, inNormal, poseRotMat) );
  2471. meta->addStatement( new GenOp( " }\r\n" ) );
  2472. }
  2473. // Assign new position and normal
  2474. meta->addStatement( new GenOp( " @ = @;\r\n", inPosition, posePos ) );
  2475. meta->addStatement( new GenOp( " @ = normalize(@);\r\n", inNormal, poseNormal ) );
  2476. output = meta;
  2477. }
  2478. //****************************************************************************
  2479. // ReflectionProbeFeatHLSL
  2480. //****************************************************************************
  2481. ReflectionProbeFeatHLSL::ReflectionProbeFeatHLSL()
  2482. : mDep(ShaderGen::smCommonShaderPath + String("/lighting.hlsl"))
  2483. {
  2484. addDependency(&mDep);
  2485. }
  2486. void ReflectionProbeFeatHLSL::processVert(Vector<ShaderComponent*>& componentList,
  2487. const MaterialFeatureData& fd)
  2488. {
  2489. MultiLine* meta = new MultiLine;
  2490. output = meta;
  2491. // Also output the worldToTanget transform which
  2492. // we use to create the world space normal.
  2493. getOutWorldToTangent(componentList, meta, fd);
  2494. }
  2495. void ReflectionProbeFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  2496. const MaterialFeatureData &fd)
  2497. {
  2498. // Skip out on realtime lighting if we don't have a normal
  2499. // or we're doing some sort of baked lighting.
  2500. //
  2501. // TODO: We can totally detect for this in the material
  2502. // feature setup... we should move it out of here!
  2503. //
  2504. if (fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit])
  2505. return;
  2506. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  2507. MultiLine *meta = new MultiLine;
  2508. // Now the wsPosition and wsView.
  2509. Var *wsPosition = getInWsPosition(componentList);
  2510. Var *wsView = getWsView(wsPosition, meta);
  2511. //Reflection Probe WIP
  2512. U32 MAX_FORWARD_PROBES = 4;
  2513. Var *numProbes = new Var("numProbes", "int");
  2514. numProbes->uniform = true;
  2515. numProbes->constSortPos = cspPotentialPrimitive;
  2516. Var *cubeMips = new Var("cubeMips", "float");
  2517. cubeMips->uniform = true;
  2518. cubeMips->constSortPos = cspPotentialPrimitive;
  2519. Var * skylightCubemapIdx = new Var("skylightCubemapIdx", "float");
  2520. skylightCubemapIdx->uniform = true;
  2521. skylightCubemapIdx->constSortPos = cspPotentialPrimitive;
  2522. Var *inProbePosArray = new Var("inProbePosArray", "float4");
  2523. inProbePosArray->arraySize = MAX_FORWARD_PROBES;
  2524. inProbePosArray->uniform = true;
  2525. inProbePosArray->constSortPos = cspPotentialPrimitive;
  2526. Var *inRefPosArray = new Var("inRefPosArray", "float4");
  2527. inRefPosArray->arraySize = MAX_FORWARD_PROBES;
  2528. inRefPosArray->uniform = true;
  2529. inRefPosArray->constSortPos = cspPotentialPrimitive;
  2530. Var * refBoxMinArray = new Var("inRefBoxMin", "float4");
  2531. refBoxMinArray->arraySize = MAX_FORWARD_PROBES;
  2532. refBoxMinArray->uniform = true;
  2533. refBoxMinArray->constSortPos = cspPotentialPrimitive;
  2534. Var * refBoxMaxArray = new Var("inRefBoxMax", "float4");
  2535. refBoxMaxArray->arraySize = MAX_FORWARD_PROBES;
  2536. refBoxMaxArray->uniform = true;
  2537. refBoxMaxArray->constSortPos = cspPotentialPrimitive;
  2538. Var *probeConfigData = new Var("probeConfigData", "float4");
  2539. probeConfigData->arraySize = MAX_FORWARD_PROBES;
  2540. probeConfigData->uniform = true;
  2541. probeConfigData->constSortPos = cspPotentialPrimitive;
  2542. Var *worldToObjArray = new Var("worldToObjArray", "float4x4");
  2543. worldToObjArray->arraySize = MAX_FORWARD_PROBES;
  2544. worldToObjArray->uniform = true;
  2545. worldToObjArray->constSortPos = cspPotentialPrimitive;
  2546. Var *BRDFTexture = new Var("BRDFTexture", "SamplerState");
  2547. BRDFTexture->uniform = true;
  2548. BRDFTexture->sampler = true;
  2549. BRDFTexture->constNum = Var::getTexUnitNum(); // used as texture unit num here
  2550. Var *BRDFTextureTex = new Var("texture_BRDFTexture", "Texture2D");
  2551. BRDFTextureTex->uniform = true;
  2552. BRDFTextureTex->texture = true;
  2553. BRDFTextureTex->constNum = BRDFTexture->constNum;
  2554. Var *specularCubemapAR = new Var("specularCubemapAR", "SamplerState");
  2555. specularCubemapAR->uniform = true;
  2556. specularCubemapAR->sampler = true;
  2557. specularCubemapAR->constNum = Var::getTexUnitNum(); // used as texture unit num here
  2558. Var *specularCubemapARTex = new Var("texture_specularCubemapAR", "TextureCubeArray");
  2559. specularCubemapARTex->uniform = true;
  2560. specularCubemapARTex->texture = true;
  2561. specularCubemapARTex->constNum = specularCubemapAR->constNum;
  2562. Var *irradianceCubemapAR = new Var("irradianceCubemapAR", "SamplerState");
  2563. irradianceCubemapAR->uniform = true;
  2564. irradianceCubemapAR->sampler = true;
  2565. irradianceCubemapAR->constNum = Var::getTexUnitNum(); // used as texture unit num here
  2566. Var *irradianceCubemapARTex = new Var("texture_irradianceCubemapAR", "TextureCubeArray");
  2567. irradianceCubemapARTex->uniform = true;
  2568. irradianceCubemapARTex->texture = true;
  2569. irradianceCubemapARTex->constNum = irradianceCubemapAR->constNum;
  2570. Var* surface = getSurface(componentList, meta, fd);
  2571. if (!surface)
  2572. {
  2573. Con::errorf("ShaderGen::ReflectionProbeFeatHLSL() - failed to generate surface!");
  2574. return;
  2575. }
  2576. Var *curColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2577. Var *matinfo = (Var*)LangElement::find("PBRConfig");
  2578. Var* metalness = (Var*)LangElement::find("metalness");
  2579. Var* smoothness = (Var*)LangElement::find("smoothness");
  2580. Var* wsEyePos = (Var*)LangElement::find("eyePosWorld");
  2581. //Reflection vec
  2582. String computeForwardProbes = String::String(" @.rgb = computeForwardProbes(@,@,@,@,@,@,@,@,@,\r\n\t\t");
  2583. computeForwardProbes += String::String("@,TORQUE_SAMPLER2D_MAKEARG(@),\r\n\t\t");
  2584. computeForwardProbes += String::String("TORQUE_SAMPLERCUBEARRAY_MAKEARG(@),TORQUE_SAMPLERCUBEARRAY_MAKEARG(@)).rgb; \r\n");
  2585. meta->addStatement(new GenOp(computeForwardProbes.c_str(), curColor, surface, cubeMips, numProbes, worldToObjArray, probeConfigData, inProbePosArray, refBoxMinArray, refBoxMaxArray, inRefPosArray,
  2586. skylightCubemapIdx, BRDFTexture,
  2587. irradianceCubemapAR, specularCubemapAR));
  2588. //meta->addStatement(new GenOp(" @.rgb = @.roughness.xxx;\r\n", albedo, surface));
  2589. output = meta;
  2590. }
  2591. ShaderFeature::Resources ReflectionProbeFeatHLSL::getResources(const MaterialFeatureData &fd)
  2592. {
  2593. Resources res;
  2594. res.numTex = 3;
  2595. res.numTexReg = 3;
  2596. return res;
  2597. }
  2598. void ReflectionProbeFeatHLSL::setTexData(Material::StageData &stageDat,
  2599. const MaterialFeatureData &stageFeatures,
  2600. RenderPassData &passData,
  2601. U32 &texIndex)
  2602. {
  2603. if (stageFeatures.features[MFT_ReflectionProbes])
  2604. {
  2605. passData.mSamplerNames[texIndex] = "BRDFTexture";
  2606. passData.mTexType[texIndex++] = Material::Standard;
  2607. // assuming here that it is a scenegraph cubemap
  2608. passData.mSamplerNames[texIndex] = "specularCubemapAR";
  2609. passData.mTexType[texIndex++] = Material::SGCube;
  2610. passData.mSamplerNames[texIndex] = "irradianceCubemapAR";
  2611. passData.mTexType[texIndex++] = Material::SGCube;
  2612. }
  2613. }