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