anv: Add initial support for Sky Lake color compression
[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 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 /* Even if we're only using it for transfer operations, clears to depth and
57 * stencil images happen as depth and stencil so they need the right ISL
58 * usage bits or else things will fall apart.
59 */
60 switch (aspect) {
61 case VK_IMAGE_ASPECT_DEPTH_BIT:
62 isl_usage |= ISL_SURF_USAGE_DEPTH_BIT;
63 break;
64 case VK_IMAGE_ASPECT_STENCIL_BIT:
65 isl_usage |= ISL_SURF_USAGE_STENCIL_BIT;
66 break;
67 case VK_IMAGE_ASPECT_COLOR_BIT:
68 break;
69 default:
70 unreachable("bad VkImageAspect");
71 }
72
73 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
74 /* blorp 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 /* blorp 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 = MAX2(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 /* Allow the user to control HiZ enabling. Disable by default on gen7
181 * because resolves are not currently implemented pre-BDW.
182 */
183 if (!(image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
184 /* It will never be used as an attachment, HiZ is pointless. */
185 } else 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 assert(image->aux_surface.isl.size == 0);
193 isl_surf_get_hiz_surf(&dev->isl_dev, &image->depth_surface.isl,
194 &image->aux_surface.isl);
195 add_surface(image, &image->aux_surface);
196 }
197 } else if (aspect == VK_IMAGE_ASPECT_COLOR_BIT && vk_info->samples == 1) {
198 if (dev->info.gen >= 9 && !unlikely(INTEL_DEBUG & DEBUG_NO_RBC)) {
199 assert(image->aux_surface.isl.size == 0);
200 ok = isl_surf_get_ccs_surf(&dev->isl_dev, &anv_surf->isl,
201 &image->aux_surface.isl);
202 if (ok)
203 add_surface(image, &image->aux_surface);
204 }
205 }
206
207 return VK_SUCCESS;
208 }
209
210 VkResult
211 anv_image_create(VkDevice _device,
212 const struct anv_image_create_info *create_info,
213 const VkAllocationCallbacks* alloc,
214 VkImage *pImage)
215 {
216 ANV_FROM_HANDLE(anv_device, device, _device);
217 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
218 struct anv_image *image = NULL;
219 VkResult r;
220
221 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
222
223 anv_assert(pCreateInfo->mipLevels > 0);
224 anv_assert(pCreateInfo->arrayLayers > 0);
225 anv_assert(pCreateInfo->samples > 0);
226 anv_assert(pCreateInfo->extent.width > 0);
227 anv_assert(pCreateInfo->extent.height > 0);
228 anv_assert(pCreateInfo->extent.depth > 0);
229
230 image = vk_alloc2(&device->alloc, alloc, sizeof(*image), 8,
231 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
232 if (!image)
233 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
234
235 memset(image, 0, sizeof(*image));
236 image->type = pCreateInfo->imageType;
237 image->extent = pCreateInfo->extent;
238 image->vk_format = pCreateInfo->format;
239 image->aspects = vk_format_aspects(image->vk_format);
240 image->levels = pCreateInfo->mipLevels;
241 image->array_size = pCreateInfo->arrayLayers;
242 image->samples = pCreateInfo->samples;
243 image->usage = pCreateInfo->usage;
244 image->tiling = pCreateInfo->tiling;
245
246 uint32_t b;
247 for_each_bit(b, image->aspects) {
248 r = make_surface(device, image, create_info, (1 << b));
249 if (r != VK_SUCCESS)
250 goto fail;
251 }
252
253 *pImage = anv_image_to_handle(image);
254
255 return VK_SUCCESS;
256
257 fail:
258 if (image)
259 vk_free2(&device->alloc, alloc, image);
260
261 return r;
262 }
263
264 VkResult
265 anv_CreateImage(VkDevice device,
266 const VkImageCreateInfo *pCreateInfo,
267 const VkAllocationCallbacks *pAllocator,
268 VkImage *pImage)
269 {
270 return anv_image_create(device,
271 &(struct anv_image_create_info) {
272 .vk_info = pCreateInfo,
273 },
274 pAllocator,
275 pImage);
276 }
277
278 void
279 anv_DestroyImage(VkDevice _device, VkImage _image,
280 const VkAllocationCallbacks *pAllocator)
281 {
282 ANV_FROM_HANDLE(anv_device, device, _device);
283 ANV_FROM_HANDLE(anv_image, image, _image);
284
285 if (!image)
286 return;
287
288 vk_free2(&device->alloc, pAllocator, image);
289 }
290
291 VkResult anv_BindImageMemory(
292 VkDevice _device,
293 VkImage _image,
294 VkDeviceMemory _memory,
295 VkDeviceSize memoryOffset)
296 {
297 ANV_FROM_HANDLE(anv_device, device, _device);
298 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
299 ANV_FROM_HANDLE(anv_image, image, _image);
300
301 if (mem) {
302 image->bo = &mem->bo;
303 image->offset = memoryOffset;
304 } else {
305 image->bo = NULL;
306 image->offset = 0;
307 }
308
309 if (image->aux_surface.isl.size > 0) {
310
311 /* The offset and size must be a multiple of 4K or else the
312 * anv_gem_mmap call below will return NULL.
313 */
314 assert((image->offset + image->aux_surface.offset) % 4096 == 0);
315 assert(image->aux_surface.isl.size % 4096 == 0);
316
317 /* Auxiliary surfaces need to have their memory cleared to 0 before they
318 * can be used. For CCS surfaces, this puts them in the "resolved"
319 * state so they can be used with CCS enabled before we ever touch it
320 * from the GPU. For HiZ, we need something valid or else we may get
321 * GPU hangs on some hardware and 0 works fine.
322 */
323 void *map = anv_gem_mmap(device, image->bo->gem_handle,
324 image->offset + image->aux_surface.offset,
325 image->aux_surface.isl.size,
326 device->info.has_llc ? 0 : I915_MMAP_WC);
327
328 /* If anv_gem_mmap returns NULL, it's likely that the kernel was
329 * not able to find space on the host to create a proper mapping.
330 */
331 if (map == NULL)
332 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
333
334 memset(map, 0, image->aux_surface.isl.size);
335
336 anv_gem_munmap(map, image->aux_surface.isl.size);
337 }
338
339 return VK_SUCCESS;
340 }
341
342 static void
343 anv_surface_get_subresource_layout(struct anv_image *image,
344 struct anv_surface *surface,
345 const VkImageSubresource *subresource,
346 VkSubresourceLayout *layout)
347 {
348 /* If we are on a non-zero mip level or array slice, we need to
349 * calculate a real offset.
350 */
351 anv_assert(subresource->mipLevel == 0);
352 anv_assert(subresource->arrayLayer == 0);
353
354 layout->offset = surface->offset;
355 layout->rowPitch = surface->isl.row_pitch;
356 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
357 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
358 layout->size = surface->isl.size;
359 }
360
361 void anv_GetImageSubresourceLayout(
362 VkDevice device,
363 VkImage _image,
364 const VkImageSubresource* pSubresource,
365 VkSubresourceLayout* pLayout)
366 {
367 ANV_FROM_HANDLE(anv_image, image, _image);
368
369 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
370
371 switch (pSubresource->aspectMask) {
372 case VK_IMAGE_ASPECT_COLOR_BIT:
373 anv_surface_get_subresource_layout(image, &image->color_surface,
374 pSubresource, pLayout);
375 break;
376 case VK_IMAGE_ASPECT_DEPTH_BIT:
377 anv_surface_get_subresource_layout(image, &image->depth_surface,
378 pSubresource, pLayout);
379 break;
380 case VK_IMAGE_ASPECT_STENCIL_BIT:
381 anv_surface_get_subresource_layout(image, &image->stencil_surface,
382 pSubresource, pLayout);
383 break;
384 default:
385 assert(!"Invalid image aspect");
386 }
387 }
388
389 static struct anv_state
390 alloc_surface_state(struct anv_device *device)
391 {
392 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
393 }
394
395 static enum isl_channel_select
396 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
397 struct isl_swizzle format_swizzle)
398 {
399 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
400 swizzle = component;
401
402 switch (swizzle) {
403 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
404 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
405 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
406 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
407 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
408 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
409 default:
410 unreachable("Invalid swizzle");
411 }
412 }
413
414
415 VkResult
416 anv_CreateImageView(VkDevice _device,
417 const VkImageViewCreateInfo *pCreateInfo,
418 const VkAllocationCallbacks *pAllocator,
419 VkImageView *pView)
420 {
421 ANV_FROM_HANDLE(anv_device, device, _device);
422 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
423 struct anv_image_view *iview;
424
425 iview = vk_alloc2(&device->alloc, pAllocator, sizeof(*iview), 8,
426 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
427 if (iview == NULL)
428 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
429
430 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
431
432 assert(range->layerCount > 0);
433 assert(range->baseMipLevel < image->levels);
434 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
435 VK_IMAGE_USAGE_STORAGE_BIT |
436 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
437 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
438
439 switch (image->type) {
440 default:
441 unreachable("bad VkImageType");
442 case VK_IMAGE_TYPE_1D:
443 case VK_IMAGE_TYPE_2D:
444 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
445 break;
446 case VK_IMAGE_TYPE_3D:
447 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
448 <= anv_minify(image->extent.depth, range->baseMipLevel));
449 break;
450 }
451
452 const struct anv_surface *surface =
453 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
454
455 iview->image = image;
456 iview->bo = image->bo;
457 iview->offset = image->offset + surface->offset;
458
459 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
460 iview->vk_format = pCreateInfo->format;
461
462 struct anv_format format = anv_get_format(&device->info, pCreateInfo->format,
463 range->aspectMask, image->tiling);
464
465 iview->isl = (struct isl_view) {
466 .format = format.isl_format,
467 .base_level = range->baseMipLevel,
468 .levels = anv_get_levelCount(image, range),
469 .base_array_layer = range->baseArrayLayer,
470 .array_len = anv_get_layerCount(image, range),
471 .swizzle = {
472 .r = remap_swizzle(pCreateInfo->components.r,
473 VK_COMPONENT_SWIZZLE_R, format.swizzle),
474 .g = remap_swizzle(pCreateInfo->components.g,
475 VK_COMPONENT_SWIZZLE_G, format.swizzle),
476 .b = remap_swizzle(pCreateInfo->components.b,
477 VK_COMPONENT_SWIZZLE_B, format.swizzle),
478 .a = remap_swizzle(pCreateInfo->components.a,
479 VK_COMPONENT_SWIZZLE_A, format.swizzle),
480 },
481 };
482
483 iview->extent = (VkExtent3D) {
484 .width = anv_minify(image->extent.width , range->baseMipLevel),
485 .height = anv_minify(image->extent.height, range->baseMipLevel),
486 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
487 };
488
489 if (image->type == VK_IMAGE_TYPE_3D) {
490 iview->isl.base_array_layer = 0;
491 iview->isl.array_len = iview->extent.depth;
492 }
493
494 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
495 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
496 iview->isl.usage = ISL_SURF_USAGE_CUBE_BIT;
497 } else {
498 iview->isl.usage = 0;
499 }
500
501 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
502 iview->sampler_surface_state = alloc_surface_state(device);
503
504 struct isl_view view = iview->isl;
505 view.usage |= ISL_SURF_USAGE_TEXTURE_BIT;
506 isl_surf_fill_state(&device->isl_dev,
507 iview->sampler_surface_state.map,
508 .surf = &surface->isl,
509 .view = &view,
510 .mocs = device->default_mocs);
511
512 if (!device->info.has_llc)
513 anv_state_clflush(iview->sampler_surface_state);
514 } else {
515 iview->sampler_surface_state.alloc_size = 0;
516 }
517
518 /* NOTE: This one needs to go last since it may stomp isl_view.format */
519 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
520 iview->storage_surface_state = alloc_surface_state(device);
521
522 if (isl_has_matching_typed_storage_image_format(&device->info,
523 format.isl_format)) {
524 struct isl_view view = iview->isl;
525 view.usage |= ISL_SURF_USAGE_STORAGE_BIT;
526 view.format = isl_lower_storage_image_format(&device->info,
527 format.isl_format);
528 isl_surf_fill_state(&device->isl_dev,
529 iview->storage_surface_state.map,
530 .surf = &surface->isl,
531 .view = &view,
532 .mocs = device->default_mocs);
533 } else {
534 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
535 ISL_FORMAT_RAW,
536 iview->offset,
537 iview->bo->size - iview->offset, 1);
538 }
539
540 isl_surf_fill_image_param(&device->isl_dev,
541 &iview->storage_image_param,
542 &surface->isl, &iview->isl);
543
544 if (!device->info.has_llc)
545 anv_state_clflush(iview->storage_surface_state);
546 } else {
547 iview->storage_surface_state.alloc_size = 0;
548 }
549
550 *pView = anv_image_view_to_handle(iview);
551
552 return VK_SUCCESS;
553 }
554
555 void
556 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
557 const VkAllocationCallbacks *pAllocator)
558 {
559 ANV_FROM_HANDLE(anv_device, device, _device);
560 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
561
562 if (!iview)
563 return;
564
565 if (iview->sampler_surface_state.alloc_size > 0) {
566 anv_state_pool_free(&device->surface_state_pool,
567 iview->sampler_surface_state);
568 }
569
570 if (iview->storage_surface_state.alloc_size > 0) {
571 anv_state_pool_free(&device->surface_state_pool,
572 iview->storage_surface_state);
573 }
574
575 vk_free2(&device->alloc, pAllocator, iview);
576 }
577
578
579 VkResult
580 anv_CreateBufferView(VkDevice _device,
581 const VkBufferViewCreateInfo *pCreateInfo,
582 const VkAllocationCallbacks *pAllocator,
583 VkBufferView *pView)
584 {
585 ANV_FROM_HANDLE(anv_device, device, _device);
586 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
587 struct anv_buffer_view *view;
588
589 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
590 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
591 if (!view)
592 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
593
594 /* TODO: Handle the format swizzle? */
595
596 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
597 VK_IMAGE_ASPECT_COLOR_BIT,
598 VK_IMAGE_TILING_LINEAR);
599 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
600 view->bo = buffer->bo;
601 view->offset = buffer->offset + pCreateInfo->offset;
602 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
603 buffer->size - pCreateInfo->offset : pCreateInfo->range;
604 view->range = align_down_npot_u32(view->range, format_bs);
605
606 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
607 view->surface_state = alloc_surface_state(device);
608
609 anv_fill_buffer_surface_state(device, view->surface_state,
610 view->format,
611 view->offset, view->range, format_bs);
612 } else {
613 view->surface_state = (struct anv_state){ 0 };
614 }
615
616 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
617 view->storage_surface_state = alloc_surface_state(device);
618
619 enum isl_format storage_format =
620 isl_has_matching_typed_storage_image_format(&device->info,
621 view->format) ?
622 isl_lower_storage_image_format(&device->info, view->format) :
623 ISL_FORMAT_RAW;
624
625 anv_fill_buffer_surface_state(device, view->storage_surface_state,
626 storage_format,
627 view->offset, view->range,
628 (storage_format == ISL_FORMAT_RAW ? 1 :
629 isl_format_get_layout(storage_format)->bpb / 8));
630
631 isl_buffer_fill_image_param(&device->isl_dev,
632 &view->storage_image_param,
633 view->format, view->range);
634 } else {
635 view->storage_surface_state = (struct anv_state){ 0 };
636 }
637
638 *pView = anv_buffer_view_to_handle(view);
639
640 return VK_SUCCESS;
641 }
642
643 void
644 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
645 const VkAllocationCallbacks *pAllocator)
646 {
647 ANV_FROM_HANDLE(anv_device, device, _device);
648 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
649
650 if (!view)
651 return;
652
653 if (view->surface_state.alloc_size > 0)
654 anv_state_pool_free(&device->surface_state_pool,
655 view->surface_state);
656
657 if (view->storage_surface_state.alloc_size > 0)
658 anv_state_pool_free(&device->surface_state_pool,
659 view->storage_surface_state);
660
661 vk_free2(&device->alloc, pAllocator, view);
662 }
663
664 const struct anv_surface *
665 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
666 VkImageAspectFlags aspect_mask)
667 {
668 switch (aspect_mask) {
669 case VK_IMAGE_ASPECT_COLOR_BIT:
670 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
671 return &image->color_surface;
672 case VK_IMAGE_ASPECT_DEPTH_BIT:
673 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
674 return &image->depth_surface;
675 case VK_IMAGE_ASPECT_STENCIL_BIT:
676 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
677 return &image->stencil_surface;
678 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
679 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
680 * combined depth stencil formats. Specifically, it states:
681 *
682 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
683 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
684 *
685 * Image views with both depth and stencil aspects are only valid for
686 * render target attachments, in which case
687 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
688 * stencil surfaces from the underlying surface.
689 */
690 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
691 return &image->depth_surface;
692 } else {
693 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
694 return &image->stencil_surface;
695 }
696 default:
697 unreachable("image does not have aspect");
698 return NULL;
699 }
700 }