anv/pipeline: Add sample locations for gen7-7.5
[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 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
124 * result with an optionally provided ISL tiling argument.
125 */
126 isl_tiling_flags_t tiling_flags =
127 (vk_info->tiling == VK_IMAGE_TILING_LINEAR) ?
128 ISL_TILING_LINEAR_BIT : ISL_TILING_ANY_MASK;
129
130 if (anv_info->isl_tiling_flags)
131 tiling_flags &= anv_info->isl_tiling_flags;
132
133 assert(tiling_flags);
134
135 struct anv_surface *anv_surf = get_surface(image, aspect);
136
137 image->extent = anv_sanitize_image_extent(vk_info->imageType,
138 vk_info->extent);
139
140 enum isl_format format = anv_get_isl_format(&dev->info, vk_info->format,
141 aspect, vk_info->tiling);
142 assert(format != ISL_FORMAT_UNSUPPORTED);
143
144 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
145 .dim = vk_to_isl_surf_dim[vk_info->imageType],
146 .format = format,
147 .width = image->extent.width,
148 .height = image->extent.height,
149 .depth = image->extent.depth,
150 .levels = vk_info->mipLevels,
151 .array_len = vk_info->arrayLayers,
152 .samples = vk_info->samples,
153 .min_alignment = 0,
154 .min_pitch = anv_info->stride,
155 .usage = choose_isl_surf_usage(image->usage, aspect),
156 .tiling_flags = tiling_flags);
157
158 /* isl_surf_init() will fail only if provided invalid input. Invalid input
159 * is illegal in Vulkan.
160 */
161 assert(ok);
162
163 anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment);
164 image->size = anv_surf->offset + anv_surf->isl.size;
165 image->alignment = MAX(image->alignment, anv_surf->isl.alignment);
166
167 return VK_SUCCESS;
168 }
169
170 /**
171 * Parameter @a format is required and overrides VkImageCreateInfo::format.
172 */
173 static VkImageUsageFlags
174 anv_image_get_full_usage(const VkImageCreateInfo *info,
175 VkImageAspectFlags aspects)
176 {
177 VkImageUsageFlags usage = info->usage;
178
179 if (info->samples > 1 &&
180 (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) {
181 /* Meta will resolve the image by binding it as a texture. */
182 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
183 }
184
185 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
186 /* Meta will transfer from the image by binding it as a texture. */
187 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
188 }
189
190 if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
191 /* For non-clear transfer operations, meta will transfer to the image by
192 * binding it as a color attachment, even if the image format is not
193 * a color format.
194 */
195 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
196
197 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
198 /* vkCmdClearDepthStencilImage() only requires that
199 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
200 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
201 * clears the image, though, by binding it as a depthstencil
202 * attachment.
203 */
204 usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
205 }
206 }
207
208 return usage;
209 }
210
211 VkResult
212 anv_image_create(VkDevice _device,
213 const struct anv_image_create_info *create_info,
214 const VkAllocationCallbacks* alloc,
215 VkImage *pImage)
216 {
217 ANV_FROM_HANDLE(anv_device, device, _device);
218 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
219 struct anv_image *image = NULL;
220 VkResult r;
221
222 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
223
224 anv_assert(pCreateInfo->mipLevels > 0);
225 anv_assert(pCreateInfo->arrayLayers > 0);
226 anv_assert(pCreateInfo->samples > 0);
227 anv_assert(pCreateInfo->extent.width > 0);
228 anv_assert(pCreateInfo->extent.height > 0);
229 anv_assert(pCreateInfo->extent.depth > 0);
230
231 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
232 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
233 if (!image)
234 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
235
236 memset(image, 0, sizeof(*image));
237 image->type = pCreateInfo->imageType;
238 image->extent = pCreateInfo->extent;
239 image->vk_format = pCreateInfo->format;
240 image->aspects = vk_format_aspects(image->vk_format);
241 image->levels = pCreateInfo->mipLevels;
242 image->array_size = pCreateInfo->arrayLayers;
243 image->samples = pCreateInfo->samples;
244 image->usage = anv_image_get_full_usage(pCreateInfo, image->aspects);
245 image->tiling = pCreateInfo->tiling;
246
247 uint32_t b;
248 for_each_bit(b, image->aspects) {
249 r = make_surface(device, image, create_info, (1 << b));
250 if (r != VK_SUCCESS)
251 goto fail;
252 }
253
254 *pImage = anv_image_to_handle(image);
255
256 return VK_SUCCESS;
257
258 fail:
259 if (image)
260 anv_free2(&device->alloc, alloc, image);
261
262 return r;
263 }
264
265 VkResult
266 anv_CreateImage(VkDevice device,
267 const VkImageCreateInfo *pCreateInfo,
268 const VkAllocationCallbacks *pAllocator,
269 VkImage *pImage)
270 {
271 return anv_image_create(device,
272 &(struct anv_image_create_info) {
273 .vk_info = pCreateInfo,
274 },
275 pAllocator,
276 pImage);
277 }
278
279 void
280 anv_DestroyImage(VkDevice _device, VkImage _image,
281 const VkAllocationCallbacks *pAllocator)
282 {
283 ANV_FROM_HANDLE(anv_device, device, _device);
284
285 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
286 }
287
288 static void
289 anv_surface_get_subresource_layout(struct anv_image *image,
290 struct anv_surface *surface,
291 const VkImageSubresource *subresource,
292 VkSubresourceLayout *layout)
293 {
294 /* If we are on a non-zero mip level or array slice, we need to
295 * calculate a real offset.
296 */
297 anv_assert(subresource->mipLevel == 0);
298 anv_assert(subresource->arrayLayer == 0);
299
300 layout->offset = surface->offset;
301 layout->rowPitch = surface->isl.row_pitch;
302 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
303 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
304 layout->size = surface->isl.size;
305 }
306
307 void anv_GetImageSubresourceLayout(
308 VkDevice device,
309 VkImage _image,
310 const VkImageSubresource* pSubresource,
311 VkSubresourceLayout* pLayout)
312 {
313 ANV_FROM_HANDLE(anv_image, image, _image);
314
315 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
316
317 switch (pSubresource->aspectMask) {
318 case VK_IMAGE_ASPECT_COLOR_BIT:
319 anv_surface_get_subresource_layout(image, &image->color_surface,
320 pSubresource, pLayout);
321 break;
322 case VK_IMAGE_ASPECT_DEPTH_BIT:
323 anv_surface_get_subresource_layout(image, &image->depth_surface,
324 pSubresource, pLayout);
325 break;
326 case VK_IMAGE_ASPECT_STENCIL_BIT:
327 anv_surface_get_subresource_layout(image, &image->stencil_surface,
328 pSubresource, pLayout);
329 break;
330 default:
331 assert(!"Invalid image aspect");
332 }
333 }
334
335 VkResult
336 anv_validate_CreateImageView(VkDevice _device,
337 const VkImageViewCreateInfo *pCreateInfo,
338 const VkAllocationCallbacks *pAllocator,
339 VkImageView *pView)
340 {
341 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
342 const VkImageSubresourceRange *subresource;
343
344 /* Validate structure type before dereferencing it. */
345 assert(pCreateInfo);
346 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
347 subresource = &pCreateInfo->subresourceRange;
348
349 /* Validate viewType is in range before using it. */
350 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
351 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
352
353 /* Validate format is in range before using it. */
354 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
355 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
356
357 /* Validate channel swizzles. */
358 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
359 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
360 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
361 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
362 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
363 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
364 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
365 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
366
367 /* Validate subresource. */
368 assert(subresource->aspectMask != 0);
369 assert(subresource->levelCount > 0);
370 assert(subresource->layerCount > 0);
371 assert(subresource->baseMipLevel < image->levels);
372 assert(subresource->baseMipLevel + anv_get_levelCount(image, subresource) <= image->levels);
373 assert(subresource->baseArrayLayer < image->array_size);
374 assert(subresource->baseArrayLayer + anv_get_layerCount(image, subresource) <= image->array_size);
375 assert(pView);
376
377 MAYBE_UNUSED const VkImageAspectFlags view_format_aspects =
378 vk_format_aspects(pCreateInfo->format);
379
380 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
381 | VK_IMAGE_ASPECT_STENCIL_BIT;
382
383 /* Validate format. */
384 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
385 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
386 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
387 assert(view_format_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
388 } else if (subresource->aspectMask & ds_flags) {
389 assert((subresource->aspectMask & ~ds_flags) == 0);
390
391 assert(pCreateInfo->format == image->vk_format);
392
393 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
394 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
395 assert(view_format_aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
396 }
397
398 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
399 /* FINISHME: Is it legal to have an R8 view of S8? */
400 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
401 assert(view_format_aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
402 }
403 } else {
404 assert(!"bad VkImageSubresourceRange::aspectFlags");
405 }
406
407 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
408 }
409
410 static struct anv_state
411 alloc_surface_state(struct anv_device *device,
412 struct anv_cmd_buffer *cmd_buffer)
413 {
414 if (cmd_buffer) {
415 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
416 } else {
417 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
418 }
419 }
420
421 static enum isl_channel_select
422 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
423 struct anv_format_swizzle format_swizzle)
424 {
425 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
426 swizzle = component;
427
428 switch (swizzle) {
429 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
430 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
431 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
432 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
433 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
434 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
435 default:
436 unreachable("Invalid swizzle");
437 }
438 }
439
440 void
441 anv_image_view_init(struct anv_image_view *iview,
442 struct anv_device *device,
443 const VkImageViewCreateInfo* pCreateInfo,
444 struct anv_cmd_buffer *cmd_buffer,
445 VkImageUsageFlags usage_mask)
446 {
447 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
448 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
449
450 assert(range->layerCount > 0);
451 assert(range->baseMipLevel < image->levels);
452 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
453 VK_IMAGE_USAGE_STORAGE_BIT |
454 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
455 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
456
457 switch (image->type) {
458 default:
459 unreachable("bad VkImageType");
460 case VK_IMAGE_TYPE_1D:
461 case VK_IMAGE_TYPE_2D:
462 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
463 break;
464 case VK_IMAGE_TYPE_3D:
465 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
466 <= anv_minify(image->extent.depth, range->baseMipLevel));
467 break;
468 }
469
470 struct anv_surface *surface =
471 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
472
473 iview->image = image;
474 iview->bo = image->bo;
475 iview->offset = image->offset + surface->offset;
476
477 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
478 iview->vk_format = pCreateInfo->format;
479
480 struct anv_format format = anv_get_format(&device->info, pCreateInfo->format,
481 range->aspectMask, image->tiling);
482
483 iview->base_layer = range->baseArrayLayer;
484 iview->base_mip = range->baseMipLevel;
485
486 struct isl_view isl_view = {
487 .format = format.isl_format,
488 .base_level = range->baseMipLevel,
489 .levels = anv_get_levelCount(image, range),
490 .base_array_layer = range->baseArrayLayer,
491 .array_len = anv_get_layerCount(image, range),
492 .channel_select = {
493 remap_swizzle(pCreateInfo->components.r,
494 VK_COMPONENT_SWIZZLE_R, format.swizzle),
495 remap_swizzle(pCreateInfo->components.g,
496 VK_COMPONENT_SWIZZLE_G, format.swizzle),
497 remap_swizzle(pCreateInfo->components.b,
498 VK_COMPONENT_SWIZZLE_B, format.swizzle),
499 remap_swizzle(pCreateInfo->components.a,
500 VK_COMPONENT_SWIZZLE_A, format.swizzle),
501 },
502 };
503
504 iview->extent = (VkExtent3D) {
505 .width = anv_minify(image->extent.width , range->baseMipLevel),
506 .height = anv_minify(image->extent.height, range->baseMipLevel),
507 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
508 };
509
510 isl_surf_usage_flags_t cube_usage;
511 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
512 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
513 cube_usage = ISL_SURF_USAGE_CUBE_BIT;
514 } else {
515 cube_usage = 0;
516 }
517
518 if (image->usage & usage_mask & VK_IMAGE_USAGE_SAMPLED_BIT) {
519 iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer);
520
521 isl_view.usage = cube_usage | ISL_SURF_USAGE_TEXTURE_BIT;
522 isl_surf_fill_state(&device->isl_dev,
523 iview->sampler_surface_state.map,
524 .surf = &surface->isl,
525 .view = &isl_view,
526 .mocs = device->default_mocs);
527
528 if (!device->info.has_llc)
529 anv_state_clflush(iview->sampler_surface_state);
530 } else {
531 iview->sampler_surface_state.alloc_size = 0;
532 }
533
534 if (image->usage & usage_mask & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
535 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
536
537 isl_view.usage = cube_usage | ISL_SURF_USAGE_RENDER_TARGET_BIT;
538 isl_surf_fill_state(&device->isl_dev,
539 iview->color_rt_surface_state.map,
540 .surf = &surface->isl,
541 .view = &isl_view,
542 .mocs = device->default_mocs);
543
544 if (!device->info.has_llc)
545 anv_state_clflush(iview->color_rt_surface_state);
546 } else {
547 iview->color_rt_surface_state.alloc_size = 0;
548 }
549
550 /* NOTE: This one needs to go last since it may stomp isl_view.format */
551 if (image->usage & usage_mask & VK_IMAGE_USAGE_STORAGE_BIT) {
552 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
553
554 if (isl_has_matching_typed_storage_image_format(&device->info,
555 format.isl_format)) {
556 isl_view.usage = cube_usage | ISL_SURF_USAGE_STORAGE_BIT;
557 isl_view.format = isl_lower_storage_image_format(&device->info,
558 isl_view.format);
559 isl_surf_fill_state(&device->isl_dev,
560 iview->storage_surface_state.map,
561 .surf = &surface->isl,
562 .view = &isl_view,
563 .mocs = device->default_mocs);
564 } else {
565 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
566 ISL_FORMAT_RAW,
567 iview->offset,
568 iview->bo->size - iview->offset, 1);
569 }
570
571 isl_surf_fill_image_param(&device->isl_dev,
572 &iview->storage_image_param,
573 &surface->isl, &isl_view);
574
575 if (!device->info.has_llc)
576 anv_state_clflush(iview->storage_surface_state);
577 } else {
578 iview->storage_surface_state.alloc_size = 0;
579 }
580 }
581
582 VkResult
583 anv_CreateImageView(VkDevice _device,
584 const VkImageViewCreateInfo *pCreateInfo,
585 const VkAllocationCallbacks *pAllocator,
586 VkImageView *pView)
587 {
588 ANV_FROM_HANDLE(anv_device, device, _device);
589 struct anv_image_view *view;
590
591 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
592 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
593 if (view == NULL)
594 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
595
596 anv_image_view_init(view, device, pCreateInfo, NULL, ~0);
597
598 *pView = anv_image_view_to_handle(view);
599
600 return VK_SUCCESS;
601 }
602
603 void
604 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
605 const VkAllocationCallbacks *pAllocator)
606 {
607 ANV_FROM_HANDLE(anv_device, device, _device);
608 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
609
610 if (iview->color_rt_surface_state.alloc_size > 0) {
611 anv_state_pool_free(&device->surface_state_pool,
612 iview->color_rt_surface_state);
613 }
614
615 if (iview->sampler_surface_state.alloc_size > 0) {
616 anv_state_pool_free(&device->surface_state_pool,
617 iview->sampler_surface_state);
618 }
619
620 if (iview->storage_surface_state.alloc_size > 0) {
621 anv_state_pool_free(&device->surface_state_pool,
622 iview->storage_surface_state);
623 }
624
625 anv_free2(&device->alloc, pAllocator, iview);
626 }
627
628
629 void anv_buffer_view_init(struct anv_buffer_view *view,
630 struct anv_device *device,
631 const VkBufferViewCreateInfo* pCreateInfo,
632 struct anv_cmd_buffer *cmd_buffer)
633 {
634 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
635
636 /* TODO: Handle the format swizzle? */
637
638 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
639 VK_IMAGE_ASPECT_COLOR_BIT,
640 VK_IMAGE_TILING_LINEAR);
641 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
642 view->bo = buffer->bo;
643 view->offset = buffer->offset + pCreateInfo->offset;
644 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
645 buffer->size - pCreateInfo->offset : pCreateInfo->range;
646 view->range = align_down_npot_u32(view->range, format_bs);
647
648 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
649 view->surface_state = alloc_surface_state(device, cmd_buffer);
650
651 anv_fill_buffer_surface_state(device, view->surface_state,
652 view->format,
653 view->offset, view->range, format_bs);
654 } else {
655 view->surface_state = (struct anv_state){ 0 };
656 }
657
658 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
659 view->storage_surface_state = alloc_surface_state(device, cmd_buffer);
660
661 enum isl_format storage_format =
662 isl_has_matching_typed_storage_image_format(&device->info,
663 view->format) ?
664 isl_lower_storage_image_format(&device->info, view->format) :
665 ISL_FORMAT_RAW;
666
667 anv_fill_buffer_surface_state(device, view->storage_surface_state,
668 storage_format,
669 view->offset, view->range,
670 (storage_format == ISL_FORMAT_RAW ? 1 :
671 isl_format_get_layout(storage_format)->bpb / 8));
672
673 isl_buffer_fill_image_param(&device->isl_dev,
674 &view->storage_image_param,
675 view->format, view->range);
676 } else {
677 view->storage_surface_state = (struct anv_state){ 0 };
678 }
679 }
680
681 VkResult
682 anv_CreateBufferView(VkDevice _device,
683 const VkBufferViewCreateInfo *pCreateInfo,
684 const VkAllocationCallbacks *pAllocator,
685 VkBufferView *pView)
686 {
687 ANV_FROM_HANDLE(anv_device, device, _device);
688 struct anv_buffer_view *view;
689
690 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
691 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
692 if (!view)
693 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
694
695 anv_buffer_view_init(view, device, pCreateInfo, NULL);
696
697 *pView = anv_buffer_view_to_handle(view);
698
699 return VK_SUCCESS;
700 }
701
702 void
703 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
704 const VkAllocationCallbacks *pAllocator)
705 {
706 ANV_FROM_HANDLE(anv_device, device, _device);
707 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
708
709 if (view->surface_state.alloc_size > 0)
710 anv_state_pool_free(&device->surface_state_pool,
711 view->surface_state);
712
713 if (view->storage_surface_state.alloc_size > 0)
714 anv_state_pool_free(&device->surface_state_pool,
715 view->storage_surface_state);
716
717 anv_free2(&device->alloc, pAllocator, view);
718 }
719
720 struct anv_surface *
721 anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
722 {
723 switch (aspect_mask) {
724 case VK_IMAGE_ASPECT_COLOR_BIT:
725 /* Dragons will eat you.
726 *
727 * Meta attaches all destination surfaces as color render targets. Guess
728 * what surface the Meta Dragons really want.
729 */
730 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
731 return &image->depth_surface;
732 } else if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
733 return &image->stencil_surface;
734 } else {
735 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
736 return &image->color_surface;
737 }
738 break;
739 case VK_IMAGE_ASPECT_DEPTH_BIT:
740 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
741 return &image->depth_surface;
742 case VK_IMAGE_ASPECT_STENCIL_BIT:
743 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
744 return &image->stencil_surface;
745 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
746 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
747 * combined depth stencil formats. Specifically, it states:
748 *
749 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
750 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
751 *
752 * Image views with both depth and stencil aspects are only valid for
753 * render target attachments, in which case
754 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
755 * stencil surfaces from the underlying surface.
756 */
757 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
758 return &image->depth_surface;
759 } else {
760 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
761 return &image->stencil_surface;
762 }
763 default:
764 unreachable("image does not have aspect");
765 return NULL;
766 }
767 }