tu: Move UBWC layout into fdl6_layout() and use that function.
[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 "vk_format.h"
33 #include "vk_util.h"
34 #include "drm-uapi/drm_fourcc.h"
35
36 static inline bool
37 image_level_linear(struct tu_image *image, int level, bool ubwc)
38 {
39 unsigned w = u_minify(image->extent.width, level);
40 /* all levels are tiled/compressed with UBWC */
41 return ubwc ? false : (w < 16);
42 }
43
44 enum a6xx_tile_mode
45 tu6_get_image_tile_mode(struct tu_image *image, int level)
46 {
47 if (image_level_linear(image, level, !!image->layout.ubwc_size))
48 return TILE6_LINEAR;
49 else
50 return image->layout.tile_mode;
51 }
52
53 VkResult
54 tu_image_create(VkDevice _device,
55 const VkImageCreateInfo *pCreateInfo,
56 const VkAllocationCallbacks *alloc,
57 VkImage *pImage,
58 uint64_t modifier)
59 {
60 TU_FROM_HANDLE(tu_device, device, _device);
61 struct tu_image *image = NULL;
62 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
63
64 tu_assert(pCreateInfo->mipLevels > 0);
65 tu_assert(pCreateInfo->arrayLayers > 0);
66 tu_assert(pCreateInfo->samples > 0);
67 tu_assert(pCreateInfo->extent.width > 0);
68 tu_assert(pCreateInfo->extent.height > 0);
69 tu_assert(pCreateInfo->extent.depth > 0);
70
71 image = vk_zalloc2(&device->alloc, alloc, sizeof(*image), 8,
72 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
73 if (!image)
74 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
75
76 image->type = pCreateInfo->imageType;
77
78 image->vk_format = pCreateInfo->format;
79 image->tiling = pCreateInfo->tiling;
80 image->usage = pCreateInfo->usage;
81 image->flags = pCreateInfo->flags;
82 image->extent = pCreateInfo->extent;
83 image->level_count = pCreateInfo->mipLevels;
84 image->layer_count = pCreateInfo->arrayLayers;
85 image->samples = pCreateInfo->samples;
86
87 image->exclusive = pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE;
88 if (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT) {
89 for (uint32_t i = 0; i < pCreateInfo->queueFamilyIndexCount; ++i)
90 if (pCreateInfo->pQueueFamilyIndices[i] ==
91 VK_QUEUE_FAMILY_EXTERNAL)
92 image->queue_family_mask |= (1u << TU_MAX_QUEUE_FAMILIES) - 1u;
93 else
94 image->queue_family_mask |=
95 1u << pCreateInfo->pQueueFamilyIndices[i];
96 }
97
98 image->shareable =
99 vk_find_struct_const(pCreateInfo->pNext,
100 EXTERNAL_MEMORY_IMAGE_CREATE_INFO) != NULL;
101
102 image->layout.tile_mode = TILE6_3;
103 bool ubwc_enabled = true;
104
105 /* disable tiling when linear is requested and for compressed formats */
106 if (pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR ||
107 modifier == DRM_FORMAT_MOD_LINEAR ||
108 vk_format_is_compressed(image->vk_format)) {
109 image->layout.tile_mode = TILE6_LINEAR;
110 ubwc_enabled = false;
111 }
112
113 /* using UBWC with D24S8 breaks the "stencil read" copy path (why?)
114 * (causes any deqp tests that need to check stencil to fail)
115 * disable UBWC for this format until we properly support copy aspect masks
116 */
117 if (image->vk_format == VK_FORMAT_D24_UNORM_S8_UINT)
118 ubwc_enabled = false;
119
120 /* UBWC can't be used with E5B9G9R9 */
121 if (image->vk_format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32)
122 ubwc_enabled = false;
123
124 if (image->extent.depth > 1) {
125 tu_finishme("UBWC with 3D textures");
126 ubwc_enabled = false;
127 }
128
129 uint32_t ubwc_blockwidth, ubwc_blockheight;
130 fdl6_get_ubwc_blockwidth(&image->layout,
131 &ubwc_blockwidth, &ubwc_blockheight);
132 if (!ubwc_blockwidth) {
133 tu_finishme("UBWC for cpp=%d", image->layout.cpp);
134 ubwc_enabled = false;
135 }
136
137 /* expect UBWC enabled if we asked for it */
138 assert(modifier != DRM_FORMAT_MOD_QCOM_COMPRESSED || ubwc_enabled);
139
140 fdl6_layout(&image->layout, vk_format_to_pipe_format(image->vk_format),
141 image->samples,
142 pCreateInfo->extent.width,
143 pCreateInfo->extent.height,
144 pCreateInfo->extent.depth,
145 pCreateInfo->mipLevels,
146 pCreateInfo->arrayLayers,
147 pCreateInfo->imageType == VK_IMAGE_TYPE_3D,
148 ubwc_enabled);
149
150 *pImage = tu_image_to_handle(image);
151
152 return VK_SUCCESS;
153 }
154
155 static enum a6xx_tex_fetchsize
156 tu6_fetchsize(VkFormat format)
157 {
158 if (vk_format_description(format)->layout == UTIL_FORMAT_LAYOUT_ASTC)
159 return TFETCH6_16_BYTE;
160
161 switch (vk_format_get_blocksize(format) / vk_format_get_blockwidth(format)) {
162 case 1: return TFETCH6_1_BYTE;
163 case 2: return TFETCH6_2_BYTE;
164 case 4: return TFETCH6_4_BYTE;
165 case 8: return TFETCH6_8_BYTE;
166 case 16: return TFETCH6_16_BYTE;
167 default:
168 unreachable("bad block size");
169 }
170 }
171
172 static uint32_t
173 tu6_texswiz(const VkComponentMapping *comps, const unsigned char *fmt_swiz)
174 {
175 unsigned char swiz[4] = {comps->r, comps->g, comps->b, comps->a};
176 unsigned char vk_swizzle[] = {
177 [VK_COMPONENT_SWIZZLE_ZERO] = A6XX_TEX_ZERO,
178 [VK_COMPONENT_SWIZZLE_ONE] = A6XX_TEX_ONE,
179 [VK_COMPONENT_SWIZZLE_R] = A6XX_TEX_X,
180 [VK_COMPONENT_SWIZZLE_G] = A6XX_TEX_Y,
181 [VK_COMPONENT_SWIZZLE_B] = A6XX_TEX_Z,
182 [VK_COMPONENT_SWIZZLE_A] = A6XX_TEX_W,
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 switch (fmt_swiz[swiz[i]]) {
189 case PIPE_SWIZZLE_0: swiz[i] = A6XX_TEX_ZERO; break;
190 case PIPE_SWIZZLE_1: swiz[i] = A6XX_TEX_ONE; break;
191 }
192 }
193 }
194
195 return A6XX_TEX_CONST_0_SWIZ_X(swiz[0]) |
196 A6XX_TEX_CONST_0_SWIZ_Y(swiz[1]) |
197 A6XX_TEX_CONST_0_SWIZ_Z(swiz[2]) |
198 A6XX_TEX_CONST_0_SWIZ_W(swiz[3]);
199 }
200
201 static enum a6xx_tex_type
202 tu6_tex_type(VkImageViewType type)
203 {
204 switch (type) {
205 default:
206 case VK_IMAGE_VIEW_TYPE_1D:
207 case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
208 return A6XX_TEX_1D;
209 case VK_IMAGE_VIEW_TYPE_2D:
210 case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
211 return A6XX_TEX_2D;
212 case VK_IMAGE_VIEW_TYPE_3D:
213 return A6XX_TEX_3D;
214 case VK_IMAGE_VIEW_TYPE_CUBE:
215 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
216 return A6XX_TEX_CUBE;
217 }
218 }
219
220 void
221 tu_image_view_init(struct tu_image_view *iview,
222 struct tu_device *device,
223 const VkImageViewCreateInfo *pCreateInfo)
224 {
225 TU_FROM_HANDLE(tu_image, image, pCreateInfo->image);
226 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
227
228 switch (image->type) {
229 case VK_IMAGE_TYPE_1D:
230 case VK_IMAGE_TYPE_2D:
231 assert(range->baseArrayLayer + tu_get_layerCount(image, range) <=
232 image->layer_count);
233 break;
234 case VK_IMAGE_TYPE_3D:
235 assert(range->baseArrayLayer + tu_get_layerCount(image, range) <=
236 tu_minify(image->extent.depth, range->baseMipLevel));
237 break;
238 default:
239 unreachable("bad VkImageType");
240 }
241
242 iview->image = image;
243 iview->type = pCreateInfo->viewType;
244 iview->vk_format = pCreateInfo->format;
245 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
246
247 if (iview->aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT) {
248 iview->vk_format = vk_format_stencil_only(iview->vk_format);
249 } else if (iview->aspect_mask == VK_IMAGE_ASPECT_DEPTH_BIT) {
250 iview->vk_format = vk_format_depth_only(iview->vk_format);
251 }
252
253 // should we minify?
254 iview->extent = image->extent;
255
256 iview->base_layer = range->baseArrayLayer;
257 iview->layer_count = tu_get_layerCount(image, range);
258 iview->base_mip = range->baseMipLevel;
259 iview->level_count = tu_get_levelCount(image, range);
260
261 memset(iview->descriptor, 0, sizeof(iview->descriptor));
262
263 const struct tu_native_format *fmt = tu6_get_native_format(iview->vk_format);
264 uint64_t base_addr = tu_image_base(image, iview->base_mip, iview->base_layer);
265 uint64_t ubwc_addr = tu_image_ubwc_base(image, iview->base_mip, iview->base_layer);
266
267 uint32_t pitch = tu_image_stride(image, iview->base_mip) / vk_format_get_blockwidth(iview->vk_format);
268 enum a6xx_tile_mode tile_mode = tu6_get_image_tile_mode(image, iview->base_mip);
269 uint32_t width = u_minify(image->extent.width, iview->base_mip);
270 uint32_t height = u_minify(image->extent.height, iview->base_mip);
271
272 iview->descriptor[0] =
273 A6XX_TEX_CONST_0_TILE_MODE(tile_mode) |
274 COND(vk_format_is_srgb(iview->vk_format), A6XX_TEX_CONST_0_SRGB) |
275 A6XX_TEX_CONST_0_FMT(fmt->tex) |
276 A6XX_TEX_CONST_0_SAMPLES(tu_msaa_samples(image->samples)) |
277 A6XX_TEX_CONST_0_SWAP(image->layout.tile_mode ? WZYX : fmt->swap) |
278 tu6_texswiz(&pCreateInfo->components, vk_format_description(iview->vk_format)->swizzle) |
279 A6XX_TEX_CONST_0_MIPLVLS(iview->level_count - 1);
280 iview->descriptor[1] = A6XX_TEX_CONST_1_WIDTH(width) | A6XX_TEX_CONST_1_HEIGHT(height);
281 iview->descriptor[2] =
282 A6XX_TEX_CONST_2_FETCHSIZE(tu6_fetchsize(iview->vk_format)) |
283 A6XX_TEX_CONST_2_PITCH(pitch) |
284 A6XX_TEX_CONST_2_TYPE(tu6_tex_type(pCreateInfo->viewType));
285 iview->descriptor[3] = A6XX_TEX_CONST_3_ARRAY_PITCH(tu_layer_size(image, iview->base_mip));
286 iview->descriptor[4] = base_addr;
287 iview->descriptor[5] = base_addr >> 32;
288
289 if (image->layout.ubwc_size) {
290 uint32_t block_width, block_height;
291 fdl6_get_ubwc_blockwidth(&image->layout,
292 &block_width, &block_height);
293
294 iview->descriptor[3] |= A6XX_TEX_CONST_3_FLAG | A6XX_TEX_CONST_3_TILE_ALL;
295 iview->descriptor[7] = ubwc_addr;
296 iview->descriptor[8] = ubwc_addr >> 32;
297 iview->descriptor[9] |= A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(tu_image_ubwc_size(image, iview->base_mip) >> 2);
298 iview->descriptor[10] |=
299 A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(tu_image_ubwc_pitch(image, iview->base_mip)) |
300 A6XX_TEX_CONST_10_FLAG_BUFFER_LOGW(util_logbase2_ceil(DIV_ROUND_UP(width, block_width))) |
301 A6XX_TEX_CONST_10_FLAG_BUFFER_LOGH(util_logbase2_ceil(DIV_ROUND_UP(height, block_height)));
302 }
303
304 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_3D) {
305 iview->descriptor[5] |= A6XX_TEX_CONST_5_DEPTH(iview->layer_count);
306 } else {
307 iview->descriptor[3] |=
308 A6XX_TEX_CONST_3_MIN_LAYERSZ(image->layout.slices[image->level_count - 1].size0);
309 iview->descriptor[5] |=
310 A6XX_TEX_CONST_5_DEPTH(u_minify(image->extent.depth, iview->base_mip));
311 }
312 }
313
314 unsigned
315 tu_image_queue_family_mask(const struct tu_image *image,
316 uint32_t family,
317 uint32_t queue_family)
318 {
319 if (!image->exclusive)
320 return image->queue_family_mask;
321 if (family == VK_QUEUE_FAMILY_EXTERNAL)
322 return (1u << TU_MAX_QUEUE_FAMILIES) - 1u;
323 if (family == VK_QUEUE_FAMILY_IGNORED)
324 return 1u << queue_family;
325 return 1u << family;
326 }
327
328 VkResult
329 tu_CreateImage(VkDevice device,
330 const VkImageCreateInfo *pCreateInfo,
331 const VkAllocationCallbacks *pAllocator,
332 VkImage *pImage)
333 {
334 #ifdef ANDROID
335 const VkNativeBufferANDROID *gralloc_info =
336 vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
337
338 if (gralloc_info)
339 return tu_image_from_gralloc(device, pCreateInfo, gralloc_info,
340 pAllocator, pImage);
341 #endif
342
343 const struct wsi_image_create_info *wsi_info =
344 vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
345 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
346
347 if (wsi_info) {
348 modifier = DRM_FORMAT_MOD_LINEAR;
349 for (unsigned i = 0; i < wsi_info->modifier_count; i++) {
350 if (wsi_info->modifiers[i] == DRM_FORMAT_MOD_QCOM_COMPRESSED)
351 modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
352 }
353 }
354
355 return tu_image_create(device, pCreateInfo, pAllocator, pImage, modifier);
356 }
357
358 void
359 tu_DestroyImage(VkDevice _device,
360 VkImage _image,
361 const VkAllocationCallbacks *pAllocator)
362 {
363 TU_FROM_HANDLE(tu_device, device, _device);
364 TU_FROM_HANDLE(tu_image, image, _image);
365
366 if (!image)
367 return;
368
369 if (image->owned_memory != VK_NULL_HANDLE)
370 tu_FreeMemory(_device, image->owned_memory, pAllocator);
371
372 vk_free2(&device->alloc, pAllocator, image);
373 }
374
375 void
376 tu_GetImageSubresourceLayout(VkDevice _device,
377 VkImage _image,
378 const VkImageSubresource *pSubresource,
379 VkSubresourceLayout *pLayout)
380 {
381 TU_FROM_HANDLE(tu_image, image, _image);
382
383 const struct fdl_slice *slice = image->layout.slices + pSubresource->mipLevel;
384
385 pLayout->offset = fdl_surface_offset(&image->layout,
386 pSubresource->mipLevel,
387 pSubresource->arrayLayer);
388 pLayout->size = slice->size0;
389 pLayout->rowPitch =
390 slice->pitch * vk_format_get_blocksize(image->vk_format);
391 pLayout->arrayPitch = image->layout.layer_size;
392 pLayout->depthPitch = slice->size0;
393
394 if (image->layout.ubwc_size) {
395 /* UBWC starts at offset 0 */
396 pLayout->offset = 0;
397 /* UBWC scanout won't match what the kernel wants if we have levels/layers */
398 assert(image->level_count == 1 && image->layer_count == 1);
399 }
400 }
401
402 VkResult
403 tu_CreateImageView(VkDevice _device,
404 const VkImageViewCreateInfo *pCreateInfo,
405 const VkAllocationCallbacks *pAllocator,
406 VkImageView *pView)
407 {
408 TU_FROM_HANDLE(tu_device, device, _device);
409 struct tu_image_view *view;
410
411 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
412 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
413 if (view == NULL)
414 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
415
416 tu_image_view_init(view, device, pCreateInfo);
417
418 *pView = tu_image_view_to_handle(view);
419
420 return VK_SUCCESS;
421 }
422
423 void
424 tu_DestroyImageView(VkDevice _device,
425 VkImageView _iview,
426 const VkAllocationCallbacks *pAllocator)
427 {
428 TU_FROM_HANDLE(tu_device, device, _device);
429 TU_FROM_HANDLE(tu_image_view, iview, _iview);
430
431 if (!iview)
432 return;
433 vk_free2(&device->alloc, pAllocator, iview);
434 }
435
436 void
437 tu_buffer_view_init(struct tu_buffer_view *view,
438 struct tu_device *device,
439 const VkBufferViewCreateInfo *pCreateInfo)
440 {
441 TU_FROM_HANDLE(tu_buffer, buffer, pCreateInfo->buffer);
442
443 view->range = pCreateInfo->range == VK_WHOLE_SIZE
444 ? buffer->size - pCreateInfo->offset
445 : pCreateInfo->range;
446 view->vk_format = pCreateInfo->format;
447 }
448
449 VkResult
450 tu_CreateBufferView(VkDevice _device,
451 const VkBufferViewCreateInfo *pCreateInfo,
452 const VkAllocationCallbacks *pAllocator,
453 VkBufferView *pView)
454 {
455 TU_FROM_HANDLE(tu_device, device, _device);
456 struct tu_buffer_view *view;
457
458 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
459 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
460 if (!view)
461 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
462
463 tu_buffer_view_init(view, device, pCreateInfo);
464
465 *pView = tu_buffer_view_to_handle(view);
466
467 return VK_SUCCESS;
468 }
469
470 void
471 tu_DestroyBufferView(VkDevice _device,
472 VkBufferView bufferView,
473 const VkAllocationCallbacks *pAllocator)
474 {
475 TU_FROM_HANDLE(tu_device, device, _device);
476 TU_FROM_HANDLE(tu_buffer_view, view, bufferView);
477
478 if (!view)
479 return;
480
481 vk_free2(&device->alloc, pAllocator, view);
482 }