shaderFeatureGLSL.cpp 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  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. LangElement * ShaderFeatureGLSL::setupTexSpaceMat( Vector<ShaderComponent*> &, // componentList
  34. Var **texSpaceMat )
  35. {
  36. Var *N = (Var*) LangElement::find( "normal" );
  37. Var *B = (Var*) LangElement::find( "B" );
  38. Var *T = (Var*) LangElement::find( "T" );
  39. // setup matrix var
  40. *texSpaceMat = new Var;
  41. (*texSpaceMat)->setType( "mat3" );
  42. (*texSpaceMat)->setName( "objToTangentSpace" );
  43. MultiLine * meta = new MultiLine;
  44. // Recreate the binormal if we don't have one.
  45. if ( !B )
  46. {
  47. B = new Var;
  48. B->setType( "vec3" );
  49. B->setName( "B" );
  50. meta->addStatement( new GenOp( " @ = cross( @, normalize(@) );\r\n", new DecOp( B ), T, N ) );
  51. }
  52. meta->addStatement( new GenOp( " @;\r\n", new DecOp( *texSpaceMat ) ) );
  53. meta->addStatement( new GenOp( " @[0] = vec3(@.x, @.x, normalize(@).x);\r\n", *texSpaceMat, T, B, N ) );
  54. meta->addStatement( new GenOp( " @[1] = vec3(@.y, @.y, normalize(@).y);\r\n", *texSpaceMat, T, B, N ) );
  55. meta->addStatement( new GenOp( " @[2] = vec3(@.z, @.z, normalize(@).z);\r\n", *texSpaceMat, T, B, N ) );
  56. return meta;
  57. }
  58. LangElement* ShaderFeatureGLSL::assignColor( LangElement *elem,
  59. Material::BlendOp blend,
  60. LangElement *lerpElem,
  61. ShaderFeature::OutputTarget outputTarget )
  62. {
  63. // search for color var
  64. Var *color = (Var*) LangElement::find( getOutputTargetVarName(outputTarget) );
  65. if ( !color )
  66. {
  67. // create color var
  68. color = new Var;
  69. color->setName( getOutputTargetVarName( outputTarget ) );
  70. color->setType( "vec4" );
  71. return new GenOp( "@ = @", new DecOp(color), elem );
  72. }
  73. LangElement *assign;
  74. switch ( blend )
  75. {
  76. case Material::Add:
  77. assign = new GenOp( "@ += @", color, elem );
  78. break;
  79. case Material::Sub:
  80. assign = new GenOp( "@ -= @", color, elem );
  81. break;
  82. case Material::Mul:
  83. assign = new GenOp( "@ *= @", color, elem );
  84. break;
  85. case Material::AddAlpha:
  86. assign = new GenOp( "@ += @ * @.a", color, elem, elem );
  87. break;
  88. case Material::LerpAlpha:
  89. if ( !lerpElem )
  90. lerpElem = elem;
  91. assign = new GenOp( "@.rgb = mix( @.rgb, (@).rgb, (@).a )", color, elem, color, lerpElem );
  92. break;
  93. case Material::ToneMap:
  94. assign = new GenOp( "@ = 1.0 - exp(-1.0 * @ * @)", color, color, elem );
  95. break;
  96. default:
  97. AssertFatal(false, "Unrecognized color blendOp");
  98. // Fallthru
  99. case Material::None:
  100. assign = new GenOp( "@ = @", color, elem );
  101. break;
  102. }
  103. return assign;
  104. }
  105. LangElement *ShaderFeatureGLSL::expandNormalMap( LangElement *sampleNormalOp,
  106. LangElement *normalDecl,
  107. LangElement *normalVar,
  108. const MaterialFeatureData &fd )
  109. {
  110. MultiLine *meta = new MultiLine;
  111. if ( fd.features.hasFeature( MFT_IsDXTnm, getProcessIndex() ) )
  112. {
  113. // DXT Swizzle trick
  114. meta->addStatement( new GenOp( " @ = vec4( @.ag * 2.0 - 1.0, 0.0, 0.0 ); // DXTnm\r\n", normalDecl, sampleNormalOp ) );
  115. meta->addStatement( new GenOp( " @.z = sqrt( 1.0 - dot( @.xy, @.xy ) ); // DXTnm\r\n", normalVar, normalVar, normalVar ) );
  116. }
  117. else
  118. {
  119. meta->addStatement( new GenOp( " @ = @;\r\n", normalDecl, sampleNormalOp ) );
  120. meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 1.0;\r\n", normalVar, normalVar ) );
  121. }
  122. return meta;
  123. }
  124. ShaderFeatureGLSL::ShaderFeatureGLSL()
  125. {
  126. output = NULL;
  127. }
  128. Var * ShaderFeatureGLSL::getVertTexCoord( const String &name )
  129. {
  130. Var *inTex = NULL;
  131. for( U32 i=0; i<LangElement::elementList.size(); i++ )
  132. {
  133. if( !dStrcmp( (char*)LangElement::elementList[i]->name, name.c_str() ) )
  134. {
  135. inTex = dynamic_cast<Var*>( LangElement::elementList[i] );
  136. if ( inTex )
  137. {
  138. break;
  139. }
  140. }
  141. }
  142. return inTex;
  143. }
  144. Var* ShaderFeatureGLSL::getOutObjToTangentSpace( Vector<ShaderComponent*> &componentList,
  145. MultiLine *meta,
  146. const MaterialFeatureData &fd )
  147. {
  148. Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" );
  149. if ( !outObjToTangentSpace )
  150. meta->addStatement( setupTexSpaceMat( componentList, &outObjToTangentSpace ) );
  151. return outObjToTangentSpace;
  152. }
  153. Var* ShaderFeatureGLSL::getOutWorldToTangent( Vector<ShaderComponent*> &componentList,
  154. MultiLine *meta,
  155. const MaterialFeatureData &fd )
  156. {
  157. Var *outWorldToTangent = (Var*)LangElement::find( "outWorldToTangent" );
  158. if ( outWorldToTangent )
  159. return outWorldToTangent;
  160. Var *worldToTangent = (Var*)LangElement::find( "worldToTangent" );
  161. if ( !worldToTangent )
  162. {
  163. Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
  164. if(!fd.features[MFT_ParticleNormal])
  165. {
  166. // turn obj->tangent into world->tangent
  167. worldToTangent = new Var;
  168. worldToTangent->setType( "mat3x3" );
  169. worldToTangent->setName( "worldToTangent" );
  170. LangElement *worldToTangentDecl = new DecOp( worldToTangent );
  171. // Get the world->obj transform
  172. Var *worldToObj = (Var*)LangElement::find( "worldToObj" );
  173. if ( !worldToObj )
  174. {
  175. worldToObj = new Var;
  176. worldToObj->setName( "worldToObj" );
  177. worldToObj->setType( "mat4x4" );
  178. worldToObj->uniform = true;
  179. worldToObj->constSortPos = cspPrimitive;
  180. }
  181. // assign world->tangent transform
  182. meta->addStatement( new GenOp( " @ = @ * mat3x3( @[0].xyz, @[1].xyz, @[2].xyz );\r\n", worldToTangentDecl, texSpaceMat, worldToObj, worldToObj, worldToObj ) );
  183. }
  184. else
  185. {
  186. worldToTangent = texSpaceMat;
  187. }
  188. }
  189. // send transform to pixel shader
  190. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  191. outWorldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  192. outWorldToTangent->setName( "outWorldToTangent" );
  193. outWorldToTangent->setType( "mat3x3" );
  194. meta->addStatement( new GenOp( " @ = @;\r\n", outWorldToTangent, worldToTangent ) );
  195. return outWorldToTangent;
  196. }
  197. Var* ShaderFeatureGLSL::getOutViewToTangent( Vector<ShaderComponent*> &componentList,
  198. MultiLine *meta,
  199. const MaterialFeatureData &fd )
  200. {
  201. Var *outViewToTangent = (Var*)LangElement::find( "outViewToTangent" );
  202. if ( outViewToTangent )
  203. return outViewToTangent;
  204. Var *viewToTangent = (Var*)LangElement::find( "viewToTangent" );
  205. if ( !viewToTangent )
  206. {
  207. Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
  208. if(!fd.features[MFT_ParticleNormal])
  209. {
  210. // turn obj->tangent into world->tangent
  211. viewToTangent = new Var;
  212. viewToTangent->setType( "mat3" );
  213. viewToTangent->setName( "viewToTangent" );
  214. LangElement *viewToTangentDecl = new DecOp( viewToTangent );
  215. // Get the view->obj transform
  216. Var *viewToObj = getInvWorldView( componentList, fd.features[MFT_UseInstancing], meta );
  217. // assign world->tangent transform
  218. meta->addStatement( new GenOp( " mat3 mat3ViewToObj;\r\n" ) );
  219. meta->addStatement( new GenOp( " mat3ViewToObj[0] = @[0].xyz;\r\n", viewToObj ) );
  220. meta->addStatement( new GenOp( " mat3ViewToObj[1] = @[1].xyz;\r\n", viewToObj ) );
  221. meta->addStatement( new GenOp( " mat3ViewToObj[2] = @[2].xyz;\r\n", viewToObj ) );
  222. meta->addStatement( new GenOp( " @ = @ * mat3ViewToObj;\r\n", viewToTangentDecl, texSpaceMat ) );
  223. }
  224. else
  225. {
  226. // Assume particle normal generation has set this up in the proper space
  227. viewToTangent = texSpaceMat;
  228. }
  229. }
  230. // send transform to pixel shader
  231. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  232. outViewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  233. outViewToTangent->setName( "outViewToTangent" );
  234. outViewToTangent->setType( "mat3" );
  235. meta->addStatement( new GenOp( " @ = @;\r\n", outViewToTangent, viewToTangent ) );
  236. return outViewToTangent;
  237. }
  238. Var* ShaderFeatureGLSL::getOutTexCoord( const char *name,
  239. const char *type,
  240. bool mapsToSampler,
  241. bool useTexAnim,
  242. MultiLine *meta,
  243. Vector<ShaderComponent*> &componentList )
  244. {
  245. String outTexName = String::ToString( "out_%s", name );
  246. Var *texCoord = (Var*)LangElement::find( outTexName );
  247. if ( !texCoord )
  248. {
  249. Var *inTex = getVertTexCoord( name );
  250. AssertFatal( inTex, "ShaderFeatureGLSL::getOutTexCoord - Unknown vertex input coord!" );
  251. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  252. texCoord = connectComp->getElement( RT_TEXCOORD );
  253. texCoord->setName( outTexName );
  254. texCoord->setType( type );
  255. texCoord->mapsToSampler = mapsToSampler;
  256. if( useTexAnim )
  257. {
  258. inTex->setType( "vec4" );
  259. // create texture mat var
  260. Var *texMat = new Var;
  261. texMat->setType( "mat4" );
  262. texMat->setName( "texMat" );
  263. texMat->uniform = true;
  264. texMat->constSortPos = cspPass;
  265. // Statement allows for casting of different types which
  266. // eliminates vector truncation problems.
  267. String statement = String::ToString( " @ = %s(@ * @);\r\n", type );
  268. meta->addStatement( new GenOp( statement , texCoord, texMat, inTex ) );
  269. }
  270. else
  271. {
  272. // Statement allows for casting of different types which
  273. // eliminates vector truncation problems.
  274. String statement = String::ToString( " @ = %s(@);\r\n", type );
  275. meta->addStatement( new GenOp( statement, texCoord, inTex ) );
  276. }
  277. }
  278. AssertFatal( dStrcmp( type, (const char*)texCoord->type ) == 0,
  279. "ShaderFeatureGLSL::getOutTexCoord - Type mismatch!" );
  280. return texCoord;
  281. }
  282. Var* ShaderFeatureGLSL::getInTexCoord( const char *name,
  283. const char *type,
  284. bool mapsToSampler,
  285. Vector<ShaderComponent*> &componentList )
  286. {
  287. Var* texCoord = (Var*)LangElement::find( name );
  288. if ( !texCoord )
  289. {
  290. ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
  291. texCoord = connectComp->getElement( RT_TEXCOORD );
  292. texCoord->setName( name );
  293. texCoord->setType( type );
  294. texCoord->mapsToSampler = mapsToSampler;
  295. }
  296. AssertFatal( dStrcmp( type, (const char*)texCoord->type ) == 0,
  297. "ShaderFeatureGLSL::getInTexCoord - Type mismatch!" );
  298. return texCoord;
  299. }
  300. Var* ShaderFeatureGLSL::getInWorldToTangent( Vector<ShaderComponent*> &componentList )
  301. {
  302. Var *worldToTangent = (Var*)LangElement::find( "outWorldToTangent" );
  303. if ( !worldToTangent )
  304. {
  305. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  306. worldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  307. worldToTangent->setName( "outWorldToTangent" );
  308. worldToTangent->setType( "mat3x3" );
  309. }
  310. return worldToTangent;
  311. }
  312. Var* ShaderFeatureGLSL::getInViewToTangent( Vector<ShaderComponent*> &componentList )
  313. {
  314. Var *viewToTangent = (Var*)LangElement::find( "outViewToTangent" );
  315. if ( !viewToTangent )
  316. {
  317. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  318. viewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  319. viewToTangent->setName( "outViewToTangent" );
  320. viewToTangent->setType( "mat3" );
  321. }
  322. return viewToTangent;
  323. }
  324. Var* ShaderFeatureGLSL::getNormalMapTex()
  325. {
  326. Var *normalMap = (Var*)LangElement::find( "bumpMap" );
  327. if ( !normalMap )
  328. {
  329. normalMap = new Var;
  330. normalMap->setType( "sampler2D" );
  331. normalMap->setName( "bumpMap" );
  332. normalMap->uniform = true;
  333. normalMap->sampler = true;
  334. normalMap->constNum = Var::getTexUnitNum();
  335. }
  336. return normalMap;
  337. }
  338. Var* ShaderFeatureGLSL::getObjTrans( Vector<ShaderComponent*> &componentList,
  339. bool useInstancing,
  340. MultiLine *meta )
  341. {
  342. Var *objTrans = (Var*) LangElement::find( "objTrans" ); if ( objTrans )
  343. return objTrans;
  344. objTrans = new Var; objTrans->setType( "mat4x4" );
  345. objTrans->setName( "objTrans" );
  346. objTrans->uniform = true;
  347. objTrans->constSortPos = cspPrimitive;
  348. return objTrans;
  349. }
  350. Var* ShaderFeatureGLSL::getModelView( Vector<ShaderComponent*> &componentList,
  351. bool useInstancing,
  352. MultiLine *meta )
  353. {
  354. Var *modelview = (Var*)LangElement::find( "modelview" );
  355. if ( modelview )
  356. return modelview;
  357. // create modelview variable
  358. modelview = new Var;
  359. modelview->setType( "mat4" );
  360. modelview->setName( "modelview" );
  361. modelview->uniform = true;
  362. modelview->constSortPos = cspPrimitive;
  363. return modelview;
  364. }
  365. Var* ShaderFeatureGLSL::getWorldView( Vector<ShaderComponent*> &componentList,
  366. bool useInstancing,
  367. MultiLine *meta )
  368. {
  369. Var *worldView = (Var*)LangElement::find( "worldViewOnly" );
  370. if ( worldView )
  371. return worldView;
  372. worldView = new Var;
  373. worldView->setType( "mat4x4" );
  374. worldView->setName( "worldViewOnly" );
  375. worldView->uniform = true;
  376. worldView->constSortPos = cspPrimitive;
  377. return worldView;
  378. }
  379. Var* ShaderFeatureGLSL::getInvWorldView( Vector<ShaderComponent*> &componentList,
  380. bool useInstancing,
  381. MultiLine *meta )
  382. {
  383. Var *viewToObj = (Var*)LangElement::find( "viewToObj" );
  384. if ( viewToObj )
  385. return viewToObj;
  386. viewToObj = new Var;
  387. viewToObj->setType( "mat4" );
  388. viewToObj->setName( "viewToObj" );
  389. viewToObj->uniform = true;
  390. viewToObj->constSortPos = cspPrimitive;
  391. return viewToObj;
  392. }
  393. void ShaderFeatureGLSL::getWsPosition( Vector<ShaderComponent*> &componentList,
  394. bool useInstancing,
  395. MultiLine *meta,
  396. LangElement *wsPosition )
  397. {
  398. Var *inPosition = (Var*)LangElement::find( "wsPosition" );
  399. if ( inPosition )
  400. {
  401. meta->addStatement( new GenOp( " @ = @.xyz;\r\n",
  402. wsPosition, inPosition ) );
  403. return;
  404. }
  405. // Get the input position.
  406. inPosition = (Var*)LangElement::find( "inPosition" );
  407. if ( !inPosition )
  408. inPosition = (Var*)LangElement::find( "position" );
  409. AssertFatal( inPosition, "ShaderFeatureGLSL::getWsPosition - The vertex position was not found!" );
  410. Var *objTrans = getObjTrans( componentList, useInstancing, meta );
  411. meta->addStatement( new GenOp( " @ = ( @ * vec4( @.xyz, 1 ) ).xyz;\r\n",
  412. wsPosition, objTrans, inPosition ) );
  413. }
  414. Var* ShaderFeatureGLSL::addOutWsPosition( Vector<ShaderComponent*> &componentList,
  415. bool useInstancing,
  416. MultiLine *meta )
  417. {
  418. Var *outWsPosition = (Var*)LangElement::find( "outWsPosition" );
  419. if ( !outWsPosition )
  420. {
  421. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  422. outWsPosition = connectComp->getElement( RT_TEXCOORD );
  423. outWsPosition->setName( "outWsPosition" );
  424. outWsPosition->setType( "vec3" );
  425. outWsPosition->mapsToSampler = false;
  426. getWsPosition( componentList, useInstancing, meta, outWsPosition );
  427. }
  428. return outWsPosition;
  429. }
  430. Var* ShaderFeatureGLSL::getInWsPosition( Vector<ShaderComponent*> &componentList )
  431. {
  432. Var *wsPosition = (Var*)LangElement::find( "outWsPosition" );
  433. if ( !wsPosition )
  434. {
  435. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  436. wsPosition = connectComp->getElement( RT_TEXCOORD );
  437. wsPosition->setName( "outWsPosition" );
  438. wsPosition->setType( "vec3" );
  439. }
  440. return wsPosition;
  441. }
  442. Var* ShaderFeatureGLSL::getWsView( Var *wsPosition, MultiLine *meta )
  443. {
  444. Var *wsView = (Var*)LangElement::find( "wsView" );
  445. if ( !wsView )
  446. {
  447. wsView = new Var( "wsView", "vec3" );
  448. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  449. if ( !eyePos )
  450. {
  451. eyePos = new Var;
  452. eyePos->setType( "vec3" );
  453. eyePos->setName( "eyePosWorld" );
  454. eyePos->uniform = true;
  455. eyePos->constSortPos = cspPass;
  456. }
  457. meta->addStatement( new GenOp( " @ = normalize( @ - @ );\r\n",
  458. new DecOp( wsView ), eyePos, wsPosition ) );
  459. }
  460. return wsView;
  461. }
  462. Var* ShaderFeatureGLSL::addOutDetailTexCoord( Vector<ShaderComponent*> &componentList,
  463. MultiLine *meta,
  464. bool useTexAnim )
  465. {
  466. // Check if its already added.
  467. Var *outTex = (Var*)LangElement::find( "detCoord" );
  468. if ( outTex )
  469. return outTex;
  470. // Grab incoming texture coords.
  471. Var *inTex = getVertTexCoord( "texCoord" );
  472. // create detail variable
  473. Var *detScale = new Var;
  474. detScale->setType( "vec2" );
  475. detScale->setName( "detailScale" );
  476. detScale->uniform = true;
  477. detScale->constSortPos = cspPotentialPrimitive;
  478. // grab connector texcoord register
  479. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  480. outTex = connectComp->getElement( RT_TEXCOORD );
  481. outTex->setName( "detCoord" );
  482. outTex->setType( "vec2" );
  483. outTex->mapsToSampler = true;
  484. if ( useTexAnim )
  485. {
  486. inTex->setType( "vec4" );
  487. // Find or create the texture matrix.
  488. Var *texMat = (Var*)LangElement::find( "texMat" );
  489. if ( !texMat )
  490. {
  491. texMat = new Var;
  492. texMat->setType( "mat4x4" );
  493. texMat->setName( "texMat" );
  494. texMat->uniform = true;
  495. texMat->constSortPos = cspPass;
  496. }
  497. meta->addStatement( new GenOp( " @ = (@ * @) * @;\r\n", outTex, texMat, inTex, detScale ) );
  498. }
  499. else
  500. {
  501. // setup output to mul texCoord by detail scale
  502. meta->addStatement( new GenOp( " @ = @ * @;\r\n", outTex, inTex, detScale ) );
  503. }
  504. return outTex;
  505. }
  506. //****************************************************************************
  507. // Base Texture
  508. //****************************************************************************
  509. void DiffuseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  510. const MaterialFeatureData &fd )
  511. {
  512. MultiLine *meta = new MultiLine;
  513. getOutTexCoord( "texCoord",
  514. "vec2",
  515. true,
  516. fd.features[MFT_TexAnim],
  517. meta,
  518. componentList );
  519. output = meta;
  520. }
  521. void DiffuseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  522. const MaterialFeatureData &fd )
  523. {
  524. // grab connector texcoord register
  525. Var *inTex = getInTexCoord( "out_texCoord", "vec2", true, componentList );
  526. // create texture var
  527. Var *diffuseMap = new Var;
  528. diffuseMap->setType( "sampler2D" );
  529. diffuseMap->setName( "diffuseMap" );
  530. diffuseMap->uniform = true;
  531. diffuseMap->sampler = true;
  532. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  533. if ( fd.features[MFT_CubeMap] )
  534. {
  535. MultiLine * meta = new MultiLine;
  536. // create sample color
  537. Var *diffColor = new Var;
  538. diffColor->setType( "vec4" );
  539. diffColor->setName( "diffuseColor" );
  540. LangElement *colorDecl = new DecOp( diffColor );
  541. meta->addStatement( new GenOp( " @ = texture2D(@, @);\r\n",
  542. colorDecl,
  543. diffuseMap,
  544. inTex ) );
  545. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffColor, Material::Mul ) ) );
  546. output = meta;
  547. }
  548. else if(fd.features[MFT_DiffuseMapAtlas])
  549. {
  550. // Handle atlased textures
  551. // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
  552. MultiLine * meta = new MultiLine;
  553. output = meta;
  554. Var *atlasedTex = new Var;
  555. atlasedTex->setName("atlasedTexCoord");
  556. atlasedTex->setType("float2");
  557. LangElement *atDecl = new DecOp(atlasedTex);
  558. // Parameters of the texture atlas
  559. Var *atParams = new Var;
  560. atParams->setType("float4");
  561. atParams->setName("diffuseAtlasParams");
  562. atParams->uniform = true;
  563. atParams->constSortPos = cspPotentialPrimitive;
  564. // Parameters of the texture (tile) this object is using in the atlas
  565. Var *tileParams = new Var;
  566. tileParams->setType("float4");
  567. tileParams->setName("diffuseAtlasTileParams");
  568. tileParams->uniform = true;
  569. tileParams->constSortPos = cspPotentialPrimitive;
  570. const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
  571. if(is_sm3)
  572. {
  573. // Figure out the mip level
  574. meta->addStatement(new GenOp(" vec2 _dx = ddx(@ * @.z);\r\n", inTex, atParams));
  575. meta->addStatement(new GenOp(" vec2 _dy = ddy(@ * @.z);\r\n", inTex, atParams));
  576. meta->addStatement(new GenOp(" float mipLod = 0.5 * log2(max(dot(_dx, _dx), dot(_dy, _dy)));\r\n"));
  577. meta->addStatement(new GenOp(" mipLod = clamp(mipLod, 0.0, @.w);\r\n", atParams));
  578. // And the size of the mip level
  579. meta->addStatement(new GenOp(" float mipPixSz = pow(2.0, @.w - mipLod);\r\n", atParams));
  580. meta->addStatement(new GenOp(" vec2 mipSz = mipPixSz / @.xy;\r\n", atParams));
  581. }
  582. else
  583. {
  584. meta->addStatement(new GenOp(" vec2 mipSz = float2(1.0, 1.0);\r\n"));
  585. }
  586. // Tiling mode
  587. // TODO: Select wrap or clamp somehow
  588. if( true ) // Wrap
  589. meta->addStatement(new GenOp(" @ = fract(@);\r\n", atDecl, inTex));
  590. else // Clamp
  591. meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, inTex));
  592. // Finally scale/offset, and correct for filtering
  593. meta->addStatement(new GenOp(" @ = @ * ((mipSz * @.xy - 1.0) / mipSz) + 0.5 / mipSz + @.xy * @.xy;\r\n",
  594. atlasedTex, atlasedTex, atParams, atParams, tileParams));
  595. // Add a newline
  596. meta->addStatement(new GenOp( "\r\n"));
  597. // For the rest of the feature...
  598. inTex = atlasedTex;
  599. // create sample color var
  600. Var *diffColor = new Var;
  601. diffColor->setType("vec4");
  602. diffColor->setName("diffuseColor");
  603. // To dump out UV coords...
  604. //#define DEBUG_ATLASED_UV_COORDS
  605. #ifdef DEBUG_ATLASED_UV_COORDS
  606. if(!fd.features[MFT_PrePassConditioner])
  607. {
  608. meta->addStatement(new GenOp(" @ = vec4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
  609. meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul)));
  610. return;
  611. }
  612. #endif
  613. if(is_sm3)
  614. {
  615. meta->addStatement(new GenOp( " @ = texture2Dlod(@, float4(@, 0.0, mipLod));\r\n",
  616. new DecOp(diffColor), diffuseMap, inTex));
  617. }
  618. else
  619. {
  620. meta->addStatement(new GenOp( " @ = texture2D(@, @);\r\n",
  621. new DecOp(diffColor), diffuseMap, inTex));
  622. }
  623. meta->addStatement(new GenOp( " @;\r\n", assignColor(diffColor, Material::Mul)));
  624. }
  625. else
  626. {
  627. LangElement *statement = new GenOp( "texture2D(@, @)", diffuseMap, inTex );
  628. output = new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) );
  629. }
  630. }
  631. ShaderFeature::Resources DiffuseMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  632. {
  633. Resources res;
  634. res.numTex = 1;
  635. res.numTexReg = 1;
  636. return res;
  637. }
  638. void DiffuseMapFeatGLSL::setTexData( Material::StageData &stageDat,
  639. const MaterialFeatureData &fd,
  640. RenderPassData &passData,
  641. U32 &texIndex )
  642. {
  643. GFXTextureObject *tex = stageDat.getTex( MFT_DiffuseMap );
  644. if ( tex )
  645. passData.mTexSlot[ texIndex++ ].texObject = tex;
  646. }
  647. //****************************************************************************
  648. // Overlay Texture
  649. //****************************************************************************
  650. void OverlayTexFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  651. const MaterialFeatureData &fd )
  652. {
  653. Var *inTex = getVertTexCoord( "texCoord2" );
  654. AssertFatal( inTex, "OverlayTexFeatGLSL::processVert() - The second UV set was not found!" );
  655. // grab connector texcoord register
  656. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  657. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  658. outTex->setName( "outTexCoord2" );
  659. outTex->setType( "vec2" );
  660. outTex->mapsToSampler = true;
  661. if( fd.features[MFT_TexAnim] )
  662. {
  663. inTex->setType( "vec4" );
  664. // Find or create the texture matrix.
  665. Var *texMat = (Var*)LangElement::find( "texMat" );
  666. if ( !texMat )
  667. {
  668. texMat = new Var;
  669. texMat->setType( "mat4x4" );
  670. texMat->setName( "texMat" );
  671. texMat->uniform = true;
  672. texMat->constSortPos = cspPass;
  673. }
  674. output = new GenOp( " @ = @ * @;\r\n", outTex, texMat, inTex );
  675. return;
  676. }
  677. // setup language elements to output incoming tex coords to output
  678. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  679. }
  680. void OverlayTexFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  681. const MaterialFeatureData &fd )
  682. {
  683. // grab connector texcoord register
  684. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  685. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  686. inTex->setName( "outTexCoord2" );
  687. inTex->setType( "vec2" );
  688. inTex->mapsToSampler = true;
  689. // create texture var
  690. Var *diffuseMap = new Var;
  691. diffuseMap->setType( "sampler2D" );
  692. diffuseMap->setName( "overlayMap" );
  693. diffuseMap->uniform = true;
  694. diffuseMap->sampler = true;
  695. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  696. LangElement *statement = new GenOp( "texture2D(@, @)", diffuseMap, inTex );
  697. output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
  698. }
  699. ShaderFeature::Resources OverlayTexFeatGLSL::getResources( const MaterialFeatureData &fd )
  700. {
  701. Resources res;
  702. res.numTex = 1;
  703. res.numTexReg = 1;
  704. return res;
  705. }
  706. void OverlayTexFeatGLSL::setTexData( Material::StageData &stageDat,
  707. const MaterialFeatureData &fd,
  708. RenderPassData &passData,
  709. U32 &texIndex )
  710. {
  711. GFXTextureObject *tex = stageDat.getTex( MFT_OverlayMap );
  712. if ( tex )
  713. passData.mTexSlot[ texIndex++ ].texObject = tex;
  714. }
  715. //****************************************************************************
  716. // Diffuse color
  717. //****************************************************************************
  718. void DiffuseFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  719. const MaterialFeatureData &fd )
  720. {
  721. Var* diffuseMaterialColor = new Var;
  722. diffuseMaterialColor->setType( "vec4" );
  723. diffuseMaterialColor->setName( "diffuseMaterialColor" );
  724. diffuseMaterialColor->uniform = true;
  725. diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
  726. MultiLine* meta = new MultiLine;
  727. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffuseMaterialColor, Material::Mul ) ) );
  728. output = meta;
  729. }
  730. //****************************************************************************
  731. // Diffuse vertex color
  732. //****************************************************************************
  733. void DiffuseVertColorFeatureGLSL::processVert( Vector< ShaderComponent* >& componentList,
  734. const MaterialFeatureData& fd )
  735. {
  736. // Create vertex color connector if it doesn't exist.
  737. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  738. if( !outColor )
  739. {
  740. // Search for vert color.
  741. Var* inColor = dynamic_cast< Var* >( LangElement::find( "diffuse" ) );
  742. if( !inColor )
  743. {
  744. output = NULL;
  745. return;
  746. }
  747. // Create connector.
  748. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  749. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  750. Var* outColor = connectComp->getElement( RT_COLOR );
  751. outColor->setName( "vertColor" );
  752. outColor->setType( "vec4" );
  753. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  754. }
  755. else
  756. output = NULL; // Nothing we need to do.
  757. }
  758. void DiffuseVertColorFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  759. const MaterialFeatureData &fd )
  760. {
  761. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  762. if( !vertColor )
  763. {
  764. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  765. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  766. vertColor = connectComp->getElement( RT_COLOR );
  767. vertColor->setName( "vertColor" );
  768. vertColor->setType( "vec4" );
  769. }
  770. MultiLine* meta = new MultiLine;
  771. meta->addStatement( new GenOp( " @;\r\n", assignColor( vertColor, Material::Mul ) ) );
  772. output = meta;
  773. }
  774. //****************************************************************************
  775. // Lightmap
  776. //****************************************************************************
  777. void LightmapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  778. const MaterialFeatureData &fd )
  779. {
  780. // grab tex register from incoming vert
  781. Var *inTex = (Var*) LangElement::find( "texCoord2" );
  782. // grab connector texcoord register
  783. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  784. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  785. outTex->setName( "outTexCoord2" );
  786. outTex->setType( "vec2" );
  787. outTex->mapsToSampler = true;
  788. // setup language elements to output incoming tex coords to output
  789. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  790. }
  791. void LightmapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  792. const MaterialFeatureData &fd )
  793. {
  794. // grab connector texcoord register
  795. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  796. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  797. inTex->setName( "outTexCoord2" );
  798. inTex->setType( "vec2" );
  799. inTex->mapsToSampler = true;
  800. // create texture var
  801. Var *lightMap = new Var;
  802. lightMap->setType( "sampler2D" );
  803. lightMap->setName( "lightMap" );
  804. lightMap->uniform = true;
  805. lightMap->sampler = true;
  806. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  807. // argh, pixel specular should prob use this too
  808. if( fd.features[MFT_NormalMap] )
  809. {
  810. Var *lmColor = new Var;
  811. lmColor->setName( "lmColor" );
  812. lmColor->setType( "vec4" );
  813. LangElement *lmColorDecl = new DecOp( lmColor );
  814. output = new GenOp( " @ = texture2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
  815. return;
  816. }
  817. // Add realtime lighting, if it is available
  818. LangElement *statement = NULL;
  819. if( fd.features[MFT_RTLighting] )
  820. {
  821. // Advanced lighting is the only dynamic lighting supported right now
  822. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  823. if(inColor != NULL)
  824. {
  825. // Find out if RTLighting should be added or substituted
  826. bool bPreProcessedLighting = false;
  827. AdvancedLightBinManager *lightBin;
  828. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  829. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  830. // Lightmap has already been included in the advanced light bin, so
  831. // no need to do any sampling or anything
  832. if(bPreProcessedLighting)
  833. statement = new GenOp( "vec4(@, 1.0)", inColor );
  834. else
  835. statement = new GenOp( "texture2D(@, @) + vec4(@.rgb, 0.0)", lightMap, inTex, inColor );
  836. }
  837. }
  838. else
  839. {
  840. statement = new GenOp( "texture2D(@, @)", lightMap, inTex );
  841. }
  842. // Assign to proper render target
  843. if( fd.features[MFT_LightbufferMRT] )
  844. output = new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget1 ) );
  845. else
  846. output = new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) );
  847. }
  848. ShaderFeature::Resources LightmapFeatGLSL::getResources( const MaterialFeatureData &fd )
  849. {
  850. Resources res;
  851. res.numTex = 1;
  852. res.numTexReg = 1;
  853. return res;
  854. }
  855. void LightmapFeatGLSL::setTexData( Material::StageData &stageDat,
  856. const MaterialFeatureData &fd,
  857. RenderPassData &passData,
  858. U32 &texIndex )
  859. {
  860. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  861. if ( tex )
  862. passData.mTexSlot[ texIndex++ ].texObject = tex;
  863. else
  864. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  865. }
  866. U32 LightmapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  867. {
  868. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  869. }
  870. //****************************************************************************
  871. // Tonemap
  872. //****************************************************************************
  873. void TonemapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  874. const MaterialFeatureData &fd )
  875. {
  876. // Grab the connector
  877. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  878. // Set up the second set of texCoords
  879. Var *inTex2 = getVertTexCoord( "texCoord2" );
  880. if ( inTex2 )
  881. {
  882. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  883. outTex2->setName( "toneMapCoord" );
  884. outTex2->setType( "vec2" );
  885. outTex2->mapsToSampler = true;
  886. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  887. }
  888. }
  889. void TonemapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  890. const MaterialFeatureData &fd )
  891. {
  892. // Grab connector
  893. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  894. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  895. inTex2->setName( "toneMapCoord" );
  896. inTex2->setType( "vec2" );
  897. inTex2->mapsToSampler = true;
  898. // create texture var
  899. Var *toneMap = new Var;
  900. toneMap->setType( "sampler2D" );
  901. toneMap->setName( "toneMap" );
  902. toneMap->uniform = true;
  903. toneMap->sampler = true;
  904. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  905. MultiLine * meta = new MultiLine;
  906. // First get the toneMap color
  907. Var *toneMapColor = new Var;
  908. toneMapColor->setType( "vec4" );
  909. toneMapColor->setName( "toneMapColor" );
  910. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  911. meta->addStatement( new GenOp( " @ = texture2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
  912. // We do a different calculation if there is a diffuse map or not
  913. Material::BlendOp blendOp = Material::Mul;
  914. if ( fd.features[MFT_DiffuseMap] )
  915. {
  916. // Reverse the tonemap
  917. meta->addStatement( new GenOp( " @ = -1.0 * log(1.0 - @);\r\n", toneMapColor, toneMapColor ) );
  918. // Re-tonemap with the current color factored in
  919. blendOp = Material::ToneMap;
  920. }
  921. // Find out if RTLighting should be added
  922. bool bPreProcessedLighting = false;
  923. AdvancedLightBinManager *lightBin;
  924. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  925. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  926. // Add in the realtime lighting contribution
  927. if ( fd.features[MFT_RTLighting] )
  928. {
  929. // Right now, only Advanced Lighting is supported
  930. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  931. if(inColor != NULL)
  932. {
  933. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  934. // the dynamic light buffer, and it already has the tonemap included
  935. if(bPreProcessedLighting)
  936. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  937. else
  938. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  939. }
  940. }
  941. // Assign to proper render target
  942. if( fd.features[MFT_LightbufferMRT] )
  943. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  944. else
  945. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  946. output = meta;
  947. }
  948. ShaderFeature::Resources TonemapFeatGLSL::getResources( const MaterialFeatureData &fd )
  949. {
  950. Resources res;
  951. res.numTex = 1;
  952. res.numTexReg = 1;
  953. return res;
  954. }
  955. void TonemapFeatGLSL::setTexData( Material::StageData &stageDat,
  956. const MaterialFeatureData &fd,
  957. RenderPassData &passData,
  958. U32 &texIndex )
  959. {
  960. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  961. if ( tex )
  962. {
  963. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  964. passData.mTexSlot[ texIndex++ ].texObject = tex;
  965. }
  966. }
  967. U32 TonemapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  968. {
  969. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  970. }
  971. //****************************************************************************
  972. // pureLIGHT Lighting
  973. //****************************************************************************
  974. void VertLitGLSL::processVert( Vector<ShaderComponent*> &componentList,
  975. const MaterialFeatureData &fd )
  976. {
  977. // If we have a lightMap or toneMap then our lighting will be
  978. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  979. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  980. {
  981. output = NULL;
  982. return;
  983. }
  984. // Create vertex color connector if it doesn't exist.
  985. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  986. if( !outColor )
  987. {
  988. // Search for vert color
  989. Var *inColor = (Var*) LangElement::find( "diffuse" );
  990. // If there isn't a vertex color then we can't do anything
  991. if( !inColor )
  992. {
  993. output = NULL;
  994. return;
  995. }
  996. // Grab the connector color
  997. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  998. Var *outColor = connectComp->getElement( RT_COLOR );
  999. outColor->setName( "vertColor" );
  1000. outColor->setType( "vec4" );
  1001. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1002. }
  1003. else
  1004. output = NULL; // Nothing we need to do.
  1005. }
  1006. void VertLitGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1007. const MaterialFeatureData &fd )
  1008. {
  1009. // If we have a lightMap or toneMap then our lighting will be
  1010. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1011. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1012. {
  1013. output = NULL;
  1014. return;
  1015. }
  1016. // Grab the connector color register
  1017. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1018. if( !vertColor )
  1019. {
  1020. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1021. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1022. vertColor = connectComp->getElement( RT_COLOR );
  1023. vertColor->setName( "vertColor" );
  1024. vertColor->setType( "vec4" );
  1025. }
  1026. MultiLine * meta = new MultiLine;
  1027. // Defaults (no diffuse map)
  1028. Material::BlendOp blendOp = Material::Mul;
  1029. LangElement *outColor = vertColor;
  1030. // We do a different calculation if there is a diffuse map or not
  1031. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1032. {
  1033. Var * finalVertColor = new Var;
  1034. finalVertColor->setName( "finalVertColor" );
  1035. finalVertColor->setType( "vec4" );
  1036. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1037. // Reverse the tonemap
  1038. meta->addStatement( new GenOp( " @ = -1.0 * log(1.0 - @);\r\n", finalVertColorDecl, vertColor ) );
  1039. // Set the blend op to tonemap
  1040. blendOp = Material::ToneMap;
  1041. outColor = finalVertColor;
  1042. }
  1043. // Add in the realtime lighting contribution, if applicable
  1044. if ( fd.features[MFT_RTLighting] )
  1045. {
  1046. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1047. if(rtLightingColor != NULL)
  1048. {
  1049. // Find out if RTLighting should be added or substituted
  1050. bool bPreProcessedLighting = false;
  1051. AdvancedLightBinManager *lightBin;
  1052. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1053. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1054. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1055. // the dynamic light buffer, and it already has the baked-vertex-color
  1056. // included in it
  1057. if(bPreProcessedLighting)
  1058. outColor = new GenOp( "vec4(@.rgb, 1.0)", rtLightingColor );
  1059. else
  1060. outColor = new GenOp( "vec4(@.rgb, 0.0) + @", rtLightingColor, outColor );
  1061. }
  1062. }
  1063. // Output the color
  1064. if ( fd.features[MFT_LightbufferMRT] )
  1065. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1066. else
  1067. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1068. output = meta;
  1069. }
  1070. U32 VertLitGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1071. {
  1072. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1073. }
  1074. //****************************************************************************
  1075. // Detail map
  1076. //****************************************************************************
  1077. void DetailFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1078. const MaterialFeatureData &fd )
  1079. {
  1080. MultiLine *meta = new MultiLine;
  1081. addOutDetailTexCoord( componentList,
  1082. meta,
  1083. fd.features[MFT_TexAnim] );
  1084. output = meta;
  1085. }
  1086. void DetailFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1087. const MaterialFeatureData &fd )
  1088. {
  1089. // Get the detail texture coord.
  1090. Var *inTex = getInTexCoord( "detCoord", "vec2", true, componentList );
  1091. // create texture var
  1092. Var *detailMap = new Var;
  1093. detailMap->setType( "sampler2D" );
  1094. detailMap->setName( "detailMap" );
  1095. detailMap->uniform = true;
  1096. detailMap->sampler = true;
  1097. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1098. // We're doing the standard greyscale detail map
  1099. // technique which can darken and lighten the
  1100. // diffuse texture.
  1101. // TODO: We could add a feature to toggle between this
  1102. // and a simple multiplication with the detail map.
  1103. LangElement *statement = new GenOp( "( texture2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
  1104. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1105. }
  1106. ShaderFeature::Resources DetailFeatGLSL::getResources( const MaterialFeatureData &fd )
  1107. {
  1108. Resources res;
  1109. res.numTex = 1;
  1110. res.numTexReg = 1;
  1111. return res;
  1112. }
  1113. void DetailFeatGLSL::setTexData( Material::StageData &stageDat,
  1114. const MaterialFeatureData &fd,
  1115. RenderPassData &passData,
  1116. U32 &texIndex )
  1117. {
  1118. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1119. if ( tex )
  1120. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1121. }
  1122. //****************************************************************************
  1123. // Vertex position
  1124. //****************************************************************************
  1125. void VertPositionGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1126. const MaterialFeatureData &fd )
  1127. {
  1128. // First check for an input position from a previous feature
  1129. // then look for the default vertex position.
  1130. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1131. if ( !inPosition )
  1132. inPosition = (Var*)LangElement::find( "position" );
  1133. // grab connector position
  1134. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1135. Var *outPosition = connectComp->getElement( RT_POSITION );
  1136. outPosition->setName( "gl_Position" );
  1137. MultiLine *meta = new MultiLine;
  1138. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1139. meta->addStatement( new GenOp( " @ = @ * vec4(@.xyz,1);\r\n", outPosition, modelview, inPosition ) );
  1140. output = meta;
  1141. }
  1142. //****************************************************************************
  1143. // Reflect Cubemap
  1144. //****************************************************************************
  1145. void ReflectCubeFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1146. const MaterialFeatureData &fd )
  1147. {
  1148. MultiLine * meta = new MultiLine;
  1149. // If a base or bump tex is present in the material, but not in the
  1150. // current pass - we need to add one to the current pass to use
  1151. // its alpha channel as a gloss map. Here we just need the tex coords.
  1152. if( !fd.features[MFT_DiffuseMap] &&
  1153. !fd.features[MFT_NormalMap] )
  1154. {
  1155. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1156. fd.materialFeatures[MFT_NormalMap] )
  1157. {
  1158. // find incoming texture var
  1159. Var *inTex = getVertTexCoord( "texCoord" );
  1160. // grab connector texcoord register
  1161. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1162. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1163. outTex->setName( "outTexCoord" );
  1164. outTex->setType( "vec2" );
  1165. outTex->mapsToSampler = true;
  1166. // setup language elements to output incoming tex coords to output
  1167. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1168. }
  1169. }
  1170. // create cubeTrans
  1171. Var *cubeTrans = new Var;
  1172. cubeTrans->setType( "mat3" );
  1173. cubeTrans->setName( "cubeTrans" );
  1174. cubeTrans->uniform = true;
  1175. cubeTrans->constSortPos = cspPrimitive;
  1176. // create cubeEye position
  1177. Var *cubeEyePos = new Var;
  1178. cubeEyePos->setType( "vec3" );
  1179. cubeEyePos->setName( "cubeEyePos" );
  1180. cubeEyePos->uniform = true;
  1181. cubeEyePos->constSortPos = cspPrimitive;
  1182. // search for vert normal
  1183. Var *inNormal = (Var*) LangElement::find( "normal" );
  1184. // cube vert position
  1185. Var * cubeVertPos = new Var;
  1186. cubeVertPos->setName( "cubeVertPos" );
  1187. cubeVertPos->setType( "vec3" );
  1188. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1189. meta->addStatement( new GenOp( " @ = @ * @.xyz;\r\n",
  1190. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1191. // cube normal
  1192. Var * cubeNormal = new Var;
  1193. cubeNormal->setName( "cubeNormal" );
  1194. cubeNormal->setType( "vec3" );
  1195. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1196. meta->addStatement( new GenOp( " @ = normalize( @ * normalize(@).xyz );\r\n",
  1197. cubeNormDecl, cubeTrans, inNormal ) );
  1198. // eye to vert
  1199. Var * eyeToVert = new Var;
  1200. eyeToVert->setName( "eyeToVert" );
  1201. eyeToVert->setType( "vec3" );
  1202. LangElement *e2vDecl = new DecOp( eyeToVert );
  1203. meta->addStatement( new GenOp( " @ = @ - @;\r\n",
  1204. e2vDecl, cubeVertPos, cubeEyePos ) );
  1205. // grab connector texcoord register
  1206. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1207. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1208. reflectVec->setName( "reflectVec" );
  1209. reflectVec->setType( "vec3" );
  1210. reflectVec->mapsToSampler = true;
  1211. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1212. output = meta;
  1213. }
  1214. void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1215. const MaterialFeatureData &fd )
  1216. {
  1217. MultiLine * meta = new MultiLine;
  1218. Var *glossColor = NULL;
  1219. // If a base or bump tex is present in the material, but not in the
  1220. // current pass - we need to add one to the current pass to use
  1221. // its alpha channel as a gloss map.
  1222. if( !fd.features[MFT_DiffuseMap] &&
  1223. !fd.features[MFT_NormalMap] )
  1224. {
  1225. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1226. fd.materialFeatures[MFT_NormalMap] )
  1227. {
  1228. // grab connector texcoord register
  1229. Var *inTex = getInTexCoord( "outTexCoord", "vec2", true, componentList );
  1230. // create texture var
  1231. Var *newMap = new Var;
  1232. newMap->setType( "sampler2D" );
  1233. newMap->setName( "glossMap" );
  1234. newMap->uniform = true;
  1235. newMap->sampler = true;
  1236. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1237. // create sample color
  1238. Var *color = new Var;
  1239. color->setType( "vec4" );
  1240. color->setName( "diffuseColor" );
  1241. LangElement *colorDecl = new DecOp( color );
  1242. glossColor = color;
  1243. meta->addStatement( new GenOp( " @ = texture2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
  1244. }
  1245. }
  1246. else
  1247. {
  1248. glossColor = (Var*) LangElement::find( "diffuseColor" );
  1249. if( !glossColor )
  1250. glossColor = (Var*) LangElement::find( "bumpNormal" );
  1251. }
  1252. // grab connector texcoord register
  1253. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1254. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1255. reflectVec->setName( "reflectVec" );
  1256. reflectVec->setType( "vec3" );
  1257. reflectVec->mapsToSampler = true;
  1258. // create cubemap var
  1259. Var *cubeMap = new Var;
  1260. cubeMap->setType( "samplerCube" );
  1261. cubeMap->setName( "cubeMap" );
  1262. cubeMap->uniform = true;
  1263. cubeMap->sampler = true;
  1264. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1265. // TODO: Restore the lighting attenuation here!
  1266. Var *attn = NULL;
  1267. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1268. //attn = (Var*)LangElement::find("attn");
  1269. //else
  1270. if ( fd.materialFeatures[MFT_RTLighting] )
  1271. attn =(Var*)LangElement::find("d_NL_Att");
  1272. LangElement *texCube = new GenOp( "textureCube( @, @ )", cubeMap, reflectVec );
  1273. LangElement *lerpVal = NULL;
  1274. Material::BlendOp blendOp = Material::LerpAlpha;
  1275. // Note that the lerpVal needs to be a float4 so that
  1276. // it will work with the LerpAlpha blend.
  1277. if ( glossColor )
  1278. {
  1279. if ( attn )
  1280. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1281. else
  1282. lerpVal = glossColor;
  1283. }
  1284. else
  1285. {
  1286. if ( attn )
  1287. lerpVal = new GenOp( "saturate( @ ).xxxx", attn );
  1288. else
  1289. blendOp = Material::Mul;
  1290. }
  1291. meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
  1292. output = meta;
  1293. }
  1294. ShaderFeature::Resources ReflectCubeFeatGLSL::getResources( const MaterialFeatureData &fd )
  1295. {
  1296. Resources res;
  1297. if( fd.features[MFT_DiffuseMap] ||
  1298. fd.features[MFT_NormalMap] )
  1299. {
  1300. res.numTex = 1;
  1301. res.numTexReg = 1;
  1302. }
  1303. else
  1304. {
  1305. res.numTex = 2;
  1306. res.numTexReg = 2;
  1307. }
  1308. return res;
  1309. }
  1310. void ReflectCubeFeatGLSL::setTexData( Material::StageData &stageDat,
  1311. const MaterialFeatureData &stageFeatures,
  1312. RenderPassData &passData,
  1313. U32 &texIndex )
  1314. {
  1315. // set up a gloss map if one is not present in the current pass
  1316. // but is present in the current material stage
  1317. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1318. !passData.mFeatureData.features[MFT_NormalMap] )
  1319. {
  1320. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1321. if ( tex &&
  1322. stageFeatures.features[MFT_DiffuseMap] )
  1323. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1324. else
  1325. {
  1326. tex = stageDat.getTex( MFT_NormalMap );
  1327. if ( tex &&
  1328. stageFeatures.features[ MFT_NormalMap ] )
  1329. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1330. }
  1331. }
  1332. if( stageDat.getCubemap() )
  1333. {
  1334. passData.mCubeMap = stageDat.getCubemap();
  1335. passData.mTexType[texIndex++] = Material::Cube;
  1336. }
  1337. else
  1338. {
  1339. if( stageFeatures.features[MFT_CubeMap] )
  1340. {
  1341. // assuming here that it is a scenegraph cubemap
  1342. passData.mTexType[texIndex++] = Material::SGCube;
  1343. }
  1344. }
  1345. }
  1346. //****************************************************************************
  1347. // RTLighting
  1348. //****************************************************************************
  1349. RTLightingFeatGLSL::RTLightingFeatGLSL()
  1350. : mDep( "shaders/common/gl/lighting.glsl" )
  1351. {
  1352. addDependency( &mDep );
  1353. }
  1354. void RTLightingFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1355. const MaterialFeatureData &fd )
  1356. {
  1357. MultiLine *meta = new MultiLine;
  1358. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1359. // Special case for lighting imposters. We dont have a vert normal and may not
  1360. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1361. if ( fd.features[MFT_ImposterVert] )
  1362. {
  1363. if ( !fd.features[MFT_NormalMap] )
  1364. {
  1365. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1366. if ( !eyePos )
  1367. {
  1368. eyePos = new Var( "eyePosWorld", "float3" );
  1369. eyePos->uniform = true;
  1370. eyePos->constSortPos = cspPass;
  1371. }
  1372. //TODO: should this be the same as "Find the incoming vertex normal" below?
  1373. Var *inPosition = (Var*)LangElement::find( "position" );
  1374. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1375. outNormal->setName( "wsNormal" );
  1376. outNormal->setStructName( "OUT" );
  1377. outNormal->setType( "float3" );
  1378. outNormal->mapsToSampler = false;
  1379. // Transform the normal to world space.
  1380. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );
  1381. }
  1382. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1383. output = meta;
  1384. return;
  1385. }
  1386. // Find the incoming vertex normal.
  1387. Var *inNormal = (Var*)LangElement::find( "inNormal" );
  1388. if ( !inNormal )
  1389. inNormal = (Var*)LangElement::find( "normal" );
  1390. // Skip out on realtime lighting if we don't have a normal
  1391. // or we're doing some sort of baked lighting.
  1392. if ( !inNormal ||
  1393. fd.features[MFT_LightMap] ||
  1394. fd.features[MFT_ToneMap] ||
  1395. fd.features[MFT_VertLit] )
  1396. return;
  1397. // Get the transform to world space.
  1398. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1399. // If there isn't a normal map then we need to pass
  1400. // the world space normal to the pixel shader ourselves.
  1401. if ( !fd.features[MFT_NormalMap] )
  1402. {
  1403. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1404. outNormal->setName( "outWsNormal" );
  1405. outNormal->setType( "vec3" );
  1406. outNormal->mapsToSampler = false;
  1407. // Transform the normal to world space.
  1408. meta->addStatement( new GenOp( " @ = ( @ * vec4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1409. }
  1410. // Get the input position.
  1411. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1412. if ( !inPosition )
  1413. inPosition = (Var*)LangElement::find( "position" );
  1414. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1415. output = meta;
  1416. }
  1417. void RTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1418. const MaterialFeatureData &fd )
  1419. {
  1420. // Skip out on realtime lighting if we don't have a normal
  1421. // or we're doing some sort of baked lighting.
  1422. //
  1423. // TODO: We can totally detect for this in the material
  1424. // feature setup... we should move it out of here!
  1425. //
  1426. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1427. return;
  1428. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1429. MultiLine *meta = new MultiLine;
  1430. // Look for a wsNormal or grab it from the connector.
  1431. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  1432. if ( !wsNormal )
  1433. {
  1434. Var *outWsNormal = connectComp->getElement( RT_TEXCOORD );
  1435. outWsNormal->setName( "outWsNormal" );
  1436. outWsNormal->setType( "vec3" );
  1437. wsNormal = new Var( "wsNormal", "vec3" );
  1438. // If we loaded the normal its our resposibility
  1439. // to normalize it... the interpolators won't.
  1440. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n",
  1441. new DecOp( wsNormal ), outWsNormal ) );
  1442. }
  1443. Var *wsPosition = getInWsPosition( componentList );
  1444. // If we have a specular feature then we need to
  1445. // get the world space view vector to pass to the
  1446. // lighting calculation.
  1447. Var *wsView = new Var( "wsView", "vec3" );
  1448. if ( fd.features[MFT_PixSpecular] )
  1449. {
  1450. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1451. if ( !eyePos )
  1452. {
  1453. eyePos = new Var;
  1454. eyePos->setType( "vec3" );
  1455. eyePos->setName( "eyePosWorld" );
  1456. eyePos->uniform = true;
  1457. eyePos->constSortPos = cspPass;
  1458. }
  1459. meta->addStatement( new GenOp( " @ = normalize( @ - @ );\r\n",
  1460. new DecOp( wsView ), eyePos, wsPosition ) );
  1461. }
  1462. else
  1463. meta->addStatement( new GenOp( " @ = vec3( 0 );\r\n", new DecOp( wsView ) ) );
  1464. // Create temporaries to hold results of lighting.
  1465. Var *rtShading = new Var( "rtShading", "vec4" );
  1466. Var *specular = new Var( "specular", "vec4" );
  1467. meta->addStatement( new GenOp( " @; @;\r\n",
  1468. new DecOp( rtShading ), new DecOp( specular ) ) );
  1469. // Calculate the diffuse shading and specular powers.
  1470. meta->addStatement( new GenOp( " compute4Lights( @, @, @, @, @ );\r\n",
  1471. wsView, wsPosition, wsNormal, rtShading, specular ) );
  1472. // Look for a light mask generated from a previous
  1473. // feature (this is done for BL terrain lightmaps).
  1474. Var *lightMask = (Var*)LangElement::find( "lightMask" );
  1475. if ( lightMask )
  1476. meta->addStatement( new GenOp( " @.rgb *= @;\r\n", rtShading, lightMask ) );
  1477. // Apply the lighting to the diffuse color.
  1478. LangElement *lighting = new GenOp( "vec4( @.rgb + ambient.rgb, 1 )", rtShading );
  1479. meta->addStatement( new GenOp( " @;\r\n", assignColor( lighting, Material::Mul ) ) );
  1480. output = meta;
  1481. }
  1482. ShaderFeature::Resources RTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
  1483. {
  1484. Resources res;
  1485. // These features disable realtime lighting.
  1486. if ( !fd.features[MFT_LightMap] &&
  1487. !fd.features[MFT_ToneMap] &&
  1488. !fd.features[MFT_VertLit] )
  1489. {
  1490. // If enabled we pass the position.
  1491. res.numTexReg = 1;
  1492. // If there isn't a bump map then we pass the
  1493. // world space normal as well.
  1494. if ( !fd.features[MFT_NormalMap] )
  1495. res.numTexReg++;
  1496. }
  1497. return res;
  1498. }
  1499. //****************************************************************************
  1500. // Fog
  1501. //****************************************************************************
  1502. FogFeatGLSL::FogFeatGLSL()
  1503. : mFogDep( "shaders/common/gl/torque.glsl" )
  1504. {
  1505. addDependency( &mFogDep );
  1506. }
  1507. void FogFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1508. const MaterialFeatureData &fd )
  1509. {
  1510. MultiLine *meta = new MultiLine;
  1511. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1512. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1513. {
  1514. // Grab the eye position.
  1515. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1516. if ( !eyePos )
  1517. {
  1518. eyePos = new Var( "eyePosWorld", "vec3" );
  1519. eyePos->uniform = true;
  1520. eyePos->constSortPos = cspPass;
  1521. }
  1522. Var *fogData = new Var( "fogData", "vec3" );
  1523. fogData->uniform = true;
  1524. fogData->constSortPos = cspPass;
  1525. Var *wsPosition = new Var( "fogPos", "vec3" );
  1526. getWsPosition( componentList,
  1527. fd.features[MFT_UseInstancing],
  1528. meta,
  1529. new DecOp( wsPosition ) );
  1530. // We pass the fog amount to the pixel shader.
  1531. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1532. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1533. fogAmount->setName( "fogAmount" );
  1534. fogAmount->setType( "float" );
  1535. fogAmount->mapsToSampler = false;
  1536. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1537. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1538. }
  1539. else
  1540. {
  1541. // We fog in world space... make sure the world space
  1542. // position is passed to the pixel shader. This is
  1543. // often already passed for lighting, so it takes up
  1544. // no extra output registers.
  1545. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1546. }
  1547. output = meta;
  1548. }
  1549. void FogFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1550. const MaterialFeatureData &fd )
  1551. {
  1552. MultiLine *meta = new MultiLine;
  1553. Var *fogColor = new Var;
  1554. fogColor->setType( "vec4" );
  1555. fogColor->setName( "fogColor" );
  1556. fogColor->uniform = true;
  1557. fogColor->constSortPos = cspPass;
  1558. // Get the out color.
  1559. Var *color = (Var*) LangElement::find( "col" );
  1560. if ( !color )
  1561. {
  1562. color = new Var;
  1563. color->setType( "vec4" );
  1564. color->setName( "col" );
  1565. }
  1566. Var *fogAmount;
  1567. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1568. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1569. {
  1570. // Per-vertex.... just get the fog amount.
  1571. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1572. fogAmount = connectComp->getElement( RT_TEXCOORD );
  1573. fogAmount->setName( "fogAmount" );
  1574. fogAmount->setType( "float" );
  1575. }
  1576. else
  1577. {
  1578. Var *wsPosition = getInWsPosition( componentList );
  1579. // grab the eye position
  1580. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1581. if ( !eyePos )
  1582. {
  1583. eyePos = new Var( "eyePosWorld", "vec3" );
  1584. eyePos->uniform = true;
  1585. eyePos->constSortPos = cspPass;
  1586. }
  1587. Var *fogData = new Var( "fogData", "vec3" );
  1588. fogData->uniform = true;
  1589. fogData->constSortPos = cspPass;
  1590. /// Get the fog amount.
  1591. fogAmount = new Var( "fogAmount", "float" );
  1592. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1593. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  1594. }
  1595. // Lerp between the fog color and diffuse color.
  1596. LangElement *fogLerp = new GenOp( "mix( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  1597. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  1598. output = meta;
  1599. }
  1600. ShaderFeature::Resources FogFeatGLSL::getResources( const MaterialFeatureData &fd )
  1601. {
  1602. Resources res;
  1603. res.numTexReg = 1;
  1604. return res;
  1605. }
  1606. //****************************************************************************
  1607. // Visibility
  1608. //****************************************************************************
  1609. void VisibilityFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1610. const MaterialFeatureData &fd )
  1611. {
  1612. // Pass screen space position to pixel shader to compute a full screen buffer uv
  1613. Var* ssPos = ( Var* ) LangElement::find( "screenspacePos" );
  1614. if( !ssPos )
  1615. {
  1616. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[C_CONNECTOR] );
  1617. AssertFatal( connectComp, "VisibilityFeatGLSL::processVert - invalid ShaderConnector" );
  1618. Var* ssPos = connectComp->getElement( RT_TEXCOORD );
  1619. ssPos->setName( "screenspacePos" );
  1620. ssPos->setType( "vec4" );
  1621. Var* outPosition = ( Var* ) LangElement::find( "gl_Position" );
  1622. AssertFatal( outPosition, "VisibilityFeatGLSL::processVert - No gl_Position" );
  1623. output = new GenOp( " @ = @;\r\n", ssPos, outPosition );
  1624. }
  1625. }
  1626. void VisibilityFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1627. const MaterialFeatureData &fd )
  1628. {
  1629. // Look up output color.
  1630. Var* color = ( Var* ) LangElement::find( "col" );
  1631. if( !color )
  1632. {
  1633. output = NULL;
  1634. return;
  1635. }
  1636. Var* visibility = (Var*)LangElement::find( "visibility" );
  1637. // Create visibility var.
  1638. if ( !visibility )
  1639. {
  1640. visibility = new Var();
  1641. visibility->setType( "float" );
  1642. visibility->setName( "visibility" );
  1643. visibility->uniform = true;
  1644. visibility->constSortPos = cspPotentialPrimitive;
  1645. }
  1646. Var* ssPos = ( Var* ) LangElement::find( "screenspacePos" );
  1647. if( !ssPos )
  1648. {
  1649. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1650. AssertFatal( connectComp, "VisibilityFeatGLSL::processPix - invalid ShaderConnector" );
  1651. ssPos = connectComp->getElement( RT_TEXCOORD );
  1652. ssPos->setName( "screenspacePos" );
  1653. ssPos->setType( "vec4" );
  1654. }
  1655. MultiLine* meta = new MultiLine;
  1656. output = meta;
  1657. //Compute final visibility from incoming visibility and strength of fade effects
  1658. Var *finalVisibility = new Var( "finalVisibility", "float" );
  1659. meta->addStatement(new GenOp( " @ = @;\r\n", new DecOp( finalVisibility ), visibility ) );
  1660. Var *imposterFade = (Var*) LangElement::find( "imposterFade" );
  1661. if ( imposterFade )
  1662. meta->addStatement( new GenOp( " @ *= @;\r\n", finalVisibility, imposterFade ) );
  1663. Var *foliageFade = (Var*) LangElement::find( "foliageFade" );
  1664. if ( foliageFade )
  1665. meta->addStatement( new GenOp( " @ *= @;\r\n", finalVisibility, foliageFade ) );
  1666. if( !fd.features[ MFT_IsTranslucent ] )
  1667. {
  1668. // Create fizzMap sampler.
  1669. Var* fizzMap = ( Var* ) LangElement::find( "fizzMap" );
  1670. if( !fizzMap )
  1671. {
  1672. fizzMap = new Var;
  1673. fizzMap->setType( "sampler2D" );
  1674. fizzMap->setName( "fizzMap" );
  1675. fizzMap->uniform = true;
  1676. fizzMap->sampler = true;
  1677. fizzMap->constNum = Var::getTexUnitNum();
  1678. }
  1679. // Create fizzScale uniform.
  1680. Var* fizzScale = new Var;
  1681. fizzScale->setType( "float2" );
  1682. fizzScale->setName( "fizzScale" );
  1683. fizzScale->uniform = true;
  1684. fizzScale->constSortPos = cspPass;
  1685. meta->addStatement( new GenOp( " float fizz = tex2D( @, (@.xy / @.w) * fizzScale ).r + 0.15;\r\n", fizzMap, ssPos, ssPos ) );
  1686. meta->addStatement( new GenOp( " if( ( fizz * @ - 0.329 ) < 0.0 )\r\n"
  1687. " discard\r\n;", finalVisibility ) );
  1688. }
  1689. else if( color )
  1690. {
  1691. meta->addStatement( new GenOp( " @.w *= @;\r\n", color, finalVisibility ) );
  1692. }
  1693. }
  1694. ShaderFeature::Resources VisibilityFeatGLSL::getResources( const MaterialFeatureData &fd )
  1695. {
  1696. Resources res;
  1697. if ( !fd.features[ MFT_IsTranslucent ] )
  1698. {
  1699. res.numTex = 1;
  1700. res.numTexReg = 1;
  1701. }
  1702. return res;
  1703. }
  1704. void VisibilityFeatGLSL::setTexData( Material::StageData &stageDat,
  1705. const MaterialFeatureData &fd,
  1706. RenderPassData &passData,
  1707. U32 &texIndex )
  1708. {
  1709. if ( !fd.features[ MFT_IsTranslucent ] )
  1710. {
  1711. GFXTexHandle texHandle( "core/art/fizz_noise.dds", &GFXDefaultStaticDiffuseProfile, "VisibilityFeatHLSL_fizz_noise" );
  1712. stageDat.setTex( MFT_Visibility, texHandle );
  1713. GFXTextureObject *tex = stageDat.getTex( MFT_Visibility );
  1714. if ( tex )
  1715. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1716. }
  1717. }
  1718. //****************************************************************************
  1719. // AlphaTest
  1720. //****************************************************************************
  1721. void AlphaTestGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1722. const MaterialFeatureData &fd )
  1723. {
  1724. // If we're below SM3 and don't have a depth output
  1725. // feature then don't waste an instruction here.
  1726. if ( GFX->getPixelShaderVersion() < 3.0 &&
  1727. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  1728. !fd.features[ MFT_DepthOut ] )
  1729. {
  1730. output = NULL;
  1731. return;
  1732. }
  1733. // If we don't have a color var then we cannot do an alpha test.
  1734. Var *color = (Var*)LangElement::find( "col" );
  1735. if ( !color )
  1736. {
  1737. output = NULL;
  1738. return;
  1739. }
  1740. // Now grab the alpha test value.
  1741. Var *alphaTestVal = new Var;
  1742. alphaTestVal->setType( "float" );
  1743. alphaTestVal->setName( "alphaTestValue" );
  1744. alphaTestVal->uniform = true;
  1745. alphaTestVal->constSortPos = cspPotentialPrimitive;
  1746. // Do the clip.
  1747. output = new GenOp( " if ( ( @.a - @ ) < 0 ) discard;\r\n", color, alphaTestVal );
  1748. }
  1749. //****************************************************************************
  1750. // GlowMask
  1751. //****************************************************************************
  1752. void GlowMaskGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1753. const MaterialFeatureData &fd )
  1754. {
  1755. output = NULL;
  1756. // Get the output color... and make it black to mask out
  1757. // glow passes rendered before us.
  1758. //
  1759. // The shader compiler will optimize out all the other
  1760. // code above that doesn't contribute to the alpha mask.
  1761. Var *color = (Var*)LangElement::find( "col" );
  1762. if ( color )
  1763. output = new GenOp( " @.rgb = 0;\r\n", color );
  1764. }
  1765. //****************************************************************************
  1766. // RenderTargetZero
  1767. //****************************************************************************
  1768. void RenderTargetZeroGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  1769. {
  1770. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  1771. // This will prevent a divide by zero causing an FP special on float render targets
  1772. output = new GenOp( " @;\r\n", assignColor( new GenOp( "0.00001" ), Material::None, NULL, mOutputTargetMask ) );
  1773. }
  1774. //****************************************************************************
  1775. // HDR Output
  1776. //****************************************************************************
  1777. HDROutGLSL::HDROutGLSL()
  1778. : mTorqueDep( "shaders/common/gl/torque.glsl" )
  1779. {
  1780. addDependency( &mTorqueDep );
  1781. }
  1782. void HDROutGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1783. const MaterialFeatureData &fd )
  1784. {
  1785. // Let the helper function do the work.
  1786. Var *color = (Var*)LangElement::find( "col" );
  1787. if ( color )
  1788. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  1789. }
  1790. //****************************************************************************
  1791. // FoliageFeatureGLSL
  1792. //****************************************************************************
  1793. #include "T3D/fx/groundCover.h"
  1794. FoliageFeatureGLSL::FoliageFeatureGLSL()
  1795. : mDep( "shaders/common/gl/foliage.glsl" )
  1796. {
  1797. addDependency( &mDep );
  1798. }
  1799. void FoliageFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1800. const MaterialFeatureData &fd )
  1801. {
  1802. MultiLine *meta = new MultiLine;
  1803. // Get the input variables we need.
  1804. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1805. if ( !inPosition ) {
  1806. // inPosition = (Var*)LangElement::find( "position" );
  1807. inPosition = new Var( "inPosition", "vec3" );
  1808. meta->addStatement( new GenOp( " @ = @;\n", new DecOp( inPosition ), (Var*)LangElement::find( "position" ) ) );
  1809. }
  1810. Var *inColor = (Var*)LangElement::find( "diffuse" );
  1811. Var *outColor = new Var( "inDiffuse", "vec4" );
  1812. meta->addStatement( new GenOp( " @ = @;\n", new DecOp( outColor ), inColor ) );
  1813. Var *inParams = (Var*)LangElement::find( "texCoord" );
  1814. Var *outParams = getOutTexCoord( "texCoord",
  1815. "vec2",
  1816. true,
  1817. fd.features[MFT_TexAnim],
  1818. meta,
  1819. componentList );
  1820. // Declare the normal and tangent variables since they do not exist
  1821. // in this vert type, but we do need to set them up for others.
  1822. Var *inNormal = (Var*)LangElement::find( "inNormal" );
  1823. if ( !inNormal ) {
  1824. inNormal = new Var( "inNormal", "vec3" );
  1825. meta->addStatement( new GenOp( " @ = @;\n", new DecOp( inNormal ), (Var*)LangElement::find( "normal" ) ) );
  1826. }
  1827. //Var *normal = (Var*)LangElement::find( "normal" );
  1828. AssertFatal( inNormal, "FoliageFeatureHLSL requires vert normal!" );
  1829. Var *tangent = new Var;
  1830. tangent->setType( "vec3" );
  1831. tangent->setName( "T" );
  1832. LangElement *tangentDec = new DecOp( tangent );
  1833. meta->addStatement( new GenOp( " @;\n", tangentDec ) );
  1834. // We add a float foliageFade to the OUT structure.
  1835. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1836. Var *fade = connectComp->getElement( RT_TEXCOORD );
  1837. fade->setName( "foliageFade" );
  1838. fade->setType( "float" );
  1839. // grab the eye position
  1840. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1841. if ( !eyePos )
  1842. {
  1843. eyePos = new Var( "eyePosWorld", "vec3" );
  1844. eyePos->uniform = true;
  1845. eyePos->constSortPos = cspPass;
  1846. }
  1847. // All actual work is offloaded to this method.
  1848. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @, @ );\r\n", inPosition, outColor, inParams, outParams, inNormal, tangent, eyePos ) );
  1849. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  1850. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, outColor ) );
  1851. output = meta;
  1852. }
  1853. void FoliageFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1854. const MaterialFeatureData &fd )
  1855. {
  1856. // Find / create IN.foliageFade
  1857. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1858. Var *fade = connectComp->getElement( RT_TEXCOORD );
  1859. fade->setName( "foliageFade" );
  1860. fade->setType( "float" );
  1861. }
  1862. void FoliageFeatureGLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  1863. {
  1864. // This isn't really necessary since the outFeatureData will be filtered after
  1865. // this call.
  1866. if ( features.hasFeature( MFT_Foliage ) )
  1867. outFeatureData->features.addFeature( type );
  1868. }
  1869. ShaderFeatureConstHandles* FoliageFeatureGLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  1870. {
  1871. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  1872. AssertFatal( gcover != NULL, "FoliageFeatureGLSL::createConstHandles - userObject was not valid!" );
  1873. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  1874. handles->mGroundCover = gcover;
  1875. handles->init( shader );
  1876. return handles;
  1877. }
  1878. //****************************************************************************
  1879. // ParticleNormal
  1880. //****************************************************************************
  1881. void ParticleNormalFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  1882. {
  1883. MultiLine *meta = new MultiLine;
  1884. output = meta;
  1885. // Calculate normal and tangent values since we want to keep particle verts
  1886. // as light-weight as possible
  1887. Var *normal = (Var*) LangElement::find("normal");
  1888. if(normal == NULL)
  1889. {
  1890. normal = new Var;
  1891. normal->setType( "vec3" );
  1892. normal->setName( "normal" );
  1893. // These values are not accidental. It is slightly adjusted from facing straight into the
  1894. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  1895. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  1896. // [11/23/2009 Pat]
  1897. meta->addStatement(new GenOp(" @ = vec3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  1898. }
  1899. Var *T = (Var*) LangElement::find( "T" );
  1900. if(T == NULL)
  1901. {
  1902. T = new Var;
  1903. T->setType( "vec3" );
  1904. T->setName( "T" );
  1905. meta->addStatement(new GenOp(" @ = vec3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  1906. }
  1907. }
  1908. //****************************************************************************
  1909. // ImposterVertFeatureGLSL
  1910. //****************************************************************************
  1911. ImposterVertFeatureGLSL::ImposterVertFeatureGLSL()
  1912. : mDep( "shaders/common/gl/imposter.glsl" )
  1913. {
  1914. addDependency( &mDep );
  1915. }
  1916. void ImposterVertFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1917. const MaterialFeatureData &fd )
  1918. {
  1919. MultiLine *meta = new MultiLine;
  1920. output = meta;
  1921. // Get the input vertex variables.
  1922. Var *inPosition = (Var*)LangElement::find( "position" );
  1923. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  1924. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  1925. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  1926. // Get the input shader constants.
  1927. Var *imposterLimits = new Var;
  1928. imposterLimits->setType( "vec4" );
  1929. imposterLimits->setName( "imposterLimits" );
  1930. imposterLimits->uniform = true;
  1931. imposterLimits->constSortPos = cspPotentialPrimitive;
  1932. Var *imposterUVs = new Var;
  1933. imposterUVs->setType( "vec4" );
  1934. imposterUVs->setName( "imposterUVs" );
  1935. imposterUVs->arraySize = 64; // See imposter.hlsl
  1936. imposterUVs->uniform = true;
  1937. imposterUVs->constSortPos = cspPotentialPrimitive;
  1938. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1939. if ( !eyePos )
  1940. {
  1941. eyePos = new Var( "eyePosWorld", "vec3" );
  1942. eyePos->uniform = true;
  1943. eyePos->constSortPos = cspPass;
  1944. }
  1945. // Declare the outputs from this feature.
  1946. Var *outInPosition = new Var;
  1947. outInPosition->setType( "vec3" );
  1948. outInPosition->setName( "inPosition" );
  1949. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  1950. Var *outTexCoord = new Var;
  1951. outTexCoord->setType( "vec2" );
  1952. outTexCoord->setName( "texCoord" );
  1953. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  1954. Var *outWorldToTangent = new Var;
  1955. outWorldToTangent->setType( "mat3" );
  1956. outWorldToTangent->setName( "worldToTangent" );
  1957. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  1958. //Var *outWorldToTangent = getOutWorldToTangent( componentList, meta, fd );
  1959. // Add imposterFade to the OUT structure.
  1960. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1961. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  1962. outFade->setName( "imposterFade" );
  1963. outFade->setType( "float" );
  1964. // Assign OUT.imposterFade
  1965. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  1966. // All actual work is done in this method.
  1967. meta->addStatement( new GenOp( " imposter_v( @.xyz, int(@.w), @.x * length(@), normalize(@), normalize(@), int(@.y), int(@.x), @.z, bool(@.w), @, @, @, @, @ );\r\n",
  1968. inPosition,
  1969. inPosition,
  1970. inMiscParams,
  1971. inRightVec,
  1972. inUpVec,
  1973. inRightVec,
  1974. imposterLimits,
  1975. imposterLimits,
  1976. imposterLimits,
  1977. imposterLimits,
  1978. eyePos,
  1979. imposterUVs,
  1980. outInPosition,
  1981. outTexCoord,
  1982. outWorldToTangent ) );
  1983. // Copy the position to wsPosition for use in shaders
  1984. // down stream instead of looking for objTrans.
  1985. Var *wsPosition = new Var;
  1986. wsPosition->setType( "vec3" );
  1987. wsPosition->setName( "wsPosition" );
  1988. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  1989. // If we new viewToTangent... its the same as the
  1990. // world to tangent for an imposter.
  1991. Var *viewToTangent = new Var;
  1992. viewToTangent->setType( "mat3" );
  1993. viewToTangent->setName( "viewToTangent" );
  1994. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  1995. }
  1996. void ImposterVertFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1997. const MaterialFeatureData &fd )
  1998. {
  1999. // Find / create IN.imposterFade
  2000. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2001. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2002. fade->setName( "imposterFade" );
  2003. fade->setType( "float" );
  2004. /*
  2005. // Find / create visibility
  2006. Var *visibility = (Var*) LangElement::find( "visibility" );
  2007. if ( !visibility )
  2008. {
  2009. visibility = new Var();
  2010. visibility->setType( "float" );
  2011. visibility->setName( "visibility" );
  2012. visibility->uniform = true;
  2013. visibility->constSortPos = cspPotentialPrimitive;
  2014. }
  2015. MultiLine *meta = new MultiLine;
  2016. // Multiply foliageFade into visibility.
  2017. //meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2018. output = meta;
  2019. */
  2020. }
  2021. void ImposterVertFeatureGLSL::determineFeature( Material *material,
  2022. const GFXVertexFormat *vertexFormat,
  2023. U32 stageNum,
  2024. const FeatureType &type,
  2025. const FeatureSet &features,
  2026. MaterialFeatureData *outFeatureData )
  2027. {
  2028. if ( features.hasFeature( MFT_ImposterVert ) )
  2029. outFeatureData->features.addFeature( MFT_ImposterVert );
  2030. }