shaderFeatureGLSL.cpp 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713
  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. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  836. passData.mTexSlot[ texIndex++ ].texObject = tex;
  837. }
  838. //****************************************************************************
  839. // Overlay Texture
  840. //****************************************************************************
  841. void OverlayTexFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  842. const MaterialFeatureData &fd )
  843. {
  844. Var *inTex = getVertTexCoord( "texCoord2" );
  845. AssertFatal( inTex, "OverlayTexFeatGLSL::processVert() - The second UV set was not found!" );
  846. // grab connector texcoord register
  847. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  848. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  849. outTex->setName( "outTexCoord2" );
  850. outTex->setStructName( "OUT" );
  851. outTex->setType( "vec2" );
  852. outTex->mapsToSampler = true;
  853. if( fd.features[MFT_TexAnim] )
  854. {
  855. inTex->setType( "vec4" );
  856. // Find or create the texture matrix.
  857. Var *texMat = (Var*)LangElement::find( "texMat" );
  858. if ( !texMat )
  859. {
  860. texMat = new Var;
  861. texMat->setType( "float4x4" );
  862. texMat->setName( "texMat" );
  863. texMat->uniform = true;
  864. texMat->constSortPos = cspPass;
  865. }
  866. output = new GenOp( " @ = tMul(@, @);\r\n", outTex, texMat, inTex );
  867. return;
  868. }
  869. // setup language elements to output incoming tex coords to output
  870. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  871. }
  872. void OverlayTexFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  873. const MaterialFeatureData &fd )
  874. {
  875. // grab connector texcoord register
  876. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  877. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  878. inTex->setName( "texCoord2" );
  879. inTex->setStructName( "IN" );
  880. inTex->setType( "vec2" );
  881. inTex->mapsToSampler = true;
  882. // create texture var
  883. Var *diffuseMap = new Var;
  884. diffuseMap->setType( "sampler2D" );
  885. diffuseMap->setName( "overlayMap" );
  886. diffuseMap->uniform = true;
  887. diffuseMap->sampler = true;
  888. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  889. LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
  890. output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
  891. }
  892. ShaderFeature::Resources OverlayTexFeatGLSL::getResources( const MaterialFeatureData &fd )
  893. {
  894. Resources res;
  895. res.numTex = 1;
  896. res.numTexReg = 1;
  897. return res;
  898. }
  899. void OverlayTexFeatGLSL::setTexData( Material::StageData &stageDat,
  900. const MaterialFeatureData &fd,
  901. RenderPassData &passData,
  902. U32 &texIndex )
  903. {
  904. GFXTextureObject *tex = stageDat.getTex( MFT_OverlayMap );
  905. if ( tex )
  906. {
  907. passData.mSamplerNames[ texIndex ] = "overlayMap";
  908. passData.mTexSlot[ texIndex++ ].texObject = tex;
  909. }
  910. }
  911. //****************************************************************************
  912. // Diffuse color
  913. //****************************************************************************
  914. void DiffuseFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  915. const MaterialFeatureData &fd )
  916. {
  917. Var* diffuseMaterialColor = new Var;
  918. diffuseMaterialColor->setType( "vec4" );
  919. diffuseMaterialColor->setName( "diffuseMaterialColor" );
  920. diffuseMaterialColor->uniform = true;
  921. diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
  922. MultiLine* meta = new MultiLine;
  923. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffuseMaterialColor, Material::Mul ) ) );
  924. output = meta;
  925. }
  926. //****************************************************************************
  927. // Diffuse vertex color
  928. //****************************************************************************
  929. void DiffuseVertColorFeatureGLSL::processVert( Vector< ShaderComponent* >& componentList,
  930. const MaterialFeatureData& fd )
  931. {
  932. // Create vertex color connector if it doesn't exist.
  933. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  934. if( !outColor )
  935. {
  936. // Search for vert color.
  937. Var* inColor = dynamic_cast< Var* >( LangElement::find( "diffuse" ) );
  938. if( !inColor )
  939. {
  940. output = NULL;
  941. return;
  942. }
  943. // Create connector.
  944. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  945. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  946. Var* outColor = connectComp->getElement( RT_COLOR );
  947. outColor->setName( "vertColor" );
  948. outColor->setStructName( "OUT" );
  949. outColor->setType( "vec4" );
  950. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  951. }
  952. else
  953. output = NULL; // Nothing we need to do.
  954. }
  955. void DiffuseVertColorFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  956. const MaterialFeatureData &fd )
  957. {
  958. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  959. if( !vertColor )
  960. {
  961. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  962. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  963. vertColor = connectComp->getElement( RT_COLOR );
  964. vertColor->setName( "vertColor" );
  965. vertColor->setStructName( "IN" );
  966. vertColor->setType( "vec4" );
  967. }
  968. MultiLine* meta = new MultiLine;
  969. meta->addStatement( new GenOp( " @;\r\n", assignColor( vertColor, Material::Mul ) ) );
  970. output = meta;
  971. }
  972. //****************************************************************************
  973. // Lightmap
  974. //****************************************************************************
  975. void LightmapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  976. const MaterialFeatureData &fd )
  977. {
  978. // grab tex register from incoming vert
  979. Var *inTex = getVertTexCoord( "texCoord2" );
  980. // grab connector texcoord register
  981. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  982. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  983. outTex->setName( "texCoord2" );
  984. outTex->setStructName( "OUT" );
  985. outTex->setType( "vec2" );
  986. outTex->mapsToSampler = true;
  987. // setup language elements to output incoming tex coords to output
  988. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  989. }
  990. void LightmapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  991. const MaterialFeatureData &fd )
  992. {
  993. // grab connector texcoord register
  994. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  995. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  996. inTex->setName( "texCoord2" );
  997. inTex->setStructName( "IN" );
  998. inTex->setType( "vec2" );
  999. inTex->mapsToSampler = true;
  1000. // create texture var
  1001. Var *lightMap = new Var;
  1002. lightMap->setType( "sampler2D" );
  1003. lightMap->setName( "lightMap" );
  1004. lightMap->uniform = true;
  1005. lightMap->sampler = true;
  1006. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1007. // argh, pixel specular should prob use this too
  1008. if( fd.features[MFT_NormalMap] )
  1009. {
  1010. Var *lmColor = new Var;
  1011. lmColor->setName( "lmColor" );
  1012. lmColor->setType( "vec4" );
  1013. LangElement *lmColorDecl = new DecOp( lmColor );
  1014. output = new GenOp( " @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
  1015. return;
  1016. }
  1017. // Add realtime lighting, if it is available
  1018. LangElement *statement = NULL;
  1019. if( fd.features[MFT_RTLighting] )
  1020. {
  1021. // Advanced lighting is the only dynamic lighting supported right now
  1022. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1023. if(inColor != NULL)
  1024. {
  1025. // Find out if RTLighting should be added or substituted
  1026. bool bPreProcessedLighting = false;
  1027. AdvancedLightBinManager *lightBin;
  1028. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1029. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1030. // Lightmap has already been included in the advanced light bin, so
  1031. // no need to do any sampling or anything
  1032. if(bPreProcessedLighting)
  1033. statement = new GenOp( "float4(@, 1.0)", inColor );
  1034. else
  1035. statement = new GenOp( "tex2D(@, @) + float4(@.rgb, 0.0)", lightMap, inTex, inColor );
  1036. }
  1037. }
  1038. // If we still don't have it... then just sample the lightmap.
  1039. if ( !statement )
  1040. statement = new GenOp( "tex2D(@, @)", lightMap, inTex );
  1041. // Assign to proper render target
  1042. MultiLine *meta = new MultiLine;
  1043. if( fd.features[MFT_LightbufferMRT] )
  1044. {
  1045. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1046. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
  1047. }
  1048. else
  1049. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
  1050. output = meta;
  1051. }
  1052. ShaderFeature::Resources LightmapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1053. {
  1054. Resources res;
  1055. res.numTex = 1;
  1056. res.numTexReg = 1;
  1057. return res;
  1058. }
  1059. void LightmapFeatGLSL::setTexData( Material::StageData &stageDat,
  1060. const MaterialFeatureData &fd,
  1061. RenderPassData &passData,
  1062. U32 &texIndex )
  1063. {
  1064. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  1065. passData.mSamplerNames[ texIndex ] = "lightMap";
  1066. if ( tex )
  1067. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1068. else
  1069. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  1070. }
  1071. U32 LightmapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1072. {
  1073. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1074. }
  1075. //****************************************************************************
  1076. // Tonemap
  1077. //****************************************************************************
  1078. void TonemapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1079. const MaterialFeatureData &fd )
  1080. {
  1081. // Grab the connector
  1082. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1083. // Set up the second set of texCoords
  1084. Var *inTex2 = getVertTexCoord( "texCoord2" );
  1085. if ( inTex2 )
  1086. {
  1087. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  1088. outTex2->setName( "texCoord2" );
  1089. outTex2->setStructName( "OUT" );
  1090. outTex2->setType( "vec2" );
  1091. outTex2->mapsToSampler = true;
  1092. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  1093. }
  1094. }
  1095. void TonemapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1096. const MaterialFeatureData &fd )
  1097. {
  1098. // Grab connector
  1099. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1100. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  1101. inTex2->setName( "texCoord2" );
  1102. inTex2->setStructName( "IN" );
  1103. inTex2->setType( "vec2" );
  1104. inTex2->mapsToSampler = true;
  1105. // create texture var
  1106. Var *toneMap = new Var;
  1107. toneMap->setType( "sampler2D" );
  1108. toneMap->setName( "toneMap" );
  1109. toneMap->uniform = true;
  1110. toneMap->sampler = true;
  1111. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1112. MultiLine * meta = new MultiLine;
  1113. // First get the toneMap color
  1114. Var *toneMapColor = new Var;
  1115. toneMapColor->setType( "vec4" );
  1116. toneMapColor->setName( "toneMapColor" );
  1117. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  1118. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
  1119. // We do a different calculation if there is a diffuse map or not
  1120. Material::BlendOp blendOp = Material::Mul;
  1121. if ( fd.features[MFT_DiffuseMap] )
  1122. {
  1123. // Reverse the tonemap
  1124. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
  1125. // Re-tonemap with the current color factored in
  1126. blendOp = Material::ToneMap;
  1127. }
  1128. // Find out if RTLighting should be added
  1129. bool bPreProcessedLighting = false;
  1130. AdvancedLightBinManager *lightBin;
  1131. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1132. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1133. // Add in the realtime lighting contribution
  1134. if ( fd.features[MFT_RTLighting] )
  1135. {
  1136. // Right now, only Advanced Lighting is supported
  1137. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1138. if(inColor != NULL)
  1139. {
  1140. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1141. // the dynamic light buffer, and it already has the tonemap included
  1142. if(bPreProcessedLighting)
  1143. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  1144. else
  1145. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  1146. }
  1147. }
  1148. // Assign to proper render target
  1149. if( fd.features[MFT_LightbufferMRT] )
  1150. {
  1151. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1152. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
  1153. }
  1154. else
  1155. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  1156. output = meta;
  1157. }
  1158. ShaderFeature::Resources TonemapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1159. {
  1160. Resources res;
  1161. res.numTex = 1;
  1162. res.numTexReg = 1;
  1163. return res;
  1164. }
  1165. void TonemapFeatGLSL::setTexData( Material::StageData &stageDat,
  1166. const MaterialFeatureData &fd,
  1167. RenderPassData &passData,
  1168. U32 &texIndex )
  1169. {
  1170. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  1171. if ( tex )
  1172. {
  1173. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  1174. passData.mSamplerNames[ texIndex ] = "toneMap";
  1175. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1176. }
  1177. }
  1178. U32 TonemapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1179. {
  1180. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1181. }
  1182. //****************************************************************************
  1183. // pureLIGHT Lighting
  1184. //****************************************************************************
  1185. void VertLitGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1186. const MaterialFeatureData &fd )
  1187. {
  1188. // If we have a lightMap or toneMap then our lighting will be
  1189. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1190. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1191. {
  1192. output = NULL;
  1193. return;
  1194. }
  1195. // Create vertex color connector if it doesn't exist.
  1196. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1197. if( !outColor )
  1198. {
  1199. // Grab the connector color
  1200. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1201. Var *outColor = connectComp->getElement( RT_COLOR );
  1202. outColor->setName( "vertColor" );
  1203. outColor->setStructName( "OUT" );
  1204. outColor->setType( "vec4" );
  1205. // Search for vert color
  1206. Var *inColor = (Var*) LangElement::find( "diffuse" );
  1207. // If there isn't a vertex color then we can't do anything
  1208. if( !inColor )
  1209. {
  1210. output = NULL;
  1211. return;
  1212. }
  1213. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1214. }
  1215. else
  1216. output = NULL; // Nothing we need to do.
  1217. }
  1218. void VertLitGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1219. const MaterialFeatureData &fd )
  1220. {
  1221. // If we have a lightMap or toneMap then our lighting will be
  1222. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1223. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1224. {
  1225. output = NULL;
  1226. return;
  1227. }
  1228. // Grab the connector color register
  1229. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1230. if( !vertColor )
  1231. {
  1232. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1233. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1234. vertColor = connectComp->getElement( RT_COLOR );
  1235. vertColor->setName( "vertColor" );
  1236. vertColor->setStructName( "IN" );
  1237. vertColor->setType( "vec4" );
  1238. }
  1239. MultiLine * meta = new MultiLine;
  1240. // Defaults (no diffuse map)
  1241. Material::BlendOp blendOp = Material::Mul;
  1242. LangElement *outColor = vertColor;
  1243. // We do a different calculation if there is a diffuse map or not
  1244. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1245. {
  1246. Var * finalVertColor = new Var;
  1247. finalVertColor->setName( "finalVertColor" );
  1248. finalVertColor->setType( "vec4" );
  1249. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1250. // Reverse the tonemap
  1251. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
  1252. // Set the blend op to tonemap
  1253. blendOp = Material::ToneMap;
  1254. outColor = finalVertColor;
  1255. }
  1256. // Add in the realtime lighting contribution, if applicable
  1257. if ( fd.features[MFT_RTLighting] )
  1258. {
  1259. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1260. if(rtLightingColor != NULL)
  1261. {
  1262. bool bPreProcessedLighting = false;
  1263. AdvancedLightBinManager *lightBin;
  1264. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1265. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1266. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1267. // the dynamic light buffer, and it already has the baked-vertex-color
  1268. // included in it
  1269. if(bPreProcessedLighting)
  1270. outColor = new GenOp( "float4(@.rgb, 1.0)", rtLightingColor );
  1271. else
  1272. outColor = new GenOp( "float4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
  1273. }
  1274. }
  1275. // Output the color
  1276. if ( fd.features[MFT_LightbufferMRT] )
  1277. {
  1278. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1279. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
  1280. }
  1281. else
  1282. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1283. output = meta;
  1284. }
  1285. U32 VertLitGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1286. {
  1287. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1288. }
  1289. //****************************************************************************
  1290. // Detail map
  1291. //****************************************************************************
  1292. void DetailFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1293. const MaterialFeatureData &fd )
  1294. {
  1295. MultiLine *meta = new MultiLine;
  1296. addOutDetailTexCoord( componentList,
  1297. meta,
  1298. fd.features[MFT_TexAnim] );
  1299. output = meta;
  1300. }
  1301. void DetailFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1302. const MaterialFeatureData &fd )
  1303. {
  1304. // Get the detail texture coord.
  1305. Var *inTex = getInTexCoord( "detCoord", "vec2", true, componentList );
  1306. // create texture var
  1307. Var *detailMap = new Var;
  1308. detailMap->setType( "sampler2D" );
  1309. detailMap->setName( "detailMap" );
  1310. detailMap->uniform = true;
  1311. detailMap->sampler = true;
  1312. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1313. // We're doing the standard greyscale detail map
  1314. // technique which can darken and lighten the
  1315. // diffuse texture.
  1316. // TODO: We could add a feature to toggle between this
  1317. // and a simple multiplication with the detail map.
  1318. LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
  1319. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1320. }
  1321. ShaderFeature::Resources DetailFeatGLSL::getResources( const MaterialFeatureData &fd )
  1322. {
  1323. Resources res;
  1324. res.numTex = 1;
  1325. res.numTexReg = 1;
  1326. return res;
  1327. }
  1328. void DetailFeatGLSL::setTexData( Material::StageData &stageDat,
  1329. const MaterialFeatureData &fd,
  1330. RenderPassData &passData,
  1331. U32 &texIndex )
  1332. {
  1333. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1334. if ( tex )
  1335. {
  1336. passData.mSamplerNames[texIndex] = "detailMap";
  1337. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1338. }
  1339. }
  1340. //****************************************************************************
  1341. // Vertex position
  1342. //****************************************************************************
  1343. void VertPositionGLSL::determineFeature( Material *material,
  1344. const GFXVertexFormat *vertexFormat,
  1345. U32 stageNum,
  1346. const FeatureType &type,
  1347. const FeatureSet &features,
  1348. MaterialFeatureData *outFeatureData )
  1349. {
  1350. // This feature is always on!
  1351. outFeatureData->features.addFeature( type );
  1352. }
  1353. void VertPositionGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1354. const MaterialFeatureData &fd )
  1355. {
  1356. // First check for an input position from a previous feature
  1357. // then look for the default vertex position.
  1358. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1359. if ( !inPosition )
  1360. inPosition = (Var*)LangElement::find( "position" );
  1361. // grab connector position
  1362. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1363. Var *outPosition = connectComp->getElement( RT_POSITION );
  1364. outPosition->setName( "gl_Position" );
  1365. MultiLine *meta = new MultiLine;
  1366. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1367. meta->addStatement( new GenOp( " @ = tMul(@, float4(@.xyz,1));\r\n",
  1368. outPosition, modelview, inPosition ) );
  1369. output = meta;
  1370. }
  1371. //****************************************************************************
  1372. // Reflect Cubemap
  1373. //****************************************************************************
  1374. void ReflectCubeFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1375. const MaterialFeatureData &fd )
  1376. {
  1377. // search for vert normal
  1378. Var *inNormal = (Var*) LangElement::find( "normal" );
  1379. if ( !inNormal )
  1380. return;
  1381. MultiLine * meta = new MultiLine;
  1382. // If a base or bump tex is present in the material, but not in the
  1383. // current pass - we need to add one to the current pass to use
  1384. // its alpha channel as a gloss map. Here we just need the tex coords.
  1385. if( !fd.features[MFT_DiffuseMap] &&
  1386. !fd.features[MFT_NormalMap] )
  1387. {
  1388. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1389. fd.materialFeatures[MFT_NormalMap] )
  1390. {
  1391. // find incoming texture var
  1392. Var *inTex = getVertTexCoord( "texCoord" );
  1393. // grab connector texcoord register
  1394. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1395. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1396. outTex->setName( "texCoord" );
  1397. outTex->setStructName( "OUT" );
  1398. outTex->setType( "vec2" );
  1399. outTex->mapsToSampler = true;
  1400. // setup language elements to output incoming tex coords to output
  1401. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1402. }
  1403. }
  1404. // create cubeTrans
  1405. bool useInstancing = fd.features[MFT_UseInstancing];
  1406. Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
  1407. // cube vert position
  1408. Var * cubeVertPos = new Var;
  1409. cubeVertPos->setName( "cubeVertPos" );
  1410. cubeVertPos->setType( "vec3" );
  1411. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1412. meta->addStatement( new GenOp( " @ = tMul( @, float4(@,1)).xyz;\r\n",
  1413. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1414. // cube normal
  1415. Var * cubeNormal = new Var;
  1416. cubeNormal->setName( "cubeNormal" );
  1417. cubeNormal->setType( "vec3" );
  1418. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1419. meta->addStatement( new GenOp( " @ = ( tMul( (@), vec4(@, 0) ) ).xyz;\r\n",
  1420. cubeNormDecl, cubeTrans, inNormal ) );
  1421. meta->addStatement( new GenOp( " @ = bool(length(@)) ? normalize(@) : @;\r\n",
  1422. cubeNormal, cubeNormal, cubeNormal, cubeNormal ) );
  1423. // grab the eye position
  1424. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1425. if ( !eyePos )
  1426. {
  1427. eyePos = new Var( "eyePosWorld", "vec3" );
  1428. eyePos->uniform = true;
  1429. eyePos->constSortPos = cspPass;
  1430. }
  1431. // eye to vert
  1432. Var * eyeToVert = new Var;
  1433. eyeToVert->setName( "eyeToVert" );
  1434. eyeToVert->setType( "vec3" );
  1435. LangElement *e2vDecl = new DecOp( eyeToVert );
  1436. meta->addStatement( new GenOp( " @ = @ - @;\r\n",
  1437. e2vDecl, cubeVertPos, eyePos ) );
  1438. // grab connector texcoord register
  1439. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1440. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1441. reflectVec->setName( "reflectVec" );
  1442. reflectVec->setStructName( "OUT" );
  1443. reflectVec->setType( "vec3" );
  1444. reflectVec->mapsToSampler = true;
  1445. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1446. output = meta;
  1447. }
  1448. void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1449. const MaterialFeatureData &fd )
  1450. {
  1451. MultiLine * meta = new MultiLine;
  1452. Var *glossColor = NULL;
  1453. // If a base or bump tex is present in the material, but not in the
  1454. // current pass - we need to add one to the current pass to use
  1455. // its alpha channel as a gloss map.
  1456. if( !fd.features[MFT_DiffuseMap] &&
  1457. !fd.features[MFT_NormalMap] )
  1458. {
  1459. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1460. fd.materialFeatures[MFT_NormalMap] )
  1461. {
  1462. // grab connector texcoord register
  1463. Var *inTex = getInTexCoord( "texCoord", "vec2", true, componentList );
  1464. // create texture var
  1465. Var *newMap = new Var;
  1466. newMap->setType( "sampler2D" );
  1467. newMap->setName( "glossMap" );
  1468. newMap->uniform = true;
  1469. newMap->sampler = true;
  1470. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1471. // create sample color
  1472. Var *color = new Var;
  1473. color->setType( "vec4" );
  1474. color->setName( "diffuseColor" );
  1475. LangElement *colorDecl = new DecOp( color );
  1476. glossColor = color;
  1477. meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
  1478. }
  1479. }
  1480. else
  1481. {
  1482. glossColor = (Var*) LangElement::find( "diffuseColor" );
  1483. if( !glossColor )
  1484. glossColor = (Var*) LangElement::find( "bumpNormal" );
  1485. }
  1486. // grab connector texcoord register
  1487. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1488. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1489. reflectVec->setName( "reflectVec" );
  1490. reflectVec->setStructName( "IN" );
  1491. reflectVec->setType( "vec3" );
  1492. reflectVec->mapsToSampler = true;
  1493. // create cubemap var
  1494. Var *cubeMap = new Var;
  1495. cubeMap->setType( "samplerCUBE" );
  1496. cubeMap->setName( "cubeMap" );
  1497. cubeMap->uniform = true;
  1498. cubeMap->sampler = true;
  1499. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1500. // TODO: Restore the lighting attenuation here!
  1501. Var *attn = NULL;
  1502. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1503. //attn = (Var*)LangElement::find("attn");
  1504. //else
  1505. if ( fd.materialFeatures[MFT_RTLighting] )
  1506. attn =(Var*)LangElement::find("d_NL_Att");
  1507. LangElement *texCube = new GenOp( "texCUBE( @, @ )", cubeMap, reflectVec );
  1508. LangElement *lerpVal = NULL;
  1509. Material::BlendOp blendOp = Material::LerpAlpha;
  1510. // Note that the lerpVal needs to be a float4 so that
  1511. // it will work with the LerpAlpha blend.
  1512. if ( glossColor )
  1513. {
  1514. if ( attn )
  1515. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1516. else
  1517. lerpVal = glossColor;
  1518. }
  1519. else
  1520. {
  1521. if ( attn )
  1522. lerpVal = new GenOp( "vec4( saturate( @ ) ).xxxx", attn );
  1523. else
  1524. blendOp = Material::Mul;
  1525. }
  1526. meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
  1527. output = meta;
  1528. }
  1529. ShaderFeature::Resources ReflectCubeFeatGLSL::getResources( const MaterialFeatureData &fd )
  1530. {
  1531. Resources res;
  1532. if( fd.features[MFT_DiffuseMap] ||
  1533. fd.features[MFT_NormalMap] )
  1534. {
  1535. res.numTex = 1;
  1536. res.numTexReg = 1;
  1537. }
  1538. else
  1539. {
  1540. res.numTex = 2;
  1541. res.numTexReg = 2;
  1542. }
  1543. return res;
  1544. }
  1545. void ReflectCubeFeatGLSL::setTexData( Material::StageData &stageDat,
  1546. const MaterialFeatureData &stageFeatures,
  1547. RenderPassData &passData,
  1548. U32 &texIndex )
  1549. {
  1550. // set up a gloss map if one is not present in the current pass
  1551. // but is present in the current material stage
  1552. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1553. !passData.mFeatureData.features[MFT_NormalMap] )
  1554. {
  1555. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1556. if ( tex && stageFeatures.features[MFT_DiffuseMap] )
  1557. {
  1558. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  1559. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1560. }
  1561. else
  1562. {
  1563. tex = stageDat.getTex( MFT_NormalMap );
  1564. if ( tex && stageFeatures.features[ MFT_NormalMap ] )
  1565. {
  1566. passData.mSamplerNames[ texIndex ] = "bumpMap";
  1567. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1568. }
  1569. }
  1570. }
  1571. if( stageDat.getCubemap() )
  1572. {
  1573. passData.mCubeMap = stageDat.getCubemap();
  1574. passData.mSamplerNames[texIndex] = "cubeMap";
  1575. passData.mTexType[texIndex++] = Material::Cube;
  1576. }
  1577. else
  1578. {
  1579. if( stageFeatures.features[MFT_CubeMap] )
  1580. {
  1581. // assuming here that it is a scenegraph cubemap
  1582. passData.mSamplerNames[texIndex] = "cubeMap";
  1583. passData.mTexType[texIndex++] = Material::SGCube;
  1584. }
  1585. }
  1586. }
  1587. //****************************************************************************
  1588. // RTLighting
  1589. //****************************************************************************
  1590. RTLightingFeatGLSL::RTLightingFeatGLSL()
  1591. : mDep( "shaders/common/gl/lighting.glsl" )
  1592. {
  1593. addDependency( &mDep );
  1594. }
  1595. void RTLightingFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1596. const MaterialFeatureData &fd )
  1597. {
  1598. MultiLine *meta = new MultiLine;
  1599. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1600. // Special case for lighting imposters. We dont have a vert normal and may not
  1601. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1602. if ( fd.features[MFT_ImposterVert] )
  1603. {
  1604. if ( !fd.features[MFT_NormalMap] )
  1605. {
  1606. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1607. if ( !eyePos )
  1608. {
  1609. eyePos = new Var( "eyePosWorld", "vec3" );
  1610. eyePos->uniform = true;
  1611. eyePos->constSortPos = cspPass;
  1612. }
  1613. Var *inPosition = (Var*)LangElement::find( "position" );
  1614. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1615. outNormal->setName( "wsNormal" );
  1616. outNormal->setStructName( "OUT" );
  1617. outNormal->setType( "vec3" );
  1618. outNormal->mapsToSampler = false;
  1619. // Transform the normal to world space.
  1620. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );
  1621. }
  1622. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1623. output = meta;
  1624. return;
  1625. }
  1626. // Find the incoming vertex normal.
  1627. Var *inNormal = (Var*)LangElement::find( "normal" );
  1628. // Skip out on realtime lighting if we don't have a normal
  1629. // or we're doing some sort of baked lighting.
  1630. if ( !inNormal ||
  1631. fd.features[MFT_LightMap] ||
  1632. fd.features[MFT_ToneMap] ||
  1633. fd.features[MFT_VertLit] )
  1634. return;
  1635. // If there isn't a normal map then we need to pass
  1636. // the world space normal to the pixel shader ourselves.
  1637. if ( !fd.features[MFT_NormalMap] )
  1638. {
  1639. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1640. outNormal->setName( "wsNormal" );
  1641. outNormal->setStructName( "OUT" );
  1642. outNormal->setType( "vec3" );
  1643. outNormal->mapsToSampler = false;
  1644. // Get the transform to world space.
  1645. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1646. // Transform the normal to world space.
  1647. meta->addStatement( new GenOp( " @ = tMul( @, float4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1648. }
  1649. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1650. output = meta;
  1651. }
  1652. void RTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1653. const MaterialFeatureData &fd )
  1654. {
  1655. // Skip out on realtime lighting if we don't have a normal
  1656. // or we're doing some sort of baked lighting.
  1657. //
  1658. // TODO: We can totally detect for this in the material
  1659. // feature setup... we should move it out of here!
  1660. //
  1661. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1662. return;
  1663. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1664. MultiLine *meta = new MultiLine;
  1665. // Look for a wsNormal or grab it from the connector.
  1666. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  1667. if ( !wsNormal )
  1668. {
  1669. wsNormal = connectComp->getElement( RT_TEXCOORD );
  1670. wsNormal->setName( "wsNormal" );
  1671. wsNormal->setStructName( "IN" );
  1672. wsNormal->setType( "vec3" );
  1673. // If we loaded the normal its our responsibility
  1674. // to normalize it... the interpolators won't.
  1675. //
  1676. // Note we cast to half here to get partial precision
  1677. // optimized code which is an acceptable loss of
  1678. // precision for normals and performs much better
  1679. // on older Geforce cards.
  1680. //
  1681. meta->addStatement( new GenOp( " @ = normalize( half3( @ ) );\r\n", wsNormal, wsNormal ) );
  1682. }
  1683. // Now the wsPosition and wsView.
  1684. Var *wsPosition = getInWsPosition( componentList );
  1685. Var *wsView = getWsView( wsPosition, meta );
  1686. // Create temporaries to hold results of lighting.
  1687. Var *rtShading = new Var( "rtShading", "vec4" );
  1688. Var *specular = new Var( "specular", "vec4" );
  1689. meta->addStatement( new GenOp( " @; @;\r\n",
  1690. new DecOp( rtShading ), new DecOp( specular ) ) );
  1691. // Look for a light mask generated from a previous
  1692. // feature (this is done for BL terrain lightmaps).
  1693. LangElement *lightMask = LangElement::find( "lightMask" );
  1694. if ( !lightMask )
  1695. lightMask = new GenOp( "float4( 1, 1, 1, 1 )" );
  1696. // Get all the light constants.
  1697. Var *inLightPos = new Var( "inLightPos", "vec4" );
  1698. inLightPos->uniform = true;
  1699. inLightPos->arraySize = 3;
  1700. inLightPos->constSortPos = cspPotentialPrimitive;
  1701. Var *inLightInvRadiusSq = new Var( "inLightInvRadiusSq", "vec4" );
  1702. inLightInvRadiusSq->uniform = true;
  1703. inLightInvRadiusSq->constSortPos = cspPotentialPrimitive;
  1704. Var *inLightColor = new Var( "inLightColor", "vec4" );
  1705. inLightColor->uniform = true;
  1706. inLightColor->arraySize = 4;
  1707. inLightColor->constSortPos = cspPotentialPrimitive;
  1708. Var *inLightSpotDir = new Var( "inLightSpotDir", "vec4" );
  1709. inLightSpotDir->uniform = true;
  1710. inLightSpotDir->arraySize = 3;
  1711. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1712. Var *inLightSpotAngle = new Var( "inLightSpotAngle", "vec4" );
  1713. inLightSpotAngle->uniform = true;
  1714. inLightSpotAngle->constSortPos = cspPotentialPrimitive;
  1715. Var *lightSpotFalloff = new Var( "inLightSpotFalloff", "vec4" );
  1716. lightSpotFalloff->uniform = true;
  1717. lightSpotFalloff->constSortPos = cspPotentialPrimitive;
  1718. Var *specularPower = new Var( "specularPower", "float" );
  1719. specularPower->uniform = true;
  1720. specularPower->constSortPos = cspPotentialPrimitive;
  1721. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  1722. if ( !specularColor )
  1723. {
  1724. specularColor = new Var( "specularColor", "vec4" );
  1725. specularColor->uniform = true;
  1726. specularColor->constSortPos = cspPotentialPrimitive;
  1727. }
  1728. Var *ambient = new Var( "ambient", "vec4" );
  1729. ambient->uniform = true;
  1730. ambient->constSortPos = cspPass;
  1731. // Calculate the diffuse shading and specular powers.
  1732. meta->addStatement( new GenOp( " compute4Lights( @, @, @, @,\r\n"
  1733. " @, @, @, @, @, @, @, @,\r\n"
  1734. " @, @ );\r\n",
  1735. wsView, wsPosition, wsNormal, lightMask,
  1736. inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, specularPower, specularColor,
  1737. rtShading, specular ) );
  1738. // Apply the lighting to the diffuse color.
  1739. LangElement *lighting = new GenOp( "float4( @.rgb + @.rgb, 1 )", rtShading, ambient );
  1740. meta->addStatement( new GenOp( " @;\r\n", assignColor( lighting, Material::Mul ) ) );
  1741. output = meta;
  1742. }
  1743. ShaderFeature::Resources RTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
  1744. {
  1745. Resources res;
  1746. // These features disable realtime lighting.
  1747. if ( !fd.features[MFT_LightMap] &&
  1748. !fd.features[MFT_ToneMap] &&
  1749. !fd.features[MFT_VertLit] )
  1750. {
  1751. // If enabled we pass the position.
  1752. res.numTexReg = 1;
  1753. // If there isn't a bump map then we pass the
  1754. // world space normal as well.
  1755. if ( !fd.features[MFT_NormalMap] )
  1756. res.numTexReg++;
  1757. }
  1758. return res;
  1759. }
  1760. //****************************************************************************
  1761. // Fog
  1762. //****************************************************************************
  1763. FogFeatGLSL::FogFeatGLSL()
  1764. : mFogDep( "shaders/common/gl/torque.glsl" )
  1765. {
  1766. addDependency( &mFogDep );
  1767. }
  1768. void FogFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1769. const MaterialFeatureData &fd )
  1770. {
  1771. MultiLine *meta = new MultiLine;
  1772. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1773. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1774. {
  1775. // Grab the eye position.
  1776. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1777. if ( !eyePos )
  1778. {
  1779. eyePos = new Var( "eyePosWorld", "vec3" );
  1780. eyePos->uniform = true;
  1781. eyePos->constSortPos = cspPass;
  1782. }
  1783. Var *fogData = new Var( "fogData", "vec3" );
  1784. fogData->uniform = true;
  1785. fogData->constSortPos = cspPass;
  1786. Var *wsPosition = new Var( "fogPos", "vec3" );
  1787. getWsPosition( componentList,
  1788. fd.features[MFT_UseInstancing],
  1789. meta,
  1790. new DecOp( wsPosition ) );
  1791. // We pass the fog amount to the pixel shader.
  1792. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1793. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1794. fogAmount->setName( "fogAmount" );
  1795. fogAmount->setStructName( "OUT" );
  1796. fogAmount->setType( "float" );
  1797. fogAmount->mapsToSampler = false;
  1798. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1799. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1800. }
  1801. else
  1802. {
  1803. // We fog in world space... make sure the world space
  1804. // position is passed to the pixel shader. This is
  1805. // often already passed for lighting, so it takes up
  1806. // no extra output registers.
  1807. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1808. }
  1809. output = meta;
  1810. }
  1811. void FogFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1812. const MaterialFeatureData &fd )
  1813. {
  1814. MultiLine *meta = new MultiLine;
  1815. Var *fogColor = new Var;
  1816. fogColor->setType( "vec4" );
  1817. fogColor->setName( "fogColor" );
  1818. fogColor->uniform = true;
  1819. fogColor->constSortPos = cspPass;
  1820. // Get the out color.
  1821. Var *color = (Var*) LangElement::find( "col" );
  1822. if ( !color )
  1823. {
  1824. color = new Var;
  1825. color->setType( "vec4" );
  1826. color->setName( "col" );
  1827. color->setStructName("OUT");
  1828. }
  1829. Var *fogAmount;
  1830. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1831. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1832. {
  1833. // Per-vertex.... just get the fog amount.
  1834. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1835. fogAmount = connectComp->getElement( RT_TEXCOORD );
  1836. fogAmount->setName( "fogAmount" );
  1837. fogAmount->setStructName( "IN" );
  1838. fogAmount->setType( "float" );
  1839. }
  1840. else
  1841. {
  1842. Var *wsPosition = getInWsPosition( componentList );
  1843. // grab the eye position
  1844. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1845. if ( !eyePos )
  1846. {
  1847. eyePos = new Var( "eyePosWorld", "vec3" );
  1848. eyePos->uniform = true;
  1849. eyePos->constSortPos = cspPass;
  1850. }
  1851. Var *fogData = new Var( "fogData", "vec3" );
  1852. fogData->uniform = true;
  1853. fogData->constSortPos = cspPass;
  1854. /// Get the fog amount.
  1855. fogAmount = new Var( "fogAmount", "float" );
  1856. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1857. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  1858. }
  1859. // Lerp between the fog color and diffuse color.
  1860. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  1861. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  1862. output = meta;
  1863. }
  1864. ShaderFeature::Resources FogFeatGLSL::getResources( const MaterialFeatureData &fd )
  1865. {
  1866. Resources res;
  1867. res.numTexReg = 1;
  1868. return res;
  1869. }
  1870. //****************************************************************************
  1871. // Visibility
  1872. //****************************************************************************
  1873. VisibilityFeatGLSL::VisibilityFeatGLSL()
  1874. : mTorqueDep( "shaders/common/gl/torque.glsl" )
  1875. {
  1876. addDependency( &mTorqueDep );
  1877. }
  1878. void VisibilityFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1879. const MaterialFeatureData &fd )
  1880. {
  1881. MultiLine *meta = new MultiLine;
  1882. output = meta;
  1883. if ( fd.features[ MFT_UseInstancing ] )
  1884. {
  1885. // We pass the visibility to the pixel shader via
  1886. // another output register.
  1887. //
  1888. // TODO: We should see if we can share this register
  1889. // with some other common instanced data.
  1890. //
  1891. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1892. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  1893. outVisibility->setStructName( "OUT" );
  1894. outVisibility->setName( "visibility" );
  1895. outVisibility->setType( "float" );
  1896. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  1897. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  1898. instVisibility->setStructName( "IN" );
  1899. instVisibility->setName( "inst_visibility" );
  1900. instVisibility->setType( "float" );
  1901. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  1902. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  1903. }
  1904. if ( fd.features[ MFT_IsTranslucent ] )
  1905. return;
  1906. addOutVpos( meta, componentList );
  1907. }
  1908. void VisibilityFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1909. const MaterialFeatureData &fd )
  1910. {
  1911. // Get the visibility constant.
  1912. Var *visibility = NULL;
  1913. if ( fd.features[ MFT_UseInstancing ] )
  1914. visibility = getInTexCoord( "visibility", "float", false, componentList );
  1915. else
  1916. {
  1917. visibility = (Var*)LangElement::find( "visibility" );
  1918. if ( !visibility )
  1919. {
  1920. visibility = new Var();
  1921. visibility->setType( "float" );
  1922. visibility->setName( "visibility" );
  1923. visibility->uniform = true;
  1924. visibility->constSortPos = cspPotentialPrimitive;
  1925. }
  1926. }
  1927. MultiLine* meta = new MultiLine;
  1928. output = meta;
  1929. // Translucent objects do a simple alpha fade.
  1930. if ( fd.features[ MFT_IsTranslucent ] )
  1931. {
  1932. Var *color = (Var*) LangElement::find( "col" );
  1933. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  1934. return;
  1935. }
  1936. // Everything else does a fizzle.
  1937. Var *vPos = getInVpos( meta, componentList );
  1938. meta->addStatement( new GenOp( " fizzle( @, @ );\r\n", vPos, visibility ) );
  1939. }
  1940. ShaderFeature::Resources VisibilityFeatGLSL::getResources( const MaterialFeatureData &fd )
  1941. {
  1942. Resources res;
  1943. // TODO: Fix for instancing.
  1944. if ( !fd.features[ MFT_IsTranslucent ] )
  1945. res.numTexReg = 1;
  1946. return res;
  1947. }
  1948. //****************************************************************************
  1949. // AlphaTest
  1950. //****************************************************************************
  1951. void AlphaTestGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1952. const MaterialFeatureData &fd )
  1953. {
  1954. // If we're below SM3 and don't have a depth output
  1955. // feature then don't waste an instruction here.
  1956. if ( GFX->getPixelShaderVersion() < 3.0 &&
  1957. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  1958. !fd.features[ MFT_DepthOut ] )
  1959. {
  1960. output = NULL;
  1961. return;
  1962. }
  1963. // If we don't have a color var then we cannot do an alpha test.
  1964. Var *color = (Var*)LangElement::find( "col" );
  1965. if ( !color )
  1966. {
  1967. output = NULL;
  1968. return;
  1969. }
  1970. // Now grab the alpha test value.
  1971. Var *alphaTestVal = new Var;
  1972. alphaTestVal->setType( "float" );
  1973. alphaTestVal->setName( "alphaTestValue" );
  1974. alphaTestVal->uniform = true;
  1975. alphaTestVal->constSortPos = cspPotentialPrimitive;
  1976. // Do the clip.
  1977. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  1978. }
  1979. //****************************************************************************
  1980. // GlowMask
  1981. //****************************************************************************
  1982. void GlowMaskGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1983. const MaterialFeatureData &fd )
  1984. {
  1985. output = NULL;
  1986. // Get the output color... and make it black to mask out
  1987. // glow passes rendered before us.
  1988. //
  1989. // The shader compiler will optimize out all the other
  1990. // code above that doesn't contribute to the alpha mask.
  1991. Var *color = (Var*)LangElement::find( "col" );
  1992. if ( color )
  1993. output = new GenOp( " @.rgb = vec3(0);\r\n", color );
  1994. }
  1995. //****************************************************************************
  1996. // RenderTargetZero
  1997. //****************************************************************************
  1998. void RenderTargetZeroGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  1999. {
  2000. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  2001. // This will prevent a divide by zero causing an FP special on float render targets
  2002. output = new GenOp( " @;\r\n", assignColor( new GenOp( "vec4(0.00001)" ), Material::None, NULL, mOutputTargetMask ) );
  2003. }
  2004. //****************************************************************************
  2005. // HDR Output
  2006. //****************************************************************************
  2007. HDROutGLSL::HDROutGLSL()
  2008. : mTorqueDep( "shaders/common/gl/torque.glsl" )
  2009. {
  2010. addDependency( &mTorqueDep );
  2011. }
  2012. void HDROutGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2013. const MaterialFeatureData &fd )
  2014. {
  2015. // Let the helper function do the work.
  2016. Var *color = (Var*)LangElement::find( "col" );
  2017. if ( color )
  2018. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2019. }
  2020. //****************************************************************************
  2021. // FoliageFeatureGLSL
  2022. //****************************************************************************
  2023. #include "T3D/fx/groundCover.h"
  2024. FoliageFeatureGLSL::FoliageFeatureGLSL()
  2025. : mDep( "shaders/common/gl/foliage.glsl" )
  2026. {
  2027. addDependency( &mDep );
  2028. }
  2029. void FoliageFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2030. const MaterialFeatureData &fd )
  2031. {
  2032. // Get the input variables we need.
  2033. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2034. if ( !inPosition )
  2035. inPosition = (Var*)LangElement::find( "position" );
  2036. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2037. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2038. MultiLine *meta = new MultiLine;
  2039. // Declare the normal and tangent variables since they do not exist
  2040. // in this vert type, but we do need to set them up for others.
  2041. Var *normal = (Var*)LangElement::find( "normal" );
  2042. AssertFatal( normal, "FoliageFeatureGLSL requires vert normal!" );
  2043. Var *tangent = new Var;
  2044. tangent->setType( "vec3" );
  2045. tangent->setName( "T" );
  2046. LangElement *tangentDec = new DecOp( tangent );
  2047. meta->addStatement( new GenOp( " @;\n", tangentDec ) );
  2048. // We add a float foliageFade to the OUT structure.
  2049. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2050. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2051. fade->setName( "foliageFade" );
  2052. fade->setStructName( "OUT" );
  2053. fade->setType( "float" );
  2054. // grab the eye position
  2055. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2056. if ( !eyePos )
  2057. {
  2058. eyePos = new Var( "eyePosWorld", "vec3" );
  2059. eyePos->uniform = true;
  2060. eyePos->constSortPos = cspPass;
  2061. }
  2062. // All actual work is offloaded to this method.
  2063. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2064. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2065. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2066. output = meta;
  2067. }
  2068. void FoliageFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2069. const MaterialFeatureData &fd )
  2070. {
  2071. // Find / create IN.foliageFade
  2072. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2073. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2074. fade->setName( "foliageFade" );
  2075. fade->setStructName( "IN" );
  2076. fade->setType( "float" );
  2077. // Find / create visibility
  2078. Var *visibility = (Var*) LangElement::find( "visibility" );
  2079. if ( !visibility )
  2080. {
  2081. visibility = new Var();
  2082. visibility->setType( "float" );
  2083. visibility->setName( "visibility" );
  2084. visibility->uniform = true;
  2085. visibility->constSortPos = cspPotentialPrimitive;
  2086. }
  2087. MultiLine *meta = new MultiLine;
  2088. // Multiply foliageFade into visibility.
  2089. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2090. output = meta;
  2091. }
  2092. void FoliageFeatureGLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2093. {
  2094. // This isn't really necessary since the outFeatureData will be filtered after
  2095. // this call.
  2096. if ( features.hasFeature( MFT_Foliage ) )
  2097. outFeatureData->features.addFeature( type );
  2098. }
  2099. ShaderFeatureConstHandles* FoliageFeatureGLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2100. {
  2101. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2102. AssertFatal( gcover != NULL, "FoliageFeatureGLSL::createConstHandles - userObject was not valid!" );
  2103. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2104. handles->mGroundCover = gcover;
  2105. handles->init( shader );
  2106. return handles;
  2107. }
  2108. void ParticleNormalFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2109. {
  2110. MultiLine *meta = new MultiLine;
  2111. output = meta;
  2112. // Calculate normal and tangent values since we want to keep particle verts
  2113. // as light-weight as possible
  2114. Var *normal = (Var*) LangElement::find("normal");
  2115. if(normal == NULL)
  2116. {
  2117. normal = new Var;
  2118. normal->setType( "vec3" );
  2119. normal->setName( "normal" );
  2120. // These values are not accidental. It is slightly adjusted from facing straight into the
  2121. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2122. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2123. // [11/23/2009 Pat]
  2124. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2125. }
  2126. Var *T = (Var*) LangElement::find( "T" );
  2127. if(T == NULL)
  2128. {
  2129. T = new Var;
  2130. T->setType( "vec3" );
  2131. T->setName( "T" );
  2132. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2133. }
  2134. }
  2135. //****************************************************************************
  2136. // ImposterVertFeatureGLSL
  2137. //****************************************************************************
  2138. ImposterVertFeatureGLSL::ImposterVertFeatureGLSL()
  2139. : mDep( "shaders/common/gl/imposter.glsl" )
  2140. {
  2141. addDependency( &mDep );
  2142. }
  2143. void ImposterVertFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2144. const MaterialFeatureData &fd )
  2145. {
  2146. MultiLine *meta = new MultiLine;
  2147. output = meta;
  2148. // Get the input vertex variables.
  2149. Var *inPosition = (Var*)LangElement::find( "position" );
  2150. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2151. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2152. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2153. // Get the input shader constants.
  2154. Var *imposterLimits = new Var;
  2155. imposterLimits->setType( "vec4" );
  2156. imposterLimits->setName( "imposterLimits" );
  2157. imposterLimits->uniform = true;
  2158. imposterLimits->constSortPos = cspPotentialPrimitive;
  2159. Var *imposterUVs = new Var;
  2160. imposterUVs->setType( "vec4" );
  2161. imposterUVs->setName( "imposterUVs" );
  2162. imposterUVs->arraySize = 64; // See imposter.glsl
  2163. imposterUVs->uniform = true;
  2164. imposterUVs->constSortPos = cspPotentialPrimitive;
  2165. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2166. if ( !eyePos )
  2167. {
  2168. eyePos = new Var( "eyePosWorld", "vec3" );
  2169. eyePos->uniform = true;
  2170. eyePos->constSortPos = cspPass;
  2171. }
  2172. // Declare the outputs from this feature.
  2173. Var *outInPosition = new Var;
  2174. outInPosition->setType( "vec3" );
  2175. outInPosition->setName( "inPosition" );
  2176. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2177. Var *outTexCoord = new Var;
  2178. outTexCoord->setType( "vec2" );
  2179. outTexCoord->setName( "texCoord" );
  2180. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2181. Var *outWorldToTangent = new Var;
  2182. outWorldToTangent->setType( "float3x3" );
  2183. outWorldToTangent->setName( "worldToTangent" );
  2184. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2185. // Add imposterFade to the OUT structure.
  2186. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2187. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2188. outFade->setName( "imposterFade" );
  2189. outFade->setStructName( "OUT" );
  2190. outFade->setType( "float" );
  2191. // Assign OUT.imposterFade
  2192. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2193. // All actual work is done in this method.
  2194. meta->addStatement( new GenOp( " imposter_v( @.xyz, int(@.w), @.x * length(@), normalize(@), normalize(@), int(@.y), int(@.x), @.z, bool(@.w), @, @, @, @, @ );\r\n",
  2195. inPosition,
  2196. inPosition,
  2197. inMiscParams,
  2198. inRightVec,
  2199. inUpVec,
  2200. inRightVec,
  2201. imposterLimits,
  2202. imposterLimits,
  2203. imposterLimits,
  2204. imposterLimits,
  2205. eyePos,
  2206. imposterUVs,
  2207. outInPosition,
  2208. outTexCoord,
  2209. outWorldToTangent ) );
  2210. // Copy the position to wsPosition for use in shaders
  2211. // down stream instead of looking for objTrans.
  2212. Var *wsPosition = new Var;
  2213. wsPosition->setType( "vec3" );
  2214. wsPosition->setName( "wsPosition" );
  2215. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2216. // If we new viewToTangent... its the same as the
  2217. // world to tangent for an imposter.
  2218. Var *viewToTangent = new Var;
  2219. viewToTangent->setType( "float3x3" );
  2220. viewToTangent->setName( "viewToTangent" );
  2221. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2222. }
  2223. void ImposterVertFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2224. const MaterialFeatureData &fd )
  2225. {
  2226. // Find / create IN.imposterFade
  2227. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2228. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2229. fade->setName( "imposterFade" );
  2230. fade->setStructName( "IN" );
  2231. fade->setType( "float" );
  2232. // Find / create visibility
  2233. Var *visibility = (Var*) LangElement::find( "visibility" );
  2234. if ( !visibility )
  2235. {
  2236. visibility = new Var();
  2237. visibility->setType( "float" );
  2238. visibility->setName( "visibility" );
  2239. visibility->uniform = true;
  2240. visibility->constSortPos = cspPotentialPrimitive;
  2241. }
  2242. MultiLine *meta = new MultiLine;
  2243. // Multiply foliageFade into visibility.
  2244. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2245. output = meta;
  2246. }
  2247. void ImposterVertFeatureGLSL::determineFeature( Material *material,
  2248. const GFXVertexFormat *vertexFormat,
  2249. U32 stageNum,
  2250. const FeatureType &type,
  2251. const FeatureSet &features,
  2252. MaterialFeatureData *outFeatureData )
  2253. {
  2254. if ( features.hasFeature( MFT_ImposterVert ) )
  2255. outFeatureData->features.addFeature( MFT_ImposterVert );
  2256. }