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