blob: 2045aa27219b3b453e0287d4b991021f30537bbe [file] [log] [blame]
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001/*
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 Versace2c2233e2015-07-17 15:04:27 -070030#include "anv_private.h"
Kristian Høgsberg769785c2015-05-08 22:32:37 -070031
Chad Versacee6162c22015-06-09 12:11:46 -070032static const uint8_t anv_halign[] = {
33 [4] = HALIGN4,
34 [8] = HALIGN8,
35 [16] = HALIGN16,
36};
37
38static const uint8_t anv_valign[] = {
39 [4] = VALIGN4,
40 [8] = VALIGN8,
41 [16] = VALIGN16,
42};
43
Chad Versacecb30aca2015-05-28 07:37:59 -070044static const uint8_t anv_surf_type_from_image_type[] = {
45 [VK_IMAGE_TYPE_1D] = SURFTYPE_1D,
46 [VK_IMAGE_TYPE_2D] = SURFTYPE_2D,
47 [VK_IMAGE_TYPE_3D] = SURFTYPE_3D,
Chad Versace69e11ad2015-07-06 16:25:59 -070048
Chad Versacecb30aca2015-05-28 07:37:59 -070049};
50
Chad Versace69e11ad2015-07-06 16:25:59 -070051static const struct anv_image_view_info
52anv_image_view_info_table[] = {
53 #define INFO(s, ...) { .surface_type = s, __VA_ARGS__ }
54 [VK_IMAGE_VIEW_TYPE_1D] = INFO(SURFTYPE_1D),
55 [VK_IMAGE_VIEW_TYPE_2D] = INFO(SURFTYPE_2D),
56 [VK_IMAGE_VIEW_TYPE_3D] = INFO(SURFTYPE_3D),
57 [VK_IMAGE_VIEW_TYPE_CUBE] = INFO(SURFTYPE_CUBE, .is_cube = 1),
58 [VK_IMAGE_VIEW_TYPE_1D_ARRAY] = INFO(SURFTYPE_1D, .is_array = 1),
59 [VK_IMAGE_VIEW_TYPE_2D_ARRAY] = INFO(SURFTYPE_2D, .is_array = 1),
60 [VK_IMAGE_VIEW_TYPE_CUBE_ARRAY] = INFO(SURFTYPE_CUBE, .is_array = 1, .is_cube = 1),
61 #undef INFO
Chad Versacecb30aca2015-05-28 07:37:59 -070062};
63
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -070064const struct anv_image_view_info *
65anv_image_view_info_for_vk_image_view_type(VkImageViewType type)
66{
67 return &anv_image_view_info_table[type];
68}
69
Chad Versacefa352962015-05-28 07:40:22 -070070static const struct anv_surf_type_limits {
71 int32_t width;
72 int32_t height;
73 int32_t depth;
74} anv_surf_type_limits[] = {
Chad Versace622a3172015-09-14 14:37:09 -070075 [SURFTYPE_1D] = {16384, 1, 2048},
Chad Versacefa352962015-05-28 07:40:22 -070076 [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 Versacee6bd5682015-06-09 13:29:08 -070083static 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øgsberg769785c2015-05-08 22:32:37 -0700104};
105
Chad Versace053d32d2015-08-28 08:04:59 -0700106/**
107 * Return -1 on failure.
108 */
109static int8_t
Chad Versacefdcd71f2015-06-26 20:06:08 -0700110anv_image_choose_tile_mode(const struct anv_image_create_info *anv_info)
Chad Versacef1db3b32015-06-09 14:33:05 -0700111{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700112 if (anv_info->force_tile_mode)
Chad Versacef1db3b32015-06-09 14:33:05 -0700113 return anv_info->tile_mode;
114
Chad Versace053d32d2015-08-28 08:04:59 -0700115 /* The Sandybridge PRM says that the stencil buffer "is supported
116 * only in Tile W memory".
117 */
Chad Versacec6e76ae2015-06-26 18:48:34 -0700118
Chad Versacefdcd71f2015-06-26 20:06:08 -0700119 switch (anv_info->vk_info->tiling) {
Chad Versacef1db3b32015-06-09 14:33:05 -0700120 case VK_IMAGE_TILING_LINEAR:
Chad Versace053d32d2015-08-28 08:04:59 -0700121 if (unlikely(anv_info->vk_info->format == VK_FORMAT_S8_UINT)) {
122 return -1;
123 } else {
124 return LINEAR;
125 }
Chad Versacef1db3b32015-06-09 14:33:05 -0700126 case VK_IMAGE_TILING_OPTIMAL:
Chad Versace053d32d2015-08-28 08:04:59 -0700127 if (unlikely(anv_info->vk_info->format == VK_FORMAT_S8_UINT)) {
128 return WMAJOR;
129 } else {
130 return YMAJOR;
131 }
Chad Versacef1db3b32015-06-09 14:33:05 -0700132 default:
133 assert(!"bad VKImageTiling");
134 return LINEAR;
135 }
136}
137
Chad Versace5a6b2e62015-08-17 13:50:43 -0700138
139/**
140 * The \a format argument is required and overrides any format in
141 * struct anv_image_create_info.
142 */
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700143static VkResult
Chad Versacec6e76ae2015-06-26 18:48:34 -0700144anv_image_make_surface(const struct anv_image_create_info *create_info,
Chad Versace5a6b2e62015-08-17 13:50:43 -0700145 const struct anv_format *format,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700146 uint64_t *inout_image_size,
147 uint32_t *inout_image_alignment,
148 struct anv_surface *out_surface)
149{
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700150 /* See RENDER_SURFACE_STATE.SurfaceQPitch */
151 static const uint16_t min_qpitch UNUSED = 0x4;
152 static const uint16_t max_qpitch UNUSED = 0x1ffc;
153
Chad Versacec6e76ae2015-06-26 18:48:34 -0700154 const VkExtent3D *restrict extent = &create_info->vk_info->extent;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700155 const uint32_t levels = create_info->vk_info->mipLevels;
156 const uint32_t array_size = create_info->vk_info->arraySize;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700157
Chad Versace053d32d2015-08-28 08:04:59 -0700158 const int8_t tile_mode = anv_image_choose_tile_mode(create_info);
159 if (tile_mode == -1)
160 return vk_error(VK_ERROR_INVALID_IMAGE);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700161
162 const struct anv_tile_info *tile_info =
163 &anv_tile_info_table[tile_mode];
164
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700165 const uint32_t i = 4; /* FINISHME: Stop hardcoding subimage alignment */
166 const uint32_t j = 4; /* FINISHME: Stop hardcoding subimage alignment */
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700167
Chad Versacee01d5a02015-09-14 10:58:43 -0700168 uint16_t qpitch = min_qpitch;
169 uint32_t mt_width = 0;
170 uint32_t mt_height = 0;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700171
Chad Versacee01d5a02015-09-14 10:58:43 -0700172 switch (create_info->vk_info->imageType) {
173 case VK_IMAGE_TYPE_1D:
Chad Versace622a3172015-09-14 14:37:09 -0700174 /* From the Broadwell PRM >> Memory Views >> Common Surface Formats >>
175 * Surface Layout >> 1D Surfaces:
176 *
177 * One-dimensional surfaces are identical to 2D surfaces with height of one.
178 *
179 * So fallthrough...
180 */
Chad Versacee01d5a02015-09-14 10:58:43 -0700181 case VK_IMAGE_TYPE_2D: {
182 const uint32_t w0 = align_u32(extent->width, i);
183 const uint32_t h0 = align_u32(extent->height, j);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700184
Chad Versacee01d5a02015-09-14 10:58:43 -0700185 if (levels == 1 && array_size == 1) {
186 qpitch = min_qpitch;
187 mt_width = w0;
188 mt_height = h0;
189 } else {
190 uint32_t w1 = align_u32(anv_minify(extent->width, 1), i);
191 uint32_t h1 = align_u32(anv_minify(extent->height, 1), j);
192 uint32_t w2 = align_u32(anv_minify(extent->width, 2), i);
193
194 /* The QPitch equation is found in the Broadwell PRM >> Volume 5: Memory
195 * Views >> Common Surface Formats >> Surface Layout >> 2D Surfaces >>
196 * Surface Arrays >> For All Surface Other Than Separate Stencil Buffer:
197 */
198 qpitch = h0 + h1 + 11 * j;
199 mt_width = MAX(w0, w1 + w2);
200 mt_height = array_size * qpitch;
201 }
202 break;
203 }
204 case VK_IMAGE_TYPE_3D:
Chad Versaceeed74e32015-09-14 11:04:08 -0700205 /* The layout of 3D surfaces is described by the Broadwell PRM >>
206 * Volume 5: Memory Views >> Common Surface Formats >> Surface Layout >>
207 * 3D Surfaces.
208 */
209 for (uint32_t l = 0; l < levels; ++l) {
210 const uint32_t w_l = align_u32(anv_minify(extent->width, l), i);
211 const uint32_t h_l = align_u32(anv_minify(extent->height, l), j);
212 const uint32_t d_l = anv_minify(extent->depth, l);
213
214 const uint32_t max_layers_horiz = MIN(d_l, 1u << l);
215 const uint32_t max_layers_vert = align_u32(d_l, 1u << l) / (1u << l);
216
217 mt_width = MAX(mt_width, w_l * max_layers_horiz);
218 mt_height += h_l * max_layers_vert;
219 }
Chad Versacee01d5a02015-09-14 10:58:43 -0700220 break;
221 default:
222 unreachable(!"bad VkImageType");
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700223 }
224
225 assert(qpitch >= min_qpitch);
226 if (qpitch > max_qpitch) {
227 anv_loge("image qpitch > 0x%x\n", max_qpitch);
228 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
229 }
230
231 /* From the Broadwell PRM, RENDER_SURFACE_STATE.SurfaceQpitch:
232 *
233 * This field must be set an integer multiple of the Surface Vertical
234 * Alignment.
235 */
236 assert(anv_is_aligned(qpitch, j));
237
Chad Versace5a6b2e62015-08-17 13:50:43 -0700238 uint32_t stride = align_u32(mt_width * format->cpp, tile_info->width);
Kristian Høgsberg Kristensen6d09d062015-08-14 10:02:19 -0700239 if (create_info->stride > 0)
240 stride = create_info->stride;
241
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700242 const uint32_t size = stride * align_u32(mt_height, tile_info->height);
243 const uint32_t offset = align_u32(*inout_image_size,
244 tile_info->surface_alignment);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700245
246 *inout_image_size = offset + size;
247 *inout_image_alignment = MAX(*inout_image_alignment,
248 tile_info->surface_alignment);
249
250 *out_surface = (struct anv_surface) {
251 .offset = offset,
252 .stride = stride,
253 .tile_mode = tile_mode,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700254 .qpitch = qpitch,
255 .h_align = i,
256 .v_align = j,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700257 };
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700258
259 return VK_SUCCESS;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700260}
261
Chad Versace127cb3f2015-06-26 20:12:42 -0700262VkResult
263anv_image_create(VkDevice _device,
264 const struct anv_image_create_info *create_info,
265 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700266{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700267 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versacefdcd71f2015-06-26 20:06:08 -0700268 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace67a76592015-06-26 09:17:52 -0700269 const VkExtent3D *restrict extent = &pCreateInfo->extent;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700270 struct anv_image *image = NULL;
271 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700272
273 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
274
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700275 anv_assert(pCreateInfo->mipLevels > 0);
276 anv_assert(pCreateInfo->arraySize > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700277 anv_assert(pCreateInfo->samples == 1);
Chad Versace5d7103e2015-06-26 09:05:46 -0700278 anv_assert(pCreateInfo->extent.width > 0);
279 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700280 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700281
Chad Versace7ea12162015-05-28 07:45:31 -0700282 /* TODO(chadv): How should we validate inputs? */
Chad Versace67a76592015-06-26 09:17:52 -0700283 const uint8_t surf_type =
284 anv_surf_type_from_image_type[pCreateInfo->imageType];
Chad Versace7ea12162015-05-28 07:45:31 -0700285
Chad Versacefa352962015-05-28 07:40:22 -0700286 const struct anv_surf_type_limits *limits =
Chad Versace67a76592015-06-26 09:17:52 -0700287 &anv_surf_type_limits[surf_type];
Chad Versacefa352962015-05-28 07:40:22 -0700288
Chad Versace67a76592015-06-26 09:17:52 -0700289 if (extent->width > limits->width ||
290 extent->height > limits->height ||
291 extent->depth > limits->depth) {
Chad Versacefa352962015-05-28 07:40:22 -0700292 /* TODO(chadv): What is the correct error? */
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700293 return vk_errorf(VK_ERROR_INVALID_MEMORY_SIZE, "image extent is too large");
Chad Versacefa352962015-05-28 07:40:22 -0700294 }
295
Chad Versacec6e76ae2015-06-26 18:48:34 -0700296 image = anv_device_alloc(device, sizeof(*image), 8,
297 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
298 if (!image)
299 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
Chad Versace67a76592015-06-26 09:17:52 -0700300
Chad Versacec6e76ae2015-06-26 18:48:34 -0700301 memset(image, 0, sizeof(*image));
302 image->type = pCreateInfo->imageType;
303 image->extent = pCreateInfo->extent;
Chad Versaceded736f2015-08-17 13:10:40 -0700304 image->format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700305 image->levels = pCreateInfo->mipLevels;
306 image->array_size = pCreateInfo->arraySize;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700307 image->surf_type = surf_type;
Chad Versace67a76592015-06-26 09:17:52 -0700308
Chad Versace941b48e2015-08-28 07:08:58 -0700309 if (likely(anv_format_is_color(image->format))) {
Chad Versace5a6b2e62015-08-17 13:50:43 -0700310 r = anv_image_make_surface(create_info, image->format,
311 &image->size, &image->alignment,
Chad Versace941b48e2015-08-28 07:08:58 -0700312 &image->color_surface);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700313 if (r != VK_SUCCESS)
314 goto fail;
Chad Versace941b48e2015-08-28 07:08:58 -0700315 } else {
316 if (image->format->depth_format) {
317 r = anv_image_make_surface(create_info, image->format,
318 &image->size, &image->alignment,
319 &image->depth_surface);
320 if (r != VK_SUCCESS)
321 goto fail;
322 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700323
Chad Versace941b48e2015-08-28 07:08:58 -0700324 if (image->format->has_stencil) {
325 r = anv_image_make_surface(create_info, anv_format_s8_uint,
326 &image->size, &image->alignment,
327 &image->stencil_surface);
328 if (r != VK_SUCCESS)
329 goto fail;
330 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700331 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700332
Jason Ekstranda52e2082015-07-09 20:24:07 -0700333 *pImage = anv_image_to_handle(image);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700334
335 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700336
337fail:
338 if (image)
339 anv_device_free(device, image);
340
341 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700342}
343
Chad Versace127cb3f2015-06-26 20:12:42 -0700344VkResult
345anv_CreateImage(VkDevice device,
346 const VkImageCreateInfo *pCreateInfo,
347 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700348{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700349 return anv_image_create(device,
350 &(struct anv_image_create_info) {
351 .vk_info = pCreateInfo,
352 },
353 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700354}
355
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700356VkResult
357anv_DestroyImage(VkDevice _device, VkImage _image)
358{
359 ANV_FROM_HANDLE(anv_device, device, _device);
360
361 anv_device_free(device, anv_image_from_handle(_image));
362
363 return VK_SUCCESS;
364}
365
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700366VkResult anv_GetImageSubresourceLayout(
367 VkDevice device,
368 VkImage image,
369 const VkImageSubresource* pSubresource,
370 VkSubresourceLayout* pLayout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700371{
Jason Ekstrandffe9f602015-05-12 13:44:43 -0700372 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700373}
374
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700375void
Jason Ekstrand84783502015-07-10 20:18:52 -0700376anv_surface_view_fini(struct anv_device *device,
377 struct anv_surface_view *view)
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700378{
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700379 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700380}
381
Chad Versace127cb3f2015-06-26 20:12:42 -0700382VkResult
Chad Versace5b04db72015-07-06 16:24:28 -0700383anv_validate_CreateImageView(VkDevice _device,
384 const VkImageViewCreateInfo *pCreateInfo,
385 VkImageView *pView)
386{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700387 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace23075bc2015-07-06 17:04:13 -0700388 const VkImageSubresourceRange *subresource;
389 const struct anv_image_view_info *view_info;
390 const struct anv_format *view_format_info;
Chad Versace5b04db72015-07-06 16:24:28 -0700391
Chad Versace23075bc2015-07-06 17:04:13 -0700392 /* Validate structure type before dereferencing it. */
Chad Versace5b04db72015-07-06 16:24:28 -0700393 assert(pCreateInfo);
394 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chad Versace23075bc2015-07-06 17:04:13 -0700395 subresource = &pCreateInfo->subresourceRange;
396
Chad Versace23075bc2015-07-06 17:04:13 -0700397 /* Validate viewType is in range before using it. */
398 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
399 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
400 view_info = &anv_image_view_info_table[pCreateInfo->viewType];
401
402 /* Validate format is in range before using it. */
403 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
404 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700405 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
406
407 /* Validate channel swizzles. */
408 assert(pCreateInfo->channels.r >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
409 assert(pCreateInfo->channels.r <= VK_CHANNEL_SWIZZLE_END_RANGE);
410 assert(pCreateInfo->channels.g >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
411 assert(pCreateInfo->channels.g <= VK_CHANNEL_SWIZZLE_END_RANGE);
412 assert(pCreateInfo->channels.b >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
413 assert(pCreateInfo->channels.b <= VK_CHANNEL_SWIZZLE_END_RANGE);
414 assert(pCreateInfo->channels.a >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
415 assert(pCreateInfo->channels.a <= VK_CHANNEL_SWIZZLE_END_RANGE);
416
417 /* Validate subresource. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700418 assert(subresource->aspectMask != 0);
Chad Versace23075bc2015-07-06 17:04:13 -0700419 assert(subresource->mipLevels > 0);
420 assert(subresource->arraySize > 0);
421 assert(subresource->baseMipLevel < image->levels);
422 assert(subresource->baseMipLevel + subresource->mipLevels <= image->levels);
423 assert(subresource->baseArraySlice < image->array_size);
424 assert(subresource->baseArraySlice + subresource->arraySize <= image->array_size);
Chad Versace5b04db72015-07-06 16:24:28 -0700425 assert(pView);
426
Chad Versace23075bc2015-07-06 17:04:13 -0700427 if (view_info->is_cube) {
428 assert(subresource->baseArraySlice % 6 == 0);
429 assert(subresource->arraySize % 6 == 0);
430 }
Chad Versace5b04db72015-07-06 16:24:28 -0700431
Chad Versace7a089bd2015-10-05 06:48:14 -0700432 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
433 | VK_IMAGE_ASPECT_STENCIL_BIT;
434
Chad Versace23075bc2015-07-06 17:04:13 -0700435 /* Validate format. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700436 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
437 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versaceded736f2015-08-17 13:10:40 -0700438 assert(!image->format->depth_format);
439 assert(!image->format->has_stencil);
Chad Versace23075bc2015-07-06 17:04:13 -0700440 assert(!view_format_info->depth_format);
441 assert(!view_format_info->has_stencil);
Chad Versaceded736f2015-08-17 13:10:40 -0700442 assert(view_format_info->cpp == image->format->cpp);
Chad Versace7a089bd2015-10-05 06:48:14 -0700443 } else if (subresource->aspectMask & ds_flags) {
444 assert((subresource->aspectMask & ~ds_flags) == 0);
445
446 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
447 assert(image->format->depth_format);
448 assert(view_format_info->depth_format);
449 assert(view_format_info->cpp == image->format->cpp);
450 }
451
452 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL) {
453 /* FINISHME: Is it legal to have an R8 view of S8? */
454 assert(image->format->has_stencil);
455 assert(view_format_info->has_stencil);
456 }
457 } else {
458 assert(!"bad VkImageSubresourceRange::aspectFlags");
Chad Versace23075bc2015-07-06 17:04:13 -0700459 }
Chad Versace5b04db72015-07-06 16:24:28 -0700460
461 return anv_CreateImageView(_device, pCreateInfo, pView);
462}
463
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700464void
465anv_image_view_init(struct anv_image_view *iview,
466 struct anv_device *device,
467 const VkImageViewCreateInfo* pCreateInfo,
468 struct anv_cmd_buffer *cmd_buffer)
469{
470 switch (device->info.gen) {
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700471 case 7:
472 gen7_image_view_init(iview, device, pCreateInfo, cmd_buffer);
473 break;
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700474 case 8:
475 gen8_image_view_init(iview, device, pCreateInfo, cmd_buffer);
476 break;
477 default:
478 unreachable("unsupported gen\n");
479 }
480}
481
Chad Versace5b04db72015-07-06 16:24:28 -0700482VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700483anv_CreateImageView(VkDevice _device,
484 const VkImageViewCreateInfo *pCreateInfo,
485 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700486{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700487 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700488 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700489
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700490 view = anv_device_alloc(device, sizeof(*view), 8,
491 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
492 if (view == NULL)
493 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
494
495 anv_image_view_init(view, device, pCreateInfo, NULL);
496
497 *pView = anv_image_view_to_handle(view);
498
499 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700500}
501
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700502VkResult
Chad Versace8213be72015-07-15 12:00:27 -0700503anv_DestroyImageView(VkDevice _device, VkImageView _iview)
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700504{
505 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versace8213be72015-07-15 12:00:27 -0700506 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700507
Chad Versace8213be72015-07-15 12:00:27 -0700508 anv_surface_view_fini(device, &iview->view);
509 anv_device_free(device, iview);
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700510
511 return VK_SUCCESS;
512}
513
Jason Ekstrand84783502015-07-10 20:18:52 -0700514static void
515anv_depth_stencil_view_init(struct anv_depth_stencil_view *view,
516 const VkAttachmentViewCreateInfo *pCreateInfo)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700517{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700518 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700519
Jason Ekstrand84783502015-07-10 20:18:52 -0700520 view->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700521
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700522 /* XXX: We don't handle any of these */
523 anv_assert(pCreateInfo->mipLevel == 0);
524 anv_assert(pCreateInfo->baseArraySlice == 0);
525 anv_assert(pCreateInfo->arraySize == 1);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700526
Chad Versacec6f19b42015-08-28 07:52:19 -0700527 view->image = image;
Chad Versaceb2ee3172015-08-28 07:44:32 -0700528 view->format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versacec6f19b42015-08-28 07:52:19 -0700529
530 assert(anv_format_is_depth_or_stencil(image->format));
Chad Versaceb2ee3172015-08-28 07:44:32 -0700531 assert(anv_format_is_depth_or_stencil(view->format));
Jason Ekstrand84783502015-07-10 20:18:52 -0700532}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700533
Chad Versace941b48e2015-08-28 07:08:58 -0700534struct anv_surface *
Chad Versace7a089bd2015-10-05 06:48:14 -0700535anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
Chad Versace941b48e2015-08-28 07:08:58 -0700536{
Chad Versace7a089bd2015-10-05 06:48:14 -0700537 switch (aspect_mask) {
538 case VK_IMAGE_ASPECT_COLOR_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700539 assert(anv_format_is_color(image->format));
540 return &image->color_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700541 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700542 assert(image->format->depth_format);
543 return &image->depth_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700544 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700545 assert(image->format->has_stencil);
546 anv_finishme("stencil image views");
Chad Versace941b48e2015-08-28 07:08:58 -0700547 return &image->stencil_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700548 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
549 /* FINISHME: Support combined depthstencil aspect. Does the Vulkan spec
550 * allow is to reject it? Until we support it, filter out the stencil
551 * aspect and use only the depth aspect.
552 */
553 anv_finishme("combined depthstencil aspect");
554 assert(image->format->depth_format);
555 return &image->depth_surface;
Chad Versace941b48e2015-08-28 07:08:58 -0700556 default:
557 unreachable("image does not have aspect");
558 return NULL;
559 }
560}
561
562/** The attachment may be a color view into a non-color image. */
563struct anv_surface *
564anv_image_get_surface_for_color_attachment(struct anv_image *image)
565{
566 if (anv_format_is_color(image->format)) {
567 return &image->color_surface;
568 } else if (image->format->depth_format) {
569 return &image->depth_surface;
570 } else if (image->format->has_stencil) {
571 return &image->stencil_surface;
572 } else {
573 unreachable("image has bad format");
574 return NULL;
575 }
576}
577
Kristian Høgsberg Kristensenf5275f72015-08-19 22:19:21 -0700578void
579anv_color_attachment_view_init(struct anv_color_attachment_view *aview,
580 struct anv_device *device,
581 const VkAttachmentViewCreateInfo* pCreateInfo,
582 struct anv_cmd_buffer *cmd_buffer)
583{
584 switch (device->info.gen) {
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700585 case 7:
586 gen7_color_attachment_view_init(aview, device, pCreateInfo, cmd_buffer);
587 break;
Kristian Høgsberg Kristensenf5275f72015-08-19 22:19:21 -0700588 case 8:
589 gen8_color_attachment_view_init(aview, device, pCreateInfo, cmd_buffer);
590 break;
591 default:
592 unreachable("unsupported gen\n");
593 }
594}
595
Jason Ekstrand84783502015-07-10 20:18:52 -0700596VkResult
597anv_CreateAttachmentView(VkDevice _device,
598 const VkAttachmentViewCreateInfo *pCreateInfo,
599 VkAttachmentView *pView)
600{
601 ANV_FROM_HANDLE(anv_device, device, _device);
602
603 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO);
604
Chad Versace6ff95bb2015-08-17 14:03:52 -0700605 const struct anv_format *format =
606 anv_format_for_vk_format(pCreateInfo->format);
607
608 if (anv_format_is_depth_or_stencil(format)) {
Jason Ekstrand84783502015-07-10 20:18:52 -0700609 struct anv_depth_stencil_view *view =
610 anv_device_alloc(device, sizeof(*view), 8,
611 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
612 if (view == NULL)
613 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
614
615 anv_depth_stencil_view_init(view, pCreateInfo);
616
617 *pView = anv_attachment_view_to_handle(&view->base);
618 } else {
619 struct anv_color_attachment_view *view =
620 anv_device_alloc(device, sizeof(*view), 8,
621 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
622 if (view == NULL)
623 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
624
625 anv_color_attachment_view_init(view, device, pCreateInfo, NULL);
626
627 *pView = anv_attachment_view_to_handle(&view->base);
628 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700629
630 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700631}
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700632
633VkResult
Jason Ekstrand84783502015-07-10 20:18:52 -0700634anv_DestroyAttachmentView(VkDevice _device, VkAttachmentView _view)
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700635{
636 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrand84783502015-07-10 20:18:52 -0700637 ANV_FROM_HANDLE(anv_attachment_view, view, _view);
638
639 if (view->attachment_type == ANV_ATTACHMENT_VIEW_TYPE_COLOR) {
640 struct anv_color_attachment_view *aview =
641 (struct anv_color_attachment_view *)view;
642
643 anv_surface_view_fini(device, &aview->view);
644 }
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700645
646 anv_device_free(device, view);
647
648 return VK_SUCCESS;
649}