shaderFeatureGLSL.cpp 106 KB

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