Skip to content

Commit bf6c848

Browse files
committed
Use the new shader include system to avoid having to upload 256 uniform floats to two different programs on the GPU
1 parent 2b6b0ea commit bf6c848

File tree

7 files changed

+41
-22
lines changed

7 files changed

+41
-22
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ kitty/key_encoding.py linguist-generated=true
55
kitty/unicode-data.c linguist-generated=true
66
kitty/rowcolumn-diacritics.c linguist-generated=true
77
kitty/rgb.py linguist-generated=true
8-
kitty/srgb_gamma.c linguist-generated=true
8+
kitty/srgb_gamma.* linguist-generated=true
99
kitty/gl-wrapper.* linguist-generated=true
1010
kitty/glfw-wrapper.* linguist-generated=true
1111
kitty/parse-graphics-command.h linguist-generated=true

gen-srgb-lut.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# vim:fileencoding=utf-8
33

44
import os
5+
from functools import lru_cache
56
from typing import List
67

78

@@ -12,7 +13,8 @@ def to_linear(a: float) -> float:
1213
return float(pow((a + 0.055) / 1.055, 2.4))
1314

1415

15-
def generate_srgb_lut(line_prefix: str = '') -> List[str]:
16+
@lru_cache
17+
def generate_srgb_lut(line_prefix: str = ' ') -> List[str]:
1618
values: List[str] = []
1719
lines: List[str] = []
1820

@@ -22,26 +24,30 @@ def generate_srgb_lut(line_prefix: str = '') -> List[str]:
2224
for i in range(16):
2325
lines.append(line_prefix + ', '.join(values[i * 16:(i + 1) * 16]) + ',')
2426

27+
lines[-1] = lines[-1].rstrip(',')
2528
return lines
2629

2730

28-
def generate_srgb_gamma_c() -> str:
31+
def generate_srgb_gamma(declaration: str = 'static const GLfloat srgb_lut[256] = {', close: str = '};') -> str:
2932
lines: List[str] = []
33+
a = lines.append
3034

31-
lines.append('// Generated by gen-srgb-lut.py DO NOT edit')
32-
lines.append('#include "srgb_gamma.h"')
33-
lines.append('')
34-
lines.append('const GLfloat srgb_lut[256] = {')
35-
lines += generate_srgb_lut(' ')
36-
lines.append('};')
35+
a('// Generated by gen-srgb-lut.py DO NOT edit')
36+
a('')
37+
a(declaration)
38+
lines += generate_srgb_lut()
39+
a(close)
3740

3841
return "\n".join(lines)
3942

4043

4144
def main() -> None:
42-
c = generate_srgb_gamma_c()
43-
with open(os.path.join('kitty', 'srgb_gamma.c'), 'w') as f:
45+
c = generate_srgb_gamma()
46+
with open(os.path.join('kitty', 'srgb_gamma.h'), 'w') as f:
4447
f.write(f'{c}\n')
48+
g = generate_srgb_gamma('const float gamma_lut[256] = float[256](', ');')
49+
with open(os.path.join('kitty', 'srgb_gamma.glsl'), 'w') as f:
50+
f.write(f'{g}\n')
4551

4652

4753
if __name__ == '__main__':

kitty/border_vertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#pragma kitty_include_shader <srgb_gamma.glsl>
12
uniform uvec2 viewport;
23
uniform uint colors[9];
34
uniform float background_opacity;
45
uniform float tint_opacity, tint_premult;
5-
uniform float gamma_lut[256];
66
in vec4 rect; // left, top, right, bottom
77
in uint rect_color;
88
out vec4 color;

kitty/cell_vertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#extension GL_ARB_explicit_attrib_location : require
2+
#pragma kitty_include_shader <srgb_gamma.glsl>
23

34
#define {WHICH_PROGRAM}
45
#define NOT_TRANSPARENT
@@ -30,7 +31,6 @@ uniform uint draw_bg_bitfield;
3031
layout(location=0) in uvec3 colors;
3132
layout(location=1) in uvec4 sprite_coords;
3233
layout(location=2) in uint is_selected;
33-
uniform float gamma_lut[256];
3434

3535

3636
const int fg_index_map[] = int[3](0, 1, 0);

kitty/shaders.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,6 @@ set_cell_uniforms(float current_inactive_text_alpha, bool force) {
593593
}
594594
for (int i = CELL_PROGRAM; i <= CELL_FG_PROGRAM; i++) {
595595
bind_program(i); const CellUniforms *cu = &cell_program_layouts[i].uniforms;
596-
glUniform1fv(cu->gamma_lut, arraysz(srgb_lut), srgb_lut);
597596
switch(i) {
598597
case CELL_PROGRAM: case CELL_FG_PROGRAM:
599598
glUniform1i(cu->sprites, SPRITE_MAP_UNIT);
@@ -1040,7 +1039,6 @@ draw_borders(ssize_t vao_idx, unsigned int num_border_rects, BorderRect *rect_bu
10401039
glUniform1f(border_program_layout.uniforms.tint_opacity, tint_opacity);
10411040
glUniform1f(border_program_layout.uniforms.tint_premult, tint_premult);
10421041
glUniform2ui(border_program_layout.uniforms.viewport, viewport_width, viewport_height);
1043-
glUniform1fv(border_program_layout.uniforms.gamma_lut, 256, srgb_lut);
10441042
if (has_bgimage(w)) {
10451043
if (w->is_semi_transparent) { BLEND_PREMULT; }
10461044
else { BLEND_ONTO_OPAQUE_WITH_OPAQUE_OUTPUT; }

kitty/srgb_gamma.c renamed to kitty/srgb_gamma.glsl

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kitty/srgb_gamma.h

Lines changed: 19 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)