-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathmaterial.h
More file actions
353 lines (237 loc) · 7.24 KB
/
material.h
File metadata and controls
353 lines (237 loc) · 7.24 KB
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
342
343
344
345
346
347
348
349
350
351
352
#ifndef MATERIAL_H
#define MATERIAL_H
#include "engine.h"
#include "texture.h"
class Transform;
class MaterialInstance;
class CommandBuffer;
class ENGINE_EXPORT Material : public Resource {
A_OBJECT(Material, Resource, Resources)
A_PROPERTIES(
A_PROPERTY(int, materialType, Material::materialType, Material::setMaterialType),
A_PROPERTY(int, lightModel, Material::lightModel, Material::setLightModel),
A_PROPERTY(bool, doubleSided, Material::doubleSided, Material::setDoubleSided),
A_PROPERTY(bool, wireframe, Material::wireframe, Material::setWireframe)
)
A_NOMETHODS()
A_ENUMS(
A_ENUM(Type,
A_VALUE(Surface),
A_VALUE(PostProcess),
A_VALUE(LightFunction)),
A_ENUM(LightModel,
A_VALUE(Unlit),
A_VALUE(Lit),
A_VALUE(Subsurface))
)
public:
enum Layer {
Opaque = (1<<0),
Translucent = (1<<1),
Visibility = (1<<2),
Shadowcast = (1<<3)
};
enum Type {
Surface,
PostProcess,
LightFunction
};
enum LightModel {
Unlit,
Lit,
Subsurface
};
enum SurfaceType {
Static,
Skinned,
Billboard
};
enum BlendOp {
Add,
Subtract,
ReverseSubtract,
Min,
Max
};
enum BlendFactor {
Zero,
One,
SourceColor,
OneMinusSourceColor,
DestinationColor,
OneMinusDestinationColor,
SourceAlpha,
OneMinusSourceAlpha,
DestinationAlpha,
OneMinusDestinationAlpha,
SourceAlphaSaturate,
ConstantColor,
OneMinusConstantColor,
ConstantAlpha,
OneMinusConstantAlpha
};
enum ActionType {
Keep,
Clear,
Replace,
Increment,
IncrementWrap,
Decrement,
DecrementWrap,
Invert
};
enum TestFunction {
Never,
Less,
LessOrEqual,
Greater,
GreaterOrEqual,
Equal,
NotEqual,
Always
};
enum CullingMode {
Front,
Back,
FrontAndBack
};
struct RasterState {
int32_t cullingMode = CullingMode::Back;
int32_t offsetUnits = 0;
float offsetFactor = 0.0f;
bool depthClip = false;
bool enabled = true;
};
struct BlendState {
int32_t alphaOperation = BlendOp::Add;
int32_t colorOperation = BlendOp::Add;
int32_t destinationAlphaBlendMode = BlendFactor::One;
int32_t destinationColorBlendMode = BlendFactor::One;
int32_t sourceAlphaBlendMode = BlendFactor::Zero;
int32_t sourceColorBlendMode = BlendFactor::Zero;
bool enabled = false;
};
struct DepthState {
int32_t compareFunction = TestFunction::Less;
bool writeEnabled = true;
bool enabled = false;
};
struct StencilState {
int32_t compareFunctionBack = TestFunction::Always;
int32_t compareFunctionFront = TestFunction::Always;
int32_t failOperationBack = ActionType::Keep;
int32_t failOperationFront = ActionType::Keep;
int32_t passOperationBack = ActionType::Keep;
int32_t passOperationFront = ActionType::Keep;
int32_t zFailOperationBack = ActionType::Keep;
int32_t zFailOperationFront = ActionType::Keep;
int32_t readMask = 1;
int32_t writeMask = 1;
int32_t reference = 0;
bool enabled = false;
};
struct TextureItem {
TString name;
Texture *texture;
int32_t binding;
int32_t flags;
};
typedef std::list<TextureItem> Textures;
public:
Material();
~Material();
bool doubleSided() const;
void setDoubleSided(bool flag);
int materialType() const;
void setMaterialType(int type);
int lightModel() const;
void setLightModel(int model);
bool wireframe() const;
void setWireframe(bool wireframe);
int proprity() const;
void setPriority(int proprity);
int uniformSize() const;
int layers() const;
virtual MaterialInstance *createInstance(SurfaceType type = SurfaceType::Static);
void loadUserData(const VariantMap &data) override;
void initInstance(MaterialInstance *instance);
protected:
void switchState(Resource::State state) override;
bool isUnloadable() override;
void addInstance(MaterialInstance *instance);
void removeInstance(MaterialInstance *instance);
private:
void loadBlendState(const VariantList &data);
void loadDepthState(const VariantList &data);
void loadStencilState(const VariantList &data);
protected:
struct UniformItem {
TString name;
Variant defaultValue;
size_t size;
size_t offset;
};
typedef std::vector<UniformItem> Uniforms;
protected:
friend class MaterialInstance;
Textures m_textures;
Uniforms m_uniforms;
std::list<MaterialInstance *> m_instances;
BlendState m_blendState;
DepthState m_depthState;
StencilState m_stencilState;
uint32_t m_uniformSize;
uint32_t m_layers;
uint32_t m_priority;
int32_t m_lightModel;
int32_t m_materialType;
bool m_doubleSided;
bool m_wireframe;
};
class ENGINE_EXPORT MaterialInstance {
public:
explicit MaterialInstance(Material *material);
virtual ~MaterialInstance();
Material *material() const;
uint32_t instanceCount() const;
void setInstanceCount(uint32_t number);
uint32_t instanceSize() const;
void setSkinSize(uint32_t size);
void setBool(const TString &name, const bool *value, int32_t count = 1);
void setInteger(const TString &name, const int32_t *value, int32_t count = 1);
void setFloat(const TString &name, const float *value, int32_t count = 1);
void setVector2(const TString &name, const Vector2 *value, int32_t count = 1);
void setVector3(const TString &name, const Vector3 *value, int32_t count = 1);
void setVector4(const TString &name, const Vector4 *value, int32_t count = 1);
void setMatrix4(const TString &name, const Matrix4 *value, int32_t count = 1);
void setTexture(const TString &name, Texture *texture);
void setTransform(Transform *transform);
void setTransform(const Matrix4 &transform);
int32_t finalPriority() const;
int32_t priority() const;
void setPriority(int32_t priority);
uint16_t surfaceType() const;
void setSurfaceType(uint16_t type);
ByteArray &rawUniformBuffer();
void setInstanceBuffer(ByteArray *buffer);
uint32_t hash() const;
protected:
void setBufferValue(const TString &name, const void *value);
Texture *texture(CommandBuffer &buffer, int32_t binding);
virtual void overrideTexture(int32_t binding, Texture *texture);
protected:
friend class Material;
std::unordered_map<int32_t, Texture *> m_textureOverride;
ByteArray m_uniformBuffer;
ByteArray *m_batchBuffer;
Material *m_material;
Transform *m_transform;
uint32_t m_instanceCount;
uint32_t m_batchesCount;
uint32_t m_hash;
uint32_t m_transformHash;
uint32_t m_priority;
uint32_t m_skinSize;
uint16_t m_surfaceType;
};
#endif // MATERIAL_H