anv: Rework fences to work more like BO semaphores
[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 #include <sys/mman.h>
30
31 #include "anv_private.h"
32 #include "util/debug.h"
33
34 #include "vk_format_info.h"
35
36 /**
37 * Exactly one bit must be set in \a aspect.
38 */
39 static isl_surf_usage_flags_t
40 choose_isl_surf_usage(VkImageCreateFlags vk_create_flags,
41 VkImageUsageFlags vk_usage,
42 VkImageAspectFlags aspect)
43 {
44 isl_surf_usage_flags_t isl_usage = 0;
45
46 if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
47 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
48
49 if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
50 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
51
52 if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
53 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
54
55 if (vk_create_flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
56 isl_usage |= ISL_SURF_USAGE_CUBE_BIT;
57
58 /* Even if we're only using it for transfer operations, clears to depth and
59 * stencil images happen as depth and stencil so they need the right ISL
60 * usage bits or else things will fall apart.
61 */
62 switch (aspect) {
63 case VK_IMAGE_ASPECT_DEPTH_BIT:
64 isl_usage |= ISL_SURF_USAGE_DEPTH_BIT;
65 break;
66 case VK_IMAGE_ASPECT_STENCIL_BIT:
67 isl_usage |= ISL_SURF_USAGE_STENCIL_BIT;
68 break;
69 case VK_IMAGE_ASPECT_COLOR_BIT:
70 break;
71 default:
72 unreachable("bad VkImageAspect");
73 }
74
75 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
76 /* blorp implements transfers by sampling from the source image. */
77 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
78 }
79
80 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT &&
81 aspect == VK_IMAGE_ASPECT_COLOR_BIT) {
82 /* blorp implements transfers by rendering into the destination image.
83 * Only request this with color images, as we deal with depth/stencil
84 * formats differently. */
85 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
86 }
87
88 return isl_usage;
89 }
90
91 /**
92 * Exactly one bit must be set in \a aspect.
93 */
94 static struct anv_surface *
95 get_surface(struct anv_image *image, VkImageAspectFlags aspect)
96 {
97 switch (aspect) {
98 default:
99 unreachable("bad VkImageAspect");
100 case VK_IMAGE_ASPECT_COLOR_BIT:
101 return &image->color_surface;
102 case VK_IMAGE_ASPECT_DEPTH_BIT:
103 return &image->depth_surface;
104 case VK_IMAGE_ASPECT_STENCIL_BIT:
105 return &image->stencil_surface;
106 }
107 }
108
109 static void
110 add_surface(struct anv_image *image, struct anv_surface *surf)
111 {
112 assert(surf->isl.size > 0); /* isl surface must be initialized */
113
114 surf->offset = align_u32(image->size, surf->isl.alignment);
115 image->size = surf->offset + surf->isl.size;
116 image->alignment = MAX2(image->alignment, surf->isl.alignment);
117 }
118
119 /**
120 * For color images that have an auxiliary surface, request allocation for an
121 * additional buffer that mainly stores fast-clear values. Use of this buffer
122 * allows us to access the image's subresources while being aware of their
123 * fast-clear values in non-trivial cases (e.g., outside of a render pass in
124 * which a fast clear has occurred).
125 *
126 * For the purpose of discoverability, the algorithm used to manage this buffer
127 * is described here. A clear value in this buffer is updated when a fast clear
128 * is performed on a subresource. One of two synchronization operations is
129 * performed in order for a following memory access to use the fast-clear
130 * value:
131 * a. Copy the value from the buffer to the surface state object used for
132 * reading. This is done implicitly when the value is the clear value
133 * predetermined to be the default in other surface state objects. This
134 * is currently only done explicitly for the operation below.
135 * b. Do (a) and use the surface state object to resolve the subresource.
136 * This is only done during layout transitions for decent performance.
137 *
138 * With the above scheme, we can fast-clear whenever the hardware allows except
139 * for two cases in which synchronization becomes impossible or undesirable:
140 * * The subresource is in the GENERAL layout and is cleared to a value
141 * other than the special default value.
142 *
143 * Performing a synchronization operation in order to read from the
144 * subresource is undesirable in this case. Firstly, b) is not an option
145 * because a layout transition isn't required between a write and read of
146 * an image in the GENERAL layout. Secondly, it's undesirable to do a)
147 * explicitly because it would require large infrastructural changes. The
148 * Vulkan API supports us in deciding not to optimize this layout by
149 * stating that using this layout may cause suboptimal performance. NOTE:
150 * the auxiliary buffer must always be enabled to support a) implicitly.
151 *
152 *
153 * * For the given miplevel, only some of the layers are cleared at once.
154 *
155 * If the user clears each layer to a different value, then tries to
156 * render to multiple layers at once, we have no ability to perform a
157 * synchronization operation in between. a) is not helpful because the
158 * object can only hold one clear value. b) is not an option because a
159 * layout transition isn't required in this case.
160 */
161 static void
162 add_fast_clear_state_buffer(struct anv_image *image,
163 const struct anv_device *device)
164 {
165 assert(image && device);
166 assert(image->aux_surface.isl.size > 0 &&
167 image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
168
169 /* The offset to the buffer of clear values must be dword-aligned for GPU
170 * memcpy operations. It is located immediately after the auxiliary surface.
171 */
172
173 /* Tiled images are guaranteed to be 4K aligned, so the image alignment
174 * should also be dword-aligned.
175 */
176 assert(image->alignment % 4 == 0);
177
178 /* Auxiliary buffers should be a multiple of 4K, so the start of the clear
179 * values buffer should already be dword-aligned.
180 */
181 assert(image->aux_surface.isl.size % 4 == 0);
182
183 /* This buffer should be at the very end of the image. */
184 assert(image->size ==
185 image->aux_surface.offset + image->aux_surface.isl.size);
186
187 const unsigned entry_size = anv_fast_clear_state_entry_size(device);
188 /* There's no padding between entries, so ensure that they're always a
189 * multiple of 32 bits in order to enable GPU memcpy operations.
190 */
191 assert(entry_size % 4 == 0);
192 image->size += entry_size * anv_image_aux_levels(image);
193 }
194
195 /**
196 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
197 * image's memory requirements (that is, the image's size and alignment).
198 *
199 * Exactly one bit must be set in \a aspect.
200 */
201 static VkResult
202 make_surface(const struct anv_device *dev,
203 struct anv_image *image,
204 const struct anv_image_create_info *anv_info,
205 VkImageAspectFlags aspect)
206 {
207 const VkImageCreateInfo *vk_info = anv_info->vk_info;
208 bool ok UNUSED;
209
210 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
211 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
212 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
213 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
214 };
215
216 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
217 * result with an optionally provided ISL tiling argument.
218 */
219 isl_tiling_flags_t tiling_flags =
220 (vk_info->tiling == VK_IMAGE_TILING_LINEAR) ?
221 ISL_TILING_LINEAR_BIT : ISL_TILING_ANY_MASK;
222
223 if (anv_info->isl_tiling_flags)
224 tiling_flags &= anv_info->isl_tiling_flags;
225
226 assert(tiling_flags);
227
228 struct anv_surface *anv_surf = get_surface(image, aspect);
229
230 image->extent = anv_sanitize_image_extent(vk_info->imageType,
231 vk_info->extent);
232
233 enum isl_format format = anv_get_isl_format(&dev->info, vk_info->format,
234 aspect, vk_info->tiling);
235 assert(format != ISL_FORMAT_UNSUPPORTED);
236
237 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
238 .dim = vk_to_isl_surf_dim[vk_info->imageType],
239 .format = format,
240 .width = image->extent.width,
241 .height = image->extent.height,
242 .depth = image->extent.depth,
243 .levels = vk_info->mipLevels,
244 .array_len = vk_info->arrayLayers,
245 .samples = vk_info->samples,
246 .min_alignment = 0,
247 .row_pitch = anv_info->stride,
248 .usage = choose_isl_surf_usage(vk_info->flags, image->usage, aspect),
249 .tiling_flags = tiling_flags);
250
251 /* isl_surf_init() will fail only if provided invalid input. Invalid input
252 * is illegal in Vulkan.
253 */
254 assert(ok);
255
256 add_surface(image, anv_surf);
257
258 /* Add a HiZ surface to a depth buffer that will be used for rendering.
259 */
260 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
261 /* We don't advertise that depth buffers could be used as storage
262 * images.
263 */
264 assert(!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT));
265
266 /* Allow the user to control HiZ enabling. Disable by default on gen7
267 * because resolves are not currently implemented pre-BDW.
268 */
269 if (!(image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
270 /* It will never be used as an attachment, HiZ is pointless. */
271 } else if (dev->info.gen == 7) {
272 anv_perf_warn("Implement gen7 HiZ");
273 } else if (vk_info->mipLevels > 1) {
274 anv_perf_warn("Enable multi-LOD HiZ");
275 } else if (vk_info->arrayLayers > 1) {
276 anv_perf_warn("Implement multi-arrayLayer HiZ clears and resolves");
277 } else if (dev->info.gen == 8 && vk_info->samples > 1) {
278 anv_perf_warn("Enable gen8 multisampled HiZ");
279 } else if (!unlikely(INTEL_DEBUG & DEBUG_NO_HIZ)) {
280 assert(image->aux_surface.isl.size == 0);
281 ok = isl_surf_get_hiz_surf(&dev->isl_dev, &image->depth_surface.isl,
282 &image->aux_surface.isl);
283 assert(ok);
284 add_surface(image, &image->aux_surface);
285 image->aux_usage = ISL_AUX_USAGE_HIZ;
286 }
287 } else if (aspect == VK_IMAGE_ASPECT_COLOR_BIT && vk_info->samples == 1) {
288 if (!unlikely(INTEL_DEBUG & DEBUG_NO_RBC)) {
289 assert(image->aux_surface.isl.size == 0);
290 ok = isl_surf_get_ccs_surf(&dev->isl_dev, &anv_surf->isl,
291 &image->aux_surface.isl, 0);
292 if (ok) {
293
294 /* Disable CCS when it is not useful (i.e., when you can't render
295 * to the image with CCS enabled).
296 */
297 if (!isl_format_supports_rendering(&dev->info, format)) {
298 /* While it may be technically possible to enable CCS for this
299 * image, we currently don't have things hooked up to get it
300 * working.
301 */
302 anv_perf_warn("This image format doesn't support rendering. "
303 "Not allocating an CCS buffer.");
304 image->aux_surface.isl.size = 0;
305 return VK_SUCCESS;
306 }
307
308 add_surface(image, &image->aux_surface);
309 add_fast_clear_state_buffer(image, dev);
310
311 /* For images created without MUTABLE_FORMAT_BIT set, we know that
312 * they will always be used with the original format. In
313 * particular, they will always be used with a format that
314 * supports color compression. If it's never used as a storage
315 * image, then it will only be used through the sampler or the as
316 * a render target. This means that it's safe to just leave
317 * compression on at all times for these formats.
318 */
319 if (!(vk_info->usage & VK_IMAGE_USAGE_STORAGE_BIT) &&
320 !(vk_info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) &&
321 isl_format_supports_ccs_e(&dev->info, format)) {
322 image->aux_usage = ISL_AUX_USAGE_CCS_E;
323 }
324 }
325 }
326 } else if (aspect == VK_IMAGE_ASPECT_COLOR_BIT && vk_info->samples > 1) {
327 assert(image->aux_surface.isl.size == 0);
328 assert(!(vk_info->usage & VK_IMAGE_USAGE_STORAGE_BIT));
329 ok = isl_surf_get_mcs_surf(&dev->isl_dev, &anv_surf->isl,
330 &image->aux_surface.isl);
331 if (ok) {
332 add_surface(image, &image->aux_surface);
333 add_fast_clear_state_buffer(image, dev);
334 image->aux_usage = ISL_AUX_USAGE_MCS;
335 }
336 }
337
338 return VK_SUCCESS;
339 }
340
341 VkResult
342 anv_image_create(VkDevice _device,
343 const struct anv_image_create_info *create_info,
344 const VkAllocationCallbacks* alloc,
345 VkImage *pImage)
346 {
347 ANV_FROM_HANDLE(anv_device, device, _device);
348 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
349 struct anv_image *image = NULL;
350 VkResult r;
351
352 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
353
354 anv_assert(pCreateInfo->mipLevels > 0);
355 anv_assert(pCreateInfo->arrayLayers > 0);
356 anv_assert(pCreateInfo->samples > 0);
357 anv_assert(pCreateInfo->extent.width > 0);
358 anv_assert(pCreateInfo->extent.height > 0);
359 anv_assert(pCreateInfo->extent.depth > 0);
360
361 image = vk_zalloc2(&device->alloc, alloc, sizeof(*image), 8,
362 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
363 if (!image)
364 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
365
366 image->type = pCreateInfo->imageType;
367 image->extent = pCreateInfo->extent;
368 image->vk_format = pCreateInfo->format;
369 image->aspects = vk_format_aspects(image->vk_format);
370 image->levels = pCreateInfo->mipLevels;
371 image->array_size = pCreateInfo->arrayLayers;
372 image->samples = pCreateInfo->samples;
373 image->usage = pCreateInfo->usage;
374 image->tiling = pCreateInfo->tiling;
375 image->aux_usage = ISL_AUX_USAGE_NONE;
376
377 uint32_t b;
378 for_each_bit(b, image->aspects) {
379 r = make_surface(device, image, create_info, (1 << b));
380 if (r != VK_SUCCESS)
381 goto fail;
382 }
383
384 *pImage = anv_image_to_handle(image);
385
386 return VK_SUCCESS;
387
388 fail:
389 if (image)
390 vk_free2(&device->alloc, alloc, image);
391
392 return r;
393 }
394
395 VkResult
396 anv_CreateImage(VkDevice device,
397 const VkImageCreateInfo *pCreateInfo,
398 const VkAllocationCallbacks *pAllocator,
399 VkImage *pImage)
400 {
401 return anv_image_create(device,
402 &(struct anv_image_create_info) {
403 .vk_info = pCreateInfo,
404 },
405 pAllocator,
406 pImage);
407 }
408
409 void
410 anv_DestroyImage(VkDevice _device, VkImage _image,
411 const VkAllocationCallbacks *pAllocator)
412 {
413 ANV_FROM_HANDLE(anv_device, device, _device);
414 ANV_FROM_HANDLE(anv_image, image, _image);
415
416 if (!image)
417 return;
418
419 vk_free2(&device->alloc, pAllocator, image);
420 }
421
422 VkResult anv_BindImageMemory(
423 VkDevice _device,
424 VkImage _image,
425 VkDeviceMemory _memory,
426 VkDeviceSize memoryOffset)
427 {
428 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
429 ANV_FROM_HANDLE(anv_image, image, _image);
430
431 if (mem == NULL) {
432 image->bo = NULL;
433 image->offset = 0;
434 return VK_SUCCESS;
435 }
436
437 image->bo = mem->bo;
438 image->offset = memoryOffset;
439
440 return VK_SUCCESS;
441 }
442
443 static void
444 anv_surface_get_subresource_layout(struct anv_image *image,
445 struct anv_surface *surface,
446 const VkImageSubresource *subresource,
447 VkSubresourceLayout *layout)
448 {
449 /* If we are on a non-zero mip level or array slice, we need to
450 * calculate a real offset.
451 */
452 anv_assert(subresource->mipLevel == 0);
453 anv_assert(subresource->arrayLayer == 0);
454
455 layout->offset = surface->offset;
456 layout->rowPitch = surface->isl.row_pitch;
457 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
458 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
459 layout->size = surface->isl.size;
460 }
461
462 void anv_GetImageSubresourceLayout(
463 VkDevice device,
464 VkImage _image,
465 const VkImageSubresource* pSubresource,
466 VkSubresourceLayout* pLayout)
467 {
468 ANV_FROM_HANDLE(anv_image, image, _image);
469
470 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
471
472 switch (pSubresource->aspectMask) {
473 case VK_IMAGE_ASPECT_COLOR_BIT:
474 anv_surface_get_subresource_layout(image, &image->color_surface,
475 pSubresource, pLayout);
476 break;
477 case VK_IMAGE_ASPECT_DEPTH_BIT:
478 anv_surface_get_subresource_layout(image, &image->depth_surface,
479 pSubresource, pLayout);
480 break;
481 case VK_IMAGE_ASPECT_STENCIL_BIT:
482 anv_surface_get_subresource_layout(image, &image->stencil_surface,
483 pSubresource, pLayout);
484 break;
485 default:
486 assert(!"Invalid image aspect");
487 }
488 }
489
490 /**
491 * This function determines the optimal buffer to use for a given
492 * VkImageLayout and other pieces of information needed to make that
493 * determination. This does not determine the optimal buffer to use
494 * during a resolve operation.
495 *
496 * @param devinfo The device information of the Intel GPU.
497 * @param image The image that may contain a collection of buffers.
498 * @param aspects The aspect(s) of the image to be accessed.
499 * @param layout The current layout of the image aspect(s).
500 *
501 * @return The primary buffer that should be used for the given layout.
502 */
503 enum isl_aux_usage
504 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
505 const struct anv_image * const image,
506 const VkImageAspectFlags aspects,
507 const VkImageLayout layout)
508 {
509 /* Validate the inputs. */
510
511 /* The devinfo is needed as the optimal buffer varies across generations. */
512 assert(devinfo != NULL);
513
514 /* The layout of a NULL image is not properly defined. */
515 assert(image != NULL);
516
517 /* The aspects must be a subset of the image aspects. */
518 assert(aspects & image->aspects && aspects <= image->aspects);
519
520 /* Determine the optimal buffer. */
521
522 /* If there is no auxiliary surface allocated, we must use the one and only
523 * main buffer.
524 */
525 if (image->aux_surface.isl.size == 0)
526 return ISL_AUX_USAGE_NONE;
527
528 /* All images that use an auxiliary surface are required to be tiled. */
529 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
530
531 /* On BDW+, when clearing the stencil aspect of a depth stencil image,
532 * the HiZ buffer allows us to record the clear with a relatively small
533 * number of packets. Prior to BDW, the HiZ buffer provides no known benefit
534 * to the stencil aspect.
535 */
536 if (devinfo->gen < 8 && aspects == VK_IMAGE_ASPECT_STENCIL_BIT)
537 return ISL_AUX_USAGE_NONE;
538
539 const bool color_aspect = aspects == VK_IMAGE_ASPECT_COLOR_BIT;
540
541 /* The following switch currently only handles depth stencil aspects.
542 * TODO: Handle the color aspect.
543 */
544 if (color_aspect)
545 return image->aux_usage;
546
547 switch (layout) {
548
549 /* Invalid Layouts */
550 case VK_IMAGE_LAYOUT_RANGE_SIZE:
551 case VK_IMAGE_LAYOUT_MAX_ENUM:
552 unreachable("Invalid image layout.");
553
554 /* Undefined layouts
555 *
556 * The pre-initialized layout is equivalent to the undefined layout for
557 * optimally-tiled images. We can only do color compression (CCS or HiZ)
558 * on tiled images.
559 */
560 case VK_IMAGE_LAYOUT_UNDEFINED:
561 case VK_IMAGE_LAYOUT_PREINITIALIZED:
562 return ISL_AUX_USAGE_NONE;
563
564
565 /* Transfer Layouts
566 *
567 * This buffer could be a depth buffer used in a transfer operation. BLORP
568 * currently doesn't use HiZ for transfer operations so we must use the main
569 * buffer for this layout. TODO: Enable HiZ in BLORP.
570 */
571 case VK_IMAGE_LAYOUT_GENERAL:
572 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
573 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
574 return ISL_AUX_USAGE_NONE;
575
576
577 /* Sampling Layouts */
578 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
579 assert(!color_aspect);
580 /* Fall-through */
581 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
582 if (anv_can_sample_with_hiz(devinfo, aspects, image->samples))
583 return ISL_AUX_USAGE_HIZ;
584 else
585 return ISL_AUX_USAGE_NONE;
586
587 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
588 assert(color_aspect);
589
590 /* On SKL+, the render buffer can be decompressed by the presentation
591 * engine. Support for this feature has not yet landed in the wider
592 * ecosystem. TODO: Update this code when support lands.
593 *
594 * From the BDW PRM, Vol 7, Render Target Resolve:
595 *
596 * If the MCS is enabled on a non-multisampled render target, the
597 * render target must be resolved before being used for other
598 * purposes (display, texture, CPU lock) The clear value from
599 * SURFACE_STATE is written into pixels in the render target
600 * indicated as clear in the MCS.
601 *
602 * Pre-SKL, the render buffer must be resolved before being used for
603 * presentation. We can infer that the auxiliary buffer is not used.
604 */
605 return ISL_AUX_USAGE_NONE;
606
607
608 /* Rendering Layouts */
609 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
610 assert(color_aspect);
611 unreachable("Color images are not yet supported.");
612
613 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
614 assert(!color_aspect);
615 return ISL_AUX_USAGE_HIZ;
616
617 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
618 unreachable("VK_KHR_shared_presentable_image is unsupported");
619 }
620
621 /* If the layout isn't recognized in the exhaustive switch above, the
622 * VkImageLayout value is not defined in vulkan.h.
623 */
624 unreachable("layout is not a VkImageLayout enumeration member.");
625 }
626
627
628 static struct anv_state
629 alloc_surface_state(struct anv_device *device)
630 {
631 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
632 }
633
634 static enum isl_channel_select
635 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
636 struct isl_swizzle format_swizzle)
637 {
638 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
639 swizzle = component;
640
641 switch (swizzle) {
642 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
643 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
644 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
645 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
646 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
647 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
648 default:
649 unreachable("Invalid swizzle");
650 }
651 }
652
653
654 VkResult
655 anv_CreateImageView(VkDevice _device,
656 const VkImageViewCreateInfo *pCreateInfo,
657 const VkAllocationCallbacks *pAllocator,
658 VkImageView *pView)
659 {
660 ANV_FROM_HANDLE(anv_device, device, _device);
661 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
662 struct anv_image_view *iview;
663
664 iview = vk_zalloc2(&device->alloc, pAllocator, sizeof(*iview), 8,
665 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
666 if (iview == NULL)
667 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
668
669 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
670
671 assert(range->layerCount > 0);
672 assert(range->baseMipLevel < image->levels);
673 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
674 VK_IMAGE_USAGE_STORAGE_BIT |
675 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
676 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
677 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
678
679 switch (image->type) {
680 default:
681 unreachable("bad VkImageType");
682 case VK_IMAGE_TYPE_1D:
683 case VK_IMAGE_TYPE_2D:
684 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
685 break;
686 case VK_IMAGE_TYPE_3D:
687 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
688 <= anv_minify(image->extent.depth, range->baseMipLevel));
689 break;
690 }
691
692 const struct anv_surface *surface =
693 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
694
695 iview->image = image;
696 iview->bo = image->bo;
697 iview->offset = image->offset + surface->offset;
698
699 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
700 iview->vk_format = pCreateInfo->format;
701
702 struct anv_format format = anv_get_format(&device->info, pCreateInfo->format,
703 range->aspectMask, image->tiling);
704
705 iview->isl = (struct isl_view) {
706 .format = format.isl_format,
707 .base_level = range->baseMipLevel,
708 .levels = anv_get_levelCount(image, range),
709 .base_array_layer = range->baseArrayLayer,
710 .array_len = anv_get_layerCount(image, range),
711 .swizzle = {
712 .r = remap_swizzle(pCreateInfo->components.r,
713 VK_COMPONENT_SWIZZLE_R, format.swizzle),
714 .g = remap_swizzle(pCreateInfo->components.g,
715 VK_COMPONENT_SWIZZLE_G, format.swizzle),
716 .b = remap_swizzle(pCreateInfo->components.b,
717 VK_COMPONENT_SWIZZLE_B, format.swizzle),
718 .a = remap_swizzle(pCreateInfo->components.a,
719 VK_COMPONENT_SWIZZLE_A, format.swizzle),
720 },
721 };
722
723 iview->extent = (VkExtent3D) {
724 .width = anv_minify(image->extent.width , range->baseMipLevel),
725 .height = anv_minify(image->extent.height, range->baseMipLevel),
726 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
727 };
728
729 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_3D) {
730 iview->isl.base_array_layer = 0;
731 iview->isl.array_len = iview->extent.depth;
732 }
733
734 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
735 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
736 iview->isl.usage = ISL_SURF_USAGE_CUBE_BIT;
737 } else {
738 iview->isl.usage = 0;
739 }
740
741 /* Input attachment surfaces for color are allocated and filled
742 * out at BeginRenderPass time because they need compression information.
743 * Compression is not yet enabled for depth textures and stencil doesn't
744 * allow compression so we can just use the texture surface state from the
745 * view.
746 */
747 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT ||
748 (image->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
749 !(iview->aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT))) {
750 iview->optimal_sampler_surface_state = alloc_surface_state(device);
751 iview->general_sampler_surface_state = alloc_surface_state(device);
752
753 iview->general_sampler_aux_usage =
754 anv_layout_to_aux_usage(&device->info, image, iview->aspect_mask,
755 VK_IMAGE_LAYOUT_GENERAL);
756 iview->optimal_sampler_aux_usage =
757 anv_layout_to_aux_usage(&device->info, image, iview->aspect_mask,
758 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
759
760 /* If this is a HiZ buffer we can sample from with a programmable clear
761 * value (SKL+), define the clear value to the optimal constant.
762 */
763 union isl_color_value clear_color = { .u32 = { 0, } };
764 if ((iview->aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) &&
765 device->info.gen >= 9)
766 clear_color.f32[0] = ANV_HZ_FC_VAL;
767
768 struct isl_view view = iview->isl;
769 view.usage |= ISL_SURF_USAGE_TEXTURE_BIT;
770
771 isl_surf_fill_state(&device->isl_dev,
772 iview->optimal_sampler_surface_state.map,
773 .surf = &surface->isl,
774 .view = &view,
775 .clear_color = clear_color,
776 .aux_surf = &image->aux_surface.isl,
777 .aux_usage = iview->optimal_sampler_aux_usage,
778 .mocs = device->default_mocs);
779
780 isl_surf_fill_state(&device->isl_dev,
781 iview->general_sampler_surface_state.map,
782 .surf = &surface->isl,
783 .view = &view,
784 .clear_color = clear_color,
785 .aux_surf = &image->aux_surface.isl,
786 .aux_usage = iview->general_sampler_aux_usage,
787 .mocs = device->default_mocs);
788
789 anv_state_flush(device, iview->optimal_sampler_surface_state);
790 anv_state_flush(device, iview->general_sampler_surface_state);
791 }
792
793 /* NOTE: This one needs to go last since it may stomp isl_view.format */
794 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
795 iview->storage_surface_state = alloc_surface_state(device);
796 iview->writeonly_storage_surface_state = alloc_surface_state(device);
797
798 struct isl_view view = iview->isl;
799 view.usage |= ISL_SURF_USAGE_STORAGE_BIT;
800
801 /* Write-only accesses always used a typed write instruction and should
802 * therefore use the real format.
803 */
804 isl_surf_fill_state(&device->isl_dev,
805 iview->writeonly_storage_surface_state.map,
806 .surf = &surface->isl,
807 .view = &view,
808 .aux_surf = &image->aux_surface.isl,
809 .aux_usage = image->aux_usage,
810 .mocs = device->default_mocs);
811
812 if (isl_has_matching_typed_storage_image_format(&device->info,
813 format.isl_format)) {
814 /* Typed surface reads support a very limited subset of the shader
815 * image formats. Translate it into the closest format the hardware
816 * supports.
817 */
818 view.format = isl_lower_storage_image_format(&device->info,
819 format.isl_format);
820
821 isl_surf_fill_state(&device->isl_dev,
822 iview->storage_surface_state.map,
823 .surf = &surface->isl,
824 .view = &view,
825 .aux_surf = &image->aux_surface.isl,
826 .aux_usage = image->aux_usage,
827 .mocs = device->default_mocs);
828 } else {
829 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
830 ISL_FORMAT_RAW,
831 iview->offset,
832 iview->bo->size - iview->offset, 1);
833 }
834
835 isl_surf_fill_image_param(&device->isl_dev,
836 &iview->storage_image_param,
837 &surface->isl, &iview->isl);
838
839 anv_state_flush(device, iview->storage_surface_state);
840 anv_state_flush(device, iview->writeonly_storage_surface_state);
841 }
842
843 *pView = anv_image_view_to_handle(iview);
844
845 return VK_SUCCESS;
846 }
847
848 void
849 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
850 const VkAllocationCallbacks *pAllocator)
851 {
852 ANV_FROM_HANDLE(anv_device, device, _device);
853 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
854
855 if (!iview)
856 return;
857
858 if (iview->optimal_sampler_surface_state.alloc_size > 0) {
859 anv_state_pool_free(&device->surface_state_pool,
860 iview->optimal_sampler_surface_state);
861 }
862
863 if (iview->general_sampler_surface_state.alloc_size > 0) {
864 anv_state_pool_free(&device->surface_state_pool,
865 iview->general_sampler_surface_state);
866 }
867
868 if (iview->storage_surface_state.alloc_size > 0) {
869 anv_state_pool_free(&device->surface_state_pool,
870 iview->storage_surface_state);
871 }
872
873 if (iview->writeonly_storage_surface_state.alloc_size > 0) {
874 anv_state_pool_free(&device->surface_state_pool,
875 iview->writeonly_storage_surface_state);
876 }
877
878 vk_free2(&device->alloc, pAllocator, iview);
879 }
880
881
882 VkResult
883 anv_CreateBufferView(VkDevice _device,
884 const VkBufferViewCreateInfo *pCreateInfo,
885 const VkAllocationCallbacks *pAllocator,
886 VkBufferView *pView)
887 {
888 ANV_FROM_HANDLE(anv_device, device, _device);
889 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
890 struct anv_buffer_view *view;
891
892 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
893 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
894 if (!view)
895 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
896
897 /* TODO: Handle the format swizzle? */
898
899 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
900 VK_IMAGE_ASPECT_COLOR_BIT,
901 VK_IMAGE_TILING_LINEAR);
902 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
903 view->bo = buffer->bo;
904 view->offset = buffer->offset + pCreateInfo->offset;
905 view->range = anv_buffer_get_range(buffer, pCreateInfo->offset,
906 pCreateInfo->range);
907 view->range = align_down_npot_u32(view->range, format_bs);
908
909 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
910 view->surface_state = alloc_surface_state(device);
911
912 anv_fill_buffer_surface_state(device, view->surface_state,
913 view->format,
914 view->offset, view->range, format_bs);
915 } else {
916 view->surface_state = (struct anv_state){ 0 };
917 }
918
919 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
920 view->storage_surface_state = alloc_surface_state(device);
921 view->writeonly_storage_surface_state = alloc_surface_state(device);
922
923 enum isl_format storage_format =
924 isl_has_matching_typed_storage_image_format(&device->info,
925 view->format) ?
926 isl_lower_storage_image_format(&device->info, view->format) :
927 ISL_FORMAT_RAW;
928
929 anv_fill_buffer_surface_state(device, view->storage_surface_state,
930 storage_format,
931 view->offset, view->range,
932 (storage_format == ISL_FORMAT_RAW ? 1 :
933 isl_format_get_layout(storage_format)->bpb / 8));
934
935 /* Write-only accesses should use the original format. */
936 anv_fill_buffer_surface_state(device, view->writeonly_storage_surface_state,
937 view->format,
938 view->offset, view->range,
939 isl_format_get_layout(view->format)->bpb / 8);
940
941 isl_buffer_fill_image_param(&device->isl_dev,
942 &view->storage_image_param,
943 view->format, view->range);
944 } else {
945 view->storage_surface_state = (struct anv_state){ 0 };
946 view->writeonly_storage_surface_state = (struct anv_state){ 0 };
947 }
948
949 *pView = anv_buffer_view_to_handle(view);
950
951 return VK_SUCCESS;
952 }
953
954 void
955 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
956 const VkAllocationCallbacks *pAllocator)
957 {
958 ANV_FROM_HANDLE(anv_device, device, _device);
959 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
960
961 if (!view)
962 return;
963
964 if (view->surface_state.alloc_size > 0)
965 anv_state_pool_free(&device->surface_state_pool,
966 view->surface_state);
967
968 if (view->storage_surface_state.alloc_size > 0)
969 anv_state_pool_free(&device->surface_state_pool,
970 view->storage_surface_state);
971
972 if (view->writeonly_storage_surface_state.alloc_size > 0)
973 anv_state_pool_free(&device->surface_state_pool,
974 view->writeonly_storage_surface_state);
975
976 vk_free2(&device->alloc, pAllocator, view);
977 }
978
979 const struct anv_surface *
980 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
981 VkImageAspectFlags aspect_mask)
982 {
983 switch (aspect_mask) {
984 case VK_IMAGE_ASPECT_COLOR_BIT:
985 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
986 return &image->color_surface;
987 case VK_IMAGE_ASPECT_DEPTH_BIT:
988 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
989 return &image->depth_surface;
990 case VK_IMAGE_ASPECT_STENCIL_BIT:
991 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
992 return &image->stencil_surface;
993 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
994 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
995 * combined depth stencil formats. Specifically, it states:
996 *
997 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
998 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
999 *
1000 * Image views with both depth and stencil aspects are only valid for
1001 * render target attachments, in which case
1002 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
1003 * stencil surfaces from the underlying surface.
1004 */
1005 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1006 return &image->depth_surface;
1007 } else {
1008 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
1009 return &image->stencil_surface;
1010 }
1011 default:
1012 unreachable("image does not have aspect");
1013 return NULL;
1014 }
1015 }