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 vk::PipelineLayout const *pipelineLayout, 32 SpirvShader const *spirvShader, 33 const vk::DescriptorSet::Bindings &descriptorSets); 34 35 virtual ~PixelRoutine(); 36 37 protected: 38 using SampleSet = std::vector<int>; 39 40 Float4 z[4]; // Multisampled z 41 Float4 w; // Used as is 42 Float4 rhw; // Reciprocal w 43 44 SpirvRoutine routine; 45 const vk::DescriptorSet::Bindings &descriptorSets; 46 47 virtual void setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cMask[4], const SampleSet &samples) = 0; 48 virtual void executeShader(Int cMask[4], Int sMask[4], Int zMask[4], const SampleSet &samples) = 0; 49 virtual Bool alphaTest(Int cMask[4], const SampleSet &samples) = 0; 50 virtual void blendColor(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4], const SampleSet &samples) = 0; 51 52 void quad(Pointer<Byte> cBuffer[4], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y) override; 53 54 void alphaTest(Int &aMask, const Short4 &alpha); 55 void alphaToCoverage(Int cMask[4], const Float4 &alpha, const SampleSet &samples); 56 57 void writeColor(int index, const Pointer<Byte> &cBuffer, const Int &x, Vector4f &color, const Int &sMask, const Int &zMask, const Int &cMask); 58 Vector4f alphaBlend(int index, const Pointer<Byte> &cBuffer, const Vector4f &sourceColor, const Int &x); 59 void writeColor(int index, const Pointer<Byte> &cBuffer, const Int &x, Vector4s ¤t, const Int &sMask, const Int &zMask, const Int &cMask); 60 61 bool isSRGB(int index) const; 62 UShort4 convertFixed16(const Float4 &cf, bool saturate = true); 63 Float4 convertFloat32(const UShort4 &cf); 64 void linearToSRGB12_16(Vector4s &c); 65 66 private: 67 Byte8 stencilReplaceRef(bool isBack); 68 void stencilTest(const Pointer<Byte> &sBuffer, const Int &x, Int sMask[4], const SampleSet &samples); 69 void stencilTest(Byte8 &value, VkCompareOp stencilCompareMode, bool isBack); 70 void stencilOperation(Byte8 &newValue, const Byte8 &bufferValue, const PixelProcessor::States::StencilOpState &ops, bool isBack, const Int &zMask, const Int &sMask); 71 void stencilOperation(Byte8 &output, const Byte8 &bufferValue, VkStencilOp operation, bool isBack); 72 Float4 clampDepth(const Float4 &z); 73 Bool depthTest(const Pointer<Byte> &zBuffer, int q, const Int &x, const Float4 &z, const Int &sMask, Int &zMask, const Int &cMask); 74 void depthBoundsTest(const Pointer<Byte> &zBuffer, int q, const Int &x, Int &zMask, Int &cMask); 75 76 void readPixel(int index, const Pointer<Byte> &cBuffer, const Int &x, Vector4s &pixel); 77 enum BlendFactorModifier { None, OneMinus }; 78 Float blendConstant(vk::Format format, int component, BlendFactorModifier modifier = None); 79 void blendFactorRGB(Vector4f &blendFactorRGB, const Vector4f &sourceColor, const Vector4f &destColor, VkBlendFactor colorBlendFactor, vk::Format format); 80 void blendFactorAlpha(Float4 &blendFactorAlpha, const Float4 &sourceAlpha, const Float4 &destAlpha, VkBlendFactor alphaBlendFactor, vk::Format format); 81 bool blendFactorCanExceedFormatRange(VkBlendFactor blendFactor, vk::Format format); 82 Vector4f computeAdvancedBlendMode(int index, const Vector4f &src, const Vector4f &dst, const Vector4f &srcFactor, const Vector4f &dstFactor); 83 Float4 blendOpOverlay(Float4 &src, Float4 &dst); 84 Float4 blendOpColorDodge(Float4 &src, Float4 &dst); 85 Float4 blendOpColorBurn(Float4 &src, Float4 &dst); 86 Float4 blendOpHardlight(Float4 &src, Float4 &dst); 87 Float4 blendOpSoftlight(Float4 &src, Float4 &dst); 88 void setLumSat(Vector4f &cbase, Vector4f &csat, Vector4f &clum, Float4 &x, Float4 &y, Float4 &z); 89 void setLum(Vector4f &cbase, Vector4f &clum, Float4 &x, Float4 &y, Float4 &z); 90 Float4 computeLum(Float4 &color, Float4 &lum, Float4 &mincol, Float4 &maxcol, Int4 &negative, Int4 &aboveOne); 91 Float4 maxRGB(Vector4f &c); 92 Float4 minRGB(Vector4f &c); 93 Float4 lumRGB(Vector4f &c); 94 void premultiply(Vector4f &c); 95 void writeStencil(Pointer<Byte> &sBuffer, const Int &x, const Int sMask[4], const Int zMask[4], const Int cMask[4], const SampleSet &samples); 96 void writeDepth(Pointer<Byte> &zBuffer, const Int &x, const Int zMask[4], const SampleSet &samples); 97 void occlusionSampleCount(const Int zMask[4], const Int sMask[4], const SampleSet &samples); 98 99 void sRGBtoLinear16_12_16(Vector4s &c); 100 void linearToSRGB16_12_16(Vector4s &c); 101 Float4 sRGBtoLinear(const Float4 &x); 102 103 Bool depthTest32F(const Pointer<Byte> &zBuffer, int q, const Int &x, const Float4 &z, const Int &sMask, Int &zMask, const Int &cMask); 104 Bool depthTest16(const Pointer<Byte> &zBuffer, int q, const Int &x, const Float4 &z, const Int &sMask, Int &zMask, const Int &cMask); 105 106 void writeDepth32F(Pointer<Byte> &zBuffer, int q, const Int &x, const Float4 &z, const Int &zMask); 107 void writeDepth16(Pointer<Byte> &zBuffer, int q, const Int &x, const Float4 &z, const Int &zMask); 108 109 Int4 depthBoundsTest32F(const Pointer<Byte> &zBuffer, int q, const Int &x); 110 Int4 depthBoundsTest16(const Pointer<Byte> &zBuffer, int q, const Int &x); 111 112 // Derived state parameters 113 const bool shaderContainsInterpolation; // TODO(b/194714095) 114 const bool shaderContainsSampleQualifier; 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