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