W3DShaderManager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DShaderManager.h /////////////////////////////////////////////////////////
  24. //
  25. // Custom shader system that allows more options and easier device validation than
  26. // possible with W3D2.
  27. //
  28. // Author: Mark Wilczynski, August 2001
  29. //
  30. ///////////////////////////////////////////////////////////////////////////////
  31. #pragma once
  32. #ifndef __W3DSHADERMANAGER_H_
  33. #define __W3DSHADERMANAGER_H_
  34. #include "WW3D2/Texture.h"
  35. enum FilterTypes;
  36. enum CustomScenePassModes;
  37. enum StaticGameLODLevel;
  38. enum ChipsetType;
  39. enum CpuType;
  40. enum GraphicsVenderID;
  41. class TextureClass; ///forward reference
  42. /** System for managing complex rendering settings which are either not handled by
  43. WW3D2 or need custom paths depending on the video card. This system will determine
  44. the proper shader given video card limitations and also allow the app to query the
  45. hardware for specific features.
  46. */
  47. class W3DShaderManager
  48. {
  49. public:
  50. //put any custom shaders (not going through W3D) in here.
  51. enum ShaderTypes
  52. { ST_INVALID, //invalid shader type.
  53. ST_TERRAIN_BASE, //shader to apply base terrain texture only
  54. ST_TERRAIN_BASE_NOISE1, //shader to apply base texture and cloud/noise 1.
  55. ST_TERRAIN_BASE_NOISE2, //shader to apply base texture and cloud/noise 2.
  56. ST_TERRAIN_BASE_NOISE12,//shader to apply base texture and both cloud/noise
  57. ST_SHROUD_TEXTURE, //shader to apply shroud texture projection.
  58. ST_MASK_TEXTURE, //shader to apply alpha mask texture projection.
  59. ST_ROAD_BASE, //shader to apply base terrain texture only
  60. ST_ROAD_BASE_NOISE1, //shader to apply base texture and cloud/noise 1.
  61. ST_ROAD_BASE_NOISE2, //shader to apply base texture and cloud/noise 2.
  62. ST_ROAD_BASE_NOISE12,//shader to apply base texture and both cloud/noise
  63. ST_CLOUD_TEXTURE, //shader to project clouds.
  64. ST_FLAT_TERRAIN_BASE, //shader to apply base terrain texture only
  65. ST_FLAT_TERRAIN_BASE_NOISE1, //shader to apply base texture and cloud/noise 1.
  66. ST_FLAT_TERRAIN_BASE_NOISE2, //shader to apply base texture and cloud/noise 2.
  67. ST_FLAT_TERRAIN_BASE_NOISE12,//shader to apply base texture and both cloud/noise
  68. ST_FLAT_SHROUD_TEXTURE, //shader to apply shroud texture projection.
  69. ST_MAX
  70. };
  71. W3DShaderManager(void); ///<constructor
  72. static void init( void ); ///<determine optimal shaders for current device.
  73. static void shutdown(void); ///<release resources used by shaders
  74. static ChipsetType getChipset(void); ///<return current device chipset.
  75. static GraphicsVenderID getCurrentVendor(void) {return m_currentVendor;} ///<return current card vendor.
  76. static getCurrentDriverVersion(void) {return m_driverVersion; } ///<return current driver version.
  77. static Int getShaderPasses(ShaderTypes shader); ///<rendering passes required for shader
  78. static Int setShader(ShaderTypes shader, Int pass); ///<enable specific shader pass.
  79. static Int setShroudTex(Int stage); ///<Set shroud in a texture stage.
  80. static void resetShader(ShaderTypes shader); ///<make sure W3D2 gets restored to normal
  81. ///Specify all textures (up to 8) which can be accessed by the shaders.
  82. static void setTexture(Int stage,TextureClass* texture) {m_Textures[stage]=texture;}
  83. ///Return current texture available to shaders.
  84. static inline TextureClass *getShaderTexture(Int stage) { return m_Textures[stage];} ///<returns currently selected texture for given stage
  85. ///Return last activated shader.
  86. static inline ShaderTypes getCurrentShader(void) {return m_currentShader;}
  87. /// Loads a .vso file and creates a vertex shader for it
  88. static HRESULT LoadAndCreateD3DShader(char* strFilePath, const DWORD* pDeclaration, DWORD Usage, Bool ShaderType, DWORD* pHandle);
  89. static Bool testMinimumRequirements(ChipsetType *videoChipType, CpuType *cpuType, Int *cpuFreq, Int *numRAM, Real *intBenchIndex, Real *floatBenchIndex, Real *memBenchIndex);
  90. static StaticGameLODLevel getGPUPerformanceIndex(void);
  91. static Real GetCPUBenchTime(void);
  92. // Filter methods
  93. static Bool filterPreRender(FilterTypes filter, Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  94. static Bool filterPostRender(FilterTypes filter, enum FilterModes mode, Coord2D &scrollDelta, Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  95. static Bool filterSetup(FilterTypes filter, enum FilterModes mode);
  96. // Support routines for filter methods.
  97. static Bool canRenderToTexture(void) { return (m_oldRenderSurface && m_newRenderSurface);}
  98. static void startRenderToTexture(void); ///< Sets render target to texture.
  99. static IDirect3DTexture8 * endRenderToTexture(void); ///< Ends render to texture, & returns texture.
  100. static IDirect3DTexture8 * getRenderTexture(void); ///< returns last used render target texture
  101. static Bool isRenderingToTexture(void) {return m_renderingToTexture; }
  102. static void drawViewport(Int color); ///<draws 2 triangles covering the current tactical viewport
  103. protected:
  104. static TextureClass *m_Textures[8]; ///textures assigned to each of the possible stages
  105. static ChipsetType m_currentChipset; ///<last video card chipset that was detected.
  106. static GraphicsVenderID m_currentVendor; ///<last video card vendor
  107. static __int64 m_driverVersion; ///<driver version of last chipset.
  108. static ShaderTypes m_currentShader; ///<last shader that was set.
  109. static Int m_currentShaderPass; ///<pass of last shader that was set.
  110. static FilterTypes m_currentFilter; ///< Last filter that was set.
  111. // Info for a render to texture surface for special effects.
  112. static Bool m_renderingToTexture;
  113. static IDirect3DSurface8 *m_oldRenderSurface; ///<previous render target
  114. static IDirect3DTexture8 *m_renderTexture; ///<texture into which rendering will be redirected.
  115. static IDirect3DSurface8 *m_newRenderSurface; ///<new render target inside m_renderTexture
  116. static IDirect3DSurface8 *m_oldDepthSurface; ///<previous depth buffer surface
  117. };
  118. class W3DFilterInterface
  119. {
  120. public:
  121. virtual Int init(void) = 0; ///<perform any one time initialization and validation
  122. virtual Int shutdown(void) { return TRUE;}; ///<release resources used by shader
  123. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode) {skipRender=false; return false;} ///< Set up at start of render. Only applies to screen filter shaders.
  124. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta, Bool &doExtraRender){return false;} ///< Called after render. Only applies to screen filter shaders.
  125. virtual Bool setup(enum FilterModes mode){return false;} ///< Called when the filter is started, one time before the first prerender.
  126. protected:
  127. virtual Int set(enum FilterModes mode) = 0; ///<setup shader for the specified rendering pass.
  128. ///do any custom resetting necessary to bring W3D in sync.
  129. virtual void reset(void) = 0;
  130. };
  131. /*========= ScreenMotionBlurFilter =============================================================*/
  132. ///applies motion blur to viewport.
  133. class ScreenMotionBlurFilter : public W3DFilterInterface
  134. {
  135. public:
  136. virtual Int set(enum FilterModes mode); ///<setup shader for the specified rendering pass.
  137. virtual Int init(void); ///<perform any one time initialization and validation
  138. virtual void reset(void); ///<do any custom resetting necessary to bring W3D in sync.
  139. virtual Int shutdown(void); ///<release resources used by shader
  140. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  141. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta, Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  142. virtual Bool setup(enum FilterModes mode); ///< Called when the filter is started, one time before the first prerender.
  143. ScreenMotionBlurFilter();
  144. static void setZoomToPos(const Coord3D *pos) {m_zoomToPos = *pos; m_zoomToValid = true;}
  145. protected:
  146. enum {MAX_COUNT = 60,
  147. MAX_LIMIT = 30,
  148. COUNT_STEP = 5,
  149. DEFAULT_PAN_FACTOR = 30};
  150. Int m_maxCount;
  151. Int m_lastFrame;
  152. Bool m_decrement;
  153. Bool m_skipRender;
  154. Bool m_additive;
  155. Bool m_doZoomTo;
  156. Coord2D m_priorDelta;
  157. Int m_panFactor;
  158. static Coord3D m_zoomToPos;
  159. static Bool m_zoomToValid;
  160. } ;
  161. /*========= ScreenBWFilter =============================================================*/
  162. ///converts viewport to black & white.
  163. class ScreenBWFilter : public W3DFilterInterface
  164. {
  165. DWORD m_dwBWPixelShader; ///<D3D handle to pixel shader which tints texture to black & white.
  166. public:
  167. virtual Int init(void); ///<perform any one time initialization and validation
  168. virtual Int shutdown(void); ///<release resources used by shader
  169. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  170. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta,Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  171. virtual Bool setup(enum FilterModes mode){return true;} ///< Called when the filter is started, one time before the first prerender.
  172. static void setFadeParameters(Int fadeFrames, Int direction)
  173. {
  174. m_curFadeFrame = 0;
  175. m_fadeFrames = fadeFrames;
  176. m_fadeDirection = direction;
  177. }
  178. protected:
  179. virtual Int set(enum FilterModes mode); ///<setup shader for the specified rendering pass.
  180. virtual void reset(void); ///<do any custom resetting necessary to bring W3D in sync.
  181. static Int m_fadeFrames;
  182. static Int m_fadeDirection;
  183. static Int m_curFadeFrame;
  184. static Real m_curFadeValue;
  185. };
  186. class ScreenBWFilterDOT3 : public ScreenBWFilter
  187. {
  188. public:
  189. virtual Int init(void); ///<perform any one time initialization and validation
  190. virtual Int shutdown(void); ///<release resources used by shader
  191. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  192. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta,Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  193. virtual Bool setup(enum FilterModes mode){return true;} ///< Called when the filter is started, one time before the first prerender.
  194. protected:
  195. virtual Int set(enum FilterModes mode); ///<setup shader for the specified rendering pass.
  196. virtual void reset(void); ///<do any custom resetting necessary to bring W3D in sync.
  197. };
  198. /*========= ScreenCrossFadeFilter =============================================================*/
  199. ///Fades between 2 different rendered frames.
  200. class ScreenCrossFadeFilter : public W3DFilterInterface
  201. {
  202. public:
  203. virtual Int init(void); ///<perform any one time initialization and validation
  204. virtual Int shutdown(void); ///<release resources used by shader
  205. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  206. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta,Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  207. virtual Bool setup(enum FilterModes mode){return true;} ///< Called when the filter is started, one time before the first prerender.
  208. static void setFadeParameters(Int fadeFrames, Int direction)
  209. {
  210. m_curFadeFrame = 0;
  211. m_fadeFrames = fadeFrames;
  212. m_fadeDirection = direction;
  213. }
  214. static Real getCurrentFadeValue(void) { return m_curFadeValue;}
  215. static TextureClass *getCurrentMaskTexture(void) { return m_fadePatternTexture;}
  216. protected:
  217. virtual Int set(enum FilterModes mode); ///<setup shader for the specified rendering pass.
  218. virtual void reset(void); ///<do any custom resetting necessary to bring W3D in sync.
  219. Bool updateFadeLevel(void); ///<updated current state of fade and return true if not finished.
  220. static Int m_fadeFrames;
  221. static Int m_fadeDirection;
  222. static Int m_curFadeFrame;
  223. static Real m_curFadeValue;
  224. static Bool m_skipRender;
  225. static TextureClass *m_fadePatternTexture; ///<shape/pattern of the fade
  226. };
  227. #endif //__W3DSHADERMANAGER_H_