blob: 8417177105c25cc690f6e303d3ffa2a267d893be [file] [log] [blame]
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001/*
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
Chad Versace2c2233e2015-07-17 15:04:27 -070030#include "anv_private.h"
Kristian Høgsberg769785c2015-05-08 22:32:37 -070031
Chad Versaceb3693892015-12-02 16:46:16 -080032/**
33 * The \a format argument is required and overrides any format found in struct
Chad Versace2f270f02015-12-04 16:29:25 -080034 * anv_image_create_info. Exactly one bit must be set in \a aspect.
Chad Versaceb3693892015-12-02 16:46:16 -080035 */
36static isl_surf_usage_flags_t
37choose_isl_surf_usage(const struct anv_image_create_info *info,
Chad Versace2f270f02015-12-04 16:29:25 -080038 VkImageAspectFlags aspect)
Chad Versaceb3693892015-12-02 16:46:16 -080039{
40 const VkImageCreateInfo *vk_info = info->vk_info;
41 isl_surf_usage_flags_t isl_flags = 0;
42
43 /* FINISHME: Support aux surfaces */
44 isl_flags |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
45
46 if (vk_info->usage & VK_IMAGE_USAGE_SAMPLED_BIT)
47 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
48
49 if (vk_info->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
50 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
51
52 if (vk_info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
53 isl_flags |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
54
55 if (vk_info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
56 isl_flags |= ISL_SURF_USAGE_CUBE_BIT;
57
58 if (vk_info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
Chad Versace2f270f02015-12-04 16:29:25 -080059 switch (aspect) {
60 default:
61 unreachable("bad VkImageAspect");
62 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versaceb3693892015-12-02 16:46:16 -080063 isl_flags |= ISL_SURF_USAGE_DEPTH_BIT;
Chad Versace2f270f02015-12-04 16:29:25 -080064 break;
65 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versaceb3693892015-12-02 16:46:16 -080066 isl_flags |= ISL_SURF_USAGE_STENCIL_BIT;
Chad Versace2f270f02015-12-04 16:29:25 -080067 break;
Chad Versaceb3693892015-12-02 16:46:16 -080068 }
69 }
70
71 if (vk_info->usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
72 /* Meta implements transfers by sampling from the source image. */
73 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
74 }
75
76 if (vk_info->usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
77 /* Meta implements transfers by rendering into the destination image. */
78 isl_flags |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
79 }
80
81 return isl_flags;
82}
Chad Versace5a6b2e62015-08-17 13:50:43 -070083
84/**
Chad Versace9098e0f2015-12-07 09:22:49 -080085 * Exactly one bit must be set in \a aspect.
86 */
87static struct anv_surface *
88get_surface(struct anv_image *image, VkImageAspectFlags aspect)
89{
90 switch (aspect) {
91 default:
92 unreachable("bad VkImageAspect");
93 case VK_IMAGE_ASPECT_COLOR_BIT:
94 return &image->color_surface;
95 case VK_IMAGE_ASPECT_DEPTH_BIT:
96 return &image->depth_surface;
97 case VK_IMAGE_ASPECT_STENCIL_BIT:
98 return &image->stencil_surface;
99 }
100}
101
102/**
103 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
104 * image's memory requirements (that is, the image's size and alignment).
105 *
106 * Exactly one bit must be set in \a aspect.
Chad Versace5a6b2e62015-08-17 13:50:43 -0700107 */
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700108static VkResult
Chad Versace9098e0f2015-12-07 09:22:49 -0800109make_surface(const struct anv_device *dev,
110 struct anv_image *image,
111 const struct anv_image_create_info *anv_info,
112 VkImageAspectFlags aspect)
Chad Versacec6e76ae2015-06-26 18:48:34 -0700113{
Chad Versaceb3693892015-12-02 16:46:16 -0800114 const VkImageCreateInfo *vk_info = anv_info->vk_info;
Chad Versace3d85a282015-12-07 08:53:43 -0800115 bool ok UNUSED;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700116
Chad Versaceb3693892015-12-02 16:46:16 -0800117 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
118 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
119 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
120 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
121 };
Chad Versacec6e76ae2015-06-26 18:48:34 -0700122
Chad Versace64e8af62015-12-07 08:50:28 -0800123 isl_tiling_flags_t tiling_flags = anv_info->isl_tiling_flags;
124 if (vk_info->tiling == VK_IMAGE_TILING_LINEAR)
125 tiling_flags &= ISL_TILING_LINEAR_BIT;
126
Chad Versace9098e0f2015-12-07 09:22:49 -0800127 struct anv_surface *anv_surf = get_surface(image, aspect);
128
129 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
Chad Versaceb3693892015-12-02 16:46:16 -0800130 .dim = vk_to_isl_surf_dim[vk_info->imageType],
Jason Ekstrand2712c0c2015-12-31 18:31:31 -0800131 .format = anv_get_isl_format(vk_info->format, aspect, vk_info->tiling),
Chad Versaceb3693892015-12-02 16:46:16 -0800132 .width = vk_info->extent.width,
133 .height = vk_info->extent.height,
134 .depth = vk_info->extent.depth,
135 .levels = vk_info->mipLevels,
136 .array_len = vk_info->arrayLayers,
137 .samples = vk_info->samples,
138 .min_alignment = 0,
139 .min_pitch = 0,
Chad Versace2f270f02015-12-04 16:29:25 -0800140 .usage = choose_isl_surf_usage(anv_info, aspect),
Chad Versace64e8af62015-12-07 08:50:28 -0800141 .tiling_flags = tiling_flags);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700142
Chad Versace3d85a282015-12-07 08:53:43 -0800143 /* isl_surf_init() will fail only if provided invalid input. Invalid input
144 * is illegal in Vulkan.
145 */
146 assert(ok);
147
Chad Versace9098e0f2015-12-07 09:22:49 -0800148 anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment);
149 image->size = anv_surf->offset + anv_surf->isl.size;
150 image->alignment = MAX(image->alignment, anv_surf->isl.alignment);
Chad Versaceb3693892015-12-02 16:46:16 -0800151
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700152 return VK_SUCCESS;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700153}
154
Chad Versace24de3d42015-10-06 19:11:58 -0700155static VkImageUsageFlags
156anv_image_get_full_usage(const VkImageCreateInfo *info)
157{
158 VkImageUsageFlags usage = info->usage;
159
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800160 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700161 /* Meta will transfer from the image by binding it as a texture. */
162 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
163 }
164
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800165 if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700166 /* Meta will transfer to the image by binding it as a color attachment,
167 * even if the image format is not a color format.
168 */
169 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
170 }
171
172 return usage;
173}
174
Chad Versace127cb3f2015-06-26 20:12:42 -0700175VkResult
176anv_image_create(VkDevice _device,
177 const struct anv_image_create_info *create_info,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800178 const VkAllocationCallbacks* alloc,
Chad Versace127cb3f2015-06-26 20:12:42 -0700179 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700180{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700181 ANV_FROM_HANDLE(anv_device, device, _device);
Chad Versacefdcd71f2015-06-26 20:06:08 -0700182 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700183 struct anv_image *image = NULL;
184 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700185
186 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
187
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700188 anv_assert(pCreateInfo->mipLevels > 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800189 anv_assert(pCreateInfo->arrayLayers > 0);
Chad Versaced96d78c2016-01-22 17:16:20 -0800190 anv_assert(pCreateInfo->samples > 0);
Chad Versace5d7103e2015-06-26 09:05:46 -0700191 anv_assert(pCreateInfo->extent.width > 0);
192 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700193 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700194
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800195 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
196 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700197 if (!image)
198 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
Chad Versace67a76592015-06-26 09:17:52 -0700199
Chad Versacec6e76ae2015-06-26 18:48:34 -0700200 memset(image, 0, sizeof(*image));
201 image->type = pCreateInfo->imageType;
202 image->extent = pCreateInfo->extent;
Jason Ekstrand3200a812015-12-31 12:39:34 -0800203 image->vk_format = pCreateInfo->format;
Chad Versaceded736f2015-08-17 13:10:40 -0700204 image->format = anv_format_for_vk_format(pCreateInfo->format);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700205 image->levels = pCreateInfo->mipLevels;
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800206 image->array_size = pCreateInfo->arrayLayers;
Chad Versacedfcb4ee2016-01-20 16:34:23 -0800207 image->samples = pCreateInfo->samples;
Chad Versace24de3d42015-10-06 19:11:58 -0700208 image->usage = anv_image_get_full_usage(pCreateInfo);
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800209 image->tiling = pCreateInfo->tiling;
Chad Versace67a76592015-06-26 09:17:52 -0700210
Jason Ekstrandff05f632015-12-07 17:17:30 -0800211 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Chad Versace24de3d42015-10-06 19:11:58 -0700212 image->needs_nonrt_surface_state = true;
Chad Versace44143a12015-10-06 18:17:09 -0700213 }
214
Chad Versace24de3d42015-10-06 19:11:58 -0700215 if (image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
216 image->needs_color_rt_surface_state = true;
Chad Versace44143a12015-10-06 18:17:09 -0700217 }
218
Jason Ekstrandff05f632015-12-07 17:17:30 -0800219 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
220 image->needs_storage_surface_state = true;
221 }
222
Chad Versace941b48e2015-08-28 07:08:58 -0700223 if (likely(anv_format_is_color(image->format))) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800224 r = make_surface(device, image, create_info,
225 VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700226 if (r != VK_SUCCESS)
227 goto fail;
Chad Versace941b48e2015-08-28 07:08:58 -0700228 } else {
229 if (image->format->depth_format) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800230 r = make_surface(device, image, create_info,
231 VK_IMAGE_ASPECT_DEPTH_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700232 if (r != VK_SUCCESS)
233 goto fail;
234 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700235
Chad Versace941b48e2015-08-28 07:08:58 -0700236 if (image->format->has_stencil) {
Chad Versace9098e0f2015-12-07 09:22:49 -0800237 r = make_surface(device, image, create_info,
238 VK_IMAGE_ASPECT_STENCIL_BIT);
Chad Versace941b48e2015-08-28 07:08:58 -0700239 if (r != VK_SUCCESS)
240 goto fail;
241 }
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700242 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700243
Jason Ekstranda52e2082015-07-09 20:24:07 -0700244 *pImage = anv_image_to_handle(image);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700245
246 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700247
248fail:
249 if (image)
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800250 anv_free2(&device->alloc, alloc, image);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700251
252 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700253}
254
Chad Versace127cb3f2015-06-26 20:12:42 -0700255VkResult
256anv_CreateImage(VkDevice device,
257 const VkImageCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800258 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700259 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700260{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700261 return anv_image_create(device,
262 &(struct anv_image_create_info) {
263 .vk_info = pCreateInfo,
Chad Versace64e8af62015-12-07 08:50:28 -0800264 .isl_tiling_flags = ISL_TILING_ANY_MASK,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700265 },
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800266 pAllocator,
Chad Versacefdcd71f2015-06-26 20:06:08 -0700267 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700268}
269
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700270void
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800271anv_DestroyImage(VkDevice _device, VkImage _image,
272 const VkAllocationCallbacks *pAllocator)
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700273{
274 ANV_FROM_HANDLE(anv_device, device, _device);
275
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800276 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
Jason Ekstrand8b342b32015-07-10 12:30:58 -0700277}
278
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700279static void
280anv_surface_get_subresource_layout(struct anv_image *image,
281 struct anv_surface *surface,
282 const VkImageSubresource *subresource,
283 VkSubresourceLayout *layout)
284{
285 /* If we are on a non-zero mip level or array slice, we need to
286 * calculate a real offset.
287 */
288 anv_assert(subresource->mipLevel == 0);
289 anv_assert(subresource->arrayLayer == 0);
290
291 layout->offset = surface->offset;
Chad Versace981ef2f2015-12-03 08:40:47 -0800292 layout->rowPitch = surface->isl.row_pitch;
293 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
Jason Ekstrand8a81d132016-01-06 19:27:10 -0800294 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
Chad Versace981ef2f2015-12-03 08:40:47 -0800295 layout->size = surface->isl.size;
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700296}
297
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800298void anv_GetImageSubresourceLayout(
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700299 VkDevice device,
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700300 VkImage _image,
Jason Ekstranddb24afe2015-07-07 18:20:18 -0700301 const VkImageSubresource* pSubresource,
302 VkSubresourceLayout* pLayout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700303{
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700304 ANV_FROM_HANDLE(anv_image, image, _image);
305
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800306 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
307
308 switch (pSubresource->aspectMask) {
309 case VK_IMAGE_ASPECT_COLOR_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700310 anv_surface_get_subresource_layout(image, &image->color_surface,
311 pSubresource, pLayout);
312 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800313 case VK_IMAGE_ASPECT_DEPTH_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700314 anv_surface_get_subresource_layout(image, &image->depth_surface,
315 pSubresource, pLayout);
316 break;
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800317 case VK_IMAGE_ASPECT_STENCIL_BIT:
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700318 anv_surface_get_subresource_layout(image, &image->stencil_surface,
319 pSubresource, pLayout);
320 break;
321 default:
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800322 assert(!"Invalid image aspect");
Jason Ekstranddb5a5fc2015-10-13 15:50:02 -0700323 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700324}
325
Chad Versace127cb3f2015-06-26 20:12:42 -0700326VkResult
Chad Versace5b04db72015-07-06 16:24:28 -0700327anv_validate_CreateImageView(VkDevice _device,
328 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800329 const VkAllocationCallbacks *pAllocator,
Chad Versace5b04db72015-07-06 16:24:28 -0700330 VkImageView *pView)
331{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700332 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace23075bc2015-07-06 17:04:13 -0700333 const VkImageSubresourceRange *subresource;
Chad Versace23075bc2015-07-06 17:04:13 -0700334 const struct anv_format *view_format_info;
Chad Versace5b04db72015-07-06 16:24:28 -0700335
Chad Versace23075bc2015-07-06 17:04:13 -0700336 /* Validate structure type before dereferencing it. */
Chad Versace5b04db72015-07-06 16:24:28 -0700337 assert(pCreateInfo);
338 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chad Versace23075bc2015-07-06 17:04:13 -0700339 subresource = &pCreateInfo->subresourceRange;
340
Chad Versace23075bc2015-07-06 17:04:13 -0700341 /* Validate viewType is in range before using it. */
342 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
343 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700344
345 /* Validate format is in range before using it. */
346 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
347 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700348 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
349
350 /* Validate channel swizzles. */
Jason Ekstranda53f23d2015-11-30 13:06:12 -0800351 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
352 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
353 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
354 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
355 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
356 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
357 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
358 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
Chad Versace23075bc2015-07-06 17:04:13 -0700359
360 /* Validate subresource. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700361 assert(subresource->aspectMask != 0);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800362 assert(subresource->levelCount > 0);
363 assert(subresource->layerCount > 0);
Chad Versace23075bc2015-07-06 17:04:13 -0700364 assert(subresource->baseMipLevel < image->levels);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800365 assert(subresource->baseMipLevel + subresource->levelCount <= image->levels);
Jason Ekstrand1e4263b2015-10-06 10:27:50 -0700366 assert(subresource->baseArrayLayer < image->array_size);
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800367 assert(subresource->baseArrayLayer + subresource->layerCount <= image->array_size);
Chad Versace5b04db72015-07-06 16:24:28 -0700368 assert(pView);
369
Chad Versace7a089bd2015-10-05 06:48:14 -0700370 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
371 | VK_IMAGE_ASPECT_STENCIL_BIT;
372
Chad Versace23075bc2015-07-06 17:04:13 -0700373 /* Validate format. */
Chad Versace7a089bd2015-10-05 06:48:14 -0700374 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
375 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
Chad Versaceded736f2015-08-17 13:10:40 -0700376 assert(!image->format->depth_format);
377 assert(!image->format->has_stencil);
Chad Versace23075bc2015-07-06 17:04:13 -0700378 assert(!view_format_info->depth_format);
379 assert(!view_format_info->has_stencil);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800380 assert(view_format_info->isl_layout->bs ==
381 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700382 } else if (subresource->aspectMask & ds_flags) {
383 assert((subresource->aspectMask & ~ds_flags) == 0);
384
385 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
386 assert(image->format->depth_format);
387 assert(view_format_info->depth_format);
Chad Versaceaddc2a92015-11-12 12:14:43 -0800388 assert(view_format_info->isl_layout->bs ==
389 image->format->isl_layout->bs);
Chad Versace7a089bd2015-10-05 06:48:14 -0700390 }
391
Jason Ekstrand407b8cc2015-12-01 12:19:11 -0800392 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Chad Versace7a089bd2015-10-05 06:48:14 -0700393 /* FINISHME: Is it legal to have an R8 view of S8? */
394 assert(image->format->has_stencil);
395 assert(view_format_info->has_stencil);
396 }
397 } else {
398 assert(!"bad VkImageSubresourceRange::aspectFlags");
Chad Versace23075bc2015-07-06 17:04:13 -0700399 }
Chad Versace5b04db72015-07-06 16:24:28 -0700400
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800401 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
Chad Versace5b04db72015-07-06 16:24:28 -0700402}
403
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700404void
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800405anv_fill_image_surface_state(struct anv_device *device, struct anv_state state,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800406 struct anv_image_view *iview,
407 const VkImageViewCreateInfo *pCreateInfo,
408 VkImageUsageFlagBits usage)
409{
410 switch (device->info.gen) {
411 case 7:
412 if (device->info.is_haswell)
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800413 gen75_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800414 pCreateInfo, usage);
415 else
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800416 gen7_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800417 pCreateInfo, usage);
418 break;
419 case 8:
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800420 gen8_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800421 pCreateInfo, usage);
422 break;
423 case 9:
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800424 gen9_fill_image_surface_state(device, state.map, iview,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800425 pCreateInfo, usage);
426 break;
427 default:
428 unreachable("unsupported gen\n");
429 }
430
431 if (!device->info.has_llc)
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800432 anv_state_clflush(state);
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800433}
434
435static struct anv_state
436alloc_surface_state(struct anv_device *device,
437 struct anv_cmd_buffer *cmd_buffer)
438{
439 if (cmd_buffer) {
440 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
441 } else {
442 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
443 }
444}
445
Francisco Jereza50dc702016-01-26 12:23:08 -0800446static bool
447has_matching_storage_typed_format(const struct anv_device *device,
448 enum isl_format format)
449{
450 return (isl_format_get_layout(format)->bs <= 4 ||
451 (isl_format_get_layout(format)->bs <= 8 &&
452 (device->info.gen >= 8 || device->info.is_haswell)) ||
453 device->info.gen >= 9);
454}
455
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800456static VkComponentSwizzle
457remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component)
458{
459 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
460 return component;
461 else
462 return swizzle;
463}
464
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800465void
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700466anv_image_view_init(struct anv_image_view *iview,
467 struct anv_device *device,
468 const VkImageViewCreateInfo* pCreateInfo,
469 struct anv_cmd_buffer *cmd_buffer)
470{
Chad Versace44143a12015-10-06 18:17:09 -0700471 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
Chad Versace24de3d42015-10-06 19:11:58 -0700472 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Chad Versace44143a12015-10-06 18:17:09 -0700473
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800474 assert(range->layerCount > 0);
Chad Versace24de3d42015-10-06 19:11:58 -0700475 assert(range->baseMipLevel < image->levels);
Chad Versace44143a12015-10-06 18:17:09 -0700476 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
Chad Versace24de3d42015-10-06 19:11:58 -0700477 VK_IMAGE_USAGE_STORAGE_BIT |
478 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Chad Versace0ca3c842015-10-07 11:39:49 -0700479 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
Chad Versace24de3d42015-10-06 19:11:58 -0700480
481 switch (image->type) {
482 default:
483 unreachable("bad VkImageType");
484 case VK_IMAGE_TYPE_1D:
485 case VK_IMAGE_TYPE_2D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800486 assert(range->baseArrayLayer + range->layerCount - 1 <= image->array_size);
Chad Versace24de3d42015-10-06 19:11:58 -0700487 break;
488 case VK_IMAGE_TYPE_3D:
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800489 assert(range->baseArrayLayer + range->layerCount - 1
Chad Versace24de3d42015-10-06 19:11:58 -0700490 <= anv_minify(image->extent.depth, range->baseMipLevel));
491 break;
492 }
Chad Versace44143a12015-10-06 18:17:09 -0700493
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800494 struct anv_surface *surface =
495 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
496
497 iview->image = image;
498 iview->bo = image->bo;
499 iview->offset = image->offset + surface->offset;
500
501 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
Jason Ekstrandf665fdf2016-01-01 14:09:17 -0800502 iview->vk_format = pCreateInfo->format;
503 iview->format = anv_get_isl_format(pCreateInfo->format, iview->aspect_mask,
504 image->tiling);
Jason Ekstrand9bc72a92016-01-26 20:16:43 -0800505 iview->swizzle.r = remap_swizzle(pCreateInfo->components.r,
506 VK_COMPONENT_SWIZZLE_R);
507 iview->swizzle.g = remap_swizzle(pCreateInfo->components.g,
508 VK_COMPONENT_SWIZZLE_G);
509 iview->swizzle.b = remap_swizzle(pCreateInfo->components.b,
510 VK_COMPONENT_SWIZZLE_B);
511 iview->swizzle.a = remap_swizzle(pCreateInfo->components.a,
512 VK_COMPONENT_SWIZZLE_A);
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800513
Jason Ekstrandaa9987a2016-01-05 13:54:02 -0800514 iview->base_layer = range->baseArrayLayer;
515 iview->base_mip = range->baseMipLevel;
Jason Ekstranda7cc1292016-01-01 13:47:18 -0800516 iview->extent = (VkExtent3D) {
517 .width = anv_minify(image->extent.width, range->baseMipLevel),
518 .height = anv_minify(image->extent.height, range->baseMipLevel),
519 .depth = anv_minify(image->extent.depth, range->baseMipLevel),
520 };
521
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800522 if (image->needs_nonrt_surface_state) {
523 iview->nonrt_surface_state = alloc_surface_state(device, cmd_buffer);
524
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800525 anv_fill_image_surface_state(device, iview->nonrt_surface_state,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800526 iview, pCreateInfo,
527 VK_IMAGE_USAGE_SAMPLED_BIT);
528 } else {
529 iview->nonrt_surface_state.alloc_size = 0;
530 }
531
532 if (image->needs_color_rt_surface_state) {
533 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
534
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800535 anv_fill_image_surface_state(device, iview->color_rt_surface_state,
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800536 iview, pCreateInfo,
537 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
538 } else {
539 iview->color_rt_surface_state.alloc_size = 0;
540 }
541
542 if (image->needs_storage_surface_state) {
543 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
544
Francisco Jereza50dc702016-01-26 12:23:08 -0800545 if (has_matching_storage_typed_format(device, iview->format))
Francisco Jerezfc7a7b32016-01-26 14:45:46 -0800546 anv_fill_image_surface_state(device, iview->storage_surface_state,
Francisco Jereza50dc702016-01-26 12:23:08 -0800547 iview, pCreateInfo,
548 VK_IMAGE_USAGE_STORAGE_BIT);
549 else
Francisco Jerez6840cc12016-01-26 14:50:52 -0800550 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
Francisco Jereza50dc702016-01-26 12:23:08 -0800551 ISL_FORMAT_RAW,
552 iview->offset,
553 iview->bo->size - iview->offset, 1);
554
Jason Ekstrande5558ff2016-01-22 11:57:01 -0800555 } else {
556 iview->storage_surface_state.alloc_size = 0;
Kristian Høgsberg Kristensen988341a2015-08-19 21:36:57 -0700557 }
558}
559
Chad Versace5b04db72015-07-06 16:24:28 -0700560VkResult
Chad Versace127cb3f2015-06-26 20:12:42 -0700561anv_CreateImageView(VkDevice _device,
562 const VkImageViewCreateInfo *pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800563 const VkAllocationCallbacks *pAllocator,
Chad Versace127cb3f2015-06-26 20:12:42 -0700564 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700565{
Jason Ekstranda52e2082015-07-09 20:24:07 -0700566 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700567 struct anv_image_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700568
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800569 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
570 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700571 if (view == NULL)
572 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
573
574 anv_image_view_init(view, device, pCreateInfo, NULL);
575
576 *pView = anv_image_view_to_handle(view);
577
578 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700579}
580
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800581void
582anv_DestroyImageView(VkDevice _device, VkImageView _iview,
583 const VkAllocationCallbacks *pAllocator)
Chad Versace24de3d42015-10-06 19:11:58 -0700584{
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800585 ANV_FROM_HANDLE(anv_device, device, _device);
586 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
587
Chad Versace24de3d42015-10-06 19:11:58 -0700588 if (iview->image->needs_color_rt_surface_state) {
589 anv_state_pool_free(&device->surface_state_pool,
590 iview->color_rt_surface_state);
591 }
592
593 if (iview->image->needs_nonrt_surface_state) {
594 anv_state_pool_free(&device->surface_state_pool,
595 iview->nonrt_surface_state);
596 }
597
Jason Ekstrandff05f632015-12-07 17:17:30 -0800598 if (iview->image->needs_storage_surface_state) {
599 anv_state_pool_free(&device->surface_state_pool,
600 iview->storage_surface_state);
601 }
602
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800603 anv_free2(&device->alloc, pAllocator, iview);
Jason Ekstrand84783502015-07-10 20:18:52 -0700604}
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700605
Jason Ekstrandc5618602015-12-12 16:11:23 -0800606VkResult
607anv_CreateBufferView(VkDevice _device,
608 const VkBufferViewCreateInfo *pCreateInfo,
609 const VkAllocationCallbacks *pAllocator,
610 VkBufferView *pView)
611{
612 ANV_FROM_HANDLE(anv_device, device, _device);
613 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
614 struct anv_buffer_view *view;
615
Jason Ekstrandc5618602015-12-12 16:11:23 -0800616 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
617 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
618 if (!view)
619 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
620
Jason Ekstrandc5618602015-12-12 16:11:23 -0800621 const struct anv_format *format =
622 anv_format_for_vk_format(pCreateInfo->format);
623
Jason Ekstrand783a2112015-12-14 16:51:12 -0800624 view->format = format->surface_format;
625 view->bo = buffer->bo;
626 view->offset = buffer->offset + pCreateInfo->offset;
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800627 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
628 buffer->size - view->offset : pCreateInfo->range;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800629
630 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
631 view->surface_state =
632 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
633
Francisco Jerez6840cc12016-01-26 14:50:52 -0800634 anv_fill_buffer_surface_state(device, view->surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800635 view->format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800636 view->offset, view->range,
Chad Versace89b68dc2016-01-05 09:59:07 -0800637 format->isl_layout->bs);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800638 } else {
639 view->surface_state = (struct anv_state){ 0 };
640 }
641
642 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
643 view->storage_surface_state =
644 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
645
646 enum isl_format storage_format =
Francisco Jereza50dc702016-01-26 12:23:08 -0800647 has_matching_storage_typed_format(device, view->format) ?
648 isl_lower_storage_image_format(&device->isl_dev, view->format) :
649 ISL_FORMAT_RAW;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800650
Francisco Jerez6840cc12016-01-26 14:50:52 -0800651 anv_fill_buffer_surface_state(device, view->storage_surface_state,
Jason Ekstrand783a2112015-12-14 16:51:12 -0800652 storage_format,
Jason Ekstrand56dbf132016-01-19 15:01:10 -0800653 view->offset, view->range,
Francisco Jereza50dc702016-01-26 12:23:08 -0800654 (storage_format == ISL_FORMAT_RAW ? 1 :
655 format->isl_layout->bs));
656
Jason Ekstrand783a2112015-12-14 16:51:12 -0800657 } else {
658 view->storage_surface_state = (struct anv_state){ 0 };
659 }
Jason Ekstrandc5618602015-12-12 16:11:23 -0800660
661 *pView = anv_buffer_view_to_handle(view);
662
663 return VK_SUCCESS;
664}
665
666void
667anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
668 const VkAllocationCallbacks *pAllocator)
669{
670 ANV_FROM_HANDLE(anv_device, device, _device);
671 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
672
Jason Ekstrand783a2112015-12-14 16:51:12 -0800673 if (view->surface_state.alloc_size > 0)
674 anv_state_pool_free(&device->surface_state_pool,
675 view->surface_state);
676
677 if (view->storage_surface_state.alloc_size > 0)
678 anv_state_pool_free(&device->surface_state_pool,
679 view->storage_surface_state);
680
Jason Ekstrandc5618602015-12-12 16:11:23 -0800681 anv_free2(&device->alloc, pAllocator, view);
682}
683
Chad Versace941b48e2015-08-28 07:08:58 -0700684struct anv_surface *
Chad Versace7a089bd2015-10-05 06:48:14 -0700685anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
Chad Versace941b48e2015-08-28 07:08:58 -0700686{
Chad Versace7a089bd2015-10-05 06:48:14 -0700687 switch (aspect_mask) {
688 case VK_IMAGE_ASPECT_COLOR_BIT:
Chad Versace24de3d42015-10-06 19:11:58 -0700689 /* Dragons will eat you.
690 *
691 * Meta attaches all destination surfaces as color render targets. Guess
692 * what surface the Meta Dragons really want.
693 */
694 if (image->format->depth_format && image->format->has_stencil) {
Chad Versace24de3d42015-10-06 19:11:58 -0700695 return &image->depth_surface;
696 } else if (image->format->depth_format) {
697 return &image->depth_surface;
698 } else if (image->format->has_stencil) {
699 return &image->stencil_surface;
700 } else {
701 return &image->color_surface;
702 }
703 break;
Chad Versace7a089bd2015-10-05 06:48:14 -0700704 case VK_IMAGE_ASPECT_DEPTH_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700705 assert(image->format->depth_format);
706 return &image->depth_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700707 case VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace941b48e2015-08-28 07:08:58 -0700708 assert(image->format->has_stencil);
Chad Versace941b48e2015-08-28 07:08:58 -0700709 return &image->stencil_surface;
Chad Versace7a089bd2015-10-05 06:48:14 -0700710 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
Chad Versace03dd7222015-10-07 09:03:47 -0700711 if (image->format->depth_format && image->format->has_stencil) {
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800712 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
713 * combined depth stencil formats. Specifically, it states:
Chad Versace03dd7222015-10-07 09:03:47 -0700714 *
715 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
716 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
Kristian Høgsberg Kristensen8e07f792016-01-25 15:14:47 -0800717 *
718 * Image views with both depth and stencil aspects are only valid for
719 * render target attachments, in which case
720 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
721 * stencil surfaces from the underlying surface.
Chad Versace03dd7222015-10-07 09:03:47 -0700722 */
Chad Versace03dd7222015-10-07 09:03:47 -0700723 return &image->depth_surface;
724 } else if (image->format->depth_format) {
725 return &image->depth_surface;
726 } else if (image->format->has_stencil) {
727 return &image->stencil_surface;
728 }
729 /* fallthrough */
Chad Versace941b48e2015-08-28 07:08:58 -0700730 default:
731 unreachable("image does not have aspect");
732 return NULL;
733 }
734}
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800735
Jason Ekstrandba393c92016-01-05 13:55:00 -0800736static void
737image_param_defaults(struct brw_image_param *param)
738{
739 memset(param, 0, sizeof *param);
740 /* Set the swizzling shifts to all-ones to effectively disable swizzling --
741 * See emit_address_calculation() in brw_fs_surface_builder.cpp for a more
742 * detailed explanation of these parameters.
743 */
744 param->swizzling[0] = 0xff;
745 param->swizzling[1] = 0xff;
746}
747
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800748void
749anv_image_view_fill_image_param(struct anv_device *device,
750 struct anv_image_view *view,
751 struct brw_image_param *param)
752{
Jason Ekstrandba393c92016-01-05 13:55:00 -0800753 image_param_defaults(param);
754
755 const struct isl_surf *surf = &view->image->color_surface.isl;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800756 const int cpp = isl_format_get_layout(surf->format)->bs;
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800757 const struct isl_extent3d image_align_sa =
758 isl_surf_get_image_alignment_sa(surf);
Jason Ekstrandba393c92016-01-05 13:55:00 -0800759
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800760 param->size[0] = view->extent.width;
761 param->size[1] = view->extent.height;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800762 if (surf->dim == ISL_SURF_DIM_3D) {
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800763 param->size[2] = view->extent.depth;
Jason Ekstrandba393c92016-01-05 13:55:00 -0800764 } else {
765 param->size[2] = surf->logical_level0_px.array_len - view->base_layer;
766 }
767
768 isl_surf_get_image_offset_sa(surf, view->base_mip, view->base_layer, 0,
769 &param->offset[0], &param->offset[1]);
770
771 param->stride[0] = cpp;
772 param->stride[1] = surf->row_pitch / cpp;
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800773
774 if (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D) {
775 param->stride[2] = util_align_npot(param->size[0], image_align_sa.w);
776 param->stride[3] = util_align_npot(param->size[1], image_align_sa.h);
777 } else {
778 param->stride[2] = 0;
779 param->stride[3] = isl_surf_get_array_pitch_el_rows(surf);
780 }
Jason Ekstrandba393c92016-01-05 13:55:00 -0800781
782 switch (surf->tiling) {
783 case ISL_TILING_LINEAR:
784 /* image_param_defaults is good enough */
785 break;
786
787 case ISL_TILING_X:
788 /* An X tile is a rectangular block of 512x8 bytes. */
789 param->tiling[0] = util_logbase2(512 / cpp);
790 param->tiling[1] = util_logbase2(8);
791
792 if (device->isl_dev.has_bit6_swizzling) {
793 /* Right shifts required to swizzle bits 9 and 10 of the memory
794 * address with bit 6.
795 */
796 param->swizzling[0] = 3;
797 param->swizzling[1] = 4;
798 }
799 break;
800
801 case ISL_TILING_Y0:
802 /* The layout of a Y-tiled surface in memory isn't really fundamentally
803 * different to the layout of an X-tiled surface, we simply pretend that
804 * the surface is broken up in a number of smaller 16Bx32 tiles, each
805 * one arranged in X-major order just like is the case for X-tiling.
806 */
807 param->tiling[0] = util_logbase2(16 / cpp);
808 param->tiling[1] = util_logbase2(32);
809
810 if (device->isl_dev.has_bit6_swizzling) {
811 /* Right shift required to swizzle bit 9 of the memory address with
812 * bit 6.
813 */
814 param->swizzling[0] = 3;
815 param->swizzling[1] = 0xff;
816 }
817 break;
818
819 default:
820 assert(!"Unhandled storage image tiling");
821 }
822
823 /* 3D textures are arranged in 2D in memory with 2^lod slices per row. The
824 * address calculation algorithm (emit_address_calculation() in
825 * brw_fs_surface_builder.cpp) handles this as a sort of tiling with
826 * modulus equal to the LOD.
827 */
Francisco Jerezd2ec5102016-01-26 12:20:01 -0800828 param->tiling[2] = (device->info.gen < 9 && surf->dim == ISL_SURF_DIM_3D ?
829 view->base_mip : 0);
Jason Ekstrand43ac9542015-11-18 15:14:05 -0800830}
Jason Ekstrand783a2112015-12-14 16:51:12 -0800831
832void
833anv_buffer_view_fill_image_param(struct anv_device *device,
834 struct anv_buffer_view *view,
835 struct brw_image_param *param)
836{
Jason Ekstrandba393c92016-01-05 13:55:00 -0800837 image_param_defaults(param);
Jason Ekstrand783a2112015-12-14 16:51:12 -0800838
Chad Versace89b68dc2016-01-05 09:59:07 -0800839 param->stride[0] = isl_format_layouts[view->format].bs;
Jason Ekstrand783a2112015-12-14 16:51:12 -0800840 param->size[0] = view->range / param->stride[0];
841}