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