vk: Add the new extension/layer enumeration entrypoints
[mesa.git] / src / vulkan / 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 "private.h"
31
32 struct anv_image_view_info {
33 uint8_t surface_type; /**< RENDER_SURFACE_STATE.SurfaceType */
34 bool is_array:1; /**< RENDER_SURFACE_STATE.SurfaceArray */
35 bool is_cube:1; /**< RENDER_SURFACE_STATE.CubeFaceEnable* */
36 };
37
38 static const uint8_t anv_halign[] = {
39 [4] = HALIGN4,
40 [8] = HALIGN8,
41 [16] = HALIGN16,
42 };
43
44 static const uint8_t anv_valign[] = {
45 [4] = VALIGN4,
46 [8] = VALIGN8,
47 [16] = VALIGN16,
48 };
49
50 static const uint8_t anv_surf_type_from_image_type[] = {
51 [VK_IMAGE_TYPE_1D] = SURFTYPE_1D,
52 [VK_IMAGE_TYPE_2D] = SURFTYPE_2D,
53 [VK_IMAGE_TYPE_3D] = SURFTYPE_3D,
54
55 };
56
57 static const struct anv_image_view_info
58 anv_image_view_info_table[] = {
59 #define INFO(s, ...) { .surface_type = s, __VA_ARGS__ }
60 [VK_IMAGE_VIEW_TYPE_1D] = INFO(SURFTYPE_1D),
61 [VK_IMAGE_VIEW_TYPE_2D] = INFO(SURFTYPE_2D),
62 [VK_IMAGE_VIEW_TYPE_3D] = INFO(SURFTYPE_3D),
63 [VK_IMAGE_VIEW_TYPE_CUBE] = INFO(SURFTYPE_CUBE, .is_cube = 1),
64 [VK_IMAGE_VIEW_TYPE_1D_ARRAY] = INFO(SURFTYPE_1D, .is_array = 1),
65 [VK_IMAGE_VIEW_TYPE_2D_ARRAY] = INFO(SURFTYPE_2D, .is_array = 1),
66 [VK_IMAGE_VIEW_TYPE_CUBE_ARRAY] = INFO(SURFTYPE_CUBE, .is_array = 1, .is_cube = 1),
67 #undef INFO
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 static VkResult
127 anv_image_make_surface(const struct anv_image_create_info *create_info,
128 uint64_t *inout_image_size,
129 uint32_t *inout_image_alignment,
130 struct anv_surface *out_surface)
131 {
132 /* See RENDER_SURFACE_STATE.SurfaceQPitch */
133 static const uint16_t min_qpitch UNUSED = 0x4;
134 static const uint16_t max_qpitch UNUSED = 0x1ffc;
135
136 const VkExtent3D *restrict extent = &create_info->vk_info->extent;
137 const uint32_t levels = create_info->vk_info->mipLevels;
138 const uint32_t array_size = create_info->vk_info->arraySize;
139
140 const uint8_t tile_mode = anv_image_choose_tile_mode(create_info);
141
142 const struct anv_tile_info *tile_info =
143 &anv_tile_info_table[tile_mode];
144
145 const struct anv_format *format_info =
146 anv_format_for_vk_format(create_info->vk_info->format);
147
148 const uint32_t i = 4; /* FINISHME: Stop hardcoding subimage alignment */
149 const uint32_t j = 4; /* FINISHME: Stop hardcoding subimage alignment */
150 const uint32_t w0 = align_u32(extent->width, i);
151 const uint32_t h0 = align_u32(extent->height, j);
152
153 uint16_t qpitch;
154 uint32_t mt_width;
155 uint32_t mt_height;
156
157 if (levels == 1 && array_size == 1) {
158 qpitch = min_qpitch;
159 mt_width = w0;
160 mt_height = h0;
161 } else {
162 uint32_t w1 = align_u32(anv_minify(extent->width, 1), i);
163 uint32_t h1 = align_u32(anv_minify(extent->height, 1), j);
164 uint32_t w2 = align_u32(anv_minify(extent->width, 2), i);
165
166 qpitch = h0 + h1 + 11 * j;
167 mt_width = MAX(w0, w1 + w2);
168 mt_height = array_size * qpitch;
169 }
170
171 assert(qpitch >= min_qpitch);
172 if (qpitch > max_qpitch) {
173 anv_loge("image qpitch > 0x%x\n", max_qpitch);
174 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
175 }
176
177 /* From the Broadwell PRM, RENDER_SURFACE_STATE.SurfaceQpitch:
178 *
179 * This field must be set an integer multiple of the Surface Vertical
180 * Alignment.
181 */
182 assert(anv_is_aligned(qpitch, j));
183
184 const uint32_t stride = align_u32(mt_width * format_info->cpp,
185 tile_info->width);
186 const uint32_t size = stride * align_u32(mt_height, tile_info->height);
187 const uint32_t offset = align_u32(*inout_image_size,
188 tile_info->surface_alignment);
189
190 *inout_image_size = offset + size;
191 *inout_image_alignment = MAX(*inout_image_alignment,
192 tile_info->surface_alignment);
193
194 *out_surface = (struct anv_surface) {
195 .offset = offset,
196 .stride = stride,
197 .tile_mode = tile_mode,
198 .qpitch = qpitch,
199 .h_align = i,
200 .v_align = j,
201 };
202
203 return VK_SUCCESS;
204 }
205
206 VkResult
207 anv_image_create(VkDevice _device,
208 const struct anv_image_create_info *create_info,
209 VkImage *pImage)
210 {
211 ANV_FROM_HANDLE(anv_device, device, _device);
212 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
213 const VkExtent3D *restrict extent = &pCreateInfo->extent;
214 struct anv_image *image = NULL;
215 VkResult r;
216
217 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
218
219 /* XXX: We don't handle any of these */
220 anv_assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D);
221 anv_assert(pCreateInfo->mipLevels > 0);
222 anv_assert(pCreateInfo->arraySize > 0);
223 anv_assert(pCreateInfo->samples == 1);
224 anv_assert(pCreateInfo->extent.width > 0);
225 anv_assert(pCreateInfo->extent.height > 0);
226 anv_assert(pCreateInfo->extent.depth > 0);
227
228 /* TODO(chadv): How should we validate inputs? */
229 const uint8_t surf_type =
230 anv_surf_type_from_image_type[pCreateInfo->imageType];
231
232 const struct anv_surf_type_limits *limits =
233 &anv_surf_type_limits[surf_type];
234
235 if (extent->width > limits->width ||
236 extent->height > limits->height ||
237 extent->depth > limits->depth) {
238 /* TODO(chadv): What is the correct error? */
239 anv_loge("image extent is too large");
240 return vk_error(VK_ERROR_INVALID_MEMORY_SIZE);
241 }
242
243 const struct anv_format *format_info =
244 anv_format_for_vk_format(pCreateInfo->format);
245
246 image = anv_device_alloc(device, sizeof(*image), 8,
247 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
248 if (!image)
249 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
250
251 memset(image, 0, sizeof(*image));
252 image->type = pCreateInfo->imageType;
253 image->extent = pCreateInfo->extent;
254 image->format = pCreateInfo->format;
255 image->levels = pCreateInfo->mipLevels;
256 image->array_size = pCreateInfo->arraySize;
257 image->surf_type = surf_type;
258
259 if (likely(!format_info->has_stencil || format_info->depth_format)) {
260 /* The image's primary surface is a color or depth surface. */
261 r = anv_image_make_surface(create_info, &image->size, &image->alignment,
262 &image->primary_surface);
263 if (r != VK_SUCCESS)
264 goto fail;
265 }
266
267 if (format_info->has_stencil) {
268 /* From the GPU's perspective, the depth buffer and stencil buffer are
269 * separate buffers. From Vulkan's perspective, though, depth and
270 * stencil reside in the same image. To satisfy Vulkan and the GPU, we
271 * place the depth and stencil buffers in the same bo.
272 */
273 VkImageCreateInfo stencil_info = *pCreateInfo;
274 stencil_info.format = VK_FORMAT_S8_UINT;
275
276 r = anv_image_make_surface(
277 &(struct anv_image_create_info) {
278 .vk_info = &stencil_info,
279 },
280 &image->size, &image->alignment, &image->stencil_surface);
281
282 if (r != VK_SUCCESS)
283 goto fail;
284 }
285
286 *pImage = anv_image_to_handle(image);
287
288 return VK_SUCCESS;
289
290 fail:
291 if (image)
292 anv_device_free(device, image);
293
294 return r;
295 }
296
297 VkResult
298 anv_CreateImage(VkDevice device,
299 const VkImageCreateInfo *pCreateInfo,
300 VkImage *pImage)
301 {
302 return anv_image_create(device,
303 &(struct anv_image_create_info) {
304 .vk_info = pCreateInfo,
305 },
306 pImage);
307 }
308
309 VkResult
310 anv_DestroyImage(VkDevice _device, VkImage _image)
311 {
312 ANV_FROM_HANDLE(anv_device, device, _device);
313
314 anv_device_free(device, anv_image_from_handle(_image));
315
316 return VK_SUCCESS;
317 }
318
319 VkResult anv_GetImageSubresourceLayout(
320 VkDevice device,
321 VkImage image,
322 const VkImageSubresource* pSubresource,
323 VkSubresourceLayout* pLayout)
324 {
325 stub_return(VK_UNSUPPORTED);
326 }
327
328 void
329 anv_surface_view_fini(struct anv_device *device,
330 struct anv_surface_view *view)
331 {
332 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
333 }
334
335 void
336 anv_image_view_init(struct anv_surface_view *view,
337 struct anv_device *device,
338 const VkImageViewCreateInfo* pCreateInfo,
339 struct anv_cmd_buffer *cmd_buffer)
340 {
341 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
342 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
343 struct anv_surface *surface;
344
345 const struct anv_format *format_info =
346 anv_format_for_vk_format(pCreateInfo->format);
347
348 const struct anv_image_view_info *view_type_info
349 = &anv_image_view_info_table[pCreateInfo->viewType];
350
351 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D)
352 anv_finishme("non-2D image views");
353
354 switch (pCreateInfo->subresourceRange.aspect) {
355 case VK_IMAGE_ASPECT_STENCIL:
356 anv_finishme("stencil image views");
357 abort();
358 break;
359 case VK_IMAGE_ASPECT_DEPTH:
360 case VK_IMAGE_ASPECT_COLOR:
361 view->offset = image->offset;
362 surface = &image->primary_surface;
363 break;
364 default:
365 unreachable("");
366 break;
367 }
368
369 view->bo = image->bo;
370 view->offset = image->offset + surface->offset;
371 view->format = pCreateInfo->format;
372
373 view->extent = (VkExtent3D) {
374 .width = anv_minify(image->extent.width, range->baseMipLevel),
375 .height = anv_minify(image->extent.height, range->baseMipLevel),
376 .depth = anv_minify(image->extent.depth, range->baseMipLevel),
377 };
378
379 uint32_t depth = 1;
380 if (range->arraySize > 1) {
381 depth = range->arraySize;
382 } else if (image->extent.depth > 1) {
383 depth = image->extent.depth;
384 }
385
386 static const uint32_t vk_to_gen_swizzle[] = {
387 [VK_CHANNEL_SWIZZLE_ZERO] = SCS_ZERO,
388 [VK_CHANNEL_SWIZZLE_ONE] = SCS_ONE,
389 [VK_CHANNEL_SWIZZLE_R] = SCS_RED,
390 [VK_CHANNEL_SWIZZLE_G] = SCS_GREEN,
391 [VK_CHANNEL_SWIZZLE_B] = SCS_BLUE,
392 [VK_CHANNEL_SWIZZLE_A] = SCS_ALPHA
393 };
394
395 struct GEN8_RENDER_SURFACE_STATE surface_state = {
396 .SurfaceType = view_type_info->surface_type,
397 .SurfaceArray = image->array_size > 1,
398 .SurfaceFormat = format_info->surface_format,
399 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
400 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
401 .TileMode = surface->tile_mode,
402 .VerticalLineStride = 0,
403 .VerticalLineStrideOffset = 0,
404 .SamplerL2BypassModeDisable = true,
405 .RenderCacheReadWriteMode = WriteOnlyCache,
406 .MemoryObjectControlState = GEN8_MOCS,
407
408 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
409 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
410 * both Base Mip Level fields nonzero".
411 */
412 .BaseMipLevel = 0.0,
413
414 .SurfaceQPitch = surface->qpitch >> 2,
415 .Height = image->extent.height - 1,
416 .Width = image->extent.width - 1,
417 .Depth = depth - 1,
418 .SurfacePitch = surface->stride - 1,
419 .MinimumArrayElement = range->baseArraySlice,
420 .NumberofMultisamples = MULTISAMPLECOUNT_1,
421 .XOffset = 0,
422 .YOffset = 0,
423
424 /* For sampler surfaces, the hardware interprets field MIPCount/LOD as
425 * MIPCount. The range of levels accessible by the sampler engine is
426 * [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
427 */
428 .MIPCountLOD = range->mipLevels - 1,
429 .SurfaceMinLOD = range->baseMipLevel,
430
431 .AuxiliarySurfaceMode = AUX_NONE,
432 .RedClearColor = 0,
433 .GreenClearColor = 0,
434 .BlueClearColor = 0,
435 .AlphaClearColor = 0,
436 .ShaderChannelSelectRed = vk_to_gen_swizzle[pCreateInfo->channels.r],
437 .ShaderChannelSelectGreen = vk_to_gen_swizzle[pCreateInfo->channels.g],
438 .ShaderChannelSelectBlue = vk_to_gen_swizzle[pCreateInfo->channels.b],
439 .ShaderChannelSelectAlpha = vk_to_gen_swizzle[pCreateInfo->channels.a],
440 .ResourceMinLOD = 0.0,
441 .SurfaceBaseAddress = { NULL, view->offset },
442 };
443
444 if (cmd_buffer)
445 view->surface_state =
446 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
447 else
448 view->surface_state =
449 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
450
451 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
452 }
453
454 VkResult
455 anv_validate_CreateImageView(VkDevice _device,
456 const VkImageViewCreateInfo *pCreateInfo,
457 VkImageView *pView)
458 {
459 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
460 const VkImageSubresourceRange *subresource;
461 const struct anv_image_view_info *view_info;
462 const struct anv_format *view_format_info;
463 const struct anv_format *image_format_info;
464
465 /* Validate structure type before dereferencing it. */
466 assert(pCreateInfo);
467 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
468 subresource = &pCreateInfo->subresourceRange;
469
470 /* Validate viewType is in range before using it. */
471 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
472 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
473 view_info = &anv_image_view_info_table[pCreateInfo->viewType];
474
475 /* Validate format is in range before using it. */
476 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
477 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
478 image_format_info = anv_format_for_vk_format(image->format);
479 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
480
481 /* Validate channel swizzles. */
482 assert(pCreateInfo->channels.r >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
483 assert(pCreateInfo->channels.r <= VK_CHANNEL_SWIZZLE_END_RANGE);
484 assert(pCreateInfo->channels.g >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
485 assert(pCreateInfo->channels.g <= VK_CHANNEL_SWIZZLE_END_RANGE);
486 assert(pCreateInfo->channels.b >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
487 assert(pCreateInfo->channels.b <= VK_CHANNEL_SWIZZLE_END_RANGE);
488 assert(pCreateInfo->channels.a >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
489 assert(pCreateInfo->channels.a <= VK_CHANNEL_SWIZZLE_END_RANGE);
490
491 /* Validate subresource. */
492 assert(subresource->aspect >= VK_IMAGE_ASPECT_BEGIN_RANGE);
493 assert(subresource->aspect <= VK_IMAGE_ASPECT_END_RANGE);
494 assert(subresource->mipLevels > 0);
495 assert(subresource->arraySize > 0);
496 assert(subresource->baseMipLevel < image->levels);
497 assert(subresource->baseMipLevel + subresource->mipLevels <= image->levels);
498 assert(subresource->baseArraySlice < image->array_size);
499 assert(subresource->baseArraySlice + subresource->arraySize <= image->array_size);
500 assert(pView);
501
502 if (view_info->is_cube) {
503 assert(subresource->baseArraySlice % 6 == 0);
504 assert(subresource->arraySize % 6 == 0);
505 }
506
507 /* Validate format. */
508 switch (subresource->aspect) {
509 case VK_IMAGE_ASPECT_COLOR:
510 assert(!image_format_info->depth_format);
511 assert(!image_format_info->has_stencil);
512 assert(!view_format_info->depth_format);
513 assert(!view_format_info->has_stencil);
514 assert(view_format_info->cpp == image_format_info->cpp);
515 break;
516 case VK_IMAGE_ASPECT_DEPTH:
517 assert(image_format_info->depth_format);
518 assert(view_format_info->depth_format);
519 assert(view_format_info->cpp == image_format_info->cpp);
520 break;
521 case VK_IMAGE_ASPECT_STENCIL:
522 /* FINISHME: Is it legal to have an R8 view of S8? */
523 assert(image_format_info->has_stencil);
524 assert(view_format_info->has_stencil);
525 break;
526 default:
527 assert(!"bad VkImageAspect");
528 break;
529 }
530
531 return anv_CreateImageView(_device, pCreateInfo, pView);
532 }
533
534 VkResult
535 anv_CreateImageView(VkDevice _device,
536 const VkImageViewCreateInfo *pCreateInfo,
537 VkImageView *pView)
538 {
539 ANV_FROM_HANDLE(anv_device, device, _device);
540 struct anv_surface_view *view;
541
542 view = anv_device_alloc(device, sizeof(*view), 8,
543 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
544 if (view == NULL)
545 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
546
547 anv_image_view_init(view, device, pCreateInfo, NULL);
548
549 *pView = (VkImageView) view;
550
551 return VK_SUCCESS;
552 }
553
554 VkResult
555 anv_DestroyImageView(VkDevice _device, VkImageView _view)
556 {
557 ANV_FROM_HANDLE(anv_device, device, _device);
558 struct anv_surface_view *view = (struct anv_surface_view *)_view;
559
560 anv_surface_view_fini(device, view);
561 anv_device_free(device, view);
562
563 return VK_SUCCESS;
564 }
565
566 void
567 anv_color_attachment_view_init(struct anv_color_attachment_view *aview,
568 struct anv_device *device,
569 const VkAttachmentViewCreateInfo* pCreateInfo,
570 struct anv_cmd_buffer *cmd_buffer)
571 {
572 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
573 struct anv_surface_view *view = &aview->view;
574 struct anv_surface *surface = &image->primary_surface;
575 const struct anv_format *format_info =
576 anv_format_for_vk_format(pCreateInfo->format);
577
578 aview->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_COLOR;
579
580 anv_assert(pCreateInfo->arraySize > 0);
581 anv_assert(pCreateInfo->mipLevel < image->levels);
582 anv_assert(pCreateInfo->baseArraySlice + pCreateInfo->arraySize <= image->array_size);
583
584 view->bo = image->bo;
585 view->offset = image->offset + surface->offset;
586 view->format = pCreateInfo->format;
587
588 view->extent = (VkExtent3D) {
589 .width = anv_minify(image->extent.width, pCreateInfo->mipLevel),
590 .height = anv_minify(image->extent.height, pCreateInfo->mipLevel),
591 .depth = anv_minify(image->extent.depth, pCreateInfo->mipLevel),
592 };
593
594 uint32_t depth = 1;
595 if (pCreateInfo->arraySize > 1) {
596 depth = pCreateInfo->arraySize;
597 } else if (image->extent.depth > 1) {
598 depth = image->extent.depth;
599 }
600
601 if (cmd_buffer)
602 view->surface_state =
603 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
604 else
605 view->surface_state =
606 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
607
608 struct GEN8_RENDER_SURFACE_STATE surface_state = {
609 .SurfaceType = SURFTYPE_2D,
610 .SurfaceArray = image->array_size > 1,
611 .SurfaceFormat = format_info->surface_format,
612 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
613 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
614 .TileMode = surface->tile_mode,
615 .VerticalLineStride = 0,
616 .VerticalLineStrideOffset = 0,
617 .SamplerL2BypassModeDisable = true,
618 .RenderCacheReadWriteMode = WriteOnlyCache,
619 .MemoryObjectControlState = GEN8_MOCS,
620
621 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
622 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
623 * both Base Mip Level fields nonzero".
624 */
625 .BaseMipLevel = 0.0,
626
627 .SurfaceQPitch = surface->qpitch >> 2,
628 .Height = image->extent.height - 1,
629 .Width = image->extent.width - 1,
630 .Depth = depth - 1,
631 .SurfacePitch = surface->stride - 1,
632 .MinimumArrayElement = pCreateInfo->baseArraySlice,
633 .NumberofMultisamples = MULTISAMPLECOUNT_1,
634 .XOffset = 0,
635 .YOffset = 0,
636
637 /* For render target surfaces, the hardware interprets field MIPCount/LOD as
638 * LOD. The Broadwell PRM says:
639 *
640 * MIPCountLOD defines the LOD that will be rendered into.
641 * SurfaceMinLOD is ignored.
642 */
643 .SurfaceMinLOD = 0,
644 .MIPCountLOD = pCreateInfo->mipLevel,
645
646 .AuxiliarySurfaceMode = AUX_NONE,
647 .RedClearColor = 0,
648 .GreenClearColor = 0,
649 .BlueClearColor = 0,
650 .AlphaClearColor = 0,
651 .ShaderChannelSelectRed = SCS_RED,
652 .ShaderChannelSelectGreen = SCS_GREEN,
653 .ShaderChannelSelectBlue = SCS_BLUE,
654 .ShaderChannelSelectAlpha = SCS_ALPHA,
655 .ResourceMinLOD = 0.0,
656 .SurfaceBaseAddress = { NULL, view->offset },
657 };
658
659 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
660 }
661
662 static void
663 anv_depth_stencil_view_init(struct anv_depth_stencil_view *view,
664 const VkAttachmentViewCreateInfo *pCreateInfo)
665 {
666 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
667 struct anv_surface *depth_surface = &image->primary_surface;
668 struct anv_surface *stencil_surface = &image->stencil_surface;
669 const struct anv_format *format =
670 anv_format_for_vk_format(image->format);
671
672 view->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL;
673
674 /* XXX: We don't handle any of these */
675 anv_assert(pCreateInfo->mipLevel == 0);
676 anv_assert(pCreateInfo->baseArraySlice == 0);
677 anv_assert(pCreateInfo->arraySize == 1);
678
679 view->bo = image->bo;
680
681 view->depth_stride = depth_surface->stride;
682 view->depth_offset = image->offset + depth_surface->offset;
683 view->depth_format = format->depth_format;
684 view->depth_qpitch = 0; /* FINISHME: QPitch */
685
686 view->stencil_stride = stencil_surface->stride;
687 view->stencil_offset = image->offset + stencil_surface->offset;
688 view->stencil_qpitch = 0; /* FINISHME: QPitch */
689 }
690
691 VkResult
692 anv_CreateAttachmentView(VkDevice _device,
693 const VkAttachmentViewCreateInfo *pCreateInfo,
694 VkAttachmentView *pView)
695 {
696 ANV_FROM_HANDLE(anv_device, device, _device);
697
698 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO);
699
700 if (anv_is_vk_format_depth_or_stencil(pCreateInfo->format)) {
701 struct anv_depth_stencil_view *view =
702 anv_device_alloc(device, sizeof(*view), 8,
703 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
704 if (view == NULL)
705 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
706
707 anv_depth_stencil_view_init(view, pCreateInfo);
708
709 *pView = anv_attachment_view_to_handle(&view->base);
710 } else {
711 struct anv_color_attachment_view *view =
712 anv_device_alloc(device, sizeof(*view), 8,
713 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
714 if (view == NULL)
715 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
716
717 anv_color_attachment_view_init(view, device, pCreateInfo, NULL);
718
719 *pView = anv_attachment_view_to_handle(&view->base);
720 }
721
722 return VK_SUCCESS;
723 }
724
725 VkResult
726 anv_DestroyAttachmentView(VkDevice _device, VkAttachmentView _view)
727 {
728 ANV_FROM_HANDLE(anv_device, device, _device);
729 ANV_FROM_HANDLE(anv_attachment_view, view, _view);
730
731 if (view->attachment_type == ANV_ATTACHMENT_VIEW_TYPE_COLOR) {
732 struct anv_color_attachment_view *aview =
733 (struct anv_color_attachment_view *)view;
734
735 anv_surface_view_fini(device, &aview->view);
736 }
737
738 anv_device_free(device, view);
739
740 return VK_SUCCESS;
741 }