Skip to content
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

Failure to build with no-fake-scoped-enums with emscripten #27

Open
slowriot opened this issue Nov 7, 2024 · 3 comments
Open

Failure to build with no-fake-scoped-enums with emscripten #27

slowriot opened this issue Nov 7, 2024 · 3 comments

Comments

@slowriot
Copy link
Contributor

slowriot commented Nov 7, 2024

When generating with fake scoped enums disabled, using the latest emscripten webgpu.h, I see multiple compilation errors from the WEBGPU_CPP_IMPLEMENTATION section of webgpu.h.

To generate:

python3 ./generate.py -u webgpu.h -t emscripten/webgpu.template.hpp -o webgpu.hpp -d defaults.txt -d extra-defaults.txt --no-fake-scoped-enums

When building, I get compilation errors such as:

webgpu.hpp:1341:16: fatal error: assigning to 'WGPUBackendType' from incompatible type 'wgpu::BackendType'
 1341 |         backendType = BackendType::Undefined;
      |                       ^~~~~~~~~~~~~~~~~~~~~~

These could be resolved with a change like backendType = static_cast<WGPUBackendType>(BackendType::Undefined).

slowriot added a commit to Armchair-Software/webgpu-demo that referenced this issue Nov 8, 2024
@eliemichel
Copy link
Owner

Thanks for reporting this, I have to admit that I undertested this feature. Keeping this open until I fix and add automated tests! Thanks again for your multiple contributions!

slowriot added a commit to Armchair-Software/webgpu-demo that referenced this issue Nov 14, 2024
@eliemichel
Copy link
Owner

Back at this, I realize that the issue you report is precisely why I had to use fake enums. So if you find a way to work it around properly, I would be happy to switch everything to real enums!

Looking at your commit history, I bet you wanted real enums to be able to use magic_enum, which is a fair need. You may always force the enum type when using magic_enum functions, e.g.:

ErrorType errorType = /* ... */;
std::cout << "Device error: " << magic_enum::enum_name<WGPUErrorType>(errorType);

But of course you will have the WGPUErrorType_ prefix in this case. Maybe a solution for you would be that the C++ wrapper also includes some enum to name conversion utilities? Ideally we should try and have this a compile-time thing so that it remains zero-overhead.

@slowriot
Copy link
Contributor Author

Looking at your commit history, I bet you wanted real enums to be able to use magic_enum

Yes, you're exactly right. I ended up writing a small utility to cast the fake enum and then strip the leading WGPUErrorType_ etc, but it adds some runtime cost vs pure enum use, so I was hoping there could be a clean solution with normal enums. But aside from magic_enum, there are other use cases where passing around strongly typed enums is preferable from an ergonomic point of view (switch statements, etc).

I've also been working with the Dawn-derived CPP wrapper that comes with current versions of Emscripten, webgpu/webgpu_cpp.h, which I know you're familiar with. Its interface is more basic, and closer to the C API, often passing raw pointers, but it has the advantage that it provides human-readable strongly typed enums, that are still easily convertible to and from the C types. Maybe it's worth duplicating their approach to enums?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants