shaderFeatureHLSL.cpp 97 KB

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