Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 1 | /* |
| 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. |
| 23 | */ |
| 24 | |
| 25 | #include "common.h" |
| 26 | |
| 27 | void app_dev_init_formats(struct app_dev *dev) |
| 28 | { |
| 29 | XGL_CHANNEL_FORMAT ch; |
| 30 | XGL_NUM_FORMAT num; |
| 31 | |
| 32 | for (ch = 0; ch < XGL_MAX_CH_FMT; ch++) { |
| 33 | for (num = 0; num < XGL_MAX_NUM_FMT; num++) { |
| 34 | const XGL_FORMAT fmt = { |
| 35 | .channelFormat = ch, |
| 36 | .numericFormat = num, |
| 37 | }; |
| 38 | XGL_RESULT err; |
Jon Ashburn | d6980d2 | 2014-09-24 14:17:04 -0600 | [diff] [blame] | 39 | XGL_SIZE size = sizeof(dev->format_props[ch][num]); |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 40 | |
| 41 | err = xglGetFormatInfo(dev->obj, fmt, |
| 42 | XGL_INFO_TYPE_FORMAT_PROPERTIES, |
| 43 | &size, &dev->format_props[ch][num]); |
| 44 | if (err) { |
| 45 | memset(&dev->format_props[ch][num], 0, |
| 46 | sizeof(dev->format_props[ch][num])); |
| 47 | } |
| 48 | else if (size != sizeof(dev->format_props[ch][num])) { |
| 49 | ERR_EXIT(XGL_ERROR_UNKNOWN); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void app_dev_init(struct app_dev *dev, struct app_gpu *gpu) |
| 56 | { |
| 57 | XGL_DEVICE_CREATE_INFO info = { |
| 58 | .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 59 | .pNext = NULL, |
| 60 | .queueRecordCount = 0, |
| 61 | .pRequestedQueues = NULL, |
| 62 | .extensionCount = 0, |
| 63 | .ppEnabledExtensionNames = NULL, |
| 64 | .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE, |
| 65 | .flags = XGL_DEVICE_CREATE_VALIDATION_BIT, |
| 66 | }; |
| 67 | XGL_RESULT err; |
| 68 | XGL_SIZE size; |
| 69 | XGL_UINT i; |
| 70 | |
Courtney Goeltzenleuchter | 4eef1ca | 2014-08-06 16:12:02 -0600 | [diff] [blame] | 71 | /* request all queues */ |
| 72 | info.queueRecordCount = gpu->queue_count; |
| 73 | info.pRequestedQueues = gpu->queue_reqs; |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 74 | |
| 75 | /* enable all extensions */ |
| 76 | info.extensionCount = gpu->extension_count; |
| 77 | info.ppEnabledExtensionNames = gpu->extensions; |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 78 | dev->gpu = gpu; |
| 79 | err = xglCreateDevice(gpu->obj, &info, &dev->obj); |
| 80 | if (err) |
| 81 | ERR_EXIT(err); |
| 82 | |
| 83 | err = xglGetMemoryHeapCount(dev->obj, &dev->heap_count); |
| 84 | if (err) |
| 85 | ERR_EXIT(err); |
| 86 | |
| 87 | dev->heap_props = |
| 88 | malloc(sizeof(dev->heap_props[0]) * dev->heap_count); |
| 89 | if (!dev->heap_props) |
| 90 | ERR_EXIT(XGL_ERROR_OUT_OF_MEMORY); |
| 91 | |
| 92 | for (i = 0; i < dev->heap_count; i++) { |
Jon Ashburn | e494a1a | 2014-09-25 14:36:58 -0600 | [diff] [blame] | 93 | size = sizeof(dev->heap_props[0]); |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 94 | err = xglGetMemoryHeapInfo(dev->obj, i, |
| 95 | XGL_INFO_TYPE_MEMORY_HEAP_PROPERTIES, |
| 96 | &size, &dev->heap_props[i]); |
| 97 | if (err || size != sizeof(dev->heap_props[0])) |
| 98 | ERR_EXIT(err); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void app_dev_destroy(struct app_dev *dev) |
| 103 | { |
| 104 | free(dev->heap_props); |
| 105 | xglDestroyDevice(dev->obj); |
| 106 | } |
| 107 | |
| 108 | void app_gpu_init_extensions(struct app_gpu *gpu) |
| 109 | { |
| 110 | XGL_RESULT err; |
| 111 | XGL_UINT i; |
| 112 | |
| 113 | static const XGL_CHAR *known_extensions[] = { |
Courtney Goeltzenleuchter | 8518357 | 2014-10-31 16:16:00 -0600 | [diff] [blame] | 114 | (const XGL_CHAR *) "XGL_WSI_X11", |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | for (i = 0; i < ARRAY_SIZE(known_extensions); i++) { |
| 118 | err = xglGetExtensionSupport(gpu->obj, known_extensions[i]); |
| 119 | if (!err) |
| 120 | gpu->extension_count++; |
| 121 | } |
| 122 | |
| 123 | gpu->extensions = |
| 124 | malloc(sizeof(gpu->extensions[0]) * gpu->extension_count); |
| 125 | if (!gpu->extensions) |
| 126 | ERR_EXIT(XGL_ERROR_OUT_OF_MEMORY); |
| 127 | |
| 128 | gpu->extension_count = 0; |
| 129 | for (i = 0; i < ARRAY_SIZE(known_extensions); i++) { |
| 130 | err = xglGetExtensionSupport(gpu->obj, known_extensions[i]); |
| 131 | if (!err) |
| 132 | gpu->extensions[gpu->extension_count++] = known_extensions[i]; |
| 133 | } |
| 134 | } |
| 135 | |
Courtney Goeltzenleuchter | 91601b8 | 2014-08-07 15:07:34 -0600 | [diff] [blame] | 136 | static const char *xgl_qtype_string(XGL_QUEUE_TYPE qtype) { |
| 137 | switch (qtype) { |
| 138 | #define STR(r) case r: return #r |
| 139 | STR(XGL_QUEUE_TYPE_GRAPHICS); |
| 140 | STR(XGL_QUEUE_TYPE_COMPUTE); |
| 141 | STR(XGL_QUEUE_TYPE_DMA); |
| 142 | default: return "UNKNOWN_RESULT"; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // TODO: May need to extend to more than one queue per type |
| 147 | void app_dev_init_queue(struct app_dev *dev, XGL_QUEUE_TYPE qtype) |
| 148 | { |
| 149 | XGL_RESULT err; |
| 150 | |
| 151 | /* abort before we go past the end of our queue array */ |
| 152 | assert(qtype < MAX_QUEUE_TYPES); |
| 153 | |
| 154 | err = xglGetDeviceQueue(dev->obj, qtype, 0, &dev->queues[qtype]); |
| 155 | if (err) { |
| 156 | printf("xglGetDeviceQueue: %s queue #%d: Failed\n", |
| 157 | xgl_qtype_string(qtype), 0); |
| 158 | } |
| 159 | } |
| 160 | |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 161 | void app_gpu_init(struct app_gpu *gpu, XGL_UINT id, XGL_PHYSICAL_GPU obj) |
| 162 | { |
| 163 | XGL_SIZE size; |
| 164 | XGL_RESULT err; |
| 165 | int i; |
| 166 | |
| 167 | memset(gpu, 0, sizeof(*gpu)); |
| 168 | |
| 169 | gpu->id = id; |
| 170 | gpu->obj = obj; |
Jon Ashburn | 1f29551 | 2014-09-24 13:15:44 -0600 | [diff] [blame] | 171 | size = sizeof(gpu->props); |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 172 | err = xglGetGpuInfo(gpu->obj, |
| 173 | XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, |
| 174 | &size, &gpu->props); |
| 175 | if (err || size != sizeof(gpu->props)) |
| 176 | ERR_EXIT(err); |
| 177 | |
Jon Ashburn | 1f29551 | 2014-09-24 13:15:44 -0600 | [diff] [blame] | 178 | size = sizeof(gpu->perf); |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 179 | err = xglGetGpuInfo(gpu->obj, |
| 180 | XGL_INFO_TYPE_PHYSICAL_GPU_PERFORMANCE, |
| 181 | &size, &gpu->perf); |
| 182 | if (err || size != sizeof(gpu->perf)) |
| 183 | ERR_EXIT(err); |
| 184 | |
| 185 | /* get queue count */ |
| 186 | err = xglGetGpuInfo(gpu->obj, |
| 187 | XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES, |
| 188 | &size, NULL); |
| 189 | if (err || size % sizeof(gpu->queue_props[0])) |
| 190 | ERR_EXIT(err); |
| 191 | gpu->queue_count = size / sizeof(gpu->queue_props[0]); |
| 192 | |
| 193 | gpu->queue_props = |
| 194 | malloc(sizeof(gpu->queue_props[0]) * gpu->queue_count); |
Jon Ashburn | 1f29551 | 2014-09-24 13:15:44 -0600 | [diff] [blame] | 195 | size = sizeof(gpu->queue_props[0]) * gpu->queue_count; |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 196 | if (!gpu->queue_props) |
| 197 | ERR_EXIT(XGL_ERROR_OUT_OF_MEMORY); |
| 198 | err = xglGetGpuInfo(gpu->obj, |
| 199 | XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES, |
| 200 | &size, gpu->queue_props); |
| 201 | if (err || size != sizeof(gpu->queue_props[0]) * gpu->queue_count) |
| 202 | ERR_EXIT(err); |
| 203 | |
Courtney Goeltzenleuchter | 4eef1ca | 2014-08-06 16:12:02 -0600 | [diff] [blame] | 204 | /* set up queue requests */ |
Jon Ashburn | 1f29551 | 2014-09-24 13:15:44 -0600 | [diff] [blame] | 205 | size = sizeof(*gpu->queue_reqs) * gpu->queue_count; |
Courtney Goeltzenleuchter | 4eef1ca | 2014-08-06 16:12:02 -0600 | [diff] [blame] | 206 | gpu->queue_reqs = malloc(sizeof(*gpu->queue_reqs) * gpu->queue_count); |
| 207 | if (!gpu->queue_reqs) |
| 208 | ERR_EXIT(XGL_ERROR_OUT_OF_MEMORY); |
| 209 | for (i = 0; i < gpu->queue_count; i++) { |
| 210 | gpu->queue_reqs[i].queueNodeIndex = i; |
| 211 | gpu->queue_reqs[i].queueCount = gpu->queue_props[i].queueCount; |
| 212 | } |
| 213 | |
Jon Ashburn | e494a1a | 2014-09-25 14:36:58 -0600 | [diff] [blame] | 214 | size = sizeof(gpu->memory_props); |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 215 | err = xglGetGpuInfo(gpu->obj, |
| 216 | XGL_INFO_TYPE_PHYSICAL_GPU_MEMORY_PROPERTIES, |
| 217 | &size, &gpu->memory_props); |
| 218 | if (err || size != sizeof(gpu->memory_props)) |
| 219 | ERR_EXIT(err); |
| 220 | |
| 221 | app_gpu_init_extensions(gpu); |
| 222 | app_dev_init(&gpu->dev, gpu); |
| 223 | app_dev_init_formats(&gpu->dev); |
| 224 | } |
| 225 | |
| 226 | void app_gpu_destroy(struct app_gpu *gpu) |
| 227 | { |
| 228 | app_dev_destroy(&gpu->dev); |
| 229 | free(gpu->extensions); |
Courtney Goeltzenleuchter | 4eef1ca | 2014-08-06 16:12:02 -0600 | [diff] [blame] | 230 | free(gpu->queue_reqs); |
| 231 | free(gpu->queue_props); |
Courtney Goeltzenleuchter | 9fd27a6 | 2014-08-06 16:11:26 -0600 | [diff] [blame] | 232 | } |
| 233 | |