shaderFeatureHLSL.cpp 111 KB

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