blob: 03a8cb88d90ff7766458d703adc5c08386d95382 [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,
Nanley Chery4eab37d2016-03-21 10:41:06 -0700141 .min_pitch = anv_info->stride,
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
Jason Ekstrandded57c32016-02-20 11:45:50 -0800427static enum isl_channel_select
Jordan Justenc20f78d2016-01-26 11:10:56 -0800428remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
429 struct anv_format_swizzle format_swizzle)
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800430{
431 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
Jordan Justenc20f78d2016-01-26 11:10:56 -0800432 swizzle = component;
433
434 switch (swizzle) {
435 case VK_COMPONENT_SWIZZLE_ZERO:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800436 return ISL_CHANNEL_SELECT_ZERO;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800437 case VK_COMPONENT_SWIZZLE_ONE:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800438 return ISL_CHANNEL_SELECT_ONE;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800439 case VK_COMPONENT_SWIZZLE_R:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800440 return ISL_CHANNEL_SELECT_RED + format_swizzle.r;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800441 case VK_COMPONENT_SWIZZLE_G:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800442 return ISL_CHANNEL_SELECT_RED + format_swizzle.g;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800443 case VK_COMPONENT_SWIZZLE_B:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800444 return ISL_CHANNEL_SELECT_RED + format_swizzle.b;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800445 case VK_COMPONENT_SWIZZLE_A:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800446 return ISL_CHANNEL_SELECT_RED + format_swizzle.a;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800447 default:
448 unreachable("Invalid swizzle");
449 }
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800450}
451
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800452void
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700453anv_image_view_init(struct anv_image_view *iview,
454 struct anv_device *device,
455 const VkImageViewCreateInfo* pCreateInfo,
Nanley Chery6a579de2016-01-26 18:53:21 -0800456 struct anv_cmd_buffer *cmd_buffer,
Jason Ekstrande9d126f2016-02-24 19:49:12 -0800457 VkImageUsageFlags usage_mask)
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700458{
Chad Versace44143a12015-10-06 18:17:09 -0700459 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace24de3d42015-10-06 19:11:58 -0700460 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Chad Versace44143a12015-10-06 18:17:09 -0700461
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800462 assert(range->layerCount > 0);
Chad Versace24de3d42015-10-06 19:11:58 -0700463 assert(range->baseMipLevel < image->levels);
Chad Versace44143a12015-10-06 18:17:09 -0700464 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
Chad Versace24de3d42015-10-06 19:11:58 -0700465 VK_IMAGE_USAGE_STORAGE_BIT |
466 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Chad Versace0ca3c842015-10-07 11:39:49 -0700467 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
Chad Versace24de3d42015-10-06 19:11:58 -0700468
469 switch (image->type) {
470 default:
471 unreachable("bad VkImageType");
472 case VK_IMAGE_TYPE_1D:
473 case VK_IMAGE_TYPE_2D:
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800474 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
Chad Versace24de3d42015-10-06 19:11:58 -0700475 break;
476 case VK_IMAGE_TYPE_3D:
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800477 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
Chad Versace24de3d42015-10-06 19:11:58 -0700478 <= anv_minify(image->extent.depth, range->baseMipLevel));
479 break;
480 }
Chad Versace44143a12015-10-06 18:17:09 -0700481
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800482 struct anv_surface *surface =
483 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
484
485 iview->image = image;
486 iview->bo = image->bo;
Jason Ekstrandb377c1d2016-03-31 09:32:05 -0700487 iview->offset = image->offset + surface->offset;
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800488
489 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800490 iview->vk_format = pCreateInfo->format;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800491
492 struct anv_format_swizzle swizzle;
Jason Ekstrand9d5b8f72016-02-20 21:40:25 -0800493 enum isl_format format = anv_get_isl_format(pCreateInfo->format,
494 range->aspectMask,
495 image->tiling, &swizzle);
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800496
Jason Ekstrandaa9987a2016-01-05 13:54:02 -0800497 iview->base_layer = range->baseArrayLayer;
498 iview->base_mip = range->baseMipLevel;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800499
Jason Ekstrandded57c32016-02-20 11:45:50 -0800500 struct isl_view isl_view = {
Jason Ekstrand9d5b8f72016-02-20 21:40:25 -0800501 .format = format,
Jason Ekstrandded57c32016-02-20 11:45:50 -0800502 .base_level = range->baseMipLevel,
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800503 .levels = anv_get_levelCount(image, range),
Jason Ekstrandded57c32016-02-20 11:45:50 -0800504 .base_array_layer = range->baseArrayLayer,
Nanley Chery4e75f9b2016-03-04 20:41:05 -0800505 .array_len = anv_get_layerCount(image, range),
Jason Ekstrandded57c32016-02-20 11:45:50 -0800506 .channel_select = {
507 remap_swizzle(pCreateInfo->components.r,
508 VK_COMPONENT_SWIZZLE_R, swizzle),
509 remap_swizzle(pCreateInfo->components.g,
510 VK_COMPONENT_SWIZZLE_G, swizzle),
511 remap_swizzle(pCreateInfo->components.b,
512 VK_COMPONENT_SWIZZLE_B, swizzle),
513 remap_swizzle(pCreateInfo->components.a,
514 VK_COMPONENT_SWIZZLE_A, swizzle),
515 },
516 };
517
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800518 iview->extent = (VkExtent3D) {
Jason Ekstrandded57c32016-02-20 11:45:50 -0800519 .width = anv_minify(image->extent.width , range->baseMipLevel),
520 .height = anv_minify(image->extent.height, range->baseMipLevel),
521 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800522 };
523
Jason Ekstrandded57c32016-02-20 11:45:50 -0800524 isl_surf_usage_flags_t cube_usage;
525 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
526 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
527 cube_usage = ISL_SURF_USAGE_CUBE_BIT;
528 } else {
529 cube_usage = 0;
530 }
531
Jason Ekstrande9d126f2016-02-24 19:49:12 -0800532 if (image->usage & usage_mask & VK_IMAGE_USAGE_SAMPLED_BIT) {
Chad Versace42b93202016-02-04 11:41:59 -0800533 iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800534
Jason Ekstrandded57c32016-02-20 11:45:50 -0800535 isl_view.usage = cube_usage | ISL_SURF_USAGE_TEXTURE_BIT;
536 isl_surf_fill_state(&device->isl_dev,
537 iview->sampler_surface_state.map,
538 .surf = &surface->isl,
539 .view = &isl_view,
Nanley Cheryb80c8eb2016-03-03 15:40:13 -0800540 .mocs = device->default_mocs);
Jason Ekstrandded57c32016-02-20 11:45:50 -0800541
542 if (!device->info.has_llc)
543 anv_state_clflush(iview->sampler_surface_state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800544 } else {
Chad Versace42b93202016-02-04 11:41:59 -0800545 iview->sampler_surface_state.alloc_size = 0;
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800546 }
547
Jason Ekstrande9d126f2016-02-24 19:49:12 -0800548 if (image->usage & usage_mask & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800549 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
550
Jason Ekstrandded57c32016-02-20 11:45:50 -0800551 isl_view.usage = cube_usage | ISL_SURF_USAGE_RENDER_TARGET_BIT;
552 isl_surf_fill_state(&device->isl_dev,
553 iview->color_rt_surface_state.map,
554 .surf = &surface->isl,
555 .view = &isl_view,
Nanley Cheryb80c8eb2016-03-03 15:40:13 -0800556 .mocs = device->default_mocs);
Jason Ekstrandded57c32016-02-20 11:45:50 -0800557
558 if (!device->info.has_llc)
559 anv_state_clflush(iview->color_rt_surface_state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800560 } else {
561 iview->color_rt_surface_state.alloc_size = 0;
562 }
563
Jason Ekstrande9d126f2016-02-24 19:49:12 -0800564 if (image->usage & usage_mask & VK_IMAGE_USAGE_STORAGE_BIT) {
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800565 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
566
Jason Ekstrandca8c5992016-04-15 17:08:18 -0700567 if (isl_has_matching_typed_storage_image_format(&device->info, format)) {
Jason Ekstrandded57c32016-02-20 11:45:50 -0800568 isl_view.usage = cube_usage | ISL_SURF_USAGE_STORAGE_BIT;
569 isl_surf_fill_state(&device->isl_dev,
570 iview->storage_surface_state.map,
571 .surf = &surface->isl,
572 .view = &isl_view,
Nanley Cheryb80c8eb2016-03-03 15:40:13 -0800573 .mocs = device->default_mocs);
Jason Ekstrandded57c32016-02-20 11:45:50 -0800574 } else {
Francisco Jerez6840cc12016-01-26 14:50:52 -0800575 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
Francisco Jereza50dc702016-01-26 12:23:08 -0800576 ISL_FORMAT_RAW,
577 iview->offset,
578 iview->bo->size - iview->offset, 1);
Jason Ekstrandded57c32016-02-20 11:45:50 -0800579 }
Francisco Jereza50dc702016-01-26 12:23:08 -0800580
Jason Ekstrand4b34f2c2016-02-24 12:50:31 -0800581 isl_surf_fill_image_param(&device->isl_dev,
582 &iview->storage_image_param,
583 &surface->isl, &isl_view);
Jason Ekstrand73630242016-02-24 11:38:14 -0800584
Jason Ekstrandded57c32016-02-20 11:45:50 -0800585 if (!device->info.has_llc)
586 anv_state_clflush(iview->storage_surface_state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800587 } else {
588 iview->storage_surface_state.alloc_size = 0;
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700589 }
590}
591
Chad Versace5b04db72015-07-06 16:24:28 -0700592VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700593anv_CreateImageView(VkDevice _device,
594 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800595 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700596 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700597{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700598 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700599 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700600
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800601 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
602 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700603 if (view == NULL)
604 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
605
Jason Ekstrandb377c1d2016-03-31 09:32:05 -0700606 anv_image_view_init(view, device, pCreateInfo, NULL, ~0);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700607
608 *pView = anv_image_view_to_handle(view);
609
610 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700611}
612
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800613void
614anv_DestroyImageView(VkDevice _device, VkImageView _iview,
615 const VkAllocationCallbacks *pAllocator)
Chad Versace24de3d42015-10-06 19:11:58 -0700616{
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800617 ANV_FROM_HANDLE(anv_device, device, _device);
618 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
619
Chad Versace3eebf362016-02-04 11:58:05 -0800620 if (iview->color_rt_surface_state.alloc_size > 0) {
Chad Versace24de3d42015-10-06 19:11:58 -0700621 anv_state_pool_free(&device->surface_state_pool,
622 iview->color_rt_surface_state);
623 }
624
Chad Versace3eebf362016-02-04 11:58:05 -0800625 if (iview->sampler_surface_state.alloc_size > 0) {
Chad Versace24de3d42015-10-06 19:11:58 -0700626 anv_state_pool_free(&device->surface_state_pool,
Chad Versace42b93202016-02-04 11:41:59 -0800627 iview->sampler_surface_state);
Chad Versace24de3d42015-10-06 19:11:58 -0700628 }
629
Chad Versace3eebf362016-02-04 11:58:05 -0800630 if (iview->storage_surface_state.alloc_size > 0) {
Jason Ekstrandff05f632015-12-07 17:17:30 -0800631 anv_state_pool_free(&device->surface_state_pool,
632 iview->storage_surface_state);
633 }
634
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800635 anv_free2(&device->alloc, pAllocator, iview);
Jason Ekstrand84783502015-07-10 20:18:52 -0700636}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700637
Jason Ekstrandc5618602015-12-12 16:11:23 -0800638
Jason Ekstrand4caba942016-03-30 15:20:11 -0700639void anv_buffer_view_init(struct anv_buffer_view *view,
640 struct anv_device *device,
641 const VkBufferViewCreateInfo* pCreateInfo,
642 struct anv_cmd_buffer *cmd_buffer)
643{
644 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
Jason Ekstrandc5618602015-12-12 16:11:23 -0800645
Jason Ekstrandc5618602015-12-12 16:11:23 -0800646 const struct anv_format *format =
647 anv_format_for_vk_format(pCreateInfo->format);
648
Chad Versace4d037b52016-02-08 18:51:52 -0800649 view->format = format->isl_format;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800650 view->bo = buffer->bo;
651 view->offset = buffer->offset + pCreateInfo->offset;
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800652 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
653 buffer->size - view->offset : pCreateInfo->range;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800654
655 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
Jason Ekstrand4caba942016-03-30 15:20:11 -0700656 view->surface_state = alloc_surface_state(device, cmd_buffer);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800657
Francisco Jerez6840cc12016-01-26 14:50:52 -0800658 anv_fill_buffer_surface_state(device, view->surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800659 view->format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800660 view->offset, view->range,
Chad Versace89b68dc2016-01-05 09:59:07 -0800661 format->isl_layout->bs);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800662 } else {
663 view->surface_state = (struct anv_state){ 0 };
664 }
665
666 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
Jason Ekstrand4caba942016-03-30 15:20:11 -0700667 view->storage_surface_state = alloc_surface_state(device, cmd_buffer);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800668
669 enum isl_format storage_format =
Jason Ekstrandca8c5992016-04-15 17:08:18 -0700670 isl_has_matching_typed_storage_image_format(&device->info,
671 view->format) ?
Jason Ekstrand90576ac2016-04-15 16:53:31 -0700672 isl_lower_storage_image_format(&device->info, view->format) :
Francisco Jereza50dc702016-01-26 12:23:08 -0800673 ISL_FORMAT_RAW;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800674
Francisco Jerez6840cc12016-01-26 14:50:52 -0800675 anv_fill_buffer_surface_state(device, view->storage_surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800676 storage_format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800677 view->offset, view->range,
Francisco Jereza50dc702016-01-26 12:23:08 -0800678 (storage_format == ISL_FORMAT_RAW ? 1 :
679 format->isl_layout->bs));
680
Jason Ekstrand4b34f2c2016-02-24 12:50:31 -0800681 isl_buffer_fill_image_param(&device->isl_dev,
682 &view->storage_image_param,
683 view->format, view->range);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800684 } else {
685 view->storage_surface_state = (struct anv_state){ 0 };
686 }
Jason Ekstrand4caba942016-03-30 15:20:11 -0700687}
688
689VkResult
690anv_CreateBufferView(VkDevice _device,
691 const VkBufferViewCreateInfo *pCreateInfo,
692 const VkAllocationCallbacks *pAllocator,
693 VkBufferView *pView)
694{
695 ANV_FROM_HANDLE(anv_device, device, _device);
696 struct anv_buffer_view *view;
697
698 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
699 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
700 if (!view)
701 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
702
703 anv_buffer_view_init(view, device, pCreateInfo, NULL);
Jason Ekstrandc5618602015-12-12 16:11:23 -0800704
705 *pView = anv_buffer_view_to_handle(view);
706
707 return VK_SUCCESS;
708}
709
710void
711anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
712 const VkAllocationCallbacks *pAllocator)
713{
714 ANV_FROM_HANDLE(anv_device, device, _device);
715 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
716
Jason Ekstrand783a2112015-12-14 16:51:12 -0800717 if (view->surface_state.alloc_size > 0)
718 anv_state_pool_free(&device->surface_state_pool,
719 view->surface_state);
720
721 if (view->storage_surface_state.alloc_size > 0)
722 anv_state_pool_free(&device->surface_state_pool,
723 view->storage_surface_state);
724
Jason Ekstrandc5618602015-12-12 16:11:23 -0800725 anv_free2(&device->alloc, pAllocator, view);
726}
727
Chad Versace941b48e2015-08-28 07:08:58 -0700728struct anv_surface *
Chad Versace7a089bd2015-10-05 06:48:14 -0700729anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
Chad Versace941b48e2015-08-28 07:08:58 -0700730{
Chad Versace7a089bd2015-10-05 06:48:14 -0700731 switch (aspect_mask) {
732 case VK_IMAGE_ASPECT_COLOR_BIT:
Chad Versace24de3d42015-10-06 19:11:58 -0700733 /* Dragons will eat you.
734 *
735 * Meta attaches all destination surfaces as color render targets. Guess
736 * what surface the Meta Dragons really want.
737 */
Chad Versacee6d34322016-02-08 19:07:10 -0800738 if (image->format->has_depth && image->format->has_stencil) {
Chad Versace24de3d42015-10-06 19:11:58 -0700739 return &image->depth_surface;
Chad Versacee6d34322016-02-08 19:07:10 -0800740 } else if (image->format->has_depth) {
Chad Versace24de3d42015-10-06 19:11:58 -0700741 return &image->depth_surface;
742 } else if (image->format->has_stencil) {
743 return &image->stencil_surface;
744 } else {
745 return &image->color_surface;
746 }
747 break;
Chad Versace7a089bd2015-10-05 06:48:14 -0700748 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versacee6d34322016-02-08 19:07:10 -0800749 assert(image->format->has_depth);
Chad Versace941b48e2015-08-28 07:08:58 -0700750 return &image->depth_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700751 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700752 assert(image->format->has_stencil);
Chad Versace941b48e2015-08-28 07:08:58 -0700753 return &image->stencil_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700754 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versacee6d34322016-02-08 19:07:10 -0800755 if (image->format->has_depth && image->format->has_stencil) {
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800756 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
757 * combined depth stencil formats. Specifically, it states:
Chad Versace03dd7222015-10-07 09:03:47 -0700758 *
759 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
760 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800761 *
762 * Image views with both depth and stencil aspects are only valid for
763 * render target attachments, in which case
764 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
765 * stencil surfaces from the underlying surface.
Chad Versace03dd7222015-10-07 09:03:47 -0700766 */
Chad Versace03dd7222015-10-07 09:03:47 -0700767 return &image->depth_surface;
Chad Versacee6d34322016-02-08 19:07:10 -0800768 } else if (image->format->has_depth) {
Chad Versace03dd7222015-10-07 09:03:47 -0700769 return &image->depth_surface;
770 } else if (image->format->has_stencil) {
771 return &image->stencil_surface;
772 }
773 /* fallthrough */
Chad Versace941b48e2015-08-28 07:08:58 -0700774 default:
775 unreachable("image does not have aspect");
776 return NULL;
777 }
778}