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