shaderFeatureHLSL.cpp 111 KB

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