forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseShader.h
341 lines (263 loc) · 11.7 KB
/
BaseShader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
// This is what all shaders inherit from.
//===========================================================================//
#ifndef BASESHADER_H
#define BASESHADER_H
#ifdef _WIN32
#pragma once
#endif
#include "materialsystem/IShader.h"
#include "materialsystem/imaterialvar.h"
#include "materialsystem/ishaderapi.h"
#include "materialsystem/imaterialsystemhardwareconfig.h"
#include "shaderlib/BaseShader.h"
#include "convar.h"
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class IMaterialVar;
//-----------------------------------------------------------------------------
// Standard material vars
//-----------------------------------------------------------------------------
// Note: if you add to these, add to s_StandardParams in CBaseShader.cpp
enum ShaderMaterialVars_t
{
FLAGS = 0,
FLAGS_DEFINED, // mask indicating if the flag was specified
FLAGS2,
FLAGS_DEFINED2,
COLOR,
ALPHA,
BASETEXTURE,
FRAME,
BASETEXTURETRANSFORM,
FLASHLIGHTTEXTURE,
FLASHLIGHTTEXTUREFRAME,
COLOR2,
SRGBTINT,
NUM_SHADER_MATERIAL_VARS
};
// Alpha belnd mode enums. Moved from basevsshader
enum BlendType_t
{
// no alpha blending
BT_NONE = 0,
// src * srcAlpha + dst * (1-srcAlpha)
// two passes for HDR:
// pass 1:
// color: src * srcAlpha + dst * (1-srcAlpha)
// alpha: srcAlpha * zero + dstAlpha * (1-srcAlpha)
// pass 2:
// color: none
// alpha: srcAlpha * one + dstAlpha * one
//
BT_BLEND,
// src * one + dst * one
// one pass for HDR
BT_ADD,
// Why do we ever use this instead of using premultiplied alpha?
// src * srcAlpha + dst * one
// two passes for HDR
// pass 1:
// color: src * srcAlpha + dst * one
// alpha: srcAlpha * one + dstAlpha * one
// pass 2:
// color: none
// alpha: srcAlpha * one + dstAlpha * one
BT_BLENDADD
};
//-----------------------------------------------------------------------------
// Base class for shaders, contains helper methods.
//-----------------------------------------------------------------------------
class CBaseShader : public IShader
{
public:
// constructor
CBaseShader();
// Methods inherited from IShader
virtual char const* GetFallbackShader( IMaterialVar** params ) const { return 0; }
virtual int GetNumParams( ) const;
virtual char const* GetParamName( int paramIndex ) const;
virtual char const* GetParamHelp( int paramIndex ) const;
virtual ShaderParamType_t GetParamType( int paramIndex ) const;
virtual char const* GetParamDefault( int paramIndex ) const;
virtual int GetParamFlags( int nParamIndex ) const;
virtual void InitShaderParams( IMaterialVar** ppParams, const char *pMaterialName );
virtual void InitShaderInstance( IMaterialVar** ppParams, IShaderInit *pShaderInit, const char *pMaterialName, const char *pTextureGroupName );
virtual void DrawElements( IMaterialVar **params, int nModulationFlags, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI,
VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContext );
virtual const SoftwareVertexShader_t GetSoftwareVertexShader() const { return m_SoftwareVertexShader; }
virtual int ComputeModulationFlags( IMaterialVar** params, IShaderDynamicAPI* pShaderAPI );
virtual bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame = true ) const;
virtual bool NeedsFullFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame = true ) const;
virtual bool IsTranslucent( IMaterialVar **params ) const;
public:
// These functions must be implemented by the shader
virtual void OnInitShaderParams( IMaterialVar** ppParams, const char *pMaterialName ) {}
virtual void OnInitShaderInstance( IMaterialVar** ppParams, IShaderInit *pShaderInit, const char *pMaterialName ) = 0;
virtual void OnDrawElements( IMaterialVar **params, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI, VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContextDataPtr ) = 0;
// Sets the default shadow state
void SetInitialShadowState( );
// Draws a snapshot
void Draw( bool bMakeActualDrawCall = true );
// Are we currently taking a snapshot?
bool IsSnapshotting() const;
// Gets at the current materialvar flags
int CurrentMaterialVarFlags() const;
// Finds a particular parameter (works because the lowest parameters match the shader)
int FindParamIndex( const char *pName ) const;
// Are we using graphics?
bool IsUsingGraphics();
// Are we using editor materials?
bool CanUseEditorMaterials();
// Gets the builder...
CMeshBuilder* MeshBuilder();
// Loads a texture
void LoadTexture( int nTextureVar, int nAdditionalCreationFlags = 0 );
// Loads a bumpmap
void LoadBumpMap( int nTextureVar );
// Loads a cubemap
void LoadCubeMap( int nTextureVar, int nAdditionalCreationFlags = 0 );
// get the shaderapi handle for a texture. BE CAREFUL WITH THIS.
ShaderAPITextureHandle_t GetShaderAPITextureBindHandle( int nTextureVar, int nFrameVar, int nTextureChannel = 0 );
// Binds a texture
void BindTexture( Sampler_t sampler1, Sampler_t sampler2, int nTextureVar, int nFrameVar = -1 );
void BindTexture( Sampler_t sampler1, int nTextureVar, int nFrameVar = -1 );
void BindTexture( Sampler_t sampler1, ITexture *pTexture, int nFrame = 0 );
void BindTexture( Sampler_t sampler1, Sampler_t sampler2, ITexture *pTexture, int nFrame = 0 );
void GetTextureDimensions( float* pOutWidth, float* pOutHeight, int nTextureVar );
// Is the texture translucent?
bool TextureIsTranslucent( int textureVar, bool isBaseTexture );
// Returns the translucency...
float GetAlpha( IMaterialVar** params = NULL );
// Is the color var white?
bool IsWhite( int colorVar );
// Helper methods for fog
void FogToOOOverbright( void );
void FogToWhite( void );
void FogToBlack( void );
void FogToGrey( void );
void FogToFogColor( void );
void DisableFog( void );
void DefaultFog( void );
// Helpers for alpha blending
void EnableAlphaBlending( ShaderBlendFactor_t src, ShaderBlendFactor_t dst );
void DisableAlphaBlending();
void SetBlendingShadowState( BlendType_t nMode );
void SetNormalBlendingShadowState( int textureVar = -1, bool isBaseTexture = true );
void SetAdditiveBlendingShadowState( int textureVar = -1, bool isBaseTexture = true );
void SetDefaultBlendingShadowState( int textureVar = -1, bool isBaseTexture = true );
void SingleTextureLightmapBlendMode( );
// Helpers for color modulation
void SetColorState( int colorVar, bool setAlpha = false );
bool IsAlphaModulating();
bool IsColorModulating();
void ComputeModulationColor( float* color );
void SetModulationShadowState( int tintVar = -1 );
void SetModulationDynamicState( int tintVar = -1 );
// Helpers for HDR
bool IsHDREnabled( void );
// Loads the identity matrix into the texture
void LoadIdentity( MaterialMatrixMode_t matrixMode );
// Loads the camera to world transform
void LoadCameraToWorldTransform( MaterialMatrixMode_t matrixMode );
void LoadCameraSpaceSphereMapTransform( MaterialMatrixMode_t matrixMode );
// Sets a texture translation transform in fixed function
void SetFixedFunctionTextureTranslation( MaterialMatrixMode_t mode, int translationVar );
void SetFixedFunctionTextureScale( MaterialMatrixMode_t mode, int scaleVar );
void SetFixedFunctionTextureScaledTransform( MaterialMatrixMode_t textureTransform, int transformVar, int scaleVar );
void SetFixedFunctionTextureTransform( MaterialMatrixMode_t textureTransform, int transformVar );
void CleanupDynamicStateFixedFunction( );
// Fixed function Base * detail pass
void FixedFunctionBaseTimesDetailPass( int baseTextureVar, int frameVar,
int baseTextureTransformVar, int detailVar, int detailScaleVar );
// Fixed function Self illumination pass
void FixedFunctionSelfIlluminationPass( Sampler_t sampler,
int baseTextureVar, int frameVar, int baseTextureTransformVar, int selfIllumTintVar );
// Masked environment map
void FixedFunctionMaskedEnvmapPass( int envMapVar, int envMapMaskVar,
int baseTextureVar, int envMapFrameVar, int envMapMaskFrameVar,
int frameVar, int maskOffsetVar, int maskScaleVar, int tintVar = -1 );
// Additive masked environment map
void FixedFunctionAdditiveMaskedEnvmapPass( int envMapVar, int envMapMaskVar,
int baseTextureVar, int envMapFrameVar, int envMapMaskFrameVar,
int frameVar, int maskOffsetVar, int maskScaleVar, int tintVar = -1 );
// Modulate by detail texture pass
void FixedFunctionMultiplyByDetailPass( int baseTextureVar, int frameVar,
int textureOffsetVar, int detailVar, int detailScaleVar );
// Multiply by lightmap pass
void FixedFunctionMultiplyByLightmapPass( int baseTextureVar, int frameVar,
int baseTextureTransformVar, float alphaOverride = -1 );
// Helper methods for environment mapping
int SetShadowEnvMappingState( int envMapMaskVar, int tintVar = -1 );
void SetDynamicEnvMappingState( int envMapVar, int envMapMaskVar,
int baseTextureVar, int envMapFrameVar, int envMapMaskFrameVar,
int frameVar, int maskOffsetVar, int maskScaleVar, int tintVar = -1 );
bool UsingFlashlight( IMaterialVar **params ) const;
bool UsingEditor( IMaterialVar **params ) const;
void DrawFlashlight_dx70( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
IShaderShadow* pShaderShadow,
int flashlightTextureVar, int flashlightTextureFrameVar,
bool suppress_lighting = false );
void SetFlashlightFixedFunctionTextureTransform( MaterialMatrixMode_t matrix );
void GetColorParameter( IMaterialVar** params, float *pColorOut ) const; // return tint color (color*color2)
void ApplyColor2Factor( float *pColorOut ) const; // (*pColorOut) *= COLOR2
static IMaterialVar **s_ppParams;
protected:
SoftwareVertexShader_t m_SoftwareVertexShader;
static const char *s_pTextureGroupName; // Current material's texture group name.
static IShaderShadow *s_pShaderShadow;
static IShaderDynamicAPI *s_pShaderAPI;
static IShaderInit *s_pShaderInit;
private:
static int s_nModulationFlags;
static CMeshBuilder *s_pMeshBuilder;
};
//-----------------------------------------------------------------------------
// Gets at the current materialvar flags
//-----------------------------------------------------------------------------
inline int CBaseShader::CurrentMaterialVarFlags() const
{
return s_ppParams[FLAGS]->GetIntValue();
}
//-----------------------------------------------------------------------------
// Are we currently taking a snapshot?
//-----------------------------------------------------------------------------
inline bool CBaseShader::IsSnapshotting() const
{
return (s_pShaderShadow != NULL);
}
//-----------------------------------------------------------------------------
// Is the color var white?
//-----------------------------------------------------------------------------
inline bool CBaseShader::IsWhite( int colorVar )
{
if (colorVar < 0)
return true;
if (!s_ppParams[colorVar]->IsDefined())
return true;
float color[3];
s_ppParams[colorVar]->GetVecValue( color, 3 );
return (color[0] >= 1.0f) && (color[1] >= 1.0f) && (color[2] >= 1.0f);
}
class CBasePerMaterialContextData // shaders can keep per material data in classes descended from this
{
public:
uint32 m_nVarChangeID;
bool m_bMaterialVarsChanged; // set by mat system when material vars change. shader should rehtink and then clear the var
FORCEINLINE CBasePerMaterialContextData( void )
{
m_bMaterialVarsChanged = true;
m_nVarChangeID = 0xffffffff;
}
// virtual destructor so that derived classes can have their own data to be cleaned up on
// delete of material
virtual ~CBasePerMaterialContextData( void )
{
}
};
#endif // BASESHADER_H