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 "PixelProgram.hpp" |
| 16 | |
| 17 | #include "SamplerCore.hpp" |
Nicolas Capens | 1d8c8db | 2018-11-05 16:30:42 -0500 | [diff] [blame] | 18 | #include "Device/Primitive.hpp" |
| 19 | #include "Device/Renderer.hpp" |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 20 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 21 | namespace sw { |
| 22 | |
| 23 | // Union all cMask and return it as 4 booleans |
| 24 | Int4 PixelProgram::maskAny(Int cMask[4]) const |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 25 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 26 | // See if at least 1 sample is used |
| 27 | Int maskUnion = cMask[0]; |
Nicolas Capens | 11cb891 | 2020-01-14 00:24:05 -0500 | [diff] [blame] | 28 | for(auto i = 1u; i < state.multiSampleCount; i++) |
Alexis Hetu | 920100c | 2019-07-15 17:25:02 -0400 | [diff] [blame] | 29 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 30 | maskUnion |= cMask[i]; |
| 31 | } |
Alexis Hetu | 920100c | 2019-07-15 17:25:02 -0400 | [diff] [blame] | 32 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 33 | // Convert to 4 booleans |
| 34 | Int4 laneBits = Int4(1, 2, 4, 8); |
| 35 | Int4 laneShiftsToMSB = Int4(31, 30, 29, 28); |
| 36 | Int4 mask(maskUnion); |
| 37 | mask = ((mask & laneBits) << laneShiftsToMSB) >> Int4(31); |
| 38 | return mask; |
| 39 | } |
| 40 | |
| 41 | // Union all cMask/sMask/zMask and return it as 4 booleans |
| 42 | Int4 PixelProgram::maskAny(Int cMask[4], Int sMask[4], Int zMask[4]) const |
| 43 | { |
| 44 | // See if at least 1 sample is used |
| 45 | Int maskUnion = cMask[0] & sMask[0] & zMask[0]; |
Nicolas Capens | 11cb891 | 2020-01-14 00:24:05 -0500 | [diff] [blame] | 46 | for(auto i = 1u; i < state.multiSampleCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 47 | { |
| 48 | maskUnion |= (cMask[i] & sMask[i] & zMask[i]); |
| 49 | } |
| 50 | |
| 51 | // Convert to 4 booleans |
| 52 | Int4 laneBits = Int4(1, 2, 4, 8); |
| 53 | Int4 laneShiftsToMSB = Int4(31, 30, 29, 28); |
| 54 | Int4 mask(maskUnion); |
| 55 | mask = ((mask & laneBits) << laneShiftsToMSB) >> Int4(31); |
| 56 | return mask; |
| 57 | } |
| 58 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 59 | void PixelProgram::setBuiltins(Int &x, Int &y, Float4 (&z)[4], Float4 &w, Int cMask[4]) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 60 | { |
| 61 | routine.setImmutableInputBuiltins(spirvShader); |
| 62 | |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 63 | // TODO(b/146486064): Consider only assigning these to the SpirvRoutine iff |
| 64 | // they are ever going to be read. |
| 65 | routine.fragCoord[0] = SIMD::Float(Float(x)) + SIMD::Float(0.5f, 1.5f, 0.5f, 1.5f); |
| 66 | routine.fragCoord[1] = SIMD::Float(Float(y)) + SIMD::Float(0.5f, 0.5f, 1.5f, 1.5f); |
| 67 | routine.fragCoord[2] = z[0]; // sample 0 |
| 68 | routine.fragCoord[3] = w; |
| 69 | routine.pointCoord[0] = SIMD::Float(0.5f) + |
| 70 | SIMD::Float(Float(x) - (*Pointer<Float>(primitive + OFFSET(Primitive, pointCoordX)))); |
| 71 | routine.pointCoord[1] = SIMD::Float(0.5f) + |
| 72 | SIMD::Float(Float(y) - (*Pointer<Float>(primitive + OFFSET(Primitive, pointCoordY)))); |
| 73 | routine.invocationsPerSubgroup = SIMD::Width; |
| 74 | routine.helperInvocation = ~maskAny(cMask); |
| 75 | routine.windowSpacePosition[0] = x + SIMD::Int(0, 1, 0, 1); |
| 76 | routine.windowSpacePosition[1] = y + SIMD::Int(0, 0, 1, 1); |
| 77 | routine.viewID = *Pointer<Int>(data + OFFSET(DrawData, viewID)); |
| 78 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 79 | routine.setInputBuiltin(spirvShader, spv::BuiltInViewIndex, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 80 | assert(builtin.SizeInComponents == 1); |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 81 | value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(routine.viewID)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 82 | }); |
| 83 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 84 | routine.setInputBuiltin(spirvShader, spv::BuiltInFragCoord, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 85 | assert(builtin.SizeInComponents == 4); |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 86 | value[builtin.FirstComponent + 0] = routine.fragCoord[0]; |
| 87 | value[builtin.FirstComponent + 1] = routine.fragCoord[1]; |
| 88 | value[builtin.FirstComponent + 2] = routine.fragCoord[2]; |
| 89 | value[builtin.FirstComponent + 3] = routine.fragCoord[3]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 90 | }); |
| 91 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 92 | routine.setInputBuiltin(spirvShader, spv::BuiltInPointCoord, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 93 | assert(builtin.SizeInComponents == 2); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 94 | value[builtin.FirstComponent + 0] = SIMD::Float(0.5f, 1.5f, 0.5f, 1.5f) + |
| 95 | SIMD::Float(Float(x) - (*Pointer<Float>(primitive + OFFSET(Primitive, pointCoordX)))); |
| 96 | value[builtin.FirstComponent + 1] = SIMD::Float(0.5f, 0.5f, 1.5f, 1.5f) + |
| 97 | SIMD::Float(Float(y) - (*Pointer<Float>(primitive + OFFSET(Primitive, pointCoordY)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 98 | }); |
| 99 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 100 | routine.setInputBuiltin(spirvShader, spv::BuiltInSubgroupSize, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 101 | assert(builtin.SizeInComponents == 1); |
| 102 | value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(SIMD::Width)); |
| 103 | }); |
| 104 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 105 | routine.setInputBuiltin(spirvShader, spv::BuiltInHelperInvocation, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 106 | assert(builtin.SizeInComponents == 1); |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 107 | value[builtin.FirstComponent] = As<SIMD::Float>(routine.helperInvocation); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 108 | }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void PixelProgram::applyShader(Int cMask[4], Int sMask[4], Int zMask[4]) |
| 112 | { |
| 113 | routine.descriptorSets = data + OFFSET(DrawData, descriptorSets); |
| 114 | routine.descriptorDynamicOffsets = data + OFFSET(DrawData, descriptorDynamicOffsets); |
| 115 | routine.pushConstants = data + OFFSET(DrawData, pushConstants); |
| 116 | routine.constants = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, constants)); |
| 117 | |
| 118 | auto it = spirvShader->inputBuiltins.find(spv::BuiltInFrontFacing); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 119 | if(it != spirvShader->inputBuiltins.end()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 120 | { |
| 121 | ASSERT(it->second.SizeInComponents == 1); |
| 122 | auto frontFacing = Int4(*Pointer<Int>(primitive + OFFSET(Primitive, clockwiseMask))); |
| 123 | routine.getVariable(it->second.Id)[it->second.FirstComponent] = As<Float4>(frontFacing); |
| 124 | } |
| 125 | |
| 126 | it = spirvShader->inputBuiltins.find(spv::BuiltInSampleMask); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 127 | if(it != spirvShader->inputBuiltins.end()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 128 | { |
| 129 | static_assert(SIMD::Width == 4, "Expects SIMD width to be 4"); |
Alexis Hetu | 920100c | 2019-07-15 17:25:02 -0400 | [diff] [blame] | 130 | Int4 laneBits = Int4(1, 2, 4, 8); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 131 | |
| 132 | Int4 inputSampleMask = Int4(1) & CmpNEQ(Int4(cMask[0]) & laneBits, Int4(0)); |
Nicolas Capens | 11cb891 | 2020-01-14 00:24:05 -0500 | [diff] [blame] | 133 | for(auto i = 1u; i < state.multiSampleCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 134 | { |
| 135 | inputSampleMask |= Int4(1 << i) & CmpNEQ(Int4(cMask[i]) & laneBits, Int4(0)); |
| 136 | } |
| 137 | |
| 138 | routine.getVariable(it->second.Id)[it->second.FirstComponent] = As<Float4>(inputSampleMask); |
| 139 | // Sample mask input is an array, as the spec contemplates MSAA levels higher than 32. |
| 140 | // Fill any non-zero indices with 0. |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 141 | for(auto i = 1u; i < it->second.SizeInComponents; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 142 | routine.getVariable(it->second.Id)[it->second.FirstComponent + i] = Float4(0); |
Alexis Hetu | 920100c | 2019-07-15 17:25:02 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 145 | // Note: all lanes initially active to facilitate derivatives etc. Actual coverage is |
| 146 | // handled separately, through the cMask. |
| 147 | auto activeLaneMask = SIMD::Int(0xFFFFFFFF); |
| 148 | auto storesAndAtomicsMask = maskAny(cMask, sMask, zMask); |
| 149 | routine.killMask = 0; |
Alexis Hetu | 920100c | 2019-07-15 17:25:02 -0400 | [diff] [blame] | 150 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 151 | spirvShader->emit(&routine, activeLaneMask, storesAndAtomicsMask, descriptorSets); |
| 152 | spirvShader->emitEpilog(&routine); |
| 153 | |
| 154 | for(int i = 0; i < RENDERTARGETS; i++) |
| 155 | { |
| 156 | c[i].x = routine.outputs[i * 4]; |
| 157 | c[i].y = routine.outputs[i * 4 + 1]; |
| 158 | c[i].z = routine.outputs[i * 4 + 2]; |
| 159 | c[i].w = routine.outputs[i * 4 + 3]; |
Alexis Hetu | 920100c | 2019-07-15 17:25:02 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 162 | clampColor(c); |
| 163 | |
| 164 | if(spirvShader->getModes().ContainsKill) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 165 | { |
Nicolas Capens | 11cb891 | 2020-01-14 00:24:05 -0500 | [diff] [blame] | 166 | for(auto i = 0u; i < state.multiSampleCount; i++) |
Chris Forbes | 02d4c0d | 2019-08-21 12:04:34 -0700 | [diff] [blame] | 167 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 168 | cMask[i] &= ~routine.killMask; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 172 | it = spirvShader->outputBuiltins.find(spv::BuiltInSampleMask); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 173 | if(it != spirvShader->outputBuiltins.end()) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 174 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 175 | auto outputSampleMask = As<SIMD::Int>(routine.getVariable(it->second.Id)[it->second.FirstComponent]); |
| 176 | |
Nicolas Capens | 11cb891 | 2020-01-14 00:24:05 -0500 | [diff] [blame] | 177 | for(auto i = 0u; i < state.multiSampleCount; i++) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 178 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 179 | cMask[i] &= SignMask(CmpNEQ(outputSampleMask & SIMD::Int(1 << i), SIMD::Int(0))); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 183 | it = spirvShader->outputBuiltins.find(spv::BuiltInFragDepth); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 184 | if(it != spirvShader->outputBuiltins.end()) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 185 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 186 | oDepth = Min(Max(routine.getVariable(it->second.Id)[it->second.FirstComponent], Float4(0.0f)), Float4(1.0f)); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 187 | } |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 188 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 189 | |
| 190 | Bool PixelProgram::alphaTest(Int cMask[4]) |
| 191 | { |
| 192 | if(!state.alphaToCoverage) |
| 193 | { |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | alphaToCoverage(cMask, c[0].w); |
| 198 | |
| 199 | Int pass = cMask[0]; |
| 200 | |
Nicolas Capens | 11cb891 | 2020-01-14 00:24:05 -0500 | [diff] [blame] | 201 | for(unsigned int q = 1; q < state.multiSampleCount; q++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 202 | { |
| 203 | pass = pass | cMask[q]; |
| 204 | } |
| 205 | |
| 206 | return pass != 0x0; |
| 207 | } |
| 208 | |
| 209 | void PixelProgram::rasterOperation(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4]) |
| 210 | { |
| 211 | for(int index = 0; index < RENDERTARGETS; index++) |
| 212 | { |
| 213 | if(!state.colorWriteActive(index)) |
| 214 | { |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | auto format = state.targetFormat[index]; |
| 219 | switch(format) |
| 220 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 221 | case VK_FORMAT_A1R5G5B5_UNORM_PACK16: |
| 222 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 223 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 224 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 225 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 226 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 227 | case VK_FORMAT_R8G8_UNORM: |
| 228 | case VK_FORMAT_R8_UNORM: |
| 229 | case VK_FORMAT_R16G16_UNORM: |
| 230 | case VK_FORMAT_R16G16B16A16_UNORM: |
| 231 | case VK_FORMAT_A8B8G8R8_UNORM_PACK32: |
| 232 | case VK_FORMAT_A8B8G8R8_SRGB_PACK32: |
| 233 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
Alexis Hetu | b8a61bf | 2020-01-09 15:26:34 -0500 | [diff] [blame] | 234 | case VK_FORMAT_A2R10G10B10_UNORM_PACK32: |
Nicolas Capens | 11cb891 | 2020-01-14 00:24:05 -0500 | [diff] [blame] | 235 | for(unsigned int q = 0; q < state.multiSampleCount; q++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 236 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 237 | if(state.multiSampleMask & (1 << q)) |
| 238 | { |
| 239 | Pointer<Byte> buffer = cBuffer[index] + q * *Pointer<Int>(data + OFFSET(DrawData, colorSliceB[index])); |
| 240 | Vector4s color; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 241 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 242 | color.x = convertFixed16(c[index].x, false); |
| 243 | color.y = convertFixed16(c[index].y, false); |
| 244 | color.z = convertFixed16(c[index].z, false); |
| 245 | color.w = convertFixed16(c[index].w, false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 246 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 247 | alphaBlend(index, buffer, color, x); |
| 248 | writeColor(index, buffer, x, color, sMask[q], zMask[q], cMask[q]); |
| 249 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 250 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 251 | break; |
| 252 | case VK_FORMAT_R16_SFLOAT: |
| 253 | case VK_FORMAT_R16G16_SFLOAT: |
| 254 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 255 | case VK_FORMAT_B10G11R11_UFLOAT_PACK32: |
| 256 | case VK_FORMAT_R32_SFLOAT: |
| 257 | case VK_FORMAT_R32G32_SFLOAT: |
| 258 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 259 | case VK_FORMAT_R32_SINT: |
| 260 | case VK_FORMAT_R32G32_SINT: |
| 261 | case VK_FORMAT_R32G32B32A32_SINT: |
| 262 | case VK_FORMAT_R32_UINT: |
| 263 | case VK_FORMAT_R32G32_UINT: |
| 264 | case VK_FORMAT_R32G32B32A32_UINT: |
| 265 | case VK_FORMAT_R16_SINT: |
| 266 | case VK_FORMAT_R16G16_SINT: |
| 267 | case VK_FORMAT_R16G16B16A16_SINT: |
| 268 | case VK_FORMAT_R16_UINT: |
| 269 | case VK_FORMAT_R16G16_UINT: |
| 270 | case VK_FORMAT_R16G16B16A16_UINT: |
| 271 | case VK_FORMAT_R8_SINT: |
| 272 | case VK_FORMAT_R8G8_SINT: |
| 273 | case VK_FORMAT_R8G8B8A8_SINT: |
| 274 | case VK_FORMAT_R8_UINT: |
| 275 | case VK_FORMAT_R8G8_UINT: |
| 276 | case VK_FORMAT_R8G8B8A8_UINT: |
| 277 | case VK_FORMAT_A8B8G8R8_UINT_PACK32: |
| 278 | case VK_FORMAT_A8B8G8R8_SINT_PACK32: |
| 279 | case VK_FORMAT_A2B10G10R10_UINT_PACK32: |
Alexis Hetu | b8a61bf | 2020-01-09 15:26:34 -0500 | [diff] [blame] | 280 | case VK_FORMAT_A2R10G10B10_UINT_PACK32: |
Nicolas Capens | 11cb891 | 2020-01-14 00:24:05 -0500 | [diff] [blame] | 281 | for(unsigned int q = 0; q < state.multiSampleCount; q++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 282 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 283 | if(state.multiSampleMask & (1 << q)) |
| 284 | { |
| 285 | Pointer<Byte> buffer = cBuffer[index] + q * *Pointer<Int>(data + OFFSET(DrawData, colorSliceB[index])); |
| 286 | Vector4f color = c[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 287 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 288 | alphaBlend(index, buffer, color, x); |
| 289 | writeColor(index, buffer, x, color, sMask[q], zMask[q], cMask[q]); |
| 290 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 291 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 292 | break; |
| 293 | default: |
Nicolas Capens | 865f889 | 2020-01-21 14:27:10 -0500 | [diff] [blame] | 294 | UNSUPPORTED("VkFormat: %d", int(format)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void PixelProgram::clampColor(Vector4f oC[RENDERTARGETS]) |
| 300 | { |
| 301 | for(int index = 0; index < RENDERTARGETS; index++) |
| 302 | { |
| 303 | if(!state.colorWriteActive(index) && !(index == 0 && state.alphaToCoverage)) |
| 304 | { |
| 305 | continue; |
| 306 | } |
| 307 | |
| 308 | switch(state.targetFormat[index]) |
| 309 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 310 | case VK_FORMAT_UNDEFINED: |
| 311 | break; |
| 312 | case VK_FORMAT_A1R5G5B5_UNORM_PACK16: |
| 313 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 314 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 315 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 316 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 317 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 318 | case VK_FORMAT_R8G8_UNORM: |
| 319 | case VK_FORMAT_R8_UNORM: |
| 320 | case VK_FORMAT_R16G16_UNORM: |
| 321 | case VK_FORMAT_R16G16B16A16_UNORM: |
| 322 | case VK_FORMAT_A8B8G8R8_UNORM_PACK32: |
| 323 | case VK_FORMAT_A8B8G8R8_SRGB_PACK32: |
| 324 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
Alexis Hetu | b8a61bf | 2020-01-09 15:26:34 -0500 | [diff] [blame] | 325 | case VK_FORMAT_A2R10G10B10_UNORM_PACK32: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 326 | oC[index].x = Max(oC[index].x, Float4(0.0f)); |
| 327 | oC[index].x = Min(oC[index].x, Float4(1.0f)); |
| 328 | oC[index].y = Max(oC[index].y, Float4(0.0f)); |
| 329 | oC[index].y = Min(oC[index].y, Float4(1.0f)); |
| 330 | oC[index].z = Max(oC[index].z, Float4(0.0f)); |
| 331 | oC[index].z = Min(oC[index].z, Float4(1.0f)); |
| 332 | oC[index].w = Max(oC[index].w, Float4(0.0f)); |
| 333 | oC[index].w = Min(oC[index].w, Float4(1.0f)); |
| 334 | break; |
| 335 | case VK_FORMAT_R32_SFLOAT: |
| 336 | case VK_FORMAT_R32G32_SFLOAT: |
| 337 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 338 | case VK_FORMAT_R32_SINT: |
| 339 | case VK_FORMAT_R32G32_SINT: |
| 340 | case VK_FORMAT_R32G32B32A32_SINT: |
| 341 | case VK_FORMAT_R32_UINT: |
| 342 | case VK_FORMAT_R32G32_UINT: |
| 343 | case VK_FORMAT_R32G32B32A32_UINT: |
| 344 | case VK_FORMAT_R16_SFLOAT: |
| 345 | case VK_FORMAT_R16G16_SFLOAT: |
| 346 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 347 | case VK_FORMAT_B10G11R11_UFLOAT_PACK32: |
| 348 | case VK_FORMAT_R16_SINT: |
| 349 | case VK_FORMAT_R16G16_SINT: |
| 350 | case VK_FORMAT_R16G16B16A16_SINT: |
| 351 | case VK_FORMAT_R16_UINT: |
| 352 | case VK_FORMAT_R16G16_UINT: |
| 353 | case VK_FORMAT_R16G16B16A16_UINT: |
| 354 | case VK_FORMAT_R8_SINT: |
| 355 | case VK_FORMAT_R8G8_SINT: |
| 356 | case VK_FORMAT_R8G8B8A8_SINT: |
| 357 | case VK_FORMAT_R8_UINT: |
| 358 | case VK_FORMAT_R8G8_UINT: |
| 359 | case VK_FORMAT_R8G8B8A8_UINT: |
| 360 | case VK_FORMAT_A8B8G8R8_UINT_PACK32: |
| 361 | case VK_FORMAT_A8B8G8R8_SINT_PACK32: |
| 362 | case VK_FORMAT_A2B10G10R10_UINT_PACK32: |
Alexis Hetu | b8a61bf | 2020-01-09 15:26:34 -0500 | [diff] [blame] | 363 | case VK_FORMAT_A2R10G10B10_UINT_PACK32: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 364 | break; |
| 365 | default: |
Nicolas Capens | 865f889 | 2020-01-21 14:27:10 -0500 | [diff] [blame] | 366 | UNSUPPORTED("VkFormat: %d", int(state.targetFormat[index])); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 371 | Float4 PixelProgram::linearToSRGB(const Float4 &x) // Approximates x^(1.0/2.2) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 372 | { |
| 373 | Float4 sqrtx = Rcp_pp(RcpSqrt_pp(x)); |
| 374 | Float4 sRGB = sqrtx * Float4(1.14f) - x * Float4(0.14f); |
| 375 | |
| 376 | return Min(Max(sRGB, Float4(0.0f)), Float4(1.0f)); |
| 377 | } |
| 378 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 379 | } // namespace sw |