Skip to content

Commit

Permalink
Updated wgpu example for multiviewport
Browse files Browse the repository at this point in the history
- Added instance to init model
- Added Present mode for any new window to adhere by
- Added Multiviewport render and update code
- Changed Texture format and Present mode to select the first available mode for capabilities (some surface creation such as laptops might not support FiFo)
  • Loading branch information
Zelif committed May 4, 2024
1 parent 9a7f1b1 commit 8d08e0b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions examples/example_glfw_wgpu/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static WGPUInstance wgpu_instance = nullptr;
static WGPUDevice wgpu_device = nullptr;
static WGPUSurface wgpu_surface = nullptr;
static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm;
static WGPUPresentMode wgpu_present_mode = WGPUPresentMode_Fifo;
static WGPUSwapChain wgpu_swap_chain = nullptr;
static int wgpu_swap_chain_width = 1280;
static int wgpu_swap_chain_height = 720;
Expand Down Expand Up @@ -93,6 +94,7 @@ int main(int, char**)
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;

// Setup Dear ImGui style
ImGui::StyleColorsDark();
Expand All @@ -104,10 +106,12 @@ int main(int, char**)
ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback("#canvas");
#endif
ImGui_ImplWGPU_InitInfo init_info;
init_info.Instance = wgpu_instance;
init_info.Device = wgpu_device;
init_info.NumFramesInFlight = 3;
init_info.RenderTargetFormat = wgpu_preferred_fmt;
init_info.DepthStencilFormat = WGPUTextureFormat_Undefined;
init_info.ViewportPresentMode = wgpu_present_mode;
ImGui_ImplWGPU_Init(&init_info);

// Load Fonts
Expand Down Expand Up @@ -236,6 +240,12 @@ int main(int, char**)
WGPUQueue queue = wgpuDeviceGetQueue(wgpu_device);
wgpuQueueSubmit(queue, 1, &cmd_buffer);

if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}

#ifndef __EMSCRIPTEN__
wgpuSwapChainPresent(wgpu_swap_chain);
#endif
Expand Down Expand Up @@ -269,7 +279,7 @@ static WGPUAdapter RequestAdapter(WGPUInstance instance)
*(WGPUAdapter*)(pUserData) = adapter;
else
printf("Could not get WebGPU adapter: %s\n", message);
};
};
WGPUAdapter adapter;
wgpuInstanceRequestAdapter(instance, nullptr, onAdapterRequestEnded, (void*)&adapter);
return adapter;
Expand Down Expand Up @@ -318,7 +328,10 @@ static bool InitWGPU(GLFWwindow* window)
wgpu::Surface surface = wgpu::glfw::CreateSurfaceForWindow(instance, window);
if (!surface)
return false;
wgpu_preferred_fmt = WGPUTextureFormat_BGRA8Unorm;
wgpu::SurfaceCapabilities capabilities;
surface.GetCapabilities(adapter, &capabilities);
wgpu_preferred_fmt = (WGPUTextureFormat) capabilities.formats[0];
wgpu_present_mode = (WGPUPresentMode) capabilities.presentModes[0];
#endif

wgpu_instance = instance.MoveToCHandle();
Expand Down

0 comments on commit 8d08e0b

Please sign in to comment.