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