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