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