forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadersystem.h
216 lines (171 loc) · 6.54 KB
/
shadersystem.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $Header: $
// $NoKeywords: $
//
// Shader system:
// The shader system makes a few fundamental assumptions about when
// certain types of state get set.
//
// 1) Anything that can potentially affect vertex format must be set up
// during the shader shadow/snapshot phase
// 2) Anything that we can dynamically mess with (through a material var)
// should happen in the dynamic/render phase
// 3) In general, we try to cache off expensive state pre-processing in
// the shader shadow phase (like texture stage pipeline).
//
//=============================================================================//
#ifndef SHADERSYSTEM_H
#define SHADERSYSTEM_H
#ifdef _WIN32
#pragma once
#endif
#include "IShaderSystem.h"
#include "shaderlib/BaseShader.h"
#include "materialsystem/materialsystem_config.h"
#include "shaderapi/ishaderapi.h"
#include "materialsystem_global.h"
//-----------------------------------------------------------------------------
// forward declarations
//-----------------------------------------------------------------------------
class IMaterialVar;
class TextureManager_t;
class ITextureInternal;
class ShaderSystem_t;
class IMesh;
class IVertexBuffer;
class IIndexBuffer;
class Vector;
enum MaterialPrimitiveType_t;
enum MaterialPropertyTypes_t;
enum MaterialIndexFormat_t;
enum ShaderParamType_t;
//-----------------------------------------------------------------------------
// for ShaderRenderState_t::m_flags
//-----------------------------------------------------------------------------
enum
{
// The flags up here are computed from the shaders themselves
/*
// lighting flags
SHADER_UNLIT = 0x0000,
SHADER_VERTEX_LIT = 0x0001,
SHADER_NEEDS_LIGHTMAP = 0x0002,
SHADER_NEEDS_BUMPED_LIGHTMAPS = 0x0004,
SHADER_LIGHTING_MASK = 0x0007,
*/
// opacity flags
SHADER_OPACITY_ALPHATEST = 0x0010,
SHADER_OPACITY_OPAQUE = 0x0020,
SHADER_OPACITY_TRANSLUCENT = 0x0040,
SHADER_OPACITY_MASK = 0x0070,
};
enum
{
MAX_RENDER_PASSES = 4
};
//-----------------------------------------------------------------------------
// Information for a single render pass
//-----------------------------------------------------------------------------
struct RenderPassList_t
{
int m_nPassCount;
StateSnapshot_t m_Snapshot[MAX_RENDER_PASSES];
// per material shader-defined state
CBasePerMaterialContextData *m_pContextData[MAX_RENDER_PASSES];
};
struct ShaderRenderState_t
{
// These are the same, regardless of whether alpha or color mod is used
int m_Flags; // Can't shrink this to a short
VertexFormat_t m_VertexFormat;
VertexFormat_t m_VertexUsage;
MorphFormat_t m_MorphFormat;
// List of all snapshots
RenderPassList_t *m_pSnapshots;
};
//-----------------------------------------------------------------------------
// Used to get the snapshot count
//-----------------------------------------------------------------------------
enum
{
SNAPSHOT_COUNT_NORMAL = 16,
SNAPSHOT_COUNT_EDITOR = 32,
};
inline int SnapshotTypeCount()
{
return MaterialSystem()->CanUseEditorMaterials() ? SNAPSHOT_COUNT_EDITOR : SNAPSHOT_COUNT_NORMAL;
}
//-----------------------------------------------------------------------------
// Utility methods
//-----------------------------------------------------------------------------
inline void SetFlags( IMaterialVar **params, MaterialVarFlags_t _flag )
{
params[FLAGS]->SetIntValue( params[FLAGS]->GetIntValueFast() | (_flag) );
}
inline void SetFlags2( IMaterialVar **params, MaterialVarFlags2_t _flag )
{
params[FLAGS2]->SetIntValue( params[FLAGS2]->GetIntValueFast() | (_flag) );
}
inline bool IsFlagSet( IMaterialVar **params, MaterialVarFlags_t _flag )
{
return ((params[FLAGS]->GetIntValueFast() & (_flag) ) != 0);
}
inline bool IsFlag2Set( IMaterialVar **params, MaterialVarFlags2_t _flag )
{
return ((params[FLAGS2]->GetIntValueFast() & (_flag) ) != 0);
}
//-----------------------------------------------------------------------------
// Poll params + renderstate
//-----------------------------------------------------------------------------
inline bool IsTranslucent( const ShaderRenderState_t* pRenderState )
{
return (pRenderState->m_Flags & SHADER_OPACITY_TRANSLUCENT) != 0;
}
inline bool IsAlphaTested( ShaderRenderState_t* pRenderState )
{
return (pRenderState->m_Flags & SHADER_OPACITY_ALPHATEST) != 0;
}
//-----------------------------------------------------------------------------
// The shader system (a singleton)
//-----------------------------------------------------------------------------
abstract_class IShaderSystemInternal : public IShaderInit, public IShaderSystem
{
public:
// Initialization, shutdown
virtual void Init() = 0;
virtual void Shutdown() = 0;
virtual void ModInit() = 0;
virtual void ModShutdown() = 0;
// Methods related to reading in shader DLLs
virtual bool LoadShaderDLL( const char *pFullPath ) = 0;
virtual void UnloadShaderDLL( const char *pFullPath ) = 0;
// Find me a shader!
virtual IShader* FindShader( char const* pShaderName ) = 0;
// returns strings associated with the shader state flags...
virtual char const* ShaderStateString( int i ) const = 0;
virtual int ShaderStateCount( ) const = 0;
// Rendering related methods
// Create debugging materials
virtual void CreateDebugMaterials() = 0;
// Cleans up the debugging materials
virtual void CleanUpDebugMaterials() = 0;
// Call the SHADER_PARAM_INIT block of the shaders
virtual void InitShaderParameters( IShader *pShader, IMaterialVar **params, const char *pMaterialName ) = 0;
// Call the SHADER_INIT block of the shaders
virtual void InitShaderInstance( IShader *pShader, IMaterialVar **params, const char *pMaterialName, const char *pTextureGroupName ) = 0;
// go through each param and make sure it is the right type, load textures,
// compute state snapshots and vertex types, etc.
virtual bool InitRenderState( IShader *pShader, int numParams, IMaterialVar **params, ShaderRenderState_t* pRenderState, char const* pMaterialName ) = 0;
// When you're done with the shader, be sure to call this to clean up
virtual void CleanupRenderState( ShaderRenderState_t* pRenderState ) = 0;
// Draws the shader
virtual void DrawElements( IShader *pShader, IMaterialVar **params, ShaderRenderState_t* pShaderState, VertexCompressionType_t vertexCompression,
uint32 nMaterialVarTimeStamp ) = 0;
// Used to iterate over all shaders for editing purposes
virtual int ShaderCount() const = 0;
virtual int GetShaders( int nFirstShader, int nCount, IShader **ppShaderList ) const = 0;
};
#endif // SHADERSYSTEM_H