shaderFeatureGLSL.cpp 109 KB

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