shaderFeatureGLSL.h 19 KB

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