Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 15 | #include "SpirvShader.hpp" |
| 16 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 17 | #include "SamplerCore.hpp" // TODO: Figure out what's needed. |
| 18 | #include "Device/Config.hpp" |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 19 | #include "System/Debug.hpp" |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 20 | #include "System/Math.hpp" |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 21 | #include "Vulkan/VkDescriptorSetLayout.hpp" |
| 22 | #include "Vulkan/VkDevice.hpp" |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 23 | #include "Vulkan/VkImageView.hpp" |
| 24 | #include "Vulkan/VkSampler.hpp" |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 25 | |
| 26 | #include <spirv/unified1/spirv.hpp> |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 27 | |
Alexis Hetu | 710fcd5 | 2019-05-24 17:20:21 -0400 | [diff] [blame] | 28 | #include <climits> |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 29 | #include <mutex> |
| 30 | |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 31 | namespace sw { |
| 32 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 33 | SpirvShader::ImageSampler *SpirvShader::getImageSampler(const vk::Device *device, uint32_t inst, uint32_t samplerId, uint32_t imageViewId) |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 34 | { |
Nicolas Capens | fa7d136 | 2019-05-01 20:26:14 -0400 | [diff] [blame] | 35 | ImageInstruction instruction(inst); |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 36 | ASSERT(imageViewId != 0 && (samplerId != 0 || instruction.samplerMethod == Fetch)); |
| 37 | ASSERT(device); |
Nicolas Capens | fa7d136 | 2019-05-01 20:26:14 -0400 | [diff] [blame] | 38 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 39 | vk::Device::SamplingRoutineCache::Key key = { inst, samplerId, imageViewId }; |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 40 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 41 | auto createSamplingRoutine = [&device](const vk::Device::SamplingRoutineCache::Key &key) { |
| 42 | ImageInstruction instruction(key.instruction); |
| 43 | const vk::Identifier::State imageViewState = vk::Identifier(key.imageView).getState(); |
| 44 | const vk::SamplerState *vkSamplerState = (key.sampler != 0) ? device->findSampler(key.sampler) : nullptr; |
Alexis Hetu | 41a476e | 2021-02-16 17:38:55 -0500 | [diff] [blame] | 45 | |
| 46 | auto type = imageViewState.imageViewType; |
| 47 | auto samplerMethod = static_cast<SamplerMethod>(instruction.samplerMethod); |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 48 | |
Nicolas Capens | 1c29477 | 2020-03-28 23:04:55 -0400 | [diff] [blame] | 49 | Sampler samplerState = {}; |
| 50 | samplerState.textureType = type; |
Alexis Hetu | 41a476e | 2021-02-16 17:38:55 -0500 | [diff] [blame] | 51 | samplerState.textureFormat = imageViewState.format; |
Chris Forbes | 45f9a93 | 2019-05-08 13:30:38 -0700 | [diff] [blame] | 52 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 53 | samplerState.addressingModeU = convertAddressingMode(0, vkSamplerState, type); |
| 54 | samplerState.addressingModeV = convertAddressingMode(1, vkSamplerState, type); |
| 55 | samplerState.addressingModeW = convertAddressingMode(2, vkSamplerState, type); |
Nicolas Capens | 51e9e56 | 2019-05-16 14:01:16 -0400 | [diff] [blame] | 56 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 57 | samplerState.mipmapFilter = convertMipmapMode(vkSamplerState); |
Alexis Hetu | 41a476e | 2021-02-16 17:38:55 -0500 | [diff] [blame] | 58 | samplerState.swizzle = imageViewState.mapping; |
Nicolas Capens | 1c29477 | 2020-03-28 23:04:55 -0400 | [diff] [blame] | 59 | samplerState.gatherComponent = instruction.gatherComponent; |
Nicolas Capens | dbd0275 | 2019-08-20 10:59:58 -0400 | [diff] [blame] | 60 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 61 | if(vkSamplerState) |
Nicolas Capens | 1c29477 | 2020-03-28 23:04:55 -0400 | [diff] [blame] | 62 | { |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 63 | samplerState.textureFilter = convertFilterMode(vkSamplerState, type, samplerMethod); |
| 64 | samplerState.border = vkSamplerState->borderColor; |
Nicolas Capens | dbd0275 | 2019-08-20 10:59:58 -0400 | [diff] [blame] | 65 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 66 | samplerState.mipmapFilter = convertMipmapMode(vkSamplerState); |
| 67 | samplerState.highPrecisionFiltering = (vkSamplerState->filteringPrecision == VK_SAMPLER_FILTERING_PRECISION_MODE_HIGH_GOOGLE); |
Nicolas Capens | 7f46917 | 2020-03-17 17:29:11 -0400 | [diff] [blame] | 68 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 69 | samplerState.compareEnable = (vkSamplerState->compareEnable != VK_FALSE); |
| 70 | samplerState.compareOp = vkSamplerState->compareOp; |
| 71 | samplerState.unnormalizedCoordinates = (vkSamplerState->unnormalizedCoordinates != VK_FALSE); |
Nicolas Capens | e2535df | 2019-05-06 10:37:50 -0400 | [diff] [blame] | 72 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 73 | samplerState.ycbcrModel = vkSamplerState->ycbcrModel; |
| 74 | samplerState.studioSwing = vkSamplerState->studioSwing; |
| 75 | samplerState.swappedChroma = vkSamplerState->swappedChroma; |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 76 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 77 | samplerState.mipLodBias = vkSamplerState->mipLodBias; |
| 78 | samplerState.maxAnisotropy = vkSamplerState->maxAnisotropy; |
| 79 | samplerState.minLod = vkSamplerState->minLod; |
| 80 | samplerState.maxLod = vkSamplerState->maxLod; |
Alexis Hetu | f239c9f | 2021-02-09 18:02:11 -0500 | [diff] [blame] | 81 | |
| 82 | // If there's a single mip level and filtering doesn't depend on the LOD level, |
| 83 | // the sampler will need to compute the LOD to produce the proper result. |
| 84 | // Otherwise, it can be ignored. |
| 85 | // We can skip the LOD computation for all modes, except LOD query, |
| 86 | // where we have to return the proper value even if nothing else requires it. |
| 87 | if(imageViewState.singleMipLevel && |
| 88 | (samplerState.textureFilter != FILTER_MIN_POINT_MAG_LINEAR) && |
| 89 | (samplerState.textureFilter != FILTER_MIN_LINEAR_MAG_POINT) && |
| 90 | (samplerMethod != Query)) |
| 91 | { |
| 92 | samplerState.minLod = 0.0f; |
| 93 | samplerState.maxLod = 0.0f; |
| 94 | } |
Nicolas Capens | 1c29477 | 2020-03-28 23:04:55 -0400 | [diff] [blame] | 95 | } |
Nicolas Capens | 1c00441 | 2020-07-27 11:55:14 -0400 | [diff] [blame] | 96 | else |
| 97 | { |
| 98 | // OpImageFetch does not take a sampler descriptor, but for VK_EXT_image_robustness |
| 99 | // requires replacing invalid texels with zero. |
Nicolas Capens | 1c00441 | 2020-07-27 11:55:14 -0400 | [diff] [blame] | 100 | // TODO(b/162327166): Only perform bounds checks when VK_EXT_image_robustness is enabled. |
| 101 | samplerState.border = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK; |
| 102 | } |
Nicolas Capens | 1c29477 | 2020-03-28 23:04:55 -0400 | [diff] [blame] | 103 | |
| 104 | return emitSamplerRoutine(instruction, samplerState); |
| 105 | }; |
| 106 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 107 | vk::Device::SamplingRoutineCache *cache = device->getSamplingRoutineCache(); |
Nicolas Capens | 1c29477 | 2020-03-28 23:04:55 -0400 | [diff] [blame] | 108 | auto routine = cache->getOrCreate(key, createSamplingRoutine); |
| 109 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 110 | return (ImageSampler *)(routine->getEntry()); |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 111 | } |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 112 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 113 | std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction instruction, const Sampler &samplerState) |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 114 | { |
| 115 | // TODO(b/129523279): Hold a separate mutex lock for the sampler being built. |
Nicolas Capens | 6c11cf2 | 2020-03-19 15:21:13 -0400 | [diff] [blame] | 116 | rr::Function<Void(Pointer<Byte>, Pointer<SIMD::Float>, Pointer<SIMD::Float>, Pointer<Byte>)> function; |
Nicolas Capens | a47a516 | 2019-04-24 02:41:27 -0400 | [diff] [blame] | 117 | { |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 118 | Pointer<Byte> texture = function.Arg<0>(); |
Nicolas Capens | 6c11cf2 | 2020-03-19 15:21:13 -0400 | [diff] [blame] | 119 | Pointer<SIMD::Float> in = function.Arg<1>(); |
| 120 | Pointer<SIMD::Float> out = function.Arg<2>(); |
| 121 | Pointer<Byte> constants = function.Arg<3>(); |
Nicolas Capens | a195abb | 2019-04-25 17:15:56 -0400 | [diff] [blame] | 122 | |
Nicolas Capens | fda74a6 | 2020-10-02 23:49:59 -0400 | [diff] [blame] | 123 | SIMD::Float uvwa[4]; |
| 124 | SIMD::Float dRef; |
| 125 | SIMD::Float lodOrBias; // Explicit level-of-detail, or bias added to the implicit level-of-detail (depending on samplerMethod). |
| 126 | Vector4f dsx; |
| 127 | Vector4f dsy; |
| 128 | Vector4i offset; |
| 129 | SIMD::Int sampleId; |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 130 | SamplerFunction samplerFunction = instruction.getSamplerFunction(); |
| 131 | |
| 132 | uint32_t i = 0; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 133 | for(; i < instruction.coordinates; i++) |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 134 | { |
Nicolas Capens | 2e40a57 | 2020-07-29 14:18:55 -0400 | [diff] [blame] | 135 | uvwa[i] = in[i]; |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 138 | if(instruction.isDref()) |
Chris Forbes | c71c17f | 2019-05-04 10:01:04 -0700 | [diff] [blame] | 139 | { |
Nicolas Capens | 2e40a57 | 2020-07-29 14:18:55 -0400 | [diff] [blame] | 140 | dRef = in[i]; |
Chris Forbes | c71c17f | 2019-05-04 10:01:04 -0700 | [diff] [blame] | 141 | i++; |
| 142 | } |
| 143 | |
Chris Forbes | f7d78f7 | 2019-05-17 16:20:01 -0700 | [diff] [blame] | 144 | if(instruction.samplerMethod == Lod || instruction.samplerMethod == Bias || instruction.samplerMethod == Fetch) |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 145 | { |
| 146 | lodOrBias = in[i]; |
| 147 | i++; |
| 148 | } |
| 149 | else if(instruction.samplerMethod == Grad) |
| 150 | { |
Nicolas Capens | 2a25ed8 | 2019-06-14 11:26:14 -0400 | [diff] [blame] | 151 | for(uint32_t j = 0; j < instruction.grad; j++, i++) |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 152 | { |
| 153 | dsx[j] = in[i]; |
| 154 | } |
| 155 | |
Nicolas Capens | 2a25ed8 | 2019-06-14 11:26:14 -0400 | [diff] [blame] | 156 | for(uint32_t j = 0; j < instruction.grad; j++, i++) |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 157 | { |
| 158 | dsy[j] = in[i]; |
| 159 | } |
| 160 | } |
| 161 | |
Nicolas Capens | 2a25ed8 | 2019-06-14 11:26:14 -0400 | [diff] [blame] | 162 | for(uint32_t j = 0; j < instruction.offset; j++, i++) |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 163 | { |
Nicolas Capens | 2e40a57 | 2020-07-29 14:18:55 -0400 | [diff] [blame] | 164 | offset[j] = As<SIMD::Int>(in[i]); |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Alexis Hetu | 8a6dcf7 | 2019-11-26 17:24:42 -0500 | [diff] [blame] | 167 | if(instruction.sample) |
| 168 | { |
| 169 | sampleId = As<SIMD::Int>(in[i]); |
| 170 | } |
Nicolas Capens | 2a25ed8 | 2019-06-14 11:26:14 -0400 | [diff] [blame] | 171 | |
Nicolas Capens | 977a0a4 | 2019-05-16 16:44:05 -0400 | [diff] [blame] | 172 | SamplerCore s(constants, samplerState); |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 173 | |
Nicolas Capens | 8ac0bd6 | 2019-06-06 13:03:56 -0400 | [diff] [blame] | 174 | // For explicit-lod instructions the LOD can be different per SIMD lane. SamplerCore currently assumes |
| 175 | // a single LOD per four elements, so we sample the image again for each LOD separately. |
| 176 | if(samplerFunction.method == Lod || samplerFunction.method == Grad) // TODO(b/133868964): Also handle divergent Bias and Fetch with Lod. |
| 177 | { |
| 178 | auto lod = Pointer<Float>(&lodOrBias); |
| 179 | |
| 180 | For(Int i = 0, i < SIMD::Width, i++) |
| 181 | { |
| 182 | SIMD::Float dPdx; |
| 183 | SIMD::Float dPdy; |
| 184 | |
| 185 | dPdx.x = Pointer<Float>(&dsx.x)[i]; |
| 186 | dPdx.y = Pointer<Float>(&dsx.y)[i]; |
| 187 | dPdx.z = Pointer<Float>(&dsx.z)[i]; |
| 188 | |
| 189 | dPdy.x = Pointer<Float>(&dsy.x)[i]; |
| 190 | dPdy.y = Pointer<Float>(&dsy.y)[i]; |
| 191 | dPdy.z = Pointer<Float>(&dsy.z)[i]; |
| 192 | |
Nicolas Capens | 2e40a57 | 2020-07-29 14:18:55 -0400 | [diff] [blame] | 193 | Vector4f sample = s.sampleTexture(texture, uvwa, dRef, lod[i], dPdx, dPdy, offset, sampleId, samplerFunction); |
Nicolas Capens | 8ac0bd6 | 2019-06-06 13:03:56 -0400 | [diff] [blame] | 194 | |
| 195 | Pointer<Float> rgba = out; |
| 196 | rgba[0 * SIMD::Width + i] = Pointer<Float>(&sample.x)[i]; |
| 197 | rgba[1 * SIMD::Width + i] = Pointer<Float>(&sample.y)[i]; |
| 198 | rgba[2 * SIMD::Width + i] = Pointer<Float>(&sample.z)[i]; |
| 199 | rgba[3 * SIMD::Width + i] = Pointer<Float>(&sample.w)[i]; |
| 200 | } |
| 201 | } |
| 202 | else |
| 203 | { |
Nicolas Capens | 2e40a57 | 2020-07-29 14:18:55 -0400 | [diff] [blame] | 204 | Vector4f sample = s.sampleTexture(texture, uvwa, dRef, lodOrBias.x, (dsx.x), (dsy.x), offset, sampleId, samplerFunction); |
Nicolas Capens | 8ac0bd6 | 2019-06-06 13:03:56 -0400 | [diff] [blame] | 205 | |
| 206 | Pointer<SIMD::Float> rgba = out; |
| 207 | rgba[0] = sample.x; |
| 208 | rgba[1] = sample.y; |
| 209 | rgba[2] = sample.z; |
| 210 | rgba[3] = sample.w; |
| 211 | } |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 214 | return function("sampler"); |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 217 | sw::FilterType SpirvShader::convertFilterMode(const vk::SamplerState *samplerState, VkImageViewType imageViewType, SamplerMethod samplerMethod) |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 218 | { |
Alexis Hetu | 41a476e | 2021-02-16 17:38:55 -0500 | [diff] [blame] | 219 | if(samplerMethod == Gather) |
Nicolas Capens | 73c24e4 | 2020-08-05 09:50:20 -0400 | [diff] [blame] | 220 | { |
| 221 | return FILTER_GATHER; |
| 222 | } |
| 223 | |
Alexis Hetu | 41a476e | 2021-02-16 17:38:55 -0500 | [diff] [blame] | 224 | if(samplerMethod == Fetch) |
Nicolas Capens | 73c24e4 | 2020-08-05 09:50:20 -0400 | [diff] [blame] | 225 | { |
| 226 | return FILTER_POINT; |
| 227 | } |
| 228 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 229 | if(samplerState->anisotropyEnable != VK_FALSE) |
Alexis Hetu | 21be09d | 2019-12-17 16:53:53 -0500 | [diff] [blame] | 230 | { |
Nicolas Capens | 73c24e4 | 2020-08-05 09:50:20 -0400 | [diff] [blame] | 231 | if(imageViewType == VK_IMAGE_VIEW_TYPE_2D || imageViewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY) |
| 232 | { |
Alexis Hetu | 41a476e | 2021-02-16 17:38:55 -0500 | [diff] [blame] | 233 | if(samplerMethod != Lod) // TODO(b/162926129): Support anisotropic filtering with explicit LOD. |
Nicolas Capens | 73c24e4 | 2020-08-05 09:50:20 -0400 | [diff] [blame] | 234 | { |
| 235 | return FILTER_ANISOTROPIC; |
| 236 | } |
| 237 | } |
Alexis Hetu | 21be09d | 2019-12-17 16:53:53 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 240 | switch(samplerState->magFilter) |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 241 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 242 | case VK_FILTER_NEAREST: |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 243 | switch(samplerState->minFilter) |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 244 | { |
| 245 | case VK_FILTER_NEAREST: return FILTER_POINT; |
| 246 | case VK_FILTER_LINEAR: return FILTER_MIN_LINEAR_MAG_POINT; |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 247 | default: |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 248 | UNSUPPORTED("minFilter %d", samplerState->minFilter); |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 249 | return FILTER_POINT; |
| 250 | } |
| 251 | break; |
| 252 | case VK_FILTER_LINEAR: |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 253 | switch(samplerState->minFilter) |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 254 | { |
| 255 | case VK_FILTER_NEAREST: return FILTER_MIN_POINT_MAG_LINEAR; |
| 256 | case VK_FILTER_LINEAR: return FILTER_LINEAR; |
| 257 | default: |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 258 | UNSUPPORTED("minFilter %d", samplerState->minFilter); |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 259 | return FILTER_POINT; |
| 260 | } |
| 261 | break; |
| 262 | default: |
| 263 | break; |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 264 | } |
Ben Clayton | 8115f1e | 2019-06-11 16:13:56 +0100 | [diff] [blame] | 265 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 266 | UNSUPPORTED("magFilter %d", samplerState->magFilter); |
Ben Clayton | 8115f1e | 2019-06-11 16:13:56 +0100 | [diff] [blame] | 267 | return FILTER_POINT; |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 270 | sw::MipmapType SpirvShader::convertMipmapMode(const vk::SamplerState *samplerState) |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 271 | { |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 272 | if(!samplerState) |
Nicolas Capens | dbd0275 | 2019-08-20 10:59:58 -0400 | [diff] [blame] | 273 | { |
| 274 | return MIPMAP_POINT; // Samplerless operations (OpImageFetch) can take an integer Lod operand. |
| 275 | } |
| 276 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 277 | if(samplerState->ycbcrModel != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY) |
Nicolas Capens | 51e9e56 | 2019-05-16 14:01:16 -0400 | [diff] [blame] | 278 | { |
Nicolas Capens | 77b43d6 | 2020-03-12 00:20:00 -0400 | [diff] [blame] | 279 | // TODO(b/151263485): Check image view level count instead. |
| 280 | return MIPMAP_NONE; |
Nicolas Capens | 51e9e56 | 2019-05-16 14:01:16 -0400 | [diff] [blame] | 281 | } |
| 282 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 283 | switch(samplerState->mipmapMode) |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 284 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 285 | case VK_SAMPLER_MIPMAP_MODE_NEAREST: return MIPMAP_POINT; |
| 286 | case VK_SAMPLER_MIPMAP_MODE_LINEAR: return MIPMAP_LINEAR; |
| 287 | default: |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 288 | UNSUPPORTED("mipmapMode %d", samplerState->mipmapMode); |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 289 | return MIPMAP_POINT; |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 293 | sw::AddressingMode SpirvShader::convertAddressingMode(int coordinateIndex, const vk::SamplerState *samplerState, VkImageViewType imageViewType) |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 294 | { |
Nicolas Capens | 6a12e09 | 2019-04-29 17:26:51 -0400 | [diff] [blame] | 295 | switch(imageViewType) |
| 296 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 297 | case VK_IMAGE_VIEW_TYPE_1D: |
| 298 | case VK_IMAGE_VIEW_TYPE_1D_ARRAY: |
| 299 | if(coordinateIndex >= 1) |
| 300 | { |
| 301 | return ADDRESSING_UNUSED; |
| 302 | } |
| 303 | break; |
| 304 | case VK_IMAGE_VIEW_TYPE_2D: |
| 305 | case VK_IMAGE_VIEW_TYPE_2D_ARRAY: |
| 306 | if(coordinateIndex == 2) |
| 307 | { |
| 308 | return ADDRESSING_UNUSED; |
| 309 | } |
| 310 | break; |
Nicolas Capens | 3087370 | 2020-08-01 09:17:23 -0400 | [diff] [blame] | 311 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 312 | case VK_IMAGE_VIEW_TYPE_3D: |
| 313 | break; |
Nicolas Capens | 3087370 | 2020-08-01 09:17:23 -0400 | [diff] [blame] | 314 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 315 | case VK_IMAGE_VIEW_TYPE_CUBE: |
| 316 | case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: |
| 317 | if(coordinateIndex <= 1) // Cube faces themselves are addressed as 2D images. |
| 318 | { |
| 319 | // Vulkan 1.1 spec: |
| 320 | // "Cube images ignore the wrap modes specified in the sampler. Instead, if VK_FILTER_NEAREST is used within a mip level then |
| 321 | // VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE is used, and if VK_FILTER_LINEAR is used within a mip level then sampling at the edges |
| 322 | // is performed as described earlier in the Cube map edge handling section." |
| 323 | // This corresponds with our 'SEAMLESS' addressing mode. |
| 324 | return ADDRESSING_SEAMLESS; |
| 325 | } |
| 326 | else // coordinateIndex == 2 |
| 327 | { |
| 328 | // The cube face is an index into 2D array layers. |
| 329 | return ADDRESSING_CUBEFACE; |
| 330 | } |
| 331 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 332 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 333 | default: |
| 334 | UNSUPPORTED("imageViewType %d", imageViewType); |
| 335 | return ADDRESSING_WRAP; |
Nicolas Capens | a195abb | 2019-04-25 17:15:56 -0400 | [diff] [blame] | 336 | } |
| 337 | |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 338 | if(!samplerState) |
Nicolas Capens | dbd0275 | 2019-08-20 10:59:58 -0400 | [diff] [blame] | 339 | { |
Nicolas Capens | 1c00441 | 2020-07-27 11:55:14 -0400 | [diff] [blame] | 340 | // OpImageFetch does not take a sampler descriptor, but still needs a valid |
| 341 | // addressing mode that prevents out-of-bounds accesses: |
Nicolas Capens | dbd0275 | 2019-08-20 10:59:58 -0400 | [diff] [blame] | 342 | // "The value returned by a read of an invalid texel is undefined, unless that |
| 343 | // read operation is from a buffer resource and the robustBufferAccess feature |
| 344 | // is enabled. In that case, an invalid texel is replaced as described by the |
| 345 | // robustBufferAccess feature." - Vulkan 1.1 |
| 346 | |
Nicolas Capens | 1c00441 | 2020-07-27 11:55:14 -0400 | [diff] [blame] | 347 | // VK_EXT_image_robustness requires nullifying out-of-bounds accesses. |
| 348 | // ADDRESSING_BORDER causes texel replacement to be performed. |
Nicolas Capens | 1c00441 | 2020-07-27 11:55:14 -0400 | [diff] [blame] | 349 | // TODO(b/162327166): Only perform bounds checks when VK_EXT_image_robustness is enabled. |
| 350 | return ADDRESSING_BORDER; |
Nicolas Capens | dbd0275 | 2019-08-20 10:59:58 -0400 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | VkSamplerAddressMode addressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT; |
| 354 | switch(coordinateIndex) |
| 355 | { |
Alexis Hetu | e260190 | 2021-05-07 13:59:00 -0400 | [diff] [blame] | 356 | case 0: addressMode = samplerState->addressModeU; break; |
| 357 | case 1: addressMode = samplerState->addressModeV; break; |
| 358 | case 2: addressMode = samplerState->addressModeW; break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 359 | default: UNSUPPORTED("coordinateIndex: %d", coordinateIndex); |
Nicolas Capens | dbd0275 | 2019-08-20 10:59:58 -0400 | [diff] [blame] | 360 | } |
| 361 | |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 362 | switch(addressMode) |
| 363 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 364 | case VK_SAMPLER_ADDRESS_MODE_REPEAT: return ADDRESSING_WRAP; |
| 365 | case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT: return ADDRESSING_MIRROR; |
| 366 | case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE: return ADDRESSING_CLAMP; |
| 367 | case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER: return ADDRESSING_BORDER; |
| 368 | case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE: return ADDRESSING_MIRRORONCE; |
| 369 | default: |
| 370 | UNSUPPORTED("addressMode %d", addressMode); |
| 371 | return ADDRESSING_WRAP; |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 372 | } |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 375 | } // namespace sw |