shaderFeatureGLSL.cpp 92 KB

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