shaderFeatureHLSL.cpp 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009
  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. if (fd.features[MFT_isDeferred])
  1064. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul, NULL, ShaderFeature::RenderTarget1)));
  1065. else
  1066. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul)));
  1067. output = meta;
  1068. }
  1069. //****************************************************************************
  1070. // Lightmap
  1071. //****************************************************************************
  1072. void LightmapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1073. const MaterialFeatureData &fd )
  1074. {
  1075. // grab tex register from incoming vert
  1076. Var *inTex = getVertTexCoord( "texCoord2" );
  1077. // grab connector texcoord register
  1078. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1079. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1080. outTex->setName( "texCoord2" );
  1081. outTex->setStructName( "OUT" );
  1082. outTex->setType( "float2" );
  1083. outTex->mapsToSampler = true;
  1084. // setup language elements to output incoming tex coords to output
  1085. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  1086. }
  1087. void LightmapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1088. const MaterialFeatureData &fd )
  1089. {
  1090. // grab connector texcoord register
  1091. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1092. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  1093. inTex->setName( "texCoord2" );
  1094. inTex->setStructName( "IN" );
  1095. inTex->setType( "float2" );
  1096. inTex->mapsToSampler = true;
  1097. // create texture var
  1098. Var *lightMap = new Var;
  1099. lightMap->setType( "sampler2D" );
  1100. lightMap->setName( "lightMap" );
  1101. lightMap->uniform = true;
  1102. lightMap->sampler = true;
  1103. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1104. Var *lightMapTex = NULL;
  1105. if (mIsDirect3D11)
  1106. {
  1107. lightMap->setType("SamplerState");
  1108. lightMapTex->setName("lightMapTex");
  1109. lightMapTex->setType("Texture2D");
  1110. lightMapTex->uniform = true;
  1111. lightMapTex->texture = true;
  1112. lightMapTex->constNum = lightMap->constNum;
  1113. }
  1114. // argh, pixel specular should prob use this too
  1115. if( fd.features[MFT_NormalMap] )
  1116. {
  1117. Var *lmColor = new Var;
  1118. lmColor->setName( "lmColor" );
  1119. lmColor->setType( "float4" );
  1120. LangElement *lmColorDecl = new DecOp( lmColor );
  1121. if (mIsDirect3D11)
  1122. output = new GenOp(" @ = @.Sample(@, @);\r\n", lmColorDecl, lightMapTex, lightMap, inTex);
  1123. else
  1124. output = new GenOp(" @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex);
  1125. return;
  1126. }
  1127. // Add realtime lighting, if it is available
  1128. LangElement *statement = NULL;
  1129. if( fd.features[MFT_RTLighting] )
  1130. {
  1131. // Advanced lighting is the only dynamic lighting supported right now
  1132. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1133. if(inColor != NULL)
  1134. {
  1135. // Find out if RTLighting should be added or substituted
  1136. bool bPreProcessedLighting = false;
  1137. AdvancedLightBinManager *lightBin;
  1138. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1139. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1140. // Lightmap has already been included in the advanced light bin, so
  1141. // no need to do any sampling or anything
  1142. if (bPreProcessedLighting)
  1143. statement = new GenOp("float4(@, 1.0)", inColor);
  1144. else
  1145. {
  1146. if (mIsDirect3D11)
  1147. statement = new GenOp("@.Sample(@, @) + float4(@.rgb, 0.0)", lightMapTex, lightMap, inTex, inColor);
  1148. else
  1149. statement = new GenOp("tex2D(@, @) + float4(@.rgb, 0.0)", lightMap, inTex, inColor);
  1150. }
  1151. }
  1152. }
  1153. // If we still don't have it... then just sample the lightmap.
  1154. if (!statement)
  1155. {
  1156. if (mIsDirect3D11)
  1157. statement = new GenOp("@.Sample(@, @)", lightMapTex, lightMap, inTex);
  1158. else
  1159. statement = new GenOp("tex2D(@, @)", lightMap, inTex);
  1160. }
  1161. // Assign to proper render target
  1162. MultiLine *meta = new MultiLine;
  1163. if( fd.features[MFT_LightbufferMRT] )
  1164. {
  1165. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1166. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1167. }
  1168. else
  1169. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
  1170. output = meta;
  1171. }
  1172. ShaderFeature::Resources LightmapFeatHLSL::getResources( const MaterialFeatureData &fd )
  1173. {
  1174. Resources res;
  1175. res.numTex = 1;
  1176. res.numTexReg = 1;
  1177. return res;
  1178. }
  1179. void LightmapFeatHLSL::setTexData( Material::StageData &stageDat,
  1180. const MaterialFeatureData &fd,
  1181. RenderPassData &passData,
  1182. U32 &texIndex )
  1183. {
  1184. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  1185. passData.mSamplerNames[ texIndex ] = "lightMap";
  1186. if ( tex )
  1187. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1188. else
  1189. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  1190. }
  1191. U32 LightmapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1192. {
  1193. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1194. }
  1195. //****************************************************************************
  1196. // Tonemap
  1197. //****************************************************************************
  1198. void TonemapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1199. const MaterialFeatureData &fd )
  1200. {
  1201. // Grab the connector
  1202. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1203. // Set up the second set of texCoords
  1204. Var *inTex2 = getVertTexCoord( "texCoord2" );
  1205. if ( inTex2 )
  1206. {
  1207. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  1208. outTex2->setName( "texCoord2" );
  1209. outTex2->setStructName( "OUT" );
  1210. outTex2->setType( "float2" );
  1211. outTex2->mapsToSampler = true;
  1212. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  1213. }
  1214. }
  1215. void TonemapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1216. const MaterialFeatureData &fd )
  1217. {
  1218. // Grab connector
  1219. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1220. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  1221. inTex2->setName( "texCoord2" );
  1222. inTex2->setStructName( "IN" );
  1223. inTex2->setType( "float2" );
  1224. inTex2->mapsToSampler = true;
  1225. // create texture var
  1226. Var *toneMap = new Var;
  1227. toneMap->setType( "sampler2D" );
  1228. toneMap->setName( "toneMap" );
  1229. toneMap->uniform = true;
  1230. toneMap->sampler = true;
  1231. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1232. Var *toneMapTex = NULL;
  1233. if (mIsDirect3D11)
  1234. {
  1235. toneMap->setType("SamplerState");
  1236. toneMapTex = new Var;
  1237. toneMapTex->setName("toneMapTex");
  1238. toneMapTex->setType("Texture2D");
  1239. toneMapTex->uniform = true;
  1240. toneMapTex->texture = true;
  1241. toneMapTex->constNum = toneMap->constNum;
  1242. }
  1243. MultiLine * meta = new MultiLine;
  1244. // First get the toneMap color
  1245. Var *toneMapColor = new Var;
  1246. toneMapColor->setType( "float4" );
  1247. toneMapColor->setName( "toneMapColor" );
  1248. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  1249. if (mIsDirect3D11)
  1250. meta->addStatement(new GenOp(" @ = @.Sample(@, @);\r\n", toneMapColorDecl, toneMapTex, toneMap, inTex2));
  1251. else
  1252. meta->addStatement(new GenOp(" @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2));
  1253. // We do a different calculation if there is a diffuse map or not
  1254. Material::BlendOp blendOp = Material::Mul;
  1255. if ( fd.features[MFT_DiffuseMap] )
  1256. {
  1257. // Reverse the tonemap
  1258. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
  1259. // Re-tonemap with the current color factored in
  1260. blendOp = Material::ToneMap;
  1261. }
  1262. // Find out if RTLighting should be added
  1263. bool bPreProcessedLighting = false;
  1264. AdvancedLightBinManager *lightBin;
  1265. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1266. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1267. // Add in the realtime lighting contribution
  1268. if ( fd.features[MFT_RTLighting] )
  1269. {
  1270. // Right now, only Advanced Lighting is supported
  1271. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1272. if(inColor != NULL)
  1273. {
  1274. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1275. // the dynamic light buffer, and it already has the tonemap included
  1276. if(bPreProcessedLighting)
  1277. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  1278. else
  1279. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  1280. }
  1281. }
  1282. // Assign to proper render target
  1283. if( fd.features[MFT_LightbufferMRT] )
  1284. {
  1285. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1286. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1287. }
  1288. else
  1289. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  1290. output = meta;
  1291. }
  1292. ShaderFeature::Resources TonemapFeatHLSL::getResources( const MaterialFeatureData &fd )
  1293. {
  1294. Resources res;
  1295. res.numTex = 1;
  1296. res.numTexReg = 1;
  1297. return res;
  1298. }
  1299. void TonemapFeatHLSL::setTexData( Material::StageData &stageDat,
  1300. const MaterialFeatureData &fd,
  1301. RenderPassData &passData,
  1302. U32 &texIndex )
  1303. {
  1304. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  1305. if ( tex )
  1306. {
  1307. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  1308. passData.mSamplerNames[ texIndex ] = "toneMap";
  1309. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1310. }
  1311. }
  1312. U32 TonemapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1313. {
  1314. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1315. }
  1316. //****************************************************************************
  1317. // pureLIGHT Lighting
  1318. //****************************************************************************
  1319. void VertLitHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1320. const MaterialFeatureData &fd )
  1321. {
  1322. // If we have a lightMap or toneMap then our lighting will be
  1323. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1324. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1325. {
  1326. output = NULL;
  1327. return;
  1328. }
  1329. // Create vertex color connector if it doesn't exist.
  1330. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1331. if( !outColor )
  1332. {
  1333. // Search for vert color
  1334. Var *inColor = (Var*) LangElement::find( "diffuse" );
  1335. // If there isn't a vertex color then we can't do anything
  1336. if( !inColor )
  1337. {
  1338. output = NULL;
  1339. return;
  1340. }
  1341. // Grab the connector color
  1342. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1343. Var *outColor = connectComp->getElement( RT_COLOR );
  1344. outColor->setName( "vertColor" );
  1345. outColor->setStructName( "OUT" );
  1346. outColor->setType( "float4" );
  1347. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1348. }
  1349. else
  1350. output = NULL; // Nothing we need to do.
  1351. }
  1352. void VertLitHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1353. const MaterialFeatureData &fd )
  1354. {
  1355. // If we have a lightMap or toneMap then our lighting will be
  1356. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1357. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1358. {
  1359. output = NULL;
  1360. return;
  1361. }
  1362. // Grab the connector color register
  1363. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1364. if( !vertColor )
  1365. {
  1366. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1367. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1368. vertColor = connectComp->getElement( RT_COLOR );
  1369. vertColor->setName( "vertColor" );
  1370. vertColor->setStructName( "IN" );
  1371. vertColor->setType( "float4" );
  1372. }
  1373. MultiLine * meta = new MultiLine;
  1374. // Defaults (no diffuse map)
  1375. Material::BlendOp blendOp = Material::Mul;
  1376. LangElement *outColor = vertColor;
  1377. // We do a different calculation if there is a diffuse map or not
  1378. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1379. {
  1380. Var * finalVertColor = new Var;
  1381. finalVertColor->setName( "finalVertColor" );
  1382. finalVertColor->setType( "float4" );
  1383. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1384. // Reverse the tonemap
  1385. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
  1386. // Set the blend op to tonemap
  1387. blendOp = Material::ToneMap;
  1388. outColor = finalVertColor;
  1389. }
  1390. // Add in the realtime lighting contribution, if applicable
  1391. if ( fd.features[MFT_RTLighting] )
  1392. {
  1393. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1394. if(rtLightingColor != NULL)
  1395. {
  1396. bool bPreProcessedLighting = false;
  1397. AdvancedLightBinManager *lightBin;
  1398. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1399. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1400. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1401. // the dynamic light buffer, and it already has the baked-vertex-color
  1402. // included in it
  1403. if(bPreProcessedLighting)
  1404. outColor = new GenOp( "float4(@.rgb, 1.0)", rtLightingColor );
  1405. else
  1406. outColor = new GenOp( "float4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
  1407. }
  1408. }
  1409. // Output the color
  1410. if ( fd.features[MFT_LightbufferMRT] )
  1411. {
  1412. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1413. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1414. }
  1415. else
  1416. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1417. output = meta;
  1418. }
  1419. U32 VertLitHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1420. {
  1421. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1422. }
  1423. //****************************************************************************
  1424. // Detail map
  1425. //****************************************************************************
  1426. void DetailFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1427. const MaterialFeatureData &fd )
  1428. {
  1429. MultiLine *meta = new MultiLine;
  1430. addOutDetailTexCoord( componentList,
  1431. meta,
  1432. fd.features[MFT_TexAnim] );
  1433. output = meta;
  1434. }
  1435. void DetailFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1436. const MaterialFeatureData &fd )
  1437. {
  1438. // Get the detail texture coord.
  1439. Var *inTex = getInTexCoord( "detCoord", "float2", true, componentList );
  1440. // create texture var
  1441. Var *detailMap = new Var;
  1442. detailMap->setType( "sampler2D" );
  1443. detailMap->setName( "detailMap" );
  1444. detailMap->uniform = true;
  1445. detailMap->sampler = true;
  1446. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1447. Var* detailMapTex = NULL;
  1448. if (mIsDirect3D11)
  1449. {
  1450. detailMap->setType("SamplerState");
  1451. detailMapTex = new Var;
  1452. detailMapTex->setName("detailMapTex");
  1453. detailMapTex->setType("Texture2D");
  1454. detailMapTex->uniform = true;
  1455. detailMapTex->texture = true;
  1456. detailMapTex->constNum = detailMap->constNum;
  1457. }
  1458. // We're doing the standard greyscale detail map
  1459. // technique which can darken and lighten the
  1460. // diffuse texture.
  1461. // TODO: We could add a feature to toggle between this
  1462. // and a simple multiplication with the detail map.
  1463. LangElement *statement = NULL;
  1464. if (mIsDirect3D11)
  1465. statement = new GenOp("( @.Sample(@, @) * 2.0 ) - 1.0", detailMapTex, detailMap, inTex);
  1466. else
  1467. statement = new GenOp("( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex);
  1468. if ( fd.features[MFT_isDeferred])
  1469. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add, NULL, ShaderFeature::RenderTarget1 ) );
  1470. else
  1471. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1472. }
  1473. ShaderFeature::Resources DetailFeatHLSL::getResources( const MaterialFeatureData &fd )
  1474. {
  1475. Resources res;
  1476. res.numTex = 1;
  1477. res.numTexReg = 1;
  1478. return res;
  1479. }
  1480. void DetailFeatHLSL::setTexData( Material::StageData &stageDat,
  1481. const MaterialFeatureData &fd,
  1482. RenderPassData &passData,
  1483. U32 &texIndex )
  1484. {
  1485. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1486. if ( tex )
  1487. {
  1488. passData.mSamplerNames[texIndex] = "detailMap";
  1489. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1490. }
  1491. }
  1492. //****************************************************************************
  1493. // Vertex position
  1494. //****************************************************************************
  1495. void VertPositionHLSL::determineFeature( Material *material,
  1496. const GFXVertexFormat *vertexFormat,
  1497. U32 stageNum,
  1498. const FeatureType &type,
  1499. const FeatureSet &features,
  1500. MaterialFeatureData *outFeatureData )
  1501. {
  1502. // This feature is always on!
  1503. outFeatureData->features.addFeature( type );
  1504. }
  1505. void VertPositionHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1506. const MaterialFeatureData &fd )
  1507. {
  1508. // First check for an input position from a previous feature
  1509. // then look for the default vertex position.
  1510. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1511. if ( !inPosition )
  1512. inPosition = (Var*)LangElement::find( "position" );
  1513. // grab connector position
  1514. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1515. Var *outPosition = NULL;
  1516. if (mIsDirect3D11)
  1517. outPosition = connectComp->getElement(RT_SVPOSITION);
  1518. else
  1519. outPosition = connectComp->getElement(RT_POSITION);
  1520. outPosition->setName( "hpos" );
  1521. outPosition->setStructName( "OUT" );
  1522. MultiLine *meta = new MultiLine;
  1523. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1524. meta->addStatement( new GenOp( " @ = mul(@, float4(@.xyz,1));\r\n",
  1525. outPosition, modelview, inPosition ) );
  1526. output = meta;
  1527. }
  1528. void VertPositionHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1529. const MaterialFeatureData &fd)
  1530. {
  1531. if (mIsDirect3D11)
  1532. {
  1533. // grab connector position
  1534. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  1535. Var *outPosition = connectComp->getElement(RT_SVPOSITION);
  1536. outPosition->setName("vpos");
  1537. outPosition->setStructName("IN");
  1538. }
  1539. }
  1540. //****************************************************************************
  1541. // Reflect Cubemap
  1542. //****************************************************************************
  1543. void ReflectCubeFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1544. const MaterialFeatureData &fd )
  1545. {
  1546. // search for vert normal
  1547. Var *inNormal = (Var*) LangElement::find( "normal" );
  1548. if ( !inNormal )
  1549. return;
  1550. MultiLine * meta = new MultiLine;
  1551. // If a base or bump tex is present in the material, but not in the
  1552. // current pass - we need to add one to the current pass to use
  1553. // its alpha channel as a gloss map. Here we just need the tex coords.
  1554. if( !fd.features[MFT_DiffuseMap] &&
  1555. !fd.features[MFT_NormalMap] )
  1556. {
  1557. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1558. fd.materialFeatures[MFT_NormalMap] )
  1559. {
  1560. // find incoming texture var
  1561. Var *inTex = getVertTexCoord( "texCoord" );
  1562. // grab connector texcoord register
  1563. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1564. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1565. outTex->setName( "texCoord" );
  1566. outTex->setStructName( "OUT" );
  1567. outTex->setType( "float2" );
  1568. outTex->mapsToSampler = true;
  1569. // setup language elements to output incoming tex coords to output
  1570. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1571. }
  1572. }
  1573. // create cubeTrans
  1574. bool useInstancing = fd.features[MFT_UseInstancing];
  1575. Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
  1576. // cube vert position
  1577. Var * cubeVertPos = new Var;
  1578. cubeVertPos->setName( "cubeVertPos" );
  1579. cubeVertPos->setType( "float3" );
  1580. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1581. meta->addStatement( new GenOp( " @ = mul(@, float4(@,1)).xyz;\r\n",
  1582. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1583. // cube normal
  1584. Var * cubeNormal = new Var;
  1585. cubeNormal->setName( "cubeNormal" );
  1586. cubeNormal->setType( "float3" );
  1587. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1588. meta->addStatement(new GenOp(" @ = ( mul( (@), float4(@, 0) ) ).xyz;\r\n",
  1589. cubeNormDecl, cubeTrans, inNormal));
  1590. meta->addStatement(new GenOp(" @ = bool(length(@)) ? normalize(@) : @;\r\n",
  1591. cubeNormal, cubeNormal, cubeNormal, cubeNormal));
  1592. // grab the eye position
  1593. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1594. if ( !eyePos )
  1595. {
  1596. eyePos = new Var( "eyePosWorld", "float3" );
  1597. eyePos->uniform = true;
  1598. eyePos->constSortPos = cspPass;
  1599. }
  1600. // eye to vert
  1601. Var * eyeToVert = new Var;
  1602. eyeToVert->setName( "eyeToVert" );
  1603. eyeToVert->setType( "float3" );
  1604. LangElement *e2vDecl = new DecOp( eyeToVert );
  1605. meta->addStatement( new GenOp( " @ = @ - @;\r\n",
  1606. e2vDecl, cubeVertPos, eyePos ) );
  1607. // grab connector texcoord register
  1608. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1609. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1610. reflectVec->setName( "reflectVec" );
  1611. reflectVec->setStructName( "OUT" );
  1612. reflectVec->setType( "float3" );
  1613. reflectVec->mapsToSampler = true;
  1614. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1615. output = meta;
  1616. }
  1617. void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1618. const MaterialFeatureData &fd )
  1619. {
  1620. MultiLine * meta = new MultiLine;
  1621. Var *glossColor = NULL;
  1622. // If a base or bump tex is present in the material, but not in the
  1623. // current pass - we need to add one to the current pass to use
  1624. // its alpha channel as a gloss map.
  1625. if( !fd.features[MFT_DiffuseMap] &&
  1626. !fd.features[MFT_NormalMap])
  1627. {
  1628. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1629. fd.materialFeatures[MFT_NormalMap])
  1630. {
  1631. // grab connector texcoord register
  1632. Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
  1633. // create texture var
  1634. Var *newMap = new Var;
  1635. newMap->setType( "sampler2D" );
  1636. newMap->setName( "glossMap" );
  1637. newMap->uniform = true;
  1638. newMap->sampler = true;
  1639. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1640. Var* glowMapTex = NULL;
  1641. if (mIsDirect3D11)
  1642. {
  1643. newMap->setType("SamplerState");
  1644. glowMapTex = new Var;
  1645. glowMapTex->setName("glowMapTex");
  1646. glowMapTex->setType("Texture2D");
  1647. glowMapTex->uniform = true;
  1648. glowMapTex->texture = true;
  1649. glowMapTex->constNum = newMap->constNum;
  1650. }
  1651. // create sample color
  1652. Var *color = new Var;
  1653. color->setType( "float4" );
  1654. color->setName( "diffuseColor" );
  1655. LangElement *colorDecl = new DecOp( color );
  1656. glossColor = color;
  1657. if (mIsDirect3D11)
  1658. meta->addStatement(new GenOp(" @ = @.Sample( @, @ );\r\n", colorDecl, glowMapTex, newMap, inTex));
  1659. else
  1660. meta->addStatement(new GenOp(" @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex));
  1661. }
  1662. }
  1663. else
  1664. {
  1665. if (fd.features[MFT_isDeferred])
  1666. glossColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1667. if (!glossColor)
  1668. glossColor = (Var*)LangElement::find("specularColor");
  1669. if (!glossColor)
  1670. glossColor = (Var*)LangElement::find("diffuseColor");
  1671. if (!glossColor)
  1672. glossColor = (Var*)LangElement::find("bumpNormal");
  1673. }
  1674. // grab connector texcoord register
  1675. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1676. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1677. reflectVec->setName( "reflectVec" );
  1678. reflectVec->setStructName( "IN" );
  1679. reflectVec->setType( "float3" );
  1680. reflectVec->mapsToSampler = true;
  1681. // create cubemap var
  1682. Var *cubeMap = new Var;
  1683. cubeMap->setType( "samplerCUBE" );
  1684. cubeMap->setName( "cubeMap" );
  1685. cubeMap->uniform = true;
  1686. cubeMap->sampler = true;
  1687. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1688. Var* cubeMapTex = NULL;
  1689. if (mIsDirect3D11)
  1690. {
  1691. cubeMap->setType("SamplerState");
  1692. cubeMapTex = new Var;
  1693. cubeMapTex->setName("cubeMapTex");
  1694. cubeMapTex->setType("TextureCube"); // cubeMapTex->setType("TextureCube");
  1695. cubeMapTex->uniform = true;
  1696. cubeMapTex->texture = true;
  1697. cubeMapTex->constNum = cubeMap->constNum;
  1698. }
  1699. // TODO: Restore the lighting attenuation here!
  1700. Var *attn = NULL;
  1701. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1702. //attn = (Var*)LangElement::find("attn");
  1703. //else
  1704. if ( fd.materialFeatures[MFT_RTLighting] )
  1705. attn =(Var*)LangElement::find("d_NL_Att");
  1706. LangElement *texCube = NULL;
  1707. Var* matinfo = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  1708. //first try and grab the gbuffer
  1709. if (fd.features[MFT_isDeferred] && matinfo)
  1710. {
  1711. // Cube LOD level = (1.0 - Roughness) * 8
  1712. // mip_levle = min((1.0 - u_glossiness)*11.0 + 1.0, 8.0)
  1713. //LangElement *texCube = new GenOp( "texCUBElod( @, float4(@, min((1.0 - (@ / 128.0)) * 11.0 + 1.0, 8.0)) )", cubeMap, reflectVec, specPower );
  1714. if (fd.features[MFT_DeferredSpecMap])
  1715. {
  1716. if (mIsDirect3D11)
  1717. texCube = new GenOp("@.SampleLevel( @, @, @.a*5)", cubeMapTex, cubeMap, reflectVec, matinfo);
  1718. else
  1719. texCube = new GenOp("texCUBElod( @, float4(@, (@.a*5)) )", cubeMap, reflectVec, matinfo);
  1720. }
  1721. else
  1722. {
  1723. if (mIsDirect3D11)
  1724. texCube = new GenOp("@.SampleLevel( @, @, ([email protected])*6 )", cubeMapTex, cubeMap, reflectVec, matinfo);
  1725. else
  1726. texCube = new GenOp("texCUBElod( @, float4(@, (([email protected])*6)) )", cubeMap, reflectVec, matinfo);
  1727. }
  1728. }
  1729. else
  1730. {
  1731. if (glossColor) //failing that, rtry and find color data
  1732. {
  1733. if (mIsDirect3D11)
  1734. texCube = new GenOp("@.SampleLevel( @, @, @.a*5)", cubeMapTex, cubeMap, reflectVec, glossColor);
  1735. else
  1736. texCube = new GenOp("texCUBElod( @, float4(@, @.a*5))", cubeMap, reflectVec, glossColor);
  1737. }
  1738. else //failing *that*, just draw the cubemap
  1739. {
  1740. if (mIsDirect3D11)
  1741. texCube = new GenOp("@.Sample( @, @ )", cubeMapTex, cubeMap, reflectVec);
  1742. else
  1743. texCube = new GenOp("texCUBE( @, @ )", cubeMap, reflectVec);
  1744. }
  1745. }
  1746. LangElement *lerpVal = NULL;
  1747. Material::BlendOp blendOp = Material::LerpAlpha;
  1748. // Note that the lerpVal needs to be a float4 so that
  1749. // it will work with the LerpAlpha blend.
  1750. if (matinfo)
  1751. {
  1752. if (attn)
  1753. lerpVal = new GenOp("@ * saturate( @ )", matinfo, attn);
  1754. else
  1755. lerpVal = new GenOp("@", matinfo);
  1756. }
  1757. else if ( glossColor )
  1758. {
  1759. if ( attn )
  1760. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1761. else
  1762. lerpVal = glossColor;
  1763. }
  1764. else
  1765. {
  1766. if ( attn )
  1767. lerpVal = new GenOp( "saturate( @ ).xxxx", attn );
  1768. else
  1769. blendOp = Material::Mul;
  1770. }
  1771. if (fd.features[MFT_isDeferred])
  1772. {
  1773. Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1774. if (fd.features[MFT_DeferredSpecMap])
  1775. meta->addStatement(new GenOp(" @.rgb = lerp( @.rgb, (@).rgb, (@.b));\r\n", targ, targ, texCube, lerpVal));
  1776. else
  1777. meta->addStatement(new GenOp(" @.rgb = lerp( @.rgb, (@).rgb, (@.b*128/5));\r\n", targ, targ, texCube, lerpVal));
  1778. }
  1779. else
  1780. meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
  1781. output = meta;
  1782. }
  1783. ShaderFeature::Resources ReflectCubeFeatHLSL::getResources( const MaterialFeatureData &fd )
  1784. {
  1785. Resources res;
  1786. if( fd.features[MFT_DiffuseMap] ||
  1787. fd.features[MFT_NormalMap] )
  1788. {
  1789. res.numTex = 1;
  1790. res.numTexReg = 1;
  1791. }
  1792. else
  1793. {
  1794. res.numTex = 2;
  1795. res.numTexReg = 2;
  1796. }
  1797. return res;
  1798. }
  1799. void ReflectCubeFeatHLSL::setTexData( Material::StageData &stageDat,
  1800. const MaterialFeatureData &stageFeatures,
  1801. RenderPassData &passData,
  1802. U32 &texIndex )
  1803. {
  1804. // set up a gloss map if one is not present in the current pass
  1805. // but is present in the current material stage
  1806. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1807. !passData.mFeatureData.features[MFT_NormalMap] )
  1808. {
  1809. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1810. if ( tex && stageFeatures.features[MFT_DiffuseMap] )
  1811. {
  1812. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  1813. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1814. }
  1815. else
  1816. {
  1817. tex = stageDat.getTex( MFT_NormalMap );
  1818. if ( tex && stageFeatures.features[ MFT_NormalMap ] )
  1819. {
  1820. passData.mSamplerNames[ texIndex ] = "bumpMap";
  1821. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1822. }
  1823. }
  1824. }
  1825. if( stageDat.getCubemap() )
  1826. {
  1827. passData.mCubeMap = stageDat.getCubemap();
  1828. passData.mSamplerNames[texIndex] = "cubeMap";
  1829. passData.mTexType[texIndex++] = Material::Cube;
  1830. }
  1831. else
  1832. {
  1833. if( stageFeatures.features[MFT_CubeMap] )
  1834. {
  1835. // assuming here that it is a scenegraph cubemap
  1836. passData.mSamplerNames[texIndex] = "cubeMap";
  1837. passData.mTexType[texIndex++] = Material::SGCube;
  1838. }
  1839. }
  1840. }
  1841. //****************************************************************************
  1842. // RTLighting
  1843. //****************************************************************************
  1844. RTLightingFeatHLSL::RTLightingFeatHLSL()
  1845. : mDep( "shaders/common/lighting.hlsl" )
  1846. {
  1847. addDependency( &mDep );
  1848. }
  1849. void RTLightingFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1850. const MaterialFeatureData &fd )
  1851. {
  1852. MultiLine *meta = new MultiLine;
  1853. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1854. // Special case for lighting imposters. We dont have a vert normal and may not
  1855. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1856. if ( fd.features[MFT_ImposterVert] )
  1857. {
  1858. if ( !fd.features[MFT_NormalMap] )
  1859. {
  1860. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1861. if ( !eyePos )
  1862. {
  1863. eyePos = new Var( "eyePosWorld", "float3" );
  1864. eyePos->uniform = true;
  1865. eyePos->constSortPos = cspPass;
  1866. }
  1867. Var *inPosition = (Var*)LangElement::find( "position" );
  1868. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1869. outNormal->setName( "wsNormal" );
  1870. outNormal->setStructName( "OUT" );
  1871. outNormal->setType( "float3" );
  1872. outNormal->mapsToSampler = false;
  1873. // Transform the normal to world space.
  1874. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );
  1875. }
  1876. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1877. output = meta;
  1878. return;
  1879. }
  1880. // Find the incoming vertex normal.
  1881. Var *inNormal = (Var*)LangElement::find( "normal" );
  1882. // Skip out on realtime lighting if we don't have a normal
  1883. // or we're doing some sort of baked lighting.
  1884. if ( !inNormal ||
  1885. fd.features[MFT_LightMap] ||
  1886. fd.features[MFT_ToneMap] ||
  1887. fd.features[MFT_VertLit] )
  1888. return;
  1889. // If there isn't a normal map then we need to pass
  1890. // the world space normal to the pixel shader ourselves.
  1891. if ( !fd.features[MFT_NormalMap] )
  1892. {
  1893. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1894. outNormal->setName( "wsNormal" );
  1895. outNormal->setStructName( "OUT" );
  1896. outNormal->setType( "float3" );
  1897. outNormal->mapsToSampler = false;
  1898. // Get the transform to world space.
  1899. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1900. // Transform the normal to world space.
  1901. meta->addStatement( new GenOp( " @ = mul( @, float4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1902. }
  1903. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1904. output = meta;
  1905. }
  1906. void RTLightingFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1907. const MaterialFeatureData &fd )
  1908. {
  1909. // Skip out on realtime lighting if we don't have a normal
  1910. // or we're doing some sort of baked lighting.
  1911. //
  1912. // TODO: We can totally detect for this in the material
  1913. // feature setup... we should move it out of here!
  1914. //
  1915. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1916. return;
  1917. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1918. MultiLine *meta = new MultiLine;
  1919. // Look for a wsNormal or grab it from the connector.
  1920. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  1921. if ( !wsNormal )
  1922. {
  1923. wsNormal = connectComp->getElement( RT_TEXCOORD );
  1924. wsNormal->setName( "wsNormal" );
  1925. wsNormal->setStructName( "IN" );
  1926. wsNormal->setType( "float3" );
  1927. // If we loaded the normal its our responsibility
  1928. // to normalize it... the interpolators won't.
  1929. //
  1930. // Note we cast to half here to get partial precision
  1931. // optimized code which is an acceptable loss of
  1932. // precision for normals and performs much better
  1933. // on older Geforce cards.
  1934. //
  1935. meta->addStatement( new GenOp( " @ = normalize( half3( @ ) );\r\n", wsNormal, wsNormal ) );
  1936. }
  1937. // Now the wsPosition and wsView.
  1938. Var *wsPosition = getInWsPosition( componentList );
  1939. Var *wsView = getWsView( wsPosition, meta );
  1940. // Create temporaries to hold results of lighting.
  1941. Var *rtShading = new Var( "rtShading", "float4" );
  1942. Var *specular = new Var( "specular", "float4" );
  1943. meta->addStatement( new GenOp( " @; @;\r\n",
  1944. new DecOp( rtShading ), new DecOp( specular ) ) );
  1945. // Look for a light mask generated from a previous
  1946. // feature (this is done for BL terrain lightmaps).
  1947. LangElement *lightMask = LangElement::find( "lightMask" );
  1948. if ( !lightMask )
  1949. lightMask = new GenOp( "float4( 1, 1, 1, 1 )" );
  1950. // Get all the light constants.
  1951. Var *inLightPos = new Var( "inLightPos", "float4" );
  1952. inLightPos->uniform = true;
  1953. inLightPos->arraySize = 3;
  1954. inLightPos->constSortPos = cspPotentialPrimitive;
  1955. Var *inLightInvRadiusSq = new Var( "inLightInvRadiusSq", "float4" );
  1956. inLightInvRadiusSq->uniform = true;
  1957. inLightInvRadiusSq->constSortPos = cspPotentialPrimitive;
  1958. Var *inLightColor = new Var( "inLightColor", "float4" );
  1959. inLightColor->uniform = true;
  1960. inLightColor->arraySize = 4;
  1961. inLightColor->constSortPos = cspPotentialPrimitive;
  1962. Var *inLightSpotDir = new Var( "inLightSpotDir", "float4" );
  1963. inLightSpotDir->uniform = true;
  1964. inLightSpotDir->arraySize = 3;
  1965. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1966. Var *inLightSpotAngle = new Var( "inLightSpotAngle", "float4" );
  1967. inLightSpotAngle->uniform = true;
  1968. inLightSpotAngle->constSortPos = cspPotentialPrimitive;
  1969. Var *lightSpotFalloff = new Var( "inLightSpotFalloff", "float4" );
  1970. lightSpotFalloff->uniform = true;
  1971. lightSpotFalloff->constSortPos = cspPotentialPrimitive;
  1972. Var *specularPower = new Var( "specularPower", "float" );
  1973. specularPower->uniform = true;
  1974. specularPower->constSortPos = cspPotentialPrimitive;
  1975. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  1976. if ( !specularColor )
  1977. {
  1978. specularColor = new Var( "specularColor", "float4" );
  1979. specularColor->uniform = true;
  1980. specularColor->constSortPos = cspPotentialPrimitive;
  1981. }
  1982. Var *ambient = new Var( "ambient", "float4" );
  1983. ambient->uniform = true;
  1984. ambient->constSortPos = cspPass;
  1985. // Calculate the diffuse shading and specular powers.
  1986. meta->addStatement( new GenOp( " compute4Lights( @, @, @, @,\r\n"
  1987. " @, @, @, @, @, @, @, @,\r\n"
  1988. " @, @ );\r\n",
  1989. wsView, wsPosition, wsNormal, lightMask,
  1990. inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, specularPower, specularColor,
  1991. rtShading, specular ) );
  1992. // Apply the lighting to the diffuse color.
  1993. LangElement *lighting = new GenOp( "float4( @.rgb + @.rgb, 1 )", rtShading, ambient );
  1994. meta->addStatement( new GenOp( " @;\r\n", assignColor( lighting, Material::Mul ) ) );
  1995. output = meta;
  1996. }
  1997. ShaderFeature::Resources RTLightingFeatHLSL::getResources( const MaterialFeatureData &fd )
  1998. {
  1999. Resources res;
  2000. // These features disable realtime lighting.
  2001. if ( !fd.features[MFT_LightMap] &&
  2002. !fd.features[MFT_ToneMap] &&
  2003. !fd.features[MFT_VertLit] )
  2004. {
  2005. // If enabled we pass the position.
  2006. res.numTexReg = 1;
  2007. // If there isn't a bump map then we pass the
  2008. // world space normal as well.
  2009. if ( !fd.features[MFT_NormalMap] )
  2010. res.numTexReg++;
  2011. }
  2012. return res;
  2013. }
  2014. //****************************************************************************
  2015. // Fog
  2016. //****************************************************************************
  2017. FogFeatHLSL::FogFeatHLSL()
  2018. : mFogDep( "shaders/common/torque.hlsl" )
  2019. {
  2020. addDependency( &mFogDep );
  2021. }
  2022. void FogFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2023. const MaterialFeatureData &fd )
  2024. {
  2025. MultiLine *meta = new MultiLine;
  2026. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  2027. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  2028. {
  2029. // Grab the eye position.
  2030. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2031. if ( !eyePos )
  2032. {
  2033. eyePos = new Var( "eyePosWorld", "float3" );
  2034. eyePos->uniform = true;
  2035. eyePos->constSortPos = cspPass;
  2036. }
  2037. Var *fogData = new Var( "fogData", "float3" );
  2038. fogData->uniform = true;
  2039. fogData->constSortPos = cspPass;
  2040. Var *wsPosition = new Var( "fogPos", "float3" );
  2041. getWsPosition( componentList,
  2042. fd.features[MFT_UseInstancing],
  2043. meta,
  2044. new DecOp( wsPosition ) );
  2045. // We pass the fog amount to the pixel shader.
  2046. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2047. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  2048. fogAmount->setName( "fogAmount" );
  2049. fogAmount->setStructName( "OUT" );
  2050. fogAmount->setType( "float" );
  2051. fogAmount->mapsToSampler = false;
  2052. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  2053. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  2054. }
  2055. else
  2056. {
  2057. // We fog in world space... make sure the world space
  2058. // position is passed to the pixel shader. This is
  2059. // often already passed for lighting, so it takes up
  2060. // no extra output registers.
  2061. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  2062. }
  2063. output = meta;
  2064. }
  2065. void FogFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2066. const MaterialFeatureData &fd )
  2067. {
  2068. MultiLine *meta = new MultiLine;
  2069. Var *fogColor = new Var;
  2070. fogColor->setType( "float4" );
  2071. fogColor->setName( "fogColor" );
  2072. fogColor->uniform = true;
  2073. fogColor->constSortPos = cspPass;
  2074. // Get the out color.
  2075. Var *color = (Var*) LangElement::find( "col" );
  2076. if ( !color )
  2077. {
  2078. color = new Var;
  2079. color->setType( "fragout" );
  2080. color->setName( "col" );
  2081. color->setStructName( "OUT" );
  2082. }
  2083. Var *fogAmount;
  2084. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  2085. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  2086. {
  2087. // Per-vertex.... just get the fog amount.
  2088. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2089. fogAmount = connectComp->getElement( RT_TEXCOORD );
  2090. fogAmount->setName( "fogAmount" );
  2091. fogAmount->setStructName( "IN" );
  2092. fogAmount->setType( "float" );
  2093. }
  2094. else
  2095. {
  2096. Var *wsPosition = getInWsPosition( componentList );
  2097. // grab the eye position
  2098. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2099. if ( !eyePos )
  2100. {
  2101. eyePos = new Var( "eyePosWorld", "float3" );
  2102. eyePos->uniform = true;
  2103. eyePos->constSortPos = cspPass;
  2104. }
  2105. Var *fogData = new Var( "fogData", "float3" );
  2106. fogData->uniform = true;
  2107. fogData->constSortPos = cspPass;
  2108. /// Get the fog amount.
  2109. fogAmount = new Var( "fogAmount", "float" );
  2110. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  2111. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  2112. }
  2113. // Lerp between the fog color and diffuse color.
  2114. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  2115. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  2116. output = meta;
  2117. }
  2118. ShaderFeature::Resources FogFeatHLSL::getResources( const MaterialFeatureData &fd )
  2119. {
  2120. Resources res;
  2121. res.numTexReg = 1;
  2122. return res;
  2123. }
  2124. //****************************************************************************
  2125. // Visibility
  2126. //****************************************************************************
  2127. VisibilityFeatHLSL::VisibilityFeatHLSL()
  2128. : mTorqueDep( "shaders/common/torque.hlsl" )
  2129. {
  2130. addDependency( &mTorqueDep );
  2131. }
  2132. void VisibilityFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2133. const MaterialFeatureData &fd )
  2134. {
  2135. MultiLine *meta = new MultiLine;
  2136. output = meta;
  2137. if ( fd.features[ MFT_UseInstancing ] )
  2138. {
  2139. // We pass the visibility to the pixel shader via
  2140. // another output register.
  2141. //
  2142. // TODO: We should see if we can share this register
  2143. // with some other common instanced data.
  2144. //
  2145. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2146. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  2147. outVisibility->setStructName( "OUT" );
  2148. outVisibility->setName( "visibility" );
  2149. outVisibility->setType( "float" );
  2150. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  2151. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  2152. instVisibility->setStructName( "IN" );
  2153. instVisibility->setName( "inst_visibility" );
  2154. instVisibility->setType( "float" );
  2155. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  2156. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  2157. }
  2158. if ( fd.features[ MFT_IsTranslucent ] )
  2159. return;
  2160. addOutVpos( meta, componentList );
  2161. }
  2162. void VisibilityFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2163. const MaterialFeatureData &fd )
  2164. {
  2165. // Get the visibility constant.
  2166. Var *visibility = NULL;
  2167. if ( fd.features[ MFT_UseInstancing ] )
  2168. visibility = getInTexCoord( "visibility", "float", false, componentList );
  2169. else
  2170. {
  2171. visibility = (Var*)LangElement::find( "visibility" );
  2172. if ( !visibility )
  2173. {
  2174. visibility = new Var();
  2175. visibility->setType( "float" );
  2176. visibility->setName( "visibility" );
  2177. visibility->uniform = true;
  2178. visibility->constSortPos = cspPotentialPrimitive;
  2179. }
  2180. }
  2181. MultiLine *meta = new MultiLine;
  2182. output = meta;
  2183. // Translucent objects do a simple alpha fade.
  2184. if ( fd.features[ MFT_IsTranslucent ] )
  2185. {
  2186. Var *color = (Var*)LangElement::find( "col" );
  2187. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  2188. return;
  2189. }
  2190. // Everything else does a fizzle.
  2191. Var *vPos = getInVpos( meta, componentList );
  2192. // vpos is a float4 in d3d11
  2193. meta->addStatement( new GenOp( " fizzle( @.xy, @ );\r\n", vPos, visibility ) );
  2194. }
  2195. ShaderFeature::Resources VisibilityFeatHLSL::getResources( const MaterialFeatureData &fd )
  2196. {
  2197. Resources res;
  2198. // TODO: Fix for instancing.
  2199. if ( !fd.features[ MFT_IsTranslucent ] )
  2200. res.numTexReg = 1;
  2201. return res;
  2202. }
  2203. //****************************************************************************
  2204. // AlphaTest
  2205. //****************************************************************************
  2206. void AlphaTestHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2207. const MaterialFeatureData &fd )
  2208. {
  2209. // If we're below SM3 and don't have a depth output
  2210. // feature then don't waste an instruction here.
  2211. if ( GFX->getPixelShaderVersion() < 3.0 &&
  2212. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  2213. !fd.features[ MFT_DepthOut ] )
  2214. {
  2215. output = NULL;
  2216. return;
  2217. }
  2218. // If we don't have a color var then we cannot do an alpha test.
  2219. Var *color = (Var*)LangElement::find( "col1" );
  2220. if (!color)
  2221. color = (Var*)LangElement::find("col");
  2222. if ( !color )
  2223. {
  2224. output = NULL;
  2225. return;
  2226. }
  2227. // Now grab the alpha test value.
  2228. Var *alphaTestVal = new Var;
  2229. alphaTestVal->setType( "float" );
  2230. alphaTestVal->setName( "alphaTestValue" );
  2231. alphaTestVal->uniform = true;
  2232. alphaTestVal->constSortPos = cspPotentialPrimitive;
  2233. // Do the clip.
  2234. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  2235. }
  2236. //****************************************************************************
  2237. // GlowMask
  2238. //****************************************************************************
  2239. void GlowMaskHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2240. const MaterialFeatureData &fd )
  2241. {
  2242. output = NULL;
  2243. // Get the output color... and make it black to mask out
  2244. // glow passes rendered before us.
  2245. //
  2246. // The shader compiler will optimize out all the other
  2247. // code above that doesn't contribute to the alpha mask.
  2248. Var *color = (Var*)LangElement::find( "col" );
  2249. if ( color )
  2250. output = new GenOp( " @.rgb = 0;\r\n", color );
  2251. }
  2252. //****************************************************************************
  2253. // RenderTargetZero
  2254. //****************************************************************************
  2255. void RenderTargetZeroHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  2256. {
  2257. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  2258. // This will prevent a divide by zero causing an FP special on float render targets
  2259. output = new GenOp( " @;\r\n", assignColor( new GenOp( "0.00001" ), Material::None, NULL, mOutputTargetMask ) );
  2260. }
  2261. //****************************************************************************
  2262. // HDR Output
  2263. //****************************************************************************
  2264. HDROutHLSL::HDROutHLSL()
  2265. : mTorqueDep( "shaders/common/torque.hlsl" )
  2266. {
  2267. addDependency( &mTorqueDep );
  2268. }
  2269. void HDROutHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2270. const MaterialFeatureData &fd )
  2271. {
  2272. // Let the helper function do the work.
  2273. Var *color = (Var*)LangElement::find( "col" );
  2274. if ( color )
  2275. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2276. }
  2277. //****************************************************************************
  2278. // FoliageFeatureHLSL
  2279. //****************************************************************************
  2280. #include "T3D/fx/groundCover.h"
  2281. FoliageFeatureHLSL::FoliageFeatureHLSL()
  2282. : mDep( "shaders/common/foliage.hlsl" )
  2283. {
  2284. addDependency( &mDep );
  2285. }
  2286. void FoliageFeatureHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2287. const MaterialFeatureData &fd )
  2288. {
  2289. // Get the input variables we need.
  2290. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2291. if ( !inPosition )
  2292. inPosition = (Var*)LangElement::find( "position" );
  2293. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2294. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2295. MultiLine *meta = new MultiLine;
  2296. // Declare the normal and tangent variables since they do not exist
  2297. // in this vert type, but we do need to set them up for others.
  2298. Var *normal = (Var*)LangElement::find( "normal" );
  2299. AssertFatal( normal, "FoliageFeatureHLSL requires vert normal!" );
  2300. Var *tangent = new Var;
  2301. tangent->setType( "float3" );
  2302. tangent->setName( "T" );
  2303. LangElement *tangentDec = new DecOp( tangent );
  2304. meta->addStatement( new GenOp( " @;\n", tangentDec ) );
  2305. // We add a float foliageFade to the OUT structure.
  2306. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2307. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2308. fade->setName( "foliageFade" );
  2309. fade->setStructName( "OUT" );
  2310. fade->setType( "float" );
  2311. // grab the eye position
  2312. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2313. if ( !eyePos )
  2314. {
  2315. eyePos = new Var( "eyePosWorld", "float3" );
  2316. eyePos->uniform = true;
  2317. eyePos->constSortPos = cspPass;
  2318. }
  2319. // All actual work is offloaded to this method.
  2320. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2321. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2322. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2323. output = meta;
  2324. }
  2325. void FoliageFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2326. const MaterialFeatureData &fd )
  2327. {
  2328. // Find / create IN.foliageFade
  2329. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2330. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2331. fade->setName( "foliageFade" );
  2332. fade->setStructName( "IN" );
  2333. fade->setType( "float" );
  2334. // Find / create visibility
  2335. Var *visibility = (Var*) LangElement::find( "visibility" );
  2336. if ( !visibility )
  2337. {
  2338. visibility = new Var();
  2339. visibility->setType( "float" );
  2340. visibility->setName( "visibility" );
  2341. visibility->uniform = true;
  2342. visibility->constSortPos = cspPotentialPrimitive;
  2343. }
  2344. MultiLine *meta = new MultiLine;
  2345. // Multiply foliageFade into visibility.
  2346. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2347. output = meta;
  2348. }
  2349. void FoliageFeatureHLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2350. {
  2351. // This isn't really necessary since the outFeatureData will be filtered after
  2352. // this call.
  2353. if ( features.hasFeature( MFT_Foliage ) )
  2354. outFeatureData->features.addFeature( type );
  2355. }
  2356. ShaderFeatureConstHandles* FoliageFeatureHLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2357. {
  2358. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2359. AssertFatal( gcover != NULL, "FoliageFeatureHLSL::createConstHandles - userObject was not valid!" );
  2360. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2361. handles->mGroundCover = gcover;
  2362. handles->init( shader );
  2363. return handles;
  2364. }
  2365. void ParticleNormalFeatureHLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2366. {
  2367. MultiLine *meta = new MultiLine;
  2368. output = meta;
  2369. // Calculate normal and tangent values since we want to keep particle verts
  2370. // as light-weight as possible
  2371. Var *normal = (Var*) LangElement::find("normal");
  2372. if(normal == NULL)
  2373. {
  2374. normal = new Var;
  2375. normal->setType( "float3" );
  2376. normal->setName( "normal" );
  2377. // These values are not accidental. It is slightly adjusted from facing straight into the
  2378. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2379. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2380. // [11/23/2009 Pat]
  2381. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2382. }
  2383. Var *T = (Var*) LangElement::find( "T" );
  2384. if(T == NULL)
  2385. {
  2386. T = new Var;
  2387. T->setType( "float3" );
  2388. T->setName( "T" );
  2389. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2390. }
  2391. }
  2392. //****************************************************************************
  2393. // ImposterVertFeatureHLSL
  2394. //****************************************************************************
  2395. ImposterVertFeatureHLSL::ImposterVertFeatureHLSL()
  2396. : mDep( "shaders/common/imposter.hlsl" )
  2397. {
  2398. addDependency( &mDep );
  2399. }
  2400. void ImposterVertFeatureHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2401. const MaterialFeatureData &fd )
  2402. {
  2403. MultiLine *meta = new MultiLine;
  2404. output = meta;
  2405. // Get the input vertex variables.
  2406. Var *inPosition = (Var*)LangElement::find( "position" );
  2407. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2408. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2409. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2410. // Get the input shader constants.
  2411. Var *imposterLimits = new Var;
  2412. imposterLimits->setType( "float4" );
  2413. imposterLimits->setName( "imposterLimits" );
  2414. imposterLimits->uniform = true;
  2415. imposterLimits->constSortPos = cspPotentialPrimitive;
  2416. Var *imposterUVs = new Var;
  2417. imposterUVs->setType( "float4" );
  2418. imposterUVs->setName( "imposterUVs" );
  2419. imposterUVs->arraySize = 64; // See imposter.hlsl
  2420. imposterUVs->uniform = true;
  2421. imposterUVs->constSortPos = cspPotentialPrimitive;
  2422. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2423. if ( !eyePos )
  2424. {
  2425. eyePos = new Var( "eyePosWorld", "float3" );
  2426. eyePos->uniform = true;
  2427. eyePos->constSortPos = cspPass;
  2428. }
  2429. // Declare the outputs from this feature.
  2430. Var *outInPosition = new Var;
  2431. outInPosition->setType( "float3" );
  2432. outInPosition->setName( "inPosition" );
  2433. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2434. Var *outTexCoord = new Var;
  2435. outTexCoord->setType( "float2" );
  2436. outTexCoord->setName( "texCoord" );
  2437. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2438. Var *outWorldToTangent = new Var;
  2439. outWorldToTangent->setType( "float3x3" );
  2440. outWorldToTangent->setName( "worldToTangent" );
  2441. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2442. // Add imposterFade to the OUT structure.
  2443. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2444. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2445. outFade->setName( "imposterFade" );
  2446. outFade->setStructName( "OUT" );
  2447. outFade->setType( "float" );
  2448. // Assign OUT.imposterFade
  2449. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2450. // All actual work is done in this method.
  2451. meta->addStatement( new GenOp( " imposter_v( @.xyz, @.w, @.x * length(@), normalize(@), normalize(@), @.y, @.x, @.z, @.w, @, @, @, @, @ );\r\n",
  2452. inPosition,
  2453. inPosition,
  2454. inMiscParams,
  2455. inRightVec,
  2456. inUpVec,
  2457. inRightVec,
  2458. imposterLimits,
  2459. imposterLimits,
  2460. imposterLimits,
  2461. imposterLimits,
  2462. eyePos,
  2463. imposterUVs,
  2464. outInPosition,
  2465. outTexCoord,
  2466. outWorldToTangent ) );
  2467. // Copy the position to wsPosition for use in shaders
  2468. // down stream instead of looking for objTrans.
  2469. Var *wsPosition = new Var;
  2470. wsPosition->setType( "float3" );
  2471. wsPosition->setName( "wsPosition" );
  2472. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2473. // If we new viewToTangent... its the same as the
  2474. // world to tangent for an imposter.
  2475. Var *viewToTangent = new Var;
  2476. viewToTangent->setType( "float3x3" );
  2477. viewToTangent->setName( "viewToTangent" );
  2478. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2479. }
  2480. void ImposterVertFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2481. const MaterialFeatureData &fd )
  2482. {
  2483. // Find / create IN.imposterFade
  2484. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2485. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2486. fade->setName( "imposterFade" );
  2487. fade->setStructName( "IN" );
  2488. fade->setType( "float" );
  2489. // Find / create visibility
  2490. Var *visibility = (Var*) LangElement::find( "visibility" );
  2491. if ( !visibility )
  2492. {
  2493. visibility = new Var();
  2494. visibility->setType( "float" );
  2495. visibility->setName( "visibility" );
  2496. visibility->uniform = true;
  2497. visibility->constSortPos = cspPotentialPrimitive;
  2498. }
  2499. MultiLine *meta = new MultiLine;
  2500. // Multiply foliageFade into visibility.
  2501. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2502. output = meta;
  2503. }
  2504. void ImposterVertFeatureHLSL::determineFeature( Material *material,
  2505. const GFXVertexFormat *vertexFormat,
  2506. U32 stageNum,
  2507. const FeatureType &type,
  2508. const FeatureSet &features,
  2509. MaterialFeatureData *outFeatureData )
  2510. {
  2511. if ( features.hasFeature( MFT_ImposterVert ) )
  2512. outFeatureData->features.addFeature( MFT_ImposterVert );
  2513. }
  2514. //****************************************************************************
  2515. // Vertex position
  2516. //****************************************************************************
  2517. void DeferredSkyHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2518. const MaterialFeatureData &fd )
  2519. {
  2520. Var *outPosition = (Var*)LangElement::find( "hpos" );
  2521. MultiLine *meta = new MultiLine;
  2522. //meta->addStatement( new GenOp( " @.w = @.z;\r\n", outPosition, outPosition ) );
  2523. output = meta;
  2524. }