shaderFeatureHLSL.cpp 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712
  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(@, @);\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(@, @) * @;\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. void DiffuseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  697. const MaterialFeatureData &fd )
  698. {
  699. MultiLine *meta = new MultiLine;
  700. getOutTexCoord( "texCoord",
  701. "float2",
  702. true,
  703. fd.features[MFT_TexAnim],
  704. meta,
  705. componentList );
  706. output = meta;
  707. }
  708. void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  709. const MaterialFeatureData &fd )
  710. {
  711. // grab connector texcoord register
  712. Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
  713. // create texture var
  714. Var *diffuseMap = new Var;
  715. diffuseMap->setType( "sampler2D" );
  716. diffuseMap->setName( "diffuseMap" );
  717. diffuseMap->uniform = true;
  718. diffuseMap->sampler = true;
  719. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  720. if ( fd.features[MFT_CubeMap] )
  721. {
  722. MultiLine * meta = new MultiLine;
  723. // create sample color
  724. Var *diffColor = new Var;
  725. diffColor->setType( "float4" );
  726. diffColor->setName( "diffuseColor" );
  727. LangElement *colorDecl = new DecOp( diffColor );
  728. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n",
  729. colorDecl,
  730. diffuseMap,
  731. inTex ) );
  732. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffColor, Material::Mul ) ) );
  733. output = meta;
  734. }
  735. else if(fd.features[MFT_DiffuseMapAtlas])
  736. {
  737. // Handle atlased textures
  738. // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
  739. MultiLine * meta = new MultiLine;
  740. output = meta;
  741. Var *atlasedTex = new Var;
  742. atlasedTex->setName("atlasedTexCoord");
  743. atlasedTex->setType("float2");
  744. LangElement *atDecl = new DecOp(atlasedTex);
  745. // Parameters of the texture atlas
  746. Var *atParams = new Var;
  747. atParams->setType("float4");
  748. atParams->setName("diffuseAtlasParams");
  749. atParams->uniform = true;
  750. atParams->constSortPos = cspPotentialPrimitive;
  751. // Parameters of the texture (tile) this object is using in the atlas
  752. Var *tileParams = new Var;
  753. tileParams->setType("float4");
  754. tileParams->setName("diffuseAtlasTileParams");
  755. tileParams->uniform = true;
  756. tileParams->constSortPos = cspPotentialPrimitive;
  757. const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
  758. if(is_sm3)
  759. {
  760. // Figure out the mip level
  761. meta->addStatement(new GenOp(" float2 _dx = ddx(@ * @.z);\r\n", inTex, atParams));
  762. meta->addStatement(new GenOp(" float2 _dy = ddy(@ * @.z);\r\n", inTex, atParams));
  763. meta->addStatement(new GenOp(" float mipLod = 0.5 * log2(max(dot(_dx, _dx), dot(_dy, _dy)));\r\n"));
  764. meta->addStatement(new GenOp(" mipLod = clamp(mipLod, 0.0, @.w);\r\n", atParams));
  765. // And the size of the mip level
  766. meta->addStatement(new GenOp(" float mipPixSz = pow(2.0, @.w - mipLod);\r\n", atParams));
  767. meta->addStatement(new GenOp(" float2 mipSz = mipPixSz / @.xy;\r\n", atParams));
  768. }
  769. else
  770. {
  771. meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
  772. }
  773. // Tiling mode
  774. // TODO: Select wrap or clamp somehow
  775. if( true ) // Wrap
  776. meta->addStatement(new GenOp(" @ = frac(@);\r\n", atDecl, inTex));
  777. else // Clamp
  778. meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, inTex));
  779. // Finally scale/offset, and correct for filtering
  780. meta->addStatement(new GenOp(" @ = @ * ((mipSz * @.xy - 1.0) / mipSz) + 0.5 / mipSz + @.xy * @.xy;\r\n",
  781. atlasedTex, atlasedTex, atParams, atParams, tileParams));
  782. // Add a newline
  783. meta->addStatement(new GenOp( "\r\n"));
  784. // For the rest of the feature...
  785. inTex = atlasedTex;
  786. // create sample color var
  787. Var *diffColor = new Var;
  788. diffColor->setType("float4");
  789. diffColor->setName("diffuseColor");
  790. // To dump out UV coords...
  791. //#define DEBUG_ATLASED_UV_COORDS
  792. #ifdef DEBUG_ATLASED_UV_COORDS
  793. if(!fd.features[MFT_PrePassConditioner])
  794. {
  795. meta->addStatement(new GenOp(" @ = float4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
  796. meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul)));
  797. return;
  798. }
  799. #endif
  800. if(is_sm3)
  801. {
  802. meta->addStatement(new GenOp( " @ = tex2Dlod(@, float4(@, 0.0, mipLod));\r\n",
  803. new DecOp(diffColor), diffuseMap, inTex));
  804. }
  805. else
  806. {
  807. meta->addStatement(new GenOp( " @ = tex2D(@, @);\r\n",
  808. new DecOp(diffColor), diffuseMap, inTex));
  809. }
  810. meta->addStatement(new GenOp( " @;\r\n", assignColor(diffColor, Material::Mul)));
  811. }
  812. else
  813. {
  814. LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
  815. output = new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) );
  816. }
  817. }
  818. ShaderFeature::Resources DiffuseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  819. {
  820. Resources res;
  821. res.numTex = 1;
  822. res.numTexReg = 1;
  823. return res;
  824. }
  825. void DiffuseMapFeatHLSL::setTexData( Material::StageData &stageDat,
  826. const MaterialFeatureData &fd,
  827. RenderPassData &passData,
  828. U32 &texIndex )
  829. {
  830. GFXTextureObject *tex = stageDat.getTex( MFT_DiffuseMap );
  831. if ( tex )
  832. {
  833. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  834. passData.mTexSlot[ texIndex++ ].texObject = tex;
  835. }
  836. }
  837. //****************************************************************************
  838. // Overlay Texture
  839. //****************************************************************************
  840. void OverlayTexFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  841. const MaterialFeatureData &fd )
  842. {
  843. Var *inTex = getVertTexCoord( "texCoord2" );
  844. AssertFatal( inTex, "OverlayTexFeatHLSL::processVert() - The second UV set was not found!" );
  845. // grab connector texcoord register
  846. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  847. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  848. outTex->setName( "outTexCoord2" );
  849. outTex->setStructName( "OUT" );
  850. outTex->setType( "float2" );
  851. outTex->mapsToSampler = true;
  852. if( fd.features[MFT_TexAnim] )
  853. {
  854. inTex->setType( "float4" );
  855. // Find or create the texture matrix.
  856. Var *texMat = (Var*)LangElement::find( "texMat" );
  857. if ( !texMat )
  858. {
  859. texMat = new Var;
  860. texMat->setType( "float4x4" );
  861. texMat->setName( "texMat" );
  862. texMat->uniform = true;
  863. texMat->constSortPos = cspPass;
  864. }
  865. output = new GenOp( " @ = mul(@, @);\r\n", outTex, texMat, inTex );
  866. return;
  867. }
  868. // setup language elements to output incoming tex coords to output
  869. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  870. }
  871. void OverlayTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  872. const MaterialFeatureData &fd )
  873. {
  874. // grab connector texcoord register
  875. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  876. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  877. inTex->setName( "texCoord2" );
  878. inTex->setStructName( "IN" );
  879. inTex->setType( "float2" );
  880. inTex->mapsToSampler = true;
  881. // create texture var
  882. Var *diffuseMap = new Var;
  883. diffuseMap->setType( "sampler2D" );
  884. diffuseMap->setName( "overlayMap" );
  885. diffuseMap->uniform = true;
  886. diffuseMap->sampler = true;
  887. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  888. LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
  889. output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
  890. }
  891. ShaderFeature::Resources OverlayTexFeatHLSL::getResources( const MaterialFeatureData &fd )
  892. {
  893. Resources res;
  894. res.numTex = 1;
  895. res.numTexReg = 1;
  896. return res;
  897. }
  898. void OverlayTexFeatHLSL::setTexData( Material::StageData &stageDat,
  899. const MaterialFeatureData &fd,
  900. RenderPassData &passData,
  901. U32 &texIndex )
  902. {
  903. GFXTextureObject *tex = stageDat.getTex( MFT_OverlayMap );
  904. if ( tex )
  905. {
  906. passData.mSamplerNames[texIndex] = "overlayMap";
  907. passData.mTexSlot[ texIndex++ ].texObject = tex;
  908. }
  909. }
  910. //****************************************************************************
  911. // Diffuse color
  912. //****************************************************************************
  913. void DiffuseFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  914. const MaterialFeatureData &fd )
  915. {
  916. Var *diffuseMaterialColor = new Var;
  917. diffuseMaterialColor->setType( "float4" );
  918. diffuseMaterialColor->setName( "diffuseMaterialColor" );
  919. diffuseMaterialColor->uniform = true;
  920. diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
  921. MultiLine * meta = new MultiLine;
  922. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffuseMaterialColor, Material::Mul ) ) );
  923. output = meta;
  924. }
  925. //****************************************************************************
  926. // Diffuse vertex color
  927. //****************************************************************************
  928. void DiffuseVertColorFeatureHLSL::processVert( Vector< ShaderComponent* >& componentList,
  929. const MaterialFeatureData& fd )
  930. {
  931. // Create vertex color connector if it doesn't exist.
  932. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  933. if( !outColor )
  934. {
  935. // Search for vert color.
  936. Var* inColor = dynamic_cast< Var* >( LangElement::find( "diffuse" ) );
  937. if( !inColor )
  938. {
  939. output = NULL;
  940. return;
  941. }
  942. // Create connector.
  943. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  944. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  945. Var* outColor = connectComp->getElement( RT_COLOR );
  946. outColor->setName( "vertColor" );
  947. outColor->setStructName( "OUT" );
  948. outColor->setType( "float4" );
  949. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  950. }
  951. else
  952. output = NULL; // Nothing we need to do.
  953. }
  954. void DiffuseVertColorFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  955. const MaterialFeatureData &fd )
  956. {
  957. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  958. if( !vertColor )
  959. {
  960. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  961. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  962. vertColor = connectComp->getElement( RT_COLOR );
  963. vertColor->setName( "vertColor" );
  964. vertColor->setStructName( "IN" );
  965. vertColor->setType( "float4" );
  966. }
  967. MultiLine* meta = new MultiLine;
  968. meta->addStatement( new GenOp( " @;\r\n", assignColor( vertColor, Material::Mul ) ) );
  969. output = meta;
  970. }
  971. //****************************************************************************
  972. // Lightmap
  973. //****************************************************************************
  974. void LightmapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  975. const MaterialFeatureData &fd )
  976. {
  977. // grab tex register from incoming vert
  978. Var *inTex = getVertTexCoord( "texCoord2" );
  979. // grab connector texcoord register
  980. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  981. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  982. outTex->setName( "texCoord2" );
  983. outTex->setStructName( "OUT" );
  984. outTex->setType( "float2" );
  985. outTex->mapsToSampler = true;
  986. // setup language elements to output incoming tex coords to output
  987. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  988. }
  989. void LightmapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  990. const MaterialFeatureData &fd )
  991. {
  992. // grab connector texcoord register
  993. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  994. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  995. inTex->setName( "texCoord2" );
  996. inTex->setStructName( "IN" );
  997. inTex->setType( "float2" );
  998. inTex->mapsToSampler = true;
  999. // create texture var
  1000. Var *lightMap = new Var;
  1001. lightMap->setType( "sampler2D" );
  1002. lightMap->setName( "lightMap" );
  1003. lightMap->uniform = true;
  1004. lightMap->sampler = true;
  1005. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1006. // argh, pixel specular should prob use this too
  1007. if( fd.features[MFT_NormalMap] )
  1008. {
  1009. Var *lmColor = new Var;
  1010. lmColor->setName( "lmColor" );
  1011. lmColor->setType( "float4" );
  1012. LangElement *lmColorDecl = new DecOp( lmColor );
  1013. output = new GenOp( " @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
  1014. return;
  1015. }
  1016. // Add realtime lighting, if it is available
  1017. LangElement *statement = NULL;
  1018. if( fd.features[MFT_RTLighting] )
  1019. {
  1020. // Advanced lighting is the only dynamic lighting supported right now
  1021. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1022. if(inColor != NULL)
  1023. {
  1024. // Find out if RTLighting should be added or substituted
  1025. bool bPreProcessedLighting = false;
  1026. AdvancedLightBinManager *lightBin;
  1027. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1028. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1029. // Lightmap has already been included in the advanced light bin, so
  1030. // no need to do any sampling or anything
  1031. if(bPreProcessedLighting)
  1032. statement = new GenOp( "float4(@, 1.0)", inColor );
  1033. else
  1034. statement = new GenOp( "tex2D(@, @) + float4(@.rgb, 0.0)", lightMap, inTex, inColor );
  1035. }
  1036. }
  1037. // If we still don't have it... then just sample the lightmap.
  1038. if ( !statement )
  1039. statement = new GenOp( "tex2D(@, @)", lightMap, inTex );
  1040. // Assign to proper render target
  1041. MultiLine *meta = new MultiLine;
  1042. if( fd.features[MFT_LightbufferMRT] )
  1043. {
  1044. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1045. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
  1046. }
  1047. else
  1048. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
  1049. output = meta;
  1050. }
  1051. ShaderFeature::Resources LightmapFeatHLSL::getResources( const MaterialFeatureData &fd )
  1052. {
  1053. Resources res;
  1054. res.numTex = 1;
  1055. res.numTexReg = 1;
  1056. return res;
  1057. }
  1058. void LightmapFeatHLSL::setTexData( Material::StageData &stageDat,
  1059. const MaterialFeatureData &fd,
  1060. RenderPassData &passData,
  1061. U32 &texIndex )
  1062. {
  1063. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  1064. passData.mSamplerNames[ texIndex ] = "lightMap";
  1065. if ( tex )
  1066. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1067. else
  1068. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  1069. }
  1070. U32 LightmapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1071. {
  1072. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1073. }
  1074. //****************************************************************************
  1075. // Tonemap
  1076. //****************************************************************************
  1077. void TonemapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1078. const MaterialFeatureData &fd )
  1079. {
  1080. // Grab the connector
  1081. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1082. // Set up the second set of texCoords
  1083. Var *inTex2 = getVertTexCoord( "texCoord2" );
  1084. if ( inTex2 )
  1085. {
  1086. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  1087. outTex2->setName( "texCoord2" );
  1088. outTex2->setStructName( "OUT" );
  1089. outTex2->setType( "float2" );
  1090. outTex2->mapsToSampler = true;
  1091. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  1092. }
  1093. }
  1094. void TonemapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1095. const MaterialFeatureData &fd )
  1096. {
  1097. // Grab connector
  1098. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1099. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  1100. inTex2->setName( "texCoord2" );
  1101. inTex2->setStructName( "IN" );
  1102. inTex2->setType( "float2" );
  1103. inTex2->mapsToSampler = true;
  1104. // create texture var
  1105. Var *toneMap = new Var;
  1106. toneMap->setType( "sampler2D" );
  1107. toneMap->setName( "toneMap" );
  1108. toneMap->uniform = true;
  1109. toneMap->sampler = true;
  1110. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1111. MultiLine * meta = new MultiLine;
  1112. // First get the toneMap color
  1113. Var *toneMapColor = new Var;
  1114. toneMapColor->setType( "float4" );
  1115. toneMapColor->setName( "toneMapColor" );
  1116. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  1117. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
  1118. // We do a different calculation if there is a diffuse map or not
  1119. Material::BlendOp blendOp = Material::Mul;
  1120. if ( fd.features[MFT_DiffuseMap] )
  1121. {
  1122. // Reverse the tonemap
  1123. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
  1124. // Re-tonemap with the current color factored in
  1125. blendOp = Material::ToneMap;
  1126. }
  1127. // Find out if RTLighting should be added
  1128. bool bPreProcessedLighting = false;
  1129. AdvancedLightBinManager *lightBin;
  1130. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1131. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1132. // Add in the realtime lighting contribution
  1133. if ( fd.features[MFT_RTLighting] )
  1134. {
  1135. // Right now, only Advanced Lighting is supported
  1136. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1137. if(inColor != NULL)
  1138. {
  1139. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1140. // the dynamic light buffer, and it already has the tonemap included
  1141. if(bPreProcessedLighting)
  1142. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  1143. else
  1144. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  1145. }
  1146. }
  1147. // Assign to proper render target
  1148. if( fd.features[MFT_LightbufferMRT] )
  1149. {
  1150. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1151. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
  1152. }
  1153. else
  1154. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  1155. output = meta;
  1156. }
  1157. ShaderFeature::Resources TonemapFeatHLSL::getResources( const MaterialFeatureData &fd )
  1158. {
  1159. Resources res;
  1160. res.numTex = 1;
  1161. res.numTexReg = 1;
  1162. return res;
  1163. }
  1164. void TonemapFeatHLSL::setTexData( Material::StageData &stageDat,
  1165. const MaterialFeatureData &fd,
  1166. RenderPassData &passData,
  1167. U32 &texIndex )
  1168. {
  1169. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  1170. if ( tex )
  1171. {
  1172. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  1173. passData.mSamplerNames[ texIndex ] = "toneMap";
  1174. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1175. }
  1176. }
  1177. U32 TonemapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1178. {
  1179. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1180. }
  1181. //****************************************************************************
  1182. // pureLIGHT Lighting
  1183. //****************************************************************************
  1184. void VertLitHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1185. const MaterialFeatureData &fd )
  1186. {
  1187. // If we have a lightMap or toneMap then our lighting will be
  1188. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1189. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1190. {
  1191. output = NULL;
  1192. return;
  1193. }
  1194. // Create vertex color connector if it doesn't exist.
  1195. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1196. if( !outColor )
  1197. {
  1198. // Search for vert color
  1199. Var *inColor = (Var*) LangElement::find( "diffuse" );
  1200. // If there isn't a vertex color then we can't do anything
  1201. if( !inColor )
  1202. {
  1203. output = NULL;
  1204. return;
  1205. }
  1206. // Grab the connector color
  1207. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1208. Var *outColor = connectComp->getElement( RT_COLOR );
  1209. outColor->setName( "vertColor" );
  1210. outColor->setStructName( "OUT" );
  1211. outColor->setType( "float4" );
  1212. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1213. }
  1214. else
  1215. output = NULL; // Nothing we need to do.
  1216. }
  1217. void VertLitHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1218. const MaterialFeatureData &fd )
  1219. {
  1220. // If we have a lightMap or toneMap then our lighting will be
  1221. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1222. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1223. {
  1224. output = NULL;
  1225. return;
  1226. }
  1227. // Grab the connector color register
  1228. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1229. if( !vertColor )
  1230. {
  1231. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1232. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1233. vertColor = connectComp->getElement( RT_COLOR );
  1234. vertColor->setName( "vertColor" );
  1235. vertColor->setStructName( "IN" );
  1236. vertColor->setType( "float4" );
  1237. }
  1238. MultiLine * meta = new MultiLine;
  1239. // Defaults (no diffuse map)
  1240. Material::BlendOp blendOp = Material::Mul;
  1241. LangElement *outColor = vertColor;
  1242. // We do a different calculation if there is a diffuse map or not
  1243. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1244. {
  1245. Var * finalVertColor = new Var;
  1246. finalVertColor->setName( "finalVertColor" );
  1247. finalVertColor->setType( "float4" );
  1248. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1249. // Reverse the tonemap
  1250. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
  1251. // Set the blend op to tonemap
  1252. blendOp = Material::ToneMap;
  1253. outColor = finalVertColor;
  1254. }
  1255. // Add in the realtime lighting contribution, if applicable
  1256. if ( fd.features[MFT_RTLighting] )
  1257. {
  1258. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1259. if(rtLightingColor != NULL)
  1260. {
  1261. bool bPreProcessedLighting = false;
  1262. AdvancedLightBinManager *lightBin;
  1263. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1264. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1265. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1266. // the dynamic light buffer, and it already has the baked-vertex-color
  1267. // included in it
  1268. if(bPreProcessedLighting)
  1269. outColor = new GenOp( "float4(@.rgb, 1.0)", rtLightingColor );
  1270. else
  1271. outColor = new GenOp( "float4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
  1272. }
  1273. }
  1274. // Output the color
  1275. if ( fd.features[MFT_LightbufferMRT] )
  1276. {
  1277. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1278. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
  1279. }
  1280. else
  1281. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1282. output = meta;
  1283. }
  1284. U32 VertLitHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1285. {
  1286. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1287. }
  1288. //****************************************************************************
  1289. // Detail map
  1290. //****************************************************************************
  1291. void DetailFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1292. const MaterialFeatureData &fd )
  1293. {
  1294. MultiLine *meta = new MultiLine;
  1295. addOutDetailTexCoord( componentList,
  1296. meta,
  1297. fd.features[MFT_TexAnim] );
  1298. output = meta;
  1299. }
  1300. void DetailFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1301. const MaterialFeatureData &fd )
  1302. {
  1303. // Get the detail texture coord.
  1304. Var *inTex = getInTexCoord( "detCoord", "float2", true, componentList );
  1305. // create texture var
  1306. Var *detailMap = new Var;
  1307. detailMap->setType( "sampler2D" );
  1308. detailMap->setName( "detailMap" );
  1309. detailMap->uniform = true;
  1310. detailMap->sampler = true;
  1311. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1312. // We're doing the standard greyscale detail map
  1313. // technique which can darken and lighten the
  1314. // diffuse texture.
  1315. // TODO: We could add a feature to toggle between this
  1316. // and a simple multiplication with the detail map.
  1317. LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
  1318. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1319. }
  1320. ShaderFeature::Resources DetailFeatHLSL::getResources( const MaterialFeatureData &fd )
  1321. {
  1322. Resources res;
  1323. res.numTex = 1;
  1324. res.numTexReg = 1;
  1325. return res;
  1326. }
  1327. void DetailFeatHLSL::setTexData( Material::StageData &stageDat,
  1328. const MaterialFeatureData &fd,
  1329. RenderPassData &passData,
  1330. U32 &texIndex )
  1331. {
  1332. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1333. if ( tex )
  1334. {
  1335. passData.mSamplerNames[texIndex] = "detailMap";
  1336. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1337. }
  1338. }
  1339. //****************************************************************************
  1340. // Vertex position
  1341. //****************************************************************************
  1342. void VertPositionHLSL::determineFeature( Material *material,
  1343. const GFXVertexFormat *vertexFormat,
  1344. U32 stageNum,
  1345. const FeatureType &type,
  1346. const FeatureSet &features,
  1347. MaterialFeatureData *outFeatureData )
  1348. {
  1349. // This feature is always on!
  1350. outFeatureData->features.addFeature( type );
  1351. }
  1352. void VertPositionHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1353. const MaterialFeatureData &fd )
  1354. {
  1355. // First check for an input position from a previous feature
  1356. // then look for the default vertex position.
  1357. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1358. if ( !inPosition )
  1359. inPosition = (Var*)LangElement::find( "position" );
  1360. // grab connector position
  1361. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1362. Var *outPosition = connectComp->getElement( RT_POSITION );
  1363. outPosition->setName( "hpos" );
  1364. outPosition->setStructName( "OUT" );
  1365. MultiLine *meta = new MultiLine;
  1366. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1367. meta->addStatement( new GenOp( " @ = mul(@, float4(@.xyz,1));\r\n",
  1368. outPosition, modelview, inPosition ) );
  1369. output = meta;
  1370. }
  1371. //****************************************************************************
  1372. // Reflect Cubemap
  1373. //****************************************************************************
  1374. void ReflectCubeFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1375. const MaterialFeatureData &fd )
  1376. {
  1377. // search for vert normal
  1378. Var *inNormal = (Var*) LangElement::find( "normal" );
  1379. if ( !inNormal )
  1380. return;
  1381. MultiLine * meta = new MultiLine;
  1382. // If a base or bump tex is present in the material, but not in the
  1383. // current pass - we need to add one to the current pass to use
  1384. // its alpha channel as a gloss map. Here we just need the tex coords.
  1385. if( !fd.features[MFT_DiffuseMap] &&
  1386. !fd.features[MFT_NormalMap] )
  1387. {
  1388. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1389. fd.materialFeatures[MFT_NormalMap] )
  1390. {
  1391. // find incoming texture var
  1392. Var *inTex = getVertTexCoord( "texCoord" );
  1393. // grab connector texcoord register
  1394. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1395. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1396. outTex->setName( "texCoord" );
  1397. outTex->setStructName( "OUT" );
  1398. outTex->setType( "float2" );
  1399. outTex->mapsToSampler = true;
  1400. // setup language elements to output incoming tex coords to output
  1401. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1402. }
  1403. }
  1404. // create cubeTrans
  1405. bool useInstancing = fd.features[MFT_UseInstancing];
  1406. Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
  1407. // cube vert position
  1408. Var * cubeVertPos = new Var;
  1409. cubeVertPos->setName( "cubeVertPos" );
  1410. cubeVertPos->setType( "float3" );
  1411. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1412. meta->addStatement( new GenOp( " @ = mul(@, float4(@,1)).xyz;\r\n",
  1413. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1414. // cube normal
  1415. Var * cubeNormal = new Var;
  1416. cubeNormal->setName( "cubeNormal" );
  1417. cubeNormal->setType( "float3" );
  1418. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1419. meta->addStatement( new GenOp( " @ = normalize( mul(@, float4(normalize(@),0.0)).xyz );\r\n",
  1420. cubeNormDecl, cubeTrans, inNormal ) );
  1421. // grab the eye position
  1422. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1423. if ( !eyePos )
  1424. {
  1425. eyePos = new Var( "eyePosWorld", "float3" );
  1426. eyePos->uniform = true;
  1427. eyePos->constSortPos = cspPass;
  1428. }
  1429. // eye to vert
  1430. Var * eyeToVert = new Var;
  1431. eyeToVert->setName( "eyeToVert" );
  1432. eyeToVert->setType( "float3" );
  1433. LangElement *e2vDecl = new DecOp( eyeToVert );
  1434. meta->addStatement( new GenOp( " @ = @ - @;\r\n",
  1435. e2vDecl, cubeVertPos, eyePos ) );
  1436. // grab connector texcoord register
  1437. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1438. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1439. reflectVec->setName( "reflectVec" );
  1440. reflectVec->setStructName( "OUT" );
  1441. reflectVec->setType( "float3" );
  1442. reflectVec->mapsToSampler = true;
  1443. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1444. output = meta;
  1445. }
  1446. void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1447. const MaterialFeatureData &fd )
  1448. {
  1449. MultiLine * meta = new MultiLine;
  1450. Var *glossColor = NULL;
  1451. // If a base or bump tex is present in the material, but not in the
  1452. // current pass - we need to add one to the current pass to use
  1453. // its alpha channel as a gloss map.
  1454. if( !fd.features[MFT_DiffuseMap] &&
  1455. !fd.features[MFT_NormalMap] )
  1456. {
  1457. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1458. fd.materialFeatures[MFT_NormalMap] )
  1459. {
  1460. // grab connector texcoord register
  1461. Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
  1462. // create texture var
  1463. Var *newMap = new Var;
  1464. newMap->setType( "sampler2D" );
  1465. newMap->setName( "glossMap" );
  1466. newMap->uniform = true;
  1467. newMap->sampler = true;
  1468. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1469. // create sample color
  1470. Var *color = new Var;
  1471. color->setType( "float4" );
  1472. color->setName( "diffuseColor" );
  1473. LangElement *colorDecl = new DecOp( color );
  1474. glossColor = color;
  1475. meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
  1476. }
  1477. }
  1478. else
  1479. {
  1480. glossColor = (Var*) LangElement::find( "diffuseColor" );
  1481. if( !glossColor )
  1482. glossColor = (Var*) LangElement::find( "bumpNormal" );
  1483. }
  1484. // grab connector texcoord register
  1485. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1486. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1487. reflectVec->setName( "reflectVec" );
  1488. reflectVec->setStructName( "IN" );
  1489. reflectVec->setType( "float3" );
  1490. reflectVec->mapsToSampler = true;
  1491. // create cubemap var
  1492. Var *cubeMap = new Var;
  1493. cubeMap->setType( "samplerCUBE" );
  1494. cubeMap->setName( "cubeMap" );
  1495. cubeMap->uniform = true;
  1496. cubeMap->sampler = true;
  1497. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1498. // TODO: Restore the lighting attenuation here!
  1499. Var *attn = NULL;
  1500. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1501. //attn = (Var*)LangElement::find("attn");
  1502. //else
  1503. if ( fd.materialFeatures[MFT_RTLighting] )
  1504. attn =(Var*)LangElement::find("d_NL_Att");
  1505. LangElement *texCube = new GenOp( "texCUBE( @, @ )", cubeMap, reflectVec );
  1506. LangElement *lerpVal = NULL;
  1507. Material::BlendOp blendOp = Material::LerpAlpha;
  1508. // Note that the lerpVal needs to be a float4 so that
  1509. // it will work with the LerpAlpha blend.
  1510. if ( glossColor )
  1511. {
  1512. if ( attn )
  1513. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1514. else
  1515. lerpVal = glossColor;
  1516. }
  1517. else
  1518. {
  1519. if ( attn )
  1520. lerpVal = new GenOp( "saturate( @ ).xxxx", attn );
  1521. else
  1522. blendOp = Material::Mul;
  1523. }
  1524. meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
  1525. output = meta;
  1526. }
  1527. ShaderFeature::Resources ReflectCubeFeatHLSL::getResources( const MaterialFeatureData &fd )
  1528. {
  1529. Resources res;
  1530. if( fd.features[MFT_DiffuseMap] ||
  1531. fd.features[MFT_NormalMap] )
  1532. {
  1533. res.numTex = 1;
  1534. res.numTexReg = 1;
  1535. }
  1536. else
  1537. {
  1538. res.numTex = 2;
  1539. res.numTexReg = 2;
  1540. }
  1541. return res;
  1542. }
  1543. void ReflectCubeFeatHLSL::setTexData( Material::StageData &stageDat,
  1544. const MaterialFeatureData &stageFeatures,
  1545. RenderPassData &passData,
  1546. U32 &texIndex )
  1547. {
  1548. // set up a gloss map if one is not present in the current pass
  1549. // but is present in the current material stage
  1550. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1551. !passData.mFeatureData.features[MFT_NormalMap] )
  1552. {
  1553. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1554. if ( tex && stageFeatures.features[MFT_DiffuseMap] )
  1555. {
  1556. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  1557. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1558. }
  1559. else
  1560. {
  1561. tex = stageDat.getTex( MFT_NormalMap );
  1562. if ( tex && stageFeatures.features[ MFT_NormalMap ] )
  1563. {
  1564. passData.mSamplerNames[ texIndex ] = "bumpMap";
  1565. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1566. }
  1567. }
  1568. }
  1569. if( stageDat.getCubemap() )
  1570. {
  1571. passData.mCubeMap = stageDat.getCubemap();
  1572. passData.mSamplerNames[texIndex] = "cubeMap";
  1573. passData.mTexType[texIndex++] = Material::Cube;
  1574. }
  1575. else
  1576. {
  1577. if( stageFeatures.features[MFT_CubeMap] )
  1578. {
  1579. // assuming here that it is a scenegraph cubemap
  1580. passData.mSamplerNames[texIndex] = "cubeMap";
  1581. passData.mTexType[texIndex++] = Material::SGCube;
  1582. }
  1583. }
  1584. }
  1585. //****************************************************************************
  1586. // RTLighting
  1587. //****************************************************************************
  1588. RTLightingFeatHLSL::RTLightingFeatHLSL()
  1589. : mDep( "shaders/common/lighting.hlsl" )
  1590. {
  1591. addDependency( &mDep );
  1592. }
  1593. void RTLightingFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1594. const MaterialFeatureData &fd )
  1595. {
  1596. MultiLine *meta = new MultiLine;
  1597. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1598. // Special case for lighting imposters. We dont have a vert normal and may not
  1599. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1600. if ( fd.features[MFT_ImposterVert] )
  1601. {
  1602. if ( !fd.features[MFT_NormalMap] )
  1603. {
  1604. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1605. if ( !eyePos )
  1606. {
  1607. eyePos = new Var( "eyePosWorld", "float3" );
  1608. eyePos->uniform = true;
  1609. eyePos->constSortPos = cspPass;
  1610. }
  1611. Var *inPosition = (Var*)LangElement::find( "position" );
  1612. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1613. outNormal->setName( "wsNormal" );
  1614. outNormal->setStructName( "OUT" );
  1615. outNormal->setType( "float3" );
  1616. outNormal->mapsToSampler = false;
  1617. // Transform the normal to world space.
  1618. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );
  1619. }
  1620. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1621. output = meta;
  1622. return;
  1623. }
  1624. // Find the incoming vertex normal.
  1625. Var *inNormal = (Var*)LangElement::find( "normal" );
  1626. // Skip out on realtime lighting if we don't have a normal
  1627. // or we're doing some sort of baked lighting.
  1628. if ( !inNormal ||
  1629. fd.features[MFT_LightMap] ||
  1630. fd.features[MFT_ToneMap] ||
  1631. fd.features[MFT_VertLit] )
  1632. return;
  1633. // If there isn't a normal map then we need to pass
  1634. // the world space normal to the pixel shader ourselves.
  1635. if ( !fd.features[MFT_NormalMap] )
  1636. {
  1637. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1638. outNormal->setName( "wsNormal" );
  1639. outNormal->setStructName( "OUT" );
  1640. outNormal->setType( "float3" );
  1641. outNormal->mapsToSampler = false;
  1642. // Get the transform to world space.
  1643. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1644. // Transform the normal to world space.
  1645. meta->addStatement( new GenOp( " @ = mul( @, float4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1646. }
  1647. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1648. output = meta;
  1649. }
  1650. void RTLightingFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1651. const MaterialFeatureData &fd )
  1652. {
  1653. // Skip out on realtime lighting if we don't have a normal
  1654. // or we're doing some sort of baked lighting.
  1655. //
  1656. // TODO: We can totally detect for this in the material
  1657. // feature setup... we should move it out of here!
  1658. //
  1659. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1660. return;
  1661. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1662. MultiLine *meta = new MultiLine;
  1663. // Look for a wsNormal or grab it from the connector.
  1664. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  1665. if ( !wsNormal )
  1666. {
  1667. wsNormal = connectComp->getElement( RT_TEXCOORD );
  1668. wsNormal->setName( "wsNormal" );
  1669. wsNormal->setStructName( "IN" );
  1670. wsNormal->setType( "float3" );
  1671. // If we loaded the normal its our responsibility
  1672. // to normalize it... the interpolators won't.
  1673. //
  1674. // Note we cast to half here to get partial precision
  1675. // optimized code which is an acceptable loss of
  1676. // precision for normals and performs much better
  1677. // on older Geforce cards.
  1678. //
  1679. meta->addStatement( new GenOp( " @ = normalize( half3( @ ) );\r\n", wsNormal, wsNormal ) );
  1680. }
  1681. // Now the wsPosition and wsView.
  1682. Var *wsPosition = getInWsPosition( componentList );
  1683. Var *wsView = getWsView( wsPosition, meta );
  1684. // Create temporaries to hold results of lighting.
  1685. Var *rtShading = new Var( "rtShading", "float4" );
  1686. Var *specular = new Var( "specular", "float4" );
  1687. meta->addStatement( new GenOp( " @; @;\r\n",
  1688. new DecOp( rtShading ), new DecOp( specular ) ) );
  1689. // Look for a light mask generated from a previous
  1690. // feature (this is done for BL terrain lightmaps).
  1691. LangElement *lightMask = LangElement::find( "lightMask" );
  1692. if ( !lightMask )
  1693. lightMask = new GenOp( "float4( 1, 1, 1, 1 )" );
  1694. // Get all the light constants.
  1695. Var *inLightPos = new Var( "inLightPos", "float4" );
  1696. inLightPos->uniform = true;
  1697. inLightPos->arraySize = 3;
  1698. inLightPos->constSortPos = cspPotentialPrimitive;
  1699. Var *inLightInvRadiusSq = new Var( "inLightInvRadiusSq", "float4" );
  1700. inLightInvRadiusSq->uniform = true;
  1701. inLightInvRadiusSq->constSortPos = cspPotentialPrimitive;
  1702. Var *inLightColor = new Var( "inLightColor", "float4" );
  1703. inLightColor->uniform = true;
  1704. inLightColor->arraySize = 4;
  1705. inLightColor->constSortPos = cspPotentialPrimitive;
  1706. Var *inLightSpotDir = new Var( "inLightSpotDir", "float4" );
  1707. inLightSpotDir->uniform = true;
  1708. inLightSpotDir->arraySize = 3;
  1709. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1710. Var *inLightSpotAngle = new Var( "inLightSpotAngle", "float4" );
  1711. inLightSpotAngle->uniform = true;
  1712. inLightSpotAngle->constSortPos = cspPotentialPrimitive;
  1713. Var *lightSpotFalloff = new Var( "inLightSpotFalloff", "float4" );
  1714. lightSpotFalloff->uniform = true;
  1715. lightSpotFalloff->constSortPos = cspPotentialPrimitive;
  1716. Var *specularPower = new Var( "specularPower", "float" );
  1717. specularPower->uniform = true;
  1718. specularPower->constSortPos = cspPotentialPrimitive;
  1719. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  1720. if ( !specularColor )
  1721. {
  1722. specularColor = new Var( "specularColor", "float4" );
  1723. specularColor->uniform = true;
  1724. specularColor->constSortPos = cspPotentialPrimitive;
  1725. }
  1726. Var *ambient = new Var( "ambient", "float4" );
  1727. ambient->uniform = true;
  1728. ambient->constSortPos = cspPass;
  1729. // Calculate the diffuse shading and specular powers.
  1730. meta->addStatement( new GenOp( " compute4Lights( @, @, @, @,\r\n"
  1731. " @, @, @, @, @, @, @, @,\r\n"
  1732. " @, @ );\r\n",
  1733. wsView, wsPosition, wsNormal, lightMask,
  1734. inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, specularPower, specularColor,
  1735. rtShading, specular ) );
  1736. // Apply the lighting to the diffuse color.
  1737. LangElement *lighting = new GenOp( "float4( @.rgb + @.rgb, 1 )", rtShading, ambient );
  1738. meta->addStatement( new GenOp( " @;\r\n", assignColor( lighting, Material::Mul ) ) );
  1739. output = meta;
  1740. }
  1741. ShaderFeature::Resources RTLightingFeatHLSL::getResources( const MaterialFeatureData &fd )
  1742. {
  1743. Resources res;
  1744. // These features disable realtime lighting.
  1745. if ( !fd.features[MFT_LightMap] &&
  1746. !fd.features[MFT_ToneMap] &&
  1747. !fd.features[MFT_VertLit] )
  1748. {
  1749. // If enabled we pass the position.
  1750. res.numTexReg = 1;
  1751. // If there isn't a bump map then we pass the
  1752. // world space normal as well.
  1753. if ( !fd.features[MFT_NormalMap] )
  1754. res.numTexReg++;
  1755. }
  1756. return res;
  1757. }
  1758. //****************************************************************************
  1759. // Fog
  1760. //****************************************************************************
  1761. FogFeatHLSL::FogFeatHLSL()
  1762. : mFogDep( "shaders/common/torque.hlsl" )
  1763. {
  1764. addDependency( &mFogDep );
  1765. }
  1766. void FogFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1767. const MaterialFeatureData &fd )
  1768. {
  1769. MultiLine *meta = new MultiLine;
  1770. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1771. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1772. {
  1773. // Grab the eye position.
  1774. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1775. if ( !eyePos )
  1776. {
  1777. eyePos = new Var( "eyePosWorld", "float3" );
  1778. eyePos->uniform = true;
  1779. eyePos->constSortPos = cspPass;
  1780. }
  1781. Var *fogData = new Var( "fogData", "float3" );
  1782. fogData->uniform = true;
  1783. fogData->constSortPos = cspPass;
  1784. Var *wsPosition = new Var( "fogPos", "float3" );
  1785. getWsPosition( componentList,
  1786. fd.features[MFT_UseInstancing],
  1787. meta,
  1788. new DecOp( wsPosition ) );
  1789. // We pass the fog amount to the pixel shader.
  1790. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1791. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1792. fogAmount->setName( "fogAmount" );
  1793. fogAmount->setStructName( "OUT" );
  1794. fogAmount->setType( "float" );
  1795. fogAmount->mapsToSampler = false;
  1796. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1797. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1798. }
  1799. else
  1800. {
  1801. // We fog in world space... make sure the world space
  1802. // position is passed to the pixel shader. This is
  1803. // often already passed for lighting, so it takes up
  1804. // no extra output registers.
  1805. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1806. }
  1807. output = meta;
  1808. }
  1809. void FogFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1810. const MaterialFeatureData &fd )
  1811. {
  1812. MultiLine *meta = new MultiLine;
  1813. Var *fogColor = new Var;
  1814. fogColor->setType( "float4" );
  1815. fogColor->setName( "fogColor" );
  1816. fogColor->uniform = true;
  1817. fogColor->constSortPos = cspPass;
  1818. // Get the out color.
  1819. Var *color = (Var*) LangElement::find( "col" );
  1820. if ( !color )
  1821. {
  1822. color = new Var;
  1823. color->setType( "fragout" );
  1824. color->setName( "col" );
  1825. color->setStructName( "OUT" );
  1826. }
  1827. Var *fogAmount;
  1828. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1829. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1830. {
  1831. // Per-vertex.... just get the fog amount.
  1832. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1833. fogAmount = connectComp->getElement( RT_TEXCOORD );
  1834. fogAmount->setName( "fogAmount" );
  1835. fogAmount->setStructName( "IN" );
  1836. fogAmount->setType( "float" );
  1837. }
  1838. else
  1839. {
  1840. Var *wsPosition = getInWsPosition( componentList );
  1841. // grab the eye position
  1842. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1843. if ( !eyePos )
  1844. {
  1845. eyePos = new Var( "eyePosWorld", "float3" );
  1846. eyePos->uniform = true;
  1847. eyePos->constSortPos = cspPass;
  1848. }
  1849. Var *fogData = new Var( "fogData", "float3" );
  1850. fogData->uniform = true;
  1851. fogData->constSortPos = cspPass;
  1852. /// Get the fog amount.
  1853. fogAmount = new Var( "fogAmount", "float" );
  1854. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1855. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  1856. }
  1857. // Lerp between the fog color and diffuse color.
  1858. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  1859. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  1860. output = meta;
  1861. }
  1862. ShaderFeature::Resources FogFeatHLSL::getResources( const MaterialFeatureData &fd )
  1863. {
  1864. Resources res;
  1865. res.numTexReg = 1;
  1866. return res;
  1867. }
  1868. //****************************************************************************
  1869. // Visibility
  1870. //****************************************************************************
  1871. VisibilityFeatHLSL::VisibilityFeatHLSL()
  1872. : mTorqueDep( "shaders/common/torque.hlsl" )
  1873. {
  1874. addDependency( &mTorqueDep );
  1875. }
  1876. void VisibilityFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1877. const MaterialFeatureData &fd )
  1878. {
  1879. MultiLine *meta = new MultiLine;
  1880. output = meta;
  1881. if ( fd.features[ MFT_UseInstancing ] )
  1882. {
  1883. // We pass the visibility to the pixel shader via
  1884. // another output register.
  1885. //
  1886. // TODO: We should see if we can share this register
  1887. // with some other common instanced data.
  1888. //
  1889. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1890. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  1891. outVisibility->setStructName( "OUT" );
  1892. outVisibility->setName( "visibility" );
  1893. outVisibility->setType( "float" );
  1894. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  1895. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  1896. instVisibility->setStructName( "IN" );
  1897. instVisibility->setName( "inst_visibility" );
  1898. instVisibility->setType( "float" );
  1899. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  1900. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  1901. }
  1902. if ( fd.features[ MFT_IsTranslucent ] )
  1903. return;
  1904. addOutVpos( meta, componentList );
  1905. }
  1906. void VisibilityFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1907. const MaterialFeatureData &fd )
  1908. {
  1909. // Get the visibility constant.
  1910. Var *visibility = NULL;
  1911. if ( fd.features[ MFT_UseInstancing ] )
  1912. visibility = getInTexCoord( "visibility", "float", false, componentList );
  1913. else
  1914. {
  1915. visibility = (Var*)LangElement::find( "visibility" );
  1916. if ( !visibility )
  1917. {
  1918. visibility = new Var();
  1919. visibility->setType( "float" );
  1920. visibility->setName( "visibility" );
  1921. visibility->uniform = true;
  1922. visibility->constSortPos = cspPotentialPrimitive;
  1923. }
  1924. }
  1925. MultiLine *meta = new MultiLine;
  1926. output = meta;
  1927. // Translucent objects do a simple alpha fade.
  1928. if ( fd.features[ MFT_IsTranslucent ] )
  1929. {
  1930. Var *color = (Var*)LangElement::find( "col" );
  1931. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  1932. return;
  1933. }
  1934. // Everything else does a fizzle.
  1935. Var *vPos = getInVpos( meta, componentList );
  1936. meta->addStatement( new GenOp( " fizzle( @, @ );\r\n", vPos, visibility ) );
  1937. }
  1938. ShaderFeature::Resources VisibilityFeatHLSL::getResources( const MaterialFeatureData &fd )
  1939. {
  1940. Resources res;
  1941. // TODO: Fix for instancing.
  1942. if ( !fd.features[ MFT_IsTranslucent ] )
  1943. res.numTexReg = 1;
  1944. return res;
  1945. }
  1946. //****************************************************************************
  1947. // AlphaTest
  1948. //****************************************************************************
  1949. void AlphaTestHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1950. const MaterialFeatureData &fd )
  1951. {
  1952. // If we're below SM3 and don't have a depth output
  1953. // feature then don't waste an instruction here.
  1954. if ( GFX->getPixelShaderVersion() < 3.0 &&
  1955. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  1956. !fd.features[ MFT_DepthOut ] )
  1957. {
  1958. output = NULL;
  1959. return;
  1960. }
  1961. // If we don't have a color var then we cannot do an alpha test.
  1962. Var *color = (Var*)LangElement::find( "col" );
  1963. if ( !color )
  1964. {
  1965. output = NULL;
  1966. return;
  1967. }
  1968. // Now grab the alpha test value.
  1969. Var *alphaTestVal = new Var;
  1970. alphaTestVal->setType( "float" );
  1971. alphaTestVal->setName( "alphaTestValue" );
  1972. alphaTestVal->uniform = true;
  1973. alphaTestVal->constSortPos = cspPotentialPrimitive;
  1974. // Do the clip.
  1975. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  1976. }
  1977. //****************************************************************************
  1978. // GlowMask
  1979. //****************************************************************************
  1980. void GlowMaskHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1981. const MaterialFeatureData &fd )
  1982. {
  1983. output = NULL;
  1984. // Get the output color... and make it black to mask out
  1985. // glow passes rendered before us.
  1986. //
  1987. // The shader compiler will optimize out all the other
  1988. // code above that doesn't contribute to the alpha mask.
  1989. Var *color = (Var*)LangElement::find( "col" );
  1990. if ( color )
  1991. output = new GenOp( " @.rgb = 0;\r\n", color );
  1992. }
  1993. //****************************************************************************
  1994. // RenderTargetZero
  1995. //****************************************************************************
  1996. void RenderTargetZeroHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  1997. {
  1998. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  1999. // This will prevent a divide by zero causing an FP special on float render targets
  2000. output = new GenOp( " @;\r\n", assignColor( new GenOp( "0.00001" ), Material::None, NULL, mOutputTargetMask ) );
  2001. }
  2002. //****************************************************************************
  2003. // HDR Output
  2004. //****************************************************************************
  2005. HDROutHLSL::HDROutHLSL()
  2006. : mTorqueDep( "shaders/common/torque.hlsl" )
  2007. {
  2008. addDependency( &mTorqueDep );
  2009. }
  2010. void HDROutHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2011. const MaterialFeatureData &fd )
  2012. {
  2013. // Let the helper function do the work.
  2014. Var *color = (Var*)LangElement::find( "col" );
  2015. if ( color )
  2016. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2017. }
  2018. //****************************************************************************
  2019. // FoliageFeatureHLSL
  2020. //****************************************************************************
  2021. #include "T3D/fx/groundCover.h"
  2022. FoliageFeatureHLSL::FoliageFeatureHLSL()
  2023. : mDep( "shaders/common/foliage.hlsl" )
  2024. {
  2025. addDependency( &mDep );
  2026. }
  2027. void FoliageFeatureHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2028. const MaterialFeatureData &fd )
  2029. {
  2030. // Get the input variables we need.
  2031. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2032. if ( !inPosition )
  2033. inPosition = (Var*)LangElement::find( "position" );
  2034. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2035. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2036. MultiLine *meta = new MultiLine;
  2037. // Declare the normal and tangent variables since they do not exist
  2038. // in this vert type, but we do need to set them up for others.
  2039. Var *normal = (Var*)LangElement::find( "normal" );
  2040. AssertFatal( normal, "FoliageFeatureHLSL requires vert normal!" );
  2041. Var *tangent = new Var;
  2042. tangent->setType( "float3" );
  2043. tangent->setName( "T" );
  2044. LangElement *tangentDec = new DecOp( tangent );
  2045. meta->addStatement( new GenOp( " @;\n", tangentDec ) );
  2046. // We add a float foliageFade to the OUT structure.
  2047. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2048. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2049. fade->setName( "foliageFade" );
  2050. fade->setStructName( "OUT" );
  2051. fade->setType( "float" );
  2052. // grab the eye position
  2053. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2054. if ( !eyePos )
  2055. {
  2056. eyePos = new Var( "eyePosWorld", "float3" );
  2057. eyePos->uniform = true;
  2058. eyePos->constSortPos = cspPass;
  2059. }
  2060. // All actual work is offloaded to this method.
  2061. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2062. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2063. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2064. output = meta;
  2065. }
  2066. void FoliageFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2067. const MaterialFeatureData &fd )
  2068. {
  2069. // Find / create IN.foliageFade
  2070. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2071. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2072. fade->setName( "foliageFade" );
  2073. fade->setStructName( "IN" );
  2074. fade->setType( "float" );
  2075. // Find / create visibility
  2076. Var *visibility = (Var*) LangElement::find( "visibility" );
  2077. if ( !visibility )
  2078. {
  2079. visibility = new Var();
  2080. visibility->setType( "float" );
  2081. visibility->setName( "visibility" );
  2082. visibility->uniform = true;
  2083. visibility->constSortPos = cspPotentialPrimitive;
  2084. }
  2085. MultiLine *meta = new MultiLine;
  2086. // Multiply foliageFade into visibility.
  2087. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2088. output = meta;
  2089. }
  2090. void FoliageFeatureHLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2091. {
  2092. // This isn't really necessary since the outFeatureData will be filtered after
  2093. // this call.
  2094. if ( features.hasFeature( MFT_Foliage ) )
  2095. outFeatureData->features.addFeature( type );
  2096. }
  2097. ShaderFeatureConstHandles* FoliageFeatureHLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2098. {
  2099. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2100. AssertFatal( gcover != NULL, "FoliageFeatureHLSL::createConstHandles - userObject was not valid!" );
  2101. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2102. handles->mGroundCover = gcover;
  2103. handles->init( shader );
  2104. return handles;
  2105. }
  2106. void ParticleNormalFeatureHLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2107. {
  2108. MultiLine *meta = new MultiLine;
  2109. output = meta;
  2110. // Calculate normal and tangent values since we want to keep particle verts
  2111. // as light-weight as possible
  2112. Var *normal = (Var*) LangElement::find("normal");
  2113. if(normal == NULL)
  2114. {
  2115. normal = new Var;
  2116. normal->setType( "float3" );
  2117. normal->setName( "normal" );
  2118. // These values are not accidental. It is slightly adjusted from facing straight into the
  2119. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2120. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2121. // [11/23/2009 Pat]
  2122. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2123. }
  2124. Var *T = (Var*) LangElement::find( "T" );
  2125. if(T == NULL)
  2126. {
  2127. T = new Var;
  2128. T->setType( "float3" );
  2129. T->setName( "T" );
  2130. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2131. }
  2132. }
  2133. //****************************************************************************
  2134. // ImposterVertFeatureHLSL
  2135. //****************************************************************************
  2136. ImposterVertFeatureHLSL::ImposterVertFeatureHLSL()
  2137. : mDep( "shaders/common/imposter.hlsl" )
  2138. {
  2139. addDependency( &mDep );
  2140. }
  2141. void ImposterVertFeatureHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2142. const MaterialFeatureData &fd )
  2143. {
  2144. MultiLine *meta = new MultiLine;
  2145. output = meta;
  2146. // Get the input vertex variables.
  2147. Var *inPosition = (Var*)LangElement::find( "position" );
  2148. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2149. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2150. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2151. // Get the input shader constants.
  2152. Var *imposterLimits = new Var;
  2153. imposterLimits->setType( "float4" );
  2154. imposterLimits->setName( "imposterLimits" );
  2155. imposterLimits->uniform = true;
  2156. imposterLimits->constSortPos = cspPotentialPrimitive;
  2157. Var *imposterUVs = new Var;
  2158. imposterUVs->setType( "float4" );
  2159. imposterUVs->setName( "imposterUVs" );
  2160. imposterUVs->arraySize = 64; // See imposter.hlsl
  2161. imposterUVs->uniform = true;
  2162. imposterUVs->constSortPos = cspPotentialPrimitive;
  2163. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2164. if ( !eyePos )
  2165. {
  2166. eyePos = new Var( "eyePosWorld", "float3" );
  2167. eyePos->uniform = true;
  2168. eyePos->constSortPos = cspPass;
  2169. }
  2170. // Declare the outputs from this feature.
  2171. Var *outInPosition = new Var;
  2172. outInPosition->setType( "float3" );
  2173. outInPosition->setName( "inPosition" );
  2174. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2175. Var *outTexCoord = new Var;
  2176. outTexCoord->setType( "float2" );
  2177. outTexCoord->setName( "texCoord" );
  2178. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2179. Var *outWorldToTangent = new Var;
  2180. outWorldToTangent->setType( "float3x3" );
  2181. outWorldToTangent->setName( "worldToTangent" );
  2182. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2183. // Add imposterFade to the OUT structure.
  2184. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2185. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2186. outFade->setName( "imposterFade" );
  2187. outFade->setStructName( "OUT" );
  2188. outFade->setType( "float" );
  2189. // Assign OUT.imposterFade
  2190. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2191. // All actual work is done in this method.
  2192. meta->addStatement( new GenOp( " imposter_v( @.xyz, @.w, @.x * length(@), normalize(@), normalize(@), @.y, @.x, @.z, @.w, @, @, @, @, @ );\r\n",
  2193. inPosition,
  2194. inPosition,
  2195. inMiscParams,
  2196. inRightVec,
  2197. inUpVec,
  2198. inRightVec,
  2199. imposterLimits,
  2200. imposterLimits,
  2201. imposterLimits,
  2202. imposterLimits,
  2203. eyePos,
  2204. imposterUVs,
  2205. outInPosition,
  2206. outTexCoord,
  2207. outWorldToTangent ) );
  2208. // Copy the position to wsPosition for use in shaders
  2209. // down stream instead of looking for objTrans.
  2210. Var *wsPosition = new Var;
  2211. wsPosition->setType( "float3" );
  2212. wsPosition->setName( "wsPosition" );
  2213. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2214. // If we new viewToTangent... its the same as the
  2215. // world to tangent for an imposter.
  2216. Var *viewToTangent = new Var;
  2217. viewToTangent->setType( "float3x3" );
  2218. viewToTangent->setName( "viewToTangent" );
  2219. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2220. }
  2221. void ImposterVertFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2222. const MaterialFeatureData &fd )
  2223. {
  2224. // Find / create IN.imposterFade
  2225. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2226. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2227. fade->setName( "imposterFade" );
  2228. fade->setStructName( "IN" );
  2229. fade->setType( "float" );
  2230. // Find / create visibility
  2231. Var *visibility = (Var*) LangElement::find( "visibility" );
  2232. if ( !visibility )
  2233. {
  2234. visibility = new Var();
  2235. visibility->setType( "float" );
  2236. visibility->setName( "visibility" );
  2237. visibility->uniform = true;
  2238. visibility->constSortPos = cspPotentialPrimitive;
  2239. }
  2240. MultiLine *meta = new MultiLine;
  2241. // Multiply foliageFade into visibility.
  2242. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2243. output = meta;
  2244. }
  2245. void ImposterVertFeatureHLSL::determineFeature( Material *material,
  2246. const GFXVertexFormat *vertexFormat,
  2247. U32 stageNum,
  2248. const FeatureType &type,
  2249. const FeatureSet &features,
  2250. MaterialFeatureData *outFeatureData )
  2251. {
  2252. if ( features.hasFeature( MFT_ImposterVert ) )
  2253. outFeatureData->features.addFeature( MFT_ImposterVert );
  2254. }