gfx-rs / wgpu-native

Native WebGPU implementation based on wgpu-core
Apache License 2.0
871 stars 104 forks source link

wgpuInstanceRequestAdapter fails when called with a specific backend #337

Open vehsakul opened 10 months ago

vehsakul commented 10 months ago
  WGPURequestAdapterOptions requestAdapterOptions;
  requestAdapterOptions.nextInChain = nullptr;
  requestAdapterOptions.compatibleSurface = surface_;
  requestAdapterOptions.powerPreference = WGPUPowerPreference_HighPerformance;
  requestAdapterOptions.forceFallbackAdapter = true;
  requestAdapterOptions.backendType = WGPUBackendType_D3D12;

result:

Validation Error

Caused by:
    No suitable adapter found

surface_ was created by SDL2 library, it used to work using the last wgpu-native release from github

Capati commented 9 months ago

I had this issue too, forcing backendType = WGPUBackendType_D3D12 rises the same error.

laurelkeys commented 9 months ago

Same error when updating from v0.18.1.4 to v0.19.1.1. As mentioned above, it's happening with WGPUBackendType_D3D12 (WGPUBackendType_Vulkan works fine).

rajveermalviya commented 9 months ago

Unfortunately I am unable to reproduce this behavior, does it fail if backendType is set to WGPUBackendType_Undefined?

laurelkeys commented 9 months ago

WGPUBackendType_Undefined works ok, and ends up choosing the Vulkan backend.

Using wgpuInstanceEnumerateAdapters I have Vulkan, Dx12 and Gl backends. Passing .backendType = WGPUBackendType_D3D12, WGPUBackendType_OpenGL and WGPUBackendType_WebGPU (not sure what's expected for WebGPU, but I tried all values) will result in "No suitable adapter found".

This should be a minimal example that repros it:

int main(int argc, char **argv)
{
    WGPUInstance instance = wgpuCreateInstance(nullptr);
    assert(instance);

    // Note: uncomment to print available adapters (requires `wgpuSetLogLevel(WGPULogLevel_Info)`).
    // const WGPUInstanceEnumerateAdapterOptions options = { .backends = WGPUInstanceBackend_All };
    // const size_t count = wgpuInstanceEnumerateAdapters(instance, &options, nullptr);
    // assert(count > 0);

    const WGPURequestAdapterOptions options { .backendType = WGPUBackendType_D3D12 }; // !!
    WGPUAdapter adapter = nullptr;
    wgpuInstanceRequestAdapter(
        instance,
        &options,
        [](WGPURequestAdapterStatus status, WGPUAdapter adapter, char const *message, void *userdata) {
            if (status == WGPURequestAdapterStatus_Success) {
                *reinterpret_cast<WGPUAdapter *>(userdata) = adapter;
            }
        },
        /* userdata */ static_cast<void *>(&adapter));
    assert(adapter);
}
rajveermalviya commented 8 months ago

This is a bug upstream https://github.com/gfx-rs/wgpu/issues/5289

Workaround for now is provide the desired backend flag/s when creating the instance rather than when requesting the adapter.

WGPUInstanceExtras instanceExtras = { 0 };
instanceExtras.chain.sType = (WGPUSType)WGPUSType_InstanceExtras;
instanceExtras.backends = WGPUInstanceBackend_DX12;

WGPUInstanceDescriptor instanceDescriptor = { 0 };
instanceDescriptor.nextInChain = &instanceExtras.chain;

WGPUInstance instance = wgpuCreateInstance(&instanceDescriptor);