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 | 69e11ad | 2015-07-06 16:25:59 -0700 | [diff] [blame] | 32 | struct anv_image_view_info { |
| 33 | uint8_t surface_type; /**< RENDER_SURFACE_STATE.SurfaceType */ |
| 34 | bool is_array:1; /**< RENDER_SURFACE_STATE.SurfaceArray */ |
| 35 | bool is_cube:1; /**< RENDER_SURFACE_STATE.CubeFaceEnable* */ |
| 36 | }; |
| 37 | |
Chad Versace | e6162c2 | 2015-06-09 12:11:46 -0700 | [diff] [blame] | 38 | static const uint8_t anv_halign[] = { |
| 39 | [4] = HALIGN4, |
| 40 | [8] = HALIGN8, |
| 41 | [16] = HALIGN16, |
| 42 | }; |
| 43 | |
| 44 | static const uint8_t anv_valign[] = { |
| 45 | [4] = VALIGN4, |
| 46 | [8] = VALIGN8, |
| 47 | [16] = VALIGN16, |
| 48 | }; |
| 49 | |
Chad Versace | cb30aca | 2015-05-28 07:37:59 -0700 | [diff] [blame] | 50 | static const uint8_t anv_surf_type_from_image_type[] = { |
| 51 | [VK_IMAGE_TYPE_1D] = SURFTYPE_1D, |
| 52 | [VK_IMAGE_TYPE_2D] = SURFTYPE_2D, |
| 53 | [VK_IMAGE_TYPE_3D] = SURFTYPE_3D, |
Chad Versace | 69e11ad | 2015-07-06 16:25:59 -0700 | [diff] [blame] | 54 | |
Chad Versace | cb30aca | 2015-05-28 07:37:59 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Chad Versace | 69e11ad | 2015-07-06 16:25:59 -0700 | [diff] [blame] | 57 | static const struct anv_image_view_info |
| 58 | anv_image_view_info_table[] = { |
| 59 | #define INFO(s, ...) { .surface_type = s, __VA_ARGS__ } |
| 60 | [VK_IMAGE_VIEW_TYPE_1D] = INFO(SURFTYPE_1D), |
| 61 | [VK_IMAGE_VIEW_TYPE_2D] = INFO(SURFTYPE_2D), |
| 62 | [VK_IMAGE_VIEW_TYPE_3D] = INFO(SURFTYPE_3D), |
| 63 | [VK_IMAGE_VIEW_TYPE_CUBE] = INFO(SURFTYPE_CUBE, .is_cube = 1), |
| 64 | [VK_IMAGE_VIEW_TYPE_1D_ARRAY] = INFO(SURFTYPE_1D, .is_array = 1), |
| 65 | [VK_IMAGE_VIEW_TYPE_2D_ARRAY] = INFO(SURFTYPE_2D, .is_array = 1), |
| 66 | [VK_IMAGE_VIEW_TYPE_CUBE_ARRAY] = INFO(SURFTYPE_CUBE, .is_array = 1, .is_cube = 1), |
| 67 | #undef INFO |
Chad Versace | cb30aca | 2015-05-28 07:37:59 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Chad Versace | fa35296 | 2015-05-28 07:40:22 -0700 | [diff] [blame] | 70 | static const struct anv_surf_type_limits { |
| 71 | int32_t width; |
| 72 | int32_t height; |
| 73 | int32_t depth; |
| 74 | } anv_surf_type_limits[] = { |
| 75 | [SURFTYPE_1D] = {16384, 0, 2048}, |
| 76 | [SURFTYPE_2D] = {16384, 16384, 2048}, |
| 77 | [SURFTYPE_3D] = {2048, 2048, 2048}, |
| 78 | [SURFTYPE_CUBE] = {16384, 16384, 340}, |
| 79 | [SURFTYPE_BUFFER] = {128, 16384, 64}, |
| 80 | [SURFTYPE_STRBUF] = {128, 16384, 64}, |
| 81 | }; |
| 82 | |
Chad Versace | e6bd568 | 2015-06-09 13:29:08 -0700 | [diff] [blame] | 83 | static const struct anv_tile_info { |
| 84 | uint32_t width; |
| 85 | uint32_t height; |
| 86 | |
| 87 | /** |
| 88 | * Alignment for RENDER_SURFACE_STATE.SurfaceBaseAddress. |
| 89 | * |
| 90 | * To simplify calculations, the alignments defined in the table are |
| 91 | * sometimes larger than required. For example, Skylake requires that X and |
| 92 | * Y tiled buffers be aligned to 4K, but Broadwell permits smaller |
| 93 | * alignment. We choose 4K to accomodate both chipsets. The alignment of |
| 94 | * a linear buffer depends on its element type and usage. Linear depth |
| 95 | * buffers have the largest alignment, 64B, so we choose that for all linear |
| 96 | * buffers. |
| 97 | */ |
| 98 | uint32_t surface_alignment; |
| 99 | } anv_tile_info_table[] = { |
| 100 | [LINEAR] = { 1, 1, 64 }, |
| 101 | [XMAJOR] = { 512, 8, 4096 }, |
| 102 | [YMAJOR] = { 128, 32, 4096 }, |
| 103 | [WMAJOR] = { 128, 32, 4096 }, |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
Chad Versace | f1db3b3 | 2015-06-09 14:33:05 -0700 | [diff] [blame] | 106 | static uint32_t |
Chad Versace | fdcd71f | 2015-06-26 20:06:08 -0700 | [diff] [blame] | 107 | anv_image_choose_tile_mode(const struct anv_image_create_info *anv_info) |
Chad Versace | f1db3b3 | 2015-06-09 14:33:05 -0700 | [diff] [blame] | 108 | { |
Chad Versace | fdcd71f | 2015-06-26 20:06:08 -0700 | [diff] [blame] | 109 | if (anv_info->force_tile_mode) |
Chad Versace | f1db3b3 | 2015-06-09 14:33:05 -0700 | [diff] [blame] | 110 | return anv_info->tile_mode; |
| 111 | |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 112 | if (anv_info->vk_info->format == VK_FORMAT_S8_UINT) |
| 113 | return WMAJOR; |
| 114 | |
Chad Versace | fdcd71f | 2015-06-26 20:06:08 -0700 | [diff] [blame] | 115 | switch (anv_info->vk_info->tiling) { |
Chad Versace | f1db3b3 | 2015-06-09 14:33:05 -0700 | [diff] [blame] | 116 | case VK_IMAGE_TILING_LINEAR: |
| 117 | return LINEAR; |
| 118 | case VK_IMAGE_TILING_OPTIMAL: |
Kristian Høgsberg Kristensen | 00494c6 | 2015-06-11 22:07:16 -0700 | [diff] [blame] | 119 | return YMAJOR; |
Chad Versace | f1db3b3 | 2015-06-09 14:33:05 -0700 | [diff] [blame] | 120 | default: |
| 121 | assert(!"bad VKImageTiling"); |
| 122 | return LINEAR; |
| 123 | } |
| 124 | } |
| 125 | |
Chad Versace | 5a6b2e6 | 2015-08-17 13:50:43 -0700 | [diff] [blame^] | 126 | |
| 127 | /** |
| 128 | * The \a format argument is required and overrides any format in |
| 129 | * struct anv_image_create_info. |
| 130 | */ |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 131 | static VkResult |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 132 | anv_image_make_surface(const struct anv_image_create_info *create_info, |
Chad Versace | 5a6b2e6 | 2015-08-17 13:50:43 -0700 | [diff] [blame^] | 133 | const struct anv_format *format, |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 134 | uint64_t *inout_image_size, |
| 135 | uint32_t *inout_image_alignment, |
| 136 | struct anv_surface *out_surface) |
| 137 | { |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 138 | /* See RENDER_SURFACE_STATE.SurfaceQPitch */ |
| 139 | static const uint16_t min_qpitch UNUSED = 0x4; |
| 140 | static const uint16_t max_qpitch UNUSED = 0x1ffc; |
| 141 | |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 142 | const VkExtent3D *restrict extent = &create_info->vk_info->extent; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 143 | const uint32_t levels = create_info->vk_info->mipLevels; |
| 144 | const uint32_t array_size = create_info->vk_info->arraySize; |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 145 | |
| 146 | const uint8_t tile_mode = anv_image_choose_tile_mode(create_info); |
| 147 | |
| 148 | const struct anv_tile_info *tile_info = |
| 149 | &anv_tile_info_table[tile_mode]; |
| 150 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 151 | const uint32_t i = 4; /* FINISHME: Stop hardcoding subimage alignment */ |
| 152 | const uint32_t j = 4; /* FINISHME: Stop hardcoding subimage alignment */ |
| 153 | const uint32_t w0 = align_u32(extent->width, i); |
| 154 | const uint32_t h0 = align_u32(extent->height, j); |
| 155 | |
| 156 | uint16_t qpitch; |
| 157 | uint32_t mt_width; |
| 158 | uint32_t mt_height; |
| 159 | |
| 160 | if (levels == 1 && array_size == 1) { |
| 161 | qpitch = min_qpitch; |
| 162 | mt_width = w0; |
| 163 | mt_height = h0; |
| 164 | } else { |
| 165 | uint32_t w1 = align_u32(anv_minify(extent->width, 1), i); |
| 166 | uint32_t h1 = align_u32(anv_minify(extent->height, 1), j); |
| 167 | uint32_t w2 = align_u32(anv_minify(extent->width, 2), i); |
| 168 | |
| 169 | qpitch = h0 + h1 + 11 * j; |
| 170 | mt_width = MAX(w0, w1 + w2); |
| 171 | mt_height = array_size * qpitch; |
| 172 | } |
| 173 | |
| 174 | assert(qpitch >= min_qpitch); |
| 175 | if (qpitch > max_qpitch) { |
| 176 | anv_loge("image qpitch > 0x%x\n", max_qpitch); |
| 177 | return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 178 | } |
| 179 | |
| 180 | /* From the Broadwell PRM, RENDER_SURFACE_STATE.SurfaceQpitch: |
| 181 | * |
| 182 | * This field must be set an integer multiple of the Surface Vertical |
| 183 | * Alignment. |
| 184 | */ |
| 185 | assert(anv_is_aligned(qpitch, j)); |
| 186 | |
Chad Versace | 5a6b2e6 | 2015-08-17 13:50:43 -0700 | [diff] [blame^] | 187 | uint32_t stride = align_u32(mt_width * format->cpp, tile_info->width); |
Kristian Høgsberg Kristensen | 6d09d06 | 2015-08-14 10:02:19 -0700 | [diff] [blame] | 188 | if (create_info->stride > 0) |
| 189 | stride = create_info->stride; |
| 190 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 191 | const uint32_t size = stride * align_u32(mt_height, tile_info->height); |
| 192 | const uint32_t offset = align_u32(*inout_image_size, |
| 193 | tile_info->surface_alignment); |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 194 | |
| 195 | *inout_image_size = offset + size; |
| 196 | *inout_image_alignment = MAX(*inout_image_alignment, |
| 197 | tile_info->surface_alignment); |
| 198 | |
| 199 | *out_surface = (struct anv_surface) { |
| 200 | .offset = offset, |
| 201 | .stride = stride, |
| 202 | .tile_mode = tile_mode, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 203 | .qpitch = qpitch, |
| 204 | .h_align = i, |
| 205 | .v_align = j, |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 206 | }; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 207 | |
| 208 | return VK_SUCCESS; |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 211 | VkResult |
| 212 | anv_image_create(VkDevice _device, |
| 213 | const struct anv_image_create_info *create_info, |
| 214 | VkImage *pImage) |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 215 | { |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 216 | ANV_FROM_HANDLE(anv_device, device, _device); |
Chad Versace | fdcd71f | 2015-06-26 20:06:08 -0700 | [diff] [blame] | 217 | const VkImageCreateInfo *pCreateInfo = create_info->vk_info; |
Chad Versace | 67a7659 | 2015-06-26 09:17:52 -0700 | [diff] [blame] | 218 | const VkExtent3D *restrict extent = &pCreateInfo->extent; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 219 | struct anv_image *image = NULL; |
| 220 | VkResult r; |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 221 | |
| 222 | assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO); |
| 223 | |
Jason Ekstrand | 2a3c296 | 2015-06-10 21:04:51 -0700 | [diff] [blame] | 224 | /* XXX: We don't handle any of these */ |
| 225 | anv_assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D); |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 226 | anv_assert(pCreateInfo->mipLevels > 0); |
| 227 | anv_assert(pCreateInfo->arraySize > 0); |
Jason Ekstrand | 2a3c296 | 2015-06-10 21:04:51 -0700 | [diff] [blame] | 228 | anv_assert(pCreateInfo->samples == 1); |
Chad Versace | 5d7103e | 2015-06-26 09:05:46 -0700 | [diff] [blame] | 229 | anv_assert(pCreateInfo->extent.width > 0); |
| 230 | anv_assert(pCreateInfo->extent.height > 0); |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 231 | anv_assert(pCreateInfo->extent.depth > 0); |
Jason Ekstrand | 2a3c296 | 2015-06-10 21:04:51 -0700 | [diff] [blame] | 232 | |
Chad Versace | 7ea1216 | 2015-05-28 07:45:31 -0700 | [diff] [blame] | 233 | /* TODO(chadv): How should we validate inputs? */ |
Chad Versace | 67a7659 | 2015-06-26 09:17:52 -0700 | [diff] [blame] | 234 | const uint8_t surf_type = |
| 235 | anv_surf_type_from_image_type[pCreateInfo->imageType]; |
Chad Versace | 7ea1216 | 2015-05-28 07:45:31 -0700 | [diff] [blame] | 236 | |
Chad Versace | fa35296 | 2015-05-28 07:40:22 -0700 | [diff] [blame] | 237 | const struct anv_surf_type_limits *limits = |
Chad Versace | 67a7659 | 2015-06-26 09:17:52 -0700 | [diff] [blame] | 238 | &anv_surf_type_limits[surf_type]; |
Chad Versace | fa35296 | 2015-05-28 07:40:22 -0700 | [diff] [blame] | 239 | |
Chad Versace | 67a7659 | 2015-06-26 09:17:52 -0700 | [diff] [blame] | 240 | if (extent->width > limits->width || |
| 241 | extent->height > limits->height || |
| 242 | extent->depth > limits->depth) { |
Chad Versace | fa35296 | 2015-05-28 07:40:22 -0700 | [diff] [blame] | 243 | /* TODO(chadv): What is the correct error? */ |
Chad Versace | 67a7659 | 2015-06-26 09:17:52 -0700 | [diff] [blame] | 244 | anv_loge("image extent is too large"); |
Chad Versace | fa35296 | 2015-05-28 07:40:22 -0700 | [diff] [blame] | 245 | return vk_error(VK_ERROR_INVALID_MEMORY_SIZE); |
| 246 | } |
| 247 | |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 248 | image = anv_device_alloc(device, sizeof(*image), 8, |
| 249 | VK_SYSTEM_ALLOC_TYPE_API_OBJECT); |
| 250 | if (!image) |
| 251 | return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY); |
Chad Versace | 67a7659 | 2015-06-26 09:17:52 -0700 | [diff] [blame] | 252 | |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 253 | memset(image, 0, sizeof(*image)); |
| 254 | image->type = pCreateInfo->imageType; |
| 255 | image->extent = pCreateInfo->extent; |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 256 | image->format = anv_format_for_vk_format(pCreateInfo->format); |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 257 | image->levels = pCreateInfo->mipLevels; |
| 258 | image->array_size = pCreateInfo->arraySize; |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 259 | image->surf_type = surf_type; |
Chad Versace | 67a7659 | 2015-06-26 09:17:52 -0700 | [diff] [blame] | 260 | |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 261 | if (likely(!image->format->has_stencil || image->format->depth_format)) { |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 262 | /* The image's primary surface is a color or depth surface. */ |
Chad Versace | 5a6b2e6 | 2015-08-17 13:50:43 -0700 | [diff] [blame^] | 263 | r = anv_image_make_surface(create_info, image->format, |
| 264 | &image->size, &image->alignment, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 265 | &image->primary_surface); |
| 266 | if (r != VK_SUCCESS) |
| 267 | goto fail; |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 270 | if (image->format->has_stencil) { |
Chad Versace | 37d6e04 | 2015-06-26 09:47:17 -0700 | [diff] [blame] | 271 | /* From the GPU's perspective, the depth buffer and stencil buffer are |
| 272 | * separate buffers. From Vulkan's perspective, though, depth and |
| 273 | * stencil reside in the same image. To satisfy Vulkan and the GPU, we |
| 274 | * place the depth and stencil buffers in the same bo. |
| 275 | */ |
Chad Versace | 5a6b2e6 | 2015-08-17 13:50:43 -0700 | [diff] [blame^] | 276 | r = anv_image_make_surface(create_info, anv_format_s8_uint, |
| 277 | &image->size, &image->alignment, |
| 278 | &image->stencil_surface); |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 279 | if (r != VK_SUCCESS) |
| 280 | goto fail; |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 281 | } |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 282 | |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 283 | *pImage = anv_image_to_handle(image); |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 284 | |
| 285 | return VK_SUCCESS; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 286 | |
| 287 | fail: |
| 288 | if (image) |
| 289 | anv_device_free(device, image); |
| 290 | |
| 291 | return r; |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 294 | VkResult |
| 295 | anv_CreateImage(VkDevice device, |
| 296 | const VkImageCreateInfo *pCreateInfo, |
| 297 | VkImage *pImage) |
Kristian Høgsberg | a29df71 | 2015-05-15 22:04:52 -0700 | [diff] [blame] | 298 | { |
Chad Versace | fdcd71f | 2015-06-26 20:06:08 -0700 | [diff] [blame] | 299 | return anv_image_create(device, |
| 300 | &(struct anv_image_create_info) { |
| 301 | .vk_info = pCreateInfo, |
| 302 | }, |
| 303 | pImage); |
Kristian Høgsberg | a29df71 | 2015-05-15 22:04:52 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Jason Ekstrand | 8b342b3 | 2015-07-10 12:30:58 -0700 | [diff] [blame] | 306 | VkResult |
| 307 | anv_DestroyImage(VkDevice _device, VkImage _image) |
| 308 | { |
| 309 | ANV_FROM_HANDLE(anv_device, device, _device); |
| 310 | |
| 311 | anv_device_free(device, anv_image_from_handle(_image)); |
| 312 | |
| 313 | return VK_SUCCESS; |
| 314 | } |
| 315 | |
Jason Ekstrand | db24afe | 2015-07-07 18:20:18 -0700 | [diff] [blame] | 316 | VkResult anv_GetImageSubresourceLayout( |
| 317 | VkDevice device, |
| 318 | VkImage image, |
| 319 | const VkImageSubresource* pSubresource, |
| 320 | VkSubresourceLayout* pLayout) |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 321 | { |
Jason Ekstrand | ffe9f60 | 2015-05-12 13:44:43 -0700 | [diff] [blame] | 322 | stub_return(VK_UNSUPPORTED); |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 325 | void |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 326 | anv_surface_view_fini(struct anv_device *device, |
| 327 | struct anv_surface_view *view) |
Jason Ekstrand | 9d6f55d | 2015-06-09 11:08:03 -0700 | [diff] [blame] | 328 | { |
Jason Ekstrand | 9d6f55d | 2015-06-09 11:08:03 -0700 | [diff] [blame] | 329 | anv_state_pool_free(&device->surface_state_pool, view->surface_state); |
Jason Ekstrand | 9d6f55d | 2015-06-09 11:08:03 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | void |
Chad Versace | 8213be7 | 2015-07-15 12:00:27 -0700 | [diff] [blame] | 333 | anv_image_view_init(struct anv_image_view *iview, |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 334 | struct anv_device *device, |
| 335 | const VkImageViewCreateInfo* pCreateInfo, |
| 336 | struct anv_cmd_buffer *cmd_buffer) |
| 337 | { |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 338 | ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image); |
Chad Versace | 8213be7 | 2015-07-15 12:00:27 -0700 | [diff] [blame] | 339 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 340 | const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange; |
Chad Versace | 8213be7 | 2015-07-15 12:00:27 -0700 | [diff] [blame] | 341 | struct anv_surface_view *view = &iview->view; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 342 | struct anv_surface *surface; |
Chad Versace | ca6cef3 | 2015-06-26 19:50:04 -0700 | [diff] [blame] | 343 | |
Chad Versace | 69e11ad | 2015-07-06 16:25:59 -0700 | [diff] [blame] | 344 | const struct anv_image_view_info *view_type_info |
| 345 | = &anv_image_view_info_table[pCreateInfo->viewType]; |
| 346 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 347 | if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D) |
| 348 | anv_finishme("non-2D image views"); |
Chad Versace | ca6cef3 | 2015-06-26 19:50:04 -0700 | [diff] [blame] | 349 | |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 350 | switch (pCreateInfo->subresourceRange.aspect) { |
| 351 | case VK_IMAGE_ASPECT_STENCIL: |
Chad Versace | 9c46ba9 | 2015-06-26 19:23:21 -0700 | [diff] [blame] | 352 | anv_finishme("stencil image views"); |
| 353 | abort(); |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 354 | break; |
| 355 | case VK_IMAGE_ASPECT_DEPTH: |
| 356 | case VK_IMAGE_ASPECT_COLOR: |
| 357 | view->offset = image->offset; |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 358 | surface = &image->primary_surface; |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 359 | break; |
| 360 | default: |
Kristian Høgsberg Kristensen | 52637c0 | 2015-06-05 11:51:30 -0700 | [diff] [blame] | 361 | unreachable(""); |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 362 | break; |
| 363 | } |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 364 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 365 | view->bo = image->bo; |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 366 | view->offset = image->offset + surface->offset; |
Chad Versace | 60c4ac5 | 2015-08-17 13:26:28 -0700 | [diff] [blame] | 367 | view->format = anv_format_for_vk_format(pCreateInfo->format); |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 368 | |
Chad Versace | f70d079 | 2015-07-17 14:48:23 -0700 | [diff] [blame] | 369 | iview->extent = (VkExtent3D) { |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 370 | .width = anv_minify(image->extent.width, range->baseMipLevel), |
| 371 | .height = anv_minify(image->extent.height, range->baseMipLevel), |
| 372 | .depth = anv_minify(image->extent.depth, range->baseMipLevel), |
| 373 | }; |
| 374 | |
| 375 | uint32_t depth = 1; |
| 376 | if (range->arraySize > 1) { |
| 377 | depth = range->arraySize; |
| 378 | } else if (image->extent.depth > 1) { |
| 379 | depth = image->extent.depth; |
| 380 | } |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 381 | |
| 382 | static const uint32_t vk_to_gen_swizzle[] = { |
Kristian Høgsberg Kristensen | 5caa408 | 2015-05-31 22:35:11 -0700 | [diff] [blame] | 383 | [VK_CHANNEL_SWIZZLE_ZERO] = SCS_ZERO, |
| 384 | [VK_CHANNEL_SWIZZLE_ONE] = SCS_ONE, |
| 385 | [VK_CHANNEL_SWIZZLE_R] = SCS_RED, |
| 386 | [VK_CHANNEL_SWIZZLE_G] = SCS_GREEN, |
| 387 | [VK_CHANNEL_SWIZZLE_B] = SCS_BLUE, |
| 388 | [VK_CHANNEL_SWIZZLE_A] = SCS_ALPHA |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 389 | }; |
| 390 | |
| 391 | struct GEN8_RENDER_SURFACE_STATE surface_state = { |
Chad Versace | 69e11ad | 2015-07-06 16:25:59 -0700 | [diff] [blame] | 392 | .SurfaceType = view_type_info->surface_type, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 393 | .SurfaceArray = image->array_size > 1, |
Chad Versace | 60c4ac5 | 2015-08-17 13:26:28 -0700 | [diff] [blame] | 394 | .SurfaceFormat = view->format->surface_format, |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 395 | .SurfaceVerticalAlignment = anv_valign[surface->v_align], |
| 396 | .SurfaceHorizontalAlignment = anv_halign[surface->h_align], |
| 397 | .TileMode = surface->tile_mode, |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 398 | .VerticalLineStride = 0, |
| 399 | .VerticalLineStrideOffset = 0, |
| 400 | .SamplerL2BypassModeDisable = true, |
| 401 | .RenderCacheReadWriteMode = WriteOnlyCache, |
| 402 | .MemoryObjectControlState = GEN8_MOCS, |
Chad Versace | 4fcb32a | 2015-07-08 14:39:05 -0700 | [diff] [blame] | 403 | |
| 404 | /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in |
| 405 | * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have |
| 406 | * both Base Mip Level fields nonzero". |
| 407 | */ |
| 408 | .BaseMipLevel = 0.0, |
| 409 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 410 | .SurfaceQPitch = surface->qpitch >> 2, |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 411 | .Height = image->extent.height - 1, |
| 412 | .Width = image->extent.width - 1, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 413 | .Depth = depth - 1, |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 414 | .SurfacePitch = surface->stride - 1, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 415 | .MinimumArrayElement = range->baseArraySlice, |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 416 | .NumberofMultisamples = MULTISAMPLECOUNT_1, |
| 417 | .XOffset = 0, |
| 418 | .YOffset = 0, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 419 | |
| 420 | /* For sampler surfaces, the hardware interprets field MIPCount/LOD as |
| 421 | * MIPCount. The range of levels accessible by the sampler engine is |
| 422 | * [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD]. |
| 423 | */ |
| 424 | .MIPCountLOD = range->mipLevels - 1, |
| 425 | .SurfaceMinLOD = range->baseMipLevel, |
| 426 | |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 427 | .AuxiliarySurfaceMode = AUX_NONE, |
| 428 | .RedClearColor = 0, |
| 429 | .GreenClearColor = 0, |
| 430 | .BlueClearColor = 0, |
| 431 | .AlphaClearColor = 0, |
| 432 | .ShaderChannelSelectRed = vk_to_gen_swizzle[pCreateInfo->channels.r], |
| 433 | .ShaderChannelSelectGreen = vk_to_gen_swizzle[pCreateInfo->channels.g], |
| 434 | .ShaderChannelSelectBlue = vk_to_gen_swizzle[pCreateInfo->channels.b], |
| 435 | .ShaderChannelSelectAlpha = vk_to_gen_swizzle[pCreateInfo->channels.a], |
Kristian Høgsberg Kristensen | a5b49d2 | 2015-06-10 23:11:37 -0700 | [diff] [blame] | 436 | .ResourceMinLOD = 0.0, |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 437 | .SurfaceBaseAddress = { NULL, view->offset }, |
| 438 | }; |
| 439 | |
Chad Versace | fc38383 | 2015-07-17 13:38:09 -0700 | [diff] [blame] | 440 | if (cmd_buffer) { |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 441 | view->surface_state = |
| 442 | anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64); |
Chad Versace | fc38383 | 2015-07-17 13:38:09 -0700 | [diff] [blame] | 443 | } else { |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 444 | view->surface_state = |
| 445 | anv_state_pool_alloc(&device->surface_state_pool, 64, 64); |
Chad Versace | fc38383 | 2015-07-17 13:38:09 -0700 | [diff] [blame] | 446 | } |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 447 | |
| 448 | GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state); |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 451 | VkResult |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 452 | anv_validate_CreateImageView(VkDevice _device, |
| 453 | const VkImageViewCreateInfo *pCreateInfo, |
| 454 | VkImageView *pView) |
| 455 | { |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 456 | ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 457 | const VkImageSubresourceRange *subresource; |
| 458 | const struct anv_image_view_info *view_info; |
| 459 | const struct anv_format *view_format_info; |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 460 | |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 461 | /* Validate structure type before dereferencing it. */ |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 462 | assert(pCreateInfo); |
| 463 | assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 464 | subresource = &pCreateInfo->subresourceRange; |
| 465 | |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 466 | /* Validate viewType is in range before using it. */ |
| 467 | assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE); |
| 468 | assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE); |
| 469 | view_info = &anv_image_view_info_table[pCreateInfo->viewType]; |
| 470 | |
| 471 | /* Validate format is in range before using it. */ |
| 472 | assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE); |
| 473 | assert(pCreateInfo->format <= VK_FORMAT_END_RANGE); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 474 | view_format_info = anv_format_for_vk_format(pCreateInfo->format); |
| 475 | |
| 476 | /* Validate channel swizzles. */ |
| 477 | assert(pCreateInfo->channels.r >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE); |
| 478 | assert(pCreateInfo->channels.r <= VK_CHANNEL_SWIZZLE_END_RANGE); |
| 479 | assert(pCreateInfo->channels.g >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE); |
| 480 | assert(pCreateInfo->channels.g <= VK_CHANNEL_SWIZZLE_END_RANGE); |
| 481 | assert(pCreateInfo->channels.b >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE); |
| 482 | assert(pCreateInfo->channels.b <= VK_CHANNEL_SWIZZLE_END_RANGE); |
| 483 | assert(pCreateInfo->channels.a >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE); |
| 484 | assert(pCreateInfo->channels.a <= VK_CHANNEL_SWIZZLE_END_RANGE); |
| 485 | |
| 486 | /* Validate subresource. */ |
| 487 | assert(subresource->aspect >= VK_IMAGE_ASPECT_BEGIN_RANGE); |
| 488 | assert(subresource->aspect <= VK_IMAGE_ASPECT_END_RANGE); |
| 489 | assert(subresource->mipLevels > 0); |
| 490 | assert(subresource->arraySize > 0); |
| 491 | assert(subresource->baseMipLevel < image->levels); |
| 492 | assert(subresource->baseMipLevel + subresource->mipLevels <= image->levels); |
| 493 | assert(subresource->baseArraySlice < image->array_size); |
| 494 | assert(subresource->baseArraySlice + subresource->arraySize <= image->array_size); |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 495 | assert(pView); |
| 496 | |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 497 | if (view_info->is_cube) { |
| 498 | assert(subresource->baseArraySlice % 6 == 0); |
| 499 | assert(subresource->arraySize % 6 == 0); |
| 500 | } |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 501 | |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 502 | /* Validate format. */ |
| 503 | switch (subresource->aspect) { |
| 504 | case VK_IMAGE_ASPECT_COLOR: |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 505 | assert(!image->format->depth_format); |
| 506 | assert(!image->format->has_stencil); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 507 | assert(!view_format_info->depth_format); |
| 508 | assert(!view_format_info->has_stencil); |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 509 | assert(view_format_info->cpp == image->format->cpp); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 510 | break; |
| 511 | case VK_IMAGE_ASPECT_DEPTH: |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 512 | assert(image->format->depth_format); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 513 | assert(view_format_info->depth_format); |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 514 | assert(view_format_info->cpp == image->format->cpp); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 515 | break; |
| 516 | case VK_IMAGE_ASPECT_STENCIL: |
| 517 | /* FINISHME: Is it legal to have an R8 view of S8? */ |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 518 | assert(image->format->has_stencil); |
Chad Versace | 23075bc | 2015-07-06 17:04:13 -0700 | [diff] [blame] | 519 | assert(view_format_info->has_stencil); |
| 520 | break; |
| 521 | default: |
| 522 | assert(!"bad VkImageAspect"); |
| 523 | break; |
| 524 | } |
Chad Versace | 5b04db7 | 2015-07-06 16:24:28 -0700 | [diff] [blame] | 525 | |
| 526 | return anv_CreateImageView(_device, pCreateInfo, pView); |
| 527 | } |
| 528 | |
| 529 | VkResult |
Chad Versace | 127cb3f | 2015-06-26 20:12:42 -0700 | [diff] [blame] | 530 | anv_CreateImageView(VkDevice _device, |
| 531 | const VkImageViewCreateInfo *pCreateInfo, |
| 532 | VkImageView *pView) |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 533 | { |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 534 | ANV_FROM_HANDLE(anv_device, device, _device); |
Chad Versace | 8213be7 | 2015-07-15 12:00:27 -0700 | [diff] [blame] | 535 | struct anv_image_view *view; |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 536 | |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 537 | view = anv_device_alloc(device, sizeof(*view), 8, |
| 538 | VK_SYSTEM_ALLOC_TYPE_API_OBJECT); |
| 539 | if (view == NULL) |
| 540 | return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 541 | |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 542 | anv_image_view_init(view, device, pCreateInfo, NULL); |
Jason Ekstrand | 1f7dcf9 | 2015-05-13 17:37:12 -0700 | [diff] [blame] | 543 | |
Chad Versace | 8213be7 | 2015-07-15 12:00:27 -0700 | [diff] [blame] | 544 | *pView = anv_image_view_to_handle(view); |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 545 | |
| 546 | return VK_SUCCESS; |
| 547 | } |
| 548 | |
Jason Ekstrand | b94b8df | 2015-07-10 12:25:30 -0700 | [diff] [blame] | 549 | VkResult |
Chad Versace | 8213be7 | 2015-07-15 12:00:27 -0700 | [diff] [blame] | 550 | anv_DestroyImageView(VkDevice _device, VkImageView _iview) |
Jason Ekstrand | b94b8df | 2015-07-10 12:25:30 -0700 | [diff] [blame] | 551 | { |
| 552 | ANV_FROM_HANDLE(anv_device, device, _device); |
Chad Versace | 8213be7 | 2015-07-15 12:00:27 -0700 | [diff] [blame] | 553 | ANV_FROM_HANDLE(anv_image_view, iview, _iview); |
Jason Ekstrand | b94b8df | 2015-07-10 12:25:30 -0700 | [diff] [blame] | 554 | |
Chad Versace | 8213be7 | 2015-07-15 12:00:27 -0700 | [diff] [blame] | 555 | anv_surface_view_fini(device, &iview->view); |
| 556 | anv_device_free(device, iview); |
Jason Ekstrand | b94b8df | 2015-07-10 12:25:30 -0700 | [diff] [blame] | 557 | |
| 558 | return VK_SUCCESS; |
| 559 | } |
| 560 | |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 561 | void |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 562 | anv_color_attachment_view_init(struct anv_color_attachment_view *aview, |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 563 | struct anv_device *device, |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 564 | const VkAttachmentViewCreateInfo* pCreateInfo, |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 565 | struct anv_cmd_buffer *cmd_buffer) |
| 566 | { |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 567 | ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image); |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 568 | struct anv_surface_view *view = &aview->view; |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 569 | struct anv_surface *surface = &image->primary_surface; |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 570 | |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 571 | aview->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_COLOR; |
| 572 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 573 | anv_assert(pCreateInfo->arraySize > 0); |
| 574 | anv_assert(pCreateInfo->mipLevel < image->levels); |
| 575 | anv_assert(pCreateInfo->baseArraySlice + pCreateInfo->arraySize <= image->array_size); |
| 576 | |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 577 | view->bo = image->bo; |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 578 | view->offset = image->offset + surface->offset; |
Chad Versace | 60c4ac5 | 2015-08-17 13:26:28 -0700 | [diff] [blame] | 579 | view->format = anv_format_for_vk_format(pCreateInfo->format); |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 580 | |
Chad Versace | f70d079 | 2015-07-17 14:48:23 -0700 | [diff] [blame] | 581 | aview->base.extent = (VkExtent3D) { |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 582 | .width = anv_minify(image->extent.width, pCreateInfo->mipLevel), |
| 583 | .height = anv_minify(image->extent.height, pCreateInfo->mipLevel), |
| 584 | .depth = anv_minify(image->extent.depth, pCreateInfo->mipLevel), |
| 585 | }; |
| 586 | |
| 587 | uint32_t depth = 1; |
| 588 | if (pCreateInfo->arraySize > 1) { |
| 589 | depth = pCreateInfo->arraySize; |
| 590 | } else if (image->extent.depth > 1) { |
| 591 | depth = image->extent.depth; |
| 592 | } |
| 593 | |
Chad Versace | fc38383 | 2015-07-17 13:38:09 -0700 | [diff] [blame] | 594 | if (cmd_buffer) { |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 595 | view->surface_state = |
| 596 | anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64); |
Chad Versace | fc38383 | 2015-07-17 13:38:09 -0700 | [diff] [blame] | 597 | } else { |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 598 | view->surface_state = |
| 599 | anv_state_pool_alloc(&device->surface_state_pool, 64, 64); |
Chad Versace | fc38383 | 2015-07-17 13:38:09 -0700 | [diff] [blame] | 600 | } |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 601 | |
| 602 | struct GEN8_RENDER_SURFACE_STATE surface_state = { |
| 603 | .SurfaceType = SURFTYPE_2D, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 604 | .SurfaceArray = image->array_size > 1, |
Chad Versace | 60c4ac5 | 2015-08-17 13:26:28 -0700 | [diff] [blame] | 605 | .SurfaceFormat = view->format->surface_format, |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 606 | .SurfaceVerticalAlignment = anv_valign[surface->v_align], |
| 607 | .SurfaceHorizontalAlignment = anv_halign[surface->h_align], |
| 608 | .TileMode = surface->tile_mode, |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 609 | .VerticalLineStride = 0, |
| 610 | .VerticalLineStrideOffset = 0, |
| 611 | .SamplerL2BypassModeDisable = true, |
| 612 | .RenderCacheReadWriteMode = WriteOnlyCache, |
| 613 | .MemoryObjectControlState = GEN8_MOCS, |
Chad Versace | 4fcb32a | 2015-07-08 14:39:05 -0700 | [diff] [blame] | 614 | |
| 615 | /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in |
| 616 | * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have |
| 617 | * both Base Mip Level fields nonzero". |
| 618 | */ |
Kristian Høgsberg Kristensen | a5b49d2 | 2015-06-10 23:11:37 -0700 | [diff] [blame] | 619 | .BaseMipLevel = 0.0, |
Chad Versace | 4fcb32a | 2015-07-08 14:39:05 -0700 | [diff] [blame] | 620 | |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 621 | .SurfaceQPitch = surface->qpitch >> 2, |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 622 | .Height = image->extent.height - 1, |
| 623 | .Width = image->extent.width - 1, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 624 | .Depth = depth - 1, |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 625 | .SurfacePitch = surface->stride - 1, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 626 | .MinimumArrayElement = pCreateInfo->baseArraySlice, |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 627 | .NumberofMultisamples = MULTISAMPLECOUNT_1, |
| 628 | .XOffset = 0, |
| 629 | .YOffset = 0, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 630 | |
| 631 | /* For render target surfaces, the hardware interprets field MIPCount/LOD as |
| 632 | * LOD. The Broadwell PRM says: |
| 633 | * |
| 634 | * MIPCountLOD defines the LOD that will be rendered into. |
| 635 | * SurfaceMinLOD is ignored. |
| 636 | */ |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 637 | .SurfaceMinLOD = 0, |
Chad Versace | 5b3a1ce | 2015-06-26 21:27:54 -0700 | [diff] [blame] | 638 | .MIPCountLOD = pCreateInfo->mipLevel, |
| 639 | |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 640 | .AuxiliarySurfaceMode = AUX_NONE, |
| 641 | .RedClearColor = 0, |
| 642 | .GreenClearColor = 0, |
| 643 | .BlueClearColor = 0, |
| 644 | .AlphaClearColor = 0, |
| 645 | .ShaderChannelSelectRed = SCS_RED, |
| 646 | .ShaderChannelSelectGreen = SCS_GREEN, |
| 647 | .ShaderChannelSelectBlue = SCS_BLUE, |
| 648 | .ShaderChannelSelectAlpha = SCS_ALPHA, |
Kristian Høgsberg Kristensen | a5b49d2 | 2015-06-10 23:11:37 -0700 | [diff] [blame] | 649 | .ResourceMinLOD = 0.0, |
Kristian Høgsberg | 0dbed61 | 2015-05-25 22:12:24 -0700 | [diff] [blame] | 650 | .SurfaceBaseAddress = { NULL, view->offset }, |
| 651 | }; |
| 652 | |
| 653 | GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state); |
Jason Ekstrand | 4668bbb | 2015-05-18 20:42:03 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 656 | static void |
| 657 | anv_depth_stencil_view_init(struct anv_depth_stencil_view *view, |
| 658 | const VkAttachmentViewCreateInfo *pCreateInfo) |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 659 | { |
Jason Ekstrand | a52e208 | 2015-07-09 20:24:07 -0700 | [diff] [blame] | 660 | ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image); |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 661 | struct anv_surface *depth_surface = &image->primary_surface; |
| 662 | struct anv_surface *stencil_surface = &image->stencil_surface; |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 663 | |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 664 | view->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL; |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 665 | |
Jason Ekstrand | 2a3c296 | 2015-06-10 21:04:51 -0700 | [diff] [blame] | 666 | /* XXX: We don't handle any of these */ |
| 667 | anv_assert(pCreateInfo->mipLevel == 0); |
| 668 | anv_assert(pCreateInfo->baseArraySlice == 0); |
| 669 | anv_assert(pCreateInfo->arraySize == 1); |
Jason Ekstrand | 2a3c296 | 2015-06-10 21:04:51 -0700 | [diff] [blame] | 670 | |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 671 | view->bo = image->bo; |
| 672 | |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 673 | view->depth_stride = depth_surface->stride; |
| 674 | view->depth_offset = image->offset + depth_surface->offset; |
Chad Versace | ded736f | 2015-08-17 13:10:40 -0700 | [diff] [blame] | 675 | view->depth_format = image->format->depth_format; |
Chad Versace | 7ea707a | 2015-06-25 19:46:42 -0700 | [diff] [blame] | 676 | view->depth_qpitch = 0; /* FINISHME: QPitch */ |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 677 | |
Chad Versace | c6e76ae | 2015-06-26 18:48:34 -0700 | [diff] [blame] | 678 | view->stencil_stride = stencil_surface->stride; |
| 679 | view->stencil_offset = image->offset + stencil_surface->offset; |
Chad Versace | 7ea707a | 2015-06-25 19:46:42 -0700 | [diff] [blame] | 680 | view->stencil_qpitch = 0; /* FINISHME: QPitch */ |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 681 | } |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 682 | |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 683 | VkResult |
| 684 | anv_CreateAttachmentView(VkDevice _device, |
| 685 | const VkAttachmentViewCreateInfo *pCreateInfo, |
| 686 | VkAttachmentView *pView) |
| 687 | { |
| 688 | ANV_FROM_HANDLE(anv_device, device, _device); |
| 689 | |
| 690 | assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO); |
| 691 | |
| 692 | if (anv_is_vk_format_depth_or_stencil(pCreateInfo->format)) { |
| 693 | struct anv_depth_stencil_view *view = |
| 694 | anv_device_alloc(device, sizeof(*view), 8, |
| 695 | VK_SYSTEM_ALLOC_TYPE_API_OBJECT); |
| 696 | if (view == NULL) |
| 697 | return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 698 | |
| 699 | anv_depth_stencil_view_init(view, pCreateInfo); |
| 700 | |
| 701 | *pView = anv_attachment_view_to_handle(&view->base); |
| 702 | } else { |
| 703 | struct anv_color_attachment_view *view = |
| 704 | anv_device_alloc(device, sizeof(*view), 8, |
| 705 | VK_SYSTEM_ALLOC_TYPE_API_OBJECT); |
| 706 | if (view == NULL) |
| 707 | return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 708 | |
| 709 | anv_color_attachment_view_init(view, device, pCreateInfo, NULL); |
| 710 | |
| 711 | *pView = anv_attachment_view_to_handle(&view->base); |
| 712 | } |
Kristian Høgsberg | 37743f9 | 2015-05-22 22:59:12 -0700 | [diff] [blame] | 713 | |
| 714 | return VK_SUCCESS; |
Kristian Høgsberg | 769785c | 2015-05-08 22:32:37 -0700 | [diff] [blame] | 715 | } |
Jason Ekstrand | b94b8df | 2015-07-10 12:25:30 -0700 | [diff] [blame] | 716 | |
| 717 | VkResult |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 718 | anv_DestroyAttachmentView(VkDevice _device, VkAttachmentView _view) |
Jason Ekstrand | b94b8df | 2015-07-10 12:25:30 -0700 | [diff] [blame] | 719 | { |
| 720 | ANV_FROM_HANDLE(anv_device, device, _device); |
Jason Ekstrand | 8478350 | 2015-07-10 20:18:52 -0700 | [diff] [blame] | 721 | ANV_FROM_HANDLE(anv_attachment_view, view, _view); |
| 722 | |
| 723 | if (view->attachment_type == ANV_ATTACHMENT_VIEW_TYPE_COLOR) { |
| 724 | struct anv_color_attachment_view *aview = |
| 725 | (struct anv_color_attachment_view *)view; |
| 726 | |
| 727 | anv_surface_view_fini(device, &aview->view); |
| 728 | } |
Jason Ekstrand | b94b8df | 2015-07-10 12:25:30 -0700 | [diff] [blame] | 729 | |
| 730 | anv_device_free(device, view); |
| 731 | |
| 732 | return VK_SUCCESS; |
| 733 | } |