shaderFeatureGLSL.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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_GLSL_SHADERFEATUREGLSL_H_
  23. #define _SHADERGEN_GLSL_SHADERFEATUREGLSL_H_
  24. #ifndef _SHADERFEATURE_H_
  25. #include "shaderGen/shaderFeature.h"
  26. #endif
  27. struct LangElement;
  28. struct MaterialFeatureData;
  29. struct RenderPassData;
  30. class ShaderFeatureGLSL : public ShaderFeature
  31. {
  32. public:
  33. ShaderFeatureGLSL();
  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 NamedFeatureGLSL : public ShaderFeatureGLSL
  120. {
  121. protected:
  122. String mName;
  123. public:
  124. NamedFeatureGLSL( const String &name )
  125. : mName( name )
  126. {}
  127. virtual String getName() { return mName; }
  128. };
  129. class RenderTargetZeroGLSL : public ShaderFeatureGLSL
  130. {
  131. protected:
  132. ShaderFeature::OutputTarget mOutputTargetMask;
  133. String mFeatureName;
  134. public:
  135. RenderTargetZeroGLSL( 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 VertPositionGLSL : public ShaderFeatureGLSL
  149. {
  150. public:
  151. virtual void processVert( Vector<ShaderComponent*> &componentList,
  152. const MaterialFeatureData &fd );
  153. virtual String getName()
  154. {
  155. return "Vert Position";
  156. }
  157. virtual void determineFeature( Material *material,
  158. const GFXVertexFormat *vertexFormat,
  159. U32 stageNum,
  160. const FeatureType &type,
  161. const FeatureSet &features,
  162. MaterialFeatureData *outFeatureData );
  163. };
  164. /// Vertex lighting based on the normal and the light
  165. /// direction passed through the vertex color.
  166. class RTLightingFeatGLSL : public ShaderFeatureGLSL
  167. {
  168. protected:
  169. ShaderIncludeDependency mDep;
  170. public:
  171. RTLightingFeatGLSL();
  172. virtual void processVert( Vector<ShaderComponent*> &componentList,
  173. const MaterialFeatureData &fd );
  174. virtual void processPix( Vector<ShaderComponent*> &componentList,
  175. const MaterialFeatureData &fd );
  176. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  177. virtual Resources getResources( const MaterialFeatureData &fd );
  178. virtual String getName()
  179. {
  180. return "RT Lighting";
  181. }
  182. };
  183. /// Base texture
  184. class DiffuseMapFeatGLSL : public ShaderFeatureGLSL
  185. {
  186. protected:
  187. ShaderIncludeDependency mTorqueDep;
  188. public:
  189. DiffuseMapFeatGLSL();
  190. virtual void processVert( Vector<ShaderComponent*> &componentList,
  191. const MaterialFeatureData &fd );
  192. virtual void processPix( Vector<ShaderComponent*> &componentList,
  193. const MaterialFeatureData &fd );
  194. virtual U32 getOutputTargets(const MaterialFeatureData &fd) const;
  195. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  196. virtual Resources getResources( const MaterialFeatureData &fd );
  197. // Sets textures and texture flags for current pass
  198. virtual void setTexData( Material::StageData &stageDat,
  199. const MaterialFeatureData &fd,
  200. RenderPassData &passData,
  201. U32 &texIndex );
  202. virtual String getName()
  203. {
  204. return "Base Texture";
  205. }
  206. };
  207. /// Overlay texture
  208. class OverlayTexFeatGLSL : public ShaderFeatureGLSL
  209. {
  210. public:
  211. virtual void processVert( Vector<ShaderComponent*> &componentList,
  212. const MaterialFeatureData &fd );
  213. virtual void processPix( Vector<ShaderComponent*> &componentList,
  214. const MaterialFeatureData &fd );
  215. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  216. virtual Resources getResources( const MaterialFeatureData &fd );
  217. // Sets textures and texture flags for current pass
  218. virtual void setTexData( Material::StageData &stageDat,
  219. const MaterialFeatureData &fd,
  220. RenderPassData &passData,
  221. U32 &texIndex );
  222. virtual String getName()
  223. {
  224. return "Overlay Texture";
  225. }
  226. };
  227. /// Diffuse color
  228. class DiffuseFeatureGLSL : public ShaderFeatureGLSL
  229. {
  230. public:
  231. virtual void processPix( Vector<ShaderComponent*> &componentList,
  232. const MaterialFeatureData &fd );
  233. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  234. virtual U32 getOutputTargets(const MaterialFeatureData &fd) const;
  235. virtual String getName()
  236. {
  237. return "Diffuse Color";
  238. }
  239. };
  240. /// Diffuse vertex color
  241. class DiffuseVertColorFeatureGLSL : public ShaderFeatureGLSL
  242. {
  243. public:
  244. virtual void processVert( Vector< ShaderComponent* >& componentList,
  245. const MaterialFeatureData& fd );
  246. virtual void processPix( Vector< ShaderComponent* >&componentList,
  247. const MaterialFeatureData& fd );
  248. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  249. virtual String getName()
  250. {
  251. return "Diffuse Vertex Color";
  252. }
  253. };
  254. /// Lightmap
  255. class LightmapFeatGLSL : public ShaderFeatureGLSL
  256. {
  257. public:
  258. virtual void processVert( Vector<ShaderComponent*> &componentList,
  259. const MaterialFeatureData &fd );
  260. virtual void processPix( Vector<ShaderComponent*> &componentList,
  261. const MaterialFeatureData &fd );
  262. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  263. virtual Resources getResources( const MaterialFeatureData &fd );
  264. // Sets textures and texture flags for current pass
  265. virtual void setTexData( Material::StageData &stageDat,
  266. const MaterialFeatureData &fd,
  267. RenderPassData &passData,
  268. U32 &texIndex );
  269. virtual String getName()
  270. {
  271. return "Lightmap";
  272. }
  273. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
  274. };
  275. /// Tonemap
  276. class TonemapFeatGLSL : public ShaderFeatureGLSL
  277. {
  278. public:
  279. virtual void processVert( Vector<ShaderComponent*> &componentList,
  280. const MaterialFeatureData &fd );
  281. virtual void processPix( Vector<ShaderComponent*> &componentList,
  282. const MaterialFeatureData &fd );
  283. virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
  284. virtual Resources getResources( const MaterialFeatureData &fd );
  285. // Sets textures and texture flags for current pass
  286. virtual void setTexData( Material::StageData &stageDat,
  287. const MaterialFeatureData &fd,
  288. RenderPassData &passData,
  289. U32 &texIndex );
  290. virtual String getName()
  291. {
  292. return "Tonemap";
  293. }
  294. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
  295. };
  296. /// Baked lighting stored on the vertex color
  297. class VertLitGLSL : public ShaderFeatureGLSL
  298. {
  299. public:
  300. virtual void processVert( Vector<ShaderComponent*> &componentList,
  301. const MaterialFeatureData &fd );
  302. virtual void processPix( Vector<ShaderComponent*> &componentList,
  303. const MaterialFeatureData &fd );
  304. virtual Material::BlendOp getBlendOp(){ return Material::None; }
  305. virtual String getName()
  306. {
  307. return "Vert Lit";
  308. }
  309. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
  310. };
  311. /// Detail map
  312. class DetailFeatGLSL : public ShaderFeatureGLSL
  313. {
  314. public:
  315. virtual void processVert( Vector<ShaderComponent*> &componentList,
  316. const MaterialFeatureData &fd );
  317. virtual void processPix( Vector<ShaderComponent*> &componentList,
  318. const MaterialFeatureData &fd );
  319. virtual Resources getResources( const MaterialFeatureData &fd );
  320. virtual Material::BlendOp getBlendOp(){ return Material::Mul; }
  321. // Sets textures and texture flags for current pass
  322. virtual void setTexData( Material::StageData &stageDat,
  323. const MaterialFeatureData &fd,
  324. RenderPassData &passData,
  325. U32 &texIndex );
  326. virtual String getName()
  327. {
  328. return "Detail";
  329. }
  330. };
  331. /// Reflect Cubemap
  332. class ReflectCubeFeatGLSL : public ShaderFeatureGLSL
  333. {
  334. public:
  335. virtual void processVert( Vector<ShaderComponent*> &componentList,
  336. const MaterialFeatureData &fd );
  337. virtual void processPix( Vector<ShaderComponent*> &componentList,
  338. const MaterialFeatureData &fd );
  339. virtual Resources getResources( const MaterialFeatureData &fd );
  340. // Sets textures and texture flags for current pass
  341. virtual void setTexData( Material::StageData &stageDat,
  342. const MaterialFeatureData &fd,
  343. RenderPassData &passData,
  344. U32 &texIndex );
  345. virtual String getName()
  346. {
  347. return "Reflect Cube";
  348. }
  349. };
  350. /// Fog
  351. class FogFeatGLSL : public ShaderFeatureGLSL
  352. {
  353. protected:
  354. ShaderIncludeDependency mFogDep;
  355. public:
  356. FogFeatGLSL();
  357. virtual void processVert( Vector<ShaderComponent*> &componentList,
  358. const MaterialFeatureData &fd );
  359. virtual void processPix( Vector<ShaderComponent*> &componentList,
  360. const MaterialFeatureData &fd );
  361. virtual Resources getResources( const MaterialFeatureData &fd );
  362. virtual Material::BlendOp getBlendOp() { return Material::LerpAlpha; }
  363. virtual String getName()
  364. {
  365. return "Fog";
  366. }
  367. };
  368. /// Tex Anim
  369. class TexAnimGLSL : public ShaderFeatureGLSL
  370. {
  371. public:
  372. virtual Material::BlendOp getBlendOp() { return Material::None; }
  373. virtual String getName()
  374. {
  375. return "Texture Animation";
  376. }
  377. };
  378. /// Visibility
  379. class VisibilityFeatGLSL : public ShaderFeatureGLSL
  380. {
  381. protected:
  382. ShaderIncludeDependency mTorqueDep;
  383. public:
  384. VisibilityFeatGLSL();
  385. virtual void processVert( Vector<ShaderComponent*> &componentList,
  386. const MaterialFeatureData &fd );
  387. virtual void processPix( Vector<ShaderComponent*> &componentList,
  388. const MaterialFeatureData &fd );
  389. virtual Resources getResources( const MaterialFeatureData &fd );
  390. virtual Material::BlendOp getBlendOp() { return Material::None; }
  391. virtual String getName()
  392. {
  393. return "Visibility";
  394. }
  395. };
  396. ///
  397. class AlphaTestGLSL : public ShaderFeatureGLSL
  398. {
  399. public:
  400. virtual void processPix( Vector<ShaderComponent*> &componentList,
  401. const MaterialFeatureData &fd );
  402. virtual Material::BlendOp getBlendOp() { return Material::None; }
  403. virtual String getName()
  404. {
  405. return "Alpha Test";
  406. }
  407. };
  408. /// Special feature used to mask out the RGB color for
  409. /// non-glow passes of glow materials.
  410. /// @see RenderGlowMgr
  411. class GlowMaskGLSL : public ShaderFeatureGLSL
  412. {
  413. public:
  414. virtual void processPix( Vector<ShaderComponent*> &componentList,
  415. const MaterialFeatureData &fd );
  416. virtual Material::BlendOp getBlendOp() { return Material::None; }
  417. virtual String getName()
  418. {
  419. return "Glow Mask";
  420. }
  421. };
  422. /// This should be the final feature on most pixel shaders which
  423. /// encodes the color for the current HDR target format.
  424. /// @see HDRPostFx
  425. /// @see LightManager
  426. /// @see torque.glsl
  427. class HDROutGLSL : public ShaderFeatureGLSL
  428. {
  429. protected:
  430. ShaderIncludeDependency mTorqueDep;
  431. public:
  432. HDROutGLSL();
  433. virtual void processPix( Vector<ShaderComponent*> &componentList,
  434. const MaterialFeatureData &fd );
  435. virtual Material::BlendOp getBlendOp() { return Material::None; }
  436. virtual String getName() { return "HDR Output"; }
  437. };
  438. ///
  439. class FoliageFeatureGLSL : public ShaderFeatureGLSL
  440. {
  441. protected:
  442. ShaderIncludeDependency mDep;
  443. public:
  444. FoliageFeatureGLSL();
  445. virtual void processVert( Vector<ShaderComponent*> &componentList,
  446. const MaterialFeatureData &fd );
  447. virtual void processPix( Vector<ShaderComponent*> &componentList,
  448. const MaterialFeatureData &fd );
  449. virtual String getName()
  450. {
  451. return "Foliage Feature";
  452. }
  453. virtual void determineFeature( Material *material,
  454. const GFXVertexFormat *vertexFormat,
  455. U32 stageNum,
  456. const FeatureType &type,
  457. const FeatureSet &features,
  458. MaterialFeatureData *outFeatureData );
  459. virtual ShaderFeatureConstHandles* createConstHandles( GFXShader *shader, SimObject *userObject );
  460. };
  461. class ParticleNormalFeatureGLSL : public ShaderFeatureGLSL
  462. {
  463. public:
  464. virtual void processVert( Vector<ShaderComponent*> &componentList,
  465. const MaterialFeatureData &fd );
  466. virtual String getName()
  467. {
  468. return "Particle Normal Generation Feature";
  469. }
  470. };
  471. /// Special feature for unpacking imposter verts.
  472. /// @see RenderImposterMgr
  473. class ImposterVertFeatureGLSL : public ShaderFeatureGLSL
  474. {
  475. protected:
  476. ShaderIncludeDependency mDep;
  477. public:
  478. ImposterVertFeatureGLSL();
  479. virtual void processVert( Vector<ShaderComponent*> &componentList,
  480. const MaterialFeatureData &fd );
  481. virtual void processPix( Vector<ShaderComponent*> &componentList,
  482. const MaterialFeatureData &fd );
  483. virtual String getName() { return "Imposter Vert"; }
  484. virtual void determineFeature( Material *material,
  485. const GFXVertexFormat *vertexFormat,
  486. U32 stageNum,
  487. const FeatureType &type,
  488. const FeatureSet &features,
  489. MaterialFeatureData *outFeatureData );
  490. };
  491. /// Hardware Skinning
  492. class HardwareSkinningFeatureGLSL : public ShaderFeatureGLSL
  493. {
  494. protected:
  495. public:
  496. virtual void processVert(Vector<ShaderComponent*> &componentList,
  497. const MaterialFeatureData &fd);
  498. virtual String getName() { return "Hardware Skinning"; }
  499. };
  500. #endif // _SHADERGEN_GLSL_SHADERFEATUREGLSL_H_