shaderFeatureGLSL.cpp 98 KB

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