shaderFeatureHLSL.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. bool useFoliageTexCoord);
  98. ///
  99. Var* getObjTrans( Vector<ShaderComponent*> &componentList,
  100. bool useInstancing,
  101. MultiLine *meta );
  102. ///
  103. Var* getModelView( Vector<ShaderComponent*> &componentList,
  104. bool useInstancing,
  105. MultiLine *meta );
  106. ///
  107. Var* getWorldView( Vector<ShaderComponent*> &componentList,
  108. bool useInstancing,
  109. MultiLine *meta );
  110. ///
  111. Var* getInvWorldView( Vector<ShaderComponent*> &componentList,
  112. bool useInstancing,
  113. MultiLine *meta );
  114. Var* getSurface(Vector<ShaderComponent*>& componentList, MultiLine* meta, const MaterialFeatureData& fd);
  115. Var* getInWorldNormal(Vector<ShaderComponent*>& componentList);
  116. // ShaderFeature
  117. Var* getVertTexCoord( const String &name );
  118. LangElement* setupTexSpaceMat( Vector<ShaderComponent*> &componentList, Var **texSpaceMat );
  119. LangElement* assignColor( LangElement *elem, Material::BlendOp blend, LangElement *lerpElem = NULL, ShaderFeature::OutputTarget outputTarget = ShaderFeature::DefaultTarget );
  120. LangElement* expandNormalMap( LangElement *sampleNormalOp, LangElement *normalDecl, LangElement *normalVar, const MaterialFeatureData &fd );
  121. };
  122. class NamedFeatureHLSL : public ShaderFeatureHLSL
  123. {
  124. protected:
  125. String mName;
  126. public:
  127. NamedFeatureHLSL( const String &name )
  128. : mName( name )
  129. {}
  130. virtual String getName() { return mName; }
  131. };
  132. class RenderTargetZeroHLSL : public ShaderFeatureHLSL
  133. {
  134. protected:
  135. ShaderFeature::OutputTarget mOutputTargetMask;
  136. String mFeatureName;
  137. public:
  138. RenderTargetZeroHLSL( const ShaderFeature::OutputTarget target )
  139. : mOutputTargetMask( target )
  140. {
  141. char buffer[256];
  142. dSprintf(buffer, sizeof(buffer), "Render Target Output = 0.0, output mask %04b", mOutputTargetMask);
  143. mFeatureName = buffer;
  144. }
  145. virtual String getName() { return mFeatureName; }
  146. virtual void processPix( Vector<ShaderComponent*> &componentList,
  147. const MaterialFeatureData &fd );
  148. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const { return mOutputTargetMask; }
  149. };
  150. /// Vertex position
  151. class VertPositionHLSL : public ShaderFeatureHLSL
  152. {
  153. public:
  154. virtual void processVert( Vector<ShaderComponent*> &componentList,
  155. const MaterialFeatureData &fd );
  156. virtual void processPix( Vector<ShaderComponent*> &componentList,
  157. const MaterialFeatureData &fd);
  158. virtual String getName()
  159. {
  160. return "Vert Position";
  161. }
  162. virtual void determineFeature( Material *material,
  163. const GFXVertexFormat *vertexFormat,
  164. U32 stageNum,
  165. const FeatureType &type,
  166. const FeatureSet &features,
  167. MaterialFeatureData *outFeatureData );
  168. };
  169. /// Vertex lighting based on the normal and the light
  170. /// direction passed through the vertex color.
  171. class RTLightingFeatHLSL : public ShaderFeatureHLSL
  172. {
  173. protected:
  174. ShaderIncludeDependency mDep;
  175. public:
  176. RTLightingFeatHLSL();
  177. virtual void processVert( Vector<ShaderComponent*> &componentList,
  178. const MaterialFeatureData &fd );
  179. virtual void processPix( Vector<ShaderComponent*> &componentList,
  180. const MaterialFeatureData &fd );
  181. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  182. virtual Resources getResources( const MaterialFeatureData &fd );
  183. virtual String getName()
  184. {
  185. return "RT Lighting";
  186. }
  187. };
  188. /// Base texture
  189. class DiffuseMapFeatHLSL : public ShaderFeatureHLSL
  190. {
  191. protected:
  192. ShaderIncludeDependency mTorqueDep;
  193. public:
  194. DiffuseMapFeatHLSL();
  195. virtual void processVert( Vector<ShaderComponent*> &componentList,
  196. const MaterialFeatureData &fd );
  197. virtual void processPix( Vector<ShaderComponent*> &componentList,
  198. const MaterialFeatureData &fd );
  199. virtual U32 getOutputTargets(const MaterialFeatureData &fd) const;
  200. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  201. virtual Resources getResources( const MaterialFeatureData &fd );
  202. // Sets textures and texture flags for current pass
  203. virtual void setTexData( Material::StageData &stageDat,
  204. const MaterialFeatureData &fd,
  205. RenderPassData &passData,
  206. U32 &texIndex );
  207. virtual String getName()
  208. {
  209. return "Base Texture";
  210. }
  211. };
  212. /// Overlay texture
  213. class OverlayTexFeatHLSL : public ShaderFeatureHLSL
  214. {
  215. public:
  216. virtual void processVert( Vector<ShaderComponent*> &componentList,
  217. const MaterialFeatureData &fd );
  218. virtual void processPix( Vector<ShaderComponent*> &componentList,
  219. const MaterialFeatureData &fd );
  220. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  221. virtual Resources getResources( const MaterialFeatureData &fd );
  222. // Sets textures and texture flags for current pass
  223. virtual void setTexData( Material::StageData &stageDat,
  224. const MaterialFeatureData &fd,
  225. RenderPassData &passData,
  226. U32 &texIndex );
  227. virtual String getName()
  228. {
  229. return "Overlay Texture";
  230. }
  231. };
  232. /// Diffuse color
  233. class DiffuseFeatureHLSL : public ShaderFeatureHLSL
  234. {
  235. public:
  236. virtual void processPix( Vector<ShaderComponent*> &componentList,
  237. const MaterialFeatureData &fd );
  238. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  239. virtual U32 getOutputTargets(const MaterialFeatureData &fd) const;
  240. virtual String getName()
  241. {
  242. return "Diffuse Color";
  243. }
  244. };
  245. /// Diffuse vertex color
  246. class DiffuseVertColorFeatureHLSL : public ShaderFeatureHLSL
  247. {
  248. public:
  249. virtual void processVert( Vector< ShaderComponent* >& componentList,
  250. const MaterialFeatureData& fd );
  251. virtual void processPix( Vector< ShaderComponent* >&componentList,
  252. const MaterialFeatureData& fd );
  253. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  254. virtual String getName()
  255. {
  256. return "Diffuse Vertex Color";
  257. }
  258. };
  259. /// Lightmap
  260. class LightmapFeatHLSL : public ShaderFeatureHLSL
  261. {
  262. public:
  263. virtual void processVert( Vector<ShaderComponent*> &componentList,
  264. const MaterialFeatureData &fd );
  265. virtual void processPix( Vector<ShaderComponent*> &componentList,
  266. const MaterialFeatureData &fd );
  267. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  268. virtual Resources getResources( const MaterialFeatureData &fd );
  269. // Sets textures and texture flags for current pass
  270. virtual void setTexData( Material::StageData &stageDat,
  271. const MaterialFeatureData &fd,
  272. RenderPassData &passData,
  273. U32 &texIndex );
  274. virtual String getName()
  275. {
  276. return "Lightmap";
  277. }
  278. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
  279. };
  280. /// Tonemap
  281. class TonemapFeatHLSL : public ShaderFeatureHLSL
  282. {
  283. public:
  284. virtual void processVert( Vector<ShaderComponent*> &componentList,
  285. const MaterialFeatureData &fd );
  286. virtual void processPix( Vector<ShaderComponent*> &componentList,
  287. const MaterialFeatureData &fd );
  288. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  289. virtual Resources getResources( const MaterialFeatureData &fd );
  290. // Sets textures and texture flags for current pass
  291. virtual void setTexData( Material::StageData &stageDat,
  292. const MaterialFeatureData &fd,
  293. RenderPassData &passData,
  294. U32 &texIndex );
  295. virtual String getName()
  296. {
  297. return "Tonemap";
  298. }
  299. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
  300. };
  301. /// Baked lighting stored on the vertex color
  302. class VertLitHLSL : public ShaderFeatureHLSL
  303. {
  304. public:
  305. virtual void processVert( Vector<ShaderComponent*> &componentList,
  306. const MaterialFeatureData &fd );
  307. virtual void processPix( Vector<ShaderComponent*> &componentList,
  308. const MaterialFeatureData &fd );
  309. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  310. virtual String getName()
  311. {
  312. return "Vert Lit";
  313. }
  314. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
  315. };
  316. /// Detail map
  317. class DetailFeatHLSL : public ShaderFeatureHLSL
  318. {
  319. public:
  320. virtual void processVert( Vector<ShaderComponent*> &componentList,
  321. const MaterialFeatureData &fd );
  322. virtual void processPix( Vector<ShaderComponent*> &componentList,
  323. const MaterialFeatureData &fd );
  324. virtual Resources getResources( const MaterialFeatureData &fd );
  325. virtual Material::BlendOp getBlendOp(){ return Material::Mul; }
  326. // Sets textures and texture flags for current pass
  327. virtual void setTexData( Material::StageData &stageDat,
  328. const MaterialFeatureData &fd,
  329. RenderPassData &passData,
  330. U32 &texIndex );
  331. virtual String getName()
  332. {
  333. return "Detail";
  334. }
  335. };
  336. /// Reflect Cubemap
  337. class ReflectCubeFeatHLSL : public ShaderFeatureHLSL
  338. {
  339. public:
  340. virtual void processVert( Vector<ShaderComponent*> &componentList,
  341. const MaterialFeatureData &fd );
  342. virtual void processPix( Vector<ShaderComponent*> &componentList,
  343. const MaterialFeatureData &fd );
  344. virtual Resources getResources( const MaterialFeatureData &fd );
  345. // Sets textures and texture flags for current pass
  346. virtual void setTexData( Material::StageData &stageDat,
  347. const MaterialFeatureData &fd,
  348. RenderPassData &passData,
  349. U32 &texIndex );
  350. virtual String getName()
  351. {
  352. return "Reflect Cube";
  353. }
  354. };
  355. /// Fog
  356. class FogFeatHLSL : public ShaderFeatureHLSL
  357. {
  358. protected:
  359. ShaderIncludeDependency mFogDep;
  360. public:
  361. FogFeatHLSL();
  362. virtual void processVert( Vector<ShaderComponent*> &componentList,
  363. const MaterialFeatureData &fd );
  364. virtual void processPix( Vector<ShaderComponent*> &componentList,
  365. const MaterialFeatureData &fd );
  366. virtual Resources getResources( const MaterialFeatureData &fd );
  367. virtual Material::BlendOp getBlendOp() { return Material::LerpAlpha; }
  368. virtual String getName()
  369. {
  370. return "Fog";
  371. }
  372. };
  373. /// Tex Anim
  374. class TexAnimHLSL : public ShaderFeatureHLSL
  375. {
  376. public:
  377. virtual Material::BlendOp getBlendOp() { return Material::None; }
  378. virtual String getName()
  379. {
  380. return "Texture Animation";
  381. }
  382. };
  383. /// Visibility
  384. class VisibilityFeatHLSL : public ShaderFeatureHLSL
  385. {
  386. protected:
  387. ShaderIncludeDependency mTorqueDep;
  388. public:
  389. VisibilityFeatHLSL();
  390. virtual void processVert( Vector<ShaderComponent*> &componentList,
  391. const MaterialFeatureData &fd );
  392. virtual void processPix( Vector<ShaderComponent*> &componentList,
  393. const MaterialFeatureData &fd );
  394. virtual Resources getResources( const MaterialFeatureData &fd );
  395. virtual Material::BlendOp getBlendOp() { return Material::None; }
  396. virtual String getName()
  397. {
  398. return "Visibility";
  399. }
  400. };
  401. ///
  402. class AlphaTestHLSL : public ShaderFeatureHLSL
  403. {
  404. public:
  405. virtual void processPix( Vector<ShaderComponent*> &componentList,
  406. const MaterialFeatureData &fd );
  407. virtual Material::BlendOp getBlendOp() { return Material::None; }
  408. virtual String getName()
  409. {
  410. return "Alpha Test";
  411. }
  412. };
  413. /// Special feature used to mask out the RGB color for
  414. /// non-glow passes of glow materials.
  415. /// @see RenderGlowMgr
  416. class GlowMaskHLSL : public ShaderFeatureHLSL
  417. {
  418. public:
  419. virtual void processPix( Vector<ShaderComponent*> &componentList,
  420. const MaterialFeatureData &fd );
  421. virtual Material::BlendOp getBlendOp() { return Material::None; }
  422. virtual String getName()
  423. {
  424. return "Glow Mask";
  425. }
  426. };
  427. /// This should be the final feature on most pixel shaders which
  428. /// encodes the color for the current HDR target format.
  429. /// @see HDRPostFx
  430. /// @see LightManager
  431. /// @see torque.hlsl
  432. class HDROutHLSL : public ShaderFeatureHLSL
  433. {
  434. protected:
  435. ShaderIncludeDependency mTorqueDep;
  436. public:
  437. HDROutHLSL();
  438. virtual void processPix( Vector<ShaderComponent*> &componentList,
  439. const MaterialFeatureData &fd );
  440. virtual Material::BlendOp getBlendOp() { return Material::None; }
  441. virtual String getName() { return "HDR Output"; }
  442. };
  443. ///
  444. class FoliageFeatureHLSL : public ShaderFeatureHLSL
  445. {
  446. protected:
  447. ShaderIncludeDependency mDep;
  448. public:
  449. FoliageFeatureHLSL();
  450. virtual void processVert( Vector<ShaderComponent*> &componentList,
  451. const MaterialFeatureData &fd );
  452. virtual void processPix( Vector<ShaderComponent*> &componentList,
  453. const MaterialFeatureData &fd );
  454. virtual String getName()
  455. {
  456. return "Foliage Feature";
  457. }
  458. virtual void determineFeature( Material *material,
  459. const GFXVertexFormat *vertexFormat,
  460. U32 stageNum,
  461. const FeatureType &type,
  462. const FeatureSet &features,
  463. MaterialFeatureData *outFeatureData );
  464. virtual ShaderFeatureConstHandles* createConstHandles( GFXShader *shader, SimObject *userObject );
  465. };
  466. class ParticleNormalFeatureHLSL : public ShaderFeatureHLSL
  467. {
  468. public:
  469. virtual void processVert( Vector<ShaderComponent*> &componentList,
  470. const MaterialFeatureData &fd );
  471. virtual String getName()
  472. {
  473. return "Particle Normal Generation Feature";
  474. }
  475. };
  476. /// Special feature for unpacking imposter verts.
  477. /// @see RenderImposterMgr
  478. class ImposterVertFeatureHLSL : public ShaderFeatureHLSL
  479. {
  480. protected:
  481. ShaderIncludeDependency mDep;
  482. public:
  483. ImposterVertFeatureHLSL();
  484. virtual void processVert( Vector<ShaderComponent*> &componentList,
  485. const MaterialFeatureData &fd );
  486. virtual void processPix( Vector<ShaderComponent*> &componentList,
  487. const MaterialFeatureData &fd );
  488. virtual String getName() { return "Imposter Vert"; }
  489. virtual void determineFeature( Material *material,
  490. const GFXVertexFormat *vertexFormat,
  491. U32 stageNum,
  492. const FeatureType &type,
  493. const FeatureSet &features,
  494. MaterialFeatureData *outFeatureData );
  495. };
  496. /// Hardware Skinning
  497. class HardwareSkinningFeatureHLSL : public ShaderFeatureHLSL
  498. {
  499. protected:
  500. public:
  501. virtual void processVert( Vector<ShaderComponent*> &componentList,
  502. const MaterialFeatureData &fd );
  503. virtual String getName() { return "Hardware Skinning"; }
  504. };
  505. /// Reflection Probes
  506. class ReflectionProbeFeatHLSL : public ShaderFeatureHLSL
  507. {
  508. protected:
  509. ShaderIncludeDependency mDep;
  510. public:
  511. ReflectionProbeFeatHLSL();
  512. virtual void processVert(Vector<ShaderComponent*>& componentList,
  513. const MaterialFeatureData& fd);
  514. virtual void processPix(Vector<ShaderComponent*> &componentList,
  515. const MaterialFeatureData &fd);
  516. virtual Resources getResources(const MaterialFeatureData &fd);
  517. // Sets textures and texture flags for current pass
  518. virtual void setTexData(Material::StageData &stageDat,
  519. const MaterialFeatureData &fd,
  520. RenderPassData &passData,
  521. U32 &texIndex);
  522. virtual String getName()
  523. {
  524. return "Reflection Probes";
  525. }
  526. };
  527. #endif // _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_