blob: 0f81dc17c547042b3e5771ec2241025f08acaa96 [file] [log] [blame]
Chia-I Wu4ea339e2014-08-08 21:56:26 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wu4ea339e2014-08-08 21:56:26 +08003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wu4ea339e2014-08-08 21:56:26 +080026 */
27
Chia-I Wue46da3e2014-08-08 21:52:48 +080028#include "kmd/winsys.h"
29#include "dev.h"
30#include "gpu.h"
Chia-I Wu41858c82015-04-04 16:39:25 +080031#include "wsi.h"
Chia-I Wue46da3e2014-08-08 21:52:48 +080032#include "img.h"
Chia-I Wu4ea339e2014-08-08 21:56:26 +080033
Chia-I Wu4ea339e2014-08-08 21:56:26 +080034/*
35 * From the Ivy Bridge PRM, volume 1 part 1, page 105:
36 *
37 * "In addition to restrictions on maximum height, width, and depth,
38 * surfaces are also restricted to a maximum size in bytes. This
39 * maximum is 2 GB for all products and all surface types."
40 */
Chia-I Wue46da3e2014-08-08 21:52:48 +080041static const size_t intel_max_resource_size = 1u << 31;
Chia-I Wu4ea339e2014-08-08 21:56:26 +080042
Chia-I Wufeb441f2014-08-08 21:27:38 +080043static void img_destroy(struct intel_obj *obj)
44{
45 struct intel_img *img = intel_img_from_obj(obj);
46
47 intel_img_destroy(img);
48}
49
Tony Barbour426b9052015-06-24 16:06:58 -060050static VkResult img_get_memory_requirements(struct intel_base *base, VkMemoryRequirements *pRequirements)
Chia-I Wufeb441f2014-08-08 21:27:38 +080051{
52 struct intel_img *img = intel_img_from_base(base);
Chia-I Wufeb441f2014-08-08 21:27:38 +080053
Tony Barbour426b9052015-06-24 16:06:58 -060054 pRequirements->size = img->total_size;
55 pRequirements->alignment = 4096;
Mark Lobodzinski72346292015-07-02 16:49:40 -060056 pRequirements->memoryTypeBits = (1 << INTEL_MEMORY_TYPE_COUNT) - 1;
Chia-I Wufeb441f2014-08-08 21:27:38 +080057
Tony Barbour426b9052015-06-24 16:06:58 -060058 return VK_SUCCESS;
Chia-I Wufeb441f2014-08-08 21:27:38 +080059}
60
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060061VkResult intel_img_create(struct intel_dev *dev,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060062 const VkImageCreateInfo *info,
63 bool scanout,
64 struct intel_img **img_ret)
Chia-I Wufeb441f2014-08-08 21:27:38 +080065{
Chia-I Wufeb441f2014-08-08 21:27:38 +080066 struct intel_img *img;
Chia-I Wu37cba152014-08-15 16:03:10 +080067 struct intel_layout *layout;
Chia-I Wufeb441f2014-08-08 21:27:38 +080068
Chia-I Wu545c2e12015-02-22 13:19:54 +080069 img = (struct intel_img *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060070 sizeof(*img), dev->base.dbg, VK_OBJECT_TYPE_IMAGE, info, 0);
Chia-I Wufeb441f2014-08-08 21:27:38 +080071 if (!img)
Tony Barbour8205d902015-04-16 15:59:00 -060072 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wufeb441f2014-08-08 21:27:38 +080073
Chia-I Wu37cba152014-08-15 16:03:10 +080074 layout = &img->layout;
75
Chia-I Wueb2da592014-08-16 14:19:39 +080076 img->type = info->imageType;
Chia-I Wu73e326f2014-08-21 11:07:57 +080077 img->depth = info->extent.depth;
Chia-I Wuaa759372014-10-18 12:47:35 +080078 img->mip_levels = info->mipLevels;
Chia-I Wueb2da592014-08-16 14:19:39 +080079 img->array_size = info->arraySize;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -070080 img->usage = info->usage;
Chia-I Wueb2da592014-08-16 14:19:39 +080081 img->samples = info->samples;
Chia-I Wu794d12a2014-09-15 14:55:25 +080082 intel_layout_init(layout, dev, info, scanout);
Chia-I Wu37cba152014-08-15 16:03:10 +080083
84 if (layout->bo_stride > intel_max_resource_size / layout->bo_height) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060085 intel_dev_log(dev, VK_DBG_REPORT_ERROR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060086 VK_NULL_HANDLE, 0, 0, "image too big");
Chia-I Wu37cba152014-08-15 16:03:10 +080087 intel_img_destroy(img);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060088 return VK_ERROR_INVALID_MEMORY_SIZE;
Chia-I Wu37cba152014-08-15 16:03:10 +080089 }
Chia-I Wufeb441f2014-08-08 21:27:38 +080090
Chia-I Wu9b752e12014-08-15 16:21:44 +080091 img->total_size = img->layout.bo_stride * img->layout.bo_height;
92
Chia-I Wu457d0a62014-08-18 13:02:26 +080093 if (layout->aux != INTEL_LAYOUT_AUX_NONE) {
Chia-I Wu9b752e12014-08-15 16:21:44 +080094 img->aux_offset = u_align(img->total_size, 4096);
95 img->total_size = img->aux_offset +
96 layout->aux_stride * layout->aux_height;
97 }
98
99 if (layout->separate_stencil) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600100 VkImageCreateInfo s8_info;
Chia-I Wu9b752e12014-08-15 16:21:44 +0800101
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800102 img->s8_layout = intel_alloc(img, sizeof(*img->s8_layout), 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600103 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800104 if (!img->s8_layout) {
105 intel_img_destroy(img);
Tony Barbour8205d902015-04-16 15:59:00 -0600106 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu9b752e12014-08-15 16:21:44 +0800107 }
108
109 s8_info = *info;
Tony Barbour8205d902015-04-16 15:59:00 -0600110 s8_info.format = VK_FORMAT_S8_UINT;
Chia-I Wud1eb90c2015-03-07 06:01:45 +0800111 /* no stencil texturing */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600112 s8_info.usage &= ~VK_IMAGE_USAGE_SAMPLED_BIT;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700113 assert(icd_format_is_ds(info->format));
Chia-I Wu9b752e12014-08-15 16:21:44 +0800114
Chia-I Wu794d12a2014-09-15 14:55:25 +0800115 intel_layout_init(img->s8_layout, dev, &s8_info, scanout);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800116
117 img->s8_offset = u_align(img->total_size, 4096);
118 img->total_size = img->s8_offset +
119 img->s8_layout->bo_stride * img->s8_layout->bo_height;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800120 }
121
Chia-I Wu41858c82015-04-04 16:39:25 +0800122 if (scanout) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600123 VkResult ret = intel_wsi_img_init(img);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600124 if (ret != VK_SUCCESS) {
Chia-I Wu41858c82015-04-04 16:39:25 +0800125 intel_img_destroy(img);
126 return ret;
127 }
128 }
129
Chia-I Wufeb441f2014-08-08 21:27:38 +0800130 img->obj.destroy = img_destroy;
Tony Barbour426b9052015-06-24 16:06:58 -0600131 img->obj.base.get_memory_requirements = img_get_memory_requirements;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800132
133 *img_ret = img;
134
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600135 return VK_SUCCESS;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800136}
137
138void intel_img_destroy(struct intel_img *img)
139{
Chia-I Wu41858c82015-04-04 16:39:25 +0800140 if (img->wsi_data)
141 intel_wsi_img_cleanup(img);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800142
Chia-I Wu9b752e12014-08-15 16:21:44 +0800143 if (img->s8_layout)
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800144 intel_free(img, img->s8_layout);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800145
Chia-I Wufeb441f2014-08-08 21:27:38 +0800146 intel_base_destroy(&img->obj.base);
147}
148
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600149ICD_EXPORT VkResult VKAPI vkCreateImage(
150 VkDevice device,
151 const VkImageCreateInfo* pCreateInfo,
152 VkImage* pImage)
Chia-I Wufeb441f2014-08-08 21:27:38 +0800153{
154 struct intel_dev *dev = intel_dev(device);
155
Chia-I Wu794d12a2014-09-15 14:55:25 +0800156 return intel_img_create(dev, pCreateInfo, false,
157 (struct intel_img **) pImage);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800158}
159
Tony Barbour426b9052015-06-24 16:06:58 -0600160ICD_EXPORT VkResult VKAPI vkGetImageSubresourceLayout(
Mike Stroyan230e6252015-04-17 12:36:38 -0600161 VkDevice device,
162 VkImage image,
163 const VkImageSubresource* pSubresource,
Tony Barbour426b9052015-06-24 16:06:58 -0600164 VkSubresourceLayout* pLayout)
Chia-I Wufeb441f2014-08-08 21:27:38 +0800165{
166 const struct intel_img *img = intel_img(image);
Tony Barbour426b9052015-06-24 16:06:58 -0600167 unsigned x, y;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800168
Tony Barbour426b9052015-06-24 16:06:58 -0600169 intel_layout_get_slice_pos(&img->layout, pSubresource->mipLevel,
170 pSubresource->arraySlice, &x, &y);
171 intel_layout_pos_to_mem(&img->layout, x, y, &x, &y);
Chia-I Wu2b685d72014-08-14 13:45:37 +0800172
Tony Barbour426b9052015-06-24 16:06:58 -0600173 pLayout->offset = intel_layout_mem_to_linear(&img->layout, x, y);
174 pLayout->size = intel_layout_get_slice_size(&img->layout,
175 pSubresource->mipLevel);
176 pLayout->rowPitch = img->layout.bo_stride;
177 pLayout->depthPitch = intel_layout_get_slice_stride(&img->layout,
178 pSubresource->mipLevel);
179 return VK_SUCCESS;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800180}
Mark Lobodzinski83d4e6a2015-07-03 15:58:09 -0600181
182VkResult VKAPI vkGetImageSparseMemoryRequirements(
183 VkDevice device,
184 VkImage image,
185 uint32_t* pNumRequirements,
186 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
187{
188 *pNumRequirements = 0;
189 return VK_SUCCESS;
190}