shaderFeatureGLSL.cpp 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815
  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. if (fd.features[MFT_isDeferred])
  1011. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul, NULL, ShaderFeature::RenderTarget1)));
  1012. else
  1013. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul)));
  1014. output = meta;
  1015. }
  1016. //****************************************************************************
  1017. // Lightmap
  1018. //****************************************************************************
  1019. void LightmapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1020. const MaterialFeatureData &fd )
  1021. {
  1022. // grab tex register from incoming vert
  1023. Var *inTex = getVertTexCoord( "texCoord2" );
  1024. // grab connector texcoord register
  1025. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1026. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1027. outTex->setName( "texCoord2" );
  1028. outTex->setStructName( "OUT" );
  1029. outTex->setType( "vec2" );
  1030. outTex->mapsToSampler = true;
  1031. // setup language elements to output incoming tex coords to output
  1032. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  1033. }
  1034. void LightmapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1035. const MaterialFeatureData &fd )
  1036. {
  1037. // grab connector texcoord register
  1038. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1039. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  1040. inTex->setName( "texCoord2" );
  1041. inTex->setStructName( "IN" );
  1042. inTex->setType( "vec2" );
  1043. inTex->mapsToSampler = true;
  1044. // create texture var
  1045. Var *lightMap = new Var;
  1046. lightMap->setType( "sampler2D" );
  1047. lightMap->setName( "lightMap" );
  1048. lightMap->uniform = true;
  1049. lightMap->sampler = true;
  1050. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1051. // argh, pixel specular should prob use this too
  1052. if( fd.features[MFT_NormalMap] )
  1053. {
  1054. Var *lmColor = new Var;
  1055. lmColor->setName( "lmColor" );
  1056. lmColor->setType( "vec4" );
  1057. LangElement *lmColorDecl = new DecOp( lmColor );
  1058. output = new GenOp( " @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
  1059. return;
  1060. }
  1061. // Add realtime lighting, if it is available
  1062. LangElement *statement = NULL;
  1063. if( fd.features[MFT_RTLighting] )
  1064. {
  1065. // Advanced lighting is the only dynamic lighting supported right now
  1066. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1067. if(inColor != NULL)
  1068. {
  1069. // Find out if RTLighting should be added or substituted
  1070. bool bPreProcessedLighting = false;
  1071. AdvancedLightBinManager *lightBin;
  1072. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1073. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1074. // Lightmap has already been included in the advanced light bin, so
  1075. // no need to do any sampling or anything
  1076. if(bPreProcessedLighting)
  1077. statement = new GenOp( "vec4(@, 1.0)", inColor );
  1078. else
  1079. statement = new GenOp( "tex2D(@, @) + vec4(@.rgb, 0.0)", lightMap, inTex, inColor );
  1080. }
  1081. }
  1082. // If we still don't have it... then just sample the lightmap.
  1083. if ( !statement )
  1084. statement = new GenOp( "tex2D(@, @)", lightMap, inTex );
  1085. // Assign to proper render target
  1086. MultiLine *meta = new MultiLine;
  1087. if( fd.features[MFT_LightbufferMRT] )
  1088. {
  1089. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1090. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1091. }
  1092. else
  1093. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
  1094. output = meta;
  1095. }
  1096. ShaderFeature::Resources LightmapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1097. {
  1098. Resources res;
  1099. res.numTex = 1;
  1100. res.numTexReg = 1;
  1101. return res;
  1102. }
  1103. void LightmapFeatGLSL::setTexData( Material::StageData &stageDat,
  1104. const MaterialFeatureData &fd,
  1105. RenderPassData &passData,
  1106. U32 &texIndex )
  1107. {
  1108. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  1109. passData.mSamplerNames[ texIndex ] = "lightMap";
  1110. if ( tex )
  1111. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1112. else
  1113. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  1114. }
  1115. U32 LightmapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1116. {
  1117. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1118. }
  1119. //****************************************************************************
  1120. // Tonemap
  1121. //****************************************************************************
  1122. void TonemapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1123. const MaterialFeatureData &fd )
  1124. {
  1125. // Grab the connector
  1126. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1127. // Set up the second set of texCoords
  1128. Var *inTex2 = getVertTexCoord( "texCoord2" );
  1129. if ( inTex2 )
  1130. {
  1131. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  1132. outTex2->setName( "texCoord2" );
  1133. outTex2->setStructName( "OUT" );
  1134. outTex2->setType( "vec2" );
  1135. outTex2->mapsToSampler = true;
  1136. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  1137. }
  1138. }
  1139. void TonemapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1140. const MaterialFeatureData &fd )
  1141. {
  1142. // Grab connector
  1143. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1144. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  1145. inTex2->setName( "texCoord2" );
  1146. inTex2->setStructName( "IN" );
  1147. inTex2->setType( "vec2" );
  1148. inTex2->mapsToSampler = true;
  1149. // create texture var
  1150. Var *toneMap = new Var;
  1151. toneMap->setType( "sampler2D" );
  1152. toneMap->setName( "toneMap" );
  1153. toneMap->uniform = true;
  1154. toneMap->sampler = true;
  1155. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1156. MultiLine * meta = new MultiLine;
  1157. // First get the toneMap color
  1158. Var *toneMapColor = new Var;
  1159. toneMapColor->setType( "vec4" );
  1160. toneMapColor->setName( "toneMapColor" );
  1161. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  1162. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
  1163. // We do a different calculation if there is a diffuse map or not
  1164. Material::BlendOp blendOp = Material::Mul;
  1165. if ( fd.features[MFT_DiffuseMap] )
  1166. {
  1167. // Reverse the tonemap
  1168. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
  1169. // Re-tonemap with the current color factored in
  1170. blendOp = Material::ToneMap;
  1171. }
  1172. // Find out if RTLighting should be added
  1173. bool bPreProcessedLighting = false;
  1174. AdvancedLightBinManager *lightBin;
  1175. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1176. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1177. // Add in the realtime lighting contribution
  1178. if ( fd.features[MFT_RTLighting] )
  1179. {
  1180. // Right now, only Advanced Lighting is supported
  1181. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1182. if(inColor != NULL)
  1183. {
  1184. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1185. // the dynamic light buffer, and it already has the tonemap included
  1186. if(bPreProcessedLighting)
  1187. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  1188. else
  1189. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  1190. }
  1191. }
  1192. // Assign to proper render target
  1193. if( fd.features[MFT_LightbufferMRT] )
  1194. {
  1195. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1196. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1197. }
  1198. else
  1199. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  1200. output = meta;
  1201. }
  1202. ShaderFeature::Resources TonemapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1203. {
  1204. Resources res;
  1205. res.numTex = 1;
  1206. res.numTexReg = 1;
  1207. return res;
  1208. }
  1209. void TonemapFeatGLSL::setTexData( Material::StageData &stageDat,
  1210. const MaterialFeatureData &fd,
  1211. RenderPassData &passData,
  1212. U32 &texIndex )
  1213. {
  1214. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  1215. if ( tex )
  1216. {
  1217. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  1218. passData.mSamplerNames[ texIndex ] = "toneMap";
  1219. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1220. }
  1221. }
  1222. U32 TonemapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1223. {
  1224. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1225. }
  1226. //****************************************************************************
  1227. // pureLIGHT Lighting
  1228. //****************************************************************************
  1229. void VertLitGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1230. const MaterialFeatureData &fd )
  1231. {
  1232. // If we have a lightMap or toneMap then our lighting will be
  1233. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1234. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1235. {
  1236. output = NULL;
  1237. return;
  1238. }
  1239. // Create vertex color connector if it doesn't exist.
  1240. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1241. if( !outColor )
  1242. {
  1243. // Grab the connector color
  1244. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1245. Var *outColor = connectComp->getElement( RT_COLOR );
  1246. outColor->setName( "vertColor" );
  1247. outColor->setStructName( "OUT" );
  1248. outColor->setType( "vec4" );
  1249. // Search for vert color
  1250. Var *inColor = (Var*) LangElement::find( "diffuse" );
  1251. // If there isn't a vertex color then we can't do anything
  1252. if( !inColor )
  1253. {
  1254. output = NULL;
  1255. return;
  1256. }
  1257. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1258. }
  1259. else
  1260. output = NULL; // Nothing we need to do.
  1261. }
  1262. void VertLitGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1263. const MaterialFeatureData &fd )
  1264. {
  1265. // If we have a lightMap or toneMap then our lighting will be
  1266. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1267. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1268. {
  1269. output = NULL;
  1270. return;
  1271. }
  1272. // Grab the connector color register
  1273. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1274. if( !vertColor )
  1275. {
  1276. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1277. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1278. vertColor = connectComp->getElement( RT_COLOR );
  1279. vertColor->setName( "vertColor" );
  1280. vertColor->setStructName( "IN" );
  1281. vertColor->setType( "vec4" );
  1282. }
  1283. MultiLine * meta = new MultiLine;
  1284. // Defaults (no diffuse map)
  1285. Material::BlendOp blendOp = Material::Mul;
  1286. LangElement *outColor = vertColor;
  1287. // We do a different calculation if there is a diffuse map or not
  1288. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1289. {
  1290. Var * finalVertColor = new Var;
  1291. finalVertColor->setName( "finalVertColor" );
  1292. finalVertColor->setType( "vec4" );
  1293. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1294. // Reverse the tonemap
  1295. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
  1296. // Set the blend op to tonemap
  1297. blendOp = Material::ToneMap;
  1298. outColor = finalVertColor;
  1299. }
  1300. // Add in the realtime lighting contribution, if applicable
  1301. if ( fd.features[MFT_RTLighting] )
  1302. {
  1303. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1304. if(rtLightingColor != NULL)
  1305. {
  1306. bool bPreProcessedLighting = false;
  1307. AdvancedLightBinManager *lightBin;
  1308. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1309. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1310. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1311. // the dynamic light buffer, and it already has the baked-vertex-color
  1312. // included in it
  1313. if(bPreProcessedLighting)
  1314. outColor = new GenOp( "vec4(@.rgb, 1.0)", rtLightingColor );
  1315. else
  1316. outColor = new GenOp( "vec4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
  1317. }
  1318. }
  1319. // Output the color
  1320. if ( fd.features[MFT_LightbufferMRT] )
  1321. {
  1322. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1323. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1324. }
  1325. else
  1326. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1327. output = meta;
  1328. }
  1329. U32 VertLitGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1330. {
  1331. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1332. }
  1333. //****************************************************************************
  1334. // Detail map
  1335. //****************************************************************************
  1336. void DetailFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1337. const MaterialFeatureData &fd )
  1338. {
  1339. MultiLine *meta = new MultiLine;
  1340. addOutDetailTexCoord( componentList,
  1341. meta,
  1342. fd.features[MFT_TexAnim] );
  1343. output = meta;
  1344. }
  1345. void DetailFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1346. const MaterialFeatureData &fd )
  1347. {
  1348. // Get the detail texture coord.
  1349. Var *inTex = getInTexCoord( "detCoord", "vec2", true, componentList );
  1350. // create texture var
  1351. Var *detailMap = new Var;
  1352. detailMap->setType( "sampler2D" );
  1353. detailMap->setName( "detailMap" );
  1354. detailMap->uniform = true;
  1355. detailMap->sampler = true;
  1356. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1357. // We're doing the standard greyscale detail map
  1358. // technique which can darken and lighten the
  1359. // diffuse texture.
  1360. // TODO: We could add a feature to toggle between this
  1361. // and a simple multiplication with the detail map.
  1362. LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
  1363. if ( fd.features[MFT_isDeferred])
  1364. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add, NULL, ShaderFeature::RenderTarget1 ) );
  1365. else
  1366. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1367. }
  1368. ShaderFeature::Resources DetailFeatGLSL::getResources( const MaterialFeatureData &fd )
  1369. {
  1370. Resources res;
  1371. res.numTex = 1;
  1372. res.numTexReg = 1;
  1373. return res;
  1374. }
  1375. void DetailFeatGLSL::setTexData( Material::StageData &stageDat,
  1376. const MaterialFeatureData &fd,
  1377. RenderPassData &passData,
  1378. U32 &texIndex )
  1379. {
  1380. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1381. if ( tex )
  1382. {
  1383. passData.mSamplerNames[texIndex] = "detailMap";
  1384. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1385. }
  1386. }
  1387. //****************************************************************************
  1388. // Vertex position
  1389. //****************************************************************************
  1390. void VertPositionGLSL::determineFeature( Material *material,
  1391. const GFXVertexFormat *vertexFormat,
  1392. U32 stageNum,
  1393. const FeatureType &type,
  1394. const FeatureSet &features,
  1395. MaterialFeatureData *outFeatureData )
  1396. {
  1397. // This feature is always on!
  1398. outFeatureData->features.addFeature( type );
  1399. }
  1400. void VertPositionGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1401. const MaterialFeatureData &fd )
  1402. {
  1403. // First check for an input position from a previous feature
  1404. // then look for the default vertex position.
  1405. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1406. if ( !inPosition )
  1407. inPosition = (Var*)LangElement::find( "position" );
  1408. // grab connector position
  1409. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1410. Var *outPosition = connectComp->getElement( RT_POSITION );
  1411. outPosition->setName( "gl_Position" );
  1412. MultiLine *meta = new MultiLine;
  1413. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1414. meta->addStatement( new GenOp( " @ = tMul(@, vec4(@.xyz,1));\r\n",
  1415. outPosition, modelview, inPosition ) );
  1416. output = meta;
  1417. }
  1418. //****************************************************************************
  1419. // Reflect Cubemap
  1420. //****************************************************************************
  1421. void ReflectCubeFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1422. const MaterialFeatureData &fd )
  1423. {
  1424. // search for vert normal
  1425. Var *inNormal = (Var*) LangElement::find( "normal" );
  1426. if ( !inNormal )
  1427. return;
  1428. MultiLine * meta = new MultiLine;
  1429. // If a base or bump tex is present in the material, but not in the
  1430. // current pass - we need to add one to the current pass to use
  1431. // its alpha channel as a gloss map. Here we just need the tex coords.
  1432. if( !fd.features[MFT_DiffuseMap] &&
  1433. !fd.features[MFT_NormalMap] )
  1434. {
  1435. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1436. fd.materialFeatures[MFT_NormalMap] )
  1437. {
  1438. // find incoming texture var
  1439. Var *inTex = getVertTexCoord( "texCoord" );
  1440. // grab connector texcoord register
  1441. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1442. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1443. outTex->setName( "texCoord" );
  1444. outTex->setStructName( "OUT" );
  1445. outTex->setType( "vec2" );
  1446. outTex->mapsToSampler = true;
  1447. // setup language elements to output incoming tex coords to output
  1448. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1449. }
  1450. }
  1451. // create cubeTrans
  1452. bool useInstancing = fd.features[MFT_UseInstancing];
  1453. Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
  1454. // cube vert position
  1455. Var * cubeVertPos = new Var;
  1456. cubeVertPos->setName( "cubeVertPos" );
  1457. cubeVertPos->setType( "vec3" );
  1458. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1459. meta->addStatement( new GenOp( " @ = tMul( @, float4(@,1)).xyz;\r\n",
  1460. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1461. // cube normal
  1462. Var * cubeNormal = new Var;
  1463. cubeNormal->setName( "cubeNormal" );
  1464. cubeNormal->setType( "vec3" );
  1465. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1466. meta->addStatement( new GenOp( " @ = ( tMul( (@), vec4(@, 0) ) ).xyz;\r\n",
  1467. cubeNormDecl, cubeTrans, inNormal ) );
  1468. meta->addStatement( new GenOp( " @ = bool(length(@)) ? normalize(@) : @;\r\n",
  1469. cubeNormal, cubeNormal, cubeNormal, cubeNormal ) );
  1470. // grab the eye position
  1471. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1472. if ( !eyePos )
  1473. {
  1474. eyePos = new Var( "eyePosWorld", "vec3" );
  1475. eyePos->uniform = true;
  1476. eyePos->constSortPos = cspPass;
  1477. }
  1478. // eye to vert
  1479. Var * eyeToVert = new Var;
  1480. eyeToVert->setName( "eyeToVert" );
  1481. eyeToVert->setType( "vec3" );
  1482. LangElement *e2vDecl = new DecOp( eyeToVert );
  1483. meta->addStatement( new GenOp( " @ = @ - @;\r\n",
  1484. e2vDecl, cubeVertPos, eyePos ) );
  1485. // grab connector texcoord register
  1486. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1487. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1488. reflectVec->setName( "reflectVec" );
  1489. reflectVec->setStructName( "OUT" );
  1490. reflectVec->setType( "vec3" );
  1491. reflectVec->mapsToSampler = true;
  1492. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1493. output = meta;
  1494. }
  1495. void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1496. const MaterialFeatureData &fd )
  1497. {
  1498. MultiLine * meta = new MultiLine;
  1499. Var *glossColor = NULL;
  1500. // If a base or bump tex is present in the material, but not in the
  1501. // current pass - we need to add one to the current pass to use
  1502. // its alpha channel as a gloss map.
  1503. if( !fd.features[MFT_DiffuseMap] &&
  1504. !fd.features[MFT_NormalMap] )
  1505. {
  1506. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1507. fd.materialFeatures[MFT_NormalMap] )
  1508. {
  1509. // grab connector texcoord register
  1510. Var *inTex = getInTexCoord( "texCoord", "vec2", true, componentList );
  1511. // create texture var
  1512. Var *newMap = new Var;
  1513. newMap->setType( "sampler2D" );
  1514. newMap->setName( "glossMap" );
  1515. newMap->uniform = true;
  1516. newMap->sampler = true;
  1517. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1518. // create sample color
  1519. Var *color = new Var;
  1520. color->setType( "vec4" );
  1521. color->setName( "diffuseColor" );
  1522. LangElement *colorDecl = new DecOp( color );
  1523. glossColor = color;
  1524. meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
  1525. }
  1526. }
  1527. else
  1528. {
  1529. if (fd.features[MFT_isDeferred])
  1530. glossColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1531. if (!glossColor)
  1532. glossColor = (Var*)LangElement::find("specularColor");
  1533. if (!glossColor)
  1534. glossColor = (Var*)LangElement::find("diffuseColor");
  1535. if (!glossColor)
  1536. glossColor = (Var*)LangElement::find("bumpNormal");
  1537. }
  1538. // grab connector texcoord register
  1539. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1540. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1541. reflectVec->setName( "reflectVec" );
  1542. reflectVec->setStructName( "IN" );
  1543. reflectVec->setType( "vec3" );
  1544. reflectVec->mapsToSampler = true;
  1545. // create cubemap var
  1546. Var *cubeMap = new Var;
  1547. cubeMap->setType( "samplerCube" );
  1548. cubeMap->setName( "cubeMap" );
  1549. cubeMap->uniform = true;
  1550. cubeMap->sampler = true;
  1551. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1552. // TODO: Restore the lighting attenuation here!
  1553. Var *attn = NULL;
  1554. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1555. //attn = (Var*)LangElement::find("attn");
  1556. //else
  1557. if ( fd.materialFeatures[MFT_RTLighting] )
  1558. attn =(Var*)LangElement::find("d_NL_Att");
  1559. LangElement *texCube = NULL;
  1560. Var* matinfo = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  1561. //first try and grab the gbuffer
  1562. if (fd.features[MFT_isDeferred] && matinfo)
  1563. {
  1564. if (fd.features[MFT_DeferredSpecMap])
  1565. texCube = new GenOp("textureLod( @, @, (@.a*5) )", cubeMap, reflectVec, matinfo);
  1566. else
  1567. texCube = new GenOp("textureLod( @, @, (([email protected])*6) )", cubeMap, reflectVec, matinfo);
  1568. }
  1569. else if(glossColor) //failing that, rtry and find color data
  1570. texCube = new GenOp("textureLod( @, @, @.a*5)", cubeMap, reflectVec, glossColor);
  1571. else
  1572. texCube = new GenOp("texture( @, @)", cubeMap, reflectVec);
  1573. LangElement *lerpVal = NULL;
  1574. Material::BlendOp blendOp = Material::LerpAlpha;
  1575. // Note that the lerpVal needs to be a float4 so that
  1576. // it will work with the LerpAlpha blend.
  1577. if (matinfo)
  1578. {
  1579. if (attn)
  1580. lerpVal = new GenOp("@ * saturate( @ )", matinfo, attn);
  1581. else
  1582. lerpVal = new GenOp("@", matinfo);
  1583. }
  1584. else if ( glossColor )
  1585. {
  1586. if ( attn )
  1587. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1588. else
  1589. lerpVal = glossColor;
  1590. }
  1591. else
  1592. {
  1593. if ( attn )
  1594. lerpVal = new GenOp( "vec4( saturate( @ ) ).xxxx", attn );
  1595. else
  1596. blendOp = Material::Mul;
  1597. }
  1598. if (fd.features[MFT_isDeferred])
  1599. {
  1600. Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1601. if (fd.features[MFT_DeferredSpecMap])
  1602. meta->addStatement(new GenOp(" @.rgb = lerp( @.rgb, (@).rgb, (@.b));\r\n", targ, targ, texCube, lerpVal));
  1603. else
  1604. meta->addStatement(new GenOp(" @.rgb = lerp( @.rgb, (@).rgb, (@.b));\r\n", targ, targ, texCube, lerpVal));
  1605. }
  1606. else
  1607. meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
  1608. output = meta;
  1609. }
  1610. ShaderFeature::Resources ReflectCubeFeatGLSL::getResources( const MaterialFeatureData &fd )
  1611. {
  1612. Resources res;
  1613. if( fd.features[MFT_DiffuseMap] ||
  1614. fd.features[MFT_NormalMap] )
  1615. {
  1616. res.numTex = 1;
  1617. res.numTexReg = 1;
  1618. }
  1619. else
  1620. {
  1621. res.numTex = 2;
  1622. res.numTexReg = 2;
  1623. }
  1624. return res;
  1625. }
  1626. void ReflectCubeFeatGLSL::setTexData( Material::StageData &stageDat,
  1627. const MaterialFeatureData &stageFeatures,
  1628. RenderPassData &passData,
  1629. U32 &texIndex )
  1630. {
  1631. // set up a gloss map if one is not present in the current pass
  1632. // but is present in the current material stage
  1633. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1634. !passData.mFeatureData.features[MFT_NormalMap] )
  1635. {
  1636. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1637. if ( tex && stageFeatures.features[MFT_DiffuseMap] )
  1638. {
  1639. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  1640. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1641. }
  1642. else
  1643. {
  1644. tex = stageDat.getTex( MFT_NormalMap );
  1645. if ( tex && stageFeatures.features[ MFT_NormalMap ] )
  1646. {
  1647. passData.mSamplerNames[ texIndex ] = "bumpMap";
  1648. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1649. }
  1650. }
  1651. }
  1652. if( stageDat.getCubemap() )
  1653. {
  1654. passData.mCubeMap = stageDat.getCubemap();
  1655. passData.mSamplerNames[texIndex] = "cubeMap";
  1656. passData.mTexType[texIndex++] = Material::Cube;
  1657. }
  1658. else
  1659. {
  1660. if( stageFeatures.features[MFT_CubeMap] )
  1661. {
  1662. // assuming here that it is a scenegraph cubemap
  1663. passData.mSamplerNames[texIndex] = "cubeMap";
  1664. passData.mTexType[texIndex++] = Material::SGCube;
  1665. }
  1666. }
  1667. }
  1668. //****************************************************************************
  1669. // RTLighting
  1670. //****************************************************************************
  1671. RTLightingFeatGLSL::RTLightingFeatGLSL()
  1672. : mDep( "shaders/common/gl/lighting.glsl" )
  1673. {
  1674. addDependency( &mDep );
  1675. }
  1676. void RTLightingFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1677. const MaterialFeatureData &fd )
  1678. {
  1679. MultiLine *meta = new MultiLine;
  1680. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1681. // Special case for lighting imposters. We dont have a vert normal and may not
  1682. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1683. if ( fd.features[MFT_ImposterVert] )
  1684. {
  1685. if ( !fd.features[MFT_NormalMap] )
  1686. {
  1687. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1688. if ( !eyePos )
  1689. {
  1690. eyePos = new Var( "eyePosWorld", "vec3" );
  1691. eyePos->uniform = true;
  1692. eyePos->constSortPos = cspPass;
  1693. }
  1694. Var *inPosition = (Var*)LangElement::find( "position" );
  1695. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1696. outNormal->setName( "wsNormal" );
  1697. outNormal->setStructName( "OUT" );
  1698. outNormal->setType( "vec3" );
  1699. outNormal->mapsToSampler = false;
  1700. // Transform the normal to world space.
  1701. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );
  1702. }
  1703. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1704. output = meta;
  1705. return;
  1706. }
  1707. // Find the incoming vertex normal.
  1708. Var *inNormal = (Var*)LangElement::find( "normal" );
  1709. // Skip out on realtime lighting if we don't have a normal
  1710. // or we're doing some sort of baked lighting.
  1711. if ( !inNormal ||
  1712. fd.features[MFT_LightMap] ||
  1713. fd.features[MFT_ToneMap] ||
  1714. fd.features[MFT_VertLit] )
  1715. return;
  1716. // If there isn't a normal map then we need to pass
  1717. // the world space normal to the pixel shader ourselves.
  1718. if ( !fd.features[MFT_NormalMap] )
  1719. {
  1720. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1721. outNormal->setName( "wsNormal" );
  1722. outNormal->setStructName( "OUT" );
  1723. outNormal->setType( "vec3" );
  1724. outNormal->mapsToSampler = false;
  1725. // Get the transform to world space.
  1726. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1727. // Transform the normal to world space.
  1728. meta->addStatement( new GenOp( " @ = tMul( @, vec4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1729. }
  1730. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1731. output = meta;
  1732. }
  1733. void RTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1734. const MaterialFeatureData &fd )
  1735. {
  1736. // Skip out on realtime lighting if we don't have a normal
  1737. // or we're doing some sort of baked lighting.
  1738. //
  1739. // TODO: We can totally detect for this in the material
  1740. // feature setup... we should move it out of here!
  1741. //
  1742. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1743. return;
  1744. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1745. MultiLine *meta = new MultiLine;
  1746. // Look for a wsNormal or grab it from the connector.
  1747. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  1748. if ( !wsNormal )
  1749. {
  1750. wsNormal = connectComp->getElement( RT_TEXCOORD );
  1751. wsNormal->setName( "wsNormal" );
  1752. wsNormal->setStructName( "IN" );
  1753. wsNormal->setType( "vec3" );
  1754. // If we loaded the normal its our responsibility
  1755. // to normalize it... the interpolators won't.
  1756. //
  1757. // Note we cast to half here to get partial precision
  1758. // optimized code which is an acceptable loss of
  1759. // precision for normals and performs much better
  1760. // on older Geforce cards.
  1761. //
  1762. meta->addStatement( new GenOp( " @ = normalize( half3( @ ) );\r\n", wsNormal, wsNormal ) );
  1763. }
  1764. // Now the wsPosition and wsView.
  1765. Var *wsPosition = getInWsPosition( componentList );
  1766. Var *wsView = getWsView( wsPosition, meta );
  1767. // Create temporaries to hold results of lighting.
  1768. Var *rtShading = new Var( "rtShading", "vec4" );
  1769. Var *specular = new Var( "specular", "vec4" );
  1770. meta->addStatement( new GenOp( " @; @;\r\n",
  1771. new DecOp( rtShading ), new DecOp( specular ) ) );
  1772. // Look for a light mask generated from a previous
  1773. // feature (this is done for BL terrain lightmaps).
  1774. LangElement *lightMask = LangElement::find( "lightMask" );
  1775. if ( !lightMask )
  1776. lightMask = new GenOp( "vec4( 1, 1, 1, 1 )" );
  1777. // Get all the light constants.
  1778. Var *inLightPos = new Var( "inLightPos", "vec4" );
  1779. inLightPos->uniform = true;
  1780. inLightPos->arraySize = 3;
  1781. inLightPos->constSortPos = cspPotentialPrimitive;
  1782. Var *inLightInvRadiusSq = new Var( "inLightInvRadiusSq", "vec4" );
  1783. inLightInvRadiusSq->uniform = true;
  1784. inLightInvRadiusSq->constSortPos = cspPotentialPrimitive;
  1785. Var *inLightColor = new Var( "inLightColor", "vec4" );
  1786. inLightColor->uniform = true;
  1787. inLightColor->arraySize = 4;
  1788. inLightColor->constSortPos = cspPotentialPrimitive;
  1789. Var *inLightSpotDir = new Var( "inLightSpotDir", "vec4" );
  1790. inLightSpotDir->uniform = true;
  1791. inLightSpotDir->arraySize = 3;
  1792. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1793. Var *inLightSpotAngle = new Var( "inLightSpotAngle", "vec4" );
  1794. inLightSpotAngle->uniform = true;
  1795. inLightSpotAngle->constSortPos = cspPotentialPrimitive;
  1796. Var *lightSpotFalloff = new Var( "inLightSpotFalloff", "vec4" );
  1797. lightSpotFalloff->uniform = true;
  1798. lightSpotFalloff->constSortPos = cspPotentialPrimitive;
  1799. Var *specularPower = new Var( "specularPower", "float" );
  1800. specularPower->uniform = true;
  1801. specularPower->constSortPos = cspPotentialPrimitive;
  1802. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  1803. if ( !specularColor )
  1804. {
  1805. specularColor = new Var( "specularColor", "vec4" );
  1806. specularColor->uniform = true;
  1807. specularColor->constSortPos = cspPotentialPrimitive;
  1808. }
  1809. Var *ambient = new Var( "ambient", "vec4" );
  1810. ambient->uniform = true;
  1811. ambient->constSortPos = cspPass;
  1812. // Calculate the diffuse shading and specular powers.
  1813. meta->addStatement( new GenOp( " compute4Lights( @, @, @, @,\r\n"
  1814. " @, @, @, @, @, @, @, @,\r\n"
  1815. " @, @ );\r\n",
  1816. wsView, wsPosition, wsNormal, lightMask,
  1817. inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, specularPower, specularColor,
  1818. rtShading, specular ) );
  1819. // Apply the lighting to the diffuse color.
  1820. LangElement *lighting = new GenOp( "vec4( @.rgb + @.rgb, 1 )", rtShading, ambient );
  1821. meta->addStatement( new GenOp( " @;\r\n", assignColor( lighting, Material::Mul ) ) );
  1822. output = meta;
  1823. }
  1824. ShaderFeature::Resources RTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
  1825. {
  1826. Resources res;
  1827. // These features disable realtime lighting.
  1828. if ( !fd.features[MFT_LightMap] &&
  1829. !fd.features[MFT_ToneMap] &&
  1830. !fd.features[MFT_VertLit] )
  1831. {
  1832. // If enabled we pass the position.
  1833. res.numTexReg = 1;
  1834. // If there isn't a bump map then we pass the
  1835. // world space normal as well.
  1836. if ( !fd.features[MFT_NormalMap] )
  1837. res.numTexReg++;
  1838. }
  1839. return res;
  1840. }
  1841. //****************************************************************************
  1842. // Fog
  1843. //****************************************************************************
  1844. FogFeatGLSL::FogFeatGLSL()
  1845. : mFogDep( "shaders/common/gl/torque.glsl" )
  1846. {
  1847. addDependency( &mFogDep );
  1848. }
  1849. void FogFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1850. const MaterialFeatureData &fd )
  1851. {
  1852. MultiLine *meta = new MultiLine;
  1853. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1854. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1855. {
  1856. // Grab the eye position.
  1857. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1858. if ( !eyePos )
  1859. {
  1860. eyePos = new Var( "eyePosWorld", "vec3" );
  1861. eyePos->uniform = true;
  1862. eyePos->constSortPos = cspPass;
  1863. }
  1864. Var *fogData = new Var( "fogData", "vec3" );
  1865. fogData->uniform = true;
  1866. fogData->constSortPos = cspPass;
  1867. Var *wsPosition = new Var( "fogPos", "vec3" );
  1868. getWsPosition( componentList,
  1869. fd.features[MFT_UseInstancing],
  1870. meta,
  1871. new DecOp( wsPosition ) );
  1872. // We pass the fog amount to the pixel shader.
  1873. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1874. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1875. fogAmount->setName( "fogAmount" );
  1876. fogAmount->setStructName( "OUT" );
  1877. fogAmount->setType( "float" );
  1878. fogAmount->mapsToSampler = false;
  1879. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1880. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1881. }
  1882. else
  1883. {
  1884. // We fog in world space... make sure the world space
  1885. // position is passed to the pixel shader. This is
  1886. // often already passed for lighting, so it takes up
  1887. // no extra output registers.
  1888. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1889. }
  1890. output = meta;
  1891. }
  1892. void FogFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1893. const MaterialFeatureData &fd )
  1894. {
  1895. MultiLine *meta = new MultiLine;
  1896. Var *fogColor = new Var;
  1897. fogColor->setType( "vec4" );
  1898. fogColor->setName( "fogColor" );
  1899. fogColor->uniform = true;
  1900. fogColor->constSortPos = cspPass;
  1901. // Get the out color.
  1902. Var *color = (Var*) LangElement::find( "col" );
  1903. if ( !color )
  1904. {
  1905. color = new Var;
  1906. color->setType( "vec4" );
  1907. color->setName( "col" );
  1908. color->setStructName("OUT");
  1909. }
  1910. Var *fogAmount;
  1911. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1912. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1913. {
  1914. // Per-vertex.... just get the fog amount.
  1915. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1916. fogAmount = connectComp->getElement( RT_TEXCOORD );
  1917. fogAmount->setName( "fogAmount" );
  1918. fogAmount->setStructName( "IN" );
  1919. fogAmount->setType( "float" );
  1920. }
  1921. else
  1922. {
  1923. Var *wsPosition = getInWsPosition( componentList );
  1924. // grab the eye position
  1925. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1926. if ( !eyePos )
  1927. {
  1928. eyePos = new Var( "eyePosWorld", "vec3" );
  1929. eyePos->uniform = true;
  1930. eyePos->constSortPos = cspPass;
  1931. }
  1932. Var *fogData = new Var( "fogData", "vec3" );
  1933. fogData->uniform = true;
  1934. fogData->constSortPos = cspPass;
  1935. /// Get the fog amount.
  1936. fogAmount = new Var( "fogAmount", "float" );
  1937. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1938. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  1939. }
  1940. // Lerp between the fog color and diffuse color.
  1941. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  1942. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  1943. output = meta;
  1944. }
  1945. ShaderFeature::Resources FogFeatGLSL::getResources( const MaterialFeatureData &fd )
  1946. {
  1947. Resources res;
  1948. res.numTexReg = 1;
  1949. return res;
  1950. }
  1951. //****************************************************************************
  1952. // Visibility
  1953. //****************************************************************************
  1954. VisibilityFeatGLSL::VisibilityFeatGLSL()
  1955. : mTorqueDep( "shaders/common/gl/torque.glsl" )
  1956. {
  1957. addDependency( &mTorqueDep );
  1958. }
  1959. void VisibilityFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1960. const MaterialFeatureData &fd )
  1961. {
  1962. MultiLine *meta = new MultiLine;
  1963. output = meta;
  1964. if ( fd.features[ MFT_UseInstancing ] )
  1965. {
  1966. // We pass the visibility to the pixel shader via
  1967. // another output register.
  1968. //
  1969. // TODO: We should see if we can share this register
  1970. // with some other common instanced data.
  1971. //
  1972. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1973. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  1974. outVisibility->setStructName( "OUT" );
  1975. outVisibility->setName( "visibility" );
  1976. outVisibility->setType( "float" );
  1977. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  1978. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  1979. instVisibility->setStructName( "IN" );
  1980. instVisibility->setName( "inst_visibility" );
  1981. instVisibility->setType( "float" );
  1982. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  1983. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  1984. }
  1985. if ( fd.features[ MFT_IsTranslucent ] )
  1986. return;
  1987. addOutVpos( meta, componentList );
  1988. }
  1989. void VisibilityFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1990. const MaterialFeatureData &fd )
  1991. {
  1992. // Get the visibility constant.
  1993. Var *visibility = NULL;
  1994. if ( fd.features[ MFT_UseInstancing ] )
  1995. visibility = getInTexCoord( "visibility", "float", false, componentList );
  1996. else
  1997. {
  1998. visibility = (Var*)LangElement::find( "visibility" );
  1999. if ( !visibility )
  2000. {
  2001. visibility = new Var();
  2002. visibility->setType( "float" );
  2003. visibility->setName( "visibility" );
  2004. visibility->uniform = true;
  2005. visibility->constSortPos = cspPotentialPrimitive;
  2006. }
  2007. }
  2008. MultiLine* meta = new MultiLine;
  2009. output = meta;
  2010. // Translucent objects do a simple alpha fade.
  2011. if ( fd.features[ MFT_IsTranslucent ] )
  2012. {
  2013. Var *color = (Var*) LangElement::find( "col" );
  2014. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  2015. return;
  2016. }
  2017. // Everything else does a fizzle.
  2018. Var *vPos = getInVpos( meta, componentList );
  2019. meta->addStatement( new GenOp( " fizzle( @, @ );\r\n", vPos, visibility ) );
  2020. }
  2021. ShaderFeature::Resources VisibilityFeatGLSL::getResources( const MaterialFeatureData &fd )
  2022. {
  2023. Resources res;
  2024. // TODO: Fix for instancing.
  2025. if ( !fd.features[ MFT_IsTranslucent ] )
  2026. res.numTexReg = 1;
  2027. return res;
  2028. }
  2029. //****************************************************************************
  2030. // AlphaTest
  2031. //****************************************************************************
  2032. void AlphaTestGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2033. const MaterialFeatureData &fd )
  2034. {
  2035. // If we're below SM3 and don't have a depth output
  2036. // feature then don't waste an instruction here.
  2037. if ( GFX->getPixelShaderVersion() < 3.0 &&
  2038. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  2039. !fd.features[ MFT_DepthOut ] )
  2040. {
  2041. output = NULL;
  2042. return;
  2043. }
  2044. // If we don't have a color var then we cannot do an alpha test.
  2045. Var *color = (Var*)LangElement::find( "col1" );
  2046. if ( !color )
  2047. color = (Var*)LangElement::find("col");
  2048. if ( !color )
  2049. {
  2050. output = NULL;
  2051. return;
  2052. }
  2053. // Now grab the alpha test value.
  2054. Var *alphaTestVal = new Var;
  2055. alphaTestVal->setType( "float" );
  2056. alphaTestVal->setName( "alphaTestValue" );
  2057. alphaTestVal->uniform = true;
  2058. alphaTestVal->constSortPos = cspPotentialPrimitive;
  2059. // Do the clip.
  2060. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  2061. }
  2062. //****************************************************************************
  2063. // GlowMask
  2064. //****************************************************************************
  2065. void GlowMaskGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2066. const MaterialFeatureData &fd )
  2067. {
  2068. output = NULL;
  2069. // Get the output color... and make it black to mask out
  2070. // glow passes rendered before us.
  2071. //
  2072. // The shader compiler will optimize out all the other
  2073. // code above that doesn't contribute to the alpha mask.
  2074. Var *color = (Var*)LangElement::find( "col" );
  2075. if ( color )
  2076. output = new GenOp( " @.rgb = vec3(0);\r\n", color );
  2077. }
  2078. //****************************************************************************
  2079. // RenderTargetZero
  2080. //****************************************************************************
  2081. void RenderTargetZeroGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  2082. {
  2083. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  2084. // This will prevent a divide by zero causing an FP special on float render targets
  2085. output = new GenOp( " @;\r\n", assignColor( new GenOp( "vec4(0.00001)" ), Material::None, NULL, mOutputTargetMask ) );
  2086. }
  2087. //****************************************************************************
  2088. // HDR Output
  2089. //****************************************************************************
  2090. HDROutGLSL::HDROutGLSL()
  2091. : mTorqueDep( "shaders/common/gl/torque.glsl" )
  2092. {
  2093. addDependency( &mTorqueDep );
  2094. }
  2095. void HDROutGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2096. const MaterialFeatureData &fd )
  2097. {
  2098. // Let the helper function do the work.
  2099. Var *color = (Var*)LangElement::find( "col" );
  2100. if ( color )
  2101. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2102. }
  2103. //****************************************************************************
  2104. // FoliageFeatureGLSL
  2105. //****************************************************************************
  2106. #include "T3D/fx/groundCover.h"
  2107. FoliageFeatureGLSL::FoliageFeatureGLSL()
  2108. : mDep( "shaders/common/gl/foliage.glsl" )
  2109. {
  2110. addDependency( &mDep );
  2111. }
  2112. void FoliageFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2113. const MaterialFeatureData &fd )
  2114. {
  2115. // Get the input variables we need.
  2116. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2117. if ( !inPosition )
  2118. inPosition = (Var*)LangElement::find( "position" );
  2119. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2120. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2121. MultiLine *meta = new MultiLine;
  2122. // Declare the normal and tangent variables since they do not exist
  2123. // in this vert type, but we do need to set them up for others.
  2124. Var *normal = (Var*)LangElement::find( "normal" );
  2125. AssertFatal( normal, "FoliageFeatureGLSL requires vert normal!" );
  2126. Var *tangent = new Var;
  2127. tangent->setType( "vec3" );
  2128. tangent->setName( "T" );
  2129. LangElement *tangentDec = new DecOp( tangent );
  2130. meta->addStatement( new GenOp( " @;\n", tangentDec ) );
  2131. // We add a float foliageFade to the OUT structure.
  2132. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2133. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2134. fade->setName( "foliageFade" );
  2135. fade->setStructName( "OUT" );
  2136. fade->setType( "float" );
  2137. // grab the eye position
  2138. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2139. if ( !eyePos )
  2140. {
  2141. eyePos = new Var( "eyePosWorld", "vec3" );
  2142. eyePos->uniform = true;
  2143. eyePos->constSortPos = cspPass;
  2144. }
  2145. // All actual work is offloaded to this method.
  2146. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2147. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2148. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2149. output = meta;
  2150. }
  2151. void FoliageFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2152. const MaterialFeatureData &fd )
  2153. {
  2154. // Find / create IN.foliageFade
  2155. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2156. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2157. fade->setName( "foliageFade" );
  2158. fade->setStructName( "IN" );
  2159. fade->setType( "float" );
  2160. // Find / create visibility
  2161. Var *visibility = (Var*) LangElement::find( "visibility" );
  2162. if ( !visibility )
  2163. {
  2164. visibility = new Var();
  2165. visibility->setType( "float" );
  2166. visibility->setName( "visibility" );
  2167. visibility->uniform = true;
  2168. visibility->constSortPos = cspPotentialPrimitive;
  2169. }
  2170. MultiLine *meta = new MultiLine;
  2171. // Multiply foliageFade into visibility.
  2172. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2173. output = meta;
  2174. }
  2175. void FoliageFeatureGLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2176. {
  2177. // This isn't really necessary since the outFeatureData will be filtered after
  2178. // this call.
  2179. if ( features.hasFeature( MFT_Foliage ) )
  2180. outFeatureData->features.addFeature( type );
  2181. }
  2182. ShaderFeatureConstHandles* FoliageFeatureGLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2183. {
  2184. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2185. AssertFatal( gcover != NULL, "FoliageFeatureGLSL::createConstHandles - userObject was not valid!" );
  2186. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2187. handles->mGroundCover = gcover;
  2188. handles->init( shader );
  2189. return handles;
  2190. }
  2191. void ParticleNormalFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2192. {
  2193. MultiLine *meta = new MultiLine;
  2194. output = meta;
  2195. // Calculate normal and tangent values since we want to keep particle verts
  2196. // as light-weight as possible
  2197. Var *normal = (Var*) LangElement::find("normal");
  2198. if(normal == NULL)
  2199. {
  2200. normal = new Var;
  2201. normal->setType( "vec3" );
  2202. normal->setName( "normal" );
  2203. // These values are not accidental. It is slightly adjusted from facing straight into the
  2204. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2205. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2206. // [11/23/2009 Pat]
  2207. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2208. }
  2209. Var *T = (Var*) LangElement::find( "T" );
  2210. if(T == NULL)
  2211. {
  2212. T = new Var;
  2213. T->setType( "vec3" );
  2214. T->setName( "T" );
  2215. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2216. }
  2217. }
  2218. //****************************************************************************
  2219. // ImposterVertFeatureGLSL
  2220. //****************************************************************************
  2221. ImposterVertFeatureGLSL::ImposterVertFeatureGLSL()
  2222. : mDep( "shaders/common/gl/imposter.glsl" )
  2223. {
  2224. addDependency( &mDep );
  2225. }
  2226. void ImposterVertFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2227. const MaterialFeatureData &fd )
  2228. {
  2229. MultiLine *meta = new MultiLine;
  2230. output = meta;
  2231. // Get the input vertex variables.
  2232. Var *inPosition = (Var*)LangElement::find( "position" );
  2233. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2234. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2235. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2236. // Get the input shader constants.
  2237. Var *imposterLimits = new Var;
  2238. imposterLimits->setType( "vec4" );
  2239. imposterLimits->setName( "imposterLimits" );
  2240. imposterLimits->uniform = true;
  2241. imposterLimits->constSortPos = cspPotentialPrimitive;
  2242. Var *imposterUVs = new Var;
  2243. imposterUVs->setType( "vec4" );
  2244. imposterUVs->setName( "imposterUVs" );
  2245. imposterUVs->arraySize = 64; // See imposter.glsl
  2246. imposterUVs->uniform = true;
  2247. imposterUVs->constSortPos = cspPotentialPrimitive;
  2248. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2249. if ( !eyePos )
  2250. {
  2251. eyePos = new Var( "eyePosWorld", "vec3" );
  2252. eyePos->uniform = true;
  2253. eyePos->constSortPos = cspPass;
  2254. }
  2255. // Declare the outputs from this feature.
  2256. Var *outInPosition = new Var;
  2257. outInPosition->setType( "vec3" );
  2258. outInPosition->setName( "inPosition" );
  2259. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2260. Var *outTexCoord = new Var;
  2261. outTexCoord->setType( "vec2" );
  2262. outTexCoord->setName( "texCoord" );
  2263. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2264. Var *outWorldToTangent = new Var;
  2265. outWorldToTangent->setType( "float3x3" );
  2266. outWorldToTangent->setName( "worldToTangent" );
  2267. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2268. // Add imposterFade to the OUT structure.
  2269. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2270. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2271. outFade->setName( "imposterFade" );
  2272. outFade->setStructName( "OUT" );
  2273. outFade->setType( "float" );
  2274. // Assign OUT.imposterFade
  2275. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2276. // All actual work is done in this method.
  2277. meta->addStatement( new GenOp( " imposter_v( @.xyz, int(@.w), @.x * length(@), normalize(@), normalize(@), int(@.y), int(@.x), @.z, bool(@.w), @, @, @, @, @ );\r\n",
  2278. inPosition,
  2279. inPosition,
  2280. inMiscParams,
  2281. inRightVec,
  2282. inUpVec,
  2283. inRightVec,
  2284. imposterLimits,
  2285. imposterLimits,
  2286. imposterLimits,
  2287. imposterLimits,
  2288. eyePos,
  2289. imposterUVs,
  2290. outInPosition,
  2291. outTexCoord,
  2292. outWorldToTangent ) );
  2293. // Copy the position to wsPosition for use in shaders
  2294. // down stream instead of looking for objTrans.
  2295. Var *wsPosition = new Var;
  2296. wsPosition->setType( "vec3" );
  2297. wsPosition->setName( "wsPosition" );
  2298. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2299. // If we new viewToTangent... its the same as the
  2300. // world to tangent for an imposter.
  2301. Var *viewToTangent = new Var;
  2302. viewToTangent->setType( "float3x3" );
  2303. viewToTangent->setName( "viewToTangent" );
  2304. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2305. }
  2306. void ImposterVertFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2307. const MaterialFeatureData &fd )
  2308. {
  2309. // Find / create IN.imposterFade
  2310. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2311. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2312. fade->setName( "imposterFade" );
  2313. fade->setStructName( "IN" );
  2314. fade->setType( "float" );
  2315. // Find / create visibility
  2316. Var *visibility = (Var*) LangElement::find( "visibility" );
  2317. if ( !visibility )
  2318. {
  2319. visibility = new Var();
  2320. visibility->setType( "float" );
  2321. visibility->setName( "visibility" );
  2322. visibility->uniform = true;
  2323. visibility->constSortPos = cspPotentialPrimitive;
  2324. }
  2325. MultiLine *meta = new MultiLine;
  2326. // Multiply foliageFade into visibility.
  2327. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2328. output = meta;
  2329. }
  2330. void ImposterVertFeatureGLSL::determineFeature( Material *material,
  2331. const GFXVertexFormat *vertexFormat,
  2332. U32 stageNum,
  2333. const FeatureType &type,
  2334. const FeatureSet &features,
  2335. MaterialFeatureData *outFeatureData )
  2336. {
  2337. if ( features.hasFeature( MFT_ImposterVert ) )
  2338. outFeatureData->features.addFeature( MFT_ImposterVert );
  2339. }
  2340. //****************************************************************************
  2341. // Vertex position
  2342. //****************************************************************************
  2343. void DeferredSkyGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2344. const MaterialFeatureData &fd )
  2345. {
  2346. Var *outPosition = (Var*)LangElement::find( "gl_Position" );
  2347. MultiLine *meta = new MultiLine;
  2348. //meta->addStatement( new GenOp( " @.w = @.z;\r\n", outPosition, outPosition ) );
  2349. output = meta;
  2350. }