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