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 #ifndef sw_Sampler_hpp 16 #define sw_Sampler_hpp 17 18 #include "Device/Config.hpp" 19 #include "System/Types.hpp" 20 #include "Vulkan/VkFormat.hpp" 21 22 namespace sw { 23 24 struct Mipmap 25 { 26 const void *buffer; 27 28 ushort4 uHalf; 29 ushort4 vHalf; 30 ushort4 wHalf; 31 uint4 width; 32 uint4 height; 33 uint4 depth; 34 short4 onePitchP; 35 uint4 pitchP; 36 uint4 sliceP; 37 uint4 samplePitchP; 38 uint4 sampleMax; 39 }; 40 41 struct Texture 42 { 43 Mipmap mipmap[MIPMAP_LEVELS]; 44 45 float4 widthWidthHeightHeight; 46 float4 width; 47 float4 height; 48 float4 depth; 49 }; 50 51 enum FilterType ENUM_UNDERLYING_TYPE_UNSIGNED_INT 52 { 53 FILTER_POINT, 54 FILTER_GATHER, 55 FILTER_MIN_POINT_MAG_LINEAR, 56 FILTER_MIN_LINEAR_MAG_POINT, 57 FILTER_LINEAR, 58 FILTER_ANISOTROPIC, 59 60 FILTER_LAST = FILTER_ANISOTROPIC 61 }; 62 63 enum MipmapType ENUM_UNDERLYING_TYPE_UNSIGNED_INT 64 { 65 MIPMAP_NONE, 66 MIPMAP_POINT, 67 MIPMAP_LINEAR, 68 69 MIPMAP_LAST = MIPMAP_LINEAR 70 }; 71 72 enum AddressingMode ENUM_UNDERLYING_TYPE_UNSIGNED_INT 73 { 74 ADDRESSING_UNUSED, 75 ADDRESSING_WRAP, 76 ADDRESSING_CLAMP, 77 ADDRESSING_MIRROR, 78 ADDRESSING_MIRRORONCE, 79 ADDRESSING_BORDER, // Single color 80 ADDRESSING_SEAMLESS, // Border of pixels 81 ADDRESSING_CUBEFACE, // Cube face layer 82 ADDRESSING_TEXELFETCH, 83 84 ADDRESSING_LAST = ADDRESSING_TEXELFETCH 85 }; 86 87 struct Sampler 88 { 89 VkImageViewType textureType; 90 vk::Format textureFormat; 91 FilterType textureFilter; 92 AddressingMode addressingModeU; 93 AddressingMode addressingModeV; 94 AddressingMode addressingModeW; 95 MipmapType mipmapFilter; 96 VkComponentMapping swizzle; 97 int gatherComponent; 98 bool highPrecisionFiltering; 99 bool compareEnable; 100 VkCompareOp compareOp; 101 VkBorderColor border; 102 VkClearColorValue customBorder; 103 bool unnormalizedCoordinates; 104 105 VkSamplerYcbcrModelConversion ycbcrModel; 106 bool studioSwing; // Narrow range 107 bool swappedChroma; // Cb/Cr components in reverse order 108 109 float mipLodBias = 0.0f; 110 float maxAnisotropy = 0.0f; 111 float minLod = -1000.0f; 112 float maxLod = 1000.0f; 113 is1Dsw::Sampler114 bool is1D() const 115 { 116 switch(textureType) 117 { 118 case VK_IMAGE_VIEW_TYPE_1D: 119 case VK_IMAGE_VIEW_TYPE_1D_ARRAY: 120 return true; 121 case VK_IMAGE_VIEW_TYPE_2D: 122 case VK_IMAGE_VIEW_TYPE_3D: 123 case VK_IMAGE_VIEW_TYPE_CUBE: 124 case VK_IMAGE_VIEW_TYPE_2D_ARRAY: 125 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: 126 return false; 127 default: 128 UNSUPPORTED("VkImageViewType %d", (int)textureType); 129 return false; 130 } 131 } 132 is2Dsw::Sampler133 bool is2D() const 134 { 135 switch(textureType) 136 { 137 case VK_IMAGE_VIEW_TYPE_2D: 138 case VK_IMAGE_VIEW_TYPE_2D_ARRAY: 139 return true; 140 case VK_IMAGE_VIEW_TYPE_1D: 141 case VK_IMAGE_VIEW_TYPE_3D: 142 case VK_IMAGE_VIEW_TYPE_CUBE: 143 case VK_IMAGE_VIEW_TYPE_1D_ARRAY: 144 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: 145 return false; 146 default: 147 UNSUPPORTED("VkImageViewType %d", (int)textureType); 148 return false; 149 } 150 } 151 is3Dsw::Sampler152 bool is3D() const 153 { 154 switch(textureType) 155 { 156 case VK_IMAGE_VIEW_TYPE_3D: 157 return true; 158 case VK_IMAGE_VIEW_TYPE_1D: 159 case VK_IMAGE_VIEW_TYPE_2D: 160 case VK_IMAGE_VIEW_TYPE_CUBE: 161 case VK_IMAGE_VIEW_TYPE_1D_ARRAY: 162 case VK_IMAGE_VIEW_TYPE_2D_ARRAY: 163 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: 164 return false; 165 default: 166 UNSUPPORTED("VkImageViewType %d", (int)textureType); 167 return false; 168 } 169 } 170 isCubesw::Sampler171 bool isCube() const 172 { 173 switch(textureType) 174 { 175 case VK_IMAGE_VIEW_TYPE_CUBE: 176 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: 177 return true; 178 case VK_IMAGE_VIEW_TYPE_1D: 179 case VK_IMAGE_VIEW_TYPE_2D: 180 case VK_IMAGE_VIEW_TYPE_3D: 181 case VK_IMAGE_VIEW_TYPE_1D_ARRAY: 182 case VK_IMAGE_VIEW_TYPE_2D_ARRAY: 183 return false; 184 default: 185 UNSUPPORTED("VkImageViewType %d", (int)textureType); 186 return false; 187 } 188 } 189 isArrayedsw::Sampler190 bool isArrayed() const 191 { 192 switch(textureType) 193 { 194 case VK_IMAGE_VIEW_TYPE_1D_ARRAY: 195 case VK_IMAGE_VIEW_TYPE_2D_ARRAY: 196 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: 197 return true; 198 case VK_IMAGE_VIEW_TYPE_1D: 199 case VK_IMAGE_VIEW_TYPE_2D: 200 case VK_IMAGE_VIEW_TYPE_3D: 201 case VK_IMAGE_VIEW_TYPE_CUBE: 202 return false; 203 default: 204 UNSUPPORTED("VkImageViewType %d", (int)textureType); 205 return false; 206 } 207 } 208 209 // Returns the number of coordinates required to sample the image, 210 // not including any array coordinate, which is indicated by isArrayed(). dimensionalitysw::Sampler211 unsigned int dimensionality() const 212 { 213 switch(textureType) 214 { 215 case VK_IMAGE_VIEW_TYPE_1D: 216 case VK_IMAGE_VIEW_TYPE_1D_ARRAY: 217 return 1; 218 case VK_IMAGE_VIEW_TYPE_2D: 219 case VK_IMAGE_VIEW_TYPE_2D_ARRAY: 220 return 2; 221 case VK_IMAGE_VIEW_TYPE_3D: 222 case VK_IMAGE_VIEW_TYPE_CUBE: 223 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: 224 return 3; 225 default: 226 UNSUPPORTED("VkImageViewType %d", (int)textureType); 227 return 0; 228 } 229 } 230 }; 231 232 } // namespace sw 233 234 #endif // sw_Sampler_hpp 235