Skip to content

Fix GPUError message persistence in device.popErrorScope() #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/webgpu/cpp/rnwgpu/api/GPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ GPU::requestAdapter(
std::unordered_set<std::string> GPU::getWgslLanguageFeatures() {
wgpu::SupportedWGSLLanguageFeatures supportedFeatures = {};
_instance.GetWGSLLanguageFeatures(&supportedFeatures);

std::unordered_set<std::string> result;
for (size_t i = 0; i < supportedFeatures.featureCount; i++) {
wgpu::WGSLLanguageFeatureName feature = supportedFeatures.features[i];
Expand Down
10 changes: 6 additions & 4 deletions packages/webgpu/cpp/rnwgpu/api/GPUError.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <memory>
#include <string>

#include "webgpu/webgpu_cpp.h"

Expand All @@ -13,10 +14,10 @@ class GPUError {

public:
GPUError(wgpu::ErrorType aType, char const *aMessage)
: type(aType), message(aMessage) {}
: type(aType), message(aMessage ? aMessage : "") {}

wgpu::ErrorType type;
char const *message;
std::string message;
};

} // namespace rnwgpu
Expand All @@ -33,8 +34,9 @@ template <> struct JSIConverter<std::shared_ptr<rnwgpu::GPUError>> {
static jsi::Value toJSI(jsi::Runtime &runtime,
std::shared_ptr<rnwgpu::GPUError> arg) {
jsi::Object result(runtime);
result.setProperty(runtime, "message",
jsi::String::createFromUtf8(runtime, arg->message));
result.setProperty(
runtime, "message",
jsi::String::createFromUtf8(runtime, arg->message.c_str()));
return result;
}
};
Expand Down
5 changes: 3 additions & 2 deletions packages/webgpu/cpp/rnwgpu/api/descriptors/Unions.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ static void convertJSUnionToEnum(const std::string &inUnion,
} else if (inUnion == "dawn-native") {
*outEnum = wgpu::FeatureName::DawnNative;
} else if (inUnion == "chromium-experimental-timestamp-query-inside-passes") {
*outEnum = wgpu::FeatureName::ChromiumExperimentalTimestampQueryInsidePasses;
*outEnum =
wgpu::FeatureName::ChromiumExperimentalTimestampQueryInsidePasses;
} else if (inUnion == "implicit-device-synchronization") {
*outEnum = wgpu::FeatureName::ImplicitDeviceSynchronization;
} else if (inUnion == "transient-attachments") {
Expand Down Expand Up @@ -613,7 +614,7 @@ static void convertEnumToJSUnion(wgpu::FeatureName inEnum,
case wgpu::FeatureName::ANGLETextureSharing:
*outUnion = "angle-texture-sharing";
break;
case wgpu::FeatureName::ChromiumExperimentalSubgroupMatrix:
case wgpu::FeatureName::ChromiumExperimentalSubgroupMatrix:
*outUnion = "chromium-experimental-subgroups-matrix";
break;
case wgpu::FeatureName::PixelLocalStorageCoherent:
Expand Down
70 changes: 70 additions & 0 deletions packages/webgpu/src/__tests__/ErrorScope.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { client } from "./setup";

describe("Error Scope", () => {
it("should capture and return error messages from popErrorScope", async () => {
const result = await client.eval(({ device }) => {
// Invalid WGSL shader with syntax error (missing closing parenthesis)
const invalidShaderWGSL = `@fragment
fn main() -> @location(0) vec4f {
return vec4(1.0, 0.0, 0.0, 1.0;
}`;

device.pushErrorScope("validation");

// This should generate a validation error due to syntax error
device.createShaderModule({
code: invalidShaderWGSL,
});

return device.popErrorScope().then((error) => {
if (error) {
return {
hasError: true,
message: error.message,
messageLength: error.message.length,
messageNotEmpty: error.message.length > 0,
messageContainsExpected:
error.message.includes("expected") ||
error.message.includes("error") ||
error.message.includes("parsing"),
};
} else {
return {
hasError: false,
message: "",
messageLength: 0,
messageNotEmpty: false,
messageContainsExpected: false,
};
}
});
});

expect(result.hasError).toBe(true);
expect(result.messageNotEmpty).toBe(true);
expect(result.messageLength).toBeGreaterThan(0);
// The error message should contain some indication that it's a parsing error
expect(result.messageContainsExpected).toBe(true);
});

it("should return null when no error occurs", async () => {
const result = await client.eval(({ device, shaders: { redFragWGSL } }) => {
device.pushErrorScope("validation");

// This should not generate any errors
device.createShaderModule({
code: redFragWGSL,
});

return device.popErrorScope().then((error) => {
return {
hasError: error !== null,
error: error,
};
});
});

expect(result.hasError).toBe(false);
expect(result.error).toBe(null);
});
});
Loading