tests: add a new XGL C++ binding

v2: various cleanups, Object::memories() return type changed
diff --git a/tests/xgltestbinding.cpp b/tests/xgltestbinding.cpp
new file mode 100644
index 0000000..1d913a6
--- /dev/null
+++ b/tests/xgltestbinding.cpp
@@ -0,0 +1,817 @@
+// XGL tests
+//
+// Copyright (C) 2014 LunarG, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+
+#include <iostream>
+#include <string.h> // memset(), memcmp()
+#include "xgltestbinding.h"
+
+namespace {
+
+#define DERIVED_OBJECT_INIT(create_func, ...)                       \
+    do {                                                            \
+        obj_type obj;                                               \
+        if (EXPECT(create_func(__VA_ARGS__, &obj) == XGL_SUCCESS))  \
+            base_type::init(obj);                                   \
+    } while (0)
+
+#define STRINGIFY(x) #x
+#define EXPECT(expr) ((expr) ? true : expect_failure(STRINGIFY(expr), __FILE__, __LINE__, __FUNCTION__))
+
+xgl_testing::ErrorCallback error_callback;
+
+bool expect_failure(const char *expr, const char *file, unsigned int line, const char *function)
+{
+    if (error_callback) {
+        error_callback(expr, file, line, function);
+    } else {
+        std::cerr << file << ":" << line << ": " << function <<
+            ": Expectation `" << expr << "' failed.\n";
+    }
+
+    return false;
+}
+
+template<class T, class S>
+std::vector<T> make_objects(const std::vector<S> &v)
+{
+    std::vector<T> objs;
+    objs.reserve(v.size());
+    for (typename std::vector<S>::const_iterator it = v.begin(); it != v.end(); it++)
+        objs.push_back((*it)->obj());
+    return objs;
+}
+
+template<typename T>
+std::vector<T> get_info(XGL_PHYSICAL_GPU gpu, XGL_PHYSICAL_GPU_INFO_TYPE type, size_t min_elems)
+{
+    std::vector<T> info;
+    size_t size;
+    if (EXPECT(xglGetGpuInfo(gpu, type, &size, NULL) == XGL_SUCCESS && size % sizeof(T) == 0)) {
+        info.resize(size / sizeof(T));
+        if (!EXPECT(xglGetGpuInfo(gpu, type, &size, &info[0]) == XGL_SUCCESS && size == info.size() * sizeof(T)))
+            info.clear();
+    }
+
+    if (info.size() < min_elems)
+        info.resize(min_elems);
+
+    return info;
+}
+
+template<typename T>
+std::vector<T> get_info(XGL_BASE_OBJECT obj, XGL_OBJECT_INFO_TYPE type, size_t min_elems)
+{
+    std::vector<T> info;
+    size_t size;
+    if (EXPECT(xglGetObjectInfo(obj, type, &size, NULL) == XGL_SUCCESS && size % sizeof(T) == 0)) {
+        info.resize(size / sizeof(T));
+        if (!EXPECT(xglGetObjectInfo(obj, type, &size, &info[0]) == XGL_SUCCESS && size == info.size() * sizeof(T)))
+            info.clear();
+    }
+
+    if (info.size() < min_elems)
+        info.resize(min_elems);
+
+    return info;
+}
+
+} // namespace
+
+namespace xgl_testing {
+
+void set_error_callback(ErrorCallback callback)
+{
+    error_callback = callback;
+}
+
+XGL_PHYSICAL_GPU_PROPERTIES PhysicalGpu::properties() const
+{
+    return get_info<XGL_PHYSICAL_GPU_PROPERTIES>(gpu_, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, 1)[0];
+}
+
+XGL_PHYSICAL_GPU_PERFORMANCE PhysicalGpu::performance() const
+{
+    return get_info<XGL_PHYSICAL_GPU_PERFORMANCE>(gpu_, XGL_INFO_TYPE_PHYSICAL_GPU_PERFORMANCE, 1)[0];
+}
+
+std::vector<XGL_PHYSICAL_GPU_QUEUE_PROPERTIES> PhysicalGpu::queue_properties() const
+{
+    return get_info<XGL_PHYSICAL_GPU_QUEUE_PROPERTIES>(gpu_, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES, 0);
+}
+
+XGL_PHYSICAL_GPU_MEMORY_PROPERTIES PhysicalGpu::memory_properties() const
+{
+    return get_info<XGL_PHYSICAL_GPU_MEMORY_PROPERTIES>(gpu_, XGL_INFO_TYPE_PHYSICAL_GPU_MEMORY_PROPERTIES, 1)[0];
+}
+
+std::vector<const char *> PhysicalGpu::layers(std::vector<char> &buf) const
+{
+    const size_t max_layer_count = 16;
+    const size_t max_string_size = 256;
+
+    buf.resize(max_layer_count * max_string_size);
+
+    std::vector<const char *> layers;
+    layers.reserve(max_layer_count);
+    for (size_t i = 0; i < max_layer_count; i++)
+        layers.push_back(&buf[0] + max_string_size * i);
+
+    char * const *out = const_cast<char * const *>(&layers[0]);
+    size_t count;
+    if (!EXPECT(xglEnumerateLayers(gpu_, max_layer_count, max_string_size, out, &count, NULL) == XGL_SUCCESS))
+        count = 0;
+    layers.resize(count);
+
+    return layers;
+}
+
+std::vector<const char *> PhysicalGpu::extensions() const
+{
+    static const char *known_exts[] = {
+        "XGL_WSI_X11",
+    };
+
+    std::vector<const char *> exts;
+    for (int i = 0; i < sizeof(known_exts) / sizeof(known_exts[0]); i++) {
+        XGL_RESULT err = xglGetExtensionSupport(gpu_, known_exts[i]);
+        if (err == XGL_SUCCESS)
+            exts.push_back(known_exts[i]);
+    }
+
+    return exts;
+}
+
+XGL_GPU_COMPATIBILITY_INFO PhysicalGpu::compatibility(const PhysicalGpu &other) const
+{
+    XGL_GPU_COMPATIBILITY_INFO data;
+    if (!EXPECT(xglGetMultiGpuCompatibility(gpu_, other.gpu_, &data) == XGL_SUCCESS))
+        memset(&data, 0, sizeof(data));
+
+    return data;
+}
+
+void BaseObject::init(XGL_BASE_OBJECT obj, bool own)
+{
+    EXPECT(!initialized());
+    reinit(obj, own);
+}
+
+void BaseObject::reinit(XGL_BASE_OBJECT obj, bool own)
+{
+    obj_ = obj;
+    own_obj_ = own;
+}
+
+uint32_t BaseObject::memory_allocation_count() const
+{
+    return memory_requirements().size();
+}
+
+std::vector<XGL_MEMORY_REQUIREMENTS> BaseObject::memory_requirements() const
+{
+    std::vector<XGL_MEMORY_REQUIREMENTS> info =
+        get_info<XGL_MEMORY_REQUIREMENTS>(obj_, XGL_INFO_TYPE_MEMORY_REQUIREMENTS, 0);
+    if (info.size() == 1 && !info[0].size)
+        info.clear();
+
+    return info;
+}
+
+void Object::init(XGL_OBJECT obj, bool own)
+{
+    BaseObject::init(obj, own);
+    mem_alloc_count_ = memory_allocation_count();
+}
+
+void Object::reinit(XGL_OBJECT obj, bool own)
+{
+    cleanup();
+    BaseObject::reinit(obj, own);
+    mem_alloc_count_ = memory_allocation_count();
+}
+
+void Object::cleanup()
+{
+    if (!initialized())
+        return;
+
+    unbind_memory();
+
+    if (internal_mems_) {
+        delete[] internal_mems_;
+        internal_mems_ = NULL;
+        primary_mem_ = NULL;
+    }
+
+    mem_alloc_count_ = 0;
+
+    if (own())
+        EXPECT(xglDestroyObject(obj()) == XGL_SUCCESS);
+}
+
+void Object::bind_memory(uint32_t alloc_idx, const GpuMemory &mem, XGL_GPU_SIZE mem_offset)
+{
+    EXPECT(!alloc_idx && xglBindObjectMemory(obj(), mem.obj(), mem_offset) == XGL_SUCCESS);
+}
+
+void Object::unbind_memory(uint32_t alloc_idx)
+{
+    EXPECT(!alloc_idx && xglBindObjectMemory(obj(), XGL_NULL_HANDLE, 0) == XGL_SUCCESS);
+}
+
+void Object::unbind_memory()
+{
+    for (uint32_t i = 0; i < mem_alloc_count_; i++)
+        unbind_memory(i);
+}
+
+void Object::alloc_memory(const Device &dev, bool for_linear_img)
+{
+    if (!EXPECT(!internal_mems_) || !mem_alloc_count_)
+        return;
+
+    internal_mems_ = new GpuMemory[mem_alloc_count_];
+
+    const std::vector<XGL_MEMORY_REQUIREMENTS> mem_reqs = memory_requirements();
+    for (int i = 0; i < mem_reqs.size(); i++) {
+        XGL_MEMORY_ALLOC_INFO info = GpuMemory::alloc_info(mem_reqs[i]);
+
+        primary_mem_ = &internal_mems_[i];
+
+        internal_mems_[i].init(dev, info);
+        bind_memory(i, internal_mems_[i], 0);
+    }
+}
+
+void Object::alloc_memory(const std::vector<XGL_GPU_MEMORY> &mems)
+{
+    if (!EXPECT(!internal_mems_) || !mem_alloc_count_)
+        return;
+
+    internal_mems_ = new GpuMemory[mem_alloc_count_];
+
+    const std::vector<XGL_MEMORY_REQUIREMENTS> mem_reqs = memory_requirements();
+    if (!EXPECT(mem_reqs.size() == mems.size()))
+        return;
+
+    for (int i = 0; i < mem_reqs.size(); i++) {
+        primary_mem_ = &internal_mems_[i];
+
+        internal_mems_[i].init(mems[i]);
+        bind_memory(i, internal_mems_[i], 0);
+    }
+}
+
+std::vector<XGL_GPU_MEMORY> Object::memories() const
+{
+    std::vector<XGL_GPU_MEMORY> mems;
+    if (internal_mems_) {
+        mems.reserve(mem_alloc_count_);
+        for (uint32_t i = 0; i < mem_alloc_count_; i++)
+            mems.push_back(internal_mems_[i].obj());
+    }
+
+    return mems;
+}
+
+Device::~Device()
+{
+    if (!initialized())
+        return;
+
+    for (int i = 0; i < QUEUE_COUNT; i++) {
+        for (std::vector<Queue *>::iterator it = queues_[i].begin(); it != queues_[i].end(); it++)
+            delete *it;
+        queues_[i].clear();
+    }
+
+    EXPECT(xglDestroyDevice(obj()) == XGL_SUCCESS);
+}
+
+void Device::init()
+{
+    // request all queues
+    const std::vector<XGL_PHYSICAL_GPU_QUEUE_PROPERTIES> queue_props = gpu_.queue_properties();
+    std::vector<XGL_DEVICE_QUEUE_CREATE_INFO> queue_info;
+    queue_info.reserve(queue_props.size());
+    for (int i = 0; i < queue_props.size(); i++) {
+        XGL_DEVICE_QUEUE_CREATE_INFO qi = {};
+        qi.queueNodeIndex = i;
+        qi.queueCount = queue_props[i].queueCount;
+        queue_info.push_back(qi);
+    }
+
+    // request all layers
+    std::vector<char> layer_buf;
+    const std::vector<const char *> layers = gpu_.layers(layer_buf);
+    XGL_LAYER_CREATE_INFO layer_info = {};
+    layer_info.sType = XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO;
+    layer_info.layerCount = layers.size();
+    layer_info.ppActiveLayerNames = &layers[0];
+
+    const std::vector<const char *> exts = gpu_.extensions();
+
+    XGL_DEVICE_CREATE_INFO dev_info = {};
+    dev_info.sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
+    dev_info.pNext = static_cast<void *>(&layer_info);
+    dev_info.queueRecordCount = queue_info.size();
+    dev_info.pRequestedQueues = &queue_info[0];
+    dev_info.extensionCount = exts.size();
+    dev_info.ppEnabledExtensionNames = &exts[0];
+    dev_info.maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE;
+    dev_info.flags = XGL_DEVICE_CREATE_VALIDATION_BIT;
+
+    init(dev_info);
+}
+
+void Device::init(const XGL_DEVICE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateDevice, gpu_.obj(), &info);
+
+    init_queues();
+    init_heap_props();
+    init_formats();
+}
+
+void Device::init_queues()
+{
+    const struct {
+        QueueIndex index;
+        XGL_QUEUE_TYPE type;
+    } queue_mapping[] = {
+        { GRAPHICS, XGL_QUEUE_TYPE_GRAPHICS },
+        { COMPUTE, XGL_QUEUE_TYPE_COMPUTE },
+        { DMA, XGL_QUEUE_TYPE_DMA },
+    };
+
+    for (int i = 0; i < QUEUE_COUNT; i++) {
+        uint32_t idx = 0;
+
+        while (true) {
+            XGL_QUEUE queue;
+            XGL_RESULT err = xglGetDeviceQueue(obj(), queue_mapping[i].type, idx++, &queue);
+            if (err != XGL_SUCCESS)
+                break;
+            queues_[queue_mapping[i].index].push_back(new Queue(queue));
+        }
+    }
+
+    EXPECT(!queues_[GRAPHICS].empty() || !queues_[COMPUTE].empty());
+}
+
+void Device::init_heap_props()
+{
+    uint32_t count;
+    if (!EXPECT(xglGetMemoryHeapCount(obj(), &count) == XGL_SUCCESS && count))
+        return;
+    if (count > XGL_MAX_MEMORY_HEAPS)
+        count = XGL_MAX_MEMORY_HEAPS;
+
+    heap_props_.reserve(count);
+    for (uint32_t i = 0; i < count; i++) {
+        const XGL_MEMORY_HEAP_INFO_TYPE type = XGL_INFO_TYPE_MEMORY_HEAP_PROPERTIES;
+        XGL_MEMORY_HEAP_PROPERTIES props;
+        XGL_SIZE size = sizeof(props);
+        if (EXPECT(xglGetMemoryHeapInfo(obj(), i, type, &size, &props) == XGL_SUCCESS && size == sizeof(props)))
+            heap_props_.push_back(props);
+    }
+}
+
+void Device::init_formats()
+{
+    for (int ch = XGL_CH_FMT_UNDEFINED; ch <= XGL_MAX_CH_FMT; ch++) {
+        for (int num = XGL_NUM_FMT_UNDEFINED; num <= XGL_MAX_NUM_FMT; num++) {
+            const XGL_FORMAT fmt = { static_cast<XGL_CHANNEL_FORMAT>(ch),
+                                     static_cast<XGL_NUM_FORMAT>(num) };
+            const XGL_FORMAT_PROPERTIES props = format_properties(fmt);
+
+            if (props.linearTilingFeatures) {
+                const Format tmp = { fmt, XGL_LINEAR_TILING, props.linearTilingFeatures };
+                formats_.push_back(tmp);
+            }
+
+            if (props.optimalTilingFeatures) {
+                const Format tmp = { fmt, XGL_OPTIMAL_TILING, props.optimalTilingFeatures };
+                formats_.push_back(tmp);
+            }
+        }
+    }
+
+    EXPECT(!formats_.empty());
+}
+
+XGL_FORMAT_PROPERTIES Device::format_properties(XGL_FORMAT format)
+{
+    const XGL_FORMAT_INFO_TYPE type = XGL_INFO_TYPE_FORMAT_PROPERTIES;
+    XGL_FORMAT_PROPERTIES data;
+    size_t size = sizeof(data);
+    if (!EXPECT(xglGetFormatInfo(obj(), format, type, &size, &data) == XGL_SUCCESS && size == sizeof(data)))
+        memset(&data, 0, sizeof(data));
+
+    return data;
+}
+
+void Device::wait()
+{
+    EXPECT(xglDeviceWaitIdle(obj()) == XGL_SUCCESS);
+}
+
+XGL_RESULT Device::wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout)
+{
+    const std::vector<XGL_FENCE> fence_objs = make_objects<XGL_FENCE>(fences);
+    XGL_RESULT err = xglWaitForFences(obj(), fence_objs.size(), &fence_objs[0], wait_all, timeout);
+    EXPECT(err == XGL_SUCCESS || err == XGL_TIMEOUT);
+
+    return err;
+}
+
+void Queue::submit(const std::vector<const CmdBuffer *> &cmds, const std::vector<XGL_MEMORY_REF> &mem_refs, Fence &fence)
+{
+    const std::vector<XGL_CMD_BUFFER> cmd_objs = make_objects<XGL_CMD_BUFFER>(cmds);
+    EXPECT(xglQueueSubmit(obj(), cmd_objs.size(), &cmd_objs[0], mem_refs.size(), &mem_refs[0], fence.obj()) == XGL_SUCCESS);
+}
+
+void Queue::submit(const CmdBuffer &cmd, const std::vector<XGL_MEMORY_REF> &mem_refs, Fence &fence)
+{
+    submit(std::vector<const CmdBuffer*>(1, &cmd), mem_refs, fence);
+}
+
+void Queue::submit(const CmdBuffer &cmd, const std::vector<XGL_MEMORY_REF> &mem_refs)
+{
+    Fence fence;
+    submit(cmd, mem_refs, fence);
+}
+
+void Queue::set_global_mem_references(const std::vector<XGL_MEMORY_REF> &mem_refs)
+{
+    EXPECT(xglQueueSetGlobalMemReferences(obj(), mem_refs.size(), &mem_refs[0]) == XGL_SUCCESS);
+}
+
+void Queue::wait()
+{
+    EXPECT(xglQueueWaitIdle(obj()) == XGL_SUCCESS);
+}
+
+void Queue::signal_semaphore(QueueSemaphore &sem)
+{
+    EXPECT(xglSignalQueueSemaphore(obj(), sem.obj()) == XGL_SUCCESS);
+}
+
+void Queue::wait_semaphore(QueueSemaphore &sem)
+{
+    EXPECT(xglWaitQueueSemaphore(obj(), sem.obj()) == XGL_SUCCESS);
+}
+
+GpuMemory::~GpuMemory()
+{
+    if (initialized() && own())
+        EXPECT(xglFreeMemory(obj()) == XGL_SUCCESS);
+}
+
+void GpuMemory::init(const Device &dev, const XGL_MEMORY_ALLOC_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglAllocMemory, dev.obj(), &info);
+}
+
+void GpuMemory::init(const Device &dev, XGL_GPU_SIZE size)
+{
+    XGL_MEMORY_ALLOC_INFO info = {};
+    info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
+    info.allocationSize = size;
+    info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
+
+    // find CPU visible heaps
+    for (XGL_UINT id = 0; id < dev.heap_properties().size(); id++) {
+        const XGL_MEMORY_HEAP_PROPERTIES &heap = dev.heap_properties()[id];
+        if (heap.flags & XGL_MEMORY_HEAP_CPU_VISIBLE_BIT)
+            info.heaps[info.heapCount++] = id;
+    }
+
+    EXPECT(info.heapCount);
+    init(dev, info);
+}
+
+void GpuMemory::init(const Device &dev, size_t size, const void *data)
+{
+    DERIVED_OBJECT_INIT(xglPinSystemMemory, dev.obj(), data, size);
+}
+
+void GpuMemory::init(const Device &dev, const XGL_MEMORY_OPEN_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglOpenSharedMemory, dev.obj(), &info);
+}
+
+void GpuMemory::init(const Device &dev, const XGL_PEER_MEMORY_OPEN_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglOpenPeerMemory, dev.obj(), &info);
+}
+
+void GpuMemory::set_priority(XGL_MEMORY_PRIORITY priority)
+{
+    EXPECT(xglSetMemoryPriority(obj(), priority) == XGL_SUCCESS);
+}
+
+const void *GpuMemory::map(XGL_FLAGS flags) const
+{
+    void *data;
+    if (!EXPECT(xglMapMemory(obj(), flags, &data) == XGL_SUCCESS))
+        data = NULL;
+
+    return data;
+}
+
+void *GpuMemory::map(XGL_FLAGS flags)
+{
+    void *data;
+    if (!EXPECT(xglMapMemory(obj(), flags, &data) == XGL_SUCCESS))
+        data = NULL;
+
+    return data;
+}
+
+void GpuMemory::unmap() const
+{
+    EXPECT(xglUnmapMemory(obj()) == XGL_SUCCESS);
+}
+
+void Fence::init(const Device &dev, const XGL_FENCE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateFence, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void QueueSemaphore::init(const Device &dev, const XGL_QUEUE_SEMAPHORE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateQueueSemaphore, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void QueueSemaphore::init(const Device &dev, const XGL_QUEUE_SEMAPHORE_OPEN_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglOpenSharedQueueSemaphore, dev.obj(), &info);
+}
+
+void Event::init(const Device &dev, const XGL_EVENT_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateEvent, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void Event::set()
+{
+    EXPECT(xglSetEvent(obj()) == XGL_SUCCESS);
+}
+
+void Event::reset()
+{
+    EXPECT(xglResetEvent(obj()) == XGL_SUCCESS);
+}
+
+void QueryPool::init(const Device &dev, const XGL_QUERY_POOL_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateQueryPool, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+XGL_RESULT QueryPool::results(uint32_t start, uint32_t count, size_t size, void *data)
+{
+    size_t tmp = size;
+    XGL_RESULT err = xglGetQueryPoolResults(obj(), start, count, &tmp, data);
+    if (err == XGL_SUCCESS) {
+        if (!EXPECT(tmp == size))
+            memset(data, 0, size);
+    } else {
+        EXPECT(err == XGL_NOT_READY);
+    }
+
+    return err;
+}
+
+void Image::init(const Device &dev, const XGL_IMAGE_CREATE_INFO &info)
+{
+    init_no_mem(dev, info);
+    alloc_memory(dev, info.tiling == XGL_LINEAR_TILING);
+}
+
+void Image::init_no_mem(const Device &dev, const XGL_IMAGE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateImage, dev.obj(), &info);
+    init_info(dev, info);
+}
+
+void Image::init(const Device &dev, const XGL_PEER_IMAGE_OPEN_INFO &info, const XGL_IMAGE_CREATE_INFO &original_info)
+{
+    XGL_IMAGE img;
+    XGL_GPU_MEMORY mem;
+    EXPECT(xglOpenPeerImage(dev.obj(), &info, &img, &mem) == XGL_SUCCESS);
+    Object::init(img);
+
+    init_info(dev, original_info);
+    alloc_memory(std::vector<XGL_GPU_MEMORY>(1, mem));
+}
+
+void Image::init_info(const Device &dev, const XGL_IMAGE_CREATE_INFO &info)
+{
+    create_info_ = info;
+
+    for (std::vector<Device::Format>::const_iterator it = dev.formats().begin(); it != dev.formats().end(); it++) {
+        if (memcmp(&it->format, &create_info_.format, sizeof(it->format)) == 0 && it->tiling == create_info_.tiling) {
+            format_features_ = it->features;
+            break;
+        }
+    }
+}
+
+XGL_SUBRESOURCE_LAYOUT Image::subresource_layout(const XGL_IMAGE_SUBRESOURCE &subres) const
+{
+    const XGL_SUBRESOURCE_INFO_TYPE type = XGL_INFO_TYPE_SUBRESOURCE_LAYOUT;
+    XGL_SUBRESOURCE_LAYOUT data;
+    size_t size = sizeof(data);
+    if (!EXPECT(xglGetImageSubresourceInfo(obj(), &subres, type, &size, &data) == XGL_SUCCESS && size == sizeof(data)))
+        memset(&data, 0, sizeof(data));
+
+    return data;
+}
+
+bool Image::transparent() const
+{
+    return (create_info_.tiling == XGL_LINEAR_TILING &&
+            create_info_.samples == 1 &&
+            !(create_info_.usage & (XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
+                                    XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT)));
+}
+
+void ImageView::init(const Device &dev, const XGL_IMAGE_VIEW_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateImageView, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void ColorAttachmentView::init(const Device &dev, const XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateColorAttachmentView, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void DepthStencilView::init(const Device &dev, const XGL_DEPTH_STENCIL_VIEW_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateDepthStencilView, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void Shader::init(const Device &dev, const XGL_SHADER_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateShader, dev.obj(), &info);
+}
+
+XGL_RESULT Shader::init_try(const Device &dev, const XGL_SHADER_CREATE_INFO &info)
+{
+    XGL_SHADER sh;
+    XGL_RESULT err = xglCreateShader(dev.obj(), &info, &sh);
+    if (err == XGL_SUCCESS)
+        Object::init(sh);
+
+    return err;
+}
+
+void Pipeline::init(const Device &dev, const XGL_GRAPHICS_PIPELINE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateGraphicsPipeline, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void Pipeline::init(const Device &dev, const XGL_COMPUTE_PIPELINE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateComputePipeline, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void Pipeline::init(const Device&dev, size_t size, const void *data)
+{
+    DERIVED_OBJECT_INIT(xglLoadPipeline, dev.obj(), size, data);
+    alloc_memory(dev);
+}
+
+size_t Pipeline::store(size_t size, void *data)
+{
+    if (!EXPECT(xglStorePipeline(obj(), &size, data) == XGL_SUCCESS))
+        size = 0;
+
+    return size;
+}
+
+void PipelineDelta::init(const Device &dev, const Pipeline &p1, const Pipeline &p2)
+{
+    DERIVED_OBJECT_INIT(xglCreatePipelineDelta, dev.obj(), p1.obj(), p2.obj());
+    alloc_memory(dev);
+}
+
+void Sampler::init(const Device &dev, const XGL_SAMPLER_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateSampler, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void DescriptorSet::init(const Device &dev, const XGL_DESCRIPTOR_SET_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateDescriptorSet, dev.obj(), &info);
+    info_ = info;
+}
+
+void DescriptorSet::attach(uint32_t start_slot, const std::vector<const Sampler *> &samplers)
+{
+    const std::vector<XGL_SAMPLER> sampler_objs = make_objects<XGL_SAMPLER>(samplers);
+    xglAttachSamplerDescriptors(obj(), start_slot, sampler_objs.size(), &sampler_objs[0]);
+}
+
+void DescriptorSet::attach(uint32_t start_slot, const std::vector<XGL_IMAGE_VIEW_ATTACH_INFO> &img_views)
+{
+    xglAttachImageViewDescriptors(obj(), start_slot, img_views.size(), &img_views[0]);
+}
+
+void DescriptorSet::attach(uint32_t start_slot, const std::vector<XGL_MEMORY_VIEW_ATTACH_INFO> &mem_views)
+{
+    xglAttachMemoryViewDescriptors(obj(), start_slot, mem_views.size(), &mem_views[0]);
+}
+
+void DescriptorSet::attach(uint32_t start_slot, const std::vector<XGL_DESCRIPTOR_SET_ATTACH_INFO> &sets)
+{
+    xglAttachNestedDescriptors(obj(), start_slot, sets.size(), &sets[0]);
+}
+
+void DynamicVpStateObject::init(const Device &dev, const XGL_VIEWPORT_STATE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateViewportState, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void DynamicRsStateObject::init(const Device &dev, const XGL_RASTER_STATE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateRasterState, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void DynamicMsaaStateObject::init(const Device &dev, const XGL_MSAA_STATE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateMsaaState, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void DynamicCbStateObject::init(const Device &dev, const XGL_COLOR_BLEND_STATE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateColorBlendState, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void DynamicDsStateObject::init(const Device &dev, const XGL_DEPTH_STENCIL_STATE_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateDepthStencilState, dev.obj(), &info);
+    alloc_memory(dev);
+}
+
+void CmdBuffer::init(const Device &dev, const XGL_CMD_BUFFER_CREATE_INFO &info)
+{
+    DERIVED_OBJECT_INIT(xglCreateCommandBuffer, dev.obj(), &info);
+}
+
+void CmdBuffer::begin(XGL_FLAGS flags)
+{
+    EXPECT(xglBeginCommandBuffer(obj(), flags) == XGL_SUCCESS);
+}
+
+void CmdBuffer::begin()
+{
+    begin(XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
+          XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT);
+}
+
+void CmdBuffer::end()
+{
+    EXPECT(xglEndCommandBuffer(obj()) == XGL_SUCCESS);
+}
+
+void CmdBuffer::reset()
+{
+    EXPECT(xglResetCommandBuffer(obj()) == XGL_SUCCESS);
+}
+
+}; // namespace xgl_testing
diff --git a/tests/xgltestbinding.h b/tests/xgltestbinding.h
new file mode 100644
index 0000000..dfd73d5
--- /dev/null
+++ b/tests/xgltestbinding.h
@@ -0,0 +1,735 @@
+// XGL tests
+//
+// Copyright (C) 2014 LunarG, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+
+#ifndef XGLTESTBINDING_H
+#define XGLTESTBINDING_H
+
+#include <vector>
+
+#define XGL_PROTOTYPES
+#include "xgl.h"
+
+namespace xgl_testing {
+
+typedef void (*ErrorCallback)(const char *expr, const char *file, unsigned int line, const char *function);
+void set_error_callback(ErrorCallback callback);
+
+class PhysicalGpu;
+class BaseObject;
+class Object;
+class DynamicStateObject;
+class Device;
+class Queue;
+class GpuMemory;
+class Fence;
+class QueueSemaphore;
+class Event;
+class QueryPool;
+class Image;
+class ImageView;
+class ColorAttachmentView;
+class DepthStencilView;
+class Shader;
+class Pipeline;
+class PipelineDelta;
+class Sampler;
+class DescriptorSet;
+class DynamicVpStateObject;
+class DynamicRsStateObject;
+class DynamicMsaaStateObject;
+class DynamicCbStateObject;
+class DynamicDsStateObject;
+class CmdBuffer;
+
+class PhysicalGpu {
+public:
+    explicit PhysicalGpu(XGL_PHYSICAL_GPU gpu) : gpu_(gpu) {}
+
+    const XGL_PHYSICAL_GPU &obj() const { return gpu_; }
+
+    // xglGetGpuInfo()
+    XGL_PHYSICAL_GPU_PROPERTIES properties() const;
+    XGL_PHYSICAL_GPU_PERFORMANCE performance() const;
+    XGL_PHYSICAL_GPU_MEMORY_PROPERTIES memory_properties() const;
+    std::vector<XGL_PHYSICAL_GPU_QUEUE_PROPERTIES> queue_properties() const;
+
+    // xglGetProcAddr()
+    void *get_proc(const char *name) const { return xglGetProcAddr(gpu_, name); }
+
+    // xglGetExtensionSupport()
+    bool has_extension(const char *ext) const { return (xglGetExtensionSupport(gpu_, ext) == XGL_SUCCESS); }
+    std::vector<const char *> extensions() const;
+
+    // xglEnumerateLayers()
+    std::vector<const char *> layers(std::vector<char> &buf) const;
+
+    // xglGetMultiGpuCompatibility()
+    XGL_GPU_COMPATIBILITY_INFO compatibility(const PhysicalGpu &other) const;
+
+private:
+    XGL_PHYSICAL_GPU gpu_;
+};
+
+class BaseObject {
+public:
+    const XGL_BASE_OBJECT &obj() const { return obj_; }
+    bool initialized() const { return (obj_ != XGL_NULL_HANDLE); }
+
+    // xglGetObjectInfo()
+    uint32_t memory_allocation_count() const;
+    std::vector<XGL_MEMORY_REQUIREMENTS> memory_requirements() const;
+
+protected:
+    explicit BaseObject() : obj_(XGL_NULL_HANDLE), own_obj_(false) {}
+    explicit BaseObject(XGL_BASE_OBJECT obj) : obj_(XGL_NULL_HANDLE), own_obj_(false) { init(obj); }
+
+    void init(XGL_BASE_OBJECT obj, bool own);
+    void init(XGL_BASE_OBJECT obj) { init(obj, true); }
+
+    void reinit(XGL_BASE_OBJECT obj, bool own);
+    void reinit(XGL_BASE_OBJECT obj) { reinit(obj, true); }
+
+    bool own() const { return own_obj_; };
+
+private:
+    // base objects are non-copyable
+    BaseObject(const BaseObject &);
+    BaseObject &operator=(const BaseObject &);
+
+    XGL_BASE_OBJECT obj_;
+    bool own_obj_;
+};
+
+class Object : public BaseObject {
+public:
+    const XGL_OBJECT &obj() const { return reinterpret_cast<const XGL_OBJECT &>(BaseObject::obj()); }
+
+    // xglBindObjectMemory()
+    void bind_memory(uint32_t alloc_idx, const GpuMemory &mem, XGL_GPU_SIZE mem_offset);
+    void unbind_memory(uint32_t alloc_idx);
+    void unbind_memory();
+
+    // Unless an object is initialized with init_no_mem(), memories are
+    // automatically allocated and bound.  These methods can be used to get
+    // the memories (for XGL_MEMORY_REFs), or to map/unmap the primary memory.
+    std::vector<XGL_GPU_MEMORY> memories() const;
+
+    const void *map(XGL_FLAGS flags) const;
+          void *map(XGL_FLAGS flags);
+    const void *map() const { return map(0); }
+          void *map()       { return map(0); }
+
+    void unmap() const;
+
+protected:
+    explicit Object() : mem_alloc_count_(0), internal_mems_(NULL), primary_mem_(NULL) {}
+    explicit Object(XGL_OBJECT obj) : mem_alloc_count_(0), internal_mems_(NULL), primary_mem_(NULL) { init(obj); }
+    ~Object() { cleanup(); }
+
+    void init(XGL_OBJECT obj, bool own);
+    void init(XGL_OBJECT obj) { init(obj, true); }
+
+    void reinit(XGL_OBJECT obj, bool own);
+    void reinit(XGL_OBJECT obj) { init(obj, true); }
+
+    // allocate and bind internal memories
+    void alloc_memory(const Device &dev, bool for_linear_img);
+    void alloc_memory(const Device &dev) { alloc_memory(dev, false); }
+    void alloc_memory(const std::vector<XGL_GPU_MEMORY> &mems);
+
+private:
+    void cleanup();
+
+    uint32_t mem_alloc_count_;
+    GpuMemory *internal_mems_;
+    GpuMemory *primary_mem_;
+};
+
+class DynamicStateObject : public Object {
+public:
+    const XGL_STATE_OBJECT &obj() const { return reinterpret_cast<const XGL_STATE_OBJECT &>(Object::obj()); }
+
+protected:
+    explicit DynamicStateObject() {}
+    explicit DynamicStateObject(XGL_STATE_OBJECT obj) : Object(obj) {}
+};
+
+template<typename T, class C>
+class DerivedObject : public C {
+public:
+    const T &obj() const { return reinterpret_cast<const T &>(C::obj()); }
+
+protected:
+    typedef T obj_type;
+    typedef C base_type;
+
+    explicit DerivedObject() {}
+    explicit DerivedObject(T obj) : C(obj) {}
+};
+
+class Device : public DerivedObject<XGL_DEVICE, BaseObject> {
+public:
+    explicit Device(XGL_PHYSICAL_GPU gpu) : gpu_(gpu) {}
+    ~Device();
+
+    // xglCreateDevice()
+    void init(const XGL_DEVICE_CREATE_INFO &info);
+    void init(); // all queues, all layers, all extensions, etc
+
+    const PhysicalGpu &gpu() const { return gpu_; }
+
+    // xglGetDeviceQueue()
+    const std::vector<Queue *> &graphics_queues() { return queues_[GRAPHICS]; }
+    const std::vector<Queue *> &compute_queues() { return queues_[COMPUTE]; }
+    const std::vector<Queue *> &dma_queues() { return queues_[DMA]; }
+
+    const std::vector<XGL_MEMORY_HEAP_PROPERTIES> &heap_properties() const { return heap_props_; }
+
+    struct Format {
+        XGL_FORMAT format;
+        XGL_IMAGE_TILING tiling;
+        XGL_FLAGS features;
+    };
+    // xglGetFormatInfo()
+    XGL_FORMAT_PROPERTIES format_properties(XGL_FORMAT format);
+    const std::vector<Format> &formats() const { return formats_; }
+
+    // xglDeviceWaitIdle()
+    void wait();
+
+    // xglWaitForFences()
+    XGL_RESULT wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout);
+    XGL_RESULT wait(const Fence &fence) { return wait(std::vector<const Fence *>(1, &fence), true, (uint64_t) -1); }
+
+private:
+    enum QueueIndex {
+        GRAPHICS,
+        COMPUTE,
+        DMA,
+        QUEUE_COUNT,
+    };
+
+    void init_queues();
+    void init_heap_props();
+    void init_formats();
+
+    PhysicalGpu gpu_;
+
+    std::vector<Queue *> queues_[QUEUE_COUNT];
+    std::vector<XGL_MEMORY_HEAP_PROPERTIES> heap_props_;
+    std::vector<Format> formats_;
+};
+
+class Queue : public DerivedObject<XGL_QUEUE, BaseObject> {
+public:
+    explicit Queue(XGL_QUEUE queue) : DerivedObject(queue) {}
+
+    // xglQueueSubmit()
+    void submit(const std::vector<const CmdBuffer *> &cmds, const std::vector<XGL_MEMORY_REF> &mem_refs, Fence &fence);
+    void submit(const CmdBuffer &cmd, const std::vector<XGL_MEMORY_REF> &mem_refs, Fence &fence);
+    void submit(const CmdBuffer &cmd, const std::vector<XGL_MEMORY_REF> &mem_refs);
+
+    // xglQueueSetGlobalMemReferences()
+    void set_global_mem_references(const std::vector<XGL_MEMORY_REF> &mem_refs);
+
+    // xglQueueWaitIdle()
+    void wait();
+
+    // xglSignalQueueSemaphore()
+    // xglWaitQueueSemaphore()
+    void signal_semaphore(QueueSemaphore &sem);
+    void wait_semaphore(QueueSemaphore &sem);
+};
+
+class GpuMemory : public DerivedObject<XGL_GPU_MEMORY, BaseObject> {
+public:
+    explicit GpuMemory() {}
+    explicit GpuMemory(const Device &dev, XGL_GPU_SIZE size) { init(dev, size); }
+    ~GpuMemory();
+
+    // xglAllocMemory()
+    void init(const Device &dev, const XGL_MEMORY_ALLOC_INFO &info);
+    void init(const Device &dev, XGL_GPU_SIZE size);
+    // xglPinSystemMemory()
+    void init(const Device &dev, size_t size, const void *data);
+    // xglOpenSharedMemory()
+    void init(const Device &dev, const XGL_MEMORY_OPEN_INFO &info);
+    // xglOpenPeerMemory()
+    void init(const Device &dev, const XGL_PEER_MEMORY_OPEN_INFO &info);
+
+    void init(XGL_GPU_MEMORY mem) { BaseObject::init(mem, false); }
+
+    // xglSetMemoryPriority()
+    void set_priority(XGL_MEMORY_PRIORITY priority);
+
+    // xglMapMemory()
+    const void *map(XGL_FLAGS flags) const;
+          void *map(XGL_FLAGS flags);
+    const void *map() const { return map(0); }
+          void *map()       { return map(0); }
+
+    // xglUnmapMemory()
+    void unmap() const;
+
+    XGL_MEMORY_STATE_TRANSITION state_transition(XGL_MEMORY_STATE old_state, XGL_MEMORY_STATE new_state,
+                                                 XGL_GPU_SIZE offset, XGL_GPU_SIZE size) const
+    {
+        XGL_MEMORY_STATE_TRANSITION transition = {};
+        transition.sType = XGL_STRUCTURE_TYPE_MEMORY_STATE_TRANSITION;
+        transition.mem = obj();
+        transition.oldState = old_state;
+        transition.newState = new_state;
+        transition.offset = offset;
+        transition.regionSize = size;
+        return transition;
+    }
+
+    static XGL_MEMORY_ALLOC_INFO alloc_info(const XGL_MEMORY_REQUIREMENTS &reqs);
+};
+
+class Fence : public DerivedObject<XGL_FENCE, Object> {
+public:
+    // xglCreateFence()
+    void init(const Device &dev, const XGL_FENCE_CREATE_INFO &info);
+
+    // xglGetFenceStatus()
+    XGL_RESULT status() const { return xglGetFenceStatus(obj()); }
+
+    static XGL_FENCE_CREATE_INFO create_info(XGL_FLAGS flags);
+};
+
+class QueueSemaphore : public DerivedObject<XGL_QUEUE_SEMAPHORE, Object> {
+public:
+    // xglCreateQueueSemaphore()
+    void init(const Device &dev, const XGL_QUEUE_SEMAPHORE_CREATE_INFO &info);
+    // xglOpenSharedQueueSemaphore()
+    void init(const Device &dev, const XGL_QUEUE_SEMAPHORE_OPEN_INFO &info);
+
+    static XGL_QUEUE_SEMAPHORE_CREATE_INFO create_info(uint32_t init_count, XGL_FLAGS flags);
+};
+
+class Event : public DerivedObject<XGL_EVENT, Object> {
+public:
+    // xglCreateEvent()
+    void init(const Device &dev, const XGL_EVENT_CREATE_INFO &info);
+
+    // xglGetEventStatus()
+    // xglSetEvent()
+    // xglResetEvent()
+    XGL_RESULT status() const { return xglGetEventStatus(obj()); }
+    void set();
+    void reset();
+
+    static XGL_EVENT_CREATE_INFO create_info(XGL_FLAGS flags);
+};
+
+class QueryPool : public DerivedObject<XGL_QUERY_POOL, Object> {
+public:
+    // xglCreateQueryPool()
+    void init(const Device &dev, const XGL_QUERY_POOL_CREATE_INFO &info);
+
+    // xglGetQueryPoolResults()
+    XGL_RESULT results(uint32_t start, uint32_t count, size_t size, void *data);
+
+    static XGL_QUERY_POOL_CREATE_INFO create_info(XGL_QUERY_TYPE type, uint32_t slot_count);
+};
+
+class Image : public DerivedObject<XGL_IMAGE, Object> {
+public:
+    explicit Image() : format_features_(0) {}
+    explicit Image(const Device &dev, const XGL_IMAGE_CREATE_INFO &info) : format_features_(0) { init(dev, info); }
+
+    // xglCreateImage()
+    void init(const Device &dev, const XGL_IMAGE_CREATE_INFO &info);
+    void init_no_mem(const Device &dev, const XGL_IMAGE_CREATE_INFO &info);
+    // xglOpenPeerImage()
+    void init(const Device &dev, const XGL_PEER_IMAGE_OPEN_INFO &info, const XGL_IMAGE_CREATE_INFO &original_info);
+
+    // xglGetImageSubresourceInfo()
+    XGL_SUBRESOURCE_LAYOUT subresource_layout(const XGL_IMAGE_SUBRESOURCE &subres) const;
+
+    bool transparent() const;
+    bool copyable() const { return (format_features_ & XGL_FORMAT_IMAGE_COPY_BIT); }
+
+    XGL_IMAGE_SUBRESOURCE_RANGE subresource_range(XGL_IMAGE_ASPECT aspect) const { return subresource_range(create_info_, aspect); }
+    XGL_EXTENT3D extent() const { return create_info_.extent; }
+    XGL_EXTENT3D extent(uint32_t mip_level) const { return extent(create_info_.extent, mip_level); }
+
+    XGL_IMAGE_STATE_TRANSITION state_transition(XGL_IMAGE_STATE old_state, XGL_IMAGE_STATE new_state,
+                                                const XGL_IMAGE_SUBRESOURCE_RANGE &range) const
+    {
+        XGL_IMAGE_STATE_TRANSITION transition = {};
+        transition.image = obj();
+        transition.oldState = old_state;
+        transition.newState = new_state;
+        transition.subresourceRange = range;
+        return transition;
+    }
+
+    static XGL_IMAGE_CREATE_INFO create_info();
+    static XGL_IMAGE_SUBRESOURCE subresource(XGL_IMAGE_ASPECT aspect, uint32_t mip_level, uint32_t array_slice);
+    static XGL_IMAGE_SUBRESOURCE subresource(const XGL_IMAGE_SUBRESOURCE_RANGE &range, uint32_t mip_level, uint32_t array_slice);
+    static XGL_IMAGE_SUBRESOURCE_RANGE subresource_range(XGL_IMAGE_ASPECT aspect, uint32_t base_mip_level, uint32_t mip_levels,
+                                                                                  uint32_t base_array_slice, uint32_t array_size);
+    static XGL_IMAGE_SUBRESOURCE_RANGE subresource_range(const XGL_IMAGE_CREATE_INFO &info, XGL_IMAGE_ASPECT aspect);
+    static XGL_IMAGE_SUBRESOURCE_RANGE subresource_range(const XGL_IMAGE_SUBRESOURCE &subres);
+
+    static XGL_EXTENT2D extent(int32_t width, int32_t height);
+    static XGL_EXTENT2D extent(const XGL_EXTENT2D &extent, uint32_t mip_level);
+    static XGL_EXTENT2D extent(const XGL_EXTENT3D &extent);
+
+    static XGL_EXTENT3D extent(int32_t width, int32_t height, int32_t depth);
+    static XGL_EXTENT3D extent(const XGL_EXTENT3D &extent, uint32_t mip_level);
+
+private:
+    void init_info(const Device &dev, const XGL_IMAGE_CREATE_INFO &info);
+
+    XGL_IMAGE_CREATE_INFO create_info_;
+    XGL_FLAGS format_features_;
+};
+
+class ImageView : public DerivedObject<XGL_IMAGE_VIEW, Object> {
+public:
+    // xglCreateImageView()
+    void init(const Device &dev, const XGL_IMAGE_VIEW_CREATE_INFO &info);
+};
+
+class ColorAttachmentView : public DerivedObject<XGL_COLOR_ATTACHMENT_VIEW, Object> {
+public:
+    // xglCreateColorAttachmentView()
+    void init(const Device &dev, const XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO &info);
+};
+
+class DepthStencilView : public DerivedObject<XGL_DEPTH_STENCIL_VIEW, Object> {
+public:
+    // xglCreateDepthStencilView()
+    void init(const Device &dev, const XGL_DEPTH_STENCIL_VIEW_CREATE_INFO &info);
+};
+
+class Shader : public DerivedObject<XGL_SHADER, Object> {
+public:
+    // xglCreateShader()
+    void init(const Device &dev, const XGL_SHADER_CREATE_INFO &info);
+    XGL_RESULT init_try(const Device &dev, const XGL_SHADER_CREATE_INFO &info);
+
+    static XGL_SHADER_CREATE_INFO create_info(size_t code_size, const void *code, XGL_FLAGS flags);
+};
+
+class Pipeline : public DerivedObject<XGL_PIPELINE, Object> {
+public:
+    // xglCreateGraphicsPipeline()
+    void init(const Device &dev, const XGL_GRAPHICS_PIPELINE_CREATE_INFO &info);
+    // xglCreateComputePipeline()
+    void init(const Device &dev, const XGL_COMPUTE_PIPELINE_CREATE_INFO &info);
+    // xglLoadPipeline()
+    void init(const Device&dev, size_t size, const void *data);
+
+    // xglStorePipeline()
+    size_t store(size_t size, void *data);
+};
+
+class PipelineDelta : public DerivedObject<XGL_PIPELINE_DELTA, Object> {
+public:
+    // xglCreatePipelineDelta()
+    void init(const Device &dev, const Pipeline &p1, const Pipeline &p2);
+};
+
+class Sampler : public DerivedObject<XGL_SAMPLER, Object> {
+public:
+    // xglCreateSampler()
+    void init(const Device &dev, const XGL_SAMPLER_CREATE_INFO &info);
+};
+
+class DescriptorSet : public DerivedObject<XGL_DESCRIPTOR_SET, Object> {
+public:
+    // xglCreateDescriptorSet()
+    void init(const Device &dev, const XGL_DESCRIPTOR_SET_CREATE_INFO &info);
+
+    // xglBeginDescriptorSetUpdate()
+    // xglEndDescriptorSetUpdate()
+    void begin() { xglBeginDescriptorSetUpdate(obj()); }
+    void end() { xglEndDescriptorSetUpdate(obj()); }
+
+    // xglAttachSamplerDescriptors()
+    void attach(uint32_t start_slot, const std::vector<const Sampler *> &samplers);
+    void attach(uint32_t start_slot, const Sampler &sampler)
+    {
+        attach(start_slot, std::vector<const Sampler *>(1, &sampler));
+    }
+
+    // xglAttachImageViewDescriptors()
+    void attach(uint32_t start_slot, const std::vector<XGL_IMAGE_VIEW_ATTACH_INFO> &img_views);
+    void attach(uint32_t start_slot, const XGL_IMAGE_VIEW_ATTACH_INFO &view)
+    {
+        attach(start_slot, std::vector<XGL_IMAGE_VIEW_ATTACH_INFO>(1, view));
+    }
+
+    // xglAttachMemoryViewDescriptors()
+    void attach(uint32_t start_slot, const std::vector<XGL_MEMORY_VIEW_ATTACH_INFO> &mem_views);
+    void attach(uint32_t start_slot, const XGL_MEMORY_VIEW_ATTACH_INFO &view)
+    {
+        attach(start_slot, std::vector<XGL_MEMORY_VIEW_ATTACH_INFO>(1, view));
+    }
+
+    // xglAttachNestedDescriptors()
+    void attach(uint32_t start_slot, const std::vector<XGL_DESCRIPTOR_SET_ATTACH_INFO> &sets);
+    void attach(uint32_t start_slot, const XGL_DESCRIPTOR_SET_ATTACH_INFO &set)
+    {
+        attach(start_slot, std::vector<XGL_DESCRIPTOR_SET_ATTACH_INFO>(1, set));
+    }
+
+    //  xglClearDescriptorSetSlots()
+    void clear(uint32_t start_slot, uint32_t count) { xglClearDescriptorSetSlots(obj(), start_slot, count); }
+    void clear() { clear(0, info_.slots); }
+
+    static XGL_DESCRIPTOR_SET_CREATE_INFO create_info(uint32_t slot_count)
+    {
+        XGL_DESCRIPTOR_SET_CREATE_INFO info = {};
+        info.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
+        info.slots = slot_count;
+        return info;
+    }
+
+private:
+    XGL_DESCRIPTOR_SET_CREATE_INFO info_;
+};
+
+class DynamicVpStateObject : public DerivedObject<XGL_VIEWPORT_STATE_OBJECT, DynamicStateObject> {
+public:
+    // xglCreateViewportState()
+    void init(const Device &dev, const XGL_VIEWPORT_STATE_CREATE_INFO &info);
+};
+
+class DynamicRsStateObject : public DerivedObject<XGL_RASTER_STATE_OBJECT, DynamicStateObject> {
+public:
+    // xglCreateRasterState()
+    void init(const Device &dev, const XGL_RASTER_STATE_CREATE_INFO &info);
+};
+
+class DynamicMsaaStateObject : public DerivedObject<XGL_MSAA_STATE_OBJECT, DynamicStateObject> {
+public:
+    // xglCreateMsaaState()
+    void init(const Device &dev, const XGL_MSAA_STATE_CREATE_INFO &info);
+};
+
+class DynamicCbStateObject : public DerivedObject<XGL_COLOR_BLEND_STATE_OBJECT, DynamicStateObject> {
+public:
+    // xglCreateColorBlendState()
+    void init(const Device &dev, const XGL_COLOR_BLEND_STATE_CREATE_INFO &info);
+};
+
+class DynamicDsStateObject : public DerivedObject<XGL_DEPTH_STENCIL_STATE_OBJECT, DynamicStateObject> {
+public:
+    // xglCreateDepthStencilState()
+    void init(const Device &dev, const XGL_DEPTH_STENCIL_STATE_CREATE_INFO &info);
+};
+
+class CmdBuffer : public DerivedObject<XGL_CMD_BUFFER, Object> {
+public:
+    explicit CmdBuffer() {}
+    explicit CmdBuffer(const Device &dev, const XGL_CMD_BUFFER_CREATE_INFO &info) { init(dev, info); }
+
+    // xglCreateCommandBuffer()
+    void init(const Device &dev, const XGL_CMD_BUFFER_CREATE_INFO &info);
+
+    // xglBeginCommandBuffer()
+    void begin(XGL_FLAGS flags);
+    void begin();
+
+    // xglEndCommandBuffer()
+    // xglResetCommandBuffer()
+    void end();
+    void reset();
+
+    static XGL_CMD_BUFFER_CREATE_INFO create_info(XGL_QUEUE_TYPE type);
+};
+
+inline const void *Object::map(XGL_FLAGS flags) const
+{
+    return (primary_mem_) ? primary_mem_->map(flags) : NULL;
+}
+
+inline void *Object::map(XGL_FLAGS flags)
+{
+    return (primary_mem_) ? primary_mem_->map(flags) : NULL;
+}
+
+inline void Object::unmap() const
+{
+    if (primary_mem_)
+        primary_mem_->unmap();
+}
+
+inline XGL_MEMORY_ALLOC_INFO GpuMemory::alloc_info(const XGL_MEMORY_REQUIREMENTS &reqs)
+{
+    XGL_MEMORY_ALLOC_INFO info = {};
+    info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
+    info.allocationSize = reqs.size;
+    info.alignment = reqs.alignment;
+    info.heapCount = reqs.heapCount;
+    for (int i = 0; i < reqs.heapCount; i++)
+        info.heaps[i] = reqs.heaps[i];
+    info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
+    return info;
+}
+
+inline XGL_FENCE_CREATE_INFO Fence::create_info(XGL_FLAGS flags)
+{
+    XGL_FENCE_CREATE_INFO info = {};
+    info.sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO;
+    info.flags = flags;
+    return info;
+}
+
+inline XGL_QUEUE_SEMAPHORE_CREATE_INFO QueueSemaphore::create_info(uint32_t init_count, XGL_FLAGS flags)
+{
+    XGL_QUEUE_SEMAPHORE_CREATE_INFO info = {};
+    info.sType = XGL_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
+    info.initialCount = init_count;
+    info.flags = flags;
+    return info;
+}
+
+inline XGL_EVENT_CREATE_INFO Event::create_info(XGL_FLAGS flags)
+{
+    XGL_EVENT_CREATE_INFO info = {};
+    info.sType = XGL_STRUCTURE_TYPE_EVENT_CREATE_INFO;
+    info.flags = flags;
+    return info;
+}
+
+inline XGL_QUERY_POOL_CREATE_INFO QueryPool::create_info(XGL_QUERY_TYPE type, uint32_t slot_count)
+{
+    XGL_QUERY_POOL_CREATE_INFO info = {};
+    info.sType = XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
+    info.queryType = type;
+    info.slots = slot_count;
+    return info;
+}
+
+inline XGL_IMAGE_CREATE_INFO Image::create_info()
+{
+    XGL_IMAGE_CREATE_INFO info = {};
+    info.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
+    info.extent.width = 1;
+    info.extent.height = 1;
+    info.extent.depth = 1;
+    info.mipLevels = 1;
+    info.arraySize = 1;
+    info.samples = 1;
+    return info;
+}
+
+inline XGL_IMAGE_SUBRESOURCE Image::subresource(XGL_IMAGE_ASPECT aspect, uint32_t mip_level, uint32_t array_slice)
+{
+    XGL_IMAGE_SUBRESOURCE subres = {};
+    subres.aspect = aspect;
+    subres.mipLevel = mip_level;
+    subres.arraySlice = array_slice;
+    return subres;
+}
+
+inline XGL_IMAGE_SUBRESOURCE Image::subresource(const XGL_IMAGE_SUBRESOURCE_RANGE &range, uint32_t mip_level, uint32_t array_slice)
+{
+    return subresource(range.aspect, range.baseMipLevel + mip_level, range.baseArraySlice + array_slice);
+}
+
+inline XGL_IMAGE_SUBRESOURCE_RANGE Image::subresource_range(XGL_IMAGE_ASPECT aspect, uint32_t base_mip_level, uint32_t mip_levels,
+                                                                                     uint32_t base_array_slice, uint32_t array_size)
+{
+    XGL_IMAGE_SUBRESOURCE_RANGE range = {};
+    range.aspect = aspect;
+    range.baseMipLevel = base_mip_level;
+    range.mipLevels = mip_levels;
+    range.baseArraySlice = base_array_slice;
+    range.arraySize = array_size;
+    return range;
+}
+
+inline XGL_IMAGE_SUBRESOURCE_RANGE Image::subresource_range(const XGL_IMAGE_CREATE_INFO &info, XGL_IMAGE_ASPECT aspect)
+{
+    return subresource_range(aspect, 0, info.mipLevels, 0, info.arraySize);
+}
+
+inline XGL_IMAGE_SUBRESOURCE_RANGE Image::subresource_range(const XGL_IMAGE_SUBRESOURCE &subres)
+{
+    return subresource_range(subres.aspect, subres.mipLevel, 1, subres.arraySlice, 1);
+}
+
+inline XGL_EXTENT2D Image::extent(int32_t width, int32_t height)
+{
+    XGL_EXTENT2D extent = {};
+    extent.width = width;
+    extent.height = height;
+    return extent;
+}
+
+inline XGL_EXTENT2D Image::extent(const XGL_EXTENT2D &extent, uint32_t mip_level)
+{
+    const int32_t width  = (extent.width  >> mip_level) ? extent.width  >> mip_level : 1;
+    const int32_t height = (extent.height >> mip_level) ? extent.height >> mip_level : 1;
+    return Image::extent(width, height);
+}
+
+inline XGL_EXTENT2D Image::extent(const XGL_EXTENT3D &extent)
+{
+    return Image::extent(extent.width, extent.height);
+}
+
+inline XGL_EXTENT3D Image::extent(int32_t width, int32_t height, int32_t depth)
+{
+    XGL_EXTENT3D extent = {};
+    extent.width = width;
+    extent.height = height;
+    extent.depth = depth;
+    return extent;
+}
+
+inline XGL_EXTENT3D Image::extent(const XGL_EXTENT3D &extent, uint32_t mip_level)
+{
+    const int32_t width  = (extent.width  >> mip_level) ? extent.width  >> mip_level : 1;
+    const int32_t height = (extent.height >> mip_level) ? extent.height >> mip_level : 1;
+    const int32_t depth  = (extent.depth  >> mip_level) ? extent.depth  >> mip_level : 1;
+    return Image::extent(width, height, depth);
+}
+
+inline XGL_SHADER_CREATE_INFO Shader::create_info(size_t code_size, const void *code, XGL_FLAGS flags)
+{
+    XGL_SHADER_CREATE_INFO info = {};
+    info.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
+    info.codeSize = code_size;
+    info.pCode = code;
+    info.flags = flags;
+    return info;
+}
+
+inline XGL_CMD_BUFFER_CREATE_INFO CmdBuffer::create_info(XGL_QUEUE_TYPE type)
+{
+    XGL_CMD_BUFFER_CREATE_INFO info = {};
+    info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
+    info.queueType = type;
+    return info;
+}
+
+}; // namespace xgl_testing
+
+#endif // XGLTESTBINDING_H