shaderFeatureGLSL.cpp 106 KB

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