Skip to content

Commit

Permalink
WebGPU: implement vsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Oct 16, 2023
1 parent c8b65a7 commit b3f2099
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion deps/ShaderTranspiler
11 changes: 6 additions & 5 deletions src/WGSwapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
#include "RGLCommon.hpp"

namespace RGL{
WGPUSwapChain makeSwapchain(std::shared_ptr<SurfaceWG> surface, uint32_t width, uint32_t height, const std::shared_ptr<DeviceWG> owningDevice){
WGPUSwapChain SwapchainWG::makeSwapchain(uint32_t width, uint32_t height){
WGPUSwapChainDescriptor swapChainDesc{
.nextInChain = nullptr,
.width = width,
.height = height,
.format = wgpuSurfaceGetPreferredFormat(surface->surface,owningDevice->adapter),
.usage = WGPUTextureUsage_RenderAttachment,
.presentMode = WGPUPresentMode_Fifo,
.presentMode = vsync ? WGPUPresentMode_Fifo : WGPUPresentMode_Immediate,
};
return wgpuDeviceCreateSwapChain(owningDevice->device, surface->surface, &swapChainDesc);
}

SwapchainWG::SwapchainWG(decltype(surface) surface, uint32_t width, uint32_t height, const std::shared_ptr<DeviceWG> owningDevice) : surface(surface), owningDevice(owningDevice){
swapchain = makeSwapchain(surface, width, height, owningDevice);
swapchain = makeSwapchain(width, height);
currentSize = {width, height};
}

Expand All @@ -28,7 +28,7 @@ namespace RGL{

void SwapchainWG::Resize(uint32_t width, uint32_t height){
wgpuSwapChainRelease(swapchain);
swapchain = makeSwapchain(surface, width, height, owningDevice);
swapchain = makeSwapchain(width, height);
currentSize = {width, height};
}

Expand All @@ -50,7 +50,8 @@ namespace RGL{
}

void SwapchainWG::SetVsyncMode(bool mode){
// TODO: implement vsync for webgpu
vsync = mode;
Resize(currentSize.width, currentSize.height);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/WGSwapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ namespace RGL{
uint32_t idx = 0;

void SetVsyncMode(bool mode) final;
private:
WGPUSwapChain makeSwapchain(uint32_t width, uint32_t height);
bool vsync = true;
};
}

0 comments on commit b3f2099

Please sign in to comment.