freedreno: Make the slice pitch be bytes, not pixels.
[mesa.git] / src / freedreno / vulkan / tu_image.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 */
27
28 #include "tu_private.h"
29
30 #include "util/debug.h"
31 #include "util/u_atomic.h"
32 #include "util/format/u_format.h"
33 #include "vk_format.h"
34 #include "vk_util.h"
35 #include "drm-uapi/drm_fourcc.h"
36
37 #include "tu_cs.h"
38
39 VkResult
40 tu_image_create(VkDevice _device,
41 const VkImageCreateInfo *pCreateInfo,
42 const VkAllocationCallbacks *alloc,
43 VkImage *pImage,
44 uint64_t modifier)
45 {
46 TU_FROM_HANDLE(tu_device, device, _device);
47 struct tu_image *image = NULL;
48 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
49
50 tu_assert(pCreateInfo->mipLevels > 0);
51 tu_assert(pCreateInfo->arrayLayers > 0);
52 tu_assert(pCreateInfo->samples > 0);
53 tu_assert(pCreateInfo->extent.width > 0);
54 tu_assert(pCreateInfo->extent.height > 0);
55 tu_assert(pCreateInfo->extent.depth > 0);
56
57 image = vk_zalloc2(&device->alloc, alloc, sizeof(*image), 8,
58 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
59 if (!image)
60 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
61
62 image->type = pCreateInfo->imageType;
63
64 image->vk_format = pCreateInfo->format;
65 image->tiling = pCreateInfo->tiling;
66 image->usage = pCreateInfo->usage;
67 image->flags = pCreateInfo->flags;
68 image->extent = pCreateInfo->extent;
69 image->level_count = pCreateInfo->mipLevels;
70 image->layer_count = pCreateInfo->arrayLayers;
71 image->samples = pCreateInfo->samples;
72
73 image->exclusive = pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE;
74 if (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT) {
75 for (uint32_t i = 0; i < pCreateInfo->queueFamilyIndexCount; ++i)
76 if (pCreateInfo->pQueueFamilyIndices[i] ==
77 VK_QUEUE_FAMILY_EXTERNAL)
78 image->queue_family_mask |= (1u << TU_MAX_QUEUE_FAMILIES) - 1u;
79 else
80 image->queue_family_mask |=
81 1u << pCreateInfo->pQueueFamilyIndices[i];
82 }
83
84 image->shareable =
85 vk_find_struct_const(pCreateInfo->pNext,
86 EXTERNAL_MEMORY_IMAGE_CREATE_INFO) != NULL;
87
88 image->layout.tile_mode = TILE6_3;
89 bool ubwc_enabled = true;
90
91 /* disable tiling when linear is requested and for compressed formats */
92 if (pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR ||
93 modifier == DRM_FORMAT_MOD_LINEAR ||
94 vk_format_is_compressed(image->vk_format)) {
95 image->layout.tile_mode = TILE6_LINEAR;
96 ubwc_enabled = false;
97 }
98
99 /* UBWC can't be used with E5B9G9R9 */
100 if (image->vk_format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32)
101 ubwc_enabled = false;
102
103 /* separate stencil doesn't have a UBWC enable bit */
104 if (image->vk_format == VK_FORMAT_S8_UINT)
105 ubwc_enabled = false;
106
107 if (image->extent.depth > 1) {
108 tu_finishme("UBWC with 3D textures");
109 ubwc_enabled = false;
110 }
111
112 /* Disable UBWC for storage images.
113 *
114 * The closed GL driver skips UBWC for storage images (and additionally
115 * uses linear for writeonly images). We seem to have image tiling working
116 * in freedreno in general, so turnip matches that. freedreno also enables
117 * UBWC on images, but it's not really tested due to the lack of
118 * UBWC-enabled mipmaps in freedreno currently. Just match the closed GL
119 * behavior of no UBWC.
120 */
121 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT)
122 ubwc_enabled = false;
123
124 uint32_t ubwc_blockwidth, ubwc_blockheight;
125 fdl6_get_ubwc_blockwidth(&image->layout,
126 &ubwc_blockwidth, &ubwc_blockheight);
127 if (!ubwc_blockwidth) {
128 tu_finishme("UBWC for cpp=%d", image->layout.cpp);
129 ubwc_enabled = false;
130 }
131
132 /* expect UBWC enabled if we asked for it */
133 assert(modifier != DRM_FORMAT_MOD_QCOM_COMPRESSED || ubwc_enabled);
134
135 image->layout.ubwc = ubwc_enabled;
136
137 fdl6_layout(&image->layout, vk_format_to_pipe_format(image->vk_format),
138 image->samples,
139 pCreateInfo->extent.width,
140 pCreateInfo->extent.height,
141 pCreateInfo->extent.depth,
142 pCreateInfo->mipLevels,
143 pCreateInfo->arrayLayers,
144 pCreateInfo->imageType == VK_IMAGE_TYPE_3D);
145
146 *pImage = tu_image_to_handle(image);
147
148 return VK_SUCCESS;
149 }
150
151 enum a6xx_tex_fetchsize
152 tu6_fetchsize(VkFormat format)
153 {
154 if (vk_format_description(format)->layout == UTIL_FORMAT_LAYOUT_ASTC)
155 return TFETCH6_16_BYTE;
156
157 switch (vk_format_get_blocksize(format) / vk_format_get_blockwidth(format)) {
158 case 1: return TFETCH6_1_BYTE;
159 case 2: return TFETCH6_2_BYTE;
160 case 4: return TFETCH6_4_BYTE;
161 case 8: return TFETCH6_8_BYTE;
162 case 16: return TFETCH6_16_BYTE;
163 default:
164 unreachable("bad block size");
165 }
166 }
167
168 static uint32_t
169 tu6_texswiz(const VkComponentMapping *comps,
170 VkFormat format,
171 VkImageAspectFlagBits aspect_mask)
172 {
173 unsigned char swiz[4] = {comps->r, comps->g, comps->b, comps->a};
174 unsigned char vk_swizzle[] = {
175 [VK_COMPONENT_SWIZZLE_ZERO] = A6XX_TEX_ZERO,
176 [VK_COMPONENT_SWIZZLE_ONE] = A6XX_TEX_ONE,
177 [VK_COMPONENT_SWIZZLE_R] = A6XX_TEX_X,
178 [VK_COMPONENT_SWIZZLE_G] = A6XX_TEX_Y,
179 [VK_COMPONENT_SWIZZLE_B] = A6XX_TEX_Z,
180 [VK_COMPONENT_SWIZZLE_A] = A6XX_TEX_W,
181 };
182 const unsigned char *fmt_swiz = vk_format_description(format)->swizzle;
183
184 for (unsigned i = 0; i < 4; i++) {
185 swiz[i] = (swiz[i] == VK_COMPONENT_SWIZZLE_IDENTITY) ? i : vk_swizzle[swiz[i]];
186 /* if format has 0/1 in channel, use that (needed for bc1_rgb) */
187 if (swiz[i] < 4) {
188 if (aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT &&
189 format == VK_FORMAT_D24_UNORM_S8_UINT)
190 swiz[i] = A6XX_TEX_Y;
191 switch (fmt_swiz[swiz[i]]) {
192 case PIPE_SWIZZLE_0: swiz[i] = A6XX_TEX_ZERO; break;
193 case PIPE_SWIZZLE_1: swiz[i] = A6XX_TEX_ONE; break;
194 }
195 }
196 }
197
198 return A6XX_TEX_CONST_0_SWIZ_X(swiz[0]) |
199 A6XX_TEX_CONST_0_SWIZ_Y(swiz[1]) |
200 A6XX_TEX_CONST_0_SWIZ_Z(swiz[2]) |
201 A6XX_TEX_CONST_0_SWIZ_W(swiz[3]);
202 }
203
204 static enum a6xx_tex_type
205 tu6_tex_type(VkImageViewType type)
206 {
207 switch (type) {
208 default:
209 case VK_IMAGE_VIEW_TYPE_1D:
210 case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
211 return A6XX_TEX_1D;
212 case VK_IMAGE_VIEW_TYPE_2D:
213 case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
214 return A6XX_TEX_2D;
215 case VK_IMAGE_VIEW_TYPE_3D:
216 return A6XX_TEX_3D;
217 case VK_IMAGE_VIEW_TYPE_CUBE:
218 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
219 return A6XX_TEX_CUBE;
220 }
221 }
222
223 void
224 tu_cs_image_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer)
225 {
226 tu_cs_emit(cs, iview->PITCH);
227 tu_cs_emit(cs, iview->layer_size >> 6);
228 tu_cs_emit_qw(cs, iview->base_addr + iview->layer_size * layer);
229 }
230
231 void
232 tu_cs_image_ref_2d(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer, bool src)
233 {
234 tu_cs_emit_qw(cs, iview->base_addr + iview->layer_size * layer);
235 /* SP_PS_2D_SRC_PITCH has shifted pitch field */
236 tu_cs_emit(cs, iview->PITCH << (src ? 9 : 0));
237 }
238
239 void
240 tu_cs_image_flag_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer)
241 {
242 tu_cs_emit_qw(cs, iview->ubwc_addr + iview->ubwc_layer_size * layer);
243 tu_cs_emit(cs, iview->FLAG_BUFFER_PITCH);
244 }
245
246 void
247 tu_image_view_init(struct tu_image_view *iview,
248 const VkImageViewCreateInfo *pCreateInfo)
249 {
250 TU_FROM_HANDLE(tu_image, image, pCreateInfo->image);
251 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
252 VkFormat format = pCreateInfo->format;
253 VkImageAspectFlagBits aspect_mask = pCreateInfo->subresourceRange.aspectMask;
254
255 switch (image->type) {
256 case VK_IMAGE_TYPE_1D:
257 case VK_IMAGE_TYPE_2D:
258 assert(range->baseArrayLayer + tu_get_layerCount(image, range) <=
259 image->layer_count);
260 break;
261 case VK_IMAGE_TYPE_3D:
262 assert(range->baseArrayLayer + tu_get_layerCount(image, range) <=
263 tu_minify(image->extent.depth, range->baseMipLevel));
264 break;
265 default:
266 unreachable("bad VkImageType");
267 }
268
269 iview->image = image;
270
271 memset(iview->descriptor, 0, sizeof(iview->descriptor));
272
273 struct fdl_layout *layout = &image->layout;
274
275 uint32_t width = u_minify(image->extent.width, range->baseMipLevel);
276 uint32_t height = u_minify(image->extent.height, range->baseMipLevel);
277 uint32_t depth = pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_3D ?
278 u_minify(image->extent.depth, range->baseMipLevel) : tu_get_layerCount(image, range);
279
280 uint64_t base_addr = image->bo->iova + image->bo_offset +
281 fdl_surface_offset(layout, range->baseMipLevel, range->baseArrayLayer);
282 uint64_t ubwc_addr = image->bo->iova + image->bo_offset +
283 fdl_ubwc_offset(layout, range->baseMipLevel, range->baseArrayLayer);
284
285 uint32_t pitch = layout->slices[range->baseMipLevel].pitch;
286 uint32_t ubwc_pitch = layout->ubwc_slices[range->baseMipLevel].pitch;
287 uint32_t layer_size = fdl_layer_stride(layout, range->baseMipLevel);
288
289 struct tu_native_format fmt = tu6_format_texture(format, layout->tile_mode);
290 /* note: freedreno layout assumes no TILE_ALL bit for non-UBWC
291 * this means smaller mipmap levels have a linear tile mode
292 */
293 fmt.tile_mode = fdl_tile_mode(layout, range->baseMipLevel);
294
295 bool ubwc_enabled = fdl_ubwc_enabled(layout, range->baseMipLevel);
296
297 unsigned fmt_tex = fmt.fmt;
298 if (fmt_tex == FMT6_Z24_UNORM_S8_UINT_AS_R8G8B8A8) {
299 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT)
300 fmt_tex = FMT6_Z24_UNORM_S8_UINT;
301 if (aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT)
302 fmt_tex = FMT6_S8Z24_UINT;
303 /* TODO: also use this format with storage descriptor ? */
304 }
305
306 iview->descriptor[0] =
307 A6XX_TEX_CONST_0_TILE_MODE(fmt.tile_mode) |
308 COND(vk_format_is_srgb(format), A6XX_TEX_CONST_0_SRGB) |
309 A6XX_TEX_CONST_0_FMT(fmt_tex) |
310 A6XX_TEX_CONST_0_SAMPLES(tu_msaa_samples(image->samples)) |
311 A6XX_TEX_CONST_0_SWAP(fmt.swap) |
312 tu6_texswiz(&pCreateInfo->components, format, aspect_mask) |
313 A6XX_TEX_CONST_0_MIPLVLS(tu_get_levelCount(image, range) - 1);
314 iview->descriptor[1] = A6XX_TEX_CONST_1_WIDTH(width) | A6XX_TEX_CONST_1_HEIGHT(height);
315 iview->descriptor[2] =
316 A6XX_TEX_CONST_2_FETCHSIZE(tu6_fetchsize(format)) |
317 A6XX_TEX_CONST_2_PITCH(pitch) |
318 A6XX_TEX_CONST_2_TYPE(tu6_tex_type(pCreateInfo->viewType));
319 iview->descriptor[3] = A6XX_TEX_CONST_3_ARRAY_PITCH(layer_size);
320 iview->descriptor[4] = base_addr;
321 iview->descriptor[5] = (base_addr >> 32) | A6XX_TEX_CONST_5_DEPTH(depth);
322
323 if (ubwc_enabled) {
324 uint32_t block_width, block_height;
325 fdl6_get_ubwc_blockwidth(&image->layout,
326 &block_width, &block_height);
327
328 iview->descriptor[3] |= A6XX_TEX_CONST_3_FLAG | A6XX_TEX_CONST_3_TILE_ALL;
329 iview->descriptor[7] = ubwc_addr;
330 iview->descriptor[8] = ubwc_addr >> 32;
331 iview->descriptor[9] |= A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(layout->ubwc_layer_size >> 2);
332 iview->descriptor[10] |=
333 A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(ubwc_pitch) |
334 A6XX_TEX_CONST_10_FLAG_BUFFER_LOGW(util_logbase2_ceil(DIV_ROUND_UP(width, block_width))) |
335 A6XX_TEX_CONST_10_FLAG_BUFFER_LOGH(util_logbase2_ceil(DIV_ROUND_UP(height, block_height)));
336 }
337
338 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_3D) {
339 iview->descriptor[3] |=
340 A6XX_TEX_CONST_3_MIN_LAYERSZ(image->layout.slices[image->level_count - 1].size0);
341 }
342
343 /* only texture descriptor is valid for TEXTURE-only formats */
344 if (!(fmt.supported & FMT_COLOR))
345 return;
346
347 struct tu_native_format cfmt = tu6_format_color(format, layout->tile_mode);
348 cfmt.tile_mode = fmt.tile_mode;
349
350 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
351 memset(iview->storage_descriptor, 0, sizeof(iview->storage_descriptor));
352
353 iview->storage_descriptor[0] =
354 A6XX_IBO_0_FMT(fmt.fmt) |
355 A6XX_IBO_0_TILE_MODE(fmt.tile_mode);
356 iview->storage_descriptor[1] =
357 A6XX_IBO_1_WIDTH(width) |
358 A6XX_IBO_1_HEIGHT(height);
359 iview->storage_descriptor[2] =
360 A6XX_IBO_2_PITCH(pitch) |
361 A6XX_IBO_2_TYPE(tu6_tex_type(pCreateInfo->viewType));
362 iview->storage_descriptor[3] = A6XX_IBO_3_ARRAY_PITCH(layer_size);
363
364 iview->storage_descriptor[4] = base_addr;
365 iview->storage_descriptor[5] = (base_addr >> 32) | A6XX_IBO_5_DEPTH(depth);
366
367 if (ubwc_enabled) {
368 iview->storage_descriptor[3] |= A6XX_IBO_3_FLAG | A6XX_IBO_3_UNK27;
369 iview->storage_descriptor[7] |= ubwc_addr;
370 iview->storage_descriptor[8] |= ubwc_addr >> 32;
371 iview->storage_descriptor[9] = A6XX_IBO_9_FLAG_BUFFER_ARRAY_PITCH(layout->ubwc_layer_size >> 2);
372 iview->storage_descriptor[10] =
373 A6XX_IBO_10_FLAG_BUFFER_PITCH(ubwc_pitch);
374 }
375 }
376
377 iview->base_addr = base_addr;
378 iview->ubwc_addr = ubwc_addr;
379 iview->layer_size = layer_size;
380 iview->ubwc_layer_size = layout->ubwc_layer_size;
381
382 iview->extent.width = width;
383 iview->extent.height = height;
384 iview->need_y2_align =
385 (fmt.tile_mode == TILE6_LINEAR && range->baseMipLevel != image->level_count - 1);
386
387 iview->ubwc_enabled = ubwc_enabled;
388
389 /* note: these have same encoding for MRT and 2D (except 2D PITCH src) */
390 iview->PITCH = A6XX_RB_DEPTH_BUFFER_PITCH(pitch).value;
391 iview->FLAG_BUFFER_PITCH = A6XX_RB_DEPTH_FLAG_BUFFER_PITCH(
392 .pitch = ubwc_pitch, .array_pitch = layout->ubwc_layer_size >> 2).value;
393
394 iview->RB_MRT_BUF_INFO = A6XX_RB_MRT_BUF_INFO(0,
395 .color_tile_mode = cfmt.tile_mode,
396 .color_format = cfmt.fmt,
397 .color_swap = cfmt.swap).value;
398 iview->SP_FS_MRT_REG = A6XX_SP_FS_MRT_REG(0,
399 .color_format = cfmt.fmt,
400 .color_sint = vk_format_is_sint(format),
401 .color_uint = vk_format_is_uint(format)).value;
402
403 iview->SP_PS_2D_SRC_INFO = A6XX_SP_PS_2D_SRC_INFO(
404 .color_format = fmt.fmt,
405 .tile_mode = fmt.tile_mode,
406 .color_swap = fmt.swap,
407 .flags = ubwc_enabled,
408 .srgb = vk_format_is_srgb(format),
409 .samples = tu_msaa_samples(image->samples),
410 .samples_average = image->samples > 1 &&
411 !vk_format_is_int(format) &&
412 !vk_format_is_depth_or_stencil(format),
413 .unk20 = 1,
414 .unk22 = 1).value;
415 iview->SP_PS_2D_SRC_SIZE =
416 A6XX_SP_PS_2D_SRC_SIZE(.width = width, .height = height).value;
417
418 iview->RB_2D_DST_INFO = A6XX_RB_2D_DST_INFO(
419 .color_format = cfmt.fmt,
420 .tile_mode = cfmt.tile_mode,
421 .color_swap = cfmt.swap,
422 .flags = ubwc_enabled,
423 .srgb = vk_format_is_srgb(format)).value;
424
425 iview->RB_BLIT_DST_INFO = A6XX_RB_BLIT_DST_INFO(
426 .tile_mode = cfmt.tile_mode,
427 .samples = tu_msaa_samples(iview->image->samples),
428 .color_format = cfmt.fmt,
429 .color_swap = cfmt.swap,
430 .flags = ubwc_enabled).value;
431 }
432
433 unsigned
434 tu_image_queue_family_mask(const struct tu_image *image,
435 uint32_t family,
436 uint32_t queue_family)
437 {
438 if (!image->exclusive)
439 return image->queue_family_mask;
440 if (family == VK_QUEUE_FAMILY_EXTERNAL)
441 return (1u << TU_MAX_QUEUE_FAMILIES) - 1u;
442 if (family == VK_QUEUE_FAMILY_IGNORED)
443 return 1u << queue_family;
444 return 1u << family;
445 }
446
447 VkResult
448 tu_CreateImage(VkDevice device,
449 const VkImageCreateInfo *pCreateInfo,
450 const VkAllocationCallbacks *pAllocator,
451 VkImage *pImage)
452 {
453 #ifdef ANDROID
454 const VkNativeBufferANDROID *gralloc_info =
455 vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
456
457 if (gralloc_info)
458 return tu_image_from_gralloc(device, pCreateInfo, gralloc_info,
459 pAllocator, pImage);
460 #endif
461
462 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
463 if (pCreateInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
464 const VkImageDrmFormatModifierListCreateInfoEXT *mod_info =
465 vk_find_struct_const(pCreateInfo->pNext,
466 IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
467
468 modifier = DRM_FORMAT_MOD_LINEAR;
469 for (unsigned i = 0; i < mod_info->drmFormatModifierCount; i++) {
470 if (mod_info->pDrmFormatModifiers[i] == DRM_FORMAT_MOD_QCOM_COMPRESSED)
471 modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
472 }
473 } else {
474 const struct wsi_image_create_info *wsi_info =
475 vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
476 if (wsi_info && wsi_info->scanout)
477 modifier = DRM_FORMAT_MOD_LINEAR;
478 }
479
480 return tu_image_create(device, pCreateInfo, pAllocator, pImage, modifier);
481 }
482
483 void
484 tu_DestroyImage(VkDevice _device,
485 VkImage _image,
486 const VkAllocationCallbacks *pAllocator)
487 {
488 TU_FROM_HANDLE(tu_device, device, _device);
489 TU_FROM_HANDLE(tu_image, image, _image);
490
491 if (!image)
492 return;
493
494 if (image->owned_memory != VK_NULL_HANDLE)
495 tu_FreeMemory(_device, image->owned_memory, pAllocator);
496
497 vk_free2(&device->alloc, pAllocator, image);
498 }
499
500 void
501 tu_GetImageSubresourceLayout(VkDevice _device,
502 VkImage _image,
503 const VkImageSubresource *pSubresource,
504 VkSubresourceLayout *pLayout)
505 {
506 TU_FROM_HANDLE(tu_image, image, _image);
507
508 const struct fdl_slice *slice = image->layout.slices + pSubresource->mipLevel;
509
510 pLayout->offset = fdl_surface_offset(&image->layout,
511 pSubresource->mipLevel,
512 pSubresource->arrayLayer);
513 pLayout->size = slice->size0;
514 pLayout->rowPitch =
515 slice->pitch * vk_format_get_blockheight(image->vk_format);
516 pLayout->arrayPitch = image->layout.layer_size;
517 pLayout->depthPitch = slice->size0;
518
519 if (image->layout.ubwc_layer_size) {
520 /* UBWC starts at offset 0 */
521 pLayout->offset = 0;
522 /* UBWC scanout won't match what the kernel wants if we have levels/layers */
523 assert(image->level_count == 1 && image->layer_count == 1);
524 }
525 }
526
527 VkResult tu_GetImageDrmFormatModifierPropertiesEXT(
528 VkDevice device,
529 VkImage _image,
530 VkImageDrmFormatModifierPropertiesEXT* pProperties)
531 {
532 TU_FROM_HANDLE(tu_image, image, _image);
533
534 assert(pProperties->sType ==
535 VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT);
536
537 /* TODO invent a modifier for tiled but not UBWC buffers */
538
539 if (!image->layout.tile_mode)
540 pProperties->drmFormatModifier = DRM_FORMAT_MOD_LINEAR;
541 else if (image->layout.ubwc_layer_size)
542 pProperties->drmFormatModifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
543 else
544 pProperties->drmFormatModifier = DRM_FORMAT_MOD_INVALID;
545
546 return VK_SUCCESS;
547 }
548
549
550 VkResult
551 tu_CreateImageView(VkDevice _device,
552 const VkImageViewCreateInfo *pCreateInfo,
553 const VkAllocationCallbacks *pAllocator,
554 VkImageView *pView)
555 {
556 TU_FROM_HANDLE(tu_device, device, _device);
557 struct tu_image_view *view;
558
559 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
560 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
561 if (view == NULL)
562 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
563
564 tu_image_view_init(view, pCreateInfo);
565
566 *pView = tu_image_view_to_handle(view);
567
568 return VK_SUCCESS;
569 }
570
571 void
572 tu_DestroyImageView(VkDevice _device,
573 VkImageView _iview,
574 const VkAllocationCallbacks *pAllocator)
575 {
576 TU_FROM_HANDLE(tu_device, device, _device);
577 TU_FROM_HANDLE(tu_image_view, iview, _iview);
578
579 if (!iview)
580 return;
581 vk_free2(&device->alloc, pAllocator, iview);
582 }
583
584 void
585 tu_buffer_view_init(struct tu_buffer_view *view,
586 struct tu_device *device,
587 const VkBufferViewCreateInfo *pCreateInfo)
588 {
589 TU_FROM_HANDLE(tu_buffer, buffer, pCreateInfo->buffer);
590
591 view->buffer = buffer;
592
593 enum VkFormat vfmt = pCreateInfo->format;
594 enum pipe_format pfmt = vk_format_to_pipe_format(vfmt);
595 const struct tu_native_format fmt = tu6_format_texture(vfmt, TILE6_LINEAR);
596
597 uint32_t range;
598 if (pCreateInfo->range == VK_WHOLE_SIZE)
599 range = buffer->size - pCreateInfo->offset;
600 else
601 range = pCreateInfo->range;
602 uint32_t elements = range / util_format_get_blocksize(pfmt);
603
604 static const VkComponentMapping components = {
605 .r = VK_COMPONENT_SWIZZLE_R,
606 .g = VK_COMPONENT_SWIZZLE_G,
607 .b = VK_COMPONENT_SWIZZLE_B,
608 .a = VK_COMPONENT_SWIZZLE_A,
609 };
610
611 uint64_t iova = tu_buffer_iova(buffer) + pCreateInfo->offset;
612
613 memset(&view->descriptor, 0, sizeof(view->descriptor));
614
615 view->descriptor[0] =
616 A6XX_TEX_CONST_0_TILE_MODE(TILE6_LINEAR) |
617 A6XX_TEX_CONST_0_SWAP(fmt.swap) |
618 A6XX_TEX_CONST_0_FMT(fmt.fmt) |
619 A6XX_TEX_CONST_0_MIPLVLS(0) |
620 tu6_texswiz(&components, vfmt, VK_IMAGE_ASPECT_COLOR_BIT);
621 COND(vk_format_is_srgb(vfmt), A6XX_TEX_CONST_0_SRGB);
622 view->descriptor[1] =
623 A6XX_TEX_CONST_1_WIDTH(elements & MASK(15)) |
624 A6XX_TEX_CONST_1_HEIGHT(elements >> 15);
625 view->descriptor[2] =
626 A6XX_TEX_CONST_2_UNK4 |
627 A6XX_TEX_CONST_2_UNK31;
628 view->descriptor[4] = iova;
629 view->descriptor[5] = iova >> 32;
630 }
631
632 VkResult
633 tu_CreateBufferView(VkDevice _device,
634 const VkBufferViewCreateInfo *pCreateInfo,
635 const VkAllocationCallbacks *pAllocator,
636 VkBufferView *pView)
637 {
638 TU_FROM_HANDLE(tu_device, device, _device);
639 struct tu_buffer_view *view;
640
641 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
642 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
643 if (!view)
644 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
645
646 tu_buffer_view_init(view, device, pCreateInfo);
647
648 *pView = tu_buffer_view_to_handle(view);
649
650 return VK_SUCCESS;
651 }
652
653 void
654 tu_DestroyBufferView(VkDevice _device,
655 VkBufferView bufferView,
656 const VkAllocationCallbacks *pAllocator)
657 {
658 TU_FROM_HANDLE(tu_device, device, _device);
659 TU_FROM_HANDLE(tu_buffer_view, view, bufferView);
660
661 if (!view)
662 return;
663
664 vk_free2(&device->alloc, pAllocator, view);
665 }