Library for rapid prototyping of native WebGPU Apps in C++ using Dawn
wgpu-lab is a library designed for rapid prototyping of native WebGPU applications in C++. Its main goal is to provide convenient wrappers and intuitive tools for working with WebGPU Dawn, minimizing boilerplate code while remaining flexible and customizable.
I’m developing this library in my free time as I learn the fundamentals of WebGPU. My hope is that it will serve the open source community as a useful resource for learning and as a foundation for building creative projects.
Please note that wgpu-lab is in an early, heavily experimental stage, and the API is likely to undergo significant changes.
Contributions are welcome!
#include <lab>
struct MyVertexFormat {
float pos[2];
float color[3];
};
int main() {
lab::Webgpu webgpu("My WebGPU Context");
lab::Shader shader("My Shader", "shaders/draw_colored.wgsl");
lab::Pipeline pipeline(shader, webgpu); // the rendering pipeline
// colored triangle data
std::vector<MyVertexFormat> vertex_data = {
// X Y R G B
{.pos = {-0.5f, -0.5f}, .color = {0.8f, 0.2f, 0.2f}},
{.pos = {+0.5f, -0.5f}, .color = {0.8f, 0.8f, 0.2f}},
{.pos = {+0.0f, +0.5f}, .color = {0.2f, 0.8f, 0.4f}},
};
// vertex buffer (sends copy of data to GPU memory)
lab::Buffer vertex_buffer("My Vertex Buffer", vertex_data, webgpu);
// pipeline needs to know about buffers and their memory layouts (vertex attributes)
pipeline.add_vertex_buffer(vertex_buffer);
pipeline.add_vertex_attrib(wgpu::VertexFormat::Float32x2, 0); // position
pipeline.add_vertex_attrib(wgpu::VertexFormat::Float32x3, 1); // color
pipeline.finalize(); // make ready for rendering
lab::Window window("Hello Triangle", 640, 400);
lab::Surface surface(window, webgpu); // surface to render onto
// main application loop
while (lab::tick()) {
pipeline.render_frame(surface, 3, 1); // 3 vertices, 1 instance
}
}
Setup:
C/C++, CMake and CMake Tools are installedBuild:
git clone https://github.com/bv7dev/wgpu-lab.git
⚙ Build button in Code's bottom toolbar (provided by CMake Tools extension) or use CMake manually to configure and buildThe first build takes a long time for downloading, generating and building dawn.
Run sample executables:
For VS Code users, there's a shared .vscode/launch.json configuration file.
This setup allows you to build and run any .cpp source file that's located in the samples/ directory,
simply by opening it in the editor and pressing F5. This runs the code in debug mode (set breakpoints and step through the code to learn how it works).
To get started, you can add your own .cpp file, tinker around and step through the code. Use CMake Tools to reconfigure the project after adding new files.
The library currently only depends on WebGPU Dawn and uses
GLFW for windowing, which already comes included with dawn.
wgpu-lab also makes heavy use of the C++ STL (see src/extra/lab_public.h).
However, to build all of the sample executables, the libraries
GLM and tinygltf
are also included.
No comments yet.
Sign in to be the first to comment.