Merge remote-tracking branch 'fdo-personal/nir-spirv' into vulkan
[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 /**
107 * Return -1 on failure.
108 */
109 static int8_t
110 anv_image_choose_tile_mode(const struct anv_image_create_info *anv_info)
111 {
112 if (anv_info->force_tile_mode)
113 return anv_info->tile_mode;
114
115 /* The Sandybridge PRM says that the stencil buffer "is supported
116 * only in Tile W memory".
117 */
118
119 switch (anv_info->vk_info->tiling) {
120 case VK_IMAGE_TILING_LINEAR:
121 if (unlikely(anv_info->vk_info->format == VK_FORMAT_S8_UINT)) {
122 return -1;
123 } else {
124 return LINEAR;
125 }
126 case VK_IMAGE_TILING_OPTIMAL:
127 if (unlikely(anv_info->vk_info->format == VK_FORMAT_S8_UINT)) {
128 return WMAJOR;
129 } else {
130 return YMAJOR;
131 }
132 default:
133 assert(!"bad VKImageTiling");
134 return LINEAR;
135 }
136 }
137
138
139 /**
140 * The \a format argument is required and overrides any format in
141 * struct anv_image_create_info.
142 */
143 static VkResult
144 anv_image_make_surface(const struct anv_image_create_info *create_info,
145 const struct anv_format *format,
146 uint64_t *inout_image_size,
147 uint32_t *inout_image_alignment,
148 struct anv_surface *out_surface)
149 {
150 /* See RENDER_SURFACE_STATE.SurfaceQPitch */
151 static const uint16_t min_qpitch UNUSED = 0x4;
152 static const uint16_t max_qpitch UNUSED = 0x1ffc;
153
154 const VkExtent3D *restrict extent = &create_info->vk_info->extent;
155 const uint32_t levels = create_info->vk_info->mipLevels;
156 const uint32_t array_size = create_info->vk_info->arraySize;
157
158 const int8_t tile_mode = anv_image_choose_tile_mode(create_info);
159 if (tile_mode == -1)
160 return vk_error(VK_ERROR_INVALID_IMAGE);
161
162 const struct anv_tile_info *tile_info =
163 &anv_tile_info_table[tile_mode];
164
165 const uint32_t i = 4; /* FINISHME: Stop hardcoding subimage alignment */
166 const uint32_t j = 4; /* FINISHME: Stop hardcoding subimage alignment */
167 const uint32_t w0 = align_u32(extent->width, i);
168 const uint32_t h0 = align_u32(extent->height, j);
169
170 uint16_t qpitch;
171 uint32_t mt_width;
172 uint32_t mt_height;
173
174 if (levels == 1 && array_size == 1) {
175 qpitch = min_qpitch;
176 mt_width = w0;
177 mt_height = h0;
178 } else {
179 uint32_t w1 = align_u32(anv_minify(extent->width, 1), i);
180 uint32_t h1 = align_u32(anv_minify(extent->height, 1), j);
181 uint32_t w2 = align_u32(anv_minify(extent->width, 2), i);
182
183 qpitch = h0 + h1 + 11 * j;
184 mt_width = MAX(w0, w1 + w2);
185 mt_height = array_size * qpitch;
186 }
187
188 assert(qpitch >= min_qpitch);
189 if (qpitch > max_qpitch) {
190 anv_loge("image qpitch > 0x%x\n", max_qpitch);
191 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
192 }
193
194 /* From the Broadwell PRM, RENDER_SURFACE_STATE.SurfaceQpitch:
195 *
196 * This field must be set an integer multiple of the Surface Vertical
197 * Alignment.
198 */
199 assert(anv_is_aligned(qpitch, j));
200
201 uint32_t stride = align_u32(mt_width * format->cpp, tile_info->width);
202 if (create_info->stride > 0)
203 stride = create_info->stride;
204
205 const uint32_t size = stride * align_u32(mt_height, tile_info->height);
206 const uint32_t offset = align_u32(*inout_image_size,
207 tile_info->surface_alignment);
208
209 *inout_image_size = offset + size;
210 *inout_image_alignment = MAX(*inout_image_alignment,
211 tile_info->surface_alignment);
212
213 *out_surface = (struct anv_surface) {
214 .offset = offset,
215 .stride = stride,
216 .tile_mode = tile_mode,
217 .qpitch = qpitch,
218 .h_align = i,
219 .v_align = j,
220 };
221
222 return VK_SUCCESS;
223 }
224
225 VkResult
226 anv_image_create(VkDevice _device,
227 const struct anv_image_create_info *create_info,
228 VkImage *pImage)
229 {
230 ANV_FROM_HANDLE(anv_device, device, _device);
231 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
232 const VkExtent3D *restrict extent = &pCreateInfo->extent;
233 struct anv_image *image = NULL;
234 VkResult r;
235
236 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
237
238 /* XXX: We don't handle any of these */
239 anv_assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D);
240 anv_assert(pCreateInfo->mipLevels > 0);
241 anv_assert(pCreateInfo->arraySize > 0);
242 anv_assert(pCreateInfo->samples == 1);
243 anv_assert(pCreateInfo->extent.width > 0);
244 anv_assert(pCreateInfo->extent.height > 0);
245 anv_assert(pCreateInfo->extent.depth > 0);
246
247 /* TODO(chadv): How should we validate inputs? */
248 const uint8_t surf_type =
249 anv_surf_type_from_image_type[pCreateInfo->imageType];
250
251 const struct anv_surf_type_limits *limits =
252 &anv_surf_type_limits[surf_type];
253
254 if (extent->width > limits->width ||
255 extent->height > limits->height ||
256 extent->depth > limits->depth) {
257 /* TODO(chadv): What is the correct error? */
258 anv_loge("image extent is too large");
259 return vk_error(VK_ERROR_INVALID_MEMORY_SIZE);
260 }
261
262 image = anv_device_alloc(device, sizeof(*image), 8,
263 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
264 if (!image)
265 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
266
267 memset(image, 0, sizeof(*image));
268 image->type = pCreateInfo->imageType;
269 image->extent = pCreateInfo->extent;
270 image->format = anv_format_for_vk_format(pCreateInfo->format);
271 image->levels = pCreateInfo->mipLevels;
272 image->array_size = pCreateInfo->arraySize;
273 image->surf_type = surf_type;
274
275 if (likely(anv_format_is_color(image->format))) {
276 r = anv_image_make_surface(create_info, image->format,
277 &image->size, &image->alignment,
278 &image->color_surface);
279 if (r != VK_SUCCESS)
280 goto fail;
281 } else {
282 if (image->format->depth_format) {
283 r = anv_image_make_surface(create_info, image->format,
284 &image->size, &image->alignment,
285 &image->depth_surface);
286 if (r != VK_SUCCESS)
287 goto fail;
288 }
289
290 if (image->format->has_stencil) {
291 r = anv_image_make_surface(create_info, anv_format_s8_uint,
292 &image->size, &image->alignment,
293 &image->stencil_surface);
294 if (r != VK_SUCCESS)
295 goto fail;
296 }
297 }
298
299 *pImage = anv_image_to_handle(image);
300
301 return VK_SUCCESS;
302
303 fail:
304 if (image)
305 anv_device_free(device, image);
306
307 return r;
308 }
309
310 VkResult
311 anv_CreateImage(VkDevice device,
312 const VkImageCreateInfo *pCreateInfo,
313 VkImage *pImage)
314 {
315 return anv_image_create(device,
316 &(struct anv_image_create_info) {
317 .vk_info = pCreateInfo,
318 },
319 pImage);
320 }
321
322 VkResult
323 anv_DestroyImage(VkDevice _device, VkImage _image)
324 {
325 ANV_FROM_HANDLE(anv_device, device, _device);
326
327 anv_device_free(device, anv_image_from_handle(_image));
328
329 return VK_SUCCESS;
330 }
331
332 VkResult anv_GetImageSubresourceLayout(
333 VkDevice device,
334 VkImage image,
335 const VkImageSubresource* pSubresource,
336 VkSubresourceLayout* pLayout)
337 {
338 stub_return(VK_UNSUPPORTED);
339 }
340
341 void
342 anv_surface_view_fini(struct anv_device *device,
343 struct anv_surface_view *view)
344 {
345 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
346 }
347
348 VkResult
349 anv_validate_CreateImageView(VkDevice _device,
350 const VkImageViewCreateInfo *pCreateInfo,
351 VkImageView *pView)
352 {
353 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
354 const VkImageSubresourceRange *subresource;
355 const struct anv_image_view_info *view_info;
356 const struct anv_format *view_format_info;
357
358 /* Validate structure type before dereferencing it. */
359 assert(pCreateInfo);
360 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
361 subresource = &pCreateInfo->subresourceRange;
362
363 /* Validate viewType is in range before using it. */
364 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
365 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
366 view_info = &anv_image_view_info_table[pCreateInfo->viewType];
367
368 /* Validate format is in range before using it. */
369 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
370 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
371 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
372
373 /* Validate channel swizzles. */
374 assert(pCreateInfo->channels.r >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
375 assert(pCreateInfo->channels.r <= VK_CHANNEL_SWIZZLE_END_RANGE);
376 assert(pCreateInfo->channels.g >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
377 assert(pCreateInfo->channels.g <= VK_CHANNEL_SWIZZLE_END_RANGE);
378 assert(pCreateInfo->channels.b >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
379 assert(pCreateInfo->channels.b <= VK_CHANNEL_SWIZZLE_END_RANGE);
380 assert(pCreateInfo->channels.a >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
381 assert(pCreateInfo->channels.a <= VK_CHANNEL_SWIZZLE_END_RANGE);
382
383 /* Validate subresource. */
384 assert(subresource->aspect >= VK_IMAGE_ASPECT_BEGIN_RANGE);
385 assert(subresource->aspect <= VK_IMAGE_ASPECT_END_RANGE);
386 assert(subresource->mipLevels > 0);
387 assert(subresource->arraySize > 0);
388 assert(subresource->baseMipLevel < image->levels);
389 assert(subresource->baseMipLevel + subresource->mipLevels <= image->levels);
390 assert(subresource->baseArraySlice < image->array_size);
391 assert(subresource->baseArraySlice + subresource->arraySize <= image->array_size);
392 assert(pView);
393
394 if (view_info->is_cube) {
395 assert(subresource->baseArraySlice % 6 == 0);
396 assert(subresource->arraySize % 6 == 0);
397 }
398
399 /* Validate format. */
400 switch (subresource->aspect) {
401 case VK_IMAGE_ASPECT_COLOR:
402 assert(!image->format->depth_format);
403 assert(!image->format->has_stencil);
404 assert(!view_format_info->depth_format);
405 assert(!view_format_info->has_stencil);
406 assert(view_format_info->cpp == image->format->cpp);
407 break;
408 case VK_IMAGE_ASPECT_DEPTH:
409 assert(image->format->depth_format);
410 assert(view_format_info->depth_format);
411 assert(view_format_info->cpp == image->format->cpp);
412 break;
413 case VK_IMAGE_ASPECT_STENCIL:
414 /* FINISHME: Is it legal to have an R8 view of S8? */
415 assert(image->format->has_stencil);
416 assert(view_format_info->has_stencil);
417 break;
418 default:
419 assert(!"bad VkImageAspect");
420 break;
421 }
422
423 return anv_CreateImageView(_device, pCreateInfo, pView);
424 }
425
426 void
427 anv_image_view_init(struct anv_image_view *iview,
428 struct anv_device *device,
429 const VkImageViewCreateInfo* pCreateInfo,
430 struct anv_cmd_buffer *cmd_buffer)
431 {
432 switch (device->info.gen) {
433 case 7:
434 gen7_image_view_init(iview, device, pCreateInfo, cmd_buffer);
435 break;
436 case 8:
437 gen8_image_view_init(iview, device, pCreateInfo, cmd_buffer);
438 break;
439 default:
440 unreachable("unsupported gen\n");
441 }
442 }
443
444 VkResult
445 anv_CreateImageView(VkDevice _device,
446 const VkImageViewCreateInfo *pCreateInfo,
447 VkImageView *pView)
448 {
449 ANV_FROM_HANDLE(anv_device, device, _device);
450 struct anv_image_view *view;
451
452 view = anv_device_alloc(device, sizeof(*view), 8,
453 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
454 if (view == NULL)
455 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
456
457 anv_image_view_init(view, device, pCreateInfo, NULL);
458
459 *pView = anv_image_view_to_handle(view);
460
461 return VK_SUCCESS;
462 }
463
464 VkResult
465 anv_DestroyImageView(VkDevice _device, VkImageView _iview)
466 {
467 ANV_FROM_HANDLE(anv_device, device, _device);
468 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
469
470 anv_surface_view_fini(device, &iview->view);
471 anv_device_free(device, iview);
472
473 return VK_SUCCESS;
474 }
475
476 static void
477 anv_depth_stencil_view_init(struct anv_depth_stencil_view *view,
478 const VkAttachmentViewCreateInfo *pCreateInfo)
479 {
480 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
481
482 view->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL;
483
484 /* XXX: We don't handle any of these */
485 anv_assert(pCreateInfo->mipLevel == 0);
486 anv_assert(pCreateInfo->baseArraySlice == 0);
487 anv_assert(pCreateInfo->arraySize == 1);
488
489 view->image = image;
490 view->format = anv_format_for_vk_format(pCreateInfo->format);
491
492 assert(anv_format_is_depth_or_stencil(image->format));
493 assert(anv_format_is_depth_or_stencil(view->format));
494 }
495
496 struct anv_surface *
497 anv_image_get_surface_for_aspect(struct anv_image *image, VkImageAspect aspect)
498 {
499 switch (aspect) {
500 case VK_IMAGE_ASPECT_COLOR:
501 assert(anv_format_is_color(image->format));
502 return &image->color_surface;
503 case VK_IMAGE_ASPECT_DEPTH:
504 assert(image->format->depth_format);
505 return &image->depth_surface;
506 case VK_IMAGE_ASPECT_STENCIL:
507 assert(image->format->has_stencil);
508 anv_finishme("stencil image views");
509 return &image->stencil_surface;
510 default:
511 unreachable("image does not have aspect");
512 return NULL;
513 }
514 }
515
516 /** The attachment may be a color view into a non-color image. */
517 struct anv_surface *
518 anv_image_get_surface_for_color_attachment(struct anv_image *image)
519 {
520 if (anv_format_is_color(image->format)) {
521 return &image->color_surface;
522 } else if (image->format->depth_format) {
523 return &image->depth_surface;
524 } else if (image->format->has_stencil) {
525 return &image->stencil_surface;
526 } else {
527 unreachable("image has bad format");
528 return NULL;
529 }
530 }
531
532 void
533 anv_color_attachment_view_init(struct anv_color_attachment_view *aview,
534 struct anv_device *device,
535 const VkAttachmentViewCreateInfo* pCreateInfo,
536 struct anv_cmd_buffer *cmd_buffer)
537 {
538 switch (device->info.gen) {
539 case 7:
540 gen7_color_attachment_view_init(aview, device, pCreateInfo, cmd_buffer);
541 break;
542 case 8:
543 gen8_color_attachment_view_init(aview, device, pCreateInfo, cmd_buffer);
544 break;
545 default:
546 unreachable("unsupported gen\n");
547 }
548 }
549
550 VkResult
551 anv_CreateAttachmentView(VkDevice _device,
552 const VkAttachmentViewCreateInfo *pCreateInfo,
553 VkAttachmentView *pView)
554 {
555 ANV_FROM_HANDLE(anv_device, device, _device);
556
557 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO);
558
559 const struct anv_format *format =
560 anv_format_for_vk_format(pCreateInfo->format);
561
562 if (anv_format_is_depth_or_stencil(format)) {
563 struct anv_depth_stencil_view *view =
564 anv_device_alloc(device, sizeof(*view), 8,
565 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
566 if (view == NULL)
567 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
568
569 anv_depth_stencil_view_init(view, pCreateInfo);
570
571 *pView = anv_attachment_view_to_handle(&view->base);
572 } else {
573 struct anv_color_attachment_view *view =
574 anv_device_alloc(device, sizeof(*view), 8,
575 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
576 if (view == NULL)
577 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
578
579 anv_color_attachment_view_init(view, device, pCreateInfo, NULL);
580
581 *pView = anv_attachment_view_to_handle(&view->base);
582 }
583
584 return VK_SUCCESS;
585 }
586
587 VkResult
588 anv_DestroyAttachmentView(VkDevice _device, VkAttachmentView _view)
589 {
590 ANV_FROM_HANDLE(anv_device, device, _device);
591 ANV_FROM_HANDLE(anv_attachment_view, view, _view);
592
593 if (view->attachment_type == ANV_ATTACHMENT_VIEW_TYPE_COLOR) {
594 struct anv_color_attachment_view *aview =
595 (struct anv_color_attachment_view *)view;
596
597 anv_surface_view_fini(device, &aview->view);
598 }
599
600 anv_device_free(device, view);
601
602 return VK_SUCCESS;
603 }