blob: 9930d90aa41a3a94ed1c8aaa22ab721e5a0788b9 [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
30#include "private.h"
31
Chad Versacee6162c22015-06-09 12:11:46 -070032static const uint8_t anv_halign[] = {
33 [4] = HALIGN4,
34 [8] = HALIGN8,
35 [16] = HALIGN16,
36};
37
38static const uint8_t anv_valign[] = {
39 [4] = VALIGN4,
40 [8] = VALIGN8,
41 [16] = VALIGN16,
42};
43
Chad Versacecb30aca2015-05-28 07:37:59 -070044static 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
50static const uint8_t anv_surf_type_from_image_view_type[] = {
51 [VK_IMAGE_VIEW_TYPE_1D] = SURFTYPE_1D,
52 [VK_IMAGE_VIEW_TYPE_2D] = SURFTYPE_2D,
53 [VK_IMAGE_VIEW_TYPE_3D] = SURFTYPE_3D,
54 [VK_IMAGE_VIEW_TYPE_CUBE] = SURFTYPE_CUBE,
55};
56
Chad Versacefa352962015-05-28 07:40:22 -070057static const struct anv_surf_type_limits {
58 int32_t width;
59 int32_t height;
60 int32_t depth;
61} anv_surf_type_limits[] = {
62 [SURFTYPE_1D] = {16384, 0, 2048},
63 [SURFTYPE_2D] = {16384, 16384, 2048},
64 [SURFTYPE_3D] = {2048, 2048, 2048},
65 [SURFTYPE_CUBE] = {16384, 16384, 340},
66 [SURFTYPE_BUFFER] = {128, 16384, 64},
67 [SURFTYPE_STRBUF] = {128, 16384, 64},
68};
69
Chad Versacee6bd5682015-06-09 13:29:08 -070070static const struct anv_tile_info {
71 uint32_t width;
72 uint32_t height;
73
74 /**
75 * Alignment for RENDER_SURFACE_STATE.SurfaceBaseAddress.
76 *
77 * To simplify calculations, the alignments defined in the table are
78 * sometimes larger than required. For example, Skylake requires that X and
79 * Y tiled buffers be aligned to 4K, but Broadwell permits smaller
80 * alignment. We choose 4K to accomodate both chipsets. The alignment of
81 * a linear buffer depends on its element type and usage. Linear depth
82 * buffers have the largest alignment, 64B, so we choose that for all linear
83 * buffers.
84 */
85 uint32_t surface_alignment;
86} anv_tile_info_table[] = {
87 [LINEAR] = { 1, 1, 64 },
88 [XMAJOR] = { 512, 8, 4096 },
89 [YMAJOR] = { 128, 32, 4096 },
90 [WMAJOR] = { 128, 32, 4096 },
Kristian Høgsberg769785c2015-05-08 22:32:37 -070091};
92
Chad Versacef1db3b32015-06-09 14:33:05 -070093static uint32_t
Chad Versacefdcd71f2015-06-26 20:06:08 -070094anv_image_choose_tile_mode(const struct anv_image_create_info *anv_info)
Chad Versacef1db3b32015-06-09 14:33:05 -070095{
Chad Versacefdcd71f2015-06-26 20:06:08 -070096 if (anv_info->force_tile_mode)
Chad Versacef1db3b32015-06-09 14:33:05 -070097 return anv_info->tile_mode;
98
Chad Versacec6e76ae2015-06-26 18:48:34 -070099 if (anv_info->vk_info->format == VK_FORMAT_S8_UINT)
100 return WMAJOR;
101
Chad Versacefdcd71f2015-06-26 20:06:08 -0700102 switch (anv_info->vk_info->tiling) {
Chad Versacef1db3b32015-06-09 14:33:05 -0700103 case VK_IMAGE_TILING_LINEAR:
104 return LINEAR;
105 case VK_IMAGE_TILING_OPTIMAL:
Kristian Høgsberg Kristensen00494c62015-06-11 22:07:16 -0700106 return YMAJOR;
Chad Versacef1db3b32015-06-09 14:33:05 -0700107 default:
108 assert(!"bad VKImageTiling");
109 return LINEAR;
110 }
111}
112
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700113static VkResult
Chad Versacec6e76ae2015-06-26 18:48:34 -0700114anv_image_make_surface(const struct anv_image_create_info *create_info,
115 uint64_t *inout_image_size,
116 uint32_t *inout_image_alignment,
117 struct anv_surface *out_surface)
118{
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700119 /* See RENDER_SURFACE_STATE.SurfaceQPitch */
120 static const uint16_t min_qpitch UNUSED = 0x4;
121 static const uint16_t max_qpitch UNUSED = 0x1ffc;
122
Chad Versacec6e76ae2015-06-26 18:48:34 -0700123 const VkExtent3D *restrict extent = &create_info->vk_info->extent;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700124 const uint32_t levels = create_info->vk_info->mipLevels;
125 const uint32_t array_size = create_info->vk_info->arraySize;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700126
127 const uint8_t tile_mode = anv_image_choose_tile_mode(create_info);
128
129 const struct anv_tile_info *tile_info =
130 &anv_tile_info_table[tile_mode];
131
132 const struct anv_format *format_info =
133 anv_format_for_vk_format(create_info->vk_info->format);
134
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700135 const uint32_t i = 4; /* FINISHME: Stop hardcoding subimage alignment */
136 const uint32_t j = 4; /* FINISHME: Stop hardcoding subimage alignment */
137 const uint32_t w0 = align_u32(extent->width, i);
138 const uint32_t h0 = align_u32(extent->height, j);
139
140 uint16_t qpitch;
141 uint32_t mt_width;
142 uint32_t mt_height;
143
144 if (levels == 1 && array_size == 1) {
145 qpitch = min_qpitch;
146 mt_width = w0;
147 mt_height = h0;
148 } else {
149 uint32_t w1 = align_u32(anv_minify(extent->width, 1), i);
150 uint32_t h1 = align_u32(anv_minify(extent->height, 1), j);
151 uint32_t w2 = align_u32(anv_minify(extent->width, 2), i);
152
153 qpitch = h0 + h1 + 11 * j;
154 mt_width = MAX(w0, w1 + w2);
155 mt_height = array_size * qpitch;
156 }
157
158 assert(qpitch >= min_qpitch);
159 if (qpitch > max_qpitch) {
160 anv_loge("image qpitch > 0x%x\n", max_qpitch);
161 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
162 }
163
164 /* From the Broadwell PRM, RENDER_SURFACE_STATE.SurfaceQpitch:
165 *
166 * This field must be set an integer multiple of the Surface Vertical
167 * Alignment.
168 */
169 assert(anv_is_aligned(qpitch, j));
170
171 const uint32_t stride = align_u32(mt_width * format_info->cpp,
172 tile_info->width);
173 const uint32_t size = stride * align_u32(mt_height, tile_info->height);
174 const uint32_t offset = align_u32(*inout_image_size,
175 tile_info->surface_alignment);
Chad Versacec6e76ae2015-06-26 18:48:34 -0700176
177 *inout_image_size = offset + size;
178 *inout_image_alignment = MAX(*inout_image_alignment,
179 tile_info->surface_alignment);
180
181 *out_surface = (struct anv_surface) {
182 .offset = offset,
183 .stride = stride,
184 .tile_mode = tile_mode,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700185 .qpitch = qpitch,
186 .h_align = i,
187 .v_align = j,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700188 };
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700189
190 return VK_SUCCESS;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700191}
192
Chad Versace127cb3f2015-06-26 20:12:42 -0700193VkResult
194anv_image_create(VkDevice _device,
195 const struct anv_image_create_info *create_info,
196 VkImage *pImage)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700197{
198 struct anv_device *device = (struct anv_device *) _device;
Chad Versacefdcd71f2015-06-26 20:06:08 -0700199 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
Chad Versace67a76592015-06-26 09:17:52 -0700200 const VkExtent3D *restrict extent = &pCreateInfo->extent;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700201 struct anv_image *image = NULL;
202 VkResult r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700203
204 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
205
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700206 /* XXX: We don't handle any of these */
207 anv_assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700208 anv_assert(pCreateInfo->mipLevels > 0);
209 anv_assert(pCreateInfo->arraySize > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700210 anv_assert(pCreateInfo->samples == 1);
Chad Versace5d7103e2015-06-26 09:05:46 -0700211 anv_assert(pCreateInfo->extent.width > 0);
212 anv_assert(pCreateInfo->extent.height > 0);
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700213 anv_assert(pCreateInfo->extent.depth > 0);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700214
Chad Versace7ea12162015-05-28 07:45:31 -0700215 /* TODO(chadv): How should we validate inputs? */
Chad Versace67a76592015-06-26 09:17:52 -0700216 const uint8_t surf_type =
217 anv_surf_type_from_image_type[pCreateInfo->imageType];
Chad Versace7ea12162015-05-28 07:45:31 -0700218
Chad Versacefa352962015-05-28 07:40:22 -0700219 const struct anv_surf_type_limits *limits =
Chad Versace67a76592015-06-26 09:17:52 -0700220 &anv_surf_type_limits[surf_type];
Chad Versacefa352962015-05-28 07:40:22 -0700221
Chad Versace67a76592015-06-26 09:17:52 -0700222 if (extent->width > limits->width ||
223 extent->height > limits->height ||
224 extent->depth > limits->depth) {
Chad Versacefa352962015-05-28 07:40:22 -0700225 /* TODO(chadv): What is the correct error? */
Chad Versace67a76592015-06-26 09:17:52 -0700226 anv_loge("image extent is too large");
Chad Versacefa352962015-05-28 07:40:22 -0700227 return vk_error(VK_ERROR_INVALID_MEMORY_SIZE);
228 }
229
Chad Versace67a76592015-06-26 09:17:52 -0700230 const struct anv_format *format_info =
231 anv_format_for_vk_format(pCreateInfo->format);
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700232
Chad Versacec6e76ae2015-06-26 18:48:34 -0700233 image = anv_device_alloc(device, sizeof(*image), 8,
234 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
235 if (!image)
236 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
Chad Versace67a76592015-06-26 09:17:52 -0700237
Chad Versacec6e76ae2015-06-26 18:48:34 -0700238 memset(image, 0, sizeof(*image));
239 image->type = pCreateInfo->imageType;
240 image->extent = pCreateInfo->extent;
241 image->format = pCreateInfo->format;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700242 image->levels = pCreateInfo->mipLevels;
243 image->array_size = pCreateInfo->arraySize;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700244 image->surf_type = surf_type;
Chad Versace67a76592015-06-26 09:17:52 -0700245
Chad Versacec6e76ae2015-06-26 18:48:34 -0700246 if (likely(!format_info->has_stencil || format_info->depth_format)) {
247 /* The image's primary surface is a color or depth surface. */
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700248 r = anv_image_make_surface(create_info, &image->size, &image->alignment,
249 &image->primary_surface);
250 if (r != VK_SUCCESS)
251 goto fail;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700252 }
253
Chad Versace45b804a2015-06-25 19:03:43 -0700254 if (format_info->has_stencil) {
Chad Versace37d6e042015-06-26 09:47:17 -0700255 /* From the GPU's perspective, the depth buffer and stencil buffer are
256 * separate buffers. From Vulkan's perspective, though, depth and
257 * stencil reside in the same image. To satisfy Vulkan and the GPU, we
258 * place the depth and stencil buffers in the same bo.
259 */
Chad Versacec6e76ae2015-06-26 18:48:34 -0700260 VkImageCreateInfo stencil_info = *pCreateInfo;
261 stencil_info.format = VK_FORMAT_S8_UINT;
Chad Versace67a76592015-06-26 09:17:52 -0700262
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700263 r = anv_image_make_surface(
264 &(struct anv_image_create_info) {
265 .vk_info = &stencil_info,
266 },
267 &image->size, &image->alignment, &image->stencil_surface);
268
269 if (r != VK_SUCCESS)
270 goto fail;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700271 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700272
273 *pImage = (VkImage) image;
274
275 return VK_SUCCESS;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700276
277fail:
278 if (image)
279 anv_device_free(device, image);
280
281 return r;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700282}
283
Chad Versace127cb3f2015-06-26 20:12:42 -0700284VkResult
285anv_CreateImage(VkDevice device,
286 const VkImageCreateInfo *pCreateInfo,
287 VkImage *pImage)
Kristian Høgsberga29df712015-05-15 22:04:52 -0700288{
Chad Versacefdcd71f2015-06-26 20:06:08 -0700289 return anv_image_create(device,
290 &(struct anv_image_create_info) {
291 .vk_info = pCreateInfo,
292 },
293 pImage);
Kristian Høgsberga29df712015-05-15 22:04:52 -0700294}
295
Chad Versace127cb3f2015-06-26 20:12:42 -0700296VkResult
297anv_GetImageSubresourceInfo(VkDevice device,
298 VkImage image,
299 const VkImageSubresource *pSubresource,
300 VkSubresourceInfoType infoType,
301 size_t *pDataSize,
302 void *pData)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700303{
Jason Ekstrandffe9f602015-05-12 13:44:43 -0700304 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700305}
306
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700307void
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700308anv_surface_view_destroy(struct anv_device *device,
309 struct anv_object *obj, VkObjectType obj_type)
310{
311 struct anv_surface_view *view = (struct anv_surface_view *)obj;
312
313 assert(obj_type == VK_OBJECT_TYPE_BUFFER_VIEW ||
314 obj_type == VK_OBJECT_TYPE_IMAGE_VIEW ||
315 obj_type == VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW);
316
317 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
318
319 anv_device_free(device, view);
320}
321
322void
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700323anv_image_view_init(struct anv_surface_view *view,
324 struct anv_device *device,
325 const VkImageViewCreateInfo* pCreateInfo,
326 struct anv_cmd_buffer *cmd_buffer)
327{
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700328 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700329 struct anv_image *image = (struct anv_image *) pCreateInfo->image;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700330 struct anv_surface *surface;
Chad Versaceca6cef32015-06-26 19:50:04 -0700331
332 const struct anv_format *format_info =
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700333 anv_format_for_vk_format(pCreateInfo->format);
334
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700335 anv_assert(range->mipLevels > 0);
336 anv_assert(range->arraySize > 0);
337 anv_assert(range->baseMipLevel + range->mipLevels <= image->levels);
338 anv_assert(range->baseArraySlice + range->arraySize <= image->array_size);
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700339
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700340 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D)
341 anv_finishme("non-2D image views");
Chad Versaceca6cef32015-06-26 19:50:04 -0700342
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700343 switch (pCreateInfo->subresourceRange.aspect) {
344 case VK_IMAGE_ASPECT_STENCIL:
Chad Versace9c46ba92015-06-26 19:23:21 -0700345 anv_finishme("stencil image views");
346 abort();
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700347 break;
348 case VK_IMAGE_ASPECT_DEPTH:
349 case VK_IMAGE_ASPECT_COLOR:
350 view->offset = image->offset;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700351 surface = &image->primary_surface;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700352 break;
353 default:
Kristian Høgsberg Kristensen52637c02015-06-05 11:51:30 -0700354 unreachable("");
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700355 break;
356 }
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700357
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700358 view->bo = image->bo;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700359 view->offset = image->offset + surface->offset;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700360 view->format = pCreateInfo->format;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700361
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700362 view->extent = (VkExtent3D) {
363 .width = anv_minify(image->extent.width, range->baseMipLevel),
364 .height = anv_minify(image->extent.height, range->baseMipLevel),
365 .depth = anv_minify(image->extent.depth, range->baseMipLevel),
366 };
367
368 uint32_t depth = 1;
369 if (range->arraySize > 1) {
370 depth = range->arraySize;
371 } else if (image->extent.depth > 1) {
372 depth = image->extent.depth;
373 }
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700374
375 static const uint32_t vk_to_gen_swizzle[] = {
Kristian Høgsberg Kristensen5caa4082015-05-31 22:35:11 -0700376 [VK_CHANNEL_SWIZZLE_ZERO] = SCS_ZERO,
377 [VK_CHANNEL_SWIZZLE_ONE] = SCS_ONE,
378 [VK_CHANNEL_SWIZZLE_R] = SCS_RED,
379 [VK_CHANNEL_SWIZZLE_G] = SCS_GREEN,
380 [VK_CHANNEL_SWIZZLE_B] = SCS_BLUE,
381 [VK_CHANNEL_SWIZZLE_A] = SCS_ALPHA
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700382 };
383
384 struct GEN8_RENDER_SURFACE_STATE surface_state = {
Chad Versace99031aa2015-05-28 07:46:31 -0700385 .SurfaceType = anv_surf_type_from_image_view_type[pCreateInfo->viewType],
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700386 .SurfaceArray = image->array_size > 1,
Chad Versaceca6cef32015-06-26 19:50:04 -0700387 .SurfaceFormat = format_info->surface_format,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700388 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
389 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
390 .TileMode = surface->tile_mode,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700391 .VerticalLineStride = 0,
392 .VerticalLineStrideOffset = 0,
393 .SamplerL2BypassModeDisable = true,
394 .RenderCacheReadWriteMode = WriteOnlyCache,
395 .MemoryObjectControlState = GEN8_MOCS,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700396 .BaseMipLevel = (float) pCreateInfo->minLod,
397 .SurfaceQPitch = surface->qpitch >> 2,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700398 .Height = image->extent.height - 1,
399 .Width = image->extent.width - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700400 .Depth = depth - 1,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700401 .SurfacePitch = surface->stride - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700402 .MinimumArrayElement = range->baseArraySlice,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700403 .NumberofMultisamples = MULTISAMPLECOUNT_1,
404 .XOffset = 0,
405 .YOffset = 0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700406
407 /* For sampler surfaces, the hardware interprets field MIPCount/LOD as
408 * MIPCount. The range of levels accessible by the sampler engine is
409 * [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
410 */
411 .MIPCountLOD = range->mipLevels - 1,
412 .SurfaceMinLOD = range->baseMipLevel,
413
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700414 .AuxiliarySurfaceMode = AUX_NONE,
415 .RedClearColor = 0,
416 .GreenClearColor = 0,
417 .BlueClearColor = 0,
418 .AlphaClearColor = 0,
419 .ShaderChannelSelectRed = vk_to_gen_swizzle[pCreateInfo->channels.r],
420 .ShaderChannelSelectGreen = vk_to_gen_swizzle[pCreateInfo->channels.g],
421 .ShaderChannelSelectBlue = vk_to_gen_swizzle[pCreateInfo->channels.b],
422 .ShaderChannelSelectAlpha = vk_to_gen_swizzle[pCreateInfo->channels.a],
Kristian Høgsberg Kristensena5b49d22015-06-10 23:11:37 -0700423 .ResourceMinLOD = 0.0,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700424 .SurfaceBaseAddress = { NULL, view->offset },
425 };
426
427 if (cmd_buffer)
428 view->surface_state =
429 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
430 else
431 view->surface_state =
432 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
433
434 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700435}
436
Chad Versace127cb3f2015-06-26 20:12:42 -0700437VkResult
438anv_CreateImageView(VkDevice _device,
439 const VkImageViewCreateInfo *pCreateInfo,
440 VkImageView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700441{
442 struct anv_device *device = (struct anv_device *) _device;
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -0700443 struct anv_surface_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700444
445 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
446
447 view = anv_device_alloc(device, sizeof(*view), 8,
448 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
449 if (view == NULL)
450 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
451
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700452 anv_image_view_init(view, device, pCreateInfo, NULL);
Jason Ekstrand1f7dcf92015-05-13 17:37:12 -0700453
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700454 view->base.destructor = anv_surface_view_destroy;
455
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700456 *pView = (VkImageView) view;
457
458 return VK_SUCCESS;
459}
460
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700461void
462anv_color_attachment_view_init(struct anv_surface_view *view,
463 struct anv_device *device,
464 const VkColorAttachmentViewCreateInfo* pCreateInfo,
465 struct anv_cmd_buffer *cmd_buffer)
466{
467 struct anv_image *image = (struct anv_image *) pCreateInfo->image;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700468 struct anv_surface *surface = &image->primary_surface;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700469 const struct anv_format *format_info =
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700470 anv_format_for_vk_format(pCreateInfo->format);
471
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700472 anv_assert(pCreateInfo->arraySize > 0);
473 anv_assert(pCreateInfo->mipLevel < image->levels);
474 anv_assert(pCreateInfo->baseArraySlice + pCreateInfo->arraySize <= image->array_size);
475
476 if (pCreateInfo->msaaResolveImage)
477 anv_finishme("msaaResolveImage");
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700478
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700479 view->bo = image->bo;
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700480 view->offset = image->offset + surface->offset;
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700481 view->format = pCreateInfo->format;
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700482
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700483 view->extent = (VkExtent3D) {
484 .width = anv_minify(image->extent.width, pCreateInfo->mipLevel),
485 .height = anv_minify(image->extent.height, pCreateInfo->mipLevel),
486 .depth = anv_minify(image->extent.depth, pCreateInfo->mipLevel),
487 };
488
489 uint32_t depth = 1;
490 if (pCreateInfo->arraySize > 1) {
491 depth = pCreateInfo->arraySize;
492 } else if (image->extent.depth > 1) {
493 depth = image->extent.depth;
494 }
495
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700496 if (cmd_buffer)
497 view->surface_state =
498 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
499 else
500 view->surface_state =
501 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
502
503 struct GEN8_RENDER_SURFACE_STATE surface_state = {
504 .SurfaceType = SURFTYPE_2D,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700505 .SurfaceArray = image->array_size > 1,
506 .SurfaceFormat = format_info->surface_format,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700507 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
508 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
509 .TileMode = surface->tile_mode,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700510 .VerticalLineStride = 0,
511 .VerticalLineStrideOffset = 0,
512 .SamplerL2BypassModeDisable = true,
513 .RenderCacheReadWriteMode = WriteOnlyCache,
514 .MemoryObjectControlState = GEN8_MOCS,
Kristian Høgsberg Kristensena5b49d22015-06-10 23:11:37 -0700515 .BaseMipLevel = 0.0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700516 .SurfaceQPitch = surface->qpitch >> 2,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700517 .Height = image->extent.height - 1,
518 .Width = image->extent.width - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700519 .Depth = depth - 1,
Chad Versacec6e76ae2015-06-26 18:48:34 -0700520 .SurfacePitch = surface->stride - 1,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700521 .MinimumArrayElement = pCreateInfo->baseArraySlice,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700522 .NumberofMultisamples = MULTISAMPLECOUNT_1,
523 .XOffset = 0,
524 .YOffset = 0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700525
526 /* For render target surfaces, the hardware interprets field MIPCount/LOD as
527 * LOD. The Broadwell PRM says:
528 *
529 * MIPCountLOD defines the LOD that will be rendered into.
530 * SurfaceMinLOD is ignored.
531 */
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700532 .SurfaceMinLOD = 0,
Chad Versace5b3a1ce2015-06-26 21:27:54 -0700533 .MIPCountLOD = pCreateInfo->mipLevel,
534
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700535 .AuxiliarySurfaceMode = AUX_NONE,
536 .RedClearColor = 0,
537 .GreenClearColor = 0,
538 .BlueClearColor = 0,
539 .AlphaClearColor = 0,
540 .ShaderChannelSelectRed = SCS_RED,
541 .ShaderChannelSelectGreen = SCS_GREEN,
542 .ShaderChannelSelectBlue = SCS_BLUE,
543 .ShaderChannelSelectAlpha = SCS_ALPHA,
Kristian Høgsberg Kristensena5b49d22015-06-10 23:11:37 -0700544 .ResourceMinLOD = 0.0,
Kristian Høgsberg0dbed612015-05-25 22:12:24 -0700545 .SurfaceBaseAddress = { NULL, view->offset },
546 };
547
548 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700549}
550
Chad Versace127cb3f2015-06-26 20:12:42 -0700551VkResult
552anv_CreateColorAttachmentView(VkDevice _device,
553 const VkColorAttachmentViewCreateInfo *pCreateInfo,
554 VkColorAttachmentView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700555{
556 struct anv_device *device = (struct anv_device *) _device;
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -0700557 struct anv_surface_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700558
559 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
560
561 view = anv_device_alloc(device, sizeof(*view), 8,
562 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
563 if (view == NULL)
564 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
565
Jason Ekstrand4668bbb2015-05-18 20:42:03 -0700566 anv_color_attachment_view_init(view, device, pCreateInfo, NULL);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700567
Jason Ekstrand9d6f55d2015-06-09 11:08:03 -0700568 view->base.destructor = anv_surface_view_destroy;
569
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700570 *pView = (VkColorAttachmentView) view;
571
572 return VK_SUCCESS;
573}
574
Chad Versace127cb3f2015-06-26 20:12:42 -0700575VkResult
576anv_CreateDepthStencilView(VkDevice _device,
577 const VkDepthStencilViewCreateInfo *pCreateInfo,
578 VkDepthStencilView *pView)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700579{
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700580 struct anv_device *device = (struct anv_device *) _device;
581 struct anv_depth_stencil_view *view;
582 struct anv_image *image = (struct anv_image *) pCreateInfo->image;
Chad Versacec6e76ae2015-06-26 18:48:34 -0700583 struct anv_surface *depth_surface = &image->primary_surface;
584 struct anv_surface *stencil_surface = &image->stencil_surface;
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700585 const struct anv_format *format =
586 anv_format_for_vk_format(image->format);
587
588 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO);
589
590 view = anv_device_alloc(device, sizeof(*view), 8,
591 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
592 if (view == NULL)
593 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
594
Jason Ekstrand2a3c2962015-06-10 21:04:51 -0700595 /* XXX: We don't handle any of these */
596 anv_assert(pCreateInfo->mipLevel == 0);
597 anv_assert(pCreateInfo->baseArraySlice == 0);
598 anv_assert(pCreateInfo->arraySize == 1);
599 anv_assert(pCreateInfo->msaaResolveImage == 0);
600
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700601 view->bo = image->bo;
602
Chad Versacec6e76ae2015-06-26 18:48:34 -0700603 view->depth_stride = depth_surface->stride;
604 view->depth_offset = image->offset + depth_surface->offset;
Chad Versaceebe1e762015-06-25 19:29:59 -0700605 view->depth_format = format->depth_format;
Chad Versace7ea707a2015-06-25 19:46:42 -0700606 view->depth_qpitch = 0; /* FINISHME: QPitch */
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700607
Chad Versacec6e76ae2015-06-26 18:48:34 -0700608 view->stencil_stride = stencil_surface->stride;
609 view->stencil_offset = image->offset + stencil_surface->offset;
Chad Versace7ea707a2015-06-25 19:46:42 -0700610 view->stencil_qpitch = 0; /* FINISHME: QPitch */
Kristian Høgsberg37743f92015-05-22 22:59:12 -0700611
612 *pView = (VkDepthStencilView) view;
613
614 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700615}