#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <utility>

import wgpu;

wgpu::Instance instance;
wgpu::Adapter adapter;

int main() {
    static constexpr auto kTimedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
    wgpu::InstanceDescriptor instanceDescriptor;
    instanceDescriptor.requiredFeatureCount = 1;
    instanceDescriptor.requiredFeatures = &kTimedWaitAny;
    instance = wgpu::CreateInstance(&instanceDescriptor);

    wgpu::RequestAdapterOptions options = {};
    instance.WaitAny(
        instance.RequestAdapter(&options, wgpu::CallbackMode::WaitAnyOnly,
            [](wgpu::RequestAdapterStatus status, wgpu::Adapter a, wgpu::StringView message) {
                if (status != wgpu::RequestAdapterStatus::Success) {
                    std::fprintf(stderr, "RequestAdapter failed: %.*s\n",
                                 static_cast<int>(message.length), message.data);
                    std::exit(1);
                }
                adapter = std::move(a);
            }),
        std::numeric_limits<std::uint64_t>::max());

    if (adapter == nullptr) {
        std::fprintf(stderr, "RequestAdapter failed!\n");
        std::exit(1);
    }

    wgpu::AdapterInfo info;
    adapter.GetInfo(&info);

    std::printf("device:       %.*s\n", static_cast<int>(info.device.length), info.device.data);
    std::printf("description:  %.*s\n", static_cast<int>(info.description.length), info.description.data);
    std::printf("architecture: %.*s\n", static_cast<int>(info.architecture.length), info.architecture.data);
    std::printf("vendor:       %.*s\n", static_cast<int>(info.vendor.length), info.vendor.data);
    std::printf("vendorID:     %u\n", info.vendorID);
    std::printf("deviceID:     %u\n", info.deviceID);
    std::printf("backend:      %u\n", static_cast<std::uint32_t>(info.backendType));
    std::printf("adapterType:  %u\n", static_cast<std::uint32_t>(info.adapterType));
    std::printf("subgroupSize: %u (min) - %u (max)\n", info.subgroupMinSize, info.subgroupMaxSize);
}
