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