51f2cf5244c85ecef25c1f13fd69850c26e4100c
[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(!image->format->has_stencil || image->format->depth_format)) {
262 /* The image's primary surface is a color or depth surface. */
263 r = anv_image_make_surface(create_info, image->format,
264 &image->size, &image->alignment,
265 &image->primary_surface);
266 if (r != VK_SUCCESS)
267 goto fail;
268 }
269
270 if (image->format->has_stencil) {
271 /* From the GPU's perspective, the depth buffer and stencil buffer are
272 * separate buffers. From Vulkan's perspective, though, depth and
273 * stencil reside in the same image. To satisfy Vulkan and the GPU, we
274 * place the depth and stencil buffers in the same bo.
275 */
276 r = anv_image_make_surface(create_info, anv_format_s8_uint,
277 &image->size, &image->alignment,
278 &image->stencil_surface);
279 if (r != VK_SUCCESS)
280 goto fail;
281 }
282
283 *pImage = anv_image_to_handle(image);
284
285 return VK_SUCCESS;
286
287 fail:
288 if (image)
289 anv_device_free(device, image);
290
291 return r;
292 }
293
294 VkResult
295 anv_CreateImage(VkDevice device,
296 const VkImageCreateInfo *pCreateInfo,
297 VkImage *pImage)
298 {
299 return anv_image_create(device,
300 &(struct anv_image_create_info) {
301 .vk_info = pCreateInfo,
302 },
303 pImage);
304 }
305
306 VkResult
307 anv_DestroyImage(VkDevice _device, VkImage _image)
308 {
309 ANV_FROM_HANDLE(anv_device, device, _device);
310
311 anv_device_free(device, anv_image_from_handle(_image));
312
313 return VK_SUCCESS;
314 }
315
316 VkResult anv_GetImageSubresourceLayout(
317 VkDevice device,
318 VkImage image,
319 const VkImageSubresource* pSubresource,
320 VkSubresourceLayout* pLayout)
321 {
322 stub_return(VK_UNSUPPORTED);
323 }
324
325 void
326 anv_surface_view_fini(struct anv_device *device,
327 struct anv_surface_view *view)
328 {
329 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
330 }
331
332 VkResult
333 anv_validate_CreateImageView(VkDevice _device,
334 const VkImageViewCreateInfo *pCreateInfo,
335 VkImageView *pView)
336 {
337 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
338 const VkImageSubresourceRange *subresource;
339 const struct anv_image_view_info *view_info;
340 const struct anv_format *view_format_info;
341
342 /* Validate structure type before dereferencing it. */
343 assert(pCreateInfo);
344 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
345 subresource = &pCreateInfo->subresourceRange;
346
347 /* Validate viewType is in range before using it. */
348 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
349 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
350 view_info = &anv_image_view_info_table[pCreateInfo->viewType];
351
352 /* Validate format is in range before using it. */
353 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
354 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
355 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
356
357 /* Validate channel swizzles. */
358 assert(pCreateInfo->channels.r >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
359 assert(pCreateInfo->channels.r <= VK_CHANNEL_SWIZZLE_END_RANGE);
360 assert(pCreateInfo->channels.g >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
361 assert(pCreateInfo->channels.g <= VK_CHANNEL_SWIZZLE_END_RANGE);
362 assert(pCreateInfo->channels.b >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
363 assert(pCreateInfo->channels.b <= VK_CHANNEL_SWIZZLE_END_RANGE);
364 assert(pCreateInfo->channels.a >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
365 assert(pCreateInfo->channels.a <= VK_CHANNEL_SWIZZLE_END_RANGE);
366
367 /* Validate subresource. */
368 assert(subresource->aspect >= VK_IMAGE_ASPECT_BEGIN_RANGE);
369 assert(subresource->aspect <= VK_IMAGE_ASPECT_END_RANGE);
370 assert(subresource->mipLevels > 0);
371 assert(subresource->arraySize > 0);
372 assert(subresource->baseMipLevel < image->levels);
373 assert(subresource->baseMipLevel + subresource->mipLevels <= image->levels);
374 assert(subresource->baseArraySlice < image->array_size);
375 assert(subresource->baseArraySlice + subresource->arraySize <= image->array_size);
376 assert(pView);
377
378 if (view_info->is_cube) {
379 assert(subresource->baseArraySlice % 6 == 0);
380 assert(subresource->arraySize % 6 == 0);
381 }
382
383 /* Validate format. */
384 switch (subresource->aspect) {
385 case VK_IMAGE_ASPECT_COLOR:
386 assert(!image->format->depth_format);
387 assert(!image->format->has_stencil);
388 assert(!view_format_info->depth_format);
389 assert(!view_format_info->has_stencil);
390 assert(view_format_info->cpp == image->format->cpp);
391 break;
392 case VK_IMAGE_ASPECT_DEPTH:
393 assert(image->format->depth_format);
394 assert(view_format_info->depth_format);
395 assert(view_format_info->cpp == image->format->cpp);
396 break;
397 case VK_IMAGE_ASPECT_STENCIL:
398 /* FINISHME: Is it legal to have an R8 view of S8? */
399 assert(image->format->has_stencil);
400 assert(view_format_info->has_stencil);
401 break;
402 default:
403 assert(!"bad VkImageAspect");
404 break;
405 }
406
407 return anv_CreateImageView(_device, pCreateInfo, pView);
408 }
409
410 void
411 anv_image_view_init(struct anv_image_view *iview,
412 struct anv_device *device,
413 const VkImageViewCreateInfo* pCreateInfo,
414 struct anv_cmd_buffer *cmd_buffer)
415 {
416 switch (device->info.gen) {
417 case 8:
418 gen8_image_view_init(iview, device, pCreateInfo, cmd_buffer);
419 break;
420 default:
421 unreachable("unsupported gen\n");
422 }
423 }
424
425 VkResult
426 anv_CreateImageView(VkDevice _device,
427 const VkImageViewCreateInfo *pCreateInfo,
428 VkImageView *pView)
429 {
430 ANV_FROM_HANDLE(anv_device, device, _device);
431
432 switch (device->info.gen) {
433 case 8:
434 return gen8_CreateImageView(_device, pCreateInfo, pView);
435 default:
436 unreachable("unsupported gen\n");
437 }
438 }
439
440
441 VkResult
442 anv_DestroyImageView(VkDevice _device, VkImageView _iview)
443 {
444 ANV_FROM_HANDLE(anv_device, device, _device);
445 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
446
447 anv_surface_view_fini(device, &iview->view);
448 anv_device_free(device, iview);
449
450 return VK_SUCCESS;
451 }
452
453 static void
454 anv_depth_stencil_view_init(struct anv_depth_stencil_view *view,
455 const VkAttachmentViewCreateInfo *pCreateInfo)
456 {
457 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
458 struct anv_surface *depth_surface = &image->primary_surface;
459 struct anv_surface *stencil_surface = &image->stencil_surface;
460
461 view->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL;
462
463 /* XXX: We don't handle any of these */
464 anv_assert(pCreateInfo->mipLevel == 0);
465 anv_assert(pCreateInfo->baseArraySlice == 0);
466 anv_assert(pCreateInfo->arraySize == 1);
467
468 view->bo = image->bo;
469
470 view->depth_stride = depth_surface->stride;
471 view->depth_offset = image->offset + depth_surface->offset;
472 view->depth_format = image->format->depth_format;
473 view->depth_qpitch = 0; /* FINISHME: QPitch */
474
475 view->stencil_stride = stencil_surface->stride;
476 view->stencil_offset = image->offset + stencil_surface->offset;
477 view->stencil_qpitch = 0; /* FINISHME: QPitch */
478 }
479
480 void
481 anv_color_attachment_view_init(struct anv_color_attachment_view *aview,
482 struct anv_device *device,
483 const VkAttachmentViewCreateInfo* pCreateInfo,
484 struct anv_cmd_buffer *cmd_buffer)
485 {
486 switch (device->info.gen) {
487 case 8:
488 gen8_color_attachment_view_init(aview, device, pCreateInfo, cmd_buffer);
489 break;
490 default:
491 unreachable("unsupported gen\n");
492 }
493 }
494
495 VkResult
496 anv_CreateAttachmentView(VkDevice _device,
497 const VkAttachmentViewCreateInfo *pCreateInfo,
498 VkAttachmentView *pView)
499 {
500 ANV_FROM_HANDLE(anv_device, device, _device);
501
502 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO);
503
504 const struct anv_format *format =
505 anv_format_for_vk_format(pCreateInfo->format);
506
507 if (anv_format_is_depth_or_stencil(format)) {
508 struct anv_depth_stencil_view *view =
509 anv_device_alloc(device, sizeof(*view), 8,
510 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
511 if (view == NULL)
512 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
513
514 anv_depth_stencil_view_init(view, pCreateInfo);
515
516 *pView = anv_attachment_view_to_handle(&view->base);
517 } else {
518 struct anv_color_attachment_view *view =
519 anv_device_alloc(device, sizeof(*view), 8,
520 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
521 if (view == NULL)
522 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
523
524 anv_color_attachment_view_init(view, device, pCreateInfo, NULL);
525
526 *pView = anv_attachment_view_to_handle(&view->base);
527 }
528
529 return VK_SUCCESS;
530 }
531
532 VkResult
533 anv_DestroyAttachmentView(VkDevice _device, VkAttachmentView _view)
534 {
535 ANV_FROM_HANDLE(anv_device, device, _device);
536 ANV_FROM_HANDLE(anv_attachment_view, view, _view);
537
538 if (view->attachment_type == ANV_ATTACHMENT_VIEW_TYPE_COLOR) {
539 struct anv_color_attachment_view *aview =
540 (struct anv_color_attachment_view *)view;
541
542 anv_surface_view_fini(device, &aview->view);
543 }
544
545 anv_device_free(device, view);
546
547 return VK_SUCCESS;
548 }