| 1 |
/* |
| 2 |
* Copyright © 2023 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to deal |
| 6 |
* in the Software without restriction, including without limitation the rights |
| 7 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
* copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <assert.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <stdint.h> |
| 27 |
#include <string.h> |
| 28 |
#include <vulkan/vulkan_core.h> |
| 29 |
|
| 30 |
#include "hwdef/rogue_hw_utils.h" |
| 31 |
#include "pvr_bo.h" |
| 32 |
#include "pvr_border.h" |
| 33 |
#include "pvr_device.h" |
| 34 |
#include "pvr_device_info.h" |
| 35 |
#include "pvr_formats.h" |
| 36 |
#include "pvr_macros.h" |
| 37 |
#include "pvr_physical_device.h" |
| 38 |
#include "pvr_sampler.h" |
| 39 |
#include "util/bitset.h" |
| 40 |
#include "util/format/u_format.h" |
| 41 |
#include "util/format/u_formats.h" |
| 42 |
#include "util/log.h" |
| 43 |
#include "util/macros.h" |
| 44 |
#include "vk_format.h" |
| 45 |
#include "vk_log.h" |
| 46 |
#include "vk_sampler.h" |
| 47 |
|
| 48 |
struct pvr_border_color_table_value { |
| 49 |
uint8_t value[16]; |
| 50 |
} PACKED; |
| 51 |
static_assert(sizeof(struct pvr_border_color_table_value) == |
| 52 |
4 * sizeof(uint32_t), |
| 53 |
"struct pvr_border_color_table_value must be 4 x u32"); |
| 54 |
|
| 55 |
struct pvr_border_color_table_entry { |
| 56 |
struct pvr_border_color_table_value values[PVR_TEX_FORMAT_COUNT]; |
| 57 |
struct pvr_border_color_table_value compressed_values[PVR_TEX_FORMAT_COUNT]; |
| 58 |
} PACKED; |
| 59 |
|
| 60 |
/* FIXME: Replace all instances of uint32_t with ROGUE_TEXSTATE_FORMAT or |
| 61 |
* ROGUE_TEXSTATE_FORMAT_COMPRESSED after the pvr_common cleanup is complete. |
| 62 |
*/ |
| 63 |
|
| 64 |
struct pvr_tex_format_description { |
| 65 |
enum pipe_format pipe_format_int; |
| 66 |
enum pipe_format pipe_format_float; |
| 67 |
}; |
| 68 |
|
| 69 |
struct pvr_tex_format_compressed_description { |
| 70 |
uint32_t tex_format; |
| 71 |
enum pipe_format pipe_format; |
| 72 |
uint32_t tex_format_simple; |
| 73 |
}; |
| 74 |
|
| 75 |
#define FORMAT(tex_fmt, pipe_fmt_int, pipe_fmt_float) \ |
| 76 |
[ROGUE_TEXSTATE_FORMAT_##tex_fmt] = { \ |
| 77 |
.desc = { \ |
| 78 |
.pipe_format_int = PIPE_FORMAT_##pipe_fmt_int, \ |
| 79 |
.pipe_format_float = PIPE_FORMAT_##pipe_fmt_float, \ |
| 80 |
}, \ |
| 81 |
.present = true, \ |
| 82 |
} |
| 83 |
|
| 84 |
static const struct pvr_tex_format_table_entry { |
| 85 |
struct pvr_tex_format_description desc; |
| 86 |
bool present; |
| 87 |
} pvr_tex_format_table[PVR_TEX_FORMAT_COUNT] = { |
| 88 |
/* 0 */ FORMAT(U8, R8_UINT, R8_UNORM), |
| 89 |
/* 1 */ FORMAT(S8, R8_SINT, R8_SNORM), |
| 90 |
/* 2 */ FORMAT(A4R4G4B4, A4R4G4B4_UINT, A4R4G4B4_UNORM), |
| 91 |
/* 4 */ FORMAT(A1R5G5B5, A1R5G5B5_UINT, B5G5R5A1_UNORM), |
| 92 |
/* 5 */ FORMAT(R5G6B5, R5G6B5_UINT, B5G6R5_UNORM), |
| 93 |
/* 7 */ FORMAT(U8U8, R8G8_UINT, R8G8_UNORM), |
| 94 |
/* 8 */ FORMAT(S8S8, R8G8_SINT, R8G8_SNORM), |
| 95 |
/* 9 */ FORMAT(U16, R16_UINT, R16_UNORM), |
| 96 |
/* 10 */ FORMAT(S16, R16_SINT, R16_SNORM), |
| 97 |
/* 11 */ FORMAT(F16, NONE, R16_FLOAT), |
| 98 |
/* 12 */ FORMAT(U8U8U8U8, R8G8B8A8_UINT, R8G8B8A8_UNORM), |
| 99 |
/* 13 */ FORMAT(S8S8S8S8, R8G8B8A8_SINT, R8G8B8A8_SNORM), |
| 100 |
/* 14 */ FORMAT(A2R10B10G10, R10G10B10A2_UINT, R10G10B10A2_UNORM), |
| 101 |
/* 15 */ FORMAT(U16U16, R16G16_UINT, R16G16_UNORM), |
| 102 |
/* 16 */ FORMAT(S16S16, R16G16_SINT, R16G16_SNORM), |
| 103 |
/* 17 */ FORMAT(F16F16, NONE, R16G16_FLOAT), |
| 104 |
/* 18 */ FORMAT(F32, NONE, R32_FLOAT), |
| 105 |
/* 21 */ FORMAT(X8U24, NONE, Z24X8_UNORM), |
| 106 |
/* 22 */ FORMAT(ST8U24, Z24_UNORM_S8_UINT, Z24_UNORM_S8_UINT), |
| 107 |
/* 23 */ FORMAT(U8X24, X24S8_UINT, NONE), |
| 108 |
/* 24 */ FORMAT(U32, R32_UINT, R32_UNORM), |
| 109 |
/* 25 */ FORMAT(S32, R32_SINT, R32_SNORM), |
| 110 |
/* 26 */ FORMAT(SE9995, NONE, R9G9B9E5_FLOAT), |
| 111 |
/* 28 */ FORMAT(F16F16F16F16, NONE, R16G16B16A16_FLOAT), |
| 112 |
/* 29 */ FORMAT(U16U16U16U16, R16G16B16A16_UINT, R16G16B16A16_UNORM), |
| 113 |
/* 30 */ FORMAT(S16S16S16S16, R16G16B16A16_SINT, R16G16B16A16_SNORM), |
| 114 |
/* 32 */ FORMAT(U16U16U16, R16G16B16_UINT, R16G16B16_UNORM), |
| 115 |
/* 33 */ FORMAT(S16S16S16, R16G16B16_SINT, R16G16B16_SNORM), |
| 116 |
/* 34 */ FORMAT(F32F32, NONE, R32G32_FLOAT), |
| 117 |
/* 35 */ FORMAT(U32U32, R32G32_UINT, R32G32_UNORM), |
| 118 |
/* 36 */ FORMAT(S32S32, R32G32_SINT, R32G32_SNORM), |
| 119 |
/* 37 */ FORMAT(X24U8F32, Z32_FLOAT_S8X24_UINT, Z32_FLOAT_S8X24_UINT), |
| 120 |
/* 38 */ FORMAT(X24X8F32, NONE, Z32_FLOAT_S8X24_UINT), |
| 121 |
/* 39 */ FORMAT(X24G8X32, X32_S8X24_UINT, NONE), |
| 122 |
/* 58 */ FORMAT(U8U8U8, R8G8B8_UINT, R8G8B8_UNORM), |
| 123 |
/* 61 */ FORMAT(F32F32F32F32, NONE, R32G32B32A32_FLOAT), |
| 124 |
/* 62 */ FORMAT(U32U32U32U32, R32G32B32A32_UINT, R32G32B32A32_UNORM), |
| 125 |
/* 63 */ FORMAT(S32S32S32S32, R32G32B32A32_SINT, R32G32B32A32_SNORM), |
| 126 |
/* 64 */ FORMAT(F32F32F32, NONE, R32G32B32_FLOAT), |
| 127 |
/* 65 */ FORMAT(U32U32U32, R32G32B32_UINT, R32G32B32_UNORM), |
| 128 |
/* 66 */ FORMAT(S32S32S32, R32G32B32_SINT, R32G32B32_SNORM), |
| 129 |
/* 88 */ FORMAT(F10F11F11, NONE, R11G11B10_FLOAT), |
| 130 |
}; |
| 131 |
|
| 132 |
#undef FORMAT |
| 133 |
|
| 134 |
#define FORMAT(tex_fmt, pipe_fmt, tex_fmt_simple) \ |
| 135 |
[ROGUE_TEXSTATE_FORMAT_COMPRESSED_##tex_fmt] = { \ |
| 136 |
.desc = { \ |
| 137 |
.tex_format = ROGUE_TEXSTATE_FORMAT_COMPRESSED_##tex_fmt, \ |
| 138 |
.pipe_format = PIPE_FORMAT_##pipe_fmt, \ |
| 139 |
.tex_format_simple = ROGUE_TEXSTATE_FORMAT_##tex_fmt_simple, \ |
| 140 |
}, \ |
| 141 |
.present = true, \ |
| 142 |
} |
| 143 |
|
| 144 |
static const struct pvr_tex_format_compressed_table_entry { |
| 145 |
struct pvr_tex_format_compressed_description desc; |
| 146 |
bool present; |
| 147 |
} pvr_tex_format_compressed_table[PVR_TEX_FORMAT_COUNT] = { |
| 148 |
/* 68 */ FORMAT(ETC2_RGB, ETC2_RGB8, U8U8U8U8), |
| 149 |
/* 69 */ FORMAT(ETC2A_RGBA, ETC2_RGBA8, U8U8U8U8), |
| 150 |
/* 70 */ FORMAT(ETC2_PUNCHTHROUGHA, ETC2_RGB8A1, U8U8U8U8), |
| 151 |
/* 71 */ FORMAT(EAC_R11_UNSIGNED, ETC2_R11_UNORM, U16U16U16U16), |
| 152 |
/* 72 */ FORMAT(EAC_R11_SIGNED, ETC2_R11_SNORM, S16S16S16S16), |
| 153 |
/* 73 */ FORMAT(EAC_RG11_UNSIGNED, ETC2_RG11_UNORM, U16U16U16U16), |
| 154 |
/* 74 */ FORMAT(EAC_RG11_SIGNED, ETC2_RG11_SNORM, S16S16S16S16), |
| 155 |
}; |
| 156 |
|
| 157 |
#undef FORMAT |
| 158 |
|
| 159 |
static inline bool tex_format_is_supported(const uint32_t tex_format) |
| 160 |
{ |
| 161 |
return tex_format < ARRAY_SIZE(pvr_tex_format_table) && |
| 162 |
pvr_tex_format_table[tex_format].present; |
| 163 |
} |
| 164 |
|
| 165 |
static inline const struct pvr_tex_format_description * |
| 166 |
get_tex_format_description(const uint32_t tex_format) |
| 167 |
{ |
| 168 |
assert(tex_format_is_supported(tex_format)); |
| 169 |
return &pvr_tex_format_table[tex_format].desc; |
| 170 |
} |
| 171 |
|
| 172 |
static inline bool tex_format_compressed_is_supported(const uint32_t tex_format) |
| 173 |
{ |
| 174 |
return tex_format < ARRAY_SIZE(pvr_tex_format_compressed_table) && |
| 175 |
pvr_tex_format_compressed_table[tex_format].present; |
| 176 |
} |
| 177 |
|
| 178 |
static inline const struct pvr_tex_format_compressed_description * |
| 179 |
get_tex_format_compressed_description(const uint32_t tex_format) |
| 180 |
{ |
| 181 |
assert(tex_format_compressed_is_supported(tex_format)); |
| 182 |
return &pvr_tex_format_compressed_table[tex_format].desc; |
| 183 |
} |
| 184 |
|
| 185 |
static inline void pvr_border_color_table_pack_single( |
| 186 |
struct pvr_border_color_table_value *const dst, |
| 187 |
const union pipe_color_union *const color, |
| 188 |
const struct pvr_tex_format_description *const pvr_tex_fmt_desc, |
| 189 |
const bool is_int, |
| 190 |
const struct pvr_device_info *const dev_info) |
| 191 |
{ |
| 192 |
enum pipe_format pipe_format = is_int ? pvr_tex_fmt_desc->pipe_format_int |
| 193 |
: pvr_tex_fmt_desc->pipe_format_float; |
| 194 |
|
| 195 |
if (pipe_format == PIPE_FORMAT_NONE) |
| 196 |
return; |
| 197 |
|
| 198 |
memset(dst->value, 0, sizeof(dst->value)); |
| 199 |
|
| 200 |
if (util_format_is_depth_or_stencil(pipe_format)) { |
| 201 |
if (is_int) { |
| 202 |
const uint8_t s_color[4] = { |
| 203 |
color->ui[0], |
| 204 |
color->ui[1], |
| 205 |
color->ui[2], |
| 206 |
color->ui[3], |
| 207 |
}; |
| 208 |
|
| 209 |
util_format_pack_s_8uint(pipe_format, dst->value, s_color, 1); |
| 210 |
} else { |
| 211 |
util_format_pack_z_float(pipe_format, dst->value, color->f, 1); |
| 212 |
} |
| 213 |
} else { |
| 214 |
if (PVR_HAS_FEATURE(dev_info, tpu_border_colour_enhanced)) { |
| 215 |
if (pipe_format == PIPE_FORMAT_R9G9B9E5_FLOAT) |
| 216 |
pipe_format = PIPE_FORMAT_R16G16B16A16_FLOAT; |
| 217 |
} |
| 218 |
|
| 219 |
util_format_pack_rgba(pipe_format, dst->value, color, 1); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
static inline void pvr_border_color_table_pack_single_compressed( |
| 224 |
struct pvr_border_color_table_value *const dst, |
| 225 |
const union pipe_color_union *const color, |
| 226 |
const struct pvr_tex_format_compressed_description *const pvr_tex_fmt_desc, |
| 227 |
const bool is_int, |
| 228 |
const struct pvr_device_info *const dev_info) |
| 229 |
{ |
| 230 |
if (PVR_HAS_FEATURE(dev_info, tpu_border_colour_enhanced)) { |
| 231 |
const struct pvr_tex_format_description *pvr_tex_fmt_desc_simple = |
| 232 |
get_tex_format_description(pvr_tex_fmt_desc->tex_format_simple); |
| 233 |
|
| 234 |
pvr_border_color_table_pack_single(dst, |
| 235 |
color, |
| 236 |
pvr_tex_fmt_desc_simple, |
| 237 |
is_int, |
| 238 |
dev_info); |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
memset(dst->value, 0, sizeof(dst->value)); |
| 243 |
|
| 244 |
pvr_finishme("Devices without tpu_border_colour_enhanced require entries " |
| 245 |
"for compressed formats to be stored in the table " |
| 246 |
"pre-compressed."); |
| 247 |
} |
| 248 |
|
| 249 |
static int32_t |
| 250 |
pvr_border_color_table_alloc_entry(struct pvr_border_color_table *const table) |
| 251 |
{ |
| 252 |
/* BITSET_FFS() returns a 1-indexed position or 0 if no bits are set. */ |
| 253 |
int32_t index = BITSET_FFS(table->unused_entries); |
| 254 |
if (!index--) |
| 255 |
return -1; |
| 256 |
|
| 257 |
assert(index >= PVR_BORDER_COLOR_TABLE_NR_BUILTIN_ENTRIES); |
| 258 |
|
| 259 |
BITSET_CLEAR(table->unused_entries, index); |
| 260 |
|
| 261 |
return index; |
| 262 |
} |
| 263 |
|
| 264 |
static void |
| 265 |
pvr_border_color_table_free_entry(struct pvr_border_color_table *const table, |
| 266 |
const uint32_t index) |
| 267 |
{ |
| 268 |
assert(pvr_border_color_table_is_index_valid(table, index)); |
| 269 |
BITSET_SET(table->unused_entries, index); |
| 270 |
} |
| 271 |
|
| 272 |
static void |
| 273 |
pvr_border_color_table_fill_entry(struct pvr_border_color_table *const table, |
| 274 |
const uint32_t index, |
| 275 |
const union pipe_color_union *const color, |
| 276 |
const bool is_int, |
| 277 |
const struct pvr_device_info *const dev_info) |
| 278 |
{ |
| 279 |
struct pvr_border_color_table_entry *const entries = table->table->bo->map; |
| 280 |
struct pvr_border_color_table_entry *const entry = &entries[index]; |
| 281 |
|
| 282 |
for (enum ROGUE_TEXSTATE_FORMAT tex_format = 0; |
| 283 |
tex_format < PVR_TEX_FORMAT_COUNT; |
| 284 |
tex_format++) { |
| 285 |
if (tex_format_is_supported(tex_format)) |
| 286 |
pvr_border_color_table_pack_single( |
| 287 |
&entry->values[tex_format], |
| 288 |
color, |
| 289 |
get_tex_format_description(tex_format), |
| 290 |
is_int, |
| 291 |
dev_info); |
| 292 |
} |
| 293 |
|
| 294 |
for (enum ROGUE_TEXSTATE_FORMAT_COMPRESSED tex_format = 0; |
| 295 |
tex_format < PVR_TEX_FORMAT_COUNT; |
| 296 |
tex_format++) { |
| 297 |
if (tex_format_compressed_is_supported(tex_format)) |
| 298 |
pvr_border_color_table_pack_single_compressed( |
| 299 |
&entry->compressed_values[tex_format], |
| 300 |
color, |
| 301 |
get_tex_format_compressed_description(tex_format), |
| 302 |
is_int, |
| 303 |
dev_info); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/** Attempt to invert a swizzle. |
| 308 |
* |
| 309 |
* If @param swz contains multiple channels with the same swizzle, this |
| 310 |
* operation will fail and return false. The @param dst should be preloaded |
| 311 |
* with suitable defaults (@var PIPE_SWIZZLE_0 or @var PIPE_SWIZZLE_1) for |
| 312 |
* channels with no source. |
| 313 |
* |
| 314 |
* For a given swizzle S, this function produces an inverse swizzle S' such |
| 315 |
* that for a given input color C: |
| 316 |
* |
| 317 |
* C * S => C' |
| 318 |
* C' * S' => C" |
| 319 |
* |
| 320 |
* The unswizzled color C" is a subset of the input color C, where channels not |
| 321 |
* contained in C' (because they weren't included as outputs in S) are set to |
| 322 |
* the defaults specified in S' as described above. |
| 323 |
* |
| 324 |
* @param swz The swizzle to invert |
| 325 |
* @param dst Output |
| 326 |
* @return true if the swizzle is invertible and the operation succeeded. |
| 327 |
*/ |
| 328 |
static bool pvr_invert_swizzle(const unsigned char swz[4], unsigned char dst[4]) |
| 329 |
{ |
| 330 |
bool found[4] = { false }; |
| 331 |
unsigned i, c; |
| 332 |
|
| 333 |
for (i = 0; i < 4; i++) { |
| 334 |
c = swz[i]; |
| 335 |
|
| 336 |
if (c > PIPE_SWIZZLE_W) |
| 337 |
continue; |
| 338 |
|
| 339 |
if (found[c]) |
| 340 |
return false; |
| 341 |
|
| 342 |
dst[c] = i; |
| 343 |
found[c] = true; |
| 344 |
} |
| 345 |
|
| 346 |
return true; |
| 347 |
} |
| 348 |
|
| 349 |
static inline void pvr_border_color_swizzle_to_tex_format( |
| 350 |
union pipe_color_union *const color, |
| 351 |
const enum pipe_format color_format, |
| 352 |
const struct pvr_tex_format_description *const pvr_tex_fmt_desc, |
| 353 |
bool is_int) |
| 354 |
{ |
| 355 |
const enum pipe_format tex_pipe_format = |
| 356 |
is_int ? pvr_tex_fmt_desc->pipe_format_int |
| 357 |
: pvr_tex_fmt_desc->pipe_format_float; |
| 358 |
|
| 359 |
const struct util_format_description *const color_format_desc = |
| 360 |
util_format_description(color_format); |
| 361 |
const struct util_format_description *const tex_format_desc = |
| 362 |
util_format_description(tex_pipe_format); |
| 363 |
|
| 364 |
union pipe_color_union swizzled_color; |
| 365 |
unsigned char composed_swizzle[4]; |
| 366 |
unsigned char color_unswizzle[4] = { |
| 367 |
PIPE_SWIZZLE_0, |
| 368 |
PIPE_SWIZZLE_0, |
| 369 |
PIPE_SWIZZLE_0, |
| 370 |
PIPE_SWIZZLE_1, |
| 371 |
}; |
| 372 |
const unsigned char *tpu_swizzle; |
| 373 |
|
| 374 |
ASSERTED bool invert_succeeded; |
| 375 |
|
| 376 |
if (color_format_desc->format == tex_pipe_format) |
| 377 |
return; |
| 378 |
|
| 379 |
/* Some format pairs (e.g. UNORM vs SRGB) fail the above test but still don't |
| 380 |
* require a re-swizzle. |
| 381 |
*/ |
| 382 |
if (memcmp(color_format_desc->swizzle, |
| 383 |
tex_format_desc->swizzle, |
| 384 |
sizeof(color_format_desc->swizzle)) == 0) { |
| 385 |
return; |
| 386 |
} |
| 387 |
|
| 388 |
mesa_logd("Mismatched border pipe formats: vk=%s, tex=%s", |
| 389 |
color_format_desc->short_name, |
| 390 |
tex_format_desc->short_name); |
| 391 |
|
| 392 |
tpu_swizzle = pvr_get_format_swizzle_for_tpu(color_format_desc); |
| 393 |
|
| 394 |
/* Any supported format for which this operation is necessary must have an |
| 395 |
* invertible swizzle. |
| 396 |
*/ |
| 397 |
invert_succeeded = pvr_invert_swizzle(tpu_swizzle, color_unswizzle); |
| 398 |
assert(invert_succeeded); |
| 399 |
|
| 400 |
util_format_compose_swizzles(color_unswizzle, |
| 401 |
tex_format_desc->swizzle, |
| 402 |
composed_swizzle); |
| 403 |
|
| 404 |
mesa_logd("Applying swizzle: %u%u%u%u", |
| 405 |
composed_swizzle[0], |
| 406 |
composed_swizzle[1], |
| 407 |
composed_swizzle[2], |
| 408 |
composed_swizzle[3]); |
| 409 |
|
| 410 |
util_format_apply_color_swizzle(&swizzled_color, |
| 411 |
color, |
| 412 |
composed_swizzle, |
| 413 |
is_int); |
| 414 |
|
| 415 |
*color = swizzled_color; |
| 416 |
} |
| 417 |
|
| 418 |
VkResult pvr_arch_border_color_table_init(struct pvr_device *const device) |
| 419 |
{ |
| 420 |
struct pvr_border_color_table *table = device->border_color_table = |
| 421 |
vk_zalloc(&device->vk.alloc, |
| 422 |
sizeof(struct pvr_border_color_table), |
| 423 |
8, |
| 424 |
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
| 425 |
if (!table) |
| 426 |
return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 427 |
|
| 428 |
const struct pvr_device_info *const dev_info = &device->pdevice->dev_info; |
| 429 |
const uint32_t cache_line_size = pvr_get_slc_cache_line_size(dev_info); |
| 430 |
const uint32_t table_size = sizeof(struct pvr_border_color_table_entry) * |
| 431 |
PVR_BORDER_COLOR_TABLE_NR_ENTRIES; |
| 432 |
|
| 433 |
VkResult result; |
| 434 |
|
| 435 |
/* Initialize to ones so ffs can be used to find unused entries. */ |
| 436 |
BITSET_ONES(table->unused_entries); |
| 437 |
|
| 438 |
result = pvr_bo_alloc(device, |
| 439 |
device->heaps.general_heap, |
| 440 |
table_size, |
| 441 |
cache_line_size, |
| 442 |
PVR_BO_ALLOC_FLAG_CPU_MAPPED, |
| 443 |
&table->table); |
| 444 |
if (result != VK_SUCCESS) |
| 445 |
goto err_out; |
| 446 |
|
| 447 |
BITSET_CLEAR_RANGE_INSIDE_WORD(table->unused_entries, |
| 448 |
0, |
| 449 |
PVR_BORDER_COLOR_TABLE_NR_BUILTIN_ENTRIES - |
| 450 |
1); |
| 451 |
|
| 452 |
for (uint32_t i = 0; i < PVR_BORDER_COLOR_TABLE_NR_BUILTIN_ENTRIES; i++) { |
| 453 |
const VkClearColorValue color = vk_border_color_value(i); |
| 454 |
const bool is_int = vk_border_color_is_int(i); |
| 455 |
|
| 456 |
pvr_border_color_table_fill_entry(table, |
| 457 |
i, |
| 458 |
(const union pipe_color_union *)&color, |
| 459 |
is_int, |
| 460 |
dev_info); |
| 461 |
} |
| 462 |
|
| 463 |
pvr_bo_cpu_unmap(device, table->table); |
| 464 |
|
| 465 |
return VK_SUCCESS; |
| 466 |
|
| 467 |
err_out: |
| 468 |
vk_free(&device->vk.alloc, table); |
| 469 |
|
| 470 |
return result; |
| 471 |
} |
| 472 |
|
| 473 |
void pvr_arch_border_color_table_finish(struct pvr_device *const device) |
| 474 |
{ |
| 475 |
#if MESA_DEBUG |
| 476 |
BITSET_SET_RANGE_INSIDE_WORD(device->border_color_table->unused_entries, |
| 477 |
0, |
| 478 |
PVR_BORDER_COLOR_TABLE_NR_BUILTIN_ENTRIES - 1); |
| 479 |
BITSET_NOT(device->border_color_table->unused_entries); |
| 480 |
assert(BITSET_IS_EMPTY(device->border_color_table->unused_entries)); |
| 481 |
#endif |
| 482 |
|
| 483 |
pvr_bo_free(device, device->border_color_table->table); |
| 484 |
vk_free(&device->vk.alloc, device->border_color_table); |
| 485 |
} |
| 486 |
|
| 487 |
static inline void pvr_border_color_table_set_custom_entry( |
| 488 |
struct pvr_border_color_table *const table, |
| 489 |
const uint32_t index, |
| 490 |
const VkFormat vk_format, |
| 491 |
const union pipe_color_union *const color, |
| 492 |
const bool is_int, |
| 493 |
const struct pvr_device_info *const dev_info) |
| 494 |
{ |
| 495 |
struct pvr_border_color_table_entry *const entries = table->table->bo->map; |
| 496 |
struct pvr_border_color_table_entry *const entry = &entries[index]; |
| 497 |
|
| 498 |
const enum pipe_format format = vk_format_to_pipe_format(vk_format); |
| 499 |
uint32_t tex_format = pvr_arch_get_tex_format(vk_format); |
| 500 |
|
| 501 |
assert(tex_format != ROGUE_TEXSTATE_FORMAT_INVALID); |
| 502 |
|
| 503 |
if (util_format_is_compressed(format)) { |
| 504 |
const struct pvr_tex_format_compressed_description *const |
| 505 |
pvr_tex_fmt_desc = get_tex_format_compressed_description(tex_format); |
| 506 |
|
| 507 |
pvr_border_color_table_pack_single_compressed( |
| 508 |
&entry->compressed_values[tex_format], |
| 509 |
color, |
| 510 |
pvr_tex_fmt_desc, |
| 511 |
is_int, |
| 512 |
dev_info); |
| 513 |
} else { |
| 514 |
const struct pvr_tex_format_description *const pvr_tex_fmt_desc = |
| 515 |
get_tex_format_description(tex_format); |
| 516 |
union pipe_color_union swizzled_color = *color; |
| 517 |
|
| 518 |
if (util_format_is_depth_or_stencil(format)) { |
| 519 |
VkImageAspectFlags aspect_mask; |
| 520 |
|
| 521 |
if (is_int) |
| 522 |
aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 523 |
else |
| 524 |
aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT; |
| 525 |
|
| 526 |
/* Write the border color entry at the index of the texture |
| 527 |
* format relative to the depth-only or stencil-only compoment |
| 528 |
* associated with this Vulkan format. |
| 529 |
*/ |
| 530 |
tex_format = pvr_arch_get_tex_format_aspect(vk_format, aspect_mask); |
| 531 |
assert(tex_format != ROGUE_TEXSTATE_FORMAT_INVALID); |
| 532 |
} |
| 533 |
|
| 534 |
pvr_border_color_swizzle_to_tex_format(&swizzled_color, |
| 535 |
format, |
| 536 |
pvr_tex_fmt_desc, |
| 537 |
is_int); |
| 538 |
|
| 539 |
pvr_border_color_table_pack_single(&entry->values[tex_format], |
| 540 |
&swizzled_color, |
| 541 |
pvr_tex_fmt_desc, |
| 542 |
is_int, |
| 543 |
dev_info); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
static VkResult pvr_border_color_table_create_custom_entry( |
| 548 |
struct pvr_device *const device, |
| 549 |
const struct pvr_sampler *const sampler, |
| 550 |
struct pvr_border_color_table *const table, |
| 551 |
uint32_t *const index_out) |
| 552 |
{ |
| 553 |
const bool is_int = vk_border_color_is_int(sampler->vk.border_color); |
| 554 |
const VkClearColorValue color = sampler->vk.border_color_value; |
| 555 |
const VkFormat vk_format = sampler->vk.format; |
| 556 |
const bool map_table = !table->table->bo->map; |
| 557 |
VkResult result; |
| 558 |
int32_t index; |
| 559 |
|
| 560 |
assert(vk_format != VK_FORMAT_UNDEFINED); |
| 561 |
|
| 562 |
index = pvr_border_color_table_alloc_entry(table); |
| 563 |
if (index < 0) |
| 564 |
goto err_out; |
| 565 |
|
| 566 |
if (map_table) { |
| 567 |
result = pvr_bo_cpu_map_unchanged(device, table->table); |
| 568 |
if (result != VK_SUCCESS) |
| 569 |
goto err_free_entry; |
| 570 |
} |
| 571 |
|
| 572 |
pvr_border_color_table_set_custom_entry( |
| 573 |
table, |
| 574 |
index, |
| 575 |
vk_format, |
| 576 |
(const union pipe_color_union *)&color, |
| 577 |
is_int, |
| 578 |
&device->pdevice->dev_info); |
| 579 |
|
| 580 |
if (map_table) |
| 581 |
pvr_bo_cpu_unmap(device, table->table); |
| 582 |
|
| 583 |
*index_out = index; |
| 584 |
|
| 585 |
return VK_SUCCESS; |
| 586 |
|
| 587 |
err_free_entry: |
| 588 |
pvr_border_color_table_free_entry(table, index); |
| 589 |
|
| 590 |
err_out: |
| 591 |
return vk_errorf(sampler, |
| 592 |
VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 593 |
"Failed to allocate border color table entry"); |
| 594 |
} |
| 595 |
|
| 596 |
VkResult pvr_arch_border_color_table_get_or_create_entry( |
| 597 |
struct pvr_device *const device, |
| 598 |
const struct pvr_sampler *const sampler, |
| 599 |
struct pvr_border_color_table *const table, |
| 600 |
uint32_t *const index_out) |
| 601 |
{ |
| 602 |
const VkBorderColor vk_type = sampler->vk.border_color; |
| 603 |
|
| 604 |
if (vk_type <= PVR_BORDER_COLOR_TABLE_NR_BUILTIN_ENTRIES) { |
| 605 |
*index_out = vk_type; |
| 606 |
return VK_SUCCESS; |
| 607 |
} |
| 608 |
|
| 609 |
return pvr_border_color_table_create_custom_entry(device, |
| 610 |
sampler, |
| 611 |
table, |
| 612 |
index_out); |
| 613 |
} |
| 614 |
|
| 615 |
void pvr_arch_border_color_table_release_entry( |
| 616 |
struct pvr_border_color_table *const table, |
| 617 |
const uint32_t index) |
| 618 |
{ |
| 619 |
if (index < PVR_BORDER_COLOR_TABLE_NR_BUILTIN_ENTRIES) |
| 620 |
return; |
| 621 |
|
| 622 |
pvr_border_color_table_free_entry(table, index); |
| 623 |
} |