blob: 3b706c6844336d47b50aac77e30d5311e177614d [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 Versace69e11ad2015-07-06 16:25:59 -070032struct 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 Versacee6162c22015-06-09 12:11:46 -070038static const uint8_t anv_halign[] = {
39 [4] = HALIGN4,
40 [8] = HALIGN8,
41 [16] = HALIGN16,
42};
43
44static const uint8_t anv_valign[] = {
45 [4] = VALIGN4,
46 [8] = VALIGN8,
47 [16] = VALIGN16,
48};
49
Chad Versacecb30aca2015-05-28 07:37:59 -070050static 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 Versace69e11ad2015-07-06 16:25:59 -070054
Chad Versacecb30aca2015-05-28 07:37:59 -070055};
56
Chad Versace69e11ad2015-07-06 16:25:59 -070057static const struct anv_image_view_info
58anv_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 Versacecb30aca2015-05-28 07:37:59 -070068};
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[] = {
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 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 Versacef1db3b32015-06-09 14:33:05 -0700106static uint32_t
Chad Versacefdcd71f2015-06-26 20:06:08 -0700107anv_image_choose_tile_mode(const struct anv_image_create_info *anv_info)
Chad Versacef1db3b32015-06-09 14:33:05 -0700108{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700109 if (anv_info->force_tile_mode)
Chad Versacef1db3b32015-06-09 14:33:05 -0700110 return anv_info->tile_mode;
111
Chad Versacec6e76ae2015-06-26 18:48:34 -0700112 if (anv_info->vk_info->format == VK_FORMAT_S8_UINT)
113 return WMAJOR;
114
Chad Versacefdcd71f2015-06-26 20:06:08 -0700115 switch (anv_info->vk_info->tiling) {
Chad Versacef1db3b32015-06-09 14:33:05 -0700116 case VK_IMAGE_TILING_LINEAR:
117 return LINEAR;
118 case VK_IMAGE_TILING_OPTIMAL:
Kristian Høgsberg Kristensen00494c62015-06-11 22:07:16 -0700119 return YMAJOR;
Chad Versacef1db3b32015-06-09 14:33:05 -0700120 default:
121 assert(!"bad VKImageTiling");
122 return LINEAR;
123 }
124}
125
Chad Versace5a6b2e62015-08-17 13:50:43 -0700126
127/**
128 * The \a format argument is required and overrides any format in
129 * struct anv_image_create_info.
130 */
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700131static VkResult
Chad Versacec6e76ae2015-06-26 18:48:34 -0700132anv_image_make_surface(const struct anv_image_create_info *create_info,
Chad Versace5a6b2e62015-08-17 13:50:43 -0700133 const struct anv_format *format,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700134 uint64_t *inout_image_size,
135 uint32_t *inout_image_alignment,
136 struct anv_surface *out_surface)
137{
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700138 /* 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 Versacec6e76ae2015-06-26 18:48:34 -0700142 const VkExtent3D *restrict extent = &create_info->vk_info->extent;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700143 const uint32_t levels = create_info->vk_info->mipLevels;
144 const uint32_t array_size = create_info->vk_info->arraySize;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700145
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 Versace5b3a1ce2015-06-26 21:27:54 -0700151 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 Versace5a6b2e62015-08-17 13:50:43 -0700187 uint32_t stride = align_u32(mt_width * format->cpp, tile_info->width);
Kristian Høgsberg Kristensen6d09d062015-08-14 10:02:19 -0700188 if (create_info->stride > 0)
189 stride = create_info->stride;
190
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700191 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 Versacec6e76ae2015-06-26 18:48:34 -0700194
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 Versace5b3a1ce2015-06-26 21:27:54 -0700203 .qpitch = qpitch,
204 .h_align = i,
205 .v_align = j,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700206 };
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700207
208 return VK_SUCCESS;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700209}
210
Chad Versace127cb3f2015-06-26 20:12:42 -0700211VkResult
212anv_image_create(VkDevice _device,
213 const struct anv_image_create_info *create_info,
214 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700215{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700216 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versacefdcd71f2015-06-26 20:06:08 -0700217 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace67a76592015-06-26 09:17:52 -0700218 const VkExtent3D *restrict extent = &pCreateInfo->extent;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700219 struct anv_image *image = NULL;
220 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700221
222 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
223
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700224 /* XXX: We don't handle any of these */
225 anv_assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700226 anv_assert(pCreateInfo->mipLevels > 0);
227 anv_assert(pCreateInfo->arraySize > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700228 anv_assert(pCreateInfo->samples == 1);
Chad Versace5d7103e2015-06-26 09:05:46 -0700229 anv_assert(pCreateInfo->extent.width > 0);
230 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700231 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700232
Chad Versace7ea12162015-05-28 07:45:31 -0700233 /* TODO(chadv): How should we validate inputs? */
Chad Versace67a76592015-06-26 09:17:52 -0700234 const uint8_t surf_type =
235 anv_surf_type_from_image_type[pCreateInfo->imageType];
Chad Versace7ea12162015-05-28 07:45:31 -0700236
Chad Versacefa352962015-05-28 07:40:22 -0700237 const struct anv_surf_type_limits *limits =
Chad Versace67a76592015-06-26 09:17:52 -0700238 &anv_surf_type_limits[surf_type];
Chad Versacefa352962015-05-28 07:40:22 -0700239
Chad Versace67a76592015-06-26 09:17:52 -0700240 if (extent->width > limits->width ||
241 extent->height > limits->height ||
242 extent->depth > limits->depth) {
Chad Versacefa352962015-05-28 07:40:22 -0700243 /* TODO(chadv): What is the correct error? */
Chad Versace67a76592015-06-26 09:17:52 -0700244 anv_loge("image extent is too large");
Chad Versacefa352962015-05-28 07:40:22 -0700245 return vk_error(VK_ERROR_INVALID_MEMORY_SIZE);
246 }
247
Chad Versacec6e76ae2015-06-26 18:48:34 -0700248 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 Versace67a76592015-06-26 09:17:52 -0700252
Chad Versacec6e76ae2015-06-26 18:48:34 -0700253 memset(image, 0, sizeof(*image));
254 image->type = pCreateInfo->imageType;
255 image->extent = pCreateInfo->extent;
Chad Versaceded736f2015-08-17 13:10:40 -0700256 image->format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700257 image->levels = pCreateInfo->mipLevels;
258 image->array_size = pCreateInfo->arraySize;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700259 image->surf_type = surf_type;
Chad Versace67a76592015-06-26 09:17:52 -0700260
Chad Versaceded736f2015-08-17 13:10:40 -0700261 if (likely(!image->format->has_stencil || image->format->depth_format)) {
Chad Versacec6e76ae2015-06-26 18:48:34 -0700262 /* The image's primary surface is a color or depth surface. */
Chad Versace5a6b2e62015-08-17 13:50:43 -0700263 r = anv_image_make_surface(create_info, image->format,
264 &image->size, &image->alignment,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700265 &image->primary_surface);
266 if (r != VK_SUCCESS)
267 goto fail;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700268 }
269
Chad Versaceded736f2015-08-17 13:10:40 -0700270 if (image->format->has_stencil) {
Chad Versace37d6e042015-06-26 09:47:17 -0700271 /* 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 Versace5a6b2e62015-08-17 13:50:43 -0700276 r = anv_image_make_surface(create_info, anv_format_s8_uint,
277 &image->size, &image->alignment,
278 &image->stencil_surface);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700279 if (r != VK_SUCCESS)
280 goto fail;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700281 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700282
Jason Ekstranda52e2082015-07-09 20:24:07 -0700283 *pImage = anv_image_to_handle(image);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700284
285 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700286
287fail:
288 if (image)
289 anv_device_free(device, image);
290
291 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700292}
293
Chad Versace127cb3f2015-06-26 20:12:42 -0700294VkResult
295anv_CreateImage(VkDevice device,
296 const VkImageCreateInfo *pCreateInfo,
297 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700298{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700299 return anv_image_create(device,
300 &(struct anv_image_create_info) {
301 .vk_info = pCreateInfo,
302 },
303 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700304}
305
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700306VkResult
307anv_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 Ekstranddb24afe2015-07-07 18:20:18 -0700316VkResult anv_GetImageSubresourceLayout(
317 VkDevice device,
318 VkImage image,
319 const VkImageSubresource* pSubresource,
320 VkSubresourceLayout* pLayout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700321{
Jason Ekstrandffe9f602015-05-12 13:44:43 -0700322 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700323}
324
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700325void
Jason Ekstrand84783502015-07-10 20:18:52 -0700326anv_surface_view_fini(struct anv_device *device,
327 struct anv_surface_view *view)
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700328{
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700329 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700330}
331
332void
Chad Versace8213be72015-07-15 12:00:27 -0700333anv_image_view_init(struct anv_image_view *iview,
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700334 struct anv_device *device,
335 const VkImageViewCreateInfo* pCreateInfo,
336 struct anv_cmd_buffer *cmd_buffer)
337{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700338 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace8213be72015-07-15 12:00:27 -0700339
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700340 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Chad Versace8213be72015-07-15 12:00:27 -0700341 struct anv_surface_view *view = &iview->view;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700342 struct anv_surface *surface;
Chad Versaceca6cef32015-06-26 19:50:04 -0700343
Chad Versace69e11ad2015-07-06 16:25:59 -0700344 const struct anv_image_view_info *view_type_info
345 = &anv_image_view_info_table[pCreateInfo->viewType];
346
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700347 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D)
348 anv_finishme("non-2D image views");
Chad Versaceca6cef32015-06-26 19:50:04 -0700349
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700350 switch (pCreateInfo->subresourceRange.aspect) {
351 case VK_IMAGE_ASPECT_STENCIL:
Chad Versace9c46ba92015-06-26 19:23:21 -0700352 anv_finishme("stencil image views");
353 abort();
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700354 break;
355 case VK_IMAGE_ASPECT_DEPTH:
356 case VK_IMAGE_ASPECT_COLOR:
357 view->offset = image->offset;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700358 surface = &image->primary_surface;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700359 break;
360 default:
Kristian Høgsberg Kristensen52637c02015-06-05 11:51:30 -0700361 unreachable("");
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700362 break;
363 }
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700364
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700365 view->bo = image->bo;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700366 view->offset = image->offset + surface->offset;
Chad Versace60c4ac52015-08-17 13:26:28 -0700367 view->format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700368
Chad Versacef70d0792015-07-17 14:48:23 -0700369 iview->extent = (VkExtent3D) {
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700370 .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øgsberg0dbed612015-05-25 22:12:24 -0700381
382 static const uint32_t vk_to_gen_swizzle[] = {
Kristian Høgsberg Kristensen5caa4082015-05-31 22:35:11 -0700383 [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øgsberg0dbed612015-05-25 22:12:24 -0700389 };
390
391 struct GEN8_RENDER_SURFACE_STATE surface_state = {
Chad Versace69e11ad2015-07-06 16:25:59 -0700392 .SurfaceType = view_type_info->surface_type,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700393 .SurfaceArray = image->array_size > 1,
Chad Versace60c4ac52015-08-17 13:26:28 -0700394 .SurfaceFormat = view->format->surface_format,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700395 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
396 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
397 .TileMode = surface->tile_mode,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700398 .VerticalLineStride = 0,
399 .VerticalLineStrideOffset = 0,
400 .SamplerL2BypassModeDisable = true,
401 .RenderCacheReadWriteMode = WriteOnlyCache,
402 .MemoryObjectControlState = GEN8_MOCS,
Chad Versace4fcb32a2015-07-08 14:39:05 -0700403
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 Versace5b3a1ce2015-06-26 21:27:54 -0700410 .SurfaceQPitch = surface->qpitch >> 2,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700411 .Height = image->extent.height - 1,
412 .Width = image->extent.width - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700413 .Depth = depth - 1,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700414 .SurfacePitch = surface->stride - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700415 .MinimumArrayElement = range->baseArraySlice,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700416 .NumberofMultisamples = MULTISAMPLECOUNT_1,
417 .XOffset = 0,
418 .YOffset = 0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700419
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øgsberg0dbed612015-05-25 22:12:24 -0700427 .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 Kristensena5b49d22015-06-10 23:11:37 -0700436 .ResourceMinLOD = 0.0,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700437 .SurfaceBaseAddress = { NULL, view->offset },
438 };
439
Chad Versacefc383832015-07-17 13:38:09 -0700440 if (cmd_buffer) {
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700441 view->surface_state =
442 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
Chad Versacefc383832015-07-17 13:38:09 -0700443 } else {
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700444 view->surface_state =
445 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
Chad Versacefc383832015-07-17 13:38:09 -0700446 }
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700447
448 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700449}
450
Chad Versace127cb3f2015-06-26 20:12:42 -0700451VkResult
Chad Versace5b04db72015-07-06 16:24:28 -0700452anv_validate_CreateImageView(VkDevice _device,
453 const VkImageViewCreateInfo *pCreateInfo,
454 VkImageView *pView)
455{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700456 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace23075bc2015-07-06 17:04:13 -0700457 const VkImageSubresourceRange *subresource;
458 const struct anv_image_view_info *view_info;
459 const struct anv_format *view_format_info;
Chad Versace5b04db72015-07-06 16:24:28 -0700460
Chad Versace23075bc2015-07-06 17:04:13 -0700461 /* Validate structure type before dereferencing it. */
Chad Versace5b04db72015-07-06 16:24:28 -0700462 assert(pCreateInfo);
463 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chad Versace23075bc2015-07-06 17:04:13 -0700464 subresource = &pCreateInfo->subresourceRange;
465
Chad Versace23075bc2015-07-06 17:04:13 -0700466 /* 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 Versace23075bc2015-07-06 17:04:13 -0700474 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 Versace5b04db72015-07-06 16:24:28 -0700495 assert(pView);
496
Chad Versace23075bc2015-07-06 17:04:13 -0700497 if (view_info->is_cube) {
498 assert(subresource->baseArraySlice % 6 == 0);
499 assert(subresource->arraySize % 6 == 0);
500 }
Chad Versace5b04db72015-07-06 16:24:28 -0700501
Chad Versace23075bc2015-07-06 17:04:13 -0700502 /* Validate format. */
503 switch (subresource->aspect) {
504 case VK_IMAGE_ASPECT_COLOR:
Chad Versaceded736f2015-08-17 13:10:40 -0700505 assert(!image->format->depth_format);
506 assert(!image->format->has_stencil);
Chad Versace23075bc2015-07-06 17:04:13 -0700507 assert(!view_format_info->depth_format);
508 assert(!view_format_info->has_stencil);
Chad Versaceded736f2015-08-17 13:10:40 -0700509 assert(view_format_info->cpp == image->format->cpp);
Chad Versace23075bc2015-07-06 17:04:13 -0700510 break;
511 case VK_IMAGE_ASPECT_DEPTH:
Chad Versaceded736f2015-08-17 13:10:40 -0700512 assert(image->format->depth_format);
Chad Versace23075bc2015-07-06 17:04:13 -0700513 assert(view_format_info->depth_format);
Chad Versaceded736f2015-08-17 13:10:40 -0700514 assert(view_format_info->cpp == image->format->cpp);
Chad Versace23075bc2015-07-06 17:04:13 -0700515 break;
516 case VK_IMAGE_ASPECT_STENCIL:
517 /* FINISHME: Is it legal to have an R8 view of S8? */
Chad Versaceded736f2015-08-17 13:10:40 -0700518 assert(image->format->has_stencil);
Chad Versace23075bc2015-07-06 17:04:13 -0700519 assert(view_format_info->has_stencil);
520 break;
521 default:
522 assert(!"bad VkImageAspect");
523 break;
524 }
Chad Versace5b04db72015-07-06 16:24:28 -0700525
526 return anv_CreateImageView(_device, pCreateInfo, pView);
527}
528
529VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700530anv_CreateImageView(VkDevice _device,
531 const VkImageViewCreateInfo *pCreateInfo,
532 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700533{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700534 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versace8213be72015-07-15 12:00:27 -0700535 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700536
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700537 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 Ekstrand4668bbb2015-05-18 20:42:03 -0700542 anv_image_view_init(view, device, pCreateInfo, NULL);
Jason Ekstrand1f7dcf92015-05-13 17:37:12 -0700543
Chad Versace8213be72015-07-15 12:00:27 -0700544 *pView = anv_image_view_to_handle(view);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700545
546 return VK_SUCCESS;
547}
548
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700549VkResult
Chad Versace8213be72015-07-15 12:00:27 -0700550anv_DestroyImageView(VkDevice _device, VkImageView _iview)
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700551{
552 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versace8213be72015-07-15 12:00:27 -0700553 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700554
Chad Versace8213be72015-07-15 12:00:27 -0700555 anv_surface_view_fini(device, &iview->view);
556 anv_device_free(device, iview);
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700557
558 return VK_SUCCESS;
559}
560
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700561void
Jason Ekstrand84783502015-07-10 20:18:52 -0700562anv_color_attachment_view_init(struct anv_color_attachment_view *aview,
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700563 struct anv_device *device,
Jason Ekstrand84783502015-07-10 20:18:52 -0700564 const VkAttachmentViewCreateInfo* pCreateInfo,
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700565 struct anv_cmd_buffer *cmd_buffer)
566{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700567 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Jason Ekstrand84783502015-07-10 20:18:52 -0700568 struct anv_surface_view *view = &aview->view;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700569 struct anv_surface *surface = &image->primary_surface;
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700570
Jason Ekstrand84783502015-07-10 20:18:52 -0700571 aview->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_COLOR;
572
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700573 anv_assert(pCreateInfo->arraySize > 0);
574 anv_assert(pCreateInfo->mipLevel < image->levels);
575 anv_assert(pCreateInfo->baseArraySlice + pCreateInfo->arraySize <= image->array_size);
576
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700577 view->bo = image->bo;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700578 view->offset = image->offset + surface->offset;
Chad Versace60c4ac52015-08-17 13:26:28 -0700579 view->format = anv_format_for_vk_format(pCreateInfo->format);
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700580
Chad Versacef70d0792015-07-17 14:48:23 -0700581 aview->base.extent = (VkExtent3D) {
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700582 .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 Versacefc383832015-07-17 13:38:09 -0700594 if (cmd_buffer) {
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700595 view->surface_state =
596 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
Chad Versacefc383832015-07-17 13:38:09 -0700597 } else {
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700598 view->surface_state =
599 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
Chad Versacefc383832015-07-17 13:38:09 -0700600 }
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700601
602 struct GEN8_RENDER_SURFACE_STATE surface_state = {
603 .SurfaceType = SURFTYPE_2D,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700604 .SurfaceArray = image->array_size > 1,
Chad Versace60c4ac52015-08-17 13:26:28 -0700605 .SurfaceFormat = view->format->surface_format,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700606 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
607 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
608 .TileMode = surface->tile_mode,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700609 .VerticalLineStride = 0,
610 .VerticalLineStrideOffset = 0,
611 .SamplerL2BypassModeDisable = true,
612 .RenderCacheReadWriteMode = WriteOnlyCache,
613 .MemoryObjectControlState = GEN8_MOCS,
Chad Versace4fcb32a2015-07-08 14:39:05 -0700614
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 Kristensena5b49d22015-06-10 23:11:37 -0700619 .BaseMipLevel = 0.0,
Chad Versace4fcb32a2015-07-08 14:39:05 -0700620
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700621 .SurfaceQPitch = surface->qpitch >> 2,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700622 .Height = image->extent.height - 1,
623 .Width = image->extent.width - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700624 .Depth = depth - 1,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700625 .SurfacePitch = surface->stride - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700626 .MinimumArrayElement = pCreateInfo->baseArraySlice,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700627 .NumberofMultisamples = MULTISAMPLECOUNT_1,
628 .XOffset = 0,
629 .YOffset = 0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700630
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øgsberg0dbed612015-05-25 22:12:24 -0700637 .SurfaceMinLOD = 0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700638 .MIPCountLOD = pCreateInfo->mipLevel,
639
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700640 .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 Kristensena5b49d22015-06-10 23:11:37 -0700649 .ResourceMinLOD = 0.0,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700650 .SurfaceBaseAddress = { NULL, view->offset },
651 };
652
653 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700654}
655
Jason Ekstrand84783502015-07-10 20:18:52 -0700656static void
657anv_depth_stencil_view_init(struct anv_depth_stencil_view *view,
658 const VkAttachmentViewCreateInfo *pCreateInfo)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700659{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700660 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700661 struct anv_surface *depth_surface = &image->primary_surface;
662 struct anv_surface *stencil_surface = &image->stencil_surface;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700663
Jason Ekstrand84783502015-07-10 20:18:52 -0700664 view->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700665
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700666 /* 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 Ekstrand2a3c2962015-06-10 21:04:51 -0700670
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700671 view->bo = image->bo;
672
Chad Versacec6e76ae2015-06-26 18:48:34 -0700673 view->depth_stride = depth_surface->stride;
674 view->depth_offset = image->offset + depth_surface->offset;
Chad Versaceded736f2015-08-17 13:10:40 -0700675 view->depth_format = image->format->depth_format;
Chad Versace7ea707a2015-06-25 19:46:42 -0700676 view->depth_qpitch = 0; /* FINISHME: QPitch */
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700677
Chad Versacec6e76ae2015-06-26 18:48:34 -0700678 view->stencil_stride = stencil_surface->stride;
679 view->stencil_offset = image->offset + stencil_surface->offset;
Chad Versace7ea707a2015-06-25 19:46:42 -0700680 view->stencil_qpitch = 0; /* FINISHME: QPitch */
Jason Ekstrand84783502015-07-10 20:18:52 -0700681}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700682
Jason Ekstrand84783502015-07-10 20:18:52 -0700683VkResult
684anv_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øgsberg37743f92015-05-22 22:59:12 -0700713
714 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700715}
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700716
717VkResult
Jason Ekstrand84783502015-07-10 20:18:52 -0700718anv_DestroyAttachmentView(VkDevice _device, VkAttachmentView _view)
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700719{
720 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrand84783502015-07-10 20:18:52 -0700721 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 Ekstrandb94b8df2015-07-10 12:25:30 -0700729
730 anv_device_free(device, view);
731
732 return VK_SUCCESS;
733}