shaderFeatureGLSL.cpp 97 KB

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