shaderFeatureGLSL.cpp 97 KB

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