blob: 11ceea388293eae0093093b0ddcf1111b5616ae6 [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)
123 tiling_flags &= ISL_TILING_LINEAR_BIT;
124
Chad Versace9098e0f2015-12-07 09:22:49 -0800125 struct anv_surface *anv_surf = get_surface(image, aspect);
126
Kristian Høgsberg Kristensen3b9b9082016-02-17 12:21:46 -0800127 VkExtent3D extent;
128 switch (vk_info->imageType) {
129 case VK_IMAGE_TYPE_1D:
130 extent = (VkExtent3D) { vk_info->extent.width, 1, 1 };
131 break;
132 case VK_IMAGE_TYPE_2D:
133 extent = (VkExtent3D) { vk_info->extent.width, vk_info->extent.height, 1 };
134 break;
135 case VK_IMAGE_TYPE_3D:
136 extent = vk_info->extent;
137 break;
138 default:
139 unreachable("invalid image type");
140 }
141
Nanley Chery9963af82016-02-17 17:20:03 -0800142 image->extent = extent;
143
Chad Versace9098e0f2015-12-07 09:22:49 -0800144 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
Chad Versaceb3693892015-12-02 16:46:16 -0800145 .dim = vk_to_isl_surf_dim[vk_info->imageType],
Jordan Justenc20f78d2016-01-26 11:10:56 -0800146 .format = anv_get_isl_format(vk_info->format, aspect,
147 vk_info->tiling, NULL),
Kristian Høgsberg Kristensen3b9b9082016-02-17 12:21:46 -0800148 .width = extent.width,
149 .height = extent.height,
150 .depth = extent.depth,
Chad Versaceb3693892015-12-02 16:46:16 -0800151 .levels = vk_info->mipLevels,
152 .array_len = vk_info->arrayLayers,
153 .samples = vk_info->samples,
154 .min_alignment = 0,
155 .min_pitch = 0,
Chad Versace2f4bb002016-02-09 12:35:39 -0800156 .usage = choose_isl_surf_usage(image->usage, aspect),
Chad Versace64e8af62015-12-07 08:50:28 -0800157 .tiling_flags = tiling_flags);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700158
Chad Versace3d85a282015-12-07 08:53:43 -0800159 /* isl_surf_init() will fail only if provided invalid input. Invalid input
160 * is illegal in Vulkan.
161 */
162 assert(ok);
163
Chad Versace9098e0f2015-12-07 09:22:49 -0800164 anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment);
165 image->size = anv_surf->offset + anv_surf->isl.size;
166 image->alignment = MAX(image->alignment, anv_surf->isl.alignment);
Chad Versaceb3693892015-12-02 16:46:16 -0800167
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700168 return VK_SUCCESS;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700169}
170
Chad Versace4c5dccc2016-02-09 12:41:08 -0800171/**
172 * Parameter @a format is required and overrides VkImageCreateInfo::format.
173 */
Chad Versace24de3d42015-10-06 19:11:58 -0700174static VkImageUsageFlags
Chad Versace4c5dccc2016-02-09 12:41:08 -0800175anv_image_get_full_usage(const VkImageCreateInfo *info,
176 const struct anv_format *format)
Chad Versace24de3d42015-10-06 19:11:58 -0700177{
178 VkImageUsageFlags usage = info->usage;
179
Chad Versace2bab3cd2016-01-28 06:28:01 -0800180 if (info->samples > 1 &&
181 (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) {
182 /* Meta will resolve the image by binding it as a texture. */
183 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
184 }
185
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800186 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700187 /* Meta will transfer from the image by binding it as a texture. */
188 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
189 }
190
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800191 if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
Chad Versace4c5dccc2016-02-09 12:41:08 -0800192 /* For non-clear transfer operations, meta will transfer to the image by
193 * binding it as a color attachment, even if the image format is not
194 * a color format.
Chad Versace24de3d42015-10-06 19:11:58 -0700195 */
196 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
Chad Versace4c5dccc2016-02-09 12:41:08 -0800197
198 if (anv_format_is_depth_or_stencil(format)) {
199 /* vkCmdClearDepthStencilImage() only requires that
200 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
201 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
202 * clears the image, though, by binding it as a depthstencil
203 * attachment.
204 */
205 usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
206 }
Chad Versace24de3d42015-10-06 19:11:58 -0700207 }
208
209 return usage;
210}
211
Chad Versace127cb3f2015-06-26 20:12:42 -0700212VkResult
213anv_image_create(VkDevice _device,
214 const struct anv_image_create_info *create_info,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800215 const VkAllocationCallbacks* alloc,
Chad Versace127cb3f2015-06-26 20:12:42 -0700216 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700217{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700218 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versacefdcd71f2015-06-26 20:06:08 -0700219 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700220 struct anv_image *image = NULL;
Chad Versace4c5dccc2016-02-09 12:41:08 -0800221 const struct anv_format *format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700222 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700223
224 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
225
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700226 anv_assert(pCreateInfo->mipLevels > 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800227 anv_assert(pCreateInfo->arrayLayers > 0);
Chad Versaced96d78c2016-01-22 17:16:20 -0800228 anv_assert(pCreateInfo->samples > 0);
Chad Versace5d7103e2015-06-26 09:05:46 -0700229 anv_assert(pCreateInfo->extent.width > 0);
230 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700231 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700232
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800233 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
234 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700235 if (!image)
236 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
Chad Versace67a76592015-06-26 09:17:52 -0700237
Chad Versacec6e76ae2015-06-26 18:48:34 -0700238 memset(image, 0, sizeof(*image));
239 image->type = pCreateInfo->imageType;
240 image->extent = pCreateInfo->extent;
Jason Ekstrand3200a812015-12-31 12:39:34 -0800241 image->vk_format = pCreateInfo->format;
Chad Versace4c5dccc2016-02-09 12:41:08 -0800242 image->format = format;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700243 image->levels = pCreateInfo->mipLevels;
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800244 image->array_size = pCreateInfo->arrayLayers;
Chad Versacedfcb4ee2016-01-20 16:34:23 -0800245 image->samples = pCreateInfo->samples;
Chad Versace4c5dccc2016-02-09 12:41:08 -0800246 image->usage = anv_image_get_full_usage(pCreateInfo, format);
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800247 image->tiling = pCreateInfo->tiling;
Chad Versace67a76592015-06-26 09:17:52 -0700248
Chad Versace4c5dccc2016-02-09 12:41:08 -0800249 if (likely(anv_format_is_color(format))) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800250 r = make_surface(device, image, create_info,
251 VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700252 if (r != VK_SUCCESS)
253 goto fail;
Chad Versace941b48e2015-08-28 07:08:58 -0700254 } else {
Chad Versacee6d34322016-02-08 19:07:10 -0800255 if (image->format->has_depth) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800256 r = make_surface(device, image, create_info,
257 VK_IMAGE_ASPECT_DEPTH_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700258 if (r != VK_SUCCESS)
259 goto fail;
260 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700261
Chad Versace941b48e2015-08-28 07:08:58 -0700262 if (image->format->has_stencil) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800263 r = make_surface(device, image, create_info,
264 VK_IMAGE_ASPECT_STENCIL_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700265 if (r != VK_SUCCESS)
266 goto fail;
267 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700268 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700269
Jason Ekstranda52e2082015-07-09 20:24:07 -0700270 *pImage = anv_image_to_handle(image);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700271
272 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700273
274fail:
275 if (image)
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800276 anv_free2(&device->alloc, alloc, image);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700277
278 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700279}
280
Chad Versace127cb3f2015-06-26 20:12:42 -0700281VkResult
282anv_CreateImage(VkDevice device,
283 const VkImageCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800284 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700285 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700286{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700287 return anv_image_create(device,
288 &(struct anv_image_create_info) {
289 .vk_info = pCreateInfo,
Chad Versace64e8af62015-12-07 08:50:28 -0800290 .isl_tiling_flags = ISL_TILING_ANY_MASK,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700291 },
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800292 pAllocator,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700293 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700294}
295
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700296void
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800297anv_DestroyImage(VkDevice _device, VkImage _image,
298 const VkAllocationCallbacks *pAllocator)
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700299{
300 ANV_FROM_HANDLE(anv_device, device, _device);
301
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800302 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700303}
304
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700305static void
306anv_surface_get_subresource_layout(struct anv_image *image,
307 struct anv_surface *surface,
308 const VkImageSubresource *subresource,
309 VkSubresourceLayout *layout)
310{
311 /* If we are on a non-zero mip level or array slice, we need to
312 * calculate a real offset.
313 */
314 anv_assert(subresource->mipLevel == 0);
315 anv_assert(subresource->arrayLayer == 0);
316
317 layout->offset = surface->offset;
Chad Versace981ef2f2015-12-03 08:40:47 -0800318 layout->rowPitch = surface->isl.row_pitch;
319 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
Jason Ekstrand8a81d132016-01-06 19:27:10 -0800320 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
Chad Versace981ef2f2015-12-03 08:40:47 -0800321 layout->size = surface->isl.size;
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700322}
323
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800324void anv_GetImageSubresourceLayout(
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700325 VkDevice device,
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700326 VkImage _image,
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700327 const VkImageSubresource* pSubresource,
328 VkSubresourceLayout* pLayout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700329{
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700330 ANV_FROM_HANDLE(anv_image, image, _image);
331
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800332 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
333
334 switch (pSubresource->aspectMask) {
335 case VK_IMAGE_ASPECT_COLOR_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700336 anv_surface_get_subresource_layout(image, &image->color_surface,
337 pSubresource, pLayout);
338 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800339 case VK_IMAGE_ASPECT_DEPTH_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700340 anv_surface_get_subresource_layout(image, &image->depth_surface,
341 pSubresource, pLayout);
342 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800343 case VK_IMAGE_ASPECT_STENCIL_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700344 anv_surface_get_subresource_layout(image, &image->stencil_surface,
345 pSubresource, pLayout);
346 break;
347 default:
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800348 assert(!"Invalid image aspect");
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700349 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700350}
351
Chad Versace127cb3f2015-06-26 20:12:42 -0700352VkResult
Chad Versace5b04db72015-07-06 16:24:28 -0700353anv_validate_CreateImageView(VkDevice _device,
354 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800355 const VkAllocationCallbacks *pAllocator,
Chad Versace5b04db72015-07-06 16:24:28 -0700356 VkImageView *pView)
357{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700358 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace23075bc2015-07-06 17:04:13 -0700359 const VkImageSubresourceRange *subresource;
Chad Versace23075bc2015-07-06 17:04:13 -0700360 const struct anv_format *view_format_info;
Chad Versace5b04db72015-07-06 16:24:28 -0700361
Chad Versace23075bc2015-07-06 17:04:13 -0700362 /* Validate structure type before dereferencing it. */
Chad Versace5b04db72015-07-06 16:24:28 -0700363 assert(pCreateInfo);
364 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chad Versace23075bc2015-07-06 17:04:13 -0700365 subresource = &pCreateInfo->subresourceRange;
366
Chad Versace23075bc2015-07-06 17:04:13 -0700367 /* Validate viewType is in range before using it. */
368 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
369 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700370
371 /* Validate format is in range before using it. */
372 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
373 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700374 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
375
376 /* Validate channel swizzles. */
Jason Ekstranda53f23d2015-11-30 13:06:12 -0800377 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
378 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
379 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
380 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
381 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
382 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
383 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
384 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700385
386 /* Validate subresource. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700387 assert(subresource->aspectMask != 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800388 assert(subresource->levelCount > 0);
389 assert(subresource->layerCount > 0);
Chad Versace23075bc2015-07-06 17:04:13 -0700390 assert(subresource->baseMipLevel < image->levels);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800391 assert(subresource->baseMipLevel + subresource->levelCount <= image->levels);
Jason Ekstrand1e4263b2015-10-06 10:27:50 -0700392 assert(subresource->baseArrayLayer < image->array_size);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800393 assert(subresource->baseArrayLayer + subresource->layerCount <= image->array_size);
Chad Versace5b04db72015-07-06 16:24:28 -0700394 assert(pView);
395
Chad Versace7a089bd2015-10-05 06:48:14 -0700396 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
397 | VK_IMAGE_ASPECT_STENCIL_BIT;
398
Chad Versace23075bc2015-07-06 17:04:13 -0700399 /* Validate format. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700400 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
401 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versacee6d34322016-02-08 19:07:10 -0800402 assert(!image->format->has_depth);
Chad Versaceded736f2015-08-17 13:10:40 -0700403 assert(!image->format->has_stencil);
Chad Versacee6d34322016-02-08 19:07:10 -0800404 assert(!view_format_info->has_depth);
Chad Versace23075bc2015-07-06 17:04:13 -0700405 assert(!view_format_info->has_stencil);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800406 assert(view_format_info->isl_layout->bs ==
407 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700408 } else if (subresource->aspectMask & ds_flags) {
409 assert((subresource->aspectMask & ~ds_flags) == 0);
410
411 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Chad Versacee6d34322016-02-08 19:07:10 -0800412 assert(image->format->has_depth);
413 assert(view_format_info->has_depth);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800414 assert(view_format_info->isl_layout->bs ==
415 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700416 }
417
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800418 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Chad Versace7a089bd2015-10-05 06:48:14 -0700419 /* FINISHME: Is it legal to have an R8 view of S8? */
420 assert(image->format->has_stencil);
421 assert(view_format_info->has_stencil);
422 }
423 } else {
424 assert(!"bad VkImageSubresourceRange::aspectFlags");
Chad Versace23075bc2015-07-06 17:04:13 -0700425 }
Chad Versace5b04db72015-07-06 16:24:28 -0700426
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800427 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
Chad Versace5b04db72015-07-06 16:24:28 -0700428}
429
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800430static struct anv_state
431alloc_surface_state(struct anv_device *device,
432 struct anv_cmd_buffer *cmd_buffer)
433{
434 if (cmd_buffer) {
435 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
436 } else {
437 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
438 }
439}
440
Francisco Jereza50dc702016-01-26 12:23:08 -0800441static bool
442has_matching_storage_typed_format(const struct anv_device *device,
443 enum isl_format format)
444{
445 return (isl_format_get_layout(format)->bs <= 4 ||
446 (isl_format_get_layout(format)->bs <= 8 &&
447 (device->info.gen >= 8 || device->info.is_haswell)) ||
448 device->info.gen >= 9);
449}
450
Jason Ekstrandded57c32016-02-20 11:45:50 -0800451static enum isl_channel_select
Jordan Justenc20f78d2016-01-26 11:10:56 -0800452remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
453 struct anv_format_swizzle format_swizzle)
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800454{
455 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
Jordan Justenc20f78d2016-01-26 11:10:56 -0800456 swizzle = component;
457
458 switch (swizzle) {
459 case VK_COMPONENT_SWIZZLE_ZERO:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800460 return ISL_CHANNEL_SELECT_ZERO;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800461 case VK_COMPONENT_SWIZZLE_ONE:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800462 return ISL_CHANNEL_SELECT_ONE;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800463 case VK_COMPONENT_SWIZZLE_R:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800464 return ISL_CHANNEL_SELECT_RED + format_swizzle.r;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800465 case VK_COMPONENT_SWIZZLE_G:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800466 return ISL_CHANNEL_SELECT_RED + format_swizzle.g;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800467 case VK_COMPONENT_SWIZZLE_B:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800468 return ISL_CHANNEL_SELECT_RED + format_swizzle.b;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800469 case VK_COMPONENT_SWIZZLE_A:
Jason Ekstrandded57c32016-02-20 11:45:50 -0800470 return ISL_CHANNEL_SELECT_RED + format_swizzle.a;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800471 default:
472 unreachable("Invalid swizzle");
473 }
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800474}
475
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800476void
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700477anv_image_view_init(struct anv_image_view *iview,
478 struct anv_device *device,
479 const VkImageViewCreateInfo* pCreateInfo,
Nanley Chery6a579de2016-01-26 18:53:21 -0800480 struct anv_cmd_buffer *cmd_buffer,
481 uint32_t offset)
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700482{
Chad Versace44143a12015-10-06 18:17:09 -0700483 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace24de3d42015-10-06 19:11:58 -0700484 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Chad Versace44143a12015-10-06 18:17:09 -0700485
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800486 assert(range->layerCount > 0);
Chad Versace24de3d42015-10-06 19:11:58 -0700487 assert(range->baseMipLevel < image->levels);
Chad Versace44143a12015-10-06 18:17:09 -0700488 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
Chad Versace24de3d42015-10-06 19:11:58 -0700489 VK_IMAGE_USAGE_STORAGE_BIT |
490 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Chad Versace0ca3c842015-10-07 11:39:49 -0700491 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
Chad Versace24de3d42015-10-06 19:11:58 -0700492
493 switch (image->type) {
494 default:
495 unreachable("bad VkImageType");
496 case VK_IMAGE_TYPE_1D:
497 case VK_IMAGE_TYPE_2D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800498 assert(range->baseArrayLayer + range->layerCount - 1 <= image->array_size);
Chad Versace24de3d42015-10-06 19:11:58 -0700499 break;
500 case VK_IMAGE_TYPE_3D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800501 assert(range->baseArrayLayer + range->layerCount - 1
Chad Versace24de3d42015-10-06 19:11:58 -0700502 <= anv_minify(image->extent.depth, range->baseMipLevel));
503 break;
504 }
Chad Versace44143a12015-10-06 18:17:09 -0700505
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800506 struct anv_surface *surface =
507 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
508
509 iview->image = image;
510 iview->bo = image->bo;
Nanley Chery6a579de2016-01-26 18:53:21 -0800511 iview->offset = image->offset + surface->offset + offset;
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800512
513 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800514 iview->vk_format = pCreateInfo->format;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800515
516 struct anv_format_swizzle swizzle;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800517 iview->format = anv_get_isl_format(pCreateInfo->format, iview->aspect_mask,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800518 image->tiling, &swizzle);
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800519
Jason Ekstrandaa9987a2016-01-05 13:54:02 -0800520 iview->base_layer = range->baseArrayLayer;
521 iview->base_mip = range->baseMipLevel;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800522
Jason Ekstrandded57c32016-02-20 11:45:50 -0800523 struct isl_view isl_view = {
524 .format = iview->format,
525 .base_level = range->baseMipLevel,
526 .levels = range->levelCount,
527 .base_array_layer = range->baseArrayLayer,
528 .array_len = range->layerCount,
529 .channel_select = {
530 remap_swizzle(pCreateInfo->components.r,
531 VK_COMPONENT_SWIZZLE_R, swizzle),
532 remap_swizzle(pCreateInfo->components.g,
533 VK_COMPONENT_SWIZZLE_G, swizzle),
534 remap_swizzle(pCreateInfo->components.b,
535 VK_COMPONENT_SWIZZLE_B, swizzle),
536 remap_swizzle(pCreateInfo->components.a,
537 VK_COMPONENT_SWIZZLE_A, swizzle),
538 },
539 };
540
541 struct isl_extent4d level0_extent_px;
542
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800543 if (!isl_format_is_compressed(iview->format) &&
Chad Versace4d037b52016-02-08 18:51:52 -0800544 isl_format_is_compressed(image->format->isl_format)) {
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800545 /* Scale the ImageView extent by the backing Image. This is used
546 * internally when an uncompressed ImageView is created on a
547 * compressed Image. The ImageView can therefore be used for copying
548 * data from a source Image to a destination Image.
549 */
550 const struct isl_format_layout * isl_layout = image->format->isl_layout;
Nanley Chery235abfb2016-01-27 12:08:56 -0800551
Jason Ekstrandded57c32016-02-20 11:45:50 -0800552 level0_extent_px.depth = anv_minify(image->extent.depth, range->baseMipLevel);
553 level0_extent_px.depth = DIV_ROUND_UP(level0_extent_px.depth, isl_layout->bd);
Nanley Chery235abfb2016-01-27 12:08:56 -0800554
Jason Ekstrandded57c32016-02-20 11:45:50 -0800555 level0_extent_px.height = isl_surf_get_array_pitch_el_rows(&surface->isl) * image->array_size;
556 level0_extent_px.width = isl_surf_get_row_pitch_el(&surface->isl);
557 isl_view.base_level = 0;
558 isl_view.base_array_layer = 0;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800559 } else {
Jason Ekstrandded57c32016-02-20 11:45:50 -0800560 level0_extent_px.width = image->extent.width;
561 level0_extent_px.height = image->extent.height;
562 level0_extent_px.depth = image->extent.depth;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800563 }
564
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800565 iview->extent = (VkExtent3D) {
Jason Ekstrandded57c32016-02-20 11:45:50 -0800566 .width = anv_minify(image->extent.width , range->baseMipLevel),
567 .height = anv_minify(image->extent.height, range->baseMipLevel),
568 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800569 };
570
Jason Ekstrandded57c32016-02-20 11:45:50 -0800571 isl_surf_usage_flags_t cube_usage;
572 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
573 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
574 cube_usage = ISL_SURF_USAGE_CUBE_BIT;
575 } else {
576 cube_usage = 0;
577 }
578
Chad Versace3eebf362016-02-04 11:58:05 -0800579 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Chad Versace42b93202016-02-04 11:41:59 -0800580 iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800581
Jason Ekstrandded57c32016-02-20 11:45:50 -0800582 isl_view.usage = cube_usage | ISL_SURF_USAGE_TEXTURE_BIT;
583 isl_surf_fill_state(&device->isl_dev,
584 iview->sampler_surface_state.map,
585 .surf = &surface->isl,
586 .view = &isl_view,
587 .mocs = device->default_mocs,
588 .level0_extent_px = level0_extent_px);
589
590 if (!device->info.has_llc)
591 anv_state_clflush(iview->sampler_surface_state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800592 } else {
Chad Versace42b93202016-02-04 11:41:59 -0800593 iview->sampler_surface_state.alloc_size = 0;
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800594 }
595
Chad Versace3eebf362016-02-04 11:58:05 -0800596 if (image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800597 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
598
Jason Ekstrandded57c32016-02-20 11:45:50 -0800599 isl_view.usage = cube_usage | ISL_SURF_USAGE_RENDER_TARGET_BIT;
600 isl_surf_fill_state(&device->isl_dev,
601 iview->color_rt_surface_state.map,
602 .surf = &surface->isl,
603 .view = &isl_view,
604 .mocs = device->default_mocs,
605 .level0_extent_px = level0_extent_px);
606
607 if (!device->info.has_llc)
608 anv_state_clflush(iview->color_rt_surface_state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800609 } else {
610 iview->color_rt_surface_state.alloc_size = 0;
611 }
612
Chad Versace3eebf362016-02-04 11:58:05 -0800613 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800614 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
615
Jason Ekstrandded57c32016-02-20 11:45:50 -0800616 if (has_matching_storage_typed_format(device, iview->format)) {
617 isl_view.usage = cube_usage | ISL_SURF_USAGE_STORAGE_BIT;
618 isl_surf_fill_state(&device->isl_dev,
619 iview->storage_surface_state.map,
620 .surf = &surface->isl,
621 .view = &isl_view,
622 .mocs = device->default_mocs,
623 .level0_extent_px = level0_extent_px);
624 } else {
Francisco Jerez6840cc12016-01-26 14:50:52 -0800625 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
Francisco Jereza50dc702016-01-26 12:23:08 -0800626 ISL_FORMAT_RAW,
627 iview->offset,
628 iview->bo->size - iview->offset, 1);
Jason Ekstrandded57c32016-02-20 11:45:50 -0800629 }
Francisco Jereza50dc702016-01-26 12:23:08 -0800630
Jason Ekstrandded57c32016-02-20 11:45:50 -0800631 if (!device->info.has_llc)
632 anv_state_clflush(iview->storage_surface_state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800633 } else {
634 iview->storage_surface_state.alloc_size = 0;
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700635 }
636}
637
Chad Versace5b04db72015-07-06 16:24:28 -0700638VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700639anv_CreateImageView(VkDevice _device,
640 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800641 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700642 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700643{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700644 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700645 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700646
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800647 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
648 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700649 if (view == NULL)
650 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
651
Nanley Chery6a579de2016-01-26 18:53:21 -0800652 anv_image_view_init(view, device, pCreateInfo, NULL, 0);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700653
654 *pView = anv_image_view_to_handle(view);
655
656 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700657}
658
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800659void
660anv_DestroyImageView(VkDevice _device, VkImageView _iview,
661 const VkAllocationCallbacks *pAllocator)
Chad Versace24de3d42015-10-06 19:11:58 -0700662{
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800663 ANV_FROM_HANDLE(anv_device, device, _device);
664 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
665
Chad Versace3eebf362016-02-04 11:58:05 -0800666 if (iview->color_rt_surface_state.alloc_size > 0) {
Chad Versace24de3d42015-10-06 19:11:58 -0700667 anv_state_pool_free(&device->surface_state_pool,
668 iview->color_rt_surface_state);
669 }
670
Chad Versace3eebf362016-02-04 11:58:05 -0800671 if (iview->sampler_surface_state.alloc_size > 0) {
Chad Versace24de3d42015-10-06 19:11:58 -0700672 anv_state_pool_free(&device->surface_state_pool,
Chad Versace42b93202016-02-04 11:41:59 -0800673 iview->sampler_surface_state);
Chad Versace24de3d42015-10-06 19:11:58 -0700674 }
675
Chad Versace3eebf362016-02-04 11:58:05 -0800676 if (iview->storage_surface_state.alloc_size > 0) {
Jason Ekstrandff05f632015-12-07 17:17:30 -0800677 anv_state_pool_free(&device->surface_state_pool,
678 iview->storage_surface_state);
679 }
680
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800681 anv_free2(&device->alloc, pAllocator, iview);
Jason Ekstrand84783502015-07-10 20:18:52 -0700682}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700683
Jason Ekstrandc5618602015-12-12 16:11:23 -0800684VkResult
685anv_CreateBufferView(VkDevice _device,
686 const VkBufferViewCreateInfo *pCreateInfo,
687 const VkAllocationCallbacks *pAllocator,
688 VkBufferView *pView)
689{
690 ANV_FROM_HANDLE(anv_device, device, _device);
691 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
692 struct anv_buffer_view *view;
693
Jason Ekstrandc5618602015-12-12 16:11:23 -0800694 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
695 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
696 if (!view)
697 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
698
Jason Ekstrandc5618602015-12-12 16:11:23 -0800699 const struct anv_format *format =
700 anv_format_for_vk_format(pCreateInfo->format);
701
Chad Versace4d037b52016-02-08 18:51:52 -0800702 view->format = format->isl_format;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800703 view->bo = buffer->bo;
704 view->offset = buffer->offset + pCreateInfo->offset;
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800705 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
706 buffer->size - view->offset : pCreateInfo->range;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800707
708 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
709 view->surface_state =
710 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
711
Francisco Jerez6840cc12016-01-26 14:50:52 -0800712 anv_fill_buffer_surface_state(device, view->surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800713 view->format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800714 view->offset, view->range,
Chad Versace89b68dc2016-01-05 09:59:07 -0800715 format->isl_layout->bs);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800716 } else {
717 view->surface_state = (struct anv_state){ 0 };
718 }
719
720 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
721 view->storage_surface_state =
722 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
723
724 enum isl_format storage_format =
Francisco Jereza50dc702016-01-26 12:23:08 -0800725 has_matching_storage_typed_format(device, view->format) ?
726 isl_lower_storage_image_format(&device->isl_dev, view->format) :
727 ISL_FORMAT_RAW;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800728
Francisco Jerez6840cc12016-01-26 14:50:52 -0800729 anv_fill_buffer_surface_state(device, view->storage_surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800730 storage_format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800731 view->offset, view->range,
Francisco Jereza50dc702016-01-26 12:23:08 -0800732 (storage_format == ISL_FORMAT_RAW ? 1 :
733 format->isl_layout->bs));
734
Jason Ekstrand783a2112015-12-14 16:51:12 -0800735 } else {
736 view->storage_surface_state = (struct anv_state){ 0 };
737 }
Jason Ekstrandc5618602015-12-12 16:11:23 -0800738
739 *pView = anv_buffer_view_to_handle(view);
740
741 return VK_SUCCESS;
742}
743
744void
745anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
746 const VkAllocationCallbacks *pAllocator)
747{
748 ANV_FROM_HANDLE(anv_device, device, _device);
749 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
750
Jason Ekstrand783a2112015-12-14 16:51:12 -0800751 if (view->surface_state.alloc_size > 0)
752 anv_state_pool_free(&device->surface_state_pool,
753 view->surface_state);
754
755 if (view->storage_surface_state.alloc_size > 0)
756 anv_state_pool_free(&device->surface_state_pool,
757 view->storage_surface_state);
758
Jason Ekstrandc5618602015-12-12 16:11:23 -0800759 anv_free2(&device->alloc, pAllocator, view);
760}
761
Chad Versace941b48e2015-08-28 07:08:58 -0700762struct anv_surface *
Chad Versace7a089bd2015-10-05 06:48:14 -0700763anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
Chad Versace941b48e2015-08-28 07:08:58 -0700764{
Chad Versace7a089bd2015-10-05 06:48:14 -0700765 switch (aspect_mask) {
766 case VK_IMAGE_ASPECT_COLOR_BIT:
Chad Versace24de3d42015-10-06 19:11:58 -0700767 /* Dragons will eat you.
768 *
769 * Meta attaches all destination surfaces as color render targets. Guess
770 * what surface the Meta Dragons really want.
771 */
Chad Versacee6d34322016-02-08 19:07:10 -0800772 if (image->format->has_depth && image->format->has_stencil) {
Chad Versace24de3d42015-10-06 19:11:58 -0700773 return &image->depth_surface;
Chad Versacee6d34322016-02-08 19:07:10 -0800774 } else if (image->format->has_depth) {
Chad Versace24de3d42015-10-06 19:11:58 -0700775 return &image->depth_surface;
776 } else if (image->format->has_stencil) {
777 return &image->stencil_surface;
778 } else {
779 return &image->color_surface;
780 }
781 break;
Chad Versace7a089bd2015-10-05 06:48:14 -0700782 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versacee6d34322016-02-08 19:07:10 -0800783 assert(image->format->has_depth);
Chad Versace941b48e2015-08-28 07:08:58 -0700784 return &image->depth_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700785 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700786 assert(image->format->has_stencil);
Chad Versace941b48e2015-08-28 07:08:58 -0700787 return &image->stencil_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700788 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versacee6d34322016-02-08 19:07:10 -0800789 if (image->format->has_depth && image->format->has_stencil) {
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800790 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
791 * combined depth stencil formats. Specifically, it states:
Chad Versace03dd7222015-10-07 09:03:47 -0700792 *
793 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
794 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800795 *
796 * Image views with both depth and stencil aspects are only valid for
797 * render target attachments, in which case
798 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
799 * stencil surfaces from the underlying surface.
Chad Versace03dd7222015-10-07 09:03:47 -0700800 */
Chad Versace03dd7222015-10-07 09:03:47 -0700801 return &image->depth_surface;
Chad Versacee6d34322016-02-08 19:07:10 -0800802 } else if (image->format->has_depth) {
Chad Versace03dd7222015-10-07 09:03:47 -0700803 return &image->depth_surface;
804 } else if (image->format->has_stencil) {
805 return &image->stencil_surface;
806 }
807 /* fallthrough */
Chad Versace941b48e2015-08-28 07:08:58 -0700808 default:
809 unreachable("image does not have aspect");
810 return NULL;
811 }
812}
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800813
Jason Ekstrandba393c92016-01-05 13:55:00 -0800814static void
815image_param_defaults(struct brw_image_param *param)
816{
817 memset(param, 0, sizeof *param);
818 /* Set the swizzling shifts to all-ones to effectively disable swizzling --
819 * See emit_address_calculation() in brw_fs_surface_builder.cpp for a more
820 * detailed explanation of these parameters.
821 */
822 param->swizzling[0] = 0xff;
823 param->swizzling[1] = 0xff;
824}
825
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800826void
827anv_image_view_fill_image_param(struct anv_device *device,
828 struct anv_image_view *view,
829 struct brw_image_param *param)
830{
Jason Ekstrandba393c92016-01-05 13:55:00 -0800831 image_param_defaults(param);
832
833 const struct isl_surf *surf = &view->image->color_surface.isl;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800834 const int cpp = isl_format_get_layout(surf->format)->bs;
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800835 const struct isl_extent3d image_align_sa =
836 isl_surf_get_image_alignment_sa(surf);
Jason Ekstrandba393c92016-01-05 13:55:00 -0800837
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800838 param->size[0] = view->extent.width;
839 param->size[1] = view->extent.height;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800840 if (surf->dim == ISL_SURF_DIM_3D) {
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800841 param->size[2] = view->extent.depth;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800842 } else {
843 param->size[2] = surf->logical_level0_px.array_len - view->base_layer;
844 }
845
Nanley Chery308ec022016-01-27 09:29:09 -0800846 isl_surf_get_image_offset_el(surf, view->base_mip, view->base_layer, 0,
Jason Ekstrandba393c92016-01-05 13:55:00 -0800847 &param->offset[0], &param->offset[1]);
848
849 param->stride[0] = cpp;
850 param->stride[1] = surf->row_pitch / cpp;
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800851
852 if (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D) {
853 param->stride[2] = util_align_npot(param->size[0], image_align_sa.w);
854 param->stride[3] = util_align_npot(param->size[1], image_align_sa.h);
855 } else {
856 param->stride[2] = 0;
857 param->stride[3] = isl_surf_get_array_pitch_el_rows(surf);
858 }
Jason Ekstrandba393c92016-01-05 13:55:00 -0800859
860 switch (surf->tiling) {
861 case ISL_TILING_LINEAR:
862 /* image_param_defaults is good enough */
863 break;
864
865 case ISL_TILING_X:
866 /* An X tile is a rectangular block of 512x8 bytes. */
867 param->tiling[0] = util_logbase2(512 / cpp);
868 param->tiling[1] = util_logbase2(8);
869
870 if (device->isl_dev.has_bit6_swizzling) {
871 /* Right shifts required to swizzle bits 9 and 10 of the memory
872 * address with bit 6.
873 */
874 param->swizzling[0] = 3;
875 param->swizzling[1] = 4;
876 }
877 break;
878
879 case ISL_TILING_Y0:
880 /* The layout of a Y-tiled surface in memory isn't really fundamentally
881 * different to the layout of an X-tiled surface, we simply pretend that
882 * the surface is broken up in a number of smaller 16Bx32 tiles, each
883 * one arranged in X-major order just like is the case for X-tiling.
884 */
885 param->tiling[0] = util_logbase2(16 / cpp);
886 param->tiling[1] = util_logbase2(32);
887
888 if (device->isl_dev.has_bit6_swizzling) {
889 /* Right shift required to swizzle bit 9 of the memory address with
890 * bit 6.
891 */
892 param->swizzling[0] = 3;
893 param->swizzling[1] = 0xff;
894 }
895 break;
896
897 default:
898 assert(!"Unhandled storage image tiling");
899 }
900
901 /* 3D textures are arranged in 2D in memory with 2^lod slices per row. The
902 * address calculation algorithm (emit_address_calculation() in
903 * brw_fs_surface_builder.cpp) handles this as a sort of tiling with
904 * modulus equal to the LOD.
905 */
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800906 param->tiling[2] = (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D ?
907 view->base_mip : 0);
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800908}
Jason Ekstrand783a2112015-12-14 16:51:12 -0800909
910void
911anv_buffer_view_fill_image_param(struct anv_device *device,
912 struct anv_buffer_view *view,
913 struct brw_image_param *param)
914{
Jason Ekstrandba393c92016-01-05 13:55:00 -0800915 image_param_defaults(param);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800916
Chad Versace89b68dc2016-01-05 09:59:07 -0800917 param->stride[0] = isl_format_layouts[view->format].bs;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800918 param->size[0] = view->range / param->stride[0];
919}