Simplify is_selected buffer Metal compatibility

This commit is contained in:
Kovid Goyal 2026-11-04 10:55:57 +05:30
parent d2e16a322a
commit c1fc37a013
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
11 changed files with 77 additions and 123 deletions

View file

@ -53,7 +53,7 @@
#include <stddef.h>
#include <stdint.h>
#define KITTY_GPU_API_VERSION 4u
#define KITTY_GPU_API_VERSION 5u
#define KITTY_GPU_DRIVER_ENTRY_POINT "kitty_gpu_driver_load"
#define GPU_NULL_HANDLE ((uint64_t)0)
@ -96,6 +96,11 @@ typedef struct GPUCapabilities {
uint32_t max_array_texture_layers; // max number of layers in an array texture
// Whether render targets can encode linear colors to sRGB on output, see set_srgb_output()
bool supports_srgb_output;
// The offsets and strides of vertex inputs must be multiples of this
// many bytes, a power of two. Per instance data with elements smaller than
// this, such as the one byte selection state of cells, must be stored
// with each element padded to this size.
uint32_t vertex_input_alignment;
} GPUCapabilities;
// }}}

View file

@ -36,8 +36,6 @@ free_buffer(Buffer *b) {
destroy_storage(&b->storage);
for (size_t i = 0; i < b->num_retired; i++) destroy_storage(b->retired + i);
b->num_retired = 0;
[b->repacked.buffer release];
b->repacked.buffer = nil;
}
// Get storage of the specified size that no commands use, reusing retired storage if possible
@ -72,7 +70,6 @@ retire_storage(Buffer *b, BufferStorage *s) {
// preserving its contents if preserve is true
static void
make_writable(Driver *d, Buffer *b, bool preserve) {
b->generation++;
if (b->storage.last_use_serial <= completed_serial(d)) return;
BufferStorage s;
if (!fresh_storage(d, b, b->storage.size, &s)) fatal(d, "Failed to allocate a GPU buffer with error: %s", d->last_error);
@ -124,7 +121,6 @@ buffer_allocate(void *driver, gpu_buffer_t buffer, size_t size, GPUUpdateFrequen
if (b->storage.buffer != nil && b->storage.size == size) return;
// Retired storage of a different size is useless
free_buffer(b);
b->generation++;
if (!size) return;
if (!create_storage(d, size, &b->storage)) fatal(d, "Failed to allocate a GPU buffer of size %zu with error: %s", size, d->last_error);
}
@ -188,59 +184,3 @@ use_buffer(Driver *d, Buffer *b) {
b->storage.last_use_serial = recording_serial(d);
}
// }}}
// Vertex buffers {{{
// Metal requires the offsets and strides of vertex buffers to be multiples
// of four bytes. kitty has vertex inputs with one byte elements that are
// tightly packed, such as the selection state of cells, whose elements are
// copied into a buffer where each element starts at a multiple of four bytes.
// The copy is cached with the buffer, it is made again only when the
// contents of the buffer change.
static bool
needs_repack(const VertexAttribute *a) {
return (a->offset % 4) != 0 || (a->stride % 4) != 0;
}
uint32_t
vertex_buffer_stride(const VertexAttribute *a, size_t element_size) {
return needs_repack(a) ? (uint32_t)((element_size + 3u) / 4u * 4u) : a->stride;
}
id<MTLBuffer>
vertex_buffer_for(Driver *d, const VertexAttribute *a, size_t element_size, size_t *offset, uint32_t *stride) {
Buffer *b = a->buffer;
if (!b || b->storage.buffer == nil) return nil;
use_buffer(d, b);
if (!needs_repack(a)) {
*offset = a->offset;
*stride = a->stride;
return b->storage.buffer;
}
const uint32_t new_stride = vertex_buffer_stride(a, element_size);
*offset = 0;
*stride = new_stride;
if (b->repacked.buffer != nil && b->repacked.generation == b->generation && b->repacked.offset == a->offset && b->repacked.stride == a->stride &&
b->repacked.element_size == element_size && b->repacked.size == b->storage.size)
return b->repacked.buffer;
// The number of whole elements in the buffer
size_t count = 0;
if (b->storage.size >= a->offset + element_size) count = (b->storage.size - a->offset - element_size) / a->stride + 1;
// The previous copy may still be used by commands, which retain it, so a new one is made
id<MTLBuffer> buf = [d->device newBufferWithLength:MAX((size_t)16u, count * new_stride) options:MTLResourceStorageModeShared];
if (buf == nil) {
log_error(d, "Failed to allocate a GPU buffer of size %zu for repacking vertex data", count * new_stride);
return nil;
}
const uint8_t *src = (const uint8_t *)[b->storage.buffer contents] + a->offset;
uint8_t *dst = [buf contents];
for (size_t i = 0; i < count; i++) memcpy(dst + i * new_stride, src + i * a->stride, element_size);
[b->repacked.buffer release];
b->repacked.buffer = buf;
b->repacked.generation = b->generation;
b->repacked.offset = a->offset;
b->repacked.stride = a->stride;
b->repacked.element_size = (uint32_t)element_size;
b->repacked.size = b->storage.size;
return buf;
}
// }}}

View file

@ -506,14 +506,13 @@ bind_vertex_buffers(Driver *d, id<MTLRenderCommandEncoder> enc, const PipelineVa
for (uint32_t loc = 0; loc < MAX_VERTEX_ATTRIBUTES; loc++) {
if (key->inputs[loc].format == MTLVertexFormatInvalid) continue;
const VertexAttribute *a = d->vertex_layout->attributes + loc;
size_t offset;
uint32_t stride;
id<MTLBuffer> buf = vertex_buffer_for(d, a, vertex_attribute_size(a->components, a->type), &offset, &stride);
if (buf == nil) {
Buffer *b = a->buffer;
if (!b || b->storage.buffer == nil) {
log_error(d, "The vertex buffer for the vertex input at location: %u has no storage", loc);
return false;
}
[enc setVertexBuffer:buf offset:offset atIndex:VERTEX_BUFFER_INDEX(loc)];
use_buffer(d, b);
[enc setVertexBuffer:b->storage.buffer offset:a->offset atIndex:VERTEX_BUFFER_INDEX(loc)];
}
return true;
}

View file

@ -198,6 +198,7 @@ get_capabilities(void *driver, GPUCapabilities *ans) {
ans->max_array_texture_layers = MAX_TEXTURE_LAYERS;
// Any texture can be viewed with the sRGB variant of its pixel format, including drawables
ans->supports_srgb_output = d->device != nil;
ans->vertex_input_alignment = VERTEX_INPUT_ALIGNMENT;
}
// }}}

View file

@ -103,6 +103,9 @@
#define MAX_TEXTURE_LAYERS 2048u
// The maximum number of render target pixel formats for the utility pipelines, see Driver
#define MAX_UTILITY_FORMATS 8
// Metal requires the offsets and strides of vertex buffers to be multiples of
// four bytes, reported to kitty in GPUCapabilities
#define VERTEX_INPUT_ALIGNMENT 4u
// Resources {{{
typedef struct BufferStorage {
@ -118,17 +121,6 @@ typedef struct Buffer {
// Storage that commands might still use, reusable once the serial completes
BufferStorage retired[4];
size_t num_retired;
// Incremented whenever the contents of the buffer change
uint64_t generation;
// A copy of the contents with the elements of a vertex input moved to
// offsets and strides that are multiples of four bytes, as required by
// Metal, see repacked_vertex_buffer()
struct {
id<MTLBuffer> buffer;
uint64_t generation;
size_t offset, size;
uint32_t stride, element_size;
} repacked;
struct Buffer *prev, *next;
} Buffer;
@ -369,12 +361,6 @@ void bind_uniform_buffer(void *driver, uint32_t slot, gpu_buffer_t buffer);
void use_buffer(Driver *d, Buffer *b);
// Write data to the start of the buffer, giving it new storage if commands might still use the current one
bool write_buffer(Driver *d, Buffer *b, const void *data, size_t size);
// The buffer and offset to use for a vertex input, repacking its elements if
// their offset or stride are not multiples of four bytes. Sets stride to the
// stride of the elements in the returned buffer. nil on failure.
id<MTLBuffer> vertex_buffer_for(Driver *d, const VertexAttribute *a, size_t element_size, size_t *offset, uint32_t *stride);
// The stride of the elements of a vertex input in the buffer returned by vertex_buffer_for()
uint32_t vertex_buffer_stride(const VertexAttribute *a, size_t element_size);
void destroy_all_buffers(Driver *d);
// textures.m

View file

@ -446,7 +446,17 @@ pipeline_for_current_state(Driver *d, bool blend, PipelineVariantKey *key) {
log_error(d, "The vertex input at location: %u has an unsupported format", loc);
return nil;
}
k->stride = vertex_buffer_stride(a, vertex_attribute_size(a->components, a->type));
if ((a->offset % VERTEX_INPUT_ALIGNMENT) != 0 || (a->stride % VERTEX_INPUT_ALIGNMENT) != 0) {
log_error(
d,
"The vertex input at location: %u has offset: %zu and stride: %u which are not multiples of: %u bytes",
loc,
a->offset,
a->stride,
VERTEX_INPUT_ALIGNMENT);
return nil;
}
k->stride = a->stride;
k->per_instance = a->divisor > 0;
k->step_rate = a->divisor > 0 ? a->divisor : 1;
}

View file

@ -270,6 +270,7 @@ get_capabilities(void *driver, GPUCapabilities *ans) {
ans->max_texture_size = d->max_texture_size > 0 ? (uint32_t)d->max_texture_size : 0;
ans->max_array_texture_layers = d->max_array_texture_layers > 0 ? (uint32_t)d->max_array_texture_layers : 0;
ans->supports_srgb_output = d->supports_srgb_output;
ans->vertex_input_alignment = 1;
}
// }}}

View file

@ -538,6 +538,8 @@ get_capabilities(void *driver, GPUCapabilities *ans) {
ans->max_array_texture_layers = d->properties.limits.maxImageArrayLayers;
// Encoding to sRGB needs both UNORM and SRGB views of swapchain images
ans->supports_srgb_output = d->has_swapchain_mutable_format;
// Vulkan places no alignment requirements on vertex input offsets and strides
ans->vertex_input_alignment = 1;
}
// }}}

View file

@ -4653,7 +4653,7 @@ render_iteration_data(const Screen *self, const Selection *s, IterationData *ans
}
static void
apply_selection(Screen *self, uint8_t *data, Selection *s, uint8_t set_mask, int extra_leading_rows, int scrolled_by) {
apply_selection(Screen *self, uint8_t *data, size_t stride, Selection *s, uint8_t set_mask, int extra_leading_rows, int scrolled_by) {
render_iteration_data(self, s, &s->last_rendered);
Line *line;
const int y_min = MAX(-extra_leading_rows - scrolled_by, s->last_rendered.y), y_limit = MIN(s->last_rendered.y_limit, (int)self->lines - scrolled_by);
@ -4669,15 +4669,15 @@ apply_selection(Screen *self, uint8_t *data, Selection *s, uint8_t set_mask, int
if (!line) continue;
}
const int y_in_data = (y + extra_leading_rows + scrolled_by);
uint8_t *line_start = data + self->columns * y_in_data;
uint8_t *line_start = data + stride * self->columns * y_in_data;
XRange xr = xrange_for_iteration_with_multicells(&s->last_rendered, y, line);
for (index_type x = xr.x; x < xr.x_limit; x++) {
line_start[x] |= set_mask;
line_start[stride * x] |= set_mask;
CPUCell *c = &line->cpu_cells[x];
if (c->is_multicell && c->scale > 1) {
for (int ym = MAX(0, y_in_data - c->y); ym < y_in_data; ym++) data[self->columns * ym + x] |= set_mask;
for (int ym = MAX(0, y_in_data - c->y); ym < y_in_data; ym++) data[stride * (self->columns * ym + x)] |= set_mask;
for (int ym = y_in_data + 1; ym < MIN((int)self->lines + extra_leading_rows, y_in_data + c->scale - c->y); ym++)
data[self->columns * ym + x] |= set_mask;
data[stride * (self->columns * ym + x)] |= set_mask;
}
}
}
@ -4765,26 +4765,26 @@ screen_scrollbar_geometry(const Screen *self, float *handle_position, float *vis
}
void
screen_apply_selection(Screen *self, void *address_, size_t size) {
screen_apply_selection(Screen *self, void *address_, size_t num_cells, size_t stride) {
uint8_t *address = address_;
memset(address, 0, size);
memset(address, 0, num_cells * stride);
const unsigned offset = pixel_scroll_enabled(self);
const unsigned scrolled_by = rendered_scrolled_by(self);
Selections *sel = self->paused_rendering.expires_at ? &self->paused_rendering.selections : &self->selections;
for (size_t i = 0; i < sel->count; i++) apply_selection(self, address, sel->items + i, 1, offset, scrolled_by);
for (size_t i = 0; i < sel->count; i++) apply_selection(self, address, stride, sel->items + i, 1, offset, scrolled_by);
sel->last_rendered_count = sel->count;
sel = self->paused_rendering.expires_at ? &self->paused_rendering.url_ranges : &self->url_ranges;
for (size_t i = 0; i < sel->count; i++) {
Selection *s = sel->items + i;
if (OPT(underline_hyperlinks) == UNDERLINE_NEVER && s->is_hyperlink) continue;
apply_selection(self, address, s, 2, offset, scrolled_by);
apply_selection(self, address, stride, s, 2, offset, scrolled_by);
}
sel->last_rendered_count = sel->count;
address += (size_t)offset * self->columns;
size -= (size_t)offset * self->columns;
address += (size_t)offset * self->columns * stride;
num_cells -= (size_t)offset * self->columns;
ExtraCursors *ec = self->paused_rendering.expires_at ? &self->paused_rendering.extra_cursors : &self->extra_cursors;
for (unsigned i = 0; i < ec->count; i++) {
if (ec->locations[i].cell < size) address[ec->locations[i].cell] |= (ec->locations[i].shape & 7) << 2;
if (ec->locations[i].cell < num_cells) address[(size_t)ec->locations[i].cell * stride] |= (ec->locations[i].shape & 7) << 2;
}
ec->dirty = false;
}
@ -7492,7 +7492,7 @@ static PyObject *
current_selections(Screen *self, PyObject *a UNUSED) {
PyObject *ans = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)render_lines_for_screen(self) * self->columns);
if (!ans) return NULL;
screen_apply_selection(self, PyBytes_AS_STRING(ans), PyBytes_GET_SIZE(ans));
screen_apply_selection(self, PyBytes_AS_STRING(ans), PyBytes_GET_SIZE(ans), 1);
self->last_rendered.scrolled_by = rendered_scrolled_by(self); // mimic what the renderer does
return ans;
}

View file

@ -352,7 +352,9 @@ void report_device_attributes(Screen *self, unsigned int UNUSED mode, char start
void select_graphic_rendition(Screen *self, int *params, unsigned int count, bool is_group, Region *r);
void report_device_status(Screen *self, unsigned int which, bool UNUSED);
void report_mode_status(Screen *self, unsigned int which, bool);
void screen_apply_selection(Screen *self, void *address, size_t size);
// Write the selection state of num_cells cells, one byte per cell, to the
// first byte of consecutive elements of stride bytes starting at address
void screen_apply_selection(Screen *self, void *address, size_t num_cells, size_t stride);
bool screen_update_graphics_layers(Screen *self);
void screen_scrollbar_geometry(const Screen *self, float *handle_position, float *visible_fraction);
bool screen_has_scrollbar(const Screen *self, bool is_hovering);

View file

@ -79,6 +79,10 @@ typedef struct {
static const SpriteMap NEW_SPRITE_MAP = {.xnum = 1, .ynum = 1, .last_num_of_layers = 1, .last_ynum = -1};
static int32_t max_texture_size = 0, max_array_texture_layers = 0;
// The number of bytes per cell in the buffers holding the selection state of
// cells. The state is a single byte, padded to the vertex input alignment
// required by the GPU driver, set in initialize_gpu().
static size_t selection_stride = 1;
static float
srgb_color(uint8_t color) {
@ -934,7 +938,7 @@ create_cell_vao(void) {
A1("colors", 3, GPU_ATTRIB_U32, fg);
add_buffer_to_vao(vao_idx, GPU_BUFFER_VERTEX);
A("is_selected", 1, GPU_ATTRIB_U8, 0, 0);
A("is_selected", 1, GPU_ATTRIB_U8, 0, (uint32_t)selection_stride);
size_t bufnum = add_buffer_to_vao(vao_idx, GPU_BUFFER_UNIFORM);
alloc_vao_buffer(vao_idx, program_uniform_block(CELL_PROGRAM, "CellRenderData").size, bufnum, GPU_UPDATE_EVERY_USE);
@ -1252,15 +1256,15 @@ cell_prepare_to_render(ssize_t vao_idx, Screen *screen, FONTS_DATA_HANDLE fonts_
} else if (screen->reload_all_gpu_data || screen->scroll_changed || screen->is_dirty || screen_resized || (disable_ligatures && cursor_pos_changed))
update_cell_data;
#define update_selection_data \
{ \
const unsigned int render_lines = render_lines_for_screen(screen); \
sz = (size_t)render_lines * screen->columns; \
address = alloc_and_map_vao_buffer_for_full_rewrite(vao_idx, sz, selection_buffer); \
screen_apply_selection(screen, address, sz); \
unmap_vao_buffer(vao_idx, selection_buffer); \
address = NULL; \
changed = true; \
#define update_selection_data \
{ \
const unsigned int render_lines = render_lines_for_screen(screen); \
sz = (size_t)render_lines * screen->columns; \
address = alloc_and_map_vao_buffer_for_full_rewrite(vao_idx, sz * selection_stride, selection_buffer); \
screen_apply_selection(screen, address, sz, selection_stride); \
unmap_vao_buffer(vao_idx, selection_buffer); \
address = NULL; \
changed = true; \
}
if (screen->paused_rendering.expires_at) {
@ -2098,8 +2102,8 @@ configure_cell_vao_attributes(ssize_t vao_idx, unsigned int base_cell, unsigned
program_attribute_location(CELL_PROGRAM, "is_selected"),
1,
GPU_ATTRIB_U8,
(uint32_t)(cell_step * sizeof(uint8_t)),
(size_t)base_cell * sizeof(uint8_t),
(uint32_t)(cell_step * selection_stride),
(size_t)base_cell * selection_stride,
1);
}
@ -2120,7 +2124,8 @@ configure_padding_vao_attributes(ssize_t vao_idx) {
1);
set_vao_attribute(
vao_idx, padding_cell_data_buffer, program_attribute_location(CELL_PROGRAM, "colors"), 3, GPU_ATTRIB_U32, sizeof(GPUCell), offsetof(GPUCell, fg), 1);
set_vao_attribute(vao_idx, padding_selection_buffer, program_attribute_location(CELL_PROGRAM, "is_selected"), 1, GPU_ATTRIB_U8, sizeof(uint8_t), 0, 1);
set_vao_attribute(
vao_idx, padding_selection_buffer, program_attribute_location(CELL_PROGRAM, "is_selected"), 1, GPU_ATTRIB_U8, (uint32_t)selection_stride, 0, 1);
}
static void
@ -2166,7 +2171,7 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
if (ct || cb) {
const unsigned int nH = (ct ? 1u : 0u) + (cb ? 1u : 0u);
alloc_vao_buffer(vao_idx, (size_t)(sizeof(GPUCell) * nH * columns), padding_cell_data_buffer, GPU_UPDATE_OFTEN);
alloc_vao_buffer(vao_idx, (size_t)(sizeof(uint8_t) * nH * columns), padding_selection_buffer, GPU_UPDATE_OFTEN);
alloc_vao_buffer(vao_idx, selection_stride * nH * columns, padding_selection_buffer, GPU_UPDATE_OFTEN);
unsigned int sidx = 0u;
if (ct) {
@ -2180,10 +2185,10 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
copy_vao_buffer_region(
vao_idx,
selection_buffer,
(size_t)(sizeof(uint8_t) * top_row * columns),
selection_stride * top_row * columns,
padding_selection_buffer,
(size_t)(sizeof(uint8_t) * sidx * columns),
(size_t)(columns * sizeof(uint8_t)));
selection_stride * sidx * columns,
selection_stride * columns);
sidx++;
}
if (cb) {
@ -2197,10 +2202,10 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
copy_vao_buffer_region(
vao_idx,
selection_buffer,
(size_t)(sizeof(uint8_t) * bottom_row * columns),
selection_stride * bottom_row * columns,
padding_selection_buffer,
(size_t)(sizeof(uint8_t) * sidx * columns),
(size_t)(columns * sizeof(uint8_t)));
selection_stride * sidx * columns,
selection_stride * columns);
}
// Strip-0 is top (or bottom when only bottom exists); strip-1 is bottom.
@ -2228,7 +2233,7 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
const unsigned int nV = (cl ? 1u : 0u) + (cr ? 1u : 0u);
// VLA sizes: lines is bounded by the window height / cell height (~<500).
GPUCell gathered_cells[2 * lines];
uint8_t gathered_sel[2 * lines];
uint8_t gathered_sel[2 * lines * selection_stride];
const unsigned int strip0_col = cl ? 0u : (columns - 1u);
const unsigned int strip1_col = columns - 1u;
@ -2241,18 +2246,20 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
unmap_vao_buffer(vao_idx, cell_data_buffer);
const uint8_t *sel = (const uint8_t *)map_vao_buffer_for_reading(vao_idx, selection_buffer);
#define SEL(idx) (sel + selection_stride * (idx))
for (unsigned int i = 0; i < lines; i++) {
gathered_sel[i] = sel[(top_row + i) * columns + strip0_col];
if (nV == 2u) gathered_sel[lines + i] = sel[(top_row + i) * columns + strip1_col];
memcpy(gathered_sel + selection_stride * i, SEL((top_row + i) * columns + strip0_col), selection_stride);
if (nV == 2u) memcpy(gathered_sel + selection_stride * (lines + i), SEL((top_row + i) * columns + strip1_col), selection_stride);
}
#undef SEL
unmap_vao_buffer(vao_idx, selection_buffer);
void *dst = alloc_and_map_vao_buffer(vao_idx, (size_t)(sizeof(GPUCell) * nV * lines), padding_cell_data_buffer);
memcpy(dst, gathered_cells, sizeof(GPUCell) * nV * lines);
unmap_vao_buffer(vao_idx, padding_cell_data_buffer);
dst = alloc_and_map_vao_buffer(vao_idx, (size_t)(sizeof(uint8_t) * nV * lines), padding_selection_buffer);
memcpy(dst, gathered_sel, sizeof(uint8_t) * nV * lines);
dst = alloc_and_map_vao_buffer(vao_idx, selection_stride * nV * lines, padding_selection_buffer);
memcpy(dst, gathered_sel, selection_stride * nV * lines);
unmap_vao_buffer(vao_idx, padding_selection_buffer);
const unsigned int base0 = top_row * columns + (cl ? 0u : (columns - 1u));
@ -3877,6 +3884,7 @@ initialize_gpu(void) {
GPUCapabilities caps;
gpu_get_capabilities(&caps);
global_state.supports_framebuffer_srgb = caps.supports_srgb_output;
selection_stride = MAX((size_t)1u, (size_t)caps.vertex_input_alignment);
query_gpu_texture_limits();
}