blob: 80e12b4e91d00af31bc6fa57867548e64e241d76 [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
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060050static VkResult img_get_info(struct intel_base *base, int type,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060051 size_t *size, void *data)
Chia-I Wufeb441f2014-08-08 21:27:38 +080052{
53 struct intel_img *img = intel_img_from_base(base);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060054 VkResult ret = VK_SUCCESS;
Chia-I Wufeb441f2014-08-08 21:27:38 +080055
56 switch (type) {
Tony Barbour8205d902015-04-16 15:59:00 -060057 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS:
Chia-I Wufeb441f2014-08-08 21:27:38 +080058 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060059 VkMemoryRequirements *mem_req = data;
Chia-I Wufeb441f2014-08-08 21:27:38 +080060
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060061 *size = sizeof(VkMemoryRequirements);
Jon Ashburn408daec2014-12-05 09:23:52 -070062 if (data == NULL)
63 return ret;
Chia-I Wu9b752e12014-08-15 16:21:44 +080064 mem_req->size = img->total_size;
Chia-I Wufeb441f2014-08-08 21:27:38 +080065 mem_req->alignment = 4096;
Jeremy Hayesd02809a2015-04-15 14:17:56 -060066 mem_req->memPropsAllowed = INTEL_MEMORY_PROPERTY_ALL;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -070067 }
68 break;
Chia-I Wufeb441f2014-08-08 21:27:38 +080069 default:
70 ret = intel_base_get_info(base, type, size, data);
71 break;
72 }
73
74 return ret;
75}
76
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060077VkResult intel_img_create(struct intel_dev *dev,
78 const VkImageCreateInfo *info,
Chia-I Wu794d12a2014-09-15 14:55:25 +080079 bool scanout,
Chia-I Wufeb441f2014-08-08 21:27:38 +080080 struct intel_img **img_ret)
81{
Chia-I Wufeb441f2014-08-08 21:27:38 +080082 struct intel_img *img;
Chia-I Wu37cba152014-08-15 16:03:10 +080083 struct intel_layout *layout;
Chia-I Wufeb441f2014-08-08 21:27:38 +080084
Chia-I Wu545c2e12015-02-22 13:19:54 +080085 img = (struct intel_img *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060086 sizeof(*img), dev->base.dbg, VK_DBG_OBJECT_IMAGE, info, 0);
Chia-I Wufeb441f2014-08-08 21:27:38 +080087 if (!img)
Tony Barbour8205d902015-04-16 15:59:00 -060088 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wufeb441f2014-08-08 21:27:38 +080089
Chia-I Wu37cba152014-08-15 16:03:10 +080090 layout = &img->layout;
91
Chia-I Wueb2da592014-08-16 14:19:39 +080092 img->type = info->imageType;
Chia-I Wu73e326f2014-08-21 11:07:57 +080093 img->depth = info->extent.depth;
Chia-I Wuaa759372014-10-18 12:47:35 +080094 img->mip_levels = info->mipLevels;
Chia-I Wueb2da592014-08-16 14:19:39 +080095 img->array_size = info->arraySize;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -070096 img->usage = info->usage;
Chia-I Wueb2da592014-08-16 14:19:39 +080097 img->samples = info->samples;
Chia-I Wu794d12a2014-09-15 14:55:25 +080098 intel_layout_init(layout, dev, info, scanout);
Chia-I Wu37cba152014-08-15 16:03:10 +080099
100 if (layout->bo_stride > intel_max_resource_size / layout->bo_height) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600101 intel_dev_log(dev, VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0,
102 VK_NULL_HANDLE, 0, 0, "image too big");
Chia-I Wu37cba152014-08-15 16:03:10 +0800103 intel_img_destroy(img);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600104 return VK_ERROR_INVALID_MEMORY_SIZE;
Chia-I Wu37cba152014-08-15 16:03:10 +0800105 }
Chia-I Wufeb441f2014-08-08 21:27:38 +0800106
Chia-I Wu9b752e12014-08-15 16:21:44 +0800107 img->total_size = img->layout.bo_stride * img->layout.bo_height;
108
Chia-I Wu457d0a62014-08-18 13:02:26 +0800109 if (layout->aux != INTEL_LAYOUT_AUX_NONE) {
Chia-I Wu9b752e12014-08-15 16:21:44 +0800110 img->aux_offset = u_align(img->total_size, 4096);
111 img->total_size = img->aux_offset +
112 layout->aux_stride * layout->aux_height;
113 }
114
115 if (layout->separate_stencil) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600116 VkImageCreateInfo s8_info;
Chia-I Wu9b752e12014-08-15 16:21:44 +0800117
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800118 img->s8_layout = intel_alloc(img, sizeof(*img->s8_layout), 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600119 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800120 if (!img->s8_layout) {
121 intel_img_destroy(img);
Tony Barbour8205d902015-04-16 15:59:00 -0600122 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu9b752e12014-08-15 16:21:44 +0800123 }
124
125 s8_info = *info;
Tony Barbour8205d902015-04-16 15:59:00 -0600126 s8_info.format = VK_FORMAT_S8_UINT;
Chia-I Wud1eb90c2015-03-07 06:01:45 +0800127 /* no stencil texturing */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600128 s8_info.usage &= ~VK_IMAGE_USAGE_SAMPLED_BIT;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700129 assert(icd_format_is_ds(info->format));
Chia-I Wu9b752e12014-08-15 16:21:44 +0800130
Chia-I Wu794d12a2014-09-15 14:55:25 +0800131 intel_layout_init(img->s8_layout, dev, &s8_info, scanout);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800132
133 img->s8_offset = u_align(img->total_size, 4096);
134 img->total_size = img->s8_offset +
135 img->s8_layout->bo_stride * img->s8_layout->bo_height;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800136 }
137
Chia-I Wu41858c82015-04-04 16:39:25 +0800138 if (scanout) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600139 VkResult ret = intel_wsi_img_init(img);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600140 if (ret != VK_SUCCESS) {
Chia-I Wu41858c82015-04-04 16:39:25 +0800141 intel_img_destroy(img);
142 return ret;
143 }
144 }
145
Chia-I Wufeb441f2014-08-08 21:27:38 +0800146 img->obj.destroy = img_destroy;
147 img->obj.base.get_info = img_get_info;
148
149 *img_ret = img;
150
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600151 return VK_SUCCESS;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800152}
153
154void intel_img_destroy(struct intel_img *img)
155{
Chia-I Wu41858c82015-04-04 16:39:25 +0800156 if (img->wsi_data)
157 intel_wsi_img_cleanup(img);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800158
Chia-I Wu9b752e12014-08-15 16:21:44 +0800159 if (img->s8_layout)
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800160 intel_free(img, img->s8_layout);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800161
Chia-I Wufeb441f2014-08-08 21:27:38 +0800162 intel_base_destroy(&img->obj.base);
163}
164
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600165ICD_EXPORT VkResult VKAPI vkOpenPeerImage(
166 VkDevice device,
167 const VkPeerImageOpenInfo* pOpenInfo,
168 VkImage* pImage,
Tony Barbour8205d902015-04-16 15:59:00 -0600169 VkDeviceMemory* pMem)
Chia-I Wu251e7d92014-08-19 13:35:42 +0800170{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600171 return VK_ERROR_UNAVAILABLE;
Chia-I Wu251e7d92014-08-19 13:35:42 +0800172}
173
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600174ICD_EXPORT VkResult VKAPI vkCreateImage(
175 VkDevice device,
176 const VkImageCreateInfo* pCreateInfo,
177 VkImage* pImage)
Chia-I Wufeb441f2014-08-08 21:27:38 +0800178{
179 struct intel_dev *dev = intel_dev(device);
180
Chia-I Wu794d12a2014-09-15 14:55:25 +0800181 return intel_img_create(dev, pCreateInfo, false,
182 (struct intel_img **) pImage);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800183}
184
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600185ICD_EXPORT VkResult VKAPI vkGetImageSubresourceInfo(
186 VkImage image,
187 const VkImageSubresource* pSubresource,
188 VkSubresourceInfoType infoType,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600189 size_t* pDataSize,
190 void* pData)
Chia-I Wufeb441f2014-08-08 21:27:38 +0800191{
192 const struct intel_img *img = intel_img(image);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600193 VkResult ret = VK_SUCCESS;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800194
195 switch (infoType) {
Tony Barbour8205d902015-04-16 15:59:00 -0600196 case VK_SUBRESOURCE_INFO_TYPE_LAYOUT:
Chia-I Wufeb441f2014-08-08 21:27:38 +0800197 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600198 VkSubresourceLayout *layout = (VkSubresourceLayout *) pData;
Chia-I Wu2b685d72014-08-14 13:45:37 +0800199 unsigned x, y;
200
201 intel_layout_get_slice_pos(&img->layout, pSubresource->mipLevel,
202 pSubresource->arraySlice, &x, &y);
203 intel_layout_pos_to_mem(&img->layout, x, y, &x, &y);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800204
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600205 *pDataSize = sizeof(VkSubresourceLayout);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800206
Jon Ashburnbf4182c2014-12-04 15:22:01 -0700207 if (pData == NULL)
208 return ret;
Chia-I Wu457d0a62014-08-18 13:02:26 +0800209 layout->offset = intel_layout_mem_to_linear(&img->layout, x, y);
Chia-I Wu2b685d72014-08-14 13:45:37 +0800210 layout->size = intel_layout_get_slice_size(&img->layout,
211 pSubresource->mipLevel);
212 layout->rowPitch = img->layout.bo_stride;
213 layout->depthPitch = intel_layout_get_slice_stride(&img->layout,
214 pSubresource->mipLevel);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800215 }
216 break;
217 default:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600218 ret = VK_ERROR_INVALID_VALUE;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800219 break;
220 }
221
222 return ret;
223}