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