Merge branch 'wip/i965-separate-sampler-tex' into vulkan
[mesa.git] / src / 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 static const uint8_t anv_halign[] = {
33 [4] = HALIGN4,
34 [8] = HALIGN8,
35 [16] = HALIGN16,
36 };
37
38 static const uint8_t anv_valign[] = {
39 [4] = VALIGN4,
40 [8] = VALIGN8,
41 [16] = VALIGN16,
42 };
43
44 static const uint8_t anv_surf_type_from_image_type[] = {
45 [VK_IMAGE_TYPE_1D] = SURFTYPE_1D,
46 [VK_IMAGE_TYPE_2D] = SURFTYPE_2D,
47 [VK_IMAGE_TYPE_3D] = SURFTYPE_3D,
48 };
49
50 static const struct anv_image_view_info
51 anv_image_view_info_table[] = {
52 #define INFO(s, ...) { .surface_type = s, __VA_ARGS__ }
53 [VK_IMAGE_VIEW_TYPE_1D] = INFO(SURFTYPE_1D),
54 [VK_IMAGE_VIEW_TYPE_2D] = INFO(SURFTYPE_2D),
55 [VK_IMAGE_VIEW_TYPE_3D] = INFO(SURFTYPE_3D),
56 [VK_IMAGE_VIEW_TYPE_CUBE] = INFO(SURFTYPE_CUBE, .is_cube = 1),
57 [VK_IMAGE_VIEW_TYPE_1D_ARRAY] = INFO(SURFTYPE_1D, .is_array = 1),
58 [VK_IMAGE_VIEW_TYPE_2D_ARRAY] = INFO(SURFTYPE_2D, .is_array = 1),
59 [VK_IMAGE_VIEW_TYPE_CUBE_ARRAY] = INFO(SURFTYPE_CUBE, .is_array = 1, .is_cube = 1),
60 #undef INFO
61 };
62
63 struct anv_image_view_info
64 anv_image_view_info_for_vk_image_view_type(VkImageViewType type)
65 {
66 return anv_image_view_info_table[type];
67 }
68
69 static const struct anv_surf_type_limits {
70 int32_t width;
71 int32_t height;
72 int32_t depth;
73 } anv_surf_type_limits[] = {
74 [SURFTYPE_1D] = {16384, 1, 2048},
75 [SURFTYPE_2D] = {16384, 16384, 2048},
76 [SURFTYPE_3D] = {2048, 2048, 2048},
77 [SURFTYPE_CUBE] = {16384, 16384, 340},
78 [SURFTYPE_BUFFER] = {128, 16384, 64},
79 [SURFTYPE_STRBUF] = {128, 16384, 64},
80 };
81
82 static const struct anv_tile_info {
83 /**
84 * Alignment for RENDER_SURFACE_STATE.SurfaceBaseAddress.
85 *
86 * To simplify calculations, the alignments defined in the table are
87 * sometimes larger than required. For example, Skylake requires that X and
88 * Y tiled buffers be aligned to 4K, but Broadwell permits smaller
89 * alignment. We choose 4K to accomodate both chipsets. The alignment of
90 * a linear buffer depends on its element type and usage. Linear depth
91 * buffers have the largest alignment, 64B, so we choose that for all linear
92 * buffers.
93 */
94 uint32_t surface_alignment;
95 } anv_tile_info_table[] = {
96 [ISL_TILING_LINEAR] = { 64 },
97 [ISL_TILING_X] = { 4096 },
98 [ISL_TILING_Y] = { 4096 },
99 [ISL_TILING_Yf] = { 4096 },
100 [ISL_TILING_Ys] = { 4096 },
101 [ISL_TILING_W] = { 4096 },
102 };
103
104 static enum isl_tiling
105 anv_image_choose_tiling(const struct anv_image_create_info *anv_info)
106 {
107 if (anv_info->force_tiling)
108 return anv_info->tiling;
109
110 /* The Sandybridge PRM says that the stencil buffer "is supported
111 * only in Tile W memory".
112 */
113
114 switch (anv_info->vk_info->tiling) {
115 case VK_IMAGE_TILING_LINEAR:
116 assert(anv_info->vk_info->format != VK_FORMAT_S8_UINT);
117 return ISL_TILING_LINEAR;
118 case VK_IMAGE_TILING_OPTIMAL:
119 if (unlikely(anv_info->vk_info->format == VK_FORMAT_S8_UINT)) {
120 return ISL_TILING_W;
121 } else {
122 return ISL_TILING_Y;
123 }
124 default:
125 assert(!"bad VKImageTiling");
126 return ISL_TILING_LINEAR;
127 }
128 }
129
130
131 /**
132 * The \a format argument is required and overrides any format in
133 * struct anv_image_create_info.
134 */
135 static VkResult
136 anv_image_make_surface(const struct anv_device *dev,
137 const struct anv_image_create_info *create_info,
138 const struct anv_format *format,
139 uint64_t *inout_image_size,
140 uint32_t *inout_image_alignment,
141 struct anv_surface *out_surface)
142 {
143 /* See RENDER_SURFACE_STATE.SurfaceQPitch */
144 static const uint16_t min_qpitch UNUSED = 0x4;
145 static const uint16_t max_qpitch UNUSED = 0x1ffc;
146
147 const VkExtent3D *restrict extent = &create_info->vk_info->extent;
148 const uint32_t levels = create_info->vk_info->mipLevels;
149 const uint32_t array_size = create_info->vk_info->arraySize;
150 const enum isl_tiling tiling = anv_image_choose_tiling(create_info);
151
152 const struct anv_tile_info *tile_info =
153 &anv_tile_info_table[tiling];
154
155 const uint32_t bs = format->isl_layout->bs;
156 const uint32_t bw = format->isl_layout->bw;
157 const uint32_t bh = format->isl_layout->bh;
158
159 struct isl_extent2d tile_extent;
160 isl_tiling_get_extent(&dev->isl_dev, tiling, bs, &tile_extent);
161
162 const uint32_t i = MAX(4, bw); /* FINISHME: Stop hardcoding subimage alignment */
163 const uint32_t j = MAX(4, bh); /* FINISHME: Stop hardcoding subimage alignment */
164 assert(i == 4 || i == 8 || i == 16);
165 assert(j == 4 || j == 8 || j == 16);
166
167 uint16_t qpitch = min_qpitch;
168 uint32_t mt_width = 0;
169 uint32_t mt_height = 0;
170
171 switch (create_info->vk_info->imageType) {
172 case VK_IMAGE_TYPE_1D:
173 /* From the Broadwell PRM >> Memory Views >> Common Surface Formats >>
174 * Surface Layout >> 1D Surfaces:
175 *
176 * One-dimensional surfaces are identical to 2D surfaces with height of one.
177 *
178 * So fallthrough...
179 */
180 case VK_IMAGE_TYPE_2D: {
181 const uint32_t w0 = align_u32(extent->width, i);
182 const uint32_t h0 = align_u32(extent->height, j);
183
184 if (levels == 1 && array_size == 1) {
185 qpitch = min_qpitch;
186 mt_width = w0;
187 mt_height = h0;
188 } else {
189 uint32_t w1 = align_u32(anv_minify(extent->width, 1), i);
190 uint32_t h1 = align_u32(anv_minify(extent->height, 1), j);
191 uint32_t w2 = align_u32(anv_minify(extent->width, 2), i);
192
193 /* The QPitch equation is found in the Broadwell PRM >> Volume 5: Memory
194 * Views >> Common Surface Formats >> Surface Layout >> 2D Surfaces >>
195 * Surface Arrays >> For All Surface Other Than Separate Stencil Buffer:
196 */
197 assert(bh ==1 || bh == 4);
198 qpitch = (h0 + h1 + 11 * j) / bh;
199 mt_width = MAX(w0, w1 + w2);
200 mt_height = array_size * qpitch;
201 }
202 break;
203 }
204 case VK_IMAGE_TYPE_3D:
205 /* The layout of 3D surfaces is described by the Broadwell PRM >>
206 * Volume 5: Memory Views >> Common Surface Formats >> Surface Layout >>
207 * 3D Surfaces.
208 */
209 for (uint32_t l = 0; l < levels; ++l) {
210 const uint32_t w_l = align_u32(anv_minify(extent->width, l), i);
211 const uint32_t h_l = align_u32(anv_minify(extent->height, l), j);
212 const uint32_t d_l = anv_minify(extent->depth, l);
213
214 const uint32_t max_layers_horiz = MIN(d_l, 1u << l);
215 const uint32_t max_layers_vert = align_u32(d_l, 1u << l) / (1u << l);
216
217 mt_width = MAX(mt_width, w_l * max_layers_horiz);
218 mt_height += h_l * max_layers_vert;
219 }
220 break;
221 default:
222 unreachable(!"bad VkImageType");
223 }
224
225 assert(qpitch >= min_qpitch);
226 if (qpitch > max_qpitch) {
227 anv_loge("image qpitch > 0x%x\n", max_qpitch);
228 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
229 }
230
231 /* From the Broadwell PRM, RENDER_SURFACE_STATE.SurfaceQpitch:
232 *
233 * This field must be set an integer multiple of the Surface Vertical
234 * Alignment.
235 */
236 assert(anv_is_aligned(qpitch, j));
237
238 uint32_t stride = align_u32(mt_width * bs / bw, tile_extent.width);
239 if (create_info->stride > 0)
240 stride = create_info->stride;
241
242 /* The padding requirement is found in the Broadwell PRM >> Volume 5: Memory
243 * Views >> Common Surface Formats >> Surface Padding Requirements >>
244 * Sampling Engine Surfaces >> Buffer Padding Requirements:
245 */
246 const uint32_t mem_rows = align_u32(mt_height / bh, 2 * bh);
247 const uint32_t size = stride * align_u32(mem_rows, tile_extent.height);
248 const uint32_t offset = align_u32(*inout_image_size,
249 tile_info->surface_alignment);
250
251 *inout_image_size = offset + size;
252 *inout_image_alignment = MAX(*inout_image_alignment,
253 tile_info->surface_alignment);
254
255 *out_surface = (struct anv_surface) {
256 .offset = offset,
257 .stride = stride,
258 .tiling = tiling,
259 .qpitch = qpitch,
260 .h_align = i,
261 .v_align = j,
262 };
263
264 return VK_SUCCESS;
265 }
266
267 static VkImageUsageFlags
268 anv_image_get_full_usage(const VkImageCreateInfo *info)
269 {
270 VkImageUsageFlags usage = info->usage;
271
272 if (usage & VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT) {
273 /* Meta will transfer from the image by binding it as a texture. */
274 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
275 }
276
277 if (usage & VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT) {
278 /* Meta will transfer to the image by binding it as a color attachment,
279 * even if the image format is not a color format.
280 */
281 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
282 }
283
284 return usage;
285 }
286
287 VkResult
288 anv_image_create(VkDevice _device,
289 const struct anv_image_create_info *create_info,
290 VkImage *pImage)
291 {
292 ANV_FROM_HANDLE(anv_device, device, _device);
293 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
294 const VkExtent3D *restrict extent = &pCreateInfo->extent;
295 struct anv_image *image = NULL;
296 VkResult r;
297
298 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
299
300 anv_assert(pCreateInfo->mipLevels > 0);
301 anv_assert(pCreateInfo->arraySize > 0);
302 anv_assert(pCreateInfo->samples == 1);
303 anv_assert(pCreateInfo->extent.width > 0);
304 anv_assert(pCreateInfo->extent.height > 0);
305 anv_assert(pCreateInfo->extent.depth > 0);
306
307 /* TODO(chadv): How should we validate inputs? */
308 const uint8_t surf_type =
309 anv_surf_type_from_image_type[pCreateInfo->imageType];
310
311 const struct anv_surf_type_limits *limits =
312 &anv_surf_type_limits[surf_type];
313
314 /* Errors should be caught by VkImageFormatProperties. */
315 assert(extent->width <= limits->width);
316 assert(extent->height <= limits->height);
317 assert(extent->depth <= limits->depth);
318
319 image = anv_device_alloc(device, sizeof(*image), 8,
320 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
321 if (!image)
322 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
323
324 memset(image, 0, sizeof(*image));
325 image->type = pCreateInfo->imageType;
326 image->extent = pCreateInfo->extent;
327 image->format = anv_format_for_vk_format(pCreateInfo->format);
328 image->levels = pCreateInfo->mipLevels;
329 image->array_size = pCreateInfo->arraySize;
330 image->usage = anv_image_get_full_usage(pCreateInfo);
331 image->surface_type = surf_type;
332
333 if (image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
334 VK_IMAGE_USAGE_STORAGE_BIT)) {
335 image->needs_nonrt_surface_state = true;
336 }
337
338 if (image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
339 image->needs_color_rt_surface_state = true;
340 }
341
342 if (likely(anv_format_is_color(image->format))) {
343 r = anv_image_make_surface(device, create_info, image->format,
344 &image->size, &image->alignment,
345 &image->color_surface);
346 if (r != VK_SUCCESS)
347 goto fail;
348 } else {
349 if (image->format->depth_format) {
350 r = anv_image_make_surface(device, create_info, image->format,
351 &image->size, &image->alignment,
352 &image->depth_surface);
353 if (r != VK_SUCCESS)
354 goto fail;
355 }
356
357 if (image->format->has_stencil) {
358 r = anv_image_make_surface(device, create_info, anv_format_s8_uint,
359 &image->size, &image->alignment,
360 &image->stencil_surface);
361 if (r != VK_SUCCESS)
362 goto fail;
363 }
364 }
365
366 *pImage = anv_image_to_handle(image);
367
368 return VK_SUCCESS;
369
370 fail:
371 if (image)
372 anv_device_free(device, image);
373
374 return r;
375 }
376
377 VkResult
378 anv_CreateImage(VkDevice device,
379 const VkImageCreateInfo *pCreateInfo,
380 VkImage *pImage)
381 {
382 return anv_image_create(device,
383 &(struct anv_image_create_info) {
384 .vk_info = pCreateInfo,
385 },
386 pImage);
387 }
388
389 void
390 anv_DestroyImage(VkDevice _device, VkImage _image)
391 {
392 ANV_FROM_HANDLE(anv_device, device, _device);
393
394 anv_device_free(device, anv_image_from_handle(_image));
395 }
396
397 static void
398 anv_surface_get_subresource_layout(struct anv_image *image,
399 struct anv_surface *surface,
400 const VkImageSubresource *subresource,
401 VkSubresourceLayout *layout)
402 {
403 /* If we are on a non-zero mip level or array slice, we need to
404 * calculate a real offset.
405 */
406 anv_assert(subresource->mipLevel == 0);
407 anv_assert(subresource->arrayLayer == 0);
408
409 layout->offset = surface->offset;
410 layout->rowPitch = surface->stride;
411 layout->depthPitch = surface->qpitch;
412
413 /* FINISHME: We really shouldn't be doing this calculation here */
414 if (image->array_size > 1)
415 layout->size = surface->qpitch * image->array_size;
416 else
417 layout->size = surface->stride * image->extent.height;
418 }
419
420 VkResult anv_GetImageSubresourceLayout(
421 VkDevice device,
422 VkImage _image,
423 const VkImageSubresource* pSubresource,
424 VkSubresourceLayout* pLayout)
425 {
426 ANV_FROM_HANDLE(anv_image, image, _image);
427
428 switch (pSubresource->aspect) {
429 case VK_IMAGE_ASPECT_COLOR:
430 anv_surface_get_subresource_layout(image, &image->color_surface,
431 pSubresource, pLayout);
432 break;
433 case VK_IMAGE_ASPECT_DEPTH:
434 anv_surface_get_subresource_layout(image, &image->depth_surface,
435 pSubresource, pLayout);
436 break;
437 case VK_IMAGE_ASPECT_STENCIL:
438 anv_surface_get_subresource_layout(image, &image->stencil_surface,
439 pSubresource, pLayout);
440 break;
441 default:
442 return vk_error(VK_UNSUPPORTED);
443 }
444
445 return VK_SUCCESS;
446 }
447
448 VkResult
449 anv_validate_CreateImageView(VkDevice _device,
450 const VkImageViewCreateInfo *pCreateInfo,
451 VkImageView *pView)
452 {
453 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
454 const VkImageSubresourceRange *subresource;
455 const struct anv_image_view_info *view_info;
456 const struct anv_format *view_format_info;
457
458 /* Validate structure type before dereferencing it. */
459 assert(pCreateInfo);
460 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
461 subresource = &pCreateInfo->subresourceRange;
462
463 /* Validate viewType is in range before using it. */
464 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
465 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
466 view_info = &anv_image_view_info_table[pCreateInfo->viewType];
467
468 /* Validate format is in range before using it. */
469 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
470 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
471 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
472
473 /* Validate channel swizzles. */
474 assert(pCreateInfo->channels.r >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
475 assert(pCreateInfo->channels.r <= VK_CHANNEL_SWIZZLE_END_RANGE);
476 assert(pCreateInfo->channels.g >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
477 assert(pCreateInfo->channels.g <= VK_CHANNEL_SWIZZLE_END_RANGE);
478 assert(pCreateInfo->channels.b >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
479 assert(pCreateInfo->channels.b <= VK_CHANNEL_SWIZZLE_END_RANGE);
480 assert(pCreateInfo->channels.a >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
481 assert(pCreateInfo->channels.a <= VK_CHANNEL_SWIZZLE_END_RANGE);
482
483 /* Validate subresource. */
484 assert(subresource->aspectMask != 0);
485 assert(subresource->mipLevels > 0);
486 assert(subresource->arraySize > 0);
487 assert(subresource->baseMipLevel < image->levels);
488 assert(subresource->baseMipLevel + subresource->mipLevels <= image->levels);
489 assert(subresource->baseArrayLayer < image->array_size);
490 assert(subresource->baseArrayLayer + subresource->arraySize <= image->array_size);
491 assert(pView);
492
493 if (view_info->is_cube) {
494 assert(subresource->baseArrayLayer % 6 == 0);
495 assert(subresource->arraySize % 6 == 0);
496 }
497
498 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
499 | VK_IMAGE_ASPECT_STENCIL_BIT;
500
501 /* Validate format. */
502 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
503 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
504 assert(!image->format->depth_format);
505 assert(!image->format->has_stencil);
506 assert(!view_format_info->depth_format);
507 assert(!view_format_info->has_stencil);
508 assert(view_format_info->isl_layout->bs ==
509 image->format->isl_layout->bs);
510 } else if (subresource->aspectMask & ds_flags) {
511 assert((subresource->aspectMask & ~ds_flags) == 0);
512
513 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
514 assert(image->format->depth_format);
515 assert(view_format_info->depth_format);
516 assert(view_format_info->isl_layout->bs ==
517 image->format->isl_layout->bs);
518 }
519
520 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL) {
521 /* FINISHME: Is it legal to have an R8 view of S8? */
522 assert(image->format->has_stencil);
523 assert(view_format_info->has_stencil);
524 }
525 } else {
526 assert(!"bad VkImageSubresourceRange::aspectFlags");
527 }
528
529 return anv_CreateImageView(_device, pCreateInfo, pView);
530 }
531
532 void
533 anv_image_view_init(struct anv_image_view *iview,
534 struct anv_device *device,
535 const VkImageViewCreateInfo* pCreateInfo,
536 struct anv_cmd_buffer *cmd_buffer)
537 {
538 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
539 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
540
541 assert(range->arraySize > 0);
542 assert(range->baseMipLevel < image->levels);
543 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
544 VK_IMAGE_USAGE_STORAGE_BIT |
545 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
546 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
547
548 switch (image->type) {
549 default:
550 unreachable("bad VkImageType");
551 case VK_IMAGE_TYPE_1D:
552 case VK_IMAGE_TYPE_2D:
553 assert(range->baseArrayLayer + range->arraySize - 1 <= image->array_size);
554 break;
555 case VK_IMAGE_TYPE_3D:
556 assert(range->baseArrayLayer + range->arraySize - 1
557 <= anv_minify(image->extent.depth, range->baseMipLevel));
558 break;
559 }
560
561 switch (device->info.gen) {
562 case 7:
563 gen7_image_view_init(iview, device, pCreateInfo, cmd_buffer);
564 break;
565 case 8:
566 gen8_image_view_init(iview, device, pCreateInfo, cmd_buffer);
567 break;
568 default:
569 unreachable("unsupported gen\n");
570 }
571 }
572
573 VkResult
574 anv_CreateImageView(VkDevice _device,
575 const VkImageViewCreateInfo *pCreateInfo,
576 VkImageView *pView)
577 {
578 ANV_FROM_HANDLE(anv_device, device, _device);
579 struct anv_image_view *view;
580
581 view = anv_device_alloc(device, sizeof(*view), 8,
582 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
583 if (view == NULL)
584 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
585
586 anv_image_view_init(view, device, pCreateInfo, NULL);
587
588 *pView = anv_image_view_to_handle(view);
589
590 return VK_SUCCESS;
591 }
592
593 static void
594 anv_image_view_destroy(struct anv_device *device,
595 struct anv_image_view *iview)
596 {
597 if (iview->image->needs_color_rt_surface_state) {
598 anv_state_pool_free(&device->surface_state_pool,
599 iview->color_rt_surface_state);
600 }
601
602 if (iview->image->needs_nonrt_surface_state) {
603 anv_state_pool_free(&device->surface_state_pool,
604 iview->nonrt_surface_state);
605 }
606
607 anv_device_free(device, iview);
608 }
609
610 void
611 anv_DestroyImageView(VkDevice _device, VkImageView _iview)
612 {
613 ANV_FROM_HANDLE(anv_device, device, _device);
614 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
615
616 anv_image_view_destroy(device, iview);
617 }
618
619 struct anv_surface *
620 anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
621 {
622 switch (aspect_mask) {
623 case VK_IMAGE_ASPECT_COLOR_BIT:
624 /* Dragons will eat you.
625 *
626 * Meta attaches all destination surfaces as color render targets. Guess
627 * what surface the Meta Dragons really want.
628 */
629 if (image->format->depth_format && image->format->has_stencil) {
630 anv_finishme("combined depth stencil formats");
631 return &image->depth_surface;
632 } else if (image->format->depth_format) {
633 return &image->depth_surface;
634 } else if (image->format->has_stencil) {
635 return &image->stencil_surface;
636 } else {
637 return &image->color_surface;
638 }
639 break;
640 case VK_IMAGE_ASPECT_DEPTH_BIT:
641 assert(image->format->depth_format);
642 return &image->depth_surface;
643 case VK_IMAGE_ASPECT_STENCIL_BIT:
644 assert(image->format->has_stencil);
645 return &image->stencil_surface;
646 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
647 if (image->format->depth_format && image->format->has_stencil) {
648 /* FINISHME: The Vulkan spec (git a511ba2) requires support for combined
649 * depth stencil formats. Specifically, it states:
650 *
651 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
652 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
653 */
654 anv_finishme("combined depthstencil aspect");
655 return &image->depth_surface;
656 } else if (image->format->depth_format) {
657 return &image->depth_surface;
658 } else if (image->format->has_stencil) {
659 return &image->stencil_surface;
660 }
661 /* fallthrough */
662 default:
663 unreachable("image does not have aspect");
664 return NULL;
665 }
666 }
667
668 #if 0
669 VkImageAspectFlags aspect_mask = 0;
670 if (format->depth_format)
671 aspect_mask |= VK_IMAGE_ASPECT_DEPTH_BIT;
672 if (format->has_stencil)
673 aspect_mask |= VK_IMAGE_ASPECT_STENCIL_BIT;
674 if (!aspect_mask)
675 aspect_mask |= VK_IMAGE_ASPECT_COLOR_BIT;
676
677 anv_image_view_init(iview, device,
678 &(VkImageViewCreateInfo) {
679 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
680 .image = info->image,
681 .viewType = VK_IMAGE_VIEW_TYPE_2D,
682 .format = info->format,
683 .channels = {
684 .r = VK_CHANNEL_SWIZZLE_R,
685 .g = VK_CHANNEL_SWIZZLE_G,
686 .b = VK_CHANNEL_SWIZZLE_B,
687 .a = VK_CHANNEL_SWIZZLE_A,
688 },
689 .subresourceRange = {
690 .aspectMask = aspect_mask,
691 .baseMipLevel = info->mipLevel,
692 .mipLevels = 1,
693 .baseArrayLayer = info->baseArraySlice,
694 .arraySize = info->arraySize,
695 },
696 },
697 NULL);
698 #endif