shaderFeatureGLSL.cpp 92 KB

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