shaderFeatureHLSL.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. #ifndef _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
  23. #define _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
  24. #ifndef _SHADERFEATURE_H_
  25. #include "shaderGen/shaderFeature.h"
  26. #endif
  27. struct LangElement;
  28. struct MaterialFeatureData;
  29. struct RenderPassData;
  30. class ShaderFeatureHLSL : public ShaderFeature
  31. {
  32. public:
  33. ShaderFeatureHLSL();
  34. ///
  35. Var* getOutTexCoord( const char *name,
  36. const char *type,
  37. bool mapsToSampler,
  38. bool useTexAnim,
  39. MultiLine *meta,
  40. Vector<ShaderComponent*> &componentList );
  41. /// Returns an input texture coord by name adding it
  42. /// to the input connector if it doesn't exist.
  43. static Var* getInTexCoord( const char *name,
  44. const char *type,
  45. bool mapsToSampler,
  46. Vector<ShaderComponent*> &componentList );
  47. static Var* getInColor( const char *name,
  48. const char *type,
  49. Vector<ShaderComponent*> &componentList );
  50. ///
  51. static Var* addOutVpos( MultiLine *meta,
  52. Vector<ShaderComponent*> &componentList );
  53. /// Returns the VPOS input register for the pixel shader.
  54. static Var* getInVpos( MultiLine *meta,
  55. Vector<ShaderComponent*> &componentList );
  56. /// Returns the "objToTangentSpace" transform or creates one if this
  57. /// is the first feature to need it.
  58. Var* getOutObjToTangentSpace( Vector<ShaderComponent*> &componentList,
  59. MultiLine *meta,
  60. const MaterialFeatureData &fd );
  61. /// Returns the existing output "outWorldToTangent" transform or
  62. /// creates one if this is the first feature to need it.
  63. Var* getOutWorldToTangent( Vector<ShaderComponent*> &componentList,
  64. MultiLine *meta,
  65. const MaterialFeatureData &fd );
  66. /// Returns the input "worldToTanget" space transform
  67. /// adding it to the input connector if it doesn't exist.
  68. static Var* getInWorldToTangent( Vector<ShaderComponent*> &componentList );
  69. /// Returns the existing output "outViewToTangent" transform or
  70. /// creates one if this is the first feature to need it.
  71. Var* getOutViewToTangent( Vector<ShaderComponent*> &componentList,
  72. MultiLine *meta,
  73. const MaterialFeatureData &fd );
  74. /// Returns the input "viewToTangent" space transform
  75. /// adding it to the input connector if it doesn't exist.
  76. static Var* getInViewToTangent( Vector<ShaderComponent*> &componentList );
  77. /// Calculates the world space position in the vertex shader and
  78. /// assigns it to the passed language element. It does not pass
  79. /// it across the connector to the pixel shader.
  80. /// @see addOutWsPosition
  81. void getWsPosition( Vector<ShaderComponent*> &componentList,
  82. bool useInstancing,
  83. MultiLine *meta,
  84. LangElement *wsPosition );
  85. /// Adds the "wsPosition" to the input connector if it doesn't exist.
  86. Var* addOutWsPosition( Vector<ShaderComponent*> &componentList,
  87. bool useInstancing,
  88. MultiLine *meta );
  89. /// Returns the input world space position from the connector.
  90. static Var* getInWsPosition( Vector<ShaderComponent*> &componentList );
  91. /// Returns the world space view vector from the wsPosition.
  92. static Var* getWsView( Var *wsPosition, MultiLine *meta );
  93. /// Returns the input normal map texture.
  94. static Var* getNormalMapTex();
  95. ///
  96. Var* addOutDetailTexCoord( Vector<ShaderComponent*> &componentList,
  97. MultiLine *meta,
  98. bool useTexAnim );
  99. ///
  100. Var* getObjTrans( Vector<ShaderComponent*> &componentList,
  101. bool useInstancing,
  102. MultiLine *meta );
  103. ///
  104. Var* getModelView( Vector<ShaderComponent*> &componentList,
  105. bool useInstancing,
  106. MultiLine *meta );
  107. ///
  108. Var* getWorldView( Vector<ShaderComponent*> &componentList,
  109. bool useInstancing,
  110. MultiLine *meta );
  111. ///
  112. Var* getInvWorldView( Vector<ShaderComponent*> &componentList,
  113. bool useInstancing,
  114. MultiLine *meta );
  115. // ShaderFeature
  116. Var* getVertTexCoord( const String &name );
  117. LangElement* setupTexSpaceMat( Vector<ShaderComponent*> &componentList, Var **texSpaceMat );
  118. LangElement* assignColor( LangElement *elem, Material::BlendOp blend, LangElement *lerpElem = NULL, ShaderFeature::OutputTarget outputTarget = ShaderFeature::DefaultTarget );
  119. LangElement* expandNormalMap( LangElement *sampleNormalOp, LangElement *normalDecl, LangElement *normalVar, const MaterialFeatureData &fd );
  120. };
  121. class NamedFeatureHLSL : public ShaderFeatureHLSL
  122. {
  123. protected:
  124. String mName;
  125. public:
  126. NamedFeatureHLSL( const String &name )
  127. : mName( name )
  128. {}
  129. virtual String getName() { return mName; }
  130. };
  131. class RenderTargetZeroHLSL : public ShaderFeatureHLSL
  132. {
  133. protected:
  134. ShaderFeature::OutputTarget mOutputTargetMask;
  135. String mFeatureName;
  136. public:
  137. RenderTargetZeroHLSL( const ShaderFeature::OutputTarget target )
  138. : mOutputTargetMask( target )
  139. {
  140. char buffer[256];
  141. dSprintf(buffer, sizeof(buffer), "Render Target Output = 0.0, output mask %04b", mOutputTargetMask);
  142. mFeatureName = buffer;
  143. }
  144. virtual String getName() { return mFeatureName; }
  145. virtual void processPix( Vector<ShaderComponent*> &componentList,
  146. const MaterialFeatureData &fd );
  147. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const { return mOutputTargetMask; }
  148. };
  149. /// Vertex position
  150. class VertPositionHLSL : public ShaderFeatureHLSL
  151. {
  152. public:
  153. virtual void processVert( Vector<ShaderComponent*> &componentList,
  154. const MaterialFeatureData &fd );
  155. virtual String getName()
  156. {
  157. return "Vert Position";
  158. }
  159. virtual void determineFeature( Material *material,
  160. const GFXVertexFormat *vertexFormat,
  161. U32 stageNum,
  162. const FeatureType &type,
  163. const FeatureSet &features,
  164. MaterialFeatureData *outFeatureData );
  165. };
  166. /// Vertex lighting based on the normal and the light
  167. /// direction passed through the vertex color.
  168. class RTLightingFeatHLSL : public ShaderFeatureHLSL
  169. {
  170. protected:
  171. ShaderIncludeDependency mDep;
  172. public:
  173. RTLightingFeatHLSL();
  174. virtual void processVert( Vector<ShaderComponent*> &componentList,
  175. const MaterialFeatureData &fd );
  176. virtual void processPix( Vector<ShaderComponent*> &componentList,
  177. const MaterialFeatureData &fd );
  178. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  179. virtual Resources getResources( const MaterialFeatureData &fd );
  180. virtual String getName()
  181. {
  182. return "RT Lighting";
  183. }
  184. };
  185. /// Base texture
  186. class DiffuseMapFeatHLSL : public ShaderFeatureHLSL
  187. {
  188. public:
  189. virtual void processVert( Vector<ShaderComponent*> &componentList,
  190. const MaterialFeatureData &fd );
  191. virtual void processPix( Vector<ShaderComponent*> &componentList,
  192. const MaterialFeatureData &fd );
  193. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  194. virtual Resources getResources( const MaterialFeatureData &fd );
  195. // Sets textures and texture flags for current pass
  196. virtual void setTexData( Material::StageData &stageDat,
  197. const MaterialFeatureData &fd,
  198. RenderPassData &passData,
  199. U32 &texIndex );
  200. virtual String getName()
  201. {
  202. return "Base Texture";
  203. }
  204. };
  205. /// Overlay texture
  206. class OverlayTexFeatHLSL : public ShaderFeatureHLSL
  207. {
  208. public:
  209. virtual void processVert( Vector<ShaderComponent*> &componentList,
  210. const MaterialFeatureData &fd );
  211. virtual void processPix( Vector<ShaderComponent*> &componentList,
  212. const MaterialFeatureData &fd );
  213. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  214. virtual Resources getResources( const MaterialFeatureData &fd );
  215. // Sets textures and texture flags for current pass
  216. virtual void setTexData( Material::StageData &stageDat,
  217. const MaterialFeatureData &fd,
  218. RenderPassData &passData,
  219. U32 &texIndex );
  220. virtual String getName()
  221. {
  222. return "Overlay Texture";
  223. }
  224. };
  225. /// Diffuse color
  226. class DiffuseFeatureHLSL : public ShaderFeatureHLSL
  227. {
  228. public:
  229. virtual void processPix( Vector<ShaderComponent*> &componentList,
  230. const MaterialFeatureData &fd );
  231. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  232. virtual String getName()
  233. {
  234. return "Diffuse Color";
  235. }
  236. };
  237. /// Diffuse vertex color
  238. class DiffuseVertColorFeatureHLSL : public ShaderFeatureHLSL
  239. {
  240. public:
  241. virtual void processVert( Vector< ShaderComponent* >& componentList,
  242. const MaterialFeatureData& fd );
  243. virtual void processPix( Vector< ShaderComponent* >&componentList,
  244. const MaterialFeatureData& fd );
  245. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  246. virtual String getName()
  247. {
  248. return "Diffuse Vertex Color";
  249. }
  250. };
  251. /// Lightmap
  252. class LightmapFeatHLSL : public ShaderFeatureHLSL
  253. {
  254. public:
  255. virtual void processVert( Vector<ShaderComponent*> &componentList,
  256. const MaterialFeatureData &fd );
  257. virtual void processPix( Vector<ShaderComponent*> &componentList,
  258. const MaterialFeatureData &fd );
  259. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  260. virtual Resources getResources( const MaterialFeatureData &fd );
  261. // Sets textures and texture flags for current pass
  262. virtual void setTexData( Material::StageData &stageDat,
  263. const MaterialFeatureData &fd,
  264. RenderPassData &passData,
  265. U32 &texIndex );
  266. virtual String getName()
  267. {
  268. return "Lightmap";
  269. }
  270. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
  271. };
  272. /// Tonemap
  273. class TonemapFeatHLSL : public ShaderFeatureHLSL
  274. {
  275. public:
  276. virtual void processVert( Vector<ShaderComponent*> &componentList,
  277. const MaterialFeatureData &fd );
  278. virtual void processPix( Vector<ShaderComponent*> &componentList,
  279. const MaterialFeatureData &fd );
  280. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  281. virtual Resources getResources( const MaterialFeatureData &fd );
  282. // Sets textures and texture flags for current pass
  283. virtual void setTexData( Material::StageData &stageDat,
  284. const MaterialFeatureData &fd,
  285. RenderPassData &passData,
  286. U32 &texIndex );
  287. virtual String getName()
  288. {
  289. return "Tonemap";
  290. }
  291. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
  292. };
  293. /// Baked lighting stored on the vertex color
  294. class VertLitHLSL : public ShaderFeatureHLSL
  295. {
  296. public:
  297. virtual void processVert( Vector<ShaderComponent*> &componentList,
  298. const MaterialFeatureData &fd );
  299. virtual void processPix( Vector<ShaderComponent*> &componentList,
  300. const MaterialFeatureData &fd );
  301. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  302. virtual String getName()
  303. {
  304. return "Vert Lit";
  305. }
  306. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
  307. };
  308. /// Detail map
  309. class DetailFeatHLSL : public ShaderFeatureHLSL
  310. {
  311. public:
  312. virtual void processVert( Vector<ShaderComponent*> &componentList,
  313. const MaterialFeatureData &fd );
  314. virtual void processPix( Vector<ShaderComponent*> &componentList,
  315. const MaterialFeatureData &fd );
  316. virtual Resources getResources( const MaterialFeatureData &fd );
  317. virtual Material::BlendOp getBlendOp(){ return Material::Mul; }
  318. // Sets textures and texture flags for current pass
  319. virtual void setTexData( Material::StageData &stageDat,
  320. const MaterialFeatureData &fd,
  321. RenderPassData &passData,
  322. U32 &texIndex );
  323. virtual String getName()
  324. {
  325. return "Detail";
  326. }
  327. };
  328. /// Reflect Cubemap
  329. class ReflectCubeFeatHLSL : public ShaderFeatureHLSL
  330. {
  331. public:
  332. virtual void processVert( Vector<ShaderComponent*> &componentList,
  333. const MaterialFeatureData &fd );
  334. virtual void processPix( Vector<ShaderComponent*> &componentList,
  335. const MaterialFeatureData &fd );
  336. virtual Resources getResources( const MaterialFeatureData &fd );
  337. // Sets textures and texture flags for current pass
  338. virtual void setTexData( Material::StageData &stageDat,
  339. const MaterialFeatureData &fd,
  340. RenderPassData &passData,
  341. U32 &texIndex );
  342. virtual String getName()
  343. {
  344. return "Reflect Cube";
  345. }
  346. };
  347. /// Fog
  348. class FogFeatHLSL : public ShaderFeatureHLSL
  349. {
  350. protected:
  351. ShaderIncludeDependency mFogDep;
  352. public:
  353. FogFeatHLSL();
  354. virtual void processVert( Vector<ShaderComponent*> &componentList,
  355. const MaterialFeatureData &fd );
  356. virtual void processPix( Vector<ShaderComponent*> &componentList,
  357. const MaterialFeatureData &fd );
  358. virtual Resources getResources( const MaterialFeatureData &fd );
  359. virtual Material::BlendOp getBlendOp() { return Material::LerpAlpha; }
  360. virtual String getName()
  361. {
  362. return "Fog";
  363. }
  364. };
  365. /// Tex Anim
  366. class TexAnimHLSL : public ShaderFeatureHLSL
  367. {
  368. public:
  369. virtual Material::BlendOp getBlendOp() { return Material::None; }
  370. virtual String getName()
  371. {
  372. return "Texture Animation";
  373. }
  374. };
  375. /// Visibility
  376. class VisibilityFeatHLSL : public ShaderFeatureHLSL
  377. {
  378. protected:
  379. ShaderIncludeDependency mTorqueDep;
  380. public:
  381. VisibilityFeatHLSL();
  382. virtual void processVert( Vector<ShaderComponent*> &componentList,
  383. const MaterialFeatureData &fd );
  384. virtual void processPix( Vector<ShaderComponent*> &componentList,
  385. const MaterialFeatureData &fd );
  386. virtual Resources getResources( const MaterialFeatureData &fd );
  387. virtual Material::BlendOp getBlendOp() { return Material::None; }
  388. virtual String getName()
  389. {
  390. return "Visibility";
  391. }
  392. };
  393. ///
  394. class AlphaTestHLSL : public ShaderFeatureHLSL
  395. {
  396. public:
  397. virtual void processPix( Vector<ShaderComponent*> &componentList,
  398. const MaterialFeatureData &fd );
  399. virtual Material::BlendOp getBlendOp() { return Material::None; }
  400. virtual String getName()
  401. {
  402. return "Alpha Test";
  403. }
  404. };
  405. /// Special feature used to mask out the RGB color for
  406. /// non-glow passes of glow materials.
  407. /// @see RenderGlowMgr
  408. class GlowMaskHLSL : public ShaderFeatureHLSL
  409. {
  410. public:
  411. virtual void processPix( Vector<ShaderComponent*> &componentList,
  412. const MaterialFeatureData &fd );
  413. virtual Material::BlendOp getBlendOp() { return Material::None; }
  414. virtual String getName()
  415. {
  416. return "Glow Mask";
  417. }
  418. };
  419. /// This should be the final feature on most pixel shaders which
  420. /// encodes the color for the current HDR target format.
  421. /// @see HDRPostFx
  422. /// @see LightManager
  423. /// @see torque.hlsl
  424. class HDROutHLSL : public ShaderFeatureHLSL
  425. {
  426. protected:
  427. ShaderIncludeDependency mTorqueDep;
  428. public:
  429. HDROutHLSL();
  430. virtual void processPix( Vector<ShaderComponent*> &componentList,
  431. const MaterialFeatureData &fd );
  432. virtual Material::BlendOp getBlendOp() { return Material::None; }
  433. virtual String getName() { return "HDR Output"; }
  434. };
  435. ///
  436. class FoliageFeatureHLSL : public ShaderFeatureHLSL
  437. {
  438. protected:
  439. ShaderIncludeDependency mDep;
  440. public:
  441. FoliageFeatureHLSL();
  442. virtual void processVert( Vector<ShaderComponent*> &componentList,
  443. const MaterialFeatureData &fd );
  444. virtual void processPix( Vector<ShaderComponent*> &componentList,
  445. const MaterialFeatureData &fd );
  446. virtual String getName()
  447. {
  448. return "Foliage Feature";
  449. }
  450. virtual void determineFeature( Material *material,
  451. const GFXVertexFormat *vertexFormat,
  452. U32 stageNum,
  453. const FeatureType &type,
  454. const FeatureSet &features,
  455. MaterialFeatureData *outFeatureData );
  456. virtual ShaderFeatureConstHandles* createConstHandles( GFXShader *shader, SimObject *userObject );
  457. };
  458. class ParticleNormalFeatureHLSL : public ShaderFeatureHLSL
  459. {
  460. public:
  461. virtual void processVert( Vector<ShaderComponent*> &componentList,
  462. const MaterialFeatureData &fd );
  463. virtual String getName()
  464. {
  465. return "Particle Normal Generation Feature";
  466. }
  467. };
  468. /// Special feature for unpacking imposter verts.
  469. /// @see RenderImposterMgr
  470. class ImposterVertFeatureHLSL : public ShaderFeatureHLSL
  471. {
  472. protected:
  473. ShaderIncludeDependency mDep;
  474. public:
  475. ImposterVertFeatureHLSL();
  476. virtual void processVert( Vector<ShaderComponent*> &componentList,
  477. const MaterialFeatureData &fd );
  478. virtual void processPix( Vector<ShaderComponent*> &componentList,
  479. const MaterialFeatureData &fd );
  480. virtual String getName() { return "Imposter Vert"; }
  481. virtual void determineFeature( Material *material,
  482. const GFXVertexFormat *vertexFormat,
  483. U32 stageNum,
  484. const FeatureType &type,
  485. const FeatureSet &features,
  486. MaterialFeatureData *outFeatureData );
  487. };
  488. #endif // _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_