shaderFeatureHLSL.cpp 106 KB

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