blob: 329955fff593e39d291b19a6b603620f9c2c891d [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 Versace5b3a1ce2015-06-26 21:27:54 -0700126static VkResult
Chad Versacec6e76ae2015-06-26 18:48:34 -0700127anv_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 Versace5b3a1ce2015-06-26 21:27:54 -0700132 /* 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 Versacec6e76ae2015-06-26 18:48:34 -0700136 const VkExtent3D *restrict extent = &create_info->vk_info->extent;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700137 const uint32_t levels = create_info->vk_info->mipLevels;
138 const uint32_t array_size = create_info->vk_info->arraySize;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700139
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 Versace5b3a1ce2015-06-26 21:27:54 -0700148 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 Kristensen6d09d062015-08-14 10:02:19 -0700184 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 Versace5b3a1ce2015-06-26 21:27:54 -0700188 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 Versacec6e76ae2015-06-26 18:48:34 -0700191
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 Versace5b3a1ce2015-06-26 21:27:54 -0700200 .qpitch = qpitch,
201 .h_align = i,
202 .v_align = j,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700203 };
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700204
205 return VK_SUCCESS;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700206}
207
Chad Versace127cb3f2015-06-26 20:12:42 -0700208VkResult
209anv_image_create(VkDevice _device,
210 const struct anv_image_create_info *create_info,
211 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700212{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700213 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versacefdcd71f2015-06-26 20:06:08 -0700214 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace67a76592015-06-26 09:17:52 -0700215 const VkExtent3D *restrict extent = &pCreateInfo->extent;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700216 struct anv_image *image = NULL;
217 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700218
219 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
220
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700221 /* XXX: We don't handle any of these */
222 anv_assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700223 anv_assert(pCreateInfo->mipLevels > 0);
224 anv_assert(pCreateInfo->arraySize > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700225 anv_assert(pCreateInfo->samples == 1);
Chad Versace5d7103e2015-06-26 09:05:46 -0700226 anv_assert(pCreateInfo->extent.width > 0);
227 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700228 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700229
Chad Versace7ea12162015-05-28 07:45:31 -0700230 /* TODO(chadv): How should we validate inputs? */
Chad Versace67a76592015-06-26 09:17:52 -0700231 const uint8_t surf_type =
232 anv_surf_type_from_image_type[pCreateInfo->imageType];
Chad Versace7ea12162015-05-28 07:45:31 -0700233
Chad Versacefa352962015-05-28 07:40:22 -0700234 const struct anv_surf_type_limits *limits =
Chad Versace67a76592015-06-26 09:17:52 -0700235 &anv_surf_type_limits[surf_type];
Chad Versacefa352962015-05-28 07:40:22 -0700236
Chad Versace67a76592015-06-26 09:17:52 -0700237 if (extent->width > limits->width ||
238 extent->height > limits->height ||
239 extent->depth > limits->depth) {
Chad Versacefa352962015-05-28 07:40:22 -0700240 /* TODO(chadv): What is the correct error? */
Chad Versace67a76592015-06-26 09:17:52 -0700241 anv_loge("image extent is too large");
Chad Versacefa352962015-05-28 07:40:22 -0700242 return vk_error(VK_ERROR_INVALID_MEMORY_SIZE);
243 }
244
Chad Versacec6e76ae2015-06-26 18:48:34 -0700245 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 Versace67a76592015-06-26 09:17:52 -0700249
Chad Versacec6e76ae2015-06-26 18:48:34 -0700250 memset(image, 0, sizeof(*image));
251 image->type = pCreateInfo->imageType;
252 image->extent = pCreateInfo->extent;
Chad Versaceded736f2015-08-17 13:10:40 -0700253 image->format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700254 image->levels = pCreateInfo->mipLevels;
255 image->array_size = pCreateInfo->arraySize;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700256 image->surf_type = surf_type;
Chad Versace67a76592015-06-26 09:17:52 -0700257
Chad Versaceded736f2015-08-17 13:10:40 -0700258 if (likely(!image->format->has_stencil || image->format->depth_format)) {
Chad Versacec6e76ae2015-06-26 18:48:34 -0700259 /* The image's primary surface is a color or depth surface. */
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700260 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øgsberg37743f92015-05-22 22:59:12 -0700264 }
265
Chad Versaceded736f2015-08-17 13:10:40 -0700266 if (image->format->has_stencil) {
Chad Versace37d6e042015-06-26 09:47:17 -0700267 /* 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 Versacec6e76ae2015-06-26 18:48:34 -0700272 VkImageCreateInfo stencil_info = *pCreateInfo;
273 stencil_info.format = VK_FORMAT_S8_UINT;
Chad Versace67a76592015-06-26 09:17:52 -0700274
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700275 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øgsberg37743f92015-05-22 22:59:12 -0700283 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700284
Jason Ekstranda52e2082015-07-09 20:24:07 -0700285 *pImage = anv_image_to_handle(image);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700286
287 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700288
289fail:
290 if (image)
291 anv_device_free(device, image);
292
293 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700294}
295
Chad Versace127cb3f2015-06-26 20:12:42 -0700296VkResult
297anv_CreateImage(VkDevice device,
298 const VkImageCreateInfo *pCreateInfo,
299 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700300{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700301 return anv_image_create(device,
302 &(struct anv_image_create_info) {
303 .vk_info = pCreateInfo,
304 },
305 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700306}
307
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700308VkResult
309anv_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 Ekstranddb24afe2015-07-07 18:20:18 -0700318VkResult anv_GetImageSubresourceLayout(
319 VkDevice device,
320 VkImage image,
321 const VkImageSubresource* pSubresource,
322 VkSubresourceLayout* pLayout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700323{
Jason Ekstrandffe9f602015-05-12 13:44:43 -0700324 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700325}
326
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700327void
Jason Ekstrand84783502015-07-10 20:18:52 -0700328anv_surface_view_fini(struct anv_device *device,
329 struct anv_surface_view *view)
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700330{
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700331 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700332}
333
334void
Chad Versace8213be72015-07-15 12:00:27 -0700335anv_image_view_init(struct anv_image_view *iview,
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700336 struct anv_device *device,
337 const VkImageViewCreateInfo* pCreateInfo,
338 struct anv_cmd_buffer *cmd_buffer)
339{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700340 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace8213be72015-07-15 12:00:27 -0700341
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700342 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Chad Versace8213be72015-07-15 12:00:27 -0700343 struct anv_surface_view *view = &iview->view;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700344 struct anv_surface *surface;
Chad Versaceca6cef32015-06-26 19:50:04 -0700345
346 const struct anv_format *format_info =
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700347 anv_format_for_vk_format(pCreateInfo->format);
348
Chad Versace69e11ad2015-07-06 16:25:59 -0700349 const struct anv_image_view_info *view_type_info
350 = &anv_image_view_info_table[pCreateInfo->viewType];
351
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700352 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D)
353 anv_finishme("non-2D image views");
Chad Versaceca6cef32015-06-26 19:50:04 -0700354
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700355 switch (pCreateInfo->subresourceRange.aspect) {
356 case VK_IMAGE_ASPECT_STENCIL:
Chad Versace9c46ba92015-06-26 19:23:21 -0700357 anv_finishme("stencil image views");
358 abort();
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700359 break;
360 case VK_IMAGE_ASPECT_DEPTH:
361 case VK_IMAGE_ASPECT_COLOR:
362 view->offset = image->offset;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700363 surface = &image->primary_surface;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700364 break;
365 default:
Kristian Høgsberg Kristensen52637c02015-06-05 11:51:30 -0700366 unreachable("");
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700367 break;
368 }
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700369
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700370 view->bo = image->bo;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700371 view->offset = image->offset + surface->offset;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700372 view->format = pCreateInfo->format;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700373
Chad Versacef70d0792015-07-17 14:48:23 -0700374 iview->extent = (VkExtent3D) {
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700375 .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øgsberg0dbed612015-05-25 22:12:24 -0700386
387 static const uint32_t vk_to_gen_swizzle[] = {
Kristian Høgsberg Kristensen5caa4082015-05-31 22:35:11 -0700388 [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øgsberg0dbed612015-05-25 22:12:24 -0700394 };
395
396 struct GEN8_RENDER_SURFACE_STATE surface_state = {
Chad Versace69e11ad2015-07-06 16:25:59 -0700397 .SurfaceType = view_type_info->surface_type,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700398 .SurfaceArray = image->array_size > 1,
Chad Versaceca6cef32015-06-26 19:50:04 -0700399 .SurfaceFormat = format_info->surface_format,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700400 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
401 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
402 .TileMode = surface->tile_mode,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700403 .VerticalLineStride = 0,
404 .VerticalLineStrideOffset = 0,
405 .SamplerL2BypassModeDisable = true,
406 .RenderCacheReadWriteMode = WriteOnlyCache,
407 .MemoryObjectControlState = GEN8_MOCS,
Chad Versace4fcb32a2015-07-08 14:39:05 -0700408
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 Versace5b3a1ce2015-06-26 21:27:54 -0700415 .SurfaceQPitch = surface->qpitch >> 2,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700416 .Height = image->extent.height - 1,
417 .Width = image->extent.width - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700418 .Depth = depth - 1,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700419 .SurfacePitch = surface->stride - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700420 .MinimumArrayElement = range->baseArraySlice,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700421 .NumberofMultisamples = MULTISAMPLECOUNT_1,
422 .XOffset = 0,
423 .YOffset = 0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700424
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øgsberg0dbed612015-05-25 22:12:24 -0700432 .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 Kristensena5b49d22015-06-10 23:11:37 -0700441 .ResourceMinLOD = 0.0,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700442 .SurfaceBaseAddress = { NULL, view->offset },
443 };
444
Chad Versacefc383832015-07-17 13:38:09 -0700445 if (cmd_buffer) {
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700446 view->surface_state =
447 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
Chad Versacefc383832015-07-17 13:38:09 -0700448 } else {
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700449 view->surface_state =
450 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
Chad Versacefc383832015-07-17 13:38:09 -0700451 }
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700452
453 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700454}
455
Chad Versace127cb3f2015-06-26 20:12:42 -0700456VkResult
Chad Versace5b04db72015-07-06 16:24:28 -0700457anv_validate_CreateImageView(VkDevice _device,
458 const VkImageViewCreateInfo *pCreateInfo,
459 VkImageView *pView)
460{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700461 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace23075bc2015-07-06 17:04:13 -0700462 const VkImageSubresourceRange *subresource;
463 const struct anv_image_view_info *view_info;
464 const struct anv_format *view_format_info;
Chad Versace5b04db72015-07-06 16:24:28 -0700465
Chad Versace23075bc2015-07-06 17:04:13 -0700466 /* Validate structure type before dereferencing it. */
Chad Versace5b04db72015-07-06 16:24:28 -0700467 assert(pCreateInfo);
468 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chad Versace23075bc2015-07-06 17:04:13 -0700469 subresource = &pCreateInfo->subresourceRange;
470
Chad Versace23075bc2015-07-06 17:04:13 -0700471 /* 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 Versace23075bc2015-07-06 17:04:13 -0700479 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 Versace5b04db72015-07-06 16:24:28 -0700500 assert(pView);
501
Chad Versace23075bc2015-07-06 17:04:13 -0700502 if (view_info->is_cube) {
503 assert(subresource->baseArraySlice % 6 == 0);
504 assert(subresource->arraySize % 6 == 0);
505 }
Chad Versace5b04db72015-07-06 16:24:28 -0700506
Chad Versace23075bc2015-07-06 17:04:13 -0700507 /* Validate format. */
508 switch (subresource->aspect) {
509 case VK_IMAGE_ASPECT_COLOR:
Chad Versaceded736f2015-08-17 13:10:40 -0700510 assert(!image->format->depth_format);
511 assert(!image->format->has_stencil);
Chad Versace23075bc2015-07-06 17:04:13 -0700512 assert(!view_format_info->depth_format);
513 assert(!view_format_info->has_stencil);
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_DEPTH:
Chad Versaceded736f2015-08-17 13:10:40 -0700517 assert(image->format->depth_format);
Chad Versace23075bc2015-07-06 17:04:13 -0700518 assert(view_format_info->depth_format);
Chad Versaceded736f2015-08-17 13:10:40 -0700519 assert(view_format_info->cpp == image->format->cpp);
Chad Versace23075bc2015-07-06 17:04:13 -0700520 break;
521 case VK_IMAGE_ASPECT_STENCIL:
522 /* FINISHME: Is it legal to have an R8 view of S8? */
Chad Versaceded736f2015-08-17 13:10:40 -0700523 assert(image->format->has_stencil);
Chad Versace23075bc2015-07-06 17:04:13 -0700524 assert(view_format_info->has_stencil);
525 break;
526 default:
527 assert(!"bad VkImageAspect");
528 break;
529 }
Chad Versace5b04db72015-07-06 16:24:28 -0700530
531 return anv_CreateImageView(_device, pCreateInfo, pView);
532}
533
534VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700535anv_CreateImageView(VkDevice _device,
536 const VkImageViewCreateInfo *pCreateInfo,
537 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700538{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700539 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versace8213be72015-07-15 12:00:27 -0700540 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700541
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700542 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 Ekstrand4668bbb2015-05-18 20:42:03 -0700547 anv_image_view_init(view, device, pCreateInfo, NULL);
Jason Ekstrand1f7dcf92015-05-13 17:37:12 -0700548
Chad Versace8213be72015-07-15 12:00:27 -0700549 *pView = anv_image_view_to_handle(view);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700550
551 return VK_SUCCESS;
552}
553
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700554VkResult
Chad Versace8213be72015-07-15 12:00:27 -0700555anv_DestroyImageView(VkDevice _device, VkImageView _iview)
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700556{
557 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versace8213be72015-07-15 12:00:27 -0700558 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700559
Chad Versace8213be72015-07-15 12:00:27 -0700560 anv_surface_view_fini(device, &iview->view);
561 anv_device_free(device, iview);
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700562
563 return VK_SUCCESS;
564}
565
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700566void
Jason Ekstrand84783502015-07-10 20:18:52 -0700567anv_color_attachment_view_init(struct anv_color_attachment_view *aview,
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700568 struct anv_device *device,
Jason Ekstrand84783502015-07-10 20:18:52 -0700569 const VkAttachmentViewCreateInfo* pCreateInfo,
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700570 struct anv_cmd_buffer *cmd_buffer)
571{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700572 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Jason Ekstrand84783502015-07-10 20:18:52 -0700573 struct anv_surface_view *view = &aview->view;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700574 struct anv_surface *surface = &image->primary_surface;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700575 const struct anv_format *format_info =
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700576 anv_format_for_vk_format(pCreateInfo->format);
577
Jason Ekstrand84783502015-07-10 20:18:52 -0700578 aview->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_COLOR;
579
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700580 anv_assert(pCreateInfo->arraySize > 0);
581 anv_assert(pCreateInfo->mipLevel < image->levels);
582 anv_assert(pCreateInfo->baseArraySlice + pCreateInfo->arraySize <= image->array_size);
583
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700584 view->bo = image->bo;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700585 view->offset = image->offset + surface->offset;
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700586 view->format = pCreateInfo->format;
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700587
Chad Versacef70d0792015-07-17 14:48:23 -0700588 aview->base.extent = (VkExtent3D) {
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700589 .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 Versacefc383832015-07-17 13:38:09 -0700601 if (cmd_buffer) {
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700602 view->surface_state =
603 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
Chad Versacefc383832015-07-17 13:38:09 -0700604 } else {
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700605 view->surface_state =
606 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
Chad Versacefc383832015-07-17 13:38:09 -0700607 }
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700608
609 struct GEN8_RENDER_SURFACE_STATE surface_state = {
610 .SurfaceType = SURFTYPE_2D,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700611 .SurfaceArray = image->array_size > 1,
612 .SurfaceFormat = format_info->surface_format,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700613 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
614 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
615 .TileMode = surface->tile_mode,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700616 .VerticalLineStride = 0,
617 .VerticalLineStrideOffset = 0,
618 .SamplerL2BypassModeDisable = true,
619 .RenderCacheReadWriteMode = WriteOnlyCache,
620 .MemoryObjectControlState = GEN8_MOCS,
Chad Versace4fcb32a2015-07-08 14:39:05 -0700621
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 Kristensena5b49d22015-06-10 23:11:37 -0700626 .BaseMipLevel = 0.0,
Chad Versace4fcb32a2015-07-08 14:39:05 -0700627
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700628 .SurfaceQPitch = surface->qpitch >> 2,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700629 .Height = image->extent.height - 1,
630 .Width = image->extent.width - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700631 .Depth = depth - 1,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700632 .SurfacePitch = surface->stride - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700633 .MinimumArrayElement = pCreateInfo->baseArraySlice,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700634 .NumberofMultisamples = MULTISAMPLECOUNT_1,
635 .XOffset = 0,
636 .YOffset = 0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700637
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øgsberg0dbed612015-05-25 22:12:24 -0700644 .SurfaceMinLOD = 0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700645 .MIPCountLOD = pCreateInfo->mipLevel,
646
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700647 .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 Kristensena5b49d22015-06-10 23:11:37 -0700656 .ResourceMinLOD = 0.0,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700657 .SurfaceBaseAddress = { NULL, view->offset },
658 };
659
660 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700661}
662
Jason Ekstrand84783502015-07-10 20:18:52 -0700663static void
664anv_depth_stencil_view_init(struct anv_depth_stencil_view *view,
665 const VkAttachmentViewCreateInfo *pCreateInfo)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700666{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700667 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700668 struct anv_surface *depth_surface = &image->primary_surface;
669 struct anv_surface *stencil_surface = &image->stencil_surface;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700670
Jason Ekstrand84783502015-07-10 20:18:52 -0700671 view->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700672
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700673 /* 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 Ekstrand2a3c2962015-06-10 21:04:51 -0700677
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700678 view->bo = image->bo;
679
Chad Versacec6e76ae2015-06-26 18:48:34 -0700680 view->depth_stride = depth_surface->stride;
681 view->depth_offset = image->offset + depth_surface->offset;
Chad Versaceded736f2015-08-17 13:10:40 -0700682 view->depth_format = image->format->depth_format;
Chad Versace7ea707a2015-06-25 19:46:42 -0700683 view->depth_qpitch = 0; /* FINISHME: QPitch */
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700684
Chad Versacec6e76ae2015-06-26 18:48:34 -0700685 view->stencil_stride = stencil_surface->stride;
686 view->stencil_offset = image->offset + stencil_surface->offset;
Chad Versace7ea707a2015-06-25 19:46:42 -0700687 view->stencil_qpitch = 0; /* FINISHME: QPitch */
Jason Ekstrand84783502015-07-10 20:18:52 -0700688}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700689
Jason Ekstrand84783502015-07-10 20:18:52 -0700690VkResult
691anv_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øgsberg37743f92015-05-22 22:59:12 -0700720
721 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700722}
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700723
724VkResult
Jason Ekstrand84783502015-07-10 20:18:52 -0700725anv_DestroyAttachmentView(VkDevice _device, VkAttachmentView _view)
Jason Ekstrandb94b8df2015-07-10 12:25:30 -0700726{
727 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrand84783502015-07-10 20:18:52 -0700728 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 Ekstrandb94b8df2015-07-10 12:25:30 -0700736
737 anv_device_free(device, view);
738
739 return VK_SUCCESS;
740}