Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Versace | 2c2233e | 2015-07-17 15:04:27 -0700 | [diff] [blame] | 30 | #include "anv_private.h" |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 31 | |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 32 | /** |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 33 | * Exactly one bit must be set in \a aspect. |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 34 | */ |
| 35 | static isl_surf_usage_flags_t |
Chad Versace | 2f4bb00 | 2016-02-09 12:35:39 -0800 | [diff] [blame] | 36 | choose_isl_surf_usage(VkImageUsageFlags vk_usage, |
Chad Versace | 2f270f0 | 2015-12-04 16:29:25 -0800 | [diff] [blame] | 37 | VkImageAspectFlags aspect) |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 38 | { |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 39 | isl_surf_usage_flags_t isl_usage = 0; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 40 | |
| 41 | /* FINISHME: Support aux surfaces */ |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 42 | isl_usage |= ISL_SURF_USAGE_DISABLE_AUX_BIT; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 43 | |
Chad Versace | 2f4bb00 | 2016-02-09 12:35:39 -0800 | [diff] [blame] | 44 | if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT) |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 45 | isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 46 | |
Chad Versace | 2f4bb00 | 2016-02-09 12:35:39 -0800 | [diff] [blame] | 47 | if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 48 | isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 49 | |
Chad Versace | 2f4bb00 | 2016-02-09 12:35:39 -0800 | [diff] [blame] | 50 | if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 51 | isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 52 | |
Chad Versace | 2f4bb00 | 2016-02-09 12:35:39 -0800 | [diff] [blame] | 53 | if (vk_usage & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 54 | isl_usage |= ISL_SURF_USAGE_CUBE_BIT; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 55 | |
Chad Versace | 2f4bb00 | 2016-02-09 12:35:39 -0800 | [diff] [blame] | 56 | if (vk_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) { |
Chad Versace | 2f270f0 | 2015-12-04 16:29:25 -0800 | [diff] [blame] | 57 | switch (aspect) { |
| 58 | default: |
| 59 | unreachable("bad VkImageAspect"); |
| 60 | case VK_IMAGE_ASPECT_DEPTH_BIT: |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 61 | isl_usage |= ISL_SURF_USAGE_DEPTH_BIT; |
Chad Versace | 2f270f0 | 2015-12-04 16:29:25 -0800 | [diff] [blame] | 62 | break; |
| 63 | case VK_IMAGE_ASPECT_STENCIL_BIT: |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 64 | isl_usage |= ISL_SURF_USAGE_STENCIL_BIT; |
Chad Versace | 2f270f0 | 2015-12-04 16:29:25 -0800 | [diff] [blame] | 65 | break; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Chad Versace | 2f4bb00 | 2016-02-09 12:35:39 -0800 | [diff] [blame] | 69 | if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) { |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 70 | /* Meta implements transfers by sampling from the source image. */ |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 71 | isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Chad Versace | 2f4bb00 | 2016-02-09 12:35:39 -0800 | [diff] [blame] | 74 | if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) { |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 75 | /* Meta implements transfers by rendering into the destination image. */ |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 76 | isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Chad Versace | c5e521f | 2016-02-09 12:36:54 -0800 | [diff] [blame] | 79 | return isl_usage; |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 80 | } |
Chad Versace | 5a6b2e6 | 2015-08-17 13:50:43 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
Chad Versace | 9098e0f | 2015-12-07 09:22:49 -0800 | [diff] [blame] | 83 | * Exactly one bit must be set in \a aspect. |
| 84 | */ |
| 85 | static struct anv_surface * |
| 86 | get_surface(struct anv_image *image, VkImageAspectFlags aspect) |
| 87 | { |
| 88 | switch (aspect) { |
| 89 | default: |
| 90 | unreachable("bad VkImageAspect"); |
| 91 | case VK_IMAGE_ASPECT_COLOR_BIT: |
| 92 | return &image->color_surface; |
| 93 | case VK_IMAGE_ASPECT_DEPTH_BIT: |
| 94 | return &image->depth_surface; |
| 95 | case VK_IMAGE_ASPECT_STENCIL_BIT: |
| 96 | return &image->stencil_surface; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Initialize the anv_image::*_surface selected by \a aspect. Then update the |
| 102 | * image's memory requirements (that is, the image's size and alignment). |
| 103 | * |
| 104 | * Exactly one bit must be set in \a aspect. |
Chad Versace | 5a6b2e6 | 2015-08-17 13:50:43 -0700 | [diff] [blame] | 105 | */ |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 106 | static VkResult |
Chad Versace | 9098e0f | 2015-12-07 09:22:49 -0800 | [diff] [blame] | 107 | make_surface(const struct anv_device *dev, |
| 108 | struct anv_image *image, |
| 109 | const struct anv_image_create_info *anv_info, |
| 110 | VkImageAspectFlags aspect) |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 111 | { |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 112 | const VkImageCreateInfo *vk_info = anv_info->vk_info; |
Chad Versace | 3d85a28 | 2015-12-07 08:53:43 -0800 | [diff] [blame] | 113 | bool ok UNUSED; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 114 | |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 115 | static const enum isl_surf_dim vk_to_isl_surf_dim[] = { |
| 116 | [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D, |
| 117 | [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D, |
| 118 | [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D, |
| 119 | }; |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 120 | |
Chad Versace | 64e8af6 | 2015-12-07 08:50:28 -0800 | [diff] [blame] | 121 | isl_tiling_flags_t tiling_flags = anv_info->isl_tiling_flags; |
| 122 | if (vk_info->tiling == VK_IMAGE_TILING_LINEAR) |
| 123 | tiling_flags &= ISL_TILING_LINEAR_BIT; |
| 124 | |
Chad Versace | 9098e0f | 2015-12-07 09:22:49 -0800 | [diff] [blame] | 125 | struct anv_surface *anv_surf = get_surface(image, aspect); |
| 126 | |
Kristian Høgsberg Kristensen | 3b9b908 | 2016-02-17 12:21:46 -0800 | [diff] [blame] | 127 | VkExtent3D extent; |
| 128 | switch (vk_info->imageType) { |
| 129 | case VK_IMAGE_TYPE_1D: |
| 130 | extent = (VkExtent3D) { vk_info->extent.width, 1, 1 }; |
| 131 | break; |
| 132 | case VK_IMAGE_TYPE_2D: |
| 133 | extent = (VkExtent3D) { vk_info->extent.width, vk_info->extent.height, 1 }; |
| 134 | break; |
| 135 | case VK_IMAGE_TYPE_3D: |
| 136 | extent = vk_info->extent; |
| 137 | break; |
| 138 | default: |
| 139 | unreachable("invalid image type"); |
| 140 | } |
| 141 | |
Nanley Chery | 9963af8 | 2016-02-17 17:20:03 -0800 | [diff] [blame] | 142 | image->extent = extent; |
| 143 | |
Chad Versace | 9098e0f | 2015-12-07 09:22:49 -0800 | [diff] [blame] | 144 | ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl, |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 145 | .dim = vk_to_isl_surf_dim[vk_info->imageType], |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 146 | .format = anv_get_isl_format(vk_info->format, aspect, |
| 147 | vk_info->tiling, NULL), |
Kristian Høgsberg Kristensen | 3b9b908 | 2016-02-17 12:21:46 -0800 | [diff] [blame] | 148 | .width = extent.width, |
| 149 | .height = extent.height, |
| 150 | .depth = extent.depth, |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 151 | .levels = vk_info->mipLevels, |
| 152 | .array_len = vk_info->arrayLayers, |
| 153 | .samples = vk_info->samples, |
| 154 | .min_alignment = 0, |
| 155 | .min_pitch = 0, |
Chad Versace | 2f4bb00 | 2016-02-09 12:35:39 -0800 | [diff] [blame] | 156 | .usage = choose_isl_surf_usage(image->usage, aspect), |
Chad Versace | 64e8af6 | 2015-12-07 08:50:28 -0800 | [diff] [blame] | 157 | .tiling_flags = tiling_flags); |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 158 | |
Chad Versace | 3d85a28 | 2015-12-07 08:53:43 -0800 | [diff] [blame] | 159 | /* isl_surf_init() will fail only if provided invalid input. Invalid input |
| 160 | * is illegal in Vulkan. |
| 161 | */ |
| 162 | assert(ok); |
| 163 | |
Chad Versace | 9098e0f | 2015-12-07 09:22:49 -0800 | [diff] [blame] | 164 | anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment); |
| 165 | image->size = anv_surf->offset + anv_surf->isl.size; |
| 166 | image->alignment = MAX(image->alignment, anv_surf->isl.alignment); |
Chad Versace | b369389 | 2015-12-02 16:46:16 -0800 | [diff] [blame] | 167 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 168 | return VK_SUCCESS; |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Chad Versace | 4c5dccc | 2016-02-09 12:41:08 -0800 | [diff] [blame] | 171 | /** |
| 172 | * Parameter @a format is required and overrides VkImageCreateInfo::format. |
| 173 | */ |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 174 | static VkImageUsageFlags |
Chad Versace | 4c5dccc | 2016-02-09 12:41:08 -0800 | [diff] [blame] | 175 | anv_image_get_full_usage(const VkImageCreateInfo *info, |
| 176 | const struct anv_format *format) |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 177 | { |
| 178 | VkImageUsageFlags usage = info->usage; |
| 179 | |
Chad Versace | 2bab3cd | 2016-01-28 06:28:01 -0800 | [diff] [blame] | 180 | if (info->samples > 1 && |
| 181 | (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) { |
| 182 | /* Meta will resolve the image by binding it as a texture. */ |
| 183 | usage |= VK_IMAGE_USAGE_SAMPLED_BIT; |
| 184 | } |
| 185 | |
Jason Ekstrand | 6a8a542 | 2015-11-30 11:12:44 -0800 | [diff] [blame] | 186 | if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) { |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 187 | /* Meta will transfer from the image by binding it as a texture. */ |
| 188 | usage |= VK_IMAGE_USAGE_SAMPLED_BIT; |
| 189 | } |
| 190 | |
Jason Ekstrand | 6a8a542 | 2015-11-30 11:12:44 -0800 | [diff] [blame] | 191 | if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) { |
Chad Versace | 4c5dccc | 2016-02-09 12:41:08 -0800 | [diff] [blame] | 192 | /* For non-clear transfer operations, meta will transfer to the image by |
| 193 | * binding it as a color attachment, even if the image format is not |
| 194 | * a color format. |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 195 | */ |
| 196 | usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
Chad Versace | 4c5dccc | 2016-02-09 12:41:08 -0800 | [diff] [blame] | 197 | |
| 198 | if (anv_format_is_depth_or_stencil(format)) { |
| 199 | /* vkCmdClearDepthStencilImage() only requires that |
| 200 | * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does |
| 201 | * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta |
| 202 | * clears the image, though, by binding it as a depthstencil |
| 203 | * attachment. |
| 204 | */ |
| 205 | usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 206 | } |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | return usage; |
| 210 | } |
| 211 | |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 212 | VkResult |
| 213 | anv_image_create(VkDevice _device, |
| 214 | const struct anv_image_create_info *create_info, |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 215 | const VkAllocationCallbacks* alloc, |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 216 | VkImage *pImage) |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 217 | { |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 218 | ANV_FROM_HANDLE(anv_device, device, _device); |
Chad Versace | fdcd71f | 2015-06-26 20:06:08 -0700 | [diff] [blame] | 219 | const VkImageCreateInfo *pCreateInfo = create_info->vk_info; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 220 | struct anv_image *image = NULL; |
Chad Versace | 4c5dccc | 2016-02-09 12:41:08 -0800 | [diff] [blame] | 221 | const struct anv_format *format = anv_format_for_vk_format(pCreateInfo->format); |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 222 | VkResult r; |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 223 | |
| 224 | assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO); |
| 225 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 226 | anv_assert(pCreateInfo->mipLevels > 0); |
Jason Ekstrand | 299f8f1 | 2015-12-01 12:52:56 -0800 | [diff] [blame] | 227 | anv_assert(pCreateInfo->arrayLayers > 0); |
Chad Versace | d96d78c | 2016-01-22 17:16:20 -0800 | [diff] [blame] | 228 | anv_assert(pCreateInfo->samples > 0); |
Chad Versace | 5d7103e | 2015-06-26 09:05:46 -0700 | [diff] [blame] | 229 | anv_assert(pCreateInfo->extent.width > 0); |
| 230 | anv_assert(pCreateInfo->extent.height > 0); |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 231 | anv_assert(pCreateInfo->extent.depth > 0); |
Jason Ekstrand | 2a3c296 | 2015-06-10 21:04:51 -0700 | [diff] [blame] | 232 | |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 233 | image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8, |
| 234 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 235 | if (!image) |
| 236 | return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY); |
Chad Versace | 67a7659 | 2015-06-26 09:17:52 -0700 | [diff] [blame] | 237 | |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 238 | memset(image, 0, sizeof(*image)); |
| 239 | image->type = pCreateInfo->imageType; |
| 240 | image->extent = pCreateInfo->extent; |
Jason Ekstrand | 3200a81 | 2015-12-31 12:39:34 -0800 | [diff] [blame] | 241 | image->vk_format = pCreateInfo->format; |
Chad Versace | 4c5dccc | 2016-02-09 12:41:08 -0800 | [diff] [blame] | 242 | image->format = format; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 243 | image->levels = pCreateInfo->mipLevels; |
Jason Ekstrand | 299f8f1 | 2015-12-01 12:52:56 -0800 | [diff] [blame] | 244 | image->array_size = pCreateInfo->arrayLayers; |
Chad Versace | dfcb4ee | 2016-01-20 16:34:23 -0800 | [diff] [blame] | 245 | image->samples = pCreateInfo->samples; |
Chad Versace | 4c5dccc | 2016-02-09 12:41:08 -0800 | [diff] [blame] | 246 | image->usage = anv_image_get_full_usage(pCreateInfo, format); |
Jason Ekstrand | f665fdf | 2016-01-01 14:09:17 -0800 | [diff] [blame] | 247 | image->tiling = pCreateInfo->tiling; |
Chad Versace | 67a7659 | 2015-06-26 09:17:52 -0700 | [diff] [blame] | 248 | |
Chad Versace | 4c5dccc | 2016-02-09 12:41:08 -0800 | [diff] [blame] | 249 | if (likely(anv_format_is_color(format))) { |
Chad Versace | 9098e0f | 2015-12-07 09:22:49 -0800 | [diff] [blame] | 250 | r = make_surface(device, image, create_info, |
| 251 | VK_IMAGE_ASPECT_COLOR_BIT); |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 252 | if (r != VK_SUCCESS) |
| 253 | goto fail; |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 254 | } else { |
Chad Versace | e6d3432 | 2016-02-08 19:07:10 -0800 | [diff] [blame] | 255 | if (image->format->has_depth) { |
Chad Versace | 9098e0f | 2015-12-07 09:22:49 -0800 | [diff] [blame] | 256 | r = make_surface(device, image, create_info, |
| 257 | VK_IMAGE_ASPECT_DEPTH_BIT); |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 258 | if (r != VK_SUCCESS) |
| 259 | goto fail; |
| 260 | } |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 261 | |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 262 | if (image->format->has_stencil) { |
Chad Versace | 9098e0f | 2015-12-07 09:22:49 -0800 | [diff] [blame] | 263 | r = make_surface(device, image, create_info, |
| 264 | VK_IMAGE_ASPECT_STENCIL_BIT); |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 265 | if (r != VK_SUCCESS) |
| 266 | goto fail; |
| 267 | } |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 268 | } |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 269 | |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 270 | *pImage = anv_image_to_handle(image); |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 271 | |
| 272 | return VK_SUCCESS; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 273 | |
| 274 | fail: |
| 275 | if (image) |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 276 | anv_free2(&device->alloc, alloc, image); |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 277 | |
| 278 | return r; |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 281 | VkResult |
| 282 | anv_CreateImage(VkDevice device, |
| 283 | const VkImageCreateInfo *pCreateInfo, |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 284 | const VkAllocationCallbacks *pAllocator, |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 285 | VkImage *pImage) |
Kristian Høgsberg | a29df71 | 2015-05-15 22:04:52 -0700 | [diff] [blame] | 286 | { |
Chad Versace | fdcd71f | 2015-06-26 20:06:08 -0700 | [diff] [blame] | 287 | return anv_image_create(device, |
| 288 | &(struct anv_image_create_info) { |
| 289 | .vk_info = pCreateInfo, |
Chad Versace | 64e8af6 | 2015-12-07 08:50:28 -0800 | [diff] [blame] | 290 | .isl_tiling_flags = ISL_TILING_ANY_MASK, |
Chad Versace | fdcd71f | 2015-06-26 20:06:08 -0700 | [diff] [blame] | 291 | }, |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 292 | pAllocator, |
Chad Versace | fdcd71f | 2015-06-26 20:06:08 -0700 | [diff] [blame] | 293 | pImage); |
Kristian Høgsberg | a29df71 | 2015-05-15 22:04:52 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Jason Ekstrand | 05a26a6 | 2015-10-05 20:50:51 -0700 | [diff] [blame] | 296 | void |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 297 | anv_DestroyImage(VkDevice _device, VkImage _image, |
| 298 | const VkAllocationCallbacks *pAllocator) |
Jason Ekstrand | 8b342b3 | 2015-07-10 12:30:58 -0700 | [diff] [blame] | 299 | { |
| 300 | ANV_FROM_HANDLE(anv_device, device, _device); |
| 301 | |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 302 | anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image)); |
Jason Ekstrand | 8b342b3 | 2015-07-10 12:30:58 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Jason Ekstrand | db5a5fc | 2015-10-13 15:50:02 -0700 | [diff] [blame] | 305 | static void |
| 306 | anv_surface_get_subresource_layout(struct anv_image *image, |
| 307 | struct anv_surface *surface, |
| 308 | const VkImageSubresource *subresource, |
| 309 | VkSubresourceLayout *layout) |
| 310 | { |
| 311 | /* If we are on a non-zero mip level or array slice, we need to |
| 312 | * calculate a real offset. |
| 313 | */ |
| 314 | anv_assert(subresource->mipLevel == 0); |
| 315 | anv_assert(subresource->arrayLayer == 0); |
| 316 | |
| 317 | layout->offset = surface->offset; |
Chad Versace | 981ef2f | 2015-12-03 08:40:47 -0800 | [diff] [blame] | 318 | layout->rowPitch = surface->isl.row_pitch; |
| 319 | layout->depthPitch = isl_surf_get_array_pitch(&surface->isl); |
Jason Ekstrand | 8a81d13 | 2016-01-06 19:27:10 -0800 | [diff] [blame] | 320 | layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl); |
Chad Versace | 981ef2f | 2015-12-03 08:40:47 -0800 | [diff] [blame] | 321 | layout->size = surface->isl.size; |
Jason Ekstrand | db5a5fc | 2015-10-13 15:50:02 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Jason Ekstrand | f1a7c78 | 2015-11-30 12:21:19 -0800 | [diff] [blame] | 324 | void anv_GetImageSubresourceLayout( |
Jason Ekstrand | db24afe | 2015-07-07 18:20:18 -0700 | [diff] [blame] | 325 | VkDevice device, |
Jason Ekstrand | db5a5fc | 2015-10-13 15:50:02 -0700 | [diff] [blame] | 326 | VkImage _image, |
Jason Ekstrand | db24afe | 2015-07-07 18:20:18 -0700 | [diff] [blame] | 327 | const VkImageSubresource* pSubresource, |
| 328 | VkSubresourceLayout* pLayout) |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 329 | { |
Jason Ekstrand | db5a5fc | 2015-10-13 15:50:02 -0700 | [diff] [blame] | 330 | ANV_FROM_HANDLE(anv_image, image, _image); |
| 331 | |
Jason Ekstrand | 407b8cc | 2015-12-01 12:19:11 -0800 | [diff] [blame] | 332 | assert(__builtin_popcount(pSubresource->aspectMask) == 1); |
| 333 | |
| 334 | switch (pSubresource->aspectMask) { |
| 335 | case VK_IMAGE_ASPECT_COLOR_BIT: |
Jason Ekstrand | db5a5fc | 2015-10-13 15:50:02 -0700 | [diff] [blame] | 336 | anv_surface_get_subresource_layout(image, &image->color_surface, |
| 337 | pSubresource, pLayout); |
| 338 | break; |
Jason Ekstrand | 407b8cc | 2015-12-01 12:19:11 -0800 | [diff] [blame] | 339 | case VK_IMAGE_ASPECT_DEPTH_BIT: |
Jason Ekstrand | db5a5fc | 2015-10-13 15:50:02 -0700 | [diff] [blame] | 340 | anv_surface_get_subresource_layout(image, &image->depth_surface, |
| 341 | pSubresource, pLayout); |
| 342 | break; |
Jason Ekstrand | 407b8cc | 2015-12-01 12:19:11 -0800 | [diff] [blame] | 343 | case VK_IMAGE_ASPECT_STENCIL_BIT: |
Jason Ekstrand | db5a5fc | 2015-10-13 15:50:02 -0700 | [diff] [blame] | 344 | anv_surface_get_subresource_layout(image, &image->stencil_surface, |
| 345 | pSubresource, pLayout); |
| 346 | break; |
| 347 | default: |
Jason Ekstrand | f1a7c78 | 2015-11-30 12:21:19 -0800 | [diff] [blame] | 348 | assert(!"Invalid image aspect"); |
Jason Ekstrand | db5a5fc | 2015-10-13 15:50:02 -0700 | [diff] [blame] | 349 | } |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 352 | VkResult |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 353 | anv_validate_CreateImageView(VkDevice _device, |
| 354 | const VkImageViewCreateInfo *pCreateInfo, |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 355 | const VkAllocationCallbacks *pAllocator, |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 356 | VkImageView *pView) |
| 357 | { |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 358 | ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 359 | const VkImageSubresourceRange *subresource; |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 360 | const struct anv_format *view_format_info; |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 361 | |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 362 | /* Validate structure type before dereferencing it. */ |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 363 | assert(pCreateInfo); |
| 364 | assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 365 | subresource = &pCreateInfo->subresourceRange; |
| 366 | |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 367 | /* Validate viewType is in range before using it. */ |
| 368 | assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE); |
| 369 | assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 370 | |
| 371 | /* Validate format is in range before using it. */ |
| 372 | assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE); |
| 373 | assert(pCreateInfo->format <= VK_FORMAT_END_RANGE); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 374 | view_format_info = anv_format_for_vk_format(pCreateInfo->format); |
| 375 | |
| 376 | /* Validate channel swizzles. */ |
Jason Ekstrand | a53f23d | 2015-11-30 13:06:12 -0800 | [diff] [blame] | 377 | assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE); |
| 378 | assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE); |
| 379 | assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE); |
| 380 | assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE); |
| 381 | assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE); |
| 382 | assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE); |
| 383 | assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE); |
| 384 | assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 385 | |
| 386 | /* Validate subresource. */ |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 387 | assert(subresource->aspectMask != 0); |
Jason Ekstrand | 299f8f1 | 2015-12-01 12:52:56 -0800 | [diff] [blame] | 388 | assert(subresource->levelCount > 0); |
| 389 | assert(subresource->layerCount > 0); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 390 | assert(subresource->baseMipLevel < image->levels); |
Jason Ekstrand | 299f8f1 | 2015-12-01 12:52:56 -0800 | [diff] [blame] | 391 | assert(subresource->baseMipLevel + subresource->levelCount <= image->levels); |
Jason Ekstrand | 1e4263b | 2015-10-06 10:27:50 -0700 | [diff] [blame] | 392 | assert(subresource->baseArrayLayer < image->array_size); |
Jason Ekstrand | 299f8f1 | 2015-12-01 12:52:56 -0800 | [diff] [blame] | 393 | assert(subresource->baseArrayLayer + subresource->layerCount <= image->array_size); |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 394 | assert(pView); |
| 395 | |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 396 | const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT |
| 397 | | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 398 | |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 399 | /* Validate format. */ |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 400 | if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) { |
| 401 | assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT); |
Chad Versace | e6d3432 | 2016-02-08 19:07:10 -0800 | [diff] [blame] | 402 | assert(!image->format->has_depth); |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 403 | assert(!image->format->has_stencil); |
Chad Versace | e6d3432 | 2016-02-08 19:07:10 -0800 | [diff] [blame] | 404 | assert(!view_format_info->has_depth); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 405 | assert(!view_format_info->has_stencil); |
Chad Versace | addc2a9 | 2015-11-12 12:14:43 -0800 | [diff] [blame] | 406 | assert(view_format_info->isl_layout->bs == |
| 407 | image->format->isl_layout->bs); |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 408 | } else if (subresource->aspectMask & ds_flags) { |
| 409 | assert((subresource->aspectMask & ~ds_flags) == 0); |
| 410 | |
| 411 | if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) { |
Chad Versace | e6d3432 | 2016-02-08 19:07:10 -0800 | [diff] [blame] | 412 | assert(image->format->has_depth); |
| 413 | assert(view_format_info->has_depth); |
Chad Versace | addc2a9 | 2015-11-12 12:14:43 -0800 | [diff] [blame] | 414 | assert(view_format_info->isl_layout->bs == |
| 415 | image->format->isl_layout->bs); |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Jason Ekstrand | 407b8cc | 2015-12-01 12:19:11 -0800 | [diff] [blame] | 418 | if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) { |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 419 | /* FINISHME: Is it legal to have an R8 view of S8? */ |
| 420 | assert(image->format->has_stencil); |
| 421 | assert(view_format_info->has_stencil); |
| 422 | } |
| 423 | } else { |
| 424 | assert(!"bad VkImageSubresourceRange::aspectFlags"); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 425 | } |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 426 | |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 427 | return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView); |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Jason Ekstrand | e5558ff | 2016-01-22 11:57:01 -0800 | [diff] [blame] | 430 | static struct anv_state |
| 431 | alloc_surface_state(struct anv_device *device, |
| 432 | struct anv_cmd_buffer *cmd_buffer) |
| 433 | { |
| 434 | if (cmd_buffer) { |
| 435 | return anv_cmd_buffer_alloc_surface_state(cmd_buffer); |
| 436 | } else { |
| 437 | return anv_state_pool_alloc(&device->surface_state_pool, 64, 64); |
| 438 | } |
| 439 | } |
| 440 | |
Francisco Jerez | a50dc70 | 2016-01-26 12:23:08 -0800 | [diff] [blame] | 441 | static bool |
| 442 | has_matching_storage_typed_format(const struct anv_device *device, |
| 443 | enum isl_format format) |
| 444 | { |
| 445 | return (isl_format_get_layout(format)->bs <= 4 || |
| 446 | (isl_format_get_layout(format)->bs <= 8 && |
| 447 | (device->info.gen >= 8 || device->info.is_haswell)) || |
| 448 | device->info.gen >= 9); |
| 449 | } |
| 450 | |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 451 | static enum isl_channel_select |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 452 | remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component, |
| 453 | struct anv_format_swizzle format_swizzle) |
Jason Ekstrand | 9bc72a9 | 2016-01-26 20:16:43 -0800 | [diff] [blame] | 454 | { |
| 455 | if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY) |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 456 | swizzle = component; |
| 457 | |
| 458 | switch (swizzle) { |
| 459 | case VK_COMPONENT_SWIZZLE_ZERO: |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 460 | return ISL_CHANNEL_SELECT_ZERO; |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 461 | case VK_COMPONENT_SWIZZLE_ONE: |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 462 | return ISL_CHANNEL_SELECT_ONE; |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 463 | case VK_COMPONENT_SWIZZLE_R: |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 464 | return ISL_CHANNEL_SELECT_RED + format_swizzle.r; |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 465 | case VK_COMPONENT_SWIZZLE_G: |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 466 | return ISL_CHANNEL_SELECT_RED + format_swizzle.g; |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 467 | case VK_COMPONENT_SWIZZLE_B: |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 468 | return ISL_CHANNEL_SELECT_RED + format_swizzle.b; |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 469 | case VK_COMPONENT_SWIZZLE_A: |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 470 | return ISL_CHANNEL_SELECT_RED + format_swizzle.a; |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 471 | default: |
| 472 | unreachable("Invalid swizzle"); |
| 473 | } |
Jason Ekstrand | 9bc72a9 | 2016-01-26 20:16:43 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Jason Ekstrand | e5558ff | 2016-01-22 11:57:01 -0800 | [diff] [blame] | 476 | void |
Kristian Høgsberg Kristensen | 988341a | 2015-08-19 21:36:57 -0700 | [diff] [blame] | 477 | anv_image_view_init(struct anv_image_view *iview, |
| 478 | struct anv_device *device, |
| 479 | const VkImageViewCreateInfo* pCreateInfo, |
Nanley Chery | 6a579de | 2016-01-26 18:53:21 -0800 | [diff] [blame] | 480 | struct anv_cmd_buffer *cmd_buffer, |
Jason Ekstrand | e9d126f | 2016-02-24 19:49:12 -0800 | [diff] [blame] | 481 | uint32_t offset, |
| 482 | VkImageUsageFlags usage_mask) |
Kristian Høgsberg Kristensen | 988341a | 2015-08-19 21:36:57 -0700 | [diff] [blame] | 483 | { |
Chad Versace | 44143a1 | 2015-10-06 18:17:09 -0700 | [diff] [blame] | 484 | ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image); |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 485 | const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange; |
Chad Versace | 44143a1 | 2015-10-06 18:17:09 -0700 | [diff] [blame] | 486 | |
Jason Ekstrand | 299f8f1 | 2015-12-01 12:52:56 -0800 | [diff] [blame] | 487 | assert(range->layerCount > 0); |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 488 | assert(range->baseMipLevel < image->levels); |
Chad Versace | 44143a1 | 2015-10-06 18:17:09 -0700 | [diff] [blame] | 489 | assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT | |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 490 | VK_IMAGE_USAGE_STORAGE_BIT | |
| 491 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
Chad Versace | 0ca3c84 | 2015-10-07 11:39:49 -0700 | [diff] [blame] | 492 | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)); |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 493 | |
| 494 | switch (image->type) { |
| 495 | default: |
| 496 | unreachable("bad VkImageType"); |
| 497 | case VK_IMAGE_TYPE_1D: |
| 498 | case VK_IMAGE_TYPE_2D: |
Jason Ekstrand | 299f8f1 | 2015-12-01 12:52:56 -0800 | [diff] [blame] | 499 | assert(range->baseArrayLayer + range->layerCount - 1 <= image->array_size); |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 500 | break; |
| 501 | case VK_IMAGE_TYPE_3D: |
Jason Ekstrand | 299f8f1 | 2015-12-01 12:52:56 -0800 | [diff] [blame] | 502 | assert(range->baseArrayLayer + range->layerCount - 1 |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 503 | <= anv_minify(image->extent.depth, range->baseMipLevel)); |
| 504 | break; |
| 505 | } |
Chad Versace | 44143a1 | 2015-10-06 18:17:09 -0700 | [diff] [blame] | 506 | |
Jason Ekstrand | a7cc129 | 2016-01-01 13:47:18 -0800 | [diff] [blame] | 507 | 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 Chery | 6a579de | 2016-01-26 18:53:21 -0800 | [diff] [blame] | 512 | iview->offset = image->offset + surface->offset + offset; |
Jason Ekstrand | a7cc129 | 2016-01-01 13:47:18 -0800 | [diff] [blame] | 513 | |
| 514 | iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask; |
Jason Ekstrand | f665fdf | 2016-01-01 14:09:17 -0800 | [diff] [blame] | 515 | iview->vk_format = pCreateInfo->format; |
Jordan Justen | c20f78d | 2016-01-26 11:10:56 -0800 | [diff] [blame] | 516 | |
| 517 | struct anv_format_swizzle swizzle; |
Jason Ekstrand | 9d5b8f7 | 2016-02-20 21:40:25 -0800 | [diff] [blame] | 518 | enum isl_format format = anv_get_isl_format(pCreateInfo->format, |
| 519 | range->aspectMask, |
| 520 | image->tiling, &swizzle); |
Jason Ekstrand | a7cc129 | 2016-01-01 13:47:18 -0800 | [diff] [blame] | 521 | |
Jason Ekstrand | aa9987a | 2016-01-05 13:54:02 -0800 | [diff] [blame] | 522 | iview->base_layer = range->baseArrayLayer; |
| 523 | iview->base_mip = range->baseMipLevel; |
Nanley Chery | 3f01bbe | 2016-01-04 12:41:22 -0800 | [diff] [blame] | 524 | |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 525 | struct isl_view isl_view = { |
Jason Ekstrand | 9d5b8f7 | 2016-02-20 21:40:25 -0800 | [diff] [blame] | 526 | .format = format, |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 527 | .base_level = range->baseMipLevel, |
| 528 | .levels = range->levelCount, |
| 529 | .base_array_layer = range->baseArrayLayer, |
| 530 | .array_len = range->layerCount, |
| 531 | .channel_select = { |
| 532 | remap_swizzle(pCreateInfo->components.r, |
| 533 | VK_COMPONENT_SWIZZLE_R, swizzle), |
| 534 | remap_swizzle(pCreateInfo->components.g, |
| 535 | VK_COMPONENT_SWIZZLE_G, swizzle), |
| 536 | remap_swizzle(pCreateInfo->components.b, |
| 537 | VK_COMPONENT_SWIZZLE_B, swizzle), |
| 538 | remap_swizzle(pCreateInfo->components.a, |
| 539 | VK_COMPONENT_SWIZZLE_A, swizzle), |
| 540 | }, |
| 541 | }; |
| 542 | |
| 543 | struct isl_extent4d level0_extent_px; |
| 544 | |
Jason Ekstrand | 9d5b8f7 | 2016-02-20 21:40:25 -0800 | [diff] [blame] | 545 | if (!isl_format_is_compressed(format) && |
Chad Versace | 4d037b5 | 2016-02-08 18:51:52 -0800 | [diff] [blame] | 546 | isl_format_is_compressed(image->format->isl_format)) { |
Nanley Chery | 3f01bbe | 2016-01-04 12:41:22 -0800 | [diff] [blame] | 547 | /* Scale the ImageView extent by the backing Image. This is used |
| 548 | * internally when an uncompressed ImageView is created on a |
| 549 | * compressed Image. The ImageView can therefore be used for copying |
| 550 | * data from a source Image to a destination Image. |
| 551 | */ |
| 552 | const struct isl_format_layout * isl_layout = image->format->isl_layout; |
Nanley Chery | 235abfb | 2016-01-27 12:08:56 -0800 | [diff] [blame] | 553 | |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 554 | level0_extent_px.depth = anv_minify(image->extent.depth, range->baseMipLevel); |
| 555 | level0_extent_px.depth = DIV_ROUND_UP(level0_extent_px.depth, isl_layout->bd); |
Nanley Chery | 235abfb | 2016-01-27 12:08:56 -0800 | [diff] [blame] | 556 | |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 557 | level0_extent_px.height = isl_surf_get_array_pitch_el_rows(&surface->isl) * image->array_size; |
| 558 | level0_extent_px.width = isl_surf_get_row_pitch_el(&surface->isl); |
| 559 | isl_view.base_level = 0; |
| 560 | isl_view.base_array_layer = 0; |
Nanley Chery | 3f01bbe | 2016-01-04 12:41:22 -0800 | [diff] [blame] | 561 | } else { |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 562 | level0_extent_px.width = image->extent.width; |
| 563 | level0_extent_px.height = image->extent.height; |
| 564 | level0_extent_px.depth = image->extent.depth; |
Nanley Chery | 3f01bbe | 2016-01-04 12:41:22 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Jason Ekstrand | a7cc129 | 2016-01-01 13:47:18 -0800 | [diff] [blame] | 567 | iview->extent = (VkExtent3D) { |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 568 | .width = anv_minify(image->extent.width , range->baseMipLevel), |
| 569 | .height = anv_minify(image->extent.height, range->baseMipLevel), |
| 570 | .depth = anv_minify(image->extent.depth , range->baseMipLevel), |
Jason Ekstrand | a7cc129 | 2016-01-01 13:47:18 -0800 | [diff] [blame] | 571 | }; |
| 572 | |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 573 | isl_surf_usage_flags_t cube_usage; |
| 574 | if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE || |
| 575 | pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) { |
| 576 | cube_usage = ISL_SURF_USAGE_CUBE_BIT; |
| 577 | } else { |
| 578 | cube_usage = 0; |
| 579 | } |
| 580 | |
Jason Ekstrand | e9d126f | 2016-02-24 19:49:12 -0800 | [diff] [blame] | 581 | if (image->usage & usage_mask & VK_IMAGE_USAGE_SAMPLED_BIT) { |
Chad Versace | 42b9320 | 2016-02-04 11:41:59 -0800 | [diff] [blame] | 582 | iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer); |
Jason Ekstrand | e5558ff | 2016-01-22 11:57:01 -0800 | [diff] [blame] | 583 | |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 584 | isl_view.usage = cube_usage | ISL_SURF_USAGE_TEXTURE_BIT; |
| 585 | isl_surf_fill_state(&device->isl_dev, |
| 586 | iview->sampler_surface_state.map, |
| 587 | .surf = &surface->isl, |
| 588 | .view = &isl_view, |
| 589 | .mocs = device->default_mocs, |
| 590 | .level0_extent_px = level0_extent_px); |
| 591 | |
| 592 | if (!device->info.has_llc) |
| 593 | anv_state_clflush(iview->sampler_surface_state); |
Jason Ekstrand | e5558ff | 2016-01-22 11:57:01 -0800 | [diff] [blame] | 594 | } else { |
Chad Versace | 42b9320 | 2016-02-04 11:41:59 -0800 | [diff] [blame] | 595 | iview->sampler_surface_state.alloc_size = 0; |
Jason Ekstrand | e5558ff | 2016-01-22 11:57:01 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Jason Ekstrand | e9d126f | 2016-02-24 19:49:12 -0800 | [diff] [blame] | 598 | if (image->usage & usage_mask & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) { |
Jason Ekstrand | e5558ff | 2016-01-22 11:57:01 -0800 | [diff] [blame] | 599 | iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer); |
| 600 | |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 601 | isl_view.usage = cube_usage | ISL_SURF_USAGE_RENDER_TARGET_BIT; |
| 602 | isl_surf_fill_state(&device->isl_dev, |
| 603 | iview->color_rt_surface_state.map, |
| 604 | .surf = &surface->isl, |
| 605 | .view = &isl_view, |
| 606 | .mocs = device->default_mocs, |
| 607 | .level0_extent_px = level0_extent_px); |
| 608 | |
| 609 | if (!device->info.has_llc) |
| 610 | anv_state_clflush(iview->color_rt_surface_state); |
Jason Ekstrand | e5558ff | 2016-01-22 11:57:01 -0800 | [diff] [blame] | 611 | } else { |
| 612 | iview->color_rt_surface_state.alloc_size = 0; |
| 613 | } |
| 614 | |
Jason Ekstrand | e9d126f | 2016-02-24 19:49:12 -0800 | [diff] [blame] | 615 | if (image->usage & usage_mask & VK_IMAGE_USAGE_STORAGE_BIT) { |
Jason Ekstrand | e5558ff | 2016-01-22 11:57:01 -0800 | [diff] [blame] | 616 | iview->storage_surface_state = alloc_surface_state(device, cmd_buffer); |
| 617 | |
Jason Ekstrand | 9d5b8f7 | 2016-02-20 21:40:25 -0800 | [diff] [blame] | 618 | if (has_matching_storage_typed_format(device, format)) { |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 619 | isl_view.usage = cube_usage | ISL_SURF_USAGE_STORAGE_BIT; |
| 620 | isl_surf_fill_state(&device->isl_dev, |
| 621 | iview->storage_surface_state.map, |
| 622 | .surf = &surface->isl, |
| 623 | .view = &isl_view, |
| 624 | .mocs = device->default_mocs, |
| 625 | .level0_extent_px = level0_extent_px); |
| 626 | } else { |
Francisco Jerez | 6840cc1 | 2016-01-26 14:50:52 -0800 | [diff] [blame] | 627 | anv_fill_buffer_surface_state(device, iview->storage_surface_state, |
Francisco Jerez | a50dc70 | 2016-01-26 12:23:08 -0800 | [diff] [blame] | 628 | ISL_FORMAT_RAW, |
| 629 | iview->offset, |
| 630 | iview->bo->size - iview->offset, 1); |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 631 | } |
Francisco Jerez | a50dc70 | 2016-01-26 12:23:08 -0800 | [diff] [blame] | 632 | |
Jason Ekstrand | 7363024 | 2016-02-24 11:38:14 -0800 | [diff] [blame^] | 633 | anv_image_view_fill_image_param(device, iview, |
| 634 | &iview->storage_image_param); |
| 635 | |
Jason Ekstrand | ded57c3 | 2016-02-20 11:45:50 -0800 | [diff] [blame] | 636 | if (!device->info.has_llc) |
| 637 | anv_state_clflush(iview->storage_surface_state); |
Jason Ekstrand | e5558ff | 2016-01-22 11:57:01 -0800 | [diff] [blame] | 638 | } else { |
| 639 | iview->storage_surface_state.alloc_size = 0; |
Kristian Høgsberg Kristensen | 988341a | 2015-08-19 21:36:57 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 643 | VkResult |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 644 | anv_CreateImageView(VkDevice _device, |
| 645 | const VkImageViewCreateInfo *pCreateInfo, |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 646 | const VkAllocationCallbacks *pAllocator, |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 647 | VkImageView *pView) |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 648 | { |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 649 | ANV_FROM_HANDLE(anv_device, device, _device); |
Kristian Høgsberg Kristensen | f1455ff | 2015-08-20 22:59:19 -0700 | [diff] [blame] | 650 | struct anv_image_view *view; |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 651 | |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 652 | view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8, |
| 653 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Kristian Høgsberg Kristensen | f1455ff | 2015-08-20 22:59:19 -0700 | [diff] [blame] | 654 | if (view == NULL) |
| 655 | return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 656 | |
Jason Ekstrand | e9d126f | 2016-02-24 19:49:12 -0800 | [diff] [blame] | 657 | anv_image_view_init(view, device, pCreateInfo, NULL, 0, ~0); |
Kristian Høgsberg Kristensen | f1455ff | 2015-08-20 22:59:19 -0700 | [diff] [blame] | 658 | |
| 659 | *pView = anv_image_view_to_handle(view); |
| 660 | |
| 661 | return VK_SUCCESS; |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 664 | void |
| 665 | anv_DestroyImageView(VkDevice _device, VkImageView _iview, |
| 666 | const VkAllocationCallbacks *pAllocator) |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 667 | { |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 668 | ANV_FROM_HANDLE(anv_device, device, _device); |
| 669 | ANV_FROM_HANDLE(anv_image_view, iview, _iview); |
| 670 | |
Chad Versace | 3eebf36 | 2016-02-04 11:58:05 -0800 | [diff] [blame] | 671 | if (iview->color_rt_surface_state.alloc_size > 0) { |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 672 | anv_state_pool_free(&device->surface_state_pool, |
| 673 | iview->color_rt_surface_state); |
| 674 | } |
| 675 | |
Chad Versace | 3eebf36 | 2016-02-04 11:58:05 -0800 | [diff] [blame] | 676 | if (iview->sampler_surface_state.alloc_size > 0) { |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 677 | anv_state_pool_free(&device->surface_state_pool, |
Chad Versace | 42b9320 | 2016-02-04 11:41:59 -0800 | [diff] [blame] | 678 | iview->sampler_surface_state); |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Chad Versace | 3eebf36 | 2016-02-04 11:58:05 -0800 | [diff] [blame] | 681 | if (iview->storage_surface_state.alloc_size > 0) { |
Jason Ekstrand | ff05f63 | 2015-12-07 17:17:30 -0800 | [diff] [blame] | 682 | anv_state_pool_free(&device->surface_state_pool, |
| 683 | iview->storage_surface_state); |
| 684 | } |
| 685 | |
Jason Ekstrand | fcfb404 | 2015-12-02 03:28:27 -0800 | [diff] [blame] | 686 | anv_free2(&device->alloc, pAllocator, iview); |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 687 | } |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 688 | |
Jason Ekstrand | c561860 | 2015-12-12 16:11:23 -0800 | [diff] [blame] | 689 | VkResult |
| 690 | anv_CreateBufferView(VkDevice _device, |
| 691 | const VkBufferViewCreateInfo *pCreateInfo, |
| 692 | const VkAllocationCallbacks *pAllocator, |
| 693 | VkBufferView *pView) |
| 694 | { |
| 695 | ANV_FROM_HANDLE(anv_device, device, _device); |
| 696 | ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer); |
| 697 | struct anv_buffer_view *view; |
| 698 | |
Jason Ekstrand | c561860 | 2015-12-12 16:11:23 -0800 | [diff] [blame] | 699 | view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8, |
| 700 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
| 701 | if (!view) |
| 702 | return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 703 | |
Jason Ekstrand | c561860 | 2015-12-12 16:11:23 -0800 | [diff] [blame] | 704 | const struct anv_format *format = |
| 705 | anv_format_for_vk_format(pCreateInfo->format); |
| 706 | |
Chad Versace | 4d037b5 | 2016-02-08 18:51:52 -0800 | [diff] [blame] | 707 | view->format = format->isl_format; |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 708 | view->bo = buffer->bo; |
| 709 | view->offset = buffer->offset + pCreateInfo->offset; |
Jason Ekstrand | 56dbf13 | 2016-01-19 15:01:10 -0800 | [diff] [blame] | 710 | view->range = pCreateInfo->range == VK_WHOLE_SIZE ? |
| 711 | buffer->size - view->offset : pCreateInfo->range; |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 712 | |
| 713 | if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) { |
| 714 | view->surface_state = |
| 715 | anv_state_pool_alloc(&device->surface_state_pool, 64, 64); |
| 716 | |
Francisco Jerez | 6840cc1 | 2016-01-26 14:50:52 -0800 | [diff] [blame] | 717 | anv_fill_buffer_surface_state(device, view->surface_state, |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 718 | view->format, |
Jason Ekstrand | 56dbf13 | 2016-01-19 15:01:10 -0800 | [diff] [blame] | 719 | view->offset, view->range, |
Chad Versace | 89b68dc | 2016-01-05 09:59:07 -0800 | [diff] [blame] | 720 | format->isl_layout->bs); |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 721 | } else { |
| 722 | view->surface_state = (struct anv_state){ 0 }; |
| 723 | } |
| 724 | |
| 725 | if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) { |
| 726 | view->storage_surface_state = |
| 727 | anv_state_pool_alloc(&device->surface_state_pool, 64, 64); |
| 728 | |
| 729 | enum isl_format storage_format = |
Francisco Jerez | a50dc70 | 2016-01-26 12:23:08 -0800 | [diff] [blame] | 730 | has_matching_storage_typed_format(device, view->format) ? |
| 731 | isl_lower_storage_image_format(&device->isl_dev, view->format) : |
| 732 | ISL_FORMAT_RAW; |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 733 | |
Francisco Jerez | 6840cc1 | 2016-01-26 14:50:52 -0800 | [diff] [blame] | 734 | anv_fill_buffer_surface_state(device, view->storage_surface_state, |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 735 | storage_format, |
Jason Ekstrand | 56dbf13 | 2016-01-19 15:01:10 -0800 | [diff] [blame] | 736 | view->offset, view->range, |
Francisco Jerez | a50dc70 | 2016-01-26 12:23:08 -0800 | [diff] [blame] | 737 | (storage_format == ISL_FORMAT_RAW ? 1 : |
| 738 | format->isl_layout->bs)); |
| 739 | |
Jason Ekstrand | 7363024 | 2016-02-24 11:38:14 -0800 | [diff] [blame^] | 740 | anv_buffer_view_fill_image_param(device, view, |
| 741 | &view->storage_image_param); |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 742 | } else { |
| 743 | view->storage_surface_state = (struct anv_state){ 0 }; |
| 744 | } |
Jason Ekstrand | c561860 | 2015-12-12 16:11:23 -0800 | [diff] [blame] | 745 | |
| 746 | *pView = anv_buffer_view_to_handle(view); |
| 747 | |
| 748 | return VK_SUCCESS; |
| 749 | } |
| 750 | |
| 751 | void |
| 752 | anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView, |
| 753 | const VkAllocationCallbacks *pAllocator) |
| 754 | { |
| 755 | ANV_FROM_HANDLE(anv_device, device, _device); |
| 756 | ANV_FROM_HANDLE(anv_buffer_view, view, bufferView); |
| 757 | |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 758 | if (view->surface_state.alloc_size > 0) |
| 759 | anv_state_pool_free(&device->surface_state_pool, |
| 760 | view->surface_state); |
| 761 | |
| 762 | if (view->storage_surface_state.alloc_size > 0) |
| 763 | anv_state_pool_free(&device->surface_state_pool, |
| 764 | view->storage_surface_state); |
| 765 | |
Jason Ekstrand | c561860 | 2015-12-12 16:11:23 -0800 | [diff] [blame] | 766 | anv_free2(&device->alloc, pAllocator, view); |
| 767 | } |
| 768 | |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 769 | struct anv_surface * |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 770 | anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask) |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 771 | { |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 772 | switch (aspect_mask) { |
| 773 | case VK_IMAGE_ASPECT_COLOR_BIT: |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 774 | /* Dragons will eat you. |
| 775 | * |
| 776 | * Meta attaches all destination surfaces as color render targets. Guess |
| 777 | * what surface the Meta Dragons really want. |
| 778 | */ |
Chad Versace | e6d3432 | 2016-02-08 19:07:10 -0800 | [diff] [blame] | 779 | if (image->format->has_depth && image->format->has_stencil) { |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 780 | return &image->depth_surface; |
Chad Versace | e6d3432 | 2016-02-08 19:07:10 -0800 | [diff] [blame] | 781 | } else if (image->format->has_depth) { |
Chad Versace | 24de3d4 | 2015-10-06 19:11:58 -0700 | [diff] [blame] | 782 | return &image->depth_surface; |
| 783 | } else if (image->format->has_stencil) { |
| 784 | return &image->stencil_surface; |
| 785 | } else { |
| 786 | return &image->color_surface; |
| 787 | } |
| 788 | break; |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 789 | case VK_IMAGE_ASPECT_DEPTH_BIT: |
Chad Versace | e6d3432 | 2016-02-08 19:07:10 -0800 | [diff] [blame] | 790 | assert(image->format->has_depth); |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 791 | return &image->depth_surface; |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 792 | case VK_IMAGE_ASPECT_STENCIL_BIT: |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 793 | assert(image->format->has_stencil); |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 794 | return &image->stencil_surface; |
Chad Versace | 7a089bd | 2015-10-05 06:48:14 -0700 | [diff] [blame] | 795 | case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT: |
Chad Versace | e6d3432 | 2016-02-08 19:07:10 -0800 | [diff] [blame] | 796 | if (image->format->has_depth && image->format->has_stencil) { |
Kristian Høgsberg Kristensen | 8e07f79 | 2016-01-25 15:14:47 -0800 | [diff] [blame] | 797 | /* FINISHME: The Vulkan spec (git a511ba2) requires support for |
| 798 | * combined depth stencil formats. Specifically, it states: |
Chad Versace | 03dd722 | 2015-10-07 09:03:47 -0700 | [diff] [blame] | 799 | * |
| 800 | * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or |
| 801 | * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported. |
Kristian Høgsberg Kristensen | 8e07f79 | 2016-01-25 15:14:47 -0800 | [diff] [blame] | 802 | * |
| 803 | * Image views with both depth and stencil aspects are only valid for |
| 804 | * render target attachments, in which case |
| 805 | * cmd_buffer_emit_depth_stencil() will pick out both the depth and |
| 806 | * stencil surfaces from the underlying surface. |
Chad Versace | 03dd722 | 2015-10-07 09:03:47 -0700 | [diff] [blame] | 807 | */ |
Chad Versace | 03dd722 | 2015-10-07 09:03:47 -0700 | [diff] [blame] | 808 | return &image->depth_surface; |
Chad Versace | e6d3432 | 2016-02-08 19:07:10 -0800 | [diff] [blame] | 809 | } else if (image->format->has_depth) { |
Chad Versace | 03dd722 | 2015-10-07 09:03:47 -0700 | [diff] [blame] | 810 | return &image->depth_surface; |
| 811 | } else if (image->format->has_stencil) { |
| 812 | return &image->stencil_surface; |
| 813 | } |
| 814 | /* fallthrough */ |
Chad Versace | 941b48e | 2015-08-28 07:08:58 -0700 | [diff] [blame] | 815 | default: |
| 816 | unreachable("image does not have aspect"); |
| 817 | return NULL; |
| 818 | } |
| 819 | } |
Jason Ekstrand | 43ac954 | 2015-11-18 15:14:05 -0800 | [diff] [blame] | 820 | |
Jason Ekstrand | ba393c9 | 2016-01-05 13:55:00 -0800 | [diff] [blame] | 821 | static void |
| 822 | image_param_defaults(struct brw_image_param *param) |
| 823 | { |
| 824 | memset(param, 0, sizeof *param); |
| 825 | /* Set the swizzling shifts to all-ones to effectively disable swizzling -- |
| 826 | * See emit_address_calculation() in brw_fs_surface_builder.cpp for a more |
| 827 | * detailed explanation of these parameters. |
| 828 | */ |
| 829 | param->swizzling[0] = 0xff; |
| 830 | param->swizzling[1] = 0xff; |
| 831 | } |
| 832 | |
Jason Ekstrand | 43ac954 | 2015-11-18 15:14:05 -0800 | [diff] [blame] | 833 | void |
| 834 | anv_image_view_fill_image_param(struct anv_device *device, |
| 835 | struct anv_image_view *view, |
| 836 | struct brw_image_param *param) |
| 837 | { |
Jason Ekstrand | ba393c9 | 2016-01-05 13:55:00 -0800 | [diff] [blame] | 838 | image_param_defaults(param); |
| 839 | |
| 840 | const struct isl_surf *surf = &view->image->color_surface.isl; |
Jason Ekstrand | ba393c9 | 2016-01-05 13:55:00 -0800 | [diff] [blame] | 841 | const int cpp = isl_format_get_layout(surf->format)->bs; |
Francisco Jerez | d2ec510 | 2016-01-26 12:20:01 -0800 | [diff] [blame] | 842 | const struct isl_extent3d image_align_sa = |
| 843 | isl_surf_get_image_alignment_sa(surf); |
Jason Ekstrand | ba393c9 | 2016-01-05 13:55:00 -0800 | [diff] [blame] | 844 | |
Francisco Jerez | d2ec510 | 2016-01-26 12:20:01 -0800 | [diff] [blame] | 845 | param->size[0] = view->extent.width; |
| 846 | param->size[1] = view->extent.height; |
Jason Ekstrand | ba393c9 | 2016-01-05 13:55:00 -0800 | [diff] [blame] | 847 | if (surf->dim == ISL_SURF_DIM_3D) { |
Francisco Jerez | d2ec510 | 2016-01-26 12:20:01 -0800 | [diff] [blame] | 848 | param->size[2] = view->extent.depth; |
Jason Ekstrand | ba393c9 | 2016-01-05 13:55:00 -0800 | [diff] [blame] | 849 | } else { |
| 850 | param->size[2] = surf->logical_level0_px.array_len - view->base_layer; |
| 851 | } |
| 852 | |
Nanley Chery | 308ec02 | 2016-01-27 09:29:09 -0800 | [diff] [blame] | 853 | isl_surf_get_image_offset_el(surf, view->base_mip, view->base_layer, 0, |
Jason Ekstrand | ba393c9 | 2016-01-05 13:55:00 -0800 | [diff] [blame] | 854 | ¶m->offset[0], ¶m->offset[1]); |
| 855 | |
| 856 | param->stride[0] = cpp; |
| 857 | param->stride[1] = surf->row_pitch / cpp; |
Francisco Jerez | d2ec510 | 2016-01-26 12:20:01 -0800 | [diff] [blame] | 858 | |
| 859 | if (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D) { |
| 860 | param->stride[2] = util_align_npot(param->size[0], image_align_sa.w); |
| 861 | param->stride[3] = util_align_npot(param->size[1], image_align_sa.h); |
| 862 | } else { |
| 863 | param->stride[2] = 0; |
| 864 | param->stride[3] = isl_surf_get_array_pitch_el_rows(surf); |
| 865 | } |
Jason Ekstrand | ba393c9 | 2016-01-05 13:55:00 -0800 | [diff] [blame] | 866 | |
| 867 | switch (surf->tiling) { |
| 868 | case ISL_TILING_LINEAR: |
| 869 | /* image_param_defaults is good enough */ |
| 870 | break; |
| 871 | |
| 872 | case ISL_TILING_X: |
| 873 | /* An X tile is a rectangular block of 512x8 bytes. */ |
| 874 | param->tiling[0] = util_logbase2(512 / cpp); |
| 875 | param->tiling[1] = util_logbase2(8); |
| 876 | |
| 877 | if (device->isl_dev.has_bit6_swizzling) { |
| 878 | /* Right shifts required to swizzle bits 9 and 10 of the memory |
| 879 | * address with bit 6. |
| 880 | */ |
| 881 | param->swizzling[0] = 3; |
| 882 | param->swizzling[1] = 4; |
| 883 | } |
| 884 | break; |
| 885 | |
| 886 | case ISL_TILING_Y0: |
| 887 | /* The layout of a Y-tiled surface in memory isn't really fundamentally |
| 888 | * different to the layout of an X-tiled surface, we simply pretend that |
| 889 | * the surface is broken up in a number of smaller 16Bx32 tiles, each |
| 890 | * one arranged in X-major order just like is the case for X-tiling. |
| 891 | */ |
| 892 | param->tiling[0] = util_logbase2(16 / cpp); |
| 893 | param->tiling[1] = util_logbase2(32); |
| 894 | |
| 895 | if (device->isl_dev.has_bit6_swizzling) { |
| 896 | /* Right shift required to swizzle bit 9 of the memory address with |
| 897 | * bit 6. |
| 898 | */ |
| 899 | param->swizzling[0] = 3; |
| 900 | param->swizzling[1] = 0xff; |
| 901 | } |
| 902 | break; |
| 903 | |
| 904 | default: |
| 905 | assert(!"Unhandled storage image tiling"); |
| 906 | } |
| 907 | |
| 908 | /* 3D textures are arranged in 2D in memory with 2^lod slices per row. The |
| 909 | * address calculation algorithm (emit_address_calculation() in |
| 910 | * brw_fs_surface_builder.cpp) handles this as a sort of tiling with |
| 911 | * modulus equal to the LOD. |
| 912 | */ |
Francisco Jerez | d2ec510 | 2016-01-26 12:20:01 -0800 | [diff] [blame] | 913 | param->tiling[2] = (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D ? |
| 914 | view->base_mip : 0); |
Jason Ekstrand | 43ac954 | 2015-11-18 15:14:05 -0800 | [diff] [blame] | 915 | } |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 916 | |
| 917 | void |
| 918 | anv_buffer_view_fill_image_param(struct anv_device *device, |
| 919 | struct anv_buffer_view *view, |
| 920 | struct brw_image_param *param) |
| 921 | { |
Jason Ekstrand | ba393c9 | 2016-01-05 13:55:00 -0800 | [diff] [blame] | 922 | image_param_defaults(param); |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 923 | |
Chad Versace | 89b68dc | 2016-01-05 09:59:07 -0800 | [diff] [blame] | 924 | param->stride[0] = isl_format_layouts[view->format].bs; |
Jason Ekstrand | 783a211 | 2015-12-14 16:51:12 -0800 | [diff] [blame] | 925 | param->size[0] = view->range / param->stride[0]; |
| 926 | } |