mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 14:45:29 +00:00
Move the kitty OpenGL code into a dynamically loaded GPU driver
All OpenGL calls now live in gpu/opengl, which is built as kitty/opengl.so, next to the glfw backends. glfw_init() is passed the path to the driver and loads it. The driver is initialized once the first OpenGL context is current. fast_data_types no longer links against libGL. kitty/gpu.c contains the driver loader and the higher level helpers (programs and their metadata, vertex array objects, viewport stack) that were previously in kitty/gl.c, implemented on top of the driver API. shaders.c and the rest of kitty use only the driver API. Also remove the unused GL_* constants and the unused bind_program, create_vao, etc. functions exported to Python.
This commit is contained in:
parent
0051fc799d
commit
ae69df7c69
27 changed files with 2526 additions and 1565 deletions
|
|
@ -1,6 +1,6 @@
|
|||
*_generated.*
|
||||
*-generated.*
|
||||
kitty/gl-wrapper.*
|
||||
gpu/opengl/gl-wrapper.*
|
||||
kitty/srgb_gamma.*
|
||||
kitty/glfw-wrapper.*
|
||||
kitty/rowcolumn-diacritics.c
|
||||
|
|
|
|||
2
.gitattributes
vendored
2
.gitattributes
vendored
|
|
@ -9,7 +9,7 @@ kitty/key_encoding.py linguist-generated=true
|
|||
kitty/rowcolumn-diacritics.c linguist-generated=true
|
||||
kitty/rgb.py linguist-generated=true
|
||||
kitty/srgb_gamma.* linguist-generated=true
|
||||
kitty/gl-wrapper.* linguist-generated=true
|
||||
gpu/opengl/gl-wrapper.* linguist-generated=true
|
||||
kitty/glfw-wrapper.* linguist-generated=true
|
||||
kitty/color-names.h linguist-generated=true
|
||||
kitty/parse-graphics-command.h linguist-generated=true
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ def generate_srgb_lut(line_prefix: str = ' ') -> list[str]:
|
|||
return lines
|
||||
|
||||
|
||||
def generate_srgb_gamma(declaration: str = 'static const GLfloat srgb_lut[256] = {', close: str = '};') -> str:
|
||||
def generate_srgb_gamma(declaration: str = 'static const float srgb_lut[256] = {', close: str = '};') -> str:
|
||||
lines: list[str] = []
|
||||
a = lines.append
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ def export() -> None:
|
|||
data = source.read()
|
||||
data = strip_trailing_whitespace(data)
|
||||
|
||||
with open('../kitty/gl-wrapper.h', 'w', encoding='utf-8') as dest:
|
||||
with open('../gpu/opengl/gl-wrapper.h', 'w', encoding='utf-8') as dest:
|
||||
dest.write(data)
|
||||
|
||||
|
||||
|
|
|
|||
1056
gpu/opengl/driver.c
Normal file
1056
gpu/opengl/driver.c
Normal file
File diff suppressed because it is too large
Load diff
0
kitty/gl-wrapper.c → gpu/opengl/gl-wrapper.c
generated
0
kitty/gl-wrapper.c → gpu/opengl/gl-wrapper.c
generated
0
kitty/gl-wrapper.h → gpu/opengl/gl-wrapper.h
generated
0
kitty/gl-wrapper.h → gpu/opengl/gl-wrapper.h
generated
17
gpu/opengl/version.h
Normal file
17
gpu/opengl/version.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
* version.h
|
||||
* Copyright (C) 2026 Kovid Goyal <kovid at kovidgoyal.net>
|
||||
*
|
||||
* Distributed under terms of the GPL3 license.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
// Required minimum OpenGL version
|
||||
#define OPENGL_REQUIRED_VERSION_MAJOR 3
|
||||
#ifdef __APPLE__
|
||||
#define OPENGL_REQUIRED_VERSION_MINOR 3
|
||||
#else
|
||||
#define OPENGL_REQUIRED_VERSION_MINOR 1
|
||||
#endif
|
||||
#define GLSL_VERSION 140
|
||||
|
|
@ -8,6 +8,7 @@
|
|||
#include "loop-utils.h"
|
||||
#include "safe-wrappers.h"
|
||||
#include "state.h"
|
||||
#include "gpu.h"
|
||||
#include "threading.h"
|
||||
#include "screen.h"
|
||||
#include "monotonic.h"
|
||||
|
|
|
|||
|
|
@ -232,6 +232,11 @@ def glfw_path(module: str) -> str:
|
|||
return os.path.join(extensions_dir, f'{prefix}glfw-{module}.so')
|
||||
|
||||
|
||||
def gpu_driver_path(driver: str = 'opengl') -> str:
|
||||
prefix = 'kitty.' if getattr(sys, 'frozen', False) else ''
|
||||
return os.path.join(extensions_dir, f'{prefix}{driver}.so')
|
||||
|
||||
|
||||
def detect_if_wayland_ok() -> bool:
|
||||
if not os.environ.get('WAYLAND_DISPLAY') and not os.environ.get('WAYLAND_SOCKET'):
|
||||
return False
|
||||
|
|
|
|||
|
|
@ -21,15 +21,11 @@
|
|||
#include <locale.h>
|
||||
#include "glfw-wrapper.h"
|
||||
#include "banned.h"
|
||||
// Required minimum OpenGL version
|
||||
#define OPENGL_REQUIRED_VERSION_MAJOR 3
|
||||
#ifdef __APPLE__
|
||||
#include <xlocale.h>
|
||||
#define OPENGL_REQUIRED_VERSION_MINOR 3
|
||||
#else
|
||||
#define OPENGL_REQUIRED_VERSION_MINOR 1
|
||||
#endif
|
||||
#define GLSL_VERSION 140
|
||||
#include "../gpu/opengl/version.h"
|
||||
#include "../gpu/api.h"
|
||||
#define GLFW_MOD_KITTY (GLFW_MOD_LAST * 2)
|
||||
#define UNUSED __attribute__((unused))
|
||||
#define PYNOARG PyObject *__a1 UNUSED, PyObject *__a2 UNUSED
|
||||
|
|
|
|||
|
|
@ -560,7 +560,7 @@ def add_window(os_window_id: int, tab_id: int, title: str) -> int:
|
|||
|
||||
def compile_program(
|
||||
which: int, vertex_shaders: Tuple[str, ...], fragment_shaders: Tuple[str, ...], metadata: Dict[str, Any], allow_recompile: bool = False
|
||||
) -> int:
|
||||
) -> None:
|
||||
pass
|
||||
|
||||
def custom_shader_needs_render(before: Tuple[bool, int, int], after: Tuple[bool, int, int], event_mask: int, now: int) -> bool: ...
|
||||
|
|
@ -669,7 +669,12 @@ def glfw_terminate() -> None:
|
|||
pass
|
||||
|
||||
def glfw_init(
|
||||
path: str, edge_spacing_func: Callable[[EdgeLiteral], float], debug_keyboard: bool = False, debug_rendering: bool = False, wayland_enable_ime: bool = True
|
||||
path: str,
|
||||
gpu_driver_path: str,
|
||||
edge_spacing_func: Callable[[EdgeLiteral], float],
|
||||
debug_keyboard: bool = False,
|
||||
debug_rendering: bool = False,
|
||||
wayland_enable_ime: bool = True,
|
||||
) -> tuple[bool, bool]:
|
||||
pass
|
||||
|
||||
|
|
|
|||
740
kitty/gl.c
740
kitty/gl.c
|
|
@ -1,740 +0,0 @@
|
|||
/*
|
||||
* gl.c
|
||||
* Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net>
|
||||
*
|
||||
* Distributed under terms of the GPL3 license.
|
||||
*/
|
||||
|
||||
#include "gl.h"
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include "glfw-wrapper.h"
|
||||
#include "state.h"
|
||||
#include "png-reader.h"
|
||||
|
||||
// GL setup and error handling {{{
|
||||
static void
|
||||
check_for_gl_error(void UNUSED *ret, const char *name, GLADapiproc UNUSED funcptr, int UNUSED len_args, ...) {
|
||||
#define f(msg) \
|
||||
fatal("OpenGL error: %s (calling function: %s)", msg, name); \
|
||||
break;
|
||||
GLenum code = glad_glGetError();
|
||||
switch (code) {
|
||||
case GL_NO_ERROR: break;
|
||||
case GL_INVALID_ENUM: f("An enum value is invalid (GL_INVALID_ENUM)");
|
||||
case GL_INVALID_VALUE: f("An numeric value is invalid (GL_INVALID_VALUE)");
|
||||
case GL_INVALID_OPERATION: f("This operation is invalid (GL_INVALID_OPERATION)");
|
||||
case GL_INVALID_FRAMEBUFFER_OPERATION: f("The framebuffer object is not complete (GL_INVALID_FRAMEBUFFER_OPERATION)");
|
||||
case GL_OUT_OF_MEMORY: f("There is not enough memory left to execute the command. (GL_OUT_OF_MEMORY)");
|
||||
case GL_STACK_UNDERFLOW: f("An attempt has been made to perform an operation that would cause an internal stack to underflow. (GL_STACK_UNDERFLOW)");
|
||||
case GL_STACK_OVERFLOW: f("An attempt has been made to perform an operation that would cause an internal stack to overflow. (GL_STACK_OVERFLOW)");
|
||||
default: fatal("An unknown OpenGL error occurred with code: %d (calling function: %s)", code, name); break;
|
||||
}
|
||||
}
|
||||
|
||||
const char *
|
||||
gl_version_string(void) {
|
||||
static char buf[256];
|
||||
int gl_major = GLAD_VERSION_MAJOR(global_state.gl_version);
|
||||
int gl_minor = GLAD_VERSION_MINOR(global_state.gl_version);
|
||||
const char *gvs = (const char *)glGetString(GL_VERSION);
|
||||
snprintf(buf, sizeof(buf), "'%s' Detected version: %d.%d", gvs, gl_major, gl_minor);
|
||||
return buf;
|
||||
}
|
||||
|
||||
void
|
||||
gl_init(void) {
|
||||
static bool glad_loaded = false;
|
||||
if (!glad_loaded) {
|
||||
global_state.gl_version = gladLoadGL(glfwGetProcAddress);
|
||||
if (!global_state.gl_version) { fatal("Loading the OpenGL library failed"); }
|
||||
if (!global_state.debug_rendering) { gladUninstallGLDebug(); }
|
||||
gladSetGLPostCallback(check_for_gl_error);
|
||||
#define ARB_TEST(name) \
|
||||
if (!GLAD_GL_ARB_##name) { fatal("The OpenGL driver on this system is missing the required extension: ARB_%s", #name); }
|
||||
ARB_TEST(texture_storage);
|
||||
#undef ARB_TEST
|
||||
#ifdef __APPLE__
|
||||
// See nsgl_context.m srgb is always supported on macOS but its OpenGL
|
||||
// drivers dont report the extensions, so hardcode to true.
|
||||
global_state.supports_framebuffer_srgb = true;
|
||||
#else
|
||||
global_state.supports_framebuffer_srgb = (GLAD_GL_ARB_framebuffer_sRGB + GLAD_GL_EXT_framebuffer_sRGB) != 0;
|
||||
#endif
|
||||
glad_loaded = true;
|
||||
int gl_major = GLAD_VERSION_MAJOR(global_state.gl_version);
|
||||
int gl_minor = GLAD_VERSION_MINOR(global_state.gl_version);
|
||||
if (global_state.debug_rendering) printf("[%.3f] GL version string: %s\n", monotonic_t_to_s_double(monotonic()), gl_version_string());
|
||||
if (gl_major < OPENGL_REQUIRED_VERSION_MAJOR || (gl_major == OPENGL_REQUIRED_VERSION_MAJOR && gl_minor < OPENGL_REQUIRED_VERSION_MINOR)) {
|
||||
fatal(
|
||||
"OpenGL version is %d.%d, version >= %d.%d required for kitty",
|
||||
gl_major,
|
||||
gl_minor,
|
||||
OPENGL_REQUIRED_VERSION_MAJOR,
|
||||
OPENGL_REQUIRED_VERSION_MINOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char *
|
||||
check_framebuffer_status(void) {
|
||||
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||
switch (status) {
|
||||
case GL_FRAMEBUFFER_COMPLETE: return NULL;
|
||||
case GL_FRAMEBUFFER_UNDEFINED: return ("GL_FRAMEBUFFER_UNDEFINED");
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: return ("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: return ("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT");
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: return ("GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER");
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: return ("GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER");
|
||||
case GL_FRAMEBUFFER_UNSUPPORTED: return ("GL_FRAMEBUFFER_UNSUPPORTED");
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: return ("GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE");
|
||||
default: return ("Unknown error");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
free_texture(GLuint *tex_id) {
|
||||
glDeleteTextures(1, tex_id);
|
||||
*tex_id = 0;
|
||||
}
|
||||
|
||||
void
|
||||
free_framebuffer(GLuint *fb_id) {
|
||||
glDeleteFramebuffers(1, fb_id);
|
||||
*fb_id = 0;
|
||||
}
|
||||
|
||||
static GLuint output_framebuffer = 0;
|
||||
|
||||
void
|
||||
bind_framebuffer_for_output(unsigned fbid) {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbid ? fbid : output_framebuffer);
|
||||
}
|
||||
|
||||
void
|
||||
set_framebuffer_to_use_for_output(unsigned fbid) {
|
||||
output_framebuffer = fbid;
|
||||
}
|
||||
|
||||
static void
|
||||
set_blending(bool allowed) {
|
||||
if (allowed) {
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||
} // blending of pre-multiplied colors
|
||||
else {
|
||||
glDisable(GL_BLEND);
|
||||
glBlendFunc(GL_ONE, GL_ZERO);
|
||||
} // no blending
|
||||
}
|
||||
|
||||
void
|
||||
draw_quad(bool blend, unsigned instance_count) {
|
||||
set_blending(blend);
|
||||
if (instance_count) glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, instance_count);
|
||||
else glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
||||
}
|
||||
|
||||
static struct {
|
||||
GLsizei items[16][4];
|
||||
size_t used;
|
||||
} saved_viewports;
|
||||
|
||||
void
|
||||
set_gpu_viewport(unsigned w, unsigned h) {
|
||||
glViewport(0, 0, w, h);
|
||||
}
|
||||
|
||||
Viewport
|
||||
get_gpu_viewport(void) {
|
||||
GLsizei v[4];
|
||||
glGetIntegerv(GL_VIEWPORT, v);
|
||||
return (Viewport){.left = v[0], .top = v[1], .width = v[2], .height = v[3]};
|
||||
}
|
||||
|
||||
void
|
||||
save_viewport_using_bottom_left_origin(GLsizei newx, GLsizei newy, GLsizei width, GLsizei height) {
|
||||
if (saved_viewports.used >= arraysz(saved_viewports.items)) fatal("Too many nested saved viewports");
|
||||
GLsizei *saved_viewport = saved_viewports.items[saved_viewports.used++];
|
||||
glGetIntegerv(GL_VIEWPORT, saved_viewport);
|
||||
glViewport(newx, newy, width, height);
|
||||
}
|
||||
|
||||
void
|
||||
save_viewport_using_top_left_origin(GLsizei newx, GLsizei newy, GLsizei width, GLsizei height, GLsizei full_framebuffer_height) {
|
||||
// Converts the viewport defined by the specified arguments which are
|
||||
// assumed to be in the usual co-ord system with origin at top left to the
|
||||
// OpenGL viewport co-ord system with origin at bottom left.
|
||||
// Use restore_viewport() to restore the viewport to what it was before.
|
||||
if (saved_viewports.used >= arraysz(saved_viewports.items)) fatal("Too many nested saved viewports");
|
||||
GLsizei *saved_viewport = saved_viewports.items[saved_viewports.used++];
|
||||
glGetIntegerv(GL_VIEWPORT, saved_viewport);
|
||||
newy = full_framebuffer_height - (newy + height);
|
||||
glViewport(newx, newy, width, height);
|
||||
}
|
||||
|
||||
void
|
||||
restore_viewport(void) {
|
||||
if (!saved_viewports.used) fatal("Trying to restore a viewport when none is saved");
|
||||
GLsizei *saved_viewport = saved_viewports.items[--saved_viewports.used];
|
||||
glViewport(saved_viewport[0], saved_viewport[1], saved_viewport[2], saved_viewport[3]);
|
||||
}
|
||||
|
||||
void
|
||||
enable_scissor_using_top_left_origin(Viewport vp, unsigned full_framebuffer_height) {
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
GLsizei newy = full_framebuffer_height - (vp.top + vp.height);
|
||||
glScissor(vp.left, newy, vp.width, vp.height);
|
||||
}
|
||||
|
||||
void
|
||||
disable_scissor(void) {
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
static float
|
||||
linear_to_srgb(float c) {
|
||||
return (c <= 0.0031308f) ? 12.92f * c : 1.055f * powf(c, 1.0f / 2.4f) - 0.055f;
|
||||
}
|
||||
|
||||
void
|
||||
save_texture_as_png(uint32_t texture_id, const char *filename) {
|
||||
GLint prev_tex = 0;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, &prev_tex);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id);
|
||||
int width = 0, height = 0;
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
|
||||
size_t sz = sizeof(uint32_t) * width * height;
|
||||
uint32_t *data = malloc(sz);
|
||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
||||
// assume data is linear and pre-multiplied
|
||||
for (int i = 0; i < width * height; i++) {
|
||||
uint32_t px = data[i];
|
||||
uint8_t r = (px >> 0) & 0xFF;
|
||||
uint8_t g = (px >> 8) & 0xFF;
|
||||
uint8_t b = (px >> 16) & 0xFF;
|
||||
uint8_t a = (px >> 24) & 0xFF;
|
||||
float alpha = a / 255.0f;
|
||||
float rf = 0, gf = 0, bf = 0;
|
||||
if (alpha > 0.0f) {
|
||||
rf = (r / 255.0f) / alpha;
|
||||
gf = (g / 255.0f) / alpha;
|
||||
bf = (b / 255.0f) / alpha;
|
||||
}
|
||||
rf = linear_to_srgb(rf);
|
||||
gf = linear_to_srgb(gf);
|
||||
bf = linear_to_srgb(bf);
|
||||
r = (uint8_t)(rf * 255);
|
||||
g = (uint8_t)(gf * 255);
|
||||
b = (uint8_t)(bf * 255);
|
||||
data[i] = (r << 0) | (g << 8) | (b << 16) | (a << 24);
|
||||
}
|
||||
|
||||
const char *png = png_from_32bit_rgba((char *)data, width, height, &sz, true);
|
||||
if (!sz) fatal("Failed to save PNG to %s with error: %s", filename, png);
|
||||
free(data);
|
||||
FILE *file = fopen(filename, "wb");
|
||||
fwrite(png, 1, sz, file);
|
||||
fclose(file);
|
||||
glBindTexture(GL_TEXTURE_2D, prev_tex);
|
||||
}
|
||||
|
||||
|
||||
// }}}
|
||||
|
||||
// Programs {{{
|
||||
|
||||
static Program programs[64] = {{0}};
|
||||
|
||||
GLuint
|
||||
compile_shaders(GLenum shader_type, GLsizei count, const GLchar *const *source) {
|
||||
GLuint shader_id = glCreateShader(shader_type);
|
||||
glShaderSource(shader_id, count, source, NULL);
|
||||
glCompileShader(shader_id);
|
||||
GLint ret = GL_FALSE;
|
||||
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &ret);
|
||||
if (ret != GL_TRUE) {
|
||||
GLsizei len;
|
||||
static char glbuf[4096];
|
||||
glGetShaderInfoLog(shader_id, sizeof(glbuf), &len, glbuf);
|
||||
glDeleteShader(shader_id);
|
||||
const char *shader_type_name = "unknown_type";
|
||||
switch (shader_type) {
|
||||
case GL_VERTEX_SHADER: shader_type_name = "vertex"; break;
|
||||
case GL_FRAGMENT_SHADER: shader_type_name = "fragment"; break;
|
||||
}
|
||||
PyErr_Format(PyExc_ValueError, "Failed to compile GLSL %s shader:\n%s", shader_type_name, glbuf);
|
||||
return 0;
|
||||
}
|
||||
return shader_id;
|
||||
}
|
||||
|
||||
Program *
|
||||
program_ptr(int program) {
|
||||
return programs + (size_t)program;
|
||||
}
|
||||
|
||||
GLuint
|
||||
program_id(int program) {
|
||||
return programs[program].id;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
init_uniforms(int program) {
|
||||
Program *p = programs + program;
|
||||
glGetProgramiv(p->id, GL_ACTIVE_UNIFORMS, &(p->num_of_uniforms));
|
||||
for (GLint i = 0; i < p->num_of_uniforms; i++) {
|
||||
Uniform *u = p->uniforms + i;
|
||||
glGetActiveUniform(p->id, (GLuint)i, sizeof(u->name) / sizeof(u->name[0]), NULL, &(u->size), &(u->type), u->name);
|
||||
char *l = strchr(u->name, '[');
|
||||
if (l) *l = 0;
|
||||
u->location = glGetUniformLocation(p->id, u->name);
|
||||
u->idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
GLint
|
||||
get_uniform_location(int program, const char *name) {
|
||||
Program *p = programs + program;
|
||||
const size_t n = strlen(name) + 1;
|
||||
for (GLint i = 0; i < p->num_of_uniforms; i++) {
|
||||
Uniform *u = p->uniforms + i;
|
||||
if (strncmp(u->name, name, n) == 0) return u->location;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
GLint
|
||||
get_uniform_information(int program, const char *name, GLenum information_type) {
|
||||
GLint q;
|
||||
GLuint t;
|
||||
const char *names[] = {""};
|
||||
names[0] = name;
|
||||
GLuint pid = program_id(program);
|
||||
glGetUniformIndices(pid, 1, (void *)names, &t);
|
||||
glGetActiveUniformsiv(pid, 1, &t, information_type, &q);
|
||||
return q;
|
||||
}
|
||||
|
||||
ArrayInformation
|
||||
get_uniform_array_information(int program, const char *name) {
|
||||
ArrayInformation ans;
|
||||
const char *names[] = {""};
|
||||
names[0] = name;
|
||||
GLuint pid = program_id(program);
|
||||
GLuint t;
|
||||
glGetUniformIndices(pid, 1, (void *)names, &t);
|
||||
if (t == GL_INVALID_INDEX) { fatal("Could not find the index for uniform array: %s", name); }
|
||||
glGetActiveUniformsiv(pid, 1, &t, GL_UNIFORM_ARRAY_STRIDE, &ans.stride);
|
||||
glGetActiveUniformsiv(pid, 1, &t, GL_UNIFORM_OFFSET, &ans.offset);
|
||||
glGetActiveUniformsiv(pid, 1, &t, GL_UNIFORM_SIZE, &ans.size);
|
||||
return ans;
|
||||
}
|
||||
|
||||
GLint
|
||||
attrib_location(int program, const char *name) {
|
||||
GLint ans = glGetAttribLocation(programs[program].id, name);
|
||||
return ans;
|
||||
}
|
||||
|
||||
GLuint
|
||||
block_index(int program, const char *name) {
|
||||
GLuint ans = glGetUniformBlockIndex(programs[program].id, name);
|
||||
if (ans == GL_INVALID_INDEX) { fatal("Could not find block index for %s", name); }
|
||||
return ans;
|
||||
}
|
||||
|
||||
|
||||
GLint
|
||||
block_size(int program, GLuint block_index) {
|
||||
GLint ans;
|
||||
glGetActiveUniformBlockiv(programs[program].id, block_index, GL_UNIFORM_BLOCK_DATA_SIZE, &ans);
|
||||
return ans;
|
||||
}
|
||||
|
||||
// Program metadata (uniform/attribute locations and UBO/array info, looked up by name) {{{
|
||||
#define NAME program_metadata_map
|
||||
#define KEY_TY const char *
|
||||
#define VAL_TY ProgramMetadataEntry
|
||||
#include "kitty-verstable.h"
|
||||
|
||||
static program_metadata_map program_layouts[arraysz(programs)];
|
||||
static bool program_layouts_initialized[arraysz(programs)] = {0};
|
||||
|
||||
static void
|
||||
clear_program_layout(int program) {
|
||||
if (!program_layouts_initialized[program]) return;
|
||||
program_metadata_map *m = program_layouts + program;
|
||||
vt_create_for_loop(program_metadata_map_itr, itr, m) free((void *)itr.data->key);
|
||||
vt_cleanup(m);
|
||||
program_layouts_initialized[program] = false;
|
||||
}
|
||||
|
||||
void
|
||||
free_program_layouts(void) {
|
||||
for (size_t i = 0; i < arraysz(program_layouts); i++) clear_program_layout((int)i);
|
||||
}
|
||||
|
||||
static void
|
||||
insert_program_metadata(int program, const char *name, ProgramMetadataEntry entry) {
|
||||
program_metadata_map_itr itr = vt_insert(program_layouts + program, strdup(name), entry);
|
||||
if (vt_is_end(itr)) fatal("Out of memory adding program metadata entry: %s", name);
|
||||
}
|
||||
|
||||
static ProgramMetadataEntry *
|
||||
program_metadata_entry(int program, const char *name, ProgramMetadataKind expected) {
|
||||
program_metadata_map_itr itr = vt_get(program_layouts + program, name);
|
||||
if (vt_is_end(itr)) fatal("No metadata entry named: %s for program: %d", name, program);
|
||||
if (itr.data->val.kind != expected) fatal("The metadata entry named: %s for program: %d is not of the expected kind", name, program);
|
||||
return &itr.data->val;
|
||||
}
|
||||
|
||||
void
|
||||
set_program_layout(int program, PyObject *metadata) {
|
||||
clear_program_layout(program);
|
||||
vt_init(program_layouts + program);
|
||||
program_layouts_initialized[program] = true;
|
||||
|
||||
PyObject *key, *val;
|
||||
Py_ssize_t pos;
|
||||
|
||||
PyObject *loose_uniforms = PyDict_GetItemString(metadata, "loose_uniforms");
|
||||
pos = 0;
|
||||
while (PyDict_Next(loose_uniforms, &pos, &key, &val)) {
|
||||
const char *actual_name = PyUnicode_AsUTF8(val);
|
||||
// The metadata records array uniforms with a [0] subscript to mark them
|
||||
// as arrays, but init_uniforms() stores them under their bare name.
|
||||
char without_subscript[256];
|
||||
const char *bracket = strchr(actual_name, '[');
|
||||
if (bracket) {
|
||||
size_t n = MIN(sizeof(without_subscript) - 1, (size_t)(bracket - actual_name));
|
||||
memcpy(without_subscript, actual_name, n);
|
||||
without_subscript[n] = 0;
|
||||
actual_name = without_subscript;
|
||||
}
|
||||
ProgramMetadataEntry e = {.kind = PROGRAM_UNIFORM, .location = get_uniform_location(program, actual_name)};
|
||||
insert_program_metadata(program, PyUnicode_AsUTF8(key), e);
|
||||
}
|
||||
|
||||
PyObject *input_locations = PyDict_GetItemString(metadata, "input_locations");
|
||||
pos = 0;
|
||||
while (PyDict_Next(input_locations, &pos, &key, &val)) {
|
||||
ProgramMetadataEntry e = {.kind = PROGRAM_ATTRIBUTE, .location = (GLint)PyLong_AsLong(val)};
|
||||
insert_program_metadata(program, PyUnicode_AsUTF8(key), e);
|
||||
}
|
||||
|
||||
PyObject *uniform_structs = PyDict_GetItemString(metadata, "uniform_structs");
|
||||
PyObject *uniform_struct_names = PyDict_GetItemString(metadata, "uniform_struct_names");
|
||||
pos = 0;
|
||||
while (PyDict_Next(uniform_structs, &pos, &key, &val)) {
|
||||
const char *glsl_block_name = PyUnicode_AsUTF8(PyDict_GetItem(uniform_struct_names, key));
|
||||
GLuint index = block_index(program, glsl_block_name);
|
||||
ProgramMetadataEntry be = {.kind = PROGRAM_BLOCK, .block = {.size = block_size(program, index), .index = (GLint)index}};
|
||||
insert_program_metadata(program, PyUnicode_AsUTF8(key), be);
|
||||
|
||||
PyObject *mkey, *mval;
|
||||
Py_ssize_t mpos = 0;
|
||||
while (PyDict_Next(val, &mpos, &mkey, &mval)) {
|
||||
const char *decl = PyUnicode_AsUTF8(mval);
|
||||
const char *bracket = strchr(decl, '[');
|
||||
if (!bracket) continue; // scalar block members are hand-mirrored in C structs, not queried here
|
||||
char actual_name[256];
|
||||
size_t n = MIN(sizeof(actual_name) - 1, (size_t)(bracket - decl));
|
||||
memcpy(actual_name, decl, n);
|
||||
actual_name[n] = 0;
|
||||
ProgramMetadataEntry ae = {.kind = PROGRAM_ARRAY, .array = get_uniform_array_information(program, actual_name)};
|
||||
insert_program_metadata(program, PyUnicode_AsUTF8(mkey), ae);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GLint
|
||||
program_uniform_location(int program, const char *name) {
|
||||
ProgramMetadataEntry *e = program_metadata_entry(program, name, PROGRAM_UNIFORM);
|
||||
// glUniform*() on location -1 is a silent no-op, so a uniform the driver
|
||||
// does not report as active leaves whatever value it was last set to,
|
||||
// rendering incorrectly with no other indication that anything is wrong.
|
||||
if (e->location < 0 && !e->missing_location_logged) {
|
||||
e->missing_location_logged = true;
|
||||
log_error("The uniform: %s is not active in the linked GPU program: %d. Setting it will have no effect and rendering may be incorrect.", name, program);
|
||||
}
|
||||
return e->location;
|
||||
}
|
||||
|
||||
GLint
|
||||
try_program_uniform_location(int program, const char *name) {
|
||||
program_metadata_map_itr itr = vt_get(program_layouts + program, name);
|
||||
if (vt_is_end(itr) || itr.data->val.kind != PROGRAM_UNIFORM) return -1;
|
||||
return itr.data->val.location;
|
||||
}
|
||||
|
||||
GLint
|
||||
program_attribute_location(int program, const char *name) {
|
||||
return program_metadata_entry(program, name, PROGRAM_ATTRIBUTE)->location;
|
||||
}
|
||||
|
||||
UniformBlock
|
||||
program_uniform_block(int program, const char *name) {
|
||||
return program_metadata_entry(program, name, PROGRAM_BLOCK)->block;
|
||||
}
|
||||
|
||||
ArrayInformation
|
||||
program_uniform_array(int program, const char *name) {
|
||||
return program_metadata_entry(program, name, PROGRAM_ARRAY)->array;
|
||||
}
|
||||
// }}}
|
||||
|
||||
void
|
||||
bind_program(int program) {
|
||||
glUseProgram(programs[program].id);
|
||||
}
|
||||
|
||||
void
|
||||
unbind_program(void) {
|
||||
glUseProgram(0);
|
||||
}
|
||||
// }}}
|
||||
|
||||
// Buffers {{{
|
||||
|
||||
typedef struct {
|
||||
GLuint id;
|
||||
GLsizeiptr size;
|
||||
GLenum usage;
|
||||
} Buffer;
|
||||
|
||||
|
||||
static Buffer buffers[MAX_CHILDREN * 6 + 4] = {{0}};
|
||||
|
||||
static ssize_t
|
||||
create_buffer(GLenum usage) {
|
||||
GLuint buffer_id;
|
||||
glGenBuffers(1, &buffer_id);
|
||||
for (size_t i = 0; i < sizeof(buffers) / sizeof(buffers[0]); i++) {
|
||||
if (buffers[i].id == 0) {
|
||||
buffers[i].id = buffer_id;
|
||||
buffers[i].size = 0;
|
||||
buffers[i].usage = usage;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
glDeleteBuffers(1, &buffer_id);
|
||||
fatal("Too many buffers");
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void
|
||||
delete_buffer(ssize_t buf_idx) {
|
||||
glDeleteBuffers(1, &(buffers[buf_idx].id));
|
||||
buffers[buf_idx].id = 0;
|
||||
buffers[buf_idx].size = 0;
|
||||
}
|
||||
|
||||
static GLuint
|
||||
bind_buffer(ssize_t buf_idx) {
|
||||
glBindBuffer(buffers[buf_idx].usage, buffers[buf_idx].id);
|
||||
return buffers[buf_idx].id;
|
||||
}
|
||||
|
||||
static void
|
||||
unbind_buffer(ssize_t buf_idx) {
|
||||
glBindBuffer(buffers[buf_idx].usage, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
alloc_buffer(ssize_t idx, GLsizeiptr size, GLenum usage) {
|
||||
Buffer *b = buffers + idx;
|
||||
if (b->size == size) return;
|
||||
b->size = size;
|
||||
glBufferData(b->usage, size, NULL, usage);
|
||||
}
|
||||
|
||||
static void *
|
||||
map_buffer(ssize_t idx, GLenum access) {
|
||||
void *ans = glMapBuffer(buffers[idx].usage, access);
|
||||
return ans;
|
||||
}
|
||||
|
||||
static void *
|
||||
map_buffer_range(ssize_t idx, GLbitfield access, int offset, unsigned size) {
|
||||
return glMapBufferRange(buffers[idx].usage, offset, size, access);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
unmap_buffer(ssize_t idx) {
|
||||
glUnmapBuffer(buffers[idx].usage);
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
// Vertex Array Objects (VAO) {{{
|
||||
|
||||
typedef struct {
|
||||
GLuint id;
|
||||
size_t num_buffers;
|
||||
ssize_t buffers[10];
|
||||
} VAO;
|
||||
|
||||
static VAO vaos[4 * MAX_CHILDREN + 10] = {{0}};
|
||||
|
||||
ssize_t
|
||||
create_vao(void) {
|
||||
GLuint vao_id;
|
||||
glGenVertexArrays(1, &vao_id);
|
||||
for (size_t i = 0; i < sizeof(vaos) / sizeof(vaos[0]); i++) {
|
||||
if (!vaos[i].id) {
|
||||
vaos[i].id = vao_id;
|
||||
vaos[i].num_buffers = 0;
|
||||
glBindVertexArray(vao_id);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
glDeleteVertexArrays(1, &vao_id);
|
||||
fatal("Too many VAOs");
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t
|
||||
add_buffer_to_vao(ssize_t vao_idx, GLenum usage) {
|
||||
VAO *vao = vaos + vao_idx;
|
||||
if (vao->num_buffers >= sizeof(vao->buffers) / sizeof(vao->buffers[0])) { fatal("Too many buffers in a single VAO"); }
|
||||
ssize_t buf = create_buffer(usage);
|
||||
vao->buffers[vao->num_buffers++] = buf;
|
||||
return vao->num_buffers - 1;
|
||||
}
|
||||
|
||||
static void
|
||||
add_located_attribute_to_vao(ssize_t vao_idx, GLint aloc, GLint size, GLenum data_type, GLsizei stride, void *offset, GLuint divisor) {
|
||||
VAO *vao = vaos + vao_idx;
|
||||
if (!vao->num_buffers) fatal("You must create a buffer for this attribute first");
|
||||
ssize_t buf = vao->buffers[vao->num_buffers - 1];
|
||||
bind_buffer(buf);
|
||||
glEnableVertexAttribArray(aloc);
|
||||
switch (data_type) {
|
||||
case GL_BYTE:
|
||||
case GL_UNSIGNED_BYTE:
|
||||
case GL_SHORT:
|
||||
case GL_UNSIGNED_SHORT:
|
||||
case GL_INT:
|
||||
case GL_UNSIGNED_INT: glVertexAttribIPointer(aloc, size, data_type, stride, offset); break;
|
||||
default: glVertexAttribPointer(aloc, size, data_type, GL_FALSE, stride, offset); break;
|
||||
}
|
||||
if (divisor) { glVertexAttribDivisorARB(aloc, divisor); }
|
||||
unbind_buffer(buf);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
add_attribute_to_vao(ssize_t vao_idx, int location, GLint size, GLenum data_type, GLsizei stride, void *offset, GLuint divisor) {
|
||||
add_located_attribute_to_vao(vao_idx, location, size, data_type, stride, offset, divisor);
|
||||
}
|
||||
|
||||
void
|
||||
set_vao_attribute(ssize_t vao_idx, size_t buffer_idx, int location, GLint size, GLenum data_type, GLsizei stride, void *offset, GLuint divisor) {
|
||||
// (Re)configure an attribute pointer that reads from a specific buffer of
|
||||
// the VAO (unlike add_attribute_to_vao which always uses the last added
|
||||
// buffer). The VAO must be bound before calling this.
|
||||
VAO *vao = vaos + vao_idx;
|
||||
ssize_t buf = vao->buffers[buffer_idx];
|
||||
bind_buffer(buf);
|
||||
glEnableVertexAttribArray(location);
|
||||
switch (data_type) {
|
||||
case GL_BYTE:
|
||||
case GL_UNSIGNED_BYTE:
|
||||
case GL_SHORT:
|
||||
case GL_UNSIGNED_SHORT:
|
||||
case GL_INT:
|
||||
case GL_UNSIGNED_INT: glVertexAttribIPointer(location, size, data_type, stride, offset); break;
|
||||
default: glVertexAttribPointer(location, size, data_type, GL_FALSE, stride, offset); break;
|
||||
}
|
||||
glVertexAttribDivisorARB(location, divisor);
|
||||
unbind_buffer(buf);
|
||||
}
|
||||
|
||||
void
|
||||
remove_vao(ssize_t vao_idx) {
|
||||
VAO *vao = vaos + vao_idx;
|
||||
while (vao->num_buffers) {
|
||||
vao->num_buffers--;
|
||||
delete_buffer(vao->buffers[vao->num_buffers]);
|
||||
}
|
||||
glDeleteVertexArrays(1, &(vao->id));
|
||||
vaos[vao_idx].id = 0;
|
||||
}
|
||||
|
||||
void
|
||||
bind_vertex_array(ssize_t vao_idx) {
|
||||
glBindVertexArray(vaos[vao_idx].id);
|
||||
}
|
||||
|
||||
void
|
||||
unbind_vertex_array(void) {
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
alloc_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum, GLenum usage) {
|
||||
ssize_t buf_idx = vaos[vao_idx].buffers[bufnum];
|
||||
bind_buffer(buf_idx);
|
||||
alloc_buffer(buf_idx, size, usage);
|
||||
return buf_idx;
|
||||
}
|
||||
|
||||
void *
|
||||
map_vao_buffer_for_write_only(ssize_t vao_idx, size_t bufnum, int offset, unsigned size) {
|
||||
ssize_t buf_idx = vaos[vao_idx].buffers[bufnum];
|
||||
bind_buffer(buf_idx);
|
||||
return map_buffer_range(buf_idx, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT, offset, size);
|
||||
}
|
||||
|
||||
void *
|
||||
alloc_and_map_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum) {
|
||||
ssize_t buf_idx = alloc_vao_buffer(vao_idx, size, bufnum, GL_STATIC_DRAW);
|
||||
return map_buffer(buf_idx, GL_WRITE_ONLY);
|
||||
}
|
||||
|
||||
void *
|
||||
alloc_and_map_vao_buffer_for_full_rewrite(ssize_t vao_idx, GLsizeiptr size, size_t bufnum) {
|
||||
// The previous contents are discarded so the driver can hand out fresh
|
||||
// storage instead of waiting for the GPU to finish reading the old data.
|
||||
// The caller MUST write every byte of the mapped range, any byte not
|
||||
// written is undefined.
|
||||
ssize_t buf_idx = alloc_vao_buffer(vao_idx, size, bufnum, GL_STREAM_DRAW);
|
||||
return map_buffer_range(buf_idx, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT, 0, (unsigned)size);
|
||||
}
|
||||
|
||||
void
|
||||
bind_vao_uniform_buffer(ssize_t vao_idx, size_t bufnum, GLuint block_index) {
|
||||
ssize_t buf_idx = vaos[vao_idx].buffers[bufnum];
|
||||
glBindBufferBase(GL_UNIFORM_BUFFER, block_index, buffers[buf_idx].id);
|
||||
}
|
||||
|
||||
void
|
||||
unmap_vao_buffer(ssize_t vao_idx, size_t bufnum) {
|
||||
ssize_t buf_idx = vaos[vao_idx].buffers[bufnum];
|
||||
unmap_buffer(buf_idx);
|
||||
unbind_buffer(buf_idx);
|
||||
}
|
||||
|
||||
void
|
||||
copy_vao_buffer_region(ssize_t vao_idx, size_t src_bufnum, GLintptr src_off, size_t dst_bufnum, GLintptr dst_off, GLsizeiptr size) {
|
||||
ssize_t src_buf = vaos[vao_idx].buffers[src_bufnum];
|
||||
ssize_t dst_buf = vaos[vao_idx].buffers[dst_bufnum];
|
||||
glBindBuffer(GL_COPY_READ_BUFFER, buffers[src_buf].id);
|
||||
glBindBuffer(GL_COPY_WRITE_BUFFER, buffers[dst_buf].id);
|
||||
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, src_off, dst_off, size);
|
||||
glBindBuffer(GL_COPY_READ_BUFFER, 0);
|
||||
glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
|
||||
}
|
||||
|
||||
const void *
|
||||
map_vao_buffer_for_reading(ssize_t vao_idx, size_t bufnum) {
|
||||
ssize_t buf_idx = vaos[vao_idx].buffers[bufnum];
|
||||
bind_buffer(buf_idx);
|
||||
return map_buffer(buf_idx, GL_READ_ONLY);
|
||||
}
|
||||
|
||||
// }}}
|
||||
100
kitty/gl.h
100
kitty/gl.h
|
|
@ -1,100 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Kovid Goyal <kovid at kovidgoyal.net>
|
||||
*
|
||||
* Distributed under terms of the GPL3 license.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "data-types.h"
|
||||
#include "gl-wrapper.h"
|
||||
|
||||
typedef struct {
|
||||
GLint size, index;
|
||||
} UniformBlock;
|
||||
|
||||
typedef struct {
|
||||
GLint offset; // byte offset from start of UBO
|
||||
GLint stride; // element stride in bytes
|
||||
GLint size; // umber of elements
|
||||
} ArrayInformation;
|
||||
|
||||
typedef struct {
|
||||
char name[256];
|
||||
GLint size, location, idx;
|
||||
GLenum type;
|
||||
} Uniform;
|
||||
|
||||
typedef struct {
|
||||
GLuint id;
|
||||
Uniform uniforms[256];
|
||||
GLint num_of_uniforms;
|
||||
} Program;
|
||||
|
||||
typedef struct Viewport {
|
||||
unsigned left, top, width, height;
|
||||
} Viewport;
|
||||
|
||||
typedef enum { PROGRAM_UNIFORM, PROGRAM_ATTRIBUTE, PROGRAM_BLOCK, PROGRAM_ARRAY } ProgramMetadataKind;
|
||||
typedef struct ProgramMetadataEntry {
|
||||
ProgramMetadataKind kind;
|
||||
bool missing_location_logged;
|
||||
union {
|
||||
GLint location;
|
||||
UniformBlock block;
|
||||
ArrayInformation array;
|
||||
};
|
||||
} ProgramMetadataEntry;
|
||||
|
||||
void gl_init(void);
|
||||
const char *gl_version_string(void);
|
||||
void set_gpu_viewport(unsigned w, unsigned h);
|
||||
Viewport get_gpu_viewport(void);
|
||||
void draw_quad(bool blend, unsigned instance_count);
|
||||
void save_texture_as_png(uint32_t texture_id, const char *filename);
|
||||
void free_texture(GLuint *tex_id);
|
||||
void free_framebuffer(GLuint *fb_id);
|
||||
void remove_vao(ssize_t vao_idx);
|
||||
void init_uniforms(int program);
|
||||
GLuint program_id(int program);
|
||||
Program *program_ptr(int program);
|
||||
GLuint block_index(int program, const char *name);
|
||||
GLint block_size(int program, GLuint block_index);
|
||||
GLint get_uniform_location(int program, const char *name);
|
||||
GLint get_uniform_information(int program, const char *name, GLenum information_type);
|
||||
ArrayInformation get_uniform_array_information(int program, const char *name);
|
||||
GLint attrib_location(int program, const char *name);
|
||||
void set_program_layout(int program, PyObject *metadata);
|
||||
void free_program_layouts(void);
|
||||
GLint program_uniform_location(int program, const char *name);
|
||||
GLint try_program_uniform_location(int program, const char *name);
|
||||
GLint program_attribute_location(int program, const char *name);
|
||||
UniformBlock program_uniform_block(int program, const char *name);
|
||||
ArrayInformation program_uniform_array(int program, const char *name);
|
||||
ssize_t create_vao(void);
|
||||
size_t add_buffer_to_vao(ssize_t vao_idx, GLenum usage);
|
||||
void add_attribute_to_vao(ssize_t vao_idx, int location, GLint size, GLenum data_type, GLsizei stride, void *offset, GLuint divisor);
|
||||
void set_vao_attribute(ssize_t vao_idx, size_t buffer_idx, int location, GLint size, GLenum data_type, GLsizei stride, void *offset, GLuint divisor);
|
||||
ssize_t alloc_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum, GLenum usage);
|
||||
void *alloc_and_map_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum);
|
||||
// Discards the old contents, the caller must write every byte of the buffer
|
||||
void *alloc_and_map_vao_buffer_for_full_rewrite(ssize_t vao_idx, GLsizeiptr size, size_t bufnum);
|
||||
void unmap_vao_buffer(ssize_t vao_idx, size_t bufnum);
|
||||
void *map_vao_buffer(ssize_t vao_idx, size_t bufnum, GLenum access);
|
||||
void *map_vao_buffer_for_write_only(ssize_t vao_idx, size_t bufnum, int offset, unsigned size);
|
||||
void bind_program(int program);
|
||||
void bind_vertex_array(ssize_t vao_idx);
|
||||
void bind_vao_uniform_buffer(ssize_t vao_idx, size_t bufnum, GLuint block_index);
|
||||
void copy_vao_buffer_region(ssize_t vao_idx, size_t src_bufnum, GLintptr src_off, size_t dst_bufnum, GLintptr dst_off, GLsizeiptr size);
|
||||
const void *map_vao_buffer_for_reading(ssize_t vao_idx, size_t bufnum);
|
||||
void unbind_vertex_array(void);
|
||||
void unbind_program(void);
|
||||
GLuint compile_shaders(GLenum shader_type, GLsizei count, const GLchar *const *string);
|
||||
void save_viewport_using_top_left_origin(GLsizei x, GLsizei y, GLsizei width, GLsizei height, GLsizei full_framebuffer_height);
|
||||
void save_viewport_using_bottom_left_origin(GLsizei x, GLsizei y, GLsizei width, GLsizei height);
|
||||
const char *check_framebuffer_status(void);
|
||||
void restore_viewport(void);
|
||||
void bind_framebuffer_for_output(unsigned fbid);
|
||||
void set_framebuffer_to_use_for_output(unsigned fbid);
|
||||
void enable_scissor_using_top_left_origin(Viewport, unsigned);
|
||||
void disable_scissor(void);
|
||||
10
kitty/glfw.c
10
kitty/glfw.c
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include "state.h"
|
||||
#include "gpu.h"
|
||||
#include "cleanup.h"
|
||||
#include "monotonic.h"
|
||||
#include "dnd.h"
|
||||
|
|
@ -2782,10 +2783,10 @@ dbus_monitor_session_state(PyObject *self UNUSED, PyObject *callback) {
|
|||
|
||||
static PyObject *
|
||||
glfw_init(PyObject UNUSED *self, PyObject *args) {
|
||||
const char *path;
|
||||
const char *path, *gpu_driver_path;
|
||||
int debug_keyboard = 0, debug_rendering = 0, wayland_enable_ime = 0;
|
||||
PyObject *edge_sf;
|
||||
if (!PyArg_ParseTuple(args, "sO|ppp", &path, &edge_sf, &debug_keyboard, &debug_rendering, &wayland_enable_ime)) return NULL;
|
||||
if (!PyArg_ParseTuple(args, "ssO|ppp", &path, &gpu_driver_path, &edge_sf, &debug_keyboard, &debug_rendering, &wayland_enable_ime)) return NULL;
|
||||
if (!PyCallable_Check(edge_sf)) {
|
||||
PyErr_SetString(PyExc_TypeError, "edge_spacing_func must be a callable");
|
||||
return NULL;
|
||||
|
|
@ -2800,6 +2801,11 @@ glfw_init(PyObject UNUSED *self, PyObject *args) {
|
|||
return NULL;
|
||||
}
|
||||
glfw_is_loaded = true;
|
||||
err = load_gpu_driver(gpu_driver_path, debug_rendering != 0, glfwGetProcAddress);
|
||||
if (err) {
|
||||
PyErr_SetString(PyExc_RuntimeError, err);
|
||||
return NULL;
|
||||
}
|
||||
push_modifier_remap_to_glfw();
|
||||
glfwSetErrorCallback(error_callback);
|
||||
glfwInitHint(GLFW_DEBUG_KEYBOARD, debug_keyboard);
|
||||
|
|
|
|||
531
kitty/gpu.c
Normal file
531
kitty/gpu.c
Normal file
|
|
@ -0,0 +1,531 @@
|
|||
/*
|
||||
* gpu.c
|
||||
* Copyright (C) 2026 Kovid Goyal <kovid at kovidgoyal.net>
|
||||
*
|
||||
* Distributed under terms of the GPL3 license.
|
||||
*/
|
||||
|
||||
#include "gpu.h"
|
||||
#include "state.h"
|
||||
#include "png-reader.h"
|
||||
#include <dlfcn.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
GPUDriver gpu_driver = {0};
|
||||
|
||||
// Driver loading {{{
|
||||
static void
|
||||
driver_log_error(const char *msg) {
|
||||
log_error("%s", msg);
|
||||
}
|
||||
|
||||
static void
|
||||
driver_debug_print(const char *msg) {
|
||||
timed_debug_print("%s\n", msg);
|
||||
}
|
||||
|
||||
static void
|
||||
driver_fatal(const char *msg) {
|
||||
fatal("%s", msg);
|
||||
}
|
||||
|
||||
const char *
|
||||
load_gpu_driver(const char *path, bool debug_rendering, gpu_get_proc_address_func get_proc_address) {
|
||||
static char errbuf[2048];
|
||||
if (gpu_driver.driver) return NULL;
|
||||
void *handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
|
||||
if (!handle) {
|
||||
snprintf(errbuf, sizeof(errbuf), "Failed to load the GPU driver from %s with error: %s", path, dlerror());
|
||||
return errbuf;
|
||||
}
|
||||
dlerror();
|
||||
gpu_driver_load_func load = NULL;
|
||||
*(void **)(&load) = dlsym(handle, KITTY_GPU_DRIVER_ENTRY_POINT);
|
||||
if (!load) {
|
||||
snprintf(errbuf, sizeof(errbuf), "The GPU driver at %s does not export %s, with error: %s", path, KITTY_GPU_DRIVER_ENTRY_POINT, dlerror());
|
||||
dlclose(handle);
|
||||
return errbuf;
|
||||
}
|
||||
const GPUDriverInitParams params = {
|
||||
.get_proc_address = get_proc_address,
|
||||
.debug_rendering = debug_rendering,
|
||||
.log_error = driver_log_error,
|
||||
.debug_print = driver_debug_print,
|
||||
.fatal = driver_fatal,
|
||||
};
|
||||
GPUDriverAPI api = {0};
|
||||
void *driver = NULL;
|
||||
char lerr[1024] = {0};
|
||||
if (!load(KITTY_GPU_API_VERSION, ¶ms, &api, &driver, lerr, sizeof(lerr))) {
|
||||
snprintf(errbuf, sizeof(errbuf), "Failed to load the GPU driver from %s with error: %s", path, lerr);
|
||||
dlclose(handle);
|
||||
return errbuf;
|
||||
}
|
||||
if (api.api_version != KITTY_GPU_API_VERSION) {
|
||||
snprintf(errbuf, sizeof(errbuf), "The GPU driver at %s implements API version %u instead of %u", path, api.api_version, KITTY_GPU_API_VERSION);
|
||||
if (api.destroy) api.destroy(driver);
|
||||
dlclose(handle);
|
||||
return errbuf;
|
||||
}
|
||||
gpu_driver.api = api;
|
||||
gpu_driver.driver = driver;
|
||||
gpu_driver.dl_handle = handle;
|
||||
gpu_driver.initialized = false;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
initialize_gpu_driver(void) {
|
||||
if (gpu_driver.initialized) return;
|
||||
if (!gpu_driver.driver) fatal("Cannot initialize the GPU as no GPU driver has been loaded");
|
||||
if (!gpu_driver.api.initialize(gpu_driver.driver))
|
||||
fatal("Failed to initialize the GPU driver with error: %s", gpu_driver.api.last_error(gpu_driver.driver));
|
||||
gpu_driver.initialized = true;
|
||||
}
|
||||
|
||||
void
|
||||
unload_gpu_driver(void) {
|
||||
// The shared library is not unloaded since code registered with
|
||||
// atexit() or TLS destructors could still reference it
|
||||
if (gpu_driver.driver) gpu_driver.api.destroy(gpu_driver.driver);
|
||||
gpu_driver.driver = NULL;
|
||||
gpu_driver.initialized = false;
|
||||
}
|
||||
// }}}
|
||||
|
||||
// Viewports, framebuffers and textures {{{
|
||||
void
|
||||
set_gpu_viewport(unsigned w, unsigned h) {
|
||||
gpu_set_viewport(0, 0, w, h);
|
||||
}
|
||||
|
||||
static struct {
|
||||
int32_t items[16][4];
|
||||
size_t used;
|
||||
} saved_viewports;
|
||||
|
||||
void
|
||||
save_viewport_using_bottom_left_origin(int32_t newx, int32_t newy, uint32_t width, uint32_t height) {
|
||||
if (saved_viewports.used >= arraysz(saved_viewports.items)) fatal("Too many nested saved viewports");
|
||||
int32_t *saved_viewport = saved_viewports.items[saved_viewports.used++];
|
||||
gpu_get_viewport(saved_viewport);
|
||||
gpu_set_viewport(newx, newy, width, height);
|
||||
}
|
||||
|
||||
void
|
||||
save_viewport_using_top_left_origin(int32_t newx, int32_t newy, uint32_t width, uint32_t height, int32_t full_framebuffer_height) {
|
||||
// Converts the viewport defined by the specified arguments which are
|
||||
// assumed to be in the usual co-ord system with origin at top left to the
|
||||
// GPU viewport co-ord system with origin at bottom left.
|
||||
// Use restore_viewport() to restore the viewport to what it was before.
|
||||
newy = full_framebuffer_height - (newy + (int32_t)height);
|
||||
save_viewport_using_bottom_left_origin(newx, newy, width, height);
|
||||
}
|
||||
|
||||
void
|
||||
restore_viewport(void) {
|
||||
if (!saved_viewports.used) fatal("Trying to restore a viewport when none is saved");
|
||||
const int32_t *saved_viewport = saved_viewports.items[--saved_viewports.used];
|
||||
gpu_set_viewport(saved_viewport[0], saved_viewport[1], (uint32_t)saved_viewport[2], (uint32_t)saved_viewport[3]);
|
||||
}
|
||||
|
||||
void
|
||||
enable_scissor_using_top_left_origin(Viewport vp, unsigned full_framebuffer_height) {
|
||||
int32_t newy = (int32_t)full_framebuffer_height - (int32_t)(vp.top + vp.height);
|
||||
gpu_set_scissor(true, vp.left, newy, vp.width, vp.height);
|
||||
}
|
||||
|
||||
void
|
||||
disable_scissor(void) {
|
||||
gpu_set_scissor(false, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
void
|
||||
draw_quad(bool blend, unsigned instance_count) {
|
||||
gpu_draw_quad(instance_count, blend ? GPU_BLEND_PREMULTIPLIED_ALPHA : GPU_BLEND_NONE);
|
||||
}
|
||||
|
||||
// Resources can be freed by Python object destructors during interpreter
|
||||
// shutdown, after the driver has been unloaded, in which case they are simply
|
||||
// dropped, as the process is exiting.
|
||||
void
|
||||
free_texture(gpu_texture_t *tex) {
|
||||
if (gpu_driver.driver) gpu_texture_destroy(*tex);
|
||||
*tex = GPU_NULL_HANDLE;
|
||||
}
|
||||
|
||||
void
|
||||
free_framebuffer(gpu_render_target_t *fb) {
|
||||
if (gpu_driver.driver) gpu_render_target_destroy(*fb);
|
||||
*fb = GPU_NULL_HANDLE;
|
||||
}
|
||||
|
||||
static gpu_render_target_t output_framebuffer = GPU_NULL_HANDLE;
|
||||
|
||||
void
|
||||
bind_framebuffer_for_output(gpu_render_target_t fb) {
|
||||
gpu_set_render_target(fb ? fb : output_framebuffer);
|
||||
}
|
||||
|
||||
void
|
||||
set_framebuffer_to_use_for_output(gpu_render_target_t fb) {
|
||||
output_framebuffer = fb;
|
||||
}
|
||||
|
||||
const char *
|
||||
check_framebuffer_status(void) {
|
||||
return gpu_render_target_status(gpu_get_render_target());
|
||||
}
|
||||
|
||||
static float
|
||||
linear_to_srgb(float c) {
|
||||
return (c <= 0.0031308f) ? 12.92f * c : 1.055f * powf(c, 1.0f / 2.4f) - 0.055f;
|
||||
}
|
||||
|
||||
void
|
||||
save_texture_as_png(gpu_texture_t texture, const char *filename) {
|
||||
GPUTextureDesc desc;
|
||||
gpu_texture_get_desc(texture, &desc);
|
||||
const size_t width = desc.width, height = desc.height;
|
||||
size_t sz = sizeof(uint32_t) * width * height;
|
||||
RAII_ALLOC(uint32_t, data, malloc(sz));
|
||||
if (!data) fatal("Out of memory");
|
||||
if (!gpu_texture_read(texture, GPU_PIXELS_RGBA8, data, sz)) fatal("Failed to read texture with error: %s", gpu_last_error());
|
||||
// assume data is linear and pre-multiplied
|
||||
for (size_t i = 0; i < width * height; i++) {
|
||||
uint32_t px = data[i];
|
||||
uint8_t r = (px >> 0) & 0xFF;
|
||||
uint8_t g = (px >> 8) & 0xFF;
|
||||
uint8_t b = (px >> 16) & 0xFF;
|
||||
uint8_t a = (px >> 24) & 0xFF;
|
||||
float alpha = a / 255.0f;
|
||||
float rf = 0, gf = 0, bf = 0;
|
||||
if (alpha > 0.0f) {
|
||||
rf = (r / 255.0f) / alpha;
|
||||
gf = (g / 255.0f) / alpha;
|
||||
bf = (b / 255.0f) / alpha;
|
||||
}
|
||||
r = (uint8_t)(linear_to_srgb(rf) * 255);
|
||||
g = (uint8_t)(linear_to_srgb(gf) * 255);
|
||||
b = (uint8_t)(linear_to_srgb(bf) * 255);
|
||||
data[i] = (r << 0) | (g << 8) | (b << 16) | ((uint32_t)a << 24);
|
||||
}
|
||||
const char *png = png_from_32bit_rgba((char *)data, width, height, &sz, true);
|
||||
if (!sz) fatal("Failed to save PNG to %s with error: %s", filename, png);
|
||||
FILE *file = fopen(filename, "wb");
|
||||
if (!file) fatal("Failed to open %s for writing with error: %s", filename, strerror(errno));
|
||||
fwrite(png, 1, sz, file);
|
||||
fclose(file);
|
||||
}
|
||||
// }}}
|
||||
|
||||
// Programs {{{
|
||||
typedef enum { PROGRAM_UNIFORM, PROGRAM_ATTRIBUTE, PROGRAM_BLOCK, PROGRAM_ARRAY } ProgramMetadataKind;
|
||||
typedef struct ProgramMetadataEntry {
|
||||
ProgramMetadataKind kind;
|
||||
bool missing_location_logged;
|
||||
union {
|
||||
int32_t location;
|
||||
UniformBlock block;
|
||||
ArrayInformation array;
|
||||
};
|
||||
} ProgramMetadataEntry;
|
||||
|
||||
static gpu_pipeline_t programs[MAX_GPU_PROGRAMS] = {0};
|
||||
|
||||
gpu_pipeline_t
|
||||
program_pipeline(int program) {
|
||||
return programs[program];
|
||||
}
|
||||
|
||||
void
|
||||
set_program_pipeline(int program, gpu_pipeline_t pipeline) {
|
||||
if (programs[program]) gpu_pipeline_destroy(programs[program]);
|
||||
programs[program] = pipeline;
|
||||
}
|
||||
|
||||
int32_t
|
||||
attrib_location(int program, const char *name) {
|
||||
return gpu_pipeline_attribute_location(programs[program], name);
|
||||
}
|
||||
|
||||
static UniformBlock
|
||||
block_info(int program, const char *name) {
|
||||
GPUUniformBlockInfo info;
|
||||
if (!gpu_pipeline_uniform_block(programs[program], name, &info)) fatal("%s", gpu_last_error());
|
||||
return (UniformBlock){.index = (int32_t)info.index, .size = (int32_t)info.size};
|
||||
}
|
||||
|
||||
static ArrayInformation
|
||||
get_uniform_array_information(int program, const char *name) {
|
||||
GPUUniformArrayInfo info;
|
||||
if (!gpu_pipeline_uniform_array(programs[program], name, &info)) fatal("%s", gpu_last_error());
|
||||
return (ArrayInformation){.offset = (int32_t)info.offset, .stride = (int32_t)info.stride, .size = (int32_t)info.count};
|
||||
}
|
||||
|
||||
// Program metadata (uniform/attribute locations and UBO/array info, looked up by name) {{{
|
||||
#define NAME program_metadata_map
|
||||
#define KEY_TY const char *
|
||||
#define VAL_TY ProgramMetadataEntry
|
||||
#include "kitty-verstable.h"
|
||||
|
||||
static program_metadata_map program_layouts[MAX_GPU_PROGRAMS];
|
||||
static bool program_layouts_initialized[MAX_GPU_PROGRAMS] = {0};
|
||||
|
||||
static void
|
||||
clear_program_layout(int program) {
|
||||
if (!program_layouts_initialized[program]) return;
|
||||
program_metadata_map *m = program_layouts + program;
|
||||
vt_create_for_loop(program_metadata_map_itr, itr, m) free((void *)itr.data->key);
|
||||
vt_cleanup(m);
|
||||
program_layouts_initialized[program] = false;
|
||||
}
|
||||
|
||||
void
|
||||
free_program_layouts(void) {
|
||||
for (size_t i = 0; i < arraysz(program_layouts); i++) clear_program_layout((int)i);
|
||||
}
|
||||
|
||||
static void
|
||||
insert_program_metadata(int program, const char *name, ProgramMetadataEntry entry) {
|
||||
program_metadata_map_itr itr = vt_insert(program_layouts + program, strdup(name), entry);
|
||||
if (vt_is_end(itr)) fatal("Out of memory adding program metadata entry: %s", name);
|
||||
}
|
||||
|
||||
static ProgramMetadataEntry *
|
||||
program_metadata_entry(int program, const char *name, ProgramMetadataKind expected) {
|
||||
program_metadata_map_itr itr = vt_get(program_layouts + program, name);
|
||||
if (vt_is_end(itr)) fatal("No metadata entry named: %s for program: %d", name, program);
|
||||
if (itr.data->val.kind != expected) fatal("The metadata entry named: %s for program: %d is not of the expected kind", name, program);
|
||||
return &itr.data->val;
|
||||
}
|
||||
|
||||
void
|
||||
set_program_layout(int program, PyObject *metadata) {
|
||||
clear_program_layout(program);
|
||||
vt_init(program_layouts + program);
|
||||
program_layouts_initialized[program] = true;
|
||||
|
||||
PyObject *key, *val;
|
||||
Py_ssize_t pos;
|
||||
|
||||
PyObject *loose_uniforms = PyDict_GetItemString(metadata, "loose_uniforms");
|
||||
pos = 0;
|
||||
while (PyDict_Next(loose_uniforms, &pos, &key, &val)) {
|
||||
const char *actual_name = PyUnicode_AsUTF8(val);
|
||||
// The metadata records array uniforms with a [0] subscript to mark them
|
||||
// as arrays, but the driver looks them up by their bare name.
|
||||
char without_subscript[256];
|
||||
const char *bracket = strchr(actual_name, '[');
|
||||
if (bracket) {
|
||||
size_t n = MIN(sizeof(without_subscript) - 1, (size_t)(bracket - actual_name));
|
||||
memcpy(without_subscript, actual_name, n);
|
||||
without_subscript[n] = 0;
|
||||
actual_name = without_subscript;
|
||||
}
|
||||
ProgramMetadataEntry e = {.kind = PROGRAM_UNIFORM, .location = gpu_pipeline_uniform_slot(programs[program], actual_name)};
|
||||
insert_program_metadata(program, PyUnicode_AsUTF8(key), e);
|
||||
}
|
||||
|
||||
PyObject *input_locations = PyDict_GetItemString(metadata, "input_locations");
|
||||
pos = 0;
|
||||
while (PyDict_Next(input_locations, &pos, &key, &val)) {
|
||||
ProgramMetadataEntry e = {.kind = PROGRAM_ATTRIBUTE, .location = (int32_t)PyLong_AsLong(val)};
|
||||
insert_program_metadata(program, PyUnicode_AsUTF8(key), e);
|
||||
}
|
||||
|
||||
PyObject *uniform_structs = PyDict_GetItemString(metadata, "uniform_structs");
|
||||
PyObject *uniform_struct_names = PyDict_GetItemString(metadata, "uniform_struct_names");
|
||||
pos = 0;
|
||||
while (PyDict_Next(uniform_structs, &pos, &key, &val)) {
|
||||
const char *glsl_block_name = PyUnicode_AsUTF8(PyDict_GetItem(uniform_struct_names, key));
|
||||
ProgramMetadataEntry be = {.kind = PROGRAM_BLOCK, .block = block_info(program, glsl_block_name)};
|
||||
insert_program_metadata(program, PyUnicode_AsUTF8(key), be);
|
||||
|
||||
PyObject *mkey, *mval;
|
||||
Py_ssize_t mpos = 0;
|
||||
while (PyDict_Next(val, &mpos, &mkey, &mval)) {
|
||||
const char *decl = PyUnicode_AsUTF8(mval);
|
||||
const char *bracket = strchr(decl, '[');
|
||||
if (!bracket) continue; // scalar block members are hand-mirrored in C structs, not queried here
|
||||
char actual_name[256];
|
||||
size_t n = MIN(sizeof(actual_name) - 1, (size_t)(bracket - decl));
|
||||
memcpy(actual_name, decl, n);
|
||||
actual_name[n] = 0;
|
||||
ProgramMetadataEntry ae = {.kind = PROGRAM_ARRAY, .array = get_uniform_array_information(program, actual_name)};
|
||||
insert_program_metadata(program, PyUnicode_AsUTF8(mkey), ae);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int32_t
|
||||
program_uniform_location(int program, const char *name) {
|
||||
ProgramMetadataEntry *e = program_metadata_entry(program, name, PROGRAM_UNIFORM);
|
||||
// Setting a uniform at location -1 is a silent no-op, so a uniform the driver
|
||||
// does not report as active leaves whatever value it was last set to,
|
||||
// rendering incorrectly with no other indication that anything is wrong.
|
||||
if (e->location < 0 && !e->missing_location_logged) {
|
||||
e->missing_location_logged = true;
|
||||
log_error("The uniform: %s is not active in the linked GPU program: %d. Setting it will have no effect and rendering may be incorrect.", name, program);
|
||||
}
|
||||
return e->location;
|
||||
}
|
||||
|
||||
int32_t
|
||||
try_program_uniform_location(int program, const char *name) {
|
||||
program_metadata_map_itr itr = vt_get(program_layouts + program, name);
|
||||
if (vt_is_end(itr) || itr.data->val.kind != PROGRAM_UNIFORM) return -1;
|
||||
return itr.data->val.location;
|
||||
}
|
||||
|
||||
int32_t
|
||||
program_attribute_location(int program, const char *name) {
|
||||
return program_metadata_entry(program, name, PROGRAM_ATTRIBUTE)->location;
|
||||
}
|
||||
|
||||
UniformBlock
|
||||
program_uniform_block(int program, const char *name) {
|
||||
return program_metadata_entry(program, name, PROGRAM_BLOCK)->block;
|
||||
}
|
||||
|
||||
ArrayInformation
|
||||
program_uniform_array(int program, const char *name) {
|
||||
return program_metadata_entry(program, name, PROGRAM_ARRAY)->array;
|
||||
}
|
||||
// }}}
|
||||
|
||||
void
|
||||
bind_program_uniform_block(int program, const char *name, uint32_t binding_point) {
|
||||
UniformBlock b = program_uniform_block(program, name);
|
||||
gpu_pipeline_bind_uniform_block(programs[program], (uint32_t)b.index, binding_point);
|
||||
}
|
||||
|
||||
void
|
||||
bind_program(int program) {
|
||||
gpu_use_pipeline(programs[program]);
|
||||
}
|
||||
|
||||
void
|
||||
unbind_program(void) {
|
||||
gpu_use_pipeline(GPU_NULL_HANDLE);
|
||||
}
|
||||
// }}}
|
||||
|
||||
// Vertex Array Objects (VAO) {{{
|
||||
typedef struct {
|
||||
gpu_vertex_layout_t layout;
|
||||
size_t num_buffers;
|
||||
gpu_buffer_t buffers[10];
|
||||
} VAO;
|
||||
|
||||
static VAO vaos[4 * MAX_CHILDREN + 10] = {{0}};
|
||||
|
||||
ssize_t
|
||||
create_vao(void) {
|
||||
for (size_t i = 0; i < arraysz(vaos); i++) {
|
||||
if (!vaos[i].layout) {
|
||||
gpu_vertex_layout_t layout = gpu_vertex_layout_create();
|
||||
if (!layout) fatal("Failed to create a vertex layout with error: %s", gpu_last_error());
|
||||
vaos[i].layout = layout;
|
||||
vaos[i].num_buffers = 0;
|
||||
gpu_bind_vertex_layout(layout);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
fatal("Too many VAOs");
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t
|
||||
add_buffer_to_vao(ssize_t vao_idx, GPUBufferKind kind) {
|
||||
VAO *vao = vaos + vao_idx;
|
||||
if (vao->num_buffers >= arraysz(vao->buffers)) fatal("Too many buffers in a single VAO");
|
||||
gpu_buffer_t buf = gpu_buffer_create(kind);
|
||||
if (!buf) fatal("Failed to create a GPU buffer with error: %s", gpu_last_error());
|
||||
vao->buffers[vao->num_buffers++] = buf;
|
||||
return vao->num_buffers - 1;
|
||||
}
|
||||
|
||||
void
|
||||
add_attribute_to_vao(ssize_t vao_idx, int location, uint32_t components, GPUAttribType type, uint32_t stride, size_t offset, uint32_t divisor) {
|
||||
VAO *vao = vaos + vao_idx;
|
||||
if (!vao->num_buffers) fatal("You must create a buffer for this attribute first");
|
||||
set_vao_attribute(vao_idx, vao->num_buffers - 1, location, components, type, stride, offset, divisor);
|
||||
}
|
||||
|
||||
void
|
||||
set_vao_attribute(ssize_t vao_idx, size_t buffer_idx, int location, uint32_t components, GPUAttribType type, uint32_t stride, size_t offset, uint32_t divisor) {
|
||||
VAO *vao = vaos + vao_idx;
|
||||
gpu_vertex_layout_set_attribute(vao->layout, (uint32_t)location, vao->buffers[buffer_idx], components, type, stride, offset, divisor);
|
||||
}
|
||||
|
||||
void
|
||||
remove_vao(ssize_t vao_idx) {
|
||||
VAO *vao = vaos + vao_idx;
|
||||
const bool loaded = gpu_driver.driver != NULL; // see free_texture()
|
||||
while (vao->num_buffers) {
|
||||
vao->num_buffers--;
|
||||
if (loaded) gpu_buffer_destroy(vao->buffers[vao->num_buffers]);
|
||||
vao->buffers[vao->num_buffers] = GPU_NULL_HANDLE;
|
||||
}
|
||||
if (loaded) gpu_vertex_layout_destroy(vao->layout);
|
||||
vao->layout = GPU_NULL_HANDLE;
|
||||
}
|
||||
|
||||
void
|
||||
bind_vertex_array(ssize_t vao_idx) {
|
||||
gpu_bind_vertex_layout(vaos[vao_idx].layout);
|
||||
}
|
||||
|
||||
void
|
||||
unbind_vertex_array(void) {
|
||||
gpu_bind_vertex_layout(GPU_NULL_HANDLE);
|
||||
}
|
||||
|
||||
void
|
||||
alloc_vao_buffer(ssize_t vao_idx, size_t size, size_t bufnum, GPUUpdateFrequency frequency) {
|
||||
gpu_buffer_allocate(vaos[vao_idx].buffers[bufnum], size, frequency);
|
||||
}
|
||||
|
||||
void *
|
||||
map_vao_buffer_for_write_only(ssize_t vao_idx, size_t bufnum, size_t offset, size_t size) {
|
||||
return gpu_buffer_map(vaos[vao_idx].buffers[bufnum], GPU_MAP_WRITE_DISCARD_RANGE, offset, size);
|
||||
}
|
||||
|
||||
void *
|
||||
alloc_and_map_vao_buffer(ssize_t vao_idx, size_t size, size_t bufnum) {
|
||||
alloc_vao_buffer(vao_idx, size, bufnum, GPU_UPDATE_RARELY);
|
||||
return gpu_buffer_map(vaos[vao_idx].buffers[bufnum], GPU_MAP_WRITE, 0, size);
|
||||
}
|
||||
|
||||
void *
|
||||
alloc_and_map_vao_buffer_for_full_rewrite(ssize_t vao_idx, size_t size, size_t bufnum) {
|
||||
// The previous contents are discarded so the driver can hand out fresh
|
||||
// storage instead of waiting for the GPU to finish reading the old data.
|
||||
// The caller MUST write every byte of the mapped range, any byte not
|
||||
// written is undefined.
|
||||
alloc_vao_buffer(vao_idx, size, bufnum, GPU_UPDATE_EVERY_USE);
|
||||
return gpu_buffer_map(vaos[vao_idx].buffers[bufnum], GPU_MAP_WRITE_DISCARD_ALL, 0, size);
|
||||
}
|
||||
|
||||
const void *
|
||||
map_vao_buffer_for_reading(ssize_t vao_idx, size_t bufnum) {
|
||||
return gpu_buffer_map(vaos[vao_idx].buffers[bufnum], GPU_MAP_READ, 0, 0);
|
||||
}
|
||||
|
||||
void
|
||||
unmap_vao_buffer(ssize_t vao_idx, size_t bufnum) {
|
||||
gpu_buffer_unmap(vaos[vao_idx].buffers[bufnum]);
|
||||
}
|
||||
|
||||
void
|
||||
bind_vao_uniform_buffer(ssize_t vao_idx, size_t bufnum, uint32_t binding_point) {
|
||||
gpu_bind_uniform_buffer(binding_point, vaos[vao_idx].buffers[bufnum]);
|
||||
}
|
||||
|
||||
void
|
||||
copy_vao_buffer_region(ssize_t vao_idx, size_t src_bufnum, size_t src_off, size_t dst_bufnum, size_t dst_off, size_t size) {
|
||||
gpu_buffer_copy(vaos[vao_idx].buffers[src_bufnum], src_off, vaos[vao_idx].buffers[dst_bufnum], dst_off, size);
|
||||
}
|
||||
// }}}
|
||||
383
kitty/gpu.h
Normal file
383
kitty/gpu.h
Normal file
|
|
@ -0,0 +1,383 @@
|
|||
/*
|
||||
* gpu.h
|
||||
* Copyright (C) 2026 Kovid Goyal <kovid at kovidgoyal.net>
|
||||
*
|
||||
* Distributed under terms of the GPL3 license.
|
||||
*/
|
||||
|
||||
// Loading of the GPU driver and thin, type safe wrappers around the driver
|
||||
// API defined in gpu/api.h, plus the higher level helpers used by the rendering
|
||||
// code in shaders.c.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "data-types.h"
|
||||
#include "../gpu/api.h"
|
||||
|
||||
typedef struct GPUDriver {
|
||||
GPUDriverAPI api;
|
||||
void *driver;
|
||||
void *dl_handle;
|
||||
bool initialized;
|
||||
} GPUDriver;
|
||||
|
||||
extern GPUDriver gpu_driver;
|
||||
|
||||
// Load the GPU driver shared library at path. Returns NULL on success or an error message.
|
||||
const char *load_gpu_driver(const char *path, bool debug_rendering, gpu_get_proc_address_func get_proc_address);
|
||||
// Must be called once a context is current, before any other GPU functions are used
|
||||
void initialize_gpu_driver(void);
|
||||
void unload_gpu_driver(void);
|
||||
|
||||
// Driver API wrappers {{{
|
||||
#define D gpu_driver.driver
|
||||
#define A gpu_driver.api
|
||||
|
||||
static inline const char *
|
||||
gpu_last_error(void) {
|
||||
return A.last_error(D);
|
||||
}
|
||||
|
||||
static inline const char *
|
||||
gpu_version_string(void) {
|
||||
return gpu_driver.initialized ? A.version_string(D) : "";
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_get_capabilities(GPUCapabilities *ans) {
|
||||
A.get_capabilities(D, ans);
|
||||
}
|
||||
|
||||
static inline gpu_texture_t
|
||||
gpu_texture_create(const GPUTextureDesc *desc, const GPUPixelData *initial) {
|
||||
return A.texture_create(D, desc, initial);
|
||||
}
|
||||
|
||||
static inline bool
|
||||
gpu_texture_reset(gpu_texture_t texture, const GPUTextureDesc *desc, const GPUPixelData *initial) {
|
||||
return A.texture_reset(D, texture, desc, initial);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_texture_destroy(gpu_texture_t texture) {
|
||||
A.texture_destroy(D, texture);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_texture_upload(gpu_texture_t texture, uint32_t x, uint32_t y, uint32_t layer, uint32_t width, uint32_t height, const GPUPixelData *pixels) {
|
||||
A.texture_upload(D, texture, x, y, layer, width, height, pixels);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_texture_copy(gpu_texture_t src, gpu_texture_t dst) {
|
||||
A.texture_copy(D, src, dst);
|
||||
}
|
||||
|
||||
static inline bool
|
||||
gpu_texture_read(gpu_texture_t texture, GPUPixelLayout layout, void *dst, size_t dst_size) {
|
||||
return A.texture_read(D, texture, layout, dst, dst_size);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_texture_get_desc(gpu_texture_t texture, GPUTextureDesc *ans) {
|
||||
A.texture_get_desc(D, texture, ans);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_bind_texture(uint32_t unit, gpu_texture_t texture) {
|
||||
A.bind_texture(D, unit, texture);
|
||||
}
|
||||
|
||||
static inline gpu_buffer_t
|
||||
gpu_buffer_create(GPUBufferKind kind) {
|
||||
return A.buffer_create(D, kind);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_buffer_destroy(gpu_buffer_t buffer) {
|
||||
A.buffer_destroy(D, buffer);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_buffer_allocate(gpu_buffer_t buffer, size_t size, GPUUpdateFrequency frequency) {
|
||||
A.buffer_allocate(D, buffer, size, frequency);
|
||||
}
|
||||
|
||||
static inline void *
|
||||
gpu_buffer_map(gpu_buffer_t buffer, GPUMapMode mode, size_t offset, size_t size) {
|
||||
return A.buffer_map(D, buffer, mode, offset, size);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_buffer_unmap(gpu_buffer_t buffer) {
|
||||
A.buffer_unmap(D, buffer);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_buffer_copy(gpu_buffer_t src, size_t src_offset, gpu_buffer_t dst, size_t dst_offset, size_t size) {
|
||||
A.buffer_copy(D, src, src_offset, dst, dst_offset, size);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_bind_uniform_buffer(uint32_t binding_point, gpu_buffer_t buffer) {
|
||||
A.bind_uniform_buffer(D, binding_point, buffer);
|
||||
}
|
||||
|
||||
static inline gpu_pipeline_t
|
||||
gpu_pipeline_create(const GPUShaderSource *sources, size_t num_sources) {
|
||||
return A.pipeline_create(D, sources, num_sources);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_pipeline_destroy(gpu_pipeline_t pipeline) {
|
||||
A.pipeline_destroy(D, pipeline);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_use_pipeline(gpu_pipeline_t pipeline) {
|
||||
A.use_pipeline(D, pipeline);
|
||||
}
|
||||
|
||||
static inline int32_t
|
||||
gpu_pipeline_uniform_slot(gpu_pipeline_t pipeline, const char *name) {
|
||||
return A.pipeline_uniform_slot(D, pipeline, name);
|
||||
}
|
||||
|
||||
static inline bool
|
||||
gpu_pipeline_uniform_block(gpu_pipeline_t pipeline, const char *name, GPUUniformBlockInfo *ans) {
|
||||
return A.pipeline_uniform_block(D, pipeline, name, ans);
|
||||
}
|
||||
|
||||
static inline bool
|
||||
gpu_pipeline_uniform_array(gpu_pipeline_t pipeline, const char *name, GPUUniformArrayInfo *ans) {
|
||||
return A.pipeline_uniform_array(D, pipeline, name, ans);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_pipeline_bind_uniform_block(gpu_pipeline_t pipeline, uint32_t block_index, uint32_t binding_point) {
|
||||
A.pipeline_bind_uniform_block(D, pipeline, block_index, binding_point);
|
||||
}
|
||||
|
||||
static inline int32_t
|
||||
gpu_pipeline_attribute_location(gpu_pipeline_t pipeline, const char *name) {
|
||||
return A.pipeline_attribute_location(D, pipeline, name);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_set_uniform_floats(int32_t slot, uint32_t components, uint32_t count, const float *values) {
|
||||
A.set_uniform_floats(D, slot, components, count, values);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_set_uniform_ints(int32_t slot, uint32_t components, uint32_t count, const int32_t *values) {
|
||||
A.set_uniform_ints(D, slot, components, count, values);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_set_uniform_uints(int32_t slot, uint32_t components, uint32_t count, const uint32_t *values) {
|
||||
A.set_uniform_uints(D, slot, components, count, values);
|
||||
}
|
||||
|
||||
static inline gpu_vertex_layout_t
|
||||
gpu_vertex_layout_create(void) {
|
||||
return A.vertex_layout_create(D);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_vertex_layout_destroy(gpu_vertex_layout_t layout) {
|
||||
A.vertex_layout_destroy(D, layout);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_bind_vertex_layout(gpu_vertex_layout_t layout) {
|
||||
A.bind_vertex_layout(D, layout);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_vertex_layout_set_attribute(
|
||||
gpu_vertex_layout_t layout,
|
||||
uint32_t location,
|
||||
gpu_buffer_t buffer,
|
||||
uint32_t components,
|
||||
GPUAttribType type,
|
||||
uint32_t stride,
|
||||
size_t offset,
|
||||
uint32_t instance_divisor) {
|
||||
A.vertex_layout_set_attribute(D, layout, location, buffer, components, type, stride, offset, instance_divisor);
|
||||
}
|
||||
|
||||
static inline gpu_render_target_t
|
||||
gpu_render_target_create(gpu_texture_t color_texture) {
|
||||
return A.render_target_create(D, color_texture);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_render_target_destroy(gpu_render_target_t target) {
|
||||
A.render_target_destroy(D, target);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_set_render_target(gpu_render_target_t target) {
|
||||
A.set_render_target(D, target);
|
||||
}
|
||||
|
||||
static inline gpu_render_target_t
|
||||
gpu_get_render_target(void) {
|
||||
return A.get_render_target(D);
|
||||
}
|
||||
|
||||
static inline const char *
|
||||
gpu_render_target_status(gpu_render_target_t target) {
|
||||
return A.render_target_status(D, target);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_clear(float r, float g, float b, float a) {
|
||||
A.clear(D, r, g, b, a);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_set_viewport(int32_t x, int32_t y, uint32_t width, uint32_t height) {
|
||||
A.set_viewport(D, x, y, width, height);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_get_viewport(int32_t ans[4]) {
|
||||
A.get_viewport(D, ans);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_set_scissor(bool enabled, int32_t x, int32_t y, uint32_t width, uint32_t height) {
|
||||
A.set_scissor(D, enabled, x, y, width, height);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_set_srgb_output(bool enabled) {
|
||||
A.set_srgb_output(D, enabled);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_read_pixels(int32_t x, int32_t y, uint32_t width, uint32_t height, void *dst) {
|
||||
A.read_pixels(D, x, y, width, height, dst);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_copy_to_texture(gpu_texture_t dst, int32_t x, int32_t y, uint32_t width, uint32_t height) {
|
||||
A.copy_to_texture(D, dst, x, y, width, height);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_draw_quad(uint32_t instance_count, GPUBlendMode blend) {
|
||||
A.draw_quad(D, instance_count, blend);
|
||||
}
|
||||
#undef D
|
||||
#undef A
|
||||
// }}}
|
||||
|
||||
// Uniform convenience functions {{{
|
||||
static inline void
|
||||
gpu_uniform1f(int32_t slot, float a) {
|
||||
gpu_set_uniform_floats(slot, 1, 1, &a);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_uniform2f(int32_t slot, float a, float b) {
|
||||
const float v[2] = {a, b};
|
||||
gpu_set_uniform_floats(slot, 2, 1, v);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_uniform3f(int32_t slot, float a, float b, float c) {
|
||||
const float v[3] = {a, b, c};
|
||||
gpu_set_uniform_floats(slot, 3, 1, v);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_uniform4f(int32_t slot, float a, float b, float c, float d) {
|
||||
const float v[4] = {a, b, c, d};
|
||||
gpu_set_uniform_floats(slot, 4, 1, v);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_uniform1i(int32_t slot, int32_t a) {
|
||||
gpu_set_uniform_ints(slot, 1, 1, &a);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gpu_uniform1ui(int32_t slot, uint32_t a) {
|
||||
gpu_set_uniform_uints(slot, 1, 1, &a);
|
||||
}
|
||||
// }}}
|
||||
|
||||
// Higher level helpers {{{
|
||||
typedef struct Viewport {
|
||||
unsigned left, top, width, height;
|
||||
} Viewport;
|
||||
|
||||
void set_gpu_viewport(unsigned w, unsigned h);
|
||||
void save_viewport_using_top_left_origin(int32_t x, int32_t y, uint32_t width, uint32_t height, int32_t full_framebuffer_height);
|
||||
void save_viewport_using_bottom_left_origin(int32_t x, int32_t y, uint32_t width, uint32_t height);
|
||||
void restore_viewport(void);
|
||||
void enable_scissor_using_top_left_origin(Viewport, unsigned);
|
||||
void disable_scissor(void);
|
||||
// Draw a quad, blending pre-multiplied colors if blend is true
|
||||
void draw_quad(bool blend, unsigned instance_count);
|
||||
void free_texture(gpu_texture_t *tex);
|
||||
void free_framebuffer(gpu_render_target_t *fb);
|
||||
// The render target used for output, render target zero is redirected to
|
||||
// the target set with set_framebuffer_to_use_for_output()
|
||||
void bind_framebuffer_for_output(gpu_render_target_t fb);
|
||||
void set_framebuffer_to_use_for_output(gpu_render_target_t fb);
|
||||
const char *check_framebuffer_status(void);
|
||||
void save_texture_as_png(gpu_texture_t texture, const char *filename);
|
||||
// }}}
|
||||
|
||||
// Programs {{{
|
||||
typedef struct UniformBlock {
|
||||
int32_t size, index;
|
||||
} UniformBlock;
|
||||
|
||||
typedef struct ArrayInformation {
|
||||
int32_t offset; // byte offset from start of UBO
|
||||
int32_t stride; // element stride in bytes
|
||||
int32_t size; // number of elements
|
||||
} ArrayInformation;
|
||||
|
||||
#define MAX_GPU_PROGRAMS 64
|
||||
|
||||
gpu_pipeline_t program_pipeline(int program);
|
||||
// Replace the pipeline of program, freeing any existing pipeline. Steals the reference to pipeline.
|
||||
void set_program_pipeline(int program, gpu_pipeline_t pipeline);
|
||||
int32_t attrib_location(int program, const char *name);
|
||||
void set_program_layout(int program, PyObject *metadata);
|
||||
void free_program_layouts(void);
|
||||
int32_t program_uniform_location(int program, const char *name);
|
||||
int32_t try_program_uniform_location(int program, const char *name);
|
||||
int32_t program_attribute_location(int program, const char *name);
|
||||
UniformBlock program_uniform_block(int program, const char *name);
|
||||
ArrayInformation program_uniform_array(int program, const char *name);
|
||||
void bind_program_uniform_block(int program, const char *name, uint32_t binding_point);
|
||||
void bind_program(int program);
|
||||
void unbind_program(void);
|
||||
// }}}
|
||||
|
||||
// Vertex array objects: a vertex layout together with the buffers it uses {{{
|
||||
ssize_t create_vao(void);
|
||||
size_t add_buffer_to_vao(ssize_t vao_idx, GPUBufferKind kind);
|
||||
// Add an attribute reading from the most recently added buffer
|
||||
void add_attribute_to_vao(ssize_t vao_idx, int location, uint32_t components, GPUAttribType type, uint32_t stride, size_t offset, uint32_t divisor);
|
||||
void
|
||||
set_vao_attribute(ssize_t vao_idx, size_t buffer_idx, int location, uint32_t components, GPUAttribType type, uint32_t stride, size_t offset, uint32_t divisor);
|
||||
void remove_vao(ssize_t vao_idx);
|
||||
void bind_vertex_array(ssize_t vao_idx);
|
||||
void unbind_vertex_array(void);
|
||||
void alloc_vao_buffer(ssize_t vao_idx, size_t size, size_t bufnum, GPUUpdateFrequency frequency);
|
||||
void *alloc_and_map_vao_buffer(ssize_t vao_idx, size_t size, size_t bufnum);
|
||||
// Discards the old contents, the caller must write every byte of the buffer
|
||||
void *alloc_and_map_vao_buffer_for_full_rewrite(ssize_t vao_idx, size_t size, size_t bufnum);
|
||||
void *map_vao_buffer_for_write_only(ssize_t vao_idx, size_t bufnum, size_t offset, size_t size);
|
||||
const void *map_vao_buffer_for_reading(ssize_t vao_idx, size_t bufnum);
|
||||
void unmap_vao_buffer(ssize_t vao_idx, size_t bufnum);
|
||||
void bind_vao_uniform_buffer(ssize_t vao_idx, size_t bufnum, uint32_t binding_point);
|
||||
void copy_vao_buffer_region(ssize_t vao_idx, size_t src_bufnum, size_t src_off, size_t dst_bufnum, size_t dst_off, size_t size);
|
||||
// }}}
|
||||
|
|
@ -8,6 +8,7 @@
|
|||
#define GRAPHICS_INTERNAL_APIS
|
||||
#include "graphics.h"
|
||||
#include "state.h"
|
||||
#include "gpu.h"
|
||||
#include "disk-cache.h"
|
||||
#include "iqsort.h"
|
||||
#include "safe-wrappers.h"
|
||||
|
|
@ -161,7 +162,7 @@ new_texture_ref(void) {
|
|||
return ans;
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
static gpu_texture_t
|
||||
texture_id_for_img(Image *img) {
|
||||
return img->texture ? img->texture->id : 0;
|
||||
}
|
||||
|
|
@ -2972,12 +2973,12 @@ image_as_dict(GraphicsManager *self, Image *img) {
|
|||
return NULL;
|
||||
}
|
||||
PyObject *ans = Py_BuildValue(
|
||||
"{sI sI sI sI sI sI sI "
|
||||
"{sK sI sI sI sI sI sI "
|
||||
"sO sI sO "
|
||||
"sI sI sI "
|
||||
"sI sI sy# sN sI}",
|
||||
"texture_id",
|
||||
texture_id_for_img(img),
|
||||
(unsigned long long)texture_id_for_img(img),
|
||||
U(client_id),
|
||||
U(width),
|
||||
U(height),
|
||||
|
|
|
|||
|
|
@ -50,7 +50,8 @@ typedef struct {
|
|||
|
||||
typedef struct {
|
||||
ImageRect src_rect, dest_rect;
|
||||
uint32_t texture_id, group_count;
|
||||
gpu_texture_t texture_id;
|
||||
uint32_t group_count;
|
||||
int z_index;
|
||||
// Zero for normal images, otherwise the number of bytes per pixel of an indexed color image
|
||||
uint8_t indexed_px_sz;
|
||||
|
|
@ -61,7 +62,7 @@ typedef struct {
|
|||
} ImageRenderData;
|
||||
|
||||
typedef struct {
|
||||
uint32_t texture_id;
|
||||
gpu_texture_t texture_id;
|
||||
unsigned int height, width;
|
||||
uint8_t *bitmap;
|
||||
uint32_t refcnt, id;
|
||||
|
|
@ -103,7 +104,8 @@ typedef struct {
|
|||
typedef enum { ANIMATION_STOPPED = 0, ANIMATION_LOADING = 1, ANIMATION_RUNNING = 2 } AnimationState;
|
||||
|
||||
typedef struct TextureRef {
|
||||
uint32_t id, refcnt;
|
||||
gpu_texture_t id;
|
||||
uint32_t refcnt;
|
||||
} TextureRef;
|
||||
|
||||
#define NAME ref_map
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ from .constants import (
|
|||
beam_cursor_data_file,
|
||||
clear_handled_signals,
|
||||
glfw_path,
|
||||
gpu_driver_path,
|
||||
is_macos,
|
||||
is_quick_access_terminal_app,
|
||||
is_wayland,
|
||||
|
|
@ -105,7 +106,7 @@ def load_all_shaders() -> None:
|
|||
|
||||
|
||||
def init_glfw_module(glfw_module: str = 'wayland', debug_keyboard: bool = False, debug_rendering: bool = False, wayland_enable_ime: bool = True) -> None:
|
||||
ok, swo = glfw_init(glfw_path(glfw_module), edge_spacing, debug_keyboard, debug_rendering, wayland_enable_ime)
|
||||
ok, swo = glfw_init(glfw_path(glfw_module), gpu_driver_path(), edge_spacing, debug_keyboard, debug_rendering, wayland_enable_ime)
|
||||
if not ok:
|
||||
raise SystemExit('GLFW initialization failed')
|
||||
supports_window_occlusion(swo)
|
||||
|
|
|
|||
1129
kitty/shaders.c
1129
kitty/shaders.c
File diff suppressed because it is too large
Load diff
2
kitty/srgb_gamma.h
generated
2
kitty/srgb_gamma.h
generated
|
|
@ -1,6 +1,6 @@
|
|||
// Generated by gen-srgb-lut.py DO NOT edit
|
||||
|
||||
static const GLfloat srgb_lut[256] = {
|
||||
static const float srgb_lut[256] = {
|
||||
0.00000000f, 0.00030353f, 0.00060705f, 0.00091058f, 0.00121411f, 0.00151763f, 0.00182116f, 0.00212469f, 0.00242822f, 0.00273174f, 0.00303527f, 0.00334654f, 0.00367651f, 0.00402472f, 0.00439144f, 0.00477695f,
|
||||
0.00518152f, 0.00560539f, 0.00604883f, 0.00651209f, 0.00699541f, 0.00749903f, 0.00802319f, 0.00856813f, 0.00913406f, 0.00972122f, 0.01032982f, 0.01096009f, 0.01161225f, 0.01228649f, 0.01298303f, 0.01370208f,
|
||||
0.01444384f, 0.01520851f, 0.01599629f, 0.01680738f, 0.01764195f, 0.01850022f, 0.01938236f, 0.02028856f, 0.02121901f, 0.02217388f, 0.02315337f, 0.02415763f, 0.02518686f, 0.02624122f, 0.02732089f, 0.02842604f,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
#include "cleanup.h"
|
||||
#include "gpu.h"
|
||||
#include "dnd.h"
|
||||
#include "options/to-c-generated.h"
|
||||
#include <math.h>
|
||||
|
|
|
|||
|
|
@ -597,12 +597,18 @@ typedef struct OSWindow {
|
|||
bool has_layout;
|
||||
} background_image;
|
||||
struct {
|
||||
uint32_t framebuffer_id, attached_texture_generation;
|
||||
uint32_t extra_fbo_id, extra_fbo_generation;
|
||||
uint32_t fbo_a_id, fbo_a_generation;
|
||||
uint32_t fbo_b_id, fbo_b_generation;
|
||||
gpu_render_target_t framebuffer_id;
|
||||
uint32_t attached_texture_generation;
|
||||
gpu_render_target_t extra_fbo_id;
|
||||
uint32_t extra_fbo_generation;
|
||||
gpu_render_target_t fbo_a_id;
|
||||
uint32_t fbo_a_generation;
|
||||
gpu_render_target_t fbo_b_id;
|
||||
uint32_t fbo_b_generation;
|
||||
} indirect_output;
|
||||
uint32_t persist_texture_id, persist_fbo_id, persist_texture_generation;
|
||||
gpu_texture_t persist_texture_id;
|
||||
gpu_render_target_t persist_fbo_id;
|
||||
uint32_t persist_texture_generation;
|
||||
unsigned int active_tab, num_tabs, capacity, last_active_tab, last_num_tabs, last_active_window_id;
|
||||
bool focused_at_last_render, needs_render, needs_layers, input_held;
|
||||
unsigned keep_rendering_till_swap;
|
||||
|
|
@ -714,7 +720,6 @@ typedef struct GlobalState {
|
|||
// Set once an IME composition has been seen, see sync_ime_position_with_cursor()
|
||||
bool ime_used;
|
||||
WindowLogoTable *all_window_logos;
|
||||
int gl_version;
|
||||
bool supports_framebuffer_srgb;
|
||||
PyObject *options_object;
|
||||
|
||||
|
|
@ -753,11 +758,16 @@ typedef struct GlobalState {
|
|||
double x, y;
|
||||
} window_being_dragged;
|
||||
struct {
|
||||
uint32_t texture_id, framebuffer_id, texture_generation;
|
||||
gpu_texture_t texture_id;
|
||||
gpu_render_target_t framebuffer_id;
|
||||
uint32_t texture_generation;
|
||||
int width, height;
|
||||
uint32_t extra_texture_id, extra_texture_setup_fbo_id;
|
||||
uint32_t texture_a_id, texture_a_fbo_id;
|
||||
uint32_t texture_b_id, texture_b_fbo_id;
|
||||
gpu_texture_t extra_texture_id;
|
||||
gpu_render_target_t extra_texture_setup_fbo_id;
|
||||
gpu_texture_t texture_a_id;
|
||||
gpu_render_target_t texture_a_fbo_id;
|
||||
gpu_texture_t texture_b_id;
|
||||
gpu_render_target_t texture_b_fbo_id;
|
||||
} layers_render_texture;
|
||||
} GlobalState;
|
||||
|
||||
|
|
@ -819,11 +829,8 @@ void upload_gamma_lut(void);
|
|||
bool send_cell_data_to_gpu(ssize_t, Screen *, OSWindow *);
|
||||
void draw_cells(const WindowRenderData *, OSWindow *, bool, bool, bool, Window *, const Window *, monotonic_t);
|
||||
bool update_cursor_trail(CursorTrail *ct, Window *w, monotonic_t now, OSWindow *os_window);
|
||||
void set_gpu_viewport(unsigned w, unsigned h);
|
||||
void free_texture(uint32_t *);
|
||||
void free_framebuffer(uint32_t *);
|
||||
void send_image_to_gpu(uint32_t *, const void *, int32_t, int32_t, bool, bool, bool, RepeatStrategy);
|
||||
void send_indexed_image_to_gpu(uint32_t *, const void *, int32_t, int32_t, unsigned, bool);
|
||||
void send_image_to_gpu(gpu_texture_t *, const void *, int32_t, int32_t, bool, bool, bool, RepeatStrategy);
|
||||
void send_indexed_image_to_gpu(gpu_texture_t *, const void *, int32_t, int32_t, unsigned, bool);
|
||||
void send_sprite_to_gpu(FONTS_DATA_HANDLE fg, sprite_index, pixel *, sprite_index);
|
||||
void blank_canvas(float, color_type, bool);
|
||||
void blank_os_window(OSWindow *);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
|
||||
#include "state.h"
|
||||
#include "gpu.h"
|
||||
#include "window_logo.h"
|
||||
#include <sys/mman.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -9,13 +9,14 @@
|
|||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "../gpu/api.h"
|
||||
|
||||
typedef unsigned int window_logo_id_t;
|
||||
|
||||
typedef struct WindowLogo {
|
||||
unsigned int height, width;
|
||||
bool load_from_disk_ok;
|
||||
uint32_t texture_id;
|
||||
gpu_texture_t texture_id;
|
||||
uint8_t *bitmap;
|
||||
size_t mmap_size;
|
||||
} WindowLogo;
|
||||
|
|
|
|||
32
setup.py
32
setup.py
|
|
@ -809,10 +809,9 @@ def kitty_env(args: Options) -> Env:
|
|||
cflags.extend(pkg_config('harfbuzz', '--cflags-only-I'))
|
||||
platform_libs.extend(pkg_config('harfbuzz', '--libs'))
|
||||
pylib = get_python_flags(args, cflags)
|
||||
gl_libs = ['-framework', 'OpenGL'] if is_macos else pkg_config('gl', '--libs')
|
||||
libpng = pkg_config('libpng', '--libs')
|
||||
lcms2 = pkg_config('lcms2', '--libs')
|
||||
ans.ldpaths += pylib + platform_libs + gl_libs + libpng + lcms2 + libcrypto_ldflags + xxhash[1]
|
||||
ans.ldpaths += pylib + platform_libs + libpng + lcms2 + libcrypto_ldflags + xxhash[1]
|
||||
if is_macos:
|
||||
ans.ldpaths.extend('-framework Cocoa'.split())
|
||||
elif not is_openbsd:
|
||||
|
|
@ -1189,6 +1188,7 @@ def find_c_files() -> Tuple[List[str], List[str]]:
|
|||
ans.append(os.path.join('kitty', x))
|
||||
elif ext == '.h':
|
||||
headers.append(os.path.join('kitty', x))
|
||||
headers.extend(gpu_api_headers())
|
||||
ans.append('kitty/vt-parser-dump.c')
|
||||
|
||||
# ringbuf
|
||||
|
|
@ -1302,6 +1302,33 @@ def build_wayland_protocols(
|
|||
parallel_run(items)
|
||||
|
||||
|
||||
def gpu_api_headers() -> List[str]:
|
||||
return ['gpu/api.h', 'gpu/opengl/version.h']
|
||||
|
||||
|
||||
def gpu_driver_init_env(env: Env, driver: str = 'opengl') -> Env:
|
||||
ans = env.copy()
|
||||
ans.cflags.append('-fPIC')
|
||||
if driver == 'opengl':
|
||||
if is_macos:
|
||||
# Apple deprecated OpenGL in Mojave (10.14) silence the endless
|
||||
# warnings about it
|
||||
ans.cppflags.append('-DGL_SILENCE_DEPRECATION')
|
||||
ans.ldpaths.extend(('-framework', 'OpenGL'))
|
||||
# On other platforms the OpenGL functions are loaded at runtime using
|
||||
# the get_proc_address() function passed in by kitty, so there is no
|
||||
# need to link against libGL
|
||||
return ans
|
||||
|
||||
|
||||
def compile_gpu_drivers(compilation_database: CompilationDatabase, build_dsym: bool = False) -> None:
|
||||
for driver in ('opengl',):
|
||||
denv = gpu_driver_init_env(env, driver)
|
||||
sources = sorted(glob.glob(f'gpu/{driver}/*.c'))
|
||||
headers = sorted(set(glob.glob(f'gpu/{driver}/*.h') + gpu_api_headers()))
|
||||
compile_c_extension(denv, f'kitty/{driver}', compilation_database, sources, headers, desc_prefix=f'[{driver}] ', build_dsym=build_dsym)
|
||||
|
||||
|
||||
def compile_glfw(compilation_database: CompilationDatabase, build_dsym: bool = False) -> None:
|
||||
modules = 'cocoa' if is_macos else 'x11 wayland'
|
||||
for module in modules.split():
|
||||
|
|
@ -1478,6 +1505,7 @@ def build(args: Options, native_optimizations: bool = True, call_init: bool = Tr
|
|||
build_dsym=args.build_dsym,
|
||||
)
|
||||
compile_glfw(args.compilation_database, args.build_dsym)
|
||||
compile_gpu_drivers(args.compilation_database, args.build_dsym)
|
||||
compile_kittens(args)
|
||||
add_builtin_fonts(args)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue