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