blob: 1337860fb71ec25f42f368f44bde54831a14d27c [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef sw_VertexProcessor_hpp
16#define sw_VertexProcessor_hpp
17
18#include "Matrix.hpp"
19#include "Context.hpp"
20#include "RoutineCache.hpp"
21
22namespace sw
23{
24 struct DrawData;
25
26 struct VertexCache // FIXME: Variable size
27 {
28 void clear();
29
30 Vertex vertex[16][4];
31 unsigned int tag[16];
32
33 int drawCall;
34 };
35
36 struct VertexTask
37 {
Nicolas Capens0bac2852016-05-07 06:09:58 -040038 unsigned int vertexCount;
Alexis Hetua62a0ca2016-04-20 15:29:51 -040039 unsigned int primitiveStart;
Nicolas Capens0bac2852016-05-07 06:09:58 -040040 VertexCache vertexCache;
41 };
42
43 class VertexProcessor
44 {
45 public:
46 struct States
47 {
48 unsigned int computeHash();
49
50 uint64_t shaderID;
51
52 bool fixedFunction : 1;
53 bool textureSampling : 1;
Nicolas Capensec0936c2016-05-18 12:32:02 -040054 unsigned int positionRegister : BITS(MAX_VERTEX_OUTPUTS);
55 unsigned int pointSizeRegister : BITS(MAX_VERTEX_OUTPUTS);
Nicolas Capens0bac2852016-05-07 06:09:58 -040056
57 unsigned int vertexBlendMatrixCount : 3;
58 bool indexedVertexBlendEnable : 1;
59 bool vertexNormalActive : 1;
60 bool normalizeNormals : 1;
61 bool vertexLightingActive : 1;
62 bool diffuseActive : 1;
63 bool specularActive : 1;
64 bool vertexSpecularActive : 1;
65 unsigned int vertexLightActive : 8;
66 MaterialSource vertexDiffuseMaterialSourceActive : BITS(MATERIAL_LAST);
67 MaterialSource vertexSpecularMaterialSourceActive : BITS(MATERIAL_LAST);
68 MaterialSource vertexAmbientMaterialSourceActive : BITS(MATERIAL_LAST);
69 MaterialSource vertexEmissiveMaterialSourceActive : BITS(MATERIAL_LAST);
70 bool fogActive : 1;
71 FogMode vertexFogMode : BITS(FOG_LAST);
72 bool rangeFogActive : 1;
73 bool localViewerActive : 1;
74 bool pointSizeActive : 1;
75 bool pointScaleActive : 1;
76 bool transformFeedbackQueryEnabled : 1;
77 uint64_t transformFeedbackEnabled : 64;
Alexis Hetua62a0ca2016-04-20 15:29:51 -040078 unsigned char verticesPerPrimitive : 2; // 1 (points), 2 (lines) or 3 (triangles)
Nicolas Capens0bac2852016-05-07 06:09:58 -040079
80 bool preTransformed : 1;
81 bool superSampling : 1;
82 bool multiSampling : 1;
83
84 struct TextureState
85 {
86 TexGen texGenActive : BITS(TEXGEN_LAST);
87 unsigned char textureTransformCountActive : 3;
88 unsigned char texCoordIndexActive : 3;
89 };
90
91 TextureState textureState[8];
92
93 Sampler::State samplerState[VERTEX_TEXTURE_IMAGE_UNITS];
94
95 struct Input
96 {
97 operator bool() const // Returns true if stream contains data
98 {
99 return count != 0;
100 }
101
102 StreamType type : BITS(STREAMTYPE_LAST);
103 unsigned int count : 3;
104 bool normalized : 1;
105 };
106
107 struct Output
108 {
109 union
110 {
111 unsigned char write : 4;
112
113 struct
114 {
115 unsigned char xWrite : 1;
116 unsigned char yWrite : 1;
117 unsigned char zWrite : 1;
118 unsigned char wWrite : 1;
119 };
120 };
121
122 union
123 {
124 unsigned char clamp : 4;
125
126 struct
127 {
128 unsigned char xClamp : 1;
129 unsigned char yClamp : 1;
130 unsigned char zClamp : 1;
131 unsigned char wClamp : 1;
132 };
133 };
134 };
135
Nicolas Capensf0aef1a2016-05-18 14:44:21 -0400136 Input input[MAX_VERTEX_INPUTS];
Nicolas Capensec0936c2016-05-18 12:32:02 -0400137 Output output[MAX_VERTEX_OUTPUTS];
Nicolas Capens0bac2852016-05-07 06:09:58 -0400138 };
139
140 struct State : States
141 {
142 State();
143
144 bool operator==(const State &state) const;
145
146 unsigned int hash;
147 };
148
149 struct FixedFunction
150 {
151 float4 transformT[12][4];
152 float4 cameraTransformT[12][4];
153 float4 normalTransformT[12][4];
154 float4 textureTransform[8][4];
155
156 float4 lightPosition[8];
157 float4 lightAmbient[8];
158 float4 lightSpecular[8];
159 float4 lightDiffuse[8];
160 float4 attenuationConstant[8];
161 float4 attenuationLinear[8];
162 float4 attenuationQuadratic[8];
163 float lightRange[8];
164 float4 materialDiffuse;
165 float4 materialSpecular;
166 float materialShininess;
167 float4 globalAmbient;
168 float4 materialEmission;
169 float4 materialAmbient;
170 };
171
172 struct PointSprite
173 {
174 float4 pointSize;
175 float pointSizeMin;
176 float pointSizeMax;
177 float pointScaleA;
178 float pointScaleB;
179 float pointScaleC;
180 };
181
182 typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw);
183
184 VertexProcessor(Context *context);
185
186 virtual ~VertexProcessor();
187
Alexis Hetuc634fb62016-06-02 10:53:13 -0400188 void setInputStream(int index, const Stream &stream);
189 void resetInputStreams(bool preTransformed);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400190
Alexis Hetuc634fb62016-06-02 10:53:13 -0400191 void setFloatConstant(unsigned int index, const float value[4]);
192 void setIntegerConstant(unsigned int index, const int integer[4]);
193 void setBooleanConstant(unsigned int index, int boolean);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400194
Alexis Hetuc634fb62016-06-02 10:53:13 -0400195 void setUniformBuffer(int index, sw::Resource* uniformBuffer, int offset);
196 void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400197
Alexis Hetuc634fb62016-06-02 10:53:13 -0400198 void setTransformFeedbackBuffer(int index, sw::Resource* transformFeedbackBuffer, int offset, unsigned int reg, unsigned int row, unsigned int col, size_t stride);
199 void lockTransformFeedbackBuffers(byte** t, unsigned int* v, unsigned int* r, unsigned int* c, unsigned int* s, sw::Resource* transformFeedbackBuffers[]);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400200
201 // Transformations
Alexis Hetuc634fb62016-06-02 10:53:13 -0400202 void setModelMatrix(const Matrix &M, int i = 0);
203 void setViewMatrix(const Matrix &V);
204 void setBaseMatrix(const Matrix &B);
205 void setProjectionMatrix(const Matrix &P);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400206
207 // Lighting
Alexis Hetuc634fb62016-06-02 10:53:13 -0400208 void setLightingEnable(bool lightingEnable);
209 void setLightEnable(unsigned int light, bool lightEnable);
210 void setSpecularEnable(bool specularEnable);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400211
Alexis Hetuc634fb62016-06-02 10:53:13 -0400212 void setGlobalAmbient(const Color<float> &globalAmbient);
213 void setLightPosition(unsigned int light, const Point &lightPosition);
214 void setLightViewPosition(unsigned int light, const Point &lightPosition);
215 void setLightDiffuse(unsigned int light, const Color<float> &lightDiffuse);
216 void setLightSpecular(unsigned int light, const Color<float> &lightSpecular);
217 void setLightAmbient(unsigned int light, const Color<float> &lightAmbient);
218 void setLightAttenuation(unsigned int light, float constant, float linear, float quadratic);
219 void setLightRange(unsigned int light, float lightRange);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400220
Alexis Hetuc634fb62016-06-02 10:53:13 -0400221 void setInstanceID(int instanceID);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400222
Alexis Hetuc634fb62016-06-02 10:53:13 -0400223 void setFogEnable(bool fogEnable);
224 void setVertexFogMode(FogMode fogMode);
225 void setRangeFogEnable(bool enable);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400226
Alexis Hetuc634fb62016-06-02 10:53:13 -0400227 void setColorVertexEnable(bool colorVertexEnable);
228 void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource);
229 void setSpecularMaterialSource(MaterialSource specularMaterialSource);
230 void setAmbientMaterialSource(MaterialSource ambientMaterialSource);
231 void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400232
Alexis Hetuc634fb62016-06-02 10:53:13 -0400233 void setMaterialEmission(const Color<float> &emission);
234 void setMaterialAmbient(const Color<float> &materialAmbient);
235 void setMaterialDiffuse(const Color<float> &diffuseColor);
236 void setMaterialSpecular(const Color<float> &specularColor);
237 void setMaterialShininess(float specularPower);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400238
Alexis Hetuc634fb62016-06-02 10:53:13 -0400239 void setIndexedVertexBlendEnable(bool indexedVertexBlendEnable);
240 void setVertexBlendMatrixCount(unsigned int vertexBlendMatrixCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400241
Alexis Hetuc634fb62016-06-02 10:53:13 -0400242 void setTextureWrap(unsigned int stage, int mask);
243 void setTexGen(unsigned int stage, TexGen texGen);
244 void setLocalViewer(bool localViewer);
245 void setNormalizeNormals(bool normalizeNormals);
246 void setTextureMatrix(int stage, const Matrix &T);
247 void setTextureTransform(int stage, int count, bool project);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400248
Alexis Hetuc634fb62016-06-02 10:53:13 -0400249 void setTextureFilter(unsigned int sampler, FilterType textureFilter);
250 void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter);
251 void setGatherEnable(unsigned int sampler, bool enable);
252 void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode);
253 void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode);
254 void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode);
255 void setReadSRGB(unsigned int sampler, bool sRGB);
256 void setMipmapLOD(unsigned int sampler, float bias);
257 void setBorderColor(unsigned int sampler, const Color<float> &borderColor);
258 void setMaxAnisotropy(unsigned int stage, float maxAnisotropy);
259 void setSwizzleR(unsigned int sampler, SwizzleType swizzleR);
260 void setSwizzleG(unsigned int sampler, SwizzleType swizzleG);
261 void setSwizzleB(unsigned int sampler, SwizzleType swizzleB);
262 void setSwizzleA(unsigned int sampler, SwizzleType swizzleA);
263 void setBaseLevel(unsigned int sampler, int baseLevel);
264 void setMaxLevel(unsigned int sampler, int maxLevel);
265 void setMinLod(unsigned int sampler, float minLod);
266 void setMaxLod(unsigned int sampler, float maxLod);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400267
Alexis Hetuc634fb62016-06-02 10:53:13 -0400268 void setPointSize(float pointSize);
269 void setPointSizeMin(float pointSizeMin);
270 void setPointSizeMax(float pointSizeMax);
271 void setPointScaleA(float pointScaleA);
272 void setPointScaleB(float pointScaleB);
273 void setPointScaleC(float pointScaleC);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400274
Alexis Hetuc634fb62016-06-02 10:53:13 -0400275 void setTransformFeedbackQueryEnabled(bool enable);
276 void enableTransformFeedback(uint64_t enable);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400277
278 protected:
279 const Matrix &getModelTransform(int i);
280 const Matrix &getViewTransform();
281
Alexis Hetua62a0ca2016-04-20 15:29:51 -0400282 const State update(DrawType drawType);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400283 Routine *routine(const State &state);
284
285 bool isFixedFunction();
286 void setRoutineCacheSize(int cacheSize);
287
288 // Shader constants
289 float4 c[VERTEX_UNIFORM_VECTORS + 1]; // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
290 int4 i[16];
291 bool b[16];
292
293 PointSprite point;
294 FixedFunction ff;
295
296 private:
297 struct UniformBufferInfo
298 {
299 UniformBufferInfo();
300
301 Resource* buffer;
302 int offset;
303 };
304 UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS];
305
306 struct TransformFeedbackInfo
307 {
308 TransformFeedbackInfo();
309
310 Resource* buffer;
311 int offset;
312 int reg;
313 int row;
314 int col;
315 size_t stride;
316 };
317 TransformFeedbackInfo transformFeedbackInfo[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
318
319 void updateTransform();
320 void setTransform(const Matrix &M, int i);
321 void setCameraTransform(const Matrix &M, int i);
322 void setNormalTransform(const Matrix &M, int i);
323
324 Context *const context;
325
326 RoutineCache<State> *routineCache;
327
328 protected:
329 Matrix M[12]; // Model/Geometry/World matrix
330 Matrix V; // View/Camera/Eye matrix
331 Matrix B; // Base matrix
332 Matrix P; // Projection matrix
333 Matrix PB; // P * B
334 Matrix PBV; // P * B * V
335 Matrix PBVM[12]; // P * B * V * M
336
337 // Update hierarchy
338 bool updateMatrix;
339 bool updateModelMatrix[12];
340 bool updateViewMatrix;
341 bool updateBaseMatrix;
342 bool updateProjectionMatrix;
343 bool updateLighting;
344 };
345}
346
347#endif // sw_VertexProcessor_hpp