webgpu support

This commit is contained in:
Jorijn van der Graaf 2026-05-18 04:58:52 +02:00
commit dedf6b0467
22 changed files with 1656 additions and 324 deletions

View file

@ -4,7 +4,9 @@
// (Tier 2 standard shader, dispatched directly). Hit-testing for the button
// and slider is the user's responsibility — see the onMouseMove listener.
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
#include "vulkan/vulkan.h"
#endif
import Crafter.Graphics;
import Crafter.Event;
@ -13,11 +15,15 @@ using namespace Crafter;
int main() {
Device::Initialize();
#ifdef CRAFTER_GRAPHICS_WINDOW_DOM
static Window window(1280, 720, "Hello UI");
#else
Window window(1280, 720, "Hello UI");
#endif
VkCommandBuffer init = window.StartInit();
auto init = window.StartInit();
DescriptorHeapVulkan heap;
GraphicsDescriptorHeap heap;
heap.Initialize(/*images*/ 16, /*buffers*/ 16, /*samplers*/ 4);
window.descriptorHeap = &heap;
@ -33,9 +39,10 @@ int main() {
// User-owned per-shader buffers. Mapped, written each frame, dispatched
// by the user. Capacity is up to the user; resize means re-Register.
VulkanBuffer<QuadItem, true> quadsBuf;
VulkanBuffer<CircleItem, true> circlesBuf;
VulkanBuffer<GlyphItem, true> glyphsBuf;
GraphicsBuffer<QuadItem, true> quadsBuf;
GraphicsBuffer<CircleItem, true> circlesBuf;
GraphicsBuffer<GlyphItem, true> glyphsBuf;
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
quadsBuf.Create(
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 256);
@ -45,6 +52,11 @@ int main() {
glyphsBuf.Create(
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 4096);
#else
quadsBuf.Create(256);
circlesBuf.Create(64);
glyphsBuf.Create(4096);
#endif
auto quadsSlot = ui.RegisterBuffer(quadsBuf);
auto circlesSlot = ui.RegisterBuffer(circlesBuf);
@ -102,7 +114,7 @@ int main() {
};
EventListener<UIBuildArgs> buildSub(&ui.onBuild, [&](UIBuildArgs a) {
VkCommandBuffer cmd = a.cmd;
auto cmd = a.cmd;
// Update demo progress.
progress = std::fmod(progress + 0.005f, 1.0f);
@ -160,15 +172,27 @@ int main() {
// Flush + dispatch. The library inserts the inter-dispatch barriers.
if (qc > 0) {
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
quadsBuf.FlushDevice(cmd, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
#else
quadsBuf.FlushDevice();
#endif
ui.DispatchQuads(cmd, quadsSlot, qc);
}
if (cc > 0) {
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
circlesBuf.FlushDevice(cmd, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
#else
circlesBuf.FlushDevice();
#endif
ui.DispatchCircles(cmd, circlesSlot, cc);
}
if (gc > 0) {
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
glyphsBuf.FlushDevice(cmd, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
#else
glyphsBuf.FlushDevice();
#endif
ui.DispatchText(cmd, glyphsSlot, gc);
}
});

View file

@ -4,6 +4,14 @@ namespace fs = std::filesystem;
using namespace Crafter;
extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args) {
bool isWasm = false;
for (std::string_view a : args) {
if (a.starts_with("--target=") && a.find("wasm") != std::string_view::npos) {
isWasm = true;
break;
}
}
Configuration* graphics = LocalProject({
.projectFile = "../../project.cpp",
.args = std::vector<std::string>(args.begin(), args.end()),
@ -13,6 +21,11 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
cfg.path = "./";
cfg.name = "HelloUI";
cfg.outputName = "HelloUI";
cfg.type = ConfigurationType::Executable;
if (isWasm) {
cfg.target = "wasm32-wasip1";
cfg.defines.push_back({"CRAFTER_GRAPHICS_WINDOW_DOM", ""});
}
ApplyStandardArgs(cfg, args);
cfg.dependencies = { graphics };
@ -21,5 +34,9 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
cfg.GetInterfacesAndImplementations(ifaces, impls);
cfg.files.push_back("font.ttf");
if (isWasm) {
EnableWasiBrowserRuntime(cfg);
}
return cfg;
}