shaderFeatureGLSL.cpp 92 KB

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