shaderFeatureGLSL.cpp 96 KB

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