blob: 278f4e1d76c6cde56305b8a941cdf58adbaaeb2c [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 Wu1db76e02014-09-15 14:21:14 +080028#include <unistd.h>
Chia-I Wue46da3e2014-08-08 21:52:48 +080029#include "kmd/winsys.h"
30#include "dev.h"
31#include "gpu.h"
Chia-I Wu1db76e02014-09-15 14:21:14 +080032#include "mem.h"
Chia-I Wue46da3e2014-08-08 21:52:48 +080033#include "img.h"
Chia-I Wu4ea339e2014-08-08 21:56:26 +080034
Chia-I Wu4ea339e2014-08-08 21:56:26 +080035/*
36 * From the Ivy Bridge PRM, volume 1 part 1, page 105:
37 *
38 * "In addition to restrictions on maximum height, width, and depth,
39 * surfaces are also restricted to a maximum size in bytes. This
40 * maximum is 2 GB for all products and all surface types."
41 */
Chia-I Wue46da3e2014-08-08 21:52:48 +080042static const size_t intel_max_resource_size = 1u << 31;
Chia-I Wu4ea339e2014-08-08 21:56:26 +080043
Chia-I Wufeb441f2014-08-08 21:27:38 +080044static void img_destroy(struct intel_obj *obj)
45{
46 struct intel_img *img = intel_img_from_obj(obj);
47
48 intel_img_destroy(img);
49}
50
51static XGL_RESULT img_get_info(struct intel_base *base, int type,
52 XGL_SIZE *size, XGL_VOID *data)
53{
54 struct intel_img *img = intel_img_from_base(base);
55 XGL_RESULT ret = XGL_SUCCESS;
56
57 switch (type) {
58 case XGL_INFO_TYPE_MEMORY_REQUIREMENTS:
59 {
60 XGL_MEMORY_REQUIREMENTS *mem_req = data;
61
Chia-I Wu9b752e12014-08-15 16:21:44 +080062 mem_req->size = img->total_size;
Chia-I Wufeb441f2014-08-08 21:27:38 +080063 mem_req->alignment = 4096;
64 mem_req->heapCount = 1;
65 mem_req->heaps[0] = 0;
66
67 *size = sizeof(*mem_req);
68 }
69 break;
70 default:
71 ret = intel_base_get_info(base, type, size, data);
72 break;
73 }
74
75 return ret;
76}
77
78XGL_RESULT intel_img_create(struct intel_dev *dev,
79 const XGL_IMAGE_CREATE_INFO *info,
Chia-I Wu794d12a2014-09-15 14:55:25 +080080 bool scanout,
Chia-I Wufeb441f2014-08-08 21:27:38 +080081 struct intel_img **img_ret)
82{
Chia-I Wufeb441f2014-08-08 21:27:38 +080083 struct intel_img *img;
Chia-I Wu37cba152014-08-15 16:03:10 +080084 struct intel_layout *layout;
Chia-I Wufeb441f2014-08-08 21:27:38 +080085
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -060086 img = (struct intel_img *) intel_base_create(dev, sizeof(*img),
Chia-I Wufeb441f2014-08-08 21:27:38 +080087 dev->base.dbg, XGL_DBG_OBJECT_IMAGE, info, 0);
88 if (!img)
89 return XGL_ERROR_OUT_OF_MEMORY;
90
Chia-I Wu37cba152014-08-15 16:03:10 +080091 layout = &img->layout;
92
Chia-I Wueb2da592014-08-16 14:19:39 +080093 img->type = info->imageType;
Chia-I Wu73e326f2014-08-21 11:07:57 +080094 img->depth = info->extent.depth;
Chia-I Wueb2da592014-08-16 14:19:39 +080095 img->array_size = info->arraySize;
96 img->samples = info->samples;
Chia-I Wu794d12a2014-09-15 14:55:25 +080097 intel_layout_init(layout, dev, info, scanout);
Chia-I Wu37cba152014-08-15 16:03:10 +080098
99 if (layout->bo_stride > intel_max_resource_size / layout->bo_height) {
100 intel_dev_log(dev, XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
101 XGL_NULL_HANDLE, 0, 0, "image too big");
102 intel_img_destroy(img);
103 return XGL_ERROR_INVALID_MEMORY_SIZE;
104 }
Chia-I Wufeb441f2014-08-08 21:27:38 +0800105
Chia-I Wu9b752e12014-08-15 16:21:44 +0800106 img->total_size = img->layout.bo_stride * img->layout.bo_height;
107
Chia-I Wu457d0a62014-08-18 13:02:26 +0800108 if (layout->aux != INTEL_LAYOUT_AUX_NONE) {
Chia-I Wu9b752e12014-08-15 16:21:44 +0800109 img->aux_offset = u_align(img->total_size, 4096);
110 img->total_size = img->aux_offset +
111 layout->aux_stride * layout->aux_height;
112 }
113
114 if (layout->separate_stencil) {
115 XGL_IMAGE_CREATE_INFO s8_info;
116
117 img->s8_layout = icd_alloc(sizeof(*img->s8_layout), 0,
118 XGL_SYSTEM_ALLOC_INTERNAL);
119 if (!img->s8_layout) {
120 intel_img_destroy(img);
121 return XGL_ERROR_OUT_OF_MEMORY;
122 }
123
124 s8_info = *info;
125 s8_info.format.channelFormat = XGL_CH_FMT_R8;
126 assert(info->format.numericFormat == XGL_NUM_FMT_DS);
127
Chia-I Wu794d12a2014-09-15 14:55:25 +0800128 intel_layout_init(img->s8_layout, dev, &s8_info, scanout);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800129
130 img->s8_offset = u_align(img->total_size, 4096);
131 img->total_size = img->s8_offset +
132 img->s8_layout->bo_stride * img->s8_layout->bo_height;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800133 }
134
Chia-I Wufeb441f2014-08-08 21:27:38 +0800135 img->obj.destroy = img_destroy;
136 img->obj.base.get_info = img_get_info;
137
Chia-I Wu1db76e02014-09-15 14:21:14 +0800138#ifdef ENABLE_WSI_X11
139 img->x11_prime_fd = -1;
140#endif
141
Chia-I Wufeb441f2014-08-08 21:27:38 +0800142 *img_ret = img;
143
144 return XGL_SUCCESS;
145}
146
147void intel_img_destroy(struct intel_img *img)
148{
Chia-I Wu1db76e02014-09-15 14:21:14 +0800149#ifdef ENABLE_WSI_X11
150 if (img->x11_prime_fd >= 0) {
151 close(img->x11_prime_fd);
152 intel_mem_free(img->obj.mem);
153 }
154#endif
155
Chia-I Wu9b752e12014-08-15 16:21:44 +0800156 if (img->s8_layout)
157 icd_free(img->s8_layout);
158
Chia-I Wufeb441f2014-08-08 21:27:38 +0800159 intel_base_destroy(&img->obj.base);
160}
161
Chia-I Wu251e7d92014-08-19 13:35:42 +0800162XGL_RESULT XGLAPI intelOpenPeerImage(
163 XGL_DEVICE device,
164 const XGL_PEER_IMAGE_OPEN_INFO* pOpenInfo,
165 XGL_IMAGE* pImage,
166 XGL_GPU_MEMORY* pMem)
167{
168 return XGL_ERROR_UNAVAILABLE;
169}
170
Chia-I Wufeb441f2014-08-08 21:27:38 +0800171XGL_RESULT XGLAPI intelCreateImage(
172 XGL_DEVICE device,
173 const XGL_IMAGE_CREATE_INFO* pCreateInfo,
174 XGL_IMAGE* pImage)
175{
176 struct intel_dev *dev = intel_dev(device);
177
Chia-I Wu794d12a2014-09-15 14:55:25 +0800178 return intel_img_create(dev, pCreateInfo, false,
179 (struct intel_img **) pImage);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800180}
181
182XGL_RESULT XGLAPI intelGetImageSubresourceInfo(
183 XGL_IMAGE image,
184 const XGL_IMAGE_SUBRESOURCE* pSubresource,
185 XGL_SUBRESOURCE_INFO_TYPE infoType,
186 XGL_SIZE* pDataSize,
187 XGL_VOID* pData)
188{
189 const struct intel_img *img = intel_img(image);
190 XGL_RESULT ret = XGL_SUCCESS;
191
192 switch (infoType) {
193 case XGL_INFO_TYPE_SUBRESOURCE_LAYOUT:
194 {
195 XGL_SUBRESOURCE_LAYOUT *layout = (XGL_SUBRESOURCE_LAYOUT *) pData;
Chia-I Wu2b685d72014-08-14 13:45:37 +0800196 unsigned x, y;
197
198 intel_layout_get_slice_pos(&img->layout, pSubresource->mipLevel,
199 pSubresource->arraySlice, &x, &y);
200 intel_layout_pos_to_mem(&img->layout, x, y, &x, &y);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800201
202 *pDataSize = sizeof(XGL_SUBRESOURCE_LAYOUT);
203
Chia-I Wu457d0a62014-08-18 13:02:26 +0800204 layout->offset = intel_layout_mem_to_linear(&img->layout, x, y);
Chia-I Wu2b685d72014-08-14 13:45:37 +0800205 layout->size = intel_layout_get_slice_size(&img->layout,
206 pSubresource->mipLevel);
207 layout->rowPitch = img->layout.bo_stride;
208 layout->depthPitch = intel_layout_get_slice_stride(&img->layout,
209 pSubresource->mipLevel);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800210 }
211 break;
212 default:
213 ret = XGL_ERROR_INVALID_VALUE;
214 break;
215 }
216
217 return ret;
218}