mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-17 05:32:41 +00:00
Skip foreground samples for blank cells
The cell fragment shader sampled the glyph, underline, strike, cursor, and underline-exclusion texels for every cell. A space is sprite 0, and that sprite is empty, so those samples add nothing. The vertex shader now sets skip_text when the glyph, both decoration indices, and the cursor sprite are all zero. The fragment writes the background it already computed and skips the samples. A block cursor is sprite 0, so its color stays in the background. An underline, strike, URL underline, or beam, underline, or hollow cursor still samples. The foreground-only pass writes zero for a skipped cell. On Linux, platform text composition is gamma 1 and contrast 0, which is pow(alpha, 1) * 1. TEXT_CONTRAST_IDENTITY compiles that function down to returning the color unchanged. legacy and any other numbers keep the old math. A config reload that crosses that line rebuilds the cell programs. The identity flag is only varied when the new gamma path is on, so the cell programs go from 18 to 27.
This commit is contained in:
parent
f5e18f430a
commit
c57495436d
3 changed files with 73 additions and 4 deletions
|
|
@ -13,6 +13,7 @@ import background;
|
|||
|
||||
extern static const uint FG_OVERRIDE_ALGO;
|
||||
extern static const bool TEXT_NEW_GAMMA;
|
||||
extern static const bool TEXT_CONTRAST_IDENTITY;
|
||||
extern static const uint RENDER_MODE;
|
||||
|
||||
static const bool ONLY_FOREGROUND = RENDER_MODE == 2;
|
||||
|
|
@ -39,6 +40,9 @@ struct VertexOutput {
|
|||
float3 cursor_pos;
|
||||
float3 strike_pos;
|
||||
nointerpolation uint underline_exclusion_pos;
|
||||
// 1 when the glyph, decorations, and cursor sprite are all 0. The block
|
||||
// cursor is sprite 0 and its color is already in the background.
|
||||
nointerpolation uint skip_text;
|
||||
float3 cell_foreground;
|
||||
float4 cursor_color_premult;
|
||||
float3 decoration_fg;
|
||||
|
|
@ -153,6 +157,7 @@ vertex_main(
|
|||
uint fg_as_uint = bo.fg_as_uint;
|
||||
uint text_attrs = bo.text_attrs;
|
||||
CellData cell_data = bo.cell_data;
|
||||
uint skip_text = 1u;
|
||||
|
||||
// Foreground {{{
|
||||
if (!ONLY_BACKGROUND) { // background does not depend on foreground
|
||||
|
|
@ -184,9 +189,14 @@ vertex_main(
|
|||
float3 final_cursor_text_color = lerp(foreground, cell_data.cursor.fg, crd.cursor_opacity);
|
||||
foreground = if_one_then(cell_data.has_block_cursor, final_cursor_text_color, foreground);
|
||||
vo.decoration_fg = if_one_then(cell_data.has_block_cursor, final_cursor_text_color, vo.decoration_fg);
|
||||
vo.cursor_pos = to_sprite_pos(cell_data.pos, cell_data.cursor_fg_sprite_idx * uint(cell_data.has_cursor));
|
||||
uint cursor_sprite = cell_data.cursor_fg_sprite_idx * uint(cell_data.has_cursor);
|
||||
vo.cursor_pos = to_sprite_pos(cell_data.pos, cursor_sprite);
|
||||
uint glyph = sprite_idx[0] & SPRITE_INDEX_MASK;
|
||||
if (glyph == 0u && decs[0] == 0u && decs[1] == 0u && cursor_sprite == 0u) skip_text = 1u;
|
||||
else skip_text = 0u;
|
||||
}
|
||||
// }}}
|
||||
vo.skip_text = skip_text;
|
||||
|
||||
vo.background = background_rgb;
|
||||
|
||||
|
|
@ -222,6 +232,7 @@ clamp_to_unit_float(float x) {
|
|||
|
||||
float4
|
||||
foreground_contrast_new(float4 over, float3 under, float text_contrast, float text_gamma_adjustment) {
|
||||
if (TEXT_CONTRAST_IDENTITY) return over;
|
||||
float under_luminance = dot(under, Y);
|
||||
float over_lumininace = dot(over.rgb, Y);
|
||||
// Apply additional gamma-adjustment scaled by the luminance difference, the darker the foreground the more adjustment we apply.
|
||||
|
|
@ -303,7 +314,7 @@ fragment_main(VertexOutput vo, uniform float text_contrast, uniform float text_g
|
|||
float4 ans_premul = 0;
|
||||
if (!ONLY_FOREGROUND) ans_premul = vo.effective_background_premul;
|
||||
|
||||
if (!ONLY_BACKGROUND) {
|
||||
if (!ONLY_BACKGROUND && vo.skip_text == 0u) {
|
||||
// blend in the foreground color
|
||||
float4 text_fg = load_text_foreground_color(vo.sprite_pos, vo.colored_sprite, vo.cell_foreground);
|
||||
text_fg = adjust_foreground_contrast_with_background(text_fg, vo.background, text_contrast, text_gamma_adjustment);
|
||||
|
|
|
|||
|
|
@ -134,6 +134,26 @@ class Specialization(NamedTuple):
|
|||
return f'.{self.name}' if self.name else '.default-specialization'
|
||||
|
||||
|
||||
def text_contrast_is_identity(strategy: str) -> bool:
|
||||
# Same numbers as text_composition_strategy() in kitty/options/to-c.h.
|
||||
# set_cell_uniforms() turns gamma 1 and contrast 0 into pow(alpha, 1) * 1.
|
||||
if strategy == 'legacy':
|
||||
return False
|
||||
if strategy == 'platform':
|
||||
return sys.platform != 'darwin'
|
||||
parts = strategy.split()
|
||||
if not parts or len(parts) > 2:
|
||||
return False
|
||||
try:
|
||||
gamma = float(parts[0])
|
||||
contrast = float(parts[1]) if len(parts) == 2 else 0.0
|
||||
except ValueError:
|
||||
return False
|
||||
gamma = max(gamma, 0.01)
|
||||
contrast = min(max(contrast, 0.0), 100.0)
|
||||
return gamma == 1.0 and contrast == 0.0
|
||||
|
||||
|
||||
def cell_variant(opts: Options = defaults, program: int = CELL_PROGRAM) -> dict[str, str]:
|
||||
text_fg_override_threshold: float = opts.text_fg_override_threshold[0]
|
||||
algo = '0'
|
||||
|
|
@ -150,6 +170,7 @@ def cell_variant(opts: Options = defaults, program: int = CELL_PROGRAM) -> dict[
|
|||
return {
|
||||
'FG_OVERRIDE_ALGO': algo,
|
||||
'TEXT_NEW_GAMMA': 'false' if opts.text_composition_strategy == 'legacy' else 'true',
|
||||
'TEXT_CONTRAST_IDENTITY': 'true' if text_contrast_is_identity(opts.text_composition_strategy) else 'false',
|
||||
'RENDER_MODE': str(render_mode),
|
||||
}
|
||||
|
||||
|
|
@ -158,8 +179,18 @@ def cell_variant(opts: Options = defaults, program: int = CELL_PROGRAM) -> dict[
|
|||
def cell_variations() -> tuple[MappingProxyType[str, str], ...]:
|
||||
variations = {'FG_OVERRIDE_ALGO': ('0', '1', '2'), 'RENDER_MODE': ('0', '1', '2')}
|
||||
bool_variations = 'false', 'true'
|
||||
variants_dict = {k: variations.get(k, bool_variations) for k in cell_variant()}
|
||||
return tuple(MappingProxyType(dict(zip(variants_dict.keys(), comb))) for comb in product(*variants_dict.values()))
|
||||
# TEXT_CONTRAST_IDENTITY is only read when TEXT_NEW_GAMMA is true.
|
||||
base_keys = [k for k in cell_variant() if k != 'TEXT_CONTRAST_IDENTITY']
|
||||
variants_dict = {k: variations.get(k, bool_variations) for k in base_keys}
|
||||
ans: list[MappingProxyType[str, str]] = []
|
||||
for comb in product(*variants_dict.values()):
|
||||
variant = dict(zip(variants_dict.keys(), comb))
|
||||
if variant['TEXT_NEW_GAMMA'] == 'true':
|
||||
for identity in bool_variations:
|
||||
ans.append(MappingProxyType({**variant, 'TEXT_CONTRAST_IDENTITY': identity}))
|
||||
else:
|
||||
ans.append(MappingProxyType({**variant, 'TEXT_CONTRAST_IDENTITY': 'false'}))
|
||||
return tuple(ans)
|
||||
|
||||
|
||||
def variant_name(variant: dict[str, str], default: dict[str, str]) -> str:
|
||||
|
|
@ -183,6 +214,7 @@ def glsl_shaders(name: str, variant_name: str = '') -> tuple[str, str]:
|
|||
class LoadShaderPrograms:
|
||||
text_fg_override_threshold: tuple[float, Literal['%', 'ratio']] = 0, '%'
|
||||
text_old_gamma: bool = False
|
||||
text_contrast_identity: bool = text_contrast_is_identity('platform')
|
||||
custom_shaders: tuple[str, ...] = ()
|
||||
force_recompile_of_custom_shaders: bool = False
|
||||
|
||||
|
|
@ -210,6 +242,7 @@ class LoadShaderPrograms:
|
|||
bool(opts.text_fg_override_threshold[0]) != bool(self.text_fg_override_threshold[0])
|
||||
or opts.text_fg_override_threshold[1] != self.text_fg_override_threshold[1]
|
||||
or (opts.text_composition_strategy == 'legacy') != self.text_old_gamma
|
||||
or text_contrast_is_identity(opts.text_composition_strategy) != self.text_contrast_identity
|
||||
)
|
||||
|
||||
def recompile_if_needed(self) -> None:
|
||||
|
|
@ -224,6 +257,7 @@ class LoadShaderPrograms:
|
|||
default_cell_variant = cell_variant()
|
||||
opts = self.get_options()
|
||||
self.text_old_gamma = opts.text_composition_strategy == 'legacy'
|
||||
self.text_contrast_identity = text_contrast_is_identity(opts.text_composition_strategy)
|
||||
self.text_fg_override_threshold = opts.text_fg_override_threshold
|
||||
metadata = load_glsl_metadata()
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import json
|
|||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from unittest.mock import patch
|
||||
|
||||
|
|
@ -20,6 +21,8 @@ from kitty.shaders.slang import (
|
|||
apply_pipeline_specializations,
|
||||
build_custom_shader_pipeline_glsl,
|
||||
build_import_graph,
|
||||
cell_variant,
|
||||
cell_variations,
|
||||
clear_caches,
|
||||
custom_shader,
|
||||
defines_pointer_map,
|
||||
|
|
@ -31,8 +34,10 @@ from kitty.shaders.slang import (
|
|||
pipeline_definition,
|
||||
pointer_map_shaders,
|
||||
slangc_version,
|
||||
text_contrast_is_identity,
|
||||
topological_layers,
|
||||
topological_sort,
|
||||
variant_name,
|
||||
)
|
||||
|
||||
from .base import BaseTest
|
||||
|
|
@ -458,6 +463,25 @@ float4 frag() { string s = "float2 in_string(float2 p) {}"; return float4(0); }
|
|||
# # not at the start of a line is not a directive
|
||||
self.ae([f.name for f in parse_slang_declarations('float a = 1; # float2 f(float2 p) { return p; }').functions], ['f'])
|
||||
|
||||
def test_cell_contrast_variants(self):
|
||||
self.assertFalse(text_contrast_is_identity('legacy'))
|
||||
self.assertTrue(text_contrast_is_identity('1'))
|
||||
self.assertTrue(text_contrast_is_identity('1.0 0'))
|
||||
self.assertFalse(text_contrast_is_identity('1.7 30'))
|
||||
self.assertFalse(text_contrast_is_identity('2 0'))
|
||||
self.ae(text_contrast_is_identity('platform'), sys.platform != 'darwin')
|
||||
variants = cell_variations()
|
||||
self.ae(len(variants), 27)
|
||||
self.ae(sum(v['TEXT_NEW_GAMMA'] == 'false' for v in variants), 9)
|
||||
self.ae(sum(v['TEXT_CONTRAST_IDENTITY'] == 'true' for v in variants), 9)
|
||||
for v in variants:
|
||||
if v['TEXT_NEW_GAMMA'] == 'false':
|
||||
self.ae(v['TEXT_CONTRAST_IDENTITY'], 'false')
|
||||
default = cell_variant()
|
||||
names = [variant_name(dict(v), default) for v in variants]
|
||||
self.ae(len(names), len(set(names)))
|
||||
self.assertIn('', names)
|
||||
|
||||
def test_apply_pipeline_specializations(self):
|
||||
src = b"""static const float4 TINT = float4(0, 0.8,
|
||||
0.6, 1); // keep me
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue