Skip to content

Commit ebbfa9d

Browse files
authored
Remove deprecated WGPU swapchain, allow window to resize (#6)
* Switch to no-swapchain approach * Add comment
1 parent eaddd45 commit ebbfa9d

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

simulator/simulator.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ namespace mrover {
273273
wgpu::Device mDevice;
274274
std::unique_ptr<wgpu::ErrorCallback> mErrorCallback;
275275
wgpu::Queue mQueue;
276-
wgpu::SwapChain mSwapChain;
277276
wgpu::Texture mDepthTexture;
278277
wgpu::TextureView mDepthTextureView;
279278
wgpu::Texture mNormalTexture;

simulator/simulator.render.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ namespace mrover {
3535
}
3636

3737
auto Simulator::makeFramebuffers(int width, int height) -> void {
38-
wgpu::SwapChainDescriptor descriptor;
39-
descriptor.usage = wgpu::TextureUsage::RenderAttachment;
40-
descriptor.format = COLOR_FORMAT;
41-
descriptor.width = width;
42-
descriptor.height = height;
43-
descriptor.presentMode = wgpu::PresentMode::Immediate;
44-
mSwapChain = mDevice.createSwapChain(mSurface, descriptor);
45-
if (!mSwapChain) throw std::runtime_error("Failed to create WGPU swap chain");
38+
wgpu::SurfaceConfiguration surfaceConfiguration;
39+
surfaceConfiguration.device = mDevice;
40+
surfaceConfiguration.format = COLOR_FORMAT;
41+
surfaceConfiguration.usage = wgpu::TextureUsage::RenderAttachment;
42+
surfaceConfiguration.width = width;
43+
surfaceConfiguration.height = height;
44+
surfaceConfiguration.alphaMode = wgpu::CompositeAlphaMode::Auto;
45+
surfaceConfiguration.presentMode = wgpu::PresentMode::Immediate;
46+
mSurface.configure(surfaceConfiguration);
4647
std::tie(mNormalTexture, mNormalTextureView) = makeTextureAndView(width, height, NORMAL_FORMAT, wgpu::TextureUsage::RenderAttachment, wgpu::TextureAspect::All);
4748
std::tie(mDepthTexture, mDepthTextureView) = makeTextureAndView(width, height, DEPTH_FORMAT, wgpu::TextureUsage::RenderAttachment, wgpu::TextureAspect::DepthOnly);
4849
}
@@ -203,7 +204,9 @@ namespace mrover {
203204
constexpr auto WINDOW_NAME = "MRover Simulator (DEBUG BUILD, MAY BE SLOW)";
204205
#endif
205206
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
206-
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
207+
// There is still an off-by-one error on Vulkan with resizing windows and ImGui.
208+
// See: https://matrix.to/#/!ZSOHTEPDbwuEgSJwYw:matrix.org
209+
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
207210
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE);
208211
mWindow = GlfwPointer<GLFWwindow, glfwCreateWindow, glfwDestroyWindow>{w, h, WINDOW_NAME, nullptr, nullptr};
209212
RCLCPP_INFO_STREAM(get_logger(), std::format("Created window of size: {}x{}", w, h));
@@ -258,7 +261,7 @@ namespace mrover {
258261
mNormalTextureView.release();
259262
mNormalTexture.destroy();
260263
mNormalTexture.release();
261-
mSwapChain.release();
264+
mSurface.unconfigure();
262265

263266
makeFramebuffers(width, height);
264267
}
@@ -492,7 +495,7 @@ namespace mrover {
492495
for (int i = 0; i < compound->getNumChildShapes(); ++i) {
493496
SE3d modelInLink = btTransformToSe3(urdfPoseToBtTransform(link->collision_array.at(i)->origin));
494497
SE3d modelInWorld = linkToWorld * modelInLink;
495-
auto* shape = compound->getChildShape(i);
498+
btCollisionShape* shape = compound->getChildShape(i);
496499
if (auto* box = dynamic_cast<btBoxShape const*>(shape)) {
497500
btVector3 extents = box->getHalfExtentsWithoutMargin() * 2;
498501
SIM3 modelToWorld{modelInWorld, R3d{extents.x(), extents.y(), extents.z()}};
@@ -723,10 +726,20 @@ namespace mrover {
723726
camerasUpdate(encoder, colorAttachment, normalAttachment, depthStencilAttachment, renderPassDescriptor);
724727

725728
if (!mIsHeadless) {
726-
wgpu::TextureView nextTexture = mSwapChain.getCurrentTextureView();
727-
if (!nextTexture) throw std::runtime_error("Failed to get WGPU next texture view");
728-
729-
colorAttachment.view = nextTexture;
729+
wgpu::SurfaceTexture surfaceTexture;
730+
mSurface.getCurrentTexture(&surfaceTexture);
731+
if (surfaceTexture.status != wgpu::SurfaceGetCurrentTextureStatus::Success)
732+
throw std::runtime_error{"Failed to get WGPU surface texture"};
733+
734+
wgpu::TextureViewDescriptor nextTextureViewDescriptor;
735+
nextTextureViewDescriptor.format = COLOR_FORMAT;
736+
nextTextureViewDescriptor.dimension = wgpu::TextureViewDimension::_2D;
737+
nextTextureViewDescriptor.mipLevelCount = 1;
738+
nextTextureViewDescriptor.arrayLayerCount = 1;
739+
nextTextureViewDescriptor.aspect = wgpu::TextureAspect::All;
740+
wgpu::TextureView nextTextureView = wgpu::Texture{surfaceTexture.texture}.createView(nextTextureViewDescriptor);
741+
742+
colorAttachment.view = nextTextureView;
730743
normalAttachment.view = mNormalTextureView;
731744
depthStencilAttachment.view = mDepthTextureView;
732745

@@ -768,7 +781,7 @@ namespace mrover {
768781

769782
bindGroup.release();
770783

771-
nextTexture.release();
784+
nextTextureView.release();
772785
}
773786

774787
wgpu::CommandBuffer commands = encoder.finish();
@@ -791,7 +804,7 @@ namespace mrover {
791804
}
792805
#endif
793806

794-
if (!mIsHeadless) mSwapChain.present();
807+
if (!mIsHeadless) mSurface.present();
795808

796809
// TODO(quintin): Remote duplicate code
797810
for (StereoCamera& stereoCamera: mStereoCameras) {

0 commit comments

Comments
 (0)