Skip to content

Commit

Permalink
* fixed brightness not working if resolution scaled is used, also opt…
Browse files Browse the repository at this point in the history
…imized LUT generation so it's not lagging anymore
  • Loading branch information
iOrange committed Dec 28, 2024
1 parent 12173d5 commit eff60b4
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions Wrappers/d3d8/IDirect3DDevice8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "d3d8wrapper.h"
#include <shlwapi.h>
#include <chrono>
#include <array>
#include <deque>
#include <ctime>
#include <numeric>
Expand Down Expand Up @@ -1579,6 +1580,11 @@ HRESULT m_IDirect3DDevice8::Present(CONST RECT* pSourceRect, CONST RECT* pDestRe
OverlayRef.DrawOverlays(ProxyInterface, BufferWidth, BufferHeight);
}

if (RestoreBrightnessSelector)
{
ApplyBrightnessLevel();
}

bool PauseMenuFlag = false;
if (IsScaledResolutionsEnabled())
{
Expand All @@ -1589,11 +1595,6 @@ HRESULT m_IDirect3DDevice8::Present(CONST RECT* pSourceRect, CONST RECT* pDestRe
DrawScaledSurface();
}

if (RestoreBrightnessSelector)
{
ApplyBrightnessLevel();
}

// Endscene
isInScene = false;
ProxyInterface->EndScene();
Expand Down Expand Up @@ -3660,28 +3661,25 @@ void m_IDirect3DDevice8::OnSetBrightnessLevel(const DWORD level)

D3DLOCKED_BOX lockedBox{};
HRESULT hr = GammaRampLUT->LockBox(0, &lockedBox, nullptr, 0);

if (SUCCEEDED(hr)) {
const float gamma = 1.0f / gammaValues[BrightnessLevel];
const float gain = gainValues[BrightnessLevel];

std::array<BYTE, kGammaRampLUTDim> rampValues;
for (UINT i = 0; i < kGammaRampLUTDim; ++i) {
float value = static_cast<float>(i) / static_cast<float>(kGammaRampLUTDim - 1);
value = powf(fabs(value * gain), gamma);
rampValues[i] = static_cast<BYTE>((std::min)(255.0f, (std::max)(0.0f, value * 255.0f)));
}

BYTE* pixels = reinterpret_cast<BYTE*>(lockedBox.pBits);
for (UINT z = 0u; z < kGammaRampLUTDim; ++z) {
float blue = static_cast<float>(z) / static_cast<float>(kGammaRampLUTDim - 1);
blue = powf(fabs(blue * gain), gamma);

for (UINT y = 0u; y < kGammaRampLUTDim; ++y) {
float green = static_cast<float>(y) / static_cast<float>(kGammaRampLUTDim - 1);
green = powf(fabs(green * gain), gamma);

for (UINT x = 0u; x < kGammaRampLUTDim; ++x, pixels += 4) {
float red = static_cast<float>(x) / static_cast<float>(kGammaRampLUTDim - 1);
red = powf(fabs(red * gain), gamma);

// ARGB
pixels[0] = static_cast<BYTE>((std::min)(255.0f, (std::max)(0.0f, blue * 255.0f)));
pixels[1] = static_cast<BYTE>((std::min)(255.0f, (std::max)(0.0f, green * 255.0f)));
pixels[2] = static_cast<BYTE>((std::min)(255.0f, (std::max)(0.0f, red * 255.0f)));
pixels[0] = rampValues[z];
pixels[1] = rampValues[y];
pixels[2] = rampValues[x];
pixels[3] = 0xFF; // alpha
}
}
Expand Down

0 comments on commit eff60b4

Please sign in to comment.