{"id":26053290,"url":"https://github.com/chrrs/webgpu-hpp","last_synced_at":"2026-04-21T19:31:34.992Z","repository":{"id":280984276,"uuid":"943796411","full_name":"chrrs/webgpu-hpp","owner":"chrrs","description":"Automatically generated C++ wrapper for webgpu-native","archived":false,"fork":false,"pushed_at":"2025-03-06T11:09:55.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-06T11:31:23.473Z","etag":null,"topics":["cmake","cpp","python","webgpu","webgpu-native"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrrs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-06T09:27:52.000Z","updated_at":"2025-03-06T11:09:58.000Z","dependencies_parsed_at":"2025-03-06T11:41:26.276Z","dependency_job_id":null,"html_url":"https://github.com/chrrs/webgpu-hpp","commit_stats":null,"previous_names":["chrrs/webgpu-hpp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrrs%2Fwebgpu-hpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrrs%2Fwebgpu-hpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrrs%2Fwebgpu-hpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrrs%2Fwebgpu-hpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrrs","download_url":"https://codeload.github.com/chrrs/webgpu-hpp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242515183,"owners_count":20141967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmake","cpp","python","webgpu","webgpu-native"],"created_at":"2025-03-08T07:27:48.459Z","updated_at":"2026-04-21T19:31:29.955Z","avatar_url":"https://github.com/chrrs.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# webgpu-hpp\n\n\u003e [!WARNING]\n\u003e Only Windows has been tested at this time. While the project does include support for other platforms, the code is\n\u003e untested, and it's unknown whether it will even compile.\n\nAutomatically generated opinionated idiomatic C++20 wrapper and utilities for WebGPU, adding features such as\ntype-safety for enums\nand bitflags and interop with STL containers, while not introducing any runtime overhead.\n\nThis project also serves as a simple way to use wgpu-native with CMake.\n\n---\n\n*Note that the actual `webgpu.hpp` header file is **not** checked into this repository, as it is generated directly from\nthe spec when CMake configures the project.*\n\n## Quick example\n\nThis library turns the verbose C-style code from this:\n\n```c++\nWGPURenderPassColorAttachment colorAttachment;\ncolorAttachment.view = targetView;\ncolorAttachment.loadOp = WGPULoadOp_Clear;\ncolorAttachment.storeOp = WGPUStoreOp_Store;\ncolorAttachment.clearValue = { 0.0f, 0.0f, 0.0f, 1.0f };\n\nWGPURenderPassDescriptor descriptor;\ndescriptor.colorAttachmentCount = 1;\ndescriptor.colorAttachments = \u0026colorAttachment;\n\nauto renderPass = wgpuCommandEncoderBeginRenderPass(encoder, \u0026descriptor);\n\nwgpuRenderPassEncoderSetPipeline(renderPass, pipeline);\nwgpuRenderPassEncoderDraw(renderPass, 3, 1, 0, 0);\n\nwgpuRenderPassEncoderEnd(renderPass);\nwgpuRenderPassEncoderRelease(renderPass);\n```\n\ninto idiomatic C++-style code such as this:\n\n```c++\nwgpu::RenderPassColorAttachment colorAttachment {\n    .view = targetView,\n    .loadOp = wgpu::LoadOp::Clear,\n    .storeOp = wgpu::StoreOp::Store,\n    .clearValue = { 0.0f, 0.0f, 0.0f, 1.0f },\n};\n\nwgpu::RenderPassDescriptor descriptor {\n    .colorAttachments = { 1, \u0026colorAttachment },\n};\n\nauto renderPass = encoder.beginRenderPass(descriptor);\n\nrenderPass.setPipeline(pipeline);\nrenderPass.draw(3, 1, 0, 0);\n\nrenderPass.end();\nrenderPass.release();\n```\n\n---\n\nFor a more thorough example, see the project in the `example` folder, which renders a basic triangle to the screen.\n\n## Installation\n\n\u003e [!IMPORTANT]\n\u003e Make sure you have some version of Python 3 installed on your system with the `pyyaml` package installed.\n\nClone this repository into a subdirectory in your CMake project, and add it to your project:\n\n```cmake\nadd_subdirectory(webgpu-hpp)\n\ntarget_link_libraries(MyProject PRIVATE webgpu-hpp)\ntarget_copy_webgpu_binaries(MyProject)\n```\n\n---\n\nYou can also use `FetchContent`:\n\n```cmake\ninclude(FetchContent)\n\nFetchContent_Declare(webgpu-hpp GIT_REPOSITORY https://github.com/chrrs/webgpu-hpp GIT_TAG main)\nFetchContent_MakeAvailable(webgpu-hpp)\n\ntarget_link_libraries(MyProject PRIVATE webgpu-hpp)\ntarget_copy_webgpu_binaries(MyProject)\n```\n\n---\n\nNote the call to the `target_copy_webgpu_binaries` function. This copies the runtime binaries to the target folder at\nbuild time, which are needed to run your application.\n\n---\n\nAfter this, you can include the header:\n\n```c++\n#include \u003cwebgpu/webgpu.hpp\u003e\n```\n\n## Usage\n\n\u003e [!TIP]\n\u003e The code generator is designed to output human-readable code! If you're ever unsure about something, looking at the\n\u003e header might give you extra information.\n\n### `wgpu` namespace\n\nAll WebGPU-related functions, structs and objects are located in the `wgpu` namespace. Their name is the same as their C\nequivalent, without the `wgpu` prefix.\n\nFor example, `wgpuCreateInstance` becomes `wgpu::createInstance`, and the type\n`WGPUDeviceDescriptor` becomes `wgpu::DeviceDescriptor`.\n\n### Objects\n\nObjects in WebGPU are reference-counted, and often have operations associated with them.\n\nA common pattern you'll see is functions like `wgpuInstanceRequestAdapter(instance, ...)`. These functions have been\nconverted to member functions. The previous example, for example, will become `instance.requestAdapter(...)`.\n\nThis also extends to ownership management. Each object has two methods, which are called `addRef` and `release`. All\nobjects still have to be manually released as you would do in C.\n\nCurrently, webgpu-hpp does not support RAII handles, but this may change in the future.\n\n```c++\nauto renderPass = encoder.beginRenderPass(renderPassDescriptor);\n\nrenderPass.setPipeline(pipeline);\nrenderPass.draw(3, 1, 0, 0);\n\nrenderPass.end();\nrenderPass.release();\n```\n\n### Structures\n\nStructs in webgpu-hpp are a mostly 1-to-1 conversion from their C-equivalents. An important difference is that a lot\nof the structs have their defaults set as recommended by the spec, or zero otherwise. This means that in a lot of\ncases, if you don't care about a field, you can safely leave it out.\n\n```c++\n// Note that RequestAdapterOptions has a lot more fields that are left out.\nwgpu::RequestAdapterOptions adapterOptions {\n    .powerPreference = wgpu::PowerPreference::HighPerformance,\n};\n```\n\n### Extensions\n\nSome structures in WebGPU support extension chains. In webgpu-hpp, any struct supporting this has a\n`ChainedStruct* next` member. Every extension has a `ChainedStruct chain` member, which can be assigned to the `next`\npointer of the struct you wish to extend.\n\nNote that for any extension, `chain.sType` is set automatically!\n\n```c++\n// Extension: Shader source\nwgpu::ShaderSourceWGSL shaderSource {\n    .code = MY_WGSL_SHADER_SOURCE,\n};\n\n// Extendable: Shader module\nwgpu::ShaderModuleDescriptor shaderModuleDescriptor {\n    .next = \u0026shaderSource.chain,\n    .label = \"Triangle Shader\",\n};\n```\n\n### Enums\n\nEnums are represented by enum classes in order to achieve type-safety. They have no type prefix, but are scoped.\n\nSome variants start with a number, which C++ disallows. In those cases, they are prefixed with `_` (underscore). For\nexample, `WGPUTextureViewDimension_2D` becomes `wgpu::TextureViewDimension::_2D`.\n\n```c++\nwgpu::RenderPipelineDescriptor pipelineDescriptor {\n    .primitive = {\n        .topology = wgpu::PrimitiveTopology::TriangleList,\n        .stripIndexFormat = wgpu::IndexFormat::Undefined,\n        .frontFace = wgpu::FrontFace::CCW,\n        .cullMode = wgpu::CullMode::Back,\n    },\n    ...\n};\n```\n\n### Bitflags\n\nTo achieve type-safety, bitflags are handled by the `wgpu::Flags\u003c\u003e` wrapper.\n\nThe individual bits are defined as an enum class, identically to normal enums. Relevant operators, such as `|` (Bitwise\nOR), `\u0026` (Bitwise AND) and `^` (Bitwise XOR) have been overloaded to automatically create the wrapper class.\n\nThis class will function identically to normal bitflags, with the added benefit of not allowing mismatched flags.\n\n```c++\nwgpu::ColorTargetState colorTarget {\n    .writeMask = wgpu::ColorWriteMask::Red | wgpu::ColorWriteMask::Green\n        | wgpu::ColorWriteMask::Blue | wgpu::ColorWriteMask::Alpha,\n    ...\n};\n```\n\n### String views\n\nStrings in WebGPU are not NUL-terminated, and have an additional length attached to them.\n\nTo make this easier, webgpu-hpp uses `wgpu::StringView` when handling strings. This type defines implicit conversions\nfrom a lot of commonly-used types such as `const char*`, `std::string_view` and `std::string`. It can also be converted\nback to `std::string_view` and `std::string` using `static_cast`.\n\nYou can manually create a string view by using the `StringView(size_t length, const char* data)` constructor.\n\n```c++\n// Creating string views:\nwgpu::DeviceDescriptor deviceDescriptor {\n    .label = \"my-device\",\n};\n\n// Using string views:\nauto deviceLostCallback = [](wgpu::Device const\u0026, wgpu::DeviceLostReason, wgpu::StringView message, void*, void*) {\n    spdlog::error(\"device lost: {}\", static_cast\u003cstd::string_view\u003e(message));\n};\n```\n\n### Arrays\n\nSimilarly to strings, arrays in WebGPU are a combination of their size, and a pointer to their first element.\n\nThe C header defines the count and pointer separately in all cases. webgpu-hpp, however, combines them into a single\n`wgpu::Array\u003c\u003e`. This type defines implicit conversions from a lot of commonly-used types such as `std::array\u003c\u003e`,\n`std::vector\u003c\u003e` and `std::span\u003c\u003e`.\n\nNo conversion is defined for raw pointers, as they have no size information attached. These can be used with the\n`Array(size_t count, T* data)` constructor.\n\n```c++\nwgpu::RenderPassDescriptor renderPassDescriptor {\n    .colorAttachments = { 1, \u0026colorAttachment },\n};\n\nauto buffers = std::array { buffer1, buffer2, buffer3 };\nm_queue.submit(buffers);\n```\n\n### Callbacks\n\nCallbacks are defined using their corresponding CallbackInfo struct, which defines the function pointer, callback mode\nand user data.\n\nEvery callback function takes two user data `void*` arguments, which are directly passed from the CallbackInfo struct.\nSome callbacks also take a callback mode, in which case a `mode` member is present in the info struct. When not\nspecified, this is defaulted to `wgpu::CallbackMode::AllowSpontaneous`.\n\nCallbacks in webgpu-hpp are somewhat limited. Notably, *lambda captures are not supported!*\n\n```c++\n// Simple callback.\nwgpu::DeviceLostCallbackInfo deviceLostCallbackInfo {\n    .callback = [](wgpu::Device const\u0026, wgpu::DeviceLostReason, wgpu::StringView message, void*, void*) {\n        spdlog::error(\"device lost: {}\", static_cast\u003cstd::string_view\u003e(message));\n    },\n};\n\n// More complex callback using user data pointers.\nbool success = false;\nwgpu::RequestDeviceCallbackInfo deviceCallbackInfo {\n    .callback = [](wgpu::RequestDeviceStatus status, wgpu::Device device, wgpu::StringView message, void* devicePtr, void* success) {\n        if (status == wgpu::RequestDeviceStatus::Success) {\n            *static_cast\u003cwgpu::Device*\u003e(devicePtr) = device;\n            *static_cast\u003cbool*\u003e(success) = true;\n        } else {\n            spdlog::error(\"failed to request device: {}\", static_cast\u003cstd::string_view\u003e(message));\n            *static_cast\u003cbool*\u003e(success) = false;\n        } },\n    .userdata1 = \u0026device,\n    .userdata2 = \u0026success,\n};\n```\n\n### C Interoperability\n\nObject handles in webgpu-hpp define implicit conversations from and to their C equivalents, allowing them to be used\ninterchangeably with functions expecting C-style handles. Enums and structs do not offer this implicit conversion, but\ncan often be cast, as their memory layout is the same.\n\n## Addional headers\n\n### `\u003cwebgpu/webgpu-glfw3.hpp\u003e`\n\nThe WebGPU standard and webgpu-native both do not provide an easy way to create a `wgpu::Surface` from a GLFW window.\nThis header contains a single function `wgpu::glfw3::createWindowSurface(wgpu::Instance, GLFWwindow*)`, which will\ncreate the window surface for the given GLFW window.\n\n---\n\nAs this header has some source files attached, you'll need to link it to your project:\n\n```cmake\ntarget_link_libraries(MyProject PRIVATE glfw webgpu-hpp webgpu-glfw3-hpp)\n```\n\nFor this to work, *make sure that the webgpu-cpp CMake file is evaluated **after** GLFW!* This is so webgpu-glfw3-hpp\ncan link against GLFW.\n\n### `\u003cwebgpu/webgpu-platform.hpp\u003e`\n\n\u003e [!WARNING]\n\u003e `webgpu-platform.hpp` is currently very incomplete!\n\nThis header defines some non-standard WebGPU functions that are found in webgpu-native.\n\n## Credits\n\n- Huge thanks to the excellent [Learn WebGPU for C++](https://eliemichel.github.io/LearnWebGPU/) series by Élie Michel,\n  and the projects surrounding it for inspiring this project!\n- The [wgpu-native](https://github.com/gfx-rs/wgpu-native) team for creating and documenting the spec files used to\n  generate the header.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrrs%2Fwebgpu-hpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrrs%2Fwebgpu-hpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrrs%2Fwebgpu-hpp/lists"}