shaderFeatureGLSL.cpp 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085
  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( " @ = normalize( @ - @ );\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. ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(componentList[C_CONNECTOR]);
  710. Var* diffuseColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  711. Var* ormConfig = (Var*)LangElement::find("ORMConfig");
  712. if (!ormConfig)
  713. {
  714. Var* metalness = (Var*)LangElement::find("metalness");
  715. if (!metalness)
  716. {
  717. metalness = new Var("metalness", "float");
  718. metalness->uniform = true;
  719. metalness->constSortPos = cspPotentialPrimitive;
  720. }
  721. Var* roughness = (Var*)LangElement::find("roughness");
  722. if (!roughness)
  723. {
  724. roughness = new Var("roughness", "float");
  725. roughness->uniform = true;
  726. roughness->constSortPos = cspPotentialPrimitive;
  727. }
  728. ormConfig = new Var("ORMConfig", "vec4");
  729. LangElement* colorDecl = new DecOp(ormConfig);
  730. meta->addStatement(new GenOp(" @ = vec4(0.0,1.0,@,@);\r\n", colorDecl, roughness, metalness)); //reconstruct ormConfig, no ao darkening
  731. }
  732. Var* normal = (Var*)LangElement::find("normal");
  733. if (!normal)
  734. {
  735. normal = new Var("normal", "vec3");
  736. meta->addStatement(new GenOp(" @;\r\n\n", new DecOp(normal)));
  737. Var* wsNormal = (Var*)LangElement::find("wsNormal");
  738. if (!fd.features[MFT_NormalMap])
  739. {
  740. if (!wsNormal)
  741. wsNormal = getInWorldNormal(componentList);
  742. meta->addStatement(new GenOp(" @ = normalize( @ );\r\n\n", normal, wsNormal));
  743. }
  744. else
  745. {
  746. meta->addStatement(new GenOp(" @ = normalize( @ );\r\n", normal, wsNormal));
  747. }
  748. }
  749. Var* wsEyePos = (Var*)LangElement::find("eyePosWorld");
  750. if (!wsEyePos)
  751. {
  752. wsEyePos = new Var("eyePosWorld", "vec3");
  753. wsEyePos->uniform = true;
  754. wsEyePos->constSortPos = cspPass;
  755. }
  756. Var* wsPosition = getInWsPosition(componentList);
  757. Var* wsView = getWsView(wsPosition, meta);
  758. Var* surface = (Var*)LangElement::find("surface");
  759. if (!surface)
  760. {
  761. surface = new Var("surface", "Surface");
  762. meta->addStatement(new GenOp(" @ = createForwardSurface(@,@,@,@,@,@);\r\n\n", new DecOp(surface), diffuseColor, normal, ormConfig,
  763. wsPosition, wsEyePos, wsView));
  764. }
  765. /*Var* surface = (Var*)LangElement::find("surface");
  766. if (!surface)
  767. {
  768. surface = new Var("surface", "float");
  769. }*/
  770. return surface;
  771. }
  772. //****************************************************************************
  773. // Base Texture
  774. //****************************************************************************
  775. DiffuseMapFeatGLSL::DiffuseMapFeatGLSL()
  776. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl"))
  777. {
  778. addDependency(&mTorqueDep);
  779. }
  780. void DiffuseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  781. const MaterialFeatureData &fd )
  782. {
  783. MultiLine *meta = new MultiLine;
  784. getOutTexCoord( "texCoord",
  785. "vec2",
  786. fd.features[MFT_TexAnim],
  787. meta,
  788. componentList );
  789. output = meta;
  790. }
  791. U32 DiffuseMapFeatGLSL::getOutputTargets(const MaterialFeatureData &fd) const
  792. {
  793. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  794. }
  795. void DiffuseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  796. const MaterialFeatureData &fd )
  797. {
  798. // grab connector texcoord register
  799. Var *inTex = getInTexCoord( "texCoord", "vec2", componentList );
  800. //determine output target
  801. ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
  802. if (fd.features[MFT_isDeferred])
  803. targ = ShaderFeature::RenderTarget1;
  804. // create texture var
  805. Var *diffuseMap = new Var;
  806. diffuseMap->setType( "sampler2D" );
  807. diffuseMap->setName( "diffuseMap" );
  808. diffuseMap->uniform = true;
  809. diffuseMap->sampler = true;
  810. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  811. // create sample color var
  812. Var *diffColor = new Var;
  813. diffColor->setType("vec4");
  814. diffColor->setName("diffuseColor");
  815. LangElement *colorDecl = new DecOp( diffColor );
  816. MultiLine * meta = new MultiLine;
  817. output = meta;
  818. if ( fd.features[MFT_CubeMap] )
  819. {
  820. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n",
  821. colorDecl,
  822. diffuseMap,
  823. inTex ) );
  824. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffColor, Material::Mul, NULL, targ) ) );
  825. }
  826. else if(fd.features[MFT_DiffuseMapAtlas])
  827. {
  828. // Handle atlased textures
  829. // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
  830. Var *atlasedTex = new Var;
  831. atlasedTex->setName("atlasedTexCoord");
  832. atlasedTex->setType("vec2");
  833. LangElement *atDecl = new DecOp(atlasedTex);
  834. // Parameters of the texture atlas
  835. Var *atParams = new Var;
  836. atParams->setType("vec4");
  837. atParams->setName("diffuseAtlasParams");
  838. atParams->uniform = true;
  839. atParams->constSortPos = cspPotentialPrimitive;
  840. // Parameters of the texture (tile) this object is using in the atlas
  841. Var *tileParams = new Var;
  842. tileParams->setType("vec4");
  843. tileParams->setName("diffuseAtlasTileParams");
  844. tileParams->uniform = true;
  845. tileParams->constSortPos = cspPotentialPrimitive;
  846. const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
  847. if(is_sm3)
  848. {
  849. // Figure out the mip level
  850. meta->addStatement(new GenOp(" float2 _dx = ddx(@ * @.z);\r\n", inTex, atParams));
  851. meta->addStatement(new GenOp(" float2 _dy = ddy(@ * @.z);\r\n", inTex, atParams));
  852. meta->addStatement(new GenOp(" float mipLod = 0.5 * log2(max(dot(_dx, _dx), dot(_dy, _dy)));\r\n"));
  853. meta->addStatement(new GenOp(" mipLod = clamp(mipLod, 0.0, @.w);\r\n", atParams));
  854. // And the size of the mip level
  855. meta->addStatement(new GenOp(" float mipPixSz = pow(2.0, @.w - mipLod);\r\n", atParams));
  856. meta->addStatement(new GenOp(" float2 mipSz = mipPixSz / @.xy;\r\n", atParams));
  857. }
  858. else
  859. {
  860. meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
  861. }
  862. // Tiling mode
  863. // TODO: Select wrap or clamp somehow
  864. if( true ) // Wrap
  865. meta->addStatement(new GenOp(" @ = frac(@);\r\n", atDecl, inTex));
  866. else // Clamp
  867. meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, inTex));
  868. // Finally scale/offset, and correct for filtering
  869. meta->addStatement(new GenOp(" @ = @ * ((mipSz * @.xy - 1.0) / mipSz) + 0.5 / mipSz + @.xy * @.xy;\r\n",
  870. atlasedTex, atlasedTex, atParams, atParams, tileParams));
  871. // Add a newline
  872. meta->addStatement(new GenOp( "\r\n"));
  873. // For the rest of the feature...
  874. inTex = atlasedTex;
  875. // To dump out UV coords...
  876. //#define DEBUG_ATLASED_UV_COORDS
  877. #ifdef DEBUG_ATLASED_UV_COORDS
  878. if(!fd.features[MFT_DeferredConditioner])
  879. {
  880. meta->addStatement(new GenOp(" @ = vec4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
  881. meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
  882. return;
  883. }
  884. #endif
  885. meta->addStatement(new GenOp( " @ = tex2Dlod(@, float4(@, 0.0, mipLod));\r\n",
  886. new DecOp(diffColor), diffuseMap, inTex));
  887. meta->addStatement(new GenOp( " @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
  888. }
  889. else
  890. {
  891. meta->addStatement(new GenOp("@ = tex2D(@, @);\r\n", colorDecl, diffuseMap, inTex));
  892. meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ)));
  893. }
  894. }
  895. ShaderFeature::Resources DiffuseMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  896. {
  897. Resources res;
  898. res.numTex = 1;
  899. res.numTexReg = 1;
  900. return res;
  901. }
  902. void DiffuseMapFeatGLSL::setTexData( Material::StageData &stageDat,
  903. const MaterialFeatureData &fd,
  904. RenderPassData &passData,
  905. U32 &texIndex )
  906. {
  907. GFXTextureObject *tex = stageDat.getTex( MFT_DiffuseMap );
  908. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  909. passData.mTexSlot[ texIndex++ ].texObject = tex;
  910. }
  911. //****************************************************************************
  912. // Overlay Texture
  913. //****************************************************************************
  914. void OverlayTexFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  915. const MaterialFeatureData &fd )
  916. {
  917. Var *inTex = getVertTexCoord( "texCoord2" );
  918. AssertFatal( inTex, "OverlayTexFeatGLSL::processVert() - The second UV set was not found!" );
  919. // grab connector texcoord register
  920. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  921. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  922. outTex->setName( "outTexCoord2" );
  923. outTex->setStructName( "OUT" );
  924. outTex->setType( "vec2" );
  925. if( fd.features[MFT_TexAnim] )
  926. {
  927. inTex->setType( "vec4" );
  928. // Find or create the texture matrix.
  929. Var *texMat = (Var*)LangElement::find( "texMat" );
  930. if ( !texMat )
  931. {
  932. texMat = new Var;
  933. texMat->setType( "float4x4" );
  934. texMat->setName( "texMat" );
  935. texMat->uniform = true;
  936. texMat->constSortPos = cspPass;
  937. }
  938. output = new GenOp( " @ = tMul(@, @);\r\n", outTex, texMat, inTex );
  939. return;
  940. }
  941. // setup language elements to output incoming tex coords to output
  942. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  943. }
  944. void OverlayTexFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  945. const MaterialFeatureData &fd )
  946. {
  947. // grab connector texcoord register
  948. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  949. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  950. inTex->setName( "texCoord2" );
  951. inTex->setStructName( "IN" );
  952. inTex->setType( "vec2" );
  953. // create texture var
  954. Var *diffuseMap = new Var;
  955. diffuseMap->setType( "sampler2D" );
  956. diffuseMap->setName( "overlayMap" );
  957. diffuseMap->uniform = true;
  958. diffuseMap->sampler = true;
  959. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  960. LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
  961. output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
  962. }
  963. ShaderFeature::Resources OverlayTexFeatGLSL::getResources( const MaterialFeatureData &fd )
  964. {
  965. Resources res;
  966. res.numTex = 1;
  967. res.numTexReg = 1;
  968. return res;
  969. }
  970. void OverlayTexFeatGLSL::setTexData( Material::StageData &stageDat,
  971. const MaterialFeatureData &fd,
  972. RenderPassData &passData,
  973. U32 &texIndex )
  974. {
  975. GFXTextureObject *tex = stageDat.getTex( MFT_OverlayMap );
  976. if ( tex )
  977. {
  978. passData.mSamplerNames[ texIndex ] = "overlayMap";
  979. passData.mTexSlot[ texIndex++ ].texObject = tex;
  980. }
  981. }
  982. //****************************************************************************
  983. // Diffuse color
  984. //****************************************************************************
  985. U32 DiffuseFeatureGLSL::getOutputTargets(const MaterialFeatureData &fd) const
  986. {
  987. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  988. }
  989. void DiffuseFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  990. const MaterialFeatureData &fd )
  991. {
  992. Var* diffuseMaterialColor = new Var;
  993. diffuseMaterialColor->setType( "vec4" );
  994. diffuseMaterialColor->setName( "diffuseMaterialColor" );
  995. diffuseMaterialColor->uniform = true;
  996. diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
  997. MultiLine* meta = new MultiLine;
  998. Var *col = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  999. ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
  1000. if (fd.features[MFT_isDeferred])
  1001. {
  1002. targ = ShaderFeature::RenderTarget1;
  1003. col = (Var*)LangElement::find(getOutputTargetVarName(targ));
  1004. meta = new MultiLine;
  1005. if (!col)
  1006. {
  1007. // create color var
  1008. col = new Var;
  1009. col->setType("vec4");
  1010. col->setName(getOutputTargetVarName(targ));
  1011. col->setStructName("OUT");
  1012. meta->addStatement(new GenOp(" @ = vec4(1.0,1.0,1.0,1.0);\r\n", col));
  1013. }
  1014. }
  1015. Material::BlendOp op;
  1016. if (fd.features[MFT_DiffuseMap])
  1017. op = Material::Mul;
  1018. else
  1019. op = Material::None;
  1020. meta->addStatement(new GenOp(" @;\r\n", assignColor(diffuseMaterialColor, op, NULL, targ)));
  1021. output = meta;
  1022. }
  1023. //****************************************************************************
  1024. // Diffuse vertex color
  1025. //****************************************************************************
  1026. void DiffuseVertColorFeatureGLSL::processVert( Vector< ShaderComponent* >& componentList,
  1027. const MaterialFeatureData& fd )
  1028. {
  1029. // Create vertex color connector if it doesn't exist.
  1030. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1031. if( !outColor )
  1032. {
  1033. // Search for vert color.
  1034. Var* inColor = dynamic_cast< Var* >( LangElement::find( "diffuse" ) );
  1035. if( !inColor )
  1036. {
  1037. output = NULL;
  1038. return;
  1039. }
  1040. // Create connector.
  1041. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1042. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1043. outColor = connectComp->getElement( RT_COLOR );
  1044. outColor->setName( "vertColor" );
  1045. outColor->setStructName( "OUT" );
  1046. outColor->setType( "vec4" );
  1047. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1048. }
  1049. else
  1050. output = NULL; // Nothing we need to do.
  1051. }
  1052. void DiffuseVertColorFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1053. const MaterialFeatureData &fd )
  1054. {
  1055. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1056. if( !vertColor )
  1057. {
  1058. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1059. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1060. vertColor = connectComp->getElement( RT_COLOR );
  1061. vertColor->setName( "vertColor" );
  1062. vertColor->setStructName( "IN" );
  1063. vertColor->setType( "vec4" );
  1064. }
  1065. MultiLine* meta = new MultiLine;
  1066. if (fd.features[MFT_isDeferred])
  1067. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul, NULL, ShaderFeature::RenderTarget1)));
  1068. else
  1069. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul)));
  1070. output = meta;
  1071. }
  1072. //****************************************************************************
  1073. // Lightmap
  1074. //****************************************************************************
  1075. void LightmapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1076. const MaterialFeatureData &fd )
  1077. {
  1078. // grab tex register from incoming vert
  1079. Var *inTex = getVertTexCoord( "texCoord2" );
  1080. // grab connector texcoord register
  1081. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1082. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1083. outTex->setName( "texCoord2" );
  1084. outTex->setStructName( "OUT" );
  1085. outTex->setType( "vec2" );
  1086. // setup language elements to output incoming tex coords to output
  1087. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  1088. }
  1089. void LightmapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1090. const MaterialFeatureData &fd )
  1091. {
  1092. // grab connector texcoord register
  1093. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1094. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  1095. inTex->setName( "texCoord2" );
  1096. inTex->setStructName( "IN" );
  1097. inTex->setType( "vec2" );
  1098. // create texture var
  1099. Var *lightMap = new Var;
  1100. lightMap->setType( "sampler2D" );
  1101. lightMap->setName( "lightMap" );
  1102. lightMap->uniform = true;
  1103. lightMap->sampler = true;
  1104. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1105. // argh, pixel specular should prob use this too
  1106. if( fd.features[MFT_NormalMap] )
  1107. {
  1108. Var *lmColor = new Var;
  1109. lmColor->setName( "lmColor" );
  1110. lmColor->setType( "vec4" );
  1111. LangElement *lmColorDecl = new DecOp( lmColor );
  1112. output = new GenOp( " @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
  1113. return;
  1114. }
  1115. // Add realtime lighting, if it is available
  1116. LangElement *statement = NULL;
  1117. if( fd.features[MFT_RTLighting] )
  1118. {
  1119. // Advanced lighting is the only dynamic lighting supported right now
  1120. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1121. if(inColor != NULL)
  1122. {
  1123. // Find out if RTLighting should be added or substituted
  1124. bool bPreProcessedLighting = false;
  1125. AdvancedLightBinManager *lightBin;
  1126. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1127. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1128. // Lightmap has already been included in the advanced light bin, so
  1129. // no need to do any sampling or anything
  1130. if(bPreProcessedLighting)
  1131. statement = new GenOp( "vec4(@, 1.0)", inColor );
  1132. else
  1133. statement = new GenOp( "tex2D(@, @) + vec4(@.rgb, 0.0)", lightMap, inTex, inColor );
  1134. }
  1135. }
  1136. // If we still don't have it... then just sample the lightmap.
  1137. if ( !statement )
  1138. statement = new GenOp( "tex2D(@, @)", lightMap, inTex );
  1139. // Assign to proper render target
  1140. MultiLine *meta = new MultiLine;
  1141. if( fd.features[MFT_LightbufferMRT] )
  1142. {
  1143. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1144. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1145. }
  1146. else
  1147. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
  1148. output = meta;
  1149. }
  1150. ShaderFeature::Resources LightmapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1151. {
  1152. Resources res;
  1153. res.numTex = 1;
  1154. res.numTexReg = 1;
  1155. return res;
  1156. }
  1157. void LightmapFeatGLSL::setTexData( Material::StageData &stageDat,
  1158. const MaterialFeatureData &fd,
  1159. RenderPassData &passData,
  1160. U32 &texIndex )
  1161. {
  1162. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  1163. passData.mSamplerNames[ texIndex ] = "lightMap";
  1164. if ( tex )
  1165. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1166. else
  1167. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  1168. }
  1169. U32 LightmapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1170. {
  1171. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1172. }
  1173. //****************************************************************************
  1174. // Tonemap
  1175. //****************************************************************************
  1176. void TonemapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1177. const MaterialFeatureData &fd )
  1178. {
  1179. // Grab the connector
  1180. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1181. // Set up the second set of texCoords
  1182. Var *inTex2 = getVertTexCoord( "texCoord2" );
  1183. if ( inTex2 )
  1184. {
  1185. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  1186. outTex2->setName( "texCoord2" );
  1187. outTex2->setStructName( "OUT" );
  1188. outTex2->setType( "vec2" );
  1189. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  1190. }
  1191. }
  1192. void TonemapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1193. const MaterialFeatureData &fd )
  1194. {
  1195. // Grab connector
  1196. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1197. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  1198. inTex2->setName( "texCoord2" );
  1199. inTex2->setStructName( "IN" );
  1200. inTex2->setType( "vec2" );
  1201. // create texture var
  1202. Var *toneMap = new Var;
  1203. toneMap->setType( "sampler2D" );
  1204. toneMap->setName( "toneMap" );
  1205. toneMap->uniform = true;
  1206. toneMap->sampler = true;
  1207. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1208. MultiLine * meta = new MultiLine;
  1209. // First get the toneMap color
  1210. Var *toneMapColor = new Var;
  1211. toneMapColor->setType( "vec4" );
  1212. toneMapColor->setName( "toneMapColor" );
  1213. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  1214. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
  1215. // We do a different calculation if there is a diffuse map or not
  1216. Material::BlendOp blendOp = Material::Mul;
  1217. if ( fd.features[MFT_DiffuseMap] )
  1218. {
  1219. // Reverse the tonemap
  1220. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
  1221. // Re-tonemap with the current color factored in
  1222. blendOp = Material::ToneMap;
  1223. }
  1224. // Find out if RTLighting should be added
  1225. bool bPreProcessedLighting = false;
  1226. AdvancedLightBinManager *lightBin;
  1227. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1228. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1229. // Add in the realtime lighting contribution
  1230. if ( fd.features[MFT_RTLighting] )
  1231. {
  1232. // Right now, only Advanced Lighting is supported
  1233. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1234. if(inColor != NULL)
  1235. {
  1236. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1237. // the dynamic light buffer, and it already has the tonemap included
  1238. if(bPreProcessedLighting)
  1239. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  1240. else
  1241. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  1242. }
  1243. }
  1244. // Assign to proper render target
  1245. if( fd.features[MFT_LightbufferMRT] )
  1246. {
  1247. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1248. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1249. }
  1250. else
  1251. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  1252. output = meta;
  1253. }
  1254. ShaderFeature::Resources TonemapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1255. {
  1256. Resources res;
  1257. res.numTex = 1;
  1258. res.numTexReg = 1;
  1259. return res;
  1260. }
  1261. void TonemapFeatGLSL::setTexData( Material::StageData &stageDat,
  1262. const MaterialFeatureData &fd,
  1263. RenderPassData &passData,
  1264. U32 &texIndex )
  1265. {
  1266. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  1267. if ( tex )
  1268. {
  1269. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  1270. passData.mSamplerNames[ texIndex ] = "toneMap";
  1271. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1272. }
  1273. }
  1274. U32 TonemapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1275. {
  1276. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1277. }
  1278. //****************************************************************************
  1279. // pureLIGHT Lighting
  1280. //****************************************************************************
  1281. void VertLitGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1282. const MaterialFeatureData &fd )
  1283. {
  1284. // If we have a lightMap or toneMap then our lighting will be
  1285. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1286. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1287. {
  1288. output = NULL;
  1289. return;
  1290. }
  1291. // Create vertex color connector if it doesn't exist.
  1292. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1293. if( !outColor )
  1294. {
  1295. // Grab the connector color
  1296. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1297. outColor = connectComp->getElement( RT_COLOR );
  1298. outColor->setName( "vertColor" );
  1299. outColor->setStructName( "OUT" );
  1300. outColor->setType( "vec4" );
  1301. // Search for vert color
  1302. Var *inColor = (Var*) LangElement::find( "diffuse" );
  1303. // If there isn't a vertex color then we can't do anything
  1304. if( !inColor )
  1305. {
  1306. output = NULL;
  1307. return;
  1308. }
  1309. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1310. }
  1311. else
  1312. output = NULL; // Nothing we need to do.
  1313. }
  1314. void VertLitGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1315. const MaterialFeatureData &fd )
  1316. {
  1317. // If we have a lightMap or toneMap then our lighting will be
  1318. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1319. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1320. {
  1321. output = NULL;
  1322. return;
  1323. }
  1324. // Grab the connector color register
  1325. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1326. if( !vertColor )
  1327. {
  1328. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1329. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1330. vertColor = connectComp->getElement( RT_COLOR );
  1331. vertColor->setName( "vertColor" );
  1332. vertColor->setStructName( "IN" );
  1333. vertColor->setType( "vec4" );
  1334. }
  1335. MultiLine * meta = new MultiLine;
  1336. // Defaults (no diffuse map)
  1337. Material::BlendOp blendOp = Material::Mul;
  1338. LangElement *outColor = vertColor;
  1339. // We do a different calculation if there is a diffuse map or not
  1340. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1341. {
  1342. Var * finalVertColor = new Var;
  1343. finalVertColor->setName( "finalVertColor" );
  1344. finalVertColor->setType( "vec4" );
  1345. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1346. // Reverse the tonemap
  1347. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
  1348. // Set the blend op to tonemap
  1349. blendOp = Material::ToneMap;
  1350. outColor = finalVertColor;
  1351. }
  1352. // Add in the realtime lighting contribution, if applicable
  1353. if ( fd.features[MFT_RTLighting] )
  1354. {
  1355. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1356. if(rtLightingColor != NULL)
  1357. {
  1358. bool bPreProcessedLighting = false;
  1359. AdvancedLightBinManager *lightBin;
  1360. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1361. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1362. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1363. // the dynamic light buffer, and it already has the baked-vertex-color
  1364. // included in it
  1365. if(bPreProcessedLighting)
  1366. outColor = new GenOp( "vec4(@.rgb, 1.0)", rtLightingColor );
  1367. else
  1368. outColor = new GenOp( "vec4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
  1369. }
  1370. }
  1371. // Output the color
  1372. if ( fd.features[MFT_LightbufferMRT] )
  1373. {
  1374. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1375. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1376. }
  1377. else
  1378. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1379. output = meta;
  1380. }
  1381. U32 VertLitGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1382. {
  1383. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1384. }
  1385. //****************************************************************************
  1386. // Detail map
  1387. //****************************************************************************
  1388. void DetailFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1389. const MaterialFeatureData &fd )
  1390. {
  1391. MultiLine *meta = new MultiLine;
  1392. addOutDetailTexCoord( componentList,
  1393. meta,
  1394. fd.features[MFT_TexAnim], fd.features[MFT_Foliage]);
  1395. output = meta;
  1396. }
  1397. void DetailFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1398. const MaterialFeatureData &fd )
  1399. {
  1400. // Get the detail texture coord.
  1401. Var *inTex = getInTexCoord( "detCoord", "vec2", componentList );
  1402. // create texture var
  1403. Var *detailMap = new Var;
  1404. detailMap->setType( "sampler2D" );
  1405. detailMap->setName( "detailMap" );
  1406. detailMap->uniform = true;
  1407. detailMap->sampler = true;
  1408. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1409. // We're doing the standard greyscale detail map
  1410. // technique which can darken and lighten the
  1411. // diffuse texture.
  1412. // TODO: We could add a feature to toggle between this
  1413. // and a simple multiplication with the detail map.
  1414. LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
  1415. if ( fd.features[MFT_isDeferred])
  1416. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add, NULL, ShaderFeature::RenderTarget1 ) );
  1417. else
  1418. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1419. }
  1420. ShaderFeature::Resources DetailFeatGLSL::getResources( const MaterialFeatureData &fd )
  1421. {
  1422. Resources res;
  1423. res.numTex = 1;
  1424. res.numTexReg = 1;
  1425. return res;
  1426. }
  1427. void DetailFeatGLSL::setTexData( Material::StageData &stageDat,
  1428. const MaterialFeatureData &fd,
  1429. RenderPassData &passData,
  1430. U32 &texIndex )
  1431. {
  1432. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1433. if ( tex )
  1434. {
  1435. passData.mSamplerNames[texIndex] = "detailMap";
  1436. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1437. }
  1438. }
  1439. //****************************************************************************
  1440. // Vertex position
  1441. //****************************************************************************
  1442. void VertPositionGLSL::determineFeature( Material *material,
  1443. const GFXVertexFormat *vertexFormat,
  1444. U32 stageNum,
  1445. const FeatureType &type,
  1446. const FeatureSet &features,
  1447. MaterialFeatureData *outFeatureData )
  1448. {
  1449. // This feature is always on!
  1450. outFeatureData->features.addFeature( type );
  1451. }
  1452. void VertPositionGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1453. const MaterialFeatureData &fd )
  1454. {
  1455. // First check for an input position from a previous feature
  1456. // then look for the default vertex position.
  1457. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1458. if ( !inPosition )
  1459. inPosition = (Var*)LangElement::find( "position" );
  1460. // grab connector position
  1461. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1462. Var *outPosition = connectComp->getElement( RT_POSITION );
  1463. outPosition->setName( "gl_Position" );
  1464. MultiLine *meta = new MultiLine;
  1465. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1466. meta->addStatement( new GenOp( " @ = tMul(@, vec4(@.xyz,1));\r\n",
  1467. outPosition, modelview, inPosition ) );
  1468. if (fd.materialFeatures[MFT_isBackground])
  1469. {
  1470. meta->addStatement(new GenOp(" @ = @.xyww;\r\n", outPosition, outPosition));
  1471. }
  1472. output = meta;
  1473. }
  1474. //****************************************************************************
  1475. // Reflect Cubemap
  1476. //****************************************************************************
  1477. void ReflectCubeFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1478. const MaterialFeatureData &fd )
  1479. {
  1480. // search for vert normal
  1481. Var *inNormal = (Var*) LangElement::find( "normal" );
  1482. if ( !inNormal )
  1483. return;
  1484. MultiLine * meta = new MultiLine;
  1485. // If a base or bump tex is present in the material, but not in the
  1486. // current pass - we need to add one to the current pass to use
  1487. // its alpha channel as a gloss map. Here we just need the tex coords.
  1488. if( !fd.features[MFT_DiffuseMap] &&
  1489. !fd.features[MFT_NormalMap] )
  1490. {
  1491. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1492. fd.materialFeatures[MFT_NormalMap] )
  1493. {
  1494. // find incoming texture var
  1495. Var *inTex = getVertTexCoord( "texCoord" );
  1496. // grab connector texcoord register
  1497. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1498. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1499. outTex->setName( "texCoord" );
  1500. outTex->setStructName( "OUT" );
  1501. outTex->setType( "vec2" );
  1502. // setup language elements to output incoming tex coords to output
  1503. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1504. }
  1505. }
  1506. // create cubeTrans
  1507. bool useInstancing = fd.features[MFT_UseInstancing];
  1508. Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
  1509. // cube vert position
  1510. Var * cubeVertPos = new Var;
  1511. cubeVertPos->setName( "cubeVertPos" );
  1512. cubeVertPos->setType( "vec3" );
  1513. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1514. meta->addStatement( new GenOp( " @ = tMul( @, float4(@,1)).xyz;\r\n",
  1515. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1516. // cube normal
  1517. Var * cubeNormal = new Var;
  1518. cubeNormal->setName( "cubeNormal" );
  1519. cubeNormal->setType( "vec3" );
  1520. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1521. meta->addStatement( new GenOp( " @ = ( tMul( (@), vec4(@, 0) ) ).xyz;\r\n",
  1522. cubeNormDecl, cubeTrans, inNormal ) );
  1523. meta->addStatement( new GenOp( " @ = bool(length(@)) ? normalize(@) : @;\r\n",
  1524. cubeNormal, cubeNormal, cubeNormal, cubeNormal ) );
  1525. // grab the eye position
  1526. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1527. if ( !eyePos )
  1528. {
  1529. eyePos = new Var( "eyePosWorld", "vec3" );
  1530. eyePos->uniform = true;
  1531. eyePos->constSortPos = cspPass;
  1532. }
  1533. // eye to vert
  1534. Var * eyeToVert = new Var;
  1535. eyeToVert->setName( "eyeToVert" );
  1536. eyeToVert->setType( "vec3" );
  1537. LangElement *e2vDecl = new DecOp( eyeToVert );
  1538. meta->addStatement( new GenOp( " @ = @ - @;\r\n",
  1539. e2vDecl, cubeVertPos, eyePos ) );
  1540. // grab connector texcoord register
  1541. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1542. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1543. reflectVec->setName( "reflectVec" );
  1544. reflectVec->setStructName( "OUT" );
  1545. reflectVec->setType( "vec3" );
  1546. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1547. output = meta;
  1548. }
  1549. void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1550. const MaterialFeatureData &fd )
  1551. {
  1552. MultiLine * meta = new MultiLine;
  1553. Var *glossColor = NULL;
  1554. // If a base or bump tex is present in the material, but not in the
  1555. // current pass - we need to add one to the current pass to use
  1556. // its alpha channel as a gloss map.
  1557. if( !fd.features[MFT_DiffuseMap] &&
  1558. !fd.features[MFT_NormalMap] )
  1559. {
  1560. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1561. fd.materialFeatures[MFT_NormalMap] )
  1562. {
  1563. // grab connector texcoord register
  1564. Var *inTex = getInTexCoord( "texCoord", "vec2", componentList );
  1565. // create texture var
  1566. Var *newMap = new Var;
  1567. newMap->setType( "sampler2D" );
  1568. newMap->setName( "glossMap" );
  1569. newMap->uniform = true;
  1570. newMap->sampler = true;
  1571. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1572. // create sample color
  1573. Var *color = new Var;
  1574. color->setType( "vec4" );
  1575. color->setName( "diffuseColor" );
  1576. LangElement *colorDecl = new DecOp( color );
  1577. glossColor = color;
  1578. meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
  1579. }
  1580. }
  1581. if (!glossColor)
  1582. {
  1583. if (fd.features[MFT_isDeferred])
  1584. glossColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1585. if (!glossColor)
  1586. glossColor = (Var*)LangElement::find("diffuseColor");
  1587. if (!glossColor)
  1588. glossColor = (Var*)LangElement::find("bumpNormal");
  1589. }
  1590. // grab connector texcoord register
  1591. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1592. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1593. reflectVec->setName( "reflectVec" );
  1594. reflectVec->setStructName( "IN" );
  1595. reflectVec->setType( "vec3" );
  1596. // create cubemap var
  1597. Var *cubeMap = new Var;
  1598. cubeMap->setType( "samplerCube" );
  1599. cubeMap->setName( "cubeMap" );
  1600. cubeMap->uniform = true;
  1601. cubeMap->sampler = true;
  1602. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1603. Var *cubeMips = new Var;
  1604. cubeMips->setType("float");
  1605. cubeMips->setName("cubeMips");
  1606. cubeMips->uniform = true;
  1607. cubeMips->constSortPos = cspPotentialPrimitive;
  1608. // TODO: Restore the lighting attenuation here!
  1609. Var *attn = NULL;
  1610. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1611. //attn = (Var*)LangElement::find("attn");
  1612. //else
  1613. if ( fd.materialFeatures[MFT_RTLighting] )
  1614. attn =(Var*)LangElement::find("d_NL_Att");
  1615. LangElement *texCube = NULL;
  1616. Var* matinfo = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  1617. Var *roughness = (Var*)LangElement::find("roughness");
  1618. if (roughness) //try to grab roughness directly
  1619. texCube = new GenOp("textureLod( @, @, min((1.0 - @)*@ + 1.0, @))", cubeMap, reflectVec, roughness, cubeMips, cubeMips);
  1620. else if (glossColor) //failing that, try and find color data
  1621. texCube = new GenOp("textureLod( @, @, min((1.0 - @.b)*@ + 1.0, @))", cubeMap, reflectVec, glossColor, cubeMips, cubeMips);
  1622. else //failing *that*, just draw the cubemap
  1623. texCube = new GenOp("texture( @, @)", cubeMap, reflectVec);
  1624. LangElement *lerpVal = NULL;
  1625. Material::BlendOp blendOp = Material::LerpAlpha;
  1626. // Note that the lerpVal needs to be a float4 so that
  1627. // it will work with the LerpAlpha blend.
  1628. if (matinfo)
  1629. {
  1630. if (attn)
  1631. lerpVal = new GenOp("@ * saturate( @ )", matinfo, attn);
  1632. else
  1633. lerpVal = new GenOp("@", matinfo);
  1634. }
  1635. else if ( glossColor )
  1636. {
  1637. if ( attn )
  1638. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1639. else
  1640. lerpVal = glossColor;
  1641. }
  1642. else
  1643. {
  1644. if ( attn )
  1645. lerpVal = new GenOp( "vec4( saturate( @ ) ).xxxx", attn );
  1646. else
  1647. blendOp = Material::Mul;
  1648. }
  1649. Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  1650. if (fd.features[MFT_isDeferred])
  1651. {
  1652. //metalness: black(0) = color, white(1) = reflection
  1653. if (fd.features[MFT_ToneMap])
  1654. meta->addStatement(new GenOp(" @ *= @;\r\n", targ, texCube));
  1655. else
  1656. meta->addStatement(new GenOp(" @ = @;\r\n", targ, texCube));
  1657. }
  1658. else
  1659. {
  1660. meta->addStatement(new GenOp(" //forward lit cubemapping\r\n"));
  1661. targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  1662. Var *metalness = (Var*)LangElement::find("metalness");
  1663. if (metalness)
  1664. {
  1665. Var *dColor = new Var("dColor", "vec3");
  1666. Var *envColor = new Var("envColor", "vec3");
  1667. meta->addStatement(new GenOp(" @ = @.rgb - (@.rgb * @);\r\n", new DecOp(dColor), targ, targ, metalness));
  1668. meta->addStatement(new GenOp(" @ = @.rgb*(@).rgb;\r\n", new DecOp(envColor), targ, texCube));
  1669. }
  1670. else if (lerpVal)
  1671. meta->addStatement(new GenOp(" @ *= vec4(@.rgb*@.a, @.a);\r\n", targ, texCube, lerpVal, targ));
  1672. else
  1673. meta->addStatement(new GenOp(" @.rgb *= @.rgb;\r\n", targ, texCube));
  1674. }
  1675. output = meta;
  1676. }
  1677. ShaderFeature::Resources ReflectCubeFeatGLSL::getResources( const MaterialFeatureData &fd )
  1678. {
  1679. Resources res;
  1680. if( fd.features[MFT_DiffuseMap] ||
  1681. fd.features[MFT_NormalMap] )
  1682. {
  1683. res.numTex = 1;
  1684. res.numTexReg = 1;
  1685. }
  1686. else
  1687. {
  1688. res.numTex = 2;
  1689. res.numTexReg = 2;
  1690. }
  1691. return res;
  1692. }
  1693. void ReflectCubeFeatGLSL::setTexData( Material::StageData &stageDat,
  1694. const MaterialFeatureData &stageFeatures,
  1695. RenderPassData &passData,
  1696. U32 &texIndex )
  1697. {
  1698. // set up a gloss map if one is not present in the current pass
  1699. // but is present in the current material stage
  1700. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1701. !passData.mFeatureData.features[MFT_NormalMap] )
  1702. {
  1703. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1704. if ( tex && stageFeatures.features[MFT_DiffuseMap] )
  1705. {
  1706. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  1707. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1708. }
  1709. else
  1710. {
  1711. tex = stageDat.getTex( MFT_NormalMap );
  1712. if ( tex && stageFeatures.features[ MFT_NormalMap ] )
  1713. {
  1714. passData.mSamplerNames[ texIndex ] = "bumpMap";
  1715. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1716. }
  1717. }
  1718. }
  1719. if( stageDat.getCubemap() )
  1720. {
  1721. passData.mCubeMap = stageDat.getCubemap();
  1722. passData.mSamplerNames[texIndex] = "cubeMap";
  1723. passData.mTexType[texIndex++] = Material::Cube;
  1724. }
  1725. else
  1726. {
  1727. if( stageFeatures.features[MFT_CubeMap] )
  1728. {
  1729. // assuming here that it is a scenegraph cubemap
  1730. passData.mSamplerNames[texIndex] = "cubeMap";
  1731. passData.mTexType[texIndex++] = Material::SGCube;
  1732. }
  1733. }
  1734. }
  1735. //****************************************************************************
  1736. // RTLighting
  1737. //****************************************************************************
  1738. RTLightingFeatGLSL::RTLightingFeatGLSL()
  1739. : mDep(ShaderGen::smCommonShaderPath + String("/gl/lighting.glsl" ))
  1740. {
  1741. addDependency( &mDep );
  1742. }
  1743. void RTLightingFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1744. const MaterialFeatureData &fd )
  1745. {
  1746. if (fd.features[MFT_ImposterVert]) return;
  1747. MultiLine *meta = new MultiLine;
  1748. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1749. // Special case for lighting imposters. We dont have a vert normal and may not
  1750. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1751. if ( fd.features[MFT_ImposterVert] )
  1752. {
  1753. if ( !fd.features[MFT_NormalMap] )
  1754. {
  1755. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1756. if ( !eyePos )
  1757. {
  1758. eyePos = new Var( "eyePosWorld", "vec3" );
  1759. eyePos->uniform = true;
  1760. eyePos->constSortPos = cspPass;
  1761. }
  1762. //Temporarily disabled while we figure out how to better handle normals without a normal map
  1763. /*Var *inPosition = (Var*)LangElement::find( "position" );
  1764. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1765. outNormal->setName( "wsNormal" );
  1766. outNormal->setStructName( "OUT" );
  1767. outNormal->setType( "vec3" );
  1768. // Transform the normal to world space.
  1769. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );*/
  1770. }
  1771. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1772. output = meta;
  1773. return;
  1774. }
  1775. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1776. getOutWorldToTangent(componentList, meta, fd);
  1777. output = meta;
  1778. // Find the incoming vertex normal.
  1779. Var *inNormal = (Var*)LangElement::find( "normal" );
  1780. if ( !inNormal ||
  1781. fd.features[MFT_LightMap] ||
  1782. fd.features[MFT_ToneMap] ||
  1783. fd.features[MFT_VertLit] )
  1784. return;
  1785. // If there isn't a normal map then we need to pass
  1786. // the world space normal to the pixel shader ourselves.
  1787. //Temporarily disabled while we figure out how to better handle normals without a normal map
  1788. /*if ( !fd.features[MFT_NormalMap] )
  1789. {
  1790. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1791. outNormal->setName( "wsNormal" );
  1792. outNormal->setStructName( "OUT" );
  1793. outNormal->setType( "vec3" );
  1794. // Get the transform to world space.
  1795. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1796. // Transform the normal to world space.
  1797. meta->addStatement( new GenOp( " @ = tMul( @, vec4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1798. }*/
  1799. }
  1800. void RTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1801. const MaterialFeatureData &fd )
  1802. {
  1803. // Skip out on realtime lighting if we don't have a normal
  1804. // or we're doing some sort of baked lighting.
  1805. //
  1806. // TODO: We can totally detect for this in the material
  1807. // feature setup... we should move it out of here!
  1808. //
  1809. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1810. return;
  1811. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1812. MultiLine *meta = new MultiLine;
  1813. // Now the wsPosition and wsView.
  1814. Var *wsPosition = getInWsPosition( componentList );
  1815. Var* worldToTangent = getInWorldToTangent(componentList);
  1816. Var* wsNormal = getInWorldNormal(componentList);
  1817. Var *wsView = getWsView( wsPosition, meta );
  1818. // Look for a light mask generated from a previous
  1819. // feature (this is done for BL terrain lightmaps).
  1820. LangElement *lightMask = LangElement::find( "lightMask" );
  1821. if ( !lightMask )
  1822. lightMask = new GenOp( "vec4( 1, 1, 1, 1 )" );
  1823. // Get all the light constants.
  1824. Var *inLightPos = new Var( "inLightPos", "vec4" );
  1825. inLightPos->uniform = true;
  1826. inLightPos->arraySize = 4;
  1827. inLightPos->constSortPos = cspPotentialPrimitive;
  1828. Var * inLightConfigData = new Var( "inLightConfigData", "vec4" );
  1829. inLightConfigData->uniform = true;
  1830. inLightConfigData->arraySize = 4;
  1831. inLightConfigData->constSortPos = cspPotentialPrimitive;
  1832. Var *inLightColor = new Var( "inLightColor", "vec4" );
  1833. inLightColor->uniform = true;
  1834. inLightColor->arraySize = 4;
  1835. inLightColor->constSortPos = cspPotentialPrimitive;
  1836. Var *inLightSpotDir = new Var( "inLightSpotDir", "vec4" );
  1837. inLightSpotDir->uniform = true;
  1838. inLightSpotDir->arraySize = 4;
  1839. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1840. Var * lightSpotParams = new Var( "lightSpotParams", "vec2" );
  1841. lightSpotParams->uniform = true;
  1842. lightSpotParams->arraySize = 4;
  1843. lightSpotParams->constSortPos = cspPotentialPrimitive;
  1844. Var* hasVectorLight = new Var("hasVectorLight", "int");
  1845. hasVectorLight->uniform = true;
  1846. hasVectorLight->constSortPos = cspPotentialPrimitive;
  1847. Var* vectorLightDirection = new Var("vectorLightDirection", "vec4");
  1848. vectorLightDirection->uniform = true;
  1849. vectorLightDirection->constSortPos = cspPotentialPrimitive;
  1850. Var* vectorLightColor = new Var("vectorLightColor", "vec4");
  1851. vectorLightColor->uniform = true;
  1852. vectorLightColor->constSortPos = cspPotentialPrimitive;
  1853. Var* vectorLightBrightness = new Var("vectorLightBrightness", "float");
  1854. vectorLightBrightness->uniform = true;
  1855. vectorLightBrightness->constSortPos = cspPotentialPrimitive;
  1856. Var* surface = getSurface(componentList, meta, fd);
  1857. if (!surface)
  1858. {
  1859. Con::errorf("ShaderGen::RTLightingFeatGLSL() - failed to generate surface!");
  1860. return;
  1861. }
  1862. Var *roughness = (Var*)LangElement::find("roughness");
  1863. Var *metalness = (Var*)LangElement::find("metalness");
  1864. Var *curColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  1865. Var *ambient = new Var( "ambient", "vec4" );
  1866. ambient->uniform = true;
  1867. ambient->constSortPos = cspPass;
  1868. Var* lighting = new Var("lighting", "vec4");
  1869. meta->addStatement(new GenOp(" @ = compute4Lights( @, @, @, @,\r\n"
  1870. " @, @, @, @, @, @, @);\r\n",
  1871. new DecOp(lighting), surface, lightMask, inLightPos, inLightConfigData, inLightColor, inLightSpotDir, lightSpotParams,
  1872. hasVectorLight, vectorLightDirection, vectorLightColor, vectorLightBrightness));
  1873. meta->addStatement(new GenOp(" @.rgb += @.rgb;\r\n", curColor, lighting));
  1874. output = meta;
  1875. }
  1876. ShaderFeature::Resources RTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
  1877. {
  1878. Resources res;
  1879. // These features disable realtime lighting.
  1880. if ( !fd.features[MFT_LightMap] &&
  1881. !fd.features[MFT_ToneMap] &&
  1882. !fd.features[MFT_VertLit] )
  1883. {
  1884. // If enabled we pass the position.
  1885. res.numTexReg = 1;
  1886. // If there isn't a bump map then we pass the
  1887. // world space normal as well.
  1888. if ( !fd.features[MFT_NormalMap] )
  1889. res.numTexReg++;
  1890. }
  1891. return res;
  1892. }
  1893. //****************************************************************************
  1894. // Fog
  1895. //****************************************************************************
  1896. FogFeatGLSL::FogFeatGLSL()
  1897. : mFogDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
  1898. {
  1899. addDependency( &mFogDep );
  1900. }
  1901. void FogFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1902. const MaterialFeatureData &fd )
  1903. {
  1904. MultiLine *meta = new MultiLine;
  1905. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1906. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1907. {
  1908. // Grab the eye position.
  1909. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1910. if ( !eyePos )
  1911. {
  1912. eyePos = new Var( "eyePosWorld", "vec3" );
  1913. eyePos->uniform = true;
  1914. eyePos->constSortPos = cspPass;
  1915. }
  1916. Var *fogData = new Var( "fogData", "vec3" );
  1917. fogData->uniform = true;
  1918. fogData->constSortPos = cspPass;
  1919. Var *wsPosition = new Var( "fogPos", "vec3" );
  1920. getWsPosition( componentList,
  1921. fd.features[MFT_UseInstancing],
  1922. meta,
  1923. new DecOp( wsPosition ) );
  1924. // We pass the fog amount to the pixel shader.
  1925. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1926. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1927. fogAmount->setName( "fogAmount" );
  1928. fogAmount->setStructName( "OUT" );
  1929. fogAmount->setType( "float" );
  1930. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1931. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1932. }
  1933. else
  1934. {
  1935. // We fog in world space... make sure the world space
  1936. // position is passed to the pixel shader. This is
  1937. // often already passed for lighting, so it takes up
  1938. // no extra output registers.
  1939. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1940. }
  1941. output = meta;
  1942. }
  1943. void FogFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1944. const MaterialFeatureData &fd )
  1945. {
  1946. MultiLine *meta = new MultiLine;
  1947. Var *fogColor = new Var;
  1948. fogColor->setType( "vec4" );
  1949. fogColor->setName( "fogColor" );
  1950. fogColor->uniform = true;
  1951. fogColor->constSortPos = cspPass;
  1952. // Get the out color.
  1953. Var *color = (Var*) LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  1954. if ( !color )
  1955. {
  1956. color = new Var;
  1957. color->setType( "vec4" );
  1958. color->setName( "col" );
  1959. color->setStructName("OUT");
  1960. }
  1961. Var *fogAmount;
  1962. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1963. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1964. {
  1965. // Per-vertex.... just get the fog amount.
  1966. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1967. fogAmount = connectComp->getElement( RT_TEXCOORD );
  1968. fogAmount->setName( "fogAmount" );
  1969. fogAmount->setStructName( "IN" );
  1970. fogAmount->setType( "float" );
  1971. }
  1972. else
  1973. {
  1974. Var *wsPosition = getInWsPosition( componentList );
  1975. // grab the eye position
  1976. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1977. if ( !eyePos )
  1978. {
  1979. eyePos = new Var( "eyePosWorld", "vec3" );
  1980. eyePos->uniform = true;
  1981. eyePos->constSortPos = cspPass;
  1982. }
  1983. Var *fogData = new Var( "fogData", "vec3" );
  1984. fogData->uniform = true;
  1985. fogData->constSortPos = cspPass;
  1986. /// Get the fog amount.
  1987. fogAmount = new Var( "fogAmount", "float" );
  1988. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1989. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  1990. }
  1991. // Lerp between the fog color and diffuse color.
  1992. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  1993. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  1994. output = meta;
  1995. }
  1996. ShaderFeature::Resources FogFeatGLSL::getResources( const MaterialFeatureData &fd )
  1997. {
  1998. Resources res;
  1999. res.numTexReg = 1;
  2000. return res;
  2001. }
  2002. //****************************************************************************
  2003. // Visibility
  2004. //****************************************************************************
  2005. VisibilityFeatGLSL::VisibilityFeatGLSL()
  2006. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
  2007. {
  2008. addDependency( &mTorqueDep );
  2009. }
  2010. void VisibilityFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2011. const MaterialFeatureData &fd )
  2012. {
  2013. MultiLine *meta = new MultiLine;
  2014. output = meta;
  2015. if ( fd.features[ MFT_UseInstancing ] )
  2016. {
  2017. // We pass the visibility to the pixel shader via
  2018. // another output register.
  2019. //
  2020. // TODO: We should see if we can share this register
  2021. // with some other common instanced data.
  2022. //
  2023. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2024. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  2025. outVisibility->setStructName( "OUT" );
  2026. outVisibility->setName( "visibility" );
  2027. outVisibility->setType( "float" );
  2028. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  2029. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  2030. instVisibility->setStructName( "IN" );
  2031. instVisibility->setName( "inst_visibility" );
  2032. instVisibility->setType( "float" );
  2033. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  2034. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  2035. }
  2036. if ( fd.features[ MFT_IsTranslucent ] )
  2037. return;
  2038. addOutVpos( meta, componentList );
  2039. }
  2040. void VisibilityFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2041. const MaterialFeatureData &fd )
  2042. {
  2043. // Get the visibility constant.
  2044. Var *visibility = NULL;
  2045. if ( fd.features[ MFT_UseInstancing ] )
  2046. visibility = getInTexCoord( "visibility", "float", componentList );
  2047. else
  2048. {
  2049. visibility = (Var*)LangElement::find( "visibility" );
  2050. if ( !visibility )
  2051. {
  2052. visibility = new Var();
  2053. visibility->setType( "float" );
  2054. visibility->setName( "visibility" );
  2055. visibility->uniform = true;
  2056. visibility->constSortPos = cspPotentialPrimitive;
  2057. }
  2058. }
  2059. MultiLine* meta = new MultiLine;
  2060. output = meta;
  2061. // Translucent objects do a simple alpha fade.
  2062. if ( fd.features[ MFT_IsTranslucent ] )
  2063. {
  2064. Var *color = (Var*) LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2065. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  2066. return;
  2067. }
  2068. // Everything else does a fizzle.
  2069. Var *vPos = getInVpos( meta, componentList );
  2070. meta->addStatement( new GenOp( " fizzle( @.xy, @ );\r\n", vPos, visibility ) );
  2071. }
  2072. ShaderFeature::Resources VisibilityFeatGLSL::getResources( const MaterialFeatureData &fd )
  2073. {
  2074. Resources res;
  2075. // TODO: Fix for instancing.
  2076. if ( !fd.features[ MFT_IsTranslucent ] )
  2077. res.numTexReg = 1;
  2078. return res;
  2079. }
  2080. //****************************************************************************
  2081. // AlphaTest
  2082. //****************************************************************************
  2083. void AlphaTestGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2084. const MaterialFeatureData &fd )
  2085. {
  2086. // If we're below SM3 and don't have a depth output
  2087. // feature then don't waste an instruction here.
  2088. if (( GFX->getPixelShaderVersion() < 3.0 &&
  2089. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  2090. !fd.features[ MFT_DepthOut ] ) ||
  2091. fd.features[MFT_IsTranslucent])
  2092. {
  2093. output = NULL;
  2094. return;
  2095. }
  2096. // If we don't have a color var then we cannot do an alpha test.
  2097. Var *color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1) );
  2098. if ( !color )
  2099. color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2100. if ( !color )
  2101. {
  2102. output = NULL;
  2103. return;
  2104. }
  2105. // Now grab the alpha test value.
  2106. Var *alphaTestVal = new Var;
  2107. alphaTestVal->setType( "float" );
  2108. alphaTestVal->setName( "alphaTestValue" );
  2109. alphaTestVal->uniform = true;
  2110. alphaTestVal->constSortPos = cspPotentialPrimitive;
  2111. // Do the clip.
  2112. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  2113. }
  2114. //****************************************************************************
  2115. // GlowMask
  2116. //****************************************************************************
  2117. void GlowMaskGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2118. const MaterialFeatureData &fd )
  2119. {
  2120. output = NULL;
  2121. // Get the output color... and make it black to mask out
  2122. // glow passes rendered before us.
  2123. //
  2124. // The shader compiler will optimize out all the other
  2125. // code above that doesn't contribute to the alpha mask.
  2126. Var *color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2127. if ( color )
  2128. output = new GenOp( " @.rgb = vec3(0);\r\n", color );
  2129. }
  2130. //****************************************************************************
  2131. // RenderTargetZero
  2132. //****************************************************************************
  2133. void RenderTargetZeroGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  2134. {
  2135. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  2136. // This will prevent a divide by zero causing an FP special on float render targets
  2137. output = new GenOp( " @;\r\n", assignColor( new GenOp( "vec4(0.00001)" ), Material::None, NULL, mOutputTargetMask ) );
  2138. }
  2139. //****************************************************************************
  2140. // HDR Output
  2141. //****************************************************************************
  2142. HDROutGLSL::HDROutGLSL()
  2143. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
  2144. {
  2145. addDependency( &mTorqueDep );
  2146. }
  2147. void HDROutGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2148. const MaterialFeatureData &fd )
  2149. {
  2150. // Let the helper function do the work.
  2151. Var *color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2152. if ( color )
  2153. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2154. }
  2155. //****************************************************************************
  2156. // FoliageFeatureGLSL
  2157. //****************************************************************************
  2158. #include "T3D/fx/groundCover.h"
  2159. FoliageFeatureGLSL::FoliageFeatureGLSL()
  2160. : mDep(ShaderGen::smCommonShaderPath + String("/gl/foliage.glsl" ))
  2161. {
  2162. addDependency( &mDep );
  2163. }
  2164. void FoliageFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2165. const MaterialFeatureData &fd )
  2166. {
  2167. // Get the input variables we need.
  2168. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2169. if ( !inPosition )
  2170. inPosition = (Var*)LangElement::find( "position" );
  2171. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2172. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2173. MultiLine *meta = new MultiLine;
  2174. // Declare the normal and tangent variables since they do not exist
  2175. // in this vert type, but we do need to set them up for others.
  2176. Var *normal = (Var*)LangElement::find( "normal" );
  2177. AssertFatal( normal, "FoliageFeatureGLSL requires vert normal!" );
  2178. Var *tangent = new Var;
  2179. tangent->setType( "vec3" );
  2180. tangent->setName( "T" );
  2181. LangElement *tangentDec = new DecOp( tangent );
  2182. meta->addStatement( new GenOp( " @ = vec3(1.0,0,0);\n", tangentDec ) );
  2183. // We add a float foliageFade to the OUT structure.
  2184. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2185. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2186. fade->setName( "foliageFade" );
  2187. fade->setStructName( "OUT" );
  2188. fade->setType( "float" );
  2189. // grab the eye position
  2190. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2191. if ( !eyePos )
  2192. {
  2193. eyePos = new Var( "eyePosWorld", "vec3" );
  2194. eyePos->uniform = true;
  2195. eyePos->constSortPos = cspPass;
  2196. }
  2197. // All actual work is offloaded to this method.
  2198. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2199. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2200. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2201. output = meta;
  2202. }
  2203. void FoliageFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2204. const MaterialFeatureData &fd )
  2205. {
  2206. // Find / create IN.foliageFade
  2207. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2208. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2209. fade->setName( "foliageFade" );
  2210. fade->setStructName( "IN" );
  2211. fade->setType( "float" );
  2212. // Find / create visibility
  2213. Var *visibility = (Var*) LangElement::find( "visibility" );
  2214. if ( !visibility )
  2215. {
  2216. visibility = new Var();
  2217. visibility->setType( "float" );
  2218. visibility->setName( "visibility" );
  2219. visibility->uniform = true;
  2220. visibility->constSortPos = cspPotentialPrimitive;
  2221. }
  2222. MultiLine *meta = new MultiLine;
  2223. // Multiply foliageFade into visibility.
  2224. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2225. output = meta;
  2226. }
  2227. void FoliageFeatureGLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2228. {
  2229. // This isn't really necessary since the outFeatureData will be filtered after
  2230. // this call.
  2231. if ( features.hasFeature( MFT_Foliage ) )
  2232. outFeatureData->features.addFeature( type );
  2233. }
  2234. ShaderFeatureConstHandles* FoliageFeatureGLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2235. {
  2236. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2237. AssertFatal( gcover != NULL, "FoliageFeatureGLSL::createConstHandles - userObject was not valid!" );
  2238. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2239. handles->mGroundCover = gcover;
  2240. handles->init( shader );
  2241. return handles;
  2242. }
  2243. void ParticleNormalFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2244. {
  2245. MultiLine *meta = new MultiLine;
  2246. output = meta;
  2247. // Calculate normal and tangent values since we want to keep particle verts
  2248. // as light-weight as possible
  2249. Var *normal = (Var*) LangElement::find("normal");
  2250. if(normal == NULL)
  2251. {
  2252. normal = new Var;
  2253. normal->setType( "vec3" );
  2254. normal->setName( "normal" );
  2255. // These values are not accidental. It is slightly adjusted from facing straight into the
  2256. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2257. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2258. // [11/23/2009 Pat]
  2259. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2260. }
  2261. Var *T = (Var*) LangElement::find( "T" );
  2262. if(T == NULL)
  2263. {
  2264. T = new Var;
  2265. T->setType( "vec3" );
  2266. T->setName( "T" );
  2267. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2268. }
  2269. }
  2270. //****************************************************************************
  2271. // ImposterVertFeatureGLSL
  2272. //****************************************************************************
  2273. ImposterVertFeatureGLSL::ImposterVertFeatureGLSL()
  2274. : mDep(ShaderGen::smCommonShaderPath + String("/gl/imposter.glsl" ))
  2275. {
  2276. addDependency( &mDep );
  2277. }
  2278. void ImposterVertFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2279. const MaterialFeatureData &fd )
  2280. {
  2281. MultiLine *meta = new MultiLine;
  2282. output = meta;
  2283. // Get the input vertex variables.
  2284. Var *inPosition = (Var*)LangElement::find( "position" );
  2285. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2286. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2287. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2288. // Get the input shader constants.
  2289. Var *imposterLimits = new Var;
  2290. imposterLimits->setType( "vec4" );
  2291. imposterLimits->setName( "imposterLimits" );
  2292. imposterLimits->uniform = true;
  2293. imposterLimits->constSortPos = cspPotentialPrimitive;
  2294. Var *imposterUVs = new Var;
  2295. imposterUVs->setType( "vec4" );
  2296. imposterUVs->setName( "imposterUVs" );
  2297. imposterUVs->arraySize = 64; // See imposter.glsl
  2298. imposterUVs->uniform = true;
  2299. imposterUVs->constSortPos = cspPotentialPrimitive;
  2300. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2301. if ( !eyePos )
  2302. {
  2303. eyePos = new Var( "eyePosWorld", "vec3" );
  2304. eyePos->uniform = true;
  2305. eyePos->constSortPos = cspPass;
  2306. }
  2307. // Declare the outputs from this feature.
  2308. Var *outInPosition = new Var;
  2309. outInPosition->setType( "vec3" );
  2310. outInPosition->setName( "inPosition" );
  2311. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2312. Var *outTexCoord = new Var;
  2313. outTexCoord->setType( "vec2" );
  2314. outTexCoord->setName( "texCoord" );
  2315. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2316. Var *outWorldToTangent = new Var;
  2317. outWorldToTangent->setType( "float3x3" );
  2318. outWorldToTangent->setName( "worldToTangent" );
  2319. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2320. // Add imposterFade to the OUT structure.
  2321. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2322. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2323. outFade->setName( "imposterFade" );
  2324. outFade->setStructName( "OUT" );
  2325. outFade->setType( "float" );
  2326. // Assign OUT.imposterFade
  2327. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2328. // All actual work is done in this method.
  2329. meta->addStatement( new GenOp( " imposter_v( @.xyz, int(@.w), @.x * length(@), normalize(@), normalize(@), int(@.y), int(@.x), @.z, bool(@.w), @, @, @, @, @ );\r\n",
  2330. inPosition,
  2331. inPosition,
  2332. inMiscParams,
  2333. inRightVec,
  2334. inUpVec,
  2335. inRightVec,
  2336. imposterLimits,
  2337. imposterLimits,
  2338. imposterLimits,
  2339. imposterLimits,
  2340. eyePos,
  2341. imposterUVs,
  2342. outInPosition,
  2343. outTexCoord,
  2344. outWorldToTangent ) );
  2345. // Copy the position to wsPosition for use in shaders
  2346. // down stream instead of looking for objTrans.
  2347. Var *wsPosition = new Var;
  2348. wsPosition->setType( "vec3" );
  2349. wsPosition->setName( "wsPosition" );
  2350. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2351. // If we new viewToTangent... its the same as the
  2352. // world to tangent for an imposter.
  2353. Var *viewToTangent = new Var;
  2354. viewToTangent->setType( "float3x3" );
  2355. viewToTangent->setName( "viewToTangent" );
  2356. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2357. }
  2358. void ImposterVertFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2359. const MaterialFeatureData &fd )
  2360. {
  2361. // Find / create IN.imposterFade
  2362. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2363. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2364. fade->setName( "imposterFade" );
  2365. fade->setStructName( "IN" );
  2366. fade->setType( "float" );
  2367. // Find / create visibility
  2368. Var *visibility = (Var*) LangElement::find( "visibility" );
  2369. if ( !visibility )
  2370. {
  2371. visibility = new Var();
  2372. visibility->setType( "float" );
  2373. visibility->setName( "visibility" );
  2374. visibility->uniform = true;
  2375. visibility->constSortPos = cspPotentialPrimitive;
  2376. }
  2377. MultiLine *meta = new MultiLine;
  2378. // Multiply foliageFade into visibility.
  2379. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2380. output = meta;
  2381. }
  2382. void ImposterVertFeatureGLSL::determineFeature( Material *material,
  2383. const GFXVertexFormat *vertexFormat,
  2384. U32 stageNum,
  2385. const FeatureType &type,
  2386. const FeatureSet &features,
  2387. MaterialFeatureData *outFeatureData )
  2388. {
  2389. if ( features.hasFeature( MFT_ImposterVert ) )
  2390. outFeatureData->features.addFeature( MFT_ImposterVert );
  2391. }
  2392. //****************************************************************************
  2393. // HardwareSkinningFeatureGLSL
  2394. //****************************************************************************
  2395. void HardwareSkinningFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList,
  2396. const MaterialFeatureData &fd)
  2397. {
  2398. MultiLine *meta = new MultiLine;
  2399. Var *inPosition = (Var*)LangElement::find("inPosition");
  2400. Var *inNormal = (Var*)LangElement::find("inNormal");
  2401. if (!inPosition)
  2402. inPosition = (Var*)LangElement::find("position");
  2403. if (!inNormal)
  2404. inNormal = (Var*)LangElement::find("normal");
  2405. Var* posePos = new Var("posePos", "vec3");
  2406. Var* poseNormal = new Var("poseNormal", "vec3");
  2407. Var* poseMat = new Var("poseMat", "mat4x3");
  2408. Var* poseRotMat = new Var("poseRotMat", "mat3x3");
  2409. Var* nodeTransforms = (Var*)LangElement::find("nodeTransforms");
  2410. if (!nodeTransforms)
  2411. {
  2412. nodeTransforms = new Var("nodeTransforms", "mat4x3");
  2413. nodeTransforms->uniform = true;
  2414. nodeTransforms->arraySize = TSShape::smMaxSkinBones;
  2415. nodeTransforms->constSortPos = cspPrimitive;
  2416. }
  2417. U32 numIndices = mVertexFormat->getNumBlendIndices();
  2418. meta->addStatement(new GenOp(" @ = vec3(0.0);\r\n", new DecOp(posePos)));
  2419. meta->addStatement(new GenOp(" @ = vec3(0.0);\r\n", new DecOp(poseNormal)));
  2420. meta->addStatement(new GenOp(" @;\r\n", new DecOp(poseMat)));
  2421. meta->addStatement(new GenOp(" @;\r\n int i;\r\n", new DecOp(poseRotMat)));
  2422. for (U32 i = 0; i<numIndices; i++)
  2423. {
  2424. // NOTE: To keep things simple, we assume all 4 bone indices are used in each element chunk.
  2425. LangElement* inIndices = (Var*)LangElement::find(String::ToString("vBlendIndex%d", i));
  2426. LangElement* inWeights = (Var*)LangElement::find(String::ToString("vBlendWeight%d", i));
  2427. AssertFatal(inIndices && inWeights, "Something went wrong here");
  2428. AssertFatal(poseMat && nodeTransforms && posePos && inPosition && inWeights && poseNormal && inNormal && poseRotMat, "Something went REALLY wrong here");
  2429. meta->addStatement(new GenOp(" for (i=0; i<4; i++) {\r\n"));
  2430. meta->addStatement(new GenOp(" int poseIdx = int(@[i]);\r\n", inIndices));
  2431. meta->addStatement(new GenOp(" float poseWeight = @[i];\r\n", inWeights));
  2432. meta->addStatement(new GenOp(" @ = @[poseIdx];\r\n", poseMat, nodeTransforms));
  2433. meta->addStatement(new GenOp(" @ = mat3x3(@);\r\n", poseRotMat, poseMat));
  2434. meta->addStatement(new GenOp(" @ += (@ * vec4(@, 1)).xyz * poseWeight;\r\n", posePos, poseMat, inPosition));
  2435. meta->addStatement(new GenOp(" @ += ((@ * @) * poseWeight);\r\n", poseNormal, poseRotMat, inNormal));
  2436. meta->addStatement(new GenOp(" }\r\n"));
  2437. }
  2438. // Assign new position and normal
  2439. meta->addStatement(new GenOp(" @ = @;\r\n", inPosition, posePos));
  2440. meta->addStatement(new GenOp(" @ = normalize(@);\r\n", inNormal, poseNormal));
  2441. output = meta;
  2442. }
  2443. //****************************************************************************
  2444. // ReflectionProbeFeatGLSL
  2445. //****************************************************************************
  2446. ReflectionProbeFeatGLSL::ReflectionProbeFeatGLSL()
  2447. : mDep(ShaderGen::smCommonShaderPath + String("/gl/lighting.glsl"))
  2448. {
  2449. addDependency(&mDep);
  2450. }
  2451. void ReflectionProbeFeatGLSL::processVert(Vector<ShaderComponent*>& componentList,
  2452. const MaterialFeatureData& fd)
  2453. {
  2454. //MultiLine* meta = new MultiLine;
  2455. //output = meta;
  2456. // Also output the worldToTanget transform which
  2457. // we use to create the world space normal.
  2458. //getOutWorldToTangent(componentList, meta, fd);
  2459. }
  2460. void ReflectionProbeFeatGLSL::processPix(Vector<ShaderComponent*>& componentList,
  2461. const MaterialFeatureData& fd)
  2462. {
  2463. // Skip out on realtime lighting if we don't have a normal
  2464. // or we're doing some sort of baked lighting.
  2465. //
  2466. // TODO: We can totally detect for this in the material
  2467. // feature setup... we should move it out of here!
  2468. //
  2469. if (fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit])
  2470. return;
  2471. ShaderConnector * connectComp = dynamic_cast<ShaderConnector*>(componentList[C_CONNECTOR]);
  2472. MultiLine * meta = new MultiLine;
  2473. // Now the wsPosition and wsView.
  2474. Var *wsPosition = getInWsPosition(componentList);
  2475. Var *wsView = getWsView(wsPosition, meta);
  2476. //Reflection Probe WIP
  2477. U32 MAX_FORWARD_PROBES = 4;
  2478. Var * numProbes = new Var("numProbes", "int");
  2479. numProbes->uniform = true;
  2480. numProbes->constSortPos = cspPotentialPrimitive;
  2481. Var * cubeMips = new Var("cubeMips", "float");
  2482. cubeMips->uniform = true;
  2483. cubeMips->constSortPos = cspPotentialPrimitive;
  2484. Var * skylightCubemapIdx = new Var("skylightCubemapIdx", "float");
  2485. skylightCubemapIdx->uniform = true;
  2486. skylightCubemapIdx->constSortPos = cspPotentialPrimitive;
  2487. Var * inProbePosArray = new Var("inProbePosArray", "vec4");
  2488. inProbePosArray->arraySize = MAX_FORWARD_PROBES;
  2489. inProbePosArray->uniform = true;
  2490. inProbePosArray->constSortPos = cspPotentialPrimitive;
  2491. Var * inRefPosArray = new Var("inRefPosArray", "vec4");
  2492. inRefPosArray->arraySize = MAX_FORWARD_PROBES;
  2493. inRefPosArray->uniform = true;
  2494. inRefPosArray->constSortPos = cspPotentialPrimitive;
  2495. Var * refScaleArray = new Var("inRefScale", "vec4");
  2496. refScaleArray->arraySize = MAX_FORWARD_PROBES;
  2497. refScaleArray->uniform = true;
  2498. refScaleArray->constSortPos = cspPotentialPrimitive;
  2499. Var * probeConfigData = new Var("probeConfigData", "vec4");
  2500. probeConfigData->arraySize = MAX_FORWARD_PROBES;
  2501. probeConfigData->uniform = true;
  2502. probeConfigData->constSortPos = cspPotentialPrimitive;
  2503. Var * worldToObjArray = new Var("worldToObjArray", "mat4");
  2504. worldToObjArray->arraySize = MAX_FORWARD_PROBES;
  2505. worldToObjArray->uniform = true;
  2506. worldToObjArray->constSortPos = cspPotentialPrimitive;
  2507. // create texture var
  2508. Var* BRDFTexture = new Var;
  2509. BRDFTexture->setType("sampler2D");
  2510. BRDFTexture->setName("BRDFTexture");
  2511. BRDFTexture->uniform = true;
  2512. BRDFTexture->sampler = true;
  2513. BRDFTexture->constNum = Var::getTexUnitNum(); // used as texture unit num here
  2514. Var * specularCubemapAR = new Var("specularCubemapAR", "samplerCubeArray");
  2515. specularCubemapAR->uniform = true;
  2516. specularCubemapAR->sampler = true;
  2517. specularCubemapAR->constNum = Var::getTexUnitNum();
  2518. Var * irradianceCubemapAR = new Var("irradianceCubemapAR", "samplerCubeArray");
  2519. irradianceCubemapAR->uniform = true;
  2520. irradianceCubemapAR->sampler = true;
  2521. irradianceCubemapAR->constNum = Var::getTexUnitNum();
  2522. Var* surface = getSurface(componentList, meta, fd);
  2523. if (!surface)
  2524. {
  2525. Con::errorf("ShaderGen::ReflectionProbeFeatGLSL() - failed to generate surface!");
  2526. return;
  2527. }
  2528. Var *curColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  2529. //Reflection vec
  2530. String computeForwardProbes = String(" @.rgb = computeForwardProbes(@,@,@,@,@,@,@,@,\r\n\t\t");
  2531. computeForwardProbes += String("@,@,\r\n\t\t");
  2532. computeForwardProbes += String("@,@).rgb; \r\n");
  2533. meta->addStatement(new GenOp(computeForwardProbes.c_str(), curColor, surface, cubeMips, numProbes, worldToObjArray, probeConfigData, inProbePosArray, refScaleArray, inRefPosArray,
  2534. skylightCubemapIdx, BRDFTexture,
  2535. irradianceCubemapAR, specularCubemapAR));
  2536. output = meta;
  2537. }
  2538. ShaderFeature::Resources ReflectionProbeFeatGLSL::getResources(const MaterialFeatureData& fd)
  2539. {
  2540. Resources res;
  2541. res.numTex = 3;
  2542. res.numTexReg = 3;
  2543. return res;
  2544. }
  2545. void ReflectionProbeFeatGLSL::setTexData(Material::StageData& stageDat,
  2546. const MaterialFeatureData& stageFeatures,
  2547. RenderPassData& passData,
  2548. U32& texIndex)
  2549. {
  2550. if (stageFeatures.features[MFT_ReflectionProbes])
  2551. {
  2552. passData.mSamplerNames[texIndex] = "BRDFTexture";
  2553. passData.mTexType[texIndex++] = Material::Standard;
  2554. // assuming here that it is a scenegraph cubemap
  2555. passData.mSamplerNames[texIndex] = "specularCubemapAR";
  2556. passData.mTexType[texIndex++] = Material::SGCube;
  2557. passData.mSamplerNames[texIndex] = "irradianceCubemapAR";
  2558. passData.mTexType[texIndex++] = Material::SGCube;
  2559. }
  2560. }