kitty/gen-srgb-lut.py
Kovid Goyal 902b9e9693
Go back to using uniforms for the gamma LUT
Apparently older macOS M1 GPU drivers' performance falls off a cliff
when using const float arrays instead. Fixes #6479
2023-07-18 19:28:15 +05:30

52 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
from functools import lru_cache
from typing import List
def to_linear(a: float) -> float:
if a <= 0.04045:
return a / 12.92
else:
return float(pow((a + 0.055) / 1.055, 2.4))
@lru_cache
def generate_srgb_lut(line_prefix: str = ' ') -> List[str]:
values: List[str] = []
lines: List[str] = []
for i in range(256):
values.append('{:1.5f}f'.format(to_linear(i / 255.0)))
for i in range(16):
lines.append(line_prefix + ', '.join(values[i * 16:(i + 1) * 16]) + ',')
lines[-1] = lines[-1].rstrip(',')
return lines
def generate_srgb_gamma(declaration: str = 'static const GLfloat srgb_lut[256] = {', close: str = '};') -> str:
lines: List[str] = []
a = lines.append
a('// Generated by gen-srgb-lut.py DO NOT edit')
a('')
a(declaration)
lines += generate_srgb_lut()
a(close)
return "\n".join(lines)
def main() -> None:
c = generate_srgb_gamma()
with open(os.path.join('kitty', 'srgb_gamma.h'), 'w') as f:
f.write(f'{c}\n')
if __name__ == '__main__':
main()