blob: 64d83d07177bb9318f2ba342e5c0625f641779e8 [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);
Chia-I Wu26f0bd02014-08-07 10:38:40 +080058 *size = s;
Jon Ashburn408daec2014-12-05 09:23:52 -070059 if (data == NULL)
60 return ret;
61 memset(data, 0, s);
62
Chia-I Wu26f0bd02014-08-07 10:38:40 +080063 break;
64 default:
65 ret = XGL_ERROR_INVALID_VALUE;
66 break;
67 }
68
69 return ret;
70}
71
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080072static bool base_dbg_copy_create_info(struct intel_base_dbg *dbg,
73 const void *create_info)
74{
75 const union {
76 const void *ptr;
77 const struct {
78 XGL_STRUCTURE_TYPE struct_type;
79 XGL_VOID *next;
80 } *header;
81 } info = { .ptr = create_info };
82 XGL_SIZE shallow_copy = 0;
83
84 if (!create_info)
85 return true;
86
Chia-I Wub1076d72014-08-18 16:10:20 +080087 switch (dbg->type) {
88 case XGL_DBG_OBJECT_DEVICE:
89 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080090 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080091 case XGL_DBG_OBJECT_GPU_MEMORY:
92 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080093 shallow_copy = sizeof(XGL_MEMORY_ALLOC_INFO);
94 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080095 case XGL_DBG_OBJECT_EVENT:
96 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_EVENT_CREATE_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080097 shallow_copy = sizeof(XGL_EVENT_CREATE_INFO);
98 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080099 case XGL_DBG_OBJECT_FENCE:
100 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800101 shallow_copy = sizeof(XGL_FENCE_CREATE_INFO);
102 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800103 case XGL_DBG_OBJECT_QUERY_POOL:
104 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
Courtney Goeltzenleuchter850d12c2014-08-07 18:13:10 -0600105 shallow_copy = sizeof(XGL_QUERY_POOL_CREATE_INFO);
106 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800107 case XGL_DBG_OBJECT_IMAGE:
108 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800109 shallow_copy = sizeof(XGL_IMAGE_CREATE_INFO);
110 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800111 case XGL_DBG_OBJECT_IMAGE_VIEW:
112 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chia-I Wu5a323262014-08-11 10:31:53 +0800113 shallow_copy = sizeof(XGL_IMAGE_VIEW_CREATE_INFO);
114 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800115 case XGL_DBG_OBJECT_COLOR_TARGET_VIEW:
116 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
Chia-I Wu5a323262014-08-11 10:31:53 +0800117 shallow_copy = sizeof(XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
118 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800119 case XGL_DBG_OBJECT_DEPTH_STENCIL_VIEW:
120 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO);
Chia-I Wu5a323262014-08-11 10:31:53 +0800121 shallow_copy = sizeof(XGL_DEPTH_STENCIL_VIEW_CREATE_INFO);
122 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800123 case XGL_DBG_OBJECT_SAMPLER:
124 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
Chia-I Wu28b89962014-08-18 14:40:49 +0800125 shallow_copy = sizeof(XGL_SAMPLER_CREATE_INFO);
126 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800127 case XGL_DBG_OBJECT_DESCRIPTOR_SET:
128 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO);
Chia-I Wub8d04c82014-08-18 15:51:10 +0800129 shallow_copy = sizeof(XGL_DESCRIPTOR_SET_CREATE_INFO);
130 break;
Chia-I Wua5714e82014-08-11 15:33:42 +0800131 case XGL_DBG_OBJECT_VIEWPORT_STATE:
132 /* no struct header! */
133 shallow_copy = sizeof(XGL_VIEWPORT_STATE_CREATE_INFO);
134 break;
135 case XGL_DBG_OBJECT_RASTER_STATE:
136 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO);
137 shallow_copy = sizeof(XGL_RASTER_STATE_CREATE_INFO);
138 break;
139 case XGL_DBG_OBJECT_MSAA_STATE:
140 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO);
141 shallow_copy = sizeof(XGL_MSAA_STATE_CREATE_INFO);
142 break;
143 case XGL_DBG_OBJECT_COLOR_BLEND_STATE:
144 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO);
145 shallow_copy = sizeof(XGL_COLOR_BLEND_STATE_CREATE_INFO);
146 break;
147 case XGL_DBG_OBJECT_DEPTH_STENCIL_STATE:
148 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO);
149 shallow_copy = sizeof(XGL_DEPTH_STENCIL_STATE_CREATE_INFO);
150 break;
Chia-I Wu730e5362014-08-19 12:15:09 +0800151 case XGL_DBG_OBJECT_CMD_BUFFER:
152 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO);
153 shallow_copy = sizeof(XGL_CMD_BUFFER_CREATE_INFO);
154 break;
Courtney Goeltzenleuchter05a60542014-08-15 14:54:34 -0600155 case XGL_DBG_OBJECT_GRAPHICS_PIPELINE:
156 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
157 break;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600158 case XGL_DBG_OBJECT_SHADER:
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -0600159 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600160 shallow_copy = sizeof(XGL_SHADER_CREATE_INFO);
161 break;
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800162 default:
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600163 // log debug message regarding invalid struct_type?
164 intel_dev_log(dbg->dev, XGL_DBG_MSG_ERROR,
165 XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0, 0,
166 "Invalid Create Info type: 0x%x", info.header->struct_type);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800167 return false;
168 break;
169 }
170
171 if (shallow_copy) {
Chia-I Wu73523682014-10-23 01:38:26 +0800172 /* XGL_VIEWPORT_STATE_CREATE_INFO has no header */
173 if (dbg->type != XGL_DBG_OBJECT_VIEWPORT_STATE)
174 assert(!info.header->next);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800175
176 dbg->create_info = icd_alloc(shallow_copy, 0, XGL_SYSTEM_ALLOC_DEBUG);
177 if (!dbg->create_info)
178 return false;
179
180 memcpy(dbg->create_info, create_info, shallow_copy);
Chia-I Wue2934f92014-08-16 13:17:22 +0800181 dbg->create_info_size = shallow_copy;
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800182 } else if (info.header->struct_type ==
183 XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO) {
184 const XGL_DEVICE_CREATE_INFO *src = info.ptr;
185 XGL_DEVICE_CREATE_INFO *dst;
186 uint8_t *d;
187 XGL_SIZE size;
188 XGL_UINT i;
189
190 size = sizeof(*src);
Chia-I Wue2934f92014-08-16 13:17:22 +0800191 dbg->create_info_size = size;
192
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800193 size += sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
194 size += sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
195 for (i = 0; i < src->extensionCount; i++) {
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800196 size += 1 + strlen(src->ppEnabledExtensionNames[i]);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800197 }
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++) {
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800217 const XGL_SIZE len = strlen(src->ppEnabledExtensionNames[i]);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800218
219 memcpy(d + size, src->ppEnabledExtensionNames[i], len + 1);
220 ((const XGL_CHAR **) d)[i] = (const XGL_CHAR *) (d + size);
221
222 size += len + 1;
223 }
Courtney Goeltzenleuchter191b06c2014-10-17 16:21:35 -0600224 dbg->create_info = dst;
Courtney Goeltzenleuchter05a60542014-08-15 14:54:34 -0600225 } else if (info.header->struct_type == XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) {
226 // TODO: What do we want to copy here?
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800227 }
228
229 return true;
230}
231
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800232/**
Chia-I Wubbf2c932014-08-07 12:20:08 +0800233 * Create an intel_base_dbg. When dbg_size is non-zero, a buffer of that
Chia-I Wu660caf82014-08-07 10:54:26 +0800234 * size is allocated and zeroed.
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800235 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600236struct intel_base_dbg *intel_base_dbg_create(struct intel_dev *dev,
237 XGL_DBG_OBJECT_TYPE type,
Chia-I Wu660caf82014-08-07 10:54:26 +0800238 const void *create_info,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800239 XGL_SIZE dbg_size)
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800240{
Chia-I Wu660caf82014-08-07 10:54:26 +0800241 struct intel_base_dbg *dbg;
242
Chia-I Wubbf2c932014-08-07 12:20:08 +0800243 if (!dbg_size)
244 dbg_size = sizeof(*dbg);
Chia-I Wu660caf82014-08-07 10:54:26 +0800245
Chia-I Wubbf2c932014-08-07 12:20:08 +0800246 assert(dbg_size >= sizeof(*dbg));
Chia-I Wu660caf82014-08-07 10:54:26 +0800247
Chia-I Wubbf2c932014-08-07 12:20:08 +0800248 dbg = icd_alloc(dbg_size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu660caf82014-08-07 10:54:26 +0800249 if (!dbg)
250 return NULL;
251
Chia-I Wubbf2c932014-08-07 12:20:08 +0800252 memset(dbg, 0, dbg_size);
Chia-I Wu660caf82014-08-07 10:54:26 +0800253
Chia-I Wu900364b2015-01-03 13:55:22 +0800254 dbg->alloc_id = icd_allocator_get_id();
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800255 dbg->type = type;
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600256 dbg->dev = dev;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800257
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800258 if (!base_dbg_copy_create_info(dbg, create_info)) {
259 icd_free(dbg);
260 return NULL;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800261 }
262
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800263 return dbg;
264}
265
266void intel_base_dbg_destroy(struct intel_base_dbg *dbg)
267{
Chia-I Wu660caf82014-08-07 10:54:26 +0800268 if (dbg->tag)
269 icd_free(dbg->tag);
270
271 if (dbg->create_info)
272 icd_free(dbg->create_info);
273
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800274 icd_free(dbg);
275}
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800276
Chia-I Wubbf2c932014-08-07 12:20:08 +0800277/**
278 * Create an intel_base. obj_size and dbg_size specify the real sizes of the
279 * object and the debug metadata. Memories are zeroed.
280 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600281struct intel_base *intel_base_create(struct intel_dev *dev,
282 XGL_SIZE obj_size, bool debug,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800283 XGL_DBG_OBJECT_TYPE type,
284 const void *create_info,
285 XGL_SIZE dbg_size)
286{
287 struct intel_base *base;
288
289 if (!obj_size)
290 obj_size = sizeof(*base);
291
292 assert(obj_size >= sizeof(*base));
293
294 base = icd_alloc(obj_size, 0, XGL_SYSTEM_ALLOC_API_OBJECT);
295 if (!base)
296 return NULL;
297
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600298 if (dev == NULL) {
299 /*
300 * dev is NULL when we are creating the base device object
301 * Set dev now so that debug setup happens correctly
302 */
303 dev = (struct intel_dev *) base;
304 }
305
Chia-I Wubbf2c932014-08-07 12:20:08 +0800306 memset(base, 0, obj_size);
307
Chia-I Wu6a42c2a2014-08-19 14:36:47 +0800308 base->dispatch = intel_dispatch_get(debug);
Chia-I Wubbf2c932014-08-07 12:20:08 +0800309 if (debug) {
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600310 base->dbg = intel_base_dbg_create(dev, type, create_info, dbg_size);
Chia-I Wubbf2c932014-08-07 12:20:08 +0800311 if (!base->dbg) {
312 icd_free(base);
313 return NULL;
314 }
315 }
Chia-I Wu6a42c2a2014-08-19 14:36:47 +0800316
Chia-I Wubbf2c932014-08-07 12:20:08 +0800317 base->get_info = intel_base_get_info;
318
319 return base;
320}
321
322void intel_base_destroy(struct intel_base *base)
323{
324 if (base->dbg)
325 intel_base_dbg_destroy(base->dbg);
326 icd_free(base);
327}
328
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800329XGL_RESULT XGLAPI intelDestroyObject(
330 XGL_OBJECT object)
331{
332 struct intel_obj *obj = intel_obj(object);
333
334 obj->destroy(obj);
335
336 return XGL_SUCCESS;
337}
338
339XGL_RESULT XGLAPI intelGetObjectInfo(
340 XGL_BASE_OBJECT object,
341 XGL_OBJECT_INFO_TYPE infoType,
342 XGL_SIZE* pDataSize,
343 XGL_VOID* pData)
344{
345 struct intel_base *base = intel_base(object);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800346
Chia-I Wu26f0bd02014-08-07 10:38:40 +0800347 return base->get_info(base, infoType, pDataSize, pData);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800348}
349
350XGL_RESULT XGLAPI intelBindObjectMemory(
351 XGL_OBJECT object,
Chia-I Wu9e61c0d2014-09-15 15:12:06 +0800352 XGL_GPU_MEMORY mem_,
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800353 XGL_GPU_SIZE offset)
354{
355 struct intel_obj *obj = intel_obj(object);
Chia-I Wu9e61c0d2014-09-15 15:12:06 +0800356 struct intel_mem *mem = intel_mem(mem_);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800357
Chia-I Wu9e61c0d2014-09-15 15:12:06 +0800358 intel_obj_bind_mem(obj, mem, offset);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800359
360 return XGL_SUCCESS;
361}
Chia-I Wu7ec9f342014-08-19 10:47:53 +0800362
363XGL_RESULT XGLAPI intelDbgSetObjectTag(
364 XGL_BASE_OBJECT object,
365 XGL_SIZE tagSize,
366 const XGL_VOID* pTag)
367{
368 struct intel_base *base = intel_base(object);
369 struct intel_base_dbg *dbg = base->dbg;
370 void *tag;
371
372 if (!dbg)
373 return XGL_SUCCESS;
374
375 tag = icd_alloc(tagSize, 0, XGL_SYSTEM_ALLOC_DEBUG);
376 if (!tag)
377 return XGL_ERROR_OUT_OF_MEMORY;
378
379 memcpy(tag, pTag, tagSize);
380
381 if (dbg->tag)
382 icd_free(dbg->tag);
383
384 dbg->tag = tag;
385 dbg->tag_size = tagSize;
386
387 return XGL_SUCCESS;
388}