OSDN Git Service

Move parameter reading to routine constructor.
[android-x86/external-swiftshader.git] / src / Renderer / VertexProcessor.hpp
1 // SwiftShader Software Renderer\r
2 //\r
3 // Copyright(c) 2005-2012 TransGaming Inc.\r
4 //\r
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,\r
6 // transcribed, stored in a retrieval system, translated into any human or computer\r
7 // language by any means, or disclosed to third parties without the explicit written\r
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express\r
9 // or implied, including but not limited to any patent rights, are granted to you.\r
10 //\r
11 \r
12 #ifndef sw_VertexProcessor_hpp\r
13 #define sw_VertexProcessor_hpp\r
14 \r
15 #include "Matrix.hpp"\r
16 #include "Context.hpp"\r
17 #include "RoutineCache.hpp"\r
18 \r
19 namespace sw\r
20 {\r
21         struct DrawData;\r
22 \r
23         struct VertexCache   // FIXME: Variable size\r
24         {\r
25                 void clear();\r
26 \r
27                 Vertex vertex[16][4];\r
28                 unsigned int tag[16];\r
29 \r
30                 int drawCall;\r
31         };\r
32 \r
33         struct VertexTask\r
34         {\r
35                 unsigned int vertexCount;\r
36                 VertexCache vertexCache;\r
37         };\r
38 \r
39         class VertexProcessor\r
40         {\r
41         public:\r
42                 struct States\r
43                 {\r
44                         unsigned int computeHash();\r
45 \r
46                         uint64_t shaderID;\r
47 \r
48                         bool fixedFunction             : 1;\r
49                         bool shaderContainsTexldl      : 1;\r
50                         unsigned int positionRegister  : 4;\r
51                         unsigned int pointSizeRegister : 4;   // 0xF signifies no vertex point size\r
52                                 \r
53                         unsigned int vertexBlendMatrixCount               : 3;\r
54                         bool indexedVertexBlendEnable                     : 1;\r
55                         bool vertexNormalActive                           : 1;\r
56                         bool normalizeNormals                             : 1;\r
57                         bool vertexLightingActive                         : 1;\r
58                         bool diffuseActive                                : 1;\r
59                         bool specularActive                               : 1;\r
60                         bool vertexSpecularActive                         : 1;\r
61                         unsigned int vertexLightActive                    : 8;\r
62                         MaterialSource vertexDiffuseMaterialSourceActive  : BITS(MATERIAL_LAST);\r
63                         MaterialSource vertexSpecularMaterialSourceActive : BITS(MATERIAL_LAST);\r
64                         MaterialSource vertexAmbientMaterialSourceActive  : BITS(MATERIAL_LAST);\r
65                         MaterialSource vertexEmissiveMaterialSourceActive : BITS(MATERIAL_LAST);\r
66                         bool fogActive                                    : 1;\r
67                         FogMode vertexFogMode                             : BITS(FOG_LAST);\r
68                         bool rangeFogActive                               : 1;\r
69                         bool localViewerActive                            : 1;\r
70                         bool pointSizeActive                              : 1;\r
71                         bool pointScaleActive                             : 1;\r
72 \r
73                         bool preTransformed : 1;\r
74                         bool superSampling  : 1;\r
75                         bool multiSampling  : 1;\r
76 \r
77                         struct TextureState\r
78                         {\r
79                                 TexGen texGenActive                       : BITS(TEXGEN_LAST);\r
80                                 unsigned char textureTransformCountActive : 3;\r
81                                 unsigned char texCoordIndexActive         : 3;\r
82                         };\r
83 \r
84                         TextureState textureState[8];\r
85 \r
86                         Sampler::State samplerState[VERTEX_TEXTURE_IMAGE_UNITS];\r
87 \r
88                         struct Input\r
89                         {\r
90                                 operator bool() const   // Returns true if stream contains data\r
91                                 {\r
92                                         return count != 0;\r
93                                 }\r
94 \r
95                                 StreamType type    : BITS(STREAMTYPE_LAST);\r
96                                 unsigned int count : 3;\r
97                                 bool normalized    : 1;\r
98                         };\r
99 \r
100                         struct Output\r
101                         {\r
102                                 union\r
103                                 {\r
104                                         unsigned char write : 4;\r
105 \r
106                                         struct\r
107                                         {\r
108                                                 unsigned char xWrite : 1;\r
109                                                 unsigned char yWrite : 1;\r
110                                                 unsigned char zWrite : 1;\r
111                                                 unsigned char wWrite : 1;\r
112                                         };\r
113                                 };\r
114 \r
115                                 union\r
116                                 {\r
117                                         unsigned char clamp : 4;\r
118                                         \r
119                                         struct\r
120                                         {\r
121                                                 unsigned char xClamp : 1;\r
122                                                 unsigned char yClamp : 1;\r
123                                                 unsigned char zClamp : 1;\r
124                                                 unsigned char wClamp : 1;\r
125                                         };\r
126                                 };\r
127                         };\r
128 \r
129                         Input input[VERTEX_ATTRIBUTES];\r
130                         Output output[12];\r
131                 };\r
132 \r
133                 struct State : States\r
134                 {\r
135                         State();\r
136 \r
137                         bool operator==(const State &state) const;\r
138 \r
139                         unsigned int hash;\r
140                 };\r
141 \r
142                 struct FixedFunction\r
143                 {\r
144                         float4 transformT[12][4];\r
145                         float4 cameraTransformT[12][4];\r
146                         float4 normalTransformT[12][4];\r
147                         float4 textureTransform[8][4];\r
148                         \r
149                         float4 lightPosition[8];\r
150                         float4 lightAmbient[8];\r
151                         float4 lightSpecular[8];\r
152                         float4 lightDiffuse[8];\r
153                         float4 attenuationConstant[8];\r
154                         float4 attenuationLinear[8];\r
155                         float4 attenuationQuadratic[8];\r
156                         float lightRange[8];\r
157                         float4 materialDiffuse;\r
158                         float4 materialSpecular;\r
159                         float materialShininess;\r
160                         float4 globalAmbient;\r
161                         float4 materialEmission;\r
162                         float4 materialAmbient;\r
163                 };\r
164 \r
165                 struct PointSprite\r
166                 {\r
167                         float4 pointSize;\r
168                         float pointSizeMin;\r
169                         float pointSizeMax;\r
170                         float pointScaleA;\r
171                         float pointScaleB;\r
172                         float pointScaleC;\r
173                 };\r
174 \r
175                 typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw);\r
176 \r
177                 VertexProcessor(Context *context);\r
178 \r
179                 virtual ~VertexProcessor();\r
180 \r
181                 virtual void setInputStream(int index, const Stream &stream);\r
182                 virtual void resetInputStreams(bool preTransformed);\r
183 \r
184                 virtual void setFloatConstant(unsigned int index, const float value[4]);\r
185                 virtual void setIntegerConstant(unsigned int index, const int integer[4]);\r
186                 virtual void setBooleanConstant(unsigned int index, int boolean);\r
187 \r
188                 // Transformations\r
189                 virtual void setModelMatrix(const Matrix &M, int i = 0);\r
190                 virtual void setViewMatrix(const Matrix &V);\r
191                 virtual void setBaseMatrix(const Matrix &B);\r
192                 virtual void setProjectionMatrix(const Matrix &P);\r
193 \r
194                 // Lighting\r
195                 virtual void setLightingEnable(bool lightingEnable);\r
196                 virtual void setLightEnable(unsigned int light, bool lightEnable);\r
197                 virtual void setSpecularEnable(bool specularEnable);\r
198 \r
199                 virtual void setGlobalAmbient(const Color<float> &globalAmbient);\r
200                 virtual void setLightPosition(unsigned int light, const Point &lightPosition);\r
201                 virtual void setLightViewPosition(unsigned int light, const Point &lightPosition);\r
202                 virtual void setLightDiffuse(unsigned int light, const Color<float> &lightDiffuse);\r
203                 virtual void setLightSpecular(unsigned int light, const Color<float> &lightSpecular);\r
204                 virtual void setLightAmbient(unsigned int light, const Color<float> &lightAmbient);\r
205                 virtual void setLightAttenuation(unsigned int light, float constant, float linear, float quadratic);\r
206                 virtual void setLightRange(unsigned int light, float lightRange);\r
207 \r
208                 virtual void setInstanceID(int instanceID);\r
209 \r
210                 virtual void setFogEnable(bool fogEnable);\r
211                 virtual void setVertexFogMode(FogMode fogMode);\r
212                 virtual void setRangeFogEnable(bool enable);\r
213 \r
214                 virtual void setColorVertexEnable(bool colorVertexEnable);\r
215                 virtual void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource);\r
216                 virtual void setSpecularMaterialSource(MaterialSource specularMaterialSource);\r
217                 virtual void setAmbientMaterialSource(MaterialSource ambientMaterialSource);\r
218                 virtual void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource);\r
219 \r
220                 virtual void setMaterialEmission(const Color<float> &emission);\r
221                 virtual void setMaterialAmbient(const Color<float> &materialAmbient);\r
222                 virtual void setMaterialDiffuse(const Color<float> &diffuseColor);\r
223                 virtual void setMaterialSpecular(const Color<float> &specularColor);\r
224                 virtual void setMaterialShininess(float specularPower);\r
225 \r
226                 virtual void setIndexedVertexBlendEnable(bool indexedVertexBlendEnable);\r
227                 virtual void setVertexBlendMatrixCount(unsigned int vertexBlendMatrixCount);\r
228 \r
229                 virtual void setTextureWrap(unsigned int stage, int mask);\r
230                 virtual void setTexGen(unsigned int stage, TexGen texGen);\r
231                 virtual void setLocalViewer(bool localViewer);\r
232                 virtual void setNormalizeNormals(bool normalizeNormals);\r
233                 virtual void setTextureMatrix(int stage, const Matrix &T);\r
234                 virtual void setTextureTransform(int stage, int count, bool project);\r
235 \r
236                 virtual void setTextureFilter(unsigned int sampler, FilterType textureFilter);\r
237                 virtual void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter);\r
238                 virtual void setGatherEnable(unsigned int sampler, bool enable);\r
239                 virtual void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode);\r
240                 virtual void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode);\r
241                 virtual void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode);\r
242                 virtual void setReadSRGB(unsigned int sampler, bool sRGB);\r
243                 virtual void setMipmapLOD(unsigned int sampler, float bias);\r
244                 virtual void setBorderColor(unsigned int sampler, const Color<float> &borderColor);\r
245                 virtual void setMaxAnisotropy(unsigned int stage, float maxAnisotropy);\r
246                 virtual void setSwizzleR(unsigned int sampler, SwizzleType swizzleR);\r
247                 virtual void setSwizzleG(unsigned int sampler, SwizzleType swizzleG);\r
248                 virtual void setSwizzleB(unsigned int sampler, SwizzleType swizzleB);\r
249                 virtual void setSwizzleA(unsigned int sampler, SwizzleType swizzleA);\r
250 \r
251                 virtual void setPointSize(float pointSize);\r
252                 virtual void setPointSizeMin(float pointSizeMin);\r
253                 virtual void setPointSizeMax(float pointSizeMax);\r
254                 virtual void setPointScaleA(float pointScaleA);\r
255                 virtual void setPointScaleB(float pointScaleB);\r
256                 virtual void setPointScaleC(float pointScaleC);\r
257 \r
258         protected:\r
259                 const Matrix &getModelTransform(int i);\r
260                 const Matrix &getViewTransform();\r
261 \r
262                 const State update();\r
263                 Routine *routine(const State &state);\r
264 \r
265                 bool isFixedFunction();\r
266                 void setRoutineCacheSize(int cacheSize);\r
267 \r
268                 // Shader constants\r
269                 float4 c[256 + 1];   // One extra for indices out of range, c[256] = {0, 0, 0, 0}\r
270                 int4 i[16];\r
271                 bool b[16];\r
272 \r
273                 PointSprite point;\r
274                 FixedFunction ff;\r
275 \r
276         private:\r
277                 void updateTransform();\r
278 \r
279                 void setTransform(const Matrix &M, int i);\r
280                 void setCameraTransform(const Matrix &M, int i);\r
281                 void setNormalTransform(const Matrix &M, int i);\r
282 \r
283                 Context *const context;\r
284 \r
285                 RoutineCache<State> *routineCache;\r
286 \r
287         protected:\r
288                 Matrix M[12];      // Model/Geometry/World matrix\r
289                 Matrix V;          // View/Camera/Eye matrix\r
290                 Matrix B;          // Base matrix\r
291                 Matrix P;          // Projection matrix\r
292                 Matrix PB;         // P * B\r
293                 Matrix PBV;        // P * B * V\r
294                 Matrix PBVM[12];   // P * B * V * M\r
295 \r
296                 // Update hierarchy\r
297                 bool updateMatrix;\r
298                 bool updateModelMatrix[12];\r
299                 bool updateViewMatrix;\r
300                 bool updateBaseMatrix;\r
301                 bool updateProjectionMatrix;\r
302                 bool updateLighting;\r
303         };\r
304 }\r
305 \r
306 #endif   // sw_VertexProcessor_hpp\r