shaderFeatureGLSL.cpp 92 KB

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