shaderFeatureHLSL.cpp 101 KB

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