shaderFeatureHLSL.cpp 101 KB

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