shaderFeatureGLSL.cpp 106 KB

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