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 #include "QuadRasterizer.hpp"
16
17 #include "Primitive.hpp"
18 #include "Renderer.hpp"
19 #include "Pipeline/Constants.hpp"
20 #include "System/Debug.hpp"
21 #include "System/Math.hpp"
22 #include "Vulkan/VkDevice.hpp"
23
24 namespace sw {
25
QuadRasterizer(const PixelProcessor::State & state,SpirvShader const * spirvShader)26 QuadRasterizer::QuadRasterizer(const PixelProcessor::State &state, SpirvShader const *spirvShader)
27 : state(state)
28 , spirvShader{ spirvShader }
29 {
30 }
31
~QuadRasterizer()32 QuadRasterizer::~QuadRasterizer()
33 {
34 }
35
generate()36 void QuadRasterizer::generate()
37 {
38 constants = device + OFFSET(vk::Device, constants);
39 occlusion = 0;
40
41 Do
42 {
43 Int yMin = *Pointer<Int>(primitive + OFFSET(Primitive, yMin));
44 Int yMax = *Pointer<Int>(primitive + OFFSET(Primitive, yMax));
45
46 Int cluster2 = cluster + cluster;
47 yMin += clusterCount * 2 - 2 - cluster2;
48 yMin &= -clusterCount * 2;
49 yMin += cluster2;
50
51 If(yMin < yMax)
52 {
53 rasterize(yMin, yMax);
54 }
55
56 primitive += sizeof(Primitive) * state.multiSampleCount;
57 count--;
58 }
59 Until(count == 0);
60
61 if(state.occlusionEnabled)
62 {
63 UInt clusterOcclusion = *Pointer<UInt>(data + OFFSET(DrawData, occlusion) + 4 * cluster);
64 clusterOcclusion += occlusion;
65 *Pointer<UInt>(data + OFFSET(DrawData, occlusion) + 4 * cluster) = clusterOcclusion;
66 }
67
68 Return();
69 }
70
rasterize(Int & yMin,Int & yMax)71 void QuadRasterizer::rasterize(Int &yMin, Int &yMax)
72 {
73 Pointer<Byte> cBuffer[MAX_COLOR_BUFFERS];
74 Pointer<Byte> zBuffer;
75 Pointer<Byte> sBuffer;
76
77 Int clusterCountLog2 = 31 - Ctlz(UInt(clusterCount), false);
78
79 for(int index = 0; index < MAX_COLOR_BUFFERS; index++)
80 {
81 if(state.colorWriteActive(index))
82 {
83 cBuffer[index] = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, colorBuffer[index])) + yMin * *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
84 }
85 }
86
87 if(state.depthTestActive || state.depthBoundsTestActive)
88 {
89 zBuffer = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, depthBuffer)) + yMin * *Pointer<Int>(data + OFFSET(DrawData, depthPitchB));
90 }
91
92 if(state.stencilActive)
93 {
94 sBuffer = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, stencilBuffer)) + yMin * *Pointer<Int>(data + OFFSET(DrawData, stencilPitchB));
95 }
96
97 Int y = yMin;
98
99 Do
100 {
101 Int x0a = Int(*Pointer<Short>(primitive + OFFSET(Primitive, outline->left) + (y + 0) * sizeof(Primitive::Span)));
102 Int x0b = Int(*Pointer<Short>(primitive + OFFSET(Primitive, outline->left) + (y + 1) * sizeof(Primitive::Span)));
103 Int x0 = Min(x0a, x0b);
104
105 for(unsigned int q = 1; q < state.multiSampleCount; q++)
106 {
107 x0a = Int(*Pointer<Short>(primitive + q * sizeof(Primitive) + OFFSET(Primitive, outline->left) + (y + 0) * sizeof(Primitive::Span)));
108 x0b = Int(*Pointer<Short>(primitive + q * sizeof(Primitive) + OFFSET(Primitive, outline->left) + (y + 1) * sizeof(Primitive::Span)));
109 x0 = Min(x0, Min(x0a, x0b));
110 }
111
112 x0 &= 0xFFFFFFFE;
113
114 Int x1a = Int(*Pointer<Short>(primitive + OFFSET(Primitive, outline->right) + (y + 0) * sizeof(Primitive::Span)));
115 Int x1b = Int(*Pointer<Short>(primitive + OFFSET(Primitive, outline->right) + (y + 1) * sizeof(Primitive::Span)));
116 Int x1 = Max(x1a, x1b);
117
118 for(unsigned int q = 1; q < state.multiSampleCount; q++)
119 {
120 x1a = Int(*Pointer<Short>(primitive + q * sizeof(Primitive) + OFFSET(Primitive, outline->right) + (y + 0) * sizeof(Primitive::Span)));
121 x1b = Int(*Pointer<Short>(primitive + q * sizeof(Primitive) + OFFSET(Primitive, outline->right) + (y + 1) * sizeof(Primitive::Span)));
122 x1 = Max(x1, Max(x1a, x1b));
123 }
124
125 Float4 yyyy = Float4(Float(y)) + *Pointer<Float4>(primitive + OFFSET(Primitive, yQuad), 16);
126
127 if(interpolateZ())
128 {
129 for(unsigned int q = 0; q < state.multiSampleCount; q++)
130 {
131 Float4 y = yyyy;
132
133 if(state.enableMultiSampling)
134 {
135 y += *Pointer<Float4>(constants + OFFSET(Constants, Y) + q * sizeof(float4));
136 }
137
138 Dz[q] = *Pointer<Float4>(primitive + OFFSET(Primitive, z.C), 16) + y * *Pointer<Float4>(primitive + OFFSET(Primitive, z.B), 16);
139 }
140 }
141
142 If(x0 < x1)
143 {
144 if(interpolateW())
145 {
146 Dw = *Pointer<Float4>(primitive + OFFSET(Primitive, w.C), 16) + yyyy * *Pointer<Float4>(primitive + OFFSET(Primitive, w.B), 16);
147 }
148
149 if(spirvShader)
150 {
151 int packedInterpolant = 0;
152 for(int interfaceInterpolant = 0; interfaceInterpolant < MAX_INTERFACE_COMPONENTS; interfaceInterpolant++)
153 {
154 if(spirvShader->inputs[interfaceInterpolant].Type == SpirvShader::ATTRIBTYPE_UNUSED)
155 continue;
156
157 Dv[interfaceInterpolant] = *Pointer<Float4>(primitive + OFFSET(Primitive, V[packedInterpolant].C), 16);
158 if(!spirvShader->inputs[interfaceInterpolant].Flat)
159 {
160 Dv[interfaceInterpolant] +=
161 yyyy * *Pointer<Float4>(primitive + OFFSET(Primitive, V[packedInterpolant].B), 16);
162 }
163 packedInterpolant++;
164 }
165
166 for(unsigned int i = 0; i < state.numClipDistances; i++)
167 {
168 DclipDistance[i] = *Pointer<Float4>(primitive + OFFSET(Primitive, clipDistance[i].C), 16) +
169 yyyy * *Pointer<Float4>(primitive + OFFSET(Primitive, clipDistance[i].B), 16);
170 }
171
172 for(unsigned int i = 0; i < state.numCullDistances; i++)
173 {
174 DcullDistance[i] = *Pointer<Float4>(primitive + OFFSET(Primitive, cullDistance[i].C), 16) +
175 yyyy * *Pointer<Float4>(primitive + OFFSET(Primitive, cullDistance[i].B), 16);
176 }
177 }
178
179 Short4 xLeft[4];
180 Short4 xRight[4];
181
182 for(unsigned int q = 0; q < state.multiSampleCount; q++)
183 {
184 xLeft[q] = *Pointer<Short4>(primitive + q * sizeof(Primitive) + OFFSET(Primitive, outline) + y * sizeof(Primitive::Span));
185 xRight[q] = xLeft[q];
186
187 xLeft[q] = Swizzle(xLeft[q], 0x0022) - Short4(1, 2, 1, 2);
188 xRight[q] = Swizzle(xRight[q], 0x1133) - Short4(0, 1, 0, 1);
189 }
190
191 For(Int x = x0, x < x1, x += 2)
192 {
193 Short4 xxxx = Short4(x);
194 Int cMask[4];
195
196 for(unsigned int q = 0; q < state.multiSampleCount; q++)
197 {
198 if(state.multiSampleMask & (1 << q))
199 {
200 unsigned int i = state.enableMultiSampling ? q : 0;
201 Short4 mask = CmpGT(xxxx, xLeft[i]) & CmpGT(xRight[i], xxxx);
202 cMask[q] = SignMask(PackSigned(mask, mask)) & 0x0000000F;
203 }
204 }
205
206 quad(cBuffer, zBuffer, sBuffer, cMask, x, y);
207 }
208 }
209
210 for(int index = 0; index < MAX_COLOR_BUFFERS; index++)
211 {
212 if(state.colorWriteActive(index))
213 {
214 cBuffer[index] += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index])) << (1 + clusterCountLog2); // FIXME: Precompute
215 }
216 }
217
218 if(state.depthTestActive || state.depthBoundsTestActive)
219 {
220 zBuffer += *Pointer<Int>(data + OFFSET(DrawData, depthPitchB)) << (1 + clusterCountLog2); // FIXME: Precompute
221 }
222
223 if(state.stencilActive)
224 {
225 sBuffer += *Pointer<Int>(data + OFFSET(DrawData, stencilPitchB)) << (1 + clusterCountLog2); // FIXME: Precompute
226 }
227
228 y += 2 * clusterCount;
229 }
230 Until(y >= yMax);
231 }
232
interpolate(Float4 & x,Float4 & D,Float4 & rhw,Pointer<Byte> planeEquation,bool flat,bool perspective)233 Float4 QuadRasterizer::interpolate(Float4 &x, Float4 &D, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
234 {
235 if(flat)
236 {
237 return D;
238 }
239
240 Float4 interpolant = mulAdd(x, *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation, A), 16), D);
241
242 if(perspective)
243 {
244 interpolant *= rhw;
245 }
246
247 return interpolant;
248 }
249
interpolateZ() const250 bool QuadRasterizer::interpolateZ() const
251 {
252 return state.depthTestActive || (spirvShader && spirvShader->hasBuiltinInput(spv::BuiltInFragCoord));
253 }
254
interpolateW() const255 bool QuadRasterizer::interpolateW() const
256 {
257 // Note: could optimize cases where there is a fragment shader but it has no
258 // perspective-correct inputs, but that's vanishingly rare.
259 return spirvShader != nullptr;
260 }
261
262 } // namespace sw
263