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_PixelRoutine_hpp 16 #define sw_PixelRoutine_hpp 17 18 #include "Device/QuadRasterizer.hpp" 19 20 #include <vector> 21 22 namespace sw { 23 24 class PixelShader; 25 class SamplerCore; 26 27 class PixelRoutine : public sw::QuadRasterizer 28 { 29 public: 30 PixelRoutine(const PixelProcessor::State &state, 31 const vk::PipelineLayout *pipelineLayout, 32 const SpirvShader *spirvShader, 33 const vk::Attachments &attachments, 34 const vk::DescriptorSet::Bindings &descriptorSets); 35 36 virtual ~PixelRoutine(); 37 38 protected: 39 using SampleSet = std::vector<int>; 40 41 SIMD::Float z[4]; // Multisampled z 42 SIMD::Float w; // Used as is 43 SIMD::Float rhw; // Reciprocal w 44 45 SpirvRoutine routine; 46 const vk::Attachments &attachments; 47 const vk::DescriptorSet::Bindings &descriptorSets; 48 49 virtual void setBuiltins(Int &x, Int &y, SIMD::Float (&z)[4], SIMD::Float &w, Int cMask[4], const SampleSet &samples) = 0; 50 virtual void executeShader(Int cMask[4], Int sMask[4], Int zMask[4], const SampleSet &samples) = 0; 51 virtual Bool alphaTest(Int cMask[4], const SampleSet &samples) = 0; 52 virtual void blendColor(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4], const SampleSet &samples) = 0; 53 54 void quad(Pointer<Byte> cBuffer[4], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y) override; 55 56 void alphaTest(Int &aMask, const Short4 &alpha); 57 void alphaToCoverage(Int cMask[4], const SIMD::Float &alpha, const SampleSet &samples); 58 59 void writeColor(int index, const Pointer<Byte> &cBuffer, const Int &x, Vector4f &color, const Int &sMask, const Int &zMask, const Int &cMask); 60 SIMD::Float4 alphaBlend(int index, const Pointer<Byte> &cBuffer, const SIMD::Float4 &sourceColor, const Int &x); 61 62 bool isSRGB(int index) const; 63 64 private: 65 bool hasStencilReplaceRef() const; 66 Byte8 stencilReplaceRef(); 67 void stencilTest(const Pointer<Byte> &sBuffer, const Int &x, Int sMask[4], const SampleSet &samples); 68 void stencilTest(Byte8 &value, VkCompareOp stencilCompareMode, bool isBack); 69 Byte8 stencilOperation(const Byte8 &bufferValue, const PixelProcessor::States::StencilOpState &ops, bool isBack, const Int &zMask, const Int &sMask); 70 Byte8 stencilOperation(const Byte8 &bufferValue, VkStencilOp operation, bool isBack); 71 SIMD::Float clampDepth(const SIMD::Float &z); 72 Bool depthTest(const Pointer<Byte> &zBuffer, int q, const Int &x, const SIMD::Float &z, const Int &sMask, Int &zMask, const Int &cMask); 73 void depthBoundsTest(const Pointer<Byte> &zBuffer, int q, const Int &x, Int &zMask, Int &cMask); 74 75 void readPixel(int index, const Pointer<Byte> &cBuffer, const Int &x, Vector4s &pixel); 76 enum BlendFactorModifier 77 { 78 None, 79 OneMinus 80 }; 81 Float blendConstant(vk::Format format, int component, BlendFactorModifier modifier = None); 82 void blendFactorRGB(SIMD::Float4 &blendFactorRGB, const SIMD::Float4 &sourceColor, const SIMD::Float4 &destColor, VkBlendFactor colorBlendFactor, vk::Format format); 83 void blendFactorAlpha(SIMD::Float &blendFactorAlpha, const SIMD::Float &sourceAlpha, const SIMD::Float &destAlpha, VkBlendFactor alphaBlendFactor, vk::Format format); 84 85 bool blendFactorCanExceedFormatRange(VkBlendFactor blendFactor, vk::Format format); 86 SIMD::Float4 computeAdvancedBlendMode(int index, const SIMD::Float4 &src, const SIMD::Float4 &dst, const SIMD::Float4 &srcFactor, const SIMD::Float4 &dstFactor); 87 SIMD::Float blendOpOverlay(SIMD::Float &src, SIMD::Float &dst); 88 SIMD::Float blendOpColorDodge(SIMD::Float &src, SIMD::Float &dst); 89 SIMD::Float blendOpColorBurn(SIMD::Float &src, SIMD::Float &dst); 90 SIMD::Float blendOpHardlight(SIMD::Float &src, SIMD::Float &dst); 91 SIMD::Float blendOpSoftlight(SIMD::Float &src, SIMD::Float &dst); 92 void setLumSat(SIMD::Float4 &cbase, SIMD::Float4 &csat, SIMD::Float4 &clum, SIMD::Float &x, SIMD::Float &y, SIMD::Float &z); 93 void setLum(SIMD::Float4 &cbase, SIMD::Float4 &clum, SIMD::Float &x, SIMD::Float &y, SIMD::Float &z); 94 SIMD::Float computeLum(SIMD::Float &color, SIMD::Float &lum, SIMD::Float &mincol, SIMD::Float &maxcol, SIMD::Int &negative, SIMD::Int &aboveOne); 95 SIMD::Float maxRGB(SIMD::Float4 &c); 96 SIMD::Float minRGB(SIMD::Float4 &c); 97 SIMD::Float lumRGB(SIMD::Float4 &c); 98 void premultiply(SIMD::Float4 &c); 99 100 void writeStencil(Pointer<Byte> &sBuffer, const Int &x, const Int sMask[4], const Int zMask[4], const Int cMask[4], const SampleSet &samples); 101 void writeDepth(Pointer<Byte> &zBuffer, const Int &x, const Int zMask[4], const SampleSet &samples); 102 void occlusionSampleCount(const Int zMask[4], const Int sMask[4], const SampleSet &samples); 103 104 SIMD::Float readDepth32F(const Pointer<Byte> &zBuffer, int q, const Int &x) const; 105 SIMD::Float readDepth16(const Pointer<Byte> &zBuffer, int q, const Int &x) const; 106 107 void writeDepth32F(Pointer<Byte> &zBuffer, int q, const Int &x, const Float4 &z, const Int &zMask); 108 void writeDepth16(Pointer<Byte> &zBuffer, int q, const Int &x, const Float4 &z, const Int &zMask); 109 110 Int4 depthBoundsTest32F(const Pointer<Byte> &zBuffer, int q, const Int &x); 111 Int4 depthBoundsTest16(const Pointer<Byte> &zBuffer, int q, const Int &x); 112 113 // Derived state parameters 114 const bool shaderContainsInterpolation; // TODO(b/194714095) 115 const bool perSampleShading; 116 const int invocationCount; 117 118 SampleSet getSampleSet(int invocation) const; 119 }; 120 121 } // namespace sw 122 123 #endif // sw_PixelRoutine_hpp 124