| 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 | |
| 49 | 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 | |
| 60 | States() : Memset(this, 0) {} |
| 61 | |
| 62 | uint32_t computeHash(); |
| 63 | |
| 64 | uint64_t shaderID; |
| 65 | |
| 66 | VkCompareOp depthCompareMode; |
| 67 | bool depthWriteEnable; |
| 68 | bool quadLayoutDepthBuffer; |
| 69 | |
| 70 | bool stencilActive; |
| 71 | StencilOpState frontStencil; |
| 72 | StencilOpState backStencil; |
| 73 | |
| 74 | bool depthTestActive; |
| 75 | bool occlusionEnabled; |
| 76 | bool perspective; |
| 77 | bool depthClamp; |
| 78 | |
| 79 | BlendState blendState[RENDERTARGETS]; |
| 80 | |
| 81 | unsigned int colorWriteMask; |
| 82 | VkFormat targetFormat[RENDERTARGETS]; |
| 83 | unsigned int multiSample; |
| 84 | unsigned int multiSampleMask; |
| 85 | bool alphaToCoverage; |
| 86 | bool centroid; |
| 87 | VkFrontFace frontFace; |
| 88 | VkFormat depthFormat; |
| 89 | }; |
| 90 | |
| 91 | struct State : States |
| 92 | { |
| 93 | bool operator==(const State &state) const; |
| 94 | |
| 95 | int colorWriteActive(int index) const |
| 96 | { |
| 97 | return (colorWriteMask >> (index * 4)) & 0xF; |
| 98 | } |
| 99 | |
| 100 | uint32_t hash; |
| 101 | }; |
| 102 | |
| 103 | struct Stencil |
| 104 | { |
| 105 | int64_t testMaskQ; |
| 106 | int64_t referenceMaskedQ; |
| 107 | int64_t referenceMaskedSignedQ; |
| 108 | int64_t writeMaskQ; |
| 109 | int64_t invWriteMaskQ; |
| 110 | int64_t referenceQ; |
| 111 | |
| 112 | void set(int reference, int testMask, int writeMask) |
| 113 | { |
| 114 | referenceQ = replicate(reference); |
| 115 | testMaskQ = replicate(testMask); |
| 116 | writeMaskQ = replicate(writeMask); |
| 117 | invWriteMaskQ = ~writeMaskQ; |
| 118 | referenceMaskedQ = referenceQ & testMaskQ; |
| 119 | referenceMaskedSignedQ = replicate(((reference & testMask) + 0x80) & 0xFF); |
| 120 | } |
| 121 | |
| 122 | static int64_t replicate(int b) |
| 123 | { |
| 124 | int64_t w = b & 0xFF; |
| 125 | |
| 126 | return (w << 0) | (w << 8) | (w << 16) | (w << 24) | (w << 32) | (w << 40) | (w << 48) | (w << 56); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | struct Factor |
| 131 | { |
| 132 | word4 alphaReference4; |
| 133 | |
| 134 | word4 blendConstant4W[4]; |
| 135 | float4 blendConstant4F[4]; |
| 136 | word4 invBlendConstant4W[4]; |
| 137 | float4 invBlendConstant4F[4]; |
| 138 | }; |
| 139 | |
| 140 | public: |
| 141 | using RoutineType = RasterizerFunction::RoutineType; |
| 142 | |
| 143 | PixelProcessor(); |
| 144 | |
| 145 | virtual ~PixelProcessor(); |
| 146 | |
| 147 | void setBlendConstant(const Color<float> &blendConstant); |
| 148 | |
| 149 | protected: |
| 150 | const State update(const Context* context) const; |
| 151 | RoutineType routine(const State &state, vk::PipelineLayout const *pipelineLayout, |
| 152 | SpirvShader const *pixelShader, const vk::DescriptorSet::Bindings &descriptorSets); |
| 153 | void setRoutineCacheSize(int routineCacheSize); |
| 154 | |
| 155 | // Other semi-constants |
| 156 | Factor factor; |
| 157 | |
| 158 | private: |
| 159 | using RoutineCacheType = RoutineCacheT<State, RasterizerFunction::CFunctionType>; |
| 160 | RoutineCacheType *routineCache; |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | #endif // sw_PixelProcessor_hpp |
| 165 | |