blob: 74f1eca48f257b11fa173ce41e9daf1498e37775 [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/**
33 * The \a format argument is required and overrides any format found in struct
Chad Versace2f270f02015-12-04 16:29:25 -080034 * anv_image_create_info. Exactly one bit must be set in \a aspect.
Chad Versaceb3693892015-12-02 16:46:16 -080035 */
36static isl_surf_usage_flags_t
Chad Versace2f4bb002016-02-09 12:35:39 -080037choose_isl_surf_usage(VkImageUsageFlags vk_usage,
Chad Versace2f270f02015-12-04 16:29:25 -080038 VkImageAspectFlags aspect)
Chad Versaceb3693892015-12-02 16:46:16 -080039{
Chad Versaceb3693892015-12-02 16:46:16 -080040 isl_surf_usage_flags_t isl_flags = 0;
41
42 /* FINISHME: Support aux surfaces */
43 isl_flags |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
44
Chad Versace2f4bb002016-02-09 12:35:39 -080045 if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
Chad Versaceb3693892015-12-02 16:46:16 -080046 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
47
Chad Versace2f4bb002016-02-09 12:35:39 -080048 if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
Chad Versaceb3693892015-12-02 16:46:16 -080049 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
50
Chad Versace2f4bb002016-02-09 12:35:39 -080051 if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
Chad Versaceb3693892015-12-02 16:46:16 -080052 isl_flags |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
53
Chad Versace2f4bb002016-02-09 12:35:39 -080054 if (vk_usage & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
Chad Versaceb3693892015-12-02 16:46:16 -080055 isl_flags |= ISL_SURF_USAGE_CUBE_BIT;
56
Chad Versace2f4bb002016-02-09 12:35:39 -080057 if (vk_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
Chad Versace2f270f02015-12-04 16:29:25 -080058 switch (aspect) {
59 default:
60 unreachable("bad VkImageAspect");
61 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versaceb3693892015-12-02 16:46:16 -080062 isl_flags |= ISL_SURF_USAGE_DEPTH_BIT;
Chad Versace2f270f02015-12-04 16:29:25 -080063 break;
64 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versaceb3693892015-12-02 16:46:16 -080065 isl_flags |= ISL_SURF_USAGE_STENCIL_BIT;
Chad Versace2f270f02015-12-04 16:29:25 -080066 break;
Chad Versaceb3693892015-12-02 16:46:16 -080067 }
68 }
69
Chad Versace2f4bb002016-02-09 12:35:39 -080070 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
Chad Versaceb3693892015-12-02 16:46:16 -080071 /* Meta implements transfers by sampling from the source image. */
72 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
73 }
74
Chad Versace2f4bb002016-02-09 12:35:39 -080075 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
Chad Versaceb3693892015-12-02 16:46:16 -080076 /* Meta implements transfers by rendering into the destination image. */
77 isl_flags |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
78 }
79
80 return isl_flags;
81}
Chad Versace5a6b2e62015-08-17 13:50:43 -070082
83/**
Chad Versace9098e0f2015-12-07 09:22:49 -080084 * Exactly one bit must be set in \a aspect.
85 */
86static struct anv_surface *
87get_surface(struct anv_image *image, VkImageAspectFlags aspect)
88{
89 switch (aspect) {
90 default:
91 unreachable("bad VkImageAspect");
92 case VK_IMAGE_ASPECT_COLOR_BIT:
93 return &image->color_surface;
94 case VK_IMAGE_ASPECT_DEPTH_BIT:
95 return &image->depth_surface;
96 case VK_IMAGE_ASPECT_STENCIL_BIT:
97 return &image->stencil_surface;
98 }
99}
100
101/**
102 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
103 * image's memory requirements (that is, the image's size and alignment).
104 *
105 * Exactly one bit must be set in \a aspect.
Chad Versace5a6b2e62015-08-17 13:50:43 -0700106 */
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700107static VkResult
Chad Versace9098e0f2015-12-07 09:22:49 -0800108make_surface(const struct anv_device *dev,
109 struct anv_image *image,
110 const struct anv_image_create_info *anv_info,
111 VkImageAspectFlags aspect)
Chad Versacec6e76ae2015-06-26 18:48:34 -0700112{
Chad Versaceb3693892015-12-02 16:46:16 -0800113 const VkImageCreateInfo *vk_info = anv_info->vk_info;
Chad Versace3d85a282015-12-07 08:53:43 -0800114 bool ok UNUSED;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700115
Chad Versaceb3693892015-12-02 16:46:16 -0800116 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
117 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
118 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
119 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
120 };
Chad Versacec6e76ae2015-06-26 18:48:34 -0700121
Chad Versace64e8af62015-12-07 08:50:28 -0800122 isl_tiling_flags_t tiling_flags = anv_info->isl_tiling_flags;
123 if (vk_info->tiling == VK_IMAGE_TILING_LINEAR)
124 tiling_flags &= ISL_TILING_LINEAR_BIT;
125
Chad Versace9098e0f2015-12-07 09:22:49 -0800126 struct anv_surface *anv_surf = get_surface(image, aspect);
127
128 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
Chad Versaceb3693892015-12-02 16:46:16 -0800129 .dim = vk_to_isl_surf_dim[vk_info->imageType],
Jordan Justenc20f78d2016-01-26 11:10:56 -0800130 .format = anv_get_isl_format(vk_info->format, aspect,
131 vk_info->tiling, NULL),
Chad Versaceb3693892015-12-02 16:46:16 -0800132 .width = vk_info->extent.width,
133 .height = vk_info->extent.height,
134 .depth = vk_info->extent.depth,
135 .levels = vk_info->mipLevels,
136 .array_len = vk_info->arrayLayers,
137 .samples = vk_info->samples,
138 .min_alignment = 0,
139 .min_pitch = 0,
Chad Versace2f4bb002016-02-09 12:35:39 -0800140 .usage = choose_isl_surf_usage(image->usage, aspect),
Chad Versace64e8af62015-12-07 08:50:28 -0800141 .tiling_flags = tiling_flags);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700142
Chad Versace3d85a282015-12-07 08:53:43 -0800143 /* isl_surf_init() will fail only if provided invalid input. Invalid input
144 * is illegal in Vulkan.
145 */
146 assert(ok);
147
Chad Versace9098e0f2015-12-07 09:22:49 -0800148 anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment);
149 image->size = anv_surf->offset + anv_surf->isl.size;
150 image->alignment = MAX(image->alignment, anv_surf->isl.alignment);
Chad Versaceb3693892015-12-02 16:46:16 -0800151
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700152 return VK_SUCCESS;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700153}
154
Chad Versace24de3d42015-10-06 19:11:58 -0700155static VkImageUsageFlags
156anv_image_get_full_usage(const VkImageCreateInfo *info)
157{
158 VkImageUsageFlags usage = info->usage;
159
Chad Versace2bab3cd2016-01-28 06:28:01 -0800160 if (info->samples > 1 &&
161 (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) {
162 /* Meta will resolve the image by binding it as a texture. */
163 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
164 }
165
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800166 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700167 /* Meta will transfer from the image by binding it as a texture. */
168 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
169 }
170
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800171 if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700172 /* Meta will transfer to the image by binding it as a color attachment,
173 * even if the image format is not a color format.
174 */
175 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
176 }
177
178 return usage;
179}
180
Chad Versace127cb3f2015-06-26 20:12:42 -0700181VkResult
182anv_image_create(VkDevice _device,
183 const struct anv_image_create_info *create_info,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800184 const VkAllocationCallbacks* alloc,
Chad Versace127cb3f2015-06-26 20:12:42 -0700185 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700186{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700187 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versacefdcd71f2015-06-26 20:06:08 -0700188 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700189 struct anv_image *image = NULL;
190 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700191
192 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
193
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700194 anv_assert(pCreateInfo->mipLevels > 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800195 anv_assert(pCreateInfo->arrayLayers > 0);
Chad Versaced96d78c2016-01-22 17:16:20 -0800196 anv_assert(pCreateInfo->samples > 0);
Chad Versace5d7103e2015-06-26 09:05:46 -0700197 anv_assert(pCreateInfo->extent.width > 0);
198 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700199 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700200
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800201 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
202 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700203 if (!image)
204 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
Chad Versace67a76592015-06-26 09:17:52 -0700205
Chad Versacec6e76ae2015-06-26 18:48:34 -0700206 memset(image, 0, sizeof(*image));
207 image->type = pCreateInfo->imageType;
208 image->extent = pCreateInfo->extent;
Jason Ekstrand3200a812015-12-31 12:39:34 -0800209 image->vk_format = pCreateInfo->format;
Chad Versaceded736f2015-08-17 13:10:40 -0700210 image->format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700211 image->levels = pCreateInfo->mipLevels;
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800212 image->array_size = pCreateInfo->arrayLayers;
Chad Versacedfcb4ee2016-01-20 16:34:23 -0800213 image->samples = pCreateInfo->samples;
Chad Versace24de3d42015-10-06 19:11:58 -0700214 image->usage = anv_image_get_full_usage(pCreateInfo);
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800215 image->tiling = pCreateInfo->tiling;
Chad Versace67a76592015-06-26 09:17:52 -0700216
Chad Versace941b48e2015-08-28 07:08:58 -0700217 if (likely(anv_format_is_color(image->format))) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800218 r = make_surface(device, image, create_info,
219 VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700220 if (r != VK_SUCCESS)
221 goto fail;
Chad Versace941b48e2015-08-28 07:08:58 -0700222 } else {
Chad Versacee6d34322016-02-08 19:07:10 -0800223 if (image->format->has_depth) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800224 r = make_surface(device, image, create_info,
225 VK_IMAGE_ASPECT_DEPTH_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700226 if (r != VK_SUCCESS)
227 goto fail;
228 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700229
Chad Versace941b48e2015-08-28 07:08:58 -0700230 if (image->format->has_stencil) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800231 r = make_surface(device, image, create_info,
232 VK_IMAGE_ASPECT_STENCIL_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700233 if (r != VK_SUCCESS)
234 goto fail;
235 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700236 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700237
Jason Ekstranda52e2082015-07-09 20:24:07 -0700238 *pImage = anv_image_to_handle(image);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700239
240 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700241
242fail:
243 if (image)
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800244 anv_free2(&device->alloc, alloc, image);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700245
246 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700247}
248
Chad Versace127cb3f2015-06-26 20:12:42 -0700249VkResult
250anv_CreateImage(VkDevice device,
251 const VkImageCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800252 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700253 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700254{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700255 return anv_image_create(device,
256 &(struct anv_image_create_info) {
257 .vk_info = pCreateInfo,
Chad Versace64e8af62015-12-07 08:50:28 -0800258 .isl_tiling_flags = ISL_TILING_ANY_MASK,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700259 },
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800260 pAllocator,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700261 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700262}
263
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700264void
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800265anv_DestroyImage(VkDevice _device, VkImage _image,
266 const VkAllocationCallbacks *pAllocator)
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700267{
268 ANV_FROM_HANDLE(anv_device, device, _device);
269
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800270 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700271}
272
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700273static void
274anv_surface_get_subresource_layout(struct anv_image *image,
275 struct anv_surface *surface,
276 const VkImageSubresource *subresource,
277 VkSubresourceLayout *layout)
278{
279 /* If we are on a non-zero mip level or array slice, we need to
280 * calculate a real offset.
281 */
282 anv_assert(subresource->mipLevel == 0);
283 anv_assert(subresource->arrayLayer == 0);
284
285 layout->offset = surface->offset;
Chad Versace981ef2f2015-12-03 08:40:47 -0800286 layout->rowPitch = surface->isl.row_pitch;
287 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
Jason Ekstrand8a81d132016-01-06 19:27:10 -0800288 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
Chad Versace981ef2f2015-12-03 08:40:47 -0800289 layout->size = surface->isl.size;
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700290}
291
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800292void anv_GetImageSubresourceLayout(
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700293 VkDevice device,
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700294 VkImage _image,
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700295 const VkImageSubresource* pSubresource,
296 VkSubresourceLayout* pLayout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700297{
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700298 ANV_FROM_HANDLE(anv_image, image, _image);
299
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800300 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
301
302 switch (pSubresource->aspectMask) {
303 case VK_IMAGE_ASPECT_COLOR_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700304 anv_surface_get_subresource_layout(image, &image->color_surface,
305 pSubresource, pLayout);
306 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800307 case VK_IMAGE_ASPECT_DEPTH_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700308 anv_surface_get_subresource_layout(image, &image->depth_surface,
309 pSubresource, pLayout);
310 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800311 case VK_IMAGE_ASPECT_STENCIL_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700312 anv_surface_get_subresource_layout(image, &image->stencil_surface,
313 pSubresource, pLayout);
314 break;
315 default:
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800316 assert(!"Invalid image aspect");
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700317 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700318}
319
Chad Versace127cb3f2015-06-26 20:12:42 -0700320VkResult
Chad Versace5b04db72015-07-06 16:24:28 -0700321anv_validate_CreateImageView(VkDevice _device,
322 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800323 const VkAllocationCallbacks *pAllocator,
Chad Versace5b04db72015-07-06 16:24:28 -0700324 VkImageView *pView)
325{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700326 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace23075bc2015-07-06 17:04:13 -0700327 const VkImageSubresourceRange *subresource;
Chad Versace23075bc2015-07-06 17:04:13 -0700328 const struct anv_format *view_format_info;
Chad Versace5b04db72015-07-06 16:24:28 -0700329
Chad Versace23075bc2015-07-06 17:04:13 -0700330 /* Validate structure type before dereferencing it. */
Chad Versace5b04db72015-07-06 16:24:28 -0700331 assert(pCreateInfo);
332 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chad Versace23075bc2015-07-06 17:04:13 -0700333 subresource = &pCreateInfo->subresourceRange;
334
Chad Versace23075bc2015-07-06 17:04:13 -0700335 /* Validate viewType is in range before using it. */
336 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
337 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700338
339 /* Validate format is in range before using it. */
340 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
341 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700342 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
343
344 /* Validate channel swizzles. */
Jason Ekstranda53f23d2015-11-30 13:06:12 -0800345 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
346 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
347 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
348 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
349 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
350 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
351 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
352 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700353
354 /* Validate subresource. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700355 assert(subresource->aspectMask != 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800356 assert(subresource->levelCount > 0);
357 assert(subresource->layerCount > 0);
Chad Versace23075bc2015-07-06 17:04:13 -0700358 assert(subresource->baseMipLevel < image->levels);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800359 assert(subresource->baseMipLevel + subresource->levelCount <= image->levels);
Jason Ekstrand1e4263b2015-10-06 10:27:50 -0700360 assert(subresource->baseArrayLayer < image->array_size);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800361 assert(subresource->baseArrayLayer + subresource->layerCount <= image->array_size);
Chad Versace5b04db72015-07-06 16:24:28 -0700362 assert(pView);
363
Chad Versace7a089bd2015-10-05 06:48:14 -0700364 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
365 | VK_IMAGE_ASPECT_STENCIL_BIT;
366
Chad Versace23075bc2015-07-06 17:04:13 -0700367 /* Validate format. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700368 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
369 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versacee6d34322016-02-08 19:07:10 -0800370 assert(!image->format->has_depth);
Chad Versaceded736f2015-08-17 13:10:40 -0700371 assert(!image->format->has_stencil);
Chad Versacee6d34322016-02-08 19:07:10 -0800372 assert(!view_format_info->has_depth);
Chad Versace23075bc2015-07-06 17:04:13 -0700373 assert(!view_format_info->has_stencil);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800374 assert(view_format_info->isl_layout->bs ==
375 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700376 } else if (subresource->aspectMask & ds_flags) {
377 assert((subresource->aspectMask & ~ds_flags) == 0);
378
379 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Chad Versacee6d34322016-02-08 19:07:10 -0800380 assert(image->format->has_depth);
381 assert(view_format_info->has_depth);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800382 assert(view_format_info->isl_layout->bs ==
383 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700384 }
385
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800386 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Chad Versace7a089bd2015-10-05 06:48:14 -0700387 /* FINISHME: Is it legal to have an R8 view of S8? */
388 assert(image->format->has_stencil);
389 assert(view_format_info->has_stencil);
390 }
391 } else {
392 assert(!"bad VkImageSubresourceRange::aspectFlags");
Chad Versace23075bc2015-07-06 17:04:13 -0700393 }
Chad Versace5b04db72015-07-06 16:24:28 -0700394
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800395 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
Chad Versace5b04db72015-07-06 16:24:28 -0700396}
397
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700398void
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800399anv_fill_image_surface_state(struct anv_device *device, struct anv_state state,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800400 struct anv_image_view *iview,
401 const VkImageViewCreateInfo *pCreateInfo,
402 VkImageUsageFlagBits usage)
403{
404 switch (device->info.gen) {
405 case 7:
406 if (device->info.is_haswell)
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800407 gen75_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800408 pCreateInfo, usage);
409 else
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800410 gen7_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800411 pCreateInfo, usage);
412 break;
413 case 8:
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800414 gen8_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800415 pCreateInfo, usage);
416 break;
417 case 9:
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800418 gen9_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800419 pCreateInfo, usage);
420 break;
421 default:
422 unreachable("unsupported gen\n");
423 }
424
425 if (!device->info.has_llc)
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800426 anv_state_clflush(state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800427}
428
429static struct anv_state
430alloc_surface_state(struct anv_device *device,
431 struct anv_cmd_buffer *cmd_buffer)
432{
433 if (cmd_buffer) {
434 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
435 } else {
436 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
437 }
438}
439
Francisco Jereza50dc702016-01-26 12:23:08 -0800440static bool
441has_matching_storage_typed_format(const struct anv_device *device,
442 enum isl_format format)
443{
444 return (isl_format_get_layout(format)->bs <= 4 ||
445 (isl_format_get_layout(format)->bs <= 8 &&
446 (device->info.gen >= 8 || device->info.is_haswell)) ||
447 device->info.gen >= 9);
448}
449
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800450static VkComponentSwizzle
Jordan Justenc20f78d2016-01-26 11:10:56 -0800451remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
452 struct anv_format_swizzle format_swizzle)
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800453{
454 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
Jordan Justenc20f78d2016-01-26 11:10:56 -0800455 swizzle = component;
456
457 switch (swizzle) {
458 case VK_COMPONENT_SWIZZLE_ZERO:
459 return VK_COMPONENT_SWIZZLE_ZERO;
460 case VK_COMPONENT_SWIZZLE_ONE:
461 return VK_COMPONENT_SWIZZLE_ONE;
462 case VK_COMPONENT_SWIZZLE_R:
463 return VK_COMPONENT_SWIZZLE_R + format_swizzle.r;
464 case VK_COMPONENT_SWIZZLE_G:
465 return VK_COMPONENT_SWIZZLE_R + format_swizzle.g;
466 case VK_COMPONENT_SWIZZLE_B:
467 return VK_COMPONENT_SWIZZLE_R + format_swizzle.b;
468 case VK_COMPONENT_SWIZZLE_A:
469 return VK_COMPONENT_SWIZZLE_R + format_swizzle.a;
470 default:
471 unreachable("Invalid swizzle");
472 }
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800473}
474
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800475void
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700476anv_image_view_init(struct anv_image_view *iview,
477 struct anv_device *device,
478 const VkImageViewCreateInfo* pCreateInfo,
Nanley Chery6a579de2016-01-26 18:53:21 -0800479 struct anv_cmd_buffer *cmd_buffer,
480 uint32_t offset)
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700481{
Chad Versace44143a12015-10-06 18:17:09 -0700482 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace24de3d42015-10-06 19:11:58 -0700483 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Nanley Cheryd3c1fd52016-01-26 18:40:54 -0800484 VkImageViewCreateInfo mCreateInfo;
485 memcpy(&mCreateInfo, pCreateInfo, sizeof(VkImageViewCreateInfo));
Chad Versace44143a12015-10-06 18:17:09 -0700486
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800487 assert(range->layerCount > 0);
Chad Versace24de3d42015-10-06 19:11:58 -0700488 assert(range->baseMipLevel < image->levels);
Chad Versace44143a12015-10-06 18:17:09 -0700489 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
Chad Versace24de3d42015-10-06 19:11:58 -0700490 VK_IMAGE_USAGE_STORAGE_BIT |
491 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Chad Versace0ca3c842015-10-07 11:39:49 -0700492 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
Chad Versace24de3d42015-10-06 19:11:58 -0700493
494 switch (image->type) {
495 default:
496 unreachable("bad VkImageType");
497 case VK_IMAGE_TYPE_1D:
498 case VK_IMAGE_TYPE_2D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800499 assert(range->baseArrayLayer + range->layerCount - 1 <= image->array_size);
Chad Versace24de3d42015-10-06 19:11:58 -0700500 break;
501 case VK_IMAGE_TYPE_3D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800502 assert(range->baseArrayLayer + range->layerCount - 1
Chad Versace24de3d42015-10-06 19:11:58 -0700503 <= anv_minify(image->extent.depth, range->baseMipLevel));
504 break;
505 }
Chad Versace44143a12015-10-06 18:17:09 -0700506
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800507 struct anv_surface *surface =
508 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
509
510 iview->image = image;
511 iview->bo = image->bo;
Nanley Chery6a579de2016-01-26 18:53:21 -0800512 iview->offset = image->offset + surface->offset + offset;
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800513
514 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800515 iview->vk_format = pCreateInfo->format;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800516
517 struct anv_format_swizzle swizzle;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800518 iview->format = anv_get_isl_format(pCreateInfo->format, iview->aspect_mask,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800519 image->tiling, &swizzle);
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800520 iview->swizzle.r = remap_swizzle(pCreateInfo->components.r,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800521 VK_COMPONENT_SWIZZLE_R, swizzle);
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800522 iview->swizzle.g = remap_swizzle(pCreateInfo->components.g,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800523 VK_COMPONENT_SWIZZLE_G, swizzle);
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800524 iview->swizzle.b = remap_swizzle(pCreateInfo->components.b,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800525 VK_COMPONENT_SWIZZLE_B, swizzle);
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800526 iview->swizzle.a = remap_swizzle(pCreateInfo->components.a,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800527 VK_COMPONENT_SWIZZLE_A, swizzle);
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800528
Jason Ekstrandaa9987a2016-01-05 13:54:02 -0800529 iview->base_layer = range->baseArrayLayer;
530 iview->base_mip = range->baseMipLevel;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800531
532 if (!isl_format_is_compressed(iview->format) &&
Chad Versace4d037b52016-02-08 18:51:52 -0800533 isl_format_is_compressed(image->format->isl_format)) {
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800534 /* Scale the ImageView extent by the backing Image. This is used
535 * internally when an uncompressed ImageView is created on a
536 * compressed Image. The ImageView can therefore be used for copying
537 * data from a source Image to a destination Image.
538 */
539 const struct isl_format_layout * isl_layout = image->format->isl_layout;
Nanley Chery235abfb2016-01-27 12:08:56 -0800540
Jason Ekstrand96cf5cf2016-01-27 11:47:07 -0800541 iview->level_0_extent.depth = anv_minify(image->extent.depth, range->baseMipLevel);
Jason Ekstrand96cf5cf2016-01-27 11:47:07 -0800542 iview->level_0_extent.depth = DIV_ROUND_UP(iview->level_0_extent.depth, isl_layout->bd);
Nanley Chery235abfb2016-01-27 12:08:56 -0800543
Jason Ekstrand162c6622016-01-27 12:37:15 -0800544 iview->level_0_extent.height = isl_surf_get_array_pitch_el_rows(&surface->isl) * image->array_size;
Nanley Chery235abfb2016-01-27 12:08:56 -0800545 iview->level_0_extent.width = isl_surf_get_row_pitch_el(&surface->isl);
Nanley Cheryd3c1fd52016-01-26 18:40:54 -0800546 mCreateInfo.subresourceRange.baseMipLevel = 0;
547 mCreateInfo.subresourceRange.baseArrayLayer = 0;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800548 } else {
Jason Ekstrand96cf5cf2016-01-27 11:47:07 -0800549 iview->level_0_extent.width = image->extent.width;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800550 iview->level_0_extent.height = image->extent.height;
Jason Ekstrand96cf5cf2016-01-27 11:47:07 -0800551 iview->level_0_extent.depth = image->extent.depth;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800552 }
553
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800554 iview->extent = (VkExtent3D) {
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800555 .width = anv_minify(iview->level_0_extent.width , range->baseMipLevel),
556 .height = anv_minify(iview->level_0_extent.height, range->baseMipLevel),
557 .depth = anv_minify(iview->level_0_extent.depth , range->baseMipLevel),
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800558 };
559
Chad Versace3eebf362016-02-04 11:58:05 -0800560 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Chad Versace42b93202016-02-04 11:41:59 -0800561 iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800562
Chad Versace42b93202016-02-04 11:41:59 -0800563 anv_fill_image_surface_state(device, iview->sampler_surface_state,
Nanley Cheryd3c1fd52016-01-26 18:40:54 -0800564 iview, &mCreateInfo,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800565 VK_IMAGE_USAGE_SAMPLED_BIT);
566 } else {
Chad Versace42b93202016-02-04 11:41:59 -0800567 iview->sampler_surface_state.alloc_size = 0;
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800568 }
569
Chad Versace3eebf362016-02-04 11:58:05 -0800570 if (image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800571 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
572
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800573 anv_fill_image_surface_state(device, iview->color_rt_surface_state,
Nanley Cheryd3c1fd52016-01-26 18:40:54 -0800574 iview, &mCreateInfo,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800575 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
576 } else {
577 iview->color_rt_surface_state.alloc_size = 0;
578 }
579
Chad Versace3eebf362016-02-04 11:58:05 -0800580 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800581 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
582
Francisco Jereza50dc702016-01-26 12:23:08 -0800583 if (has_matching_storage_typed_format(device, iview->format))
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800584 anv_fill_image_surface_state(device, iview->storage_surface_state,
Nanley Cheryd3c1fd52016-01-26 18:40:54 -0800585 iview, &mCreateInfo,
Francisco Jereza50dc702016-01-26 12:23:08 -0800586 VK_IMAGE_USAGE_STORAGE_BIT);
587 else
Francisco Jerez6840cc12016-01-26 14:50:52 -0800588 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
Francisco Jereza50dc702016-01-26 12:23:08 -0800589 ISL_FORMAT_RAW,
590 iview->offset,
591 iview->bo->size - iview->offset, 1);
592
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800593 } else {
594 iview->storage_surface_state.alloc_size = 0;
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700595 }
596}
597
Chad Versace5b04db72015-07-06 16:24:28 -0700598VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700599anv_CreateImageView(VkDevice _device,
600 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800601 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700602 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700603{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700604 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700605 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700606
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800607 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
608 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700609 if (view == NULL)
610 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
611
Nanley Chery6a579de2016-01-26 18:53:21 -0800612 anv_image_view_init(view, device, pCreateInfo, NULL, 0);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700613
614 *pView = anv_image_view_to_handle(view);
615
616 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700617}
618
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800619void
620anv_DestroyImageView(VkDevice _device, VkImageView _iview,
621 const VkAllocationCallbacks *pAllocator)
Chad Versace24de3d42015-10-06 19:11:58 -0700622{
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800623 ANV_FROM_HANDLE(anv_device, device, _device);
624 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
625
Chad Versace3eebf362016-02-04 11:58:05 -0800626 if (iview->color_rt_surface_state.alloc_size > 0) {
Chad Versace24de3d42015-10-06 19:11:58 -0700627 anv_state_pool_free(&device->surface_state_pool,
628 iview->color_rt_surface_state);
629 }
630
Chad Versace3eebf362016-02-04 11:58:05 -0800631 if (iview->sampler_surface_state.alloc_size > 0) {
Chad Versace24de3d42015-10-06 19:11:58 -0700632 anv_state_pool_free(&device->surface_state_pool,
Chad Versace42b93202016-02-04 11:41:59 -0800633 iview->sampler_surface_state);
Chad Versace24de3d42015-10-06 19:11:58 -0700634 }
635
Chad Versace3eebf362016-02-04 11:58:05 -0800636 if (iview->storage_surface_state.alloc_size > 0) {
Jason Ekstrandff05f632015-12-07 17:17:30 -0800637 anv_state_pool_free(&device->surface_state_pool,
638 iview->storage_surface_state);
639 }
640
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800641 anv_free2(&device->alloc, pAllocator, iview);
Jason Ekstrand84783502015-07-10 20:18:52 -0700642}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700643
Jason Ekstrandc5618602015-12-12 16:11:23 -0800644VkResult
645anv_CreateBufferView(VkDevice _device,
646 const VkBufferViewCreateInfo *pCreateInfo,
647 const VkAllocationCallbacks *pAllocator,
648 VkBufferView *pView)
649{
650 ANV_FROM_HANDLE(anv_device, device, _device);
651 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
652 struct anv_buffer_view *view;
653
Jason Ekstrandc5618602015-12-12 16:11:23 -0800654 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
655 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
656 if (!view)
657 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
658
Jason Ekstrandc5618602015-12-12 16:11:23 -0800659 const struct anv_format *format =
660 anv_format_for_vk_format(pCreateInfo->format);
661
Chad Versace4d037b52016-02-08 18:51:52 -0800662 view->format = format->isl_format;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800663 view->bo = buffer->bo;
664 view->offset = buffer->offset + pCreateInfo->offset;
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800665 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
666 buffer->size - view->offset : pCreateInfo->range;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800667
668 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
669 view->surface_state =
670 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
671
Francisco Jerez6840cc12016-01-26 14:50:52 -0800672 anv_fill_buffer_surface_state(device, view->surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800673 view->format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800674 view->offset, view->range,
Chad Versace89b68dc2016-01-05 09:59:07 -0800675 format->isl_layout->bs);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800676 } else {
677 view->surface_state = (struct anv_state){ 0 };
678 }
679
680 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
681 view->storage_surface_state =
682 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
683
684 enum isl_format storage_format =
Francisco Jereza50dc702016-01-26 12:23:08 -0800685 has_matching_storage_typed_format(device, view->format) ?
686 isl_lower_storage_image_format(&device->isl_dev, view->format) :
687 ISL_FORMAT_RAW;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800688
Francisco Jerez6840cc12016-01-26 14:50:52 -0800689 anv_fill_buffer_surface_state(device, view->storage_surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800690 storage_format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800691 view->offset, view->range,
Francisco Jereza50dc702016-01-26 12:23:08 -0800692 (storage_format == ISL_FORMAT_RAW ? 1 :
693 format->isl_layout->bs));
694
Jason Ekstrand783a2112015-12-14 16:51:12 -0800695 } else {
696 view->storage_surface_state = (struct anv_state){ 0 };
697 }
Jason Ekstrandc5618602015-12-12 16:11:23 -0800698
699 *pView = anv_buffer_view_to_handle(view);
700
701 return VK_SUCCESS;
702}
703
704void
705anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
706 const VkAllocationCallbacks *pAllocator)
707{
708 ANV_FROM_HANDLE(anv_device, device, _device);
709 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
710
Jason Ekstrand783a2112015-12-14 16:51:12 -0800711 if (view->surface_state.alloc_size > 0)
712 anv_state_pool_free(&device->surface_state_pool,
713 view->surface_state);
714
715 if (view->storage_surface_state.alloc_size > 0)
716 anv_state_pool_free(&device->surface_state_pool,
717 view->storage_surface_state);
718
Jason Ekstrandc5618602015-12-12 16:11:23 -0800719 anv_free2(&device->alloc, pAllocator, view);
720}
721
Chad Versace941b48e2015-08-28 07:08:58 -0700722struct anv_surface *
Chad Versace7a089bd2015-10-05 06:48:14 -0700723anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
Chad Versace941b48e2015-08-28 07:08:58 -0700724{
Chad Versace7a089bd2015-10-05 06:48:14 -0700725 switch (aspect_mask) {
726 case VK_IMAGE_ASPECT_COLOR_BIT:
Chad Versace24de3d42015-10-06 19:11:58 -0700727 /* Dragons will eat you.
728 *
729 * Meta attaches all destination surfaces as color render targets. Guess
730 * what surface the Meta Dragons really want.
731 */
Chad Versacee6d34322016-02-08 19:07:10 -0800732 if (image->format->has_depth && image->format->has_stencil) {
Chad Versace24de3d42015-10-06 19:11:58 -0700733 return &image->depth_surface;
Chad Versacee6d34322016-02-08 19:07:10 -0800734 } else if (image->format->has_depth) {
Chad Versace24de3d42015-10-06 19:11:58 -0700735 return &image->depth_surface;
736 } else if (image->format->has_stencil) {
737 return &image->stencil_surface;
738 } else {
739 return &image->color_surface;
740 }
741 break;
Chad Versace7a089bd2015-10-05 06:48:14 -0700742 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versacee6d34322016-02-08 19:07:10 -0800743 assert(image->format->has_depth);
Chad Versace941b48e2015-08-28 07:08:58 -0700744 return &image->depth_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700745 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700746 assert(image->format->has_stencil);
Chad Versace941b48e2015-08-28 07:08:58 -0700747 return &image->stencil_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700748 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versacee6d34322016-02-08 19:07:10 -0800749 if (image->format->has_depth && image->format->has_stencil) {
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800750 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
751 * combined depth stencil formats. Specifically, it states:
Chad Versace03dd7222015-10-07 09:03:47 -0700752 *
753 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
754 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800755 *
756 * Image views with both depth and stencil aspects are only valid for
757 * render target attachments, in which case
758 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
759 * stencil surfaces from the underlying surface.
Chad Versace03dd7222015-10-07 09:03:47 -0700760 */
Chad Versace03dd7222015-10-07 09:03:47 -0700761 return &image->depth_surface;
Chad Versacee6d34322016-02-08 19:07:10 -0800762 } else if (image->format->has_depth) {
Chad Versace03dd7222015-10-07 09:03:47 -0700763 return &image->depth_surface;
764 } else if (image->format->has_stencil) {
765 return &image->stencil_surface;
766 }
767 /* fallthrough */
Chad Versace941b48e2015-08-28 07:08:58 -0700768 default:
769 unreachable("image does not have aspect");
770 return NULL;
771 }
772}
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800773
Jason Ekstrandba393c92016-01-05 13:55:00 -0800774static void
775image_param_defaults(struct brw_image_param *param)
776{
777 memset(param, 0, sizeof *param);
778 /* Set the swizzling shifts to all-ones to effectively disable swizzling --
779 * See emit_address_calculation() in brw_fs_surface_builder.cpp for a more
780 * detailed explanation of these parameters.
781 */
782 param->swizzling[0] = 0xff;
783 param->swizzling[1] = 0xff;
784}
785
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800786void
787anv_image_view_fill_image_param(struct anv_device *device,
788 struct anv_image_view *view,
789 struct brw_image_param *param)
790{
Jason Ekstrandba393c92016-01-05 13:55:00 -0800791 image_param_defaults(param);
792
793 const struct isl_surf *surf = &view->image->color_surface.isl;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800794 const int cpp = isl_format_get_layout(surf->format)->bs;
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800795 const struct isl_extent3d image_align_sa =
796 isl_surf_get_image_alignment_sa(surf);
Jason Ekstrandba393c92016-01-05 13:55:00 -0800797
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800798 param->size[0] = view->extent.width;
799 param->size[1] = view->extent.height;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800800 if (surf->dim == ISL_SURF_DIM_3D) {
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800801 param->size[2] = view->extent.depth;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800802 } else {
803 param->size[2] = surf->logical_level0_px.array_len - view->base_layer;
804 }
805
Nanley Chery308ec022016-01-27 09:29:09 -0800806 isl_surf_get_image_offset_el(surf, view->base_mip, view->base_layer, 0,
Jason Ekstrandba393c92016-01-05 13:55:00 -0800807 &param->offset[0], &param->offset[1]);
808
809 param->stride[0] = cpp;
810 param->stride[1] = surf->row_pitch / cpp;
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800811
812 if (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D) {
813 param->stride[2] = util_align_npot(param->size[0], image_align_sa.w);
814 param->stride[3] = util_align_npot(param->size[1], image_align_sa.h);
815 } else {
816 param->stride[2] = 0;
817 param->stride[3] = isl_surf_get_array_pitch_el_rows(surf);
818 }
Jason Ekstrandba393c92016-01-05 13:55:00 -0800819
820 switch (surf->tiling) {
821 case ISL_TILING_LINEAR:
822 /* image_param_defaults is good enough */
823 break;
824
825 case ISL_TILING_X:
826 /* An X tile is a rectangular block of 512x8 bytes. */
827 param->tiling[0] = util_logbase2(512 / cpp);
828 param->tiling[1] = util_logbase2(8);
829
830 if (device->isl_dev.has_bit6_swizzling) {
831 /* Right shifts required to swizzle bits 9 and 10 of the memory
832 * address with bit 6.
833 */
834 param->swizzling[0] = 3;
835 param->swizzling[1] = 4;
836 }
837 break;
838
839 case ISL_TILING_Y0:
840 /* The layout of a Y-tiled surface in memory isn't really fundamentally
841 * different to the layout of an X-tiled surface, we simply pretend that
842 * the surface is broken up in a number of smaller 16Bx32 tiles, each
843 * one arranged in X-major order just like is the case for X-tiling.
844 */
845 param->tiling[0] = util_logbase2(16 / cpp);
846 param->tiling[1] = util_logbase2(32);
847
848 if (device->isl_dev.has_bit6_swizzling) {
849 /* Right shift required to swizzle bit 9 of the memory address with
850 * bit 6.
851 */
852 param->swizzling[0] = 3;
853 param->swizzling[1] = 0xff;
854 }
855 break;
856
857 default:
858 assert(!"Unhandled storage image tiling");
859 }
860
861 /* 3D textures are arranged in 2D in memory with 2^lod slices per row. The
862 * address calculation algorithm (emit_address_calculation() in
863 * brw_fs_surface_builder.cpp) handles this as a sort of tiling with
864 * modulus equal to the LOD.
865 */
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800866 param->tiling[2] = (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D ?
867 view->base_mip : 0);
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800868}
Jason Ekstrand783a2112015-12-14 16:51:12 -0800869
870void
871anv_buffer_view_fill_image_param(struct anv_device *device,
872 struct anv_buffer_view *view,
873 struct brw_image_param *param)
874{
Jason Ekstrandba393c92016-01-05 13:55:00 -0800875 image_param_defaults(param);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800876
Chad Versace89b68dc2016-01-05 09:59:07 -0800877 param->stride[0] = isl_format_layouts[view->format].bs;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800878 param->size[0] = view->range / param->stride[0];
879}