2 * Copyright © 2015 Intel Corporation
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:
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
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
30 #include "anv_private.h"
32 static const uint8_t anv_halign
[] = {
38 static const uint8_t anv_valign
[] = {
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
,
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),
64 const struct anv_image_view_info
*
65 anv_image_view_info_for_vk_image_view_type(VkImageViewType type
)
67 return &anv_image_view_info_table
[type
];
70 static const struct anv_surf_type_limits
{
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},
83 static const struct anv_tile_info
{
88 * Alignment for RENDER_SURFACE_STATE.SurfaceBaseAddress.
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
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 },
107 anv_image_choose_tile_mode(const struct anv_image_create_info
*anv_info
)
109 if (anv_info
->force_tile_mode
)
110 return anv_info
->tile_mode
;
112 if (anv_info
->vk_info
->format
== VK_FORMAT_S8_UINT
)
115 switch (anv_info
->vk_info
->tiling
) {
116 case VK_IMAGE_TILING_LINEAR
:
118 case VK_IMAGE_TILING_OPTIMAL
:
121 assert(!"bad VKImageTiling");
128 * The \a format argument is required and overrides any format in
129 * struct anv_image_create_info.
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
)
138 /* See RENDER_SURFACE_STATE.SurfaceQPitch */
139 static const uint16_t min_qpitch UNUSED
= 0x4;
140 static const uint16_t max_qpitch UNUSED
= 0x1ffc;
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
;
146 const uint8_t tile_mode
= anv_image_choose_tile_mode(create_info
);
148 const struct anv_tile_info
*tile_info
=
149 &anv_tile_info_table
[tile_mode
];
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
);
160 if (levels
== 1 && array_size
== 1) {
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
);
169 qpitch
= h0
+ h1
+ 11 * j
;
170 mt_width
= MAX(w0
, w1
+ w2
);
171 mt_height
= array_size
* qpitch
;
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
);
180 /* From the Broadwell PRM, RENDER_SURFACE_STATE.SurfaceQpitch:
182 * This field must be set an integer multiple of the Surface Vertical
185 assert(anv_is_aligned(qpitch
, j
));
187 uint32_t stride
= align_u32(mt_width
* format
->cpp
, tile_info
->width
);
188 if (create_info
->stride
> 0)
189 stride
= create_info
->stride
;
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
);
195 *inout_image_size
= offset
+ size
;
196 *inout_image_alignment
= MAX(*inout_image_alignment
,
197 tile_info
->surface_alignment
);
199 *out_surface
= (struct anv_surface
) {
202 .tile_mode
= tile_mode
,
212 anv_image_create(VkDevice _device
,
213 const struct anv_image_create_info
*create_info
,
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
;
222 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
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);
233 /* TODO(chadv): How should we validate inputs? */
234 const uint8_t surf_type
=
235 anv_surf_type_from_image_type
[pCreateInfo
->imageType
];
237 const struct anv_surf_type_limits
*limits
=
238 &anv_surf_type_limits
[surf_type
];
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
);
248 image
= anv_device_alloc(device
, sizeof(*image
), 8,
249 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
251 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
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
;
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
);
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.
276 r
= anv_image_make_surface(create_info
, anv_format_s8_uint
,
277 &image
->size
, &image
->alignment
,
278 &image
->stencil_surface
);
283 *pImage
= anv_image_to_handle(image
);
289 anv_device_free(device
, image
);
295 anv_CreateImage(VkDevice device
,
296 const VkImageCreateInfo
*pCreateInfo
,
299 return anv_image_create(device
,
300 &(struct anv_image_create_info
) {
301 .vk_info
= pCreateInfo
,
307 anv_DestroyImage(VkDevice _device
, VkImage _image
)
309 ANV_FROM_HANDLE(anv_device
, device
, _device
);
311 anv_device_free(device
, anv_image_from_handle(_image
));
316 VkResult
anv_GetImageSubresourceLayout(
319 const VkImageSubresource
* pSubresource
,
320 VkSubresourceLayout
* pLayout
)
322 stub_return(VK_UNSUPPORTED
);
326 anv_surface_view_fini(struct anv_device
*device
,
327 struct anv_surface_view
*view
)
329 anv_state_pool_free(&device
->surface_state_pool
, view
->surface_state
);
333 anv_validate_CreateImageView(VkDevice _device
,
334 const VkImageViewCreateInfo
*pCreateInfo
,
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
;
342 /* Validate structure type before dereferencing it. */
344 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
345 subresource
= &pCreateInfo
->subresourceRange
;
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
];
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
);
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
);
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
);
378 if (view_info
->is_cube
) {
379 assert(subresource
->baseArraySlice
% 6 == 0);
380 assert(subresource
->arraySize
% 6 == 0);
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
);
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
);
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
);
403 assert(!"bad VkImageAspect");
407 return anv_CreateImageView(_device
, pCreateInfo
, pView
);
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
)
416 switch (device
->info
.gen
) {
418 gen7_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
421 gen8_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
424 unreachable("unsupported gen\n");
429 anv_CreateImageView(VkDevice _device
,
430 const VkImageViewCreateInfo
*pCreateInfo
,
433 ANV_FROM_HANDLE(anv_device
, device
, _device
);
434 struct anv_image_view
*view
;
436 view
= anv_device_alloc(device
, sizeof(*view
), 8,
437 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
439 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
441 anv_image_view_init(view
, device
, pCreateInfo
, NULL
);
443 *pView
= anv_image_view_to_handle(view
);
449 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
)
451 ANV_FROM_HANDLE(anv_device
, device
, _device
);
452 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
454 anv_surface_view_fini(device
, &iview
->view
);
455 anv_device_free(device
, iview
);
461 anv_depth_stencil_view_init(struct anv_depth_stencil_view
*view
,
462 const VkAttachmentViewCreateInfo
*pCreateInfo
)
464 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
465 struct anv_surface
*depth_surface
= &image
->primary_surface
;
466 struct anv_surface
*stencil_surface
= &image
->stencil_surface
;
468 view
->base
.attachment_type
= ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL
;
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);
475 view
->bo
= image
->bo
;
477 view
->depth_stride
= depth_surface
->stride
;
478 view
->depth_offset
= image
->offset
+ depth_surface
->offset
;
479 view
->depth_format
= image
->format
->depth_format
;
480 view
->depth_qpitch
= 0; /* FINISHME: QPitch */
482 view
->stencil_stride
= stencil_surface
->stride
;
483 view
->stencil_offset
= image
->offset
+ stencil_surface
->offset
;
484 view
->stencil_qpitch
= 0; /* FINISHME: QPitch */
488 anv_color_attachment_view_init(struct anv_color_attachment_view
*aview
,
489 struct anv_device
*device
,
490 const VkAttachmentViewCreateInfo
* pCreateInfo
,
491 struct anv_cmd_buffer
*cmd_buffer
)
493 switch (device
->info
.gen
) {
495 gen7_color_attachment_view_init(aview
, device
, pCreateInfo
, cmd_buffer
);
498 gen8_color_attachment_view_init(aview
, device
, pCreateInfo
, cmd_buffer
);
501 unreachable("unsupported gen\n");
506 anv_CreateAttachmentView(VkDevice _device
,
507 const VkAttachmentViewCreateInfo
*pCreateInfo
,
508 VkAttachmentView
*pView
)
510 ANV_FROM_HANDLE(anv_device
, device
, _device
);
512 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO
);
514 const struct anv_format
*format
=
515 anv_format_for_vk_format(pCreateInfo
->format
);
517 if (anv_format_is_depth_or_stencil(format
)) {
518 struct anv_depth_stencil_view
*view
=
519 anv_device_alloc(device
, sizeof(*view
), 8,
520 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
522 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
524 anv_depth_stencil_view_init(view
, pCreateInfo
);
526 *pView
= anv_attachment_view_to_handle(&view
->base
);
528 struct anv_color_attachment_view
*view
=
529 anv_device_alloc(device
, sizeof(*view
), 8,
530 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
532 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
534 anv_color_attachment_view_init(view
, device
, pCreateInfo
, NULL
);
536 *pView
= anv_attachment_view_to_handle(&view
->base
);
543 anv_DestroyAttachmentView(VkDevice _device
, VkAttachmentView _view
)
545 ANV_FROM_HANDLE(anv_device
, device
, _device
);
546 ANV_FROM_HANDLE(anv_attachment_view
, view
, _view
);
548 if (view
->attachment_type
== ANV_ATTACHMENT_VIEW_TYPE_COLOR
) {
549 struct anv_color_attachment_view
*aview
=
550 (struct anv_color_attachment_view
*)view
;
552 anv_surface_view_fini(device
, &aview
->view
);
555 anv_device_free(device
, view
);