blob: 7321e699122d3363d6eb0823fe2f86a23f6b5633 [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"
Alexis Hetub7508b82016-09-22 15:36:45 -040021#include "Shader/VertexShader.hpp"
Nicolas Capens0bac2852016-05-07 06:09:58 -040022
23namespace sw
24{
25 struct DrawData;
26
27 struct VertexCache // FIXME: Variable size
28 {
29 void clear();
30
31 Vertex vertex[16][4];
32 unsigned int tag[16];
33
34 int drawCall;
35 };
36
37 struct VertexTask
38 {
Nicolas Capens0bac2852016-05-07 06:09:58 -040039 unsigned int vertexCount;
Alexis Hetua62a0ca2016-04-20 15:29:51 -040040 unsigned int primitiveStart;
Nicolas Capens0bac2852016-05-07 06:09:58 -040041 VertexCache vertexCache;
42 };
43
44 class VertexProcessor
45 {
46 public:
Nicolas Capens92eb0412019-08-20 14:13:17 -040047 struct States : Memset<States>
Nicolas Capens0bac2852016-05-07 06:09:58 -040048 {
Nicolas Capens92eb0412019-08-20 14:13:17 -040049 States() : Memset(this, 0) {}
50
51 uint32_t computeHash();
Nicolas Capens0bac2852016-05-07 06:09:58 -040052
53 uint64_t shaderID;
54
Nicolas Capensac6d5052018-01-05 15:34:00 -050055 bool fixedFunction : 1; // TODO: Eliminate by querying shader.
56 bool textureSampling : 1; // TODO: Eliminate by querying shader.
57 unsigned int positionRegister : BITS(MAX_VERTEX_OUTPUTS); // TODO: Eliminate by querying shader.
58 unsigned int pointSizeRegister : BITS(MAX_VERTEX_OUTPUTS); // TODO: Eliminate by querying shader.
Nicolas Capens0bac2852016-05-07 06:09:58 -040059
60 unsigned int vertexBlendMatrixCount : 3;
61 bool indexedVertexBlendEnable : 1;
62 bool vertexNormalActive : 1;
63 bool normalizeNormals : 1;
64 bool vertexLightingActive : 1;
65 bool diffuseActive : 1;
66 bool specularActive : 1;
67 bool vertexSpecularActive : 1;
68 unsigned int vertexLightActive : 8;
69 MaterialSource vertexDiffuseMaterialSourceActive : BITS(MATERIAL_LAST);
70 MaterialSource vertexSpecularMaterialSourceActive : BITS(MATERIAL_LAST);
71 MaterialSource vertexAmbientMaterialSourceActive : BITS(MATERIAL_LAST);
72 MaterialSource vertexEmissiveMaterialSourceActive : BITS(MATERIAL_LAST);
73 bool fogActive : 1;
74 FogMode vertexFogMode : BITS(FOG_LAST);
75 bool rangeFogActive : 1;
76 bool localViewerActive : 1;
77 bool pointSizeActive : 1;
78 bool pointScaleActive : 1;
79 bool transformFeedbackQueryEnabled : 1;
80 uint64_t transformFeedbackEnabled : 64;
Alexis Hetua62a0ca2016-04-20 15:29:51 -040081 unsigned char verticesPerPrimitive : 2; // 1 (points), 2 (lines) or 3 (triangles)
Nicolas Capens0bac2852016-05-07 06:09:58 -040082
83 bool preTransformed : 1;
84 bool superSampling : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -040085
86 struct TextureState
87 {
88 TexGen texGenActive : BITS(TEXGEN_LAST);
89 unsigned char textureTransformCountActive : 3;
90 unsigned char texCoordIndexActive : 3;
91 };
92
93 TextureState textureState[8];
94
Nicolas Capensf878d502017-11-06 15:29:46 -050095 Sampler::State sampler[VERTEX_TEXTURE_IMAGE_UNITS];
Nicolas Capens0bac2852016-05-07 06:09:58 -040096
97 struct Input
98 {
99 operator bool() const // Returns true if stream contains data
100 {
101 return count != 0;
102 }
103
104 StreamType type : BITS(STREAMTYPE_LAST);
105 unsigned int count : 3;
106 bool normalized : 1;
Alexis Hetub7508b82016-09-22 15:36:45 -0400107 unsigned int attribType : BITS(VertexShader::ATTRIBTYPE_LAST);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400108 };
109
110 struct Output
111 {
112 union
113 {
114 unsigned char write : 4;
115
116 struct
117 {
118 unsigned char xWrite : 1;
119 unsigned char yWrite : 1;
120 unsigned char zWrite : 1;
121 unsigned char wWrite : 1;
122 };
123 };
124
125 union
126 {
127 unsigned char clamp : 4;
128
129 struct
130 {
131 unsigned char xClamp : 1;
132 unsigned char yClamp : 1;
133 unsigned char zClamp : 1;
134 unsigned char wClamp : 1;
135 };
136 };
137 };
138
Nicolas Capensf0aef1a2016-05-18 14:44:21 -0400139 Input input[MAX_VERTEX_INPUTS];
Nicolas Capensec0936c2016-05-18 12:32:02 -0400140 Output output[MAX_VERTEX_OUTPUTS];
Nicolas Capens0bac2852016-05-07 06:09:58 -0400141 };
142
143 struct State : States
144 {
Nicolas Capens0bac2852016-05-07 06:09:58 -0400145 bool operator==(const State &state) const;
146
Nicolas Capens92eb0412019-08-20 14:13:17 -0400147 uint32_t hash;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400148 };
149
150 struct FixedFunction
151 {
152 float4 transformT[12][4];
153 float4 cameraTransformT[12][4];
154 float4 normalTransformT[12][4];
155 float4 textureTransform[8][4];
156
157 float4 lightPosition[8];
158 float4 lightAmbient[8];
159 float4 lightSpecular[8];
160 float4 lightDiffuse[8];
161 float4 attenuationConstant[8];
162 float4 attenuationLinear[8];
163 float4 attenuationQuadratic[8];
164 float lightRange[8];
165 float4 materialDiffuse;
166 float4 materialSpecular;
167 float materialShininess;
168 float4 globalAmbient;
169 float4 materialEmission;
170 float4 materialAmbient;
171 };
172
173 struct PointSprite
174 {
175 float4 pointSize;
176 float pointSizeMin;
177 float pointSizeMax;
178 float pointScaleA;
179 float pointScaleB;
180 float pointScaleC;
181 };
182
183 typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw);
184
185 VertexProcessor(Context *context);
186
187 virtual ~VertexProcessor();
188
Nicolas Capens532770b2021-01-21 13:34:33 -0500189 void *operator new(size_t size);
190 void operator delete(void *mem);
191
Alexis Hetuc634fb62016-06-02 10:53:13 -0400192 void setInputStream(int index, const Stream &stream);
193 void resetInputStreams(bool preTransformed);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400194
Alexis Hetuc634fb62016-06-02 10:53:13 -0400195 void setFloatConstant(unsigned int index, const float value[4]);
196 void setIntegerConstant(unsigned int index, const int integer[4]);
197 void setBooleanConstant(unsigned int index, int boolean);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400198
Alexis Hetuc634fb62016-06-02 10:53:13 -0400199 void setUniformBuffer(int index, sw::Resource* uniformBuffer, int offset);
200 void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400201
Alexis Hetu1d672442016-06-23 11:24:00 -0400202 void setTransformFeedbackBuffer(int index, sw::Resource* transformFeedbackBuffer, int offset, unsigned int reg, unsigned int row, unsigned int col, unsigned int stride);
Alexis Hetuc634fb62016-06-02 10:53:13 -0400203 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 -0400204
205 // Transformations
Alexis Hetuc634fb62016-06-02 10:53:13 -0400206 void setModelMatrix(const Matrix &M, int i = 0);
207 void setViewMatrix(const Matrix &V);
208 void setBaseMatrix(const Matrix &B);
209 void setProjectionMatrix(const Matrix &P);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400210
211 // Lighting
Alexis Hetuc634fb62016-06-02 10:53:13 -0400212 void setLightingEnable(bool lightingEnable);
213 void setLightEnable(unsigned int light, bool lightEnable);
214 void setSpecularEnable(bool specularEnable);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400215
Alexis Hetuc634fb62016-06-02 10:53:13 -0400216 void setGlobalAmbient(const Color<float> &globalAmbient);
217 void setLightPosition(unsigned int light, const Point &lightPosition);
218 void setLightViewPosition(unsigned int light, const Point &lightPosition);
219 void setLightDiffuse(unsigned int light, const Color<float> &lightDiffuse);
220 void setLightSpecular(unsigned int light, const Color<float> &lightSpecular);
221 void setLightAmbient(unsigned int light, const Color<float> &lightAmbient);
222 void setLightAttenuation(unsigned int light, float constant, float linear, float quadratic);
223 void setLightRange(unsigned int light, float lightRange);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400224
Alexis Hetuc634fb62016-06-02 10:53:13 -0400225 void setInstanceID(int instanceID);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400226
Alexis Hetuc634fb62016-06-02 10:53:13 -0400227 void setFogEnable(bool fogEnable);
228 void setVertexFogMode(FogMode fogMode);
229 void setRangeFogEnable(bool enable);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400230
Alexis Hetuc634fb62016-06-02 10:53:13 -0400231 void setColorVertexEnable(bool colorVertexEnable);
232 void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource);
233 void setSpecularMaterialSource(MaterialSource specularMaterialSource);
234 void setAmbientMaterialSource(MaterialSource ambientMaterialSource);
235 void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400236
Alexis Hetuc634fb62016-06-02 10:53:13 -0400237 void setMaterialEmission(const Color<float> &emission);
238 void setMaterialAmbient(const Color<float> &materialAmbient);
239 void setMaterialDiffuse(const Color<float> &diffuseColor);
240 void setMaterialSpecular(const Color<float> &specularColor);
241 void setMaterialShininess(float specularPower);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400242
Alexis Hetuc634fb62016-06-02 10:53:13 -0400243 void setIndexedVertexBlendEnable(bool indexedVertexBlendEnable);
244 void setVertexBlendMatrixCount(unsigned int vertexBlendMatrixCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400245
Alexis Hetuc634fb62016-06-02 10:53:13 -0400246 void setTextureWrap(unsigned int stage, int mask);
247 void setTexGen(unsigned int stage, TexGen texGen);
248 void setLocalViewer(bool localViewer);
249 void setNormalizeNormals(bool normalizeNormals);
250 void setTextureMatrix(int stage, const Matrix &T);
251 void setTextureTransform(int stage, int count, bool project);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400252
Alexis Hetuc634fb62016-06-02 10:53:13 -0400253 void setTextureFilter(unsigned int sampler, FilterType textureFilter);
254 void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter);
255 void setGatherEnable(unsigned int sampler, bool enable);
256 void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode);
257 void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode);
258 void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode);
259 void setReadSRGB(unsigned int sampler, bool sRGB);
260 void setMipmapLOD(unsigned int sampler, float bias);
261 void setBorderColor(unsigned int sampler, const Color<float> &borderColor);
262 void setMaxAnisotropy(unsigned int stage, float maxAnisotropy);
Alexis Hetu010a4642017-07-18 14:33:04 -0400263 void setHighPrecisionFiltering(unsigned int sampler, bool highPrecisionFiltering);
Alexis Hetuc634fb62016-06-02 10:53:13 -0400264 void setSwizzleR(unsigned int sampler, SwizzleType swizzleR);
265 void setSwizzleG(unsigned int sampler, SwizzleType swizzleG);
266 void setSwizzleB(unsigned int sampler, SwizzleType swizzleB);
267 void setSwizzleA(unsigned int sampler, SwizzleType swizzleA);
Nicolas Capensf878d502017-11-06 15:29:46 -0500268 void setCompareFunc(unsigned int sampler, CompareFunc compare);
Alexis Hetuc634fb62016-06-02 10:53:13 -0400269 void setBaseLevel(unsigned int sampler, int baseLevel);
270 void setMaxLevel(unsigned int sampler, int maxLevel);
271 void setMinLod(unsigned int sampler, float minLod);
272 void setMaxLod(unsigned int sampler, float maxLod);
Alexis Hetu88482c32018-06-05 17:05:17 -0400273 void setSyncRequired(unsigned int sampler, bool isSincRequired);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400274
Alexis Hetuc634fb62016-06-02 10:53:13 -0400275 void setPointSize(float pointSize);
276 void setPointSizeMin(float pointSizeMin);
277 void setPointSizeMax(float pointSizeMax);
278 void setPointScaleA(float pointScaleA);
279 void setPointScaleB(float pointScaleB);
280 void setPointScaleC(float pointScaleC);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400281
Alexis Hetuc634fb62016-06-02 10:53:13 -0400282 void setTransformFeedbackQueryEnabled(bool enable);
283 void enableTransformFeedback(uint64_t enable);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400284
285 protected:
286 const Matrix &getModelTransform(int i);
287 const Matrix &getViewTransform();
288
Alexis Hetua62a0ca2016-04-20 15:29:51 -0400289 const State update(DrawType drawType);
Ben Clayton6897e9b2019-07-16 17:27:27 +0100290 std::shared_ptr<Routine> routine(const State &state);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400291
292 bool isFixedFunction();
293 void setRoutineCacheSize(int cacheSize);
294
295 // Shader constants
296 float4 c[VERTEX_UNIFORM_VECTORS + 1]; // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
297 int4 i[16];
298 bool b[16];
299
300 PointSprite point;
301 FixedFunction ff;
302
303 private:
304 struct UniformBufferInfo
305 {
306 UniformBufferInfo();
307
308 Resource* buffer;
309 int offset;
310 };
311 UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS];
312
313 struct TransformFeedbackInfo
314 {
315 TransformFeedbackInfo();
316
317 Resource* buffer;
Alexis Hetu1d672442016-06-23 11:24:00 -0400318 unsigned int offset;
319 unsigned int reg;
320 unsigned int row;
321 unsigned int col;
322 unsigned int stride;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400323 };
324 TransformFeedbackInfo transformFeedbackInfo[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
325
326 void updateTransform();
327 void setTransform(const Matrix &M, int i);
328 void setCameraTransform(const Matrix &M, int i);
329 void setNormalTransform(const Matrix &M, int i);
330
331 Context *const context;
332
333 RoutineCache<State> *routineCache;
334
335 protected:
336 Matrix M[12]; // Model/Geometry/World matrix
337 Matrix V; // View/Camera/Eye matrix
338 Matrix B; // Base matrix
339 Matrix P; // Projection matrix
340 Matrix PB; // P * B
341 Matrix PBV; // P * B * V
342 Matrix PBVM[12]; // P * B * V * M
343
344 // Update hierarchy
345 bool updateMatrix;
346 bool updateModelMatrix[12];
347 bool updateViewMatrix;
348 bool updateBaseMatrix;
349 bool updateProjectionMatrix;
350 bool updateLighting;
351 };
352}
353
354#endif // sw_VertexProcessor_hpp