anv/formats: Add an anv_get_format helper
[mesa.git] / src / intel / vulkan / anv_image.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 #include "vk_format_info.h"
33
34 /**
35 * Exactly one bit must be set in \a aspect.
36 */
37 static isl_surf_usage_flags_t
38 choose_isl_surf_usage(VkImageUsageFlags vk_usage,
39 VkImageAspectFlags aspect)
40 {
41 isl_surf_usage_flags_t isl_usage = 0;
42
43 /* FINISHME: Support aux surfaces */
44 isl_usage |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
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_usage & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
56 isl_usage |= ISL_SURF_USAGE_CUBE_BIT;
57
58 if (vk_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
59 switch (aspect) {
60 default:
61 unreachable("bad VkImageAspect");
62 case VK_IMAGE_ASPECT_DEPTH_BIT:
63 isl_usage |= ISL_SURF_USAGE_DEPTH_BIT;
64 break;
65 case VK_IMAGE_ASPECT_STENCIL_BIT:
66 isl_usage |= ISL_SURF_USAGE_STENCIL_BIT;
67 break;
68 }
69 }
70
71 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
72 /* Meta implements transfers by sampling from the source image. */
73 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
74 }
75
76 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
77 /* Meta implements transfers by rendering into the destination image. */
78 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
79 }
80
81 return isl_usage;
82 }
83
84 /**
85 * Exactly one bit must be set in \a aspect.
86 */
87 static struct anv_surface *
88 get_surface(struct anv_image *image, VkImageAspectFlags aspect)
89 {
90 switch (aspect) {
91 default:
92 unreachable("bad VkImageAspect");
93 case VK_IMAGE_ASPECT_COLOR_BIT:
94 return &image->color_surface;
95 case VK_IMAGE_ASPECT_DEPTH_BIT:
96 return &image->depth_surface;
97 case VK_IMAGE_ASPECT_STENCIL_BIT:
98 return &image->stencil_surface;
99 }
100 }
101
102 /**
103 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
104 * image's memory requirements (that is, the image's size and alignment).
105 *
106 * Exactly one bit must be set in \a aspect.
107 */
108 static VkResult
109 make_surface(const struct anv_device *dev,
110 struct anv_image *image,
111 const struct anv_image_create_info *anv_info,
112 VkImageAspectFlags aspect)
113 {
114 const VkImageCreateInfo *vk_info = anv_info->vk_info;
115 bool ok UNUSED;
116
117 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
118 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
119 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
120 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
121 };
122
123 isl_tiling_flags_t tiling_flags = anv_info->isl_tiling_flags;
124 if (vk_info->tiling == VK_IMAGE_TILING_LINEAR)
125 tiling_flags = ISL_TILING_LINEAR_BIT;
126
127 struct anv_surface *anv_surf = get_surface(image, aspect);
128
129 image->extent = anv_sanitize_image_extent(vk_info->imageType,
130 vk_info->extent);
131
132 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
133 .dim = vk_to_isl_surf_dim[vk_info->imageType],
134 .format = anv_get_isl_format(vk_info->format, aspect, vk_info->tiling),
135 .width = image->extent.width,
136 .height = image->extent.height,
137 .depth = image->extent.depth,
138 .levels = vk_info->mipLevels,
139 .array_len = vk_info->arrayLayers,
140 .samples = vk_info->samples,
141 .min_alignment = 0,
142 .min_pitch = anv_info->stride,
143 .usage = choose_isl_surf_usage(image->usage, aspect),
144 .tiling_flags = tiling_flags);
145
146 /* isl_surf_init() will fail only if provided invalid input. Invalid input
147 * is illegal in Vulkan.
148 */
149 assert(ok);
150
151 anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment);
152 image->size = anv_surf->offset + anv_surf->isl.size;
153 image->alignment = MAX(image->alignment, anv_surf->isl.alignment);
154
155 return VK_SUCCESS;
156 }
157
158 /**
159 * Parameter @a format is required and overrides VkImageCreateInfo::format.
160 */
161 static VkImageUsageFlags
162 anv_image_get_full_usage(const VkImageCreateInfo *info,
163 VkImageAspectFlags aspects)
164 {
165 VkImageUsageFlags usage = info->usage;
166
167 if (info->samples > 1 &&
168 (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) {
169 /* Meta will resolve the image by binding it as a texture. */
170 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
171 }
172
173 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
174 /* Meta will transfer from the image by binding it as a texture. */
175 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
176 }
177
178 if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
179 /* For non-clear transfer operations, meta will transfer to the image by
180 * binding it as a color attachment, even if the image format is not
181 * a color format.
182 */
183 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
184
185 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
186 /* vkCmdClearDepthStencilImage() only requires that
187 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
188 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
189 * clears the image, though, by binding it as a depthstencil
190 * attachment.
191 */
192 usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
193 }
194 }
195
196 return usage;
197 }
198
199 VkResult
200 anv_image_create(VkDevice _device,
201 const struct anv_image_create_info *create_info,
202 const VkAllocationCallbacks* alloc,
203 VkImage *pImage)
204 {
205 ANV_FROM_HANDLE(anv_device, device, _device);
206 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
207 struct anv_image *image = NULL;
208 VkResult r;
209
210 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
211
212 anv_assert(pCreateInfo->mipLevels > 0);
213 anv_assert(pCreateInfo->arrayLayers > 0);
214 anv_assert(pCreateInfo->samples > 0);
215 anv_assert(pCreateInfo->extent.width > 0);
216 anv_assert(pCreateInfo->extent.height > 0);
217 anv_assert(pCreateInfo->extent.depth > 0);
218
219 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
220 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
221 if (!image)
222 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
223
224 memset(image, 0, sizeof(*image));
225 image->type = pCreateInfo->imageType;
226 image->extent = pCreateInfo->extent;
227 image->vk_format = pCreateInfo->format;
228 image->aspects = vk_format_aspects(image->vk_format);
229 image->levels = pCreateInfo->mipLevels;
230 image->array_size = pCreateInfo->arrayLayers;
231 image->samples = pCreateInfo->samples;
232 image->usage = anv_image_get_full_usage(pCreateInfo, image->aspects);
233 image->tiling = pCreateInfo->tiling;
234
235 uint32_t b;
236 for_each_bit(b, image->aspects) {
237 r = make_surface(device, image, create_info, (1 << b));
238 if (r != VK_SUCCESS)
239 goto fail;
240 }
241
242 *pImage = anv_image_to_handle(image);
243
244 return VK_SUCCESS;
245
246 fail:
247 if (image)
248 anv_free2(&device->alloc, alloc, image);
249
250 return r;
251 }
252
253 VkResult
254 anv_CreateImage(VkDevice device,
255 const VkImageCreateInfo *pCreateInfo,
256 const VkAllocationCallbacks *pAllocator,
257 VkImage *pImage)
258 {
259 return anv_image_create(device,
260 &(struct anv_image_create_info) {
261 .vk_info = pCreateInfo,
262 .isl_tiling_flags = ISL_TILING_ANY_MASK,
263 },
264 pAllocator,
265 pImage);
266 }
267
268 void
269 anv_DestroyImage(VkDevice _device, VkImage _image,
270 const VkAllocationCallbacks *pAllocator)
271 {
272 ANV_FROM_HANDLE(anv_device, device, _device);
273
274 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
275 }
276
277 static void
278 anv_surface_get_subresource_layout(struct anv_image *image,
279 struct anv_surface *surface,
280 const VkImageSubresource *subresource,
281 VkSubresourceLayout *layout)
282 {
283 /* If we are on a non-zero mip level or array slice, we need to
284 * calculate a real offset.
285 */
286 anv_assert(subresource->mipLevel == 0);
287 anv_assert(subresource->arrayLayer == 0);
288
289 layout->offset = surface->offset;
290 layout->rowPitch = surface->isl.row_pitch;
291 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
292 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
293 layout->size = surface->isl.size;
294 }
295
296 void anv_GetImageSubresourceLayout(
297 VkDevice device,
298 VkImage _image,
299 const VkImageSubresource* pSubresource,
300 VkSubresourceLayout* pLayout)
301 {
302 ANV_FROM_HANDLE(anv_image, image, _image);
303
304 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
305
306 switch (pSubresource->aspectMask) {
307 case VK_IMAGE_ASPECT_COLOR_BIT:
308 anv_surface_get_subresource_layout(image, &image->color_surface,
309 pSubresource, pLayout);
310 break;
311 case VK_IMAGE_ASPECT_DEPTH_BIT:
312 anv_surface_get_subresource_layout(image, &image->depth_surface,
313 pSubresource, pLayout);
314 break;
315 case VK_IMAGE_ASPECT_STENCIL_BIT:
316 anv_surface_get_subresource_layout(image, &image->stencil_surface,
317 pSubresource, pLayout);
318 break;
319 default:
320 assert(!"Invalid image aspect");
321 }
322 }
323
324 VkResult
325 anv_validate_CreateImageView(VkDevice _device,
326 const VkImageViewCreateInfo *pCreateInfo,
327 const VkAllocationCallbacks *pAllocator,
328 VkImageView *pView)
329 {
330 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
331 const VkImageSubresourceRange *subresource;
332
333 /* Validate structure type before dereferencing it. */
334 assert(pCreateInfo);
335 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
336 subresource = &pCreateInfo->subresourceRange;
337
338 /* Validate viewType is in range before using it. */
339 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
340 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
341
342 /* Validate format is in range before using it. */
343 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
344 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
345
346 /* Validate channel swizzles. */
347 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
348 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
349 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
350 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
351 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
352 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
353 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
354 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
355
356 /* Validate subresource. */
357 assert(subresource->aspectMask != 0);
358 assert(subresource->levelCount > 0);
359 assert(subresource->layerCount > 0);
360 assert(subresource->baseMipLevel < image->levels);
361 assert(subresource->baseMipLevel + anv_get_levelCount(image, subresource) <= image->levels);
362 assert(subresource->baseArrayLayer < image->array_size);
363 assert(subresource->baseArrayLayer + anv_get_layerCount(image, subresource) <= image->array_size);
364 assert(pView);
365
366 MAYBE_UNUSED const VkImageAspectFlags view_format_aspects =
367 vk_format_aspects(pCreateInfo->format);
368
369 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
370 | VK_IMAGE_ASPECT_STENCIL_BIT;
371
372 /* Validate format. */
373 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
374 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
375 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
376 assert(view_format_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
377 } else if (subresource->aspectMask & ds_flags) {
378 assert((subresource->aspectMask & ~ds_flags) == 0);
379
380 assert(pCreateInfo->format == image->vk_format);
381
382 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
383 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
384 assert(view_format_aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
385 }
386
387 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
388 /* FINISHME: Is it legal to have an R8 view of S8? */
389 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
390 assert(view_format_aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
391 }
392 } else {
393 assert(!"bad VkImageSubresourceRange::aspectFlags");
394 }
395
396 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
397 }
398
399 static struct anv_state
400 alloc_surface_state(struct anv_device *device,
401 struct anv_cmd_buffer *cmd_buffer)
402 {
403 if (cmd_buffer) {
404 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
405 } else {
406 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
407 }
408 }
409
410 static enum isl_channel_select
411 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
412 struct anv_format_swizzle format_swizzle)
413 {
414 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
415 swizzle = component;
416
417 switch (swizzle) {
418 case VK_COMPONENT_SWIZZLE_ZERO:
419 return ISL_CHANNEL_SELECT_ZERO;
420 case VK_COMPONENT_SWIZZLE_ONE:
421 return ISL_CHANNEL_SELECT_ONE;
422 case VK_COMPONENT_SWIZZLE_R:
423 return ISL_CHANNEL_SELECT_RED + format_swizzle.r;
424 case VK_COMPONENT_SWIZZLE_G:
425 return ISL_CHANNEL_SELECT_RED + format_swizzle.g;
426 case VK_COMPONENT_SWIZZLE_B:
427 return ISL_CHANNEL_SELECT_RED + format_swizzle.b;
428 case VK_COMPONENT_SWIZZLE_A:
429 return ISL_CHANNEL_SELECT_RED + format_swizzle.a;
430 default:
431 unreachable("Invalid swizzle");
432 }
433 }
434
435 void
436 anv_image_view_init(struct anv_image_view *iview,
437 struct anv_device *device,
438 const VkImageViewCreateInfo* pCreateInfo,
439 struct anv_cmd_buffer *cmd_buffer,
440 VkImageUsageFlags usage_mask)
441 {
442 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
443 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
444
445 assert(range->layerCount > 0);
446 assert(range->baseMipLevel < image->levels);
447 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
448 VK_IMAGE_USAGE_STORAGE_BIT |
449 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
450 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
451
452 switch (image->type) {
453 default:
454 unreachable("bad VkImageType");
455 case VK_IMAGE_TYPE_1D:
456 case VK_IMAGE_TYPE_2D:
457 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
458 break;
459 case VK_IMAGE_TYPE_3D:
460 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
461 <= anv_minify(image->extent.depth, range->baseMipLevel));
462 break;
463 }
464
465 struct anv_surface *surface =
466 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
467
468 iview->image = image;
469 iview->bo = image->bo;
470 iview->offset = image->offset + surface->offset;
471
472 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
473 iview->vk_format = pCreateInfo->format;
474
475 struct anv_format format =
476 anv_get_format(pCreateInfo->format, range->aspectMask, image->tiling);
477
478 iview->base_layer = range->baseArrayLayer;
479 iview->base_mip = range->baseMipLevel;
480
481 struct isl_view isl_view = {
482 .format = format.isl_format,
483 .base_level = range->baseMipLevel,
484 .levels = anv_get_levelCount(image, range),
485 .base_array_layer = range->baseArrayLayer,
486 .array_len = anv_get_layerCount(image, range),
487 .channel_select = {
488 remap_swizzle(pCreateInfo->components.r,
489 VK_COMPONENT_SWIZZLE_R, format.swizzle),
490 remap_swizzle(pCreateInfo->components.g,
491 VK_COMPONENT_SWIZZLE_G, format.swizzle),
492 remap_swizzle(pCreateInfo->components.b,
493 VK_COMPONENT_SWIZZLE_B, format.swizzle),
494 remap_swizzle(pCreateInfo->components.a,
495 VK_COMPONENT_SWIZZLE_A, format.swizzle),
496 },
497 };
498
499 iview->extent = (VkExtent3D) {
500 .width = anv_minify(image->extent.width , range->baseMipLevel),
501 .height = anv_minify(image->extent.height, range->baseMipLevel),
502 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
503 };
504
505 isl_surf_usage_flags_t cube_usage;
506 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
507 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
508 cube_usage = ISL_SURF_USAGE_CUBE_BIT;
509 } else {
510 cube_usage = 0;
511 }
512
513 if (image->usage & usage_mask & VK_IMAGE_USAGE_SAMPLED_BIT) {
514 iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer);
515
516 isl_view.usage = cube_usage | ISL_SURF_USAGE_TEXTURE_BIT;
517 isl_surf_fill_state(&device->isl_dev,
518 iview->sampler_surface_state.map,
519 .surf = &surface->isl,
520 .view = &isl_view,
521 .mocs = device->default_mocs);
522
523 if (!device->info.has_llc)
524 anv_state_clflush(iview->sampler_surface_state);
525 } else {
526 iview->sampler_surface_state.alloc_size = 0;
527 }
528
529 if (image->usage & usage_mask & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
530 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
531
532 isl_view.usage = cube_usage | ISL_SURF_USAGE_RENDER_TARGET_BIT;
533 isl_surf_fill_state(&device->isl_dev,
534 iview->color_rt_surface_state.map,
535 .surf = &surface->isl,
536 .view = &isl_view,
537 .mocs = device->default_mocs);
538
539 if (!device->info.has_llc)
540 anv_state_clflush(iview->color_rt_surface_state);
541 } else {
542 iview->color_rt_surface_state.alloc_size = 0;
543 }
544
545 if (image->usage & usage_mask & VK_IMAGE_USAGE_STORAGE_BIT) {
546 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
547
548 if (isl_has_matching_typed_storage_image_format(&device->info,
549 format.isl_format)) {
550 isl_view.usage = cube_usage | ISL_SURF_USAGE_STORAGE_BIT;
551 isl_surf_fill_state(&device->isl_dev,
552 iview->storage_surface_state.map,
553 .surf = &surface->isl,
554 .view = &isl_view,
555 .mocs = device->default_mocs);
556 } else {
557 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
558 ISL_FORMAT_RAW,
559 iview->offset,
560 iview->bo->size - iview->offset, 1);
561 }
562
563 isl_surf_fill_image_param(&device->isl_dev,
564 &iview->storage_image_param,
565 &surface->isl, &isl_view);
566
567 if (!device->info.has_llc)
568 anv_state_clflush(iview->storage_surface_state);
569 } else {
570 iview->storage_surface_state.alloc_size = 0;
571 }
572 }
573
574 VkResult
575 anv_CreateImageView(VkDevice _device,
576 const VkImageViewCreateInfo *pCreateInfo,
577 const VkAllocationCallbacks *pAllocator,
578 VkImageView *pView)
579 {
580 ANV_FROM_HANDLE(anv_device, device, _device);
581 struct anv_image_view *view;
582
583 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
584 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
585 if (view == NULL)
586 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
587
588 anv_image_view_init(view, device, pCreateInfo, NULL, ~0);
589
590 *pView = anv_image_view_to_handle(view);
591
592 return VK_SUCCESS;
593 }
594
595 void
596 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
597 const VkAllocationCallbacks *pAllocator)
598 {
599 ANV_FROM_HANDLE(anv_device, device, _device);
600 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
601
602 if (iview->color_rt_surface_state.alloc_size > 0) {
603 anv_state_pool_free(&device->surface_state_pool,
604 iview->color_rt_surface_state);
605 }
606
607 if (iview->sampler_surface_state.alloc_size > 0) {
608 anv_state_pool_free(&device->surface_state_pool,
609 iview->sampler_surface_state);
610 }
611
612 if (iview->storage_surface_state.alloc_size > 0) {
613 anv_state_pool_free(&device->surface_state_pool,
614 iview->storage_surface_state);
615 }
616
617 anv_free2(&device->alloc, pAllocator, iview);
618 }
619
620
621 void anv_buffer_view_init(struct anv_buffer_view *view,
622 struct anv_device *device,
623 const VkBufferViewCreateInfo* pCreateInfo,
624 struct anv_cmd_buffer *cmd_buffer)
625 {
626 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
627
628 /* TODO: Handle the format swizzle? */
629
630 view->format = anv_get_isl_format(pCreateInfo->format,
631 VK_IMAGE_ASPECT_COLOR_BIT,
632 VK_IMAGE_TILING_LINEAR);
633 view->bo = buffer->bo;
634 view->offset = buffer->offset + pCreateInfo->offset;
635 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
636 buffer->size - view->offset : pCreateInfo->range;
637
638 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
639 view->surface_state = alloc_surface_state(device, cmd_buffer);
640
641 anv_fill_buffer_surface_state(device, view->surface_state,
642 view->format,
643 view->offset, view->range,
644 isl_format_get_layout(view->format)->bs);
645 } else {
646 view->surface_state = (struct anv_state){ 0 };
647 }
648
649 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
650 view->storage_surface_state = alloc_surface_state(device, cmd_buffer);
651
652 enum isl_format storage_format =
653 isl_has_matching_typed_storage_image_format(&device->info,
654 view->format) ?
655 isl_lower_storage_image_format(&device->info, view->format) :
656 ISL_FORMAT_RAW;
657
658 anv_fill_buffer_surface_state(device, view->storage_surface_state,
659 storage_format,
660 view->offset, view->range,
661 (storage_format == ISL_FORMAT_RAW ? 1 :
662 isl_format_get_layout(storage_format)->bs));
663
664 isl_buffer_fill_image_param(&device->isl_dev,
665 &view->storage_image_param,
666 view->format, view->range);
667 } else {
668 view->storage_surface_state = (struct anv_state){ 0 };
669 }
670 }
671
672 VkResult
673 anv_CreateBufferView(VkDevice _device,
674 const VkBufferViewCreateInfo *pCreateInfo,
675 const VkAllocationCallbacks *pAllocator,
676 VkBufferView *pView)
677 {
678 ANV_FROM_HANDLE(anv_device, device, _device);
679 struct anv_buffer_view *view;
680
681 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
682 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
683 if (!view)
684 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
685
686 anv_buffer_view_init(view, device, pCreateInfo, NULL);
687
688 *pView = anv_buffer_view_to_handle(view);
689
690 return VK_SUCCESS;
691 }
692
693 void
694 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
695 const VkAllocationCallbacks *pAllocator)
696 {
697 ANV_FROM_HANDLE(anv_device, device, _device);
698 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
699
700 if (view->surface_state.alloc_size > 0)
701 anv_state_pool_free(&device->surface_state_pool,
702 view->surface_state);
703
704 if (view->storage_surface_state.alloc_size > 0)
705 anv_state_pool_free(&device->surface_state_pool,
706 view->storage_surface_state);
707
708 anv_free2(&device->alloc, pAllocator, view);
709 }
710
711 struct anv_surface *
712 anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
713 {
714 switch (aspect_mask) {
715 case VK_IMAGE_ASPECT_COLOR_BIT:
716 /* Dragons will eat you.
717 *
718 * Meta attaches all destination surfaces as color render targets. Guess
719 * what surface the Meta Dragons really want.
720 */
721 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
722 return &image->depth_surface;
723 } else if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
724 return &image->stencil_surface;
725 } else {
726 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
727 return &image->color_surface;
728 }
729 break;
730 case VK_IMAGE_ASPECT_DEPTH_BIT:
731 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
732 return &image->depth_surface;
733 case VK_IMAGE_ASPECT_STENCIL_BIT:
734 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
735 return &image->stencil_surface;
736 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
737 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
738 * combined depth stencil formats. Specifically, it states:
739 *
740 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
741 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
742 *
743 * Image views with both depth and stencil aspects are only valid for
744 * render target attachments, in which case
745 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
746 * stencil surfaces from the underlying surface.
747 */
748 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
749 return &image->depth_surface;
750 } else {
751 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
752 return &image->stencil_surface;
753 }
754 default:
755 unreachable("image does not have aspect");
756 return NULL;
757 }
758 }