AMFP Enhanced
C++ reference: the code this unofficial fork added
Loading...
Searching...
No Matches
SoftwareGamma.h
Go to the documentation of this file.
1/*
2 * AMFP-2026: gamma (Options > Gamma) when the display cannot do it. The game sets gamma with
3 * SDL_SetWindowBrightness, a hardware gamma ramp. SDL3 (under sdl2-compat on macOS, and on
4 * Wayland) has no gamma ramps, so the call fails and the slider did nothing. Then the last thing
5 * drawn each frame is a full-screen pass with the same curve SDL's ramp used:
6 * out = in ^ (1 / gamma). Gamma 1 costs nothing. Pure, unit tested in tests/cpp/test_software_gamma.cpp.
7 */
8#ifndef HPL_SOFTWARE_GAMMA_H
9#define HPL_SOFTWARE_GAMMA_H
10
11#include <cmath>
12
13/** The exponent applied to each colour channel (SDL_CalculateGammaRamp: 1 / gamma). */
14inline float SoftwareGammaExponent(float afGamma)
15{
16 if(!(afGamma > 0.1f)) afGamma = 0.1f; // also catches NaN
17 if(afGamma > 10.0f) afGamma = 10.0f;
18 return 1.0f / afGamma;
19}
20
21/** Draw the pass this frame: hardware gamma failed and gamma is not (about) 1. */
22inline bool SoftwareGammaNeeded(bool abHardwareGammaWorks, float afGamma)
23{
24 return !abHardwareGammaWorks && std::fabs(afGamma - 1.0f) > 0.001f;
25}
26
27/** One channel through the curve, 0..1 (what the shader does; for tests and references). */
28inline float SoftwareGammaApply(float afValue, float afGamma)
29{
30 if(afValue <= 0) return 0;
31 if(afValue >= 1) return 1;
32 return std::pow(afValue, SoftwareGammaExponent(afGamma));
33}
34
35// GLSL 1.10 (OpenGL 2.x compatibility contexts, including macOS 2.1): a full-screen quad given
36// in clip space, sampling the copied back buffer.
37static const char* const kSoftwareGammaVS =
38 "void main() { gl_TexCoord[0] = gl_MultiTexCoord0; gl_Position = gl_Vertex; }\n";
39static const char* const kSoftwareGammaFS =
40 "uniform sampler2D tex; uniform float exponent;\n"
41 "void main() { vec3 c = texture2D(tex, gl_TexCoord[0].st).rgb;\n"
42 " gl_FragColor = vec4(pow(max(c, vec3(0.0)), vec3(exponent)), 1.0); }\n";
43
44#endif
static const char *const kSoftwareGammaVS
bool SoftwareGammaNeeded(bool abHardwareGammaWorks, float afGamma)
Draw the pass this frame: hardware gamma failed and gamma is not (about) 1.
static const char *const kSoftwareGammaFS
float SoftwareGammaExponent(float afGamma)
The exponent applied to each colour channel (SDL_CalculateGammaRamp: 1 / gamma).
float SoftwareGammaApply(float afValue, float afGamma)
One channel through the curve, 0..1 (what the shader does; for tests and references).