DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

RenderProgs.h (10879B)


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