Calculate only one composition strategy in the fragment shader

Use defines instead of mix() avoiding doing a bunch or per pixel work on
the GPU
This commit is contained in:
Kovid Goyal
2023-05-24 21:09:28 +05:30
parent b0b65c452e
commit 42edc3ebc8
3 changed files with 14 additions and 6 deletions

View File

@@ -2,6 +2,7 @@
#define {WHICH_PROGRAM}
#define NOT_TRANSPARENT
#define NO_FG_OVERRIDE
#define TEXT_NEW_GAMMA
#if defined(SIMPLE) || defined(BACKGROUND) || defined(SPECIAL)
#define NEEDS_BACKROUND
@@ -21,7 +22,6 @@ in float bg_alpha;
#ifdef NEEDS_FOREGROUND
uniform sampler2DArray sprites;
uniform int text_old_gamma;
uniform float text_contrast;
uniform float text_gamma_adjustment;
uniform float text_fg_override_threshold;
@@ -195,7 +195,11 @@ vec4 calculate_foreground(vec3 bg) {
// When rendering on a background we can adjust the alpha channel contrast
// to improve legibility based on the source and destination colors
vec4 text_fg = foreground_color();
text_fg = mix(foreground_contrast(text_fg, bg), foreground_contrast_incorrect(text_fg, bg), text_old_gamma);
#ifdef TEXT_OLD_GAMMA
text_fg = foreground_contrast_incorrect(text_fg, bg);
#else
text_fg = foreground_contrast(text_fg, bg);
#endif
return foreground_with_decorations(text_fg);
}

View File

@@ -570,8 +570,6 @@ set_cell_uniforms(float current_inactive_text_alpha, bool force) {
S(CELL_PROGRAM, sprites, SPRITE_MAP_UNIT, 1i); S(CELL_FG_PROGRAM, sprites, SPRITE_MAP_UNIT, 1i);
S(CELL_PROGRAM, dim_opacity, OPT(dim_opacity), 1f); S(CELL_FG_PROGRAM, dim_opacity, OPT(dim_opacity), 1f);
S(CELL_BG_PROGRAM, defaultbg, OPT(background), 1f);
int text_old_gamma = OPT(text_old_gamma) ? 1 : 0;
S(CELL_PROGRAM, text_old_gamma, text_old_gamma, 1i); S(CELL_FG_PROGRAM, text_old_gamma, text_old_gamma, 1i);
float text_contrast = 1.0f + OPT(text_contrast) * 0.01f;
S(CELL_PROGRAM, text_contrast, text_contrast, 1f); S(CELL_FG_PROGRAM, text_contrast, text_contrast, 1f);
float text_gamma_adjustment = OPT(text_gamma_adjustment) < 0.01f ? 1.0f : 1.0f / OPT(text_gamma_adjustment);

View File

@@ -375,11 +375,13 @@ def multi_replace(src: str, **replacements: Any) -> str:
class LoadShaderPrograms:
text_fg_override_threshold: float = 0
text_old_gamma: bool = False
semi_transparent: bool = False
@property
def needs_recompile(self) -> bool:
return get_options().text_fg_override_threshold != self.text_fg_override_threshold
opts = get_options()
return opts.text_fg_override_threshold != self.text_fg_override_threshold or (opts.text_composition_strategy == 'legacy') != self.text_old_gamma
def recompile_if_needed(self) -> None:
if self.needs_recompile:
@@ -387,6 +389,9 @@ class LoadShaderPrograms:
def __call__(self, semi_transparent: bool = False, allow_recompile: bool = False) -> None:
self.semi_transparent = semi_transparent
opts = get_options()
self.text_old_gamma = opts.text_composition_strategy == 'legacy'
self.text_fg_override_threshold = opts.text_fg_override_threshold
compile_program(BLIT_PROGRAM, *load_shaders('blit'), allow_recompile)
v, f = load_shaders('cell')
@@ -409,9 +414,10 @@ class LoadShaderPrograms:
DECORATION_MASK=DECORATION_MASK,
STRIKE_SPRITE_INDEX=NUM_UNDERLINE_STYLES + 1,
)
self.text_fg_override_threshold = get_options().text_fg_override_threshold
if self.text_fg_override_threshold != 0.:
ff = ff.replace('#define NO_FG_OVERRIDE', '#define FG_OVERRIDE')
if self.text_old_gamma:
ff = ff.replace('#define TEXT_NEW_GAMMA', '#define TEXT_OLD_GAMMA')
if semi_transparent:
vv = vv.replace('#define NOT_TRANSPARENT', '#define TRANSPARENT')
ff = ff.replace('#define NOT_TRANSPARENT', '#define TRANSPARENT')