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 "Memset.hpp" 20 #include "RoutineCache.hpp" 21 22 namespace sw { 23 24 class PixelShader; 25 class Rasterizer; 26 struct Texture; 27 struct DrawData; 28 struct Primitive; 29 30 using RasterizerFunction = FunctionT<void(const Primitive *primitive, int count, int cluster, int clusterCount, DrawData *draw)>; 31 32 class PixelProcessor 33 { 34 public: 35 struct States : Memset<States> 36 { 37 // Same as VkStencilOpState, but with no reference, as it's not part of the state 38 // (it doesn't require a different program to be generated) 39 struct StencilOpState 40 { 41 VkStencilOp failOp; 42 VkStencilOp passOp; 43 VkStencilOp depthFailOp; 44 VkCompareOp compareOp; 45 uint32_t compareMask; 46 uint32_t writeMask; 47 operator =sw::PixelProcessor::States::StencilOpState48 void operator=(const VkStencilOpState &rhs) 49 { 50 failOp = rhs.failOp; 51 passOp = rhs.passOp; 52 depthFailOp = rhs.depthFailOp; 53 compareOp = rhs.compareOp; 54 compareMask = rhs.compareMask; 55 writeMask = rhs.writeMask; 56 } 57 }; 58 Statessw::PixelProcessor::States59 States() 60 : Memset(this, 0) 61 {} 62 63 uint32_t computeHash(); 64 65 uint64_t shaderID; 66 67 unsigned int numClipDistances; 68 unsigned int numCullDistances; 69 70 VkCompareOp depthCompareMode; 71 bool depthWriteEnable; 72 73 bool stencilActive; 74 StencilOpState frontStencil; 75 StencilOpState backStencil; 76 77 bool depthTestActive; 78 bool occlusionEnabled; 79 bool perspective; 80 bool depthClamp; 81 82 BlendState blendState[RENDERTARGETS]; 83 84 unsigned int colorWriteMask; 85 VkFormat targetFormat[RENDERTARGETS]; 86 unsigned int multiSampleCount; 87 unsigned int multiSampleMask; 88 bool enableMultiSampling; 89 bool alphaToCoverage; 90 bool centroid; 91 VkFrontFace frontFace; 92 VkFormat depthFormat; 93 }; 94 95 struct State : States 96 { 97 bool operator==(const State &state) const; 98 colorWriteActivesw::PixelProcessor::State99 int colorWriteActive(int index) const 100 { 101 return (colorWriteMask >> (index * 4)) & 0xF; 102 } 103 104 uint32_t hash; 105 }; 106 107 struct Stencil 108 { 109 int64_t testMaskQ; 110 int64_t referenceMaskedQ; 111 int64_t referenceMaskedSignedQ; 112 int64_t writeMaskQ; 113 int64_t invWriteMaskQ; 114 int64_t referenceQ; 115 setsw::PixelProcessor::Stencil116 void set(int reference, int testMask, int writeMask) 117 { 118 referenceQ = replicate(reference); 119 testMaskQ = replicate(testMask); 120 writeMaskQ = replicate(writeMask); 121 invWriteMaskQ = ~writeMaskQ; 122 referenceMaskedQ = referenceQ & testMaskQ; 123 referenceMaskedSignedQ = replicate(((reference & testMask) + 0x80) & 0xFF); 124 } 125 replicatesw::PixelProcessor::Stencil126 static int64_t replicate(int b) 127 { 128 int64_t w = b & 0xFF; 129 130 return (w << 0) | (w << 8) | (w << 16) | (w << 24) | (w << 32) | (w << 40) | (w << 48) | (w << 56); 131 } 132 }; 133 134 struct Factor 135 { 136 word4 alphaReference4; 137 138 word4 blendConstant4W[4]; 139 float4 blendConstant4F[4]; 140 word4 invBlendConstant4W[4]; 141 float4 invBlendConstant4F[4]; 142 }; 143 144 public: 145 using RoutineType = RasterizerFunction::RoutineType; 146 147 PixelProcessor(); 148 149 virtual ~PixelProcessor(); 150 151 void setBlendConstant(const float4 &blendConstant); 152 153 protected: 154 const State update(const Context *context) const; 155 RoutineType routine(const State &state, vk::PipelineLayout const *pipelineLayout, 156 SpirvShader const *pixelShader, const vk::DescriptorSet::Bindings &descriptorSets); 157 void setRoutineCacheSize(int routineCacheSize); 158 159 // Other semi-constants 160 Factor factor; 161 162 private: 163 using RoutineCacheType = RoutineCacheT<State, RasterizerFunction::CFunctionType>; 164 RoutineCacheType *routineCache; 165 }; 166 167 } // namespace sw 168 169 #endif // sw_PixelProcessor_hpp 170