|
| 1 | +#include <iostream> |
| 2 | + |
| 3 | +#include <GLFW/glfw3.h> |
| 4 | +#if defined(__EMSCRIPTEN__) |
| 5 | +#include <emscripten/emscripten.h> |
| 6 | +#endif |
| 7 | +#include <dawn/webgpu_cpp_print.h> |
| 8 | +#include <webgpu/webgpu_cpp.h> |
| 9 | +#include <webgpu/webgpu_glfw.h> |
| 10 | + |
| 11 | +wgpu::Instance instance; |
| 12 | +wgpu::Adapter adapter; |
| 13 | +wgpu::Device device; |
| 14 | +wgpu::RenderPipeline pipeline; |
| 15 | + |
| 16 | +wgpu::Surface surface; |
| 17 | +wgpu::TextureFormat format; |
| 18 | +const uint32_t kWidth = 512; |
| 19 | +const uint32_t kHeight = 512; |
| 20 | + |
| 21 | +void ConfigureSurface() { |
| 22 | + wgpu::SurfaceCapabilities capabilities; |
| 23 | + surface.GetCapabilities(adapter, &capabilities); |
| 24 | + format = capabilities.formats[0]; |
| 25 | + |
| 26 | + wgpu::SurfaceConfiguration config{.device = device, |
| 27 | + .format = format, |
| 28 | + .width = kWidth, |
| 29 | + .height = kHeight, |
| 30 | + .presentMode = wgpu::PresentMode::Fifo}; |
| 31 | + surface.Configure(&config); |
| 32 | +} |
| 33 | + |
| 34 | +void Init() { |
| 35 | + wgpu::InstanceDescriptor instanceDesc{ |
| 36 | + .capabilities = {.timedWaitAnyEnable = true}}; |
| 37 | + instance = wgpu::CreateInstance(&instanceDesc); |
| 38 | + |
| 39 | + wgpu::Future f1 = instance.RequestAdapter( |
| 40 | + nullptr, wgpu::CallbackMode::WaitAnyOnly, |
| 41 | + [](wgpu::RequestAdapterStatus status, wgpu::Adapter a, |
| 42 | + wgpu::StringView message) { |
| 43 | + if (status != wgpu::RequestAdapterStatus::Success) { |
| 44 | + std::cout << "RequestAdapter: " << message << "\n"; |
| 45 | + exit(0); |
| 46 | + } |
| 47 | + adapter = std::move(a); |
| 48 | + }); |
| 49 | + instance.WaitAny(f1, UINT64_MAX); |
| 50 | + |
| 51 | + wgpu::DeviceDescriptor desc{}; |
| 52 | + desc.SetUncapturedErrorCallback([](const wgpu::Device&, |
| 53 | + wgpu::ErrorType errorType, |
| 54 | + wgpu::StringView message) { |
| 55 | + std::cout << "Error: " << errorType << " - message: " << message << "\n"; |
| 56 | + }); |
| 57 | + |
| 58 | + wgpu::Future f2 = adapter.RequestDevice( |
| 59 | + &desc, wgpu::CallbackMode::WaitAnyOnly, |
| 60 | + [](wgpu::RequestDeviceStatus status, wgpu::Device d, |
| 61 | + wgpu::StringView message) { |
| 62 | + if (status != wgpu::RequestDeviceStatus::Success) { |
| 63 | + std::cout << "RequestDevice: " << message << "\n"; |
| 64 | + exit(0); |
| 65 | + } |
| 66 | + device = std::move(d); |
| 67 | + }); |
| 68 | + instance.WaitAny(f2, UINT64_MAX); |
| 69 | +} |
| 70 | + |
| 71 | +const char shaderCode[] = R"( |
| 72 | + @vertex fn vertexMain(@builtin(vertex_index) i : u32) -> |
| 73 | + @builtin(position) vec4f { |
| 74 | + const pos = array(vec2f(0, 1), vec2f(-1, -1), vec2f(1, -1)); |
| 75 | + return vec4f(pos[i], 0, 1); |
| 76 | + } |
| 77 | + @fragment fn fragmentMain() -> @location(0) vec4f { |
| 78 | + return vec4f(1, 0, 0, 1); |
| 79 | + } |
| 80 | +)"; |
| 81 | + |
| 82 | +void CreateRenderPipeline() { |
| 83 | + wgpu::ShaderSourceWGSL wgsl{{.code = shaderCode}}; |
| 84 | + |
| 85 | + wgpu::ShaderModuleDescriptor shaderModuleDescriptor{.nextInChain = &wgsl}; |
| 86 | + wgpu::ShaderModule shaderModule = |
| 87 | + device.CreateShaderModule(&shaderModuleDescriptor); |
| 88 | + |
| 89 | + wgpu::ColorTargetState colorTargetState{.format = format}; |
| 90 | + |
| 91 | + wgpu::FragmentState fragmentState{ |
| 92 | + .module = shaderModule, .targetCount = 1, .targets = &colorTargetState}; |
| 93 | + |
| 94 | + wgpu::RenderPipelineDescriptor descriptor{.vertex = {.module = shaderModule}, |
| 95 | + .fragment = &fragmentState}; |
| 96 | + pipeline = device.CreateRenderPipeline(&descriptor); |
| 97 | +} |
| 98 | + |
| 99 | +void Render() { |
| 100 | + wgpu::SurfaceTexture surfaceTexture; |
| 101 | + surface.GetCurrentTexture(&surfaceTexture); |
| 102 | + |
| 103 | + wgpu::RenderPassColorAttachment attachment{ |
| 104 | + .view = surfaceTexture.texture.CreateView(), |
| 105 | + .loadOp = wgpu::LoadOp::Clear, |
| 106 | + .storeOp = wgpu::StoreOp::Store}; |
| 107 | + |
| 108 | + wgpu::RenderPassDescriptor renderpass{.colorAttachmentCount = 1, |
| 109 | + .colorAttachments = &attachment}; |
| 110 | + |
| 111 | + wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| 112 | + wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderpass); |
| 113 | + pass.SetPipeline(pipeline); |
| 114 | + pass.Draw(3); |
| 115 | + pass.End(); |
| 116 | + wgpu::CommandBuffer commands = encoder.Finish(); |
| 117 | + device.GetQueue().Submit(1, &commands); |
| 118 | +} |
| 119 | + |
| 120 | +void InitGraphics() { |
| 121 | + ConfigureSurface(); |
| 122 | + CreateRenderPipeline(); |
| 123 | +} |
| 124 | + |
| 125 | +void Start() { |
| 126 | + if (!glfwInit()) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); |
| 131 | + GLFWwindow* window = |
| 132 | + glfwCreateWindow(kWidth, kHeight, "WebGPU window", nullptr, nullptr); |
| 133 | + surface = wgpu::glfw::CreateSurfaceForWindow(instance, window); |
| 134 | + |
| 135 | + InitGraphics(); |
| 136 | + |
| 137 | +#if defined(__EMSCRIPTEN__) |
| 138 | + emscripten_set_main_loop(Render, 0, false); |
| 139 | +#else |
| 140 | + while (!glfwWindowShouldClose(window)) { |
| 141 | + glfwPollEvents(); |
| 142 | + Render(); |
| 143 | + surface.Present(); |
| 144 | + instance.ProcessEvents(); |
| 145 | + } |
| 146 | +#endif |
| 147 | +} |
| 148 | + |
| 149 | +int main() { |
| 150 | + Init(); |
| 151 | + Start(); |
| 152 | +} |
0 commit comments