vk: Don't duplicate anv_depth_stencil_view's surface data
[mesa.git] / src / vulkan / anv_image.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 static const uint8_t anv_halign[] = {
33 [4] = HALIGN4,
34 [8] = HALIGN8,
35 [16] = HALIGN16,
36 };
37
38 static const uint8_t anv_valign[] = {
39 [4] = VALIGN4,
40 [8] = VALIGN8,
41 [16] = VALIGN16,
42 };
43
44 static 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,
48
49 };
50
51 static const struct anv_image_view_info
52 anv_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
62 };
63
64 const struct anv_image_view_info *
65 anv_image_view_info_for_vk_image_view_type(VkImageViewType type)
66 {
67 return &anv_image_view_info_table[type];
68 }
69
70 static const struct anv_surf_type_limits {
71 int32_t width;
72 int32_t height;
73 int32_t depth;
74 } anv_surf_type_limits[] = {
75 [SURFTYPE_1D] = {16384, 0, 2048},
76 [SURFTYPE_2D] = {16384, 16384, 2048},
77 [SURFTYPE_3D] = {2048, 2048, 2048},
78 [SURFTYPE_CUBE] = {16384, 16384, 340},
79 [SURFTYPE_BUFFER] = {128, 16384, 64},
80 [SURFTYPE_STRBUF] = {128, 16384, 64},
81 };
82
83 static const struct anv_tile_info {
84 uint32_t width;
85 uint32_t height;
86
87 /**
88 * Alignment for RENDER_SURFACE_STATE.SurfaceBaseAddress.
89 *
90 * To simplify calculations, the alignments defined in the table are
91 * sometimes larger than required. For example, Skylake requires that X and
92 * Y tiled buffers be aligned to 4K, but Broadwell permits smaller
93 * alignment. We choose 4K to accomodate both chipsets. The alignment of
94 * a linear buffer depends on its element type and usage. Linear depth
95 * buffers have the largest alignment, 64B, so we choose that for all linear
96 * buffers.
97 */
98 uint32_t surface_alignment;
99 } anv_tile_info_table[] = {
100 [LINEAR] = { 1, 1, 64 },
101 [XMAJOR] = { 512, 8, 4096 },
102 [YMAJOR] = { 128, 32, 4096 },
103 [WMAJOR] = { 128, 32, 4096 },
104 };
105
106 static uint32_t
107 anv_image_choose_tile_mode(const struct anv_image_create_info *anv_info)
108 {
109 if (anv_info->force_tile_mode)
110 return anv_info->tile_mode;
111
112 if (anv_info->vk_info->format == VK_FORMAT_S8_UINT)
113 return WMAJOR;
114
115 switch (anv_info->vk_info->tiling) {
116 case VK_IMAGE_TILING_LINEAR:
117 return LINEAR;
118 case VK_IMAGE_TILING_OPTIMAL:
119 return YMAJOR;
120 default:
121 assert(!"bad VKImageTiling");
122 return LINEAR;
123 }
124 }
125
126
127 /**
128 * The \a format argument is required and overrides any format in
129 * struct anv_image_create_info.
130 */
131 static VkResult
132 anv_image_make_surface(const struct anv_image_create_info *create_info,
133 const struct anv_format *format,
134 uint64_t *inout_image_size,
135 uint32_t *inout_image_alignment,
136 struct anv_surface *out_surface)
137 {
138 /* See RENDER_SURFACE_STATE.SurfaceQPitch */
139 static const uint16_t min_qpitch UNUSED = 0x4;
140 static const uint16_t max_qpitch UNUSED = 0x1ffc;
141
142 const VkExtent3D *restrict extent = &create_info->vk_info->extent;
143 const uint32_t levels = create_info->vk_info->mipLevels;
144 const uint32_t array_size = create_info->vk_info->arraySize;
145
146 const uint8_t tile_mode = anv_image_choose_tile_mode(create_info);
147
148 const struct anv_tile_info *tile_info =
149 &anv_tile_info_table[tile_mode];
150
151 const uint32_t i = 4; /* FINISHME: Stop hardcoding subimage alignment */
152 const uint32_t j = 4; /* FINISHME: Stop hardcoding subimage alignment */
153 const uint32_t w0 = align_u32(extent->width, i);
154 const uint32_t h0 = align_u32(extent->height, j);
155
156 uint16_t qpitch;
157 uint32_t mt_width;
158 uint32_t mt_height;
159
160 if (levels == 1 && array_size == 1) {
161 qpitch = min_qpitch;
162 mt_width = w0;
163 mt_height = h0;
164 } else {
165 uint32_t w1 = align_u32(anv_minify(extent->width, 1), i);
166 uint32_t h1 = align_u32(anv_minify(extent->height, 1), j);
167 uint32_t w2 = align_u32(anv_minify(extent->width, 2), i);
168
169 qpitch = h0 + h1 + 11 * j;
170 mt_width = MAX(w0, w1 + w2);
171 mt_height = array_size * qpitch;
172 }
173
174 assert(qpitch >= min_qpitch);
175 if (qpitch > max_qpitch) {
176 anv_loge("image qpitch > 0x%x\n", max_qpitch);
177 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
178 }
179
180 /* From the Broadwell PRM, RENDER_SURFACE_STATE.SurfaceQpitch:
181 *
182 * This field must be set an integer multiple of the Surface Vertical
183 * Alignment.
184 */
185 assert(anv_is_aligned(qpitch, j));
186
187 uint32_t stride = align_u32(mt_width * format->cpp, tile_info->width);
188 if (create_info->stride > 0)
189 stride = create_info->stride;
190
191 const uint32_t size = stride * align_u32(mt_height, tile_info->height);
192 const uint32_t offset = align_u32(*inout_image_size,
193 tile_info->surface_alignment);
194
195 *inout_image_size = offset + size;
196 *inout_image_alignment = MAX(*inout_image_alignment,
197 tile_info->surface_alignment);
198
199 *out_surface = (struct anv_surface) {
200 .offset = offset,
201 .stride = stride,
202 .tile_mode = tile_mode,
203 .qpitch = qpitch,
204 .h_align = i,
205 .v_align = j,
206 };
207
208 return VK_SUCCESS;
209 }
210
211 VkResult
212 anv_image_create(VkDevice _device,
213 const struct anv_image_create_info *create_info,
214 VkImage *pImage)
215 {
216 ANV_FROM_HANDLE(anv_device, device, _device);
217 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
218 const VkExtent3D *restrict extent = &pCreateInfo->extent;
219 struct anv_image *image = NULL;
220 VkResult r;
221
222 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
223
224 /* XXX: We don't handle any of these */
225 anv_assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D);
226 anv_assert(pCreateInfo->mipLevels > 0);
227 anv_assert(pCreateInfo->arraySize > 0);
228 anv_assert(pCreateInfo->samples == 1);
229 anv_assert(pCreateInfo->extent.width > 0);
230 anv_assert(pCreateInfo->extent.height > 0);
231 anv_assert(pCreateInfo->extent.depth > 0);
232
233 /* TODO(chadv): How should we validate inputs? */
234 const uint8_t surf_type =
235 anv_surf_type_from_image_type[pCreateInfo->imageType];
236
237 const struct anv_surf_type_limits *limits =
238 &anv_surf_type_limits[surf_type];
239
240 if (extent->width > limits->width ||
241 extent->height > limits->height ||
242 extent->depth > limits->depth) {
243 /* TODO(chadv): What is the correct error? */
244 anv_loge("image extent is too large");
245 return vk_error(VK_ERROR_INVALID_MEMORY_SIZE);
246 }
247
248 image = anv_device_alloc(device, sizeof(*image), 8,
249 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
250 if (!image)
251 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
252
253 memset(image, 0, sizeof(*image));
254 image->type = pCreateInfo->imageType;
255 image->extent = pCreateInfo->extent;
256 image->format = anv_format_for_vk_format(pCreateInfo->format);
257 image->levels = pCreateInfo->mipLevels;
258 image->array_size = pCreateInfo->arraySize;
259 image->surf_type = surf_type;
260
261 if (likely(anv_format_is_color(image->format))) {
262 r = anv_image_make_surface(create_info, image->format,
263 &image->size, &image->alignment,
264 &image->color_surface);
265 if (r != VK_SUCCESS)
266 goto fail;
267 } else {
268 if (image->format->depth_format) {
269 r = anv_image_make_surface(create_info, image->format,
270 &image->size, &image->alignment,
271 &image->depth_surface);
272 if (r != VK_SUCCESS)
273 goto fail;
274 }
275
276 if (image->format->has_stencil) {
277 r = anv_image_make_surface(create_info, anv_format_s8_uint,
278 &image->size, &image->alignment,
279 &image->stencil_surface);
280 if (r != VK_SUCCESS)
281 goto fail;
282 }
283 }
284
285 *pImage = anv_image_to_handle(image);
286
287 return VK_SUCCESS;
288
289 fail:
290 if (image)
291 anv_device_free(device, image);
292
293 return r;
294 }
295
296 VkResult
297 anv_CreateImage(VkDevice device,
298 const VkImageCreateInfo *pCreateInfo,
299 VkImage *pImage)
300 {
301 return anv_image_create(device,
302 &(struct anv_image_create_info) {
303 .vk_info = pCreateInfo,
304 },
305 pImage);
306 }
307
308 VkResult
309 anv_DestroyImage(VkDevice _device, VkImage _image)
310 {
311 ANV_FROM_HANDLE(anv_device, device, _device);
312
313 anv_device_free(device, anv_image_from_handle(_image));
314
315 return VK_SUCCESS;
316 }
317
318 VkResult anv_GetImageSubresourceLayout(
319 VkDevice device,
320 VkImage image,
321 const VkImageSubresource* pSubresource,
322 VkSubresourceLayout* pLayout)
323 {
324 stub_return(VK_UNSUPPORTED);
325 }
326
327 void
328 anv_surface_view_fini(struct anv_device *device,
329 struct anv_surface_view *view)
330 {
331 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
332 }
333
334 VkResult
335 anv_validate_CreateImageView(VkDevice _device,
336 const VkImageViewCreateInfo *pCreateInfo,
337 VkImageView *pView)
338 {
339 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
340 const VkImageSubresourceRange *subresource;
341 const struct anv_image_view_info *view_info;
342 const struct anv_format *view_format_info;
343
344 /* Validate structure type before dereferencing it. */
345 assert(pCreateInfo);
346 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
347 subresource = &pCreateInfo->subresourceRange;
348
349 /* Validate viewType is in range before using it. */
350 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
351 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
352 view_info = &anv_image_view_info_table[pCreateInfo->viewType];
353
354 /* Validate format is in range before using it. */
355 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
356 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
357 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
358
359 /* Validate channel swizzles. */
360 assert(pCreateInfo->channels.r >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
361 assert(pCreateInfo->channels.r <= VK_CHANNEL_SWIZZLE_END_RANGE);
362 assert(pCreateInfo->channels.g >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
363 assert(pCreateInfo->channels.g <= VK_CHANNEL_SWIZZLE_END_RANGE);
364 assert(pCreateInfo->channels.b >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
365 assert(pCreateInfo->channels.b <= VK_CHANNEL_SWIZZLE_END_RANGE);
366 assert(pCreateInfo->channels.a >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
367 assert(pCreateInfo->channels.a <= VK_CHANNEL_SWIZZLE_END_RANGE);
368
369 /* Validate subresource. */
370 assert(subresource->aspect >= VK_IMAGE_ASPECT_BEGIN_RANGE);
371 assert(subresource->aspect <= VK_IMAGE_ASPECT_END_RANGE);
372 assert(subresource->mipLevels > 0);
373 assert(subresource->arraySize > 0);
374 assert(subresource->baseMipLevel < image->levels);
375 assert(subresource->baseMipLevel + subresource->mipLevels <= image->levels);
376 assert(subresource->baseArraySlice < image->array_size);
377 assert(subresource->baseArraySlice + subresource->arraySize <= image->array_size);
378 assert(pView);
379
380 if (view_info->is_cube) {
381 assert(subresource->baseArraySlice % 6 == 0);
382 assert(subresource->arraySize % 6 == 0);
383 }
384
385 /* Validate format. */
386 switch (subresource->aspect) {
387 case VK_IMAGE_ASPECT_COLOR:
388 assert(!image->format->depth_format);
389 assert(!image->format->has_stencil);
390 assert(!view_format_info->depth_format);
391 assert(!view_format_info->has_stencil);
392 assert(view_format_info->cpp == image->format->cpp);
393 break;
394 case VK_IMAGE_ASPECT_DEPTH:
395 assert(image->format->depth_format);
396 assert(view_format_info->depth_format);
397 assert(view_format_info->cpp == image->format->cpp);
398 break;
399 case VK_IMAGE_ASPECT_STENCIL:
400 /* FINISHME: Is it legal to have an R8 view of S8? */
401 assert(image->format->has_stencil);
402 assert(view_format_info->has_stencil);
403 break;
404 default:
405 assert(!"bad VkImageAspect");
406 break;
407 }
408
409 return anv_CreateImageView(_device, pCreateInfo, pView);
410 }
411
412 void
413 anv_image_view_init(struct anv_image_view *iview,
414 struct anv_device *device,
415 const VkImageViewCreateInfo* pCreateInfo,
416 struct anv_cmd_buffer *cmd_buffer)
417 {
418 switch (device->info.gen) {
419 case 7:
420 gen7_image_view_init(iview, device, pCreateInfo, cmd_buffer);
421 break;
422 case 8:
423 gen8_image_view_init(iview, device, pCreateInfo, cmd_buffer);
424 break;
425 default:
426 unreachable("unsupported gen\n");
427 }
428 }
429
430 VkResult
431 anv_CreateImageView(VkDevice _device,
432 const VkImageViewCreateInfo *pCreateInfo,
433 VkImageView *pView)
434 {
435 ANV_FROM_HANDLE(anv_device, device, _device);
436 struct anv_image_view *view;
437
438 view = anv_device_alloc(device, sizeof(*view), 8,
439 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
440 if (view == NULL)
441 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
442
443 anv_image_view_init(view, device, pCreateInfo, NULL);
444
445 *pView = anv_image_view_to_handle(view);
446
447 return VK_SUCCESS;
448 }
449
450 VkResult
451 anv_DestroyImageView(VkDevice _device, VkImageView _iview)
452 {
453 ANV_FROM_HANDLE(anv_device, device, _device);
454 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
455
456 anv_surface_view_fini(device, &iview->view);
457 anv_device_free(device, iview);
458
459 return VK_SUCCESS;
460 }
461
462 static void
463 anv_depth_stencil_view_init(struct anv_depth_stencil_view *view,
464 const VkAttachmentViewCreateInfo *pCreateInfo)
465 {
466 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
467
468 view->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL;
469
470 /* XXX: We don't handle any of these */
471 anv_assert(pCreateInfo->mipLevel == 0);
472 anv_assert(pCreateInfo->baseArraySlice == 0);
473 anv_assert(pCreateInfo->arraySize == 1);
474
475 view->image = image;
476 view->format = anv_format_for_vk_format(pCreateInfo->format);
477
478 assert(anv_format_is_depth_or_stencil(image->format));
479 assert(anv_format_is_depth_or_stencil(view->format));
480 }
481
482 struct anv_surface *
483 anv_image_get_surface_for_aspect(struct anv_image *image, VkImageAspect aspect)
484 {
485 switch (aspect) {
486 case VK_IMAGE_ASPECT_COLOR:
487 assert(anv_format_is_color(image->format));
488 return &image->color_surface;
489 case VK_IMAGE_ASPECT_DEPTH:
490 assert(image->format->depth_format);
491 return &image->depth_surface;
492 case VK_IMAGE_ASPECT_STENCIL:
493 assert(image->format->has_stencil);
494 anv_finishme("stencil image views");
495 abort();
496 return &image->stencil_surface;
497 default:
498 unreachable("image does not have aspect");
499 return NULL;
500 }
501 }
502
503 /** The attachment may be a color view into a non-color image. */
504 struct anv_surface *
505 anv_image_get_surface_for_color_attachment(struct anv_image *image)
506 {
507 if (anv_format_is_color(image->format)) {
508 return &image->color_surface;
509 } else if (image->format->depth_format) {
510 return &image->depth_surface;
511 } else if (image->format->has_stencil) {
512 return &image->stencil_surface;
513 } else {
514 unreachable("image has bad format");
515 return NULL;
516 }
517 }
518
519 void
520 anv_color_attachment_view_init(struct anv_color_attachment_view *aview,
521 struct anv_device *device,
522 const VkAttachmentViewCreateInfo* pCreateInfo,
523 struct anv_cmd_buffer *cmd_buffer)
524 {
525 switch (device->info.gen) {
526 case 7:
527 gen7_color_attachment_view_init(aview, device, pCreateInfo, cmd_buffer);
528 break;
529 case 8:
530 gen8_color_attachment_view_init(aview, device, pCreateInfo, cmd_buffer);
531 break;
532 default:
533 unreachable("unsupported gen\n");
534 }
535 }
536
537 VkResult
538 anv_CreateAttachmentView(VkDevice _device,
539 const VkAttachmentViewCreateInfo *pCreateInfo,
540 VkAttachmentView *pView)
541 {
542 ANV_FROM_HANDLE(anv_device, device, _device);
543
544 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO);
545
546 const struct anv_format *format =
547 anv_format_for_vk_format(pCreateInfo->format);
548
549 if (anv_format_is_depth_or_stencil(format)) {
550 struct anv_depth_stencil_view *view =
551 anv_device_alloc(device, sizeof(*view), 8,
552 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
553 if (view == NULL)
554 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
555
556 anv_depth_stencil_view_init(view, pCreateInfo);
557
558 *pView = anv_attachment_view_to_handle(&view->base);
559 } else {
560 struct anv_color_attachment_view *view =
561 anv_device_alloc(device, sizeof(*view), 8,
562 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
563 if (view == NULL)
564 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
565
566 anv_color_attachment_view_init(view, device, pCreateInfo, NULL);
567
568 *pView = anv_attachment_view_to_handle(&view->base);
569 }
570
571 return VK_SUCCESS;
572 }
573
574 VkResult
575 anv_DestroyAttachmentView(VkDevice _device, VkAttachmentView _view)
576 {
577 ANV_FROM_HANDLE(anv_device, device, _device);
578 ANV_FROM_HANDLE(anv_attachment_view, view, _view);
579
580 if (view->attachment_type == ANV_ATTACHMENT_VIEW_TYPE_COLOR) {
581 struct anv_color_attachment_view *aview =
582 (struct anv_color_attachment_view *)view;
583
584 anv_surface_view_fini(device, &aview->view);
585 }
586
587 anv_device_free(device, view);
588
589 return VK_SUCCESS;
590 }