curv3d / curv

a language for making art using mathematics
Apache License 2.0
1.14k stars 73 forks source link

rewrite Curv to use WebGPU #146

Open doug-moen opened 3 years ago

doug-moen commented 3 years ago

The proposal is to rewrite the Curv "viewer" window to use WebGPU, replacing the use of OpenGL.

Rationale: Currently Curv is stuck at version 3.3 of OpenGL due to MacOS compatibility requirements. There's no way to use advanced features like compute shaders in OpenGL due to MacOS limitations. WebGPU can be thought of as the modern replacement for OpenGL. It is faster and more powerful than OpenGL. It is much easier to use than Vulkan. WebGPU is more portable than Vulkan, since it runs on all desktop and mobile platforms, plus it supports web assembly (WASM) and web browsers.

Once Curv is ported to WebGPU, we can build a faster and more powerful rendering engine based on the use of compute shaders.

How To Do It: The only Curv code that needs to change is located in libcurv/viewer. This code runs the graphical "viewer" window. This software component is well isolated from the rest of Curv. One approach is to write a new viewer from scratch, instead of attempting to translate the OpenGL code into WebGPU. There are lots of "GPU shader sandbox" apps available in open source, so we could use an existing such app coded to the WebGPU API as a starting point. (The current Curv viewer is based on GLSLViewer.)

The initial prototype need not be integrated into Curv, it could be a standalone app. Curv has the ability to export a shape as a *.jgpu file: this is a JSON encoding data structure containing all the inputs required to run a Viewer window. So the prototype could just read and parse this JSON file then display the shape in a window. This may simplify the development process.

Since Curv is written in C++, we can use Google's Dawn library, which has a C++ interface. The Curv shader compiler generates GLSL code, so we can use a library to translate GLSL into SPIR-V, which Dawn accepts as input. The current viewer code depends on the ImGUI library for GUI widgets. There's no requirement to use ImGUI in the new viewer implementation, but I see that there is already a WebGPU port for imgui.

Resources:

pbsds commented 1 year ago

An alternative backend is the WGPU C++ api: https://github.com/gfx-rs/wgpu-native I haven't tried it, but I believe it can run on directx, vulcan, opengl and metal, possibly featuring support for multiple shader languages.

doug-moen commented 1 year ago

I agree.There is a C/C++ implementation from Google (Dawn) that doesn't introduce a Rust dependency. Nice to know that gfx-rs now has a C++ wrapper. WGPU is on track to be finalized some time this year. (Right now it is still an unstable alpha, with breaking changes still planned.) WGPU will open up a richer API, including compute shaders, which will be very useful to Curv.