shaderFeatureGLSL.cpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102
  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( " @ = @ - @;\r\n",
  643. new DecOp( wsView ), eyePos, wsPosition ) );
  644. }
  645. return wsView;
  646. }
  647. Var* ShaderFeatureGLSL::getInWorldNormal(Vector<ShaderComponent*>& componentList)
  648. {
  649. Var* wsNormal = (Var*)LangElement::find("wsNormal");
  650. if (!wsNormal)
  651. {
  652. ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(componentList[C_CONNECTOR]);
  653. wsNormal = connectComp->getElement(RT_TEXCOORD);
  654. wsNormal->setName("wsNormal");
  655. wsNormal->setStructName("IN");
  656. wsNormal->setType("float3");
  657. }
  658. return wsNormal;
  659. }
  660. Var* ShaderFeatureGLSL::addOutDetailTexCoord( Vector<ShaderComponent*> &componentList,
  661. MultiLine *meta,
  662. bool useTexAnim,
  663. bool useFoliageTexCoord)
  664. {
  665. // Check if its already added.
  666. Var *outTex = (Var*)LangElement::find( "detCoord" );
  667. if ( outTex )
  668. return outTex;
  669. // Grab incoming texture coords.
  670. Var *inTex = getVertTexCoord( "texCoord" );
  671. if(useFoliageTexCoord)
  672. inTex->setType("float4");
  673. // create detail variable
  674. Var *detScale = new Var;
  675. detScale->setType( "vec2" );
  676. detScale->setName( "detailScale" );
  677. detScale->uniform = true;
  678. detScale->constSortPos = cspPotentialPrimitive;
  679. // grab connector texcoord register
  680. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  681. outTex = connectComp->getElement( RT_TEXCOORD );
  682. outTex->setName( "detCoord" );
  683. outTex->setStructName( "OUT" );
  684. outTex->setType( "vec2" );
  685. if ( useTexAnim )
  686. {
  687. inTex->setType( "vec4" );
  688. // Find or create the texture matrix.
  689. Var *texMat = (Var*)LangElement::find( "texMat" );
  690. if ( !texMat )
  691. {
  692. texMat = new Var;
  693. texMat->setType( "float4x4" );
  694. texMat->setName( "texMat" );
  695. texMat->uniform = true;
  696. texMat->constSortPos = cspPass;
  697. }
  698. meta->addStatement( new GenOp( " @ = tMul(@.xy, @).xy * @;\r\n", outTex, texMat, inTex, detScale ) );
  699. }
  700. else
  701. {
  702. // setup output to mul texCoord by detail scale
  703. meta->addStatement( new GenOp( " @ = @.xy * @;\r\n", outTex, inTex, detScale ) );
  704. }
  705. return outTex;
  706. }
  707. Var* ShaderFeatureGLSL::getSurface(Vector<ShaderComponent*>& componentList, MultiLine* meta, const MaterialFeatureData& fd)
  708. {
  709. Var *surface = (Var *)LangElement::find("surface");
  710. if (!surface)
  711. {
  712. Var* diffuseColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  713. Var* ormConfig = (Var*)LangElement::find("ORMConfig");
  714. if (!ormConfig)
  715. {
  716. Var* metalness = (Var*)LangElement::find("metalness");
  717. if (!metalness)
  718. {
  719. metalness = new Var("metalness", "float");
  720. metalness->uniform = true;
  721. metalness->constSortPos = cspPotentialPrimitive;
  722. }
  723. Var* roughness = (Var*)LangElement::find("roughness");
  724. if (!roughness)
  725. {
  726. roughness = new Var("roughness", "float");
  727. roughness->uniform = true;
  728. roughness->constSortPos = cspPotentialPrimitive;
  729. }
  730. ormConfig = new Var("ORMConfig", "vec4");
  731. LangElement* colorDecl = new DecOp(ormConfig);
  732. meta->addStatement(new GenOp(" @ = vec4(0.0,1.0,@,@);\r\n", colorDecl, roughness, metalness)); //reconstruct ormConfig, no ao darkening
  733. }
  734. Var* normal = (Var*)LangElement::find("normal");
  735. if (!normal)
  736. {
  737. normal = new Var("normal", "vec3");
  738. meta->addStatement(new GenOp(" @;\r\n\n", new DecOp(normal)));
  739. Var *wsNormal = (Var *)LangElement::find("wsNormal");
  740. if (!wsNormal)
  741. wsNormal = getInWorldNormal(componentList);
  742. meta->addStatement(new GenOp(" @ = normalize( @ );\r\n", normal, wsNormal));
  743. }
  744. Var* wsEyePos = (Var*)LangElement::find("eyePosWorld");
  745. if (!wsEyePos)
  746. {
  747. wsEyePos = new Var("eyePosWorld", "vec3");
  748. wsEyePos->uniform = true;
  749. wsEyePos->constSortPos = cspPass;
  750. }
  751. Var* wsPosition = getInWsPosition(componentList);
  752. Var* wsView = getWsView(wsPosition, meta);
  753. surface = new Var("surface", "Surface");
  754. meta->addStatement(new GenOp(" @ = createForwardSurface(@,normalize(@),@,@,@,@);\r\n\n", new DecOp(surface), diffuseColor, normal, ormConfig,
  755. wsPosition, wsEyePos, wsView));
  756. }
  757. return surface;
  758. }
  759. //****************************************************************************
  760. // Base Texture
  761. //****************************************************************************
  762. DiffuseMapFeatGLSL::DiffuseMapFeatGLSL()
  763. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl"))
  764. {
  765. addDependency(&mTorqueDep);
  766. }
  767. void DiffuseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  768. const MaterialFeatureData &fd )
  769. {
  770. MultiLine *meta = new MultiLine;
  771. getOutTexCoord( "texCoord",
  772. "vec2",
  773. fd.features[MFT_TexAnim],
  774. meta,
  775. componentList );
  776. output = meta;
  777. }
  778. U32 DiffuseMapFeatGLSL::getOutputTargets(const MaterialFeatureData &fd) const
  779. {
  780. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  781. }
  782. void DiffuseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  783. const MaterialFeatureData &fd )
  784. {
  785. // grab connector texcoord register
  786. Var *inTex = getInTexCoord( "texCoord", "vec2", componentList );
  787. //determine output target
  788. ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
  789. if (fd.features[MFT_isDeferred])
  790. targ = ShaderFeature::RenderTarget1;
  791. // create texture var
  792. Var *diffuseMap = new Var;
  793. diffuseMap->setType( "sampler2D" );
  794. diffuseMap->setName( "diffuseMap" );
  795. diffuseMap->uniform = true;
  796. diffuseMap->sampler = true;
  797. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  798. // create sample color var
  799. Var *diffColor = new Var;
  800. diffColor->setType("vec4");
  801. diffColor->setName("diffuseColor");
  802. LangElement *colorDecl = new DecOp( diffColor );
  803. MultiLine * meta = new MultiLine;
  804. output = meta;
  805. if ( fd.features[MFT_CubeMap] )
  806. {
  807. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n",
  808. colorDecl,
  809. diffuseMap,
  810. inTex ) );
  811. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffColor, Material::Mul, NULL, targ) ) );
  812. }
  813. else if(fd.features[MFT_DiffuseMapAtlas])
  814. {
  815. // Handle atlased textures
  816. // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
  817. Var *atlasedTex = new Var;
  818. atlasedTex->setName("atlasedTexCoord");
  819. atlasedTex->setType("vec2");
  820. LangElement *atDecl = new DecOp(atlasedTex);
  821. // Parameters of the texture atlas
  822. Var *atParams = new Var;
  823. atParams->setType("vec4");
  824. atParams->setName("diffuseAtlasParams");
  825. atParams->uniform = true;
  826. atParams->constSortPos = cspPotentialPrimitive;
  827. // Parameters of the texture (tile) this object is using in the atlas
  828. Var *tileParams = new Var;
  829. tileParams->setType("vec4");
  830. tileParams->setName("diffuseAtlasTileParams");
  831. tileParams->uniform = true;
  832. tileParams->constSortPos = cspPotentialPrimitive;
  833. const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
  834. if(is_sm3)
  835. {
  836. // Figure out the mip level
  837. meta->addStatement(new GenOp(" float2 _dx = ddx(@ * @.z);\r\n", inTex, atParams));
  838. meta->addStatement(new GenOp(" float2 _dy = ddy(@ * @.z);\r\n", inTex, atParams));
  839. meta->addStatement(new GenOp(" float mipLod = 0.5 * log2(max(dot(_dx, _dx), dot(_dy, _dy)));\r\n"));
  840. meta->addStatement(new GenOp(" mipLod = clamp(mipLod, 0.0, @.w);\r\n", atParams));
  841. // And the size of the mip level
  842. meta->addStatement(new GenOp(" float mipPixSz = pow(2.0, @.w - mipLod);\r\n", atParams));
  843. meta->addStatement(new GenOp(" float2 mipSz = mipPixSz / @.xy;\r\n", atParams));
  844. }
  845. else
  846. {
  847. meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
  848. }
  849. // Tiling mode
  850. // TODO: Select wrap or clamp somehow
  851. if( true ) // Wrap
  852. meta->addStatement(new GenOp(" @ = frac(@);\r\n", atDecl, inTex));
  853. else // Clamp
  854. meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, inTex));
  855. // Finally scale/offset, and correct for filtering
  856. meta->addStatement(new GenOp(" @ = @ * ((mipSz * @.xy - 1.0) / mipSz) + 0.5 / mipSz + @.xy * @.xy;\r\n",
  857. atlasedTex, atlasedTex, atParams, atParams, tileParams));
  858. // Add a newline
  859. meta->addStatement(new GenOp( "\r\n"));
  860. // For the rest of the feature...
  861. inTex = atlasedTex;
  862. // To dump out UV coords...
  863. //#define DEBUG_ATLASED_UV_COORDS
  864. #ifdef DEBUG_ATLASED_UV_COORDS
  865. if(!fd.features[MFT_DeferredConditioner])
  866. {
  867. meta->addStatement(new GenOp(" @ = vec4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
  868. meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
  869. return;
  870. }
  871. #endif
  872. meta->addStatement(new GenOp( " @ = tex2Dlod(@, float4(@, 0.0, mipLod));\r\n",
  873. new DecOp(diffColor), diffuseMap, inTex));
  874. meta->addStatement(new GenOp( " @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
  875. }
  876. else
  877. {
  878. meta->addStatement(new GenOp("@ = tex2D(@, @);\r\n", colorDecl, diffuseMap, inTex));
  879. meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ)));
  880. }
  881. }
  882. ShaderFeature::Resources DiffuseMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  883. {
  884. Resources res;
  885. res.numTex = 1;
  886. res.numTexReg = 1;
  887. return res;
  888. }
  889. void DiffuseMapFeatGLSL::setTexData( Material::StageData &stageDat,
  890. const MaterialFeatureData &fd,
  891. RenderPassData &passData,
  892. U32 &texIndex )
  893. {
  894. GFXTextureObject *tex = stageDat.getTex( MFT_DiffuseMap );
  895. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  896. passData.mTexSlot[ texIndex++ ].texObject = tex;
  897. }
  898. //****************************************************************************
  899. // Overlay Texture
  900. //****************************************************************************
  901. void OverlayTexFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  902. const MaterialFeatureData &fd )
  903. {
  904. Var *inTex = getVertTexCoord( "texCoord2" );
  905. AssertFatal( inTex, "OverlayTexFeatGLSL::processVert() - The second UV set was not found!" );
  906. // grab connector texcoord register
  907. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  908. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  909. outTex->setName( "outTexCoord2" );
  910. outTex->setStructName( "OUT" );
  911. outTex->setType( "vec2" );
  912. if( fd.features[MFT_TexAnim] )
  913. {
  914. inTex->setType( "vec4" );
  915. // Find or create the texture matrix.
  916. Var *texMat = (Var*)LangElement::find( "texMat" );
  917. if ( !texMat )
  918. {
  919. texMat = new Var;
  920. texMat->setType( "float4x4" );
  921. texMat->setName( "texMat" );
  922. texMat->uniform = true;
  923. texMat->constSortPos = cspPass;
  924. }
  925. output = new GenOp( " @ = tMul(@, @);\r\n", outTex, texMat, inTex );
  926. return;
  927. }
  928. // setup language elements to output incoming tex coords to output
  929. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  930. }
  931. void OverlayTexFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  932. const MaterialFeatureData &fd )
  933. {
  934. // grab connector texcoord register
  935. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  936. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  937. inTex->setName( "texCoord2" );
  938. inTex->setStructName( "IN" );
  939. inTex->setType( "vec2" );
  940. // create texture var
  941. Var *diffuseMap = new Var;
  942. diffuseMap->setType( "sampler2D" );
  943. diffuseMap->setName( "overlayMap" );
  944. diffuseMap->uniform = true;
  945. diffuseMap->sampler = true;
  946. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  947. LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
  948. output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
  949. }
  950. ShaderFeature::Resources OverlayTexFeatGLSL::getResources( const MaterialFeatureData &fd )
  951. {
  952. Resources res;
  953. res.numTex = 1;
  954. res.numTexReg = 1;
  955. return res;
  956. }
  957. void OverlayTexFeatGLSL::setTexData( Material::StageData &stageDat,
  958. const MaterialFeatureData &fd,
  959. RenderPassData &passData,
  960. U32 &texIndex )
  961. {
  962. GFXTextureObject *tex = stageDat.getTex( MFT_OverlayMap );
  963. if ( tex )
  964. {
  965. passData.mSamplerNames[ texIndex ] = "overlayMap";
  966. passData.mTexSlot[ texIndex++ ].texObject = tex;
  967. }
  968. }
  969. //****************************************************************************
  970. // Diffuse color
  971. //****************************************************************************
  972. U32 DiffuseFeatureGLSL::getOutputTargets(const MaterialFeatureData &fd) const
  973. {
  974. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  975. }
  976. void DiffuseFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  977. const MaterialFeatureData &fd )
  978. {
  979. Var* diffuseMaterialColor = new Var;
  980. diffuseMaterialColor->setType( "vec4" );
  981. diffuseMaterialColor->setName( "diffuseMaterialColor" );
  982. diffuseMaterialColor->uniform = true;
  983. diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
  984. MultiLine* meta = new MultiLine;
  985. Var *col = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  986. ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
  987. if (fd.features[MFT_isDeferred])
  988. {
  989. targ = ShaderFeature::RenderTarget1;
  990. col = (Var*)LangElement::find(getOutputTargetVarName(targ));
  991. meta = new MultiLine;
  992. if (!col)
  993. {
  994. // create color var
  995. col = new Var;
  996. col->setType("vec4");
  997. col->setName(getOutputTargetVarName(targ));
  998. col->setStructName("OUT");
  999. meta->addStatement(new GenOp(" @ = vec4(1.0,1.0,1.0,1.0);\r\n", col));
  1000. }
  1001. }
  1002. Material::BlendOp op;
  1003. if (fd.features[MFT_DiffuseMap])
  1004. op = Material::Mul;
  1005. else
  1006. op = Material::None;
  1007. meta->addStatement(new GenOp(" @;\r\n", assignColor(diffuseMaterialColor, op, NULL, targ)));
  1008. output = meta;
  1009. }
  1010. //****************************************************************************
  1011. // Diffuse vertex color
  1012. //****************************************************************************
  1013. void DiffuseVertColorFeatureGLSL::processVert( Vector< ShaderComponent* >& componentList,
  1014. const MaterialFeatureData& fd )
  1015. {
  1016. // Create vertex color connector if it doesn't exist.
  1017. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1018. if( !outColor )
  1019. {
  1020. // Search for vert color.
  1021. Var* inColor = dynamic_cast< Var* >( LangElement::find( "diffuse" ) );
  1022. if( !inColor )
  1023. {
  1024. output = NULL;
  1025. return;
  1026. }
  1027. // Create connector.
  1028. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1029. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1030. outColor = connectComp->getElement( RT_COLOR );
  1031. outColor->setName( "vertColor" );
  1032. outColor->setStructName( "OUT" );
  1033. outColor->setType( "vec4" );
  1034. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1035. }
  1036. else
  1037. output = NULL; // Nothing we need to do.
  1038. }
  1039. void DiffuseVertColorFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1040. const MaterialFeatureData &fd )
  1041. {
  1042. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1043. if( !vertColor )
  1044. {
  1045. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1046. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1047. vertColor = connectComp->getElement( RT_COLOR );
  1048. vertColor->setName( "vertColor" );
  1049. vertColor->setStructName( "IN" );
  1050. vertColor->setType( "vec4" );
  1051. }
  1052. MultiLine* meta = new MultiLine;
  1053. if (fd.features[MFT_isDeferred])
  1054. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul, NULL, ShaderFeature::RenderTarget1)));
  1055. else
  1056. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul)));
  1057. output = meta;
  1058. }
  1059. //****************************************************************************
  1060. // Lightmap
  1061. //****************************************************************************
  1062. void LightmapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1063. const MaterialFeatureData &fd )
  1064. {
  1065. // grab tex register from incoming vert
  1066. Var *inTex = getVertTexCoord( "texCoord2" );
  1067. // grab connector texcoord register
  1068. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1069. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1070. outTex->setName( "texCoord2" );
  1071. outTex->setStructName( "OUT" );
  1072. outTex->setType( "vec2" );
  1073. // setup language elements to output incoming tex coords to output
  1074. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  1075. }
  1076. void LightmapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1077. const MaterialFeatureData &fd )
  1078. {
  1079. // grab connector texcoord register
  1080. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1081. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  1082. inTex->setName( "texCoord2" );
  1083. inTex->setStructName( "IN" );
  1084. inTex->setType( "vec2" );
  1085. // create texture var
  1086. Var *lightMap = new Var;
  1087. lightMap->setType( "sampler2D" );
  1088. lightMap->setName( "lightMap" );
  1089. lightMap->uniform = true;
  1090. lightMap->sampler = true;
  1091. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1092. // argh, pixel specular should prob use this too
  1093. if( fd.features[MFT_NormalMap] )
  1094. {
  1095. Var *lmColor = new Var;
  1096. lmColor->setName( "lmColor" );
  1097. lmColor->setType( "vec4" );
  1098. LangElement *lmColorDecl = new DecOp( lmColor );
  1099. output = new GenOp( " @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
  1100. return;
  1101. }
  1102. // Add realtime lighting, if it is available
  1103. LangElement *statement = NULL;
  1104. if( fd.features[MFT_RTLighting] )
  1105. {
  1106. // Advanced lighting is the only dynamic lighting supported right now
  1107. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1108. if(inColor != NULL)
  1109. {
  1110. // Find out if RTLighting should be added or substituted
  1111. bool bPreProcessedLighting = false;
  1112. AdvancedLightBinManager *lightBin;
  1113. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1114. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1115. // Lightmap has already been included in the advanced light bin, so
  1116. // no need to do any sampling or anything
  1117. if(bPreProcessedLighting)
  1118. statement = new GenOp( "vec4(@, 1.0)", inColor );
  1119. else
  1120. statement = new GenOp( "tex2D(@, @) + vec4(@.rgb, 0.0)", lightMap, inTex, inColor );
  1121. }
  1122. }
  1123. // If we still don't have it... then just sample the lightmap.
  1124. if ( !statement )
  1125. statement = new GenOp( "tex2D(@, @)", lightMap, inTex );
  1126. // Assign to proper render target
  1127. MultiLine *meta = new MultiLine;
  1128. if( fd.features[MFT_LightbufferMRT] )
  1129. {
  1130. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1131. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1132. }
  1133. else
  1134. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
  1135. output = meta;
  1136. }
  1137. ShaderFeature::Resources LightmapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1138. {
  1139. Resources res;
  1140. res.numTex = 1;
  1141. res.numTexReg = 1;
  1142. return res;
  1143. }
  1144. void LightmapFeatGLSL::setTexData( Material::StageData &stageDat,
  1145. const MaterialFeatureData &fd,
  1146. RenderPassData &passData,
  1147. U32 &texIndex )
  1148. {
  1149. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  1150. passData.mSamplerNames[ texIndex ] = "lightMap";
  1151. if ( tex )
  1152. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1153. else
  1154. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  1155. }
  1156. U32 LightmapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1157. {
  1158. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1159. }
  1160. //****************************************************************************
  1161. // Tonemap
  1162. //****************************************************************************
  1163. void TonemapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1164. const MaterialFeatureData &fd )
  1165. {
  1166. // Grab the connector
  1167. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1168. // Set up the second set of texCoords
  1169. Var *inTex2 = getVertTexCoord( "texCoord2" );
  1170. if ( inTex2 )
  1171. {
  1172. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  1173. outTex2->setName( "texCoord2" );
  1174. outTex2->setStructName( "OUT" );
  1175. outTex2->setType( "vec2" );
  1176. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  1177. }
  1178. }
  1179. void TonemapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1180. const MaterialFeatureData &fd )
  1181. {
  1182. // Grab connector
  1183. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1184. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  1185. inTex2->setName( "texCoord2" );
  1186. inTex2->setStructName( "IN" );
  1187. inTex2->setType( "vec2" );
  1188. // create texture var
  1189. Var *toneMap = new Var;
  1190. toneMap->setType( "sampler2D" );
  1191. toneMap->setName( "toneMap" );
  1192. toneMap->uniform = true;
  1193. toneMap->sampler = true;
  1194. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1195. MultiLine * meta = new MultiLine;
  1196. // First get the toneMap color
  1197. Var *toneMapColor = new Var;
  1198. toneMapColor->setType( "vec4" );
  1199. toneMapColor->setName( "toneMapColor" );
  1200. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  1201. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
  1202. // We do a different calculation if there is a diffuse map or not
  1203. Material::BlendOp blendOp = Material::Mul;
  1204. if ( fd.features[MFT_DiffuseMap] )
  1205. {
  1206. // Reverse the tonemap
  1207. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
  1208. // Re-tonemap with the current color factored in
  1209. blendOp = Material::ToneMap;
  1210. }
  1211. // Find out if RTLighting should be added
  1212. bool bPreProcessedLighting = false;
  1213. AdvancedLightBinManager *lightBin;
  1214. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1215. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1216. // Add in the realtime lighting contribution
  1217. if ( fd.features[MFT_RTLighting] )
  1218. {
  1219. // Right now, only Advanced Lighting is supported
  1220. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1221. if(inColor != NULL)
  1222. {
  1223. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1224. // the dynamic light buffer, and it already has the tonemap included
  1225. if(bPreProcessedLighting)
  1226. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  1227. else
  1228. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  1229. }
  1230. }
  1231. // Assign to proper render target
  1232. if( fd.features[MFT_LightbufferMRT] )
  1233. {
  1234. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1235. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1236. }
  1237. else
  1238. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  1239. output = meta;
  1240. }
  1241. ShaderFeature::Resources TonemapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1242. {
  1243. Resources res;
  1244. res.numTex = 1;
  1245. res.numTexReg = 1;
  1246. return res;
  1247. }
  1248. void TonemapFeatGLSL::setTexData( Material::StageData &stageDat,
  1249. const MaterialFeatureData &fd,
  1250. RenderPassData &passData,
  1251. U32 &texIndex )
  1252. {
  1253. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  1254. if ( tex )
  1255. {
  1256. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  1257. passData.mSamplerNames[ texIndex ] = "toneMap";
  1258. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1259. }
  1260. }
  1261. U32 TonemapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1262. {
  1263. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1264. }
  1265. //****************************************************************************
  1266. // pureLIGHT Lighting
  1267. //****************************************************************************
  1268. void VertLitGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1269. const MaterialFeatureData &fd )
  1270. {
  1271. // If we have a lightMap or toneMap then our lighting will be
  1272. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1273. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1274. {
  1275. output = NULL;
  1276. return;
  1277. }
  1278. // Create vertex color connector if it doesn't exist.
  1279. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1280. if( !outColor )
  1281. {
  1282. // Grab the connector color
  1283. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1284. outColor = connectComp->getElement( RT_COLOR );
  1285. outColor->setName( "vertColor" );
  1286. outColor->setStructName( "OUT" );
  1287. outColor->setType( "vec4" );
  1288. // Search for vert color
  1289. Var *inColor = (Var*) LangElement::find( "diffuse" );
  1290. // If there isn't a vertex color then we can't do anything
  1291. if( !inColor )
  1292. {
  1293. output = NULL;
  1294. return;
  1295. }
  1296. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1297. }
  1298. else
  1299. output = NULL; // Nothing we need to do.
  1300. }
  1301. void VertLitGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1302. const MaterialFeatureData &fd )
  1303. {
  1304. // If we have a lightMap or toneMap then our lighting will be
  1305. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1306. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1307. {
  1308. output = NULL;
  1309. return;
  1310. }
  1311. // Grab the connector color register
  1312. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1313. if( !vertColor )
  1314. {
  1315. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1316. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1317. vertColor = connectComp->getElement( RT_COLOR );
  1318. vertColor->setName( "vertColor" );
  1319. vertColor->setStructName( "IN" );
  1320. vertColor->setType( "vec4" );
  1321. }
  1322. MultiLine * meta = new MultiLine;
  1323. // Defaults (no diffuse map)
  1324. Material::BlendOp blendOp = Material::Mul;
  1325. LangElement *outColor = vertColor;
  1326. // We do a different calculation if there is a diffuse map or not
  1327. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1328. {
  1329. Var * finalVertColor = new Var;
  1330. finalVertColor->setName( "finalVertColor" );
  1331. finalVertColor->setType( "vec4" );
  1332. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1333. // Reverse the tonemap
  1334. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
  1335. // Set the blend op to tonemap
  1336. blendOp = Material::ToneMap;
  1337. outColor = finalVertColor;
  1338. }
  1339. // Add in the realtime lighting contribution, if applicable
  1340. if ( fd.features[MFT_RTLighting] )
  1341. {
  1342. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1343. if(rtLightingColor != NULL)
  1344. {
  1345. bool bPreProcessedLighting = false;
  1346. AdvancedLightBinManager *lightBin;
  1347. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1348. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1349. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1350. // the dynamic light buffer, and it already has the baked-vertex-color
  1351. // included in it
  1352. if(bPreProcessedLighting)
  1353. outColor = new GenOp( "vec4(@.rgb, 1.0)", rtLightingColor );
  1354. else
  1355. outColor = new GenOp( "vec4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
  1356. }
  1357. }
  1358. // Output the color
  1359. if ( fd.features[MFT_LightbufferMRT] )
  1360. {
  1361. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1362. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1363. }
  1364. else
  1365. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1366. output = meta;
  1367. }
  1368. U32 VertLitGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1369. {
  1370. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1371. }
  1372. //****************************************************************************
  1373. // Detail map
  1374. //****************************************************************************
  1375. void DetailFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1376. const MaterialFeatureData &fd )
  1377. {
  1378. MultiLine *meta = new MultiLine;
  1379. addOutDetailTexCoord( componentList,
  1380. meta,
  1381. fd.features[MFT_TexAnim], fd.features[MFT_Foliage]);
  1382. output = meta;
  1383. }
  1384. void DetailFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1385. const MaterialFeatureData &fd )
  1386. {
  1387. // Get the detail texture coord.
  1388. Var *inTex = getInTexCoord( "detCoord", "vec2", componentList );
  1389. // create texture var
  1390. Var *detailMap = new Var;
  1391. detailMap->setType( "sampler2D" );
  1392. detailMap->setName( "detailMap" );
  1393. detailMap->uniform = true;
  1394. detailMap->sampler = true;
  1395. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1396. // We're doing the standard greyscale detail map
  1397. // technique which can darken and lighten the
  1398. // diffuse texture.
  1399. // TODO: We could add a feature to toggle between this
  1400. // and a simple multiplication with the detail map.
  1401. LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
  1402. if ( fd.features[MFT_isDeferred])
  1403. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add, NULL, ShaderFeature::RenderTarget1 ) );
  1404. else
  1405. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1406. }
  1407. ShaderFeature::Resources DetailFeatGLSL::getResources( const MaterialFeatureData &fd )
  1408. {
  1409. Resources res;
  1410. res.numTex = 1;
  1411. res.numTexReg = 1;
  1412. return res;
  1413. }
  1414. void DetailFeatGLSL::setTexData( Material::StageData &stageDat,
  1415. const MaterialFeatureData &fd,
  1416. RenderPassData &passData,
  1417. U32 &texIndex )
  1418. {
  1419. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1420. if ( tex )
  1421. {
  1422. passData.mSamplerNames[texIndex] = "detailMap";
  1423. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1424. }
  1425. }
  1426. //****************************************************************************
  1427. // Vertex position
  1428. //****************************************************************************
  1429. void VertPositionGLSL::determineFeature( Material *material,
  1430. const GFXVertexFormat *vertexFormat,
  1431. U32 stageNum,
  1432. const FeatureType &type,
  1433. const FeatureSet &features,
  1434. MaterialFeatureData *outFeatureData )
  1435. {
  1436. // This feature is always on!
  1437. outFeatureData->features.addFeature( type );
  1438. }
  1439. void VertPositionGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1440. const MaterialFeatureData &fd )
  1441. {
  1442. // First check for an input position from a previous feature
  1443. // then look for the default vertex position.
  1444. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1445. if ( !inPosition )
  1446. inPosition = (Var*)LangElement::find( "position" );
  1447. // grab connector position
  1448. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1449. Var *outPosition = connectComp->getElement( RT_POSITION );
  1450. outPosition->setName( "gl_Position" );
  1451. MultiLine *meta = new MultiLine;
  1452. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1453. meta->addStatement( new GenOp( " @ = tMul(@, vec4(@.xyz,1));\r\n",
  1454. outPosition, modelview, inPosition ) );
  1455. if (fd.materialFeatures[MFT_isBackground])
  1456. {
  1457. meta->addStatement(new GenOp(" @ = @.xyww;\r\n", outPosition, outPosition));
  1458. }
  1459. output = meta;
  1460. }
  1461. //****************************************************************************
  1462. // Reflect Cubemap
  1463. //****************************************************************************
  1464. void ReflectCubeFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1465. const MaterialFeatureData &fd )
  1466. {
  1467. // search for vert normal
  1468. Var *inNormal = (Var*) LangElement::find( "normal" );
  1469. if ( !inNormal )
  1470. return;
  1471. MultiLine * meta = new MultiLine;
  1472. // If a base or bump tex is present in the material, but not in the
  1473. // current pass - we need to add one to the current pass to use
  1474. // its alpha channel as a gloss map. Here we just need the tex coords.
  1475. if( !fd.features[MFT_DiffuseMap] &&
  1476. !fd.features[MFT_NormalMap] )
  1477. {
  1478. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1479. fd.materialFeatures[MFT_NormalMap] )
  1480. {
  1481. // find incoming texture var
  1482. Var *inTex = getVertTexCoord( "texCoord" );
  1483. // grab connector texcoord register
  1484. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1485. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1486. outTex->setName( "texCoord" );
  1487. outTex->setStructName( "OUT" );
  1488. outTex->setType( "vec2" );
  1489. // setup language elements to output incoming tex coords to output
  1490. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1491. }
  1492. }
  1493. // create cubeTrans
  1494. bool useInstancing = fd.features[MFT_UseInstancing];
  1495. Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
  1496. // cube vert position
  1497. Var * cubeVertPos = new Var;
  1498. cubeVertPos->setName( "cubeVertPos" );
  1499. cubeVertPos->setType( "vec3" );
  1500. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1501. meta->addStatement( new GenOp( " @ = tMul( @, float4(@,1)).xyz;\r\n",
  1502. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1503. // cube normal
  1504. Var * cubeNormal = new Var;
  1505. cubeNormal->setName( "cubeNormal" );
  1506. cubeNormal->setType( "vec3" );
  1507. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1508. meta->addStatement( new GenOp( " @ = ( tMul( (@), vec4(@, 0) ) ).xyz;\r\n",
  1509. cubeNormDecl, cubeTrans, inNormal ) );
  1510. meta->addStatement( new GenOp( " @ = bool(length(@)) ? normalize(@) : @;\r\n",
  1511. cubeNormal, cubeNormal, cubeNormal, cubeNormal ) );
  1512. // grab the eye position
  1513. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1514. if ( !eyePos )
  1515. {
  1516. eyePos = new Var( "eyePosWorld", "vec3" );
  1517. eyePos->uniform = true;
  1518. eyePos->constSortPos = cspPass;
  1519. }
  1520. // eye to vert
  1521. Var * eyeToVert = new Var;
  1522. eyeToVert->setName( "eyeToVert" );
  1523. eyeToVert->setType( "vec3" );
  1524. LangElement *e2vDecl = new DecOp( eyeToVert );
  1525. meta->addStatement( new GenOp( " @ = @ - @;\r\n",
  1526. e2vDecl, cubeVertPos, eyePos ) );
  1527. // grab connector texcoord register
  1528. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1529. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1530. reflectVec->setName( "reflectVec" );
  1531. reflectVec->setStructName( "OUT" );
  1532. reflectVec->setType( "vec3" );
  1533. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1534. output = meta;
  1535. }
  1536. void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1537. const MaterialFeatureData &fd )
  1538. {
  1539. MultiLine * meta = new MultiLine;
  1540. Var *glossColor = NULL;
  1541. // If a base or bump tex is present in the material, but not in the
  1542. // current pass - we need to add one to the current pass to use
  1543. // its alpha channel as a gloss map.
  1544. if( !fd.features[MFT_DiffuseMap] &&
  1545. !fd.features[MFT_NormalMap] )
  1546. {
  1547. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1548. fd.materialFeatures[MFT_NormalMap] )
  1549. {
  1550. // grab connector texcoord register
  1551. Var *inTex = getInTexCoord( "texCoord", "vec2", componentList );
  1552. // create texture var
  1553. Var *newMap = new Var;
  1554. newMap->setType( "sampler2D" );
  1555. newMap->setName( "glossMap" );
  1556. newMap->uniform = true;
  1557. newMap->sampler = true;
  1558. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1559. // create sample color
  1560. Var *color = new Var;
  1561. color->setType( "vec4" );
  1562. color->setName( "diffuseColor" );
  1563. LangElement *colorDecl = new DecOp( color );
  1564. glossColor = color;
  1565. meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
  1566. }
  1567. }
  1568. if (!glossColor)
  1569. {
  1570. if (fd.features[MFT_isDeferred])
  1571. glossColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1572. if (!glossColor)
  1573. glossColor = (Var*)LangElement::find("diffuseColor");
  1574. if (!glossColor)
  1575. glossColor = (Var*)LangElement::find("bumpNormal");
  1576. }
  1577. // grab connector texcoord register
  1578. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1579. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1580. reflectVec->setName( "reflectVec" );
  1581. reflectVec->setStructName( "IN" );
  1582. reflectVec->setType( "vec3" );
  1583. // create cubemap var
  1584. Var *cubeMap = new Var;
  1585. cubeMap->setType( "samplerCube" );
  1586. cubeMap->setName( "cubeMap" );
  1587. cubeMap->uniform = true;
  1588. cubeMap->sampler = true;
  1589. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1590. Var *cubeMips = new Var;
  1591. cubeMips->setType("float");
  1592. cubeMips->setName("cubeMips");
  1593. cubeMips->uniform = true;
  1594. cubeMips->constSortPos = cspPotentialPrimitive;
  1595. // TODO: Restore the lighting attenuation here!
  1596. Var *attn = NULL;
  1597. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1598. //attn = (Var*)LangElement::find("attn");
  1599. //else
  1600. if ( fd.materialFeatures[MFT_RTLighting] )
  1601. attn =(Var*)LangElement::find("d_NL_Att");
  1602. LangElement *texCube = NULL;
  1603. Var* matinfo = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  1604. Var *roughness = (Var*)LangElement::find("roughness");
  1605. if (roughness) //try to grab roughness directly
  1606. texCube = new GenOp("textureLod( @, @, min((1.0 - @)*@ + 1.0, @))", cubeMap, reflectVec, roughness, cubeMips, cubeMips);
  1607. else if (glossColor) //failing that, try and find color data
  1608. texCube = new GenOp("textureLod( @, @, min((1.0 - @.b)*@ + 1.0, @))", cubeMap, reflectVec, glossColor, cubeMips, cubeMips);
  1609. else //failing *that*, just draw the cubemap
  1610. texCube = new GenOp("texture( @, @)", cubeMap, reflectVec);
  1611. LangElement *lerpVal = NULL;
  1612. Material::BlendOp blendOp = Material::LerpAlpha;
  1613. // Note that the lerpVal needs to be a float4 so that
  1614. // it will work with the LerpAlpha blend.
  1615. if (matinfo)
  1616. {
  1617. if (attn)
  1618. lerpVal = new GenOp("@ * saturate( @ )", matinfo, attn);
  1619. else
  1620. lerpVal = new GenOp("@", matinfo);
  1621. }
  1622. else if ( glossColor )
  1623. {
  1624. if ( attn )
  1625. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1626. else
  1627. lerpVal = glossColor;
  1628. }
  1629. else
  1630. {
  1631. if ( attn )
  1632. lerpVal = new GenOp( "vec4( saturate( @ ) ).xxxx", attn );
  1633. else
  1634. blendOp = Material::Mul;
  1635. }
  1636. Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  1637. if (fd.features[MFT_isDeferred])
  1638. {
  1639. //metalness: black(0) = color, white(1) = reflection
  1640. if (fd.features[MFT_ToneMap])
  1641. meta->addStatement(new GenOp(" @ *= @;\r\n", targ, texCube));
  1642. else
  1643. meta->addStatement(new GenOp(" @ = @;\r\n", targ, texCube));
  1644. }
  1645. else
  1646. {
  1647. meta->addStatement(new GenOp(" //forward lit cubemapping\r\n"));
  1648. targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  1649. Var *metalness = (Var*)LangElement::find("metalness");
  1650. if (metalness)
  1651. {
  1652. Var *dColor = new Var("dColor", "vec3");
  1653. Var *envColor = new Var("envColor", "vec3");
  1654. meta->addStatement(new GenOp(" @ = @.rgb - (@.rgb * @);\r\n", new DecOp(dColor), targ, targ, metalness));
  1655. meta->addStatement(new GenOp(" @ = @.rgb*(@).rgb;\r\n", new DecOp(envColor), targ, texCube));
  1656. meta->addStatement(new GenOp(" @.rgb = @+@;\r\n", targ, dColor, envColor));
  1657. }
  1658. else if (lerpVal)
  1659. meta->addStatement(new GenOp(" @ *= vec4(@.rgb*@.a, @.a);\r\n", targ, texCube, lerpVal, targ));
  1660. else
  1661. meta->addStatement(new GenOp(" @.rgb *= @.rgb;\r\n", targ, texCube));
  1662. }
  1663. output = meta;
  1664. }
  1665. ShaderFeature::Resources ReflectCubeFeatGLSL::getResources( const MaterialFeatureData &fd )
  1666. {
  1667. Resources res;
  1668. if( fd.features[MFT_DiffuseMap] ||
  1669. fd.features[MFT_NormalMap] )
  1670. {
  1671. res.numTex = 1;
  1672. res.numTexReg = 1;
  1673. }
  1674. else
  1675. {
  1676. res.numTex = 2;
  1677. res.numTexReg = 2;
  1678. }
  1679. return res;
  1680. }
  1681. void ReflectCubeFeatGLSL::setTexData( Material::StageData &stageDat,
  1682. const MaterialFeatureData &stageFeatures,
  1683. RenderPassData &passData,
  1684. U32 &texIndex )
  1685. {
  1686. // set up a gloss map if one is not present in the current pass
  1687. // but is present in the current material stage
  1688. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1689. !passData.mFeatureData.features[MFT_NormalMap] )
  1690. {
  1691. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1692. if ( tex && stageFeatures.features[MFT_DiffuseMap] )
  1693. {
  1694. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  1695. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1696. }
  1697. else
  1698. {
  1699. tex = stageDat.getTex( MFT_NormalMap );
  1700. if ( tex && stageFeatures.features[ MFT_NormalMap ] )
  1701. {
  1702. passData.mSamplerNames[ texIndex ] = "bumpMap";
  1703. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1704. }
  1705. }
  1706. }
  1707. if( stageDat.getCubemap() )
  1708. {
  1709. passData.mCubeMap = stageDat.getCubemap();
  1710. passData.mSamplerNames[texIndex] = "cubeMap";
  1711. passData.mTexType[texIndex++] = Material::Cube;
  1712. }
  1713. else
  1714. {
  1715. if( stageFeatures.features[MFT_CubeMap] )
  1716. {
  1717. // assuming here that it is a scenegraph cubemap
  1718. passData.mSamplerNames[texIndex] = "cubeMap";
  1719. passData.mTexType[texIndex++] = Material::SGCube;
  1720. }
  1721. }
  1722. }
  1723. //****************************************************************************
  1724. // RTLighting
  1725. //****************************************************************************
  1726. RTLightingFeatGLSL::RTLightingFeatGLSL()
  1727. : mDep(ShaderGen::smCommonShaderPath + String("/gl/lighting.glsl" ))
  1728. {
  1729. addDependency( &mDep );
  1730. }
  1731. void RTLightingFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1732. const MaterialFeatureData &fd )
  1733. {
  1734. MultiLine *meta = new MultiLine;
  1735. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1736. // Special case for lighting imposters. We dont have a vert normal and may not
  1737. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1738. if ( fd.features[MFT_ImposterVert] )
  1739. {
  1740. if ( !fd.features[MFT_NormalMap] )
  1741. {
  1742. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1743. if ( !eyePos )
  1744. {
  1745. eyePos = new Var( "eyePosWorld", "vec3" );
  1746. eyePos->uniform = true;
  1747. eyePos->constSortPos = cspPass;
  1748. }
  1749. //Temporarily disabled while we figure out how to better handle normals without a normal map
  1750. /*Var *inPosition = (Var*)LangElement::find( "position" );
  1751. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1752. outNormal->setName( "wsNormal" );
  1753. outNormal->setStructName( "OUT" );
  1754. outNormal->setType( "vec3" );
  1755. // Transform the normal to world space.
  1756. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );*/
  1757. }
  1758. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1759. output = meta;
  1760. return;
  1761. }
  1762. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1763. getOutWorldToTangent(componentList, meta, fd);
  1764. output = meta;
  1765. // Find the incoming vertex normal.
  1766. Var *inNormal = (Var*)LangElement::find( "normal" );
  1767. if ( !inNormal ||
  1768. fd.features[MFT_LightMap] ||
  1769. fd.features[MFT_ToneMap] ||
  1770. fd.features[MFT_VertLit] )
  1771. return;
  1772. // If there isn't a normal map then we need to pass
  1773. // the world space normal to the pixel shader ourselves.
  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* worldToTangent = getInWorldToTangent(componentList);
  1802. Var* wsNormal = getInWorldNormal(componentList);
  1803. Var *wsView = getWsView( wsPosition, meta );
  1804. // Look for a light mask generated from a previous
  1805. // feature (this is done for BL terrain lightmaps).
  1806. LangElement *lightMask = LangElement::find( "lightMask" );
  1807. if ( !lightMask )
  1808. lightMask = new GenOp( "vec4( 1, 1, 1, 1 )" );
  1809. // Get all the light constants.
  1810. Var *inLightPos = new Var( "inLightPos", "vec4" );
  1811. inLightPos->uniform = true;
  1812. inLightPos->arraySize = 4;
  1813. inLightPos->constSortPos = cspPotentialPrimitive;
  1814. Var * inLightConfigData = new Var( "inLightConfigData", "vec4" );
  1815. inLightConfigData->uniform = true;
  1816. inLightConfigData->arraySize = 4;
  1817. inLightConfigData->constSortPos = cspPotentialPrimitive;
  1818. Var *inLightColor = new Var( "inLightColor", "vec4" );
  1819. inLightColor->uniform = true;
  1820. inLightColor->arraySize = 4;
  1821. inLightColor->constSortPos = cspPotentialPrimitive;
  1822. Var *inLightSpotDir = new Var( "inLightSpotDir", "vec4" );
  1823. inLightSpotDir->uniform = true;
  1824. inLightSpotDir->arraySize = 4;
  1825. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1826. Var * lightSpotParams = new Var( "inlightSpotParams", "vec2" );
  1827. lightSpotParams->uniform = true;
  1828. lightSpotParams->arraySize = 4;
  1829. lightSpotParams->constSortPos = cspPotentialPrimitive;
  1830. Var* hasVectorLight = new Var("hasVectorLight", "int");
  1831. hasVectorLight->uniform = true;
  1832. hasVectorLight->constSortPos = cspPotentialPrimitive;
  1833. Var* vectorLightDirection = new Var("vectorLightDirection", "vec4");
  1834. vectorLightDirection->uniform = true;
  1835. vectorLightDirection->constSortPos = cspPotentialPrimitive;
  1836. Var* vectorLightColor = new Var("vectorLightColor", "vec4");
  1837. vectorLightColor->uniform = true;
  1838. vectorLightColor->constSortPos = cspPotentialPrimitive;
  1839. Var* vectorLightBrightness = new Var("vectorLightBrightness", "float");
  1840. vectorLightBrightness->uniform = true;
  1841. vectorLightBrightness->constSortPos = cspPotentialPrimitive;
  1842. Var* surface = getSurface(componentList, meta, fd);
  1843. if (!surface)
  1844. {
  1845. Con::errorf("ShaderGen::RTLightingFeatGLSL() - failed to generate surface!");
  1846. return;
  1847. }
  1848. Var *roughness = (Var*)LangElement::find("roughness");
  1849. Var *metalness = (Var*)LangElement::find("metalness");
  1850. Var *curColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  1851. Var *ambient = new Var( "ambient", "vec4" );
  1852. ambient->uniform = true;
  1853. ambient->constSortPos = cspPass;
  1854. Var* lighting = new Var("lighting", "vec4");
  1855. meta->addStatement(new GenOp(" @ = compute4Lights( @, @, @, @,\r\n"
  1856. " @, @, @, @, @, @, @);\r\n",
  1857. new DecOp(lighting), surface, lightMask, inLightPos, inLightConfigData, inLightColor, inLightSpotDir, lightSpotParams,
  1858. hasVectorLight, vectorLightDirection, vectorLightColor, vectorLightBrightness));
  1859. meta->addStatement(new GenOp(" @.rgb += @.rgb;\r\n", curColor, lighting));
  1860. output = meta;
  1861. }
  1862. ShaderFeature::Resources RTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
  1863. {
  1864. Resources res;
  1865. // These features disable realtime lighting.
  1866. if ( !fd.features[MFT_LightMap] &&
  1867. !fd.features[MFT_ToneMap] &&
  1868. !fd.features[MFT_VertLit] )
  1869. {
  1870. // If enabled we pass the position.
  1871. res.numTexReg = 1;
  1872. // If there isn't a bump map then we pass the
  1873. // world space normal as well.
  1874. if ( !fd.features[MFT_NormalMap] )
  1875. res.numTexReg++;
  1876. }
  1877. return res;
  1878. }
  1879. //****************************************************************************
  1880. // Fog
  1881. //****************************************************************************
  1882. FogFeatGLSL::FogFeatGLSL()
  1883. : mFogDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
  1884. {
  1885. addDependency( &mFogDep );
  1886. }
  1887. void FogFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1888. const MaterialFeatureData &fd )
  1889. {
  1890. MultiLine *meta = new MultiLine;
  1891. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1892. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1893. {
  1894. // Grab the eye position.
  1895. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1896. if ( !eyePos )
  1897. {
  1898. eyePos = new Var( "eyePosWorld", "vec3" );
  1899. eyePos->uniform = true;
  1900. eyePos->constSortPos = cspPass;
  1901. }
  1902. Var *fogData = new Var( "fogData", "vec3" );
  1903. fogData->uniform = true;
  1904. fogData->constSortPos = cspPass;
  1905. Var *wsPosition = new Var( "fogPos", "vec3" );
  1906. getWsPosition( componentList,
  1907. fd.features[MFT_UseInstancing],
  1908. meta,
  1909. new DecOp( wsPosition ) );
  1910. // We pass the fog amount to the pixel shader.
  1911. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1912. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1913. fogAmount->setName( "fogAmount" );
  1914. fogAmount->setStructName( "OUT" );
  1915. fogAmount->setType( "float" );
  1916. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1917. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1918. }
  1919. else
  1920. {
  1921. // We fog in world space... make sure the world space
  1922. // position is passed to the pixel shader. This is
  1923. // often already passed for lighting, so it takes up
  1924. // no extra output registers.
  1925. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1926. }
  1927. output = meta;
  1928. }
  1929. void FogFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1930. const MaterialFeatureData &fd )
  1931. {
  1932. MultiLine *meta = new MultiLine;
  1933. Var *fogColor = new Var;
  1934. fogColor->setType( "vec4" );
  1935. fogColor->setName( "fogColor" );
  1936. fogColor->uniform = true;
  1937. fogColor->constSortPos = cspPass;
  1938. // Get the out color.
  1939. Var *color = (Var*) LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  1940. if (fd.features[MFT_isDeferred])
  1941. color = (Var *)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1942. if ( !color )
  1943. {
  1944. color = new Var;
  1945. color->setType( "vec4" );
  1946. color->setName( "col" );
  1947. color->setStructName("OUT");
  1948. }
  1949. Var *fogAmount;
  1950. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1951. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1952. {
  1953. // Per-vertex.... just get the fog amount.
  1954. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1955. fogAmount = connectComp->getElement( RT_TEXCOORD );
  1956. fogAmount->setName( "fogAmount" );
  1957. fogAmount->setStructName( "IN" );
  1958. fogAmount->setType( "float" );
  1959. }
  1960. else
  1961. {
  1962. Var *wsPosition = getInWsPosition( componentList );
  1963. // grab the eye position
  1964. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1965. if ( !eyePos )
  1966. {
  1967. eyePos = new Var( "eyePosWorld", "vec3" );
  1968. eyePos->uniform = true;
  1969. eyePos->constSortPos = cspPass;
  1970. }
  1971. Var *fogData = new Var( "fogData", "vec3" );
  1972. fogData->uniform = true;
  1973. fogData->constSortPos = cspPass;
  1974. /// Get the fog amount.
  1975. fogAmount = new Var( "fogAmount", "float" );
  1976. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1977. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  1978. }
  1979. // Lerp between the fog color and diffuse color.
  1980. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  1981. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  1982. output = meta;
  1983. }
  1984. ShaderFeature::Resources FogFeatGLSL::getResources( const MaterialFeatureData &fd )
  1985. {
  1986. Resources res;
  1987. res.numTexReg = 1;
  1988. return res;
  1989. }
  1990. //****************************************************************************
  1991. // Visibility
  1992. //****************************************************************************
  1993. VisibilityFeatGLSL::VisibilityFeatGLSL()
  1994. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
  1995. {
  1996. addDependency( &mTorqueDep );
  1997. }
  1998. void VisibilityFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1999. const MaterialFeatureData &fd )
  2000. {
  2001. MultiLine *meta = new MultiLine;
  2002. output = meta;
  2003. if ( fd.features[ MFT_UseInstancing ] )
  2004. {
  2005. // We pass the visibility to the pixel shader via
  2006. // another output register.
  2007. //
  2008. // TODO: We should see if we can share this register
  2009. // with some other common instanced data.
  2010. //
  2011. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2012. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  2013. outVisibility->setStructName( "OUT" );
  2014. outVisibility->setName( "visibility" );
  2015. outVisibility->setType( "float" );
  2016. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  2017. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  2018. instVisibility->setStructName( "IN" );
  2019. instVisibility->setName( "inst_visibility" );
  2020. instVisibility->setType( "float" );
  2021. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  2022. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  2023. }
  2024. if ( fd.features[ MFT_IsTranslucent ] )
  2025. return;
  2026. addOutVpos( meta, componentList );
  2027. }
  2028. void VisibilityFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2029. const MaterialFeatureData &fd )
  2030. {
  2031. // Get the visibility constant.
  2032. Var *visibility = NULL;
  2033. if ( fd.features[ MFT_UseInstancing ] )
  2034. visibility = getInTexCoord( "visibility", "float", componentList );
  2035. else
  2036. {
  2037. visibility = (Var*)LangElement::find( "visibility" );
  2038. if ( !visibility )
  2039. {
  2040. visibility = new Var();
  2041. visibility->setType( "float" );
  2042. visibility->setName( "visibility" );
  2043. visibility->uniform = true;
  2044. visibility->constSortPos = cspPotentialPrimitive;
  2045. }
  2046. }
  2047. MultiLine* meta = new MultiLine;
  2048. output = meta;
  2049. // Translucent objects do a simple alpha fade.
  2050. if ( fd.features[ MFT_IsTranslucent ] )
  2051. {
  2052. Var *color = (Var*) LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2053. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  2054. return;
  2055. }
  2056. // Everything else does a fizzle.
  2057. Var *vPos = getInVpos( meta, componentList );
  2058. meta->addStatement( new GenOp( " fizzle( @.xy, @ );\r\n", vPos, visibility ) );
  2059. }
  2060. ShaderFeature::Resources VisibilityFeatGLSL::getResources( const MaterialFeatureData &fd )
  2061. {
  2062. Resources res;
  2063. // TODO: Fix for instancing.
  2064. if ( !fd.features[ MFT_IsTranslucent ] )
  2065. res.numTexReg = 1;
  2066. return res;
  2067. }
  2068. //****************************************************************************
  2069. // AlphaTest
  2070. //****************************************************************************
  2071. void AlphaTestGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2072. const MaterialFeatureData &fd )
  2073. {
  2074. // If we're below SM3 and don't have a depth output
  2075. // feature then don't waste an instruction here.
  2076. if (( GFX->getPixelShaderVersion() < 3.0 &&
  2077. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  2078. !fd.features[ MFT_DepthOut ] ) ||
  2079. fd.features[MFT_IsTranslucent])
  2080. {
  2081. output = NULL;
  2082. return;
  2083. }
  2084. // If we don't have a color var then we cannot do an alpha test.
  2085. Var *color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1) );
  2086. if ( !color )
  2087. color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2088. if ( !color )
  2089. {
  2090. output = NULL;
  2091. return;
  2092. }
  2093. // Now grab the alpha test value.
  2094. Var *alphaTestVal = new Var;
  2095. alphaTestVal->setType( "float" );
  2096. alphaTestVal->setName( "alphaTestValue" );
  2097. alphaTestVal->uniform = true;
  2098. alphaTestVal->constSortPos = cspPotentialPrimitive;
  2099. // Do the clip.
  2100. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  2101. }
  2102. //****************************************************************************
  2103. // GlowMask
  2104. //****************************************************************************
  2105. void GlowMaskGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2106. const MaterialFeatureData &fd )
  2107. {
  2108. output = NULL;
  2109. // Get the output color... and make it black to mask out
  2110. // glow passes rendered before us.
  2111. //
  2112. // The shader compiler will optimize out all the other
  2113. // code above that doesn't contribute to the alpha mask.
  2114. Var *color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2115. if ( color )
  2116. output = new GenOp( " @.rgb = vec3(0);\r\n", color );
  2117. }
  2118. //****************************************************************************
  2119. // RenderTargetZero
  2120. //****************************************************************************
  2121. void RenderTargetZeroGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  2122. {
  2123. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  2124. // This will prevent a divide by zero causing an FP special on float render targets
  2125. output = new GenOp( " @;\r\n", assignColor( new GenOp( "vec4(0.00001)" ), Material::None, NULL, mOutputTargetMask ) );
  2126. }
  2127. //****************************************************************************
  2128. // HDR Output
  2129. //****************************************************************************
  2130. HDROutGLSL::HDROutGLSL()
  2131. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
  2132. {
  2133. addDependency( &mTorqueDep );
  2134. }
  2135. void HDROutGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2136. const MaterialFeatureData &fd )
  2137. {
  2138. // Let the helper function do the work.
  2139. Var *color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2140. if ( color )
  2141. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2142. }
  2143. //****************************************************************************
  2144. // FoliageFeatureGLSL
  2145. //****************************************************************************
  2146. #include "T3D/fx/groundCover.h"
  2147. FoliageFeatureGLSL::FoliageFeatureGLSL()
  2148. : mDep(ShaderGen::smCommonShaderPath + String("/gl/foliage.glsl" ))
  2149. {
  2150. addDependency( &mDep );
  2151. }
  2152. void FoliageFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2153. const MaterialFeatureData &fd )
  2154. {
  2155. // Get the input variables we need.
  2156. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2157. if ( !inPosition )
  2158. inPosition = (Var*)LangElement::find( "position" );
  2159. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2160. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2161. MultiLine *meta = new MultiLine;
  2162. // Declare the normal and tangent variables since they do not exist
  2163. // in this vert type, but we do need to set them up for others.
  2164. Var *normal = (Var*)LangElement::find( "normal" );
  2165. AssertFatal( normal, "FoliageFeatureGLSL requires vert normal!" );
  2166. Var *tangent = new Var;
  2167. tangent->setType( "vec3" );
  2168. tangent->setName( "T" );
  2169. LangElement *tangentDec = new DecOp( tangent );
  2170. meta->addStatement( new GenOp( " @ = vec3(1.0,0,0);\n", tangentDec ) );
  2171. // We add a float foliageFade to the OUT structure.
  2172. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2173. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2174. fade->setName( "foliageFade" );
  2175. fade->setStructName( "OUT" );
  2176. fade->setType( "float" );
  2177. // grab the eye position
  2178. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2179. if ( !eyePos )
  2180. {
  2181. eyePos = new Var( "eyePosWorld", "vec3" );
  2182. eyePos->uniform = true;
  2183. eyePos->constSortPos = cspPass;
  2184. }
  2185. // All actual work is offloaded to this method.
  2186. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2187. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2188. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2189. output = meta;
  2190. }
  2191. void FoliageFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2192. const MaterialFeatureData &fd )
  2193. {
  2194. // Find / create IN.foliageFade
  2195. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2196. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2197. fade->setName( "foliageFade" );
  2198. fade->setStructName( "IN" );
  2199. fade->setType( "float" );
  2200. // Find / create visibility
  2201. Var *visibility = (Var*) LangElement::find( "visibility" );
  2202. if ( !visibility )
  2203. {
  2204. visibility = new Var();
  2205. visibility->setType( "float" );
  2206. visibility->setName( "visibility" );
  2207. visibility->uniform = true;
  2208. visibility->constSortPos = cspPotentialPrimitive;
  2209. }
  2210. MultiLine *meta = new MultiLine;
  2211. // Multiply foliageFade into visibility.
  2212. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2213. output = meta;
  2214. }
  2215. void FoliageFeatureGLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2216. {
  2217. // This isn't really necessary since the outFeatureData will be filtered after
  2218. // this call.
  2219. if ( features.hasFeature( MFT_Foliage ) )
  2220. outFeatureData->features.addFeature( type );
  2221. }
  2222. ShaderFeatureConstHandles* FoliageFeatureGLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2223. {
  2224. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2225. AssertFatal( gcover != NULL, "FoliageFeatureGLSL::createConstHandles - userObject was not valid!" );
  2226. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2227. handles->mGroundCover = gcover;
  2228. handles->init( shader );
  2229. return handles;
  2230. }
  2231. void ParticleNormalFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2232. {
  2233. MultiLine *meta = new MultiLine;
  2234. output = meta;
  2235. // Calculate normal and tangent values since we want to keep particle verts
  2236. // as light-weight as possible
  2237. Var *normal = (Var*) LangElement::find("normal");
  2238. if(normal == NULL)
  2239. {
  2240. normal = new Var;
  2241. normal->setType( "vec3" );
  2242. normal->setName( "normal" );
  2243. // These values are not accidental. It is slightly adjusted from facing straight into the
  2244. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2245. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2246. // [11/23/2009 Pat]
  2247. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2248. }
  2249. Var *T = (Var*) LangElement::find( "T" );
  2250. if(T == NULL)
  2251. {
  2252. T = new Var;
  2253. T->setType( "vec3" );
  2254. T->setName( "T" );
  2255. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2256. }
  2257. }
  2258. //****************************************************************************
  2259. // ImposterVertFeatureGLSL
  2260. //****************************************************************************
  2261. ImposterVertFeatureGLSL::ImposterVertFeatureGLSL()
  2262. : mDep(ShaderGen::smCommonShaderPath + String("/gl/imposter.glsl" ))
  2263. {
  2264. addDependency( &mDep );
  2265. }
  2266. void ImposterVertFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2267. const MaterialFeatureData &fd )
  2268. {
  2269. MultiLine *meta = new MultiLine;
  2270. output = meta;
  2271. // Get the input vertex variables.
  2272. Var *inPosition = (Var*)LangElement::find( "position" );
  2273. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2274. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2275. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2276. // Get the input shader constants.
  2277. Var *imposterLimits = new Var;
  2278. imposterLimits->setType( "vec4" );
  2279. imposterLimits->setName( "imposterLimits" );
  2280. imposterLimits->uniform = true;
  2281. imposterLimits->constSortPos = cspPotentialPrimitive;
  2282. Var *imposterUVs = new Var;
  2283. imposterUVs->setType( "vec4" );
  2284. imposterUVs->setName( "imposterUVs" );
  2285. imposterUVs->arraySize = 64; // See imposter.glsl
  2286. imposterUVs->uniform = true;
  2287. imposterUVs->constSortPos = cspPotentialPrimitive;
  2288. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2289. if ( !eyePos )
  2290. {
  2291. eyePos = new Var( "eyePosWorld", "vec3" );
  2292. eyePos->uniform = true;
  2293. eyePos->constSortPos = cspPass;
  2294. }
  2295. // Declare the outputs from this feature.
  2296. Var *outInPosition = new Var;
  2297. outInPosition->setType( "vec3" );
  2298. outInPosition->setName( "inPosition" );
  2299. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2300. Var *outTexCoord = new Var;
  2301. outTexCoord->setType( "vec2" );
  2302. outTexCoord->setName( "texCoord" );
  2303. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2304. Var *outWorldToTangent = new Var;
  2305. outWorldToTangent->setType( "float3x3" );
  2306. outWorldToTangent->setName( "worldToTangent" );
  2307. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2308. // Add imposterFade to the OUT structure.
  2309. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2310. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2311. outFade->setName( "imposterFade" );
  2312. outFade->setStructName( "OUT" );
  2313. outFade->setType( "float" );
  2314. // Assign OUT.imposterFade
  2315. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2316. // All actual work is done in this method.
  2317. meta->addStatement( new GenOp( " imposter_v( @.xyz, int(@.w), @.x * length(@), normalize(@), normalize(@), int(@.y), int(@.x), @.z, bool(@.w), @, @, @, @, @ );\r\n",
  2318. inPosition,
  2319. inPosition,
  2320. inMiscParams,
  2321. inRightVec,
  2322. inUpVec,
  2323. inRightVec,
  2324. imposterLimits,
  2325. imposterLimits,
  2326. imposterLimits,
  2327. imposterLimits,
  2328. eyePos,
  2329. imposterUVs,
  2330. outInPosition,
  2331. outTexCoord,
  2332. outWorldToTangent ) );
  2333. // Copy the position to wsPosition for use in shaders
  2334. // down stream instead of looking for objTrans.
  2335. Var *wsPosition = new Var;
  2336. wsPosition->setType( "vec3" );
  2337. wsPosition->setName( "wsPosition" );
  2338. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2339. // If we new viewToTangent... its the same as the
  2340. // world to tangent for an imposter.
  2341. Var *viewToTangent = new Var;
  2342. viewToTangent->setType( "float3x3" );
  2343. viewToTangent->setName( "viewToTangent" );
  2344. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2345. }
  2346. void ImposterVertFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2347. const MaterialFeatureData &fd )
  2348. {
  2349. // Find / create IN.imposterFade
  2350. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2351. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2352. fade->setName( "imposterFade" );
  2353. fade->setStructName( "IN" );
  2354. fade->setType( "float" );
  2355. // Find / create visibility
  2356. Var *visibility = (Var*) LangElement::find( "visibility" );
  2357. if ( !visibility )
  2358. {
  2359. visibility = new Var();
  2360. visibility->setType( "float" );
  2361. visibility->setName( "visibility" );
  2362. visibility->uniform = true;
  2363. visibility->constSortPos = cspPotentialPrimitive;
  2364. }
  2365. MultiLine *meta = new MultiLine;
  2366. // Multiply foliageFade into visibility.
  2367. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2368. output = meta;
  2369. }
  2370. void ImposterVertFeatureGLSL::determineFeature( Material *material,
  2371. const GFXVertexFormat *vertexFormat,
  2372. U32 stageNum,
  2373. const FeatureType &type,
  2374. const FeatureSet &features,
  2375. MaterialFeatureData *outFeatureData )
  2376. {
  2377. if ( features.hasFeature( MFT_ImposterVert ) )
  2378. outFeatureData->features.addFeature( MFT_ImposterVert );
  2379. }
  2380. //****************************************************************************
  2381. // HardwareSkinningFeatureGLSL
  2382. //****************************************************************************
  2383. void HardwareSkinningFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList,
  2384. const MaterialFeatureData &fd)
  2385. {
  2386. MultiLine *meta = new MultiLine;
  2387. Var *inPosition = (Var*)LangElement::find("inPosition");
  2388. Var *inNormal = (Var*)LangElement::find("inNormal");
  2389. if (!inPosition)
  2390. inPosition = (Var*)LangElement::find("position");
  2391. if (!inNormal)
  2392. inNormal = (Var*)LangElement::find("normal");
  2393. Var* posePos = new Var("posePos", "vec3");
  2394. Var* poseNormal = new Var("poseNormal", "vec3");
  2395. Var* poseMat = new Var("poseMat", "mat4x3");
  2396. Var* poseRotMat = new Var("poseRotMat", "mat3x3");
  2397. Var* nodeTransforms = (Var*)LangElement::find("nodeTransforms");
  2398. if (!nodeTransforms)
  2399. {
  2400. nodeTransforms = new Var("nodeTransforms", "mat4x3");
  2401. nodeTransforms->uniform = true;
  2402. nodeTransforms->arraySize = TSShape::smMaxSkinBones;
  2403. nodeTransforms->constSortPos = cspPrimitive;
  2404. }
  2405. U32 numIndices = mVertexFormat->getNumBlendIndices();
  2406. meta->addStatement(new GenOp(" @ = vec3(0.0);\r\n", new DecOp(posePos)));
  2407. meta->addStatement(new GenOp(" @ = vec3(0.0);\r\n", new DecOp(poseNormal)));
  2408. meta->addStatement(new GenOp(" @;\r\n", new DecOp(poseMat)));
  2409. meta->addStatement(new GenOp(" @;\r\n int i;\r\n", new DecOp(poseRotMat)));
  2410. for (U32 i = 0; i<numIndices; i++)
  2411. {
  2412. // NOTE: To keep things simple, we assume all 4 bone indices are used in each element chunk.
  2413. LangElement* inIndices = (Var*)LangElement::find(String::ToString("vBlendIndex%d", i));
  2414. LangElement* inWeights = (Var*)LangElement::find(String::ToString("vBlendWeight%d", i));
  2415. AssertFatal(inIndices && inWeights, "Something went wrong here");
  2416. AssertFatal(poseMat && nodeTransforms && posePos && inPosition && inWeights && poseNormal && inNormal && poseRotMat, "Something went REALLY wrong here");
  2417. meta->addStatement(new GenOp(" for (i=0; i<4; i++) {\r\n"));
  2418. meta->addStatement(new GenOp(" int poseIdx = int(@[i]);\r\n", inIndices));
  2419. meta->addStatement(new GenOp(" float poseWeight = @[i];\r\n", inWeights));
  2420. meta->addStatement(new GenOp(" @ = @[poseIdx];\r\n", poseMat, nodeTransforms));
  2421. meta->addStatement(new GenOp(" @ = mat3x3(@);\r\n", poseRotMat, poseMat));
  2422. meta->addStatement(new GenOp(" @ += (@ * vec4(@, 1)).xyz * poseWeight;\r\n", posePos, poseMat, inPosition));
  2423. meta->addStatement(new GenOp(" @ += ((@ * @) * poseWeight);\r\n", poseNormal, poseRotMat, inNormal));
  2424. meta->addStatement(new GenOp(" }\r\n"));
  2425. }
  2426. // Assign new position and normal
  2427. meta->addStatement(new GenOp(" @ = @;\r\n", inPosition, posePos));
  2428. meta->addStatement(new GenOp(" @ = normalize(@);\r\n", inNormal, poseNormal));
  2429. output = meta;
  2430. }
  2431. //****************************************************************************
  2432. // ReflectionProbeFeatGLSL
  2433. //****************************************************************************
  2434. ReflectionProbeFeatGLSL::ReflectionProbeFeatGLSL()
  2435. : mDep(ShaderGen::smCommonShaderPath + String("/gl/lighting.glsl"))
  2436. {
  2437. addDependency(&mDep);
  2438. }
  2439. void ReflectionProbeFeatGLSL::processVert(Vector<ShaderComponent*>& componentList,
  2440. const MaterialFeatureData& fd)
  2441. {
  2442. //MultiLine* meta = new MultiLine;
  2443. //output = meta;
  2444. // Also output the worldToTanget transform which
  2445. // we use to create the world space normal.
  2446. //getOutWorldToTangent(componentList, meta, fd);
  2447. }
  2448. void ReflectionProbeFeatGLSL::processPix(Vector<ShaderComponent*>& componentList,
  2449. const MaterialFeatureData& fd)
  2450. {
  2451. // Skip out on realtime lighting if we don't have a normal
  2452. // or we're doing some sort of baked lighting.
  2453. //
  2454. // TODO: We can totally detect for this in the material
  2455. // feature setup... we should move it out of here!
  2456. //
  2457. if (fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit])
  2458. return;
  2459. ShaderConnector * connectComp = dynamic_cast<ShaderConnector*>(componentList[C_CONNECTOR]);
  2460. MultiLine * meta = new MultiLine;
  2461. // Now the wsPosition and wsView.
  2462. Var *wsPosition = getInWsPosition(componentList);
  2463. Var *worldToTangent = getInWorldToTangent(componentList);
  2464. Var *wsNormal = getInWorldNormal(componentList);
  2465. Var *wsView = getWsView(wsPosition, meta);
  2466. //Reflection Probe WIP
  2467. U32 MAX_FORWARD_PROBES = 4;
  2468. Var * numProbes = new Var("inNumProbes", "int");
  2469. numProbes->uniform = true;
  2470. numProbes->constSortPos = cspPotentialPrimitive;
  2471. Var * cubeMips = new Var("cubeMips", "float");
  2472. cubeMips->uniform = true;
  2473. cubeMips->constSortPos = cspPotentialPrimitive;
  2474. Var * skylightCubemapIdx = new Var("inSkylightCubemapIdx", "float");
  2475. skylightCubemapIdx->uniform = true;
  2476. skylightCubemapIdx->constSortPos = cspPotentialPrimitive;
  2477. Var * inProbePosArray = new Var("inProbePosArray", "vec4");
  2478. inProbePosArray->arraySize = MAX_FORWARD_PROBES;
  2479. inProbePosArray->uniform = true;
  2480. inProbePosArray->constSortPos = cspPotentialPrimitive;
  2481. Var * inRefPosArray = new Var("inRefPosArray", "vec4");
  2482. inRefPosArray->arraySize = MAX_FORWARD_PROBES;
  2483. inRefPosArray->uniform = true;
  2484. inRefPosArray->constSortPos = cspPotentialPrimitive;
  2485. Var * refScaleArray = new Var("inRefScaleArray", "vec4");
  2486. refScaleArray->arraySize = MAX_FORWARD_PROBES;
  2487. refScaleArray->uniform = true;
  2488. refScaleArray->constSortPos = cspPotentialPrimitive;
  2489. Var * probeConfigData = new Var("inProbeConfigDataArray", "vec4");
  2490. probeConfigData->arraySize = MAX_FORWARD_PROBES;
  2491. probeConfigData->uniform = true;
  2492. probeConfigData->constSortPos = cspPotentialPrimitive;
  2493. Var * worldToObjArray = new Var("inWorldToObjArray", "mat4");
  2494. worldToObjArray->arraySize = MAX_FORWARD_PROBES;
  2495. worldToObjArray->uniform = true;
  2496. worldToObjArray->constSortPos = cspPotentialPrimitive;
  2497. // create texture var
  2498. Var* BRDFTexture = new Var;
  2499. BRDFTexture->setType("sampler2D");
  2500. BRDFTexture->setName("BRDFTexture");
  2501. BRDFTexture->uniform = true;
  2502. BRDFTexture->sampler = true;
  2503. BRDFTexture->constNum = Var::getTexUnitNum(); // used as texture unit num here
  2504. Var * specularCubemapAR = new Var("SpecularCubemapAR", "samplerCubeArray");
  2505. specularCubemapAR->uniform = true;
  2506. specularCubemapAR->sampler = true;
  2507. specularCubemapAR->constNum = Var::getTexUnitNum();
  2508. Var * irradianceCubemapAR = new Var("IrradianceCubemapAR", "samplerCubeArray");
  2509. irradianceCubemapAR->uniform = true;
  2510. irradianceCubemapAR->sampler = true;
  2511. irradianceCubemapAR->constNum = Var::getTexUnitNum();
  2512. Var* surface = getSurface(componentList, meta, fd);
  2513. if (!surface)
  2514. {
  2515. Con::errorf("ShaderGen::ReflectionProbeFeatGLSL() - failed to generate surface!");
  2516. return;
  2517. }
  2518. Var *curColor = (Var *)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2519. //Reflection vec
  2520. Var *ibl = (Var *)LangElement::find("ibl");
  2521. if (!ibl)
  2522. {
  2523. ibl = new Var("ibl", "float3");
  2524. }
  2525. Var* eyePos = (Var*)LangElement::find("eyePosWorld");
  2526. if (!eyePos)
  2527. {
  2528. eyePos = new Var;
  2529. eyePos->setType("vec3");
  2530. eyePos->setName("eyePosWorld");
  2531. eyePos->uniform = true;
  2532. eyePos->constSortPos = cspPass;
  2533. }
  2534. //Reflection vec
  2535. String computeForwardProbes = String(" @ = computeForwardProbes(@,@,@,@,@,@,@,@,@,\r\n\t\t");
  2536. computeForwardProbes += String("@,@,\r\n\t\t");
  2537. computeForwardProbes += String("@,@).rgb; \r\n");
  2538. meta->addStatement(new GenOp(computeForwardProbes.c_str(), new DecOp(ibl), surface, cubeMips, numProbes, worldToObjArray, probeConfigData, inProbePosArray, refScaleArray, inRefPosArray, eyePos,
  2539. skylightCubemapIdx, BRDFTexture,
  2540. irradianceCubemapAR, specularCubemapAR));
  2541. Var *ambient = (Var *)LangElement::find("ambient");
  2542. if (!ambient)
  2543. {
  2544. ambient = new Var("ambient", "vec3");
  2545. eyePos->uniform = true;
  2546. eyePos->constSortPos = cspPass;
  2547. }
  2548. meta->addStatement(new GenOp(" @.rgb *= @.rgb;\r\n", ibl, ambient));
  2549. meta->addStatement(new GenOp(" @.rgb = @.rgb;\r\n", curColor, ibl));
  2550. output = meta;
  2551. }
  2552. ShaderFeature::Resources ReflectionProbeFeatGLSL::getResources(const MaterialFeatureData& fd)
  2553. {
  2554. Resources res;
  2555. res.numTex = 3;
  2556. res.numTexReg = 3;
  2557. return res;
  2558. }
  2559. void ReflectionProbeFeatGLSL::setTexData(Material::StageData& stageDat,
  2560. const MaterialFeatureData& stageFeatures,
  2561. RenderPassData& passData,
  2562. U32& texIndex)
  2563. {
  2564. if (stageFeatures.features[MFT_ReflectionProbes])
  2565. {
  2566. passData.mSamplerNames[texIndex] = "BRDFTexture";
  2567. passData.mTexType[texIndex++] = Material::Standard;
  2568. // assuming here that it is a scenegraph cubemap
  2569. passData.mSamplerNames[texIndex] = "SpecularCubemapAR";
  2570. passData.mTexType[texIndex++] = Material::SGCube;
  2571. passData.mSamplerNames[texIndex] = "IrradianceCubemapAR";
  2572. passData.mTexType[texIndex++] = Material::SGCube;
  2573. }
  2574. }