| 1 | // 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 | #include "Shader/VertexShader.hpp" | 
| 22 |  | 
| 23 | namespace 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 | 	{ | 
| 39 | 		unsigned int vertexCount; | 
| 40 | 		unsigned int primitiveStart; | 
| 41 | 		VertexCache vertexCache; | 
| 42 | 	}; | 
| 43 |  | 
| 44 | 	class VertexProcessor | 
| 45 | 	{ | 
| 46 | 	public: | 
| 47 | 		struct States : Memset<States> | 
| 48 | 		{ | 
| 49 | 			States() : Memset(this, 0) {} | 
| 50 |  | 
| 51 | 			uint32_t computeHash(); | 
| 52 |  | 
| 53 | 			uint64_t shaderID; | 
| 54 |  | 
| 55 | 			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. | 
| 59 |  | 
| 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; | 
| 81 | 			unsigned char verticesPerPrimitive                : 2; // 1 (points), 2 (lines) or 3 (triangles) | 
| 82 |  | 
| 83 | 			bool preTransformed : 1; | 
| 84 | 			bool superSampling  : 1; | 
| 85 |  | 
| 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 |  | 
| 95 | 			Sampler::State sampler[VERTEX_TEXTURE_IMAGE_UNITS]; | 
| 96 |  | 
| 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; | 
| 107 | 				unsigned int attribType : BITS(VertexShader::ATTRIBTYPE_LAST); | 
| 108 | 			}; | 
| 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 |  | 
| 139 | 			Input input[MAX_VERTEX_INPUTS]; | 
| 140 | 			Output output[MAX_VERTEX_OUTPUTS]; | 
| 141 | 		}; | 
| 142 |  | 
| 143 | 		struct State : States | 
| 144 | 		{ | 
| 145 | 			bool operator==(const State &state) const; | 
| 146 |  | 
| 147 | 			uint32_t hash; | 
| 148 | 		}; | 
| 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 |  | 
| 189 | 		void setInputStream(int index, const Stream &stream); | 
| 190 | 		void resetInputStreams(bool preTransformed); | 
| 191 |  | 
| 192 | 		void setFloatConstant(unsigned int index, const float value[4]); | 
| 193 | 		void setIntegerConstant(unsigned int index, const int integer[4]); | 
| 194 | 		void setBooleanConstant(unsigned int index, int boolean); | 
| 195 |  | 
| 196 | 		void setUniformBuffer(int index, sw::Resource* uniformBuffer, int offset); | 
| 197 | 		void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]); | 
| 198 |  | 
| 199 | 		void setTransformFeedbackBuffer(int index, sw::Resource* transformFeedbackBuffer, int offset, unsigned int reg, unsigned int row, unsigned int col, unsigned int stride); | 
| 200 | 		void lockTransformFeedbackBuffers(byte** t, unsigned int* v, unsigned int* r, unsigned int* c, unsigned int* s, sw::Resource* transformFeedbackBuffers[]); | 
| 201 |  | 
| 202 | 		// Transformations | 
| 203 | 		void setModelMatrix(const Matrix &M, int i = 0); | 
| 204 | 		void setViewMatrix(const Matrix &V); | 
| 205 | 		void setBaseMatrix(const Matrix &B); | 
| 206 | 		void setProjectionMatrix(const Matrix &P); | 
| 207 |  | 
| 208 | 		// Lighting | 
| 209 | 		void setLightingEnable(bool lightingEnable); | 
| 210 | 		void setLightEnable(unsigned int light, bool lightEnable); | 
| 211 | 		void setSpecularEnable(bool specularEnable); | 
| 212 |  | 
| 213 | 		void setGlobalAmbient(const Color<float> &globalAmbient); | 
| 214 | 		void setLightPosition(unsigned int light, const Point &lightPosition); | 
| 215 | 		void setLightViewPosition(unsigned int light, const Point &lightPosition); | 
| 216 | 		void setLightDiffuse(unsigned int light, const Color<float> &lightDiffuse); | 
| 217 | 		void setLightSpecular(unsigned int light, const Color<float> &lightSpecular); | 
| 218 | 		void setLightAmbient(unsigned int light, const Color<float> &lightAmbient); | 
| 219 | 		void setLightAttenuation(unsigned int light, float constant, float linear, float quadratic); | 
| 220 | 		void setLightRange(unsigned int light, float lightRange); | 
| 221 |  | 
| 222 | 		void setInstanceID(int instanceID); | 
| 223 |  | 
| 224 | 		void setFogEnable(bool fogEnable); | 
| 225 | 		void setVertexFogMode(FogMode fogMode); | 
| 226 | 		void setRangeFogEnable(bool enable); | 
| 227 |  | 
| 228 | 		void setColorVertexEnable(bool colorVertexEnable); | 
| 229 | 		void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource); | 
| 230 | 		void setSpecularMaterialSource(MaterialSource specularMaterialSource); | 
| 231 | 		void setAmbientMaterialSource(MaterialSource ambientMaterialSource); | 
| 232 | 		void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource); | 
| 233 |  | 
| 234 | 		void setMaterialEmission(const Color<float> &emission); | 
| 235 | 		void setMaterialAmbient(const Color<float> &materialAmbient); | 
| 236 | 		void setMaterialDiffuse(const Color<float> &diffuseColor); | 
| 237 | 		void setMaterialSpecular(const Color<float> &specularColor); | 
| 238 | 		void setMaterialShininess(float specularPower); | 
| 239 |  | 
| 240 | 		void setIndexedVertexBlendEnable(bool indexedVertexBlendEnable); | 
| 241 | 		void setVertexBlendMatrixCount(unsigned int vertexBlendMatrixCount); | 
| 242 |  | 
| 243 | 		void setTextureWrap(unsigned int stage, int mask); | 
| 244 | 		void setTexGen(unsigned int stage, TexGen texGen); | 
| 245 | 		void setLocalViewer(bool localViewer); | 
| 246 | 		void setNormalizeNormals(bool normalizeNormals); | 
| 247 | 		void setTextureMatrix(int stage, const Matrix &T); | 
| 248 | 		void setTextureTransform(int stage, int count, bool project); | 
| 249 |  | 
| 250 | 		void setTextureFilter(unsigned int sampler, FilterType textureFilter); | 
| 251 | 		void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter); | 
| 252 | 		void setGatherEnable(unsigned int sampler, bool enable); | 
| 253 | 		void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode); | 
| 254 | 		void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode); | 
| 255 | 		void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode); | 
| 256 | 		void setReadSRGB(unsigned int sampler, bool sRGB); | 
| 257 | 		void setMipmapLOD(unsigned int sampler, float bias); | 
| 258 | 		void setBorderColor(unsigned int sampler, const Color<float> &borderColor); | 
| 259 | 		void setMaxAnisotropy(unsigned int stage, float maxAnisotropy); | 
| 260 | 		void setHighPrecisionFiltering(unsigned int sampler, bool highPrecisionFiltering); | 
| 261 | 		void setSwizzleR(unsigned int sampler, SwizzleType swizzleR); | 
| 262 | 		void setSwizzleG(unsigned int sampler, SwizzleType swizzleG); | 
| 263 | 		void setSwizzleB(unsigned int sampler, SwizzleType swizzleB); | 
| 264 | 		void setSwizzleA(unsigned int sampler, SwizzleType swizzleA); | 
| 265 | 		void setCompareFunc(unsigned int sampler, CompareFunc compare); | 
| 266 | 		void setBaseLevel(unsigned int sampler, int baseLevel); | 
| 267 | 		void setMaxLevel(unsigned int sampler, int maxLevel); | 
| 268 | 		void setMinLod(unsigned int sampler, float minLod); | 
| 269 | 		void setMaxLod(unsigned int sampler, float maxLod); | 
| 270 | 		void setSyncRequired(unsigned int sampler, bool isSincRequired); | 
| 271 |  | 
| 272 | 		void setPointSize(float pointSize); | 
| 273 | 		void setPointSizeMin(float pointSizeMin); | 
| 274 | 		void setPointSizeMax(float pointSizeMax); | 
| 275 | 		void setPointScaleA(float pointScaleA); | 
| 276 | 		void setPointScaleB(float pointScaleB); | 
| 277 | 		void setPointScaleC(float pointScaleC); | 
| 278 |  | 
| 279 | 		void setTransformFeedbackQueryEnabled(bool enable); | 
| 280 | 		void enableTransformFeedback(uint64_t enable); | 
| 281 |  | 
| 282 | 	protected: | 
| 283 | 		const Matrix &getModelTransform(int i); | 
| 284 | 		const Matrix &getViewTransform(); | 
| 285 |  | 
| 286 | 		const State update(DrawType drawType); | 
| 287 | 		std::shared_ptr<Routine> routine(const State &state); | 
| 288 |  | 
| 289 | 		bool isFixedFunction(); | 
| 290 | 		void setRoutineCacheSize(int cacheSize); | 
| 291 |  | 
| 292 | 		// Shader constants | 
| 293 | 		float4 c[VERTEX_UNIFORM_VECTORS + 1];   // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0} | 
| 294 | 		int4 i[16]; | 
| 295 | 		bool b[16]; | 
| 296 |  | 
| 297 | 		PointSprite point; | 
| 298 | 		FixedFunction ff; | 
| 299 |  | 
| 300 | 	private: | 
| 301 | 		struct UniformBufferInfo | 
| 302 | 		{ | 
| 303 | 			UniformBufferInfo(); | 
| 304 |  | 
| 305 | 			Resource* buffer; | 
| 306 | 			int offset; | 
| 307 | 		}; | 
| 308 | 		UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS]; | 
| 309 |  | 
| 310 | 		struct TransformFeedbackInfo | 
| 311 | 		{ | 
| 312 | 			TransformFeedbackInfo(); | 
| 313 |  | 
| 314 | 			Resource* buffer; | 
| 315 | 			unsigned int offset; | 
| 316 | 			unsigned int reg; | 
| 317 | 			unsigned int row; | 
| 318 | 			unsigned int col; | 
| 319 | 			unsigned int stride; | 
| 320 | 		}; | 
| 321 | 		TransformFeedbackInfo transformFeedbackInfo[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; | 
| 322 |  | 
| 323 | 		void updateTransform(); | 
| 324 | 		void setTransform(const Matrix &M, int i); | 
| 325 | 		void setCameraTransform(const Matrix &M, int i); | 
| 326 | 		void setNormalTransform(const Matrix &M, int i); | 
| 327 |  | 
| 328 | 		Context *const context; | 
| 329 |  | 
| 330 | 		RoutineCache<State> *routineCache; | 
| 331 |  | 
| 332 | 	protected: | 
| 333 | 		Matrix M[12];      // Model/Geometry/World matrix | 
| 334 | 		Matrix V;          // View/Camera/Eye matrix | 
| 335 | 		Matrix B;          // Base matrix | 
| 336 | 		Matrix P;          // Projection matrix | 
| 337 | 		Matrix PB;         // P * B | 
| 338 | 		Matrix PBV;        // P * B * V | 
| 339 | 		Matrix PBVM[12];   // P * B * V * M | 
| 340 |  | 
| 341 | 		// Update hierarchy | 
| 342 | 		bool updateMatrix; | 
| 343 | 		bool updateModelMatrix[12]; | 
| 344 | 		bool updateViewMatrix; | 
| 345 | 		bool updateBaseMatrix; | 
| 346 | 		bool updateProjectionMatrix; | 
| 347 | 		bool updateLighting; | 
| 348 | 	}; | 
| 349 | } | 
| 350 |  | 
| 351 | #endif   // sw_VertexProcessor_hpp | 
| 352 |  |