Skip to content

Commit

Permalink
Fix VSync being forced enabled incorrectly (if driver lacks support f…
Browse files Browse the repository at this point in the history
…or Mailbox Mode)
  • Loading branch information
thr3343 committed Sep 25, 2023
1 parent e10ec50 commit 6599a58
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/vulkanmod/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Config {

public int frameQueueSize = 2;
public VideoResolution resolution = VideoResolution.getFirstAvailable();
public boolean useImmediate = !VideoResolution.isWayLand(); //Necessary until tearing-control-unstable-v1 is fully implemented on all GPU Drivers for Wayland
public boolean windowedFullscreen = false;
public boolean guiOptimizations = false;
public int advCulling = 2;
Expand All @@ -27,7 +28,6 @@ public class Config {
.setPrettyPrinting()
.excludeFieldsWithModifiers(Modifier.PRIVATE)
.create();
public boolean useImmediate = !VideoResolution.isWayLand();

public static Config load(Path path) {
Config config;
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/net/vulkanmod/vulkan/framebuffer/SwapChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Arrays;
import java.util.List;

import static net.vulkanmod.Initializer.LOGGER;
import static net.vulkanmod.vulkan.Vulkan.*;
import static net.vulkanmod.vulkan.util.VUtil.UINT32_MAX;
import static org.lwjgl.glfw.GLFW.glfwGetFramebufferSize;
Expand All @@ -31,13 +30,14 @@
public class SwapChain extends Framebuffer {
private static int DEFAULT_DEPTH_FORMAT = 0;

//Drivers are not guaranteed to support Mailbox Mode
private static final int defNoTearMode = checkDisplayMode(VK_PRESENT_MODE_MAILBOX_KHR) ? VK_PRESENT_MODE_MAILBOX_KHR : VK_PRESENT_MODE_IMMEDIATE_KHR;

public static int getDefaultDepthFormat() {
return DEFAULT_DEPTH_FORMAT;
}

private RenderPass renderPass;
//Necessary until tearing-control-unstable-v1 is fully implemented on all GPU Drivers for Wayland
private static final int defUncappedMode = VideoResolution.isWayLand() ? VK_PRESENT_MODE_MAILBOX_KHR : VK_PRESENT_MODE_IMMEDIATE_KHR;
private long[] framebuffers;
private long swapChain = VK_NULL_HANDLE;
private List<VulkanImage> swapChainImages;
Expand All @@ -53,10 +53,25 @@ public SwapChain() {
this.attachmentCount = 2;

this.depthFormat = DEFAULT_DEPTH_FORMAT;

createSwapChain();

}

private static boolean checkDisplayMode(int requestedMode) {
try(MemoryStack stack = MemoryStack.stackPush()) {
IntBuffer availablePresentModes = Device.querySurfaceProperties(getDevice().getPhysicalDevice(), stack).presentModes;

for (int i = 0; i < availablePresentModes.capacity(); i++) {
if (availablePresentModes.get(i) == requestedMode) {
return true;
}
}
return false;
}

}

public int recreateSwapChain() {
Synchronization.INSTANCE.waitFences();

Expand Down Expand Up @@ -357,11 +372,12 @@ private VkSurfaceFormatKHR getFormat(VkSurfaceFormatKHR.Buffer availableFormats)

private int getPresentMode(IntBuffer availablePresentModes) {

int requestedMode = Initializer.CONFIG.useImmediate ? VK_PRESENT_MODE_IMMEDIATE_KHR : VK_PRESENT_MODE_MAILBOX_KHR;
int requestedMode = vsync ? VK_PRESENT_MODE_FIFO_KHR
: Initializer.CONFIG.useImmediate ? VK_PRESENT_MODE_IMMEDIATE_KHR : defNoTearMode;

//fifo mode is the only mode that has to be supported
if (vsync) {
return VK_PRESENT_MODE_FIFO_KHR;
return requestedMode;
}

//Available Display modes in fullscreen and windowed can vary, so we can't optimise out this loop
Expand Down

0 comments on commit 6599a58

Please sign in to comment.