Look up cell styles in the shaders

Upload only the sprite index and the style index and mark of each cell,
8 bytes instead of 20, and have the cell and padding shaders get the
style from a per screen texture holding the style table. Styles are
uploaded incrementally, all of them when the table is garbage collected.

Also only build the attributes and sprites of cells when rendering
lines, as nothing else is used. Together these bring the CPU cost of
preparing cells for the GPU back to what it was before styles were
interned.
This commit is contained in:
Kovid Goyal 2026-11-08 08:34:20 +05:30
parent 0b74124dce
commit 2cd4ad9aed
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
9 changed files with 172 additions and 86 deletions

View file

@ -2086,7 +2086,12 @@ expanded_line_for_render(const Line *line) {
render_scratch.cells = n;
render_scratch.capacity = line->xnum;
}
expand_cells_for_gpu(line_style_table(line), line->cpu_cells, line->render_cells, line->xnum, render_scratch.cells);
// Only the attributes (for font selection) and sprites are used when rendering
const StyleTable *t = line_style_table(line);
for (index_type i = 0; i < line->xnum; i++) {
render_scratch.cells[i] = (GPUCell){
.attrs = style_table_get(t, line->cpu_cells[i].style_idx)->attrs, .sprite_idx = line->render_cells ? line->render_cells[i].sprite_idx : 0};
}
return render_scratch.cells;
}
@ -2323,18 +2328,16 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor,
&& num_spaces < MAX_NUM_EXTRA_GLYPHS_PUA && num_spaces + 1 < desired_cells) {
num_spaces++;
// We have a private use char followed by space(s), render it as a multi-cell ligature.
GPUCell *space_cell = gpu_cells + i + num_spaces;
// Ensure the space cell uses the foreground color from the PUA cell.
// This is needed because there are applications like
// Powerline that use PUA+space with different foreground colors
// for the space and the PUA. See for example: https://github.com/kovidgoyal/kitty/issues/467
space_cell->fg = gpu_cell->fg;
space_cell->decoration_fg = gpu_cell->decoration_fg;
// The style is also changed in the CPUCell as GPUCells are rebuilt from it when uploading
// The colors are changed in the style of the cell, as the GPU gets colors from it
CPUCell *space_cpu_cell = line->cpu_cells + i + num_spaces;
const Style *pua_style = style_table_get(style_table, cpu_cell->style_idx);
Style st = *style_table_get(style_table, space_cpu_cell->style_idx);
st.fg = gpu_cell->fg;
st.decoration_fg = gpu_cell->decoration_fg;
st.fg = pua_style->fg;
st.decoration_fg = pua_style->decoration_fg;
space_cpu_cell->style_idx = style_table_intern(style_table, &st);
}
if (num_spaces) {

View file

@ -26,8 +26,16 @@ typedef struct {
CellAttrs attrs;
} GPUCell;
static_assert(sizeof(GPUCell) == 20, "Fix the ordering of GPUCell");
// GPUCell is the format of cell data uploaded to the GPU, it is built from the
// CPUCell, the Style of the cell and the RenderCell of the cell.
// GPUCell is used when rendering lines, see render_line()
// The data for a cell uploaded to the GPU. The shaders get the style of the
// cell from a texture holding the StyleTable of the screen.
typedef struct CellGPUData {
sprite_index sprite_idx;
uint32_t style_and_mark; // style_idx << CELL_GPU_STYLE_SHIFT | mark
} CellGPUData;
static_assert(sizeof(CellGPUData) == 8, "Fix the ordering of CellGPUData");
#define CELL_GPU_STYLE_SHIFT 2u
// Data computed when rendering a cell. The style of a cell is stored in its CPUCell.
typedef struct RenderCell {
@ -256,21 +264,12 @@ remap_style_idx_range(StyleTable *dest, const StyleTable *src, CPUCell *c, index
for (index_type i = 0; i < num; i++) c[i].style_idx = style_table_intern_cached(dest, style_table_get(src, c[i].style_idx), &cache);
}
static inline GPUCell
gpu_cell_for(const Style *st, const RenderCell *r) {
GPUCell ans = {.fg = st->fg, .bg = st->bg, .decoration_fg = st->decoration_fg, .attrs = st->attrs};
if (r) {
ans.sprite_idx = r->sprite_idx;
ans.attrs.mark = r->mark;
}
return ans;
}
static inline void
expand_cells_for_gpu(const StyleTable *t, const CPUCell *c, const RenderCell *r, index_type num, GPUCell *dest) {
cells_for_gpu(const CPUCell *c, const RenderCell *r, index_type num, CellGPUData *dest) {
// Build the cell data uploaded to the GPU. dest may be write combined memory so it is only written to, sequentially.
static_assert(MARK_MASK < (1u << CELL_GPU_STYLE_SHIFT), "Increase CELL_GPU_STYLE_SHIFT");
for (index_type i = 0; i < num; i++) {
const GPUCell g = gpu_cell_for(style_table_get(t, c[i].style_idx), r ? r + i : NULL);
memcpy(dest + i, &g, sizeof(g));
const CellGPUData d = {.sprite_idx = r[i].sprite_idx, .style_and_mark = (c[i].style_idx << CELL_GPU_STYLE_SHIFT) | r[i].mark};
memcpy(dest + i, &d, sizeof(d));
}
}

View file

@ -29,6 +29,7 @@
#include "charsets.h"
#include "lineops.h"
#include "hyperlink.h"
#include "gpu.h"
#include <structmember.h>
#include <limits.h>
#include <sys/types.h>
@ -1000,6 +1001,7 @@ dealloc(Screen *self) {
PyMem_Free(self->main_tabstops);
Py_CLEAR(self->paused_rendering.linebuf);
Py_CLEAR(self->paused_rendering.grman);
if (self->style_texture.texture) free_texture(&self->style_texture.texture);
free(self->selections.items);
free(self->url_ranges.items);
free(self->paused_rendering.url_ranges.items);
@ -4484,13 +4486,13 @@ get_line_edge_colors(Screen *self, color_type *left, color_type *right) {
static void
update_line_data(Line *line, unsigned int dest_y, uint8_t *data) {
GPUCell *dest = (GPUCell *)(data + sizeof(GPUCell) * dest_y * line->xnum);
expand_cells_for_gpu(line_style_table(line), line->cpu_cells, line->render_cells, line->xnum, dest);
CellGPUData *dest = (CellGPUData *)(data + sizeof(CellGPUData) * dest_y * line->xnum);
cells_for_gpu(line->cpu_cells, line->render_cells, line->xnum, dest);
}
static void
update_line_data_blank(unsigned xnum, unsigned int dest_y, uint8_t *data) {
const size_t sz = xnum * sizeof(GPUCell);
const size_t sz = xnum * sizeof(CellGPUData);
memset(data + sz * dest_y, 0, sz);
}
@ -5793,8 +5795,8 @@ render_overlay_line(Screen *self, Line *line, FONTS_DATA_HANDLE fonts_data) {
static void
update_overlay_line_data(Screen *self, uint8_t *data) {
const int render_row_offset = pixel_scroll_enabled(self);
const size_t base = sizeof(GPUCell) * (self->overlay_line.ynum + self->scrolled_by + render_row_offset) * self->columns;
expand_cells_for_gpu(self->style_table, self->overlay_line.cpu_cells, self->overlay_line.render_cells, self->columns, (GPUCell *)(data + base));
const size_t base = sizeof(CellGPUData) * (self->overlay_line.ynum + self->scrolled_by + render_row_offset) * self->columns;
cells_for_gpu(self->overlay_line.cpu_cells, self->overlay_line.render_cells, self->columns, (CellGPUData *)(data + base));
}
// }}}

View file

@ -153,6 +153,11 @@ typedef struct {
TextCache *text_cache;
StyleTable *style_table; // owned by text_cache
uint32_t hyperlink_style_idx_cache;
// The texture holding the style table for the shaders, see sync_style_texture() in shaders.c
struct {
gpu_texture_t texture;
uint32_t width, height, count, generation;
} style_texture;
LineBuf *linebuf, *main_linebuf, *alt_linebuf;
GraphicsManager *grman, *main_grman, *alt_grman;
HistoryBuf *historybuf;

View file

@ -459,8 +459,7 @@ init_cell_program(void) {
if (aloc != expected && aloc != -1) fatal("The attribute location for %s is %d != %d in program: %d", #name, aloc, expected, p); \
}
for (int p = CELL_PROGRAM; p < CELL_PROGRAM_SENTINEL; p++) {
C(p, colors, 0);
C(p, sprite_idx, 1);
C(p, cell_input, 0);
C(p, is_selected, 2);
C(p, decorations_sprite_map, 3);
}
@ -472,8 +471,7 @@ init_cell_program(void) {
int aloc = attrib_location(PADDING_PROGRAM, #name); \
if (aloc != expected && aloc != -1) fatal("The attribute location for %s is %d != %d in the padding program", #name, aloc, expected); \
}
C(colors, 0);
C(sprite_idx, 1);
C(cell_input, 0);
C(is_selected, 2);
#undef C
// The sprite textures are bound once for all cell programs, see bind_sprite_map()
@ -931,11 +929,9 @@ create_cell_vao(void) {
#define A(name, size, dtype, offset, stride) \
add_attribute_to_vao( \
vao_idx, program_attribute_location(CELL_PROGRAM, name), /*size=*/size, /*dtype=*/dtype, /*stride=*/stride, /*offset=*/offset, /*divisor=*/1);
#define A1(name, size, dtype, offset) A(name, size, dtype, offsetof(GPUCell, offset), sizeof(GPUCell))
add_buffer_to_vao(vao_idx, GPU_BUFFER_VERTEX);
A1("sprite_idx", 2, GPU_ATTRIB_U32, sprite_idx);
A1("colors", 3, GPU_ATTRIB_U32, fg);
A("cell_input", 2, GPU_ATTRIB_U32, 0, sizeof(CellGPUData));
add_buffer_to_vao(vao_idx, GPU_BUFFER_VERTEX);
A("is_selected", 1, GPU_ATTRIB_U8, 0, (uint32_t)selection_stride);
@ -1226,6 +1222,70 @@ cell_update_uniform_block(
return default_bg;
}
// Style table texture {{{
// The cell shaders get the style of each cell from a texture holding the style
// table of its screen. Each Style is four R32UI texels, fg, bg, decoration_fg
// and attrs, in the same layout as the C struct, so styles are uploaded
// directly from the table. The texture width is a multiple of four, so the
// texels of a style are always in a single row.
#define TEXELS_PER_STYLE (sizeof(Style) / sizeof(uint32_t))
static_assert(sizeof(Style) == 4 * sizeof(uint32_t), "Fix TEXELS_PER_STYLE and the shaders");
static void
upload_style_texels(gpu_texture_t texture, uint32_t width, const uint32_t *texels, uint32_t start, uint32_t limit) {
// Upload the texels in [start, limit) as a partial first row, full rows and a partial last row
while (start < limit) {
const uint32_t y = start / width, x = start % width;
uint32_t w, h;
if (x || limit - start < width) {
w = MIN(width - x, limit - start);
h = 1;
} else {
w = width;
h = (limit - start) / width;
}
const GPUPixelData pixels = {.layout = GPU_PIXELS_R32UI, .row_alignment = 4, .data = texels + start};
gpu_texture_upload(texture, x, y, 0, w, h, &pixels);
start += w * h;
}
}
static void
sync_style_texture(Screen *screen) {
// Upload styles added to the table since the last upload, or all of them
// if the table was garbage collected since then
const StyleTable *t = screen->style_table;
__typeof__(screen->style_texture) *st = &screen->style_texture;
bool upload_all = st->generation != t->generation || !st->texture;
const uint32_t needed = t->count * TEXELS_PER_STYLE;
if (!st->texture || needed > st->width * st->height) {
const uint32_t width = (uint32_t)MIN(4096, max_texture_size) & ~(uint32_t)(TEXELS_PER_STYLE - 1);
const uint32_t max_texels = MAX_NUM_OF_STYLES * TEXELS_PER_STYLE;
const uint32_t capacity = MIN(MAX(needed * 2, 1024u * TEXELS_PER_STYLE), max_texels);
const uint32_t height = (capacity + width - 1) / width;
if ((int32_t)height > max_texture_size) fatal("Max texture size too small for the style table");
const GPUTextureDesc desc = {
.kind = GPU_TEXTURE_2D, .format = GPU_FORMAT_R32UI, .width = width, .height = height, .filter = GPU_FILTER_NEAREST, .wrap = GPU_WRAP_CLAMP_TO_EDGE};
if (st->texture) {
if (!gpu_texture_reset(st->texture, &desc, NULL)) fatal("Failed to re-allocate the style table texture with error: %s", gpu_last_error());
} else st->texture = create_texture_or_die(&desc, NULL);
st->width = width;
st->height = height;
upload_all = true;
}
const uint32_t start = upload_all ? 0 : st->count * TEXELS_PER_STYLE;
upload_style_texels(st->texture, st->width, (const uint32_t *)t->styles, start, needed);
st->count = t->count;
st->generation = t->generation;
}
static void
bind_style_texture(int program, const Screen *screen) {
uint32_t slot;
if (screen->style_texture.texture && try_program_texture_slot(program, "cell_styles", &slot)) gpu_bind_texture(slot, screen->style_texture.texture);
}
// }}}
static bool
cell_prepare_to_render(ssize_t vao_idx, Screen *screen, FONTS_DATA_HANDLE fonts_data) {
size_t sz;
@ -1239,11 +1299,14 @@ cell_prepare_to_render(ssize_t vao_idx, Screen *screen, FONTS_DATA_HANDLE fonts_
bool cursor_pos_changed = cursor->x != screen->last_rendered.cursor.x || cursor->y != screen->last_rendered.cursor.y;
bool disable_ligatures = screen->disable_ligatures == DISABLE_LIGATURES_CURSOR;
bool screen_resized = screen->last_rendered.columns != screen->columns || screen->last_rendered.lines != screen->lines;
// Garbage collecting the style table changes the style indices of cells, so all cell data must be uploaded again
const bool styles_remapped = screen->style_texture.texture && screen->style_texture.generation != screen->style_table->generation;
if (styles_remapped) screen->paused_rendering.cell_data_updated = false;
#define update_cell_data \
{ \
const unsigned int render_lines = render_lines_for_screen(screen); \
sz = sizeof(GPUCell) * render_lines * screen->columns; \
sz = sizeof(CellGPUData) * render_lines * screen->columns; \
address = alloc_and_map_vao_buffer_for_full_rewrite(vao_idx, sz, cell_data_buffer); \
screen_update_cell_data(screen, address, fonts_data, disable_ligatures && cursor_pos_changed); \
unmap_vao_buffer(vao_idx, cell_data_buffer); \
@ -1253,8 +1316,16 @@ cell_prepare_to_render(ssize_t vao_idx, Screen *screen, FONTS_DATA_HANDLE fonts_
if (screen->paused_rendering.expires_at) {
if (!screen->paused_rendering.cell_data_updated) update_cell_data;
} else if (screen->reload_all_gpu_data || screen->scroll_changed || screen->is_dirty || screen_resized || (disable_ligatures && cursor_pos_changed))
} else if (
screen->reload_all_gpu_data || screen->scroll_changed || screen->is_dirty || screen_resized || styles_remapped ||
(disable_ligatures && cursor_pos_changed))
update_cell_data;
// After updating cell data as rendering lines can add styles
if (!screen->style_texture.texture || screen->style_texture.count != screen->style_table->count ||
screen->style_texture.generation != screen->style_table->generation) {
sync_style_texture(screen);
changed = true;
}
#define update_selection_data \
{ \
@ -2062,6 +2133,7 @@ enum { DRAW_NEITHER_BG = 0, DRAW_DEFAULT_BG = 1, DRAW_NON_DEFAULT_BG = 2, DRAW_B
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);
bind_style_texture(program, ui->screen);
CELL_BUFFERS;
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);
@ -2083,19 +2155,9 @@ configure_cell_vao_attributes(ssize_t vao_idx, unsigned int base_cell, unsigned
// layout after padding draws (base_cell=0, cell_step=1). The VAO must be
// bound before calling this.
CELL_BUFFERS;
const uint32_t cell_stride = (uint32_t)(cell_step * sizeof(GPUCell));
const size_t cell_base = (size_t)base_cell * sizeof(GPUCell);
set_vao_attribute(
vao_idx,
cell_data_buffer,
program_attribute_location(CELL_PROGRAM, "sprite_idx"),
2,
GPU_ATTRIB_U32,
cell_stride,
cell_base + offsetof(GPUCell, sprite_idx),
1);
set_vao_attribute(
vao_idx, cell_data_buffer, program_attribute_location(CELL_PROGRAM, "colors"), 3, GPU_ATTRIB_U32, cell_stride, cell_base + offsetof(GPUCell, fg), 1);
const uint32_t cell_stride = (uint32_t)(cell_step * sizeof(CellGPUData));
const size_t cell_base = (size_t)base_cell * sizeof(CellGPUData);
set_vao_attribute(vao_idx, cell_data_buffer, program_attribute_location(CELL_PROGRAM, "cell_input"), 2, GPU_ATTRIB_U32, cell_stride, cell_base, 1);
set_vao_attribute(
vao_idx,
selection_buffer,
@ -2110,20 +2172,10 @@ configure_cell_vao_attributes(ssize_t vao_idx, unsigned int base_cell, unsigned
static void
configure_padding_vao_attributes(ssize_t vao_idx) {
// Point the instanced cell attributes at the dedicated padding buffers
// (base=0, stride=sizeof(GPUCell)), which are pre-packed before each
// (base=0, stride=sizeof(CellGPUData)), which are pre-packed before each
// combined draw. The VAO must be bound before calling this.
CELL_BUFFERS;
set_vao_attribute(
vao_idx,
padding_cell_data_buffer,
program_attribute_location(CELL_PROGRAM, "sprite_idx"),
2,
GPU_ATTRIB_U32,
sizeof(GPUCell),
offsetof(GPUCell, sprite_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_cell_data_buffer, program_attribute_location(CELL_PROGRAM, "cell_input"), 2, GPU_ATTRIB_U32, sizeof(CellGPUData), 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);
}
@ -2155,6 +2207,7 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
const float dx = 2.f * cw / fbw, dy = 2.f * ch / fbh;
bind_program(PADDING_PROGRAM);
bind_style_texture(PADDING_PROGRAM, ui->screen);
bind_vertex_array(vao_idx);
CELL_BUFFERS;
bind_vao_uniform_buffer(vao_idx, uniform_buffer, program_uniform_block(PADDING_PROGRAM, "CellRenderData").slot);
@ -2170,7 +2223,7 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
// Cell data is packed contiguously via GPU-side copies (no CPU round-trip).
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(CellGPUData) * nH * columns), padding_cell_data_buffer, GPU_UPDATE_OFTEN);
alloc_vao_buffer(vao_idx, selection_stride * nH * columns, padding_selection_buffer, GPU_UPDATE_OFTEN);
unsigned int sidx = 0u;
@ -2178,10 +2231,10 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
copy_vao_buffer_region(
vao_idx,
cell_data_buffer,
(size_t)(sizeof(GPUCell) * top_row * columns),
(size_t)(sizeof(CellGPUData) * top_row * columns),
padding_cell_data_buffer,
(size_t)(sizeof(GPUCell) * sidx * columns),
(size_t)(columns * sizeof(GPUCell)));
(size_t)(sizeof(CellGPUData) * sidx * columns),
(size_t)(columns * sizeof(CellGPUData)));
copy_vao_buffer_region(
vao_idx,
selection_buffer,
@ -2195,10 +2248,10 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
copy_vao_buffer_region(
vao_idx,
cell_data_buffer,
(size_t)(sizeof(GPUCell) * bottom_row * columns),
(size_t)(sizeof(CellGPUData) * bottom_row * columns),
padding_cell_data_buffer,
(size_t)(sizeof(GPUCell) * sidx * columns),
(size_t)(sizeof(GPUCell) * columns));
(size_t)(sizeof(CellGPUData) * sidx * columns),
(size_t)(sizeof(CellGPUData) * columns));
copy_vao_buffer_region(
vao_idx,
selection_buffer,
@ -2232,13 +2285,13 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
if (cl || cr) {
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];
CellGPUData gathered_cells[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;
const GPUCell *cells = (const GPUCell *)map_vao_buffer_for_reading(vao_idx, cell_data_buffer);
const CellGPUData *cells = (const CellGPUData *)map_vao_buffer_for_reading(vao_idx, cell_data_buffer);
for (unsigned int i = 0; i < lines; i++) {
gathered_cells[i] = cells[(top_row + i) * columns + strip0_col];
if (nV == 2u) gathered_cells[lines + i] = cells[(top_row + i) * columns + strip1_col];
@ -2254,8 +2307,8 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
#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);
void *dst = alloc_and_map_vao_buffer(vao_idx, (size_t)(sizeof(CellGPUData) * nV * lines), padding_cell_data_buffer);
memcpy(dst, gathered_cells, sizeof(CellGPUData) * nV * lines);
unmap_vao_buffer(vao_idx, padding_cell_data_buffer);
dst = alloc_and_map_vao_buffer(vao_idx, selection_stride * nV * lines, padding_selection_buffer);

View file

@ -56,6 +56,34 @@ public struct ColorTable {
public ConstantBuffer<ColorTable> ctb;
#define color_table ctb.color_table
// The style table of the screen, each style is four texels: fg, bg,
// decoration_fg and attrs. The texture width is a multiple of four.
[[vk::binding(11)]]
uniform Sampler2D<uint> cell_styles; // a single channel R32UI texture
// The data uploaded per cell: the sprite index and the style index shifted
// left by CELL_STYLE_SHIFT with the mark in the low bits
static const uint CELL_STYLE_SHIFT = 2u;
public struct CellInput {
public uint3 colors; // fg, bg, decoration_fg
public uint2 sprite_idx; // sprite index, text attributes including the mark
};
public CellInput
load_cell(uint2 cell_data) {
CellInput ans;
uint style_idx = cell_data[1] >> CELL_STYLE_SHIFT;
uint mark = cell_data[1] & ((1u << CELL_STYLE_SHIFT) - 1u);
int width = texture_size(cell_styles)[0];
int texel = int(style_idx) * 4;
int y = texel / width;
int x = texel - y * width;
ans.colors = uint3(texel_fetch(cell_styles, int2(x, y)), texel_fetch(cell_styles, int2(x + 1, y)), texel_fetch(cell_styles, int2(x + 2, y)));
ans.sprite_idx = uint2(cell_data[0], texel_fetch(cell_styles, int2(x + 3, y)) | (mark << MARK_SHIFT));
return ans;
}
// The gamma LUT is a compile time constant and, unlike every other block
// here, it is emitted into the default uniform block for OpenGL rather than
// into a UBO, see GLSL_LOOSE_UNIFORM_BLOCKS in slang.py for why.

View file

@ -151,13 +151,11 @@ override_foreground_color(float3 over, float3 under, float colored_sprite) {
[shader("vertex")]
VertexOutput
vertex_main(
[[vk::location(0)]] uint3 colors,
[[vk::location(1)]] uint2 sprite_idx,
[[vk::location(2)]] uint is_selected,
uint vertex_id: SV_VertexID,
uint instance_id: SV_InstanceID) {
vertex_main([[vk::location(0)]] uint2 cell_input, [[vk::location(2)]] uint is_selected, uint vertex_id: SV_VertexID, uint instance_id: SV_InstanceID) {
VertexOutput vo;
const CellInput ci = load_cell(cell_input);
const uint3 colors = ci.colors;
const uint2 sprite_idx = ci.sprite_idx;
BackgroundOutput bo = compute_background(colors, sprite_idx, is_selected, instance_id, vertex_id);
vo.position = bo.position;

View file

@ -11,7 +11,7 @@ import background;
// This shader colors the compensatory window padding (the thin strips that
// appear when the window size is not an exact multiple of the cell size) so
// that each strip matches the background of its neighboring cell. It re-uses
// the cell VAO (colors/sprite_idx/is_selected instanced attributes) and the
// the cell VAO (cell_input/is_selected instanced attributes) and the
// background module's padding_background_premul() to compute the color.
//
// Each draw call covers one or two parallel strips (e.g. top+bottom or
@ -64,12 +64,7 @@ struct VertexOutput {
[shader("vertex")]
VertexOutput
vertex_main(
[[vk::location(0)]] uint3 colors,
[[vk::location(1)]] uint2 sprite_idx,
[[vk::location(2)]] uint is_selected,
uint vertex_id: SV_VertexID,
uint instance_id: SV_InstanceID) {
vertex_main([[vk::location(0)]] uint2 cell_input, [[vk::location(2)]] uint is_selected, uint vertex_id: SV_VertexID, uint instance_id: SV_InstanceID) {
VertexOutput vo;
// Branchless strip selector: 0.0 for strip 0, 1.0 for strip 1.
@ -82,7 +77,8 @@ vertex_main(
uint real_id = uint(fbase) + strip_i * uniforms.instance_step;
float2 chosen_across = lerp(uniforms.across, uniforms.across2, strip_f);
vo.color_premul = padding_background_premul(colors, sprite_idx, is_selected, real_id);
const CellInput ci = load_cell(cell_input);
vo.color_premul = padding_background_premul(ci.colors, ci.sprite_idx, is_selected, real_id);
uint2 p = pad_pos_map[vertex_id];
float along_lo = uniforms.along_start + float(strip_i) * uniforms.along_step;

View file

@ -1502,6 +1502,8 @@ SHADER_BINDING_SLOTS = MappingProxyType(
'GammaLUT': 8,
'Colors': 9,
'KittyCustomShaderData': 10,
# textures added after the uniform blocks
'cell_styles': 11,
}
)