Skip to content

Commit f1a6c37

Browse files
committed
vulkan: fix wayland create swapchain fail width/height is 0
1 parent 98f005f commit f1a6c37

File tree

4 files changed

+56
-33
lines changed

4 files changed

+56
-33
lines changed

axmol/platform/desktop/RenderViewImpl.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -637,17 +637,6 @@ bool RenderViewImpl::initWithRect(std::string_view viewName,
637637
// Init driver after load GL
638638
axdrv;
639639
glfwSetWindowUserPointer(_mainWindow, rhi::gl::__state);
640-
#elif AX_RENDER_API == AX_RENDER_API_VK
641-
auto driver = static_cast<ax::rhi::vk::DriverImpl*>(axdrv);
642-
bool ok = driver->setupSurface(_mainWindow, [](VkInstance inst, void* window, VkSurfaceKHR* surface) {
643-
return glfwCreateWindowSurface(inst, static_cast<GLFWwindow*>(window), nullptr, surface);
644-
});
645-
if (!ok)
646-
{
647-
AXLOGE("Failed to create Vulkan window surface.");
648-
return false;
649-
}
650-
_vkSurface = driver->getSurface();
651640
#endif
652641

653642
/*
@@ -665,6 +654,22 @@ bool RenderViewImpl::initWithRect(std::string_view viewName,
665654
glfwGetFramebufferSize(_mainWindow, &fbWidth, &fbHeight);
666655
updateRenderSurface(fbWidth, fbHeight, SurfaceUpdateFlag::RenderSizeChanged | SurfaceUpdateFlag::SilentUpdate);
667656

657+
#if AX_RENDER_API == AX_RENDER_API_VK
658+
auto _createSurface = [](VkInstance inst, void* window, VkSurfaceKHR* surface) {
659+
return glfwCreateWindowSurface(inst, static_cast<GLFWwindow*>(window), nullptr, surface);
660+
};
661+
auto driver = static_cast<ax::rhi::vk::DriverImpl*>(axdrv);
662+
const rhi::vk::SurfaceCreateInfo createInfo{
663+
.window = _mainWindow, .width = fbWidth, .height = fbHeight, .createFunc = _createSurface};
664+
bool ok = driver->setupSurface(createInfo);
665+
if (!ok)
666+
{
667+
AXLOGE("Failed to create Vulkan window surface.");
668+
return false;
669+
}
670+
_vkSurface = driver->getSurface();
671+
#endif
672+
668673
int w, h;
669674
glfwGetWindowSize(_mainWindow, &w, &h);
670675
updateScaledWindowSize(w, h, SurfaceUpdateFlag::WindowSizeChanged | SurfaceUpdateFlag::SilentUpdate);

axmol/rhi/vulkan/DriverVK.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,15 @@ void DriverImpl::initializeFactory()
293293
#elif AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
294294
extensions.push_back("VK_KHR_android_surface");
295295
#elif AX_TARGET_PLATFORM == AX_PLATFORM_LINUX
296-
glfwInit();
297-
auto platform = glfwGetPlatform();
298-
if (platform == GLFW_PLATFORM_WAYLAND)
296+
const char* waylandDisplay = getenv("WAYLAND_DISPLAY");
297+
const char* x11Display = getenv("DISPLAY");
298+
if (waylandDisplay)
299299
extensions.push_back("VK_KHR_wayland_surface");
300-
else if (platform == GLFW_PLATFORM_X11)
300+
else if (x11Display)
301+
extensions.push_back("VK_KHR_xcb_surface");
302+
else
301303
{
302-
extensions.push_back("VK_KHR_xcb_surface"); // glfw preferred xcb
303-
extensions.push_back("VK_KHR_xlib_surface");
304-
}
305-
else {
306-
AXLOGE("Unsupported window platform: {}", (int)platform);
304+
AXLOGE("Unsupported window platform: neither WAYLAND_DISPLAY nor DISPLAY found");
307305
assert(false);
308306
}
309307
#endif
@@ -403,12 +401,15 @@ void DriverImpl::initializeDevice()
403401
"vkCreateCommandPool failed for transient pool");
404402
}
405403

406-
bool DriverImpl::setupSurface(void* window, CreateSurfaceFunc func)
404+
bool DriverImpl::setupSurface(const SurfaceCreateInfo& info)
407405
{
408-
auto result = func(_factory, window, &_surface);
406+
auto result = info.createFunc(_factory, info.window, &_surface);
409407
if (result != VK_SUCCESS)
410408
return false;
411409

410+
_surfaceInitalExtent.width = info.width;
411+
_surfaceInitalExtent.height = info.height;
412+
412413
uint32_t queueCount = 0;
413414
vkGetPhysicalDeviceQueueFamilyProperties(_physical, &queueCount, nullptr);
414415
std::vector<VkQueueFamilyProperties> qprops(queueCount);

axmol/rhi/vulkan/DriverVK.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ struct DisposableResource
5757
};
5858

5959
using CreateSurfaceFunc = std::function<VkResult(VkInstance, void* window, VkSurfaceKHR* surface)>;
60+
struct SurfaceCreateInfo
61+
{
62+
void* window{};
63+
int width{0};
64+
int height{0};
65+
CreateSurfaceFunc createFunc{};
66+
};
67+
6068
class DriverImpl : public DriverBase
6169
{
6270
friend class RenderContextImpl;
@@ -72,9 +80,11 @@ class DriverImpl : public DriverBase
7280

7381
void init();
7482

75-
bool setupSurface(void* window, CreateSurfaceFunc);
83+
bool setupSurface(const SurfaceCreateInfo& info);
7684
VkSurfaceKHR getSurface() const { return _surface; }
7785

86+
const VkExtent2D& getInitialSurfaceExtent() const { return _surfaceInitalExtent; }
87+
7888
RenderContext* createRenderContext(void* surfaceContext) override;
7989
Buffer* createBuffer(std::size_t size, BufferType type, BufferUsage usage, const void* initial) override;
8090
Texture* createTexture(const TextureDesc& descriptor) override;
@@ -133,6 +143,7 @@ class DriverImpl : public DriverBase
133143
VkPhysicalDevice _physical{VK_NULL_HANDLE};
134144
VkDevice _device{VK_NULL_HANDLE};
135145
VkSurfaceKHR _surface{VK_NULL_HANDLE};
146+
VkExtent2D _surfaceInitalExtent{};
136147

137148
VkQueue _graphicsQueue{VK_NULL_HANDLE};
138149
VkQueue _presentQueue{VK_NULL_HANDLE};

axmol/rhi/vulkan/RenderContextVK.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ RenderContextImpl::RenderContextImpl(DriverImpl* driver, VkSurfaceKHR surface)
9797
VkResult vr = vkCreateCommandPool(_device, &poolInfo, nullptr, &_commandPool);
9898
AXASSERT(vr == VK_SUCCESS, "vkCreateCommandPool failed");
9999

100+
auto& extent = driver->getInitialSurfaceExtent();
101+
_screenWidth = extent.width;
102+
_screenHeight = extent.height;
103+
100104
createCommandBuffers();
101105
createDescriptorPool();
102106
rebuildSwapchain();
@@ -404,7 +408,9 @@ void RenderContextImpl::rebuildSwapchain()
404408
extent.height = _screenHeight;
405409
}
406410
if (extent.width == 0 || extent.height == 0)
407-
return;
411+
{
412+
AXLOGE("axmol: Failed to create swapchain: extent width/height is 0");
413+
}
408414

409415
uint32_t imageCount = caps.minImageCount + 1;
410416
if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount)
@@ -525,7 +531,7 @@ bool RenderContextImpl::beginFrame()
525531
if (result == VK_SUBOPTIMAL_KHR && !_suboptimal)
526532
{
527533
_suboptimal = true;
528-
AXLOGW("Vulkan Driver: Suboptimal swap chain.");
534+
AXLOGW("axmol: Suboptimal swap chain.");
529535
}
530536
AXASSERT(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR || result == VK_TIMEOUT,
531537
"vkAcquireNextImageKHR failed");
@@ -630,12 +636,12 @@ void RenderContextImpl::endFrame()
630636
case VK_SUBOPTIMAL_KHR:
631637
if (!_suboptimal)
632638
{
633-
AXLOGW("Vulkan Driver: Suboptimal swap chain.");
639+
AXLOGW("axmol: Suboptimal swap chain.");
634640
_suboptimal = true;
635641
}
636642
break;
637643
case VK_ERROR_OUT_OF_DATE_KHR:
638-
AXLOGD("Vulkan Driver: swapchain out of date");
644+
AXLOGD("axmol: Swapchain out of date");
639645
break;
640646
default:
641647
AXASSERT(vr && false, "vkQueuePresentKHR failed");
@@ -854,12 +860,12 @@ void RenderContextImpl::prepareDrawing()
854860
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
855861

856862
VkWriteDescriptorSet& write = writes.emplace_back();
857-
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
858-
write.dstSet = descriptorSets[1];
859-
write.dstBinding = bindingIndex + k; // preserve shader binding order
860-
write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
861-
write.descriptorCount = 1;
862-
write.pImageInfo = &imageInfo;
863+
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
864+
write.dstSet = descriptorSets[1];
865+
write.dstBinding = bindingIndex + k; // preserve shader binding order
866+
write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
867+
write.descriptorCount = 1;
868+
write.pImageInfo = &imageInfo;
863869
}
864870
}
865871

0 commit comments

Comments
 (0)