/
ishaderlayer.h
410 lines (341 loc) · 11.3 KB
/
ishaderlayer.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#pragma once
#include "Texture.h"
#include "ishaderexpression.h"
#include <memory>
#include <vector>
#include <string>
#include "math/Vector2.h"
#include "math/Vector4.h"
#include "math/Matrix4.h"
#include "render/Colour4.h"
class IRenderEntity;
// Texture repeat types
enum ClampType
{
CLAMP_REPEAT = 1 << 0, // default = no clamping
CLAMP_NOREPEAT = 1 << 1, // "clamp"
CLAMP_ZEROCLAMP = 1 << 2, // "zeroclamp"
CLAMP_ALPHAZEROCLAMP = 1 << 3, // "alphazeroclamp"
};
/**
* \brief Representation of a GL blend function.
*
* A GL blend function consists of two GLenums representing the operations that
* should be performed on the source and destination pixel colours respectively,
* before the two results are added together into a final pixel colour.
*/
class BlendFunc
{
public:
// Source pixel function
GLenum src;
// Destination pixel function
GLenum dest;
// Constructor
BlendFunc(GLenum s, GLenum d)
: src(s), dest(d)
{ }
};
/**
* \brief A single layer of a material shader.
*
* Each shader layer contains an image texture, a blend mode (e.g. add,
* modulate) and various other data.
*/
class IShaderLayer
{
public:
using Ptr = std::shared_ptr<IShaderLayer>;
/// Enumeration of layer types.
enum Type
{
DIFFUSE,
BUMP,
SPECULAR,
BLEND
};
// Stage flags
enum Flags
{
FLAG_IGNORE_ALPHATEST = 1 << 0,
FLAG_FILTER_NEAREST = 1 << 1,
FLAG_FILTER_LINEAR = 1 << 2,
FLAG_HIGHQUALITY = 1 << 3, // "uncompressed"
FLAG_FORCE_HIGHQUALITY = 1 << 4,
FLAG_NO_PICMIP = 1 << 5,
FLAG_MASK_RED = 1 << 6,
FLAG_MASK_GREEN = 1 << 7,
FLAG_MASK_BLUE = 1 << 8,
FLAG_MASK_ALPHA = 1 << 9,
FLAG_MASK_DEPTH = 1 << 10,
FLAG_IGNORE_DEPTH = 1 << 11, // use depthfunc always
};
enum TexGenType
{
TEXGEN_NORMAL = 1 << 0,
TEXGEN_REFLECT = 1 << 1,
TEXGEN_SKYBOX = 1 << 2,
TEXGEN_WOBBLESKY = 1 << 3,
TEXGEN_SCREEN = 1 << 4, // screen aligned, for mirrorRenders and screen space temporaries
};
enum ParseFlags
{
PF_HasTexGenKeyword = 1 << 1, // texgen has been specified
PF_HasNoclampKeyword = 1 << 2, // noclamp has been specified
PF_HasColoredKeyword = 1 << 3, // colored has been specified
};
// Expression slot selector
struct Expression
{
enum Slot
{
AlphaTest = 0,
Condition,
TexGenParam1,
TexGenParam2,
TexGenParam3,
ColourRed,
ColourGreen,
ColourBlue,
ColourAlpha,
TextureMatrixRow0Col0,
TextureMatrixRow0Col1,
TextureMatrixRow0Col2,
TextureMatrixRow1Col0,
TextureMatrixRow1Col1,
TextureMatrixRow1Col2,
NumExpressionSlots
};
};
// The various texture transformations
enum class TransformType
{
Translate,
Scale,
CenterScale,
Shear,
Rotate,
};
// A texture transformation consists of a type and two arguments at most
struct Transformation
{
TransformType type;
shaders::IShaderExpression::Ptr expression1;
shaders::IShaderExpression::Ptr expression2;
};
/**
* \brief
* Destructor
*/
virtual ~IShaderLayer() {}
/// Return the layer type.
virtual Type getType() const = 0;
/**
* \brief
* Return the Texture object corresponding to this layer (may be NULL).
*/
virtual TexturePtr getTexture() const = 0;
/**
* Evaluate all shader expressions used in this stage. Call this once (each frame)
* before requesting things like getAlphaTest(), getColour() or isVisible()
*/
virtual void evaluateExpressions(std::size_t time) = 0;
/**
* Like evaluateExpressions(time), but with an additional renderentity as argument
* to give this stage the ability to resolve parm0..parm11 values.
*/
virtual void evaluateExpressions(std::size_t time, const IRenderEntity& entity) = 0;
// Returns the requested expression
virtual shaders::IShaderExpression::Ptr getExpression(Expression::Slot slot) = 0;
/**
* The flags set on this stage.
*/
virtual int getStageFlags() const = 0;
/**
* Each stage can have its own clamp type, overriding the per-material one.
*/
virtual ClampType getClampType() const = 0;
/**
* Returns the texgen type: normal, reflect, skybox, etc.
* Use getTexGenParam(i) to retrieve the wobblesky parameters [0..2]
*/
virtual TexGenType getTexGenType() const = 0;
/**
* TexGen type wobblesky has 3 parameters, get them here, with index in [0..2]
*/
virtual float getTexGenParam(std::size_t index) const = 0;
// The expressions used to calculate the tex gen params. Index in [0..2]
virtual shaders::IShaderExpression::Ptr getTexGenExpression(std::size_t index) const = 0;
/**
* \brief
* Return the GL blend function for this layer.
*
* Only layers of type BLEND use a BlendFunc. Layers of type DIFFUSE, BUMP
* and SPECULAR do not use blend functions.
*/
virtual BlendFunc getBlendFunc() const = 0;
// Get the blend string as defined in the material def, e.g. "add" or "gl_one, gl_zero"
virtual const std::pair<std::string, std::string>& getBlendFuncStrings() const = 0;
/**
* \brief
* Multiplicative layer colour (set with "red 0.6", "green 0.2" etc)
*/
virtual Colour4 getColour() const = 0;
// An enum used to select which colour components are affected by an operation
enum ColourComponentSelector
{
COMP_RED, // red only
COMP_GREEN, // green only
COMP_BLUE, // blue only
COMP_ALPHA, // alpha only
COMP_RGB, // red, green and blue
COMP_RGBA, // all: red, greeb, blue, alpha
};
// Returns the expression to calculate the RGBA vertex colour values
virtual const shaders::IShaderExpression::Ptr& getColourExpression(ColourComponentSelector component) const = 0;
/**
* \brief
* Vertex colour blend mode enumeration.
*/
enum VertexColourMode
{
VERTEX_COLOUR_NONE, // no vertex colours
VERTEX_COLOUR_MULTIPLY, // "vertexColor"
VERTEX_COLOUR_INVERSE_MULTIPLY // "inverseVertexColor"
};
/**
* \brief
* Get the vertex colour mode for this layer.
*/
virtual VertexColourMode getVertexColourMode() const = 0;
enum class MapType
{
Map, // regular map
CubeMap, // corresponds to CUBE_MAP_OBJECT
CameraCubeMap, // corresponds to CUBE_MAP_CAMERA
VideoMap,
SoundMap,
MirrorRenderMap,
RemoteRenderMap,
};
// Get the map type used by this stage
virtual MapType getMapType() const = 0;
/**
* \brief
* Enumeration of cube map modes for this layer.
*/
enum CubeMapMode
{
CUBE_MAP_NONE,
CUBE_MAP_CAMERA, // cube map in camera space ("cameraCubeMap")
CUBE_MAP_OBJECT // cube map in object space ("cubeMap")
};
/**
* \brief
* Get the cube map mode for this layer.
*/
virtual CubeMapMode getCubeMapMode() const = 0;
/**
* Returns the dimensions specifying the map size for
* stages using the "mirrorRenderMap", "remoteRenderMap" keywords.
*/
virtual const Vector2& getRenderMapSize() const = 0;
// Adds a transformation at the end of the list of existing transforms in this stage
virtual void appendTransformation(const Transformation& transform) = 0;
// The list of transformations defined in this stage, in the order they appear in the declaration
virtual const std::vector<Transformation>& getTransformations() = 0;
// Returns the current texture matrix
virtual Matrix4 getTextureTransform() = 0;
// Returns true if this layer has an alphatest expression defined
virtual bool hasAlphaTest() const = 0;
/**
* \brief
* Get the alpha test value for this layer.
*
* \return
* The alpha test value, within (0..1] if it is set. If no alpha test
* value is set, 0 will be returned.
*/
virtual float getAlphaTest() const = 0;
// Returns the expression used to calculate the alpha test value
virtual const shaders::IShaderExpression::Ptr& getAlphaTestExpression() const = 0;
/**
* Whether this stage is active. Unconditional stages always return true,
* conditional ones return the result of the most recent condition expression evaluation.
*/
virtual bool isVisible() const = 0;
// Returns the if-expression used to evaluate this stage's visibility, or null if none defined
virtual const shaders::IShaderExpression::Ptr& getConditionExpression() const = 0;
/**
* Returns the name of this stage's fragment program.
*/
virtual const std::string& getVertexProgram()const = 0;
/**
* Returns the name of this stage's fragment program.
*/
virtual const std::string& getFragmentProgram() const = 0;
// The number of defined vertex parameters
virtual int getNumVertexParms() const = 0;
/**
* Returns the 4 parameter values for the vertexParm index <parm>.
*/
virtual Vector4 getVertexParmValue(int parm) const = 0;
// A vertex parm has an index and 4 expressions at most
struct VertexParm
{
VertexParm() :
index(-1) // a negative index indicates this parm has not been defined in the stage
{}
int index;
shaders::IShaderExpression::Ptr expressions[4];
};
// Returns the vertex parameter with the given index [0..3]
virtual const VertexParm& getVertexParm(int index) const = 0;
/**
* Returns the number of fragment maps in this stage.
*/
virtual std::size_t getNumFragmentMaps() const = 0;
struct FragmentMap
{
FragmentMap() :
index(-1)
{}
int index;
std::vector<std::string> options;
shaders::IMapExpression::Ptr map;
};
virtual const FragmentMap& getFragmentMap(int index) const = 0;
/**
* Returns the fragment map image with the given index.
*/
virtual TexturePtr getFragmentMapTexture(int index) const = 0;
/**
* Stage-specific polygon offset, overriding the "global" one defined on the material.
*/
virtual float getPrivatePolygonOffset() const = 0;
// If this stage is referring to a single image file, this will return
// the VFS path to it with the file extension removed.
// If this layer doesn't refer to a single image file, an empty string is returned
virtual std::string getMapImageFilename() const = 0;
// The map expression used to generate/define the texture of this stage
virtual shaders::IMapExpression::Ptr getMapExpression() const = 0;
// Parser information, to reconstruct the use of certain keywords
virtual int getParseFlags() const = 0;
};
/**
* \brief
* Vector of IShaderLayer pointers.
*/
typedef std::vector<IShaderLayer::Ptr> IShaderLayerVector;
// Interface extension to IShaderLayer, offering editing functions
class IEditableShaderLayer :
public IShaderLayer
{
public:
using Ptr = std::shared_ptr<IEditableShaderLayer>;
virtual ~IEditableShaderLayer() {}
// Set the alpha test expression from the given string
virtual void setAlphaTestExpressionFromString(const std::string& expression) = 0;
// Update the "map" expression of this stage
virtual void setMapExpressionFromString(const std::string& expression) = 0;
};