anv/formats: Use the isl_channel_select enum for the swizzle
[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: return ISL_CHANNEL_SELECT_ZERO;
419 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
420 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
421 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
422 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
423 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
424 default:
425 unreachable("Invalid swizzle");
426 }
427 }
428
429 void
430 anv_image_view_init(struct anv_image_view *iview,
431 struct anv_device *device,
432 const VkImageViewCreateInfo* pCreateInfo,
433 struct anv_cmd_buffer *cmd_buffer,
434 VkImageUsageFlags usage_mask)
435 {
436 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
437 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
438
439 assert(range->layerCount > 0);
440 assert(range->baseMipLevel < image->levels);
441 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
442 VK_IMAGE_USAGE_STORAGE_BIT |
443 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
444 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
445
446 switch (image->type) {
447 default:
448 unreachable("bad VkImageType");
449 case VK_IMAGE_TYPE_1D:
450 case VK_IMAGE_TYPE_2D:
451 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
452 break;
453 case VK_IMAGE_TYPE_3D:
454 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
455 <= anv_minify(image->extent.depth, range->baseMipLevel));
456 break;
457 }
458
459 struct anv_surface *surface =
460 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
461
462 iview->image = image;
463 iview->bo = image->bo;
464 iview->offset = image->offset + surface->offset;
465
466 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
467 iview->vk_format = pCreateInfo->format;
468
469 struct anv_format format =
470 anv_get_format(pCreateInfo->format, range->aspectMask, image->tiling);
471
472 iview->base_layer = range->baseArrayLayer;
473 iview->base_mip = range->baseMipLevel;
474
475 struct isl_view isl_view = {
476 .format = format.isl_format,
477 .base_level = range->baseMipLevel,
478 .levels = anv_get_levelCount(image, range),
479 .base_array_layer = range->baseArrayLayer,
480 .array_len = anv_get_layerCount(image, range),
481 .channel_select = {
482 remap_swizzle(pCreateInfo->components.r,
483 VK_COMPONENT_SWIZZLE_R, format.swizzle),
484 remap_swizzle(pCreateInfo->components.g,
485 VK_COMPONENT_SWIZZLE_G, format.swizzle),
486 remap_swizzle(pCreateInfo->components.b,
487 VK_COMPONENT_SWIZZLE_B, format.swizzle),
488 remap_swizzle(pCreateInfo->components.a,
489 VK_COMPONENT_SWIZZLE_A, format.swizzle),
490 },
491 };
492
493 iview->extent = (VkExtent3D) {
494 .width = anv_minify(image->extent.width , range->baseMipLevel),
495 .height = anv_minify(image->extent.height, range->baseMipLevel),
496 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
497 };
498
499 isl_surf_usage_flags_t cube_usage;
500 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
501 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
502 cube_usage = ISL_SURF_USAGE_CUBE_BIT;
503 } else {
504 cube_usage = 0;
505 }
506
507 if (image->usage & usage_mask & VK_IMAGE_USAGE_SAMPLED_BIT) {
508 iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer);
509
510 isl_view.usage = cube_usage | ISL_SURF_USAGE_TEXTURE_BIT;
511 isl_surf_fill_state(&device->isl_dev,
512 iview->sampler_surface_state.map,
513 .surf = &surface->isl,
514 .view = &isl_view,
515 .mocs = device->default_mocs);
516
517 if (!device->info.has_llc)
518 anv_state_clflush(iview->sampler_surface_state);
519 } else {
520 iview->sampler_surface_state.alloc_size = 0;
521 }
522
523 if (image->usage & usage_mask & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
524 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
525
526 isl_view.usage = cube_usage | ISL_SURF_USAGE_RENDER_TARGET_BIT;
527 isl_surf_fill_state(&device->isl_dev,
528 iview->color_rt_surface_state.map,
529 .surf = &surface->isl,
530 .view = &isl_view,
531 .mocs = device->default_mocs);
532
533 if (!device->info.has_llc)
534 anv_state_clflush(iview->color_rt_surface_state);
535 } else {
536 iview->color_rt_surface_state.alloc_size = 0;
537 }
538
539 if (image->usage & usage_mask & VK_IMAGE_USAGE_STORAGE_BIT) {
540 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
541
542 if (isl_has_matching_typed_storage_image_format(&device->info,
543 format.isl_format)) {
544 isl_view.usage = cube_usage | ISL_SURF_USAGE_STORAGE_BIT;
545 isl_surf_fill_state(&device->isl_dev,
546 iview->storage_surface_state.map,
547 .surf = &surface->isl,
548 .view = &isl_view,
549 .mocs = device->default_mocs);
550 } else {
551 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
552 ISL_FORMAT_RAW,
553 iview->offset,
554 iview->bo->size - iview->offset, 1);
555 }
556
557 isl_surf_fill_image_param(&device->isl_dev,
558 &iview->storage_image_param,
559 &surface->isl, &isl_view);
560
561 if (!device->info.has_llc)
562 anv_state_clflush(iview->storage_surface_state);
563 } else {
564 iview->storage_surface_state.alloc_size = 0;
565 }
566 }
567
568 VkResult
569 anv_CreateImageView(VkDevice _device,
570 const VkImageViewCreateInfo *pCreateInfo,
571 const VkAllocationCallbacks *pAllocator,
572 VkImageView *pView)
573 {
574 ANV_FROM_HANDLE(anv_device, device, _device);
575 struct anv_image_view *view;
576
577 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
578 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
579 if (view == NULL)
580 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
581
582 anv_image_view_init(view, device, pCreateInfo, NULL, ~0);
583
584 *pView = anv_image_view_to_handle(view);
585
586 return VK_SUCCESS;
587 }
588
589 void
590 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
591 const VkAllocationCallbacks *pAllocator)
592 {
593 ANV_FROM_HANDLE(anv_device, device, _device);
594 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
595
596 if (iview->color_rt_surface_state.alloc_size > 0) {
597 anv_state_pool_free(&device->surface_state_pool,
598 iview->color_rt_surface_state);
599 }
600
601 if (iview->sampler_surface_state.alloc_size > 0) {
602 anv_state_pool_free(&device->surface_state_pool,
603 iview->sampler_surface_state);
604 }
605
606 if (iview->storage_surface_state.alloc_size > 0) {
607 anv_state_pool_free(&device->surface_state_pool,
608 iview->storage_surface_state);
609 }
610
611 anv_free2(&device->alloc, pAllocator, iview);
612 }
613
614
615 void anv_buffer_view_init(struct anv_buffer_view *view,
616 struct anv_device *device,
617 const VkBufferViewCreateInfo* pCreateInfo,
618 struct anv_cmd_buffer *cmd_buffer)
619 {
620 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
621
622 /* TODO: Handle the format swizzle? */
623
624 view->format = anv_get_isl_format(pCreateInfo->format,
625 VK_IMAGE_ASPECT_COLOR_BIT,
626 VK_IMAGE_TILING_LINEAR);
627 view->bo = buffer->bo;
628 view->offset = buffer->offset + pCreateInfo->offset;
629 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
630 buffer->size - view->offset : pCreateInfo->range;
631
632 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
633 view->surface_state = alloc_surface_state(device, cmd_buffer);
634
635 anv_fill_buffer_surface_state(device, view->surface_state,
636 view->format,
637 view->offset, view->range,
638 isl_format_get_layout(view->format)->bs);
639 } else {
640 view->surface_state = (struct anv_state){ 0 };
641 }
642
643 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
644 view->storage_surface_state = alloc_surface_state(device, cmd_buffer);
645
646 enum isl_format storage_format =
647 isl_has_matching_typed_storage_image_format(&device->info,
648 view->format) ?
649 isl_lower_storage_image_format(&device->info, view->format) :
650 ISL_FORMAT_RAW;
651
652 anv_fill_buffer_surface_state(device, view->storage_surface_state,
653 storage_format,
654 view->offset, view->range,
655 (storage_format == ISL_FORMAT_RAW ? 1 :
656 isl_format_get_layout(storage_format)->bs));
657
658 isl_buffer_fill_image_param(&device->isl_dev,
659 &view->storage_image_param,
660 view->format, view->range);
661 } else {
662 view->storage_surface_state = (struct anv_state){ 0 };
663 }
664 }
665
666 VkResult
667 anv_CreateBufferView(VkDevice _device,
668 const VkBufferViewCreateInfo *pCreateInfo,
669 const VkAllocationCallbacks *pAllocator,
670 VkBufferView *pView)
671 {
672 ANV_FROM_HANDLE(anv_device, device, _device);
673 struct anv_buffer_view *view;
674
675 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
676 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
677 if (!view)
678 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
679
680 anv_buffer_view_init(view, device, pCreateInfo, NULL);
681
682 *pView = anv_buffer_view_to_handle(view);
683
684 return VK_SUCCESS;
685 }
686
687 void
688 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
689 const VkAllocationCallbacks *pAllocator)
690 {
691 ANV_FROM_HANDLE(anv_device, device, _device);
692 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
693
694 if (view->surface_state.alloc_size > 0)
695 anv_state_pool_free(&device->surface_state_pool,
696 view->surface_state);
697
698 if (view->storage_surface_state.alloc_size > 0)
699 anv_state_pool_free(&device->surface_state_pool,
700 view->storage_surface_state);
701
702 anv_free2(&device->alloc, pAllocator, view);
703 }
704
705 struct anv_surface *
706 anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
707 {
708 switch (aspect_mask) {
709 case VK_IMAGE_ASPECT_COLOR_BIT:
710 /* Dragons will eat you.
711 *
712 * Meta attaches all destination surfaces as color render targets. Guess
713 * what surface the Meta Dragons really want.
714 */
715 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
716 return &image->depth_surface;
717 } else if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
718 return &image->stencil_surface;
719 } else {
720 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
721 return &image->color_surface;
722 }
723 break;
724 case VK_IMAGE_ASPECT_DEPTH_BIT:
725 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
726 return &image->depth_surface;
727 case VK_IMAGE_ASPECT_STENCIL_BIT:
728 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
729 return &image->stencil_surface;
730 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
731 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
732 * combined depth stencil formats. Specifically, it states:
733 *
734 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
735 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
736 *
737 * Image views with both depth and stencil aspects are only valid for
738 * render target attachments, in which case
739 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
740 * stencil surfaces from the underlying surface.
741 */
742 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
743 return &image->depth_surface;
744 } else {
745 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
746 return &image->stencil_surface;
747 }
748 default:
749 unreachable("image does not have aspect");
750 return NULL;
751 }
752 }