shaderFeatureHLSL.cpp 109 KB

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