shaderFeatureHLSL.cpp 110 KB

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