blob: b51938740bd59252d9085af5f17c1777339f2d6c [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
37choose_isl_surf_usage(const struct anv_image_create_info *info,
Chad Versace2f270f02015-12-04 16:29:25 -080038 VkImageAspectFlags aspect)
Chad Versaceb3693892015-12-02 16:46:16 -080039{
40 const VkImageCreateInfo *vk_info = info->vk_info;
41 isl_surf_usage_flags_t isl_flags = 0;
42
43 /* FINISHME: Support aux surfaces */
44 isl_flags |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
45
46 if (vk_info->usage & VK_IMAGE_USAGE_SAMPLED_BIT)
47 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
48
49 if (vk_info->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
50 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
51
52 if (vk_info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
53 isl_flags |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
54
55 if (vk_info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
56 isl_flags |= ISL_SURF_USAGE_CUBE_BIT;
57
58 if (vk_info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
Chad Versace2f270f02015-12-04 16:29:25 -080059 switch (aspect) {
60 default:
61 unreachable("bad VkImageAspect");
62 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versaceb3693892015-12-02 16:46:16 -080063 isl_flags |= ISL_SURF_USAGE_DEPTH_BIT;
Chad Versace2f270f02015-12-04 16:29:25 -080064 break;
65 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versaceb3693892015-12-02 16:46:16 -080066 isl_flags |= ISL_SURF_USAGE_STENCIL_BIT;
Chad Versace2f270f02015-12-04 16:29:25 -080067 break;
Chad Versaceb3693892015-12-02 16:46:16 -080068 }
69 }
70
71 if (vk_info->usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
72 /* Meta implements transfers by sampling from the source image. */
73 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
74 }
75
76 if (vk_info->usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
77 /* Meta implements transfers by rendering into the destination image. */
78 isl_flags |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
79 }
80
81 return isl_flags;
82}
Chad Versace5a6b2e62015-08-17 13:50:43 -070083
84/**
Chad Versace9098e0f2015-12-07 09:22:49 -080085 * Exactly one bit must be set in \a aspect.
86 */
87static struct anv_surface *
88get_surface(struct anv_image *image, VkImageAspectFlags aspect)
89{
90 switch (aspect) {
91 default:
92 unreachable("bad VkImageAspect");
93 case VK_IMAGE_ASPECT_COLOR_BIT:
94 return &image->color_surface;
95 case VK_IMAGE_ASPECT_DEPTH_BIT:
96 return &image->depth_surface;
97 case VK_IMAGE_ASPECT_STENCIL_BIT:
98 return &image->stencil_surface;
99 }
100}
101
102/**
103 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
104 * image's memory requirements (that is, the image's size and alignment).
105 *
106 * Exactly one bit must be set in \a aspect.
Chad Versace5a6b2e62015-08-17 13:50:43 -0700107 */
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700108static VkResult
Chad Versace9098e0f2015-12-07 09:22:49 -0800109make_surface(const struct anv_device *dev,
110 struct anv_image *image,
111 const struct anv_image_create_info *anv_info,
112 VkImageAspectFlags aspect)
Chad Versacec6e76ae2015-06-26 18:48:34 -0700113{
Chad Versaceb3693892015-12-02 16:46:16 -0800114 const VkImageCreateInfo *vk_info = anv_info->vk_info;
Chad Versace3d85a282015-12-07 08:53:43 -0800115 bool ok UNUSED;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700116
Chad Versaceb3693892015-12-02 16:46:16 -0800117 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
118 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
119 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
120 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
121 };
Chad Versacec6e76ae2015-06-26 18:48:34 -0700122
Chad Versace64e8af62015-12-07 08:50:28 -0800123 isl_tiling_flags_t tiling_flags = anv_info->isl_tiling_flags;
124 if (vk_info->tiling == VK_IMAGE_TILING_LINEAR)
125 tiling_flags &= ISL_TILING_LINEAR_BIT;
126
Chad Versace9098e0f2015-12-07 09:22:49 -0800127 struct anv_surface *anv_surf = get_surface(image, aspect);
128
129 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
Chad Versaceb3693892015-12-02 16:46:16 -0800130 .dim = vk_to_isl_surf_dim[vk_info->imageType],
Jason Ekstrand2712c0c2015-12-31 18:31:31 -0800131 .format = anv_get_isl_format(vk_info->format, aspect, vk_info->tiling),
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 Versace2f270f02015-12-04 16:29:25 -0800140 .usage = choose_isl_surf_usage(anv_info, 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
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800160 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700161 /* Meta will transfer from the image by binding it as a texture. */
162 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
163 }
164
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800165 if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700166 /* Meta will transfer to the image by binding it as a color attachment,
167 * even if the image format is not a color format.
168 */
169 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
170 }
171
172 return usage;
173}
174
Chad Versace127cb3f2015-06-26 20:12:42 -0700175VkResult
176anv_image_create(VkDevice _device,
177 const struct anv_image_create_info *create_info,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800178 const VkAllocationCallbacks* alloc,
Chad Versace127cb3f2015-06-26 20:12:42 -0700179 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700180{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700181 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versacefdcd71f2015-06-26 20:06:08 -0700182 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700183 struct anv_image *image = NULL;
184 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700185
186 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
187
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700188 anv_assert(pCreateInfo->mipLevels > 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800189 anv_assert(pCreateInfo->arrayLayers > 0);
Jason Ekstrandd3547e72015-12-01 14:09:17 -0800190 anv_assert(pCreateInfo->samples == VK_SAMPLE_COUNT_1_BIT);
Chad Versace5d7103e2015-06-26 09:05:46 -0700191 anv_assert(pCreateInfo->extent.width > 0);
192 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700193 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700194
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800195 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
196 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700197 if (!image)
198 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
Chad Versace67a76592015-06-26 09:17:52 -0700199
Chad Versacec6e76ae2015-06-26 18:48:34 -0700200 memset(image, 0, sizeof(*image));
201 image->type = pCreateInfo->imageType;
202 image->extent = pCreateInfo->extent;
Jason Ekstrand3200a812015-12-31 12:39:34 -0800203 image->vk_format = pCreateInfo->format;
Chad Versaceded736f2015-08-17 13:10:40 -0700204 image->format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700205 image->levels = pCreateInfo->mipLevels;
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800206 image->array_size = pCreateInfo->arrayLayers;
Chad Versace24de3d42015-10-06 19:11:58 -0700207 image->usage = anv_image_get_full_usage(pCreateInfo);
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800208 image->tiling = pCreateInfo->tiling;
Chad Versace67a76592015-06-26 09:17:52 -0700209
Jason Ekstrandff05f632015-12-07 17:17:30 -0800210 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700211 image->needs_nonrt_surface_state = true;
Chad Versace44143a12015-10-06 18:17:09 -0700212 }
213
Chad Versace24de3d42015-10-06 19:11:58 -0700214 if (image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
215 image->needs_color_rt_surface_state = true;
Chad Versace44143a12015-10-06 18:17:09 -0700216 }
217
Jason Ekstrandff05f632015-12-07 17:17:30 -0800218 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
219 image->needs_storage_surface_state = true;
220 }
221
Chad Versace941b48e2015-08-28 07:08:58 -0700222 if (likely(anv_format_is_color(image->format))) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800223 r = make_surface(device, image, create_info,
224 VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700225 if (r != VK_SUCCESS)
226 goto fail;
Chad Versace941b48e2015-08-28 07:08:58 -0700227 } else {
228 if (image->format->depth_format) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800229 r = make_surface(device, image, create_info,
230 VK_IMAGE_ASPECT_DEPTH_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700231 if (r != VK_SUCCESS)
232 goto fail;
233 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700234
Chad Versace941b48e2015-08-28 07:08:58 -0700235 if (image->format->has_stencil) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800236 r = make_surface(device, image, create_info,
237 VK_IMAGE_ASPECT_STENCIL_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700238 if (r != VK_SUCCESS)
239 goto fail;
240 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700241 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700242
Jason Ekstranda52e2082015-07-09 20:24:07 -0700243 *pImage = anv_image_to_handle(image);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700244
245 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700246
247fail:
248 if (image)
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800249 anv_free2(&device->alloc, alloc, image);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700250
251 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700252}
253
Chad Versace127cb3f2015-06-26 20:12:42 -0700254VkResult
255anv_CreateImage(VkDevice device,
256 const VkImageCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800257 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700258 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700259{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700260 return anv_image_create(device,
261 &(struct anv_image_create_info) {
262 .vk_info = pCreateInfo,
Chad Versace64e8af62015-12-07 08:50:28 -0800263 .isl_tiling_flags = ISL_TILING_ANY_MASK,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700264 },
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800265 pAllocator,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700266 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700267}
268
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700269void
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800270anv_DestroyImage(VkDevice _device, VkImage _image,
271 const VkAllocationCallbacks *pAllocator)
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700272{
273 ANV_FROM_HANDLE(anv_device, device, _device);
274
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800275 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700276}
277
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700278static void
279anv_surface_get_subresource_layout(struct anv_image *image,
280 struct anv_surface *surface,
281 const VkImageSubresource *subresource,
282 VkSubresourceLayout *layout)
283{
284 /* If we are on a non-zero mip level or array slice, we need to
285 * calculate a real offset.
286 */
287 anv_assert(subresource->mipLevel == 0);
288 anv_assert(subresource->arrayLayer == 0);
289
290 layout->offset = surface->offset;
Chad Versace981ef2f2015-12-03 08:40:47 -0800291 layout->rowPitch = surface->isl.row_pitch;
292 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
Jason Ekstrand8a81d132016-01-06 19:27:10 -0800293 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
Chad Versace981ef2f2015-12-03 08:40:47 -0800294 layout->size = surface->isl.size;
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700295}
296
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800297void anv_GetImageSubresourceLayout(
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700298 VkDevice device,
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700299 VkImage _image,
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700300 const VkImageSubresource* pSubresource,
301 VkSubresourceLayout* pLayout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700302{
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700303 ANV_FROM_HANDLE(anv_image, image, _image);
304
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800305 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
306
307 switch (pSubresource->aspectMask) {
308 case VK_IMAGE_ASPECT_COLOR_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700309 anv_surface_get_subresource_layout(image, &image->color_surface,
310 pSubresource, pLayout);
311 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800312 case VK_IMAGE_ASPECT_DEPTH_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700313 anv_surface_get_subresource_layout(image, &image->depth_surface,
314 pSubresource, pLayout);
315 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800316 case VK_IMAGE_ASPECT_STENCIL_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700317 anv_surface_get_subresource_layout(image, &image->stencil_surface,
318 pSubresource, pLayout);
319 break;
320 default:
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800321 assert(!"Invalid image aspect");
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700322 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700323}
324
Chad Versace127cb3f2015-06-26 20:12:42 -0700325VkResult
Chad Versace5b04db72015-07-06 16:24:28 -0700326anv_validate_CreateImageView(VkDevice _device,
327 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800328 const VkAllocationCallbacks *pAllocator,
Chad Versace5b04db72015-07-06 16:24:28 -0700329 VkImageView *pView)
330{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700331 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace23075bc2015-07-06 17:04:13 -0700332 const VkImageSubresourceRange *subresource;
Chad Versace23075bc2015-07-06 17:04:13 -0700333 const struct anv_format *view_format_info;
Chad Versace5b04db72015-07-06 16:24:28 -0700334
Chad Versace23075bc2015-07-06 17:04:13 -0700335 /* Validate structure type before dereferencing it. */
Chad Versace5b04db72015-07-06 16:24:28 -0700336 assert(pCreateInfo);
337 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chad Versace23075bc2015-07-06 17:04:13 -0700338 subresource = &pCreateInfo->subresourceRange;
339
Chad Versace23075bc2015-07-06 17:04:13 -0700340 /* Validate viewType is in range before using it. */
341 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
342 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700343
344 /* Validate format is in range before using it. */
345 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
346 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700347 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
348
349 /* Validate channel swizzles. */
Jason Ekstranda53f23d2015-11-30 13:06:12 -0800350 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
351 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
352 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
353 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
354 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
355 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
356 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
357 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700358
359 /* Validate subresource. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700360 assert(subresource->aspectMask != 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800361 assert(subresource->levelCount > 0);
362 assert(subresource->layerCount > 0);
Chad Versace23075bc2015-07-06 17:04:13 -0700363 assert(subresource->baseMipLevel < image->levels);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800364 assert(subresource->baseMipLevel + subresource->levelCount <= image->levels);
Jason Ekstrand1e4263b2015-10-06 10:27:50 -0700365 assert(subresource->baseArrayLayer < image->array_size);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800366 assert(subresource->baseArrayLayer + subresource->layerCount <= image->array_size);
Chad Versace5b04db72015-07-06 16:24:28 -0700367 assert(pView);
368
Chad Versace7a089bd2015-10-05 06:48:14 -0700369 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
370 | VK_IMAGE_ASPECT_STENCIL_BIT;
371
Chad Versace23075bc2015-07-06 17:04:13 -0700372 /* Validate format. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700373 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
374 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versaceded736f2015-08-17 13:10:40 -0700375 assert(!image->format->depth_format);
376 assert(!image->format->has_stencil);
Chad Versace23075bc2015-07-06 17:04:13 -0700377 assert(!view_format_info->depth_format);
378 assert(!view_format_info->has_stencil);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800379 assert(view_format_info->isl_layout->bs ==
380 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700381 } else if (subresource->aspectMask & ds_flags) {
382 assert((subresource->aspectMask & ~ds_flags) == 0);
383
384 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
385 assert(image->format->depth_format);
386 assert(view_format_info->depth_format);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800387 assert(view_format_info->isl_layout->bs ==
388 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700389 }
390
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800391 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Chad Versace7a089bd2015-10-05 06:48:14 -0700392 /* FINISHME: Is it legal to have an R8 view of S8? */
393 assert(image->format->has_stencil);
394 assert(view_format_info->has_stencil);
395 }
396 } else {
397 assert(!"bad VkImageSubresourceRange::aspectFlags");
Chad Versace23075bc2015-07-06 17:04:13 -0700398 }
Chad Versace5b04db72015-07-06 16:24:28 -0700399
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800400 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
Chad Versace5b04db72015-07-06 16:24:28 -0700401}
402
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700403void
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800404anv_fill_image_surface_state(struct anv_device *device, void *state_map,
405 struct anv_image_view *iview,
406 const VkImageViewCreateInfo *pCreateInfo,
407 VkImageUsageFlagBits usage)
408{
409 switch (device->info.gen) {
410 case 7:
411 if (device->info.is_haswell)
412 gen75_fill_image_surface_state(device, state_map, iview,
413 pCreateInfo, usage);
414 else
415 gen7_fill_image_surface_state(device, state_map, iview,
416 pCreateInfo, usage);
417 break;
418 case 8:
419 gen8_fill_image_surface_state(device, state_map, iview,
420 pCreateInfo, usage);
421 break;
422 case 9:
423 gen9_fill_image_surface_state(device, state_map, iview,
424 pCreateInfo, usage);
425 break;
426 default:
427 unreachable("unsupported gen\n");
428 }
429
430 if (!device->info.has_llc)
431 anv_state_clflush(iview->nonrt_surface_state);
432}
433
434static struct anv_state
435alloc_surface_state(struct anv_device *device,
436 struct anv_cmd_buffer *cmd_buffer)
437{
438 if (cmd_buffer) {
439 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
440 } else {
441 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
442 }
443}
444
445void
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700446anv_image_view_init(struct anv_image_view *iview,
447 struct anv_device *device,
448 const VkImageViewCreateInfo* pCreateInfo,
449 struct anv_cmd_buffer *cmd_buffer)
450{
Chad Versace44143a12015-10-06 18:17:09 -0700451 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace24de3d42015-10-06 19:11:58 -0700452 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Chad Versace44143a12015-10-06 18:17:09 -0700453
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800454 assert(range->layerCount > 0);
Chad Versace24de3d42015-10-06 19:11:58 -0700455 assert(range->baseMipLevel < image->levels);
Chad Versace44143a12015-10-06 18:17:09 -0700456 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
Chad Versace24de3d42015-10-06 19:11:58 -0700457 VK_IMAGE_USAGE_STORAGE_BIT |
458 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Chad Versace0ca3c842015-10-07 11:39:49 -0700459 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
Chad Versace24de3d42015-10-06 19:11:58 -0700460
461 switch (image->type) {
462 default:
463 unreachable("bad VkImageType");
464 case VK_IMAGE_TYPE_1D:
465 case VK_IMAGE_TYPE_2D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800466 assert(range->baseArrayLayer + range->layerCount - 1 <= image->array_size);
Chad Versace24de3d42015-10-06 19:11:58 -0700467 break;
468 case VK_IMAGE_TYPE_3D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800469 assert(range->baseArrayLayer + range->layerCount - 1
Chad Versace24de3d42015-10-06 19:11:58 -0700470 <= anv_minify(image->extent.depth, range->baseMipLevel));
471 break;
472 }
Chad Versace44143a12015-10-06 18:17:09 -0700473
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800474 struct anv_surface *surface =
475 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
476
477 iview->image = image;
478 iview->bo = image->bo;
479 iview->offset = image->offset + surface->offset;
480
481 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800482 iview->vk_format = pCreateInfo->format;
483 iview->format = anv_get_isl_format(pCreateInfo->format, iview->aspect_mask,
484 image->tiling);
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800485
486 iview->extent = (VkExtent3D) {
487 .width = anv_minify(image->extent.width, range->baseMipLevel),
488 .height = anv_minify(image->extent.height, range->baseMipLevel),
489 .depth = anv_minify(image->extent.depth, range->baseMipLevel),
490 };
491
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800492 if (image->needs_nonrt_surface_state) {
493 iview->nonrt_surface_state = alloc_surface_state(device, cmd_buffer);
494
495 anv_fill_image_surface_state(device, iview->nonrt_surface_state.map,
496 iview, pCreateInfo,
497 VK_IMAGE_USAGE_SAMPLED_BIT);
498 } else {
499 iview->nonrt_surface_state.alloc_size = 0;
500 }
501
502 if (image->needs_color_rt_surface_state) {
503 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
504
505 anv_fill_image_surface_state(device, iview->color_rt_surface_state.map,
506 iview, pCreateInfo,
507 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
508 } else {
509 iview->color_rt_surface_state.alloc_size = 0;
510 }
511
512 if (image->needs_storage_surface_state) {
513 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
514
515 anv_fill_image_surface_state(device, iview->storage_surface_state.map,
516 iview, pCreateInfo,
517 VK_IMAGE_USAGE_STORAGE_BIT);
518 } else {
519 iview->storage_surface_state.alloc_size = 0;
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700520 }
521}
522
Chad Versace5b04db72015-07-06 16:24:28 -0700523VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700524anv_CreateImageView(VkDevice _device,
525 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800526 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700527 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700528{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700529 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700530 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700531
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800532 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
533 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700534 if (view == NULL)
535 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
536
537 anv_image_view_init(view, device, pCreateInfo, NULL);
538
539 *pView = anv_image_view_to_handle(view);
540
541 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700542}
543
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800544void
545anv_DestroyImageView(VkDevice _device, VkImageView _iview,
546 const VkAllocationCallbacks *pAllocator)
Chad Versace24de3d42015-10-06 19:11:58 -0700547{
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800548 ANV_FROM_HANDLE(anv_device, device, _device);
549 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
550
Chad Versace24de3d42015-10-06 19:11:58 -0700551 if (iview->image->needs_color_rt_surface_state) {
552 anv_state_pool_free(&device->surface_state_pool,
553 iview->color_rt_surface_state);
554 }
555
556 if (iview->image->needs_nonrt_surface_state) {
557 anv_state_pool_free(&device->surface_state_pool,
558 iview->nonrt_surface_state);
559 }
560
Jason Ekstrandff05f632015-12-07 17:17:30 -0800561 if (iview->image->needs_storage_surface_state) {
562 anv_state_pool_free(&device->surface_state_pool,
563 iview->storage_surface_state);
564 }
565
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800566 anv_free2(&device->alloc, pAllocator, iview);
Jason Ekstrand84783502015-07-10 20:18:52 -0700567}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700568
Jason Ekstrandc5618602015-12-12 16:11:23 -0800569VkResult
570anv_CreateBufferView(VkDevice _device,
571 const VkBufferViewCreateInfo *pCreateInfo,
572 const VkAllocationCallbacks *pAllocator,
573 VkBufferView *pView)
574{
575 ANV_FROM_HANDLE(anv_device, device, _device);
576 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
577 struct anv_buffer_view *view;
578
Jason Ekstrandc5618602015-12-12 16:11:23 -0800579 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
580 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
581 if (!view)
582 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
583
Jason Ekstrandc5618602015-12-12 16:11:23 -0800584 const struct anv_format *format =
585 anv_format_for_vk_format(pCreateInfo->format);
586
Jason Ekstrand783a2112015-12-14 16:51:12 -0800587 view->format = format->surface_format;
588 view->bo = buffer->bo;
589 view->offset = buffer->offset + pCreateInfo->offset;
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800590 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
591 buffer->size - view->offset : pCreateInfo->range;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800592
593 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
594 view->surface_state =
595 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
596
597 anv_fill_buffer_surface_state(device, view->surface_state.map,
598 view->format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800599 view->offset, view->range,
Chad Versace89b68dc2016-01-05 09:59:07 -0800600 format->isl_layout->bs);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800601 } else {
602 view->surface_state = (struct anv_state){ 0 };
603 }
604
605 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
606 view->storage_surface_state =
607 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
608
609 enum isl_format storage_format =
610 isl_lower_storage_image_format(&device->isl_dev, view->format);
611
612 anv_fill_buffer_surface_state(device, view->storage_surface_state.map,
613 storage_format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800614 view->offset, view->range,
Chad Versace89b68dc2016-01-05 09:59:07 -0800615 format->isl_layout->bs);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800616 } else {
617 view->storage_surface_state = (struct anv_state){ 0 };
618 }
Jason Ekstrandc5618602015-12-12 16:11:23 -0800619
620 *pView = anv_buffer_view_to_handle(view);
621
622 return VK_SUCCESS;
623}
624
625void
626anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
627 const VkAllocationCallbacks *pAllocator)
628{
629 ANV_FROM_HANDLE(anv_device, device, _device);
630 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
631
Jason Ekstrand783a2112015-12-14 16:51:12 -0800632 if (view->surface_state.alloc_size > 0)
633 anv_state_pool_free(&device->surface_state_pool,
634 view->surface_state);
635
636 if (view->storage_surface_state.alloc_size > 0)
637 anv_state_pool_free(&device->surface_state_pool,
638 view->storage_surface_state);
639
Jason Ekstrandc5618602015-12-12 16:11:23 -0800640 anv_free2(&device->alloc, pAllocator, view);
641}
642
Chad Versace941b48e2015-08-28 07:08:58 -0700643struct anv_surface *
Chad Versace7a089bd2015-10-05 06:48:14 -0700644anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
Chad Versace941b48e2015-08-28 07:08:58 -0700645{
Chad Versace7a089bd2015-10-05 06:48:14 -0700646 switch (aspect_mask) {
647 case VK_IMAGE_ASPECT_COLOR_BIT:
Chad Versace24de3d42015-10-06 19:11:58 -0700648 /* Dragons will eat you.
649 *
650 * Meta attaches all destination surfaces as color render targets. Guess
651 * what surface the Meta Dragons really want.
652 */
653 if (image->format->depth_format && image->format->has_stencil) {
654 anv_finishme("combined depth stencil formats");
655 return &image->depth_surface;
656 } else if (image->format->depth_format) {
657 return &image->depth_surface;
658 } else if (image->format->has_stencil) {
659 return &image->stencil_surface;
660 } else {
661 return &image->color_surface;
662 }
663 break;
Chad Versace7a089bd2015-10-05 06:48:14 -0700664 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700665 assert(image->format->depth_format);
666 return &image->depth_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700667 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700668 assert(image->format->has_stencil);
Chad Versace941b48e2015-08-28 07:08:58 -0700669 return &image->stencil_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700670 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace03dd7222015-10-07 09:03:47 -0700671 if (image->format->depth_format && image->format->has_stencil) {
672 /* FINISHME: The Vulkan spec (git a511ba2) requires support for combined
673 * depth stencil formats. Specifically, it states:
674 *
675 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
676 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
677 */
678 anv_finishme("combined depthstencil aspect");
679 return &image->depth_surface;
680 } else if (image->format->depth_format) {
681 return &image->depth_surface;
682 } else if (image->format->has_stencil) {
683 return &image->stencil_surface;
684 }
685 /* fallthrough */
Chad Versace941b48e2015-08-28 07:08:58 -0700686 default:
687 unreachable("image does not have aspect");
688 return NULL;
689 }
690}
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800691
692void
693anv_image_view_fill_image_param(struct anv_device *device,
694 struct anv_image_view *view,
695 struct brw_image_param *param)
696{
697 memset(param, 0, sizeof *param);
698 anv_finishme("Actually fill out brw_image_param");
699}
Jason Ekstrand783a2112015-12-14 16:51:12 -0800700
701void
702anv_buffer_view_fill_image_param(struct anv_device *device,
703 struct anv_buffer_view *view,
704 struct brw_image_param *param)
705{
706 /* Set the swizzling shifts to all-ones to effectively disable swizzling --
707 * See emit_address_calculation() in brw_fs_surface_builder.cpp for a more
708 * detailed explanation of these parameters.
709 */
710 param->swizzling[0] = 0xff;
711 param->swizzling[1] = 0xff;
712
Chad Versace89b68dc2016-01-05 09:59:07 -0800713 param->stride[0] = isl_format_layouts[view->format].bs;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800714 param->size[0] = view->range / param->stride[0];
715}