shaderFeatureHLSL.cpp 93 KB

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