shaderFeatureGLSL.cpp 107 KB

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