1 /* 2 * Copyright 2016 Red Hat. 3 * Copyright 2016 Bas Nieuwenhuizen 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * IN THE SOFTWARE. 23 */ 24 25 #include "radv_private.h" 26 27 #include "vk_format.h" 28 #include "sid.h" 29 #include "r600d_common.h" 30 31 #include "util/u_half.h" 32 #include "util/format_srgb.h" 33 34 uint32_t radv_translate_buffer_dataformat(const struct vk_format_description *desc, 35 int first_non_void) 36 { 37 unsigned type; 38 int i; 39 40 if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) 41 return V_008F0C_BUF_DATA_FORMAT_10_11_11; 42 43 if (first_non_void < 0) 44 return V_008F0C_BUF_DATA_FORMAT_INVALID; 45 type = desc->channel[first_non_void].type; 46 47 if (type == VK_FORMAT_TYPE_FIXED) 48 return V_008F0C_BUF_DATA_FORMAT_INVALID; 49 if (desc->nr_channels == 4 && 50 desc->channel[0].size == 10 && 51 desc->channel[1].size == 10 && 52 desc->channel[2].size == 10 && 53 desc->channel[3].size == 2) 54 return V_008F0C_BUF_DATA_FORMAT_2_10_10_10; 55 56 /* See whether the components are of the same size. */ 57 for (i = 0; i < desc->nr_channels; i++) { 58 if (desc->channel[first_non_void].size != desc->channel[i].size) 59 return V_008F0C_BUF_DATA_FORMAT_INVALID; 60 } 61 62 switch (desc->channel[first_non_void].size) { 63 case 8: 64 switch (desc->nr_channels) { 65 case 1: 66 return V_008F0C_BUF_DATA_FORMAT_8; 67 case 2: 68 return V_008F0C_BUF_DATA_FORMAT_8_8; 69 case 4: 70 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8; 71 } 72 break; 73 case 16: 74 switch (desc->nr_channels) { 75 case 1: 76 return V_008F0C_BUF_DATA_FORMAT_16; 77 case 2: 78 return V_008F0C_BUF_DATA_FORMAT_16_16; 79 case 4: 80 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16; 81 } 82 break; 83 case 32: 84 /* From the Southern Islands ISA documentation about MTBUF: 85 * 'Memory reads of data in memory that is 32 or 64 bits do not 86 * undergo any format conversion.' 87 */ 88 if (type != VK_FORMAT_TYPE_FLOAT && 89 !desc->channel[first_non_void].pure_integer) 90 return V_008F0C_BUF_DATA_FORMAT_INVALID; 91 92 switch (desc->nr_channels) { 93 case 1: 94 return V_008F0C_BUF_DATA_FORMAT_32; 95 case 2: 96 return V_008F0C_BUF_DATA_FORMAT_32_32; 97 case 3: 98 return V_008F0C_BUF_DATA_FORMAT_32_32_32; 99 case 4: 100 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32; 101 } 102 break; 103 } 104 105 return V_008F0C_BUF_DATA_FORMAT_INVALID; 106 } 107 108 uint32_t radv_translate_buffer_numformat(const struct vk_format_description *desc, 109 int first_non_void) 110 { 111 if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) 112 return V_008F0C_BUF_NUM_FORMAT_FLOAT; 113 114 if (first_non_void < 0) 115 return ~0; 116 117 switch (desc->channel[first_non_void].type) { 118 case VK_FORMAT_TYPE_SIGNED: 119 if (desc->channel[first_non_void].normalized) 120 return V_008F0C_BUF_NUM_FORMAT_SNORM; 121 else if (desc->channel[first_non_void].pure_integer) 122 return V_008F0C_BUF_NUM_FORMAT_SINT; 123 else 124 return V_008F0C_BUF_NUM_FORMAT_SSCALED; 125 break; 126 case VK_FORMAT_TYPE_UNSIGNED: 127 if (desc->channel[first_non_void].normalized) 128 return V_008F0C_BUF_NUM_FORMAT_UNORM; 129 else if (desc->channel[first_non_void].pure_integer) 130 return V_008F0C_BUF_NUM_FORMAT_UINT; 131 else 132 return V_008F0C_BUF_NUM_FORMAT_USCALED; 133 break; 134 case VK_FORMAT_TYPE_FLOAT: 135 default: 136 return V_008F0C_BUF_NUM_FORMAT_FLOAT; 137 } 138 } 139 140 uint32_t radv_translate_tex_dataformat(VkFormat format, 141 const struct vk_format_description *desc, 142 int first_non_void) 143 { 144 bool uniform = true; 145 int i; 146 147 if (!desc) 148 return ~0; 149 /* Colorspace (return non-RGB formats directly). */ 150 switch (desc->colorspace) { 151 /* Depth stencil formats */ 152 case VK_FORMAT_COLORSPACE_ZS: 153 switch (format) { 154 case VK_FORMAT_D16_UNORM: 155 return V_008F14_IMG_DATA_FORMAT_16; 156 case VK_FORMAT_D24_UNORM_S8_UINT: 157 case VK_FORMAT_X8_D24_UNORM_PACK32: 158 return V_008F14_IMG_DATA_FORMAT_8_24; 159 case VK_FORMAT_S8_UINT: 160 return V_008F14_IMG_DATA_FORMAT_8; 161 case VK_FORMAT_D32_SFLOAT: 162 return V_008F14_IMG_DATA_FORMAT_32; 163 case VK_FORMAT_D32_SFLOAT_S8_UINT: 164 return V_008F14_IMG_DATA_FORMAT_X24_8_32; 165 default: 166 goto out_unknown; 167 } 168 169 case VK_FORMAT_COLORSPACE_YUV: 170 goto out_unknown; /* TODO */ 171 172 case VK_FORMAT_COLORSPACE_SRGB: 173 if (desc->nr_channels != 4 && desc->nr_channels != 1) 174 goto out_unknown; 175 break; 176 177 default: 178 break; 179 } 180 181 if (desc->layout == VK_FORMAT_LAYOUT_RGTC) { 182 switch(format) { 183 case VK_FORMAT_BC4_UNORM_BLOCK: 184 case VK_FORMAT_BC4_SNORM_BLOCK: 185 return V_008F14_IMG_DATA_FORMAT_BC4; 186 case VK_FORMAT_BC5_UNORM_BLOCK: 187 case VK_FORMAT_BC5_SNORM_BLOCK: 188 return V_008F14_IMG_DATA_FORMAT_BC5; 189 default: 190 break; 191 } 192 } 193 194 if (desc->layout == VK_FORMAT_LAYOUT_S3TC) { 195 switch(format) { 196 case VK_FORMAT_BC1_RGB_UNORM_BLOCK: 197 case VK_FORMAT_BC1_RGB_SRGB_BLOCK: 198 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: 199 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK: 200 return V_008F14_IMG_DATA_FORMAT_BC1; 201 case VK_FORMAT_BC2_UNORM_BLOCK: 202 case VK_FORMAT_BC2_SRGB_BLOCK: 203 return V_008F14_IMG_DATA_FORMAT_BC2; 204 case VK_FORMAT_BC3_UNORM_BLOCK: 205 case VK_FORMAT_BC3_SRGB_BLOCK: 206 return V_008F14_IMG_DATA_FORMAT_BC3; 207 default: 208 break; 209 } 210 } 211 212 if (desc->layout == VK_FORMAT_LAYOUT_BPTC) { 213 switch(format) { 214 case VK_FORMAT_BC6H_UFLOAT_BLOCK: 215 case VK_FORMAT_BC6H_SFLOAT_BLOCK: 216 return V_008F14_IMG_DATA_FORMAT_BC6; 217 case VK_FORMAT_BC7_UNORM_BLOCK: 218 case VK_FORMAT_BC7_SRGB_BLOCK: 219 return V_008F14_IMG_DATA_FORMAT_BC7; 220 default: 221 break; 222 } 223 } 224 225 if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) { 226 return V_008F14_IMG_DATA_FORMAT_5_9_9_9; 227 } else if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) { 228 return V_008F14_IMG_DATA_FORMAT_10_11_11; 229 } 230 231 /* R8G8Bx_SNORM - TODO CxV8U8 */ 232 233 /* hw cannot support mixed formats (except depth/stencil, since only 234 * depth is read).*/ 235 if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS) 236 goto out_unknown; 237 238 /* See whether the components are of the same size. */ 239 for (i = 1; i < desc->nr_channels; i++) { 240 uniform = uniform && desc->channel[0].size == desc->channel[i].size; 241 } 242 243 /* Non-uniform formats. */ 244 if (!uniform) { 245 switch(desc->nr_channels) { 246 case 3: 247 if (desc->channel[0].size == 5 && 248 desc->channel[1].size == 6 && 249 desc->channel[2].size == 5) { 250 return V_008F14_IMG_DATA_FORMAT_5_6_5; 251 } 252 goto out_unknown; 253 case 4: 254 if (desc->channel[0].size == 5 && 255 desc->channel[1].size == 5 && 256 desc->channel[2].size == 5 && 257 desc->channel[3].size == 1) { 258 return V_008F14_IMG_DATA_FORMAT_1_5_5_5; 259 } 260 if (desc->channel[0].size == 1 && 261 desc->channel[1].size == 5 && 262 desc->channel[2].size == 5 && 263 desc->channel[3].size == 5) { 264 return V_008F14_IMG_DATA_FORMAT_5_5_5_1; 265 } 266 if (desc->channel[0].size == 10 && 267 desc->channel[1].size == 10 && 268 desc->channel[2].size == 10 && 269 desc->channel[3].size == 2) { 270 /* Closed VK driver does this also no 2/10/10/10 snorm */ 271 if (desc->channel[0].type == VK_FORMAT_TYPE_SIGNED && 272 desc->channel[0].normalized) 273 goto out_unknown; 274 return V_008F14_IMG_DATA_FORMAT_2_10_10_10; 275 } 276 goto out_unknown; 277 } 278 goto out_unknown; 279 } 280 281 if (first_non_void < 0 || first_non_void > 3) 282 goto out_unknown; 283 284 /* uniform formats */ 285 switch (desc->channel[first_non_void].size) { 286 case 4: 287 switch (desc->nr_channels) { 288 #if 0 /* Not supported for render targets */ 289 case 2: 290 return V_008F14_IMG_DATA_FORMAT_4_4; 291 #endif 292 case 4: 293 return V_008F14_IMG_DATA_FORMAT_4_4_4_4; 294 } 295 break; 296 case 8: 297 switch (desc->nr_channels) { 298 case 1: 299 return V_008F14_IMG_DATA_FORMAT_8; 300 case 2: 301 return V_008F14_IMG_DATA_FORMAT_8_8; 302 case 4: 303 return V_008F14_IMG_DATA_FORMAT_8_8_8_8; 304 } 305 break; 306 case 16: 307 switch (desc->nr_channels) { 308 case 1: 309 return V_008F14_IMG_DATA_FORMAT_16; 310 case 2: 311 return V_008F14_IMG_DATA_FORMAT_16_16; 312 case 4: 313 return V_008F14_IMG_DATA_FORMAT_16_16_16_16; 314 } 315 break; 316 case 32: 317 switch (desc->nr_channels) { 318 case 1: 319 return V_008F14_IMG_DATA_FORMAT_32; 320 case 2: 321 return V_008F14_IMG_DATA_FORMAT_32_32; 322 #if 0 /* Not supported for render targets */ 323 case 3: 324 return V_008F14_IMG_DATA_FORMAT_32_32_32; 325 #endif 326 case 4: 327 return V_008F14_IMG_DATA_FORMAT_32_32_32_32; 328 } 329 } 330 331 out_unknown: 332 /* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */ 333 return ~0; 334 } 335 336 uint32_t radv_translate_tex_numformat(VkFormat format, 337 const struct vk_format_description *desc, 338 int first_non_void) 339 { 340 switch (format) { 341 case VK_FORMAT_D24_UNORM_S8_UINT: 342 return V_008F14_IMG_NUM_FORMAT_UNORM; 343 default: 344 if (first_non_void < 0) { 345 if (vk_format_is_compressed(format)) { 346 switch (format) { 347 case VK_FORMAT_BC1_RGB_SRGB_BLOCK: 348 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK: 349 case VK_FORMAT_BC2_SRGB_BLOCK: 350 case VK_FORMAT_BC3_SRGB_BLOCK: 351 case VK_FORMAT_BC7_SRGB_BLOCK: 352 return V_008F14_IMG_NUM_FORMAT_SRGB; 353 case VK_FORMAT_BC4_SNORM_BLOCK: 354 case VK_FORMAT_BC5_SNORM_BLOCK: 355 case VK_FORMAT_BC6H_SFLOAT_BLOCK: 356 return V_008F14_IMG_NUM_FORMAT_SNORM; 357 default: 358 return V_008F14_IMG_NUM_FORMAT_UNORM; 359 } 360 } else if (desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) { 361 return V_008F14_IMG_NUM_FORMAT_UNORM; 362 } else { 363 return V_008F14_IMG_NUM_FORMAT_FLOAT; 364 } 365 } else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) { 366 return V_008F14_IMG_NUM_FORMAT_SRGB; 367 } else { 368 switch (desc->channel[first_non_void].type) { 369 case VK_FORMAT_TYPE_FLOAT: 370 return V_008F14_IMG_NUM_FORMAT_FLOAT; 371 case VK_FORMAT_TYPE_SIGNED: 372 if (desc->channel[first_non_void].normalized) 373 return V_008F14_IMG_NUM_FORMAT_SNORM; 374 else if (desc->channel[first_non_void].pure_integer) 375 return V_008F14_IMG_NUM_FORMAT_SINT; 376 else 377 return V_008F14_IMG_NUM_FORMAT_SSCALED; 378 case VK_FORMAT_TYPE_UNSIGNED: 379 if (desc->channel[first_non_void].normalized) 380 return V_008F14_IMG_NUM_FORMAT_UNORM; 381 else if (desc->channel[first_non_void].pure_integer) 382 return V_008F14_IMG_NUM_FORMAT_UINT; 383 else 384 return V_008F14_IMG_NUM_FORMAT_USCALED; 385 default: 386 return V_008F14_IMG_NUM_FORMAT_UNORM; 387 } 388 } 389 } 390 } 391 392 uint32_t radv_translate_color_numformat(VkFormat format, 393 const struct vk_format_description *desc, 394 int first_non_void) 395 { 396 unsigned ntype; 397 if (first_non_void == -1 || desc->channel[first_non_void].type == VK_FORMAT_TYPE_FLOAT) 398 ntype = V_028C70_NUMBER_FLOAT; 399 else { 400 ntype = V_028C70_NUMBER_UNORM; 401 if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) 402 ntype = V_028C70_NUMBER_SRGB; 403 else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_SIGNED) { 404 if (desc->channel[first_non_void].pure_integer) { 405 ntype = V_028C70_NUMBER_SINT; 406 } else if (desc->channel[first_non_void].normalized) { 407 ntype = V_028C70_NUMBER_SNORM; 408 } else 409 ntype = ~0u; 410 } else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_UNSIGNED) { 411 if (desc->channel[first_non_void].pure_integer) { 412 ntype = V_028C70_NUMBER_UINT; 413 } else if (desc->channel[first_non_void].normalized) { 414 ntype = V_028C70_NUMBER_UNORM; 415 } else 416 ntype = ~0u; 417 } 418 } 419 return ntype; 420 } 421 422 static bool radv_is_sampler_format_supported(VkFormat format, bool *linear_sampling) 423 { 424 const struct vk_format_description *desc = vk_format_description(format); 425 uint32_t num_format; 426 if (!desc || format == VK_FORMAT_UNDEFINED) 427 return false; 428 num_format = radv_translate_tex_numformat(format, desc, 429 vk_format_get_first_non_void_channel(format)); 430 431 if (num_format == V_008F14_IMG_NUM_FORMAT_USCALED || 432 num_format == V_008F14_IMG_NUM_FORMAT_SSCALED) 433 return false; 434 435 if (num_format == V_008F14_IMG_NUM_FORMAT_UNORM || 436 num_format == V_008F14_IMG_NUM_FORMAT_SNORM || 437 num_format == V_008F14_IMG_NUM_FORMAT_FLOAT || 438 num_format == V_008F14_IMG_NUM_FORMAT_SRGB) 439 *linear_sampling = true; 440 else 441 *linear_sampling = false; 442 return radv_translate_tex_dataformat(format, vk_format_description(format), 443 vk_format_get_first_non_void_channel(format)) != ~0U; 444 } 445 446 447 static bool radv_is_storage_image_format_supported(struct radv_physical_device *physical_device, 448 VkFormat format) 449 { 450 const struct vk_format_description *desc = vk_format_description(format); 451 unsigned data_format, num_format; 452 if (!desc || format == VK_FORMAT_UNDEFINED) 453 return false; 454 455 data_format = radv_translate_tex_dataformat(format, desc, 456 vk_format_get_first_non_void_channel(format)); 457 num_format = radv_translate_tex_numformat(format, desc, 458 vk_format_get_first_non_void_channel(format)); 459 460 if(data_format == ~0 || num_format == ~0) 461 return false; 462 463 /* Extracted from the GCN3 ISA document. */ 464 switch(num_format) { 465 case V_008F14_IMG_NUM_FORMAT_UNORM: 466 case V_008F14_IMG_NUM_FORMAT_SNORM: 467 case V_008F14_IMG_NUM_FORMAT_UINT: 468 case V_008F14_IMG_NUM_FORMAT_SINT: 469 case V_008F14_IMG_NUM_FORMAT_FLOAT: 470 break; 471 default: 472 return false; 473 } 474 475 switch(data_format) { 476 case V_008F14_IMG_DATA_FORMAT_8: 477 case V_008F14_IMG_DATA_FORMAT_16: 478 case V_008F14_IMG_DATA_FORMAT_8_8: 479 case V_008F14_IMG_DATA_FORMAT_32: 480 case V_008F14_IMG_DATA_FORMAT_16_16: 481 case V_008F14_IMG_DATA_FORMAT_10_11_11: 482 case V_008F14_IMG_DATA_FORMAT_11_11_10: 483 case V_008F14_IMG_DATA_FORMAT_10_10_10_2: 484 case V_008F14_IMG_DATA_FORMAT_2_10_10_10: 485 case V_008F14_IMG_DATA_FORMAT_8_8_8_8: 486 case V_008F14_IMG_DATA_FORMAT_32_32: 487 case V_008F14_IMG_DATA_FORMAT_16_16_16_16: 488 case V_008F14_IMG_DATA_FORMAT_32_32_32_32: 489 case V_008F14_IMG_DATA_FORMAT_5_6_5: 490 case V_008F14_IMG_DATA_FORMAT_1_5_5_5: 491 case V_008F14_IMG_DATA_FORMAT_5_5_5_1: 492 case V_008F14_IMG_DATA_FORMAT_4_4_4_4: 493 /* TODO: FMASK formats. */ 494 return true; 495 default: 496 return false; 497 } 498 } 499 500 static bool radv_is_buffer_format_supported(VkFormat format) 501 { 502 const struct vk_format_description *desc = vk_format_description(format); 503 unsigned data_format, num_format; 504 if (!desc || format == VK_FORMAT_UNDEFINED) 505 return false; 506 507 data_format = radv_translate_buffer_dataformat(desc, 508 vk_format_get_first_non_void_channel(format)); 509 num_format = radv_translate_buffer_numformat(desc, 510 vk_format_get_first_non_void_channel(format)); 511 512 return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID && 513 num_format != ~0; 514 } 515 516 bool radv_is_colorbuffer_format_supported(VkFormat format, bool *blendable) 517 { 518 const struct vk_format_description *desc = vk_format_description(format); 519 uint32_t color_format = radv_translate_colorformat(format); 520 uint32_t color_swap = radv_translate_colorswap(format, false); 521 uint32_t color_num_format = radv_translate_color_numformat(format, 522 desc, 523 vk_format_get_first_non_void_channel(format)); 524 525 if (color_num_format == V_028C70_NUMBER_UINT || color_num_format == V_028C70_NUMBER_SINT || 526 color_format == V_028C70_COLOR_8_24 || color_format == V_028C70_COLOR_24_8 || 527 color_format == V_028C70_COLOR_X24_8_32_FLOAT) { 528 *blendable = false; 529 } else 530 *blendable = true; 531 return color_format != V_028C70_COLOR_INVALID && 532 color_swap != ~0U && 533 color_num_format != ~0; 534 } 535 536 static bool radv_is_zs_format_supported(VkFormat format) 537 { 538 return radv_translate_dbformat(format) != V_028040_Z_INVALID; 539 } 540 541 static void 542 radv_physical_device_get_format_properties(struct radv_physical_device *physical_device, 543 VkFormat format, 544 VkFormatProperties *out_properties) 545 { 546 VkFormatFeatureFlags linear = 0, tiled = 0, buffer = 0; 547 const struct vk_format_description *desc = vk_format_description(format); 548 bool blendable; 549 if (!desc) { 550 out_properties->linearTilingFeatures = linear; 551 out_properties->optimalTilingFeatures = tiled; 552 out_properties->bufferFeatures = buffer; 553 return; 554 } 555 556 if (radv_is_storage_image_format_supported(physical_device, format)) { 557 tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT; 558 linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT; 559 } 560 561 if (radv_is_buffer_format_supported(format)) { 562 buffer |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT | 563 VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT | 564 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT; 565 } 566 567 if (vk_format_is_depth_or_stencil(format)) { 568 if (radv_is_zs_format_supported(format)) 569 tiled |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT; 570 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT; 571 tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT | 572 VK_FORMAT_FEATURE_BLIT_DST_BIT; 573 } else { 574 bool linear_sampling; 575 if (radv_is_sampler_format_supported(format, &linear_sampling)) { 576 linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | 577 VK_FORMAT_FEATURE_BLIT_SRC_BIT; 578 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | 579 VK_FORMAT_FEATURE_BLIT_SRC_BIT; 580 if (linear_sampling) { 581 linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT; 582 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT; 583 } 584 } 585 if (radv_is_colorbuffer_format_supported(format, &blendable)) { 586 linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT; 587 tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT; 588 if (blendable) { 589 linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT; 590 tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT; 591 } 592 } 593 } 594 595 if (format == VK_FORMAT_R32_UINT || format == VK_FORMAT_R32_SINT) { 596 buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT; 597 linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT; 598 tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT; 599 } 600 601 out_properties->linearTilingFeatures = linear; 602 out_properties->optimalTilingFeatures = tiled; 603 out_properties->bufferFeatures = buffer; 604 } 605 606 uint32_t radv_translate_colorformat(VkFormat format) 607 { 608 const struct vk_format_description *desc = vk_format_description(format); 609 610 #define HAS_SIZE(x,y,z,w) \ 611 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \ 612 desc->channel[2].size == (z) && desc->channel[3].size == (w)) 613 614 if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) /* isn't plain */ 615 return V_028C70_COLOR_10_11_11; 616 617 if (desc->layout != VK_FORMAT_LAYOUT_PLAIN) 618 return V_028C70_COLOR_INVALID; 619 620 /* hw cannot support mixed formats (except depth/stencil, since 621 * stencil is not written to). */ 622 if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS) 623 return V_028C70_COLOR_INVALID; 624 625 switch (desc->nr_channels) { 626 case 1: 627 switch (desc->channel[0].size) { 628 case 8: 629 return V_028C70_COLOR_8; 630 case 16: 631 return V_028C70_COLOR_16; 632 case 32: 633 return V_028C70_COLOR_32; 634 } 635 break; 636 case 2: 637 if (desc->channel[0].size == desc->channel[1].size) { 638 switch (desc->channel[0].size) { 639 case 8: 640 return V_028C70_COLOR_8_8; 641 case 16: 642 return V_028C70_COLOR_16_16; 643 case 32: 644 return V_028C70_COLOR_32_32; 645 } 646 } else if (HAS_SIZE(8,24,0,0)) { 647 return V_028C70_COLOR_24_8; 648 } else if (HAS_SIZE(24,8,0,0)) { 649 return V_028C70_COLOR_8_24; 650 } 651 break; 652 case 3: 653 if (HAS_SIZE(5,6,5,0)) { 654 return V_028C70_COLOR_5_6_5; 655 } else if (HAS_SIZE(32,8,24,0)) { 656 return V_028C70_COLOR_X24_8_32_FLOAT; 657 } 658 break; 659 case 4: 660 if (desc->channel[0].size == desc->channel[1].size && 661 desc->channel[0].size == desc->channel[2].size && 662 desc->channel[0].size == desc->channel[3].size) { 663 switch (desc->channel[0].size) { 664 case 4: 665 return V_028C70_COLOR_4_4_4_4; 666 case 8: 667 return V_028C70_COLOR_8_8_8_8; 668 case 16: 669 return V_028C70_COLOR_16_16_16_16; 670 case 32: 671 return V_028C70_COLOR_32_32_32_32; 672 } 673 } else if (HAS_SIZE(5,5,5,1)) { 674 return V_028C70_COLOR_1_5_5_5; 675 } else if (HAS_SIZE(1,5,5,5)) { 676 return V_028C70_COLOR_5_5_5_1; 677 } else if (HAS_SIZE(10,10,10,2)) { 678 return V_028C70_COLOR_2_10_10_10; 679 } 680 break; 681 } 682 return V_028C70_COLOR_INVALID; 683 } 684 685 uint32_t radv_colorformat_endian_swap(uint32_t colorformat) 686 { 687 if (0/*SI_BIG_ENDIAN*/) { 688 switch(colorformat) { 689 /* 8-bit buffers. */ 690 case V_028C70_COLOR_8: 691 return V_028C70_ENDIAN_NONE; 692 693 /* 16-bit buffers. */ 694 case V_028C70_COLOR_5_6_5: 695 case V_028C70_COLOR_1_5_5_5: 696 case V_028C70_COLOR_4_4_4_4: 697 case V_028C70_COLOR_16: 698 case V_028C70_COLOR_8_8: 699 return V_028C70_ENDIAN_8IN16; 700 701 /* 32-bit buffers. */ 702 case V_028C70_COLOR_8_8_8_8: 703 case V_028C70_COLOR_2_10_10_10: 704 case V_028C70_COLOR_8_24: 705 case V_028C70_COLOR_24_8: 706 case V_028C70_COLOR_16_16: 707 return V_028C70_ENDIAN_8IN32; 708 709 /* 64-bit buffers. */ 710 case V_028C70_COLOR_16_16_16_16: 711 return V_028C70_ENDIAN_8IN16; 712 713 case V_028C70_COLOR_32_32: 714 return V_028C70_ENDIAN_8IN32; 715 716 /* 128-bit buffers. */ 717 case V_028C70_COLOR_32_32_32_32: 718 return V_028C70_ENDIAN_8IN32; 719 default: 720 return V_028C70_ENDIAN_NONE; /* Unsupported. */ 721 } 722 } else { 723 return V_028C70_ENDIAN_NONE; 724 } 725 } 726 727 uint32_t radv_translate_dbformat(VkFormat format) 728 { 729 switch (format) { 730 case VK_FORMAT_D16_UNORM: 731 case VK_FORMAT_D16_UNORM_S8_UINT: 732 return V_028040_Z_16; 733 case VK_FORMAT_D32_SFLOAT: 734 case VK_FORMAT_D32_SFLOAT_S8_UINT: 735 return V_028040_Z_32_FLOAT; 736 default: 737 return V_028040_Z_INVALID; 738 } 739 } 740 741 unsigned radv_translate_colorswap(VkFormat format, bool do_endian_swap) 742 { 743 const struct vk_format_description *desc = vk_format_description(format); 744 745 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == VK_SWIZZLE_##swz) 746 747 if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) 748 return V_0280A0_SWAP_STD; 749 750 if (desc->layout != VK_FORMAT_LAYOUT_PLAIN) 751 return ~0U; 752 753 switch (desc->nr_channels) { 754 case 1: 755 if (HAS_SWIZZLE(0,X)) 756 return V_0280A0_SWAP_STD; /* X___ */ 757 else if (HAS_SWIZZLE(3,X)) 758 return V_0280A0_SWAP_ALT_REV; /* ___X */ 759 break; 760 case 2: 761 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) || 762 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) || 763 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y))) 764 return V_0280A0_SWAP_STD; /* XY__ */ 765 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) || 766 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) || 767 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X))) 768 /* YX__ */ 769 return (do_endian_swap ? V_0280A0_SWAP_STD : V_0280A0_SWAP_STD_REV); 770 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y)) 771 return V_0280A0_SWAP_ALT; /* X__Y */ 772 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X)) 773 return V_0280A0_SWAP_ALT_REV; /* Y__X */ 774 break; 775 case 3: 776 if (HAS_SWIZZLE(0,X)) 777 return (do_endian_swap ? V_0280A0_SWAP_STD_REV : V_0280A0_SWAP_STD); 778 else if (HAS_SWIZZLE(0,Z)) 779 return V_0280A0_SWAP_STD_REV; /* ZYX */ 780 break; 781 case 4: 782 /* check the middle channels, the 1st and 4th channel can be NONE */ 783 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) { 784 return V_0280A0_SWAP_STD; /* XYZW */ 785 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) { 786 return V_0280A0_SWAP_STD_REV; /* WZYX */ 787 } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) { 788 return V_0280A0_SWAP_ALT; /* ZYXW */ 789 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) { 790 /* YZWX */ 791 if (desc->is_array) 792 return V_0280A0_SWAP_ALT_REV; 793 else 794 return (do_endian_swap ? V_0280A0_SWAP_ALT : V_0280A0_SWAP_ALT_REV); 795 } 796 break; 797 } 798 return ~0U; 799 } 800 801 bool radv_format_pack_clear_color(VkFormat format, 802 uint32_t clear_vals[2], 803 VkClearColorValue *value) 804 { 805 uint8_t r = 0, g = 0, b = 0, a = 0; 806 const struct vk_format_description *desc = vk_format_description(format); 807 808 if (vk_format_get_component_bits(format, VK_FORMAT_COLORSPACE_RGB, 0) <= 8) { 809 if (desc->colorspace == VK_FORMAT_COLORSPACE_RGB) { 810 r = float_to_ubyte(value->float32[0]); 811 g = float_to_ubyte(value->float32[1]); 812 b = float_to_ubyte(value->float32[2]); 813 a = float_to_ubyte(value->float32[3]); 814 } else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) { 815 r = util_format_linear_float_to_srgb_8unorm(value->float32[0]); 816 g = util_format_linear_float_to_srgb_8unorm(value->float32[1]); 817 b = util_format_linear_float_to_srgb_8unorm(value->float32[2]); 818 a = float_to_ubyte(value->float32[3]); 819 } 820 } 821 switch (format) { 822 case VK_FORMAT_R8_UNORM: 823 case VK_FORMAT_R8_SRGB: 824 clear_vals[0] = r; 825 clear_vals[1] = 0; 826 break; 827 case VK_FORMAT_R8G8_UNORM: 828 case VK_FORMAT_R8G8_SRGB: 829 clear_vals[0] = r | g << 8; 830 clear_vals[1] = 0; 831 break; 832 case VK_FORMAT_R8G8B8A8_SRGB: 833 case VK_FORMAT_R8G8B8A8_UNORM: 834 clear_vals[0] = r | g << 8 | b << 16 | a << 24; 835 clear_vals[1] = 0; 836 break; 837 case VK_FORMAT_B8G8R8A8_SRGB: 838 case VK_FORMAT_B8G8R8A8_UNORM: 839 clear_vals[0] = b | g << 8 | r << 16 | a << 24; 840 clear_vals[1] = 0; 841 break; 842 case VK_FORMAT_A8B8G8R8_UNORM_PACK32: 843 case VK_FORMAT_A8B8G8R8_SRGB_PACK32: 844 clear_vals[0] = r | g << 8 | b << 16 | a << 24; 845 clear_vals[1] = 0; 846 break; 847 case VK_FORMAT_R8_UINT: 848 clear_vals[0] = value->uint32[0] & 0xff; 849 clear_vals[1] = 0; 850 break; 851 case VK_FORMAT_R16_UINT: 852 clear_vals[0] = value->uint32[0] & 0xffff; 853 clear_vals[1] = 0; 854 break; 855 case VK_FORMAT_R8G8_UINT: 856 clear_vals[0] = value->uint32[0] & 0xff; 857 clear_vals[0] |= (value->uint32[1] & 0xff) << 8; 858 clear_vals[1] = 0; 859 break; 860 case VK_FORMAT_R8G8B8A8_UINT: 861 clear_vals[0] = value->uint32[0] & 0xff; 862 clear_vals[0] |= (value->uint32[1] & 0xff) << 8; 863 clear_vals[0] |= (value->uint32[2] & 0xff) << 16; 864 clear_vals[0] |= (value->uint32[3] & 0xff) << 24; 865 clear_vals[1] = 0; 866 break; 867 case VK_FORMAT_A8B8G8R8_UINT_PACK32: 868 clear_vals[0] = value->uint32[0] & 0xff; 869 clear_vals[0] |= (value->uint32[1] & 0xff) << 8; 870 clear_vals[0] |= (value->uint32[2] & 0xff) << 16; 871 clear_vals[0] |= (value->uint32[3] & 0xff) << 24; 872 clear_vals[1] = 0; 873 break; 874 case VK_FORMAT_R16G16_UINT: 875 clear_vals[0] = value->uint32[0] & 0xffff; 876 clear_vals[0] |= (value->uint32[1] & 0xffff) << 16; 877 clear_vals[1] = 0; 878 break; 879 case VK_FORMAT_R16G16B16A16_UINT: 880 clear_vals[0] = value->uint32[0] & 0xffff; 881 clear_vals[0] |= (value->uint32[1] & 0xffff) << 16; 882 clear_vals[1] = value->uint32[2] & 0xffff; 883 clear_vals[1] |= (value->uint32[3] & 0xffff) << 16; 884 break; 885 case VK_FORMAT_R32_UINT: 886 clear_vals[0] = value->uint32[0]; 887 clear_vals[1] = 0; 888 break; 889 case VK_FORMAT_R32G32_UINT: 890 clear_vals[0] = value->uint32[0]; 891 clear_vals[1] = value->uint32[1]; 892 break; 893 case VK_FORMAT_R32_SINT: 894 clear_vals[0] = value->int32[0]; 895 clear_vals[1] = 0; 896 break; 897 case VK_FORMAT_R16_SFLOAT: 898 clear_vals[0] = util_float_to_half(value->float32[0]); 899 clear_vals[1] = 0; 900 break; 901 case VK_FORMAT_R16G16_SFLOAT: 902 clear_vals[0] = util_float_to_half(value->float32[0]); 903 clear_vals[0] |= (uint32_t)util_float_to_half(value->float32[1]) << 16; 904 clear_vals[1] = 0; 905 break; 906 case VK_FORMAT_R16G16B16A16_SFLOAT: 907 clear_vals[0] = util_float_to_half(value->float32[0]); 908 clear_vals[0] |= (uint32_t)util_float_to_half(value->float32[1]) << 16; 909 clear_vals[1] = util_float_to_half(value->float32[2]); 910 clear_vals[1] |= (uint32_t)util_float_to_half(value->float32[3]) << 16; 911 break; 912 case VK_FORMAT_R16_UNORM: 913 clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff; 914 clear_vals[1] = 0; 915 break; 916 case VK_FORMAT_R16G16_UNORM: 917 clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff; 918 clear_vals[0] |= ((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0xffff)) << 16; 919 clear_vals[1] = 0; 920 break; 921 case VK_FORMAT_R16G16B16A16_UNORM: 922 clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff; 923 clear_vals[0] |= ((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0xffff)) << 16; 924 clear_vals[1] = ((uint16_t)util_iround(CLAMP(value->float32[2], 0.0f, 1.0f) * 0xffff)) & 0xffff; 925 clear_vals[1] |= ((uint16_t)util_iround(CLAMP(value->float32[3], 0.0f, 1.0f) * 0xffff)) << 16; 926 break; 927 case VK_FORMAT_A2B10G10R10_UNORM_PACK32: 928 /* TODO */ 929 return false; 930 case VK_FORMAT_R32G32_SFLOAT: 931 clear_vals[0] = fui(value->float32[0]); 932 clear_vals[1] = fui(value->float32[1]); 933 break; 934 case VK_FORMAT_R32_SFLOAT: 935 clear_vals[1] = 0; 936 clear_vals[0] = fui(value->float32[0]); 937 break; 938 default: 939 fprintf(stderr, "failed to fast clear %d\n", format); 940 return false; 941 } 942 return true; 943 } 944 945 void radv_GetPhysicalDeviceFormatProperties( 946 VkPhysicalDevice physicalDevice, 947 VkFormat format, 948 VkFormatProperties* pFormatProperties) 949 { 950 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice); 951 952 radv_physical_device_get_format_properties(physical_device, 953 format, 954 pFormatProperties); 955 } 956 957 VkResult radv_GetPhysicalDeviceImageFormatProperties( 958 VkPhysicalDevice physicalDevice, 959 VkFormat format, 960 VkImageType type, 961 VkImageTiling tiling, 962 VkImageUsageFlags usage, 963 VkImageCreateFlags createFlags, 964 VkImageFormatProperties* pImageFormatProperties) 965 { 966 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice); 967 VkFormatProperties format_props; 968 VkFormatFeatureFlags format_feature_flags; 969 VkExtent3D maxExtent; 970 uint32_t maxMipLevels; 971 uint32_t maxArraySize; 972 VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT; 973 974 radv_physical_device_get_format_properties(physical_device, format, 975 &format_props); 976 if (tiling == VK_IMAGE_TILING_LINEAR) { 977 format_feature_flags = format_props.linearTilingFeatures; 978 } else if (tiling == VK_IMAGE_TILING_OPTIMAL) { 979 format_feature_flags = format_props.optimalTilingFeatures; 980 } else { 981 unreachable("bad VkImageTiling"); 982 } 983 984 if (format_feature_flags == 0) 985 goto unsupported; 986 987 switch (type) { 988 default: 989 unreachable("bad vkimage type\n"); 990 case VK_IMAGE_TYPE_1D: 991 maxExtent.width = 16384; 992 maxExtent.height = 1; 993 maxExtent.depth = 1; 994 maxMipLevels = 15; /* log2(maxWidth) + 1 */ 995 maxArraySize = 2048; 996 break; 997 case VK_IMAGE_TYPE_2D: 998 maxExtent.width = 16384; 999 maxExtent.height = 16384; 1000 maxExtent.depth = 1; 1001 maxMipLevels = 15; /* log2(maxWidth) + 1 */ 1002 maxArraySize = 2048; 1003 break; 1004 case VK_IMAGE_TYPE_3D: 1005 maxExtent.width = 2048; 1006 maxExtent.height = 2048; 1007 maxExtent.depth = 2048; 1008 maxMipLevels = 12; /* log2(maxWidth) + 1 */ 1009 maxArraySize = 1; 1010 break; 1011 } 1012 1013 if (tiling == VK_IMAGE_TILING_OPTIMAL && 1014 type == VK_IMAGE_TYPE_2D && 1015 (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | 1016 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) && 1017 !(createFlags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) && 1018 !(usage & VK_IMAGE_USAGE_STORAGE_BIT)) { 1019 sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT; 1020 } 1021 1022 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) { 1023 if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) { 1024 goto unsupported; 1025 } 1026 } 1027 1028 if (usage & VK_IMAGE_USAGE_STORAGE_BIT) { 1029 if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) { 1030 goto unsupported; 1031 } 1032 } 1033 1034 if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) { 1035 if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) { 1036 goto unsupported; 1037 } 1038 } 1039 1040 if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) { 1041 if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) { 1042 goto unsupported; 1043 } 1044 } 1045 1046 *pImageFormatProperties = (VkImageFormatProperties) { 1047 .maxExtent = maxExtent, 1048 .maxMipLevels = maxMipLevels, 1049 .maxArrayLayers = maxArraySize, 1050 .sampleCounts = sampleCounts, 1051 1052 /* FINISHME: Accurately calculate 1053 * VkImageFormatProperties::maxResourceSize. 1054 */ 1055 .maxResourceSize = UINT32_MAX, 1056 }; 1057 1058 return VK_SUCCESS; 1059 unsupported: 1060 *pImageFormatProperties = (VkImageFormatProperties) { 1061 .maxExtent = { 0, 0, 0 }, 1062 .maxMipLevels = 0, 1063 .maxArrayLayers = 0, 1064 .sampleCounts = 0, 1065 .maxResourceSize = 0, 1066 }; 1067 1068 return VK_ERROR_FORMAT_NOT_SUPPORTED; 1069 } 1070 1071 void radv_GetPhysicalDeviceSparseImageFormatProperties( 1072 VkPhysicalDevice physicalDevice, 1073 VkFormat format, 1074 VkImageType type, 1075 uint32_t samples, 1076 VkImageUsageFlags usage, 1077 VkImageTiling tiling, 1078 uint32_t* pNumProperties, 1079 VkSparseImageFormatProperties* pProperties) 1080 { 1081 /* Sparse images are not yet supported. */ 1082 *pNumProperties = 0; 1083 } 1084