shaderFeatureHLSL.cpp 103 KB

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