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