vk/0.132: Add vkDestroyDescriptorPool()
[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_destroy(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 anv_device_free(device, view);
335 }
336
337 void
338 anv_image_view_init(struct anv_surface_view *view,
339 struct anv_device *device,
340 const VkImageViewCreateInfo* pCreateInfo,
341 struct anv_cmd_buffer *cmd_buffer)
342 {
343 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
344 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
345 struct anv_surface *surface;
346
347 const struct anv_format *format_info =
348 anv_format_for_vk_format(pCreateInfo->format);
349
350 const struct anv_image_view_info *view_type_info
351 = &anv_image_view_info_table[pCreateInfo->viewType];
352
353 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D)
354 anv_finishme("non-2D image views");
355
356 switch (pCreateInfo->subresourceRange.aspect) {
357 case VK_IMAGE_ASPECT_STENCIL:
358 anv_finishme("stencil image views");
359 abort();
360 break;
361 case VK_IMAGE_ASPECT_DEPTH:
362 case VK_IMAGE_ASPECT_COLOR:
363 view->offset = image->offset;
364 surface = &image->primary_surface;
365 break;
366 default:
367 unreachable("");
368 break;
369 }
370
371 view->bo = image->bo;
372 view->offset = image->offset + surface->offset;
373 view->format = pCreateInfo->format;
374
375 view->extent = (VkExtent3D) {
376 .width = anv_minify(image->extent.width, range->baseMipLevel),
377 .height = anv_minify(image->extent.height, range->baseMipLevel),
378 .depth = anv_minify(image->extent.depth, range->baseMipLevel),
379 };
380
381 uint32_t depth = 1;
382 if (range->arraySize > 1) {
383 depth = range->arraySize;
384 } else if (image->extent.depth > 1) {
385 depth = image->extent.depth;
386 }
387
388 static const uint32_t vk_to_gen_swizzle[] = {
389 [VK_CHANNEL_SWIZZLE_ZERO] = SCS_ZERO,
390 [VK_CHANNEL_SWIZZLE_ONE] = SCS_ONE,
391 [VK_CHANNEL_SWIZZLE_R] = SCS_RED,
392 [VK_CHANNEL_SWIZZLE_G] = SCS_GREEN,
393 [VK_CHANNEL_SWIZZLE_B] = SCS_BLUE,
394 [VK_CHANNEL_SWIZZLE_A] = SCS_ALPHA
395 };
396
397 struct GEN8_RENDER_SURFACE_STATE surface_state = {
398 .SurfaceType = view_type_info->surface_type,
399 .SurfaceArray = image->array_size > 1,
400 .SurfaceFormat = format_info->surface_format,
401 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
402 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
403 .TileMode = surface->tile_mode,
404 .VerticalLineStride = 0,
405 .VerticalLineStrideOffset = 0,
406 .SamplerL2BypassModeDisable = true,
407 .RenderCacheReadWriteMode = WriteOnlyCache,
408 .MemoryObjectControlState = GEN8_MOCS,
409
410 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
411 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
412 * both Base Mip Level fields nonzero".
413 */
414 .BaseMipLevel = 0.0,
415
416 .SurfaceQPitch = surface->qpitch >> 2,
417 .Height = image->extent.height - 1,
418 .Width = image->extent.width - 1,
419 .Depth = depth - 1,
420 .SurfacePitch = surface->stride - 1,
421 .MinimumArrayElement = range->baseArraySlice,
422 .NumberofMultisamples = MULTISAMPLECOUNT_1,
423 .XOffset = 0,
424 .YOffset = 0,
425
426 /* For sampler surfaces, the hardware interprets field MIPCount/LOD as
427 * MIPCount. The range of levels accessible by the sampler engine is
428 * [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
429 */
430 .MIPCountLOD = range->mipLevels - 1,
431 .SurfaceMinLOD = range->baseMipLevel,
432
433 .AuxiliarySurfaceMode = AUX_NONE,
434 .RedClearColor = 0,
435 .GreenClearColor = 0,
436 .BlueClearColor = 0,
437 .AlphaClearColor = 0,
438 .ShaderChannelSelectRed = vk_to_gen_swizzle[pCreateInfo->channels.r],
439 .ShaderChannelSelectGreen = vk_to_gen_swizzle[pCreateInfo->channels.g],
440 .ShaderChannelSelectBlue = vk_to_gen_swizzle[pCreateInfo->channels.b],
441 .ShaderChannelSelectAlpha = vk_to_gen_swizzle[pCreateInfo->channels.a],
442 .ResourceMinLOD = 0.0,
443 .SurfaceBaseAddress = { NULL, view->offset },
444 };
445
446 if (cmd_buffer)
447 view->surface_state =
448 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
449 else
450 view->surface_state =
451 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
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 const struct anv_format *image_format_info;
466
467 /* Validate structure type before dereferencing it. */
468 assert(pCreateInfo);
469 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
470 subresource = &pCreateInfo->subresourceRange;
471
472 /* Validate viewType is in range before using it. */
473 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
474 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
475 view_info = &anv_image_view_info_table[pCreateInfo->viewType];
476
477 /* Validate format is in range before using it. */
478 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
479 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
480 image_format_info = anv_format_for_vk_format(image->format);
481 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
482
483 /* Validate channel swizzles. */
484 assert(pCreateInfo->channels.r >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
485 assert(pCreateInfo->channels.r <= VK_CHANNEL_SWIZZLE_END_RANGE);
486 assert(pCreateInfo->channels.g >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
487 assert(pCreateInfo->channels.g <= VK_CHANNEL_SWIZZLE_END_RANGE);
488 assert(pCreateInfo->channels.b >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
489 assert(pCreateInfo->channels.b <= VK_CHANNEL_SWIZZLE_END_RANGE);
490 assert(pCreateInfo->channels.a >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
491 assert(pCreateInfo->channels.a <= VK_CHANNEL_SWIZZLE_END_RANGE);
492
493 /* Validate subresource. */
494 assert(subresource->aspect >= VK_IMAGE_ASPECT_BEGIN_RANGE);
495 assert(subresource->aspect <= VK_IMAGE_ASPECT_END_RANGE);
496 assert(subresource->mipLevels > 0);
497 assert(subresource->arraySize > 0);
498 assert(subresource->baseMipLevel < image->levels);
499 assert(subresource->baseMipLevel + subresource->mipLevels <= image->levels);
500 assert(subresource->baseArraySlice < image->array_size);
501 assert(subresource->baseArraySlice + subresource->arraySize <= image->array_size);
502 assert(pView);
503
504 if (view_info->is_cube) {
505 assert(subresource->baseArraySlice % 6 == 0);
506 assert(subresource->arraySize % 6 == 0);
507 }
508
509 /* Validate format. */
510 switch (subresource->aspect) {
511 case VK_IMAGE_ASPECT_COLOR:
512 assert(!image_format_info->depth_format);
513 assert(!image_format_info->has_stencil);
514 assert(!view_format_info->depth_format);
515 assert(!view_format_info->has_stencil);
516 assert(view_format_info->cpp == image_format_info->cpp);
517 break;
518 case VK_IMAGE_ASPECT_DEPTH:
519 assert(image_format_info->depth_format);
520 assert(view_format_info->depth_format);
521 assert(view_format_info->cpp == image_format_info->cpp);
522 break;
523 case VK_IMAGE_ASPECT_STENCIL:
524 /* FINISHME: Is it legal to have an R8 view of S8? */
525 assert(image_format_info->has_stencil);
526 assert(view_format_info->has_stencil);
527 break;
528 default:
529 assert(!"bad VkImageAspect");
530 break;
531 }
532
533 return anv_CreateImageView(_device, pCreateInfo, pView);
534 }
535
536 VkResult
537 anv_CreateImageView(VkDevice _device,
538 const VkImageViewCreateInfo *pCreateInfo,
539 VkImageView *pView)
540 {
541 ANV_FROM_HANDLE(anv_device, device, _device);
542 struct anv_surface_view *view;
543
544 view = anv_device_alloc(device, sizeof(*view), 8,
545 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
546 if (view == NULL)
547 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
548
549 anv_image_view_init(view, device, pCreateInfo, NULL);
550
551 *pView = (VkImageView) view;
552
553 return VK_SUCCESS;
554 }
555
556 VkResult
557 anv_DestroyImageView(VkDevice _device, VkImageView _view)
558 {
559 ANV_FROM_HANDLE(anv_device, device, _device);
560
561 anv_surface_view_destroy(device, (struct anv_surface_view *)_view);
562
563 return VK_SUCCESS;
564 }
565
566 void
567 anv_color_attachment_view_init(struct anv_surface_view *view,
568 struct anv_device *device,
569 const VkColorAttachmentViewCreateInfo* pCreateInfo,
570 struct anv_cmd_buffer *cmd_buffer)
571 {
572 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
573 struct anv_surface *surface = &image->primary_surface;
574 const struct anv_format *format_info =
575 anv_format_for_vk_format(pCreateInfo->format);
576
577 anv_assert(pCreateInfo->arraySize > 0);
578 anv_assert(pCreateInfo->mipLevel < image->levels);
579 anv_assert(pCreateInfo->baseArraySlice + pCreateInfo->arraySize <= image->array_size);
580
581 if (pCreateInfo->msaaResolveImage)
582 anv_finishme("msaaResolveImage");
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 VkResult
663 anv_CreateColorAttachmentView(VkDevice _device,
664 const VkColorAttachmentViewCreateInfo *pCreateInfo,
665 VkColorAttachmentView *pView)
666 {
667 ANV_FROM_HANDLE(anv_device, device, _device);
668 struct anv_surface_view *view;
669
670 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
671
672 view = anv_device_alloc(device, sizeof(*view), 8,
673 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
674 if (view == NULL)
675 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
676
677 anv_color_attachment_view_init(view, device, pCreateInfo, NULL);
678
679 *pView = (VkColorAttachmentView) view;
680
681 return VK_SUCCESS;
682 }
683
684 VkResult
685 anv_DestroyColorAttachmentView(VkDevice _device, VkColorAttachmentView _view)
686 {
687 ANV_FROM_HANDLE(anv_device, device, _device);
688
689 anv_surface_view_destroy(device, (struct anv_surface_view *)_view);
690
691 return VK_SUCCESS;
692 }
693
694 VkResult
695 anv_CreateDepthStencilView(VkDevice _device,
696 const VkDepthStencilViewCreateInfo *pCreateInfo,
697 VkDepthStencilView *pView)
698 {
699 ANV_FROM_HANDLE(anv_device, device, _device);
700 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
701 struct anv_depth_stencil_view *view;
702 struct anv_surface *depth_surface = &image->primary_surface;
703 struct anv_surface *stencil_surface = &image->stencil_surface;
704 const struct anv_format *format =
705 anv_format_for_vk_format(image->format);
706
707 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO);
708
709 view = anv_device_alloc(device, sizeof(*view), 8,
710 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
711 if (view == NULL)
712 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
713
714 /* XXX: We don't handle any of these */
715 anv_assert(pCreateInfo->mipLevel == 0);
716 anv_assert(pCreateInfo->baseArraySlice == 0);
717 anv_assert(pCreateInfo->arraySize == 1);
718
719 view->bo = image->bo;
720
721 view->depth_stride = depth_surface->stride;
722 view->depth_offset = image->offset + depth_surface->offset;
723 view->depth_format = format->depth_format;
724 view->depth_qpitch = 0; /* FINISHME: QPitch */
725
726 view->stencil_stride = stencil_surface->stride;
727 view->stencil_offset = image->offset + stencil_surface->offset;
728 view->stencil_qpitch = 0; /* FINISHME: QPitch */
729
730 *pView = anv_depth_stencil_view_to_handle(view);
731
732 return VK_SUCCESS;
733 }
734
735 VkResult
736 anv_DestroyDepthStencilView(VkDevice _device, VkDepthStencilView _view)
737 {
738 ANV_FROM_HANDLE(anv_device, device, _device);
739 ANV_FROM_HANDLE(anv_depth_stencil_view, view, _view);
740
741 anv_device_free(device, view);
742
743 return VK_SUCCESS;
744 }