| 1 |
/* |
| 2 |
* Copyright © 2021 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "genxml/gen_macros.h" |
| 7 |
|
| 8 |
#include "panvk_device.h" |
| 9 |
#include "panvk_entrypoints.h" |
| 10 |
#include "panvk_sampler.h" |
| 11 |
|
| 12 |
#include "pan_afbc.h" |
| 13 |
#include "pan_encoder.h" |
| 14 |
#include "pan_format.h" |
| 15 |
|
| 16 |
#include "vk_format.h" |
| 17 |
#include "vk_log.h" |
| 18 |
#include "vk_ycbcr_conversion.h" |
| 19 |
|
| 20 |
static enum mali_mipmap_mode |
| 21 |
panvk_translate_sampler_mipmap_mode(VkSamplerMipmapMode mode) |
| 22 |
{ |
| 23 |
switch (mode) { |
| 24 |
case VK_SAMPLER_MIPMAP_MODE_NEAREST: |
| 25 |
return MALI_MIPMAP_MODE_NEAREST; |
| 26 |
case VK_SAMPLER_MIPMAP_MODE_LINEAR: |
| 27 |
return MALI_MIPMAP_MODE_TRILINEAR; |
| 28 |
default: |
| 29 |
UNREACHABLE("Invalid mipmap mode"); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
static unsigned |
| 34 |
panvk_translate_sampler_address_mode(VkSamplerAddressMode mode) |
| 35 |
{ |
| 36 |
switch (mode) { |
| 37 |
case VK_SAMPLER_ADDRESS_MODE_REPEAT: |
| 38 |
return MALI_WRAP_MODE_REPEAT; |
| 39 |
case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT: |
| 40 |
return MALI_WRAP_MODE_MIRRORED_REPEAT; |
| 41 |
case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE: |
| 42 |
return MALI_WRAP_MODE_CLAMP_TO_EDGE; |
| 43 |
case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER: |
| 44 |
return MALI_WRAP_MODE_CLAMP_TO_BORDER; |
| 45 |
case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE: |
| 46 |
return MALI_WRAP_MODE_MIRRORED_CLAMP_TO_EDGE; |
| 47 |
default: |
| 48 |
UNREACHABLE("Invalid wrap"); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static enum mali_func |
| 53 |
panvk_translate_sampler_compare_func(const VkSamplerCreateInfo *pCreateInfo) |
| 54 |
{ |
| 55 |
if (!pCreateInfo->compareEnable) |
| 56 |
return MALI_FUNC_NEVER; |
| 57 |
|
| 58 |
return pan_flip_compare_func((enum mali_func)pCreateInfo->compareOp); |
| 59 |
} |
| 60 |
|
| 61 |
#if PAN_ARCH >= 10 |
| 62 |
static enum mali_reduction_mode |
| 63 |
panvk_translate_reduction_mode(VkSamplerReductionMode reduction_mode) |
| 64 |
{ |
| 65 |
switch (reduction_mode) { |
| 66 |
case VK_SAMPLER_REDUCTION_MODE_WEIGHTED_AVERAGE: |
| 67 |
return MALI_REDUCTION_MODE_AVERAGE; |
| 68 |
case VK_SAMPLER_REDUCTION_MODE_MIN: |
| 69 |
return MALI_REDUCTION_MODE_MINIMUM; |
| 70 |
case VK_SAMPLER_REDUCTION_MODE_MAX: |
| 71 |
return MALI_REDUCTION_MODE_MAXIMUM; |
| 72 |
default: |
| 73 |
UNREACHABLE("Invalid reduction mode"); |
| 74 |
} |
| 75 |
} |
| 76 |
#endif |
| 77 |
|
| 78 |
#if PAN_ARCH == 7 |
| 79 |
static void |
| 80 |
panvk_afbc_reswizzle_border_color(VkClearColorValue *border_color, VkFormat fmt) |
| 81 |
{ |
| 82 |
/* Doing border color reswizzle implies disabling support for |
| 83 |
* customBorderColorWithoutFormat. */ |
| 84 |
|
| 85 |
enum pipe_format pfmt = vk_format_to_pipe_format(fmt); |
| 86 |
if (pan_format_is_yuv(pfmt) || util_format_is_depth_or_stencil(pfmt) || |
| 87 |
!pan_afbc_supports_format(PAN_ARCH, pfmt)) |
| 88 |
return; |
| 89 |
|
| 90 |
const struct util_format_description *fdesc = util_format_description(pfmt); |
| 91 |
if (fdesc->swizzle[0] == PIPE_SWIZZLE_Z && |
| 92 |
fdesc->swizzle[2] == PIPE_SWIZZLE_X) { |
| 93 |
uint32_t red = border_color->uint32[0]; |
| 94 |
|
| 95 |
border_color->uint32[0] = border_color->uint32[2]; |
| 96 |
border_color->uint32[2] = red; |
| 97 |
} |
| 98 |
} |
| 99 |
#endif |
| 100 |
|
| 101 |
static void |
| 102 |
panvk_sampler_fill_desc(const struct VkSamplerCreateInfo *info, |
| 103 |
struct mali_sampler_packed *desc, |
| 104 |
VkClearColorValue border_color, |
| 105 |
VkFilter min_filter, VkFilter mag_filter, |
| 106 |
VkSamplerReductionMode reduction_mode, |
| 107 |
VkSamplerCreateFlags flags) |
| 108 |
{ |
| 109 |
pan_pack(desc, SAMPLER, cfg) { |
| 110 |
cfg.magnify_nearest = mag_filter == VK_FILTER_NEAREST; |
| 111 |
cfg.minify_nearest = min_filter == VK_FILTER_NEAREST; |
| 112 |
cfg.mipmap_mode = |
| 113 |
panvk_translate_sampler_mipmap_mode(info->mipmapMode); |
| 114 |
cfg.normalized_coordinates = !info->unnormalizedCoordinates; |
| 115 |
cfg.clamp_integer_array_indices = false; |
| 116 |
|
| 117 |
/* Normalized float texture coordinates are rounded to fixed-point |
| 118 |
* before rounding to integer coordinates. When round_to_nearest_even is |
| 119 |
* enabled with VK_FILTER_NEAREST, the upper 2^-9 float coordinates in |
| 120 |
* each texel are rounded up to the next texel. |
| 121 |
* |
| 122 |
* The Vulkan 1.4.304 spec seems to allow both rounding modes for all |
| 123 |
* filters, but a CTS bug[1] causes test failures when round-to-nearest |
| 124 |
* is used with VK_FILTER_NEAREST. |
| 125 |
* |
| 126 |
* Regardless, disabling round_to_nearest_even for NEAREST filters |
| 127 |
* is a desirable precision improvement. |
| 128 |
* |
| 129 |
* [1]: https://gitlab.khronos.org/Tracker/vk-gl-cts/-/issues/5547 |
| 130 |
*/ |
| 131 |
if (min_filter == VK_FILTER_NEAREST && |
| 132 |
mag_filter == VK_FILTER_NEAREST) |
| 133 |
cfg.round_to_nearest_even = false; |
| 134 |
|
| 135 |
cfg.lod_bias = info->mipLodBias; |
| 136 |
cfg.minimum_lod = info->minLod; |
| 137 |
cfg.maximum_lod = info->maxLod; |
| 138 |
cfg.wrap_mode_s = |
| 139 |
panvk_translate_sampler_address_mode(info->addressModeU); |
| 140 |
cfg.wrap_mode_t = |
| 141 |
panvk_translate_sampler_address_mode(info->addressModeV); |
| 142 |
|
| 143 |
/* " |
| 144 |
* When unnormalizedCoordinates is VK_TRUE, images the sampler is used |
| 145 |
* with in the shader have the following requirements: |
| 146 |
* - The viewType must be either VK_IMAGE_VIEW_TYPE_1D or |
| 147 |
* VK_IMAGE_VIEW_TYPE_2D. |
| 148 |
* - The image view must have a single layer and a single mip level. |
| 149 |
* " |
| 150 |
* |
| 151 |
* This means addressModeW should be ignored. We pick a default value |
| 152 |
* that works for normalized_coordinates=false. |
| 153 |
*/ |
| 154 |
cfg.wrap_mode_r = |
| 155 |
info->unnormalizedCoordinates |
| 156 |
? MALI_WRAP_MODE_CLAMP_TO_EDGE |
| 157 |
: panvk_translate_sampler_address_mode(info->addressModeW); |
| 158 |
|
| 159 |
cfg.seamless_cube_map = |
| 160 |
(flags & VK_SAMPLER_CREATE_NON_SEAMLESS_CUBE_MAP_BIT_EXT) == 0; |
| 161 |
|
| 162 |
cfg.compare_function = panvk_translate_sampler_compare_func(info); |
| 163 |
cfg.border_color_r = border_color.uint32[0]; |
| 164 |
cfg.border_color_g = border_color.uint32[1]; |
| 165 |
cfg.border_color_b = border_color.uint32[2]; |
| 166 |
cfg.border_color_a = border_color.uint32[3]; |
| 167 |
|
| 168 |
if (info->anisotropyEnable && info->maxAnisotropy > 1) { |
| 169 |
cfg.maximum_anisotropy = info->maxAnisotropy; |
| 170 |
cfg.lod_algorithm = MALI_LOD_ALGORITHM_ANISOTROPIC; |
| 171 |
} |
| 172 |
|
| 173 |
#if PAN_ARCH >= 10 |
| 174 |
cfg.reduction_mode = panvk_translate_reduction_mode(reduction_mode); |
| 175 |
#endif |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 180 |
panvk_per_arch(CreateSampler)(VkDevice _device, |
| 181 |
const VkSamplerCreateInfo *pCreateInfo, |
| 182 |
const VkAllocationCallbacks *pAllocator, |
| 183 |
VkSampler *pSampler) |
| 184 |
{ |
| 185 |
VK_FROM_HANDLE(panvk_device, device, _device); |
| 186 |
struct panvk_sampler *sampler; |
| 187 |
|
| 188 |
assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO); |
| 189 |
|
| 190 |
sampler = |
| 191 |
vk_sampler_create(&device->vk, pCreateInfo, pAllocator, sizeof(*sampler)); |
| 192 |
if (!sampler) |
| 193 |
return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 194 |
|
| 195 |
STATIC_ASSERT(sizeof(sampler->descs[0]) >= pan_size(SAMPLER)); |
| 196 |
|
| 197 |
VkFormat fmt; |
| 198 |
VkClearColorValue border_color = |
| 199 |
vk_sampler_border_color_value(pCreateInfo, &fmt); |
| 200 |
|
| 201 |
#if PAN_ARCH == 7 |
| 202 |
panvk_afbc_reswizzle_border_color(&border_color, fmt); |
| 203 |
#endif |
| 204 |
|
| 205 |
sampler->desc_count = 1; |
| 206 |
panvk_sampler_fill_desc(pCreateInfo, &sampler->descs[0], border_color, |
| 207 |
pCreateInfo->minFilter, pCreateInfo->magFilter, |
| 208 |
sampler->vk.reduction_mode, pCreateInfo->flags); |
| 209 |
|
| 210 |
/* In order to support CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT, |
| 211 |
* we need multiple sampler planes: at minimum we will need one for |
| 212 |
* luminance (the default), and one for chroma. |
| 213 |
*/ |
| 214 |
if (sampler->vk.ycbcr_conversion) { |
| 215 |
const VkFilter chroma_filter = |
| 216 |
sampler->vk.ycbcr_conversion->state.chroma_filter; |
| 217 |
if (pCreateInfo->magFilter != chroma_filter || |
| 218 |
pCreateInfo->minFilter != chroma_filter) { |
| 219 |
sampler->desc_count = 2; |
| 220 |
panvk_sampler_fill_desc(pCreateInfo, &sampler->descs[1], |
| 221 |
border_color, chroma_filter, chroma_filter, |
| 222 |
sampler->vk.reduction_mode, |
| 223 |
pCreateInfo->flags); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
*pSampler = panvk_sampler_to_handle(sampler); |
| 228 |
return VK_SUCCESS; |
| 229 |
} |
| 230 |
|
| 231 |
VKAPI_ATTR void VKAPI_CALL |
| 232 |
panvk_per_arch(DestroySampler)(VkDevice _device, VkSampler _sampler, |
| 233 |
const VkAllocationCallbacks *pAllocator) |
| 234 |
{ |
| 235 |
VK_FROM_HANDLE(panvk_device, device, _device); |
| 236 |
VK_FROM_HANDLE(panvk_sampler, sampler, _sampler); |
| 237 |
|
| 238 |
if (!sampler) |
| 239 |
return; |
| 240 |
|
| 241 |
vk_sampler_destroy(&device->vk, pAllocator, &sampler->vk); |
| 242 |
} |