RenderProgs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __RENDERPROGS_H__
  21. #define __RENDERPROGS_H__
  22. static const int PC_ATTRIB_INDEX_VERTEX = 0;
  23. static const int PC_ATTRIB_INDEX_NORMAL = 2;
  24. static const int PC_ATTRIB_INDEX_COLOR = 3;
  25. static const int PC_ATTRIB_INDEX_COLOR2 = 4;
  26. static const int PC_ATTRIB_INDEX_ST = 8;
  27. static const int PC_ATTRIB_INDEX_TANGENT = 9;
  28. // This enum list corresponds to the global constant register indecies as defined in global.inc for all
  29. // shaders. We used a shared pool to keeps things simple. If something changes here then it also
  30. // needs to change in global.inc and vice versa
  31. enum renderParm_t {
  32. // For backwards compatibility, do not change the order of the first 17 items
  33. RENDERPARM_SCREENCORRECTIONFACTOR = 0,
  34. RENDERPARM_WINDOWCOORD,
  35. RENDERPARM_DIFFUSEMODIFIER,
  36. RENDERPARM_SPECULARMODIFIER,
  37. RENDERPARM_LOCALLIGHTORIGIN,
  38. RENDERPARM_LOCALVIEWORIGIN,
  39. RENDERPARM_LIGHTPROJECTION_S,
  40. RENDERPARM_LIGHTPROJECTION_T,
  41. RENDERPARM_LIGHTPROJECTION_Q,
  42. RENDERPARM_LIGHTFALLOFF_S,
  43. RENDERPARM_BUMPMATRIX_S,
  44. RENDERPARM_BUMPMATRIX_T,
  45. RENDERPARM_DIFFUSEMATRIX_S,
  46. RENDERPARM_DIFFUSEMATRIX_T,
  47. RENDERPARM_SPECULARMATRIX_S,
  48. RENDERPARM_SPECULARMATRIX_T,
  49. RENDERPARM_VERTEXCOLOR_MODULATE,
  50. RENDERPARM_VERTEXCOLOR_ADD,
  51. // The following are new and can be in any order
  52. RENDERPARM_COLOR,
  53. RENDERPARM_VIEWORIGIN,
  54. RENDERPARM_GLOBALEYEPOS,
  55. RENDERPARM_MVPMATRIX_X,
  56. RENDERPARM_MVPMATRIX_Y,
  57. RENDERPARM_MVPMATRIX_Z,
  58. RENDERPARM_MVPMATRIX_W,
  59. RENDERPARM_MODELMATRIX_X,
  60. RENDERPARM_MODELMATRIX_Y,
  61. RENDERPARM_MODELMATRIX_Z,
  62. RENDERPARM_MODELMATRIX_W,
  63. RENDERPARM_PROJMATRIX_X,
  64. RENDERPARM_PROJMATRIX_Y,
  65. RENDERPARM_PROJMATRIX_Z,
  66. RENDERPARM_PROJMATRIX_W,
  67. RENDERPARM_MODELVIEWMATRIX_X,
  68. RENDERPARM_MODELVIEWMATRIX_Y,
  69. RENDERPARM_MODELVIEWMATRIX_Z,
  70. RENDERPARM_MODELVIEWMATRIX_W,
  71. RENDERPARM_TEXTUREMATRIX_S,
  72. RENDERPARM_TEXTUREMATRIX_T,
  73. RENDERPARM_TEXGEN_0_S,
  74. RENDERPARM_TEXGEN_0_T,
  75. RENDERPARM_TEXGEN_0_Q,
  76. RENDERPARM_TEXGEN_0_ENABLED,
  77. RENDERPARM_TEXGEN_1_S,
  78. RENDERPARM_TEXGEN_1_T,
  79. RENDERPARM_TEXGEN_1_Q,
  80. RENDERPARM_TEXGEN_1_ENABLED,
  81. RENDERPARM_WOBBLESKY_X,
  82. RENDERPARM_WOBBLESKY_Y,
  83. RENDERPARM_WOBBLESKY_Z,
  84. RENDERPARM_OVERBRIGHT,
  85. RENDERPARM_ENABLE_SKINNING,
  86. RENDERPARM_ALPHA_TEST,
  87. RENDERPARM_TOTAL,
  88. RENDERPARM_USER = 128,
  89. };
  90. struct glslUniformLocation_t {
  91. int parmIndex;
  92. GLint uniformIndex;
  93. };
  94. /*
  95. ================================================================================================
  96. idRenderProgManager
  97. ================================================================================================
  98. */
  99. class idRenderProgManager {
  100. public:
  101. idRenderProgManager();
  102. virtual ~idRenderProgManager();
  103. void Init();
  104. void Shutdown();
  105. void SetRenderParm( renderParm_t rp, const float * value );
  106. void SetRenderParms( renderParm_t rp, const float * values, int numValues );
  107. int FindVertexShader( const char * name );
  108. int FindFragmentShader( const char * name );
  109. void BindShader( int vIndex, int fIndex );
  110. void BindShader_GUI( ) { BindShader_Builtin( BUILTIN_GUI ); }
  111. void BindShader_Color( ) { BindShader_Builtin( BUILTIN_COLOR ); }
  112. void BindShader_Texture( ) { BindShader_Builtin( BUILTIN_TEXTURED ); }
  113. void BindShader_TextureVertexColor() { BindShader_Builtin( BUILTIN_TEXTURE_VERTEXCOLOR ); };
  114. void BindShader_TextureVertexColorSkinned() { BindShader_Builtin( BUILTIN_TEXTURE_VERTEXCOLOR_SKINNED ); };
  115. void BindShader_TextureTexGenVertexColor() { BindShader_Builtin( BUILTIN_TEXTURE_TEXGEN_VERTEXCOLOR ); };
  116. void BindShader_Interaction() { BindShader_Builtin( BUILTIN_INTERACTION ); }
  117. void BindShader_InteractionSkinned() { BindShader_Builtin( BUILTIN_INTERACTION_SKINNED ); }
  118. void BindShader_InteractionAmbient() { BindShader_Builtin( BUILTIN_INTERACTION_AMBIENT ); }
  119. void BindShader_InteractionAmbientSkinned() { BindShader_Builtin( BUILTIN_INTERACTION_AMBIENT_SKINNED ); }
  120. void BindShader_SimpleShade() { BindShader_Builtin( BUILTIN_SIMPLESHADE ); }
  121. void BindShader_Environment() { BindShader_Builtin( BUILTIN_ENVIRONMENT ); }
  122. void BindShader_EnvironmentSkinned() { BindShader_Builtin( BUILTIN_ENVIRONMENT_SKINNED ); }
  123. void BindShader_BumpyEnvironment() { BindShader_Builtin( BUILTIN_BUMPY_ENVIRONMENT ); }
  124. void BindShader_BumpyEnvironmentSkinned() { BindShader_Builtin( BUILTIN_BUMPY_ENVIRONMENT_SKINNED ); }
  125. void BindShader_Depth() { BindShader_Builtin( BUILTIN_DEPTH ); }
  126. void BindShader_DepthSkinned() { BindShader_Builtin( BUILTIN_DEPTH_SKINNED ); }
  127. void BindShader_Shadow() { BindShader( builtinShaders[BUILTIN_SHADOW], -1 ); }
  128. void BindShader_ShadowSkinned() { BindShader( builtinShaders[BUILTIN_SHADOW_SKINNED], -1 ); }
  129. void BindShader_ShadowDebug() { BindShader_Builtin( BUILTIN_SHADOW_DEBUG ); }
  130. void BindShader_ShadowDebugSkinned() { BindShader_Builtin( BUILTIN_SHADOW_DEBUG_SKINNED ); }
  131. void BindShader_BlendLight() { BindShader_Builtin( BUILTIN_BLENDLIGHT ); }
  132. void BindShader_Fog() { BindShader_Builtin( BUILTIN_FOG ); }
  133. void BindShader_FogSkinned() { BindShader_Builtin( BUILTIN_FOG_SKINNED ); }
  134. void BindShader_SkyBox() { BindShader_Builtin( BUILTIN_SKYBOX ); }
  135. void BindShader_WobbleSky() { BindShader_Builtin( BUILTIN_WOBBLESKY ); }
  136. void BindShader_StereoDeGhost() { BindShader_Builtin( BUILTIN_STEREO_DEGHOST ); }
  137. void BindShader_StereoWarp() { BindShader_Builtin( BUILTIN_STEREO_WARP ); }
  138. void BindShader_StereoInterlace() { BindShader_Builtin( BUILTIN_STEREO_INTERLACE ); }
  139. void BindShader_PostProcess() { BindShader_Builtin( BUILTIN_POSTPROCESS ); }
  140. void BindShader_ZCullReconstruct() { BindShader_Builtin( BUILTIN_ZCULL_RECONSTRUCT ); }
  141. void BindShader_Bink() { BindShader_Builtin( BUILTIN_BINK ); }
  142. void BindShader_BinkGUI() { BindShader_Builtin( BUILTIN_BINK_GUI ); }
  143. void BindShader_MotionBlur() { BindShader_Builtin( BUILTIN_MOTION_BLUR); }
  144. // the joints buffer should only be bound for vertex programs that use joints
  145. bool ShaderUsesJoints() const { return vertexShaders[currentVertexShader].usesJoints; }
  146. // the rpEnableSkinning render parm should only be set for vertex programs that use it
  147. bool ShaderHasOptionalSkinning() const { return vertexShaders[currentVertexShader].optionalSkinning; }
  148. // unbind the currently bound render program
  149. void Unbind();
  150. // this should only be called via the reload shader console command
  151. void LoadAllShaders();
  152. void KillAllShaders();
  153. static const int MAX_GLSL_USER_PARMS = 8;
  154. const char* GetGLSLParmName( int rp ) const;
  155. int GetGLSLCurrentProgram() const { return currentRenderProgram; }
  156. void SetUniformValue( const renderParm_t rp, const float * value );
  157. void CommitUniforms();
  158. int FindGLSLProgram( const char* name, int vIndex, int fIndex );
  159. void ZeroUniforms();
  160. protected:
  161. void LoadVertexShader( int index );
  162. void LoadFragmentShader( int index );
  163. enum {
  164. BUILTIN_GUI,
  165. BUILTIN_COLOR,
  166. BUILTIN_SIMPLESHADE,
  167. BUILTIN_TEXTURED,
  168. BUILTIN_TEXTURE_VERTEXCOLOR,
  169. BUILTIN_TEXTURE_VERTEXCOLOR_SKINNED,
  170. BUILTIN_TEXTURE_TEXGEN_VERTEXCOLOR,
  171. BUILTIN_INTERACTION,
  172. BUILTIN_INTERACTION_SKINNED,
  173. BUILTIN_INTERACTION_AMBIENT,
  174. BUILTIN_INTERACTION_AMBIENT_SKINNED,
  175. BUILTIN_ENVIRONMENT,
  176. BUILTIN_ENVIRONMENT_SKINNED,
  177. BUILTIN_BUMPY_ENVIRONMENT,
  178. BUILTIN_BUMPY_ENVIRONMENT_SKINNED,
  179. BUILTIN_DEPTH,
  180. BUILTIN_DEPTH_SKINNED,
  181. BUILTIN_SHADOW,
  182. BUILTIN_SHADOW_SKINNED,
  183. BUILTIN_SHADOW_DEBUG,
  184. BUILTIN_SHADOW_DEBUG_SKINNED,
  185. BUILTIN_BLENDLIGHT,
  186. BUILTIN_FOG,
  187. BUILTIN_FOG_SKINNED,
  188. BUILTIN_SKYBOX,
  189. BUILTIN_WOBBLESKY,
  190. BUILTIN_POSTPROCESS,
  191. BUILTIN_STEREO_DEGHOST,
  192. BUILTIN_STEREO_WARP,
  193. BUILTIN_ZCULL_RECONSTRUCT,
  194. BUILTIN_BINK,
  195. BUILTIN_BINK_GUI,
  196. BUILTIN_STEREO_INTERLACE,
  197. BUILTIN_MOTION_BLUR,
  198. MAX_BUILTINS
  199. };
  200. int builtinShaders[MAX_BUILTINS];
  201. void BindShader_Builtin( int i ) { BindShader( builtinShaders[i], builtinShaders[i] ); }
  202. GLuint LoadShader( GLenum target, const char * name, const char * startToken );
  203. bool CompileGLSL( GLenum target, const char * name );
  204. GLuint LoadGLSLShader( GLenum target, const char * name, idList<int> & uniforms );
  205. void LoadGLSLProgram( const int programIndex, const int vertexShaderIndex, const int fragmentShaderIndex );
  206. static const GLuint INVALID_PROGID = 0xFFFFFFFF;
  207. struct vertexShader_t {
  208. vertexShader_t() : progId( INVALID_PROGID ), usesJoints( false ), optionalSkinning( false ) {}
  209. idStr name;
  210. GLuint progId;
  211. bool usesJoints;
  212. bool optionalSkinning;
  213. idList<int> uniforms;
  214. };
  215. struct fragmentShader_t {
  216. fragmentShader_t() : progId( INVALID_PROGID ) {}
  217. idStr name;
  218. GLuint progId;
  219. idList<int> uniforms;
  220. };
  221. struct glslProgram_t {
  222. glslProgram_t() : progId( INVALID_PROGID ),
  223. vertexShaderIndex( -1 ),
  224. fragmentShaderIndex( -1 ),
  225. vertexUniformArray( -1 ),
  226. fragmentUniformArray( -1 ) {}
  227. idStr name;
  228. GLuint progId;
  229. int vertexShaderIndex;
  230. int fragmentShaderIndex;
  231. GLint vertexUniformArray;
  232. GLint fragmentUniformArray;
  233. idList<glslUniformLocation_t> uniformLocations;
  234. };
  235. int currentRenderProgram;
  236. idList<glslProgram_t, TAG_RENDER> glslPrograms;
  237. idStaticList<idVec4, RENDERPARM_USER + MAX_GLSL_USER_PARMS> glslUniforms;
  238. int currentVertexShader;
  239. int currentFragmentShader;
  240. idList<vertexShader_t, TAG_RENDER> vertexShaders;
  241. idList<fragmentShader_t, TAG_RENDER> fragmentShaders;
  242. };
  243. extern idRenderProgManager renderProgManager;
  244. #endif