shaderFeatureHLSL.h 21 KB

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