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