shaderFeatureGLSL.cpp 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722
  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(mat3( @ ), @).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. // grab the eye position
  1425. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1426. if ( !eyePos )
  1427. {
  1428. eyePos = new Var( "eyePosWorld", "vec3" );
  1429. eyePos->uniform = true;
  1430. eyePos->constSortPos = cspPass;
  1431. }
  1432. // cube position
  1433. Var * cubePos = new Var;
  1434. cubePos->setName( "cubePos" );
  1435. cubePos->setType( "vec3" );
  1436. LangElement *cubePosDecl = new DecOp( cubePos );
  1437. meta->addStatement( new GenOp( " @ = vec3( @[3][0], @[3][1], @[3][2] );\r\n",
  1438. cubePosDecl, cubeTrans, cubeTrans, cubeTrans ) );
  1439. // eye to vert
  1440. Var * eyeToVert = new Var;
  1441. eyeToVert->setName( "eyeToVert" );
  1442. eyeToVert->setType( "vec3" );
  1443. LangElement *e2vDecl = new DecOp( eyeToVert );
  1444. meta->addStatement( new GenOp( " @ = @ - ( @ - @ );\r\n",
  1445. e2vDecl, cubeVertPos, eyePos, cubePos ) );
  1446. // grab connector texcoord register
  1447. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1448. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1449. reflectVec->setName( "reflectVec" );
  1450. reflectVec->setStructName( "OUT" );
  1451. reflectVec->setType( "vec3" );
  1452. reflectVec->mapsToSampler = true;
  1453. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1454. output = meta;
  1455. }
  1456. void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1457. const MaterialFeatureData &fd )
  1458. {
  1459. MultiLine * meta = new MultiLine;
  1460. Var *glossColor = NULL;
  1461. // If a base or bump tex is present in the material, but not in the
  1462. // current pass - we need to add one to the current pass to use
  1463. // its alpha channel as a gloss map.
  1464. if( !fd.features[MFT_DiffuseMap] &&
  1465. !fd.features[MFT_NormalMap] )
  1466. {
  1467. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1468. fd.materialFeatures[MFT_NormalMap] )
  1469. {
  1470. // grab connector texcoord register
  1471. Var *inTex = getInTexCoord( "texCoord", "vec2", true, componentList );
  1472. // create texture var
  1473. Var *newMap = new Var;
  1474. newMap->setType( "sampler2D" );
  1475. newMap->setName( "glossMap" );
  1476. newMap->uniform = true;
  1477. newMap->sampler = true;
  1478. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1479. // create sample color
  1480. Var *color = new Var;
  1481. color->setType( "vec4" );
  1482. color->setName( "diffuseColor" );
  1483. LangElement *colorDecl = new DecOp( color );
  1484. glossColor = color;
  1485. meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
  1486. }
  1487. }
  1488. else
  1489. {
  1490. glossColor = (Var*) LangElement::find( "diffuseColor" );
  1491. if( !glossColor )
  1492. glossColor = (Var*) LangElement::find( "bumpNormal" );
  1493. }
  1494. // grab connector texcoord register
  1495. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1496. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1497. reflectVec->setName( "reflectVec" );
  1498. reflectVec->setStructName( "IN" );
  1499. reflectVec->setType( "vec3" );
  1500. reflectVec->mapsToSampler = true;
  1501. // create cubemap var
  1502. Var *cubeMap = new Var;
  1503. cubeMap->setType( "samplerCUBE" );
  1504. cubeMap->setName( "cubeMap" );
  1505. cubeMap->uniform = true;
  1506. cubeMap->sampler = true;
  1507. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1508. // TODO: Restore the lighting attenuation here!
  1509. Var *attn = NULL;
  1510. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1511. //attn = (Var*)LangElement::find("attn");
  1512. //else
  1513. if ( fd.materialFeatures[MFT_RTLighting] )
  1514. attn =(Var*)LangElement::find("d_NL_Att");
  1515. LangElement *texCube = new GenOp( "texCUBE( @, @ )", cubeMap, reflectVec );
  1516. LangElement *lerpVal = NULL;
  1517. Material::BlendOp blendOp = Material::LerpAlpha;
  1518. // Note that the lerpVal needs to be a float4 so that
  1519. // it will work with the LerpAlpha blend.
  1520. if ( glossColor )
  1521. {
  1522. if ( attn )
  1523. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1524. else
  1525. lerpVal = glossColor;
  1526. }
  1527. else
  1528. {
  1529. if ( attn )
  1530. lerpVal = new GenOp( "saturate( @ ).xxxx", attn );
  1531. else
  1532. blendOp = Material::Mul;
  1533. }
  1534. meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
  1535. output = meta;
  1536. }
  1537. ShaderFeature::Resources ReflectCubeFeatGLSL::getResources( const MaterialFeatureData &fd )
  1538. {
  1539. Resources res;
  1540. if( fd.features[MFT_DiffuseMap] ||
  1541. fd.features[MFT_NormalMap] )
  1542. {
  1543. res.numTex = 1;
  1544. res.numTexReg = 1;
  1545. }
  1546. else
  1547. {
  1548. res.numTex = 2;
  1549. res.numTexReg = 2;
  1550. }
  1551. return res;
  1552. }
  1553. void ReflectCubeFeatGLSL::setTexData( Material::StageData &stageDat,
  1554. const MaterialFeatureData &stageFeatures,
  1555. RenderPassData &passData,
  1556. U32 &texIndex )
  1557. {
  1558. // set up a gloss map if one is not present in the current pass
  1559. // but is present in the current material stage
  1560. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1561. !passData.mFeatureData.features[MFT_NormalMap] )
  1562. {
  1563. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1564. if ( tex && stageFeatures.features[MFT_DiffuseMap] )
  1565. {
  1566. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  1567. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1568. }
  1569. else
  1570. {
  1571. tex = stageDat.getTex( MFT_NormalMap );
  1572. if ( tex && stageFeatures.features[ MFT_NormalMap ] )
  1573. {
  1574. passData.mSamplerNames[ texIndex ] = "bumpMap";
  1575. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1576. }
  1577. }
  1578. }
  1579. if( stageDat.getCubemap() )
  1580. {
  1581. passData.mCubeMap = stageDat.getCubemap();
  1582. passData.mSamplerNames[texIndex] = "cubeMap";
  1583. passData.mTexType[texIndex++] = Material::Cube;
  1584. }
  1585. else
  1586. {
  1587. if( stageFeatures.features[MFT_CubeMap] )
  1588. {
  1589. // assuming here that it is a scenegraph cubemap
  1590. passData.mSamplerNames[texIndex] = "cubeMap";
  1591. passData.mTexType[texIndex++] = Material::SGCube;
  1592. }
  1593. }
  1594. }
  1595. //****************************************************************************
  1596. // RTLighting
  1597. //****************************************************************************
  1598. RTLightingFeatGLSL::RTLightingFeatGLSL()
  1599. : mDep( "shaders/common/gl/lighting.glsl" )
  1600. {
  1601. addDependency( &mDep );
  1602. }
  1603. void RTLightingFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1604. const MaterialFeatureData &fd )
  1605. {
  1606. MultiLine *meta = new MultiLine;
  1607. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1608. // Special case for lighting imposters. We dont have a vert normal and may not
  1609. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1610. if ( fd.features[MFT_ImposterVert] )
  1611. {
  1612. if ( !fd.features[MFT_NormalMap] )
  1613. {
  1614. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1615. if ( !eyePos )
  1616. {
  1617. eyePos = new Var( "eyePosWorld", "vec3" );
  1618. eyePos->uniform = true;
  1619. eyePos->constSortPos = cspPass;
  1620. }
  1621. Var *inPosition = (Var*)LangElement::find( "position" );
  1622. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1623. outNormal->setName( "wsNormal" );
  1624. outNormal->setStructName( "OUT" );
  1625. outNormal->setType( "vec3" );
  1626. outNormal->mapsToSampler = false;
  1627. // Transform the normal to world space.
  1628. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );
  1629. }
  1630. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1631. output = meta;
  1632. return;
  1633. }
  1634. // Find the incoming vertex normal.
  1635. Var *inNormal = (Var*)LangElement::find( "normal" );
  1636. // Skip out on realtime lighting if we don't have a normal
  1637. // or we're doing some sort of baked lighting.
  1638. if ( !inNormal ||
  1639. fd.features[MFT_LightMap] ||
  1640. fd.features[MFT_ToneMap] ||
  1641. fd.features[MFT_VertLit] )
  1642. return;
  1643. // If there isn't a normal map then we need to pass
  1644. // the world space normal to the pixel shader ourselves.
  1645. if ( !fd.features[MFT_NormalMap] )
  1646. {
  1647. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1648. outNormal->setName( "wsNormal" );
  1649. outNormal->setStructName( "OUT" );
  1650. outNormal->setType( "vec3" );
  1651. outNormal->mapsToSampler = false;
  1652. // Get the transform to world space.
  1653. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1654. // Transform the normal to world space.
  1655. meta->addStatement( new GenOp( " @ = tMul( @, float4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1656. }
  1657. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1658. output = meta;
  1659. }
  1660. void RTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1661. const MaterialFeatureData &fd )
  1662. {
  1663. // Skip out on realtime lighting if we don't have a normal
  1664. // or we're doing some sort of baked lighting.
  1665. //
  1666. // TODO: We can totally detect for this in the material
  1667. // feature setup... we should move it out of here!
  1668. //
  1669. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1670. return;
  1671. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1672. MultiLine *meta = new MultiLine;
  1673. // Look for a wsNormal or grab it from the connector.
  1674. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  1675. if ( !wsNormal )
  1676. {
  1677. wsNormal = connectComp->getElement( RT_TEXCOORD );
  1678. wsNormal->setName( "wsNormal" );
  1679. wsNormal->setStructName( "IN" );
  1680. wsNormal->setType( "vec3" );
  1681. // If we loaded the normal its our responsibility
  1682. // to normalize it... the interpolators won't.
  1683. //
  1684. // Note we cast to half here to get partial precision
  1685. // optimized code which is an acceptable loss of
  1686. // precision for normals and performs much better
  1687. // on older Geforce cards.
  1688. //
  1689. meta->addStatement( new GenOp( " @ = normalize( half3( @ ) );\r\n", wsNormal, wsNormal ) );
  1690. }
  1691. // Now the wsPosition and wsView.
  1692. Var *wsPosition = getInWsPosition( componentList );
  1693. Var *wsView = getWsView( wsPosition, meta );
  1694. // Create temporaries to hold results of lighting.
  1695. Var *rtShading = new Var( "rtShading", "vec4" );
  1696. Var *specular = new Var( "specular", "vec4" );
  1697. meta->addStatement( new GenOp( " @; @;\r\n",
  1698. new DecOp( rtShading ), new DecOp( specular ) ) );
  1699. // Look for a light mask generated from a previous
  1700. // feature (this is done for BL terrain lightmaps).
  1701. LangElement *lightMask = LangElement::find( "lightMask" );
  1702. if ( !lightMask )
  1703. lightMask = new GenOp( "float4( 1, 1, 1, 1 )" );
  1704. // Get all the light constants.
  1705. Var *inLightPos = new Var( "inLightPos", "vec4" );
  1706. inLightPos->uniform = true;
  1707. inLightPos->arraySize = 3;
  1708. inLightPos->constSortPos = cspPotentialPrimitive;
  1709. Var *inLightInvRadiusSq = new Var( "inLightInvRadiusSq", "vec4" );
  1710. inLightInvRadiusSq->uniform = true;
  1711. inLightInvRadiusSq->constSortPos = cspPotentialPrimitive;
  1712. Var *inLightColor = new Var( "inLightColor", "vec4" );
  1713. inLightColor->uniform = true;
  1714. inLightColor->arraySize = 4;
  1715. inLightColor->constSortPos = cspPotentialPrimitive;
  1716. Var *inLightSpotDir = new Var( "inLightSpotDir", "vec4" );
  1717. inLightSpotDir->uniform = true;
  1718. inLightSpotDir->arraySize = 3;
  1719. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1720. Var *inLightSpotAngle = new Var( "inLightSpotAngle", "vec4" );
  1721. inLightSpotAngle->uniform = true;
  1722. inLightSpotAngle->constSortPos = cspPotentialPrimitive;
  1723. Var *lightSpotFalloff = new Var( "inLightSpotFalloff", "vec4" );
  1724. lightSpotFalloff->uniform = true;
  1725. lightSpotFalloff->constSortPos = cspPotentialPrimitive;
  1726. Var *specularPower = new Var( "specularPower", "float" );
  1727. specularPower->uniform = true;
  1728. specularPower->constSortPos = cspPotentialPrimitive;
  1729. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  1730. if ( !specularColor )
  1731. {
  1732. specularColor = new Var( "specularColor", "vec4" );
  1733. specularColor->uniform = true;
  1734. specularColor->constSortPos = cspPotentialPrimitive;
  1735. }
  1736. Var *ambient = new Var( "ambient", "vec4" );
  1737. ambient->uniform = true;
  1738. ambient->constSortPos = cspPass;
  1739. // Calculate the diffuse shading and specular powers.
  1740. meta->addStatement( new GenOp( " compute4Lights( @, @, @, @,\r\n"
  1741. " @, @, @, @, @, @, @, @,\r\n"
  1742. " @, @ );\r\n",
  1743. wsView, wsPosition, wsNormal, lightMask,
  1744. inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, specularPower, specularColor,
  1745. rtShading, specular ) );
  1746. // Apply the lighting to the diffuse color.
  1747. LangElement *lighting = new GenOp( "float4( @.rgb + @.rgb, 1 )", rtShading, ambient );
  1748. meta->addStatement( new GenOp( " @;\r\n", assignColor( lighting, Material::Mul ) ) );
  1749. output = meta;
  1750. }
  1751. ShaderFeature::Resources RTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
  1752. {
  1753. Resources res;
  1754. // These features disable realtime lighting.
  1755. if ( !fd.features[MFT_LightMap] &&
  1756. !fd.features[MFT_ToneMap] &&
  1757. !fd.features[MFT_VertLit] )
  1758. {
  1759. // If enabled we pass the position.
  1760. res.numTexReg = 1;
  1761. // If there isn't a bump map then we pass the
  1762. // world space normal as well.
  1763. if ( !fd.features[MFT_NormalMap] )
  1764. res.numTexReg++;
  1765. }
  1766. return res;
  1767. }
  1768. //****************************************************************************
  1769. // Fog
  1770. //****************************************************************************
  1771. FogFeatGLSL::FogFeatGLSL()
  1772. : mFogDep( "shaders/common/gl/torque.glsl" )
  1773. {
  1774. addDependency( &mFogDep );
  1775. }
  1776. void FogFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1777. const MaterialFeatureData &fd )
  1778. {
  1779. MultiLine *meta = new MultiLine;
  1780. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1781. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1782. {
  1783. // Grab the eye position.
  1784. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1785. if ( !eyePos )
  1786. {
  1787. eyePos = new Var( "eyePosWorld", "vec3" );
  1788. eyePos->uniform = true;
  1789. eyePos->constSortPos = cspPass;
  1790. }
  1791. Var *fogData = new Var( "fogData", "vec3" );
  1792. fogData->uniform = true;
  1793. fogData->constSortPos = cspPass;
  1794. Var *wsPosition = new Var( "fogPos", "vec3" );
  1795. getWsPosition( componentList,
  1796. fd.features[MFT_UseInstancing],
  1797. meta,
  1798. new DecOp( wsPosition ) );
  1799. // We pass the fog amount to the pixel shader.
  1800. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1801. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1802. fogAmount->setName( "fogAmount" );
  1803. fogAmount->setStructName( "OUT" );
  1804. fogAmount->setType( "float" );
  1805. fogAmount->mapsToSampler = false;
  1806. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1807. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1808. }
  1809. else
  1810. {
  1811. // We fog in world space... make sure the world space
  1812. // position is passed to the pixel shader. This is
  1813. // often already passed for lighting, so it takes up
  1814. // no extra output registers.
  1815. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1816. }
  1817. output = meta;
  1818. }
  1819. void FogFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1820. const MaterialFeatureData &fd )
  1821. {
  1822. MultiLine *meta = new MultiLine;
  1823. Var *fogColor = new Var;
  1824. fogColor->setType( "vec4" );
  1825. fogColor->setName( "fogColor" );
  1826. fogColor->uniform = true;
  1827. fogColor->constSortPos = cspPass;
  1828. // Get the out color.
  1829. Var *color = (Var*) LangElement::find( "col" );
  1830. if ( !color )
  1831. {
  1832. color = new Var;
  1833. color->setType( "vec4" );
  1834. color->setName( "col" );
  1835. color->setStructName("OUT");
  1836. }
  1837. Var *fogAmount;
  1838. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1839. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1840. {
  1841. // Per-vertex.... just get the fog amount.
  1842. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1843. fogAmount = connectComp->getElement( RT_TEXCOORD );
  1844. fogAmount->setName( "fogAmount" );
  1845. fogAmount->setStructName( "IN" );
  1846. fogAmount->setType( "float" );
  1847. }
  1848. else
  1849. {
  1850. Var *wsPosition = getInWsPosition( componentList );
  1851. // grab the eye position
  1852. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1853. if ( !eyePos )
  1854. {
  1855. eyePos = new Var( "eyePosWorld", "vec3" );
  1856. eyePos->uniform = true;
  1857. eyePos->constSortPos = cspPass;
  1858. }
  1859. Var *fogData = new Var( "fogData", "vec3" );
  1860. fogData->uniform = true;
  1861. fogData->constSortPos = cspPass;
  1862. /// Get the fog amount.
  1863. fogAmount = new Var( "fogAmount", "float" );
  1864. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1865. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  1866. }
  1867. // Lerp between the fog color and diffuse color.
  1868. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  1869. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  1870. output = meta;
  1871. }
  1872. ShaderFeature::Resources FogFeatGLSL::getResources( const MaterialFeatureData &fd )
  1873. {
  1874. Resources res;
  1875. res.numTexReg = 1;
  1876. return res;
  1877. }
  1878. //****************************************************************************
  1879. // Visibility
  1880. //****************************************************************************
  1881. VisibilityFeatGLSL::VisibilityFeatGLSL()
  1882. : mTorqueDep( "shaders/common/gl/torque.glsl" )
  1883. {
  1884. addDependency( &mTorqueDep );
  1885. }
  1886. void VisibilityFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1887. const MaterialFeatureData &fd )
  1888. {
  1889. MultiLine *meta = new MultiLine;
  1890. output = meta;
  1891. if ( fd.features[ MFT_UseInstancing ] )
  1892. {
  1893. // We pass the visibility to the pixel shader via
  1894. // another output register.
  1895. //
  1896. // TODO: We should see if we can share this register
  1897. // with some other common instanced data.
  1898. //
  1899. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1900. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  1901. outVisibility->setStructName( "OUT" );
  1902. outVisibility->setName( "visibility" );
  1903. outVisibility->setType( "float" );
  1904. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  1905. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  1906. instVisibility->setStructName( "IN" );
  1907. instVisibility->setName( "inst_visibility" );
  1908. instVisibility->setType( "float" );
  1909. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  1910. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  1911. }
  1912. if ( fd.features[ MFT_IsTranslucent ] )
  1913. return;
  1914. addOutVpos( meta, componentList );
  1915. }
  1916. void VisibilityFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1917. const MaterialFeatureData &fd )
  1918. {
  1919. // Get the visibility constant.
  1920. Var *visibility = NULL;
  1921. if ( fd.features[ MFT_UseInstancing ] )
  1922. visibility = getInTexCoord( "visibility", "float", false, componentList );
  1923. else
  1924. {
  1925. visibility = (Var*)LangElement::find( "visibility" );
  1926. if ( !visibility )
  1927. {
  1928. visibility = new Var();
  1929. visibility->setType( "float" );
  1930. visibility->setName( "visibility" );
  1931. visibility->uniform = true;
  1932. visibility->constSortPos = cspPotentialPrimitive;
  1933. }
  1934. }
  1935. MultiLine* meta = new MultiLine;
  1936. output = meta;
  1937. // Translucent objects do a simple alpha fade.
  1938. if ( fd.features[ MFT_IsTranslucent ] )
  1939. {
  1940. Var *color = (Var*) LangElement::find( "col" );
  1941. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  1942. return;
  1943. }
  1944. // Everything else does a fizzle.
  1945. Var *vPos = getInVpos( meta, componentList );
  1946. meta->addStatement( new GenOp( " fizzle( @, @ );\r\n", vPos, visibility ) );
  1947. }
  1948. ShaderFeature::Resources VisibilityFeatGLSL::getResources( const MaterialFeatureData &fd )
  1949. {
  1950. Resources res;
  1951. // TODO: Fix for instancing.
  1952. if ( !fd.features[ MFT_IsTranslucent ] )
  1953. res.numTexReg = 1;
  1954. return res;
  1955. }
  1956. //****************************************************************************
  1957. // AlphaTest
  1958. //****************************************************************************
  1959. void AlphaTestGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1960. const MaterialFeatureData &fd )
  1961. {
  1962. // If we're below SM3 and don't have a depth output
  1963. // feature then don't waste an instruction here.
  1964. if ( GFX->getPixelShaderVersion() < 3.0 &&
  1965. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  1966. !fd.features[ MFT_DepthOut ] )
  1967. {
  1968. output = NULL;
  1969. return;
  1970. }
  1971. // If we don't have a color var then we cannot do an alpha test.
  1972. Var *color = (Var*)LangElement::find( "col" );
  1973. if ( !color )
  1974. {
  1975. output = NULL;
  1976. return;
  1977. }
  1978. // Now grab the alpha test value.
  1979. Var *alphaTestVal = new Var;
  1980. alphaTestVal->setType( "float" );
  1981. alphaTestVal->setName( "alphaTestValue" );
  1982. alphaTestVal->uniform = true;
  1983. alphaTestVal->constSortPos = cspPotentialPrimitive;
  1984. // Do the clip.
  1985. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  1986. }
  1987. //****************************************************************************
  1988. // GlowMask
  1989. //****************************************************************************
  1990. void GlowMaskGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1991. const MaterialFeatureData &fd )
  1992. {
  1993. output = NULL;
  1994. // Get the output color... and make it black to mask out
  1995. // glow passes rendered before us.
  1996. //
  1997. // The shader compiler will optimize out all the other
  1998. // code above that doesn't contribute to the alpha mask.
  1999. Var *color = (Var*)LangElement::find( "col" );
  2000. if ( color )
  2001. output = new GenOp( " @.rgb = vec3(0);\r\n", color );
  2002. }
  2003. //****************************************************************************
  2004. // RenderTargetZero
  2005. //****************************************************************************
  2006. void RenderTargetZeroGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  2007. {
  2008. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  2009. // This will prevent a divide by zero causing an FP special on float render targets
  2010. output = new GenOp( " @;\r\n", assignColor( new GenOp( "vec4(0.00001)" ), Material::None, NULL, mOutputTargetMask ) );
  2011. }
  2012. //****************************************************************************
  2013. // HDR Output
  2014. //****************************************************************************
  2015. HDROutGLSL::HDROutGLSL()
  2016. : mTorqueDep( "shaders/common/gl/torque.glsl" )
  2017. {
  2018. addDependency( &mTorqueDep );
  2019. }
  2020. void HDROutGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2021. const MaterialFeatureData &fd )
  2022. {
  2023. // Let the helper function do the work.
  2024. Var *color = (Var*)LangElement::find( "col" );
  2025. if ( color )
  2026. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2027. }
  2028. //****************************************************************************
  2029. // FoliageFeatureGLSL
  2030. //****************************************************************************
  2031. #include "T3D/fx/groundCover.h"
  2032. FoliageFeatureGLSL::FoliageFeatureGLSL()
  2033. : mDep( "shaders/common/gl/foliage.glsl" )
  2034. {
  2035. addDependency( &mDep );
  2036. }
  2037. void FoliageFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2038. const MaterialFeatureData &fd )
  2039. {
  2040. // Get the input variables we need.
  2041. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2042. if ( !inPosition )
  2043. inPosition = (Var*)LangElement::find( "position" );
  2044. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2045. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2046. MultiLine *meta = new MultiLine;
  2047. // Declare the normal and tangent variables since they do not exist
  2048. // in this vert type, but we do need to set them up for others.
  2049. Var *normal = (Var*)LangElement::find( "normal" );
  2050. AssertFatal( normal, "FoliageFeatureGLSL requires vert normal!" );
  2051. Var *tangent = new Var;
  2052. tangent->setType( "vec3" );
  2053. tangent->setName( "T" );
  2054. LangElement *tangentDec = new DecOp( tangent );
  2055. meta->addStatement( new GenOp( " @;\n", tangentDec ) );
  2056. // We add a float foliageFade to the OUT structure.
  2057. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2058. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2059. fade->setName( "foliageFade" );
  2060. fade->setStructName( "OUT" );
  2061. fade->setType( "float" );
  2062. // grab the eye position
  2063. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2064. if ( !eyePos )
  2065. {
  2066. eyePos = new Var( "eyePosWorld", "vec3" );
  2067. eyePos->uniform = true;
  2068. eyePos->constSortPos = cspPass;
  2069. }
  2070. // All actual work is offloaded to this method.
  2071. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2072. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2073. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2074. output = meta;
  2075. }
  2076. void FoliageFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2077. const MaterialFeatureData &fd )
  2078. {
  2079. // Find / create IN.foliageFade
  2080. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2081. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2082. fade->setName( "foliageFade" );
  2083. fade->setStructName( "IN" );
  2084. fade->setType( "float" );
  2085. // Find / create visibility
  2086. Var *visibility = (Var*) LangElement::find( "visibility" );
  2087. if ( !visibility )
  2088. {
  2089. visibility = new Var();
  2090. visibility->setType( "float" );
  2091. visibility->setName( "visibility" );
  2092. visibility->uniform = true;
  2093. visibility->constSortPos = cspPotentialPrimitive;
  2094. }
  2095. MultiLine *meta = new MultiLine;
  2096. // Multiply foliageFade into visibility.
  2097. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2098. output = meta;
  2099. }
  2100. void FoliageFeatureGLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2101. {
  2102. // This isn't really necessary since the outFeatureData will be filtered after
  2103. // this call.
  2104. if ( features.hasFeature( MFT_Foliage ) )
  2105. outFeatureData->features.addFeature( type );
  2106. }
  2107. ShaderFeatureConstHandles* FoliageFeatureGLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2108. {
  2109. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2110. AssertFatal( gcover != NULL, "FoliageFeatureGLSL::createConstHandles - userObject was not valid!" );
  2111. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2112. handles->mGroundCover = gcover;
  2113. handles->init( shader );
  2114. return handles;
  2115. }
  2116. void ParticleNormalFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2117. {
  2118. MultiLine *meta = new MultiLine;
  2119. output = meta;
  2120. // Calculate normal and tangent values since we want to keep particle verts
  2121. // as light-weight as possible
  2122. Var *normal = (Var*) LangElement::find("normal");
  2123. if(normal == NULL)
  2124. {
  2125. normal = new Var;
  2126. normal->setType( "vec3" );
  2127. normal->setName( "normal" );
  2128. // These values are not accidental. It is slightly adjusted from facing straight into the
  2129. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2130. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2131. // [11/23/2009 Pat]
  2132. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2133. }
  2134. Var *T = (Var*) LangElement::find( "T" );
  2135. if(T == NULL)
  2136. {
  2137. T = new Var;
  2138. T->setType( "vec3" );
  2139. T->setName( "T" );
  2140. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2141. }
  2142. }
  2143. //****************************************************************************
  2144. // ImposterVertFeatureGLSL
  2145. //****************************************************************************
  2146. ImposterVertFeatureGLSL::ImposterVertFeatureGLSL()
  2147. : mDep( "shaders/common/gl/imposter.glsl" )
  2148. {
  2149. addDependency( &mDep );
  2150. }
  2151. void ImposterVertFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2152. const MaterialFeatureData &fd )
  2153. {
  2154. MultiLine *meta = new MultiLine;
  2155. output = meta;
  2156. // Get the input vertex variables.
  2157. Var *inPosition = (Var*)LangElement::find( "position" );
  2158. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2159. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2160. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2161. // Get the input shader constants.
  2162. Var *imposterLimits = new Var;
  2163. imposterLimits->setType( "vec4" );
  2164. imposterLimits->setName( "imposterLimits" );
  2165. imposterLimits->uniform = true;
  2166. imposterLimits->constSortPos = cspPotentialPrimitive;
  2167. Var *imposterUVs = new Var;
  2168. imposterUVs->setType( "vec4" );
  2169. imposterUVs->setName( "imposterUVs" );
  2170. imposterUVs->arraySize = 64; // See imposter.glsl
  2171. imposterUVs->uniform = true;
  2172. imposterUVs->constSortPos = cspPotentialPrimitive;
  2173. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2174. if ( !eyePos )
  2175. {
  2176. eyePos = new Var( "eyePosWorld", "vec3" );
  2177. eyePos->uniform = true;
  2178. eyePos->constSortPos = cspPass;
  2179. }
  2180. // Declare the outputs from this feature.
  2181. Var *outInPosition = new Var;
  2182. outInPosition->setType( "vec3" );
  2183. outInPosition->setName( "inPosition" );
  2184. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2185. Var *outTexCoord = new Var;
  2186. outTexCoord->setType( "vec2" );
  2187. outTexCoord->setName( "texCoord" );
  2188. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2189. Var *outWorldToTangent = new Var;
  2190. outWorldToTangent->setType( "float3x3" );
  2191. outWorldToTangent->setName( "worldToTangent" );
  2192. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2193. // Add imposterFade to the OUT structure.
  2194. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2195. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2196. outFade->setName( "imposterFade" );
  2197. outFade->setStructName( "OUT" );
  2198. outFade->setType( "float" );
  2199. // Assign OUT.imposterFade
  2200. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2201. // All actual work is done in this method.
  2202. meta->addStatement( new GenOp( " imposter_v( @.xyz, int(@.w), @.x * length(@), normalize(@), normalize(@), int(@.y), int(@.x), @.z, bool(@.w), @, @, @, @, @ );\r\n",
  2203. inPosition,
  2204. inPosition,
  2205. inMiscParams,
  2206. inRightVec,
  2207. inUpVec,
  2208. inRightVec,
  2209. imposterLimits,
  2210. imposterLimits,
  2211. imposterLimits,
  2212. imposterLimits,
  2213. eyePos,
  2214. imposterUVs,
  2215. outInPosition,
  2216. outTexCoord,
  2217. outWorldToTangent ) );
  2218. // Copy the position to wsPosition for use in shaders
  2219. // down stream instead of looking for objTrans.
  2220. Var *wsPosition = new Var;
  2221. wsPosition->setType( "vec3" );
  2222. wsPosition->setName( "wsPosition" );
  2223. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2224. // If we new viewToTangent... its the same as the
  2225. // world to tangent for an imposter.
  2226. Var *viewToTangent = new Var;
  2227. viewToTangent->setType( "float3x3" );
  2228. viewToTangent->setName( "viewToTangent" );
  2229. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2230. }
  2231. void ImposterVertFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2232. const MaterialFeatureData &fd )
  2233. {
  2234. // Find / create IN.imposterFade
  2235. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2236. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2237. fade->setName( "imposterFade" );
  2238. fade->setStructName( "IN" );
  2239. fade->setType( "float" );
  2240. // Find / create visibility
  2241. Var *visibility = (Var*) LangElement::find( "visibility" );
  2242. if ( !visibility )
  2243. {
  2244. visibility = new Var();
  2245. visibility->setType( "float" );
  2246. visibility->setName( "visibility" );
  2247. visibility->uniform = true;
  2248. visibility->constSortPos = cspPotentialPrimitive;
  2249. }
  2250. MultiLine *meta = new MultiLine;
  2251. // Multiply foliageFade into visibility.
  2252. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2253. output = meta;
  2254. }
  2255. void ImposterVertFeatureGLSL::determineFeature( Material *material,
  2256. const GFXVertexFormat *vertexFormat,
  2257. U32 stageNum,
  2258. const FeatureType &type,
  2259. const FeatureSet &features,
  2260. MaterialFeatureData *outFeatureData )
  2261. {
  2262. if ( features.hasFeature( MFT_ImposterVert ) )
  2263. outFeatureData->features.addFeature( MFT_ImposterVert );
  2264. }