blob: b9563dfe12e6043174a4352b5a40097dc92b4cd5 [file] [log] [blame]
Chia-I Wu82f50aa2014-08-05 10:43:03 +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 Wu82f50aa2014-08-05 10:43:03 +080026 */
27
Chia-I Wude2bb862014-08-19 14:32:47 +080028#include "dispatch.h"
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -060029#include "dev.h"
Chia-I Wu9e61c0d2014-09-15 15:12:06 +080030#include "gpu.h"
31#include "mem.h"
Chia-I Wu82f50aa2014-08-05 10:43:03 +080032#include "obj.h"
33
34/**
35 * Return true if an (not so) arbitrary pointer casted to intel_base points to
36 * a valid intel_base. This assumes at least the first sizeof(void*) bytes of
37 * the address are accessible, and they does not happen to be our magic
38 * values.
39 */
40bool intel_base_is_valid(const struct intel_base *base)
41{
Chia-I Wu6a42c2a2014-08-19 14:36:47 +080042 if (base->dispatch != intel_dispatch_get(true) &&
43 base->dispatch != intel_dispatch_get(false))
Chia-I Wu82f50aa2014-08-05 10:43:03 +080044 return false;
45
46 return !intel_gpu_is_valid((const struct intel_gpu *) base);
47}
48
Chia-I Wu26f0bd02014-08-07 10:38:40 +080049XGL_RESULT intel_base_get_info(struct intel_base *base, int type,
50 XGL_SIZE *size, XGL_VOID *data)
51{
52 XGL_RESULT ret = XGL_SUCCESS;
53 XGL_SIZE s;
54
55 switch (type) {
56 case XGL_INFO_TYPE_MEMORY_REQUIREMENTS:
57 s = sizeof(XGL_MEMORY_REQUIREMENTS);
58 memset(data, 0, s);
59 *size = s;
60 break;
61 default:
62 ret = XGL_ERROR_INVALID_VALUE;
63 break;
64 }
65
66 return ret;
67}
68
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080069static bool base_dbg_copy_create_info(struct intel_base_dbg *dbg,
70 const void *create_info)
71{
72 const union {
73 const void *ptr;
74 const struct {
75 XGL_STRUCTURE_TYPE struct_type;
76 XGL_VOID *next;
77 } *header;
78 } info = { .ptr = create_info };
79 XGL_SIZE shallow_copy = 0;
80
81 if (!create_info)
82 return true;
83
Chia-I Wub1076d72014-08-18 16:10:20 +080084 switch (dbg->type) {
85 case XGL_DBG_OBJECT_DEVICE:
86 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080087 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080088 case XGL_DBG_OBJECT_GPU_MEMORY:
89 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080090 shallow_copy = sizeof(XGL_MEMORY_ALLOC_INFO);
91 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080092 case XGL_DBG_OBJECT_EVENT:
93 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_EVENT_CREATE_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080094 shallow_copy = sizeof(XGL_EVENT_CREATE_INFO);
95 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080096 case XGL_DBG_OBJECT_FENCE:
97 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080098 shallow_copy = sizeof(XGL_FENCE_CREATE_INFO);
99 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800100 case XGL_DBG_OBJECT_QUERY_POOL:
101 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
Courtney Goeltzenleuchter850d12c2014-08-07 18:13:10 -0600102 shallow_copy = sizeof(XGL_QUERY_POOL_CREATE_INFO);
103 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800104 case XGL_DBG_OBJECT_IMAGE:
105 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800106 shallow_copy = sizeof(XGL_IMAGE_CREATE_INFO);
107 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800108 case XGL_DBG_OBJECT_IMAGE_VIEW:
109 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chia-I Wu5a323262014-08-11 10:31:53 +0800110 shallow_copy = sizeof(XGL_IMAGE_VIEW_CREATE_INFO);
111 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800112 case XGL_DBG_OBJECT_COLOR_TARGET_VIEW:
113 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
Chia-I Wu5a323262014-08-11 10:31:53 +0800114 shallow_copy = sizeof(XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
115 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800116 case XGL_DBG_OBJECT_DEPTH_STENCIL_VIEW:
117 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO);
Chia-I Wu5a323262014-08-11 10:31:53 +0800118 shallow_copy = sizeof(XGL_DEPTH_STENCIL_VIEW_CREATE_INFO);
119 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800120 case XGL_DBG_OBJECT_SAMPLER:
121 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
Chia-I Wu28b89962014-08-18 14:40:49 +0800122 shallow_copy = sizeof(XGL_SAMPLER_CREATE_INFO);
123 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800124 case XGL_DBG_OBJECT_DESCRIPTOR_SET:
125 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO);
Chia-I Wub8d04c82014-08-18 15:51:10 +0800126 shallow_copy = sizeof(XGL_DESCRIPTOR_SET_CREATE_INFO);
127 break;
Chia-I Wua5714e82014-08-11 15:33:42 +0800128 case XGL_DBG_OBJECT_VIEWPORT_STATE:
129 /* no struct header! */
130 shallow_copy = sizeof(XGL_VIEWPORT_STATE_CREATE_INFO);
131 break;
132 case XGL_DBG_OBJECT_RASTER_STATE:
133 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO);
134 shallow_copy = sizeof(XGL_RASTER_STATE_CREATE_INFO);
135 break;
136 case XGL_DBG_OBJECT_MSAA_STATE:
137 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO);
138 shallow_copy = sizeof(XGL_MSAA_STATE_CREATE_INFO);
139 break;
140 case XGL_DBG_OBJECT_COLOR_BLEND_STATE:
141 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO);
142 shallow_copy = sizeof(XGL_COLOR_BLEND_STATE_CREATE_INFO);
143 break;
144 case XGL_DBG_OBJECT_DEPTH_STENCIL_STATE:
145 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO);
146 shallow_copy = sizeof(XGL_DEPTH_STENCIL_STATE_CREATE_INFO);
147 break;
Chia-I Wu730e5362014-08-19 12:15:09 +0800148 case XGL_DBG_OBJECT_CMD_BUFFER:
149 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO);
150 shallow_copy = sizeof(XGL_CMD_BUFFER_CREATE_INFO);
151 break;
Courtney Goeltzenleuchter05a60542014-08-15 14:54:34 -0600152 case XGL_DBG_OBJECT_GRAPHICS_PIPELINE:
153 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
154 break;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600155 case XGL_DBG_OBJECT_SHADER:
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600156 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO ||
157 (dbg->dev->exts[INTEL_EXT_WSI_X11] &&
158 ((XGL_INTEL_STRUCTURE_TYPE)info.header->struct_type == XGL_INTEL_STRUCTURE_TYPE_SHADER_CREATE_INFO)));
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600159 shallow_copy = sizeof(XGL_SHADER_CREATE_INFO);
160 break;
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800161 default:
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600162 // log debug message regarding invalid struct_type?
163 intel_dev_log(dbg->dev, XGL_DBG_MSG_ERROR,
164 XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0, 0,
165 "Invalid Create Info type: 0x%x", info.header->struct_type);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800166 return false;
167 break;
168 }
169
170 if (shallow_copy) {
Chia-I Wu73523682014-10-23 01:38:26 +0800171 /* XGL_VIEWPORT_STATE_CREATE_INFO has no header */
172 if (dbg->type != XGL_DBG_OBJECT_VIEWPORT_STATE)
173 assert(!info.header->next);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800174
175 dbg->create_info = icd_alloc(shallow_copy, 0, XGL_SYSTEM_ALLOC_DEBUG);
176 if (!dbg->create_info)
177 return false;
178
179 memcpy(dbg->create_info, create_info, shallow_copy);
Chia-I Wue2934f92014-08-16 13:17:22 +0800180 dbg->create_info_size = shallow_copy;
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800181 } else if (info.header->struct_type ==
182 XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO) {
183 const XGL_DEVICE_CREATE_INFO *src = info.ptr;
184 XGL_DEVICE_CREATE_INFO *dst;
185 uint8_t *d;
186 XGL_SIZE size;
187 XGL_UINT i;
188
189 size = sizeof(*src);
Chia-I Wue2934f92014-08-16 13:17:22 +0800190 dbg->create_info_size = size;
191
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800192 size += sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
193 size += sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
194 for (i = 0; i < src->extensionCount; i++) {
195 size += 1 +
196 strlen((const char *) src->ppEnabledExtensionNames[i]);
197 }
198
Chia-I Wu98dcfab2014-08-07 12:07:52 +0800199 dst = icd_alloc(size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800200 if (!dst)
201 return false;
202
203 memcpy(dst, src, sizeof(*src));
204
205 d = (uint8_t *) dst;
206 d += sizeof(*src);
207
208 size = sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
209 memcpy(d, src->pRequestedQueues, size);
210 dst->pRequestedQueues = (const XGL_DEVICE_QUEUE_CREATE_INFO *) d;
211 d += size;
212
213 size = sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
214 dst->ppEnabledExtensionNames = (const XGL_CHAR * const *) d;
215
216 for (i = 0; i < src->extensionCount; i++) {
217 const XGL_SIZE len =
218 strlen((const char *) src->ppEnabledExtensionNames[i]);
219
220 memcpy(d + size, src->ppEnabledExtensionNames[i], len + 1);
221 ((const XGL_CHAR **) d)[i] = (const XGL_CHAR *) (d + size);
222
223 size += len + 1;
224 }
Courtney Goeltzenleuchter191b06c2014-10-17 16:21:35 -0600225 dbg->create_info = dst;
Courtney Goeltzenleuchter05a60542014-08-15 14:54:34 -0600226 } else if (info.header->struct_type == XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) {
227 // TODO: What do we want to copy here?
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800228 }
229
230 return true;
231}
232
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800233/**
Chia-I Wubbf2c932014-08-07 12:20:08 +0800234 * Create an intel_base_dbg. When dbg_size is non-zero, a buffer of that
Chia-I Wu660caf82014-08-07 10:54:26 +0800235 * size is allocated and zeroed.
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800236 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600237struct intel_base_dbg *intel_base_dbg_create(struct intel_dev *dev,
238 XGL_DBG_OBJECT_TYPE type,
Chia-I Wu660caf82014-08-07 10:54:26 +0800239 const void *create_info,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800240 XGL_SIZE dbg_size)
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800241{
Chia-I Wu660caf82014-08-07 10:54:26 +0800242 struct intel_base_dbg *dbg;
243
Chia-I Wubbf2c932014-08-07 12:20:08 +0800244 if (!dbg_size)
245 dbg_size = sizeof(*dbg);
Chia-I Wu660caf82014-08-07 10:54:26 +0800246
Chia-I Wubbf2c932014-08-07 12:20:08 +0800247 assert(dbg_size >= sizeof(*dbg));
Chia-I Wu660caf82014-08-07 10:54:26 +0800248
Chia-I Wubbf2c932014-08-07 12:20:08 +0800249 dbg = icd_alloc(dbg_size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu660caf82014-08-07 10:54:26 +0800250 if (!dbg)
251 return NULL;
252
Chia-I Wubbf2c932014-08-07 12:20:08 +0800253 memset(dbg, 0, dbg_size);
Chia-I Wu660caf82014-08-07 10:54:26 +0800254
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800255 dbg->alloc_id = icd_get_allocator_id();
256 dbg->type = type;
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600257 dbg->dev = dev;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800258
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800259 if (!base_dbg_copy_create_info(dbg, create_info)) {
260 icd_free(dbg);
261 return NULL;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800262 }
263
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800264 return dbg;
265}
266
267void intel_base_dbg_destroy(struct intel_base_dbg *dbg)
268{
Chia-I Wu660caf82014-08-07 10:54:26 +0800269 if (dbg->tag)
270 icd_free(dbg->tag);
271
272 if (dbg->create_info)
273 icd_free(dbg->create_info);
274
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800275 icd_free(dbg);
276}
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800277
Chia-I Wubbf2c932014-08-07 12:20:08 +0800278/**
279 * Create an intel_base. obj_size and dbg_size specify the real sizes of the
280 * object and the debug metadata. Memories are zeroed.
281 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600282struct intel_base *intel_base_create(struct intel_dev *dev,
283 XGL_SIZE obj_size, bool debug,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800284 XGL_DBG_OBJECT_TYPE type,
285 const void *create_info,
286 XGL_SIZE dbg_size)
287{
288 struct intel_base *base;
289
290 if (!obj_size)
291 obj_size = sizeof(*base);
292
293 assert(obj_size >= sizeof(*base));
294
295 base = icd_alloc(obj_size, 0, XGL_SYSTEM_ALLOC_API_OBJECT);
296 if (!base)
297 return NULL;
298
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600299 if (dev == NULL) {
300 /*
301 * dev is NULL when we are creating the base device object
302 * Set dev now so that debug setup happens correctly
303 */
304 dev = (struct intel_dev *) base;
305 }
306
Chia-I Wubbf2c932014-08-07 12:20:08 +0800307 memset(base, 0, obj_size);
308
Chia-I Wu6a42c2a2014-08-19 14:36:47 +0800309 base->dispatch = intel_dispatch_get(debug);
Chia-I Wubbf2c932014-08-07 12:20:08 +0800310 if (debug) {
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600311 base->dbg = intel_base_dbg_create(dev, type, create_info, dbg_size);
Chia-I Wubbf2c932014-08-07 12:20:08 +0800312 if (!base->dbg) {
313 icd_free(base);
314 return NULL;
315 }
316 }
Chia-I Wu6a42c2a2014-08-19 14:36:47 +0800317
Chia-I Wubbf2c932014-08-07 12:20:08 +0800318 base->get_info = intel_base_get_info;
319
320 return base;
321}
322
323void intel_base_destroy(struct intel_base *base)
324{
325 if (base->dbg)
326 intel_base_dbg_destroy(base->dbg);
327 icd_free(base);
328}
329
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800330XGL_RESULT XGLAPI intelDestroyObject(
331 XGL_OBJECT object)
332{
333 struct intel_obj *obj = intel_obj(object);
334
335 obj->destroy(obj);
336
337 return XGL_SUCCESS;
338}
339
340XGL_RESULT XGLAPI intelGetObjectInfo(
341 XGL_BASE_OBJECT object,
342 XGL_OBJECT_INFO_TYPE infoType,
343 XGL_SIZE* pDataSize,
344 XGL_VOID* pData)
345{
346 struct intel_base *base = intel_base(object);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800347
Chia-I Wu26f0bd02014-08-07 10:38:40 +0800348 return base->get_info(base, infoType, pDataSize, pData);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800349}
350
351XGL_RESULT XGLAPI intelBindObjectMemory(
352 XGL_OBJECT object,
Chia-I Wu9e61c0d2014-09-15 15:12:06 +0800353 XGL_GPU_MEMORY mem_,
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800354 XGL_GPU_SIZE offset)
355{
356 struct intel_obj *obj = intel_obj(object);
Chia-I Wu9e61c0d2014-09-15 15:12:06 +0800357 struct intel_mem *mem = intel_mem(mem_);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800358
Chia-I Wu9e61c0d2014-09-15 15:12:06 +0800359 intel_obj_bind_mem(obj, mem, offset);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800360
361 return XGL_SUCCESS;
362}
Chia-I Wu7ec9f342014-08-19 10:47:53 +0800363
364XGL_RESULT XGLAPI intelDbgSetObjectTag(
365 XGL_BASE_OBJECT object,
366 XGL_SIZE tagSize,
367 const XGL_VOID* pTag)
368{
369 struct intel_base *base = intel_base(object);
370 struct intel_base_dbg *dbg = base->dbg;
371 void *tag;
372
373 if (!dbg)
374 return XGL_SUCCESS;
375
376 tag = icd_alloc(tagSize, 0, XGL_SYSTEM_ALLOC_DEBUG);
377 if (!tag)
378 return XGL_ERROR_OUT_OF_MEMORY;
379
380 memcpy(tag, pTag, tagSize);
381
382 if (dbg->tag)
383 icd_free(dbg->tag);
384
385 dbg->tag = tag;
386 dbg->tag_size = tagSize;
387
388 return XGL_SUCCESS;
389}