shaderFeatureGLSL.h 21 KB

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