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