13#include "common/config.h"
15#if defined(EVENGINE_WEBGPU)
25#if defined(WIN32) && defined(min)
33#ifndef IMGUI_IMPL_WEBGPU_DEFAULT_FONT_SAMPLER
34#define IMGUI_IMPL_WEBGPU_DEFAULT_FONT_SAMPLER 0
41struct ImGui_ImplWGPU_Data {
42 WGPUDevice wgpuDevice =
nullptr;
43 WGPUTextureFormat rtFormat = WGPUTextureFormat_BGRA8Unorm;
45 WGPUQueue defaultQueue =
nullptr;
47 WGPUBindGroupLayout bindGroupLayout =
nullptr;
48 WGPUBindGroup bindGroup =
nullptr;
49 WGPUBuffer uniformBuffer =
nullptr;
50 uint32_t uniformBufferSize = 0;
51 WGPURenderPipeline renderPipeline =
nullptr;
53 WGPUTexture fontTexture =
nullptr;
54 WGPUTextureView fontTextureView =
nullptr;
55 WGPUSampler fontSampler =
nullptr;
57 int numFramesInFlight = 1;
63static ImGui_ImplWGPU_Data *ImGui_ImplWGPU_GetBackendData() {
64 return ImGui::GetCurrentContext() ? (ImGui_ImplWGPU_Data *)ImGui::GetIO().BackendRendererUserData
68static const char *ImGui_ImplWGPU_GetWGSLVertexShader() {
74 @location(0) position: vec2f,
75 @location(1) uv: vec2f,
76 @location(2) color: vec4f,
79 @builtin(position) position: vec4f,
80 @location(0) uv: vec2f,
81 @location(1) color: vec4f,
83@group(0) @binding(0) var<uniform> uniforms: Uniforms;
85fn vs_main(in: VertexInput) -> VertexOutput {
86 var out: VertexOutput;
87 out.position = uniforms.mvp * vec4f(in.position, 0.0, 1.0);
95static const char *ImGui_ImplWGPU_GetWGSLFragmentShader() {
98 @builtin(position) position: vec4f,
99 @location(0) uv: vec2f,
100 @location(1) color: vec4f,
102@group(0) @binding(1) var fontSampler: sampler;
103@group(0) @binding(2) var fontTexture: texture_2d<f32>;
105fn fs_main(in: VertexOutput) -> @location(0) vec4f {
106 return textureSample(fontTexture, fontSampler, in.uv) * in.color;
111static void ImGui_ImplWGPU_SetupRenderState(ImDrawData *draw_data,
112 WGPURenderPassEncoder pass_encoder) {
113 ImGui_ImplWGPU_Data *bd = ImGui_ImplWGPU_GetBackendData();
117 float L = draw_data->DisplayPos.x;
118 float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
119 float T = draw_data->DisplayPos.y;
120 float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
122 {2.0f / (
R - L), 0.0f, 0.0f, 0.0f},
123 {0.0f, 2.0f / (T -
B), 0.0f, 0.0f},
124 {0.0f, 0.0f, 0.5f, 0.0f},
125 {(
R + L) / (L - R), (T +
B) / (B - T), 0.5f, 1.0f},
127 wgpuQueueWriteBuffer(bd->defaultQueue, bd->uniformBuffer, 0,
mvp,
sizeof(
mvp));
130 wgpuRenderPassEncoderSetPipeline(pass_encoder, bd->renderPipeline);
131 wgpuRenderPassEncoderSetBindGroup(pass_encoder, 0, bd->bindGroup, 0,
nullptr);
134static void ImGui_ImplWGPU_CreatePipeline() {
135 ImGui_ImplWGPU_Data *bd = ImGui_ImplWGPU_GetBackendData();
136 WGPUDevice
device = bd->wgpuDevice;
139 WGPUShaderSourceWGSL wgslVert{};
140 wgslVert.chain.sType = WGPUSType_ShaderSourceWGSL;
141 wgslVert.code = WGPUStringView{ImGui_ImplWGPU_GetWGSLVertexShader(),
142 strlen(ImGui_ImplWGPU_GetWGSLVertexShader())};
143 WGPUShaderModuleDescriptor vsDesc{};
144 vsDesc.nextInChain = &wgslVert.chain;
145 WGPUShaderModule vsModule = wgpuDeviceCreateShaderModule(
device, &vsDesc);
147 WGPUShaderSourceWGSL wgslFrag{};
148 wgslFrag.chain.sType = WGPUSType_ShaderSourceWGSL;
149 wgslFrag.code = WGPUStringView{ImGui_ImplWGPU_GetWGSLFragmentShader(),
150 strlen(ImGui_ImplWGPU_GetWGSLFragmentShader())};
151 WGPUShaderModuleDescriptor fsDesc{};
152 fsDesc.nextInChain = &wgslFrag.chain;
153 WGPUShaderModule fsModule = wgpuDeviceCreateShaderModule(
device, &fsDesc);
156 WGPUBindGroupLayoutEntry
entries[3] = {};
158 entries[0].visibility = WGPUShaderStage_Vertex;
159 entries[0].buffer.type = WGPUBufferBindingType_Uniform;
160 entries[0].buffer.minBindingSize = 64;
162 entries[1].visibility = WGPUShaderStage_Fragment;
163 entries[1].sampler.type = WGPUSamplerBindingType_Filtering;
165 entries[2].visibility = WGPUShaderStage_Fragment;
166 entries[2].texture.sampleType = WGPUTextureSampleType_Float;
167 entries[2].texture.viewDimension = WGPUTextureViewDimension_2D;
168 WGPUBindGroupLayoutDescriptor bglDesc{};
169 bglDesc.entryCount = 3;
171 bd->bindGroupLayout = wgpuDeviceCreateBindGroupLayout(
device, &bglDesc);
174 WGPUPipelineLayoutDescriptor plDesc{};
175 plDesc.bindGroupLayoutCount = 1;
176 plDesc.bindGroupLayouts = &bd->bindGroupLayout;
177 WGPUPipelineLayout pipelineLayout = wgpuDeviceCreatePipelineLayout(
device, &plDesc);
180 WGPUVertexAttribute
attrs[3] = {};
181 attrs[0].format = WGPUVertexFormat_Float32x2;
182 attrs[0].offset = IM_OFFSETOF(ImDrawVert,
pos);
183 attrs[0].shaderLocation = 0;
184 attrs[1].format = WGPUVertexFormat_Float32x2;
185 attrs[1].offset = IM_OFFSETOF(ImDrawVert,
uv);
186 attrs[1].shaderLocation = 1;
187 attrs[2].format = WGPUVertexFormat_Unorm8x4;
188 attrs[2].offset = IM_OFFSETOF(ImDrawVert, col);
189 attrs[2].shaderLocation = 2;
190 WGPUVertexBufferLayout vbLayout{};
191 vbLayout.arrayStride =
sizeof(ImDrawVert);
192 vbLayout.stepMode = WGPUVertexStepMode_Vertex;
193 vbLayout.attributeCount = 3;
194 vbLayout.attributes =
attrs;
196 WGPUBlendState
blend{};
197 blend.color.srcFactor = WGPUBlendFactor_SrcAlpha;
198 blend.color.dstFactor = WGPUBlendFactor_OneMinusSrcAlpha;
199 blend.color.operation = WGPUBlendOperation_Add;
200 blend.alpha.srcFactor = WGPUBlendFactor_One;
201 blend.alpha.dstFactor = WGPUBlendFactor_OneMinusSrcAlpha;
202 blend.alpha.operation = WGPUBlendOperation_Add;
204 WGPUColorTargetState colorTarget{};
205 colorTarget.format = bd->rtFormat;
206 colorTarget.blend = &
blend;
207 colorTarget.writeMask = WGPUColorWriteMask_All;
209 WGPURenderPipelineDescriptor
desc{};
210 desc.layout = pipelineLayout;
211 desc.vertex.module = vsModule;
212 desc.vertex.entryPoint = WGPUStringView{
"vs_main",
sizeof(
"vs_main") - 1};
213 desc.vertex.bufferCount = 1;
214 desc.vertex.buffers = &vbLayout;
215 desc.primitive.topology = WGPUPrimitiveTopology_TriangleList;
216 desc.primitive.frontFace = WGPUFrontFace_CCW;
217 desc.primitive.cullMode = WGPUCullMode_None;
218 desc.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
219 WGPUFragmentState fragState{};
220 fragState.module = fsModule;
221 fragState.entryPoint = WGPUStringView{
"fs_main",
sizeof(
"fs_main") - 1};
222 fragState.targetCount = 1;
223 fragState.targets = &colorTarget;
224 desc.fragment = &fragState;
225 desc.multisample.count = 1;
226 desc.multisample.mask = UINT32_MAX;
227 bd->renderPipeline = wgpuDeviceCreateRenderPipeline(
device, &
desc);
230static void ImGui_ImplWGPU_CreateUniformBuffer() {
231 ImGui_ImplWGPU_Data *bd = ImGui_ImplWGPU_GetBackendData();
232 WGPUDevice
device = bd->wgpuDevice;
234 bd->uniformBufferSize = 64;
235 WGPUBufferDescriptor
desc{};
236 desc.size = bd->uniformBufferSize;
237 desc.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst;
238 desc.mappedAtCreation =
false;
239 bd->uniformBuffer = wgpuDeviceCreateBuffer(
device, &
desc);
243 ImGuiIO &io = ImGui::GetIO();
244 ImGui_ImplWGPU_Data *bd = ImGui_ImplWGPU_GetBackendData();
245 WGPUDevice
device = bd->wgpuDevice;
250 size_t stride =
static_cast<size_t>(
width) * 4;
251 if (
width <= 0 || height <= 0 || stride > std::numeric_limits<uint32_t>::max())
return false;
253 WGPUTextureDescriptor texDesc{};
254 texDesc.dimension = WGPUTextureDimension_2D;
255 texDesc.size.width =
width;
256 texDesc.size.height =
height;
257 texDesc.size.depthOrArrayLayers = 1;
258 texDesc.sampleCount = 1;
259 texDesc.format = WGPUTextureFormat_RGBA8Unorm;
260 texDesc.mipLevelCount = 1;
261 texDesc.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_CopyDst;
262 bd->fontTexture = wgpuDeviceCreateTexture(
device, &texDesc);
264 WGPUTexelCopyBufferLayout
layout{};
266 layout.bytesPerRow =
static_cast<uint32_t
>(stride);
268 WGPUTexelCopyTextureInfo copyDst{};
269 copyDst.texture = bd->fontTexture;
270 copyDst.mipLevel = 0;
271 copyDst.aspect = WGPUTextureAspect_All;
272 WGPUExtent3D extent{
static_cast<uint32_t
>(
width),
static_cast<uint32_t
>(
height), 1};
273 wgpuQueueWriteTexture(bd->defaultQueue, ©Dst,
pixels, stride *
height, &
layout, &extent);
275 WGPUTextureViewDescriptor viewDesc{};
276 viewDesc.format = WGPUTextureFormat_RGBA8Unorm;
277 viewDesc.dimension = WGPUTextureViewDimension_2D;
278 viewDesc.baseMipLevel = 0;
279 viewDesc.mipLevelCount = 1;
280 viewDesc.baseArrayLayer = 0;
281 viewDesc.arrayLayerCount = 1;
282 bd->fontTextureView = wgpuTextureCreateView(bd->fontTexture, &viewDesc);
284 WGPUSamplerDescriptor samplerDesc{};
285 samplerDesc.maxAnisotropy = 1;
286 samplerDesc.lodMaxClamp = 32.f;
287 samplerDesc.minFilter = WGPUFilterMode_Linear;
288 samplerDesc.magFilter = WGPUFilterMode_Linear;
289 samplerDesc.mipmapFilter = WGPUMipmapFilterMode_Linear;
290 samplerDesc.addressModeU = WGPUAddressMode_ClampToEdge;
291 samplerDesc.addressModeV = WGPUAddressMode_ClampToEdge;
292 samplerDesc.addressModeW = WGPUAddressMode_ClampToEdge;
293 bd->fontSampler = wgpuDeviceCreateSampler(
device, &samplerDesc);
295 WGPUBindGroupEntry
entries[3] = {};
297 entries[0].buffer = bd->uniformBuffer;
298 entries[0].size = bd->uniformBufferSize;
300 entries[1].sampler = bd->fontSampler;
302 entries[2].textureView = bd->fontTextureView;
304 WGPUBindGroupDescriptor bgDesc{};
305 bgDesc.layout = bd->bindGroupLayout;
306 bgDesc.entryCount = 3;
308 bd->bindGroup = wgpuDeviceCreateBindGroup(
device, &bgDesc);
310 io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->fontTextureView);
315 ImGui_ImplWGPU_Data *bd = ImGui_ImplWGPU_GetBackendData();
317 if (bd->fontTexture) { wgpuTextureRelease(bd->fontTexture); bd->fontTexture =
nullptr; }
318 if (bd->fontTextureView) { wgpuTextureViewRelease(bd->fontTextureView); bd->fontTextureView =
nullptr; }
319 if (bd->fontSampler) { wgpuSamplerRelease(bd->fontSampler); bd->fontSampler =
nullptr; }
320 if (bd->bindGroup) { wgpuBindGroupRelease(bd->bindGroup); bd->bindGroup =
nullptr; }
321 if (bd->bindGroupLayout) { wgpuBindGroupLayoutRelease(bd->bindGroupLayout); bd->bindGroupLayout =
nullptr; }
322 if (bd->uniformBuffer) { wgpuBufferRelease(bd->uniformBuffer); bd->uniformBuffer =
nullptr; }
323 if (bd->renderPipeline) { wgpuRenderPipelineRelease(bd->renderPipeline); bd->renderPipeline =
nullptr; }
324 if (ImGui::GetCurrentContext() && ImGui::GetIO().Fonts)
325 ImGui::GetIO().Fonts->SetTexID(0);
329 ImGuiIO &io = ImGui::GetIO();
330 IM_ASSERT(io.BackendRendererUserData == NULL &&
"Already initialized a renderer backend!");
332 ImGui_ImplWGPU_Data *bd = IM_NEW(ImGui_ImplWGPU_Data)();
333 io.BackendRendererUserData = (
void *)bd;
334 io.BackendRendererName =
"imgui_impl_webgpu";
335 io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;
339 bd->rtFormat = rt_format;
340 bd->numFramesInFlight = num_frames_in_flight;
341 bd->defaultQueue = wgpuDeviceGetQueue(
device);
343 ImGui_ImplWGPU_CreateUniformBuffer();
344 ImGui_ImplWGPU_CreatePipeline();
351 ImGui_ImplWGPU_Data *bd = ImGui_ImplWGPU_GetBackendData();
352 IM_ASSERT(bd != NULL &&
"No renderer backend to shutdown, or already shutdown?");
354 if (bd->defaultQueue) { wgpuQueueRelease(bd->defaultQueue); bd->defaultQueue =
nullptr; }
355 if (bd->wgpuDevice) { wgpuDeviceRelease(bd->wgpuDevice); bd->wgpuDevice =
nullptr; }
356 ImGuiIO &io = ImGui::GetIO();
357 io.BackendRendererName = NULL;
358 io.BackendRendererUserData = NULL;
363 ImGui_ImplWGPU_Data *bd = ImGui_ImplWGPU_GetBackendData();
364 IM_ASSERT(bd != NULL &&
"Did you call ImGui_ImplWGPU_Init()?");
365 bd->frameIndex = (bd->frameIndex + 1) % bd->numFramesInFlight;
369 ImGui_ImplWGPU_Data *bd = ImGui_ImplWGPU_GetBackendData();
370 if (draw_data->CmdListsCount == 0)
return;
372 ImGui_ImplWGPU_SetupRenderState(draw_data, pass_encoder);
375 ImVec2 clip_off = draw_data->DisplayPos;
376 ImVec2 clip_scale = draw_data->FramebufferScale;
378 const float fb_width = draw_data->DisplaySize.x * clip_scale.x;
379 const float fb_height = draw_data->DisplaySize.y * clip_scale.y;
380 if (fb_width <= 0.0f || fb_height <= 0.0f)
return;
382 for (
int n = 0;
n < draw_data->CmdListsCount;
n++) {
383 const ImDrawList *cmd_list = draw_data->CmdLists[
n];
386 size_t vbSize = (size_t)cmd_list->VtxBuffer.Size *
sizeof(ImDrawVert);
387 size_t ibSize = (size_t)cmd_list->IdxBuffer.Size *
sizeof(ImDrawIdx);
388 WGPUBuffer vb =
nullptr;
389 WGPUBuffer ib =
nullptr;
391 WGPUBufferDescriptor vbDesc{};
392 vbDesc.size = vbSize;
393 vbDesc.usage = WGPUBufferUsage_Vertex | WGPUBufferUsage_CopyDst;
394 vbDesc.mappedAtCreation =
false;
395 vb = wgpuDeviceCreateBuffer(bd->wgpuDevice, &vbDesc);
396 wgpuQueueWriteBuffer(bd->defaultQueue, vb, 0, cmd_list->VtxBuffer.Data, vbSize);
398 WGPUBufferDescriptor ibDesc{};
399 const size_t alignedIbSize = (ibSize + 3) & ~
size_t(3);
400 ibDesc.size = alignedIbSize;
401 ibDesc.usage = WGPUBufferUsage_Index | WGPUBufferUsage_CopyDst;
402 ibDesc.mappedAtCreation =
false;
403 ib = wgpuDeviceCreateBuffer(bd->wgpuDevice, &ibDesc);
405 std::vector<unsigned char> indexUpload(alignedIbSize, 0);
406 memcpy(indexUpload.data(), cmd_list->IdxBuffer.Data, ibSize);
407 wgpuQueueWriteBuffer(bd->defaultQueue, ib, 0, indexUpload.data(), alignedIbSize);
409 wgpuRenderPassEncoderSetVertexBuffer(pass_encoder, 0, vb, 0, vbSize);
410 wgpuRenderPassEncoderSetIndexBuffer(pass_encoder, ib,
411 sizeof(ImDrawIdx) == 2 ? WGPUIndexFormat_Uint16
412 : WGPUIndexFormat_Uint32,
415 for (
int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
416 const ImDrawCmd *pcmd = &cmd_list->CmdBuffer[cmd_i];
417 if (pcmd->UserCallback != NULL) {
418 pcmd->UserCallback(cmd_list, pcmd);
421 ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x,
422 (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
423 ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x,
424 (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
425 if (clip_min.x < 0.0f) clip_min.x = 0.0f;
426 if (clip_min.y < 0.0f) clip_min.y = 0.0f;
427 if (clip_max.x > fb_width) clip_max.x = fb_width;
428 if (clip_max.y > fb_height) clip_max.y = fb_height;
429 if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
continue;
431 wgpuRenderPassEncoderSetScissorRect(pass_encoder,
432 (uint32_t)clip_min.x, (uint32_t)clip_min.y,
433 (uint32_t)(clip_max.x - clip_min.x),
434 (uint32_t)(clip_max.y - clip_min.y));
437 WGPUTextureView fontTextureView =
438 (WGPUTextureView)(intptr_t)pcmd->TextureId;
439 if (fontTextureView != bd->fontTextureView) {
440 WGPUBindGroupEntry
entries[3] = {};
442 entries[0].buffer = bd->uniformBuffer;
443 entries[0].size = bd->uniformBufferSize;
445 entries[1].sampler = bd->fontSampler;
447 entries[2].textureView = fontTextureView;
448 WGPUBindGroupDescriptor bgDesc{};
449 bgDesc.layout = bd->bindGroupLayout;
450 bgDesc.entryCount = 3;
452 WGPUBindGroup bg = wgpuDeviceCreateBindGroup(bd->wgpuDevice, &bgDesc);
453 wgpuRenderPassEncoderSetBindGroup(pass_encoder, 0, bg, 0,
nullptr);
454 wgpuBindGroupRelease(bg);
456 wgpuRenderPassEncoderSetBindGroup(pass_encoder, 0, bd->bindGroup, 0,
nullptr);
459 wgpuRenderPassEncoderDrawIndexed(pass_encoder, pcmd->ElemCount, 1,
460 pcmd->IdxOffset, (int32_t)pcmd->VtxOffset, 0);
464 wgpuBufferRelease(vb);
465 wgpuBufferRelease(ib);
std::unordered_map< std::string, QuestRuntime > entries
bool ImGui_ImplWGPU_CreateFontsTexture()
Internal EVUI font-upload compatibility entry point following the ImGui backend convention.
void ImGui_ImplWGPU_InvalidateDeviceObjects()
Im gui impl wgpu invalidate device objects.
void ImGui_ImplWGPU_RenderDrawData(ImDrawData *draw_data, WGPURenderPassEncoder pass_encoder)
Im gui impl wgpu render draw data.
bool ImGui_ImplWGPU_Init(WGPUDevice device, int num_frames_in_flight, WGPUTextureFormat rt_format)
Internal EVUI compatibility entry point following the ImGui renderer backend convention.
void ImGui_ImplWGPU_Shutdown()
Im gui impl wgpu shutdown.
void ImGui_ImplWGPU_NewFrame()
Im gui impl wgpu new frame.
std::vector< int64_t > attrs(const Node &n, const char *key, std::vector< int64_t > fallback)
Attrs.