anv/image: Make heavier use of aspects
[mesa.git] / src / intel / vulkan / anv_image.c
1 /*
2 * Copyright © 2015 Intel Corporation
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 <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 #include "vk_format_info.h"
33
34 /**
35 * Exactly one bit must be set in \a aspect.
36 */
37 static isl_surf_usage_flags_t
38 choose_isl_surf_usage(VkImageUsageFlags vk_usage,
39 VkImageAspectFlags aspect)
40 {
41 isl_surf_usage_flags_t isl_usage = 0;
42
43 /* FINISHME: Support aux surfaces */
44 isl_usage |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
45
46 if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
47 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
48
49 if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
50 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
51
52 if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
53 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
54
55 if (vk_usage & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
56 isl_usage |= ISL_SURF_USAGE_CUBE_BIT;
57
58 if (vk_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
59 switch (aspect) {
60 default:
61 unreachable("bad VkImageAspect");
62 case VK_IMAGE_ASPECT_DEPTH_BIT:
63 isl_usage |= ISL_SURF_USAGE_DEPTH_BIT;
64 break;
65 case VK_IMAGE_ASPECT_STENCIL_BIT:
66 isl_usage |= ISL_SURF_USAGE_STENCIL_BIT;
67 break;
68 }
69 }
70
71 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
72 /* Meta implements transfers by sampling from the source image. */
73 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
74 }
75
76 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
77 /* Meta implements transfers by rendering into the destination image. */
78 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
79 }
80
81 return isl_usage;
82 }
83
84 /**
85 * Exactly one bit must be set in \a aspect.
86 */
87 static struct anv_surface *
88 get_surface(struct anv_image *image, VkImageAspectFlags aspect)
89 {
90 switch (aspect) {
91 default:
92 unreachable("bad VkImageAspect");
93 case VK_IMAGE_ASPECT_COLOR_BIT:
94 return &image->color_surface;
95 case VK_IMAGE_ASPECT_DEPTH_BIT:
96 return &image->depth_surface;
97 case VK_IMAGE_ASPECT_STENCIL_BIT:
98 return &image->stencil_surface;
99 }
100 }
101
102 /**
103 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
104 * image's memory requirements (that is, the image's size and alignment).
105 *
106 * Exactly one bit must be set in \a aspect.
107 */
108 static VkResult
109 make_surface(const struct anv_device *dev,
110 struct anv_image *image,
111 const struct anv_image_create_info *anv_info,
112 VkImageAspectFlags aspect)
113 {
114 const VkImageCreateInfo *vk_info = anv_info->vk_info;
115 bool ok UNUSED;
116
117 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
118 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
119 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
120 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
121 };
122
123 isl_tiling_flags_t tiling_flags = anv_info->isl_tiling_flags;
124 if (vk_info->tiling == VK_IMAGE_TILING_LINEAR)
125 tiling_flags = ISL_TILING_LINEAR_BIT;
126
127 struct anv_surface *anv_surf = get_surface(image, aspect);
128
129 image->extent = anv_sanitize_image_extent(vk_info->imageType,
130 vk_info->extent);
131
132 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
133 .dim = vk_to_isl_surf_dim[vk_info->imageType],
134 .format = anv_get_isl_format(vk_info->format, aspect,
135 vk_info->tiling, NULL),
136 .width = image->extent.width,
137 .height = image->extent.height,
138 .depth = image->extent.depth,
139 .levels = vk_info->mipLevels,
140 .array_len = vk_info->arrayLayers,
141 .samples = vk_info->samples,
142 .min_alignment = 0,
143 .min_pitch = anv_info->stride,
144 .usage = choose_isl_surf_usage(image->usage, aspect),
145 .tiling_flags = tiling_flags);
146
147 /* isl_surf_init() will fail only if provided invalid input. Invalid input
148 * is illegal in Vulkan.
149 */
150 assert(ok);
151
152 anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment);
153 image->size = anv_surf->offset + anv_surf->isl.size;
154 image->alignment = MAX(image->alignment, anv_surf->isl.alignment);
155
156 return VK_SUCCESS;
157 }
158
159 /**
160 * Parameter @a format is required and overrides VkImageCreateInfo::format.
161 */
162 static VkImageUsageFlags
163 anv_image_get_full_usage(const VkImageCreateInfo *info,
164 const struct anv_format *format)
165 {
166 VkImageUsageFlags usage = info->usage;
167
168 if (info->samples > 1 &&
169 (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) {
170 /* Meta will resolve the image by binding it as a texture. */
171 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
172 }
173
174 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
175 /* Meta will transfer from the image by binding it as a texture. */
176 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
177 }
178
179 if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
180 /* For non-clear transfer operations, meta will transfer to the image by
181 * binding it as a color attachment, even if the image format is not
182 * a color format.
183 */
184 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
185
186 if (anv_format_is_depth_or_stencil(format)) {
187 /* vkCmdClearDepthStencilImage() only requires that
188 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
189 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
190 * clears the image, though, by binding it as a depthstencil
191 * attachment.
192 */
193 usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
194 }
195 }
196
197 return usage;
198 }
199
200 VkResult
201 anv_image_create(VkDevice _device,
202 const struct anv_image_create_info *create_info,
203 const VkAllocationCallbacks* alloc,
204 VkImage *pImage)
205 {
206 ANV_FROM_HANDLE(anv_device, device, _device);
207 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
208 struct anv_image *image = NULL;
209 const struct anv_format *format = anv_format_for_vk_format(pCreateInfo->format);
210 VkResult r;
211
212 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
213
214 anv_assert(pCreateInfo->mipLevels > 0);
215 anv_assert(pCreateInfo->arrayLayers > 0);
216 anv_assert(pCreateInfo->samples > 0);
217 anv_assert(pCreateInfo->extent.width > 0);
218 anv_assert(pCreateInfo->extent.height > 0);
219 anv_assert(pCreateInfo->extent.depth > 0);
220
221 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
222 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
223 if (!image)
224 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
225
226 memset(image, 0, sizeof(*image));
227 image->type = pCreateInfo->imageType;
228 image->extent = pCreateInfo->extent;
229 image->vk_format = pCreateInfo->format;
230 image->format = format;
231 image->aspects = vk_format_aspects(image->vk_format);
232 image->levels = pCreateInfo->mipLevels;
233 image->array_size = pCreateInfo->arrayLayers;
234 image->samples = pCreateInfo->samples;
235 image->usage = anv_image_get_full_usage(pCreateInfo, format);
236 image->tiling = pCreateInfo->tiling;
237
238 uint32_t b;
239 for_each_bit(b, image->aspects) {
240 r = make_surface(device, image, create_info, (1 << b));
241 if (r != VK_SUCCESS)
242 goto fail;
243 }
244
245 *pImage = anv_image_to_handle(image);
246
247 return VK_SUCCESS;
248
249 fail:
250 if (image)
251 anv_free2(&device->alloc, alloc, image);
252
253 return r;
254 }
255
256 VkResult
257 anv_CreateImage(VkDevice device,
258 const VkImageCreateInfo *pCreateInfo,
259 const VkAllocationCallbacks *pAllocator,
260 VkImage *pImage)
261 {
262 return anv_image_create(device,
263 &(struct anv_image_create_info) {
264 .vk_info = pCreateInfo,
265 .isl_tiling_flags = ISL_TILING_ANY_MASK,
266 },
267 pAllocator,
268 pImage);
269 }
270
271 void
272 anv_DestroyImage(VkDevice _device, VkImage _image,
273 const VkAllocationCallbacks *pAllocator)
274 {
275 ANV_FROM_HANDLE(anv_device, device, _device);
276
277 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
278 }
279
280 static void
281 anv_surface_get_subresource_layout(struct anv_image *image,
282 struct anv_surface *surface,
283 const VkImageSubresource *subresource,
284 VkSubresourceLayout *layout)
285 {
286 /* If we are on a non-zero mip level or array slice, we need to
287 * calculate a real offset.
288 */
289 anv_assert(subresource->mipLevel == 0);
290 anv_assert(subresource->arrayLayer == 0);
291
292 layout->offset = surface->offset;
293 layout->rowPitch = surface->isl.row_pitch;
294 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
295 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
296 layout->size = surface->isl.size;
297 }
298
299 void anv_GetImageSubresourceLayout(
300 VkDevice device,
301 VkImage _image,
302 const VkImageSubresource* pSubresource,
303 VkSubresourceLayout* pLayout)
304 {
305 ANV_FROM_HANDLE(anv_image, image, _image);
306
307 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
308
309 switch (pSubresource->aspectMask) {
310 case VK_IMAGE_ASPECT_COLOR_BIT:
311 anv_surface_get_subresource_layout(image, &image->color_surface,
312 pSubresource, pLayout);
313 break;
314 case VK_IMAGE_ASPECT_DEPTH_BIT:
315 anv_surface_get_subresource_layout(image, &image->depth_surface,
316 pSubresource, pLayout);
317 break;
318 case VK_IMAGE_ASPECT_STENCIL_BIT:
319 anv_surface_get_subresource_layout(image, &image->stencil_surface,
320 pSubresource, pLayout);
321 break;
322 default:
323 assert(!"Invalid image aspect");
324 }
325 }
326
327 VkResult
328 anv_validate_CreateImageView(VkDevice _device,
329 const VkImageViewCreateInfo *pCreateInfo,
330 const VkAllocationCallbacks *pAllocator,
331 VkImageView *pView)
332 {
333 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
334 const VkImageSubresourceRange *subresource;
335 MAYBE_UNUSED const struct anv_format *view_format_info;
336
337 /* Validate structure type before dereferencing it. */
338 assert(pCreateInfo);
339 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
340 subresource = &pCreateInfo->subresourceRange;
341
342 /* Validate viewType is in range before using it. */
343 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
344 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
345
346 /* Validate format is in range before using it. */
347 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
348 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
349 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
350
351 /* Validate channel swizzles. */
352 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
353 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
354 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
355 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
356 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
357 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
358 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
359 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
360
361 /* Validate subresource. */
362 assert(subresource->aspectMask != 0);
363 assert(subresource->levelCount > 0);
364 assert(subresource->layerCount > 0);
365 assert(subresource->baseMipLevel < image->levels);
366 assert(subresource->baseMipLevel + anv_get_levelCount(image, subresource) <= image->levels);
367 assert(subresource->baseArrayLayer < image->array_size);
368 assert(subresource->baseArrayLayer + anv_get_layerCount(image, subresource) <= image->array_size);
369 assert(pView);
370
371 MAYBE_UNUSED const VkImageAspectFlags view_format_aspects =
372 vk_format_aspects(pCreateInfo->format);
373
374 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
375 | VK_IMAGE_ASPECT_STENCIL_BIT;
376
377 /* Validate format. */
378 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
379 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
380 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
381 assert(view_format_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
382 assert(view_format_info->isl_layout->bs ==
383 image->format->isl_layout->bs);
384 } else if (subresource->aspectMask & ds_flags) {
385 assert((subresource->aspectMask & ~ds_flags) == 0);
386
387 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
388 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
389 assert(view_format_aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
390 assert(view_format_info->isl_layout->bs ==
391 image->format->isl_layout->bs);
392 }
393
394 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
395 /* FINISHME: Is it legal to have an R8 view of S8? */
396 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
397 assert(view_format_aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
398 }
399 } else {
400 assert(!"bad VkImageSubresourceRange::aspectFlags");
401 }
402
403 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
404 }
405
406 static struct anv_state
407 alloc_surface_state(struct anv_device *device,
408 struct anv_cmd_buffer *cmd_buffer)
409 {
410 if (cmd_buffer) {
411 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
412 } else {
413 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
414 }
415 }
416
417 static enum isl_channel_select
418 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
419 struct anv_format_swizzle format_swizzle)
420 {
421 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
422 swizzle = component;
423
424 switch (swizzle) {
425 case VK_COMPONENT_SWIZZLE_ZERO:
426 return ISL_CHANNEL_SELECT_ZERO;
427 case VK_COMPONENT_SWIZZLE_ONE:
428 return ISL_CHANNEL_SELECT_ONE;
429 case VK_COMPONENT_SWIZZLE_R:
430 return ISL_CHANNEL_SELECT_RED + format_swizzle.r;
431 case VK_COMPONENT_SWIZZLE_G:
432 return ISL_CHANNEL_SELECT_RED + format_swizzle.g;
433 case VK_COMPONENT_SWIZZLE_B:
434 return ISL_CHANNEL_SELECT_RED + format_swizzle.b;
435 case VK_COMPONENT_SWIZZLE_A:
436 return ISL_CHANNEL_SELECT_RED + format_swizzle.a;
437 default:
438 unreachable("Invalid swizzle");
439 }
440 }
441
442 void
443 anv_image_view_init(struct anv_image_view *iview,
444 struct anv_device *device,
445 const VkImageViewCreateInfo* pCreateInfo,
446 struct anv_cmd_buffer *cmd_buffer,
447 VkImageUsageFlags usage_mask)
448 {
449 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
450 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
451
452 assert(range->layerCount > 0);
453 assert(range->baseMipLevel < image->levels);
454 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
455 VK_IMAGE_USAGE_STORAGE_BIT |
456 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
457 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
458
459 switch (image->type) {
460 default:
461 unreachable("bad VkImageType");
462 case VK_IMAGE_TYPE_1D:
463 case VK_IMAGE_TYPE_2D:
464 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
465 break;
466 case VK_IMAGE_TYPE_3D:
467 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
468 <= anv_minify(image->extent.depth, range->baseMipLevel));
469 break;
470 }
471
472 struct anv_surface *surface =
473 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
474
475 iview->image = image;
476 iview->bo = image->bo;
477 iview->offset = image->offset + surface->offset;
478
479 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
480 iview->vk_format = pCreateInfo->format;
481
482 struct anv_format_swizzle swizzle;
483 enum isl_format format = anv_get_isl_format(pCreateInfo->format,
484 range->aspectMask,
485 image->tiling, &swizzle);
486
487 iview->base_layer = range->baseArrayLayer;
488 iview->base_mip = range->baseMipLevel;
489
490 struct isl_view isl_view = {
491 .format = format,
492 .base_level = range->baseMipLevel,
493 .levels = anv_get_levelCount(image, range),
494 .base_array_layer = range->baseArrayLayer,
495 .array_len = anv_get_layerCount(image, range),
496 .channel_select = {
497 remap_swizzle(pCreateInfo->components.r,
498 VK_COMPONENT_SWIZZLE_R, swizzle),
499 remap_swizzle(pCreateInfo->components.g,
500 VK_COMPONENT_SWIZZLE_G, swizzle),
501 remap_swizzle(pCreateInfo->components.b,
502 VK_COMPONENT_SWIZZLE_B, swizzle),
503 remap_swizzle(pCreateInfo->components.a,
504 VK_COMPONENT_SWIZZLE_A, swizzle),
505 },
506 };
507
508 iview->extent = (VkExtent3D) {
509 .width = anv_minify(image->extent.width , range->baseMipLevel),
510 .height = anv_minify(image->extent.height, range->baseMipLevel),
511 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
512 };
513
514 isl_surf_usage_flags_t cube_usage;
515 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
516 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
517 cube_usage = ISL_SURF_USAGE_CUBE_BIT;
518 } else {
519 cube_usage = 0;
520 }
521
522 if (image->usage & usage_mask & VK_IMAGE_USAGE_SAMPLED_BIT) {
523 iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer);
524
525 isl_view.usage = cube_usage | ISL_SURF_USAGE_TEXTURE_BIT;
526 isl_surf_fill_state(&device->isl_dev,
527 iview->sampler_surface_state.map,
528 .surf = &surface->isl,
529 .view = &isl_view,
530 .mocs = device->default_mocs);
531
532 if (!device->info.has_llc)
533 anv_state_clflush(iview->sampler_surface_state);
534 } else {
535 iview->sampler_surface_state.alloc_size = 0;
536 }
537
538 if (image->usage & usage_mask & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
539 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
540
541 isl_view.usage = cube_usage | ISL_SURF_USAGE_RENDER_TARGET_BIT;
542 isl_surf_fill_state(&device->isl_dev,
543 iview->color_rt_surface_state.map,
544 .surf = &surface->isl,
545 .view = &isl_view,
546 .mocs = device->default_mocs);
547
548 if (!device->info.has_llc)
549 anv_state_clflush(iview->color_rt_surface_state);
550 } else {
551 iview->color_rt_surface_state.alloc_size = 0;
552 }
553
554 if (image->usage & usage_mask & VK_IMAGE_USAGE_STORAGE_BIT) {
555 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
556
557 if (isl_has_matching_typed_storage_image_format(&device->info, format)) {
558 isl_view.usage = cube_usage | ISL_SURF_USAGE_STORAGE_BIT;
559 isl_surf_fill_state(&device->isl_dev,
560 iview->storage_surface_state.map,
561 .surf = &surface->isl,
562 .view = &isl_view,
563 .mocs = device->default_mocs);
564 } else {
565 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
566 ISL_FORMAT_RAW,
567 iview->offset,
568 iview->bo->size - iview->offset, 1);
569 }
570
571 isl_surf_fill_image_param(&device->isl_dev,
572 &iview->storage_image_param,
573 &surface->isl, &isl_view);
574
575 if (!device->info.has_llc)
576 anv_state_clflush(iview->storage_surface_state);
577 } else {
578 iview->storage_surface_state.alloc_size = 0;
579 }
580 }
581
582 VkResult
583 anv_CreateImageView(VkDevice _device,
584 const VkImageViewCreateInfo *pCreateInfo,
585 const VkAllocationCallbacks *pAllocator,
586 VkImageView *pView)
587 {
588 ANV_FROM_HANDLE(anv_device, device, _device);
589 struct anv_image_view *view;
590
591 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
592 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
593 if (view == NULL)
594 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
595
596 anv_image_view_init(view, device, pCreateInfo, NULL, ~0);
597
598 *pView = anv_image_view_to_handle(view);
599
600 return VK_SUCCESS;
601 }
602
603 void
604 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
605 const VkAllocationCallbacks *pAllocator)
606 {
607 ANV_FROM_HANDLE(anv_device, device, _device);
608 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
609
610 if (iview->color_rt_surface_state.alloc_size > 0) {
611 anv_state_pool_free(&device->surface_state_pool,
612 iview->color_rt_surface_state);
613 }
614
615 if (iview->sampler_surface_state.alloc_size > 0) {
616 anv_state_pool_free(&device->surface_state_pool,
617 iview->sampler_surface_state);
618 }
619
620 if (iview->storage_surface_state.alloc_size > 0) {
621 anv_state_pool_free(&device->surface_state_pool,
622 iview->storage_surface_state);
623 }
624
625 anv_free2(&device->alloc, pAllocator, iview);
626 }
627
628
629 void anv_buffer_view_init(struct anv_buffer_view *view,
630 struct anv_device *device,
631 const VkBufferViewCreateInfo* pCreateInfo,
632 struct anv_cmd_buffer *cmd_buffer)
633 {
634 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
635
636 /* TODO: Handle the format swizzle? */
637
638 view->format = anv_get_isl_format(pCreateInfo->format,
639 VK_IMAGE_ASPECT_COLOR_BIT,
640 VK_IMAGE_TILING_LINEAR, NULL);
641 view->bo = buffer->bo;
642 view->offset = buffer->offset + pCreateInfo->offset;
643 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
644 buffer->size - view->offset : pCreateInfo->range;
645
646 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
647 view->surface_state = alloc_surface_state(device, cmd_buffer);
648
649 anv_fill_buffer_surface_state(device, view->surface_state,
650 view->format,
651 view->offset, view->range,
652 isl_format_get_layout(view->format)->bs);
653 } else {
654 view->surface_state = (struct anv_state){ 0 };
655 }
656
657 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
658 view->storage_surface_state = alloc_surface_state(device, cmd_buffer);
659
660 enum isl_format storage_format =
661 isl_has_matching_typed_storage_image_format(&device->info,
662 view->format) ?
663 isl_lower_storage_image_format(&device->info, view->format) :
664 ISL_FORMAT_RAW;
665
666 anv_fill_buffer_surface_state(device, view->storage_surface_state,
667 storage_format,
668 view->offset, view->range,
669 (storage_format == ISL_FORMAT_RAW ? 1 :
670 isl_format_get_layout(storage_format)->bs));
671
672 isl_buffer_fill_image_param(&device->isl_dev,
673 &view->storage_image_param,
674 view->format, view->range);
675 } else {
676 view->storage_surface_state = (struct anv_state){ 0 };
677 }
678 }
679
680 VkResult
681 anv_CreateBufferView(VkDevice _device,
682 const VkBufferViewCreateInfo *pCreateInfo,
683 const VkAllocationCallbacks *pAllocator,
684 VkBufferView *pView)
685 {
686 ANV_FROM_HANDLE(anv_device, device, _device);
687 struct anv_buffer_view *view;
688
689 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
690 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
691 if (!view)
692 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
693
694 anv_buffer_view_init(view, device, pCreateInfo, NULL);
695
696 *pView = anv_buffer_view_to_handle(view);
697
698 return VK_SUCCESS;
699 }
700
701 void
702 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
703 const VkAllocationCallbacks *pAllocator)
704 {
705 ANV_FROM_HANDLE(anv_device, device, _device);
706 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
707
708 if (view->surface_state.alloc_size > 0)
709 anv_state_pool_free(&device->surface_state_pool,
710 view->surface_state);
711
712 if (view->storage_surface_state.alloc_size > 0)
713 anv_state_pool_free(&device->surface_state_pool,
714 view->storage_surface_state);
715
716 anv_free2(&device->alloc, pAllocator, view);
717 }
718
719 struct anv_surface *
720 anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
721 {
722 switch (aspect_mask) {
723 case VK_IMAGE_ASPECT_COLOR_BIT:
724 /* Dragons will eat you.
725 *
726 * Meta attaches all destination surfaces as color render targets. Guess
727 * what surface the Meta Dragons really want.
728 */
729 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
730 return &image->depth_surface;
731 } else if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
732 return &image->stencil_surface;
733 } else {
734 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
735 return &image->color_surface;
736 }
737 break;
738 case VK_IMAGE_ASPECT_DEPTH_BIT:
739 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
740 return &image->depth_surface;
741 case VK_IMAGE_ASPECT_STENCIL_BIT:
742 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
743 return &image->stencil_surface;
744 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
745 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
746 * combined depth stencil formats. Specifically, it states:
747 *
748 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
749 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
750 *
751 * Image views with both depth and stencil aspects are only valid for
752 * render target attachments, in which case
753 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
754 * stencil surfaces from the underlying surface.
755 */
756 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
757 return &image->depth_surface;
758 } else {
759 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
760 return &image->stencil_surface;
761 }
762 default:
763 unreachable("image does not have aspect");
764 return NULL;
765 }
766 }