• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::DescriptorSet::Bindings &descriptorSets);
34 
35 	virtual ~PixelRoutine();
36 
37 protected:
38 	using SampleSet = std::vector<int>;
39 
40 	SIMD::Float z[4];  // Multisampled z
41 	SIMD::Float w;     // Used as is
42 	SIMD::Float rhw;   // Reciprocal w
43 
44 	SpirvRoutine routine;
45 	const vk::DescriptorSet::Bindings &descriptorSets;
46 
47 	virtual void setBuiltins(Int &x, Int &y, SIMD::Float (&z)[4], SIMD::Float &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 SIMD::Float &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 	SIMD::Float4 alphaBlend(int index, const Pointer<Byte> &cBuffer, const SIMD::Float4 &sourceColor, const Int &x);
59 
60 	bool isSRGB(int index) const;
61 
62 private:
63 	bool hasStencilReplaceRef() const;
64 	Byte8 stencilReplaceRef();
65 	void stencilTest(const Pointer<Byte> &sBuffer, const Int &x, Int sMask[4], const SampleSet &samples);
66 	void stencilTest(Byte8 &value, VkCompareOp stencilCompareMode, bool isBack);
67 	Byte8 stencilOperation(const Byte8 &bufferValue, const PixelProcessor::States::StencilOpState &ops, bool isBack, const Int &zMask, const Int &sMask);
68 	Byte8 stencilOperation(const Byte8 &bufferValue, VkStencilOp operation, bool isBack);
69 	SIMD::Float clampDepth(const SIMD::Float &z);
70 	Bool depthTest(const Pointer<Byte> &zBuffer, int q, const Int &x, const SIMD::Float &z, const Int &sMask, Int &zMask, const Int &cMask);
71 	void depthBoundsTest(const Pointer<Byte> &zBuffer, int q, const Int &x, Int &zMask, Int &cMask);
72 
73 	void readPixel(int index, const Pointer<Byte> &cBuffer, const Int &x, Vector4s &pixel);
74 	enum BlendFactorModifier
75 	{
76 		None,
77 		OneMinus
78 	};
79 	Float blendConstant(vk::Format format, int component, BlendFactorModifier modifier = None);
80 	void blendFactorRGB(SIMD::Float4 &blendFactorRGB, const SIMD::Float4 &sourceColor, const SIMD::Float4 &destColor, VkBlendFactor colorBlendFactor, vk::Format format);
81 	void blendFactorAlpha(SIMD::Float &blendFactorAlpha, const SIMD::Float &sourceAlpha, const SIMD::Float &destAlpha, VkBlendFactor alphaBlendFactor, vk::Format format);
82 
83 	bool blendFactorCanExceedFormatRange(VkBlendFactor blendFactor, vk::Format format);
84 	SIMD::Float4 computeAdvancedBlendMode(int index, const SIMD::Float4 &src, const SIMD::Float4 &dst, const SIMD::Float4 &srcFactor, const SIMD::Float4 &dstFactor);
85 	SIMD::Float blendOpOverlay(SIMD::Float &src, SIMD::Float &dst);
86 	SIMD::Float blendOpColorDodge(SIMD::Float &src, SIMD::Float &dst);
87 	SIMD::Float blendOpColorBurn(SIMD::Float &src, SIMD::Float &dst);
88 	SIMD::Float blendOpHardlight(SIMD::Float &src, SIMD::Float &dst);
89 	SIMD::Float blendOpSoftlight(SIMD::Float &src, SIMD::Float &dst);
90 	void setLumSat(SIMD::Float4 &cbase, SIMD::Float4 &csat, SIMD::Float4 &clum, SIMD::Float &x, SIMD::Float &y, SIMD::Float &z);
91 	void setLum(SIMD::Float4 &cbase, SIMD::Float4 &clum, SIMD::Float &x, SIMD::Float &y, SIMD::Float &z);
92 	SIMD::Float computeLum(SIMD::Float &color, SIMD::Float &lum, SIMD::Float &mincol, SIMD::Float &maxcol, SIMD::Int &negative, SIMD::Int &aboveOne);
93 	SIMD::Float maxRGB(SIMD::Float4 &c);
94 	SIMD::Float minRGB(SIMD::Float4 &c);
95 	SIMD::Float lumRGB(SIMD::Float4 &c);
96 	void premultiply(SIMD::Float4 &c);
97 
98 	void writeStencil(Pointer<Byte> &sBuffer, const Int &x, const Int sMask[4], const Int zMask[4], const Int cMask[4], const SampleSet &samples);
99 	void writeDepth(Pointer<Byte> &zBuffer, const Int &x, const Int zMask[4], const SampleSet &samples);
100 	void occlusionSampleCount(const Int zMask[4], const Int sMask[4], const SampleSet &samples);
101 
102 	SIMD::Float readDepth32F(const Pointer<Byte> &zBuffer, int q, const Int &x) const;
103 	SIMD::Float readDepth16(const Pointer<Byte> &zBuffer, int q, const Int &x) const;
104 
105 	void writeDepth32F(Pointer<Byte> &zBuffer, int q, const Int &x, const Float4 &z, const Int &zMask);
106 	void writeDepth16(Pointer<Byte> &zBuffer, int q, const Int &x, const Float4 &z, const Int &zMask);
107 
108 	Int4 depthBoundsTest32F(const Pointer<Byte> &zBuffer, int q, const Int &x);
109 	Int4 depthBoundsTest16(const Pointer<Byte> &zBuffer, int q, const Int &x);
110 
111 	// Derived state parameters
112 	const bool shaderContainsInterpolation;  // TODO(b/194714095)
113 	const bool shaderContainsSampleQualifier;
114 	const bool perSampleShading;
115 	const int invocationCount;
116 
117 	SampleSet getSampleSet(int invocation) const;
118 };
119 
120 }  // namespace sw
121 
122 #endif  // sw_PixelRoutine_hpp
123