shaderFeatureGLSL.cpp 108 KB

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