blob: b47425bd0e18d000b969b4bcaff80a39a213e548 [file] [log] [blame]
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001/*
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
Chad Versace2c2233e2015-07-17 15:04:27 -070030#include "anv_private.h"
Kristian Høgsberg769785c2015-05-08 22:32:37 -070031
Chad Versaceb3693892015-12-02 16:46:16 -080032/**
Chad Versacec5e521f2016-02-09 12:36:54 -080033 * Exactly one bit must be set in \a aspect.
Chad Versaceb3693892015-12-02 16:46:16 -080034 */
35static isl_surf_usage_flags_t
Chad Versace2f4bb002016-02-09 12:35:39 -080036choose_isl_surf_usage(VkImageUsageFlags vk_usage,
Chad Versace2f270f02015-12-04 16:29:25 -080037 VkImageAspectFlags aspect)
Chad Versaceb3693892015-12-02 16:46:16 -080038{
Chad Versacec5e521f2016-02-09 12:36:54 -080039 isl_surf_usage_flags_t isl_usage = 0;
Chad Versaceb3693892015-12-02 16:46:16 -080040
41 /* FINISHME: Support aux surfaces */
Chad Versacec5e521f2016-02-09 12:36:54 -080042 isl_usage |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
Chad Versaceb3693892015-12-02 16:46:16 -080043
Chad Versace2f4bb002016-02-09 12:35:39 -080044 if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
Chad Versacec5e521f2016-02-09 12:36:54 -080045 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
Chad Versaceb3693892015-12-02 16:46:16 -080046
Chad Versace2f4bb002016-02-09 12:35:39 -080047 if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
Chad Versacec5e521f2016-02-09 12:36:54 -080048 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
Chad Versaceb3693892015-12-02 16:46:16 -080049
Chad Versace2f4bb002016-02-09 12:35:39 -080050 if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
Chad Versacec5e521f2016-02-09 12:36:54 -080051 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
Chad Versaceb3693892015-12-02 16:46:16 -080052
Chad Versace2f4bb002016-02-09 12:35:39 -080053 if (vk_usage & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
Chad Versacec5e521f2016-02-09 12:36:54 -080054 isl_usage |= ISL_SURF_USAGE_CUBE_BIT;
Chad Versaceb3693892015-12-02 16:46:16 -080055
Chad Versace2f4bb002016-02-09 12:35:39 -080056 if (vk_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
Chad Versace2f270f02015-12-04 16:29:25 -080057 switch (aspect) {
58 default:
59 unreachable("bad VkImageAspect");
60 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versacec5e521f2016-02-09 12:36:54 -080061 isl_usage |= ISL_SURF_USAGE_DEPTH_BIT;
Chad Versace2f270f02015-12-04 16:29:25 -080062 break;
63 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versacec5e521f2016-02-09 12:36:54 -080064 isl_usage |= ISL_SURF_USAGE_STENCIL_BIT;
Chad Versace2f270f02015-12-04 16:29:25 -080065 break;
Chad Versaceb3693892015-12-02 16:46:16 -080066 }
67 }
68
Chad Versace2f4bb002016-02-09 12:35:39 -080069 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
Chad Versaceb3693892015-12-02 16:46:16 -080070 /* Meta implements transfers by sampling from the source image. */
Chad Versacec5e521f2016-02-09 12:36:54 -080071 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
Chad Versaceb3693892015-12-02 16:46:16 -080072 }
73
Chad Versace2f4bb002016-02-09 12:35:39 -080074 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
Chad Versaceb3693892015-12-02 16:46:16 -080075 /* Meta implements transfers by rendering into the destination image. */
Chad Versacec5e521f2016-02-09 12:36:54 -080076 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
Chad Versaceb3693892015-12-02 16:46:16 -080077 }
78
Chad Versacec5e521f2016-02-09 12:36:54 -080079 return isl_usage;
Chad Versaceb3693892015-12-02 16:46:16 -080080}
Chad Versace5a6b2e62015-08-17 13:50:43 -070081
82/**
Chad Versace9098e0f2015-12-07 09:22:49 -080083 * Exactly one bit must be set in \a aspect.
84 */
85static struct anv_surface *
86get_surface(struct anv_image *image, VkImageAspectFlags aspect)
87{
88 switch (aspect) {
89 default:
90 unreachable("bad VkImageAspect");
91 case VK_IMAGE_ASPECT_COLOR_BIT:
92 return &image->color_surface;
93 case VK_IMAGE_ASPECT_DEPTH_BIT:
94 return &image->depth_surface;
95 case VK_IMAGE_ASPECT_STENCIL_BIT:
96 return &image->stencil_surface;
97 }
98}
99
100/**
101 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
102 * image's memory requirements (that is, the image's size and alignment).
103 *
104 * Exactly one bit must be set in \a aspect.
Chad Versace5a6b2e62015-08-17 13:50:43 -0700105 */
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700106static VkResult
Chad Versace9098e0f2015-12-07 09:22:49 -0800107make_surface(const struct anv_device *dev,
108 struct anv_image *image,
109 const struct anv_image_create_info *anv_info,
110 VkImageAspectFlags aspect)
Chad Versacec6e76ae2015-06-26 18:48:34 -0700111{
Chad Versaceb3693892015-12-02 16:46:16 -0800112 const VkImageCreateInfo *vk_info = anv_info->vk_info;
Chad Versace3d85a282015-12-07 08:53:43 -0800113 bool ok UNUSED;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700114
Chad Versaceb3693892015-12-02 16:46:16 -0800115 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
116 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
117 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
118 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
119 };
Chad Versacec6e76ae2015-06-26 18:48:34 -0700120
Chad Versace64e8af62015-12-07 08:50:28 -0800121 isl_tiling_flags_t tiling_flags = anv_info->isl_tiling_flags;
122 if (vk_info->tiling == VK_IMAGE_TILING_LINEAR)
Nanley Chery1d9d90d2016-03-02 09:44:48 -0800123 tiling_flags = ISL_TILING_LINEAR_BIT;
Chad Versace64e8af62015-12-07 08:50:28 -0800124
Chad Versace9098e0f2015-12-07 09:22:49 -0800125 struct anv_surface *anv_surf = get_surface(image, aspect);
126
Nanley Cherya5dc3c02016-03-22 10:53:37 -0700127 image->extent = anv_sanitize_image_extent(vk_info->imageType,
128 vk_info->extent);
Nanley Chery9963af82016-02-17 17:20:03 -0800129
Chad Versace9098e0f2015-12-07 09:22:49 -0800130 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
Chad Versaceb3693892015-12-02 16:46:16 -0800131 .dim = vk_to_isl_surf_dim[vk_info->imageType],
Jordan Justenc20f78d2016-01-26 11:10:56 -0800132 .format = anv_get_isl_format(vk_info->format, aspect,
133 vk_info->tiling, NULL),
Nanley Cherya5dc3c02016-03-22 10:53:37 -0700134 .width = image->extent.width,
135 .height = image->extent.height,
136 .depth = image->extent.depth,
Chad Versaceb3693892015-12-02 16:46:16 -0800137 .levels = vk_info->mipLevels,
138 .array_len = vk_info->arrayLayers,
139 .samples = vk_info->samples,
140 .min_alignment = 0,
141 .min_pitch = 0,
Chad Versace2f4bb002016-02-09 12:35:39 -0800142 .usage = choose_isl_surf_usage(image->usage, aspect),
Chad Versace64e8af62015-12-07 08:50:28 -0800143 .tiling_flags = tiling_flags);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700144
Chad Versace3d85a282015-12-07 08:53:43 -0800145 /* isl_surf_init() will fail only if provided invalid input. Invalid input
146 * is illegal in Vulkan.
147 */
148 assert(ok);
149
Chad Versace9098e0f2015-12-07 09:22:49 -0800150 anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment);
151 image->size = anv_surf->offset + anv_surf->isl.size;
152 image->alignment = MAX(image->alignment, anv_surf->isl.alignment);
Chad Versaceb3693892015-12-02 16:46:16 -0800153
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700154 return VK_SUCCESS;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700155}
156
Chad Versace4c5dccc2016-02-09 12:41:08 -0800157/**
158 * Parameter @a format is required and overrides VkImageCreateInfo::format.
159 */
Chad Versace24de3d42015-10-06 19:11:58 -0700160static VkImageUsageFlags
Chad Versace4c5dccc2016-02-09 12:41:08 -0800161anv_image_get_full_usage(const VkImageCreateInfo *info,
162 const struct anv_format *format)
Chad Versace24de3d42015-10-06 19:11:58 -0700163{
164 VkImageUsageFlags usage = info->usage;
165
Chad Versace2bab3cd2016-01-28 06:28:01 -0800166 if (info->samples > 1 &&
167 (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) {
168 /* Meta will resolve the image by binding it as a texture. */
169 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
170 }
171
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800172 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700173 /* Meta will transfer from the image by binding it as a texture. */
174 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
175 }
176
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800177 if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
Chad Versace4c5dccc2016-02-09 12:41:08 -0800178 /* For non-clear transfer operations, meta will transfer to the image by
179 * binding it as a color attachment, even if the image format is not
180 * a color format.
Chad Versace24de3d42015-10-06 19:11:58 -0700181 */
182 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
Chad Versace4c5dccc2016-02-09 12:41:08 -0800183
184 if (anv_format_is_depth_or_stencil(format)) {
185 /* vkCmdClearDepthStencilImage() only requires that
186 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
187 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
188 * clears the image, though, by binding it as a depthstencil
189 * attachment.
190 */
191 usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
192 }
Chad Versace24de3d42015-10-06 19:11:58 -0700193 }
194
195 return usage;
196}
197
Chad Versace127cb3f2015-06-26 20:12:42 -0700198VkResult
199anv_image_create(VkDevice _device,
200 const struct anv_image_create_info *create_info,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800201 const VkAllocationCallbacks* alloc,
Chad Versace127cb3f2015-06-26 20:12:42 -0700202 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700203{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700204 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versacefdcd71f2015-06-26 20:06:08 -0700205 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700206 struct anv_image *image = NULL;
Chad Versace4c5dccc2016-02-09 12:41:08 -0800207 const struct anv_format *format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700208 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700209
210 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
211
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700212 anv_assert(pCreateInfo->mipLevels > 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800213 anv_assert(pCreateInfo->arrayLayers > 0);
Chad Versaced96d78c2016-01-22 17:16:20 -0800214 anv_assert(pCreateInfo->samples > 0);
Chad Versace5d7103e2015-06-26 09:05:46 -0700215 anv_assert(pCreateInfo->extent.width > 0);
216 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700217 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700218
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800219 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
220 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700221 if (!image)
222 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
Chad Versace67a76592015-06-26 09:17:52 -0700223
Chad Versacec6e76ae2015-06-26 18:48:34 -0700224 memset(image, 0, sizeof(*image));
225 image->type = pCreateInfo->imageType;
226 image->extent = pCreateInfo->extent;
Jason Ekstrand3200a812015-12-31 12:39:34 -0800227 image->vk_format = pCreateInfo->format;
Chad Versace4c5dccc2016-02-09 12:41:08 -0800228 image->format = format;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700229 image->levels = pCreateInfo->mipLevels;
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800230 image->array_size = pCreateInfo->arrayLayers;
Chad Versacedfcb4ee2016-01-20 16:34:23 -0800231 image->samples = pCreateInfo->samples;
Chad Versace4c5dccc2016-02-09 12:41:08 -0800232 image->usage = anv_image_get_full_usage(pCreateInfo, format);
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800233 image->tiling = pCreateInfo->tiling;
Chad Versace67a76592015-06-26 09:17:52 -0700234
Chad Versace4c5dccc2016-02-09 12:41:08 -0800235 if (likely(anv_format_is_color(format))) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800236 r = make_surface(device, image, create_info,
237 VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700238 if (r != VK_SUCCESS)
239 goto fail;
Chad Versace941b48e2015-08-28 07:08:58 -0700240 } else {
Chad Versacee6d34322016-02-08 19:07:10 -0800241 if (image->format->has_depth) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800242 r = make_surface(device, image, create_info,
243 VK_IMAGE_ASPECT_DEPTH_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700244 if (r != VK_SUCCESS)
245 goto fail;
246 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700247
Chad Versace941b48e2015-08-28 07:08:58 -0700248 if (image->format->has_stencil) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800249 r = make_surface(device, image, create_info,
250 VK_IMAGE_ASPECT_STENCIL_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700251 if (r != VK_SUCCESS)
252 goto fail;
253 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700254 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700255
Jason Ekstranda52e2082015-07-09 20:24:07 -0700256 *pImage = anv_image_to_handle(image);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700257
258 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700259
260fail:
261 if (image)
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800262 anv_free2(&device->alloc, alloc, image);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700263
264 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700265}
266
Chad Versace127cb3f2015-06-26 20:12:42 -0700267VkResult
268anv_CreateImage(VkDevice device,
269 const VkImageCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800270 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700271 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700272{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700273 return anv_image_create(device,
274 &(struct anv_image_create_info) {
275 .vk_info = pCreateInfo,
Chad Versace64e8af62015-12-07 08:50:28 -0800276 .isl_tiling_flags = ISL_TILING_ANY_MASK,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700277 },
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800278 pAllocator,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700279 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700280}
281
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700282void
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800283anv_DestroyImage(VkDevice _device, VkImage _image,
284 const VkAllocationCallbacks *pAllocator)
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700285{
286 ANV_FROM_HANDLE(anv_device, device, _device);
287
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800288 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700289}
290
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700291static void
292anv_surface_get_subresource_layout(struct anv_image *image,
293 struct anv_surface *surface,
294 const VkImageSubresource *subresource,
295 VkSubresourceLayout *layout)
296{
297 /* If we are on a non-zero mip level or array slice, we need to
298 * calculate a real offset.
299 */
300 anv_assert(subresource->mipLevel == 0);
301 anv_assert(subresource->arrayLayer == 0);
302
303 layout->offset = surface->offset;
Chad Versace981ef2f2015-12-03 08:40:47 -0800304 layout->rowPitch = surface->isl.row_pitch;
305 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
Jason Ekstrand8a81d132016-01-06 19:27:10 -0800306 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
Chad Versace981ef2f2015-12-03 08:40:47 -0800307 layout->size = surface->isl.size;
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700308}
309
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800310void anv_GetImageSubresourceLayout(
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700311 VkDevice device,
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700312 VkImage _image,
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700313 const VkImageSubresource* pSubresource,
314 VkSubresourceLayout* pLayout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700315{
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700316 ANV_FROM_HANDLE(anv_image, image, _image);
317
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800318 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
319
320 switch (pSubresource->aspectMask) {
321 case VK_IMAGE_ASPECT_COLOR_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700322 anv_surface_get_subresource_layout(image, &image->color_surface,
323 pSubresource, pLayout);
324 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800325 case VK_IMAGE_ASPECT_DEPTH_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700326 anv_surface_get_subresource_layout(image, &image->depth_surface,
327 pSubresource, pLayout);
328 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800329 case VK_IMAGE_ASPECT_STENCIL_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700330 anv_surface_get_subresource_layout(image, &image->stencil_surface,
331 pSubresource, pLayout);
332 break;
333 default:
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800334 assert(!"Invalid image aspect");
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700335 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700336}
337
Chad Versace127cb3f2015-06-26 20:12:42 -0700338VkResult
Chad Versace5b04db72015-07-06 16:24:28 -0700339anv_validate_CreateImageView(VkDevice _device,
340 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800341 const VkAllocationCallbacks *pAllocator,
Chad Versace5b04db72015-07-06 16:24:28 -0700342 VkImageView *pView)
343{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700344 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace23075bc2015-07-06 17:04:13 -0700345 const VkImageSubresourceRange *subresource;
Chad Versace23075bc2015-07-06 17:04:13 -0700346 const struct anv_format *view_format_info;
Chad Versace5b04db72015-07-06 16:24:28 -0700347
Chad Versace23075bc2015-07-06 17:04:13 -0700348 /* Validate structure type before dereferencing it. */
Chad Versace5b04db72015-07-06 16:24:28 -0700349 assert(pCreateInfo);
350 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chad Versace23075bc2015-07-06 17:04:13 -0700351 subresource = &pCreateInfo->subresourceRange;
352
Chad Versace23075bc2015-07-06 17:04:13 -0700353 /* Validate viewType is in range before using it. */
354 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
355 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700356
357 /* Validate format is in range before using it. */
358 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
359 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700360 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
361
362 /* Validate channel swizzles. */
Jason Ekstranda53f23d2015-11-30 13:06:12 -0800363 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
364 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
365 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
366 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
367 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
368 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
369 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
370 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700371
372 /* Validate subresource. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700373 assert(subresource->aspectMask != 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800374 assert(subresource->levelCount > 0);
375 assert(subresource->layerCount > 0);
Chad Versace23075bc2015-07-06 17:04:13 -0700376 assert(subresource->baseMipLevel < image->levels);
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800377 assert(subresource->baseMipLevel + anv_get_levelCount(image, subresource) <= image->levels);
Jason Ekstrand1e4263b2015-10-06 10:27:50 -0700378 assert(subresource->baseArrayLayer < image->array_size);
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800379 assert(subresource->baseArrayLayer + anv_get_layerCount(image, subresource) <= image->array_size);
Chad Versace5b04db72015-07-06 16:24:28 -0700380 assert(pView);
381
Chad Versace7a089bd2015-10-05 06:48:14 -0700382 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
383 | VK_IMAGE_ASPECT_STENCIL_BIT;
384
Chad Versace23075bc2015-07-06 17:04:13 -0700385 /* Validate format. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700386 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
387 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versacee6d34322016-02-08 19:07:10 -0800388 assert(!image->format->has_depth);
Chad Versaceded736f2015-08-17 13:10:40 -0700389 assert(!image->format->has_stencil);
Chad Versacee6d34322016-02-08 19:07:10 -0800390 assert(!view_format_info->has_depth);
Chad Versace23075bc2015-07-06 17:04:13 -0700391 assert(!view_format_info->has_stencil);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800392 assert(view_format_info->isl_layout->bs ==
393 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700394 } else if (subresource->aspectMask & ds_flags) {
395 assert((subresource->aspectMask & ~ds_flags) == 0);
396
397 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Chad Versacee6d34322016-02-08 19:07:10 -0800398 assert(image->format->has_depth);
399 assert(view_format_info->has_depth);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800400 assert(view_format_info->isl_layout->bs ==
401 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700402 }
403
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800404 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Chad Versace7a089bd2015-10-05 06:48:14 -0700405 /* FINISHME: Is it legal to have an R8 view of S8? */
406 assert(image->format->has_stencil);
407 assert(view_format_info->has_stencil);
408 }
409 } else {
410 assert(!"bad VkImageSubresourceRange::aspectFlags");
Chad Versace23075bc2015-07-06 17:04:13 -0700411 }
Chad Versace5b04db72015-07-06 16:24:28 -0700412
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800413 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
Chad Versace5b04db72015-07-06 16:24:28 -0700414}
415
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800416static struct anv_state
417alloc_surface_state(struct anv_device *device,
418 struct anv_cmd_buffer *cmd_buffer)
419{
420 if (cmd_buffer) {
421 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
422 } else {
423 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
424 }
425}
426
Francisco Jereza50dc702016-01-26 12:23:08 -0800427static bool
428has_matching_storage_typed_format(const struct anv_device *device,
429 enum isl_format format)
430{
431 return (isl_format_get_layout(format)->bs <= 4 ||
432 (isl_format_get_layout(format)->bs <= 8 &&
433 (device->info.gen >= 8 || device->info.is_haswell)) ||
434 device->info.gen >= 9);
435}
436
Jason Ekstrandded57c32016-02-20 11:45:50 -0800437static enum isl_channel_select
Jordan Justenc20f78d2016-01-26 11:10:56 -0800438remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
439 struct anv_format_swizzle format_swizzle)
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800440{
441 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
Jordan Justenc20f78d2016-01-26 11:10:56 -0800442 swizzle = component;
443
444 switch (swizzle) {
445 case VK_COMPONENT_SWIZZLE_ZERO:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800446 return ISL_CHANNEL_SELECT_ZERO;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800447 case VK_COMPONENT_SWIZZLE_ONE:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800448 return ISL_CHANNEL_SELECT_ONE;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800449 case VK_COMPONENT_SWIZZLE_R:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800450 return ISL_CHANNEL_SELECT_RED + format_swizzle.r;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800451 case VK_COMPONENT_SWIZZLE_G:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800452 return ISL_CHANNEL_SELECT_RED + format_swizzle.g;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800453 case VK_COMPONENT_SWIZZLE_B:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800454 return ISL_CHANNEL_SELECT_RED + format_swizzle.b;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800455 case VK_COMPONENT_SWIZZLE_A:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800456 return ISL_CHANNEL_SELECT_RED + format_swizzle.a;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800457 default:
458 unreachable("Invalid swizzle");
459 }
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800460}
461
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800462void
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700463anv_image_view_init(struct anv_image_view *iview,
464 struct anv_device *device,
465 const VkImageViewCreateInfo* pCreateInfo,
Nanley Chery6a579de2016-01-26 18:53:21 -0800466 struct anv_cmd_buffer *cmd_buffer,
Jason Ekstrande9d126f2016-02-24 19:49:12 -0800467 uint32_t offset,
468 VkImageUsageFlags usage_mask)
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700469{
Chad Versace44143a12015-10-06 18:17:09 -0700470 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace24de3d42015-10-06 19:11:58 -0700471 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Chad Versace44143a12015-10-06 18:17:09 -0700472
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800473 assert(range->layerCount > 0);
Chad Versace24de3d42015-10-06 19:11:58 -0700474 assert(range->baseMipLevel < image->levels);
Chad Versace44143a12015-10-06 18:17:09 -0700475 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
Chad Versace24de3d42015-10-06 19:11:58 -0700476 VK_IMAGE_USAGE_STORAGE_BIT |
477 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Chad Versace0ca3c842015-10-07 11:39:49 -0700478 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
Chad Versace24de3d42015-10-06 19:11:58 -0700479
480 switch (image->type) {
481 default:
482 unreachable("bad VkImageType");
483 case VK_IMAGE_TYPE_1D:
484 case VK_IMAGE_TYPE_2D:
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800485 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
Chad Versace24de3d42015-10-06 19:11:58 -0700486 break;
487 case VK_IMAGE_TYPE_3D:
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800488 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
Chad Versace24de3d42015-10-06 19:11:58 -0700489 <= anv_minify(image->extent.depth, range->baseMipLevel));
490 break;
491 }
Chad Versace44143a12015-10-06 18:17:09 -0700492
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800493 struct anv_surface *surface =
494 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
495
496 iview->image = image;
497 iview->bo = image->bo;
Nanley Chery6a579de2016-01-26 18:53:21 -0800498 iview->offset = image->offset + surface->offset + offset;
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800499
500 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800501 iview->vk_format = pCreateInfo->format;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800502
503 struct anv_format_swizzle swizzle;
Jason Ekstrand9d5b8f72016-02-20 21:40:25 -0800504 enum isl_format format = anv_get_isl_format(pCreateInfo->format,
505 range->aspectMask,
506 image->tiling, &swizzle);
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800507
Jason Ekstrandaa9987a2016-01-05 13:54:02 -0800508 iview->base_layer = range->baseArrayLayer;
509 iview->base_mip = range->baseMipLevel;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800510
Jason Ekstrandded57c32016-02-20 11:45:50 -0800511 struct isl_view isl_view = {
Jason Ekstrand9d5b8f72016-02-20 21:40:25 -0800512 .format = format,
Jason Ekstrandded57c32016-02-20 11:45:50 -0800513 .base_level = range->baseMipLevel,
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800514 .levels = anv_get_levelCount(image, range),
Jason Ekstrandded57c32016-02-20 11:45:50 -0800515 .base_array_layer = range->baseArrayLayer,
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800516 .array_len = anv_get_layerCount(image, range),
Jason Ekstrandded57c32016-02-20 11:45:50 -0800517 .channel_select = {
518 remap_swizzle(pCreateInfo->components.r,
519 VK_COMPONENT_SWIZZLE_R, swizzle),
520 remap_swizzle(pCreateInfo->components.g,
521 VK_COMPONENT_SWIZZLE_G, swizzle),
522 remap_swizzle(pCreateInfo->components.b,
523 VK_COMPONENT_SWIZZLE_B, swizzle),
524 remap_swizzle(pCreateInfo->components.a,
525 VK_COMPONENT_SWIZZLE_A, swizzle),
526 },
527 };
528
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800529 iview->extent = (VkExtent3D) {
Jason Ekstrandded57c32016-02-20 11:45:50 -0800530 .width = anv_minify(image->extent.width , range->baseMipLevel),
531 .height = anv_minify(image->extent.height, range->baseMipLevel),
532 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800533 };
534
Jason Ekstrandded57c32016-02-20 11:45:50 -0800535 isl_surf_usage_flags_t cube_usage;
536 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
537 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
538 cube_usage = ISL_SURF_USAGE_CUBE_BIT;
539 } else {
540 cube_usage = 0;
541 }
542
Jason Ekstrande9d126f2016-02-24 19:49:12 -0800543 if (image->usage & usage_mask & VK_IMAGE_USAGE_SAMPLED_BIT) {
Chad Versace42b93202016-02-04 11:41:59 -0800544 iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800545
Jason Ekstrandded57c32016-02-20 11:45:50 -0800546 isl_view.usage = cube_usage | ISL_SURF_USAGE_TEXTURE_BIT;
547 isl_surf_fill_state(&device->isl_dev,
548 iview->sampler_surface_state.map,
549 .surf = &surface->isl,
550 .view = &isl_view,
Nanley Cheryb80c8eb2016-03-03 15:40:13 -0800551 .mocs = device->default_mocs);
Jason Ekstrandded57c32016-02-20 11:45:50 -0800552
553 if (!device->info.has_llc)
554 anv_state_clflush(iview->sampler_surface_state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800555 } else {
Chad Versace42b93202016-02-04 11:41:59 -0800556 iview->sampler_surface_state.alloc_size = 0;
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800557 }
558
Jason Ekstrande9d126f2016-02-24 19:49:12 -0800559 if (image->usage & usage_mask & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800560 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
561
Jason Ekstrandded57c32016-02-20 11:45:50 -0800562 isl_view.usage = cube_usage | ISL_SURF_USAGE_RENDER_TARGET_BIT;
563 isl_surf_fill_state(&device->isl_dev,
564 iview->color_rt_surface_state.map,
565 .surf = &surface->isl,
566 .view = &isl_view,
Nanley Cheryb80c8eb2016-03-03 15:40:13 -0800567 .mocs = device->default_mocs);
Jason Ekstrandded57c32016-02-20 11:45:50 -0800568
569 if (!device->info.has_llc)
570 anv_state_clflush(iview->color_rt_surface_state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800571 } else {
572 iview->color_rt_surface_state.alloc_size = 0;
573 }
574
Jason Ekstrande9d126f2016-02-24 19:49:12 -0800575 if (image->usage & usage_mask & VK_IMAGE_USAGE_STORAGE_BIT) {
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800576 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
577
Jason Ekstrand9d5b8f72016-02-20 21:40:25 -0800578 if (has_matching_storage_typed_format(device, format)) {
Jason Ekstrandded57c32016-02-20 11:45:50 -0800579 isl_view.usage = cube_usage | ISL_SURF_USAGE_STORAGE_BIT;
580 isl_surf_fill_state(&device->isl_dev,
581 iview->storage_surface_state.map,
582 .surf = &surface->isl,
583 .view = &isl_view,
Nanley Cheryb80c8eb2016-03-03 15:40:13 -0800584 .mocs = device->default_mocs);
Jason Ekstrandded57c32016-02-20 11:45:50 -0800585 } else {
Francisco Jerez6840cc12016-01-26 14:50:52 -0800586 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
Francisco Jereza50dc702016-01-26 12:23:08 -0800587 ISL_FORMAT_RAW,
588 iview->offset,
589 iview->bo->size - iview->offset, 1);
Jason Ekstrandded57c32016-02-20 11:45:50 -0800590 }
Francisco Jereza50dc702016-01-26 12:23:08 -0800591
Jason Ekstrand4b34f2c2016-02-24 12:50:31 -0800592 isl_surf_fill_image_param(&device->isl_dev,
593 &iview->storage_image_param,
594 &surface->isl, &isl_view);
Jason Ekstrand73630242016-02-24 11:38:14 -0800595
Jason Ekstrandded57c32016-02-20 11:45:50 -0800596 if (!device->info.has_llc)
597 anv_state_clflush(iview->storage_surface_state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800598 } else {
599 iview->storage_surface_state.alloc_size = 0;
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700600 }
601}
602
Chad Versace5b04db72015-07-06 16:24:28 -0700603VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700604anv_CreateImageView(VkDevice _device,
605 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800606 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700607 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700608{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700609 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700610 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700611
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800612 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
613 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700614 if (view == NULL)
615 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
616
Jason Ekstrande9d126f2016-02-24 19:49:12 -0800617 anv_image_view_init(view, device, pCreateInfo, NULL, 0, ~0);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700618
619 *pView = anv_image_view_to_handle(view);
620
621 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700622}
623
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800624void
625anv_DestroyImageView(VkDevice _device, VkImageView _iview,
626 const VkAllocationCallbacks *pAllocator)
Chad Versace24de3d42015-10-06 19:11:58 -0700627{
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800628 ANV_FROM_HANDLE(anv_device, device, _device);
629 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
630
Chad Versace3eebf362016-02-04 11:58:05 -0800631 if (iview->color_rt_surface_state.alloc_size > 0) {
Chad Versace24de3d42015-10-06 19:11:58 -0700632 anv_state_pool_free(&device->surface_state_pool,
633 iview->color_rt_surface_state);
634 }
635
Chad Versace3eebf362016-02-04 11:58:05 -0800636 if (iview->sampler_surface_state.alloc_size > 0) {
Chad Versace24de3d42015-10-06 19:11:58 -0700637 anv_state_pool_free(&device->surface_state_pool,
Chad Versace42b93202016-02-04 11:41:59 -0800638 iview->sampler_surface_state);
Chad Versace24de3d42015-10-06 19:11:58 -0700639 }
640
Chad Versace3eebf362016-02-04 11:58:05 -0800641 if (iview->storage_surface_state.alloc_size > 0) {
Jason Ekstrandff05f632015-12-07 17:17:30 -0800642 anv_state_pool_free(&device->surface_state_pool,
643 iview->storage_surface_state);
644 }
645
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800646 anv_free2(&device->alloc, pAllocator, iview);
Jason Ekstrand84783502015-07-10 20:18:52 -0700647}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700648
Jason Ekstrandc5618602015-12-12 16:11:23 -0800649VkResult
650anv_CreateBufferView(VkDevice _device,
651 const VkBufferViewCreateInfo *pCreateInfo,
652 const VkAllocationCallbacks *pAllocator,
653 VkBufferView *pView)
654{
655 ANV_FROM_HANDLE(anv_device, device, _device);
656 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
657 struct anv_buffer_view *view;
658
Jason Ekstrandc5618602015-12-12 16:11:23 -0800659 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
660 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
661 if (!view)
662 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
663
Jason Ekstrandc5618602015-12-12 16:11:23 -0800664 const struct anv_format *format =
665 anv_format_for_vk_format(pCreateInfo->format);
666
Chad Versace4d037b52016-02-08 18:51:52 -0800667 view->format = format->isl_format;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800668 view->bo = buffer->bo;
669 view->offset = buffer->offset + pCreateInfo->offset;
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800670 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
671 buffer->size - view->offset : pCreateInfo->range;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800672
673 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
674 view->surface_state =
675 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
676
Francisco Jerez6840cc12016-01-26 14:50:52 -0800677 anv_fill_buffer_surface_state(device, view->surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800678 view->format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800679 view->offset, view->range,
Chad Versace89b68dc2016-01-05 09:59:07 -0800680 format->isl_layout->bs);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800681 } else {
682 view->surface_state = (struct anv_state){ 0 };
683 }
684
685 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
686 view->storage_surface_state =
687 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
688
689 enum isl_format storage_format =
Francisco Jereza50dc702016-01-26 12:23:08 -0800690 has_matching_storage_typed_format(device, view->format) ?
691 isl_lower_storage_image_format(&device->isl_dev, view->format) :
692 ISL_FORMAT_RAW;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800693
Francisco Jerez6840cc12016-01-26 14:50:52 -0800694 anv_fill_buffer_surface_state(device, view->storage_surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800695 storage_format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800696 view->offset, view->range,
Francisco Jereza50dc702016-01-26 12:23:08 -0800697 (storage_format == ISL_FORMAT_RAW ? 1 :
698 format->isl_layout->bs));
699
Jason Ekstrand4b34f2c2016-02-24 12:50:31 -0800700 isl_buffer_fill_image_param(&device->isl_dev,
701 &view->storage_image_param,
702 view->format, view->range);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800703 } else {
704 view->storage_surface_state = (struct anv_state){ 0 };
705 }
Jason Ekstrandc5618602015-12-12 16:11:23 -0800706
707 *pView = anv_buffer_view_to_handle(view);
708
709 return VK_SUCCESS;
710}
711
712void
713anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
714 const VkAllocationCallbacks *pAllocator)
715{
716 ANV_FROM_HANDLE(anv_device, device, _device);
717 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
718
Jason Ekstrand783a2112015-12-14 16:51:12 -0800719 if (view->surface_state.alloc_size > 0)
720 anv_state_pool_free(&device->surface_state_pool,
721 view->surface_state);
722
723 if (view->storage_surface_state.alloc_size > 0)
724 anv_state_pool_free(&device->surface_state_pool,
725 view->storage_surface_state);
726
Jason Ekstrandc5618602015-12-12 16:11:23 -0800727 anv_free2(&device->alloc, pAllocator, view);
728}
729
Chad Versace941b48e2015-08-28 07:08:58 -0700730struct anv_surface *
Chad Versace7a089bd2015-10-05 06:48:14 -0700731anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
Chad Versace941b48e2015-08-28 07:08:58 -0700732{
Chad Versace7a089bd2015-10-05 06:48:14 -0700733 switch (aspect_mask) {
734 case VK_IMAGE_ASPECT_COLOR_BIT:
Chad Versace24de3d42015-10-06 19:11:58 -0700735 /* Dragons will eat you.
736 *
737 * Meta attaches all destination surfaces as color render targets. Guess
738 * what surface the Meta Dragons really want.
739 */
Chad Versacee6d34322016-02-08 19:07:10 -0800740 if (image->format->has_depth && image->format->has_stencil) {
Chad Versace24de3d42015-10-06 19:11:58 -0700741 return &image->depth_surface;
Chad Versacee6d34322016-02-08 19:07:10 -0800742 } else if (image->format->has_depth) {
Chad Versace24de3d42015-10-06 19:11:58 -0700743 return &image->depth_surface;
744 } else if (image->format->has_stencil) {
745 return &image->stencil_surface;
746 } else {
747 return &image->color_surface;
748 }
749 break;
Chad Versace7a089bd2015-10-05 06:48:14 -0700750 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versacee6d34322016-02-08 19:07:10 -0800751 assert(image->format->has_depth);
Chad Versace941b48e2015-08-28 07:08:58 -0700752 return &image->depth_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700753 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700754 assert(image->format->has_stencil);
Chad Versace941b48e2015-08-28 07:08:58 -0700755 return &image->stencil_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700756 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versacee6d34322016-02-08 19:07:10 -0800757 if (image->format->has_depth && image->format->has_stencil) {
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800758 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
759 * combined depth stencil formats. Specifically, it states:
Chad Versace03dd7222015-10-07 09:03:47 -0700760 *
761 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
762 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800763 *
764 * Image views with both depth and stencil aspects are only valid for
765 * render target attachments, in which case
766 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
767 * stencil surfaces from the underlying surface.
Chad Versace03dd7222015-10-07 09:03:47 -0700768 */
Chad Versace03dd7222015-10-07 09:03:47 -0700769 return &image->depth_surface;
Chad Versacee6d34322016-02-08 19:07:10 -0800770 } else if (image->format->has_depth) {
Chad Versace03dd7222015-10-07 09:03:47 -0700771 return &image->depth_surface;
772 } else if (image->format->has_stencil) {
773 return &image->stencil_surface;
774 }
775 /* fallthrough */
Chad Versace941b48e2015-08-28 07:08:58 -0700776 default:
777 unreachable("image does not have aspect");
778 return NULL;
779 }
780}