shaderFeatureGLSL.cpp 109 KB

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