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_PixelProcessor_hpp |
| 16 | #define sw_PixelProcessor_hpp |
| 17 | |
| 18 | #include "Context.hpp" |
| 19 | #include "RoutineCache.hpp" |
| 20 | |
| 21 | namespace sw |
| 22 | { |
| 23 | class PixelShader; |
| 24 | class Rasterizer; |
| 25 | struct Texture; |
| 26 | struct DrawData; |
| 27 | |
| 28 | class PixelProcessor |
| 29 | { |
| 30 | public: |
| 31 | struct States |
| 32 | { |
| 33 | unsigned int computeHash(); |
| 34 | |
| 35 | int shaderID; |
| 36 | |
| 37 | bool depthOverride : 1; |
| 38 | bool shaderContainsKill : 1; |
| 39 | |
| 40 | DepthCompareMode depthCompareMode : BITS(DEPTH_LAST); |
| 41 | AlphaCompareMode alphaCompareMode : BITS(ALPHA_LAST); |
| 42 | bool depthWriteEnable : 1; |
| 43 | bool quadLayoutDepthBuffer : 1; |
| 44 | |
| 45 | bool stencilActive : 1; |
| 46 | StencilCompareMode stencilCompareMode : BITS(STENCIL_LAST); |
| 47 | StencilOperation stencilFailOperation : BITS(OPERATION_LAST); |
| 48 | StencilOperation stencilPassOperation : BITS(OPERATION_LAST); |
| 49 | StencilOperation stencilZFailOperation : BITS(OPERATION_LAST); |
| 50 | bool noStencilMask : 1; |
| 51 | bool noStencilWriteMask : 1; |
| 52 | bool stencilWriteMasked : 1; |
| 53 | bool twoSidedStencil : 1; |
| 54 | StencilCompareMode stencilCompareModeCCW : BITS(STENCIL_LAST); |
| 55 | StencilOperation stencilFailOperationCCW : BITS(OPERATION_LAST); |
| 56 | StencilOperation stencilPassOperationCCW : BITS(OPERATION_LAST); |
| 57 | StencilOperation stencilZFailOperationCCW : BITS(OPERATION_LAST); |
| 58 | bool noStencilMaskCCW : 1; |
| 59 | bool noStencilWriteMaskCCW : 1; |
| 60 | bool stencilWriteMaskedCCW : 1; |
| 61 | |
| 62 | bool depthTestActive : 1; |
| 63 | bool fogActive : 1; |
| 64 | FogMode pixelFogMode : BITS(FOG_LAST); |
| 65 | bool specularAdd : 1; |
| 66 | bool occlusionEnabled : 1; |
| 67 | bool wBasedFog : 1; |
| 68 | bool perspective : 1; |
| 69 | |
| 70 | bool alphaBlendActive : 1; |
| 71 | BlendFactor sourceBlendFactor : BITS(BLEND_LAST); |
| 72 | BlendFactor destBlendFactor : BITS(BLEND_LAST); |
| 73 | BlendOperation blendOperation : BITS(BLENDOP_LAST); |
| 74 | BlendFactor sourceBlendFactorAlpha : BITS(BLEND_LAST); |
| 75 | BlendFactor destBlendFactorAlpha : BITS(BLEND_LAST); |
| 76 | BlendOperation blendOperationAlpha : BITS(BLENDOP_LAST); |
| 77 | |
| 78 | unsigned int colorWriteMask : RENDERTARGETS * 4; // Four component bit masks |
| 79 | Format targetFormat[RENDERTARGETS]; |
| 80 | bool writeSRGB : 1; |
| 81 | unsigned int multiSample : 3; |
| 82 | unsigned int multiSampleMask : 4; |
| 83 | TransparencyAntialiasing transparencyAntialiasing : BITS(TRANSPARENCY_LAST); |
| 84 | bool centroid : 1; |
| 85 | |
| 86 | LogicalOperation logicalOperation : BITS(LOGICALOP_LAST); |
| 87 | |
| 88 | Sampler::State sampler[TEXTURE_IMAGE_UNITS]; |
| 89 | TextureStage::State textureStage[8]; |
| 90 | |
| 91 | struct Interpolant |
| 92 | { |
| 93 | unsigned char component : 4; |
| 94 | unsigned char flat : 4; |
| 95 | unsigned char project : 2; |
| 96 | bool centroid : 1; |
| 97 | }; |
| 98 | |
| 99 | union |
| 100 | { |
| 101 | struct |
| 102 | { |
| 103 | Interpolant color[2]; |
| 104 | Interpolant texture[8]; |
| 105 | }; |
| 106 | |
| 107 | Interpolant interpolant[10]; |
| 108 | }; |
| 109 | |
| 110 | Interpolant fog; |
| 111 | }; |
| 112 | |
| 113 | struct State : States |
| 114 | { |
| 115 | State(); |
| 116 | |
| 117 | bool operator==(const State &state) const; |
| 118 | |
| 119 | int colorWriteActive(int index) const |
| 120 | { |
| 121 | return (colorWriteMask >> (index * 4)) & 0xF; |
| 122 | } |
| 123 | |
| 124 | bool alphaTestActive() const |
| 125 | { |
| 126 | return alphaCompareMode != ALPHA_ALWAYS; |
| 127 | } |
| 128 | |
| 129 | bool pixelFogActive() const |
| 130 | { |
| 131 | return pixelFogMode != FOG_NONE; |
| 132 | } |
| 133 | |
| 134 | unsigned int hash; |
| 135 | }; |
| 136 | |
| 137 | struct Stencil |
| 138 | { |
| 139 | int64_t testMaskQ; |
| 140 | int64_t referenceMaskedQ; |
| 141 | int64_t referenceMaskedSignedQ; |
| 142 | int64_t writeMaskQ; |
| 143 | int64_t invWriteMaskQ; |
| 144 | int64_t referenceQ; |
| 145 | |
| 146 | void set(int reference, int testMask, int writeMask) |
| 147 | { |
| 148 | referenceQ = replicate(reference); |
| 149 | testMaskQ = replicate(testMask); |
| 150 | writeMaskQ = replicate(writeMask); |
| 151 | invWriteMaskQ = ~writeMaskQ; |
| 152 | referenceMaskedQ = referenceQ & testMaskQ; |
| 153 | referenceMaskedSignedQ = replicate((reference + 0x80) & 0xFF & testMask); |
| 154 | } |
| 155 | |
| 156 | static int64_t replicate(int b) |
| 157 | { |
| 158 | int64_t w = b & 0xFF; |
| 159 | |
| 160 | return (w << 0) | (w << 8) | (w << 16) | (w << 24) | (w << 32) | (w << 40) | (w << 48) | (w << 56); |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | struct Fog |
| 165 | { |
| 166 | float4 scale; |
| 167 | float4 offset; |
| 168 | word4 color4[3]; |
| 169 | float4 colorF[3]; |
| 170 | float4 densityE; |
| 171 | float4 density2E; |
| 172 | }; |
| 173 | |
| 174 | struct Factor |
| 175 | { |
| 176 | word4 textureFactor4[4]; |
| 177 | |
| 178 | word4 alphaReference4; |
| 179 | |
| 180 | word4 blendConstant4W[4]; |
| 181 | float4 blendConstant4F[4]; |
| 182 | word4 invBlendConstant4W[4]; |
| 183 | float4 invBlendConstant4F[4]; |
| 184 | }; |
| 185 | |
| 186 | public: |
| 187 | typedef void (*RoutinePointer)(const Primitive *primitive, int count, int thread, DrawData *draw); |
| 188 | |
| 189 | PixelProcessor(Context *context); |
| 190 | |
| 191 | virtual ~PixelProcessor(); |
| 192 | |
| 193 | virtual void setFloatConstant(unsigned int index, const float value[4]); |
| 194 | virtual void setIntegerConstant(unsigned int index, const int value[4]); |
| 195 | virtual void setBooleanConstant(unsigned int index, int boolean); |
| 196 | |
| 197 | virtual void setUniformBuffer(int index, sw::Resource* buffer, int offset); |
| 198 | virtual void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]); |
| 199 | |
| 200 | virtual void setRenderTarget(int index, Surface *renderTarget); |
| 201 | virtual void setDepthBuffer(Surface *depthBuffer); |
| 202 | virtual void setStencilBuffer(Surface *stencilBuffer); |
| 203 | |
| 204 | virtual void setTexCoordIndex(unsigned int stage, int texCoordIndex); |
| 205 | virtual void setStageOperation(unsigned int stage, TextureStage::StageOperation stageOperation); |
| 206 | virtual void setFirstArgument(unsigned int stage, TextureStage::SourceArgument firstArgument); |
| 207 | virtual void setSecondArgument(unsigned int stage, TextureStage::SourceArgument secondArgument); |
| 208 | virtual void setThirdArgument(unsigned int stage, TextureStage::SourceArgument thirdArgument); |
| 209 | virtual void setStageOperationAlpha(unsigned int stage, TextureStage::StageOperation stageOperationAlpha); |
| 210 | virtual void setFirstArgumentAlpha(unsigned int stage, TextureStage::SourceArgument firstArgumentAlpha); |
| 211 | virtual void setSecondArgumentAlpha(unsigned int stage, TextureStage::SourceArgument secondArgumentAlpha); |
| 212 | virtual void setThirdArgumentAlpha(unsigned int stage, TextureStage::SourceArgument thirdArgumentAlpha); |
| 213 | virtual void setFirstModifier(unsigned int stage, TextureStage::ArgumentModifier firstModifier); |
| 214 | virtual void setSecondModifier(unsigned int stage, TextureStage::ArgumentModifier secondModifier); |
| 215 | virtual void setThirdModifier(unsigned int stage, TextureStage::ArgumentModifier thirdModifier); |
| 216 | virtual void setFirstModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier firstModifierAlpha); |
| 217 | virtual void setSecondModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier secondModifierAlpha); |
| 218 | virtual void setThirdModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier thirdModifierAlpha); |
| 219 | virtual void setDestinationArgument(unsigned int stage, TextureStage::DestinationArgument destinationArgument); |
| 220 | virtual void setConstantColor(unsigned int stage, const Color<float> &constantColor); |
| 221 | virtual void setBumpmapMatrix(unsigned int stage, int element, float value); |
| 222 | virtual void setLuminanceScale(unsigned int stage, float value); |
| 223 | virtual void setLuminanceOffset(unsigned int stage, float value); |
| 224 | |
| 225 | virtual void setTextureFilter(unsigned int sampler, FilterType textureFilter); |
| 226 | virtual void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter); |
| 227 | virtual void setGatherEnable(unsigned int sampler, bool enable); |
| 228 | virtual void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode); |
| 229 | virtual void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode); |
| 230 | virtual void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode); |
| 231 | virtual void setReadSRGB(unsigned int sampler, bool sRGB); |
| 232 | virtual void setMipmapLOD(unsigned int sampler, float bias); |
| 233 | virtual void setBorderColor(unsigned int sampler, const Color<float> &borderColor); |
| 234 | virtual void setMaxAnisotropy(unsigned int sampler, float maxAnisotropy); |
| 235 | virtual void setSwizzleR(unsigned int sampler, SwizzleType swizzleR); |
| 236 | virtual void setSwizzleG(unsigned int sampler, SwizzleType swizzleG); |
| 237 | virtual void setSwizzleB(unsigned int sampler, SwizzleType swizzleB); |
| 238 | virtual void setSwizzleA(unsigned int sampler, SwizzleType swizzleA); |
| 239 | |
| 240 | virtual void setWriteSRGB(bool sRGB); |
| 241 | virtual void setDepthBufferEnable(bool depthBufferEnable); |
| 242 | virtual void setDepthCompare(DepthCompareMode depthCompareMode); |
| 243 | virtual void setAlphaCompare(AlphaCompareMode alphaCompareMode); |
| 244 | virtual void setDepthWriteEnable(bool depthWriteEnable); |
| 245 | virtual void setAlphaTestEnable(bool alphaTestEnable); |
| 246 | virtual void setCullMode(CullMode cullMode); |
| 247 | virtual void setColorWriteMask(int index, int rgbaMask); |
| 248 | |
| 249 | virtual void setColorLogicOpEnabled(bool colorLogicOpEnabled); |
| 250 | virtual void setLogicalOperation(LogicalOperation logicalOperation); |
| 251 | |
| 252 | virtual void setStencilEnable(bool stencilEnable); |
| 253 | virtual void setStencilCompare(StencilCompareMode stencilCompareMode); |
| 254 | virtual void setStencilReference(int stencilReference); |
| 255 | virtual void setStencilMask(int stencilMask); |
| 256 | virtual void setStencilFailOperation(StencilOperation stencilFailOperation); |
| 257 | virtual void setStencilPassOperation(StencilOperation stencilPassOperation); |
| 258 | virtual void setStencilZFailOperation(StencilOperation stencilZFailOperation); |
| 259 | virtual void setStencilWriteMask(int stencilWriteMask); |
| 260 | virtual void setTwoSidedStencil(bool enable); |
| 261 | virtual void setStencilCompareCCW(StencilCompareMode stencilCompareMode); |
| 262 | virtual void setStencilReferenceCCW(int stencilReference); |
| 263 | virtual void setStencilMaskCCW(int stencilMask); |
| 264 | virtual void setStencilFailOperationCCW(StencilOperation stencilFailOperation); |
| 265 | virtual void setStencilPassOperationCCW(StencilOperation stencilPassOperation); |
| 266 | virtual void setStencilZFailOperationCCW(StencilOperation stencilZFailOperation); |
| 267 | virtual void setStencilWriteMaskCCW(int stencilWriteMask); |
| 268 | |
| 269 | virtual void setTextureFactor(const Color<float> &textureFactor); |
| 270 | virtual void setBlendConstant(const Color<float> &blendConstant); |
| 271 | |
| 272 | virtual void setFillMode(FillMode fillMode); |
| 273 | virtual void setShadingMode(ShadingMode shadingMode); |
| 274 | |
| 275 | virtual void setAlphaBlendEnable(bool alphaBlendEnable); |
| 276 | virtual void setSourceBlendFactor(BlendFactor sourceBlendFactor); |
| 277 | virtual void setDestBlendFactor(BlendFactor destBlendFactor); |
| 278 | virtual void setBlendOperation(BlendOperation blendOperation); |
| 279 | |
| 280 | virtual void setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable); |
| 281 | virtual void setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha); |
| 282 | virtual void setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha); |
| 283 | virtual void setBlendOperationAlpha(BlendOperation blendOperationAlpha); |
| 284 | |
| 285 | virtual void setAlphaReference(float alphaReference); |
| 286 | |
| 287 | virtual void setGlobalMipmapBias(float bias); |
| 288 | |
| 289 | virtual void setFogStart(float start); |
| 290 | virtual void setFogEnd(float end); |
| 291 | virtual void setFogColor(Color<float> fogColor); |
| 292 | virtual void setFogDensity(float fogDensity); |
| 293 | virtual void setPixelFogMode(FogMode fogMode); |
| 294 | |
| 295 | virtual void setPerspectiveCorrection(bool perspectiveCorrection); |
| 296 | |
| 297 | virtual void setOcclusionEnabled(bool enable); |
| 298 | |
| 299 | protected: |
| 300 | const State update() const; |
| 301 | Routine *routine(const State &state); |
| 302 | void setRoutineCacheSize(int routineCacheSize); |
| 303 | |
| 304 | // Shader constants |
| 305 | word4 cW[8][4]; |
| 306 | float4 c[FRAGMENT_UNIFORM_VECTORS]; |
| 307 | int4 i[16]; |
| 308 | bool b[16]; |
| 309 | |
| 310 | // Other semi-constants |
| 311 | Stencil stencil; |
| 312 | Stencil stencilCCW; |
| 313 | Fog fog; |
| 314 | Factor factor; |
| 315 | |
| 316 | private: |
| 317 | struct UniformBufferInfo |
| 318 | { |
| 319 | UniformBufferInfo(); |
| 320 | |
| 321 | Resource* buffer; |
| 322 | int offset; |
| 323 | }; |
| 324 | UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS]; |
| 325 | |
| 326 | void setFogRanges(float start, float end); |
| 327 | |
| 328 | Context *const context; |
| 329 | |
| 330 | RoutineCache<State> *routineCache; |
| 331 | }; |
| 332 | } |
| 333 | |
| 334 | #endif // sw_PixelProcessor_hpp |