blob: 8b2003587ac7873e7d1e35e9ac1ca6df4e5eaa2e [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],
Jordan Justenc20f78d2016-01-26 11:10:56 -0800131 .format = anv_get_isl_format(vk_info->format, aspect,
132 vk_info->tiling, NULL),
Chad Versaceb3693892015-12-02 16:46:16 -0800133 .width = vk_info->extent.width,
134 .height = vk_info->extent.height,
135 .depth = vk_info->extent.depth,
136 .levels = vk_info->mipLevels,
137 .array_len = vk_info->arrayLayers,
138 .samples = vk_info->samples,
139 .min_alignment = 0,
140 .min_pitch = 0,
Chad Versace2f270f02015-12-04 16:29:25 -0800141 .usage = choose_isl_surf_usage(anv_info, aspect),
Chad Versace64e8af62015-12-07 08:50:28 -0800142 .tiling_flags = tiling_flags);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700143
Chad Versace3d85a282015-12-07 08:53:43 -0800144 /* isl_surf_init() will fail only if provided invalid input. Invalid input
145 * is illegal in Vulkan.
146 */
147 assert(ok);
148
Chad Versace9098e0f2015-12-07 09:22:49 -0800149 anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment);
150 image->size = anv_surf->offset + anv_surf->isl.size;
151 image->alignment = MAX(image->alignment, anv_surf->isl.alignment);
Chad Versaceb3693892015-12-02 16:46:16 -0800152
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700153 return VK_SUCCESS;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700154}
155
Chad Versace24de3d42015-10-06 19:11:58 -0700156static VkImageUsageFlags
157anv_image_get_full_usage(const VkImageCreateInfo *info)
158{
159 VkImageUsageFlags usage = info->usage;
160
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800161 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700162 /* Meta will transfer from 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_DST_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700167 /* Meta will transfer to the image by binding it as a color attachment,
168 * even if the image format is not a color format.
169 */
170 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
171 }
172
173 return usage;
174}
175
Chad Versace127cb3f2015-06-26 20:12:42 -0700176VkResult
177anv_image_create(VkDevice _device,
178 const struct anv_image_create_info *create_info,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800179 const VkAllocationCallbacks* alloc,
Chad Versace127cb3f2015-06-26 20:12:42 -0700180 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700181{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700182 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versacefdcd71f2015-06-26 20:06:08 -0700183 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700184 struct anv_image *image = NULL;
185 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700186
187 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
188
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700189 anv_assert(pCreateInfo->mipLevels > 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800190 anv_assert(pCreateInfo->arrayLayers > 0);
Chad Versaced96d78c2016-01-22 17:16:20 -0800191 anv_assert(pCreateInfo->samples > 0);
Chad Versace5d7103e2015-06-26 09:05:46 -0700192 anv_assert(pCreateInfo->extent.width > 0);
193 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700194 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700195
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800196 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
197 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700198 if (!image)
199 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
Chad Versace67a76592015-06-26 09:17:52 -0700200
Chad Versacec6e76ae2015-06-26 18:48:34 -0700201 memset(image, 0, sizeof(*image));
202 image->type = pCreateInfo->imageType;
203 image->extent = pCreateInfo->extent;
Jason Ekstrand3200a812015-12-31 12:39:34 -0800204 image->vk_format = pCreateInfo->format;
Chad Versaceded736f2015-08-17 13:10:40 -0700205 image->format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700206 image->levels = pCreateInfo->mipLevels;
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800207 image->array_size = pCreateInfo->arrayLayers;
Chad Versacedfcb4ee2016-01-20 16:34:23 -0800208 image->samples = pCreateInfo->samples;
Chad Versace24de3d42015-10-06 19:11:58 -0700209 image->usage = anv_image_get_full_usage(pCreateInfo);
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800210 image->tiling = pCreateInfo->tiling;
Chad Versace67a76592015-06-26 09:17:52 -0700211
Jason Ekstrandff05f632015-12-07 17:17:30 -0800212 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700213 image->needs_nonrt_surface_state = true;
Chad Versace44143a12015-10-06 18:17:09 -0700214 }
215
Chad Versace24de3d42015-10-06 19:11:58 -0700216 if (image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
217 image->needs_color_rt_surface_state = true;
Chad Versace44143a12015-10-06 18:17:09 -0700218 }
219
Jason Ekstrandff05f632015-12-07 17:17:30 -0800220 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
221 image->needs_storage_surface_state = true;
222 }
223
Chad Versace941b48e2015-08-28 07:08:58 -0700224 if (likely(anv_format_is_color(image->format))) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800225 r = make_surface(device, image, create_info,
226 VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700227 if (r != VK_SUCCESS)
228 goto fail;
Chad Versace941b48e2015-08-28 07:08:58 -0700229 } else {
230 if (image->format->depth_format) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800231 r = make_surface(device, image, create_info,
232 VK_IMAGE_ASPECT_DEPTH_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
Chad Versace941b48e2015-08-28 07:08:58 -0700237 if (image->format->has_stencil) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800238 r = make_surface(device, image, create_info,
239 VK_IMAGE_ASPECT_STENCIL_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700240 if (r != VK_SUCCESS)
241 goto fail;
242 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700243 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700244
Jason Ekstranda52e2082015-07-09 20:24:07 -0700245 *pImage = anv_image_to_handle(image);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700246
247 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700248
249fail:
250 if (image)
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800251 anv_free2(&device->alloc, alloc, image);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700252
253 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700254}
255
Chad Versace127cb3f2015-06-26 20:12:42 -0700256VkResult
257anv_CreateImage(VkDevice device,
258 const VkImageCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800259 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700260 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700261{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700262 return anv_image_create(device,
263 &(struct anv_image_create_info) {
264 .vk_info = pCreateInfo,
Chad Versace64e8af62015-12-07 08:50:28 -0800265 .isl_tiling_flags = ISL_TILING_ANY_MASK,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700266 },
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800267 pAllocator,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700268 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700269}
270
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700271void
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800272anv_DestroyImage(VkDevice _device, VkImage _image,
273 const VkAllocationCallbacks *pAllocator)
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700274{
275 ANV_FROM_HANDLE(anv_device, device, _device);
276
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800277 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700278}
279
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700280static void
281anv_surface_get_subresource_layout(struct anv_image *image,
282 struct anv_surface *surface,
283 const VkImageSubresource *subresource,
284 VkSubresourceLayout *layout)
285{
286 /* If we are on a non-zero mip level or array slice, we need to
287 * calculate a real offset.
288 */
289 anv_assert(subresource->mipLevel == 0);
290 anv_assert(subresource->arrayLayer == 0);
291
292 layout->offset = surface->offset;
Chad Versace981ef2f2015-12-03 08:40:47 -0800293 layout->rowPitch = surface->isl.row_pitch;
294 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
Jason Ekstrand8a81d132016-01-06 19:27:10 -0800295 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
Chad Versace981ef2f2015-12-03 08:40:47 -0800296 layout->size = surface->isl.size;
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700297}
298
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800299void anv_GetImageSubresourceLayout(
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700300 VkDevice device,
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700301 VkImage _image,
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700302 const VkImageSubresource* pSubresource,
303 VkSubresourceLayout* pLayout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700304{
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700305 ANV_FROM_HANDLE(anv_image, image, _image);
306
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800307 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
308
309 switch (pSubresource->aspectMask) {
310 case VK_IMAGE_ASPECT_COLOR_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700311 anv_surface_get_subresource_layout(image, &image->color_surface,
312 pSubresource, pLayout);
313 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800314 case VK_IMAGE_ASPECT_DEPTH_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700315 anv_surface_get_subresource_layout(image, &image->depth_surface,
316 pSubresource, pLayout);
317 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800318 case VK_IMAGE_ASPECT_STENCIL_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700319 anv_surface_get_subresource_layout(image, &image->stencil_surface,
320 pSubresource, pLayout);
321 break;
322 default:
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800323 assert(!"Invalid image aspect");
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700324 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700325}
326
Chad Versace127cb3f2015-06-26 20:12:42 -0700327VkResult
Chad Versace5b04db72015-07-06 16:24:28 -0700328anv_validate_CreateImageView(VkDevice _device,
329 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800330 const VkAllocationCallbacks *pAllocator,
Chad Versace5b04db72015-07-06 16:24:28 -0700331 VkImageView *pView)
332{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700333 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace23075bc2015-07-06 17:04:13 -0700334 const VkImageSubresourceRange *subresource;
Chad Versace23075bc2015-07-06 17:04:13 -0700335 const struct anv_format *view_format_info;
Chad Versace5b04db72015-07-06 16:24:28 -0700336
Chad Versace23075bc2015-07-06 17:04:13 -0700337 /* Validate structure type before dereferencing it. */
Chad Versace5b04db72015-07-06 16:24:28 -0700338 assert(pCreateInfo);
339 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chad Versace23075bc2015-07-06 17:04:13 -0700340 subresource = &pCreateInfo->subresourceRange;
341
Chad Versace23075bc2015-07-06 17:04:13 -0700342 /* Validate viewType is in range before using it. */
343 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
344 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700345
346 /* Validate format is in range before using it. */
347 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
348 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700349 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
350
351 /* Validate channel swizzles. */
Jason Ekstranda53f23d2015-11-30 13:06:12 -0800352 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
353 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
354 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
355 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
356 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
357 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
358 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
359 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700360
361 /* Validate subresource. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700362 assert(subresource->aspectMask != 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800363 assert(subresource->levelCount > 0);
364 assert(subresource->layerCount > 0);
Chad Versace23075bc2015-07-06 17:04:13 -0700365 assert(subresource->baseMipLevel < image->levels);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800366 assert(subresource->baseMipLevel + subresource->levelCount <= image->levels);
Jason Ekstrand1e4263b2015-10-06 10:27:50 -0700367 assert(subresource->baseArrayLayer < image->array_size);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800368 assert(subresource->baseArrayLayer + subresource->layerCount <= image->array_size);
Chad Versace5b04db72015-07-06 16:24:28 -0700369 assert(pView);
370
Chad Versace7a089bd2015-10-05 06:48:14 -0700371 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
372 | VK_IMAGE_ASPECT_STENCIL_BIT;
373
Chad Versace23075bc2015-07-06 17:04:13 -0700374 /* Validate format. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700375 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
376 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versaceded736f2015-08-17 13:10:40 -0700377 assert(!image->format->depth_format);
378 assert(!image->format->has_stencil);
Chad Versace23075bc2015-07-06 17:04:13 -0700379 assert(!view_format_info->depth_format);
380 assert(!view_format_info->has_stencil);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800381 assert(view_format_info->isl_layout->bs ==
382 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700383 } else if (subresource->aspectMask & ds_flags) {
384 assert((subresource->aspectMask & ~ds_flags) == 0);
385
386 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
387 assert(image->format->depth_format);
388 assert(view_format_info->depth_format);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800389 assert(view_format_info->isl_layout->bs ==
390 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700391 }
392
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800393 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Chad Versace7a089bd2015-10-05 06:48:14 -0700394 /* FINISHME: Is it legal to have an R8 view of S8? */
395 assert(image->format->has_stencil);
396 assert(view_format_info->has_stencil);
397 }
398 } else {
399 assert(!"bad VkImageSubresourceRange::aspectFlags");
Chad Versace23075bc2015-07-06 17:04:13 -0700400 }
Chad Versace5b04db72015-07-06 16:24:28 -0700401
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800402 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
Chad Versace5b04db72015-07-06 16:24:28 -0700403}
404
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700405void
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800406anv_fill_image_surface_state(struct anv_device *device, struct anv_state state,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800407 struct anv_image_view *iview,
408 const VkImageViewCreateInfo *pCreateInfo,
409 VkImageUsageFlagBits usage)
410{
411 switch (device->info.gen) {
412 case 7:
413 if (device->info.is_haswell)
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800414 gen75_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800415 pCreateInfo, usage);
416 else
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800417 gen7_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800418 pCreateInfo, usage);
419 break;
420 case 8:
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800421 gen8_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800422 pCreateInfo, usage);
423 break;
424 case 9:
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800425 gen9_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800426 pCreateInfo, usage);
427 break;
428 default:
429 unreachable("unsupported gen\n");
430 }
431
432 if (!device->info.has_llc)
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800433 anv_state_clflush(state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800434}
435
436static struct anv_state
437alloc_surface_state(struct anv_device *device,
438 struct anv_cmd_buffer *cmd_buffer)
439{
440 if (cmd_buffer) {
441 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
442 } else {
443 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
444 }
445}
446
Francisco Jereza50dc702016-01-26 12:23:08 -0800447static bool
448has_matching_storage_typed_format(const struct anv_device *device,
449 enum isl_format format)
450{
451 return (isl_format_get_layout(format)->bs <= 4 ||
452 (isl_format_get_layout(format)->bs <= 8 &&
453 (device->info.gen >= 8 || device->info.is_haswell)) ||
454 device->info.gen >= 9);
455}
456
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800457static VkComponentSwizzle
Jordan Justenc20f78d2016-01-26 11:10:56 -0800458remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
459 struct anv_format_swizzle format_swizzle)
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800460{
461 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
Jordan Justenc20f78d2016-01-26 11:10:56 -0800462 swizzle = component;
463
464 switch (swizzle) {
465 case VK_COMPONENT_SWIZZLE_ZERO:
466 return VK_COMPONENT_SWIZZLE_ZERO;
467 case VK_COMPONENT_SWIZZLE_ONE:
468 return VK_COMPONENT_SWIZZLE_ONE;
469 case VK_COMPONENT_SWIZZLE_R:
470 return VK_COMPONENT_SWIZZLE_R + format_swizzle.r;
471 case VK_COMPONENT_SWIZZLE_G:
472 return VK_COMPONENT_SWIZZLE_R + format_swizzle.g;
473 case VK_COMPONENT_SWIZZLE_B:
474 return VK_COMPONENT_SWIZZLE_R + format_swizzle.b;
475 case VK_COMPONENT_SWIZZLE_A:
476 return VK_COMPONENT_SWIZZLE_R + format_swizzle.a;
477 default:
478 unreachable("Invalid swizzle");
479 }
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800480}
481
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800482void
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700483anv_image_view_init(struct anv_image_view *iview,
484 struct anv_device *device,
485 const VkImageViewCreateInfo* pCreateInfo,
486 struct anv_cmd_buffer *cmd_buffer)
487{
Chad Versace44143a12015-10-06 18:17:09 -0700488 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace24de3d42015-10-06 19:11:58 -0700489 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Chad Versace44143a12015-10-06 18:17:09 -0700490
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800491 assert(range->layerCount > 0);
Chad Versace24de3d42015-10-06 19:11:58 -0700492 assert(range->baseMipLevel < image->levels);
Chad Versace44143a12015-10-06 18:17:09 -0700493 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
Chad Versace24de3d42015-10-06 19:11:58 -0700494 VK_IMAGE_USAGE_STORAGE_BIT |
495 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Chad Versace0ca3c842015-10-07 11:39:49 -0700496 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
Chad Versace24de3d42015-10-06 19:11:58 -0700497
498 switch (image->type) {
499 default:
500 unreachable("bad VkImageType");
501 case VK_IMAGE_TYPE_1D:
502 case VK_IMAGE_TYPE_2D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800503 assert(range->baseArrayLayer + range->layerCount - 1 <= image->array_size);
Chad Versace24de3d42015-10-06 19:11:58 -0700504 break;
505 case VK_IMAGE_TYPE_3D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800506 assert(range->baseArrayLayer + range->layerCount - 1
Chad Versace24de3d42015-10-06 19:11:58 -0700507 <= anv_minify(image->extent.depth, range->baseMipLevel));
508 break;
509 }
Chad Versace44143a12015-10-06 18:17:09 -0700510
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800511 struct anv_surface *surface =
512 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
513
514 iview->image = image;
515 iview->bo = image->bo;
516 iview->offset = image->offset + surface->offset;
517
518 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800519 iview->vk_format = pCreateInfo->format;
Jordan Justenc20f78d2016-01-26 11:10:56 -0800520
521 struct anv_format_swizzle swizzle;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800522 iview->format = anv_get_isl_format(pCreateInfo->format, iview->aspect_mask,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800523 image->tiling, &swizzle);
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800524 iview->swizzle.r = remap_swizzle(pCreateInfo->components.r,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800525 VK_COMPONENT_SWIZZLE_R, swizzle);
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800526 iview->swizzle.g = remap_swizzle(pCreateInfo->components.g,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800527 VK_COMPONENT_SWIZZLE_G, swizzle);
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800528 iview->swizzle.b = remap_swizzle(pCreateInfo->components.b,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800529 VK_COMPONENT_SWIZZLE_B, swizzle);
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800530 iview->swizzle.a = remap_swizzle(pCreateInfo->components.a,
Jordan Justenc20f78d2016-01-26 11:10:56 -0800531 VK_COMPONENT_SWIZZLE_A, swizzle);
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800532
Jason Ekstrandaa9987a2016-01-05 13:54:02 -0800533 iview->base_layer = range->baseArrayLayer;
534 iview->base_mip = range->baseMipLevel;
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800535
536 if (!isl_format_is_compressed(iview->format) &&
537 isl_format_is_compressed(image->format->surface_format)) {
538 /* Scale the ImageView extent by the backing Image. This is used
539 * internally when an uncompressed ImageView is created on a
540 * compressed Image. The ImageView can therefore be used for copying
541 * data from a source Image to a destination Image.
542 */
543 const struct isl_format_layout * isl_layout = image->format->isl_layout;
544 iview->level_0_extent.width = DIV_ROUND_UP(image->extent.width , isl_layout->bw);
545 iview->level_0_extent.height = DIV_ROUND_UP(image->extent.height, isl_layout->bh);
546 iview->level_0_extent.depth = DIV_ROUND_UP(image->extent.depth , isl_layout->bd);
547 } else {
548 iview->level_0_extent.width = image->extent.width ;
549 iview->level_0_extent.height = image->extent.height;
550 iview->level_0_extent.depth = image->extent.depth ;
551 }
552
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800553 iview->extent = (VkExtent3D) {
Nanley Chery3f01bbe2016-01-04 12:41:22 -0800554 .width = anv_minify(iview->level_0_extent.width , range->baseMipLevel),
555 .height = anv_minify(iview->level_0_extent.height, range->baseMipLevel),
556 .depth = anv_minify(iview->level_0_extent.depth , range->baseMipLevel),
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800557 };
558
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800559 if (image->needs_nonrt_surface_state) {
560 iview->nonrt_surface_state = alloc_surface_state(device, cmd_buffer);
561
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800562 anv_fill_image_surface_state(device, iview->nonrt_surface_state,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800563 iview, pCreateInfo,
564 VK_IMAGE_USAGE_SAMPLED_BIT);
565 } else {
566 iview->nonrt_surface_state.alloc_size = 0;
567 }
568
569 if (image->needs_color_rt_surface_state) {
570 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
571
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800572 anv_fill_image_surface_state(device, iview->color_rt_surface_state,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800573 iview, pCreateInfo,
574 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
575 } else {
576 iview->color_rt_surface_state.alloc_size = 0;
577 }
578
579 if (image->needs_storage_surface_state) {
580 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
581
Francisco Jereza50dc702016-01-26 12:23:08 -0800582 if (has_matching_storage_typed_format(device, iview->format))
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800583 anv_fill_image_surface_state(device, iview->storage_surface_state,
Francisco Jereza50dc702016-01-26 12:23:08 -0800584 iview, pCreateInfo,
585 VK_IMAGE_USAGE_STORAGE_BIT);
586 else
Francisco Jerez6840cc12016-01-26 14:50:52 -0800587 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
Francisco Jereza50dc702016-01-26 12:23:08 -0800588 ISL_FORMAT_RAW,
589 iview->offset,
590 iview->bo->size - iview->offset, 1);
591
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800592 } else {
593 iview->storage_surface_state.alloc_size = 0;
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700594 }
595}
596
Chad Versace5b04db72015-07-06 16:24:28 -0700597VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700598anv_CreateImageView(VkDevice _device,
599 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800600 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700601 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700602{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700603 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700604 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700605
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800606 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
607 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700608 if (view == NULL)
609 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
610
611 anv_image_view_init(view, device, pCreateInfo, NULL);
612
613 *pView = anv_image_view_to_handle(view);
614
615 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700616}
617
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800618void
619anv_DestroyImageView(VkDevice _device, VkImageView _iview,
620 const VkAllocationCallbacks *pAllocator)
Chad Versace24de3d42015-10-06 19:11:58 -0700621{
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800622 ANV_FROM_HANDLE(anv_device, device, _device);
623 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
624
Chad Versace24de3d42015-10-06 19:11:58 -0700625 if (iview->image->needs_color_rt_surface_state) {
626 anv_state_pool_free(&device->surface_state_pool,
627 iview->color_rt_surface_state);
628 }
629
630 if (iview->image->needs_nonrt_surface_state) {
631 anv_state_pool_free(&device->surface_state_pool,
632 iview->nonrt_surface_state);
633 }
634
Jason Ekstrandff05f632015-12-07 17:17:30 -0800635 if (iview->image->needs_storage_surface_state) {
636 anv_state_pool_free(&device->surface_state_pool,
637 iview->storage_surface_state);
638 }
639
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800640 anv_free2(&device->alloc, pAllocator, iview);
Jason Ekstrand84783502015-07-10 20:18:52 -0700641}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700642
Jason Ekstrandc5618602015-12-12 16:11:23 -0800643VkResult
644anv_CreateBufferView(VkDevice _device,
645 const VkBufferViewCreateInfo *pCreateInfo,
646 const VkAllocationCallbacks *pAllocator,
647 VkBufferView *pView)
648{
649 ANV_FROM_HANDLE(anv_device, device, _device);
650 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
651 struct anv_buffer_view *view;
652
Jason Ekstrandc5618602015-12-12 16:11:23 -0800653 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
654 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
655 if (!view)
656 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
657
Jason Ekstrandc5618602015-12-12 16:11:23 -0800658 const struct anv_format *format =
659 anv_format_for_vk_format(pCreateInfo->format);
660
Jason Ekstrand783a2112015-12-14 16:51:12 -0800661 view->format = format->surface_format;
662 view->bo = buffer->bo;
663 view->offset = buffer->offset + pCreateInfo->offset;
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800664 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
665 buffer->size - view->offset : pCreateInfo->range;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800666
667 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
668 view->surface_state =
669 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
670
Francisco Jerez6840cc12016-01-26 14:50:52 -0800671 anv_fill_buffer_surface_state(device, view->surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800672 view->format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800673 view->offset, view->range,
Chad Versace89b68dc2016-01-05 09:59:07 -0800674 format->isl_layout->bs);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800675 } else {
676 view->surface_state = (struct anv_state){ 0 };
677 }
678
679 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
680 view->storage_surface_state =
681 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
682
683 enum isl_format storage_format =
Francisco Jereza50dc702016-01-26 12:23:08 -0800684 has_matching_storage_typed_format(device, view->format) ?
685 isl_lower_storage_image_format(&device->isl_dev, view->format) :
686 ISL_FORMAT_RAW;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800687
Francisco Jerez6840cc12016-01-26 14:50:52 -0800688 anv_fill_buffer_surface_state(device, view->storage_surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800689 storage_format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800690 view->offset, view->range,
Francisco Jereza50dc702016-01-26 12:23:08 -0800691 (storage_format == ISL_FORMAT_RAW ? 1 :
692 format->isl_layout->bs));
693
Jason Ekstrand783a2112015-12-14 16:51:12 -0800694 } else {
695 view->storage_surface_state = (struct anv_state){ 0 };
696 }
Jason Ekstrandc5618602015-12-12 16:11:23 -0800697
698 *pView = anv_buffer_view_to_handle(view);
699
700 return VK_SUCCESS;
701}
702
703void
704anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
705 const VkAllocationCallbacks *pAllocator)
706{
707 ANV_FROM_HANDLE(anv_device, device, _device);
708 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
709
Jason Ekstrand783a2112015-12-14 16:51:12 -0800710 if (view->surface_state.alloc_size > 0)
711 anv_state_pool_free(&device->surface_state_pool,
712 view->surface_state);
713
714 if (view->storage_surface_state.alloc_size > 0)
715 anv_state_pool_free(&device->surface_state_pool,
716 view->storage_surface_state);
717
Jason Ekstrandc5618602015-12-12 16:11:23 -0800718 anv_free2(&device->alloc, pAllocator, view);
719}
720
Chad Versace941b48e2015-08-28 07:08:58 -0700721struct anv_surface *
Chad Versace7a089bd2015-10-05 06:48:14 -0700722anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
Chad Versace941b48e2015-08-28 07:08:58 -0700723{
Chad Versace7a089bd2015-10-05 06:48:14 -0700724 switch (aspect_mask) {
725 case VK_IMAGE_ASPECT_COLOR_BIT:
Chad Versace24de3d42015-10-06 19:11:58 -0700726 /* Dragons will eat you.
727 *
728 * Meta attaches all destination surfaces as color render targets. Guess
729 * what surface the Meta Dragons really want.
730 */
731 if (image->format->depth_format && image->format->has_stencil) {
Chad Versace24de3d42015-10-06 19:11:58 -0700732 return &image->depth_surface;
733 } else if (image->format->depth_format) {
734 return &image->depth_surface;
735 } else if (image->format->has_stencil) {
736 return &image->stencil_surface;
737 } else {
738 return &image->color_surface;
739 }
740 break;
Chad Versace7a089bd2015-10-05 06:48:14 -0700741 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700742 assert(image->format->depth_format);
743 return &image->depth_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700744 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700745 assert(image->format->has_stencil);
Chad Versace941b48e2015-08-28 07:08:58 -0700746 return &image->stencil_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700747 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace03dd7222015-10-07 09:03:47 -0700748 if (image->format->depth_format && image->format->has_stencil) {
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800749 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
750 * combined depth stencil formats. Specifically, it states:
Chad Versace03dd7222015-10-07 09:03:47 -0700751 *
752 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
753 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800754 *
755 * Image views with both depth and stencil aspects are only valid for
756 * render target attachments, in which case
757 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
758 * stencil surfaces from the underlying surface.
Chad Versace03dd7222015-10-07 09:03:47 -0700759 */
Chad Versace03dd7222015-10-07 09:03:47 -0700760 return &image->depth_surface;
761 } else if (image->format->depth_format) {
762 return &image->depth_surface;
763 } else if (image->format->has_stencil) {
764 return &image->stencil_surface;
765 }
766 /* fallthrough */
Chad Versace941b48e2015-08-28 07:08:58 -0700767 default:
768 unreachable("image does not have aspect");
769 return NULL;
770 }
771}
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800772
Jason Ekstrandba393c92016-01-05 13:55:00 -0800773static void
774image_param_defaults(struct brw_image_param *param)
775{
776 memset(param, 0, sizeof *param);
777 /* Set the swizzling shifts to all-ones to effectively disable swizzling --
778 * See emit_address_calculation() in brw_fs_surface_builder.cpp for a more
779 * detailed explanation of these parameters.
780 */
781 param->swizzling[0] = 0xff;
782 param->swizzling[1] = 0xff;
783}
784
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800785void
786anv_image_view_fill_image_param(struct anv_device *device,
787 struct anv_image_view *view,
788 struct brw_image_param *param)
789{
Jason Ekstrandba393c92016-01-05 13:55:00 -0800790 image_param_defaults(param);
791
792 const struct isl_surf *surf = &view->image->color_surface.isl;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800793 const int cpp = isl_format_get_layout(surf->format)->bs;
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800794 const struct isl_extent3d image_align_sa =
795 isl_surf_get_image_alignment_sa(surf);
Jason Ekstrandba393c92016-01-05 13:55:00 -0800796
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800797 param->size[0] = view->extent.width;
798 param->size[1] = view->extent.height;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800799 if (surf->dim == ISL_SURF_DIM_3D) {
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800800 param->size[2] = view->extent.depth;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800801 } else {
802 param->size[2] = surf->logical_level0_px.array_len - view->base_layer;
803 }
804
Nanley Chery308ec022016-01-27 09:29:09 -0800805 isl_surf_get_image_offset_el(surf, view->base_mip, view->base_layer, 0,
Jason Ekstrandba393c92016-01-05 13:55:00 -0800806 &param->offset[0], &param->offset[1]);
807
808 param->stride[0] = cpp;
809 param->stride[1] = surf->row_pitch / cpp;
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800810
811 if (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D) {
812 param->stride[2] = util_align_npot(param->size[0], image_align_sa.w);
813 param->stride[3] = util_align_npot(param->size[1], image_align_sa.h);
814 } else {
815 param->stride[2] = 0;
816 param->stride[3] = isl_surf_get_array_pitch_el_rows(surf);
817 }
Jason Ekstrandba393c92016-01-05 13:55:00 -0800818
819 switch (surf->tiling) {
820 case ISL_TILING_LINEAR:
821 /* image_param_defaults is good enough */
822 break;
823
824 case ISL_TILING_X:
825 /* An X tile is a rectangular block of 512x8 bytes. */
826 param->tiling[0] = util_logbase2(512 / cpp);
827 param->tiling[1] = util_logbase2(8);
828
829 if (device->isl_dev.has_bit6_swizzling) {
830 /* Right shifts required to swizzle bits 9 and 10 of the memory
831 * address with bit 6.
832 */
833 param->swizzling[0] = 3;
834 param->swizzling[1] = 4;
835 }
836 break;
837
838 case ISL_TILING_Y0:
839 /* The layout of a Y-tiled surface in memory isn't really fundamentally
840 * different to the layout of an X-tiled surface, we simply pretend that
841 * the surface is broken up in a number of smaller 16Bx32 tiles, each
842 * one arranged in X-major order just like is the case for X-tiling.
843 */
844 param->tiling[0] = util_logbase2(16 / cpp);
845 param->tiling[1] = util_logbase2(32);
846
847 if (device->isl_dev.has_bit6_swizzling) {
848 /* Right shift required to swizzle bit 9 of the memory address with
849 * bit 6.
850 */
851 param->swizzling[0] = 3;
852 param->swizzling[1] = 0xff;
853 }
854 break;
855
856 default:
857 assert(!"Unhandled storage image tiling");
858 }
859
860 /* 3D textures are arranged in 2D in memory with 2^lod slices per row. The
861 * address calculation algorithm (emit_address_calculation() in
862 * brw_fs_surface_builder.cpp) handles this as a sort of tiling with
863 * modulus equal to the LOD.
864 */
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800865 param->tiling[2] = (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D ?
866 view->base_mip : 0);
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800867}
Jason Ekstrand783a2112015-12-14 16:51:12 -0800868
869void
870anv_buffer_view_fill_image_param(struct anv_device *device,
871 struct anv_buffer_view *view,
872 struct brw_image_param *param)
873{
Jason Ekstrandba393c92016-01-05 13:55:00 -0800874 image_param_defaults(param);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800875
Chad Versace89b68dc2016-01-05 09:59:07 -0800876 param->stride[0] = isl_format_layouts[view->format].bs;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800877 param->size[0] = view->range / param->stride[0];
878}