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