Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/broadcom/vulkan/v3dv_image.c
4560 views
1
/*
2
* Copyright © 2019 Raspberry Pi
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
*/
23
24
#include "v3dv_private.h"
25
26
#include "drm-uapi/drm_fourcc.h"
27
#include "util/format/u_format.h"
28
#include "util/u_math.h"
29
#include "vk_format_info.h"
30
#include "vk_util.h"
31
#include "vulkan/wsi/wsi_common.h"
32
33
/**
34
* Computes the HW's UIFblock padding for a given height/cpp.
35
*
36
* The goal of the padding is to keep pages of the same color (bank number) at
37
* least half a page away from each other vertically when crossing between
38
* columns of UIF blocks.
39
*/
40
static uint32_t
41
v3d_get_ub_pad(uint32_t cpp, uint32_t height)
42
{
43
uint32_t utile_h = v3d_utile_height(cpp);
44
uint32_t uif_block_h = utile_h * 2;
45
uint32_t height_ub = height / uif_block_h;
46
47
uint32_t height_offset_in_pc = height_ub % PAGE_CACHE_UB_ROWS;
48
49
/* For the perfectly-aligned-for-UIF-XOR case, don't add any pad. */
50
if (height_offset_in_pc == 0)
51
return 0;
52
53
/* Try padding up to where we're offset by at least half a page. */
54
if (height_offset_in_pc < PAGE_UB_ROWS_TIMES_1_5) {
55
/* If we fit entirely in the page cache, don't pad. */
56
if (height_ub < PAGE_CACHE_UB_ROWS)
57
return 0;
58
else
59
return PAGE_UB_ROWS_TIMES_1_5 - height_offset_in_pc;
60
}
61
62
/* If we're close to being aligned to page cache size, then round up
63
* and rely on XOR.
64
*/
65
if (height_offset_in_pc > PAGE_CACHE_MINUS_1_5_UB_ROWS)
66
return PAGE_CACHE_UB_ROWS - height_offset_in_pc;
67
68
/* Otherwise, we're far enough away (top and bottom) to not need any
69
* padding.
70
*/
71
return 0;
72
}
73
74
static void
75
v3d_setup_slices(struct v3dv_image *image)
76
{
77
assert(image->cpp > 0);
78
79
uint32_t width = image->extent.width;
80
uint32_t height = image->extent.height;
81
uint32_t depth = image->extent.depth;
82
83
/* Note that power-of-two padding is based on level 1. These are not
84
* equivalent to just util_next_power_of_two(dimension), because at a
85
* level 0 dimension of 9, the level 1 power-of-two padded value is 4,
86
* not 8.
87
*/
88
uint32_t pot_width = 2 * util_next_power_of_two(u_minify(width, 1));
89
uint32_t pot_height = 2 * util_next_power_of_two(u_minify(height, 1));
90
uint32_t pot_depth = 2 * util_next_power_of_two(u_minify(depth, 1));
91
92
uint32_t utile_w = v3d_utile_width(image->cpp);
93
uint32_t utile_h = v3d_utile_height(image->cpp);
94
uint32_t uif_block_w = utile_w * 2;
95
uint32_t uif_block_h = utile_h * 2;
96
97
uint32_t block_width = vk_format_get_blockwidth(image->vk_format);
98
uint32_t block_height = vk_format_get_blockheight(image->vk_format);
99
100
assert(image->samples == VK_SAMPLE_COUNT_1_BIT ||
101
image->samples == VK_SAMPLE_COUNT_4_BIT);
102
bool msaa = image->samples != VK_SAMPLE_COUNT_1_BIT;
103
104
bool uif_top = msaa;
105
106
assert(image->array_size > 0);
107
assert(depth > 0);
108
assert(image->levels >= 1);
109
110
uint32_t offset = 0;
111
for (int32_t i = image->levels - 1; i >= 0; i--) {
112
struct v3d_resource_slice *slice = &image->slices[i];
113
114
uint32_t level_width, level_height, level_depth;
115
if (i < 2) {
116
level_width = u_minify(width, i);
117
level_height = u_minify(height, i);
118
} else {
119
level_width = u_minify(pot_width, i);
120
level_height = u_minify(pot_height, i);
121
}
122
123
if (i < 1)
124
level_depth = u_minify(depth, i);
125
else
126
level_depth = u_minify(pot_depth, i);
127
128
if (msaa) {
129
level_width *= 2;
130
level_height *= 2;
131
}
132
133
level_width = DIV_ROUND_UP(level_width, block_width);
134
level_height = DIV_ROUND_UP(level_height, block_height);
135
136
if (!image->tiled) {
137
slice->tiling = V3D_TILING_RASTER;
138
if (image->type == VK_IMAGE_TYPE_1D)
139
level_width = align(level_width, 64 / image->cpp);
140
} else {
141
if ((i != 0 || !uif_top) &&
142
(level_width <= utile_w || level_height <= utile_h)) {
143
slice->tiling = V3D_TILING_LINEARTILE;
144
level_width = align(level_width, utile_w);
145
level_height = align(level_height, utile_h);
146
} else if ((i != 0 || !uif_top) && level_width <= uif_block_w) {
147
slice->tiling = V3D_TILING_UBLINEAR_1_COLUMN;
148
level_width = align(level_width, uif_block_w);
149
level_height = align(level_height, uif_block_h);
150
} else if ((i != 0 || !uif_top) && level_width <= 2 * uif_block_w) {
151
slice->tiling = V3D_TILING_UBLINEAR_2_COLUMN;
152
level_width = align(level_width, 2 * uif_block_w);
153
level_height = align(level_height, uif_block_h);
154
} else {
155
/* We align the width to a 4-block column of UIF blocks, but we
156
* only align height to UIF blocks.
157
*/
158
level_width = align(level_width, 4 * uif_block_w);
159
level_height = align(level_height, uif_block_h);
160
161
slice->ub_pad = v3d_get_ub_pad(image->cpp, level_height);
162
level_height += slice->ub_pad * uif_block_h;
163
164
/* If the padding set us to to be aligned to the page cache size,
165
* then the HW will use the XOR bit on odd columns to get us
166
* perfectly misaligned.
167
*/
168
if ((level_height / uif_block_h) %
169
(V3D_PAGE_CACHE_SIZE / V3D_UIFBLOCK_ROW_SIZE) == 0) {
170
slice->tiling = V3D_TILING_UIF_XOR;
171
} else {
172
slice->tiling = V3D_TILING_UIF_NO_XOR;
173
}
174
}
175
}
176
177
slice->offset = offset;
178
slice->stride = level_width * image->cpp;
179
slice->padded_height = level_height;
180
if (slice->tiling == V3D_TILING_UIF_NO_XOR ||
181
slice->tiling == V3D_TILING_UIF_XOR) {
182
slice->padded_height_of_output_image_in_uif_blocks =
183
slice->padded_height / (2 * v3d_utile_height(image->cpp));
184
}
185
186
slice->size = level_height * slice->stride;
187
uint32_t slice_total_size = slice->size * level_depth;
188
189
/* The HW aligns level 1's base to a page if any of level 1 or
190
* below could be UIF XOR. The lower levels then inherit the
191
* alignment for as long as necesary, thanks to being power of
192
* two aligned.
193
*/
194
if (i == 1 &&
195
level_width > 4 * uif_block_w &&
196
level_height > PAGE_CACHE_MINUS_1_5_UB_ROWS * uif_block_h) {
197
slice_total_size = align(slice_total_size, V3D_UIFCFG_PAGE_SIZE);
198
}
199
200
offset += slice_total_size;
201
}
202
203
image->size = offset;
204
205
/* UIF/UBLINEAR levels need to be aligned to UIF-blocks, and LT only
206
* needs to be aligned to utile boundaries. Since tiles are laid out
207
* from small to big in memory, we need to align the later UIF slices
208
* to UIF blocks, if they were preceded by non-UIF-block-aligned LT
209
* slices.
210
*
211
* We additionally align to 4k, which improves UIF XOR performance.
212
*/
213
image->alignment =
214
image->tiling == VK_IMAGE_TILING_LINEAR ? image->cpp : 4096;
215
uint32_t align_offset =
216
align(image->slices[0].offset, image->alignment) - image->slices[0].offset;
217
if (align_offset) {
218
image->size += align_offset;
219
for (int i = 0; i < image->levels; i++)
220
image->slices[i].offset += align_offset;
221
}
222
223
/* Arrays and cube textures have a stride which is the distance from
224
* one full mipmap tree to the next (64b aligned). For 3D textures,
225
* we need to program the stride between slices of miplevel 0.
226
*/
227
if (image->type != VK_IMAGE_TYPE_3D) {
228
image->cube_map_stride =
229
align(image->slices[0].offset + image->slices[0].size, 64);
230
image->size += image->cube_map_stride * (image->array_size - 1);
231
} else {
232
image->cube_map_stride = image->slices[0].size;
233
}
234
}
235
236
uint32_t
237
v3dv_layer_offset(const struct v3dv_image *image, uint32_t level, uint32_t layer)
238
{
239
const struct v3d_resource_slice *slice = &image->slices[level];
240
241
if (image->type == VK_IMAGE_TYPE_3D)
242
return image->mem_offset + slice->offset + layer * slice->size;
243
else
244
return image->mem_offset + slice->offset + layer * image->cube_map_stride;
245
}
246
247
static VkResult
248
create_image(struct v3dv_device *device,
249
const VkImageCreateInfo *pCreateInfo,
250
const VkAllocationCallbacks *pAllocator,
251
VkImage *pImage)
252
{
253
struct v3dv_image *image = NULL;
254
255
assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
256
257
v3dv_assert(pCreateInfo->mipLevels > 0);
258
v3dv_assert(pCreateInfo->arrayLayers > 0);
259
v3dv_assert(pCreateInfo->samples > 0);
260
v3dv_assert(pCreateInfo->extent.width > 0);
261
v3dv_assert(pCreateInfo->extent.height > 0);
262
v3dv_assert(pCreateInfo->extent.depth > 0);
263
264
/* When using the simulator the WSI common code will see that our
265
* driver wsi device doesn't match the display device and because of that
266
* it will not attempt to present directly from the swapchain images,
267
* instead it will use the prime blit path (use_prime_blit flag in
268
* struct wsi_swapchain), where it copies the contents of the swapchain
269
* images to a linear buffer with appropriate row stride for presentation.
270
* As a result, on that path, swapchain images do not have any special
271
* requirements and are not created with the pNext structs below.
272
*/
273
uint64_t modifier = DRM_FORMAT_MOD_INVALID;
274
if (pCreateInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
275
const VkImageDrmFormatModifierListCreateInfoEXT *mod_info =
276
vk_find_struct_const(pCreateInfo->pNext,
277
IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
278
assert(mod_info);
279
for (uint32_t i = 0; i < mod_info->drmFormatModifierCount; i++) {
280
switch (mod_info->pDrmFormatModifiers[i]) {
281
case DRM_FORMAT_MOD_LINEAR:
282
if (modifier == DRM_FORMAT_MOD_INVALID)
283
modifier = DRM_FORMAT_MOD_LINEAR;
284
break;
285
case DRM_FORMAT_MOD_BROADCOM_UIF:
286
modifier = DRM_FORMAT_MOD_BROADCOM_UIF;
287
break;
288
}
289
}
290
} else {
291
const struct wsi_image_create_info *wsi_info =
292
vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
293
if (wsi_info && wsi_info->scanout)
294
modifier = DRM_FORMAT_MOD_LINEAR;
295
}
296
297
const VkExternalMemoryImageCreateInfo *external_info =
298
vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_MEMORY_IMAGE_CREATE_INFO);
299
300
/* 1D and 1D_ARRAY textures are always raster-order */
301
VkImageTiling tiling;
302
if (pCreateInfo->imageType == VK_IMAGE_TYPE_1D)
303
tiling = VK_IMAGE_TILING_LINEAR;
304
else if (modifier == DRM_FORMAT_MOD_INVALID)
305
tiling = pCreateInfo->tiling;
306
else if (modifier == DRM_FORMAT_MOD_BROADCOM_UIF)
307
tiling = VK_IMAGE_TILING_OPTIMAL;
308
else
309
tiling = VK_IMAGE_TILING_LINEAR;
310
311
const struct v3dv_format *format = v3dv_X(device, get_format)(pCreateInfo->format);
312
v3dv_assert(format != NULL && format->supported);
313
314
image = vk_object_zalloc(&device->vk, pAllocator, sizeof(*image),
315
VK_OBJECT_TYPE_IMAGE);
316
if (!image)
317
return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
318
319
assert(pCreateInfo->samples == VK_SAMPLE_COUNT_1_BIT ||
320
pCreateInfo->samples == VK_SAMPLE_COUNT_4_BIT);
321
322
image->type = pCreateInfo->imageType;
323
image->extent = pCreateInfo->extent;
324
image->vk_format = pCreateInfo->format;
325
image->format = format;
326
image->aspects = vk_format_aspects(image->vk_format);
327
image->levels = pCreateInfo->mipLevels;
328
image->array_size = pCreateInfo->arrayLayers;
329
image->samples = pCreateInfo->samples;
330
image->usage = pCreateInfo->usage;
331
image->flags = pCreateInfo->flags;
332
333
image->drm_format_mod = modifier;
334
image->tiling = tiling;
335
image->tiled = tiling == VK_IMAGE_TILING_OPTIMAL;
336
image->external = external_info != NULL;
337
338
image->cpp = vk_format_get_blocksize(image->vk_format);
339
340
v3d_setup_slices(image);
341
342
*pImage = v3dv_image_to_handle(image);
343
344
return VK_SUCCESS;
345
}
346
347
static VkResult
348
create_image_from_swapchain(struct v3dv_device *device,
349
const VkImageCreateInfo *pCreateInfo,
350
const VkImageSwapchainCreateInfoKHR *swapchain_info,
351
const VkAllocationCallbacks *pAllocator,
352
VkImage *pImage)
353
{
354
struct v3dv_image *swapchain_image =
355
v3dv_wsi_get_image_from_swapchain(swapchain_info->swapchain, 0);
356
assert(swapchain_image);
357
358
VkImageCreateInfo local_create_info = *pCreateInfo;
359
local_create_info.pNext = NULL;
360
361
/* Added by wsi code. */
362
local_create_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
363
364
/* The spec requires TILING_OPTIMAL as input, but the swapchain image may
365
* privately use a different tiling. See spec anchor
366
* #swapchain-wsi-image-create-info .
367
*/
368
assert(local_create_info.tiling == VK_IMAGE_TILING_OPTIMAL);
369
local_create_info.tiling = swapchain_image->tiling;
370
371
VkImageDrmFormatModifierListCreateInfoEXT local_modifier_info = {
372
.sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT,
373
.drmFormatModifierCount = 1,
374
.pDrmFormatModifiers = &swapchain_image->drm_format_mod,
375
};
376
377
if (swapchain_image->drm_format_mod != DRM_FORMAT_MOD_INVALID)
378
__vk_append_struct(&local_create_info, &local_modifier_info);
379
380
assert(swapchain_image->type == local_create_info.imageType);
381
assert(swapchain_image->vk_format == local_create_info.format);
382
assert(swapchain_image->extent.width == local_create_info.extent.width);
383
assert(swapchain_image->extent.height == local_create_info.extent.height);
384
assert(swapchain_image->extent.depth == local_create_info.extent.depth);
385
assert(swapchain_image->array_size == local_create_info.arrayLayers);
386
assert(swapchain_image->samples == local_create_info.samples);
387
assert(swapchain_image->tiling == local_create_info.tiling);
388
assert((swapchain_image->usage & local_create_info.usage) ==
389
local_create_info.usage);
390
391
return create_image(device, &local_create_info, pAllocator, pImage);
392
}
393
394
VKAPI_ATTR VkResult VKAPI_CALL
395
v3dv_CreateImage(VkDevice _device,
396
const VkImageCreateInfo *pCreateInfo,
397
const VkAllocationCallbacks *pAllocator,
398
VkImage *pImage)
399
{
400
V3DV_FROM_HANDLE(v3dv_device, device, _device);
401
402
const VkImageSwapchainCreateInfoKHR *swapchain_info =
403
vk_find_struct_const(pCreateInfo->pNext, IMAGE_SWAPCHAIN_CREATE_INFO_KHR);
404
if (swapchain_info && swapchain_info->swapchain != VK_NULL_HANDLE)
405
return create_image_from_swapchain(device, pCreateInfo, swapchain_info,
406
pAllocator, pImage);
407
408
return create_image(device, pCreateInfo, pAllocator, pImage);
409
}
410
411
VKAPI_ATTR void VKAPI_CALL
412
v3dv_GetImageSubresourceLayout(VkDevice device,
413
VkImage _image,
414
const VkImageSubresource *subresource,
415
VkSubresourceLayout *layout)
416
{
417
V3DV_FROM_HANDLE(v3dv_image, image, _image);
418
419
const struct v3d_resource_slice *slice =
420
&image->slices[subresource->mipLevel];
421
layout->offset =
422
v3dv_layer_offset(image, subresource->mipLevel, subresource->arrayLayer);
423
layout->rowPitch = slice->stride;
424
layout->depthPitch = image->cube_map_stride;
425
layout->arrayPitch = image->cube_map_stride;
426
427
if (image->type != VK_IMAGE_TYPE_3D) {
428
layout->size = slice->size;
429
} else {
430
/* For 3D images, the size of the slice represents the size of a 2D slice
431
* in the 3D image, so we have to multiply by the depth extent of the
432
* miplevel. For levels other than the first, we just compute the size
433
* as the distance between consecutive levels (notice that mip levels are
434
* arranged in memory from last to first).
435
*/
436
if (subresource->mipLevel == 0) {
437
layout->size = slice->size * image->extent.depth;
438
} else {
439
const struct v3d_resource_slice *prev_slice =
440
&image->slices[subresource->mipLevel - 1];
441
layout->size = prev_slice->offset - slice->offset;
442
}
443
}
444
}
445
446
VKAPI_ATTR VkResult VKAPI_CALL
447
v3dv_GetImageDrmFormatModifierPropertiesEXT(
448
VkDevice device,
449
VkImage _image,
450
VkImageDrmFormatModifierPropertiesEXT *pProperties)
451
{
452
V3DV_FROM_HANDLE(v3dv_image, image, _image);
453
454
assert(pProperties->sType ==
455
VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT);
456
457
pProperties->drmFormatModifier = image->drm_format_mod;
458
459
return VK_SUCCESS;
460
}
461
462
VKAPI_ATTR void VKAPI_CALL
463
v3dv_DestroyImage(VkDevice _device,
464
VkImage _image,
465
const VkAllocationCallbacks* pAllocator)
466
{
467
V3DV_FROM_HANDLE(v3dv_device, device, _device);
468
V3DV_FROM_HANDLE(v3dv_image, image, _image);
469
470
if (image == NULL)
471
return;
472
473
vk_object_free(&device->vk, pAllocator, image);
474
}
475
476
VkImageViewType
477
v3dv_image_type_to_view_type(VkImageType type)
478
{
479
switch (type) {
480
case VK_IMAGE_TYPE_1D: return VK_IMAGE_VIEW_TYPE_1D;
481
case VK_IMAGE_TYPE_2D: return VK_IMAGE_VIEW_TYPE_2D;
482
case VK_IMAGE_TYPE_3D: return VK_IMAGE_VIEW_TYPE_3D;
483
default:
484
unreachable("Invalid image type");
485
}
486
}
487
488
static enum pipe_swizzle
489
vk_component_mapping_to_pipe_swizzle(VkComponentSwizzle comp,
490
VkComponentSwizzle swz)
491
{
492
if (swz == VK_COMPONENT_SWIZZLE_IDENTITY)
493
swz = comp;
494
495
switch (swz) {
496
case VK_COMPONENT_SWIZZLE_ZERO:
497
return PIPE_SWIZZLE_0;
498
case VK_COMPONENT_SWIZZLE_ONE:
499
return PIPE_SWIZZLE_1;
500
case VK_COMPONENT_SWIZZLE_R:
501
return PIPE_SWIZZLE_X;
502
case VK_COMPONENT_SWIZZLE_G:
503
return PIPE_SWIZZLE_Y;
504
case VK_COMPONENT_SWIZZLE_B:
505
return PIPE_SWIZZLE_Z;
506
case VK_COMPONENT_SWIZZLE_A:
507
return PIPE_SWIZZLE_W;
508
default:
509
unreachable("Unknown VkComponentSwizzle");
510
};
511
}
512
513
VKAPI_ATTR VkResult VKAPI_CALL
514
v3dv_CreateImageView(VkDevice _device,
515
const VkImageViewCreateInfo *pCreateInfo,
516
const VkAllocationCallbacks *pAllocator,
517
VkImageView *pView)
518
{
519
V3DV_FROM_HANDLE(v3dv_device, device, _device);
520
V3DV_FROM_HANDLE(v3dv_image, image, pCreateInfo->image);
521
struct v3dv_image_view *iview;
522
523
iview = vk_object_zalloc(&device->vk, pAllocator, sizeof(*iview),
524
VK_OBJECT_TYPE_IMAGE_VIEW);
525
if (iview == NULL)
526
return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
527
528
const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
529
530
assert(range->layerCount > 0);
531
assert(range->baseMipLevel < image->levels);
532
533
#ifdef DEBUG
534
switch (image->type) {
535
case VK_IMAGE_TYPE_1D:
536
case VK_IMAGE_TYPE_2D:
537
assert(range->baseArrayLayer + v3dv_layer_count(image, range) - 1 <=
538
image->array_size);
539
break;
540
case VK_IMAGE_TYPE_3D:
541
assert(range->baseArrayLayer + v3dv_layer_count(image, range) - 1
542
<= u_minify(image->extent.depth, range->baseMipLevel));
543
/* VK_KHR_maintenance1 */
544
assert(pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D ||
545
((image->flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT) &&
546
range->levelCount == 1 && range->layerCount == 1));
547
assert(pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D_ARRAY ||
548
((image->flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT) &&
549
range->levelCount == 1));
550
break;
551
default:
552
unreachable("bad VkImageType");
553
}
554
#endif
555
556
iview->image = image;
557
iview->aspects = range->aspectMask;
558
iview->type = pCreateInfo->viewType;
559
560
iview->base_level = range->baseMipLevel;
561
iview->max_level = iview->base_level + v3dv_level_count(image, range) - 1;
562
iview->extent = (VkExtent3D) {
563
.width = u_minify(image->extent.width , iview->base_level),
564
.height = u_minify(image->extent.height, iview->base_level),
565
.depth = u_minify(image->extent.depth , iview->base_level),
566
};
567
568
iview->first_layer = range->baseArrayLayer;
569
iview->last_layer = range->baseArrayLayer +
570
v3dv_layer_count(image, range) - 1;
571
iview->offset =
572
v3dv_layer_offset(image, iview->base_level, iview->first_layer);
573
574
/* If we have D24S8 format but the view only selects the stencil aspect
575
* we want to re-interpret the format as RGBA8_UINT, then map our stencil
576
* data reads to the R component and ignore the GBA channels that contain
577
* the depth aspect data.
578
*/
579
VkFormat format;
580
uint8_t image_view_swizzle[4];
581
if (pCreateInfo->format == VK_FORMAT_D24_UNORM_S8_UINT &&
582
range->aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) {
583
format = VK_FORMAT_R8G8B8A8_UINT;
584
image_view_swizzle[0] = PIPE_SWIZZLE_X;
585
image_view_swizzle[1] = PIPE_SWIZZLE_0;
586
image_view_swizzle[2] = PIPE_SWIZZLE_0;
587
image_view_swizzle[3] = PIPE_SWIZZLE_1;
588
} else {
589
format = pCreateInfo->format;
590
591
/* FIXME: we are doing this vk to pipe swizzle mapping just to call
592
* util_format_compose_swizzles. Would be good to check if it would be
593
* better to reimplement the latter using vk component
594
*/
595
image_view_swizzle[0] =
596
vk_component_mapping_to_pipe_swizzle(VK_COMPONENT_SWIZZLE_R,
597
pCreateInfo->components.r);
598
image_view_swizzle[1] =
599
vk_component_mapping_to_pipe_swizzle(VK_COMPONENT_SWIZZLE_G,
600
pCreateInfo->components.g);
601
image_view_swizzle[2] =
602
vk_component_mapping_to_pipe_swizzle(VK_COMPONENT_SWIZZLE_B,
603
pCreateInfo->components.b);
604
image_view_swizzle[3] =
605
vk_component_mapping_to_pipe_swizzle(VK_COMPONENT_SWIZZLE_A,
606
pCreateInfo->components.a);
607
}
608
609
iview->vk_format = format;
610
iview->format = v3dv_X(device, get_format)(format);
611
assert(iview->format && iview->format->supported);
612
613
if (vk_format_is_depth_or_stencil(iview->vk_format)) {
614
iview->internal_type = v3dv_X(device, get_internal_depth_type)(iview->vk_format);
615
} else {
616
v3dv_X(device, get_internal_type_bpp_for_output_format)
617
(iview->format->rt_type, &iview->internal_type, &iview->internal_bpp);
618
}
619
620
const uint8_t *format_swizzle = v3dv_get_format_swizzle(device, format);
621
util_format_compose_swizzles(format_swizzle, image_view_swizzle,
622
iview->swizzle);
623
iview->swap_rb = iview->swizzle[0] == PIPE_SWIZZLE_Z;
624
625
v3dv_X(device, pack_texture_shader_state)(device, iview);
626
627
*pView = v3dv_image_view_to_handle(iview);
628
629
return VK_SUCCESS;
630
}
631
632
VKAPI_ATTR void VKAPI_CALL
633
v3dv_DestroyImageView(VkDevice _device,
634
VkImageView imageView,
635
const VkAllocationCallbacks* pAllocator)
636
{
637
V3DV_FROM_HANDLE(v3dv_device, device, _device);
638
V3DV_FROM_HANDLE(v3dv_image_view, image_view, imageView);
639
640
if (image_view == NULL)
641
return;
642
643
vk_object_free(&device->vk, pAllocator, image_view);
644
}
645
646
VKAPI_ATTR VkResult VKAPI_CALL
647
v3dv_CreateBufferView(VkDevice _device,
648
const VkBufferViewCreateInfo *pCreateInfo,
649
const VkAllocationCallbacks *pAllocator,
650
VkBufferView *pView)
651
{
652
V3DV_FROM_HANDLE(v3dv_device, device, _device);
653
654
struct v3dv_buffer *buffer =
655
v3dv_buffer_from_handle(pCreateInfo->buffer);
656
657
struct v3dv_buffer_view *view =
658
vk_object_zalloc(&device->vk, pAllocator, sizeof(*view),
659
VK_OBJECT_TYPE_BUFFER_VIEW);
660
if (!view)
661
return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
662
663
uint32_t range;
664
if (pCreateInfo->range == VK_WHOLE_SIZE)
665
range = buffer->size - pCreateInfo->offset;
666
else
667
range = pCreateInfo->range;
668
669
enum pipe_format pipe_format = vk_format_to_pipe_format(pCreateInfo->format);
670
uint32_t num_elements = range / util_format_get_blocksize(pipe_format);
671
672
view->buffer = buffer;
673
view->offset = pCreateInfo->offset;
674
view->size = view->offset + range;
675
view->num_elements = num_elements;
676
view->vk_format = pCreateInfo->format;
677
view->format = v3dv_X(device, get_format)(view->vk_format);
678
679
v3dv_X(device, get_internal_type_bpp_for_output_format)
680
(view->format->rt_type, &view->internal_type, &view->internal_bpp);
681
682
if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT ||
683
buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)
684
v3dv_X(device, pack_texture_shader_state_from_buffer_view)(device, view);
685
686
*pView = v3dv_buffer_view_to_handle(view);
687
688
return VK_SUCCESS;
689
}
690
691
VKAPI_ATTR void VKAPI_CALL
692
v3dv_DestroyBufferView(VkDevice _device,
693
VkBufferView bufferView,
694
const VkAllocationCallbacks *pAllocator)
695
{
696
V3DV_FROM_HANDLE(v3dv_device, device, _device);
697
V3DV_FROM_HANDLE(v3dv_buffer_view, buffer_view, bufferView);
698
699
if (buffer_view == NULL)
700
return;
701
702
vk_object_free(&device->vk, pAllocator, buffer_view);
703
}
704
705