shaderFeatureHLSL.cpp 110 KB

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