| 1 |
/* |
| 2 |
* Copyright © 2022 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* based in part on anv driver which is: |
| 5 |
* Copyright © 2015 Intel Corporation |
| 6 |
* |
| 7 |
* based in part on radv driver which is: |
| 8 |
* Copyright © 2016 Red Hat. |
| 9 |
* Copyright © 2016 Bas Nieuwenhuizen |
| 10 |
* |
| 11 |
* SPDX-License-Identifier: MIT |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "pvr_sampler.h" |
| 15 |
|
| 16 |
#include "vk_log.h" |
| 17 |
|
| 18 |
#include "pvr_border.h" |
| 19 |
#include "pvr_csb.h" |
| 20 |
#include "pvr_device.h" |
| 21 |
#include "pvr_entrypoints.h" |
| 22 |
#include "pvr_physical_device.h" |
| 23 |
|
| 24 |
static uint32_t |
| 25 |
pvr_sampler_get_hw_filter_from_vk(const struct pvr_device_info *dev_info, |
| 26 |
VkFilter filter) |
| 27 |
{ |
| 28 |
switch (filter) { |
| 29 |
case VK_FILTER_NEAREST: |
| 30 |
return ROGUE_TEXSTATE_FILTER_POINT; |
| 31 |
case VK_FILTER_LINEAR: |
| 32 |
return ROGUE_TEXSTATE_FILTER_LINEAR; |
| 33 |
default: |
| 34 |
UNREACHABLE("Unknown filter type."); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
static uint32_t |
| 39 |
pvr_sampler_get_hw_addr_mode_from_vk(VkSamplerAddressMode addr_mode) |
| 40 |
{ |
| 41 |
switch (addr_mode) { |
| 42 |
case VK_SAMPLER_ADDRESS_MODE_REPEAT: |
| 43 |
return ROGUE_TEXSTATE_ADDRMODE_REPEAT; |
| 44 |
case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT: |
| 45 |
return ROGUE_TEXSTATE_ADDRMODE_FLIP; |
| 46 |
case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE: |
| 47 |
return ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE; |
| 48 |
case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE: |
| 49 |
return ROGUE_TEXSTATE_ADDRMODE_FLIP_ONCE_THEN_CLAMP; |
| 50 |
case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER: |
| 51 |
return ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_BORDER; |
| 52 |
default: |
| 53 |
UNREACHABLE("Invalid sampler address mode."); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
VkResult PVR_PER_ARCH(CreateSampler)(VkDevice _device, |
| 58 |
const VkSamplerCreateInfo *pCreateInfo, |
| 59 |
const VkAllocationCallbacks *pAllocator, |
| 60 |
VkSampler *pSampler) |
| 61 |
{ |
| 62 |
VK_FROM_HANDLE(pvr_device, device, _device); |
| 63 |
struct pvr_sampler *sampler; |
| 64 |
float lod_rounding_bias; |
| 65 |
VkFilter min_filter; |
| 66 |
VkFilter mag_filter; |
| 67 |
VkResult result; |
| 68 |
float min_lod; |
| 69 |
float max_lod; |
| 70 |
|
| 71 |
sampler = |
| 72 |
vk_sampler_create(&device->vk, pCreateInfo, pAllocator, sizeof(*sampler)); |
| 73 |
if (!sampler) { |
| 74 |
result = vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 75 |
goto err_out; |
| 76 |
} |
| 77 |
|
| 78 |
mag_filter = pCreateInfo->magFilter; |
| 79 |
min_filter = pCreateInfo->minFilter; |
| 80 |
|
| 81 |
result = pvr_arch_border_color_table_get_or_create_entry( |
| 82 |
device, |
| 83 |
sampler, |
| 84 |
device->border_color_table, |
| 85 |
&sampler->border_color_table_index); |
| 86 |
if (result != VK_SUCCESS) |
| 87 |
goto err_free_sampler; |
| 88 |
|
| 89 |
if (PVR_HAS_QUIRK(&device->pdevice->dev_info, 51025)) { |
| 90 |
/* The min/mag filters may need adjustment here, the GPU should decide |
| 91 |
* which of the two filters to use based on the clamped LOD value: LOD |
| 92 |
* <= 0 implies magnification, while LOD > 0 implies minification. |
| 93 |
* |
| 94 |
* As a workaround, we override magFilter with minFilter if we know that |
| 95 |
* the magnification filter will never be used due to clamping anyway |
| 96 |
* (i.e. minLod > 0). Conversely, we override minFilter with magFilter |
| 97 |
* if maxLod <= 0. |
| 98 |
*/ |
| 99 |
if (pCreateInfo->minLod > 0.0f) { |
| 100 |
/* The clamped LOD will always be positive => always minify. */ |
| 101 |
mag_filter = pCreateInfo->minFilter; |
| 102 |
} |
| 103 |
|
| 104 |
if (pCreateInfo->maxLod <= 0.0f) { |
| 105 |
/* The clamped LOD will always be negative or zero => always |
| 106 |
* magnify. |
| 107 |
*/ |
| 108 |
min_filter = pCreateInfo->magFilter; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (pCreateInfo->compareEnable) { |
| 113 |
sampler->descriptor.meta[PCO_SAMPLER_META_COMPARE_OP] = |
| 114 |
pCreateInfo->compareOp; |
| 115 |
} else { |
| 116 |
sampler->descriptor.meta[PCO_SAMPLER_META_COMPARE_OP] = |
| 117 |
VK_COMPARE_OP_NEVER; |
| 118 |
} |
| 119 |
|
| 120 |
pvr_csb_pack (&sampler->descriptor.words[0], TEXSTATE_SAMPLER_WORD0, word) { |
| 121 |
const struct pvr_device_info *dev_info = &device->pdevice->dev_info; |
| 122 |
const float lod_clamp_max = (float)ROGUE_TEXSTATE_CLAMP_MAX / |
| 123 |
(1 << ROGUE_TEXSTATE_CLAMP_FRACTIONAL_BITS); |
| 124 |
const float max_dadjust = ((float)(ROGUE_TEXSTATE_DADJUST_MAX_UINT - |
| 125 |
ROGUE_TEXSTATE_DADJUST_ZERO_UINT)) / |
| 126 |
(1 << ROGUE_TEXSTATE_DADJUST_FRACTIONAL_BITS); |
| 127 |
const float min_dadjust = ((float)(ROGUE_TEXSTATE_DADJUST_MIN_UINT - |
| 128 |
ROGUE_TEXSTATE_DADJUST_ZERO_UINT)) / |
| 129 |
(1 << ROGUE_TEXSTATE_DADJUST_FRACTIONAL_BITS); |
| 130 |
|
| 131 |
word.magfilter = pvr_sampler_get_hw_filter_from_vk(dev_info, mag_filter); |
| 132 |
word.minfilter = pvr_sampler_get_hw_filter_from_vk(dev_info, min_filter); |
| 133 |
|
| 134 |
if (pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_LINEAR) |
| 135 |
word.mipfilter = true; |
| 136 |
|
| 137 |
word.addrmode_u = |
| 138 |
pvr_sampler_get_hw_addr_mode_from_vk(pCreateInfo->addressModeU); |
| 139 |
word.addrmode_v = |
| 140 |
pvr_sampler_get_hw_addr_mode_from_vk(pCreateInfo->addressModeV); |
| 141 |
word.addrmode_w = |
| 142 |
pvr_sampler_get_hw_addr_mode_from_vk(pCreateInfo->addressModeW); |
| 143 |
|
| 144 |
/* The Vulkan 1.0.205 spec says: |
| 145 |
* |
| 146 |
* The absolute value of mipLodBias must be less than or equal to |
| 147 |
* VkPhysicalDeviceLimits::maxSamplerLodBias. |
| 148 |
*/ |
| 149 |
word.dadjust = |
| 150 |
ROGUE_TEXSTATE_DADJUST_ZERO_UINT + |
| 151 |
util_signed_fixed( |
| 152 |
CLAMP(pCreateInfo->mipLodBias, min_dadjust, max_dadjust), |
| 153 |
ROGUE_TEXSTATE_DADJUST_FRACTIONAL_BITS); |
| 154 |
|
| 155 |
word.anisoctl = ROGUE_TEXSTATE_ANISOCTL_DISABLED; |
| 156 |
if (pCreateInfo->anisotropyEnable) { |
| 157 |
if (pCreateInfo->maxAnisotropy >= 16.0f) |
| 158 |
word.anisoctl = ROGUE_TEXSTATE_ANISOCTL_X16; |
| 159 |
else if (pCreateInfo->maxAnisotropy >= 8.0f) |
| 160 |
word.anisoctl = ROGUE_TEXSTATE_ANISOCTL_X8; |
| 161 |
else if (pCreateInfo->maxAnisotropy >= 4.0f) |
| 162 |
word.anisoctl = ROGUE_TEXSTATE_ANISOCTL_X4; |
| 163 |
else if (pCreateInfo->maxAnisotropy >= 2.0f) |
| 164 |
word.anisoctl = ROGUE_TEXSTATE_ANISOCTL_X2; |
| 165 |
} |
| 166 |
|
| 167 |
if (PVR_HAS_QUIRK(&device->pdevice->dev_info, 51025) && |
| 168 |
pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_NEAREST) { |
| 169 |
/* When MIPMAP_MODE_NEAREST is enabled, the LOD level should be |
| 170 |
* selected by adding 0.5 and then truncating the input LOD value. |
| 171 |
* This hardware adds the 0.5 bias before clamping against |
| 172 |
* lodmin/lodmax, while Vulkan specifies the bias to be added after |
| 173 |
* clamping. We compensate for this difference by adding the 0.5 |
| 174 |
* bias to the LOD bounds, too. |
| 175 |
*/ |
| 176 |
lod_rounding_bias = 0.5f; |
| 177 |
} else { |
| 178 |
lod_rounding_bias = 0.0f; |
| 179 |
} |
| 180 |
|
| 181 |
min_lod = pCreateInfo->minLod + lod_rounding_bias; |
| 182 |
word.minlod = util_unsigned_fixed(CLAMP(min_lod, 0.0f, lod_clamp_max), |
| 183 |
ROGUE_TEXSTATE_CLAMP_FRACTIONAL_BITS); |
| 184 |
|
| 185 |
max_lod = pCreateInfo->maxLod + lod_rounding_bias; |
| 186 |
word.maxlod = util_unsigned_fixed(CLAMP(max_lod, 0.0f, lod_clamp_max), |
| 187 |
ROGUE_TEXSTATE_CLAMP_FRACTIONAL_BITS); |
| 188 |
|
| 189 |
word.bordercolor_index = sampler->border_color_table_index; |
| 190 |
|
| 191 |
if (pCreateInfo->unnormalizedCoordinates) |
| 192 |
word.non_normalized_coords = true; |
| 193 |
|
| 194 |
/* These fields overlap with plane addresses on rogue */ |
| 195 |
if (sampler->vk.ycbcr_conversion) { |
| 196 |
word.bordercolor_index = 0; |
| 197 |
word.addrmode_w = ROGUE_TEXSTATE_ADDRMODE_REPEAT; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
pvr_csb_pack (&sampler->descriptor.words[1], TEXSTATE_SAMPLER_WORD1, word) { |
| 202 |
word.cemedge_dontfilter = |
| 203 |
(pCreateInfo->flags & VK_SAMPLER_CREATE_NON_SEAMLESS_CUBE_MAP_BIT_EXT); |
| 204 |
} |
| 205 |
|
| 206 |
#ifndef NDEBUG |
| 207 |
/* YUV plane addresses are stored in the sampler on rogue. We need to add |
| 208 |
* them whilst writing descriptor sets. We assert here that none of the |
| 209 |
* bits that overlap with plane addresses are set. |
| 210 |
*/ |
| 211 |
if (sampler->vk.ycbcr_conversion) { |
| 212 |
uint64_t yuv_word0_addr_mask = 0; |
| 213 |
pvr_csb_pack (&yuv_word0_addr_mask, TEXSTATE_SAMPLER_WORD0, word) { |
| 214 |
word.texaddr_plane2_lo = PVR_DEV_ADDR(~0); |
| 215 |
} |
| 216 |
assert(!(sampler->descriptor.words[0] & yuv_word0_addr_mask)); |
| 217 |
} |
| 218 |
#endif |
| 219 |
|
| 220 |
/* Setup gather sampler. */ |
| 221 |
|
| 222 |
struct ROGUE_TEXSTATE_SAMPLER_WORD0 word0; |
| 223 |
ROGUE_TEXSTATE_SAMPLER_WORD0_unpack(&sampler->descriptor.words[0], &word0); |
| 224 |
word0.mipfilter = false; |
| 225 |
word0.minfilter = ROGUE_TEXSTATE_FILTER_LINEAR; |
| 226 |
word0.magfilter = ROGUE_TEXSTATE_FILTER_LINEAR; |
| 227 |
ROGUE_TEXSTATE_SAMPLER_WORD0_pack(&sampler->descriptor.gather_words[0], |
| 228 |
&word0); |
| 229 |
|
| 230 |
memcpy(&sampler->descriptor.gather_words[1], |
| 231 |
&sampler->descriptor.words[1], |
| 232 |
sizeof(sampler->descriptor.words[1])); |
| 233 |
|
| 234 |
*pSampler = pvr_sampler_to_handle(sampler); |
| 235 |
|
| 236 |
return VK_SUCCESS; |
| 237 |
|
| 238 |
err_free_sampler: |
| 239 |
vk_object_free(&device->vk, pAllocator, sampler); |
| 240 |
|
| 241 |
err_out: |
| 242 |
return result; |
| 243 |
} |
| 244 |
|
| 245 |
void PVR_PER_ARCH(DestroySampler)(VkDevice _device, |
| 246 |
VkSampler _sampler, |
| 247 |
const VkAllocationCallbacks *pAllocator) |
| 248 |
{ |
| 249 |
VK_FROM_HANDLE(pvr_device, device, _device); |
| 250 |
VK_FROM_HANDLE(pvr_sampler, sampler, _sampler); |
| 251 |
|
| 252 |
if (!sampler) |
| 253 |
return; |
| 254 |
|
| 255 |
pvr_arch_border_color_table_release_entry(device->border_color_table, |
| 256 |
sampler->border_color_table_index); |
| 257 |
|
| 258 |
vk_sampler_destroy(&device->vk, pAllocator, &sampler->vk); |
| 259 |
} |