Skip to content

Commit

Permalink
fix WGPUInstanceEnumerateAdapterOptions, `wgpuSwapChainGetCurrentTe…
Browse files Browse the repository at this point in the history
…xtureView` & `wgpuDevicePopErrorScope` (gfx-rs#276)
  • Loading branch information
rajveermalviya authored and Beyley committed Jul 15, 2023
1 parent b27189b commit 1fe3f99
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
31 changes: 31 additions & 0 deletions examples/triangle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(WGPU_TARGET_MACOS)
#include <Foundation/Foundation.h>
Expand Down Expand Up @@ -31,6 +32,7 @@ struct demo {
WGPUDevice device;
WGPUSwapChainDescriptor config;
WGPUSwapChain swapchain;
bool skip_curr_frame;
};

static void handle_request_adapter(WGPURequestAdapterStatus status,
Expand Down Expand Up @@ -88,6 +90,27 @@ static void handle_glfw_framebuffer_size(GLFWwindow *window, int width,
wgpuDeviceCreateSwapChain(demo->device, demo->surface, &demo->config);
assert(demo->swapchain);
}
static void handle_curr_texture_error(WGPUErrorType type, char const *message,
void *userdata) {
if (type == WGPUErrorType_NoError)
return;

printf(LOG_PREFIX " curr_texture_error type=%#.8x message=%s\n", type,
message);

struct demo *demo = userdata;

if (strstr(message, "Surface timed out") != NULL) {
demo->skip_curr_frame = true;
return;
} else if (strstr(message, "Surface is outdated") != NULL) {
demo->skip_curr_frame = true;
return;
} else if (strstr(message, "Surface was lost") != NULL) {
demo->skip_curr_frame = true;
return;
}
}

int main(int argc, char *argv[]) {
UNUSED(argc)
Expand Down Expand Up @@ -295,10 +318,18 @@ int main(int argc, char *argv[]) {
ASSERT_CHECK(demo.swapchain);

while (!glfwWindowShouldClose(window)) {
demo.skip_curr_frame = false;
glfwPollEvents();

wgpuDevicePushErrorScope(demo.device, WGPUErrorFilter_Validation);
WGPUTextureView next_texture =
wgpuSwapChainGetCurrentTextureView(demo.swapchain);
wgpuDevicePopErrorScope(demo.device, handle_curr_texture_error, &demo);
if (demo.skip_curr_frame) {
if (next_texture)
wgpuTextureViewRelease(next_texture);
continue;
}
assert(next_texture);

WGPUCommandEncoder command_encoder = wgpuDeviceCreateCommandEncoder(
Expand Down
2 changes: 1 addition & 1 deletion ffi/wgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ typedef struct WGPUSwapChainDescriptorExtras {
} WGPUSwapChainDescriptorExtras;

typedef struct WGPUInstanceEnumerateAdapterOptions {
WGPUChainedStruct chain;
WGPUChainedStruct const * nextInChain;
WGPUInstanceBackendFlags backends;
} WGPUInstanceEnumerateAdapterOptions;

Expand Down
34 changes: 22 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2574,20 +2574,27 @@ pub unsafe extern "C" fn wgpuDevicePopErrorScope(
let mut error_sink = device.error_sink.lock();
let scope = error_sink.scopes.pop().unwrap();

if let Some(error) = scope.error {
let typ = match error {
crate::Error::OutOfMemory { .. } => native::WGPUErrorType_OutOfMemory,
crate::Error::Validation { .. } => native::WGPUErrorType_Validation,
// We handle device lost error early in ErrorSinkRaw::handle_error
// so we should never get device lost error here.
crate::Error::DeviceLost { .. } => unreachable!(),
};
match scope.error {
Some(error) => {
let typ = match error {
crate::Error::OutOfMemory { .. } => native::WGPUErrorType_OutOfMemory,
crate::Error::Validation { .. } => native::WGPUErrorType_Validation,
// We handle device lost error early in ErrorSinkRaw::handle_error
// so we should never get device lost error here.
crate::Error::DeviceLost { .. } => unreachable!(),
};

let msg = CString::new(error.to_string()).unwrap();
unsafe {
callback(typ, msg.as_ptr(), userdata);
let msg = CString::new(error.to_string()).unwrap();
unsafe {
callback(typ, msg.as_ptr(), userdata);
};
}
}
None => {
unsafe {
callback(native::WGPUErrorType_NoError, std::ptr::null(), userdata);
};
}
};
}

#[no_mangle]
Expand Down Expand Up @@ -3689,6 +3696,9 @@ pub unsafe extern "C" fn wgpuSwapChainGetCurrentTextureView(
}))
}
_ => {
if let Some(texture_id) = result.texture_id {
gfx_select!(texture_id => context.texture_drop(texture_id, false));
}
handle_error(
context,
error_sink,
Expand Down

0 comments on commit 1fe3f99

Please sign in to comment.