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