shaderFeatureHLSL.cpp 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704
  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. void DiffuseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  697. const MaterialFeatureData &fd )
  698. {
  699. MultiLine *meta = new MultiLine;
  700. getOutTexCoord( "texCoord",
  701. "float2",
  702. true,
  703. fd.features[MFT_TexAnim],
  704. meta,
  705. componentList );
  706. output = meta;
  707. }
  708. void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  709. const MaterialFeatureData &fd )
  710. {
  711. // grab connector texcoord register
  712. Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
  713. // create texture var
  714. Var *diffuseMap = new Var;
  715. diffuseMap->setType( "sampler2D" );
  716. diffuseMap->setName( "diffuseMap" );
  717. diffuseMap->uniform = true;
  718. diffuseMap->sampler = true;
  719. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  720. if ( fd.features[MFT_CubeMap] )
  721. {
  722. MultiLine * meta = new MultiLine;
  723. // create sample color
  724. Var *diffColor = new Var;
  725. diffColor->setType( "float4" );
  726. diffColor->setName( "diffuseColor" );
  727. LangElement *colorDecl = new DecOp( diffColor );
  728. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n",
  729. colorDecl,
  730. diffuseMap,
  731. inTex ) );
  732. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffColor, Material::Mul ) ) );
  733. output = meta;
  734. }
  735. else if(fd.features[MFT_DiffuseMapAtlas])
  736. {
  737. // Handle atlased textures
  738. // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
  739. MultiLine * meta = new MultiLine;
  740. output = meta;
  741. Var *atlasedTex = new Var;
  742. atlasedTex->setName("atlasedTexCoord");
  743. atlasedTex->setType("float2");
  744. LangElement *atDecl = new DecOp(atlasedTex);
  745. // Parameters of the texture atlas
  746. Var *atParams = new Var;
  747. atParams->setType("float4");
  748. atParams->setName("diffuseAtlasParams");
  749. atParams->uniform = true;
  750. atParams->constSortPos = cspPotentialPrimitive;
  751. // Parameters of the texture (tile) this object is using in the atlas
  752. Var *tileParams = new Var;
  753. tileParams->setType("float4");
  754. tileParams->setName("diffuseAtlasTileParams");
  755. tileParams->uniform = true;
  756. tileParams->constSortPos = cspPotentialPrimitive;
  757. const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
  758. if(is_sm3)
  759. {
  760. // Figure out the mip level
  761. meta->addStatement(new GenOp(" float2 _dx = ddx(@ * @.z);\r\n", inTex, atParams));
  762. meta->addStatement(new GenOp(" float2 _dy = ddy(@ * @.z);\r\n", inTex, atParams));
  763. meta->addStatement(new GenOp(" float mipLod = 0.5 * log2(max(dot(_dx, _dx), dot(_dy, _dy)));\r\n"));
  764. meta->addStatement(new GenOp(" mipLod = clamp(mipLod, 0.0, @.w);\r\n", atParams));
  765. // And the size of the mip level
  766. meta->addStatement(new GenOp(" float mipPixSz = pow(2.0, @.w - mipLod);\r\n", atParams));
  767. meta->addStatement(new GenOp(" float2 mipSz = mipPixSz / @.xy;\r\n", atParams));
  768. }
  769. else
  770. {
  771. meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
  772. }
  773. // Tiling mode
  774. // TODO: Select wrap or clamp somehow
  775. if( true ) // Wrap
  776. meta->addStatement(new GenOp(" @ = frac(@);\r\n", atDecl, inTex));
  777. else // Clamp
  778. meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, inTex));
  779. // Finally scale/offset, and correct for filtering
  780. meta->addStatement(new GenOp(" @ = @ * ((mipSz * @.xy - 1.0) / mipSz) + 0.5 / mipSz + @.xy * @.xy;\r\n",
  781. atlasedTex, atlasedTex, atParams, atParams, tileParams));
  782. // Add a newline
  783. meta->addStatement(new GenOp( "\r\n"));
  784. // For the rest of the feature...
  785. inTex = atlasedTex;
  786. // create sample color var
  787. Var *diffColor = new Var;
  788. diffColor->setType("float4");
  789. diffColor->setName("diffuseColor");
  790. // To dump out UV coords...
  791. //#define DEBUG_ATLASED_UV_COORDS
  792. #ifdef DEBUG_ATLASED_UV_COORDS
  793. if(!fd.features[MFT_PrePassConditioner])
  794. {
  795. meta->addStatement(new GenOp(" @ = float4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
  796. meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul)));
  797. return;
  798. }
  799. #endif
  800. if(is_sm3)
  801. {
  802. meta->addStatement(new GenOp( " @ = tex2Dlod(@, float4(@, 0.0, mipLod));\r\n",
  803. new DecOp(diffColor), diffuseMap, inTex));
  804. }
  805. else
  806. {
  807. meta->addStatement(new GenOp( " @ = tex2D(@, @);\r\n",
  808. new DecOp(diffColor), diffuseMap, inTex));
  809. }
  810. meta->addStatement(new GenOp( " @;\r\n", assignColor(diffColor, Material::Mul)));
  811. }
  812. else
  813. {
  814. LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
  815. output = new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) );
  816. }
  817. }
  818. ShaderFeature::Resources DiffuseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  819. {
  820. Resources res;
  821. res.numTex = 1;
  822. res.numTexReg = 1;
  823. return res;
  824. }
  825. void DiffuseMapFeatHLSL::setTexData( Material::StageData &stageDat,
  826. const MaterialFeatureData &fd,
  827. RenderPassData &passData,
  828. U32 &texIndex )
  829. {
  830. GFXTextureObject *tex = stageDat.getTex( MFT_DiffuseMap );
  831. if ( tex )
  832. passData.mTexSlot[ texIndex++ ].texObject = tex;
  833. }
  834. //****************************************************************************
  835. // Overlay Texture
  836. //****************************************************************************
  837. void OverlayTexFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  838. const MaterialFeatureData &fd )
  839. {
  840. Var *inTex = getVertTexCoord( "texCoord2" );
  841. AssertFatal( inTex, "OverlayTexFeatHLSL::processVert() - The second UV set was not found!" );
  842. // grab connector texcoord register
  843. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  844. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  845. outTex->setName( "outTexCoord2" );
  846. outTex->setStructName( "OUT" );
  847. outTex->setType( "float2" );
  848. outTex->mapsToSampler = true;
  849. if( fd.features[MFT_TexAnim] )
  850. {
  851. inTex->setType( "float4" );
  852. // Find or create the texture matrix.
  853. Var *texMat = (Var*)LangElement::find( "texMat" );
  854. if ( !texMat )
  855. {
  856. texMat = new Var;
  857. texMat->setType( "float4x4" );
  858. texMat->setName( "texMat" );
  859. texMat->uniform = true;
  860. texMat->constSortPos = cspPass;
  861. }
  862. output = new GenOp( " @ = mul(@, @);\r\n", outTex, texMat, inTex );
  863. return;
  864. }
  865. // setup language elements to output incoming tex coords to output
  866. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  867. }
  868. void OverlayTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  869. const MaterialFeatureData &fd )
  870. {
  871. // grab connector texcoord register
  872. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  873. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  874. inTex->setName( "texCoord2" );
  875. inTex->setStructName( "IN" );
  876. inTex->setType( "float2" );
  877. inTex->mapsToSampler = true;
  878. // create texture var
  879. Var *diffuseMap = new Var;
  880. diffuseMap->setType( "sampler2D" );
  881. diffuseMap->setName( "overlayMap" );
  882. diffuseMap->uniform = true;
  883. diffuseMap->sampler = true;
  884. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  885. LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
  886. output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
  887. }
  888. ShaderFeature::Resources OverlayTexFeatHLSL::getResources( const MaterialFeatureData &fd )
  889. {
  890. Resources res;
  891. res.numTex = 1;
  892. res.numTexReg = 1;
  893. return res;
  894. }
  895. void OverlayTexFeatHLSL::setTexData( Material::StageData &stageDat,
  896. const MaterialFeatureData &fd,
  897. RenderPassData &passData,
  898. U32 &texIndex )
  899. {
  900. GFXTextureObject *tex = stageDat.getTex( MFT_OverlayMap );
  901. if ( tex )
  902. passData.mTexSlot[ texIndex++ ].texObject = tex;
  903. }
  904. //****************************************************************************
  905. // Diffuse color
  906. //****************************************************************************
  907. void DiffuseFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  908. const MaterialFeatureData &fd )
  909. {
  910. Var *diffuseMaterialColor = new Var;
  911. diffuseMaterialColor->setType( "float4" );
  912. diffuseMaterialColor->setName( "diffuseMaterialColor" );
  913. diffuseMaterialColor->uniform = true;
  914. diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
  915. MultiLine * meta = new MultiLine;
  916. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffuseMaterialColor, Material::Mul ) ) );
  917. output = meta;
  918. }
  919. //****************************************************************************
  920. // Diffuse vertex color
  921. //****************************************************************************
  922. void DiffuseVertColorFeatureHLSL::processVert( Vector< ShaderComponent* >& componentList,
  923. const MaterialFeatureData& fd )
  924. {
  925. // Create vertex color connector if it doesn't exist.
  926. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  927. if( !outColor )
  928. {
  929. // Search for vert color.
  930. Var* inColor = dynamic_cast< Var* >( LangElement::find( "diffuse" ) );
  931. if( !inColor )
  932. {
  933. output = NULL;
  934. return;
  935. }
  936. // Create connector.
  937. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  938. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  939. Var* outColor = connectComp->getElement( RT_COLOR );
  940. outColor->setName( "vertColor" );
  941. outColor->setStructName( "OUT" );
  942. outColor->setType( "float4" );
  943. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  944. }
  945. else
  946. output = NULL; // Nothing we need to do.
  947. }
  948. void DiffuseVertColorFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  949. const MaterialFeatureData &fd )
  950. {
  951. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  952. if( !vertColor )
  953. {
  954. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  955. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  956. vertColor = connectComp->getElement( RT_COLOR );
  957. vertColor->setName( "vertColor" );
  958. vertColor->setStructName( "IN" );
  959. vertColor->setType( "float4" );
  960. }
  961. MultiLine* meta = new MultiLine;
  962. meta->addStatement( new GenOp( " @;\r\n", assignColor( vertColor, Material::Mul ) ) );
  963. output = meta;
  964. }
  965. //****************************************************************************
  966. // Lightmap
  967. //****************************************************************************
  968. void LightmapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  969. const MaterialFeatureData &fd )
  970. {
  971. // grab tex register from incoming vert
  972. Var *inTex = getVertTexCoord( "texCoord2" );
  973. // grab connector texcoord register
  974. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  975. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  976. outTex->setName( "texCoord2" );
  977. outTex->setStructName( "OUT" );
  978. outTex->setType( "float2" );
  979. outTex->mapsToSampler = true;
  980. // setup language elements to output incoming tex coords to output
  981. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  982. }
  983. void LightmapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  984. const MaterialFeatureData &fd )
  985. {
  986. // grab connector texcoord register
  987. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  988. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  989. inTex->setName( "texCoord2" );
  990. inTex->setStructName( "IN" );
  991. inTex->setType( "float2" );
  992. inTex->mapsToSampler = true;
  993. // create texture var
  994. Var *lightMap = new Var;
  995. lightMap->setType( "sampler2D" );
  996. lightMap->setName( "lightMap" );
  997. lightMap->uniform = true;
  998. lightMap->sampler = true;
  999. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1000. // argh, pixel specular should prob use this too
  1001. if( fd.features[MFT_NormalMap] )
  1002. {
  1003. Var *lmColor = new Var;
  1004. lmColor->setName( "lmColor" );
  1005. lmColor->setType( "float4" );
  1006. LangElement *lmColorDecl = new DecOp( lmColor );
  1007. output = new GenOp( " @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
  1008. return;
  1009. }
  1010. // Add realtime lighting, if it is available
  1011. LangElement *statement = NULL;
  1012. if( fd.features[MFT_RTLighting] )
  1013. {
  1014. // Advanced lighting is the only dynamic lighting supported right now
  1015. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1016. if(inColor != NULL)
  1017. {
  1018. // Find out if RTLighting should be added or substituted
  1019. bool bPreProcessedLighting = false;
  1020. AdvancedLightBinManager *lightBin;
  1021. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1022. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1023. // Lightmap has already been included in the advanced light bin, so
  1024. // no need to do any sampling or anything
  1025. if(bPreProcessedLighting)
  1026. statement = new GenOp( "float4(@, 1.0)", inColor );
  1027. else
  1028. statement = new GenOp( "tex2D(@, @) + float4(@.rgb, 0.0)", lightMap, inTex, inColor );
  1029. }
  1030. }
  1031. // If we still don't have it... then just sample the lightmap.
  1032. if ( !statement )
  1033. statement = new GenOp( "tex2D(@, @)", lightMap, inTex );
  1034. // Assign to proper render target
  1035. MultiLine *meta = new MultiLine;
  1036. if( fd.features[MFT_LightbufferMRT] )
  1037. {
  1038. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1039. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
  1040. }
  1041. else
  1042. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
  1043. output = meta;
  1044. }
  1045. ShaderFeature::Resources LightmapFeatHLSL::getResources( const MaterialFeatureData &fd )
  1046. {
  1047. Resources res;
  1048. res.numTex = 1;
  1049. res.numTexReg = 1;
  1050. return res;
  1051. }
  1052. void LightmapFeatHLSL::setTexData( Material::StageData &stageDat,
  1053. const MaterialFeatureData &fd,
  1054. RenderPassData &passData,
  1055. U32 &texIndex )
  1056. {
  1057. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  1058. if ( tex )
  1059. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1060. else
  1061. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  1062. }
  1063. U32 LightmapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1064. {
  1065. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1066. }
  1067. //****************************************************************************
  1068. // Tonemap
  1069. //****************************************************************************
  1070. void TonemapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1071. const MaterialFeatureData &fd )
  1072. {
  1073. // Grab the connector
  1074. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1075. // Set up the second set of texCoords
  1076. Var *inTex2 = getVertTexCoord( "texCoord2" );
  1077. if ( inTex2 )
  1078. {
  1079. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  1080. outTex2->setName( "texCoord2" );
  1081. outTex2->setStructName( "OUT" );
  1082. outTex2->setType( "float2" );
  1083. outTex2->mapsToSampler = true;
  1084. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  1085. }
  1086. }
  1087. void TonemapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1088. const MaterialFeatureData &fd )
  1089. {
  1090. // Grab connector
  1091. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1092. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  1093. inTex2->setName( "texCoord2" );
  1094. inTex2->setStructName( "IN" );
  1095. inTex2->setType( "float2" );
  1096. inTex2->mapsToSampler = true;
  1097. // create texture var
  1098. Var *toneMap = new Var;
  1099. toneMap->setType( "sampler2D" );
  1100. toneMap->setName( "toneMap" );
  1101. toneMap->uniform = true;
  1102. toneMap->sampler = true;
  1103. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1104. MultiLine * meta = new MultiLine;
  1105. // First get the toneMap color
  1106. Var *toneMapColor = new Var;
  1107. toneMapColor->setType( "float4" );
  1108. toneMapColor->setName( "toneMapColor" );
  1109. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  1110. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
  1111. // We do a different calculation if there is a diffuse map or not
  1112. Material::BlendOp blendOp = Material::Mul;
  1113. if ( fd.features[MFT_DiffuseMap] )
  1114. {
  1115. // Reverse the tonemap
  1116. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
  1117. // Re-tonemap with the current color factored in
  1118. blendOp = Material::ToneMap;
  1119. }
  1120. // Find out if RTLighting should be added
  1121. bool bPreProcessedLighting = false;
  1122. AdvancedLightBinManager *lightBin;
  1123. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1124. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1125. // Add in the realtime lighting contribution
  1126. if ( fd.features[MFT_RTLighting] )
  1127. {
  1128. // Right now, only Advanced Lighting is supported
  1129. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1130. if(inColor != NULL)
  1131. {
  1132. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1133. // the dynamic light buffer, and it already has the tonemap included
  1134. if(bPreProcessedLighting)
  1135. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  1136. else
  1137. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  1138. }
  1139. }
  1140. // Assign to proper render target
  1141. if( fd.features[MFT_LightbufferMRT] )
  1142. {
  1143. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1144. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
  1145. }
  1146. else
  1147. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  1148. output = meta;
  1149. }
  1150. ShaderFeature::Resources TonemapFeatHLSL::getResources( const MaterialFeatureData &fd )
  1151. {
  1152. Resources res;
  1153. res.numTex = 1;
  1154. res.numTexReg = 1;
  1155. return res;
  1156. }
  1157. void TonemapFeatHLSL::setTexData( Material::StageData &stageDat,
  1158. const MaterialFeatureData &fd,
  1159. RenderPassData &passData,
  1160. U32 &texIndex )
  1161. {
  1162. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  1163. if ( tex )
  1164. {
  1165. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  1166. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1167. }
  1168. }
  1169. U32 TonemapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1170. {
  1171. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1172. }
  1173. //****************************************************************************
  1174. // pureLIGHT Lighting
  1175. //****************************************************************************
  1176. void VertLitHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1177. const MaterialFeatureData &fd )
  1178. {
  1179. // If we have a lightMap or toneMap then our lighting will be
  1180. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1181. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1182. {
  1183. output = NULL;
  1184. return;
  1185. }
  1186. // Create vertex color connector if it doesn't exist.
  1187. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1188. if( !outColor )
  1189. {
  1190. // Search for vert color
  1191. Var *inColor = (Var*) LangElement::find( "diffuse" );
  1192. // If there isn't a vertex color then we can't do anything
  1193. if( !inColor )
  1194. {
  1195. output = NULL;
  1196. return;
  1197. }
  1198. // Grab the connector color
  1199. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1200. Var *outColor = connectComp->getElement( RT_COLOR );
  1201. outColor->setName( "vertColor" );
  1202. outColor->setStructName( "OUT" );
  1203. outColor->setType( "float4" );
  1204. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1205. }
  1206. else
  1207. output = NULL; // Nothing we need to do.
  1208. }
  1209. void VertLitHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1210. const MaterialFeatureData &fd )
  1211. {
  1212. // If we have a lightMap or toneMap then our lighting will be
  1213. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1214. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1215. {
  1216. output = NULL;
  1217. return;
  1218. }
  1219. // Grab the connector color register
  1220. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1221. if( !vertColor )
  1222. {
  1223. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1224. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1225. vertColor = connectComp->getElement( RT_COLOR );
  1226. vertColor->setName( "vertColor" );
  1227. vertColor->setStructName( "IN" );
  1228. vertColor->setType( "float4" );
  1229. }
  1230. MultiLine * meta = new MultiLine;
  1231. // Defaults (no diffuse map)
  1232. Material::BlendOp blendOp = Material::Mul;
  1233. LangElement *outColor = vertColor;
  1234. // We do a different calculation if there is a diffuse map or not
  1235. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1236. {
  1237. Var * finalVertColor = new Var;
  1238. finalVertColor->setName( "finalVertColor" );
  1239. finalVertColor->setType( "float4" );
  1240. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1241. // Reverse the tonemap
  1242. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
  1243. // Set the blend op to tonemap
  1244. blendOp = Material::ToneMap;
  1245. outColor = finalVertColor;
  1246. }
  1247. // Add in the realtime lighting contribution, if applicable
  1248. if ( fd.features[MFT_RTLighting] )
  1249. {
  1250. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1251. if(rtLightingColor != NULL)
  1252. {
  1253. bool bPreProcessedLighting = false;
  1254. AdvancedLightBinManager *lightBin;
  1255. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1256. bPreProcessedLighting = lightBin->MRTLightmapsDuringPrePass();
  1257. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1258. // the dynamic light buffer, and it already has the baked-vertex-color
  1259. // included in it
  1260. if(bPreProcessedLighting)
  1261. outColor = new GenOp( "float4(@.rgb, 1.0)", rtLightingColor );
  1262. else
  1263. outColor = new GenOp( "float4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
  1264. }
  1265. }
  1266. // Output the color
  1267. if ( fd.features[MFT_LightbufferMRT] )
  1268. {
  1269. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
  1270. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
  1271. }
  1272. else
  1273. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1274. output = meta;
  1275. }
  1276. U32 VertLitHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1277. {
  1278. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  1279. }
  1280. //****************************************************************************
  1281. // Detail map
  1282. //****************************************************************************
  1283. void DetailFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1284. const MaterialFeatureData &fd )
  1285. {
  1286. MultiLine *meta = new MultiLine;
  1287. addOutDetailTexCoord( componentList,
  1288. meta,
  1289. fd.features[MFT_TexAnim] );
  1290. output = meta;
  1291. }
  1292. void DetailFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1293. const MaterialFeatureData &fd )
  1294. {
  1295. // Get the detail texture coord.
  1296. Var *inTex = getInTexCoord( "detCoord", "float2", true, componentList );
  1297. // create texture var
  1298. Var *detailMap = new Var;
  1299. detailMap->setType( "sampler2D" );
  1300. detailMap->setName( "detailMap" );
  1301. detailMap->uniform = true;
  1302. detailMap->sampler = true;
  1303. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1304. // We're doing the standard greyscale detail map
  1305. // technique which can darken and lighten the
  1306. // diffuse texture.
  1307. // TODO: We could add a feature to toggle between this
  1308. // and a simple multiplication with the detail map.
  1309. LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
  1310. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1311. }
  1312. ShaderFeature::Resources DetailFeatHLSL::getResources( const MaterialFeatureData &fd )
  1313. {
  1314. Resources res;
  1315. res.numTex = 1;
  1316. res.numTexReg = 1;
  1317. return res;
  1318. }
  1319. void DetailFeatHLSL::setTexData( Material::StageData &stageDat,
  1320. const MaterialFeatureData &fd,
  1321. RenderPassData &passData,
  1322. U32 &texIndex )
  1323. {
  1324. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1325. if ( tex )
  1326. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1327. }
  1328. //****************************************************************************
  1329. // Vertex position
  1330. //****************************************************************************
  1331. void VertPositionHLSL::determineFeature( Material *material,
  1332. const GFXVertexFormat *vertexFormat,
  1333. U32 stageNum,
  1334. const FeatureType &type,
  1335. const FeatureSet &features,
  1336. MaterialFeatureData *outFeatureData )
  1337. {
  1338. // This feature is always on!
  1339. outFeatureData->features.addFeature( type );
  1340. }
  1341. void VertPositionHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1342. const MaterialFeatureData &fd )
  1343. {
  1344. // First check for an input position from a previous feature
  1345. // then look for the default vertex position.
  1346. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1347. if ( !inPosition )
  1348. inPosition = (Var*)LangElement::find( "position" );
  1349. // grab connector position
  1350. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1351. Var *outPosition = connectComp->getElement( RT_POSITION );
  1352. outPosition->setName( "hpos" );
  1353. outPosition->setStructName( "OUT" );
  1354. MultiLine *meta = new MultiLine;
  1355. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1356. meta->addStatement( new GenOp( " @ = mul(@, float4(@.xyz,1));\r\n",
  1357. outPosition, modelview, inPosition ) );
  1358. output = meta;
  1359. }
  1360. //****************************************************************************
  1361. // Reflect Cubemap
  1362. //****************************************************************************
  1363. void ReflectCubeFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1364. const MaterialFeatureData &fd )
  1365. {
  1366. // search for vert normal
  1367. Var *inNormal = (Var*) LangElement::find( "normal" );
  1368. if ( !inNormal )
  1369. return;
  1370. MultiLine * meta = new MultiLine;
  1371. // If a base or bump tex is present in the material, but not in the
  1372. // current pass - we need to add one to the current pass to use
  1373. // its alpha channel as a gloss map. Here we just need the tex coords.
  1374. if( !fd.features[MFT_DiffuseMap] &&
  1375. !fd.features[MFT_NormalMap] )
  1376. {
  1377. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1378. fd.materialFeatures[MFT_NormalMap] )
  1379. {
  1380. // find incoming texture var
  1381. Var *inTex = getVertTexCoord( "texCoord" );
  1382. // grab connector texcoord register
  1383. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1384. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1385. outTex->setName( "texCoord" );
  1386. outTex->setStructName( "OUT" );
  1387. outTex->setType( "float2" );
  1388. outTex->mapsToSampler = true;
  1389. // setup language elements to output incoming tex coords to output
  1390. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1391. }
  1392. }
  1393. // create cubeTrans
  1394. bool useInstancing = fd.features[MFT_UseInstancing];
  1395. Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
  1396. // cube vert position
  1397. Var * cubeVertPos = new Var;
  1398. cubeVertPos->setName( "cubeVertPos" );
  1399. cubeVertPos->setType( "float3" );
  1400. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1401. meta->addStatement( new GenOp( " @ = mul((float3x3)@, @).xyz;\r\n",
  1402. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1403. // cube normal
  1404. Var * cubeNormal = new Var;
  1405. cubeNormal->setName( "cubeNormal" );
  1406. cubeNormal->setType( "float3" );
  1407. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1408. meta->addStatement( new GenOp( " @ = normalize( mul(@, float4(normalize(@),0.0)).xyz );\r\n",
  1409. cubeNormDecl, cubeTrans, inNormal ) );
  1410. // grab the eye position
  1411. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1412. if ( !eyePos )
  1413. {
  1414. eyePos = new Var( "eyePosWorld", "float3" );
  1415. eyePos->uniform = true;
  1416. eyePos->constSortPos = cspPass;
  1417. }
  1418. // cube position
  1419. Var * cubePos = new Var;
  1420. cubePos->setName( "cubePos" );
  1421. cubePos->setType( "float3" );
  1422. LangElement *cubePosDecl = new DecOp( cubePos );
  1423. meta->addStatement( new GenOp( " @ = float3( @[0][3], @[1][3], @[2][3] );\r\n",
  1424. cubePosDecl, cubeTrans, cubeTrans, cubeTrans ) );
  1425. // eye to vert
  1426. Var * eyeToVert = new Var;
  1427. eyeToVert->setName( "eyeToVert" );
  1428. eyeToVert->setType( "float3" );
  1429. LangElement *e2vDecl = new DecOp( eyeToVert );
  1430. meta->addStatement( new GenOp( " @ = @ - ( @ - @ );\r\n",
  1431. e2vDecl, cubeVertPos, eyePos, cubePos ) );
  1432. // grab connector texcoord register
  1433. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1434. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1435. reflectVec->setName( "reflectVec" );
  1436. reflectVec->setStructName( "OUT" );
  1437. reflectVec->setType( "float3" );
  1438. reflectVec->mapsToSampler = true;
  1439. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1440. output = meta;
  1441. }
  1442. void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1443. const MaterialFeatureData &fd )
  1444. {
  1445. MultiLine * meta = new MultiLine;
  1446. Var *glossColor = NULL;
  1447. // If a base or bump tex is present in the material, but not in the
  1448. // current pass - we need to add one to the current pass to use
  1449. // its alpha channel as a gloss map.
  1450. if( !fd.features[MFT_DiffuseMap] &&
  1451. !fd.features[MFT_NormalMap] )
  1452. {
  1453. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1454. fd.materialFeatures[MFT_NormalMap] )
  1455. {
  1456. // grab connector texcoord register
  1457. Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
  1458. // create texture var
  1459. Var *newMap = new Var;
  1460. newMap->setType( "sampler2D" );
  1461. newMap->setName( "glossMap" );
  1462. newMap->uniform = true;
  1463. newMap->sampler = true;
  1464. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1465. // create sample color
  1466. Var *color = new Var;
  1467. color->setType( "float4" );
  1468. color->setName( "diffuseColor" );
  1469. LangElement *colorDecl = new DecOp( color );
  1470. glossColor = color;
  1471. meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
  1472. }
  1473. }
  1474. else
  1475. {
  1476. glossColor = (Var*) LangElement::find( "diffuseColor" );
  1477. if( !glossColor )
  1478. glossColor = (Var*) LangElement::find( "bumpNormal" );
  1479. }
  1480. // grab connector texcoord register
  1481. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1482. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1483. reflectVec->setName( "reflectVec" );
  1484. reflectVec->setStructName( "IN" );
  1485. reflectVec->setType( "float3" );
  1486. reflectVec->mapsToSampler = true;
  1487. // create cubemap var
  1488. Var *cubeMap = new Var;
  1489. cubeMap->setType( "samplerCUBE" );
  1490. cubeMap->setName( "cubeMap" );
  1491. cubeMap->uniform = true;
  1492. cubeMap->sampler = true;
  1493. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1494. // TODO: Restore the lighting attenuation here!
  1495. Var *attn = NULL;
  1496. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1497. //attn = (Var*)LangElement::find("attn");
  1498. //else
  1499. if ( fd.materialFeatures[MFT_RTLighting] )
  1500. attn =(Var*)LangElement::find("d_NL_Att");
  1501. LangElement *texCube = new GenOp( "texCUBE( @, @ )", cubeMap, reflectVec );
  1502. LangElement *lerpVal = NULL;
  1503. Material::BlendOp blendOp = Material::LerpAlpha;
  1504. // Note that the lerpVal needs to be a float4 so that
  1505. // it will work with the LerpAlpha blend.
  1506. if ( glossColor )
  1507. {
  1508. if ( attn )
  1509. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1510. else
  1511. lerpVal = glossColor;
  1512. }
  1513. else
  1514. {
  1515. if ( attn )
  1516. lerpVal = new GenOp( "saturate( @ ).xxxx", attn );
  1517. else
  1518. blendOp = Material::Mul;
  1519. }
  1520. meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
  1521. output = meta;
  1522. }
  1523. ShaderFeature::Resources ReflectCubeFeatHLSL::getResources( const MaterialFeatureData &fd )
  1524. {
  1525. Resources res;
  1526. if( fd.features[MFT_DiffuseMap] ||
  1527. fd.features[MFT_NormalMap] )
  1528. {
  1529. res.numTex = 1;
  1530. res.numTexReg = 1;
  1531. }
  1532. else
  1533. {
  1534. res.numTex = 2;
  1535. res.numTexReg = 2;
  1536. }
  1537. return res;
  1538. }
  1539. void ReflectCubeFeatHLSL::setTexData( Material::StageData &stageDat,
  1540. const MaterialFeatureData &stageFeatures,
  1541. RenderPassData &passData,
  1542. U32 &texIndex )
  1543. {
  1544. // set up a gloss map if one is not present in the current pass
  1545. // but is present in the current material stage
  1546. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1547. !passData.mFeatureData.features[MFT_NormalMap] )
  1548. {
  1549. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1550. if ( tex &&
  1551. stageFeatures.features[MFT_DiffuseMap] )
  1552. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1553. else
  1554. {
  1555. tex = stageDat.getTex( MFT_NormalMap );
  1556. if ( tex &&
  1557. stageFeatures.features[ MFT_NormalMap ] )
  1558. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1559. }
  1560. }
  1561. if( stageDat.getCubemap() )
  1562. {
  1563. passData.mCubeMap = stageDat.getCubemap();
  1564. passData.mTexType[texIndex++] = Material::Cube;
  1565. }
  1566. else
  1567. {
  1568. if( stageFeatures.features[MFT_CubeMap] )
  1569. {
  1570. // assuming here that it is a scenegraph cubemap
  1571. passData.mTexType[texIndex++] = Material::SGCube;
  1572. }
  1573. }
  1574. }
  1575. //****************************************************************************
  1576. // RTLighting
  1577. //****************************************************************************
  1578. RTLightingFeatHLSL::RTLightingFeatHLSL()
  1579. : mDep( "shaders/common/lighting.hlsl" )
  1580. {
  1581. addDependency( &mDep );
  1582. }
  1583. void RTLightingFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1584. const MaterialFeatureData &fd )
  1585. {
  1586. MultiLine *meta = new MultiLine;
  1587. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1588. // Special case for lighting imposters. We dont have a vert normal and may not
  1589. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1590. if ( fd.features[MFT_ImposterVert] )
  1591. {
  1592. if ( !fd.features[MFT_NormalMap] )
  1593. {
  1594. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1595. if ( !eyePos )
  1596. {
  1597. eyePos = new Var( "eyePosWorld", "float3" );
  1598. eyePos->uniform = true;
  1599. eyePos->constSortPos = cspPass;
  1600. }
  1601. Var *inPosition = (Var*)LangElement::find( "position" );
  1602. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1603. outNormal->setName( "wsNormal" );
  1604. outNormal->setStructName( "OUT" );
  1605. outNormal->setType( "float3" );
  1606. outNormal->mapsToSampler = false;
  1607. // Transform the normal to world space.
  1608. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );
  1609. }
  1610. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1611. output = meta;
  1612. return;
  1613. }
  1614. // Find the incoming vertex normal.
  1615. Var *inNormal = (Var*)LangElement::find( "normal" );
  1616. // Skip out on realtime lighting if we don't have a normal
  1617. // or we're doing some sort of baked lighting.
  1618. if ( !inNormal ||
  1619. fd.features[MFT_LightMap] ||
  1620. fd.features[MFT_ToneMap] ||
  1621. fd.features[MFT_VertLit] )
  1622. return;
  1623. // If there isn't a normal map then we need to pass
  1624. // the world space normal to the pixel shader ourselves.
  1625. if ( !fd.features[MFT_NormalMap] )
  1626. {
  1627. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1628. outNormal->setName( "wsNormal" );
  1629. outNormal->setStructName( "OUT" );
  1630. outNormal->setType( "float3" );
  1631. outNormal->mapsToSampler = false;
  1632. // Get the transform to world space.
  1633. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1634. // Transform the normal to world space.
  1635. meta->addStatement( new GenOp( " @ = mul( @, float4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1636. }
  1637. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1638. output = meta;
  1639. }
  1640. void RTLightingFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1641. const MaterialFeatureData &fd )
  1642. {
  1643. // Skip out on realtime lighting if we don't have a normal
  1644. // or we're doing some sort of baked lighting.
  1645. //
  1646. // TODO: We can totally detect for this in the material
  1647. // feature setup... we should move it out of here!
  1648. //
  1649. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1650. return;
  1651. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1652. MultiLine *meta = new MultiLine;
  1653. // Look for a wsNormal or grab it from the connector.
  1654. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  1655. if ( !wsNormal )
  1656. {
  1657. wsNormal = connectComp->getElement( RT_TEXCOORD );
  1658. wsNormal->setName( "wsNormal" );
  1659. wsNormal->setStructName( "IN" );
  1660. wsNormal->setType( "float3" );
  1661. // If we loaded the normal its our responsibility
  1662. // to normalize it... the interpolators won't.
  1663. //
  1664. // Note we cast to half here to get partial precision
  1665. // optimized code which is an acceptable loss of
  1666. // precision for normals and performs much better
  1667. // on older Geforce cards.
  1668. //
  1669. meta->addStatement( new GenOp( " @ = normalize( half3( @ ) );\r\n", wsNormal, wsNormal ) );
  1670. }
  1671. // Now the wsPosition and wsView.
  1672. Var *wsPosition = getInWsPosition( componentList );
  1673. Var *wsView = getWsView( wsPosition, meta );
  1674. // Create temporaries to hold results of lighting.
  1675. Var *rtShading = new Var( "rtShading", "float4" );
  1676. Var *specular = new Var( "specular", "float4" );
  1677. meta->addStatement( new GenOp( " @; @;\r\n",
  1678. new DecOp( rtShading ), new DecOp( specular ) ) );
  1679. // Look for a light mask generated from a previous
  1680. // feature (this is done for BL terrain lightmaps).
  1681. LangElement *lightMask = LangElement::find( "lightMask" );
  1682. if ( !lightMask )
  1683. lightMask = new GenOp( "float4( 1, 1, 1, 1 )" );
  1684. // Get all the light constants.
  1685. Var *inLightPos = new Var( "inLightPos", "float4" );
  1686. inLightPos->uniform = true;
  1687. inLightPos->arraySize = 3;
  1688. inLightPos->constSortPos = cspPotentialPrimitive;
  1689. Var *inLightInvRadiusSq = new Var( "inLightInvRadiusSq", "float4" );
  1690. inLightInvRadiusSq->uniform = true;
  1691. inLightInvRadiusSq->constSortPos = cspPotentialPrimitive;
  1692. Var *inLightColor = new Var( "inLightColor", "float4" );
  1693. inLightColor->uniform = true;
  1694. inLightColor->arraySize = 4;
  1695. inLightColor->constSortPos = cspPotentialPrimitive;
  1696. Var *inLightSpotDir = new Var( "inLightSpotDir", "float4" );
  1697. inLightSpotDir->uniform = true;
  1698. inLightSpotDir->arraySize = 3;
  1699. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1700. Var *inLightSpotAngle = new Var( "inLightSpotAngle", "float4" );
  1701. inLightSpotAngle->uniform = true;
  1702. inLightSpotAngle->constSortPos = cspPotentialPrimitive;
  1703. Var *lightSpotFalloff = new Var( "inLightSpotFalloff", "float4" );
  1704. lightSpotFalloff->uniform = true;
  1705. lightSpotFalloff->constSortPos = cspPotentialPrimitive;
  1706. Var *specularPower = new Var( "specularPower", "float" );
  1707. specularPower->uniform = true;
  1708. specularPower->constSortPos = cspPotentialPrimitive;
  1709. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  1710. if ( !specularColor )
  1711. {
  1712. specularColor = new Var( "specularColor", "float4" );
  1713. specularColor->uniform = true;
  1714. specularColor->constSortPos = cspPotentialPrimitive;
  1715. }
  1716. Var *ambient = new Var( "ambient", "float4" );
  1717. ambient->uniform = true;
  1718. ambient->constSortPos = cspPass;
  1719. // Calculate the diffuse shading and specular powers.
  1720. meta->addStatement( new GenOp( " compute4Lights( @, @, @, @,\r\n"
  1721. " @, @, @, @, @, @, @, @,\r\n"
  1722. " @, @ );\r\n",
  1723. wsView, wsPosition, wsNormal, lightMask,
  1724. inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, specularPower, specularColor,
  1725. rtShading, specular ) );
  1726. // Apply the lighting to the diffuse color.
  1727. LangElement *lighting = new GenOp( "float4( @.rgb + @.rgb, 1 )", rtShading, ambient );
  1728. meta->addStatement( new GenOp( " @;\r\n", assignColor( lighting, Material::Mul ) ) );
  1729. output = meta;
  1730. }
  1731. ShaderFeature::Resources RTLightingFeatHLSL::getResources( const MaterialFeatureData &fd )
  1732. {
  1733. Resources res;
  1734. // These features disable realtime lighting.
  1735. if ( !fd.features[MFT_LightMap] &&
  1736. !fd.features[MFT_ToneMap] &&
  1737. !fd.features[MFT_VertLit] )
  1738. {
  1739. // If enabled we pass the position.
  1740. res.numTexReg = 1;
  1741. // If there isn't a bump map then we pass the
  1742. // world space normal as well.
  1743. if ( !fd.features[MFT_NormalMap] )
  1744. res.numTexReg++;
  1745. }
  1746. return res;
  1747. }
  1748. //****************************************************************************
  1749. // Fog
  1750. //****************************************************************************
  1751. FogFeatHLSL::FogFeatHLSL()
  1752. : mFogDep( "shaders/common/torque.hlsl" )
  1753. {
  1754. addDependency( &mFogDep );
  1755. }
  1756. void FogFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1757. const MaterialFeatureData &fd )
  1758. {
  1759. MultiLine *meta = new MultiLine;
  1760. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1761. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1762. {
  1763. // Grab the eye position.
  1764. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1765. if ( !eyePos )
  1766. {
  1767. eyePos = new Var( "eyePosWorld", "float3" );
  1768. eyePos->uniform = true;
  1769. eyePos->constSortPos = cspPass;
  1770. }
  1771. Var *fogData = new Var( "fogData", "float3" );
  1772. fogData->uniform = true;
  1773. fogData->constSortPos = cspPass;
  1774. Var *wsPosition = new Var( "fogPos", "float3" );
  1775. getWsPosition( componentList,
  1776. fd.features[MFT_UseInstancing],
  1777. meta,
  1778. new DecOp( wsPosition ) );
  1779. // We pass the fog amount to the pixel shader.
  1780. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1781. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1782. fogAmount->setName( "fogAmount" );
  1783. fogAmount->setStructName( "OUT" );
  1784. fogAmount->setType( "float" );
  1785. fogAmount->mapsToSampler = false;
  1786. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1787. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1788. }
  1789. else
  1790. {
  1791. // We fog in world space... make sure the world space
  1792. // position is passed to the pixel shader. This is
  1793. // often already passed for lighting, so it takes up
  1794. // no extra output registers.
  1795. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1796. }
  1797. output = meta;
  1798. }
  1799. void FogFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1800. const MaterialFeatureData &fd )
  1801. {
  1802. MultiLine *meta = new MultiLine;
  1803. Var *fogColor = new Var;
  1804. fogColor->setType( "float4" );
  1805. fogColor->setName( "fogColor" );
  1806. fogColor->uniform = true;
  1807. fogColor->constSortPos = cspPass;
  1808. // Get the out color.
  1809. Var *color = (Var*) LangElement::find( "col" );
  1810. if ( !color )
  1811. {
  1812. color = new Var;
  1813. color->setType( "fragout" );
  1814. color->setName( "col" );
  1815. color->setStructName( "OUT" );
  1816. }
  1817. Var *fogAmount;
  1818. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1819. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1820. {
  1821. // Per-vertex.... just get the fog amount.
  1822. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1823. fogAmount = connectComp->getElement( RT_TEXCOORD );
  1824. fogAmount->setName( "fogAmount" );
  1825. fogAmount->setStructName( "IN" );
  1826. fogAmount->setType( "float" );
  1827. }
  1828. else
  1829. {
  1830. Var *wsPosition = getInWsPosition( componentList );
  1831. // grab the eye position
  1832. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1833. if ( !eyePos )
  1834. {
  1835. eyePos = new Var( "eyePosWorld", "float3" );
  1836. eyePos->uniform = true;
  1837. eyePos->constSortPos = cspPass;
  1838. }
  1839. Var *fogData = new Var( "fogData", "float3" );
  1840. fogData->uniform = true;
  1841. fogData->constSortPos = cspPass;
  1842. /// Get the fog amount.
  1843. fogAmount = new Var( "fogAmount", "float" );
  1844. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1845. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  1846. }
  1847. // Lerp between the fog color and diffuse color.
  1848. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  1849. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  1850. output = meta;
  1851. }
  1852. ShaderFeature::Resources FogFeatHLSL::getResources( const MaterialFeatureData &fd )
  1853. {
  1854. Resources res;
  1855. res.numTexReg = 1;
  1856. return res;
  1857. }
  1858. //****************************************************************************
  1859. // Visibility
  1860. //****************************************************************************
  1861. VisibilityFeatHLSL::VisibilityFeatHLSL()
  1862. : mTorqueDep( "shaders/common/torque.hlsl" )
  1863. {
  1864. addDependency( &mTorqueDep );
  1865. }
  1866. void VisibilityFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  1867. const MaterialFeatureData &fd )
  1868. {
  1869. MultiLine *meta = new MultiLine;
  1870. output = meta;
  1871. if ( fd.features[ MFT_UseInstancing ] )
  1872. {
  1873. // We pass the visibility to the pixel shader via
  1874. // another output register.
  1875. //
  1876. // TODO: We should see if we can share this register
  1877. // with some other common instanced data.
  1878. //
  1879. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1880. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  1881. outVisibility->setStructName( "OUT" );
  1882. outVisibility->setName( "visibility" );
  1883. outVisibility->setType( "float" );
  1884. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  1885. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  1886. instVisibility->setStructName( "IN" );
  1887. instVisibility->setName( "inst_visibility" );
  1888. instVisibility->setType( "float" );
  1889. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  1890. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  1891. }
  1892. if ( fd.features[ MFT_IsTranslucent ] )
  1893. return;
  1894. addOutVpos( meta, componentList );
  1895. }
  1896. void VisibilityFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1897. const MaterialFeatureData &fd )
  1898. {
  1899. // Get the visibility constant.
  1900. Var *visibility = NULL;
  1901. if ( fd.features[ MFT_UseInstancing ] )
  1902. visibility = getInTexCoord( "visibility", "float", false, componentList );
  1903. else
  1904. {
  1905. visibility = (Var*)LangElement::find( "visibility" );
  1906. if ( !visibility )
  1907. {
  1908. visibility = new Var();
  1909. visibility->setType( "float" );
  1910. visibility->setName( "visibility" );
  1911. visibility->uniform = true;
  1912. visibility->constSortPos = cspPotentialPrimitive;
  1913. }
  1914. }
  1915. MultiLine *meta = new MultiLine;
  1916. output = meta;
  1917. // Translucent objects do a simple alpha fade.
  1918. if ( fd.features[ MFT_IsTranslucent ] )
  1919. {
  1920. Var *color = (Var*)LangElement::find( "col" );
  1921. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  1922. return;
  1923. }
  1924. // Everything else does a fizzle.
  1925. Var *vPos = getInVpos( meta, componentList );
  1926. meta->addStatement( new GenOp( " fizzle( @, @ );\r\n", vPos, visibility ) );
  1927. }
  1928. ShaderFeature::Resources VisibilityFeatHLSL::getResources( const MaterialFeatureData &fd )
  1929. {
  1930. Resources res;
  1931. // TODO: Fix for instancing.
  1932. if ( !fd.features[ MFT_IsTranslucent ] )
  1933. res.numTexReg = 1;
  1934. return res;
  1935. }
  1936. //****************************************************************************
  1937. // AlphaTest
  1938. //****************************************************************************
  1939. void AlphaTestHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1940. const MaterialFeatureData &fd )
  1941. {
  1942. // If we're below SM3 and don't have a depth output
  1943. // feature then don't waste an instruction here.
  1944. if ( GFX->getPixelShaderVersion() < 3.0 &&
  1945. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  1946. !fd.features[ MFT_DepthOut ] )
  1947. {
  1948. output = NULL;
  1949. return;
  1950. }
  1951. // If we don't have a color var then we cannot do an alpha test.
  1952. Var *color = (Var*)LangElement::find( "col" );
  1953. if ( !color )
  1954. {
  1955. output = NULL;
  1956. return;
  1957. }
  1958. // Now grab the alpha test value.
  1959. Var *alphaTestVal = new Var;
  1960. alphaTestVal->setType( "float" );
  1961. alphaTestVal->setName( "alphaTestValue" );
  1962. alphaTestVal->uniform = true;
  1963. alphaTestVal->constSortPos = cspPotentialPrimitive;
  1964. // Do the clip.
  1965. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  1966. }
  1967. //****************************************************************************
  1968. // GlowMask
  1969. //****************************************************************************
  1970. void GlowMaskHLSL::processPix( Vector<ShaderComponent*> &componentList,
  1971. const MaterialFeatureData &fd )
  1972. {
  1973. output = NULL;
  1974. // Get the output color... and make it black to mask out
  1975. // glow passes rendered before us.
  1976. //
  1977. // The shader compiler will optimize out all the other
  1978. // code above that doesn't contribute to the alpha mask.
  1979. Var *color = (Var*)LangElement::find( "col" );
  1980. if ( color )
  1981. output = new GenOp( " @.rgb = 0;\r\n", color );
  1982. }
  1983. //****************************************************************************
  1984. // RenderTargetZero
  1985. //****************************************************************************
  1986. void RenderTargetZeroHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  1987. {
  1988. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  1989. // This will prevent a divide by zero causing an FP special on float render targets
  1990. output = new GenOp( " @;\r\n", assignColor( new GenOp( "0.00001" ), Material::None, NULL, mOutputTargetMask ) );
  1991. }
  1992. //****************************************************************************
  1993. // HDR Output
  1994. //****************************************************************************
  1995. HDROutHLSL::HDROutHLSL()
  1996. : mTorqueDep( "shaders/common/torque.hlsl" )
  1997. {
  1998. addDependency( &mTorqueDep );
  1999. }
  2000. void HDROutHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2001. const MaterialFeatureData &fd )
  2002. {
  2003. // Let the helper function do the work.
  2004. Var *color = (Var*)LangElement::find( "col" );
  2005. if ( color )
  2006. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2007. }
  2008. //****************************************************************************
  2009. // FoliageFeatureHLSL
  2010. //****************************************************************************
  2011. #include "T3D/fx/groundCover.h"
  2012. FoliageFeatureHLSL::FoliageFeatureHLSL()
  2013. : mDep( "shaders/common/foliage.hlsl" )
  2014. {
  2015. addDependency( &mDep );
  2016. }
  2017. void FoliageFeatureHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2018. const MaterialFeatureData &fd )
  2019. {
  2020. // Get the input variables we need.
  2021. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2022. if ( !inPosition )
  2023. inPosition = (Var*)LangElement::find( "position" );
  2024. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2025. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2026. MultiLine *meta = new MultiLine;
  2027. // Declare the normal and tangent variables since they do not exist
  2028. // in this vert type, but we do need to set them up for others.
  2029. Var *normal = (Var*)LangElement::find( "normal" );
  2030. AssertFatal( normal, "FoliageFeatureHLSL requires vert normal!" );
  2031. Var *tangent = new Var;
  2032. tangent->setType( "float3" );
  2033. tangent->setName( "T" );
  2034. LangElement *tangentDec = new DecOp( tangent );
  2035. meta->addStatement( new GenOp( " @;\n", tangentDec ) );
  2036. // We add a float foliageFade to the OUT structure.
  2037. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2038. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2039. fade->setName( "foliageFade" );
  2040. fade->setStructName( "OUT" );
  2041. fade->setType( "float" );
  2042. // grab the eye position
  2043. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2044. if ( !eyePos )
  2045. {
  2046. eyePos = new Var( "eyePosWorld", "float3" );
  2047. eyePos->uniform = true;
  2048. eyePos->constSortPos = cspPass;
  2049. }
  2050. // All actual work is offloaded to this method.
  2051. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2052. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2053. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2054. output = meta;
  2055. }
  2056. void FoliageFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2057. const MaterialFeatureData &fd )
  2058. {
  2059. // Find / create IN.foliageFade
  2060. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2061. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2062. fade->setName( "foliageFade" );
  2063. fade->setStructName( "IN" );
  2064. fade->setType( "float" );
  2065. // Find / create visibility
  2066. Var *visibility = (Var*) LangElement::find( "visibility" );
  2067. if ( !visibility )
  2068. {
  2069. visibility = new Var();
  2070. visibility->setType( "float" );
  2071. visibility->setName( "visibility" );
  2072. visibility->uniform = true;
  2073. visibility->constSortPos = cspPotentialPrimitive;
  2074. }
  2075. MultiLine *meta = new MultiLine;
  2076. // Multiply foliageFade into visibility.
  2077. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2078. output = meta;
  2079. }
  2080. void FoliageFeatureHLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2081. {
  2082. // This isn't really necessary since the outFeatureData will be filtered after
  2083. // this call.
  2084. if ( features.hasFeature( MFT_Foliage ) )
  2085. outFeatureData->features.addFeature( type );
  2086. }
  2087. ShaderFeatureConstHandles* FoliageFeatureHLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2088. {
  2089. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2090. AssertFatal( gcover != NULL, "FoliageFeatureHLSL::createConstHandles - userObject was not valid!" );
  2091. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2092. handles->mGroundCover = gcover;
  2093. handles->init( shader );
  2094. return handles;
  2095. }
  2096. void ParticleNormalFeatureHLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2097. {
  2098. MultiLine *meta = new MultiLine;
  2099. output = meta;
  2100. // Calculate normal and tangent values since we want to keep particle verts
  2101. // as light-weight as possible
  2102. Var *normal = (Var*) LangElement::find("normal");
  2103. if(normal == NULL)
  2104. {
  2105. normal = new Var;
  2106. normal->setType( "float3" );
  2107. normal->setName( "normal" );
  2108. // These values are not accidental. It is slightly adjusted from facing straight into the
  2109. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2110. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2111. // [11/23/2009 Pat]
  2112. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2113. }
  2114. Var *T = (Var*) LangElement::find( "T" );
  2115. if(T == NULL)
  2116. {
  2117. T = new Var;
  2118. T->setType( "float3" );
  2119. T->setName( "T" );
  2120. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2121. }
  2122. }
  2123. //****************************************************************************
  2124. // ImposterVertFeatureHLSL
  2125. //****************************************************************************
  2126. ImposterVertFeatureHLSL::ImposterVertFeatureHLSL()
  2127. : mDep( "shaders/common/imposter.hlsl" )
  2128. {
  2129. addDependency( &mDep );
  2130. }
  2131. void ImposterVertFeatureHLSL::processVert( Vector<ShaderComponent*> &componentList,
  2132. const MaterialFeatureData &fd )
  2133. {
  2134. MultiLine *meta = new MultiLine;
  2135. output = meta;
  2136. // Get the input vertex variables.
  2137. Var *inPosition = (Var*)LangElement::find( "position" );
  2138. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2139. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2140. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2141. // Get the input shader constants.
  2142. Var *imposterLimits = new Var;
  2143. imposterLimits->setType( "float4" );
  2144. imposterLimits->setName( "imposterLimits" );
  2145. imposterLimits->uniform = true;
  2146. imposterLimits->constSortPos = cspPotentialPrimitive;
  2147. Var *imposterUVs = new Var;
  2148. imposterUVs->setType( "float4" );
  2149. imposterUVs->setName( "imposterUVs" );
  2150. imposterUVs->arraySize = 64; // See imposter.hlsl
  2151. imposterUVs->uniform = true;
  2152. imposterUVs->constSortPos = cspPotentialPrimitive;
  2153. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2154. if ( !eyePos )
  2155. {
  2156. eyePos = new Var( "eyePosWorld", "float3" );
  2157. eyePos->uniform = true;
  2158. eyePos->constSortPos = cspPass;
  2159. }
  2160. // Declare the outputs from this feature.
  2161. Var *outInPosition = new Var;
  2162. outInPosition->setType( "float3" );
  2163. outInPosition->setName( "inPosition" );
  2164. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2165. Var *outTexCoord = new Var;
  2166. outTexCoord->setType( "float2" );
  2167. outTexCoord->setName( "texCoord" );
  2168. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2169. Var *outWorldToTangent = new Var;
  2170. outWorldToTangent->setType( "float3x3" );
  2171. outWorldToTangent->setName( "worldToTangent" );
  2172. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2173. // Add imposterFade to the OUT structure.
  2174. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2175. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2176. outFade->setName( "imposterFade" );
  2177. outFade->setStructName( "OUT" );
  2178. outFade->setType( "float" );
  2179. // Assign OUT.imposterFade
  2180. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2181. // All actual work is done in this method.
  2182. meta->addStatement( new GenOp( " imposter_v( @.xyz, @.w, @.x * length(@), normalize(@), normalize(@), @.y, @.x, @.z, @.w, @, @, @, @, @ );\r\n",
  2183. inPosition,
  2184. inPosition,
  2185. inMiscParams,
  2186. inRightVec,
  2187. inUpVec,
  2188. inRightVec,
  2189. imposterLimits,
  2190. imposterLimits,
  2191. imposterLimits,
  2192. imposterLimits,
  2193. eyePos,
  2194. imposterUVs,
  2195. outInPosition,
  2196. outTexCoord,
  2197. outWorldToTangent ) );
  2198. // Copy the position to wsPosition for use in shaders
  2199. // down stream instead of looking for objTrans.
  2200. Var *wsPosition = new Var;
  2201. wsPosition->setType( "float3" );
  2202. wsPosition->setName( "wsPosition" );
  2203. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2204. // If we new viewToTangent... its the same as the
  2205. // world to tangent for an imposter.
  2206. Var *viewToTangent = new Var;
  2207. viewToTangent->setType( "float3x3" );
  2208. viewToTangent->setName( "viewToTangent" );
  2209. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2210. }
  2211. void ImposterVertFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
  2212. const MaterialFeatureData &fd )
  2213. {
  2214. // Find / create IN.imposterFade
  2215. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2216. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2217. fade->setName( "imposterFade" );
  2218. fade->setStructName( "IN" );
  2219. fade->setType( "float" );
  2220. // Find / create visibility
  2221. Var *visibility = (Var*) LangElement::find( "visibility" );
  2222. if ( !visibility )
  2223. {
  2224. visibility = new Var();
  2225. visibility->setType( "float" );
  2226. visibility->setName( "visibility" );
  2227. visibility->uniform = true;
  2228. visibility->constSortPos = cspPotentialPrimitive;
  2229. }
  2230. MultiLine *meta = new MultiLine;
  2231. // Multiply foliageFade into visibility.
  2232. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2233. output = meta;
  2234. }
  2235. void ImposterVertFeatureHLSL::determineFeature( Material *material,
  2236. const GFXVertexFormat *vertexFormat,
  2237. U32 stageNum,
  2238. const FeatureType &type,
  2239. const FeatureSet &features,
  2240. MaterialFeatureData *outFeatureData )
  2241. {
  2242. if ( features.hasFeature( MFT_ImposterVert ) )
  2243. outFeatureData->features.addFeature( MFT_ImposterVert );
  2244. }