shaderFeatureGLSL.cpp 106 KB

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