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
|
#pragma once
#include <string>
#include "External/shaderlab/Library/FastPropertyName.h"
#include "GfxDeviceTypes.h"
#include "External/shaderlab/Library/shadertypes.h"
#include "Runtime/Utilities/dynamic_array.h"
#include "Runtime/Utilities/vector_set.h"
#include "BuiltinShaderParams.h"
#include "GfxDeviceConfigure.h"
#include "Runtime/GfxDevice/threaded/ClientIDMapper.h"
class ShaderErrors;
class GpuProgram;
class GpuProgramParameters;
class CreateGpuProgramOutput;
class GfxPatchInfo;
class ChannelAssigns;
using ShaderLab::FastPropertyName;
namespace ShaderLab { class PropertySheet; class SubProgram; }
enum GpuProgramLevel {
kGpuProgramNone = 0, // fixed function
kGpuProgramSM1,
kGpuProgramSM2,
kGpuProgramSM3,
kGpuProgramSM4,
kGpuProgramSM5,
kGpuProgramCount // keep this last
};
struct PropertyNamesSet {
PropertyNamesSet() : valueSize(0) { }
vector_set<int> names; // IDs of shader property names
UInt32 valueSize; // Buffer size needed to hold all property values of the above
};
// ------------------------------------------------------------------------
bool CheckGpuProgramUsable (const char* str);
// Create GPU programs with this function. This can return NULL if the program is not
// recognized. When done with a program call Release() on it, never delete directly.
GpuProgram* CreateGpuProgram( const std::string& source, CreateGpuProgramOutput& output );
// ------------------------------------------------------------------------
class GpuProgramParameters {
public:
struct ValueParameter {
FastPropertyName m_Name; // The name of the value to look up.
int m_Index; // The index of the first parameter to upload to
int m_ArraySize; // The index of the first parameter to upload to
ShaderParamType m_Type;
UInt8 m_RowCount;
UInt8 m_ColCount;
ValueParameter (FastPropertyName n, ShaderParamType type, int idx, int arraySize, int rowCount, int colCount) : m_Name(n), m_Index(idx), m_ArraySize(arraySize), m_Type(type), m_RowCount(rowCount), m_ColCount(colCount) {}
ValueParameter () : m_Index(0), m_ArraySize(0), m_RowCount(0), m_ColCount(0) {}
bool operator == (const ValueParameter& other) const { return m_Name.index == other.m_Name.index; }
bool operator < (const ValueParameter& other) const { return m_Name.index < other.m_Name.index; }
};
typedef dynamic_array<ValueParameter> ValueParameterArray;
struct TextureParameter {
FastPropertyName m_Name;
int m_Index;
int m_SamplerIndex;
TextureDimension m_Dim;
TextureParameter (FastPropertyName n, int idx, int samplerIdx, TextureDimension dim) : m_Name(n), m_Index(idx), m_SamplerIndex(samplerIdx), m_Dim(dim) { }
TextureParameter () : m_Index(0), m_SamplerIndex(0), m_Dim(kTexDimNone) {}
};
typedef std::vector<TextureParameter> TextureParameterList;
struct BufferParameter {
FastPropertyName m_Name;
int m_Index;
BufferParameter (FastPropertyName n, int idx) : m_Name(n), m_Index(idx) { }
BufferParameter () : m_Index(0) {}
};
typedef dynamic_array<BufferParameter> BufferParameterArray;
#if GFX_SUPPORTS_CONSTANT_BUFFERS
struct ConstantBuffer {
FastPropertyName m_Name;
ValueParameterArray m_ValueParams;
int m_Size;
int m_BindIndex;
};
typedef std::vector<ConstantBuffer> ConstantBufferList;
#endif
public:
GpuProgramParameters () : m_ValuesSize(0), m_Status(kBlank)
#if ENABLE_GFXDEVICE_REMOTE_PROCESS_CLIENT
,m_InternalHandle(0)
#endif
{ }
void AddValueParam (const ValueParameter& param);
void AddTextureParam (const TextureParameter& param);
void AddVectorParam (int index, ShaderParamType type, int dimension, const char* nameStr, int cbIndex, PropertyNamesSet* outNames);
void AddMatrixParam (int index, const char* nameStr, int rowCount, int colCount, int cbIndex, PropertyNamesSet* outNames);
void AddTextureParam (int index, int samplerIndex, const char* nameStr, TextureDimension dim, PropertyNamesSet* outNames);
void AddBufferParam (int index, const char* nameStr, PropertyNamesSet* outNames);
const ValueParameterArray& GetValueParams() const;
const TextureParameterList& GetTextureParams() const { return m_TextureParams; }
TextureParameterList& GetTextureParams() { return m_TextureParams; }
const BufferParameterArray& GetBufferParams() const { return m_BufferParams; }
const ValueParameter* FindParam(const FastPropertyName& name, int* outCBIndex = NULL) const;
const TextureParameter* FindTextureParam(const FastPropertyName& name, TextureDimension dim) const;
#if GFX_SUPPORTS_CONSTANT_BUFFERS
const ConstantBufferList& GetConstantBuffers() const { return m_ConstantBuffers; }
ConstantBufferList& GetConstantBuffers() { return m_ConstantBuffers; }
void SetConstantBufferCount (size_t count)
{
m_ConstantBuffers.resize (count);
}
#endif
const BuiltinShaderParamIndices& GetBuiltinParams() const { return m_BuiltinParams; }
int GetValuesSize() const { return m_ValuesSize; }
UInt8* PrepareValues (
const ShaderLab::PropertySheet* props,
UInt8* buffer,
const UInt8* bufferStart = NULL,
GfxPatchInfo* outPatchInfo = NULL,
bool* outMissingTextures = NULL) const;
bool IsReady() const { return m_Status == kReady; }
bool IsDirty() const { return m_Status == kDirty; }
virtual void MakeReady();
#if ENABLE_GFXDEVICE_REMOTE_PROCESS_CLIENT
// Reference to the created GpuPogramParameters on the worker.
ClientIDWrapper(GpuProgramParameters) m_InternalHandle;
#endif
private:
enum State {
kBlank,
kDirty,
kReady
};
struct NameToValueIndex {
int nameIndex;
SInt16 cbIndex;
UInt16 valueIndex;
bool operator == (const NameToValueIndex& other) const { return nameIndex == other.nameIndex; }
bool operator < (const NameToValueIndex& other) const { return nameIndex < other.nameIndex; }
};
void MakeValueParamsReady (ValueParameterArray& values, int cbIndex);
ValueParameterArray& GetValuesArray (int cbIndex) {
ValueParameterArray* values = &m_ValueParams;
#if GFX_SUPPORTS_CONSTANT_BUFFERS
if (cbIndex >= 0)
{
DebugAssert (cbIndex < m_ConstantBuffers.size());
values = &m_ConstantBuffers[cbIndex].m_ValueParams;
}
#endif
return *values;
}
const ValueParameterArray& GetValuesArray (int cbIndex) const {
const ValueParameterArray* values = &m_ValueParams;
#if GFX_SUPPORTS_D3D11
if (cbIndex >= 0)
{
DebugAssert (cbIndex < m_ConstantBuffers.size());
values = &m_ConstantBuffers[cbIndex].m_ValueParams;
}
#endif
return *values;
}
private:
typedef dynamic_array<NameToValueIndex> NameToValueIndexMap;
ValueParameterArray m_ValueParams;
NameToValueIndexMap m_NamedParams;
TextureParameterList m_TextureParams;
BufferParameterArray m_BufferParams;
#if GFX_SUPPORTS_CONSTANT_BUFFERS
ConstantBufferList m_ConstantBuffers;
#endif
BuiltinShaderParamIndices m_BuiltinParams;
int m_ValuesSize;
State m_Status;
};
// ------------------------------------------------------------------------
// Base class for GPU programs.
class GpuProgram {
public:
virtual ~GpuProgram();
virtual void ApplyGpuProgram (const GpuProgramParameters& params, const UInt8* buffer) = 0;
ShaderImplType GetImplType() const { return m_ImplType; }
virtual bool IsSupported () const;
GpuProgramLevel GetLevel() const { return m_GpuProgramLevel; }
void SetNotSupported (bool v) { m_NotSupported = v; }
protected:
GpuProgram();
protected:
ShaderImplType m_ImplType; // Actual implementation type
bool m_NotSupported;
bool m_WasDestroyed;
GpuProgramLevel m_GpuProgramLevel;
};
class CreateGpuProgramOutput
{
public:
CreateGpuProgramOutput();
~CreateGpuProgramOutput();
bool GetPerFogModeParamsEnabled() const { return m_PerFogModeParamsEnabled; }
void SetPerFogModeParamsEnabled(bool enable) { m_PerFogModeParamsEnabled = enable; }
const GpuProgramParameters* GetParams() const { return m_Params; }
GpuProgramParameters& CreateParams();
const ShaderErrors* GetShaderErrors() const { return m_ShaderErrors; }
ShaderErrors& CreateShaderErrors();
PropertyNamesSet* GetOutNames() const { return m_OutNames; }
const void SetOutNames(PropertyNamesSet* value) { m_OutNames = value; }
const ChannelAssigns* GetChannelAssigns() const { return m_ChannelAssigns; }
ChannelAssigns& CreateChannelAssigns();
private:
bool m_PerFogModeParamsEnabled;
GpuProgramParameters* m_Params;
ShaderErrors* m_ShaderErrors;
ChannelAssigns* m_ChannelAssigns;
PropertyNamesSet* m_OutNames;
};
#if GFX_SUPPORTS_OPENGL || GFX_SUPPORTS_OPENGLES20 || GFX_SUPPORTS_OPENGLES30
typedef unsigned int GLShaderID;
class GpuProgramGL : public GpuProgram {
public:
virtual ~GpuProgramGL();
GLShaderID GetGLProgramIfCreated(FogMode fog) const { return m_Programs[fog]; }
protected:
GpuProgramGL();
protected:
std::string m_SourceForFog; // original source, used for fog patching if needed
GLShaderID m_Programs[kFogModeCount];
};
#endif
|