shaderFeatureGLSL.cpp 98 KB

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