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