Declare texture and uniform block binding slots in the shaders

Every texture and uniform block in kitty's shaders now declares its
binding slot with [[vk::binding(N)]], from a single documented table,
SHADER_BINDING_SLOTS, which the shader build verifies. Slots are unique
across shaders and kinds of resource, as Vulkan requires, and so that
resources bound once and used by many programs, such as the sprite
textures, are never displaced by another program's bindings.

The slots are passed to the C code in the program metadata, which no
longer hard codes texture units or uniform buffer binding points, and no
longer sets sampler uniforms or calls glUniformBlockBinding() equivalents.
Instead the driver receives the slots when a pipeline is created. The
OpenGL driver applies them to the linked program since OpenGL 3 has no
layout(binding) qualifiers; targets where the slots are part of the
compiled code can ignore them.
This commit is contained in:
Kovid Goyal 2026-11-03 10:32:26 +05:30
parent 9f776800f6
commit 6859aef001
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
15 changed files with 254 additions and 119 deletions

View file

@ -235,6 +235,22 @@ typedef struct GPUUniformBlockInfo {
// Some targets, such as GLSL for OpenGL, look up parameters by the identifier
// used in the compiled code, which can differ from the Slang name. For those
// the mapping is supplied when creating the pipeline.
// The binding slot of a texture or uniform block of a pipeline, from the
// reflection data. Textures and uniform buffers are bound to these slots with
// bind_texture() and bind_uniform_buffer(). Targets where the slots are not
// part of the compiled code, such as GLSL for OpenGL 3, apply them when the
// pipeline is created.
typedef enum {
GPU_BINDING_TEXTURE = 1,
GPU_BINDING_UNIFORM_BLOCK,
} GPUBindingKind;
typedef struct GPUBinding {
GPUBindingKind kind;
const char *name; // name of the texture, or of the type of the uniform block, in the Slang source
uint32_t slot;
} GPUBinding;
typedef struct GPUNativeName {
const char *name; // the name of a parameter, vertex input or uniform block type in the Slang source
const char *native_name; // the corresponding identifier in the compiled code
@ -298,8 +314,8 @@ typedef struct GPUDriverAPI {
// width * height pixels of the specified layout with rows aligned to 4 bytes.
bool (*texture_read)(void *driver, gpu_texture_t texture, GPUPixelLayout layout, void *dst, size_t dst_size);
void (*texture_get_desc)(void *driver, gpu_texture_t texture, GPUTextureDesc *ans);
// Bind a texture to a texture unit, for sampling by the current pipeline
void (*bind_texture)(void *driver, uint32_t unit, gpu_texture_t texture);
// Bind a texture to a slot, for sampling by pipelines, see GPUBinding
void (*bind_texture)(void *driver, uint32_t slot, gpu_texture_t texture);
// }}}
// Buffers {{{
@ -314,17 +330,24 @@ typedef struct GPUDriverAPI {
void *(*buffer_map)(void *driver, gpu_buffer_t buffer, GPUMapMode mode, size_t offset, size_t size);
void (*buffer_unmap)(void *driver, gpu_buffer_t buffer);
void (*buffer_copy)(void *driver, gpu_buffer_t src, size_t src_offset, gpu_buffer_t dst, size_t dst_offset, size_t size);
// Make a uniform buffer available to uniform blocks associated with binding_point
void (*bind_uniform_buffer)(void *driver, uint32_t binding_point, gpu_buffer_t buffer);
// Bind a uniform buffer to a slot, for use by pipelines, see GPUBinding
void (*bind_uniform_buffer)(void *driver, uint32_t slot, gpu_buffer_t buffer);
// }}}
// Pipelines {{{
// Create a pipeline from shader sources compiled for the shader target of
// the driver. native_names maps Slang names to identifiers in the compiled
// code, see GPUNativeName, and may be empty for targets that do not need
// it. On failure last_error() contains the compiler output.
// the driver. bindings are the binding slots of its textures and uniform
// blocks, see GPUBinding. native_names maps Slang names to identifiers in
// the compiled code, see GPUNativeName, and may be empty for targets that
// do not need it. On failure last_error() contains the compiler output.
gpu_pipeline_t (*pipeline_create)(
void *driver, const GPUShaderSource *sources, size_t num_sources, const GPUNativeName *native_names, size_t num_native_names);
void *driver,
const GPUShaderSource *sources,
size_t num_sources,
const GPUBinding *bindings,
size_t num_bindings,
const GPUNativeName *native_names,
size_t num_native_names);
void (*pipeline_destroy)(void *driver, gpu_pipeline_t pipeline);
// Make a pipeline current, GPU_NULL_HANDLE for none
void (*use_pipeline)(void *driver, gpu_pipeline_t pipeline);
@ -336,8 +359,6 @@ typedef struct GPUDriverAPI {
// name is the name of the type of the uniform block. The size reported
// by the driver is used to verify the layout from the reflection data.
bool (*pipeline_uniform_block)(void *driver, gpu_pipeline_t pipeline, const char *name, GPUUniformBlockInfo *ans);
// Associate a uniform block of the pipeline with a binding point, see bind_uniform_buffer()
void (*pipeline_bind_uniform_block)(void *driver, gpu_pipeline_t pipeline, uint32_t block_index, uint32_t binding_point);
// The location of the named vertex input, -1 if it is not used
int32_t (*pipeline_attribute_location)(void *driver, gpu_pipeline_t pipeline, const char *name);
// Set the value of a loose uniform of the current pipeline. components is

View file

@ -542,8 +542,8 @@ texture_get_desc(void *driver UNUSED, gpu_texture_t texture, GPUTextureDesc *ans
}
static void
bind_texture(void *driver UNUSED, uint32_t unit, gpu_texture_t texture) {
glActiveTexture(GL_TEXTURE0 + unit);
bind_texture(void *driver UNUSED, uint32_t slot, gpu_texture_t texture) {
glActiveTexture(GL_TEXTURE0 + slot);
const Texture *t = AS_TEXTURE(texture);
if (t) glBindTexture(t->target, t->id);
else glBindTexture(GL_TEXTURE_2D, 0);
@ -622,8 +622,8 @@ buffer_copy(void *driver UNUSED, gpu_buffer_t src, size_t src_offset, gpu_buffer
}
static void
bind_uniform_buffer(void *driver UNUSED, uint32_t binding_point, gpu_buffer_t buffer) {
glBindBufferBase(GL_UNIFORM_BUFFER, binding_point, AS_BUFFER(buffer)->id);
bind_uniform_buffer(void *driver UNUSED, uint32_t slot, gpu_buffer_t buffer) {
glBindBufferBase(GL_UNIFORM_BUFFER, slot, AS_BUFFER(buffer)->id);
}
// }}}
@ -726,8 +726,40 @@ native_name(const Pipeline *p, const char *name) {
return name;
}
// OpenGL 3 has no layout(binding) qualifiers, so the binding slots are applied
// to the linked program
static void
apply_bindings(const Pipeline *p, const GPUBinding *bindings, size_t num_bindings) {
GLint previous_program = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, &previous_program);
glUseProgram(p->id);
for (size_t i = 0; i < num_bindings; i++) {
const GPUBinding *b = bindings + i;
const char *name = native_name(p, b->name);
switch (b->kind) {
case GPU_BINDING_TEXTURE: {
// Unused samplers are optimized away by the GLSL compiler
GLint loc = glGetUniformLocation(p->id, name);
if (loc >= 0) glUniform1i(loc, (GLint)b->slot);
} break;
case GPU_BINDING_UNIFORM_BLOCK: {
GLuint index = glGetUniformBlockIndex(p->id, name);
if (index != GL_INVALID_INDEX) glUniformBlockBinding(p->id, index, b->slot);
} break;
}
}
glUseProgram((GLuint)previous_program);
}
static gpu_pipeline_t
pipeline_create(void *driver, const GPUShaderSource *sources, size_t num_sources, const GPUNativeName *native_names, size_t num_native_names) {
pipeline_create(
void *driver,
const GPUShaderSource *sources,
size_t num_sources,
const GPUBinding *bindings,
size_t num_bindings,
const GPUNativeName *native_names,
size_t num_native_names) {
Driver *d = driver;
Pipeline *p = calloc(1, sizeof(Pipeline));
if (!p) {
@ -763,6 +795,7 @@ pipeline_create(void *driver, const GPUShaderSource *sources, size_t num_sources
pipeline_destroy(d, AS_HANDLE(p));
return GPU_NULL_HANDLE;
}
apply_bindings(p, bindings, num_bindings);
return AS_HANDLE(p);
}
@ -800,11 +833,6 @@ pipeline_uniform_block(void *driver, gpu_pipeline_t pipeline, const char *name,
return true;
}
static void
pipeline_bind_uniform_block(void *driver UNUSED, gpu_pipeline_t pipeline, uint32_t block_index, uint32_t binding_point) {
glUniformBlockBinding(AS_PIPELINE(pipeline)->id, block_index, binding_point);
}
static int32_t
pipeline_attribute_location(void *driver UNUSED, gpu_pipeline_t pipeline, const char *name) {
const Pipeline *p = AS_PIPELINE(pipeline);
@ -1270,7 +1298,6 @@ kitty_gpu_driver_load(uint32_t api_version, const GPUDriverInitParams *params, G
F(use_pipeline),
F(pipeline_uniform_slot),
F(pipeline_uniform_block),
F(pipeline_bind_uniform_block),
F(pipeline_attribute_location),
F(set_uniform_floats),
F(set_uniform_ints),

View file

@ -232,7 +232,7 @@ save_texture_as_png(gpu_texture_t texture, const char *filename) {
// }}}
// Programs {{{
typedef enum { PROGRAM_UNIFORM, PROGRAM_ATTRIBUTE, PROGRAM_BLOCK, PROGRAM_ARRAY } ProgramMetadataKind;
typedef enum { PROGRAM_UNIFORM, PROGRAM_ATTRIBUTE, PROGRAM_BLOCK, PROGRAM_ARRAY, PROGRAM_TEXTURE } ProgramMetadataKind;
typedef struct ProgramMetadataEntry {
ProgramMetadataKind kind;
bool missing_location_logged;
@ -355,14 +355,23 @@ set_program_layout(int program, PyObject *metadata) {
insert_program_metadata(program, name, e);
}
PyObject *textures = metadata_item(metadata, "textures", &PyDict_Type);
if (!textures) return false;
pos = 0;
while (PyDict_Next(textures, &pos, &key, &val)) {
ProgramMetadataEntry e = {.kind = PROGRAM_TEXTURE, .location = (int32_t)PyLong_AsLong(val)};
if (PyErr_Occurred()) return false;
insert_program_metadata(program, PyUnicode_AsUTF8(key), e);
}
PyObject *blocks = metadata_item(metadata, "blocks", &PyDict_Type);
if (!blocks) return false;
pos = 0;
while (PyDict_Next(blocks, &pos, &key, &val)) {
const char *block_name = PyUnicode_AsUTF8(key);
if (!block_name) return false;
int32_t size;
if (!metadata_uint(val, "size", &size)) return false;
int32_t size, slot;
if (!metadata_uint(val, "size", &size) || !metadata_uint(val, "slot", &slot)) return false;
GPUUniformBlockInfo info;
if (!gpu_pipeline_uniform_block(pipeline, block_name, &info)) {
PyErr_Format(PyExc_ValueError, "The GPU program: %d has no uniform block: %s with error: %s", program, block_name, gpu_last_error());
@ -380,7 +389,7 @@ set_program_layout(int program, PyObject *metadata) {
size);
return false;
}
ProgramMetadataEntry be = {.kind = PROGRAM_BLOCK, .block = {.index = (int32_t)info.index, .size = size}};
ProgramMetadataEntry be = {.kind = PROGRAM_BLOCK, .block = {.index = (int32_t)info.index, .size = size, .slot = (uint32_t)slot}};
insert_program_metadata(program, block_name, be);
PyObject *arrays = metadata_item(val, "arrays", &PyDict_Type);
@ -436,10 +445,18 @@ program_uniform_array(int program, const char *name) {
}
// }}}
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);
uint32_t
program_texture_slot(int program, const char *name) {
return (uint32_t)program_metadata_entry(program, name, PROGRAM_TEXTURE)->location;
}
bool
try_program_texture_slot(int program, const char *name, uint32_t *slot) {
if (!program_layouts_initialized[program]) return false;
program_metadata_map_itr itr = vt_get(program_layouts + program, name);
if (vt_is_end(itr) || itr.data->val.kind != PROGRAM_TEXTURE) return false;
*slot = (uint32_t)itr.data->val.location;
return true;
}
void

View file

@ -190,8 +190,14 @@ gpu_bind_uniform_buffer(uint32_t binding_point, gpu_buffer_t buffer) {
}
static inline gpu_pipeline_t
gpu_pipeline_create(const GPUShaderSource *sources, size_t num_sources, const GPUNativeName *native_names, size_t num_native_names) {
return A.pipeline_create(D, sources, num_sources, native_names, num_native_names);
gpu_pipeline_create(
const GPUShaderSource *sources,
size_t num_sources,
const GPUBinding *bindings,
size_t num_bindings,
const GPUNativeName *native_names,
size_t num_native_names) {
return A.pipeline_create(D, sources, num_sources, bindings, num_bindings, native_names, num_native_names);
}
static inline void
@ -214,11 +220,6 @@ gpu_pipeline_uniform_block(gpu_pipeline_t pipeline, const char *name, GPUUniform
return A.pipeline_uniform_block(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);
@ -396,6 +397,7 @@ void save_texture_as_png(gpu_texture_t texture, const char *filename);
// Programs {{{
typedef struct UniformBlock {
int32_t size, index;
uint32_t slot; // the binding slot, see bind_uniform_buffer()
} UniformBlock;
typedef struct ArrayInformation {
@ -418,7 +420,10 @@ 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);
// The binding slot of the named texture of the program, see bind_texture()
uint32_t program_texture_slot(int program, const char *name);
// As above, returns false if the program has no such texture
bool try_program_texture_slot(int program, const char *name, uint32_t *slot);
void bind_program(int program);
void unbind_program(void);
// }}}

View file

@ -43,7 +43,8 @@ enum {
NUM_PROGRAMS
};
static_assert(NUM_PROGRAMS <= MAX_GPU_PROGRAMS, "Too many GPU programs");
enum { SPRITE_MAP_UNIT, GRAPHICS_UNIT, SPRITE_DECORATIONS_MAP_UNIT, CUSTOM_END_TEXTURE_A_UNIT, CUSTOM_END_TEXTURE_B_UNIT, CUSTOM_END_TEXTURE_PERSIST_UNIT };
// The binding slots of textures and uniform blocks come from the program
// metadata, see SHADER_BINDING_SLOTS in shaders/slang.py
typedef struct UIRenderData {
unsigned screen_width, screen_height, cell_width, cell_height, screen_left, screen_top, full_framebuffer_width, full_framebuffer_height;
@ -228,8 +229,13 @@ realloc_sprite_texture(FONTS_DATA_HANDLE fg) {
static void
bind_sprite_map(const SpriteMap *sprite_map) {
gpu_bind_texture(SPRITE_DECORATIONS_MAP_UNIT, sprite_map->decorations_map.texture_id);
gpu_bind_texture(SPRITE_MAP_UNIT, sprite_map->texture_id);
// The sprite textures are bound once and used by all the cell programs,
// which share the same layout. They have binding slots distinct from those
// of every other program, so drawing images in between does not displace them.
uint32_t slot;
// The cell programs are compiled only once the first OS window is created
if (try_program_texture_slot(CELL_PROGRAM, "sprite_decorations_map", &slot)) gpu_bind_texture(slot, sprite_map->decorations_map.texture_id);
if (try_program_texture_slot(CELL_PROGRAM, "sprites", &slot)) gpu_bind_texture(slot, sprite_map->texture_id);
}
static void
@ -354,7 +360,6 @@ draw_rounded_rect(
// Cell {{{
enum { CELL_RENDER_DATA_BINDING_POINT = 0, COLOR_TABLE_BINDING_POINT = 1, BORDER_COLORS_BINDING_POINT = 2, CUSTOM_END_DATA_BINDING_POINT = 3 };
enum { BORDER_COLORS_GLOBAL_BUFFER };
// VAOs used only to hold buffers for UBOs that are shared amongst programs/windows,
// their vertex attribute/array facilities are unused.
@ -446,13 +451,6 @@ upload_gamma_lut(void) {
static void
init_cell_program(void) {
for (int i = CELL_PROGRAM; i < CELL_PROGRAM_SENTINEL; i++) {
UniformBlock crd = program_uniform_block(i, "CellRenderData");
gpu_pipeline_bind_uniform_block(program_pipeline(i), (uint32_t)crd.index, CELL_RENDER_DATA_BINDING_POINT);
UniformBlock ct = program_uniform_block(i, "ColorTable");
gpu_pipeline_bind_uniform_block(program_pipeline(i), (uint32_t)ct.index, COLOR_TABLE_BINDING_POINT);
}
// Sanity check to ensure the attribute location binding worked
#define C(p, name, expected) \
{ \
@ -467,16 +465,7 @@ init_cell_program(void) {
}
#undef C
UniformBlock border_colors = program_uniform_block(BORDERS_PROGRAM, "Colors");
gpu_pipeline_bind_uniform_block(program_pipeline(BORDERS_PROGRAM), (uint32_t)border_colors.index, BORDER_COLORS_BINDING_POINT);
// The padding program shares the cell background computation and so uses the
// same uniform blocks bound to the same binding points as the cell programs.
{
UniformBlock crd = program_uniform_block(PADDING_PROGRAM, "CellRenderData");
gpu_pipeline_bind_uniform_block(program_pipeline(PADDING_PROGRAM), (uint32_t)crd.index, CELL_RENDER_DATA_BINDING_POINT);
UniformBlock ct = program_uniform_block(PADDING_PROGRAM, "ColorTable");
gpu_pipeline_bind_uniform_block(program_pipeline(PADDING_PROGRAM), (uint32_t)ct.index, COLOR_TABLE_BINDING_POINT);
}
#define C(name, expected) \
{ \
int aloc = attrib_location(PADDING_PROGRAM, #name); \
@ -486,10 +475,11 @@ init_cell_program(void) {
C(sprite_idx, 1);
C(is_selected, 2);
#undef C
// Indexed color images get their colors from the color table of the window they are in
for (int i = GRAPHICS_INDEXED_PROGRAM; i <= GRAPHICS_INDEXED_UNSCALED_PROGRAM; i++) {
UniformBlock ct = program_uniform_block(i, "ColorTable");
gpu_pipeline_bind_uniform_block(program_pipeline(i), (uint32_t)ct.index, COLOR_TABLE_BINDING_POINT);
// The sprite textures are bound once for all cell programs, see bind_sprite_map()
for (int i = CELL_PROGRAM + 1; i < CELL_PROGRAM_SENTINEL; i++) {
uint32_t a, b;
if (try_program_texture_slot(i, "sprites", &a) && (!try_program_texture_slot(CELL_PROGRAM, "sprites", &b) || a != b))
fatal("The cell programs use different binding slots for the sprites texture");
}
if (shader_globals_vao_idx == -1) {
@ -645,16 +635,6 @@ init_custom_programs(void) {
custom_shaders.count++;
if (i == CUSTOM_END_PROGRAM) {
bind_program(i);
gpu_uniform1i(program_uniform_location(i, "backbuffer"), GRAPHICS_UNIT);
#define set_optional_sampler(name, unit) \
{ \
int32_t loc = try_program_uniform_location(i, name); \
if (loc >= 0) gpu_uniform1i(loc, unit); \
}
set_optional_sampler("a", CUSTOM_END_TEXTURE_A_UNIT);
set_optional_sampler("b", CUSTOM_END_TEXTURE_B_UNIT);
set_optional_sampler("persist", CUSTOM_END_TEXTURE_PERSIST_UNIT);
#undef set_optional_sampler
int32_t group_loc = try_program_uniform_location(i, "group");
if (group_loc >= 0) gpu_uniform1i(group_loc, 0);
else if (custom_shaders.end.num_groups > 1) {
@ -667,7 +647,6 @@ init_custom_programs(void) {
custom_shaders.end.num_groups);
}
UniformBlock ubd = program_uniform_block(i, "KittyCustomShaderData");
gpu_pipeline_bind_uniform_block(program_pipeline(i), (uint32_t)ubd.index, CUSTOM_END_DATA_BINDING_POINT);
if (custom_end_vao_idx == -1) {
custom_end_vao_idx = create_vao();
add_buffer_to_vao(custom_end_vao_idx, GPU_BUFFER_UNIFORM);
@ -940,7 +919,7 @@ custom_shader_needs_render(const ShaderAnimState *before, const ShaderAnimState
void
bind_shader_globals_to_current_context(void) {
if (shader_globals_vao_idx == -1) return;
bind_vao_uniform_buffer(shader_globals_vao_idx, BORDER_COLORS_GLOBAL_BUFFER, BORDER_COLORS_BINDING_POINT);
bind_vao_uniform_buffer(shader_globals_vao_idx, BORDER_COLORS_GLOBAL_BUFFER, program_uniform_block(BORDERS_PROGRAM, "Colors").slot);
}
#define CELL_BUFFERS enum { cell_data_buffer, selection_buffer, uniform_buffer, color_table_buffer, padding_cell_data_buffer, padding_selection_buffer };
@ -1315,9 +1294,10 @@ draw_graphics(int program, ImageRenderData *data, uint32_t start, uint32_t count
int32_t dest_rect_loc = program_uniform_location(program, "dest_rect");
const bool is_indexed = program == GRAPHICS_INDEXED_PROGRAM || program == GRAPHICS_INDEXED_UNSCALED_PROGRAM;
const int32_t has_alpha_loc = is_indexed ? program_uniform_location(program, "has_alpha") : -1;
const uint32_t image_slot = program_texture_slot(program, "image");
for (uint32_t i = 0; i < count;) {
ImageRenderData *group = data + start + i;
gpu_bind_texture(GRAPHICS_UNIT, group->texture_id);
gpu_bind_texture(image_slot, group->texture_id);
if (has_alpha_loc > -1) gpu_uniform1ui(has_alpha_loc, group->indexed_px_sz == 2);
if (group->group_count == 0) {
i++;
@ -1381,10 +1361,6 @@ set_cell_uniforms(bool force) {
if (!constants_set || force) {
float text_contrast = 1.0f + OPT(text_contrast) * 0.01f;
float text_gamma_adjustment = OPT(text_gamma_adjustment) < 0.01f ? 1.0f : 1.0f / OPT(text_gamma_adjustment);
for (int i = GRAPHICS_PROGRAM; i <= GRAPHICS_INDEXED_UNSCALED_PROGRAM; i++) {
bind_program(i);
gpu_uniform1i(program_uniform_location(i, "image"), GRAPHICS_UNIT);
}
// These are set for every cell program, but the background program draws no
// text and so does not have them.
#define set_optional(setter, name, value) \
@ -1394,16 +1370,10 @@ set_cell_uniforms(bool force) {
}
for (int i = CELL_PROGRAM; i < CELL_PROGRAM_SENTINEL; i++) {
bind_program(i);
set_optional(gpu_uniform1i, "sprites", SPRITE_MAP_UNIT);
set_optional(gpu_uniform1i, "sprite_decorations_map", SPRITE_DECORATIONS_MAP_UNIT);
set_optional(gpu_uniform1f, "text_contrast", text_contrast);
set_optional(gpu_uniform1f, "text_gamma_adjustment", text_gamma_adjustment);
}
#undef set_optional
bind_program(BLIT_PROGRAM);
gpu_uniform1i(program_uniform_location(BLIT_PROGRAM, "image"), GRAPHICS_UNIT);
bind_program(SCREENSHOT_PROGRAM);
gpu_uniform1i(program_uniform_location(SCREENSHOT_PROGRAM, "image"), GRAPHICS_UNIT);
constants_set = true;
}
}
@ -1728,7 +1698,6 @@ draw_window_number(const UIRenderData *ui) {
bind_program(GRAPHICS_ALPHA_MASK_PROGRAM);
ImageRenderData *ird = load_alpha_mask_texture(lr.width_px, lr.height_px, lr.canvas);
gpu_data_for_image(ird, -1, 1, 1, -1);
gpu_uniform1i(program_uniform_location(GRAPHICS_ALPHA_MASK_PROGRAM, "image"), GRAPHICS_UNIT);
color_type digit_color = colorprofile_to_color_with_fallback(
ui->screen->color_profile,
ui->screen->color_profile->overridden.highlight_bg,
@ -2098,8 +2067,8 @@ static void
call_cell_program(int program, const UIRenderData *ui, ssize_t vao_idx, bool for_final_output, unsigned draw_bg_bitfield) {
bind_program(program);
CELL_BUFFERS;
bind_vao_uniform_buffer(vao_idx, uniform_buffer, CELL_RENDER_DATA_BINDING_POINT);
bind_vao_uniform_buffer(vao_idx, color_table_buffer, COLOR_TABLE_BINDING_POINT);
bind_vao_uniform_buffer(vao_idx, uniform_buffer, program_uniform_block(program, "CellRenderData").slot);
bind_vao_uniform_buffer(vao_idx, color_table_buffer, program_uniform_block(program, "ColorTable").slot);
gpu_uniform1ui(program_uniform_location(program, "draw_bg_bitfield"), draw_bg_bitfield);
if (for_final_output) gpu_set_srgb_output(true);
draw_quad(!for_final_output, render_lines_for_screen(ui->screen) * ui->screen->columns);
@ -2191,8 +2160,8 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
bind_program(PADDING_PROGRAM);
bind_vertex_array(vao_idx);
CELL_BUFFERS;
bind_vao_uniform_buffer(vao_idx, uniform_buffer, CELL_RENDER_DATA_BINDING_POINT);
bind_vao_uniform_buffer(vao_idx, color_table_buffer, COLOR_TABLE_BINDING_POINT);
bind_vao_uniform_buffer(vao_idx, uniform_buffer, program_uniform_block(PADDING_PROGRAM, "CellRenderData").slot);
bind_vao_uniform_buffer(vao_idx, color_table_buffer, program_uniform_block(PADDING_PROGRAM, "ColorTable").slot);
if (for_final_output) gpu_set_srgb_output(true);
// Point instanced attributes at the padding-specific buffers once for both draws.
@ -2344,7 +2313,7 @@ draw_image_layer(const UIRenderData *ui, ssize_t vao_idx, uint32_t start, uint32
}
CELL_BUFFERS;
// The color table may not be bound yet, for example, when the window has a background image
bind_vao_uniform_buffer(vao_idx, color_table_buffer, COLOR_TABLE_BINDING_POINT);
bind_vao_uniform_buffer(vao_idx, color_table_buffer, program_uniform_block(GRAPHICS_INDEXED_PROGRAM, "ColorTable").slot);
const ImageRenderData *data = ui->grd.images;
for (uint32_t run_start = start, end = start + count; run_start < end;) {
const int program = program_for_image(data + run_start);
@ -2665,9 +2634,8 @@ draw_bg_image(OSWindow *os_window, Tab *tab) {
gpu_uniform4f(program_uniform_location(BGIMAGE_PROGRAM, "sizes"), vwidth, vheight, iwidth, iheight);
gpu_uniform1f(program_uniform_location(BGIMAGE_PROGRAM, "tiled"), tiled);
gpu_uniform4f(program_uniform_location(BGIMAGE_PROGRAM, "positions"), left, top, right, bottom);
gpu_uniform1i(program_uniform_location(BGIMAGE_PROGRAM, "image"), GRAPHICS_UNIT);
color_vec4(program_uniform_location(BGIMAGE_PROGRAM, "background"), s.bgcolor, s.opacity);
gpu_bind_texture(GRAPHICS_UNIT, bg->texture_id);
gpu_bind_texture(program_texture_slot(BGIMAGE_PROGRAM, "image"), bg->texture_id);
draw_quad(false, 0);
unbind_program();
}
@ -2685,7 +2653,6 @@ draw_centered_alpha_mask(size_t screen_width, size_t screen_height, size_t width
ImageRenderData *data = load_alpha_mask_texture(width, height, canvas);
gpu_data_for_centered_image(data, screen_width, screen_height, width, height);
bind_program(GRAPHICS_ALPHA_MASK_PROGRAM);
gpu_uniform1i(program_uniform_location(GRAPHICS_ALPHA_MASK_PROGRAM, "image"), GRAPHICS_UNIT);
color_vec3(program_uniform_location(GRAPHICS_ALPHA_MASK_PROGRAM, "amask_fg"), OPT(foreground));
color_vec4_premult(program_uniform_location(GRAPHICS_ALPHA_MASK_PROGRAM, "amask_bg_premult"), OPT(background), background_opacity);
draw_graphics(GRAPHICS_ALPHA_MASK_PROGRAM, data, 0, 1, 1.0);
@ -2903,8 +2870,11 @@ update_pointer_map(OSWindow *os_window, int32_t group_loc) {
if (!pointer_map_target.framebuffer_id)
setup_texture_as_render_target(POINTER_MAP_SIZE, POINTER_MAP_SIZE, &pointer_map_target.texture_id, &pointer_map_target.framebuffer_id);
// The pointer map does not sample any textures, but every sampler must be bound to a valid one
static const int units[] = {GRAPHICS_UNIT, CUSTOM_END_TEXTURE_A_UNIT, CUSTOM_END_TEXTURE_B_UNIT, CUSTOM_END_TEXTURE_PERSIST_UNIT};
for (size_t i = 0; i < arraysz(units); i++) { gpu_bind_texture(units[i], global_state.layers_render_texture.texture_id); }
static const char *const names[] = {"backbuffer", "a", "b", "persist"};
for (size_t i = 0; i < arraysz(names); i++) {
uint32_t slot;
if (try_program_texture_slot(CUSTOM_END_PROGRAM, names[i], &slot)) gpu_bind_texture(slot, global_state.layers_render_texture.texture_id);
}
bind_framebuffer_for_output(pointer_map_target.framebuffer_id);
gpu_set_viewport(0, 0, POINTER_MAP_SIZE, POINTER_MAP_SIZE);
gpu_uniform1i(group_loc, -1);
@ -3170,7 +3140,7 @@ run_custom_end_shader(OSWindow *os_window, float sx, float sy, monotonic_t now)
}
}
unmap_vao_buffer(custom_end_vao_idx, 0);
bind_vao_uniform_buffer(custom_end_vao_idx, 0, CUSTOM_END_DATA_BINDING_POINT);
bind_vao_uniform_buffer(custom_end_vao_idx, 0, program_uniform_block(CUSTOM_END_PROGRAM, "KittyCustomShaderData").slot);
// A custom shader need not reference any of these, in which case the driver
// drops them and glUniform*() on the resulting -1 location is a no-op.
@ -3196,12 +3166,13 @@ run_custom_end_shader(OSWindow *os_window, float sx, float sy, monotonic_t now)
os_window->indirect_output.fbo_b_id,
os_window->persist_fbo_id,
};
static const int named_tex_units[4] = {
GRAPHICS_UNIT, // DEFAULT maps to backbuffer slot
CUSTOM_END_TEXTURE_A_UNIT,
CUSTOM_END_TEXTURE_B_UNIT,
CUSTOM_END_TEXTURE_PERSIST_UNIT,
};
// The binding slots of the named textures, indexed by NamedTexture enum
// value, false in has_named_tex_slot when the program does not use the texture
static const char *const named_tex_names[4] = {"backbuffer", "a", "b", "persist"};
uint32_t named_tex_slots[4] = {0};
bool has_named_tex_slot[4];
for (size_t i = 0; i < arraysz(named_tex_names); i++)
has_named_tex_slot[i] = try_program_texture_slot(CUSTOM_END_PROGRAM, named_tex_names[i], named_tex_slots + i);
// Pre-scan to find the last group that will actually run this frame.
// That group gets convert_to_srgb=true so its draw call replaces the
@ -3231,7 +3202,7 @@ run_custom_end_shader(OSWindow *os_window, float sx, float sy, monotonic_t now)
backbuffer_is_main ? global_state.layers_render_texture.texture_id : global_state.layers_render_texture.extra_texture_id;
// Bind backbuffer to the backbuffer texture unit
gpu_bind_texture(GRAPHICS_UNIT, backbuffer);
if (has_named_tex_slot[CUSTOM_SHADER_TEXTURE_DEFAULT]) gpu_bind_texture(named_tex_slots[CUSTOM_SHADER_TEXTURE_DEFAULT], backbuffer);
// Bind each named texture to its own unit.
// If the texture is the output for this group, or is not declared in the
@ -3245,7 +3216,7 @@ run_custom_end_shader(OSWindow *os_window, float sx, float sy, monotonic_t now)
} else {
tex = backbuffer; // not present — fall back to backbuffer
}
gpu_bind_texture(named_tex_units[t], tex);
if (has_named_tex_slot[t]) gpu_bind_texture(named_tex_slots[t], tex);
}
// Set the output framebuffer
@ -3317,7 +3288,7 @@ stop_os_window_rendering(OSWindow *os_window, Tab *tab, Window *active_window, m
set_framebuffer_to_use_for_output(0);
bind_framebuffer_for_output(0);
bind_program(BLIT_PROGRAM);
gpu_bind_texture(GRAPHICS_UNIT, global_state.layers_render_texture.texture_id);
gpu_bind_texture(program_texture_slot(BLIT_PROGRAM, "image"), global_state.layers_render_texture.texture_id);
gpu_uniform4f(program_uniform_location(BLIT_PROGRAM, "src_rect"), 0, sy, sx, 0);
gpu_uniform4f(program_uniform_location(BLIT_PROGRAM, "dest_rect"), -1, 1, 1, -1);
restore_viewport();
@ -3413,7 +3384,7 @@ take_screenshot_of_rectangular_region(OSWindow *os_window, Region region, unsign
gpu_uniform2f(program_uniform_location(SCREENSHOT_PROGRAM, "src_size"), (float)vw, (float)vh);
// Bind the source texture
gpu_bind_texture(GRAPHICS_UNIT, src_texture);
gpu_bind_texture(program_texture_slot(SCREENSHOT_PROGRAM, "image"), src_texture);
// Draw the scaled quad
draw_quad(false, 0);
@ -3697,6 +3668,39 @@ compile_program(PyObject UNUSED *self, PyObject *args) {
num_names++;
}
}
// The binding slots of textures and uniform blocks
PyObject *textures = PyDict_GetItemString(metadata, "textures"), *blocks = PyDict_GetItemString(metadata, "blocks");
if (!textures || !PyDict_Check(textures) || !blocks || !PyDict_Check(blocks)) {
PyErr_SetString(PyExc_KeyError, "The shader program metadata has no textures or blocks dict");
return NULL;
}
RAII_ALLOC(GPUBinding, bindings, calloc(MAX(1, PyDict_GET_SIZE(textures) + PyDict_GET_SIZE(blocks)), sizeof(GPUBinding)));
if (!bindings) return PyErr_NoMemory();
size_t num_bindings = 0;
{
PyObject *key, *val;
Py_ssize_t pos = 0;
while (PyDict_Next(textures, &pos, &key, &val)) {
GPUBinding *b = bindings + num_bindings++;
b->kind = GPU_BINDING_TEXTURE;
if (!(b->name = PyUnicode_AsUTF8(key))) return NULL;
b->slot = (uint32_t)PyLong_AsUnsignedLong(val);
if (PyErr_Occurred()) return NULL;
}
pos = 0;
while (PyDict_Next(blocks, &pos, &key, &val)) {
GPUBinding *b = bindings + num_bindings++;
b->kind = GPU_BINDING_UNIFORM_BLOCK;
if (!(b->name = PyUnicode_AsUTF8(key))) return NULL;
PyObject *slot = PyDict_Check(val) ? PyDict_GetItemString(val, "slot") : NULL;
if (!slot) {
PyErr_Format(PyExc_KeyError, "The uniform block %s in the shader program metadata has no slot", b->name);
return NULL;
}
b->slot = (uint32_t)PyLong_AsUnsignedLong(slot);
if (PyErr_Occurred()) return NULL;
}
}
RAII_PyObject(vertex_source, join_shader_sources(vertex_shaders));
if (!vertex_source) return NULL;
RAII_PyObject(fragment_source, join_shader_sources(fragment_shaders));
@ -3710,7 +3714,7 @@ compile_program(PyObject UNUSED *self, PyObject *args) {
{.stage = GPU_SHADER_VERTEX, .language = language, .code = vcode, .size = (size_t)vsz},
{.stage = GPU_SHADER_FRAGMENT, .language = language, .code = fcode, .size = (size_t)fsz},
};
gpu_pipeline_t pipeline = gpu_pipeline_create(sources, arraysz(sources), names, num_names);
gpu_pipeline_t pipeline = gpu_pipeline_create(sources, arraysz(sources), bindings, num_bindings, names, num_names);
if (!pipeline) {
PyErr_SetString(PyExc_ValueError, gpu_last_error());
return NULL;

View file

@ -42,7 +42,8 @@ public struct CellRenderData {
};
// Uniform Blocks (adjust binding slots as needed for your pipeline layout)
public ConstantBuffer<CellRenderData> crd;
[[vk::binding(6)]]
public ConstantBuffer<CellRenderData> crd; // binding slots: see SHADER_BINDING_SLOTS in slang.py
// The SGR color table, then the mark background and foreground colors and
// finally the special colors used by indexed color images.
@ -51,6 +52,7 @@ public struct ColorTable {
uint color_table[INDEXED_IMAGE_SPECIAL_COLORS_OFFSET + NUM_INDEXED_IMAGE_SPECIAL_COLORS];
};
[[vk::binding(7)]]
public ConstantBuffer<ColorTable> ctb;
#define color_table ctb.color_table
@ -61,6 +63,7 @@ public struct GammaLUT {
float gamma_lut[256];
};
[[vk::binding(8)]]
public ConstantBuffer<GammaLUT> glt;
#define gamma_lut glt.gamma_lut
//

View file

@ -59,7 +59,8 @@ vertex_main(uint vertex_id: SV_VertexID, uniform float tiled, uniform float4 siz
}
// Global Texture Bindings
uniform Sampler2D image;
[[vk::binding(1)]]
uniform Sampler2D image; // binding slots: see SHADER_BINDING_SLOTS in slang.py
// Main Fragment Shader Entry Point
[shader("fragment")]

View file

@ -20,7 +20,8 @@ vertex_main(uint vertex_id: SV_VertexID, uniform float4 src_rect, uniform float4
return { ans.texcoord, float4(ans.position[0], ans.position[1], 0.0, 1.0) };
}
uniform Sampler2D image;
[[vk::binding(1)]]
uniform Sampler2D image; // binding slots: see SHADER_BINDING_SLOTS in slang.py
[shader("fragment")]
float4

View file

@ -20,12 +20,14 @@ import utils;
struct GammaLUT {
uniform float gamma_lut[256];
};
ConstantBuffer<GammaLUT> glt;
[[vk::binding(8)]]
ConstantBuffer<GammaLUT> glt; // binding slots: see SHADER_BINDING_SLOTS in slang.py
#define gamma_lut glt.gamma_lut
struct Colors {
uniform uint colors[9];
};
[[vk::binding(9)]]
ConstantBuffer<Colors> clrs;
#define colors clrs.colors

View file

@ -23,7 +23,8 @@ static const bool ONLY_BACKGROUND = RENDER_MODE == 1;
// LUT as well as most color utility functions live in the background module so
// that they can be shared with the padding shader.
uniform Sampler2D<uint4> sprite_decorations_map;
[[vk::binding(2)]]
uniform Sampler2D<uint4> sprite_decorations_map; // binding slots: see SHADER_BINDING_SLOTS in slang.py
static const uint SPRITE_INDEX_MASK = 0x7fffffff;
static const uint SPRITE_COLORED_MASK = 0x80000000;
@ -217,6 +218,7 @@ vertex_main(
return vo;
}
[[vk::binding(0)]]
uniform Sampler2DArray sprites;
// Scaling factor for the extra text-alpha adjustment for luminance-difference.
static const float text_gamma_scaling = 0.5;

View file

@ -29,7 +29,8 @@ get_coords_for_blit(uint vertex_id, float4 src_rect, float4 dest_rect) {
}
public ConstantBuffer<KittyCustomShaderData> csd;
[[vk::binding(10)]]
public ConstantBuffer<KittyCustomShaderData> csd; // binding slots: see SHADER_BINDING_SLOTS in slang.py
public float4
@ -59,7 +60,14 @@ linear2srgb(float3 x) { // vector
return lerp(lower, upper, step(float3(0.0031308f), x));
}
uniform Sampler2D backbuffer, a, b, persist;
[[vk::binding(1)]]
uniform Sampler2D backbuffer;
[[vk::binding(3)]]
uniform Sampler2D a;
[[vk::binding(4)]]
uniform Sampler2D b;
[[vk::binding(5)]]
uniform Sampler2D persist;
// The size of the pointer map grid, POINTER_MAP_SIZE is defined by kitty from state.h when compiling
static const float pointer_map_size = POINTER_MAP_SIZE;

View file

@ -23,7 +23,8 @@ vertex_main(uint vertex_id: SV_VertexID, uniform float4 src_rect, uniform float4
return { ans.texcoord, float4(ans.position[0], ans.position[1], 0.0, 1.0) };
}
uniform Sampler2D image;
[[vk::binding(1)]]
uniform Sampler2D image; // binding slots: see SHADER_BINDING_SLOTS in slang.py
[shader("fragment")]
float4

View file

@ -28,7 +28,8 @@ vertex_main(uint vertex_id: SV_VertexID, uniform float4 src_rect, uniform float4
// The red channel is the palette index and, when has_alpha is non-zero, the
// green channel is the straight alpha.
uniform Sampler2D<uint4> image;
[[vk::binding(1)]]
uniform Sampler2D<uint4> image; // binding slots: see SHADER_BINDING_SLOTS in slang.py
float4
premult_color_at(int2 pos, uint has_alpha) {

View file

@ -18,7 +18,8 @@ vertex_main(uint vertex_id: SV_VertexID, uniform float4 src_rect, uniform float4
return { ans.texcoord, float4(ans.position[0], ans.position[1], 0.0, 1.0) };
}
uniform Sampler2D image;
[[vk::binding(1)]]
uniform Sampler2D image; // binding slots: see SHADER_BINDING_SLOTS in slang.py
float3
safe_unpremult_to_linear(float4 s) {

View file

@ -1317,6 +1317,44 @@ def shader_name_from_path(path: str) -> str:
return '.'.join(parts[:2])
# The binding slots of every texture and uniform block in kitty's shaders,
# declared in the shaders with [[vk::binding(N)]]. Slots are unique across all
# shaders and kinds of resource, which is required by Vulkan where textures and
# uniform blocks share a single namespace of bindings. Unique slots also mean
# that resources bound once and used by many programs, such as the sprite
# textures, which are bound once per frame while drawing both cells and
# images, are never displaced by binding the resources of another program.
# The C code never hard codes slots, it gets them from the program metadata, so
# drivers for targets that assign slots differently, such as Metal, work too.
SHADER_BINDING_SLOTS = MappingProxyType(
{
# textures
'sprites': 0,
'image': 1,
'backbuffer': 1, # the custom shader equivalent of image, both are bound before every draw
'sprite_decorations_map': 2,
'a': 3,
'b': 4,
'persist': 5,
# uniform blocks, identified by the name of their type
'CellRenderData': 6,
'ColorTable': 7,
'GammaLUT': 8,
'Colors': 9,
'KittyCustomShaderData': 10,
}
)
def check_binding_slot(name: str, slot: int) -> int:
expected = SHADER_BINDING_SLOTS.get(name)
if expected is None:
raise ValueError(f'The shader resource {name} has no binding slot in SHADER_BINDING_SLOTS')
if slot != expected:
raise ValueError(f'The shader resource {name} uses binding slot {slot} instead of {expected}, declare it with [[vk::binding({expected})]]')
return slot
def program_metadata(target: ShaderTarget, reflections: Sequence[dict[str, Any]], native: GLSLMetadata) -> dict[str, Any]:
"""
The metadata about a program passed to the C code. It is generated from the
@ -1327,6 +1365,7 @@ def program_metadata(target: ShaderTarget, reflections: Sequence[dict[str, Any]]
"""
inputs: dict[str, int] = {}
uniforms: set[str] = set()
textures: dict[str, int] = {}
blocks: dict[str, dict[str, Any]] = {}
params: dict[str, dict[str, dict[str, int]]] = {}
# Uniform blocks that are emitted as uniform blocks rather than as loose uniforms by the target
@ -1339,17 +1378,18 @@ def program_metadata(target: ShaderTarget, reflections: Sequence[dict[str, Any]]
evl = t['elementVarLayout']
et = evl['type']
tname = et['name']
slot = check_binding_slot(tname, p['binding']['index'])
if tname in real_blocks:
arrays = {}
for f in et['fields']:
ft = f['type']
if ft['kind'] == 'array':
arrays[f['name']] = {'offset': f['binding']['offset'], 'stride': ft['uniformStride'], 'count': ft['elementCount']}
blocks[tname] = {'size': evl['binding']['size'], 'arrays': arrays}
blocks[tname] = {'size': evl['binding']['size'], 'arrays': arrays, 'slot': slot}
else:
uniforms.update(f['name'] for f in et['fields'])
case 'resource':
uniforms.add(p['name'])
textures[p['name']] = check_binding_slot(p['name'], p['binding']['index'])
case _:
raise ValueError(f'Unsupported shader parameter: {p["name"]} of kind: {t["kind"]}')
for ep in refl['entryPoints']:
@ -1372,6 +1412,7 @@ def program_metadata(target: ShaderTarget, reflections: Sequence[dict[str, Any]]
'target': target.id,
'inputs': inputs,
'uniforms': sorted(uniforms),
'textures': textures,
'blocks': blocks,
'params': params,
'native_names': native_names,