329955fff593e39d291b19a6b603620f9c2c891d
[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 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 uint32_t stride = align_u32(mt_width * format_info->cpp, tile_info->width);
185 if (create_info->stride > 0)
186 stride = create_info->stride;
187
188 const uint32_t size = stride * align_u32(mt_height, tile_info->height);
189 const uint32_t offset = align_u32(*inout_image_size,
190 tile_info->surface_alignment);
191
192 *inout_image_size = offset + size;
193 *inout_image_alignment = MAX(*inout_image_alignment,
194 tile_info->surface_alignment);
195
196 *out_surface = (struct anv_surface) {
197 .offset = offset,
198 .stride = stride,
199 .tile_mode = tile_mode,
200 .qpitch = qpitch,
201 .h_align = i,
202 .v_align = j,
203 };
204
205 return VK_SUCCESS;
206 }
207
208 VkResult
209 anv_image_create(VkDevice _device,
210 const struct anv_image_create_info *create_info,
211 VkImage *pImage)
212 {
213 ANV_FROM_HANDLE(anv_device, device, _device);
214 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
215 const VkExtent3D *restrict extent = &pCreateInfo->extent;
216 struct anv_image *image = NULL;
217 VkResult r;
218
219 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
220
221 /* XXX: We don't handle any of these */
222 anv_assert(pCreateInfo->imageType == VK_IMAGE_TYPE_2D);
223 anv_assert(pCreateInfo->mipLevels > 0);
224 anv_assert(pCreateInfo->arraySize > 0);
225 anv_assert(pCreateInfo->samples == 1);
226 anv_assert(pCreateInfo->extent.width > 0);
227 anv_assert(pCreateInfo->extent.height > 0);
228 anv_assert(pCreateInfo->extent.depth > 0);
229
230 /* TODO(chadv): How should we validate inputs? */
231 const uint8_t surf_type =
232 anv_surf_type_from_image_type[pCreateInfo->imageType];
233
234 const struct anv_surf_type_limits *limits =
235 &anv_surf_type_limits[surf_type];
236
237 if (extent->width > limits->width ||
238 extent->height > limits->height ||
239 extent->depth > limits->depth) {
240 /* TODO(chadv): What is the correct error? */
241 anv_loge("image extent is too large");
242 return vk_error(VK_ERROR_INVALID_MEMORY_SIZE);
243 }
244
245 image = anv_device_alloc(device, sizeof(*image), 8,
246 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
247 if (!image)
248 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
249
250 memset(image, 0, sizeof(*image));
251 image->type = pCreateInfo->imageType;
252 image->extent = pCreateInfo->extent;
253 image->format = anv_format_for_vk_format(pCreateInfo->format);
254 image->levels = pCreateInfo->mipLevels;
255 image->array_size = pCreateInfo->arraySize;
256 image->surf_type = surf_type;
257
258 if (likely(!image->format->has_stencil || image->format->depth_format)) {
259 /* The image's primary surface is a color or depth surface. */
260 r = anv_image_make_surface(create_info, &image->size, &image->alignment,
261 &image->primary_surface);
262 if (r != VK_SUCCESS)
263 goto fail;
264 }
265
266 if (image->format->has_stencil) {
267 /* From the GPU's perspective, the depth buffer and stencil buffer are
268 * separate buffers. From Vulkan's perspective, though, depth and
269 * stencil reside in the same image. To satisfy Vulkan and the GPU, we
270 * place the depth and stencil buffers in the same bo.
271 */
272 VkImageCreateInfo stencil_info = *pCreateInfo;
273 stencil_info.format = VK_FORMAT_S8_UINT;
274
275 r = anv_image_make_surface(
276 &(struct anv_image_create_info) {
277 .vk_info = &stencil_info,
278 },
279 &image->size, &image->alignment, &image->stencil_surface);
280
281 if (r != VK_SUCCESS)
282 goto fail;
283 }
284
285 *pImage = anv_image_to_handle(image);
286
287 return VK_SUCCESS;
288
289 fail:
290 if (image)
291 anv_device_free(device, image);
292
293 return r;
294 }
295
296 VkResult
297 anv_CreateImage(VkDevice device,
298 const VkImageCreateInfo *pCreateInfo,
299 VkImage *pImage)
300 {
301 return anv_image_create(device,
302 &(struct anv_image_create_info) {
303 .vk_info = pCreateInfo,
304 },
305 pImage);
306 }
307
308 VkResult
309 anv_DestroyImage(VkDevice _device, VkImage _image)
310 {
311 ANV_FROM_HANDLE(anv_device, device, _device);
312
313 anv_device_free(device, anv_image_from_handle(_image));
314
315 return VK_SUCCESS;
316 }
317
318 VkResult anv_GetImageSubresourceLayout(
319 VkDevice device,
320 VkImage image,
321 const VkImageSubresource* pSubresource,
322 VkSubresourceLayout* pLayout)
323 {
324 stub_return(VK_UNSUPPORTED);
325 }
326
327 void
328 anv_surface_view_fini(struct anv_device *device,
329 struct anv_surface_view *view)
330 {
331 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
332 }
333
334 void
335 anv_image_view_init(struct anv_image_view *iview,
336 struct anv_device *device,
337 const VkImageViewCreateInfo* pCreateInfo,
338 struct anv_cmd_buffer *cmd_buffer)
339 {
340 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
341
342 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
343 struct anv_surface_view *view = &iview->view;
344 struct anv_surface *surface;
345
346 const struct anv_format *format_info =
347 anv_format_for_vk_format(pCreateInfo->format);
348
349 const struct anv_image_view_info *view_type_info
350 = &anv_image_view_info_table[pCreateInfo->viewType];
351
352 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D)
353 anv_finishme("non-2D image views");
354
355 switch (pCreateInfo->subresourceRange.aspect) {
356 case VK_IMAGE_ASPECT_STENCIL:
357 anv_finishme("stencil image views");
358 abort();
359 break;
360 case VK_IMAGE_ASPECT_DEPTH:
361 case VK_IMAGE_ASPECT_COLOR:
362 view->offset = image->offset;
363 surface = &image->primary_surface;
364 break;
365 default:
366 unreachable("");
367 break;
368 }
369
370 view->bo = image->bo;
371 view->offset = image->offset + surface->offset;
372 view->format = pCreateInfo->format;
373
374 iview->extent = (VkExtent3D) {
375 .width = anv_minify(image->extent.width, range->baseMipLevel),
376 .height = anv_minify(image->extent.height, range->baseMipLevel),
377 .depth = anv_minify(image->extent.depth, range->baseMipLevel),
378 };
379
380 uint32_t depth = 1;
381 if (range->arraySize > 1) {
382 depth = range->arraySize;
383 } else if (image->extent.depth > 1) {
384 depth = image->extent.depth;
385 }
386
387 static const uint32_t vk_to_gen_swizzle[] = {
388 [VK_CHANNEL_SWIZZLE_ZERO] = SCS_ZERO,
389 [VK_CHANNEL_SWIZZLE_ONE] = SCS_ONE,
390 [VK_CHANNEL_SWIZZLE_R] = SCS_RED,
391 [VK_CHANNEL_SWIZZLE_G] = SCS_GREEN,
392 [VK_CHANNEL_SWIZZLE_B] = SCS_BLUE,
393 [VK_CHANNEL_SWIZZLE_A] = SCS_ALPHA
394 };
395
396 struct GEN8_RENDER_SURFACE_STATE surface_state = {
397 .SurfaceType = view_type_info->surface_type,
398 .SurfaceArray = image->array_size > 1,
399 .SurfaceFormat = format_info->surface_format,
400 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
401 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
402 .TileMode = surface->tile_mode,
403 .VerticalLineStride = 0,
404 .VerticalLineStrideOffset = 0,
405 .SamplerL2BypassModeDisable = true,
406 .RenderCacheReadWriteMode = WriteOnlyCache,
407 .MemoryObjectControlState = GEN8_MOCS,
408
409 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
410 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
411 * both Base Mip Level fields nonzero".
412 */
413 .BaseMipLevel = 0.0,
414
415 .SurfaceQPitch = surface->qpitch >> 2,
416 .Height = image->extent.height - 1,
417 .Width = image->extent.width - 1,
418 .Depth = depth - 1,
419 .SurfacePitch = surface->stride - 1,
420 .MinimumArrayElement = range->baseArraySlice,
421 .NumberofMultisamples = MULTISAMPLECOUNT_1,
422 .XOffset = 0,
423 .YOffset = 0,
424
425 /* For sampler surfaces, the hardware interprets field MIPCount/LOD as
426 * MIPCount. The range of levels accessible by the sampler engine is
427 * [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
428 */
429 .MIPCountLOD = range->mipLevels - 1,
430 .SurfaceMinLOD = range->baseMipLevel,
431
432 .AuxiliarySurfaceMode = AUX_NONE,
433 .RedClearColor = 0,
434 .GreenClearColor = 0,
435 .BlueClearColor = 0,
436 .AlphaClearColor = 0,
437 .ShaderChannelSelectRed = vk_to_gen_swizzle[pCreateInfo->channels.r],
438 .ShaderChannelSelectGreen = vk_to_gen_swizzle[pCreateInfo->channels.g],
439 .ShaderChannelSelectBlue = vk_to_gen_swizzle[pCreateInfo->channels.b],
440 .ShaderChannelSelectAlpha = vk_to_gen_swizzle[pCreateInfo->channels.a],
441 .ResourceMinLOD = 0.0,
442 .SurfaceBaseAddress = { NULL, view->offset },
443 };
444
445 if (cmd_buffer) {
446 view->surface_state =
447 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
448 } else {
449 view->surface_state =
450 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
451 }
452
453 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
454 }
455
456 VkResult
457 anv_validate_CreateImageView(VkDevice _device,
458 const VkImageViewCreateInfo *pCreateInfo,
459 VkImageView *pView)
460 {
461 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
462 const VkImageSubresourceRange *subresource;
463 const struct anv_image_view_info *view_info;
464 const struct anv_format *view_format_info;
465
466 /* Validate structure type before dereferencing it. */
467 assert(pCreateInfo);
468 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
469 subresource = &pCreateInfo->subresourceRange;
470
471 /* Validate viewType is in range before using it. */
472 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
473 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
474 view_info = &anv_image_view_info_table[pCreateInfo->viewType];
475
476 /* Validate format is in range before using it. */
477 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
478 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
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->depth_format);
511 assert(!image->format->has_stencil);
512 assert(!view_format_info->depth_format);
513 assert(!view_format_info->has_stencil);
514 assert(view_format_info->cpp == image->format->cpp);
515 break;
516 case VK_IMAGE_ASPECT_DEPTH:
517 assert(image->format->depth_format);
518 assert(view_format_info->depth_format);
519 assert(view_format_info->cpp == image->format->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->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_image_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 = anv_image_view_to_handle(view);
550
551 return VK_SUCCESS;
552 }
553
554 VkResult
555 anv_DestroyImageView(VkDevice _device, VkImageView _iview)
556 {
557 ANV_FROM_HANDLE(anv_device, device, _device);
558 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
559
560 anv_surface_view_fini(device, &iview->view);
561 anv_device_free(device, iview);
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 aview->base.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
609 struct GEN8_RENDER_SURFACE_STATE surface_state = {
610 .SurfaceType = SURFTYPE_2D,
611 .SurfaceArray = image->array_size > 1,
612 .SurfaceFormat = format_info->surface_format,
613 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
614 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
615 .TileMode = surface->tile_mode,
616 .VerticalLineStride = 0,
617 .VerticalLineStrideOffset = 0,
618 .SamplerL2BypassModeDisable = true,
619 .RenderCacheReadWriteMode = WriteOnlyCache,
620 .MemoryObjectControlState = GEN8_MOCS,
621
622 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
623 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
624 * both Base Mip Level fields nonzero".
625 */
626 .BaseMipLevel = 0.0,
627
628 .SurfaceQPitch = surface->qpitch >> 2,
629 .Height = image->extent.height - 1,
630 .Width = image->extent.width - 1,
631 .Depth = depth - 1,
632 .SurfacePitch = surface->stride - 1,
633 .MinimumArrayElement = pCreateInfo->baseArraySlice,
634 .NumberofMultisamples = MULTISAMPLECOUNT_1,
635 .XOffset = 0,
636 .YOffset = 0,
637
638 /* For render target surfaces, the hardware interprets field MIPCount/LOD as
639 * LOD. The Broadwell PRM says:
640 *
641 * MIPCountLOD defines the LOD that will be rendered into.
642 * SurfaceMinLOD is ignored.
643 */
644 .SurfaceMinLOD = 0,
645 .MIPCountLOD = pCreateInfo->mipLevel,
646
647 .AuxiliarySurfaceMode = AUX_NONE,
648 .RedClearColor = 0,
649 .GreenClearColor = 0,
650 .BlueClearColor = 0,
651 .AlphaClearColor = 0,
652 .ShaderChannelSelectRed = SCS_RED,
653 .ShaderChannelSelectGreen = SCS_GREEN,
654 .ShaderChannelSelectBlue = SCS_BLUE,
655 .ShaderChannelSelectAlpha = SCS_ALPHA,
656 .ResourceMinLOD = 0.0,
657 .SurfaceBaseAddress = { NULL, view->offset },
658 };
659
660 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
661 }
662
663 static void
664 anv_depth_stencil_view_init(struct anv_depth_stencil_view *view,
665 const VkAttachmentViewCreateInfo *pCreateInfo)
666 {
667 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
668 struct anv_surface *depth_surface = &image->primary_surface;
669 struct anv_surface *stencil_surface = &image->stencil_surface;
670
671 view->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL;
672
673 /* XXX: We don't handle any of these */
674 anv_assert(pCreateInfo->mipLevel == 0);
675 anv_assert(pCreateInfo->baseArraySlice == 0);
676 anv_assert(pCreateInfo->arraySize == 1);
677
678 view->bo = image->bo;
679
680 view->depth_stride = depth_surface->stride;
681 view->depth_offset = image->offset + depth_surface->offset;
682 view->depth_format = image->format->depth_format;
683 view->depth_qpitch = 0; /* FINISHME: QPitch */
684
685 view->stencil_stride = stencil_surface->stride;
686 view->stencil_offset = image->offset + stencil_surface->offset;
687 view->stencil_qpitch = 0; /* FINISHME: QPitch */
688 }
689
690 VkResult
691 anv_CreateAttachmentView(VkDevice _device,
692 const VkAttachmentViewCreateInfo *pCreateInfo,
693 VkAttachmentView *pView)
694 {
695 ANV_FROM_HANDLE(anv_device, device, _device);
696
697 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO);
698
699 if (anv_is_vk_format_depth_or_stencil(pCreateInfo->format)) {
700 struct anv_depth_stencil_view *view =
701 anv_device_alloc(device, sizeof(*view), 8,
702 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
703 if (view == NULL)
704 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
705
706 anv_depth_stencil_view_init(view, pCreateInfo);
707
708 *pView = anv_attachment_view_to_handle(&view->base);
709 } else {
710 struct anv_color_attachment_view *view =
711 anv_device_alloc(device, sizeof(*view), 8,
712 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
713 if (view == NULL)
714 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
715
716 anv_color_attachment_view_init(view, device, pCreateInfo, NULL);
717
718 *pView = anv_attachment_view_to_handle(&view->base);
719 }
720
721 return VK_SUCCESS;
722 }
723
724 VkResult
725 anv_DestroyAttachmentView(VkDevice _device, VkAttachmentView _view)
726 {
727 ANV_FROM_HANDLE(anv_device, device, _device);
728 ANV_FROM_HANDLE(anv_attachment_view, view, _view);
729
730 if (view->attachment_type == ANV_ATTACHMENT_VIEW_TYPE_COLOR) {
731 struct anv_color_attachment_view *aview =
732 (struct anv_color_attachment_view *)view;
733
734 anv_surface_view_fini(device, &aview->view);
735 }
736
737 anv_device_free(device, view);
738
739 return VK_SUCCESS;
740 }