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