shaderFeatureHLSL.cpp 113 KB

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