W3DShaderManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. ** Command & Conquer Generals(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. class TextureClass; ///forward reference
  41. /** System for managing complex rendering settings which are either not handled by
  42. WW3D2 or need custom paths depending on the video card. This system will determine
  43. the proper shader given video card limitations and also allow the app to query the
  44. hardware for specific features.
  45. */
  46. class W3DShaderManager
  47. {
  48. public:
  49. //put any custom shaders (not going through W3D) in here.
  50. enum ShaderTypes
  51. { ST_INVALID, //invalid shader type.
  52. ST_TERRAIN_BASE, //shader to apply base terrain texture only
  53. ST_TERRAIN_BASE_NOISE1, //shader to apply base texture and cloud/noise 1.
  54. ST_TERRAIN_BASE_NOISE2, //shader to apply base texture and cloud/noise 2.
  55. ST_TERRAIN_BASE_NOISE12,//shader to apply base texture and both cloud/noise
  56. ST_SHROUD_TEXTURE, //shader to apply shroud texture projection.
  57. ST_MASK_TEXTURE, //shader to apply alpha mask texture projection.
  58. ST_ROAD_BASE, //shader to apply base terrain texture only
  59. ST_ROAD_BASE_NOISE1, //shader to apply base texture and cloud/noise 1.
  60. ST_ROAD_BASE_NOISE2, //shader to apply base texture and cloud/noise 2.
  61. ST_ROAD_BASE_NOISE12,//shader to apply base texture and both cloud/noise
  62. ST_CLOUD_TEXTURE, //shader to project clouds.
  63. ST_MAX
  64. };
  65. W3DShaderManager(void); ///<constructor
  66. static void init( void ); ///<determine optimal shaders for current device.
  67. static void shutdown(void); ///<release resources used by shaders
  68. static ChipsetType getChipset(void); ///<return current device chipset.
  69. static Int getShaderPasses(ShaderTypes shader); ///<rendering passes required for shader
  70. static Int setShader(ShaderTypes shader, Int pass); ///<enable specific shader pass.
  71. static void resetShader(ShaderTypes shader); ///<make sure W3D2 gets restored to normal
  72. ///Specify all textures (up to 8) which can be accessed by the shaders.
  73. static void setTexture(Int stage,TextureClass* texture) {m_Textures[stage]=texture;}
  74. ///Return current texture available to shaders.
  75. static inline TextureClass *getShaderTexture(Int stage) { return m_Textures[stage];} ///<returns currently selected texture for given stage
  76. ///Return last activated shader.
  77. static inline ShaderTypes getCurrentShader(void) {return m_currentShader;}
  78. /// Loads a .vso file and creates a vertex shader for it
  79. static HRESULT LoadAndCreateD3DShader(char* strFilePath, const DWORD* pDeclaration, DWORD Usage, Bool ShaderType, DWORD* pHandle);
  80. static Bool testMinimumRequirements(ChipsetType *videoChipType, CpuType *cpuType, Int *cpuFreq, Int *numRAM, Real *intBenchIndex, Real *floatBenchIndex, Real *memBenchIndex);
  81. static StaticGameLODLevel getGPUPerformanceIndex(void);
  82. static Real GetCPUBenchTime(void);
  83. // Filter methods
  84. static Bool filterPreRender(FilterTypes filter, Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  85. static Bool filterPostRender(FilterTypes filter, enum FilterModes mode, Coord2D &scrollDelta, Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  86. static Bool filterSetup(FilterTypes filter, enum FilterModes mode);
  87. // Support routines for filter methods.
  88. static Bool canRenderToTexture(void) { return (m_oldRenderSurface && m_newRenderSurface);}
  89. static void startRenderToTexture(void); ///< Sets render target to texture.
  90. static IDirect3DTexture8 * endRenderToTexture(void); ///< Ends render to texture, & returns texture.
  91. static IDirect3DTexture8 * getRenderTexture(void); ///< returns last used render target texture
  92. static void drawViewport(Int color); ///<draws 2 triangles covering the current tactical viewport
  93. protected:
  94. static TextureClass *m_Textures[8]; ///textures assigned to each of the possible stages
  95. static ChipsetType m_currentChipset; ///<last video card chipset that was detected.
  96. static ShaderTypes m_currentShader; ///<last shader that was set.
  97. static Int m_currentShaderPass; ///<pass of last shader that was set.
  98. static FilterTypes m_currentFilter; ///< Last filter that was set.
  99. // Info for a render to texture surface for special effects.
  100. static Bool m_renderingToTexture;
  101. static IDirect3DSurface8 *m_oldRenderSurface; ///<previous render target
  102. static IDirect3DTexture8 *m_renderTexture; ///<texture into which rendering will be redirected.
  103. static IDirect3DSurface8 *m_newRenderSurface; ///<new render target inside m_renderTexture
  104. static IDirect3DSurface8 *m_oldDepthSurface; ///<previous depth buffer surface
  105. };
  106. class W3DFilterInterface
  107. {
  108. public:
  109. virtual Int init(void) = 0; ///<perform any one time initialization and validation
  110. virtual Int shutdown(void) { return TRUE;}; ///<release resources used by shader
  111. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode) {skipRender=false; return false;} ///< Set up at start of render. Only applies to screen filter shaders.
  112. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta, Bool &doExtraRender){return false;} ///< Called after render. Only applies to screen filter shaders.
  113. virtual Bool setup(enum FilterModes mode){return false;} ///< Called when the filter is started, one time before the first prerender.
  114. protected:
  115. virtual Int set(enum FilterModes mode) = 0; ///<setup shader for the specified rendering pass.
  116. ///do any custom resetting necessary to bring W3D in sync.
  117. virtual void reset(void) = 0;
  118. };
  119. /*========= ScreenMotionBlurFilter =============================================================*/
  120. ///applies motion blur to viewport.
  121. class ScreenMotionBlurFilter : public W3DFilterInterface
  122. {
  123. public:
  124. virtual Int set(enum FilterModes mode); ///<setup shader for the specified rendering pass.
  125. virtual Int init(void); ///<perform any one time initialization and validation
  126. virtual void reset(void); ///<do any custom resetting necessary to bring W3D in sync.
  127. virtual Int shutdown(void); ///<release resources used by shader
  128. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  129. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta, Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  130. virtual Bool setup(enum FilterModes mode); ///< Called when the filter is started, one time before the first prerender.
  131. ScreenMotionBlurFilter();
  132. static void setZoomToPos(const Coord3D *pos) {m_zoomToPos = *pos; m_zoomToValid = true;}
  133. protected:
  134. enum {MAX_COUNT = 60,
  135. MAX_LIMIT = 30,
  136. COUNT_STEP = 5,
  137. DEFAULT_PAN_FACTOR = 30};
  138. Int m_maxCount;
  139. Int m_lastFrame;
  140. Bool m_decrement;
  141. Bool m_skipRender;
  142. Bool m_additive;
  143. Bool m_doZoomTo;
  144. Coord2D m_priorDelta;
  145. Int m_panFactor;
  146. static Coord3D m_zoomToPos;
  147. static Bool m_zoomToValid;
  148. } ;
  149. /*========= ScreenBWFilter =============================================================*/
  150. ///converts viewport to black & white.
  151. class ScreenBWFilter : public W3DFilterInterface
  152. {
  153. DWORD m_dwBWPixelShader; ///<D3D handle to pixel shader which tints texture to black & white.
  154. public:
  155. virtual Int init(void); ///<perform any one time initialization and validation
  156. virtual Int shutdown(void); ///<release resources used by shader
  157. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  158. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta,Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  159. virtual Bool setup(enum FilterModes mode){return true;} ///< Called when the filter is started, one time before the first prerender.
  160. static void setFadeParameters(Int fadeFrames, Int direction)
  161. {
  162. m_curFadeFrame = 0;
  163. m_fadeFrames = fadeFrames;
  164. m_fadeDirection = direction;
  165. }
  166. protected:
  167. virtual Int set(enum FilterModes mode); ///<setup shader for the specified rendering pass.
  168. virtual void reset(void); ///<do any custom resetting necessary to bring W3D in sync.
  169. static Int m_fadeFrames;
  170. static Int m_fadeDirection;
  171. static Int m_curFadeFrame;
  172. static Real m_curFadeValue;
  173. };
  174. class ScreenBWFilterDOT3 : public ScreenBWFilter
  175. {
  176. public:
  177. virtual Int init(void); ///<perform any one time initialization and validation
  178. virtual Int shutdown(void); ///<release resources used by shader
  179. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  180. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta,Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  181. virtual Bool setup(enum FilterModes mode){return true;} ///< Called when the filter is started, one time before the first prerender.
  182. protected:
  183. virtual Int set(enum FilterModes mode); ///<setup shader for the specified rendering pass.
  184. virtual void reset(void); ///<do any custom resetting necessary to bring W3D in sync.
  185. };
  186. /*========= ScreenCrossFadeFilter =============================================================*/
  187. ///Fades between 2 different rendered frames.
  188. class ScreenCrossFadeFilter : public W3DFilterInterface
  189. {
  190. public:
  191. virtual Int init(void); ///<perform any one time initialization and validation
  192. virtual Int shutdown(void); ///<release resources used by shader
  193. virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
  194. virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta,Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
  195. virtual Bool setup(enum FilterModes mode){return true;} ///< Called when the filter is started, one time before the first prerender.
  196. static void setFadeParameters(Int fadeFrames, Int direction)
  197. {
  198. m_curFadeFrame = 0;
  199. m_fadeFrames = fadeFrames;
  200. m_fadeDirection = direction;
  201. }
  202. static Real getCurrentFadeValue(void) { return m_curFadeValue;}
  203. static TextureClass *getCurrentMaskTexture(void) { return m_fadePatternTexture;}
  204. protected:
  205. virtual Int set(enum FilterModes mode); ///<setup shader for the specified rendering pass.
  206. virtual void reset(void); ///<do any custom resetting necessary to bring W3D in sync.
  207. Bool updateFadeLevel(void); ///<updated current state of fade and return true if not finished.
  208. static Int m_fadeFrames;
  209. static Int m_fadeDirection;
  210. static Int m_curFadeFrame;
  211. static Real m_curFadeValue;
  212. static Bool m_skipRender;
  213. static TextureClass *m_fadePatternTexture; ///<shape/pattern of the fade
  214. };
  215. #endif //__W3DSHADERMANAGER_H_