shaderFeatureHLSL.cpp 111 KB

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