Skip to content

Commit

Permalink
Fix black screen bug
Browse files Browse the repository at this point in the history
Improve the borderless fullscreen to support any game resolution
  • Loading branch information
dpjudas committed Feb 10, 2024
1 parent e3aefd7 commit 1f1163d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 29 deletions.
2 changes: 1 addition & 1 deletion VulkanDrv/CommandBufferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void CommandBufferManager::SubmitCommands(bool present, int presentWidth, int pr
PresentImageIndex = SwapChain->AcquireImage(ImageAvailableSemaphore.get());
if (PresentImageIndex != -1)
{
renderer->DrawPresentTexture(0, 0, presentWidth, presentHeight);
renderer->DrawPresentTexture(presentWidth, presentHeight);
}
}

Expand Down
2 changes: 1 addition & 1 deletion VulkanDrv/FramebufferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void FramebufferManager::CreateSwapChainFramebuffers()
SwapChainFramebuffers.push_back(
FramebufferBuilder()
.RenderPass(renderer->RenderPasses->Present.RenderPass.get())
.Size(renderer->Textures->Scene->Width, renderer->Textures->Scene->Height)
.Size(renderer->Commands->SwapChain->Width(), renderer->Commands->SwapChain->Height())
.AddAttachment(swapchain->GetImageView(i))
.DebugName("SwapChainFramebuffer")
.Create(renderer->Device.get()));
Expand Down
2 changes: 1 addition & 1 deletion VulkanDrv/RenderPassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void RenderPassManager::BeginPresent(VulkanCommandBuffer* cmdbuffer)
RenderPassBegin()
.RenderPass(Present.RenderPass.get())
.Framebuffer(renderer->Framebuffers->GetSwapChainFramebuffer())
.RenderArea(0, 0, renderer->Textures->Scene->Width, renderer->Textures->Scene->Height)
.RenderArea(0, 0, renderer->Commands->SwapChain->Width(), renderer->Commands->SwapChain->Height())
.AddClearColor(0.0f, 0.0f, 0.0f, 1.0f)
.Execute(cmdbuffer);
}
Expand Down
87 changes: 62 additions & 25 deletions VulkanDrv/UVulkanRenderDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,46 @@ UBOOL UVulkanRenderDevice::SetRes(INT NewX, INT NewY, INT NewColorBytes, UBOOL F
if (NewX == 0 || NewY == 0)
return 1;

if (!Viewport->ResizeViewport(Fullscreen ? (BLIT_Fullscreen | BLIT_OpenGL) : (BLIT_HardwarePaint | BLIT_OpenGL), NewX, NewY, NewColorBytes))
if (!Fullscreen && FullscreenState.Enabled) // Leaving fullscreen
{
// Restore old state
SetWindowLong((HWND)Viewport->GetWindow(), GWL_STYLE, FullscreenState.Style);
SetWindowLong((HWND)Viewport->GetWindow(), GWL_EXSTYLE, FullscreenState.ExStyle);
SetWindowPos(
(HWND)Viewport->GetWindow(),
HWND_TOP,
FullscreenState.WindowPos.left,
FullscreenState.WindowPos.top,
FullscreenState.WindowPos.right - FullscreenState.WindowPos.left,
FullscreenState.WindowPos.bottom - FullscreenState.WindowPos.top,
SWP_FRAMECHANGED | SWP_NOSENDCHANGING | SWP_NOACTIVATE | SWP_NOZORDER);

FullscreenState.Enabled = false;
}

if (!Viewport->ResizeViewport(Fullscreen ? (BLIT_Fullscreen | BLIT_Direct3D) : (BLIT_HardwarePaint | BLIT_Direct3D), NewX, NewY, NewColorBytes))
return 0;

#ifdef USE_HORRIBLE_WIN32_MODE_SWITCHES
if (Fullscreen)
if (Fullscreen && !FullscreenState.Enabled) // Entering fullscreen
{
DEVMODE devmode = {};
devmode.dmSize = sizeof(DEVMODE);
devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT; // | DM_DISPLAYFREQUENCY
devmode.dmPelsWidth = NewX;
devmode.dmPelsHeight = NewY;
// devmode.dmDisplayFrequency = 360;
ChangeDisplaySettingsEx(nullptr, &devmode, 0, CDS_FULLSCREEN, 0);
// Save old state
GetWindowRect((HWND)Viewport->GetWindow(), &FullscreenState.WindowPos);
FullscreenState.Style = GetWindowLong((HWND)Viewport->GetWindow(), GWL_STYLE);
FullscreenState.ExStyle = GetWindowLong((HWND)Viewport->GetWindow(), GWL_EXSTYLE);

// Find primary monitor resolution
HDC screenDC = GetDC(0);
int screenWidth = GetDeviceCaps(screenDC, HORZRES);
int screenHeight = GetDeviceCaps(screenDC, VERTRES);
ReleaseDC(0, screenDC);

// Create borderless full screen window (our present shader will letterbox any resolution to fit)
SetWindowLong((HWND)Viewport->GetWindow(), GWL_STYLE, WS_OVERLAPPED | WS_VISIBLE);
SetWindowLong((HWND)Viewport->GetWindow(), GWL_EXSTYLE, WS_EX_APPWINDOW);
SetWindowPos((HWND)Viewport->GetWindow(), HWND_TOP, 0, 0, screenWidth, screenHeight, SWP_FRAMECHANGED | SWP_NOSENDCHANGING | SWP_NOACTIVATE | SWP_NOZORDER);

FullscreenState.Enabled = true;
}
#endif

SaveConfig();

Expand All @@ -263,10 +288,6 @@ void UVulkanRenderDevice::Exit()

if (Device) vkDeviceWaitIdle(Device->device);

#ifdef USE_HORRIBLE_WIN32_MODE_SWITCHES
ChangeDisplaySettingsEx(nullptr, nullptr, 0, 0, 0);
#endif

Framebuffers.reset();
RenderPasses.reset();
DescriptorSets.reset();
Expand Down Expand Up @@ -405,7 +426,6 @@ UBOOL UVulkanRenderDevice::Exec(const TCHAR* Cmd, FOutputDevice& Ar)
resolutions.insert({ screenWidth, screenHeight });
ReleaseDC(0, screenDC);

#ifdef USE_HORRIBLE_WIN32_MODE_SWITCHES
// Get what else is available according to Windows
DEVMODE devmode = {};
devmode.dmSize = sizeof(DEVMODE);
Expand All @@ -424,11 +444,14 @@ UBOOL UVulkanRenderDevice::Exec(const TCHAR* Cmd, FOutputDevice& Ar)
// Add a letterboxed 4:3 mode for widescreen monitors
resolutions.insert({ (screenHeight * 4 + 2) / 3, screenHeight });

// Add a letterboxed 16:9 mode for ultra widescreen monitors
resolutions.insert({ (screenHeight * 16 + 8) / 9, screenHeight });

// Include a few classics from the era
resolutions.insert({ 800, 600 });
resolutions.insert({ 1024, 768 });
resolutions.insert({ 1280, 1024 });
resolutions.insert({ 1600, 1200 });
#endif
#else
resolutions.insert({ 800, 600 });
resolutions.insert({ 1280, 720 });
Expand Down Expand Up @@ -571,7 +594,10 @@ void UVulkanRenderDevice::Unlock(UBOOL Blit)
{
RunBloomPass();
}
SubmitAndWait(Blit ? true : false, Viewport->SizeX, Viewport->SizeY, Viewport->IsFullscreen());

RECT box = {};
GetClientRect((HWND)Viewport->GetWindow(), &box);
SubmitAndWait(Blit ? true : false, box.right, box.bottom, Viewport->IsFullscreen());

if (Samplers->LODBias != LODBias)
{
Expand Down Expand Up @@ -778,6 +804,9 @@ void UVulkanRenderDevice::DrawComplexSurface(FSceneNode* Frame, FSurfaceInfo& Su
icount += (vcount - 2) * 3;
}

SceneVertexPos = vpos;
SceneIndexPos = ipos + icount;

Stats.ComplexSurfaces++;

if (!GIsEditor || (Surface.PolyFlags & (PF_Selected | PF_FlatShaded)) == 0)
Expand Down Expand Up @@ -1893,7 +1922,7 @@ PresentPushConstants UVulkanRenderDevice::GetPresentPushConstants()
return pushconstants;
}

void UVulkanRenderDevice::DrawPresentTexture(int x, int y, int width, int height)
void UVulkanRenderDevice::DrawPresentTexture(int width, int height)
{
PresentPushConstants pushconstants = GetPresentPushConstants();

Expand All @@ -1905,17 +1934,25 @@ void UVulkanRenderDevice::DrawPresentTexture(int x, int y, int width, int height
if (GammaMode == 1) presentShader |= 2;
if (pushconstants.Brightness != 0.0f || pushconstants.Contrast != 1.0f || pushconstants.Saturation != 1.0f) presentShader |= (Clamp(GrayFormula, 0, 2) + 1) << 2;

float scale = std::min(width / (float)Viewport->SizeX, height / (float)Viewport->SizeY);
int letterboxWidth = (int)std::round(Viewport->SizeX * scale);
int letterboxHeight = (int)std::round(Viewport->SizeY * scale);
int letterboxX = (width - letterboxWidth) / 2;
int letterboxY = (height - letterboxHeight) / 2;

VkViewport viewport = {};
viewport.x = x;
viewport.y = y;
viewport.width = width;
viewport.height = height;
viewport.x = letterboxX;
viewport.y = letterboxY;
viewport.width = letterboxWidth;
viewport.height = letterboxHeight;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;

VkRect2D scissor = {};
scissor.extent.width = width;
scissor.extent.height = height;
scissor.offset.x = letterboxX;
scissor.offset.y = letterboxY;
scissor.extent.width = letterboxWidth;
scissor.extent.height = letterboxHeight;

auto cmdbuffer = Commands->GetDrawCommands();

Expand Down
10 changes: 9 additions & 1 deletion VulkanDrv/UVulkanRenderDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class UVulkanRenderDevice : public URenderDevice
static float ComputeBlurGaussian(float n, float theta);
static void ComputeBlurSamples(int sampleCount, float blurAmount, float* sampleWeights);

void DrawPresentTexture(int x, int y, int width, int height);
void DrawPresentTexture(int width, int height);
PresentPushConstants GetPresentPushConstants();

struct
Expand Down Expand Up @@ -188,6 +188,14 @@ class UVulkanRenderDevice : public URenderDevice

int ForceHitIndex = -1;
HitQuery ForceHit;

struct
{
RECT WindowPos = {};
LONG Style = 0;
LONG ExStyle = 0;
bool Enabled = false;
} FullscreenState;
};

inline void UVulkanRenderDevice::SetPipeline(VulkanPipeline* pipeline)
Expand Down

0 comments on commit 1f1163d

Please sign in to comment.