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