Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame^] | 1 | // XGL tests |
| 2 | // |
| 3 | // Copyright (C) 2014 LunarG, Inc. |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | // copy of this software and associated documentation files (the "Software"), |
| 7 | // to deal in the Software without restriction, including without limitation |
| 8 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | // and/or sell copies of the Software, and to permit persons to whom the |
| 10 | // Software is furnished to do so, subject to the following conditions: |
| 11 | // |
| 12 | // The above copyright notice and this permission notice shall be included |
| 13 | // in all copies or substantial portions of the Software. |
| 14 | // |
| 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | // DEALINGS IN THE SOFTWARE. |
| 22 | |
| 23 | #include <iostream> |
| 24 | #include <string.h> // memset(), memcmp() |
| 25 | #include "xgltestbinding.h" |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | #define DERIVED_OBJECT_INIT(create_func, ...) \ |
| 30 | do { \ |
| 31 | obj_type obj; \ |
| 32 | if (EXPECT(create_func(__VA_ARGS__, &obj) == XGL_SUCCESS)) \ |
| 33 | base_type::init(obj); \ |
| 34 | } while (0) |
| 35 | |
| 36 | #define STRINGIFY(x) #x |
| 37 | #define EXPECT(expr) ((expr) ? true : expect_failure(STRINGIFY(expr), __FILE__, __LINE__, __FUNCTION__)) |
| 38 | |
| 39 | xgl_testing::ErrorCallback error_callback; |
| 40 | |
| 41 | bool expect_failure(const char *expr, const char *file, unsigned int line, const char *function) |
| 42 | { |
| 43 | if (error_callback) { |
| 44 | error_callback(expr, file, line, function); |
| 45 | } else { |
| 46 | std::cerr << file << ":" << line << ": " << function << |
| 47 | ": Expectation `" << expr << "' failed.\n"; |
| 48 | } |
| 49 | |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | template<class T, class S> |
| 54 | std::vector<T> make_objects(const std::vector<S> &v) |
| 55 | { |
| 56 | std::vector<T> objs; |
| 57 | objs.reserve(v.size()); |
| 58 | for (typename std::vector<S>::const_iterator it = v.begin(); it != v.end(); it++) |
| 59 | objs.push_back((*it)->obj()); |
| 60 | return objs; |
| 61 | } |
| 62 | |
| 63 | template<typename T> |
| 64 | std::vector<T> get_info(XGL_PHYSICAL_GPU gpu, XGL_PHYSICAL_GPU_INFO_TYPE type, size_t min_elems) |
| 65 | { |
| 66 | std::vector<T> info; |
| 67 | size_t size; |
| 68 | if (EXPECT(xglGetGpuInfo(gpu, type, &size, NULL) == XGL_SUCCESS && size % sizeof(T) == 0)) { |
| 69 | info.resize(size / sizeof(T)); |
| 70 | if (!EXPECT(xglGetGpuInfo(gpu, type, &size, &info[0]) == XGL_SUCCESS && size == info.size() * sizeof(T))) |
| 71 | info.clear(); |
| 72 | } |
| 73 | |
| 74 | if (info.size() < min_elems) |
| 75 | info.resize(min_elems); |
| 76 | |
| 77 | return info; |
| 78 | } |
| 79 | |
| 80 | template<typename T> |
| 81 | std::vector<T> get_info(XGL_BASE_OBJECT obj, XGL_OBJECT_INFO_TYPE type, size_t min_elems) |
| 82 | { |
| 83 | std::vector<T> info; |
| 84 | size_t size; |
| 85 | if (EXPECT(xglGetObjectInfo(obj, type, &size, NULL) == XGL_SUCCESS && size % sizeof(T) == 0)) { |
| 86 | info.resize(size / sizeof(T)); |
| 87 | if (!EXPECT(xglGetObjectInfo(obj, type, &size, &info[0]) == XGL_SUCCESS && size == info.size() * sizeof(T))) |
| 88 | info.clear(); |
| 89 | } |
| 90 | |
| 91 | if (info.size() < min_elems) |
| 92 | info.resize(min_elems); |
| 93 | |
| 94 | return info; |
| 95 | } |
| 96 | |
| 97 | } // namespace |
| 98 | |
| 99 | namespace xgl_testing { |
| 100 | |
| 101 | void set_error_callback(ErrorCallback callback) |
| 102 | { |
| 103 | error_callback = callback; |
| 104 | } |
| 105 | |
| 106 | XGL_PHYSICAL_GPU_PROPERTIES PhysicalGpu::properties() const |
| 107 | { |
| 108 | return get_info<XGL_PHYSICAL_GPU_PROPERTIES>(gpu_, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, 1)[0]; |
| 109 | } |
| 110 | |
| 111 | XGL_PHYSICAL_GPU_PERFORMANCE PhysicalGpu::performance() const |
| 112 | { |
| 113 | return get_info<XGL_PHYSICAL_GPU_PERFORMANCE>(gpu_, XGL_INFO_TYPE_PHYSICAL_GPU_PERFORMANCE, 1)[0]; |
| 114 | } |
| 115 | |
| 116 | std::vector<XGL_PHYSICAL_GPU_QUEUE_PROPERTIES> PhysicalGpu::queue_properties() const |
| 117 | { |
| 118 | return get_info<XGL_PHYSICAL_GPU_QUEUE_PROPERTIES>(gpu_, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES, 0); |
| 119 | } |
| 120 | |
| 121 | XGL_PHYSICAL_GPU_MEMORY_PROPERTIES PhysicalGpu::memory_properties() const |
| 122 | { |
| 123 | return get_info<XGL_PHYSICAL_GPU_MEMORY_PROPERTIES>(gpu_, XGL_INFO_TYPE_PHYSICAL_GPU_MEMORY_PROPERTIES, 1)[0]; |
| 124 | } |
| 125 | |
| 126 | std::vector<const char *> PhysicalGpu::layers(std::vector<char> &buf) const |
| 127 | { |
| 128 | const size_t max_layer_count = 16; |
| 129 | const size_t max_string_size = 256; |
| 130 | |
| 131 | buf.resize(max_layer_count * max_string_size); |
| 132 | |
| 133 | std::vector<const char *> layers; |
| 134 | layers.reserve(max_layer_count); |
| 135 | for (size_t i = 0; i < max_layer_count; i++) |
| 136 | layers.push_back(&buf[0] + max_string_size * i); |
| 137 | |
| 138 | char * const *out = const_cast<char * const *>(&layers[0]); |
| 139 | size_t count; |
| 140 | if (!EXPECT(xglEnumerateLayers(gpu_, max_layer_count, max_string_size, out, &count, NULL) == XGL_SUCCESS)) |
| 141 | count = 0; |
| 142 | layers.resize(count); |
| 143 | |
| 144 | return layers; |
| 145 | } |
| 146 | |
| 147 | std::vector<const char *> PhysicalGpu::extensions() const |
| 148 | { |
| 149 | static const char *known_exts[] = { |
| 150 | "XGL_WSI_X11", |
| 151 | }; |
| 152 | |
| 153 | std::vector<const char *> exts; |
| 154 | for (int i = 0; i < sizeof(known_exts) / sizeof(known_exts[0]); i++) { |
| 155 | XGL_RESULT err = xglGetExtensionSupport(gpu_, known_exts[i]); |
| 156 | if (err == XGL_SUCCESS) |
| 157 | exts.push_back(known_exts[i]); |
| 158 | } |
| 159 | |
| 160 | return exts; |
| 161 | } |
| 162 | |
| 163 | XGL_GPU_COMPATIBILITY_INFO PhysicalGpu::compatibility(const PhysicalGpu &other) const |
| 164 | { |
| 165 | XGL_GPU_COMPATIBILITY_INFO data; |
| 166 | if (!EXPECT(xglGetMultiGpuCompatibility(gpu_, other.gpu_, &data) == XGL_SUCCESS)) |
| 167 | memset(&data, 0, sizeof(data)); |
| 168 | |
| 169 | return data; |
| 170 | } |
| 171 | |
| 172 | void BaseObject::init(XGL_BASE_OBJECT obj, bool own) |
| 173 | { |
| 174 | EXPECT(!initialized()); |
| 175 | reinit(obj, own); |
| 176 | } |
| 177 | |
| 178 | void BaseObject::reinit(XGL_BASE_OBJECT obj, bool own) |
| 179 | { |
| 180 | obj_ = obj; |
| 181 | own_obj_ = own; |
| 182 | } |
| 183 | |
| 184 | uint32_t BaseObject::memory_allocation_count() const |
| 185 | { |
| 186 | return memory_requirements().size(); |
| 187 | } |
| 188 | |
| 189 | std::vector<XGL_MEMORY_REQUIREMENTS> BaseObject::memory_requirements() const |
| 190 | { |
| 191 | std::vector<XGL_MEMORY_REQUIREMENTS> info = |
| 192 | get_info<XGL_MEMORY_REQUIREMENTS>(obj_, XGL_INFO_TYPE_MEMORY_REQUIREMENTS, 0); |
| 193 | if (info.size() == 1 && !info[0].size) |
| 194 | info.clear(); |
| 195 | |
| 196 | return info; |
| 197 | } |
| 198 | |
| 199 | void Object::init(XGL_OBJECT obj, bool own) |
| 200 | { |
| 201 | BaseObject::init(obj, own); |
| 202 | mem_alloc_count_ = memory_allocation_count(); |
| 203 | } |
| 204 | |
| 205 | void Object::reinit(XGL_OBJECT obj, bool own) |
| 206 | { |
| 207 | cleanup(); |
| 208 | BaseObject::reinit(obj, own); |
| 209 | mem_alloc_count_ = memory_allocation_count(); |
| 210 | } |
| 211 | |
| 212 | void Object::cleanup() |
| 213 | { |
| 214 | if (!initialized()) |
| 215 | return; |
| 216 | |
| 217 | unbind_memory(); |
| 218 | |
| 219 | if (internal_mems_) { |
| 220 | delete[] internal_mems_; |
| 221 | internal_mems_ = NULL; |
| 222 | primary_mem_ = NULL; |
| 223 | } |
| 224 | |
| 225 | mem_alloc_count_ = 0; |
| 226 | |
| 227 | if (own()) |
| 228 | EXPECT(xglDestroyObject(obj()) == XGL_SUCCESS); |
| 229 | } |
| 230 | |
| 231 | void Object::bind_memory(uint32_t alloc_idx, const GpuMemory &mem, XGL_GPU_SIZE mem_offset) |
| 232 | { |
| 233 | EXPECT(!alloc_idx && xglBindObjectMemory(obj(), mem.obj(), mem_offset) == XGL_SUCCESS); |
| 234 | } |
| 235 | |
| 236 | void Object::unbind_memory(uint32_t alloc_idx) |
| 237 | { |
| 238 | EXPECT(!alloc_idx && xglBindObjectMemory(obj(), XGL_NULL_HANDLE, 0) == XGL_SUCCESS); |
| 239 | } |
| 240 | |
| 241 | void Object::unbind_memory() |
| 242 | { |
| 243 | for (uint32_t i = 0; i < mem_alloc_count_; i++) |
| 244 | unbind_memory(i); |
| 245 | } |
| 246 | |
| 247 | void Object::alloc_memory(const Device &dev, bool for_linear_img) |
| 248 | { |
| 249 | if (!EXPECT(!internal_mems_) || !mem_alloc_count_) |
| 250 | return; |
| 251 | |
| 252 | internal_mems_ = new GpuMemory[mem_alloc_count_]; |
| 253 | |
| 254 | const std::vector<XGL_MEMORY_REQUIREMENTS> mem_reqs = memory_requirements(); |
| 255 | for (int i = 0; i < mem_reqs.size(); i++) { |
| 256 | XGL_MEMORY_ALLOC_INFO info = GpuMemory::alloc_info(mem_reqs[i]); |
| 257 | |
| 258 | primary_mem_ = &internal_mems_[i]; |
| 259 | |
| 260 | internal_mems_[i].init(dev, info); |
| 261 | bind_memory(i, internal_mems_[i], 0); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void Object::alloc_memory(const std::vector<XGL_GPU_MEMORY> &mems) |
| 266 | { |
| 267 | if (!EXPECT(!internal_mems_) || !mem_alloc_count_) |
| 268 | return; |
| 269 | |
| 270 | internal_mems_ = new GpuMemory[mem_alloc_count_]; |
| 271 | |
| 272 | const std::vector<XGL_MEMORY_REQUIREMENTS> mem_reqs = memory_requirements(); |
| 273 | if (!EXPECT(mem_reqs.size() == mems.size())) |
| 274 | return; |
| 275 | |
| 276 | for (int i = 0; i < mem_reqs.size(); i++) { |
| 277 | primary_mem_ = &internal_mems_[i]; |
| 278 | |
| 279 | internal_mems_[i].init(mems[i]); |
| 280 | bind_memory(i, internal_mems_[i], 0); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | std::vector<XGL_GPU_MEMORY> Object::memories() const |
| 285 | { |
| 286 | std::vector<XGL_GPU_MEMORY> mems; |
| 287 | if (internal_mems_) { |
| 288 | mems.reserve(mem_alloc_count_); |
| 289 | for (uint32_t i = 0; i < mem_alloc_count_; i++) |
| 290 | mems.push_back(internal_mems_[i].obj()); |
| 291 | } |
| 292 | |
| 293 | return mems; |
| 294 | } |
| 295 | |
| 296 | Device::~Device() |
| 297 | { |
| 298 | if (!initialized()) |
| 299 | return; |
| 300 | |
| 301 | for (int i = 0; i < QUEUE_COUNT; i++) { |
| 302 | for (std::vector<Queue *>::iterator it = queues_[i].begin(); it != queues_[i].end(); it++) |
| 303 | delete *it; |
| 304 | queues_[i].clear(); |
| 305 | } |
| 306 | |
| 307 | EXPECT(xglDestroyDevice(obj()) == XGL_SUCCESS); |
| 308 | } |
| 309 | |
| 310 | void Device::init() |
| 311 | { |
| 312 | // request all queues |
| 313 | const std::vector<XGL_PHYSICAL_GPU_QUEUE_PROPERTIES> queue_props = gpu_.queue_properties(); |
| 314 | std::vector<XGL_DEVICE_QUEUE_CREATE_INFO> queue_info; |
| 315 | queue_info.reserve(queue_props.size()); |
| 316 | for (int i = 0; i < queue_props.size(); i++) { |
| 317 | XGL_DEVICE_QUEUE_CREATE_INFO qi = {}; |
| 318 | qi.queueNodeIndex = i; |
| 319 | qi.queueCount = queue_props[i].queueCount; |
| 320 | queue_info.push_back(qi); |
| 321 | } |
| 322 | |
| 323 | // request all layers |
| 324 | std::vector<char> layer_buf; |
| 325 | const std::vector<const char *> layers = gpu_.layers(layer_buf); |
| 326 | XGL_LAYER_CREATE_INFO layer_info = {}; |
| 327 | layer_info.sType = XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO; |
| 328 | layer_info.layerCount = layers.size(); |
| 329 | layer_info.ppActiveLayerNames = &layers[0]; |
| 330 | |
| 331 | const std::vector<const char *> exts = gpu_.extensions(); |
| 332 | |
| 333 | XGL_DEVICE_CREATE_INFO dev_info = {}; |
| 334 | dev_info.sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
| 335 | dev_info.pNext = static_cast<void *>(&layer_info); |
| 336 | dev_info.queueRecordCount = queue_info.size(); |
| 337 | dev_info.pRequestedQueues = &queue_info[0]; |
| 338 | dev_info.extensionCount = exts.size(); |
| 339 | dev_info.ppEnabledExtensionNames = &exts[0]; |
| 340 | dev_info.maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE; |
| 341 | dev_info.flags = XGL_DEVICE_CREATE_VALIDATION_BIT; |
| 342 | |
| 343 | init(dev_info); |
| 344 | } |
| 345 | |
| 346 | void Device::init(const XGL_DEVICE_CREATE_INFO &info) |
| 347 | { |
| 348 | DERIVED_OBJECT_INIT(xglCreateDevice, gpu_.obj(), &info); |
| 349 | |
| 350 | init_queues(); |
| 351 | init_heap_props(); |
| 352 | init_formats(); |
| 353 | } |
| 354 | |
| 355 | void Device::init_queues() |
| 356 | { |
| 357 | const struct { |
| 358 | QueueIndex index; |
| 359 | XGL_QUEUE_TYPE type; |
| 360 | } queue_mapping[] = { |
| 361 | { GRAPHICS, XGL_QUEUE_TYPE_GRAPHICS }, |
| 362 | { COMPUTE, XGL_QUEUE_TYPE_COMPUTE }, |
| 363 | { DMA, XGL_QUEUE_TYPE_DMA }, |
| 364 | }; |
| 365 | |
| 366 | for (int i = 0; i < QUEUE_COUNT; i++) { |
| 367 | uint32_t idx = 0; |
| 368 | |
| 369 | while (true) { |
| 370 | XGL_QUEUE queue; |
| 371 | XGL_RESULT err = xglGetDeviceQueue(obj(), queue_mapping[i].type, idx++, &queue); |
| 372 | if (err != XGL_SUCCESS) |
| 373 | break; |
| 374 | queues_[queue_mapping[i].index].push_back(new Queue(queue)); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | EXPECT(!queues_[GRAPHICS].empty() || !queues_[COMPUTE].empty()); |
| 379 | } |
| 380 | |
| 381 | void Device::init_heap_props() |
| 382 | { |
| 383 | uint32_t count; |
| 384 | if (!EXPECT(xglGetMemoryHeapCount(obj(), &count) == XGL_SUCCESS && count)) |
| 385 | return; |
| 386 | if (count > XGL_MAX_MEMORY_HEAPS) |
| 387 | count = XGL_MAX_MEMORY_HEAPS; |
| 388 | |
| 389 | heap_props_.reserve(count); |
| 390 | for (uint32_t i = 0; i < count; i++) { |
| 391 | const XGL_MEMORY_HEAP_INFO_TYPE type = XGL_INFO_TYPE_MEMORY_HEAP_PROPERTIES; |
| 392 | XGL_MEMORY_HEAP_PROPERTIES props; |
| 393 | XGL_SIZE size = sizeof(props); |
| 394 | if (EXPECT(xglGetMemoryHeapInfo(obj(), i, type, &size, &props) == XGL_SUCCESS && size == sizeof(props))) |
| 395 | heap_props_.push_back(props); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void Device::init_formats() |
| 400 | { |
| 401 | for (int ch = XGL_CH_FMT_UNDEFINED; ch <= XGL_MAX_CH_FMT; ch++) { |
| 402 | for (int num = XGL_NUM_FMT_UNDEFINED; num <= XGL_MAX_NUM_FMT; num++) { |
| 403 | const XGL_FORMAT fmt = { static_cast<XGL_CHANNEL_FORMAT>(ch), |
| 404 | static_cast<XGL_NUM_FORMAT>(num) }; |
| 405 | const XGL_FORMAT_PROPERTIES props = format_properties(fmt); |
| 406 | |
| 407 | if (props.linearTilingFeatures) { |
| 408 | const Format tmp = { fmt, XGL_LINEAR_TILING, props.linearTilingFeatures }; |
| 409 | formats_.push_back(tmp); |
| 410 | } |
| 411 | |
| 412 | if (props.optimalTilingFeatures) { |
| 413 | const Format tmp = { fmt, XGL_OPTIMAL_TILING, props.optimalTilingFeatures }; |
| 414 | formats_.push_back(tmp); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | EXPECT(!formats_.empty()); |
| 420 | } |
| 421 | |
| 422 | XGL_FORMAT_PROPERTIES Device::format_properties(XGL_FORMAT format) |
| 423 | { |
| 424 | const XGL_FORMAT_INFO_TYPE type = XGL_INFO_TYPE_FORMAT_PROPERTIES; |
| 425 | XGL_FORMAT_PROPERTIES data; |
| 426 | size_t size = sizeof(data); |
| 427 | if (!EXPECT(xglGetFormatInfo(obj(), format, type, &size, &data) == XGL_SUCCESS && size == sizeof(data))) |
| 428 | memset(&data, 0, sizeof(data)); |
| 429 | |
| 430 | return data; |
| 431 | } |
| 432 | |
| 433 | void Device::wait() |
| 434 | { |
| 435 | EXPECT(xglDeviceWaitIdle(obj()) == XGL_SUCCESS); |
| 436 | } |
| 437 | |
| 438 | XGL_RESULT Device::wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout) |
| 439 | { |
| 440 | const std::vector<XGL_FENCE> fence_objs = make_objects<XGL_FENCE>(fences); |
| 441 | XGL_RESULT err = xglWaitForFences(obj(), fence_objs.size(), &fence_objs[0], wait_all, timeout); |
| 442 | EXPECT(err == XGL_SUCCESS || err == XGL_TIMEOUT); |
| 443 | |
| 444 | return err; |
| 445 | } |
| 446 | |
| 447 | void Queue::submit(const std::vector<const CmdBuffer *> &cmds, const std::vector<XGL_MEMORY_REF> &mem_refs, Fence &fence) |
| 448 | { |
| 449 | const std::vector<XGL_CMD_BUFFER> cmd_objs = make_objects<XGL_CMD_BUFFER>(cmds); |
| 450 | EXPECT(xglQueueSubmit(obj(), cmd_objs.size(), &cmd_objs[0], mem_refs.size(), &mem_refs[0], fence.obj()) == XGL_SUCCESS); |
| 451 | } |
| 452 | |
| 453 | void Queue::submit(const CmdBuffer &cmd, const std::vector<XGL_MEMORY_REF> &mem_refs, Fence &fence) |
| 454 | { |
| 455 | submit(std::vector<const CmdBuffer*>(1, &cmd), mem_refs, fence); |
| 456 | } |
| 457 | |
| 458 | void Queue::submit(const CmdBuffer &cmd, const std::vector<XGL_MEMORY_REF> &mem_refs) |
| 459 | { |
| 460 | Fence fence; |
| 461 | submit(cmd, mem_refs, fence); |
| 462 | } |
| 463 | |
| 464 | void Queue::set_global_mem_references(const std::vector<XGL_MEMORY_REF> &mem_refs) |
| 465 | { |
| 466 | EXPECT(xglQueueSetGlobalMemReferences(obj(), mem_refs.size(), &mem_refs[0]) == XGL_SUCCESS); |
| 467 | } |
| 468 | |
| 469 | void Queue::wait() |
| 470 | { |
| 471 | EXPECT(xglQueueWaitIdle(obj()) == XGL_SUCCESS); |
| 472 | } |
| 473 | |
| 474 | void Queue::signal_semaphore(QueueSemaphore &sem) |
| 475 | { |
| 476 | EXPECT(xglSignalQueueSemaphore(obj(), sem.obj()) == XGL_SUCCESS); |
| 477 | } |
| 478 | |
| 479 | void Queue::wait_semaphore(QueueSemaphore &sem) |
| 480 | { |
| 481 | EXPECT(xglWaitQueueSemaphore(obj(), sem.obj()) == XGL_SUCCESS); |
| 482 | } |
| 483 | |
| 484 | GpuMemory::~GpuMemory() |
| 485 | { |
| 486 | if (initialized() && own()) |
| 487 | EXPECT(xglFreeMemory(obj()) == XGL_SUCCESS); |
| 488 | } |
| 489 | |
| 490 | void GpuMemory::init(const Device &dev, const XGL_MEMORY_ALLOC_INFO &info) |
| 491 | { |
| 492 | DERIVED_OBJECT_INIT(xglAllocMemory, dev.obj(), &info); |
| 493 | } |
| 494 | |
| 495 | void GpuMemory::init(const Device &dev, XGL_GPU_SIZE size) |
| 496 | { |
| 497 | XGL_MEMORY_ALLOC_INFO info = {}; |
| 498 | info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 499 | info.allocationSize = size; |
| 500 | info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 501 | |
| 502 | // find CPU visible heaps |
| 503 | for (XGL_UINT id = 0; id < dev.heap_properties().size(); id++) { |
| 504 | const XGL_MEMORY_HEAP_PROPERTIES &heap = dev.heap_properties()[id]; |
| 505 | if (heap.flags & XGL_MEMORY_HEAP_CPU_VISIBLE_BIT) |
| 506 | info.heaps[info.heapCount++] = id; |
| 507 | } |
| 508 | |
| 509 | EXPECT(info.heapCount); |
| 510 | init(dev, info); |
| 511 | } |
| 512 | |
| 513 | void GpuMemory::init(const Device &dev, size_t size, const void *data) |
| 514 | { |
| 515 | DERIVED_OBJECT_INIT(xglPinSystemMemory, dev.obj(), data, size); |
| 516 | } |
| 517 | |
| 518 | void GpuMemory::init(const Device &dev, const XGL_MEMORY_OPEN_INFO &info) |
| 519 | { |
| 520 | DERIVED_OBJECT_INIT(xglOpenSharedMemory, dev.obj(), &info); |
| 521 | } |
| 522 | |
| 523 | void GpuMemory::init(const Device &dev, const XGL_PEER_MEMORY_OPEN_INFO &info) |
| 524 | { |
| 525 | DERIVED_OBJECT_INIT(xglOpenPeerMemory, dev.obj(), &info); |
| 526 | } |
| 527 | |
| 528 | void GpuMemory::set_priority(XGL_MEMORY_PRIORITY priority) |
| 529 | { |
| 530 | EXPECT(xglSetMemoryPriority(obj(), priority) == XGL_SUCCESS); |
| 531 | } |
| 532 | |
| 533 | const void *GpuMemory::map(XGL_FLAGS flags) const |
| 534 | { |
| 535 | void *data; |
| 536 | if (!EXPECT(xglMapMemory(obj(), flags, &data) == XGL_SUCCESS)) |
| 537 | data = NULL; |
| 538 | |
| 539 | return data; |
| 540 | } |
| 541 | |
| 542 | void *GpuMemory::map(XGL_FLAGS flags) |
| 543 | { |
| 544 | void *data; |
| 545 | if (!EXPECT(xglMapMemory(obj(), flags, &data) == XGL_SUCCESS)) |
| 546 | data = NULL; |
| 547 | |
| 548 | return data; |
| 549 | } |
| 550 | |
| 551 | void GpuMemory::unmap() const |
| 552 | { |
| 553 | EXPECT(xglUnmapMemory(obj()) == XGL_SUCCESS); |
| 554 | } |
| 555 | |
| 556 | void Fence::init(const Device &dev, const XGL_FENCE_CREATE_INFO &info) |
| 557 | { |
| 558 | DERIVED_OBJECT_INIT(xglCreateFence, dev.obj(), &info); |
| 559 | alloc_memory(dev); |
| 560 | } |
| 561 | |
| 562 | void QueueSemaphore::init(const Device &dev, const XGL_QUEUE_SEMAPHORE_CREATE_INFO &info) |
| 563 | { |
| 564 | DERIVED_OBJECT_INIT(xglCreateQueueSemaphore, dev.obj(), &info); |
| 565 | alloc_memory(dev); |
| 566 | } |
| 567 | |
| 568 | void QueueSemaphore::init(const Device &dev, const XGL_QUEUE_SEMAPHORE_OPEN_INFO &info) |
| 569 | { |
| 570 | DERIVED_OBJECT_INIT(xglOpenSharedQueueSemaphore, dev.obj(), &info); |
| 571 | } |
| 572 | |
| 573 | void Event::init(const Device &dev, const XGL_EVENT_CREATE_INFO &info) |
| 574 | { |
| 575 | DERIVED_OBJECT_INIT(xglCreateEvent, dev.obj(), &info); |
| 576 | alloc_memory(dev); |
| 577 | } |
| 578 | |
| 579 | void Event::set() |
| 580 | { |
| 581 | EXPECT(xglSetEvent(obj()) == XGL_SUCCESS); |
| 582 | } |
| 583 | |
| 584 | void Event::reset() |
| 585 | { |
| 586 | EXPECT(xglResetEvent(obj()) == XGL_SUCCESS); |
| 587 | } |
| 588 | |
| 589 | void QueryPool::init(const Device &dev, const XGL_QUERY_POOL_CREATE_INFO &info) |
| 590 | { |
| 591 | DERIVED_OBJECT_INIT(xglCreateQueryPool, dev.obj(), &info); |
| 592 | alloc_memory(dev); |
| 593 | } |
| 594 | |
| 595 | XGL_RESULT QueryPool::results(uint32_t start, uint32_t count, size_t size, void *data) |
| 596 | { |
| 597 | size_t tmp = size; |
| 598 | XGL_RESULT err = xglGetQueryPoolResults(obj(), start, count, &tmp, data); |
| 599 | if (err == XGL_SUCCESS) { |
| 600 | if (!EXPECT(tmp == size)) |
| 601 | memset(data, 0, size); |
| 602 | } else { |
| 603 | EXPECT(err == XGL_NOT_READY); |
| 604 | } |
| 605 | |
| 606 | return err; |
| 607 | } |
| 608 | |
| 609 | void Image::init(const Device &dev, const XGL_IMAGE_CREATE_INFO &info) |
| 610 | { |
| 611 | init_no_mem(dev, info); |
| 612 | alloc_memory(dev, info.tiling == XGL_LINEAR_TILING); |
| 613 | } |
| 614 | |
| 615 | void Image::init_no_mem(const Device &dev, const XGL_IMAGE_CREATE_INFO &info) |
| 616 | { |
| 617 | DERIVED_OBJECT_INIT(xglCreateImage, dev.obj(), &info); |
| 618 | init_info(dev, info); |
| 619 | } |
| 620 | |
| 621 | void Image::init(const Device &dev, const XGL_PEER_IMAGE_OPEN_INFO &info, const XGL_IMAGE_CREATE_INFO &original_info) |
| 622 | { |
| 623 | XGL_IMAGE img; |
| 624 | XGL_GPU_MEMORY mem; |
| 625 | EXPECT(xglOpenPeerImage(dev.obj(), &info, &img, &mem) == XGL_SUCCESS); |
| 626 | Object::init(img); |
| 627 | |
| 628 | init_info(dev, original_info); |
| 629 | alloc_memory(std::vector<XGL_GPU_MEMORY>(1, mem)); |
| 630 | } |
| 631 | |
| 632 | void Image::init_info(const Device &dev, const XGL_IMAGE_CREATE_INFO &info) |
| 633 | { |
| 634 | create_info_ = info; |
| 635 | |
| 636 | for (std::vector<Device::Format>::const_iterator it = dev.formats().begin(); it != dev.formats().end(); it++) { |
| 637 | if (memcmp(&it->format, &create_info_.format, sizeof(it->format)) == 0 && it->tiling == create_info_.tiling) { |
| 638 | format_features_ = it->features; |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | XGL_SUBRESOURCE_LAYOUT Image::subresource_layout(const XGL_IMAGE_SUBRESOURCE &subres) const |
| 645 | { |
| 646 | const XGL_SUBRESOURCE_INFO_TYPE type = XGL_INFO_TYPE_SUBRESOURCE_LAYOUT; |
| 647 | XGL_SUBRESOURCE_LAYOUT data; |
| 648 | size_t size = sizeof(data); |
| 649 | if (!EXPECT(xglGetImageSubresourceInfo(obj(), &subres, type, &size, &data) == XGL_SUCCESS && size == sizeof(data))) |
| 650 | memset(&data, 0, sizeof(data)); |
| 651 | |
| 652 | return data; |
| 653 | } |
| 654 | |
| 655 | bool Image::transparent() const |
| 656 | { |
| 657 | return (create_info_.tiling == XGL_LINEAR_TILING && |
| 658 | create_info_.samples == 1 && |
| 659 | !(create_info_.usage & (XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 660 | XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT))); |
| 661 | } |
| 662 | |
| 663 | void ImageView::init(const Device &dev, const XGL_IMAGE_VIEW_CREATE_INFO &info) |
| 664 | { |
| 665 | DERIVED_OBJECT_INIT(xglCreateImageView, dev.obj(), &info); |
| 666 | alloc_memory(dev); |
| 667 | } |
| 668 | |
| 669 | void ColorAttachmentView::init(const Device &dev, const XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO &info) |
| 670 | { |
| 671 | DERIVED_OBJECT_INIT(xglCreateColorAttachmentView, dev.obj(), &info); |
| 672 | alloc_memory(dev); |
| 673 | } |
| 674 | |
| 675 | void DepthStencilView::init(const Device &dev, const XGL_DEPTH_STENCIL_VIEW_CREATE_INFO &info) |
| 676 | { |
| 677 | DERIVED_OBJECT_INIT(xglCreateDepthStencilView, dev.obj(), &info); |
| 678 | alloc_memory(dev); |
| 679 | } |
| 680 | |
| 681 | void Shader::init(const Device &dev, const XGL_SHADER_CREATE_INFO &info) |
| 682 | { |
| 683 | DERIVED_OBJECT_INIT(xglCreateShader, dev.obj(), &info); |
| 684 | } |
| 685 | |
| 686 | XGL_RESULT Shader::init_try(const Device &dev, const XGL_SHADER_CREATE_INFO &info) |
| 687 | { |
| 688 | XGL_SHADER sh; |
| 689 | XGL_RESULT err = xglCreateShader(dev.obj(), &info, &sh); |
| 690 | if (err == XGL_SUCCESS) |
| 691 | Object::init(sh); |
| 692 | |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | void Pipeline::init(const Device &dev, const XGL_GRAPHICS_PIPELINE_CREATE_INFO &info) |
| 697 | { |
| 698 | DERIVED_OBJECT_INIT(xglCreateGraphicsPipeline, dev.obj(), &info); |
| 699 | alloc_memory(dev); |
| 700 | } |
| 701 | |
| 702 | void Pipeline::init(const Device &dev, const XGL_COMPUTE_PIPELINE_CREATE_INFO &info) |
| 703 | { |
| 704 | DERIVED_OBJECT_INIT(xglCreateComputePipeline, dev.obj(), &info); |
| 705 | alloc_memory(dev); |
| 706 | } |
| 707 | |
| 708 | void Pipeline::init(const Device&dev, size_t size, const void *data) |
| 709 | { |
| 710 | DERIVED_OBJECT_INIT(xglLoadPipeline, dev.obj(), size, data); |
| 711 | alloc_memory(dev); |
| 712 | } |
| 713 | |
| 714 | size_t Pipeline::store(size_t size, void *data) |
| 715 | { |
| 716 | if (!EXPECT(xglStorePipeline(obj(), &size, data) == XGL_SUCCESS)) |
| 717 | size = 0; |
| 718 | |
| 719 | return size; |
| 720 | } |
| 721 | |
| 722 | void PipelineDelta::init(const Device &dev, const Pipeline &p1, const Pipeline &p2) |
| 723 | { |
| 724 | DERIVED_OBJECT_INIT(xglCreatePipelineDelta, dev.obj(), p1.obj(), p2.obj()); |
| 725 | alloc_memory(dev); |
| 726 | } |
| 727 | |
| 728 | void Sampler::init(const Device &dev, const XGL_SAMPLER_CREATE_INFO &info) |
| 729 | { |
| 730 | DERIVED_OBJECT_INIT(xglCreateSampler, dev.obj(), &info); |
| 731 | alloc_memory(dev); |
| 732 | } |
| 733 | |
| 734 | void DescriptorSet::init(const Device &dev, const XGL_DESCRIPTOR_SET_CREATE_INFO &info) |
| 735 | { |
| 736 | DERIVED_OBJECT_INIT(xglCreateDescriptorSet, dev.obj(), &info); |
| 737 | info_ = info; |
| 738 | } |
| 739 | |
| 740 | void DescriptorSet::attach(uint32_t start_slot, const std::vector<const Sampler *> &samplers) |
| 741 | { |
| 742 | const std::vector<XGL_SAMPLER> sampler_objs = make_objects<XGL_SAMPLER>(samplers); |
| 743 | xglAttachSamplerDescriptors(obj(), start_slot, sampler_objs.size(), &sampler_objs[0]); |
| 744 | } |
| 745 | |
| 746 | void DescriptorSet::attach(uint32_t start_slot, const std::vector<XGL_IMAGE_VIEW_ATTACH_INFO> &img_views) |
| 747 | { |
| 748 | xglAttachImageViewDescriptors(obj(), start_slot, img_views.size(), &img_views[0]); |
| 749 | } |
| 750 | |
| 751 | void DescriptorSet::attach(uint32_t start_slot, const std::vector<XGL_MEMORY_VIEW_ATTACH_INFO> &mem_views) |
| 752 | { |
| 753 | xglAttachMemoryViewDescriptors(obj(), start_slot, mem_views.size(), &mem_views[0]); |
| 754 | } |
| 755 | |
| 756 | void DescriptorSet::attach(uint32_t start_slot, const std::vector<XGL_DESCRIPTOR_SET_ATTACH_INFO> &sets) |
| 757 | { |
| 758 | xglAttachNestedDescriptors(obj(), start_slot, sets.size(), &sets[0]); |
| 759 | } |
| 760 | |
| 761 | void DynamicVpStateObject::init(const Device &dev, const XGL_VIEWPORT_STATE_CREATE_INFO &info) |
| 762 | { |
| 763 | DERIVED_OBJECT_INIT(xglCreateViewportState, dev.obj(), &info); |
| 764 | alloc_memory(dev); |
| 765 | } |
| 766 | |
| 767 | void DynamicRsStateObject::init(const Device &dev, const XGL_RASTER_STATE_CREATE_INFO &info) |
| 768 | { |
| 769 | DERIVED_OBJECT_INIT(xglCreateRasterState, dev.obj(), &info); |
| 770 | alloc_memory(dev); |
| 771 | } |
| 772 | |
| 773 | void DynamicMsaaStateObject::init(const Device &dev, const XGL_MSAA_STATE_CREATE_INFO &info) |
| 774 | { |
| 775 | DERIVED_OBJECT_INIT(xglCreateMsaaState, dev.obj(), &info); |
| 776 | alloc_memory(dev); |
| 777 | } |
| 778 | |
| 779 | void DynamicCbStateObject::init(const Device &dev, const XGL_COLOR_BLEND_STATE_CREATE_INFO &info) |
| 780 | { |
| 781 | DERIVED_OBJECT_INIT(xglCreateColorBlendState, dev.obj(), &info); |
| 782 | alloc_memory(dev); |
| 783 | } |
| 784 | |
| 785 | void DynamicDsStateObject::init(const Device &dev, const XGL_DEPTH_STENCIL_STATE_CREATE_INFO &info) |
| 786 | { |
| 787 | DERIVED_OBJECT_INIT(xglCreateDepthStencilState, dev.obj(), &info); |
| 788 | alloc_memory(dev); |
| 789 | } |
| 790 | |
| 791 | void CmdBuffer::init(const Device &dev, const XGL_CMD_BUFFER_CREATE_INFO &info) |
| 792 | { |
| 793 | DERIVED_OBJECT_INIT(xglCreateCommandBuffer, dev.obj(), &info); |
| 794 | } |
| 795 | |
| 796 | void CmdBuffer::begin(XGL_FLAGS flags) |
| 797 | { |
| 798 | EXPECT(xglBeginCommandBuffer(obj(), flags) == XGL_SUCCESS); |
| 799 | } |
| 800 | |
| 801 | void CmdBuffer::begin() |
| 802 | { |
| 803 | begin(XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT | |
| 804 | XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT); |
| 805 | } |
| 806 | |
| 807 | void CmdBuffer::end() |
| 808 | { |
| 809 | EXPECT(xglEndCommandBuffer(obj()) == XGL_SUCCESS); |
| 810 | } |
| 811 | |
| 812 | void CmdBuffer::reset() |
| 813 | { |
| 814 | EXPECT(xglResetCommandBuffer(obj()) == XGL_SUCCESS); |
| 815 | } |
| 816 | |
| 817 | }; // namespace xgl_testing |