Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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" |
Alexis Hetu | b7508b8 | 2016-09-22 15:36:45 -0400 | [diff] [blame] | 21 | #include "Shader/VertexShader.hpp" |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 39 | unsigned int vertexCount; |
Alexis Hetu | a62a0ca | 2016-04-20 15:29:51 -0400 | [diff] [blame] | 40 | unsigned int primitiveStart; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 41 | VertexCache vertexCache; |
| 42 | }; |
| 43 | |
| 44 | class VertexProcessor |
| 45 | { |
| 46 | public: |
| 47 | struct States |
| 48 | { |
| 49 | unsigned int computeHash(); |
| 50 | |
| 51 | uint64_t shaderID; |
| 52 | |
| 53 | bool fixedFunction : 1; |
| 54 | bool textureSampling : 1; |
Nicolas Capens | ec0936c | 2016-05-18 12:32:02 -0400 | [diff] [blame] | 55 | unsigned int positionRegister : BITS(MAX_VERTEX_OUTPUTS); |
| 56 | unsigned int pointSizeRegister : BITS(MAX_VERTEX_OUTPUTS); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 57 | |
| 58 | unsigned int vertexBlendMatrixCount : 3; |
| 59 | bool indexedVertexBlendEnable : 1; |
| 60 | bool vertexNormalActive : 1; |
| 61 | bool normalizeNormals : 1; |
| 62 | bool vertexLightingActive : 1; |
| 63 | bool diffuseActive : 1; |
| 64 | bool specularActive : 1; |
| 65 | bool vertexSpecularActive : 1; |
| 66 | unsigned int vertexLightActive : 8; |
| 67 | MaterialSource vertexDiffuseMaterialSourceActive : BITS(MATERIAL_LAST); |
| 68 | MaterialSource vertexSpecularMaterialSourceActive : BITS(MATERIAL_LAST); |
| 69 | MaterialSource vertexAmbientMaterialSourceActive : BITS(MATERIAL_LAST); |
| 70 | MaterialSource vertexEmissiveMaterialSourceActive : BITS(MATERIAL_LAST); |
| 71 | bool fogActive : 1; |
| 72 | FogMode vertexFogMode : BITS(FOG_LAST); |
| 73 | bool rangeFogActive : 1; |
| 74 | bool localViewerActive : 1; |
| 75 | bool pointSizeActive : 1; |
| 76 | bool pointScaleActive : 1; |
| 77 | bool transformFeedbackQueryEnabled : 1; |
| 78 | uint64_t transformFeedbackEnabled : 64; |
Alexis Hetu | a62a0ca | 2016-04-20 15:29:51 -0400 | [diff] [blame] | 79 | unsigned char verticesPerPrimitive : 2; // 1 (points), 2 (lines) or 3 (triangles) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 80 | |
| 81 | bool preTransformed : 1; |
| 82 | bool superSampling : 1; |
| 83 | bool multiSampling : 1; |
| 84 | |
| 85 | struct TextureState |
| 86 | { |
| 87 | TexGen texGenActive : BITS(TEXGEN_LAST); |
| 88 | unsigned char textureTransformCountActive : 3; |
| 89 | unsigned char texCoordIndexActive : 3; |
| 90 | }; |
| 91 | |
| 92 | TextureState textureState[8]; |
| 93 | |
| 94 | Sampler::State samplerState[VERTEX_TEXTURE_IMAGE_UNITS]; |
| 95 | |
| 96 | struct Input |
| 97 | { |
| 98 | operator bool() const // Returns true if stream contains data |
| 99 | { |
| 100 | return count != 0; |
| 101 | } |
| 102 | |
| 103 | StreamType type : BITS(STREAMTYPE_LAST); |
| 104 | unsigned int count : 3; |
| 105 | bool normalized : 1; |
Alexis Hetu | b7508b8 | 2016-09-22 15:36:45 -0400 | [diff] [blame] | 106 | unsigned int attribType : BITS(VertexShader::ATTRIBTYPE_LAST); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | struct Output |
| 110 | { |
| 111 | union |
| 112 | { |
| 113 | unsigned char write : 4; |
| 114 | |
| 115 | struct |
| 116 | { |
| 117 | unsigned char xWrite : 1; |
| 118 | unsigned char yWrite : 1; |
| 119 | unsigned char zWrite : 1; |
| 120 | unsigned char wWrite : 1; |
| 121 | }; |
| 122 | }; |
| 123 | |
| 124 | union |
| 125 | { |
| 126 | unsigned char clamp : 4; |
| 127 | |
| 128 | struct |
| 129 | { |
| 130 | unsigned char xClamp : 1; |
| 131 | unsigned char yClamp : 1; |
| 132 | unsigned char zClamp : 1; |
| 133 | unsigned char wClamp : 1; |
| 134 | }; |
| 135 | }; |
| 136 | }; |
| 137 | |
Nicolas Capens | f0aef1a | 2016-05-18 14:44:21 -0400 | [diff] [blame] | 138 | Input input[MAX_VERTEX_INPUTS]; |
Nicolas Capens | ec0936c | 2016-05-18 12:32:02 -0400 | [diff] [blame] | 139 | Output output[MAX_VERTEX_OUTPUTS]; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | struct State : States |
| 143 | { |
| 144 | State(); |
| 145 | |
| 146 | bool operator==(const State &state) const; |
| 147 | |
| 148 | unsigned int hash; |
| 149 | }; |
| 150 | |
| 151 | struct FixedFunction |
| 152 | { |
| 153 | float4 transformT[12][4]; |
| 154 | float4 cameraTransformT[12][4]; |
| 155 | float4 normalTransformT[12][4]; |
| 156 | float4 textureTransform[8][4]; |
| 157 | |
| 158 | float4 lightPosition[8]; |
| 159 | float4 lightAmbient[8]; |
| 160 | float4 lightSpecular[8]; |
| 161 | float4 lightDiffuse[8]; |
| 162 | float4 attenuationConstant[8]; |
| 163 | float4 attenuationLinear[8]; |
| 164 | float4 attenuationQuadratic[8]; |
| 165 | float lightRange[8]; |
| 166 | float4 materialDiffuse; |
| 167 | float4 materialSpecular; |
| 168 | float materialShininess; |
| 169 | float4 globalAmbient; |
| 170 | float4 materialEmission; |
| 171 | float4 materialAmbient; |
| 172 | }; |
| 173 | |
| 174 | struct PointSprite |
| 175 | { |
| 176 | float4 pointSize; |
| 177 | float pointSizeMin; |
| 178 | float pointSizeMax; |
| 179 | float pointScaleA; |
| 180 | float pointScaleB; |
| 181 | float pointScaleC; |
| 182 | }; |
| 183 | |
| 184 | typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw); |
| 185 | |
| 186 | VertexProcessor(Context *context); |
| 187 | |
| 188 | virtual ~VertexProcessor(); |
| 189 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 190 | void setInputStream(int index, const Stream &stream); |
| 191 | void resetInputStreams(bool preTransformed); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 192 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 193 | void setFloatConstant(unsigned int index, const float value[4]); |
| 194 | void setIntegerConstant(unsigned int index, const int integer[4]); |
| 195 | void setBooleanConstant(unsigned int index, int boolean); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 196 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 197 | void setUniformBuffer(int index, sw::Resource* uniformBuffer, int offset); |
| 198 | void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 199 | |
Alexis Hetu | 1d67244 | 2016-06-23 11:24:00 -0400 | [diff] [blame] | 200 | void setTransformFeedbackBuffer(int index, sw::Resource* transformFeedbackBuffer, int offset, unsigned int reg, unsigned int row, unsigned int col, unsigned int stride); |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 201 | void lockTransformFeedbackBuffers(byte** t, unsigned int* v, unsigned int* r, unsigned int* c, unsigned int* s, sw::Resource* transformFeedbackBuffers[]); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 202 | |
| 203 | // Transformations |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 204 | void setModelMatrix(const Matrix &M, int i = 0); |
| 205 | void setViewMatrix(const Matrix &V); |
| 206 | void setBaseMatrix(const Matrix &B); |
| 207 | void setProjectionMatrix(const Matrix &P); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 208 | |
| 209 | // Lighting |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 210 | void setLightingEnable(bool lightingEnable); |
| 211 | void setLightEnable(unsigned int light, bool lightEnable); |
| 212 | void setSpecularEnable(bool specularEnable); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 213 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 214 | void setGlobalAmbient(const Color<float> &globalAmbient); |
| 215 | void setLightPosition(unsigned int light, const Point &lightPosition); |
| 216 | void setLightViewPosition(unsigned int light, const Point &lightPosition); |
| 217 | void setLightDiffuse(unsigned int light, const Color<float> &lightDiffuse); |
| 218 | void setLightSpecular(unsigned int light, const Color<float> &lightSpecular); |
| 219 | void setLightAmbient(unsigned int light, const Color<float> &lightAmbient); |
| 220 | void setLightAttenuation(unsigned int light, float constant, float linear, float quadratic); |
| 221 | void setLightRange(unsigned int light, float lightRange); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 222 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 223 | void setInstanceID(int instanceID); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 224 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 225 | void setFogEnable(bool fogEnable); |
| 226 | void setVertexFogMode(FogMode fogMode); |
| 227 | void setRangeFogEnable(bool enable); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 228 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 229 | void setColorVertexEnable(bool colorVertexEnable); |
| 230 | void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource); |
| 231 | void setSpecularMaterialSource(MaterialSource specularMaterialSource); |
| 232 | void setAmbientMaterialSource(MaterialSource ambientMaterialSource); |
| 233 | void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 234 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 235 | void setMaterialEmission(const Color<float> &emission); |
| 236 | void setMaterialAmbient(const Color<float> &materialAmbient); |
| 237 | void setMaterialDiffuse(const Color<float> &diffuseColor); |
| 238 | void setMaterialSpecular(const Color<float> &specularColor); |
| 239 | void setMaterialShininess(float specularPower); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 240 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 241 | void setIndexedVertexBlendEnable(bool indexedVertexBlendEnable); |
| 242 | void setVertexBlendMatrixCount(unsigned int vertexBlendMatrixCount); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 243 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 244 | void setTextureWrap(unsigned int stage, int mask); |
| 245 | void setTexGen(unsigned int stage, TexGen texGen); |
| 246 | void setLocalViewer(bool localViewer); |
| 247 | void setNormalizeNormals(bool normalizeNormals); |
| 248 | void setTextureMatrix(int stage, const Matrix &T); |
| 249 | void setTextureTransform(int stage, int count, bool project); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 250 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 251 | void setTextureFilter(unsigned int sampler, FilterType textureFilter); |
| 252 | void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter); |
| 253 | void setGatherEnable(unsigned int sampler, bool enable); |
| 254 | void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode); |
| 255 | void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode); |
| 256 | void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode); |
| 257 | void setReadSRGB(unsigned int sampler, bool sRGB); |
| 258 | void setMipmapLOD(unsigned int sampler, float bias); |
| 259 | void setBorderColor(unsigned int sampler, const Color<float> &borderColor); |
| 260 | void setMaxAnisotropy(unsigned int stage, float maxAnisotropy); |
Alexis Hetu | 010a464 | 2017-07-18 14:33:04 -0400 | [diff] [blame^] | 261 | void setHighPrecisionFiltering(unsigned int sampler, bool highPrecisionFiltering); |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 262 | void setSwizzleR(unsigned int sampler, SwizzleType swizzleR); |
| 263 | void setSwizzleG(unsigned int sampler, SwizzleType swizzleG); |
| 264 | void setSwizzleB(unsigned int sampler, SwizzleType swizzleB); |
| 265 | void setSwizzleA(unsigned int sampler, SwizzleType swizzleA); |
| 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); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 270 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 271 | void setPointSize(float pointSize); |
| 272 | void setPointSizeMin(float pointSizeMin); |
| 273 | void setPointSizeMax(float pointSizeMax); |
| 274 | void setPointScaleA(float pointScaleA); |
| 275 | void setPointScaleB(float pointScaleB); |
| 276 | void setPointScaleC(float pointScaleC); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 277 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 278 | void setTransformFeedbackQueryEnabled(bool enable); |
| 279 | void enableTransformFeedback(uint64_t enable); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 280 | |
| 281 | protected: |
| 282 | const Matrix &getModelTransform(int i); |
| 283 | const Matrix &getViewTransform(); |
| 284 | |
Alexis Hetu | a62a0ca | 2016-04-20 15:29:51 -0400 | [diff] [blame] | 285 | const State update(DrawType drawType); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 286 | Routine *routine(const State &state); |
| 287 | |
| 288 | bool isFixedFunction(); |
| 289 | void setRoutineCacheSize(int cacheSize); |
| 290 | |
| 291 | // Shader constants |
| 292 | float4 c[VERTEX_UNIFORM_VECTORS + 1]; // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0} |
| 293 | int4 i[16]; |
| 294 | bool b[16]; |
| 295 | |
| 296 | PointSprite point; |
| 297 | FixedFunction ff; |
| 298 | |
| 299 | private: |
| 300 | struct UniformBufferInfo |
| 301 | { |
| 302 | UniformBufferInfo(); |
| 303 | |
| 304 | Resource* buffer; |
| 305 | int offset; |
| 306 | }; |
| 307 | UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS]; |
| 308 | |
| 309 | struct TransformFeedbackInfo |
| 310 | { |
| 311 | TransformFeedbackInfo(); |
| 312 | |
| 313 | Resource* buffer; |
Alexis Hetu | 1d67244 | 2016-06-23 11:24:00 -0400 | [diff] [blame] | 314 | unsigned int offset; |
| 315 | unsigned int reg; |
| 316 | unsigned int row; |
| 317 | unsigned int col; |
| 318 | unsigned int stride; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 319 | }; |
| 320 | TransformFeedbackInfo transformFeedbackInfo[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]; |
| 321 | |
| 322 | void updateTransform(); |
| 323 | void setTransform(const Matrix &M, int i); |
| 324 | void setCameraTransform(const Matrix &M, int i); |
| 325 | void setNormalTransform(const Matrix &M, int i); |
| 326 | |
| 327 | Context *const context; |
| 328 | |
| 329 | RoutineCache<State> *routineCache; |
| 330 | |
| 331 | protected: |
| 332 | Matrix M[12]; // Model/Geometry/World matrix |
| 333 | Matrix V; // View/Camera/Eye matrix |
| 334 | Matrix B; // Base matrix |
| 335 | Matrix P; // Projection matrix |
| 336 | Matrix PB; // P * B |
| 337 | Matrix PBV; // P * B * V |
| 338 | Matrix PBVM[12]; // P * B * V * M |
| 339 | |
| 340 | // Update hierarchy |
| 341 | bool updateMatrix; |
| 342 | bool updateModelMatrix[12]; |
| 343 | bool updateViewMatrix; |
| 344 | bool updateBaseMatrix; |
| 345 | bool updateProjectionMatrix; |
| 346 | bool updateLighting; |
| 347 | }; |
| 348 | } |
| 349 | |
| 350 | #endif // sw_VertexProcessor_hpp |