shaderFeatureHLSL.cpp 111 KB

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