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]; |
Nicolas Capens | 3b4c93f | 2016-05-18 12:51:37 -0400 | [diff] [blame] | 105 | Interpolant fog; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 106 | }; |
| 107 | |
Nicolas Capens | 3b4c93f | 2016-05-18 12:51:37 -0400 | [diff] [blame] | 108 | Interpolant interpolant[MAX_FRAGMENT_INPUTS]; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 109 | }; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | struct State : States |
| 113 | { |
| 114 | State(); |
| 115 | |
| 116 | bool operator==(const State &state) const; |
| 117 | |
| 118 | int colorWriteActive(int index) const |
| 119 | { |
| 120 | return (colorWriteMask >> (index * 4)) & 0xF; |
| 121 | } |
| 122 | |
| 123 | bool alphaTestActive() const |
| 124 | { |
Alexis Hetu | a9beed3 | 2016-12-13 16:48:01 -0500 | [diff] [blame^] | 125 | return (alphaCompareMode != ALPHA_ALWAYS) || (transparencyAntialiasing != TRANSPARENCY_NONE); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | bool pixelFogActive() const |
| 129 | { |
| 130 | return pixelFogMode != FOG_NONE; |
| 131 | } |
| 132 | |
| 133 | unsigned int hash; |
| 134 | }; |
| 135 | |
| 136 | struct Stencil |
| 137 | { |
| 138 | int64_t testMaskQ; |
| 139 | int64_t referenceMaskedQ; |
| 140 | int64_t referenceMaskedSignedQ; |
| 141 | int64_t writeMaskQ; |
| 142 | int64_t invWriteMaskQ; |
| 143 | int64_t referenceQ; |
| 144 | |
| 145 | void set(int reference, int testMask, int writeMask) |
| 146 | { |
| 147 | referenceQ = replicate(reference); |
| 148 | testMaskQ = replicate(testMask); |
| 149 | writeMaskQ = replicate(writeMask); |
| 150 | invWriteMaskQ = ~writeMaskQ; |
| 151 | referenceMaskedQ = referenceQ & testMaskQ; |
| 152 | referenceMaskedSignedQ = replicate((reference + 0x80) & 0xFF & testMask); |
| 153 | } |
| 154 | |
| 155 | static int64_t replicate(int b) |
| 156 | { |
| 157 | int64_t w = b & 0xFF; |
| 158 | |
| 159 | return (w << 0) | (w << 8) | (w << 16) | (w << 24) | (w << 32) | (w << 40) | (w << 48) | (w << 56); |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | struct Fog |
| 164 | { |
| 165 | float4 scale; |
| 166 | float4 offset; |
| 167 | word4 color4[3]; |
| 168 | float4 colorF[3]; |
| 169 | float4 densityE; |
| 170 | float4 density2E; |
| 171 | }; |
| 172 | |
| 173 | struct Factor |
| 174 | { |
| 175 | word4 textureFactor4[4]; |
| 176 | |
| 177 | word4 alphaReference4; |
| 178 | |
| 179 | word4 blendConstant4W[4]; |
| 180 | float4 blendConstant4F[4]; |
| 181 | word4 invBlendConstant4W[4]; |
| 182 | float4 invBlendConstant4F[4]; |
| 183 | }; |
| 184 | |
| 185 | public: |
| 186 | typedef void (*RoutinePointer)(const Primitive *primitive, int count, int thread, DrawData *draw); |
| 187 | |
| 188 | PixelProcessor(Context *context); |
| 189 | |
| 190 | virtual ~PixelProcessor(); |
| 191 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 192 | void setFloatConstant(unsigned int index, const float value[4]); |
| 193 | void setIntegerConstant(unsigned int index, const int value[4]); |
| 194 | void setBooleanConstant(unsigned int index, int boolean); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 195 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 196 | void setUniformBuffer(int index, sw::Resource* buffer, int offset); |
| 197 | void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 198 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 199 | void setRenderTarget(int index, Surface *renderTarget); |
| 200 | void setDepthBuffer(Surface *depthBuffer); |
| 201 | void setStencilBuffer(Surface *stencilBuffer); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 202 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 203 | void setTexCoordIndex(unsigned int stage, int texCoordIndex); |
| 204 | void setStageOperation(unsigned int stage, TextureStage::StageOperation stageOperation); |
| 205 | void setFirstArgument(unsigned int stage, TextureStage::SourceArgument firstArgument); |
| 206 | void setSecondArgument(unsigned int stage, TextureStage::SourceArgument secondArgument); |
| 207 | void setThirdArgument(unsigned int stage, TextureStage::SourceArgument thirdArgument); |
| 208 | void setStageOperationAlpha(unsigned int stage, TextureStage::StageOperation stageOperationAlpha); |
| 209 | void setFirstArgumentAlpha(unsigned int stage, TextureStage::SourceArgument firstArgumentAlpha); |
| 210 | void setSecondArgumentAlpha(unsigned int stage, TextureStage::SourceArgument secondArgumentAlpha); |
| 211 | void setThirdArgumentAlpha(unsigned int stage, TextureStage::SourceArgument thirdArgumentAlpha); |
| 212 | void setFirstModifier(unsigned int stage, TextureStage::ArgumentModifier firstModifier); |
| 213 | void setSecondModifier(unsigned int stage, TextureStage::ArgumentModifier secondModifier); |
| 214 | void setThirdModifier(unsigned int stage, TextureStage::ArgumentModifier thirdModifier); |
| 215 | void setFirstModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier firstModifierAlpha); |
| 216 | void setSecondModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier secondModifierAlpha); |
| 217 | void setThirdModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier thirdModifierAlpha); |
| 218 | void setDestinationArgument(unsigned int stage, TextureStage::DestinationArgument destinationArgument); |
| 219 | void setConstantColor(unsigned int stage, const Color<float> &constantColor); |
| 220 | void setBumpmapMatrix(unsigned int stage, int element, float value); |
| 221 | void setLuminanceScale(unsigned int stage, float value); |
| 222 | void setLuminanceOffset(unsigned int stage, float value); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 223 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 224 | void setTextureFilter(unsigned int sampler, FilterType textureFilter); |
| 225 | void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter); |
| 226 | void setGatherEnable(unsigned int sampler, bool enable); |
| 227 | void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode); |
| 228 | void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode); |
| 229 | void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode); |
| 230 | void setReadSRGB(unsigned int sampler, bool sRGB); |
| 231 | void setMipmapLOD(unsigned int sampler, float bias); |
| 232 | void setBorderColor(unsigned int sampler, const Color<float> &borderColor); |
| 233 | void setMaxAnisotropy(unsigned int sampler, float maxAnisotropy); |
| 234 | void setSwizzleR(unsigned int sampler, SwizzleType swizzleR); |
| 235 | void setSwizzleG(unsigned int sampler, SwizzleType swizzleG); |
| 236 | void setSwizzleB(unsigned int sampler, SwizzleType swizzleB); |
| 237 | void setSwizzleA(unsigned int sampler, SwizzleType swizzleA); |
| 238 | void setBaseLevel(unsigned int sampler, int baseLevel); |
| 239 | void setMaxLevel(unsigned int sampler, int maxLevel); |
| 240 | void setMinLod(unsigned int sampler, float minLod); |
| 241 | void setMaxLod(unsigned int sampler, float maxLod); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 242 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 243 | void setWriteSRGB(bool sRGB); |
| 244 | void setDepthBufferEnable(bool depthBufferEnable); |
| 245 | void setDepthCompare(DepthCompareMode depthCompareMode); |
| 246 | void setAlphaCompare(AlphaCompareMode alphaCompareMode); |
| 247 | void setDepthWriteEnable(bool depthWriteEnable); |
| 248 | void setAlphaTestEnable(bool alphaTestEnable); |
| 249 | void setCullMode(CullMode cullMode); |
| 250 | void setColorWriteMask(int index, int rgbaMask); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 251 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 252 | void setColorLogicOpEnabled(bool colorLogicOpEnabled); |
| 253 | void setLogicalOperation(LogicalOperation logicalOperation); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 254 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 255 | void setStencilEnable(bool stencilEnable); |
| 256 | void setStencilCompare(StencilCompareMode stencilCompareMode); |
| 257 | void setStencilReference(int stencilReference); |
| 258 | void setStencilMask(int stencilMask); |
| 259 | void setStencilFailOperation(StencilOperation stencilFailOperation); |
| 260 | void setStencilPassOperation(StencilOperation stencilPassOperation); |
| 261 | void setStencilZFailOperation(StencilOperation stencilZFailOperation); |
| 262 | void setStencilWriteMask(int stencilWriteMask); |
| 263 | void setTwoSidedStencil(bool enable); |
| 264 | void setStencilCompareCCW(StencilCompareMode stencilCompareMode); |
| 265 | void setStencilReferenceCCW(int stencilReference); |
| 266 | void setStencilMaskCCW(int stencilMask); |
| 267 | void setStencilFailOperationCCW(StencilOperation stencilFailOperation); |
| 268 | void setStencilPassOperationCCW(StencilOperation stencilPassOperation); |
| 269 | void setStencilZFailOperationCCW(StencilOperation stencilZFailOperation); |
| 270 | void setStencilWriteMaskCCW(int stencilWriteMask); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 271 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 272 | void setTextureFactor(const Color<float> &textureFactor); |
| 273 | void setBlendConstant(const Color<float> &blendConstant); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 274 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 275 | void setFillMode(FillMode fillMode); |
| 276 | void setShadingMode(ShadingMode shadingMode); |
Nicolas Capens | 3b4c93f | 2016-05-18 12:51:37 -0400 | [diff] [blame] | 277 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 278 | void setAlphaBlendEnable(bool alphaBlendEnable); |
| 279 | void setSourceBlendFactor(BlendFactor sourceBlendFactor); |
| 280 | void setDestBlendFactor(BlendFactor destBlendFactor); |
| 281 | void setBlendOperation(BlendOperation blendOperation); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 282 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 283 | void setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable); |
| 284 | void setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha); |
| 285 | void setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha); |
| 286 | void setBlendOperationAlpha(BlendOperation blendOperationAlpha); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 287 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 288 | void setAlphaReference(float alphaReference); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 289 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 290 | void setGlobalMipmapBias(float bias); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 291 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 292 | void setFogStart(float start); |
| 293 | void setFogEnd(float end); |
| 294 | void setFogColor(Color<float> fogColor); |
| 295 | void setFogDensity(float fogDensity); |
| 296 | void setPixelFogMode(FogMode fogMode); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 297 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 298 | void setPerspectiveCorrection(bool perspectiveCorrection); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 299 | |
Alexis Hetu | c634fb6 | 2016-06-02 10:53:13 -0400 | [diff] [blame] | 300 | void setOcclusionEnabled(bool enable); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 301 | |
| 302 | protected: |
| 303 | const State update() const; |
| 304 | Routine *routine(const State &state); |
| 305 | void setRoutineCacheSize(int routineCacheSize); |
| 306 | |
| 307 | // Shader constants |
| 308 | word4 cW[8][4]; |
| 309 | float4 c[FRAGMENT_UNIFORM_VECTORS]; |
| 310 | int4 i[16]; |
| 311 | bool b[16]; |
| 312 | |
| 313 | // Other semi-constants |
| 314 | Stencil stencil; |
| 315 | Stencil stencilCCW; |
| 316 | Fog fog; |
| 317 | Factor factor; |
| 318 | |
| 319 | private: |
| 320 | struct UniformBufferInfo |
| 321 | { |
| 322 | UniformBufferInfo(); |
| 323 | |
| 324 | Resource* buffer; |
| 325 | int offset; |
| 326 | }; |
| 327 | UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS]; |
| 328 | |
| 329 | void setFogRanges(float start, float end); |
| 330 | |
| 331 | Context *const context; |
| 332 | |
| 333 | RoutineCache<State> *routineCache; |
| 334 | }; |
| 335 | } |
| 336 | |
| 337 | #endif // sw_PixelProcessor_hpp |