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