fix: proper/somewhat decent gamma and rendering fixes

This commit is contained in:
NOTPIES
2026-03-03 13:31:01 -03:00
parent ea25036375
commit bd535e5b49
4 changed files with 355 additions and 42 deletions

View File

@@ -37,8 +37,11 @@
#include "Resource.h"
#include "..\..\Minecraft.World\compression.h"
#include "..\..\Minecraft.World\OldChunkStorage.h"
#include "Network\WinsockNetLayer.h"
#include "Windows64_PostProcess.h"
#include "Xbox/resource.h"
HINSTANCE hMyInst;
@@ -343,54 +346,23 @@ ID3D11Device* g_pd3dDevice = NULL;
ID3D11DeviceContext* g_pImmediateContext = NULL;
IDXGISwapChain* g_pSwapChain = NULL;
static WORD g_originalGammaRamp[3][256];
static bool g_gammaRampSaved = false;
ID3D11RenderTargetView* g_pRenderTargetView = NULL;
ID3D11DepthStencilView* g_pDepthStencilView = NULL;
ID3D11Texture2D* g_pDepthStencilBuffer = NULL;
void Windows64_UpdateGamma(unsigned short usGamma)
{
if (!g_hWnd) return;
HDC hdc = GetDC(g_hWnd);
if (!hdc) return;
if (!g_gammaRampSaved)
{
GetDeviceGammaRamp(hdc, g_originalGammaRamp);
g_gammaRampSaved = true;
}
float gamma = (float)usGamma / 32768.0f;
if (gamma < 0.01f) gamma = 0.01f;
if (gamma < 0.0f) gamma = 0.0f;
if (gamma > 1.0f) gamma = 1.0f;
float invGamma = 1.0f / (0.5f + gamma * 0.5f);
WORD ramp[3][256];
for (int i = 0; i < 256; i++)
{
float normalized = (float)i / 255.0f;
float corrected = powf(normalized, invGamma);
WORD val = (WORD)(corrected * 65535.0f + 0.5f);
ramp[0][i] = val;
ramp[1][i] = val;
ramp[2][i] = val;
}
SetDeviceGammaRamp(hdc, ramp);
ReleaseDC(g_hWnd, hdc);
SetGammaValue(0.5f * powf(4.0f, gamma));
}
void Windows64_RestoreGamma()
{
if (!g_gammaRampSaved || !g_hWnd) return;
HDC hdc = GetDC(g_hWnd);
if (!hdc) return;
SetDeviceGammaRamp(hdc, g_originalGammaRamp);
ReleaseDC(g_hWnd, hdc);
}
ID3D11RenderTargetView* g_pRenderTargetView = NULL;
ID3D11DepthStencilView* g_pDepthStencilView = NULL;
ID3D11Texture2D* g_pDepthStencilBuffer = NULL;
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
@@ -712,6 +684,7 @@ app.DebugPrintf("width: %d, height: %d\n", width, height);
// Create a depth stencil buffer
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = width;
descDepth.Height = height;
@@ -727,11 +700,10 @@ app.DebugPrintf("width: %d, height: %d\n", width, height);
hr = g_pd3dDevice->CreateTexture2D(&descDepth, NULL, &g_pDepthStencilBuffer);
D3D11_DEPTH_STENCIL_VIEW_DESC descDSView;
ZeroMemory(&descDSView, sizeof(descDSView));
descDSView.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDSView.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSView.Texture2D.MipSlice = 0;
// when would this ever be a non-garbage value?
descDSView.Flags = 0;
hr = g_pd3dDevice->CreateDepthStencilView(g_pDepthStencilBuffer, &descDSView, &g_pDepthStencilView);
@@ -777,6 +749,8 @@ void CleanupDevice()
extern void Windows64_RestoreGamma();
Windows64_RestoreGamma();
CleanupGammaPostProcess();
if( g_pImmediateContext ) g_pImmediateContext->ClearState();
if( g_pRenderTargetView ) g_pRenderTargetView->Release();
@@ -861,6 +835,8 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
return 0;
}
InitGammaPostProcess();
#if 0
// Main message loop
MSG msg = {0};
@@ -1337,6 +1313,8 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
RenderManager.Set_matrixDirty();
#endif
ApplyGammaPostProcess();
// Present the frame.
RenderManager.Present();