vk/pipeline.c: Use the casting functions
[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 anv_GetImageSubresourceLayout(
310 VkDevice device,
311 VkImage image,
312 const VkImageSubresource* pSubresource,
313 VkSubresourceLayout* pLayout)
314 {
315 stub_return(VK_UNSUPPORTED);
316 }
317
318 void
319 anv_surface_view_destroy(struct anv_device *device,
320 struct anv_object *obj, VkObjectType obj_type)
321 {
322 struct anv_surface_view *view = (struct anv_surface_view *)obj;
323
324 assert(obj_type == VK_OBJECT_TYPE_BUFFER_VIEW ||
325 obj_type == VK_OBJECT_TYPE_IMAGE_VIEW ||
326 obj_type == VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW);
327
328 anv_state_pool_free(&device->surface_state_pool, view->surface_state);
329
330 anv_device_free(device, view);
331 }
332
333 void
334 anv_image_view_init(struct anv_surface_view *view,
335 struct anv_device *device,
336 const VkImageViewCreateInfo* pCreateInfo,
337 struct anv_cmd_buffer *cmd_buffer)
338 {
339 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
340 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
341 struct anv_surface *surface;
342
343 const struct anv_format *format_info =
344 anv_format_for_vk_format(pCreateInfo->format);
345
346 const struct anv_image_view_info *view_type_info
347 = &anv_image_view_info_table[pCreateInfo->viewType];
348
349 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D)
350 anv_finishme("non-2D image views");
351
352 switch (pCreateInfo->subresourceRange.aspect) {
353 case VK_IMAGE_ASPECT_STENCIL:
354 anv_finishme("stencil image views");
355 abort();
356 break;
357 case VK_IMAGE_ASPECT_DEPTH:
358 case VK_IMAGE_ASPECT_COLOR:
359 view->offset = image->offset;
360 surface = &image->primary_surface;
361 break;
362 default:
363 unreachable("");
364 break;
365 }
366
367 view->bo = image->bo;
368 view->offset = image->offset + surface->offset;
369 view->format = pCreateInfo->format;
370
371 view->extent = (VkExtent3D) {
372 .width = anv_minify(image->extent.width, range->baseMipLevel),
373 .height = anv_minify(image->extent.height, range->baseMipLevel),
374 .depth = anv_minify(image->extent.depth, range->baseMipLevel),
375 };
376
377 uint32_t depth = 1;
378 if (range->arraySize > 1) {
379 depth = range->arraySize;
380 } else if (image->extent.depth > 1) {
381 depth = image->extent.depth;
382 }
383
384 static const uint32_t vk_to_gen_swizzle[] = {
385 [VK_CHANNEL_SWIZZLE_ZERO] = SCS_ZERO,
386 [VK_CHANNEL_SWIZZLE_ONE] = SCS_ONE,
387 [VK_CHANNEL_SWIZZLE_R] = SCS_RED,
388 [VK_CHANNEL_SWIZZLE_G] = SCS_GREEN,
389 [VK_CHANNEL_SWIZZLE_B] = SCS_BLUE,
390 [VK_CHANNEL_SWIZZLE_A] = SCS_ALPHA
391 };
392
393 struct GEN8_RENDER_SURFACE_STATE surface_state = {
394 .SurfaceType = view_type_info->surface_type,
395 .SurfaceArray = image->array_size > 1,
396 .SurfaceFormat = format_info->surface_format,
397 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
398 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
399 .TileMode = surface->tile_mode,
400 .VerticalLineStride = 0,
401 .VerticalLineStrideOffset = 0,
402 .SamplerL2BypassModeDisable = true,
403 .RenderCacheReadWriteMode = WriteOnlyCache,
404 .MemoryObjectControlState = GEN8_MOCS,
405
406 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
407 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
408 * both Base Mip Level fields nonzero".
409 */
410 .BaseMipLevel = 0.0,
411
412 .SurfaceQPitch = surface->qpitch >> 2,
413 .Height = image->extent.height - 1,
414 .Width = image->extent.width - 1,
415 .Depth = depth - 1,
416 .SurfacePitch = surface->stride - 1,
417 .MinimumArrayElement = range->baseArraySlice,
418 .NumberofMultisamples = MULTISAMPLECOUNT_1,
419 .XOffset = 0,
420 .YOffset = 0,
421
422 /* For sampler surfaces, the hardware interprets field MIPCount/LOD as
423 * MIPCount. The range of levels accessible by the sampler engine is
424 * [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
425 */
426 .MIPCountLOD = range->mipLevels - 1,
427 .SurfaceMinLOD = range->baseMipLevel,
428
429 .AuxiliarySurfaceMode = AUX_NONE,
430 .RedClearColor = 0,
431 .GreenClearColor = 0,
432 .BlueClearColor = 0,
433 .AlphaClearColor = 0,
434 .ShaderChannelSelectRed = vk_to_gen_swizzle[pCreateInfo->channels.r],
435 .ShaderChannelSelectGreen = vk_to_gen_swizzle[pCreateInfo->channels.g],
436 .ShaderChannelSelectBlue = vk_to_gen_swizzle[pCreateInfo->channels.b],
437 .ShaderChannelSelectAlpha = vk_to_gen_swizzle[pCreateInfo->channels.a],
438 .ResourceMinLOD = 0.0,
439 .SurfaceBaseAddress = { NULL, view->offset },
440 };
441
442 if (cmd_buffer)
443 view->surface_state =
444 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
445 else
446 view->surface_state =
447 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
448
449 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
450 }
451
452 VkResult
453 anv_validate_CreateImageView(VkDevice _device,
454 const VkImageViewCreateInfo *pCreateInfo,
455 VkImageView *pView)
456 {
457 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
458 const VkImageSubresourceRange *subresource;
459 const struct anv_image_view_info *view_info;
460 const struct anv_format *view_format_info;
461 const struct anv_format *image_format_info;
462
463 /* Validate structure type before dereferencing it. */
464 assert(pCreateInfo);
465 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
466 subresource = &pCreateInfo->subresourceRange;
467
468 /* Validate viewType is in range before using it. */
469 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
470 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
471 view_info = &anv_image_view_info_table[pCreateInfo->viewType];
472
473 /* Validate format is in range before using it. */
474 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
475 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
476 image_format_info = anv_format_for_vk_format(image->format);
477 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
478
479 /* Validate channel swizzles. */
480 assert(pCreateInfo->channels.r >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
481 assert(pCreateInfo->channels.r <= VK_CHANNEL_SWIZZLE_END_RANGE);
482 assert(pCreateInfo->channels.g >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
483 assert(pCreateInfo->channels.g <= VK_CHANNEL_SWIZZLE_END_RANGE);
484 assert(pCreateInfo->channels.b >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
485 assert(pCreateInfo->channels.b <= VK_CHANNEL_SWIZZLE_END_RANGE);
486 assert(pCreateInfo->channels.a >= VK_CHANNEL_SWIZZLE_BEGIN_RANGE);
487 assert(pCreateInfo->channels.a <= VK_CHANNEL_SWIZZLE_END_RANGE);
488
489 /* Validate subresource. */
490 assert(subresource->aspect >= VK_IMAGE_ASPECT_BEGIN_RANGE);
491 assert(subresource->aspect <= VK_IMAGE_ASPECT_END_RANGE);
492 assert(subresource->mipLevels > 0);
493 assert(subresource->arraySize > 0);
494 assert(subresource->baseMipLevel < image->levels);
495 assert(subresource->baseMipLevel + subresource->mipLevels <= image->levels);
496 assert(subresource->baseArraySlice < image->array_size);
497 assert(subresource->baseArraySlice + subresource->arraySize <= image->array_size);
498 assert(pView);
499
500 if (view_info->is_cube) {
501 assert(subresource->baseArraySlice % 6 == 0);
502 assert(subresource->arraySize % 6 == 0);
503 }
504
505 /* Validate format. */
506 switch (subresource->aspect) {
507 case VK_IMAGE_ASPECT_COLOR:
508 assert(!image_format_info->depth_format);
509 assert(!image_format_info->has_stencil);
510 assert(!view_format_info->depth_format);
511 assert(!view_format_info->has_stencil);
512 assert(view_format_info->cpp == image_format_info->cpp);
513 break;
514 case VK_IMAGE_ASPECT_DEPTH:
515 assert(image_format_info->depth_format);
516 assert(view_format_info->depth_format);
517 assert(view_format_info->cpp == image_format_info->cpp);
518 break;
519 case VK_IMAGE_ASPECT_STENCIL:
520 /* FINISHME: Is it legal to have an R8 view of S8? */
521 assert(image_format_info->has_stencil);
522 assert(view_format_info->has_stencil);
523 break;
524 default:
525 assert(!"bad VkImageAspect");
526 break;
527 }
528
529 return anv_CreateImageView(_device, pCreateInfo, pView);
530 }
531
532 VkResult
533 anv_CreateImageView(VkDevice _device,
534 const VkImageViewCreateInfo *pCreateInfo,
535 VkImageView *pView)
536 {
537 ANV_FROM_HANDLE(anv_device, device, _device);
538 struct anv_surface_view *view;
539
540 view = anv_device_alloc(device, sizeof(*view), 8,
541 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
542 if (view == NULL)
543 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
544
545 anv_image_view_init(view, device, pCreateInfo, NULL);
546
547 view->base.destructor = anv_surface_view_destroy;
548
549 *pView = (VkImageView) view;
550
551 return VK_SUCCESS;
552 }
553
554 void
555 anv_color_attachment_view_init(struct anv_surface_view *view,
556 struct anv_device *device,
557 const VkColorAttachmentViewCreateInfo* pCreateInfo,
558 struct anv_cmd_buffer *cmd_buffer)
559 {
560 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
561 struct anv_surface *surface = &image->primary_surface;
562 const struct anv_format *format_info =
563 anv_format_for_vk_format(pCreateInfo->format);
564
565 anv_assert(pCreateInfo->arraySize > 0);
566 anv_assert(pCreateInfo->mipLevel < image->levels);
567 anv_assert(pCreateInfo->baseArraySlice + pCreateInfo->arraySize <= image->array_size);
568
569 if (pCreateInfo->msaaResolveImage)
570 anv_finishme("msaaResolveImage");
571
572 view->bo = image->bo;
573 view->offset = image->offset + surface->offset;
574 view->format = pCreateInfo->format;
575
576 view->extent = (VkExtent3D) {
577 .width = anv_minify(image->extent.width, pCreateInfo->mipLevel),
578 .height = anv_minify(image->extent.height, pCreateInfo->mipLevel),
579 .depth = anv_minify(image->extent.depth, pCreateInfo->mipLevel),
580 };
581
582 uint32_t depth = 1;
583 if (pCreateInfo->arraySize > 1) {
584 depth = pCreateInfo->arraySize;
585 } else if (image->extent.depth > 1) {
586 depth = image->extent.depth;
587 }
588
589 if (cmd_buffer)
590 view->surface_state =
591 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
592 else
593 view->surface_state =
594 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
595
596 struct GEN8_RENDER_SURFACE_STATE surface_state = {
597 .SurfaceType = SURFTYPE_2D,
598 .SurfaceArray = image->array_size > 1,
599 .SurfaceFormat = format_info->surface_format,
600 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
601 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
602 .TileMode = surface->tile_mode,
603 .VerticalLineStride = 0,
604 .VerticalLineStrideOffset = 0,
605 .SamplerL2BypassModeDisable = true,
606 .RenderCacheReadWriteMode = WriteOnlyCache,
607 .MemoryObjectControlState = GEN8_MOCS,
608
609 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
610 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
611 * both Base Mip Level fields nonzero".
612 */
613 .BaseMipLevel = 0.0,
614
615 .SurfaceQPitch = surface->qpitch >> 2,
616 .Height = image->extent.height - 1,
617 .Width = image->extent.width - 1,
618 .Depth = depth - 1,
619 .SurfacePitch = surface->stride - 1,
620 .MinimumArrayElement = pCreateInfo->baseArraySlice,
621 .NumberofMultisamples = MULTISAMPLECOUNT_1,
622 .XOffset = 0,
623 .YOffset = 0,
624
625 /* For render target surfaces, the hardware interprets field MIPCount/LOD as
626 * LOD. The Broadwell PRM says:
627 *
628 * MIPCountLOD defines the LOD that will be rendered into.
629 * SurfaceMinLOD is ignored.
630 */
631 .SurfaceMinLOD = 0,
632 .MIPCountLOD = pCreateInfo->mipLevel,
633
634 .AuxiliarySurfaceMode = AUX_NONE,
635 .RedClearColor = 0,
636 .GreenClearColor = 0,
637 .BlueClearColor = 0,
638 .AlphaClearColor = 0,
639 .ShaderChannelSelectRed = SCS_RED,
640 .ShaderChannelSelectGreen = SCS_GREEN,
641 .ShaderChannelSelectBlue = SCS_BLUE,
642 .ShaderChannelSelectAlpha = SCS_ALPHA,
643 .ResourceMinLOD = 0.0,
644 .SurfaceBaseAddress = { NULL, view->offset },
645 };
646
647 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
648 }
649
650 VkResult
651 anv_CreateColorAttachmentView(VkDevice _device,
652 const VkColorAttachmentViewCreateInfo *pCreateInfo,
653 VkColorAttachmentView *pView)
654 {
655 ANV_FROM_HANDLE(anv_device, device, _device);
656 struct anv_surface_view *view;
657
658 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
659
660 view = anv_device_alloc(device, sizeof(*view), 8,
661 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
662 if (view == NULL)
663 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
664
665 anv_color_attachment_view_init(view, device, pCreateInfo, NULL);
666
667 view->base.destructor = anv_surface_view_destroy;
668
669 *pView = (VkColorAttachmentView) view;
670
671 return VK_SUCCESS;
672 }
673
674 VkResult
675 anv_CreateDepthStencilView(VkDevice _device,
676 const VkDepthStencilViewCreateInfo *pCreateInfo,
677 VkDepthStencilView *pView)
678 {
679 ANV_FROM_HANDLE(anv_device, device, _device);
680 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
681 struct anv_depth_stencil_view *view;
682 struct anv_surface *depth_surface = &image->primary_surface;
683 struct anv_surface *stencil_surface = &image->stencil_surface;
684 const struct anv_format *format =
685 anv_format_for_vk_format(image->format);
686
687 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO);
688
689 view = anv_device_alloc(device, sizeof(*view), 8,
690 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
691 if (view == NULL)
692 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
693
694 /* XXX: We don't handle any of these */
695 anv_assert(pCreateInfo->mipLevel == 0);
696 anv_assert(pCreateInfo->baseArraySlice == 0);
697 anv_assert(pCreateInfo->arraySize == 1);
698
699 view->bo = image->bo;
700
701 view->depth_stride = depth_surface->stride;
702 view->depth_offset = image->offset + depth_surface->offset;
703 view->depth_format = format->depth_format;
704 view->depth_qpitch = 0; /* FINISHME: QPitch */
705
706 view->stencil_stride = stencil_surface->stride;
707 view->stencil_offset = image->offset + stencil_surface->offset;
708 view->stencil_qpitch = 0; /* FINISHME: QPitch */
709
710 *pView = anv_depth_stencil_view_to_handle(view);
711
712 return VK_SUCCESS;
713 }