blob: cb2faf3cb399e199b8a71c5b2bc01701272787f5 [file] [log] [blame]
Chia-I Wu4ea339e2014-08-08 21:56:26 +08001/*
2 * XGL
3 *
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
50static XGL_RESULT 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);
54 XGL_RESULT ret = XGL_SUCCESS;
55
56 switch (type) {
57 case XGL_INFO_TYPE_MEMORY_REQUIREMENTS:
58 {
59 XGL_MEMORY_REQUIREMENTS *mem_req = data;
60
Jon Ashburn408daec2014-12-05 09:23:52 -070061 *size = sizeof(XGL_MEMORY_REQUIREMENTS);
62 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;
Courtney Goeltzenleuchter4f73aae2015-02-25 13:54:22 -070066 if (img->format_class == XGL_IMAGE_FORMAT_CLASS_LINEAR) {
67 mem_req->memType = XGL_MEMORY_TYPE_BUFFER;
68 } else {
69 mem_req->memType = XGL_MEMORY_TYPE_IMAGE;
70 }
Chia-I Wufeb441f2014-08-08 21:27:38 +080071 }
72 break;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -070073 case XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS:
74 {
75 XGL_IMAGE_MEMORY_REQUIREMENTS *img_req = data;
76
77 *size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
78 if (data == NULL)
79 return ret;
80 img_req->usage = img->usage;
81 img_req->formatClass = img->format_class;
82 img_req->samples = img->samples;
83 }
84 break;
85 case XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS:
86 {
87 XGL_BUFFER_MEMORY_REQUIREMENTS *buf_req = data;
88
Courtney Goeltzenleuchterca83e1d2015-02-18 11:40:27 -070089 *size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -070090 if (data == NULL)
91 return ret;
92 buf_req->usage = img->usage;
93 }
94 break;
Chia-I Wufeb441f2014-08-08 21:27:38 +080095 default:
96 ret = intel_base_get_info(base, type, size, data);
97 break;
98 }
99
100 return ret;
101}
102
103XGL_RESULT intel_img_create(struct intel_dev *dev,
104 const XGL_IMAGE_CREATE_INFO *info,
Chia-I Wu794d12a2014-09-15 14:55:25 +0800105 bool scanout,
Chia-I Wufeb441f2014-08-08 21:27:38 +0800106 struct intel_img **img_ret)
107{
Chia-I Wufeb441f2014-08-08 21:27:38 +0800108 struct intel_img *img;
Chia-I Wu37cba152014-08-15 16:03:10 +0800109 struct intel_layout *layout;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800110
Chia-I Wu545c2e12015-02-22 13:19:54 +0800111 img = (struct intel_img *) intel_base_create(&dev->base.handle,
112 sizeof(*img), dev->base.dbg, XGL_DBG_OBJECT_IMAGE, info, 0);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800113 if (!img)
114 return XGL_ERROR_OUT_OF_MEMORY;
115
Chia-I Wu37cba152014-08-15 16:03:10 +0800116 layout = &img->layout;
117
Chia-I Wueb2da592014-08-16 14:19:39 +0800118 img->type = info->imageType;
Chia-I Wu73e326f2014-08-21 11:07:57 +0800119 img->depth = info->extent.depth;
Chia-I Wuaa759372014-10-18 12:47:35 +0800120 img->mip_levels = info->mipLevels;
Chia-I Wueb2da592014-08-16 14:19:39 +0800121 img->array_size = info->arraySize;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700122 img->usage = info->usage;
Jon Ashburn3cda13b2015-01-20 17:22:07 -0700123 if (info->tiling == XGL_LINEAR_TILING)
124 img->format_class = XGL_IMAGE_FORMAT_CLASS_LINEAR;
125 else
126 img->format_class = icd_format_get_class(info->format);
Chia-I Wueb2da592014-08-16 14:19:39 +0800127 img->samples = info->samples;
Chia-I Wu794d12a2014-09-15 14:55:25 +0800128 intel_layout_init(layout, dev, info, scanout);
Chia-I Wu37cba152014-08-15 16:03:10 +0800129
130 if (layout->bo_stride > intel_max_resource_size / layout->bo_height) {
131 intel_dev_log(dev, XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
132 XGL_NULL_HANDLE, 0, 0, "image too big");
133 intel_img_destroy(img);
134 return XGL_ERROR_INVALID_MEMORY_SIZE;
135 }
Chia-I Wufeb441f2014-08-08 21:27:38 +0800136
Chia-I Wu9b752e12014-08-15 16:21:44 +0800137 img->total_size = img->layout.bo_stride * img->layout.bo_height;
138
Chia-I Wu457d0a62014-08-18 13:02:26 +0800139 if (layout->aux != INTEL_LAYOUT_AUX_NONE) {
Chia-I Wu9b752e12014-08-15 16:21:44 +0800140 img->aux_offset = u_align(img->total_size, 4096);
141 img->total_size = img->aux_offset +
142 layout->aux_stride * layout->aux_height;
143 }
144
145 if (layout->separate_stencil) {
146 XGL_IMAGE_CREATE_INFO s8_info;
147
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800148 img->s8_layout = intel_alloc(img, sizeof(*img->s8_layout), 0,
Chia-I Wu9b752e12014-08-15 16:21:44 +0800149 XGL_SYSTEM_ALLOC_INTERNAL);
150 if (!img->s8_layout) {
151 intel_img_destroy(img);
152 return XGL_ERROR_OUT_OF_MEMORY;
153 }
154
155 s8_info = *info;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700156 s8_info.format = XGL_FMT_S8_UINT;
Chia-I Wud1eb90c2015-03-07 06:01:45 +0800157 /* no stencil texturing */
158 s8_info.usage &= ~XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700159 assert(icd_format_is_ds(info->format));
Chia-I Wu9b752e12014-08-15 16:21:44 +0800160
Chia-I Wu794d12a2014-09-15 14:55:25 +0800161 intel_layout_init(img->s8_layout, dev, &s8_info, scanout);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800162
163 img->s8_offset = u_align(img->total_size, 4096);
164 img->total_size = img->s8_offset +
165 img->s8_layout->bo_stride * img->s8_layout->bo_height;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800166 }
167
Chia-I Wu41858c82015-04-04 16:39:25 +0800168 if (scanout) {
169 XGL_RESULT ret = intel_wsi_img_init(img);
170 if (ret != XGL_SUCCESS) {
171 intel_img_destroy(img);
172 return ret;
173 }
174 }
175
Chia-I Wufeb441f2014-08-08 21:27:38 +0800176 img->obj.destroy = img_destroy;
177 img->obj.base.get_info = img_get_info;
178
179 *img_ret = img;
180
181 return XGL_SUCCESS;
182}
183
184void intel_img_destroy(struct intel_img *img)
185{
Chia-I Wu41858c82015-04-04 16:39:25 +0800186 if (img->wsi_data)
187 intel_wsi_img_cleanup(img);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800188
Chia-I Wu9b752e12014-08-15 16:21:44 +0800189 if (img->s8_layout)
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800190 intel_free(img, img->s8_layout);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800191
Chia-I Wufeb441f2014-08-08 21:27:38 +0800192 intel_base_destroy(&img->obj.base);
193}
194
Chia-I Wu96177272015-01-03 15:27:41 +0800195ICD_EXPORT XGL_RESULT XGLAPI xglOpenPeerImage(
Chia-I Wu251e7d92014-08-19 13:35:42 +0800196 XGL_DEVICE device,
197 const XGL_PEER_IMAGE_OPEN_INFO* pOpenInfo,
198 XGL_IMAGE* pImage,
199 XGL_GPU_MEMORY* pMem)
200{
201 return XGL_ERROR_UNAVAILABLE;
202}
203
Chia-I Wu96177272015-01-03 15:27:41 +0800204ICD_EXPORT XGL_RESULT XGLAPI xglCreateImage(
Chia-I Wufeb441f2014-08-08 21:27:38 +0800205 XGL_DEVICE device,
206 const XGL_IMAGE_CREATE_INFO* pCreateInfo,
207 XGL_IMAGE* pImage)
208{
209 struct intel_dev *dev = intel_dev(device);
210
Chia-I Wu794d12a2014-09-15 14:55:25 +0800211 return intel_img_create(dev, pCreateInfo, false,
212 (struct intel_img **) pImage);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800213}
214
Chia-I Wu96177272015-01-03 15:27:41 +0800215ICD_EXPORT XGL_RESULT XGLAPI xglGetImageSubresourceInfo(
Chia-I Wufeb441f2014-08-08 21:27:38 +0800216 XGL_IMAGE image,
217 const XGL_IMAGE_SUBRESOURCE* pSubresource,
218 XGL_SUBRESOURCE_INFO_TYPE infoType,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600219 size_t* pDataSize,
220 void* pData)
Chia-I Wufeb441f2014-08-08 21:27:38 +0800221{
222 const struct intel_img *img = intel_img(image);
223 XGL_RESULT ret = XGL_SUCCESS;
224
225 switch (infoType) {
226 case XGL_INFO_TYPE_SUBRESOURCE_LAYOUT:
227 {
228 XGL_SUBRESOURCE_LAYOUT *layout = (XGL_SUBRESOURCE_LAYOUT *) pData;
Chia-I Wu2b685d72014-08-14 13:45:37 +0800229 unsigned x, y;
230
231 intel_layout_get_slice_pos(&img->layout, pSubresource->mipLevel,
232 pSubresource->arraySlice, &x, &y);
233 intel_layout_pos_to_mem(&img->layout, x, y, &x, &y);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800234
235 *pDataSize = sizeof(XGL_SUBRESOURCE_LAYOUT);
236
Jon Ashburnbf4182c2014-12-04 15:22:01 -0700237 if (pData == NULL)
238 return ret;
Chia-I Wu457d0a62014-08-18 13:02:26 +0800239 layout->offset = intel_layout_mem_to_linear(&img->layout, x, y);
Chia-I Wu2b685d72014-08-14 13:45:37 +0800240 layout->size = intel_layout_get_slice_size(&img->layout,
241 pSubresource->mipLevel);
242 layout->rowPitch = img->layout.bo_stride;
243 layout->depthPitch = intel_layout_get_slice_stride(&img->layout,
244 pSubresource->mipLevel);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800245 }
246 break;
247 default:
248 ret = XGL_ERROR_INVALID_VALUE;
249 break;
250 }
251
252 return ret;
253}