shaderFeatureGLSL.cpp 106 KB

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