Remove duplicate definitions of linear2srgb functions in shaders

This commit is contained in:
Kovid Goyal 2023-06-13 14:54:20 +05:30
parent 444ec2484d
commit 288bb034b5
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
17 changed files with 41 additions and 61 deletions

View file

@ -1,4 +1,3 @@
#version GLSL_VERSION
uniform sampler2D image;
uniform float opacity;

View file

@ -1,4 +1,3 @@
#version GLSL_VERSION
#define left 0
#define top 1
#define right 2

View file

@ -1,18 +1,10 @@
#version GLSL_VERSION
#pragma kitty_include_shader <linear2srgb.glsl>
uniform sampler2D image;
in vec2 texcoord;
out vec4 color;
float linear2srgb(float x) {
// Linear to sRGB conversion.
float lower = 12.92 * x;
float upper = 1.055 * pow(x, 1.0f / 2.4f) - 0.055f;
return mix(lower, upper, step(0.0031308f, x));
}
void main() {
color = texture(image, texcoord);

View file

@ -1,4 +1,3 @@
#version GLSL_VERSION
#define left -1.0f
#define top 1.0f
#define right 1.0f

View file

@ -1,4 +1,3 @@
#version GLSL_VERSION
in vec4 color;
out vec4 final_color;

View file

@ -1,4 +1,5 @@
#version GLSL_VERSION
#pragma kitty_include_shader <linear2srgb.glsl>
uniform uvec2 viewport;
uniform uint colors[9];
uniform float background_opacity, do_srgb_correction;
@ -22,14 +23,6 @@ const uvec2 pos_map[] = uvec2[4](
uvec2(LEFT, TOP)
);
float linear2srgb(float x) {
// Linear to sRGB conversion. Needed to match alpha from the cell shader
float lower = 12.92 * x;
float upper = 1.055 * pow(x, 1.0f / 2.4f) - 0.055f;
return mix(lower, upper, step(0.0031308f, x));
}
float to_color(uint c) {
return gamma_lut[c & FF];
}

View file

@ -1,4 +1,5 @@
#version GLSL_VERSION
#pragma kitty_include_shader <linear2srgb.glsl>
#define {WHICH_PROGRAM}
#define NOT_TRANSPARENT
#define NO_FG_OVERRIDE
@ -39,22 +40,6 @@ in float colored_sprite;
out vec4 final_color;
// Util functions {{{
float linear2srgb(float x) {
// Linear to sRGB conversion.
float lower = 12.92 * x;
float upper = 1.055 * pow(x, 1.0f / 2.4f) - 0.055f;
return mix(lower, upper, step(0.0031308f, x));
}
float srgb2linear(float x) {
// sRGB to linear conversion
float lower = x / 12.92;
float upper = pow((x + 0.055f) / 1.055f, 2.4f);
return mix(lower, upper, step(0.04045f, x));
}
vec4 alpha_blend(vec4 over, vec4 under) {
// Alpha blend two colors returning the resulting color pre-multiplied by its alpha
// and its alpha.

View file

@ -1,4 +1,3 @@
#version GLSL_VERSION
#extension GL_ARB_explicit_attrib_location : require
#define {WHICH_PROGRAM}

View file

@ -94,9 +94,9 @@ free_framebuffer(GLuint *fb_id) {
static Program programs[64] = {{0}};
GLuint
compile_shader(GLenum shader_type, const char *source) {
compile_shaders(GLenum shader_type, GLsizei count, const GLchar * const * source) {
GLuint shader_id = glCreateShader(shader_type);
glShaderSource(shader_id, 1, (const GLchar **)&source, NULL);
glShaderSource(shader_id, count, source, NULL);
glCompileShader(shader_id);
GLint ret = GL_FALSE;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &ret);

View file

@ -55,4 +55,4 @@ void bind_vertex_array(ssize_t vao_idx);
void bind_vao_uniform_buffer(ssize_t vao_idx, size_t bufnum, GLuint block_index);
void unbind_vertex_array(void);
void unbind_program(void);
GLuint compile_shader(GLenum shader_type, const char *source);
GLuint compile_shaders(GLenum shader_type, GLsizei count, const GLchar * const * string);

View file

@ -1,4 +1,3 @@
#version GLSL_VERSION
#define ALPHA_TYPE
uniform sampler2D image;

View file

@ -1,4 +1,3 @@
#version GLSL_VERSION
#extension GL_ARB_explicit_attrib_location : require
// Have to use fixed locations here as all variants of the program share the same VAO

15
kitty/linear2srgb.glsl Normal file
View file

@ -0,0 +1,15 @@
float srgb2linear(float x) {
// sRGB to linear conversion
float lower = x / 12.92;
float upper = pow((x + 0.055f) / 1.055f, 2.4f);
return mix(lower, upper, step(0.04045f, x));
}
float linear2srgb(float x) {
// Linear to sRGB conversion.
float lower = 12.92 * x;
float upper = 1.055 * pow(x, 1.0f / 2.4f) - 0.055f;
return mix(lower, upper, step(0.0031308f, x));
}

View file

@ -1073,13 +1073,14 @@ draw_borders(ssize_t vao_idx, unsigned int num_border_rects, BorderRect *rect_bu
static bool
attach_shaders(PyObject *sources, GLuint program_id, GLenum shader_type) {
FREE_AFTER_FUNCTION const GLchar * * c_sources = calloc(sizeof(char*), PyTuple_GET_SIZE(sources));
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(sources); i++) {
PyObject *temp = PyTuple_GET_ITEM(sources, i);
if (!PyUnicode_Check(temp)) { PyErr_SetString(PyExc_TypeError, "shaders must be strings"); return false; }
const char *vertex_shader = PyUnicode_AsUTF8(temp);
GLuint shader_id = compile_shader(shader_type, vertex_shader);
glAttachShader(program_id, shader_id);
c_sources[i] = PyUnicode_AsUTF8(temp);
}
GLuint shader_id = compile_shaders(shader_type, PyTuple_GET_SIZE(sources), c_sources);
glAttachShader(program_id, shader_id);
return true;
}

View file

@ -1,5 +1,3 @@
#version GLSL_VERSION
uniform vec4 tint_color;
out vec4 color;

View file

@ -1,4 +1,3 @@
#version GLSL_VERSION
uniform vec4 edges;

View file

@ -106,23 +106,27 @@ def platform_window_id(os_window_id: int) -> Optional[int]:
def load_shaders(name: str, vertex_name: str = '', fragment_name: str = '') -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
from .fast_data_types import GLSL_VERSION
pat = re.compile(r'^#pragma kitty_include_shader <(.+?)>', re.MULTILINE)
pat = re.compile(r'^#pragma\s+kitty_include_shader\s+<(.+?)>', re.MULTILINE)
def load_source(name: str) -> str:
return read_kitty_resource(name).decode('utf-8').replace('GLSL_VERSION', str(GLSL_VERSION), 1)
def load_sources(name: str) -> Tuple[str, ...]:
src = load_source(name)
ans: Tuple[str, ...] = src,
def load_sources(name: str, level: int = 0) -> Iterator[str]:
if level == 0:
yield f'#version {GLSL_VERSION}\n'
src = read_kitty_resource(name).decode('utf-8')
pos = 0
for m in pat.finditer(src):
prefix = src[pos:m.start()]
if prefix:
yield prefix
iname = m.group(1)
ans += load_sources(iname)
return ans
yield from load_sources(iname, level+1)
pos = m.start()
if pos < len(src):
yield src[pos:]
def load(which: str, lname: str = '') -> Tuple[str, ...]:
lname = lname or name
main = f'{lname}_{which}.glsl'
return load_sources(main)
return tuple(load_sources(main))
return load('vertex', vertex_name), load('fragment', fragment_name)