forked from google/swiftshader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertexProcessor.hpp
352 lines (290 loc) · 11.3 KB
/
VertexProcessor.hpp
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
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef sw_VertexProcessor_hpp
#define sw_VertexProcessor_hpp
#include "Matrix.hpp"
#include "Context.hpp"
#include "RoutineCache.hpp"
#include "Shader/VertexShader.hpp"
namespace sw
{
struct DrawData;
struct VertexCache // FIXME: Variable size
{
void clear();
Vertex vertex[16][4];
unsigned int tag[16];
int drawCall;
};
struct VertexTask
{
unsigned int vertexCount;
unsigned int primitiveStart;
VertexCache vertexCache;
};
class VertexProcessor
{
public:
struct States
{
unsigned int computeHash();
uint64_t shaderID;
bool fixedFunction : 1; // TODO: Eliminate by querying shader.
bool textureSampling : 1; // TODO: Eliminate by querying shader.
unsigned int positionRegister : BITS(MAX_VERTEX_OUTPUTS); // TODO: Eliminate by querying shader.
unsigned int pointSizeRegister : BITS(MAX_VERTEX_OUTPUTS); // TODO: Eliminate by querying shader.
unsigned int vertexBlendMatrixCount : 3;
bool indexedVertexBlendEnable : 1;
bool vertexNormalActive : 1;
bool normalizeNormals : 1;
bool vertexLightingActive : 1;
bool diffuseActive : 1;
bool specularActive : 1;
bool vertexSpecularActive : 1;
unsigned int vertexLightActive : 8;
MaterialSource vertexDiffuseMaterialSourceActive : BITS(MATERIAL_LAST);
MaterialSource vertexSpecularMaterialSourceActive : BITS(MATERIAL_LAST);
MaterialSource vertexAmbientMaterialSourceActive : BITS(MATERIAL_LAST);
MaterialSource vertexEmissiveMaterialSourceActive : BITS(MATERIAL_LAST);
bool fogActive : 1;
FogMode vertexFogMode : BITS(FOG_LAST);
bool rangeFogActive : 1;
bool localViewerActive : 1;
bool pointSizeActive : 1;
bool pointScaleActive : 1;
bool transformFeedbackQueryEnabled : 1;
uint64_t transformFeedbackEnabled : 64;
unsigned char verticesPerPrimitive : 2; // 1 (points), 2 (lines) or 3 (triangles)
bool preTransformed : 1;
bool superSampling : 1;
bool multiSampling : 1;
struct TextureState
{
TexGen texGenActive : BITS(TEXGEN_LAST);
unsigned char textureTransformCountActive : 3;
unsigned char texCoordIndexActive : 3;
};
TextureState textureState[8];
Sampler::State sampler[VERTEX_TEXTURE_IMAGE_UNITS];
struct Input
{
operator bool() const // Returns true if stream contains data
{
return count != 0;
}
StreamType type : BITS(STREAMTYPE_LAST);
unsigned int count : 3;
bool normalized : 1;
unsigned int attribType : BITS(VertexShader::ATTRIBTYPE_LAST);
};
struct Output
{
union
{
unsigned char write : 4;
struct
{
unsigned char xWrite : 1;
unsigned char yWrite : 1;
unsigned char zWrite : 1;
unsigned char wWrite : 1;
};
};
union
{
unsigned char clamp : 4;
struct
{
unsigned char xClamp : 1;
unsigned char yClamp : 1;
unsigned char zClamp : 1;
unsigned char wClamp : 1;
};
};
};
Input input[MAX_VERTEX_INPUTS];
Output output[MAX_VERTEX_OUTPUTS];
};
struct State : States
{
State();
bool operator==(const State &state) const;
unsigned int hash;
};
struct FixedFunction
{
float4 transformT[12][4];
float4 cameraTransformT[12][4];
float4 normalTransformT[12][4];
float4 textureTransform[8][4];
float4 lightPosition[8];
float4 lightAmbient[8];
float4 lightSpecular[8];
float4 lightDiffuse[8];
float4 attenuationConstant[8];
float4 attenuationLinear[8];
float4 attenuationQuadratic[8];
float lightRange[8];
float4 materialDiffuse;
float4 materialSpecular;
float materialShininess;
float4 globalAmbient;
float4 materialEmission;
float4 materialAmbient;
};
struct PointSprite
{
float4 pointSize;
float pointSizeMin;
float pointSizeMax;
float pointScaleA;
float pointScaleB;
float pointScaleC;
};
typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw);
VertexProcessor(Context *context);
virtual ~VertexProcessor();
void setInputStream(int index, const Stream &stream);
void resetInputStreams(bool preTransformed);
void setFloatConstant(unsigned int index, const float value[4]);
void setIntegerConstant(unsigned int index, const int integer[4]);
void setBooleanConstant(unsigned int index, int boolean);
void setUniformBuffer(int index, sw::Resource* uniformBuffer, int offset);
void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]);
void setTransformFeedbackBuffer(int index, sw::Resource* transformFeedbackBuffer, int offset, unsigned int reg, unsigned int row, unsigned int col, unsigned int stride);
void lockTransformFeedbackBuffers(byte** t, unsigned int* v, unsigned int* r, unsigned int* c, unsigned int* s, sw::Resource* transformFeedbackBuffers[]);
// Transformations
void setModelMatrix(const Matrix &M, int i = 0);
void setViewMatrix(const Matrix &V);
void setBaseMatrix(const Matrix &B);
void setProjectionMatrix(const Matrix &P);
// Lighting
void setLightingEnable(bool lightingEnable);
void setLightEnable(unsigned int light, bool lightEnable);
void setSpecularEnable(bool specularEnable);
void setGlobalAmbient(const Color<float> &globalAmbient);
void setLightPosition(unsigned int light, const Point &lightPosition);
void setLightViewPosition(unsigned int light, const Point &lightPosition);
void setLightDiffuse(unsigned int light, const Color<float> &lightDiffuse);
void setLightSpecular(unsigned int light, const Color<float> &lightSpecular);
void setLightAmbient(unsigned int light, const Color<float> &lightAmbient);
void setLightAttenuation(unsigned int light, float constant, float linear, float quadratic);
void setLightRange(unsigned int light, float lightRange);
void setInstanceID(int instanceID);
void setFogEnable(bool fogEnable);
void setVertexFogMode(FogMode fogMode);
void setRangeFogEnable(bool enable);
void setColorVertexEnable(bool colorVertexEnable);
void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource);
void setSpecularMaterialSource(MaterialSource specularMaterialSource);
void setAmbientMaterialSource(MaterialSource ambientMaterialSource);
void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource);
void setMaterialEmission(const Color<float> &emission);
void setMaterialAmbient(const Color<float> &materialAmbient);
void setMaterialDiffuse(const Color<float> &diffuseColor);
void setMaterialSpecular(const Color<float> &specularColor);
void setMaterialShininess(float specularPower);
void setIndexedVertexBlendEnable(bool indexedVertexBlendEnable);
void setVertexBlendMatrixCount(unsigned int vertexBlendMatrixCount);
void setTextureWrap(unsigned int stage, int mask);
void setTexGen(unsigned int stage, TexGen texGen);
void setLocalViewer(bool localViewer);
void setNormalizeNormals(bool normalizeNormals);
void setTextureMatrix(int stage, const Matrix &T);
void setTextureTransform(int stage, int count, bool project);
void setTextureFilter(unsigned int sampler, FilterType textureFilter);
void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter);
void setGatherEnable(unsigned int sampler, bool enable);
void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode);
void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode);
void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode);
void setReadSRGB(unsigned int sampler, bool sRGB);
void setMipmapLOD(unsigned int sampler, float bias);
void setBorderColor(unsigned int sampler, const Color<float> &borderColor);
void setMaxAnisotropy(unsigned int stage, float maxAnisotropy);
void setHighPrecisionFiltering(unsigned int sampler, bool highPrecisionFiltering);
void setSwizzleR(unsigned int sampler, SwizzleType swizzleR);
void setSwizzleG(unsigned int sampler, SwizzleType swizzleG);
void setSwizzleB(unsigned int sampler, SwizzleType swizzleB);
void setSwizzleA(unsigned int sampler, SwizzleType swizzleA);
void setCompareFunc(unsigned int sampler, CompareFunc compare);
void setBaseLevel(unsigned int sampler, int baseLevel);
void setMaxLevel(unsigned int sampler, int maxLevel);
void setMinLod(unsigned int sampler, float minLod);
void setMaxLod(unsigned int sampler, float maxLod);
void setSyncRequired(unsigned int sampler, bool isSincRequired);
void setPointSize(float pointSize);
void setPointSizeMin(float pointSizeMin);
void setPointSizeMax(float pointSizeMax);
void setPointScaleA(float pointScaleA);
void setPointScaleB(float pointScaleB);
void setPointScaleC(float pointScaleC);
void setTransformFeedbackQueryEnabled(bool enable);
void enableTransformFeedback(uint64_t enable);
protected:
const Matrix &getModelTransform(int i);
const Matrix &getViewTransform();
const State update(DrawType drawType);
Routine *routine(const State &state);
bool isFixedFunction();
void setRoutineCacheSize(int cacheSize);
// Shader constants
float4 c[VERTEX_UNIFORM_VECTORS + 1]; // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
int4 i[16];
bool b[16];
PointSprite point;
FixedFunction ff;
private:
struct UniformBufferInfo
{
UniformBufferInfo();
Resource* buffer;
int offset;
};
UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS];
struct TransformFeedbackInfo
{
TransformFeedbackInfo();
Resource* buffer;
unsigned int offset;
unsigned int reg;
unsigned int row;
unsigned int col;
unsigned int stride;
};
TransformFeedbackInfo transformFeedbackInfo[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
void updateTransform();
void setTransform(const Matrix &M, int i);
void setCameraTransform(const Matrix &M, int i);
void setNormalTransform(const Matrix &M, int i);
Context *const context;
RoutineCache<State> *routineCache;
protected:
Matrix M[12]; // Model/Geometry/World matrix
Matrix V; // View/Camera/Eye matrix
Matrix B; // Base matrix
Matrix P; // Projection matrix
Matrix PB; // P * B
Matrix PBV; // P * B * V
Matrix PBVM[12]; // P * B * V * M
// Update hierarchy
bool updateMatrix;
bool updateModelMatrix[12];
bool updateViewMatrix;
bool updateBaseMatrix;
bool updateProjectionMatrix;
bool updateLighting;
};
}
#endif // sw_VertexProcessor_hpp