shaderFeatureHLSL.cpp 92 KB

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