diff --git a/gpu/api.h b/gpu/api.h index 08dfa2cfe..c525df4cb 100644 --- a/gpu/api.h +++ b/gpu/api.h @@ -251,6 +251,44 @@ typedef struct GPUBinding { uint32_t slot; } GPUBinding; +// Parameter blocks hold the small, frequently changing, parameters of a +// pipeline: the uniform parameters of each entry point, and uniform blocks +// whose contents are constant for the lifetime of the pipeline. Their layout +// comes from the reflection data. kitty writes their contents into memory it +// owns, laid out as described here, and passes it to set_params() before +// drawing. Values persist with the pipeline until set again. Drivers are free +// to implement them as they see fit, for example as loose uniforms in OpenGL, +// push constants in Vulkan or set*Bytes() in Metal. +typedef enum { + GPU_PARAM_FLOAT = 1, // 32 bit float + GPU_PARAM_INT, // 32 bit signed integer + GPU_PARAM_UINT, // 32 bit unsigned integer + GPU_PARAM_BOOL, // 32 bit, zero is false +} GPUParamScalar; + +typedef struct GPUParamField { + const char *name; // name in the Slang source + uint32_t offset; // byte offset from the start of the block + GPUParamScalar scalar; + uint32_t components; // number of components in each element, 1 to 4 + uint32_t count; // number of elements, greater than one for arrays + uint32_t stride; // bytes between consecutive elements +} GPUParamField; + +typedef struct GPUParamBlock { + // For the uniform parameters of an entry point name is NULL and stage is + // the stage of the entry point. For uniform blocks name is the name of the + // type of the block in the Slang source, stage is zero and slot is the + // binding slot of the block, which drivers that implement it as a + // uniform buffer must use. + const char *name; + GPUShaderStage stage; + int32_t slot; + uint32_t size; // size in bytes of the block + const GPUParamField *fields; + size_t num_fields; +} GPUParamBlock; + 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 @@ -336,14 +374,18 @@ typedef struct GPUDriverAPI { // Pipelines {{{ // Create a pipeline from shader sources compiled for the shader target of - // 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. + // the driver. param_blocks describe its parameter blocks, see + // GPUParamBlock, and are identified by their index in this array. + // 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 GPUParamBlock *param_blocks, + size_t num_param_blocks, const GPUBinding *bindings, size_t num_bindings, const GPUNativeName *native_names, @@ -352,21 +394,15 @@ typedef struct GPUDriverAPI { // Make a pipeline current, GPU_NULL_HANDLE for none void (*use_pipeline)(void *driver, gpu_pipeline_t pipeline); // All names below are names in the Slang source. - // The slot used to set the named loose uniform with the set_uniform_*() - // functions, or -1 if the pipeline does not use that uniform. For arrays, - // name must not include a subscript. - int32_t (*pipeline_uniform_slot)(void *driver, gpu_pipeline_t pipeline, const char *name); // 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); // 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 - // the number of components in each element (1 to 4) and count the number - // of elements. Slots that are negative are ignored. - void (*set_uniform_floats)(void *driver, int32_t slot, uint32_t components, uint32_t count, const float *values); - void (*set_uniform_ints)(void *driver, int32_t slot, uint32_t components, uint32_t count, const int32_t *values); - void (*set_uniform_uints)(void *driver, int32_t slot, uint32_t components, uint32_t count, const uint32_t *values); + // Set the contents of a parameter block of the pipeline, which must be + // the current pipeline. block is the index of the block in the array + // passed to pipeline_create() and data is laid out as described there. + void (*set_params)(void *driver, gpu_pipeline_t pipeline, uint32_t block, const void *data, size_t size); // }}} // Vertex layouts {{{ diff --git a/gpu/opengl/driver.c b/gpu/opengl/driver.c index 157f8221c..63755810d 100644 --- a/gpu/opengl/driver.c +++ b/gpu/opengl/driver.c @@ -19,6 +19,7 @@ #define UNUSED __attribute__((unused)) #define arraysz(x) (sizeof(x) / sizeof(x[0])) #define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define MAX(x, y) ((x) > (y) ? (x) : (y)) // All texture management (creation, uploads, copies) is done using this unit, // so that it never disturbs the textures bound to the units used for drawing. @@ -37,10 +38,20 @@ typedef struct Buffer { size_t size; } Buffer; -typedef struct Uniform { - char name[256]; - GLint location; -} Uniform; +typedef struct ParamField { + GLint location; // -1 if the GLSL compiler optimized the uniform away + uint32_t offset, components, count, stride; + GPUParamScalar scalar; +} ParamField; + +typedef struct ParamBlock { + uint32_t size; + ParamField *fields; + size_t num_fields; + // The contents last uploaded, so that only changed fields are uploaded + uint8_t *uploaded; + bool has_uploaded; +} ParamBlock; typedef struct NativeName { char *name, *native_name; @@ -48,8 +59,8 @@ typedef struct NativeName { typedef struct Pipeline { GLuint id; - Uniform *uniforms; - GLint num_uniforms; + ParamBlock *param_blocks; + size_t num_param_blocks; // Maps Slang names to GLSL identifiers NativeName *native_names; size_t num_native_names; @@ -659,7 +670,11 @@ pipeline_destroy(void *driver UNUSED, gpu_pipeline_t pipeline) { Pipeline *p = AS_PIPELINE(pipeline); if (!p) return; if (p->id) glDeleteProgram(p->id); - free(p->uniforms); + for (size_t i = 0; i < p->num_param_blocks; i++) { + free(p->param_blocks[i].fields); + free(p->param_blocks[i].uploaded); + } + free(p->param_blocks); for (size_t i = 0; i < p->num_native_names; i++) { free(p->native_names[i].name); free(p->native_names[i].native_name); @@ -668,27 +683,6 @@ pipeline_destroy(void *driver UNUSED, gpu_pipeline_t pipeline) { free(p); } -static bool -read_active_uniforms(Driver *d, Pipeline *p) { - glGetProgramiv(p->id, GL_ACTIVE_UNIFORMS, &p->num_uniforms); - if (p->num_uniforms <= 0) return true; - p->uniforms = calloc(p->num_uniforms, sizeof(Uniform)); - if (!p->uniforms) { - set_error(d, "Out of memory reading the uniforms of a GPU program"); - return false; - } - for (GLint i = 0; i < p->num_uniforms; i++) { - Uniform *u = p->uniforms + i; - GLint size; - GLenum type; - glGetActiveUniform(p->id, (GLuint)i, sizeof(u->name), NULL, &size, &type, u->name); - char *l = strchr(u->name, '['); - if (l) *l = 0; - u->location = glGetUniformLocation(p->id, u->name); - } - return true; -} - static char * dup_string(const char *src) { const size_t n = strlen(src) + 1; @@ -751,11 +745,49 @@ apply_bindings(const Pipeline *p, const GPUBinding *bindings, size_t num_binding glUseProgram((GLuint)previous_program); } +// Parameter blocks are implemented as loose uniforms, which are part of the +// program state and so persist with the program +static bool +create_param_blocks(Driver *d, Pipeline *p, const GPUParamBlock *blocks, size_t num_blocks) { + if (!num_blocks) return true; + if (!(p->param_blocks = calloc(num_blocks, sizeof(ParamBlock)))) goto oom; + p->num_param_blocks = num_blocks; + for (size_t b = 0; b < num_blocks; b++) { + const GPUParamBlock *src = blocks + b; + ParamBlock *pb = p->param_blocks + b; + pb->size = src->size; + if (!(pb->uploaded = calloc(1, MAX(1u, src->size)))) goto oom; + if (src->num_fields && !(pb->fields = calloc(src->num_fields, sizeof(ParamField)))) goto oom; + pb->num_fields = src->num_fields; + for (size_t i = 0; i < src->num_fields; i++) { + const GPUParamField *f = src->fields + i; + ParamField *pf = pb->fields + i; + if (f->components < 1 || f->components > 4 || !f->count || + (uint64_t)f->offset + (uint64_t)(f->count - 1) * f->stride + 4u * f->components > src->size) { + set_error(d, "The parameter %s does not fit in its parameter block", f->name); + return false; + } + pf->offset = f->offset; + pf->components = f->components; + pf->count = f->count; + pf->stride = f->stride; + pf->scalar = f->scalar; + pf->location = glGetUniformLocation(p->id, native_name(p, f->name)); + } + } + return true; +oom: + set_error(d, "Out of memory allocating a GPU program"); + return false; +} + static gpu_pipeline_t pipeline_create( void *driver, const GPUShaderSource *sources, size_t num_sources, + const GPUParamBlock *param_blocks, + size_t num_param_blocks, const GPUBinding *bindings, size_t num_bindings, const GPUNativeName *native_names, @@ -791,7 +823,7 @@ pipeline_create( pipeline_destroy(d, AS_HANDLE(p)); return GPU_NULL_HANDLE; } - if (!read_active_uniforms(d, p)) { + if (!create_param_blocks(d, p, param_blocks, num_param_blocks)) { pipeline_destroy(d, AS_HANDLE(p)); return GPU_NULL_HANDLE; } @@ -805,18 +837,6 @@ use_pipeline(void *driver UNUSED, gpu_pipeline_t pipeline) { glUseProgram(p ? p->id : 0); } -static int32_t -pipeline_uniform_slot(void *driver UNUSED, gpu_pipeline_t pipeline, const char *name) { - const Pipeline *p = AS_PIPELINE(pipeline); - name = native_name(p, name); - const size_t n = strlen(name) + 1; - for (GLint i = 0; i < p->num_uniforms; i++) { - const Uniform *u = p->uniforms + i; - if (strncmp(u->name, name, n) == 0) return u->location; - } - return -1; -} - static bool pipeline_uniform_block(void *driver, gpu_pipeline_t pipeline, const char *name, GPUUniformBlockInfo *ans) { const Pipeline *p = AS_PIPELINE(pipeline); @@ -840,36 +860,56 @@ pipeline_attribute_location(void *driver UNUSED, gpu_pipeline_t pipeline, const } static void -set_uniform_floats(void *driver UNUSED, int32_t slot, uint32_t components, uint32_t count, const float *v) { - if (slot < 0) return; - switch (components) { - case 1: glUniform1fv(slot, count, v); break; - case 2: glUniform2fv(slot, count, v); break; - case 3: glUniform3fv(slot, count, v); break; - case 4: glUniform4fv(slot, count, v); break; +upload_param(const ParamField *f, const uint8_t *data) { + // Uniform arrays are tightly packed, unlike arrays in parameter blocks + uint32_t packed[4 * 256]; + const uint8_t *src = data + f->offset; + const size_t elem = 4u * f->components; + const void *values = src; + GLsizei count = (GLsizei)f->count; + if (f->count > 1 && f->stride != elem) { + count = (GLsizei)MIN((size_t)f->count, sizeof(packed) / elem); + for (GLsizei i = 0; i < count; i++) memcpy((uint8_t *)packed + i * elem, src + (size_t)i * f->stride, elem); + values = packed; } +#define S(suffix, type) \ + switch (f->components) { \ + case 1: glUniform1##suffix##v(f->location, count, (const type *)values); break; \ + case 2: glUniform2##suffix##v(f->location, count, (const type *)values); break; \ + case 3: glUniform3##suffix##v(f->location, count, (const type *)values); break; \ + case 4: glUniform4##suffix##v(f->location, count, (const type *)values); break; \ + } + switch (f->scalar) { + case GPU_PARAM_FLOAT: S(f, GLfloat); break; + case GPU_PARAM_INT: + case GPU_PARAM_BOOL: S(i, GLint); break; + case GPU_PARAM_UINT: S(ui, GLuint); break; + } +#undef S } static void -set_uniform_ints(void *driver UNUSED, int32_t slot, uint32_t components, uint32_t count, const int32_t *v) { - if (slot < 0) return; - switch (components) { - case 1: glUniform1iv(slot, count, v); break; - case 2: glUniform2iv(slot, count, v); break; - case 3: glUniform3iv(slot, count, v); break; - case 4: glUniform4iv(slot, count, v); break; +set_params(void *driver, gpu_pipeline_t pipeline, uint32_t block, const void *data, size_t size) { + Pipeline *p = AS_PIPELINE(pipeline); + if (block >= p->num_param_blocks) { + log_error(driver, "Invalid parameter block index: %u", block); + return; } -} - -static void -set_uniform_uints(void *driver UNUSED, int32_t slot, uint32_t components, uint32_t count, const uint32_t *v) { - if (slot < 0) return; - switch (components) { - case 1: glUniform1uiv(slot, count, v); break; - case 2: glUniform2uiv(slot, count, v); break; - case 3: glUniform3uiv(slot, count, v); break; - case 4: glUniform4uiv(slot, count, v); break; + ParamBlock *pb = p->param_blocks + block; + if (size < pb->size) { + log_error(driver, "The data for parameter block %u is %zu bytes instead of %u", block, size, pb->size); + return; } + const uint8_t *d = data; + for (size_t i = 0; i < pb->num_fields; i++) { + const ParamField *f = pb->fields + i; + if (f->location < 0) continue; + const size_t extent = (size_t)(f->count - 1) * f->stride + 4u * f->components; + if (pb->has_uploaded && memcmp(pb->uploaded + f->offset, d + f->offset, extent) == 0) continue; + upload_param(f, d); + memcpy(pb->uploaded + f->offset, d + f->offset, extent); + } + pb->has_uploaded = true; } // }}} @@ -1296,12 +1336,9 @@ kitty_gpu_driver_load(uint32_t api_version, const GPUDriverInitParams *params, G F(pipeline_create), F(pipeline_destroy), F(use_pipeline), - F(pipeline_uniform_slot), F(pipeline_uniform_block), F(pipeline_attribute_location), - F(set_uniform_floats), - F(set_uniform_ints), - F(set_uniform_uints), + F(set_params), F(vertex_layout_create), F(vertex_layout_destroy), diff --git a/kitty/gpu.c b/kitty/gpu.c index e917198d4..c27d3ffcc 100644 --- a/kitty/gpu.c +++ b/kitty/gpu.c @@ -152,8 +152,11 @@ disable_scissor(void) { gpu_set_scissor(false, 0, 0, 0, 0); } +static void flush_params(void); + void draw_quad(bool blend, unsigned instance_count) { + flush_params(); gpu_draw_quad(instance_count, blend ? GPU_BLEND_PREMULTIPLIED_ALPHA : GPU_BLEND_NONE); } @@ -232,36 +235,80 @@ save_texture_as_png(gpu_texture_t texture, const char *filename) { // }}} // Programs {{{ -typedef enum { PROGRAM_UNIFORM, PROGRAM_ATTRIBUTE, PROGRAM_BLOCK, PROGRAM_ARRAY, PROGRAM_TEXTURE } ProgramMetadataKind; +typedef enum { PROGRAM_PARAM, PROGRAM_ATTRIBUTE, PROGRAM_BLOCK, PROGRAM_ARRAY, PROGRAM_TEXTURE } ProgramMetadataKind; typedef struct ProgramMetadataEntry { ProgramMetadataKind kind; - bool missing_location_logged; union { - int32_t location; + int32_t location; // the param ref index for PROGRAM_PARAM, the location for PROGRAM_ATTRIBUTE and the slot for PROGRAM_TEXTURE UniformBlock block; ArrayInformation array; }; } ProgramMetadataEntry; +// Parameter blocks, see GPUParamBlock {{{ +typedef struct ParamField { + char *name; + uint32_t offset, components, count, stride; + GPUParamScalar scalar; + bool type_error_logged; +} ParamField; + +typedef struct ParamBlock { + char *name; // NULL for the uniform parameters of an entry point + GPUShaderStage stage; + int32_t slot; + uint32_t size; + uint8_t *data; // the contents, laid out as described by the fields + bool dirty; // the contents have changed since they were last passed to the driver + ParamField *fields; + size_t num_fields; +} ParamBlock; + +// The fields with a given name, at most one per stage +typedef struct ParamRef { + uint8_t num; + struct { + uint16_t block, field; + } items[2]; +} ParamRef; + +typedef struct ProgramParams { + ParamBlock *blocks; + size_t num_blocks; + ParamRef *refs; + size_t num_refs; +} ProgramParams; +// }}} + static gpu_pipeline_t programs[MAX_GPU_PROGRAMS] = {0}; +static ProgramParams program_params[MAX_GPU_PROGRAMS] = {0}; +static int current_program = -1; + +static void +free_program_params(ProgramParams *pp) { + for (size_t b = 0; b < pp->num_blocks; b++) { + ParamBlock *pb = pp->blocks + b; + for (size_t i = 0; i < pb->num_fields; i++) free(pb->fields[i].name); + free(pb->fields); + free(pb->name); + free(pb->data); + } + free(pp->blocks); + free(pp->refs); + zero_at_ptr(pp); +} 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); } -// Program metadata (uniform/attribute locations and UBO/array info, looked up by name) {{{ +// Program metadata (parameter, attribute and texture locations and UBO/array info, looked up by name) {{{ #define NAME program_metadata_map #define KEY_TY const char * #define VAL_TY ProgramMetadataEntry @@ -272,6 +319,7 @@ static bool program_layouts_initialized[MAX_GPU_PROGRAMS] = {0}; static void clear_program_layout(int program) { + free_program_params(program_params + 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); @@ -284,6 +332,14 @@ free_program_layouts(void) { for (size_t i = 0; i < arraysz(program_layouts); i++) clear_program_layout((int)i); } +void +free_program(int program) { + if (current_program == program) current_program = -1; + if (programs[program]) gpu_pipeline_destroy(programs[program]); + programs[program] = GPU_NULL_HANDLE; + clear_program_layout(program); +} + 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); @@ -298,10 +354,25 @@ program_metadata_entry(int program, const char *name, ProgramMetadataKind expect return &itr.data->val; } +static bool +find_program_metadata(int program, const char *name, ProgramMetadataKind expected, ProgramMetadataEntry **ans) { + 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 != expected) return false; + *ans = &itr.data->val; + return true; +} +// }}} + +// Parsing the metadata {{{ // Fetches a required item from a dict in the program metadata, setting a // Python exception if it is missing or of the wrong type static PyObject * metadata_item(PyObject *d, const char *key, PyTypeObject *type) { + if (!PyDict_Check(d)) { + PyErr_Format(PyExc_TypeError, "Looking up %s in shader program metadata that is not a dict", key); + return NULL; + } PyObject *ans = PyDict_GetItemString(d, key); if (!ans) { PyErr_Format(PyExc_KeyError, "The shader program metadata has no %s entry", key); @@ -315,7 +386,7 @@ metadata_item(PyObject *d, const char *key, PyTypeObject *type) { } static bool -metadata_uint(PyObject *d, const char *key, int32_t *ans) { +metadata_int(PyObject *d, const char *key, int32_t *ans) { PyObject *v = metadata_item(d, key, &PyLong_Type); if (!v) return false; long val = PyLong_AsLong(v); @@ -324,10 +395,129 @@ metadata_uint(PyObject *d, const char *key, int32_t *ans) { return true; } -// The metadata is generated from the reflection data produced by the Slang -// compiler, see program_metadata() in shaders/slang.py. It is backend neutral, -// all names are names from the Slang source. -bool +static bool +metadata_uint(PyObject *d, const char *key, uint32_t *ans) { + int32_t v; + if (!metadata_int(d, key, &v)) return false; + if (v < 0) { + PyErr_Format(PyExc_ValueError, "The %s entry in the shader program metadata is negative", key); + return false; + } + *ans = (uint32_t)v; + return true; +} + +static const char * +metadata_str(PyObject *d, const char *key) { + PyObject *v = metadata_item(d, key, &PyUnicode_Type); + return v ? PyUnicode_AsUTF8(v) : NULL; +} + +static bool +parse_stage(const char *s, GPUShaderStage *ans) { + if (strcmp(s, "vertex") == 0) *ans = GPU_SHADER_VERTEX; + else if (strcmp(s, "fragment") == 0) *ans = GPU_SHADER_FRAGMENT; + else if (!s[0]) *ans = 0; + else { + PyErr_Format(PyExc_ValueError, "Unknown shader stage: %s", s); + return false; + } + return true; +} + +static bool +parse_scalar(const char *s, GPUParamScalar *ans) { + if (strcmp(s, "float") == 0) *ans = GPU_PARAM_FLOAT; + else if (strcmp(s, "int") == 0) *ans = GPU_PARAM_INT; + else if (strcmp(s, "uint") == 0) *ans = GPU_PARAM_UINT; + else if (strcmp(s, "bool") == 0) *ans = GPU_PARAM_BOOL; + else { + PyErr_Format(PyExc_ValueError, "Unknown shader parameter type: %s", s); + return false; + } + return true; +} + +static bool +parse_param_blocks(PyObject *metadata, ProgramParams *pp) { + PyObject *blocks = metadata_item(metadata, "param_blocks", &PyList_Type); + if (!blocks) return false; + const size_t num_blocks = (size_t)PyList_GET_SIZE(blocks); + if (!num_blocks) return true; + if (!(pp->blocks = calloc(num_blocks, sizeof(ParamBlock)))) goto oom; + pp->num_blocks = num_blocks; + for (size_t b = 0; b < num_blocks; b++) { + PyObject *src = PyList_GET_ITEM(blocks, b); + ParamBlock *pb = pp->blocks + b; + const char *name = metadata_str(src, "name"), *stage = metadata_str(src, "stage"); + if (!name || !stage || !parse_stage(stage, &pb->stage)) return false; + if (name[0] && !(pb->name = strdup(name))) goto oom; + if (!metadata_int(src, "slot", &pb->slot) || !metadata_uint(src, "size", &pb->size)) return false; + if (!(pb->data = calloc(1, MAX(1u, pb->size)))) goto oom; + PyObject *fields = metadata_item(src, "fields", &PyList_Type); + if (!fields) return false; + pb->num_fields = (size_t)PyList_GET_SIZE(fields); + if (pb->num_fields && !(pb->fields = calloc(pb->num_fields, sizeof(ParamField)))) goto oom; + for (size_t i = 0; i < pb->num_fields; i++) { + PyObject *fsrc = PyList_GET_ITEM(fields, i); + ParamField *f = pb->fields + i; + const char *fname = metadata_str(fsrc, "name"), *scalar = metadata_str(fsrc, "scalar"); + if (!fname || !scalar || !parse_scalar(scalar, &f->scalar)) return false; + if (!(f->name = strdup(fname))) goto oom; + if (!metadata_uint(fsrc, "offset", &f->offset) || !metadata_uint(fsrc, "components", &f->components) || !metadata_uint(fsrc, "count", &f->count) || + !metadata_uint(fsrc, "stride", &f->stride)) + return false; + if (f->components < 1 || f->components > 4 || !f->count || + (uint64_t)f->offset + (uint64_t)(f->count - 1) * f->stride + 4u * f->components > pb->size) { + PyErr_Format(PyExc_ValueError, "The shader parameter %s does not fit in its parameter block", fname); + return false; + } + } + } + return true; +oom: + PyErr_NoMemory(); + return false; +} + +// Map parameter names to the fields that have that name +static bool +index_params(int program, ProgramParams *pp) { + size_t total = 0; + for (size_t b = 0; b < pp->num_blocks; b++) total += pp->blocks[b].num_fields; + if (!total) return true; + if (total > UINT16_MAX || pp->num_blocks > UINT16_MAX) { + PyErr_SetString(PyExc_ValueError, "Too many shader parameters"); + return false; + } + if (!(pp->refs = calloc(total, sizeof(ParamRef)))) { + PyErr_NoMemory(); + return false; + } + for (size_t b = 0; b < pp->num_blocks; b++) { + for (size_t i = 0; i < pp->blocks[b].num_fields; i++) { + const char *name = pp->blocks[b].fields[i].name; + ProgramMetadataEntry *e; + ParamRef *ref; + if (find_program_metadata(program, name, PROGRAM_PARAM, &e)) ref = pp->refs + e->location; + else { + ref = pp->refs + pp->num_refs; + insert_program_metadata(program, name, (ProgramMetadataEntry){.kind = PROGRAM_PARAM, .location = (int32_t)pp->num_refs}); + pp->num_refs++; + } + if (ref->num >= arraysz(ref->items)) { + PyErr_Format(PyExc_ValueError, "The shader parameter %s is used in too many parameter blocks", name); + return false; + } + ref->items[ref->num].block = (uint16_t)b; + ref->items[ref->num].field = (uint16_t)i; + ref->num++; + } + } + return true; +} + +static bool set_program_layout(int program, PyObject *metadata) { clear_program_layout(program); vt_init(program_layouts + program); @@ -346,15 +536,6 @@ set_program_layout(int program, PyObject *metadata) { insert_program_metadata(program, PyUnicode_AsUTF8(key), e); } - PyObject *uniforms = metadata_item(metadata, "uniforms", &PyList_Type); - if (!uniforms) return false; - for (Py_ssize_t i = 0; i < PyList_GET_SIZE(uniforms); i++) { - const char *name = PyUnicode_AsUTF8(PyList_GET_ITEM(uniforms, i)); - if (!name) return false; - ProgramMetadataEntry e = {.kind = PROGRAM_UNIFORM, .location = gpu_pipeline_uniform_slot(pipeline, name)}; - insert_program_metadata(program, name, e); - } - PyObject *textures = metadata_item(metadata, "textures", &PyDict_Type); if (!textures) return false; pos = 0; @@ -370,7 +551,7 @@ set_program_layout(int program, PyObject *metadata) { while (PyDict_Next(blocks, &pos, &key, &val)) { const char *block_name = PyUnicode_AsUTF8(key); if (!block_name) return false; - int32_t size, slot; + uint32_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)) { @@ -379,17 +560,17 @@ set_program_layout(int program, PyObject *metadata) { } // The layout of the block is taken from the reflection data, so // verify that it matches what the driver actually uses - if ((uint32_t)size != info.size) { + if (size != info.size) { PyErr_Format( PyExc_ValueError, - "The uniform block: %s in GPU program: %d has size %u according to the GPU driver but %d according to the shader reflection data", + "The uniform block: %s in GPU program: %d has size %u according to the GPU driver but %u according to the shader reflection data", block_name, program, info.size, size); return false; } - ProgramMetadataEntry be = {.kind = PROGRAM_BLOCK, .block = {.index = (int32_t)info.index, .size = size, .slot = (uint32_t)slot}}; + ProgramMetadataEntry be = {.kind = PROGRAM_BLOCK, .block = {.index = (int32_t)info.index, .size = (int32_t)size, .slot = slot}}; insert_program_metadata(program, block_name, be); PyObject *arrays = metadata_item(val, "arrays", &PyDict_Type); @@ -399,36 +580,200 @@ set_program_layout(int program, PyObject *metadata) { while (PyDict_Next(arrays, &apos, &akey, &aval)) { const char *array_name = PyUnicode_AsUTF8(akey); if (!array_name) return false; - ArrayInformation ai; - if (!metadata_uint(aval, "offset", &ai.offset) || !metadata_uint(aval, "stride", &ai.stride) || !metadata_uint(aval, "count", &ai.size)) - return false; - ProgramMetadataEntry ae = {.kind = PROGRAM_ARRAY, .array = ai}; + int32_t offset, stride, count; + if (!metadata_int(aval, "offset", &offset) || !metadata_int(aval, "stride", &stride) || !metadata_int(aval, "count", &count)) return false; + ProgramMetadataEntry ae = {.kind = PROGRAM_ARRAY, .array = {.offset = offset, .stride = stride, .size = count}}; insert_program_metadata(program, array_name, ae); } } + return parse_param_blocks(metadata, program_params + program) && index_params(program, program_params + program); +} +// }}} + +bool +compile_gpu_program(int program, const char *vertex, size_t vertex_size, const char *fragment, size_t fragment_size, PyObject *metadata) { + free_program(program); + // The shaders must have been compiled for the target of the loaded driver + const char *target = metadata_str(metadata, "target"); + if (!target) return false; + if (strcmp(target, gpu_shader_target()) != 0) { + PyErr_Format(PyExc_ValueError, "The shaders were compiled for the target: %s but the GPU driver needs: %s", target, gpu_shader_target()); + return false; + } + GPUShaderLanguage language; + if (strncmp(target, "glsl-", 5) == 0) language = GPU_SHADER_LANGUAGE_GLSL; + else if (strncmp(target, "spirv-", 6) == 0) language = GPU_SHADER_LANGUAGE_SPIRV; + else if (strncmp(target, "msl-", 4) == 0) language = GPU_SHADER_LANGUAGE_MSL; + else { + PyErr_Format(PyExc_ValueError, "Unknown shader target: %s", target); + return false; + } + // The strings referenced below remain valid as long as the metadata does + PyObject *native_names = metadata_item(metadata, "native_names", &PyDict_Type); + PyObject *textures = metadata_item(metadata, "textures", &PyDict_Type); + PyObject *blocks = metadata_item(metadata, "blocks", &PyDict_Type); + if (!native_names || !textures || !blocks) return false; + RAII_ALLOC(GPUNativeName, names, calloc(MAX(1, PyDict_GET_SIZE(native_names)), sizeof(GPUNativeName))); + RAII_ALLOC(GPUBinding, bindings, calloc(MAX(1, PyDict_GET_SIZE(textures) + PyDict_GET_SIZE(blocks)), sizeof(GPUBinding))); + if (!names || !bindings) { + PyErr_NoMemory(); + return false; + } + size_t num_names = 0, num_bindings = 0; + PyObject *key, *val; + Py_ssize_t pos = 0; + while (PyDict_Next(native_names, &pos, &key, &val)) { + if (!(names[num_names].name = PyUnicode_AsUTF8(key)) || !(names[num_names].native_name = PyUnicode_AsUTF8(val))) return false; + num_names++; + } + 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 false; + b->slot = (uint32_t)PyLong_AsUnsignedLong(val); + if (PyErr_Occurred()) return false; + } + 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)) || !metadata_uint(val, "slot", &b->slot)) return false; + } + // Parse the parameter blocks first, to pass their layout to the driver + ProgramParams pp = {0}; + if (!parse_param_blocks(metadata, &pp)) { + free_program_params(&pp); + return false; + } + size_t total_fields = 0; + for (size_t b = 0; b < pp.num_blocks; b++) total_fields += pp.blocks[b].num_fields; + RAII_ALLOC(GPUParamBlock, gpb, calloc(MAX(1u, pp.num_blocks), sizeof(GPUParamBlock))); + RAII_ALLOC(GPUParamField, gpf, calloc(MAX(1u, total_fields), sizeof(GPUParamField))); + if (!gpb || !gpf) { + free_program_params(&pp); + PyErr_NoMemory(); + return false; + } + for (size_t b = 0, fi = 0; b < pp.num_blocks; b++) { + const ParamBlock *pb = pp.blocks + b; + gpb[b] = (GPUParamBlock){.name = pb->name, .stage = pb->stage, .slot = pb->slot, .size = pb->size, .fields = gpf + fi, .num_fields = pb->num_fields}; + for (size_t i = 0; i < pb->num_fields; i++, fi++) { + const ParamField *f = pb->fields + i; + gpf[fi] = + (GPUParamField){.name = f->name, .offset = f->offset, .scalar = f->scalar, .components = f->components, .count = f->count, .stride = f->stride}; + } + } + const GPUShaderSource sources[2] = { + {.stage = GPU_SHADER_VERTEX, .language = language, .code = vertex, .size = vertex_size}, + {.stage = GPU_SHADER_FRAGMENT, .language = language, .code = fragment, .size = fragment_size}, + }; + programs[program] = gpu_pipeline_create(sources, arraysz(sources), gpb, pp.num_blocks, bindings, num_bindings, names, num_names); + free_program_params(&pp); + if (!programs[program]) { + PyErr_SetString(PyExc_ValueError, gpu_last_error()); + return false; + } + if (!set_program_layout(program, metadata)) { + free_program(program); + return false; + } return true; } +// Parameters {{{ 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; +try_program_param(int program, const char *name) { + ProgramMetadataEntry *e; + if (!find_program_metadata(program, name, PROGRAM_PARAM, &e)) return -1; + return (int32_t)(((uint32_t)program << 16) | (uint32_t)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; +program_param(int program, const char *name) { + int32_t ans = try_program_param(program, name); + if (ans < 0) log_error("The GPU program: %d has no parameter named: %s", program, name); + return ans; } +static void +write_param(int32_t handle, GPUParamScalar scalar, uint32_t components, uint32_t count, const void *values) { + if (handle < 0) return; + const int program = handle >> 16; + const uint32_t r = handle & 0xffff; + ProgramParams *pp = program_params + program; + if (r >= pp->num_refs) return; + const ParamRef *ref = pp->refs + r; + const size_t elem = 4u * components; + for (uint8_t i = 0; i < ref->num; i++) { + ParamBlock *pb = pp->blocks + ref->items[i].block; + ParamField *f = pb->fields + ref->items[i].field; + const bool scalar_ok = f->scalar == scalar || (f->scalar == GPU_PARAM_BOOL && scalar == GPU_PARAM_INT); + if (!scalar_ok || f->components != components || count > f->count) { + if (!f->type_error_logged) { + f->type_error_logged = true; + log_error("Setting the shader parameter: %s in GPU program: %d with a value of the wrong type", f->name, program); + } + continue; + } + for (uint32_t e = 0; e < count; e++) memcpy(pb->data + f->offset + (size_t)e * f->stride, (const uint8_t *)values + e * elem, elem); + pb->dirty = true; + } +} + +void +param_floats(int32_t handle, uint32_t components, uint32_t count, const float *values) { + write_param(handle, GPU_PARAM_FLOAT, components, count, values); +} + +void +param_1f(int32_t handle, float a) { + write_param(handle, GPU_PARAM_FLOAT, 1, 1, &a); +} + +void +param_2f(int32_t handle, float a, float b) { + const float v[2] = {a, b}; + write_param(handle, GPU_PARAM_FLOAT, 2, 1, v); +} + +void +param_3f(int32_t handle, float a, float b, float c) { + const float v[3] = {a, b, c}; + write_param(handle, GPU_PARAM_FLOAT, 3, 1, v); +} + +void +param_4f(int32_t handle, float a, float b, float c, float d) { + const float v[4] = {a, b, c, d}; + write_param(handle, GPU_PARAM_FLOAT, 4, 1, v); +} + +void +param_1i(int32_t handle, int32_t a) { + write_param(handle, GPU_PARAM_INT, 1, 1, &a); +} + +void +param_1ui(int32_t handle, uint32_t a) { + write_param(handle, GPU_PARAM_UINT, 1, 1, &a); +} + +// Pass changed parameters of the current program to the driver, done just before drawing +static void +flush_params(void) { + if (current_program < 0 || !programs[current_program]) return; + ProgramParams *pp = program_params + current_program; + for (size_t b = 0; b < pp->num_blocks; b++) { + ParamBlock *pb = pp->blocks + b; + if (pb->dirty) { + gpu_set_params(programs[current_program], (uint32_t)b, pb->data, pb->size); + pb->dirty = false; + } + } +} +// }}} + int32_t program_attribute_location(int program, const char *name) { return program_metadata_entry(program, name, PROGRAM_ATTRIBUTE)->location; @@ -443,7 +788,6 @@ ArrayInformation program_uniform_array(int program, const char *name) { return program_metadata_entry(program, name, PROGRAM_ARRAY)->array; } -// }}} uint32_t program_texture_slot(int program, const char *name) { @@ -452,20 +796,21 @@ program_texture_slot(int program, const char *name) { 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; + ProgramMetadataEntry *e; + if (!find_program_metadata(program, name, PROGRAM_TEXTURE, &e)) return false; + *slot = (uint32_t)e->location; return true; } void bind_program(int program) { + current_program = program; gpu_use_pipeline(programs[program]); } void unbind_program(void) { + current_program = -1; gpu_use_pipeline(GPU_NULL_HANDLE); } // }}} diff --git a/kitty/gpu.h b/kitty/gpu.h index 6564d4d63..ef9333b22 100644 --- a/kitty/gpu.h +++ b/kitty/gpu.h @@ -193,11 +193,18 @@ static inline gpu_pipeline_t gpu_pipeline_create( const GPUShaderSource *sources, size_t num_sources, + const GPUParamBlock *param_blocks, + size_t num_param_blocks, 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); + return A.pipeline_create(D, sources, num_sources, param_blocks, num_param_blocks, bindings, num_bindings, native_names, num_native_names); +} + +static inline void +gpu_set_params(gpu_pipeline_t pipeline, uint32_t block, const void *data, size_t size) { + A.set_params(D, pipeline, block, data, size); } static inline void @@ -210,11 +217,6 @@ 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); @@ -225,21 +227,6 @@ 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); @@ -336,41 +323,6 @@ gpu_draw_quad(uint32_t instance_count, GPUBlendMode blend) { #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; @@ -409,14 +361,29 @@ typedef struct 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); +// Free the pipeline of the program, if any +void free_program(int program); +// Create the pipeline for program from shader sources compiled for the shader +// target of the driver and their metadata, see program_metadata() in +// shaders/slang.py. Returns false with a Python exception set on failure. +bool compile_gpu_program(int program, const char *vertex, size_t vertex_size, const char *fragment, size_t fragment_size, PyObject *metadata); int32_t attrib_location(int program, const char *name); -// Returns false with a Python exception set on failure -bool 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); +// Parameters, the uniform parameters of the entry points and the pipeline +// constant blocks of a program. Values are written into memory owned by +// kitty and passed to the driver before the next draw call using the +// program. A handle identifies a parameter of a program, negative handles +// are ignored by the setters. +int32_t program_param(int program, const char *name); +// As above, but returns -1 without logging an error if the program has no such parameter +int32_t try_program_param(int program, const char *name); +void param_floats(int32_t handle, uint32_t components, uint32_t count, const float *values); +void param_1f(int32_t handle, float a); +void param_2f(int32_t handle, float a, float b); +void param_3f(int32_t handle, float a, float b, float c); +void param_4f(int32_t handle, float a, float b, float c, float d); +void param_1i(int32_t handle, int32_t a); +void param_1ui(int32_t handle, uint32_t a); 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); diff --git a/kitty/shaders.c b/kitty/shaders.c index b2d36920e..6905911c4 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -87,17 +87,17 @@ srgb_color(uint8_t color) { static void color_vec3(int32_t location, color_type color) { - gpu_uniform3f(location, srgb_lut[(color >> 16) & 0xFF], srgb_lut[(color >> 8) & 0xFF], srgb_lut[color & 0xFF]); + param_3f(location, srgb_lut[(color >> 16) & 0xFF], srgb_lut[(color >> 8) & 0xFF], srgb_lut[color & 0xFF]); } static void color_vec4_premult(int32_t location, color_type color, float alpha) { - gpu_uniform4f(location, srgb_lut[(color >> 16) & 0xFF] * alpha, srgb_lut[(color >> 8) & 0xFF] * alpha, srgb_lut[color & 0xFF] * alpha, alpha); + param_4f(location, srgb_lut[(color >> 16) & 0xFF] * alpha, srgb_lut[(color >> 8) & 0xFF] * alpha, srgb_lut[color & 0xFF] * alpha, alpha); } static void color_vec4(int32_t location, color_type color, float alpha) { - gpu_uniform4f(location, srgb_lut[(color >> 16) & 0xFF], srgb_lut[(color >> 8) & 0xFF], srgb_lut[color & 0xFF], alpha); + param_4f(location, srgb_lut[(color >> 16) & 0xFF], srgb_lut[(color >> 8) & 0xFF], srgb_lut[color & 0xFF], alpha); } // Write a color into a std140 float4 whose fourth component is unused. Uses @@ -345,13 +345,13 @@ draw_rounded_rect( color_type srgb_outer, float outer_alpha) { bind_program(ROUNDED_RECT_PROGRAM); - color_vec4(program_uniform_location(ROUNDED_RECT_PROGRAM, "color"), srgb_color, 1.f); - color_vec4(program_uniform_location(ROUNDED_RECT_PROGRAM, "background_color"), srgb_background, bg_alpha); - color_vec4(program_uniform_location(ROUNDED_RECT_PROGRAM, "outer_color"), srgb_outer, outer_alpha); + color_vec4(program_param(ROUNDED_RECT_PROGRAM, "color"), srgb_color, 1.f); + color_vec4(program_param(ROUNDED_RECT_PROGRAM, "background_color"), srgb_background, bg_alpha); + color_vec4(program_param(ROUNDED_RECT_PROGRAM, "outer_color"), srgb_outer, outer_alpha); // y co-ord has to be changed to co-ord system with origin at bottom left float y = (float)framebuffer_height - (float)(rect.top + rect.height); - gpu_uniform4f(program_uniform_location(ROUNDED_RECT_PROGRAM, "rect"), rect.left, y, rect.width, rect.height); - gpu_uniform2f(program_uniform_location(ROUNDED_RECT_PROGRAM, "params"), thickness, corner_radius_px); + param_4f(program_param(ROUNDED_RECT_PROGRAM, "rect"), rect.left, y, rect.width, rect.height); + param_2f(program_param(ROUNDED_RECT_PROGRAM, "params"), thickness, corner_radius_px); save_viewport_using_top_left_origin(rect.left, rect.top, rect.width, rect.height, framebuffer_height); draw_quad(true, 0); restore_viewport(); @@ -434,11 +434,11 @@ write_uint_array_to_ubo(void *dest, const uint32_t *src, size_t count, const Arr // See https://github.com/kovidgoyal/kitty/issues/10571 void upload_gamma_lut(void) { -#define U(program) \ - { \ - bind_program(program); \ - int32_t loc = program_uniform_location(program, "gamma_lut"); \ - if (loc >= 0) gpu_set_uniform_floats(loc, 1, arraysz(srgb_lut), srgb_lut); \ +#define U(program) \ + { \ + bind_program(program); \ + int32_t loc = program_param(program, "gamma_lut"); \ + if (loc >= 0) param_floats(loc, 1, arraysz(srgb_lut), srgb_lut); \ } for (int i = CELL_PROGRAM; i < CELL_PROGRAM_SENTINEL; i++) U(i); U(BORDERS_PROGRAM); @@ -635,8 +635,8 @@ init_custom_programs(void) { custom_shaders.count++; if (i == CUSTOM_END_PROGRAM) { bind_program(i); - int32_t group_loc = try_program_uniform_location(i, "group"); - if (group_loc >= 0) gpu_uniform1i(group_loc, 0); + int32_t group_loc = try_program_param(i, "group"); + if (group_loc >= 0) param_1i(group_loc, 0); else if (custom_shaders.end.num_groups > 1) { // Every group is dispatched by setting this uniform, so without it // all of them render as group zero, which for a multi pass pipeline @@ -1289,24 +1289,24 @@ cell_prepare_to_render(ssize_t vao_idx, Screen *screen, FONTS_DATA_HANDLE fonts_ static void draw_graphics(int program, ImageRenderData *data, uint32_t start, uint32_t count, float extra_alpha) { bind_program(program); - if (program != GRAPHICS_ALPHA_MASK_PROGRAM) gpu_uniform1f(program_uniform_location(program, "extra_alpha"), extra_alpha); - int32_t src_rect_loc = program_uniform_location(program, "src_rect"); - int32_t dest_rect_loc = program_uniform_location(program, "dest_rect"); + if (program != GRAPHICS_ALPHA_MASK_PROGRAM) param_1f(program_param(program, "extra_alpha"), extra_alpha); + int32_t src_rect_loc = program_param(program, "src_rect"); + int32_t dest_rect_loc = program_param(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 int32_t has_alpha_loc = is_indexed ? program_param(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(image_slot, group->texture_id); - if (has_alpha_loc > -1) gpu_uniform1ui(has_alpha_loc, group->indexed_px_sz == 2); + if (has_alpha_loc > -1) param_1ui(has_alpha_loc, group->indexed_px_sz == 2); if (group->group_count == 0) { i++; continue; } for (uint32_t k = 0; k < group->group_count; k++, i++) { ImageRenderData *rd = data + start + i; - gpu_uniform4f(src_rect_loc, rd->src_rect.left, rd->src_rect.top, rd->src_rect.right, rd->src_rect.bottom); - gpu_uniform4f(dest_rect_loc, rd->dest_rect.left, rd->dest_rect.top, rd->dest_rect.right, rd->dest_rect.bottom); + param_4f(src_rect_loc, rd->src_rect.left, rd->src_rect.top, rd->src_rect.right, rd->src_rect.bottom); + param_4f(dest_rect_loc, rd->dest_rect.left, rd->dest_rect.top, rd->dest_rect.right, rd->dest_rect.bottom); draw_quad(true, 0); } } @@ -1363,15 +1363,15 @@ set_cell_uniforms(bool force) { float text_gamma_adjustment = OPT(text_gamma_adjustment) < 0.01f ? 1.0f : 1.0f / OPT(text_gamma_adjustment); // 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) \ - { \ - int32_t loc = try_program_uniform_location(i, name); \ - if (loc >= 0) setter(loc, value); \ +#define set_optional(setter, name, value) \ + { \ + int32_t loc = try_program_param(i, name); \ + if (loc >= 0) setter(loc, value); \ } for (int i = CELL_PROGRAM; i < CELL_PROGRAM_SENTINEL; i++) { bind_program(i); - set_optional(gpu_uniform1f, "text_contrast", text_contrast); - set_optional(gpu_uniform1f, "text_gamma_adjustment", text_gamma_adjustment); + set_optional(param_1f, "text_contrast", text_contrast); + set_optional(param_1f, "text_gamma_adjustment", text_gamma_adjustment); } #undef set_optional constants_set = true; @@ -1412,9 +1412,9 @@ draw_visual_bell_flash(float intensity, const color_type flash) { #undef C #define C(x) (x * intensity * attenuation) if (max_channel > 0.45) attenuation = 0.6f; // light color - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "tint_color"), C(r), C(g), C(b), C(1)); + param_4f(program_param(TINT_PROGRAM, "tint_color"), C(r), C(g), C(b), C(1)); #undef C - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "edges"), -1, 1, 1, -1); + param_4f(program_param(TINT_PROGRAM, "edges"), -1, 1, 1, -1); draw_quad(true, 0); } @@ -1461,9 +1461,9 @@ draw_drag_preview_overlay(const UIRenderData *ui) { float a = intensity * 0.25f; color_type hint = get_flash_color(screen); #define C(shift) (srgb_color((hint >> shift) & 0xFF) * a) - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "tint_color"), C(16), C(8), C(0), a); + param_4f(program_param(TINT_PROGRAM, "tint_color"), C(16), C(8), C(0), a); #undef C - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "edges"), left, top, right, bottom); + param_4f(program_param(TINT_PROGRAM, "edges"), left, top, right, bottom); draw_quad(true, 0); } @@ -1704,8 +1704,8 @@ draw_window_number(const UIRenderData *ui) { ui->screen->color_profile->configured.highlight_bg, ui->screen->color_profile->overridden.default_fg, ui->screen->color_profile->configured.default_fg); - color_vec3(program_uniform_location(GRAPHICS_ALPHA_MASK_PROGRAM, "amask_fg"), digit_color); - gpu_uniform4f(program_uniform_location(GRAPHICS_ALPHA_MASK_PROGRAM, "amask_bg_premult"), 0.f, 0.f, 0.f, 0.f); + color_vec3(program_param(GRAPHICS_ALPHA_MASK_PROGRAM, "amask_fg"), digit_color); + param_4f(program_param(GRAPHICS_ALPHA_MASK_PROGRAM, "amask_bg_premult"), 0.f, 0.f, 0.f, 0.f); save_viewport_using_top_left_origin(ui->screen_left + letter_x, ui->screen_top + letter_y, lr.width_px, lr.height_px, ui->full_framebuffer_height); draw_graphics(GRAPHICS_ALPHA_MASK_PROGRAM, ird, 0, 1, 1.f); restore_viewport(); @@ -1718,7 +1718,7 @@ set_color_uniform_with_opacity(color_type color, float opacity) { float r = srgb_color((color >> 16) & 0xFF) * opacity; float g = srgb_color((color >> 8) & 0xFF) * opacity; float b = srgb_color(color & 0xFF) * opacity; - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "tint_color"), r, g, b, opacity); + param_4f(program_param(TINT_PROGRAM, "tint_color"), r, g, b, opacity); } static color_type @@ -1784,7 +1784,7 @@ draw_scrollbar(const UIRenderData *ui) { if (track_opacity > 0) { bind_program(TINT_PROGRAM); set_color_uniform_with_opacity(track_color, track_opacity); - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "edges"), -1.f, 1.f, 1.f, -1.f); + param_4f(program_param(TINT_PROGRAM, "edges"), -1.f, 1.f, 1.f, -1.f); draw_quad(true, 0); } @@ -1797,22 +1797,22 @@ draw_scrollbar(const UIRenderData *ui) { restore_viewport(); bind_program(ROUNDED_RECT_PROGRAM); - color_vec4(program_uniform_location(ROUNDED_RECT_PROGRAM, "color"), bar_color, opacity); - color_vec4(program_uniform_location(ROUNDED_RECT_PROGRAM, "background_color"), 0, 0.0f); - color_vec4(program_uniform_location(ROUNDED_RECT_PROGRAM, "outer_color"), 0, 0.0f); + color_vec4(program_param(ROUNDED_RECT_PROGRAM, "color"), bar_color, opacity); + color_vec4(program_param(ROUNDED_RECT_PROGRAM, "background_color"), 0, 0.0f); + color_vec4(program_param(ROUNDED_RECT_PROGRAM, "outer_color"), 0, 0.0f); float y = (float)ui->full_framebuffer_height - (float)(thumb_top_px + thumb_height_px); - gpu_uniform4f(program_uniform_location(ROUNDED_RECT_PROGRAM, "rect"), (float)scrollbar_left, y, (float)scrollbar_width_px, (float)thumb_height_px); + param_4f(program_param(ROUNDED_RECT_PROGRAM, "rect"), (float)scrollbar_left, y, (float)scrollbar_width_px, (float)thumb_height_px); float thickness = (float)MAX(scrollbar_width_px, thumb_height_px); - gpu_uniform2f(program_uniform_location(ROUNDED_RECT_PROGRAM, "params"), thickness, (float)scrollbar_radius); + param_2f(program_param(ROUNDED_RECT_PROGRAM, "params"), thickness, (float)scrollbar_radius); save_viewport_using_top_left_origin(scrollbar_left, thumb_top_px, scrollbar_width_px, thumb_height_px, ui->full_framebuffer_height); draw_quad(true, 0); restore_viewport(); } else { set_color_uniform_with_opacity(bar_color, opacity); - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "edges"), -1.f, thumb_top_gl, 1.f, thumb_bottom_gl); + param_4f(program_param(TINT_PROGRAM, "edges"), -1.f, thumb_top_gl, 1.f, thumb_bottom_gl); draw_quad(true, 0); restore_viewport(); } @@ -1864,15 +1864,15 @@ draw_progress_handle( if (bar_radius > 0) { bind_program(ROUNDED_RECT_PROGRAM); - color_vec4(program_uniform_location(ROUNDED_RECT_PROGRAM, "color"), bar_color, opacity); - color_vec4(program_uniform_location(ROUNDED_RECT_PROGRAM, "background_color"), 0, 0.0f); - color_vec4(program_uniform_location(ROUNDED_RECT_PROGRAM, "outer_color"), 0, 0.0f); + color_vec4(program_param(ROUNDED_RECT_PROGRAM, "color"), bar_color, opacity); + color_vec4(program_param(ROUNDED_RECT_PROGRAM, "background_color"), 0, 0.0f); + color_vec4(program_param(ROUNDED_RECT_PROGRAM, "outer_color"), 0, 0.0f); float y = (float)ui->full_framebuffer_height - (float)(handle_top + handle_h); - gpu_uniform4f(program_uniform_location(ROUNDED_RECT_PROGRAM, "rect"), (float)handle_left, y, (float)handle_w, (float)handle_h); + param_4f(program_param(ROUNDED_RECT_PROGRAM, "rect"), (float)handle_left, y, (float)handle_w, (float)handle_h); float thickness = (float)(is_horizontal ? handle_h : handle_w); - gpu_uniform2f(program_uniform_location(ROUNDED_RECT_PROGRAM, "params"), thickness, (float)bar_radius); + param_2f(program_param(ROUNDED_RECT_PROGRAM, "params"), thickness, (float)bar_radius); save_viewport_using_top_left_origin(handle_left, handle_top, handle_w, handle_h, ui->full_framebuffer_height); draw_quad(true, 0); @@ -1889,11 +1889,11 @@ draw_progress_handle( if (is_horizontal) { // edges: left, top, right, bottom save_viewport_using_top_left_origin(track_left, track_top, track_width, track_height, ui->full_framebuffer_height); - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "edges"), start_gl, 1.f, end_gl, -1.f); + param_4f(program_param(TINT_PROGRAM, "edges"), start_gl, 1.f, end_gl, -1.f); } else { save_viewport_using_top_left_origin(track_left, track_top, track_width, track_height, ui->full_framebuffer_height); // For vertical: start_gl is top in GL coords (inverted y), so bottom_gl = -end_gl, top_gl = -start_gl - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "edges"), -1.f, -start_gl, 1.f, -end_gl); + param_4f(program_param(TINT_PROGRAM, "edges"), -1.f, -start_gl, 1.f, -end_gl); } draw_quad(true, 0); restore_viewport(); @@ -1970,7 +1970,7 @@ draw_progress_bar(const UIRenderData *ui) { if (track_opacity > 0) { bind_program(TINT_PROGRAM); set_color_uniform_with_opacity(track_color, track_opacity); - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "edges"), -1.f, 1.f, 1.f, -1.f); + param_4f(program_param(TINT_PROGRAM, "edges"), -1.f, 1.f, 1.f, -1.f); draw_quad(true, 0); } restore_viewport(); @@ -2069,7 +2069,7 @@ call_cell_program(int program, const UIRenderData *ui, ssize_t vao_idx, bool for 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); - gpu_uniform1ui(program_uniform_location(program, "draw_bg_bitfield"), draw_bg_bitfield); + param_1ui(program_param(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); if (for_final_output) gpu_set_srgb_output(false); @@ -2167,7 +2167,7 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un // Point instanced attributes at the padding-specific buffers once for both draws. configure_padding_vao_attributes(vao_idx); -#define PL(x) program_uniform_location(PADDING_PROGRAM, #x) +#define PL(x) program_param(PADDING_PROGRAM, #x) // Horizontal combined draw: top strip (strip 0) + bottom strip (strip 1). // Cell data is packed contiguously via GPU-side copies (no CPU round-trip). @@ -2216,16 +2216,16 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un // the shader lerp is a no-op for all instances (strip_f is always 0.0). const unsigned int base0 = (ct ? top_row : bottom_row) * columns; const unsigned int base1 = bottom_row * columns; - gpu_uniform1ui(PL(is_horizontal), 1u); - gpu_uniform1ui(PL(along_count), columns); - gpu_uniform1ui(PL(instance_step), 1u); - gpu_uniform1ui(PL(base_instance), base0); - gpu_uniform1ui(PL(base_instance2), base1); - gpu_uniform2f(PL(across), ct ? NY(T - ct) : NY(B), ct ? NY(T) : NY(B + cb)); - gpu_uniform2f(PL(across2), NY(B), NY(B + cb)); - gpu_uniform1f(PL(along_start), NX(fL)); - gpu_uniform1f(PL(along_step), dx); - gpu_uniform2f(PL(along_clamp), NX(fL), NX(R)); + param_1ui(PL(is_horizontal), 1u); + param_1ui(PL(along_count), columns); + param_1ui(PL(instance_step), 1u); + param_1ui(PL(base_instance), base0); + param_1ui(PL(base_instance2), base1); + param_2f(PL(across), ct ? NY(T - ct) : NY(B), ct ? NY(T) : NY(B + cb)); + param_2f(PL(across2), NY(B), NY(B + cb)); + param_1f(PL(along_start), NX(fL)); + param_1f(PL(along_step), dx); + param_2f(PL(along_clamp), NX(fL), NX(R)); draw_quad(!for_final_output, nH * columns); } @@ -2265,16 +2265,16 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un const unsigned int base0 = top_row * columns + (cl ? 0u : (columns - 1u)); const unsigned int base1 = top_row * columns + (columns - 1u); - gpu_uniform1ui(PL(is_horizontal), 0u); - gpu_uniform1ui(PL(along_count), lines); - gpu_uniform1ui(PL(instance_step), columns); - gpu_uniform1ui(PL(base_instance), base0); - gpu_uniform1ui(PL(base_instance2), base1); - gpu_uniform2f(PL(across), cl ? NX(fL - cl) : NX(R), cl ? NX(fL) : NX(R + cr)); - gpu_uniform2f(PL(across2), NX(R), NX(R + cr)); - gpu_uniform1f(PL(along_start), NY(T)); - gpu_uniform1f(PL(along_step), -dy); - gpu_uniform2f(PL(along_clamp), NY(T - ct), NY(B + cb)); + param_1ui(PL(is_horizontal), 0u); + param_1ui(PL(along_count), lines); + param_1ui(PL(instance_step), columns); + param_1ui(PL(base_instance), base0); + param_1ui(PL(base_instance2), base1); + param_2f(PL(across), cl ? NX(fL - cl) : NX(R), cl ? NX(fL) : NX(R + cr)); + param_2f(PL(across2), NX(R), NX(R + cr)); + param_1f(PL(along_start), NY(T)); + param_1f(PL(along_step), -dy); + param_2f(PL(along_clamp), NY(T - ct), NY(B + cb)); draw_quad(!for_final_output, nV * lines); } @@ -2290,8 +2290,8 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un static void draw_tint(const UIRenderData *ui) { bind_program(TINT_PROGRAM); - color_vec4_premult(program_uniform_location(TINT_PROGRAM, "tint_color"), ui->background_color, OPT(background_tint)); - gpu_uniform4f(program_uniform_location(TINT_PROGRAM, "edges"), -1, 1, 1, -1); + color_vec4_premult(program_param(TINT_PROGRAM, "tint_color"), ui->background_color, OPT(background_tint)); + param_4f(program_param(TINT_PROGRAM, "edges"), -1, 1, 1, -1); draw_quad(true, 0); } @@ -2519,7 +2519,7 @@ draw_borders( const ArrayInformation border_colors_array = program_uniform_array(BORDERS_PROGRAM, "colors"); write_uint_array_to_ubo(colors_buf, colors, arraysz(colors), &border_colors_array); unmap_vao_buffer(shader_globals_vao_idx, BORDER_COLORS_GLOBAL_BUFFER); - gpu_uniform1f(program_uniform_location(BORDERS_PROGRAM, "background_opacity"), background_opacity); + param_1f(program_param(BORDERS_PROGRAM, "background_opacity"), background_opacity); if (!w->needs_layers) gpu_set_srgb_output(true); draw_quad(has_background_image, num_border_rects); if (!w->needs_layers) gpu_set_srgb_output(false); @@ -2563,19 +2563,19 @@ void draw_cursor_trail(CursorTrail *trail, Window *active_window) { bind_program(TRAIL_PROGRAM); - gpu_set_uniform_floats(program_uniform_location(TRAIL_PROGRAM, "x_coords"), 4, 1, trail->corner_x); - gpu_set_uniform_floats(program_uniform_location(TRAIL_PROGRAM, "y_coords"), 4, 1, trail->corner_y); + param_floats(program_param(TRAIL_PROGRAM, "x_coords"), 4, 1, trail->corner_x); + param_floats(program_param(TRAIL_PROGRAM, "y_coords"), 4, 1, trail->corner_y); - gpu_set_uniform_floats(program_uniform_location(TRAIL_PROGRAM, "cursor_edge_x"), 2, 1, trail->cursor_edge_x); - gpu_set_uniform_floats(program_uniform_location(TRAIL_PROGRAM, "cursor_edge_y"), 2, 1, trail->cursor_edge_y); + param_floats(program_param(TRAIL_PROGRAM, "cursor_edge_x"), 2, 1, trail->cursor_edge_x); + param_floats(program_param(TRAIL_PROGRAM, "cursor_edge_y"), 2, 1, trail->cursor_edge_y); color_type trail_color = OPT(cursor_trail_color); if (trail_color == 0) { // 0 means "none" was specified trail_color = active_window ? active_window->render_data.screen->last_rendered.cursor_bg : OPT(foreground); } - color_vec3(program_uniform_location(TRAIL_PROGRAM, "trail_color"), trail_color); + color_vec3(program_param(TRAIL_PROGRAM, "trail_color"), trail_color); - gpu_uniform1f(program_uniform_location(TRAIL_PROGRAM, "trail_opacity"), trail->opacity); + param_1f(program_param(TRAIL_PROGRAM, "trail_opacity"), trail->opacity); draw_quad(true, 0); unbind_program(); @@ -2631,10 +2631,10 @@ draw_bg_image(OSWindow *os_window, Tab *tab) { bind_program(BGIMAGE_PROGRAM); // altough we dont use this VO we need to ensure *some* VAO is bound at this point. bind_vertex_array(tab->border_rects.vao_idx); - 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); - color_vec4(program_uniform_location(BGIMAGE_PROGRAM, "background"), s.bgcolor, s.opacity); + param_4f(program_param(BGIMAGE_PROGRAM, "sizes"), vwidth, vheight, iwidth, iheight); + param_1f(program_param(BGIMAGE_PROGRAM, "tiled"), tiled); + param_4f(program_param(BGIMAGE_PROGRAM, "positions"), left, top, right, bottom); + color_vec4(program_param(BGIMAGE_PROGRAM, "background"), s.bgcolor, s.opacity); gpu_bind_texture(program_texture_slot(BGIMAGE_PROGRAM, "image"), bg->texture_id); draw_quad(false, 0); unbind_program(); @@ -2653,8 +2653,8 @@ 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); - 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); + color_vec3(program_param(GRAPHICS_ALPHA_MASK_PROGRAM, "amask_fg"), OPT(foreground)); + color_vec4_premult(program_param(GRAPHICS_ALPHA_MASK_PROGRAM, "amask_bg_premult"), OPT(background), background_opacity); draw_graphics(GRAPHICS_ALPHA_MASK_PROGRAM, data, 0, 1, 1.0); } @@ -2877,10 +2877,10 @@ update_pointer_map(OSWindow *os_window, int32_t group_loc) { } 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); + param_1i(group_loc, -1); // Absent when no group with a pointer_map() is turned on and off by events - int32_t active_groups_loc = try_program_uniform_location(CUSTOM_END_PROGRAM, "active_groups"); - if (active_groups_loc >= 0) gpu_uniform1ui(active_groups_loc, groups); + int32_t active_groups_loc = try_program_param(CUSTOM_END_PROGRAM, "active_groups"); + if (active_groups_loc >= 0) param_1ui(active_groups_loc, groups); draw_quad(false, 0); static uint8_t pixels[POINTER_MAP_SIZE * POINTER_MAP_SIZE * 4]; gpu_read_pixels(0, 0, POINTER_MAP_SIZE, POINTER_MAP_SIZE, pixels); @@ -3144,10 +3144,10 @@ run_custom_end_shader(OSWindow *os_window, float sx, float sy, monotonic_t now) // 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. - int32_t group_loc = try_program_uniform_location(CUSTOM_END_PROGRAM, "group"); - int32_t viewport_loc = try_program_uniform_location(CUSTOM_END_PROGRAM, "viewport"); - int32_t anim_progress_loc = try_program_uniform_location(CUSTOM_END_PROGRAM, "animation_progress"); - int32_t convert_to_srgb_loc = try_program_uniform_location(CUSTOM_END_PROGRAM, "convert_to_srgb"); + int32_t group_loc = try_program_param(CUSTOM_END_PROGRAM, "group"); + int32_t viewport_loc = try_program_param(CUSTOM_END_PROGRAM, "viewport"); + int32_t anim_progress_loc = try_program_param(CUSTOM_END_PROGRAM, "animation_progress"); + int32_t convert_to_srgb_loc = try_program_param(CUSTOM_END_PROGRAM, "convert_to_srgb"); if (custom_shaders.end.has_pointer_map && group_loc >= 0) update_pointer_map(os_window, group_loc); const unsigned num_groups = (unsigned)custom_shaders.end.num_groups; const unsigned textures_mask = custom_shaders.end.textures; @@ -3261,10 +3261,10 @@ run_custom_end_shader(OSWindow *os_window, float sx, float sy, monotonic_t now) vp_h = sh; } - gpu_uniform1i(group_loc, (int32_t)g); - gpu_uniform4f(viewport_loc, vp_x, vp_y, vp_w, vp_h); - gpu_uniform1f(anim_progress_loc, anim_progress[g]); - gpu_uniform1i(convert_to_srgb_loc, is_last ? 1 : 0); + param_1i(group_loc, (int32_t)g); + param_4f(viewport_loc, vp_x, vp_y, vp_w, vp_h); + param_1f(anim_progress_loc, anim_progress[g]); + param_1i(convert_to_srgb_loc, is_last ? 1 : 0); // debug_rendering("Custom shader draw group %u: anim_progress=%.4f viewport=(%.2f,%.2f,%.2f,%.2f)\n", g, anim_progress[g], vp_x, vp_y, vp_w, vp_h); draw_quad(false, 0); } @@ -3289,8 +3289,8 @@ stop_os_window_rendering(OSWindow *os_window, Tab *tab, Window *active_window, m bind_framebuffer_for_output(0); bind_program(BLIT_PROGRAM); 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); + param_4f(program_param(BLIT_PROGRAM, "src_rect"), 0, sy, sx, 0); + param_4f(program_param(BLIT_PROGRAM, "dest_rect"), -1, 1, 1, -1); restore_viewport(); if (os_window->live_resize.in_progress) save_viewport_using_top_left_origin(0, 0, os_window->viewport_width, os_window->viewport_height, os_window->live_resize.height); @@ -3375,13 +3375,13 @@ take_screenshot_of_rectangular_region(OSWindow *os_window, Region region, unsign float src_right_norm = (float)region.right / (float)vw; float src_bottom_norm = (float)(vh - region.bottom) / (float)vh; float src_top_norm = (float)(vh - region.top) / (float)vh; - gpu_uniform4f(program_uniform_location(SCREENSHOT_PROGRAM, "src_rect"), src_left_norm, src_top_norm, src_right_norm, src_bottom_norm); + param_4f(program_param(SCREENSHOT_PROGRAM, "src_rect"), src_left_norm, src_top_norm, src_right_norm, src_bottom_norm); // Set destination rectangle (NDC coordinates: -1 to 1) - gpu_uniform4f(program_uniform_location(SCREENSHOT_PROGRAM, "dest_rect"), -1.0f, -1.0f, 1.0f, 1.0f); + param_4f(program_param(SCREENSHOT_PROGRAM, "dest_rect"), -1.0f, -1.0f, 1.0f, 1.0f); // Set the source texture size for proper downscaling - gpu_uniform2f(program_uniform_location(SCREENSHOT_PROGRAM, "src_size"), (float)vw, (float)vh); + param_2f(program_param(SCREENSHOT_PROGRAM, "src_size"), (float)vw, (float)vh); // Bind the source texture gpu_bind_texture(program_texture_slot(SCREENSHOT_PROGRAM, "image"), src_texture); @@ -3621,86 +3621,13 @@ compile_program(PyObject UNUSED *self, PyObject *args) { zero_at_ptr(&custom_shaders.end); custom_shaders.generation++; } - set_program_pipeline(which, GPU_NULL_HANDLE); + free_program(which); Py_RETURN_NONE; } - if (program_pipeline(which)) { - if (allow_recompile) { - set_program_pipeline(which, GPU_NULL_HANDLE); - } else { - PyErr_SetString(PyExc_ValueError, "program already compiled"); - return NULL; - } - } - // The shaders must have been compiled for the target of the loaded driver - PyObject *target = PyDict_GetItemString(metadata, "target"); - const char *target_name = target && PyUnicode_Check(target) ? PyUnicode_AsUTF8(target) : NULL; - if (!target_name || strcmp(target_name, gpu_shader_target()) != 0) { - PyErr_Format( - PyExc_ValueError, - "The shaders were compiled for the target: %s but the GPU driver needs: %s", - target_name ? target_name : "unknown", - gpu_shader_target()); + if (program_pipeline(which) && !allow_recompile) { + PyErr_SetString(PyExc_ValueError, "program already compiled"); return NULL; } - GPUShaderLanguage language; - if (strncmp(target_name, "glsl-", 5) == 0) language = GPU_SHADER_LANGUAGE_GLSL; - else if (strncmp(target_name, "spirv-", 6) == 0) language = GPU_SHADER_LANGUAGE_SPIRV; - else if (strncmp(target_name, "msl-", 4) == 0) language = GPU_SHADER_LANGUAGE_MSL; - else { - PyErr_Format(PyExc_ValueError, "Unknown shader target: %s", target_name); - return NULL; - } - PyObject *native_names = PyDict_GetItemString(metadata, "native_names"); - if (!native_names || !PyDict_Check(native_names)) { - PyErr_SetString(PyExc_KeyError, "The shader program metadata has no native_names dict"); - return NULL; - } - RAII_ALLOC(GPUNativeName, names, calloc(MAX(1, PyDict_GET_SIZE(native_names)), sizeof(GPUNativeName))); - if (!names) return PyErr_NoMemory(); - size_t num_names = 0; - { - PyObject *key, *val; - Py_ssize_t pos = 0; - while (PyDict_Next(native_names, &pos, &key, &val)) { - // The strings remain valid as long as the metadata dict does - if (!(names[num_names].name = PyUnicode_AsUTF8(key)) || !(names[num_names].native_name = PyUnicode_AsUTF8(val))) return NULL; - 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,22 +3637,13 @@ compile_program(PyObject UNUSED *self, PyObject *args) { if (!vcode) return NULL; const char *fcode = PyUnicode_AsUTF8AndSize(fragment_source, &fsz); if (!fcode) return NULL; - const GPUShaderSource sources[2] = { - {.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), bindings, num_bindings, names, num_names); - if (!pipeline) { - PyErr_SetString(PyExc_ValueError, gpu_last_error()); - return NULL; - } - set_program_pipeline(which, pipeline); + if (!compile_gpu_program(which, vcode, (size_t)vsz, fcode, (size_t)fsz, metadata)) return NULL; if (which == CUSTOM_END_PROGRAM) { free_custom_shader_pipeline(&custom_shaders.end); zero_at_ptr(&custom_shaders.end); PyObject *pg = PyDict_GetItemString(metadata, "pipeline"); if (pg && !transfer_pipeline_to_struct(pg, &custom_shaders.end)) { - set_program_pipeline(which, GPU_NULL_HANDLE); + free_program(which); return NULL; } custom_shaders.end.active = true; @@ -3738,16 +3656,12 @@ compile_program(PyObject UNUSED *self, PyObject *args) { if (g >= 0 && (size_t)g < custom_shaders.end.num_groups) custom_shaders.end.pointer_map_groups |= 1u << g; } if (PyErr_Occurred()) { - set_program_pipeline(which, GPU_NULL_HANDLE); + free_program(which); return NULL; } } custom_shaders.generation++; } - if (!set_program_layout(which, metadata)) { - set_program_pipeline(which, GPU_NULL_HANDLE); - return NULL; - } Py_RETURN_NONE; } diff --git a/kitty/shaders/slang.py b/kitty/shaders/slang.py index f3aed2b0d..7034b698f 100644 --- a/kitty/shaders/slang.py +++ b/kitty/shaders/slang.py @@ -1149,16 +1149,17 @@ class GLSLMetadata: return ans -# Uniform blocks that must be emitted as loose uniforms in the default uniform -# block instead of as a UBO. Uniform values are part of program object state, -# which the GL implementation is responsible for maintaining, whereas the data -# store of a buffer object belongs to us and some drivers, notably the macOS -# OpenGL on Metal layer waking from sleep, silently discard it. Anything we -# upload to a buffer once and never again then stays corrupt forever. Only -# suitable for blocks whose contents are compile time constants, since setting -# a loose uniform costs a GL call per program rather than one buffer write. -# See https://github.com/kovidgoyal/kitty/issues/10571 -GLSL_LOOSE_UNIFORM_BLOCKS = frozenset({'GammaLUT'}) +# Uniform blocks whose contents are constant for the lifetime of a pipeline. +# kitty sets them once, as parameter blocks, see program_metadata(), and the +# GPU driver is responsible for retaining their contents, rather than kitty +# keeping them in uniform buffers it owns. For OpenGL they are emitted as loose +# uniforms in the default uniform block. Uniform values are part of program +# object state, which the GL implementation is responsible for maintaining, +# whereas the data store of a buffer object belongs to us and some drivers, +# notably the macOS OpenGL on Metal layer waking from sleep, silently discard +# it. Anything we upload to a buffer once and never again then stays corrupt +# forever. See https://github.com/kovidgoyal/kitty/issues/10571 +PIPELINE_CONSTANT_BLOCKS = frozenset({'GammaLUT'}) def fixup_opengl_code(glsl_code: str, shader_name: str, existing_metadata: GLSLMetadata | None, glsl_version: int) -> tuple[str, GLSLMetadata]: @@ -1260,7 +1261,7 @@ def fixup_opengl_code(glsl_code: str, shader_name: str, existing_metadata: GLSLM in_uniform_block_contents = False block_name = words[-1] slang_block_name = block_name[len('block_') :].rpartition('_')[0] if block_name.startswith('block_') else '' - uniform_block_is_struct = line.startswith('layout(std140') and slang_block_name not in GLSL_LOOSE_UNIFORM_BLOCKS # ) + uniform_block_is_struct = line.startswith('layout(std140') and slang_block_name not in PIPELINE_CONSTANT_BLOCKS # ) if uniform_block_is_struct: current_uniform_struct_name = slang_block_name assert block_name.startswith('block_') @@ -1355,6 +1356,32 @@ def check_binding_slot(name: str, slot: int) -> int: return slot +_PARAM_SCALARS = MappingProxyType({'float32': 'float', 'int32': 'int', 'uint32': 'uint', 'bool': 'bool'}) + + +def param_field(name: str, offset: int, t: dict[str, Any]) -> dict[str, Any]: + # A field of a parameter block, described by its scalar type, the number + # of components in each element and the number and stride of elements + count, stride = 1, 0 + if t['kind'] == 'array': + count, stride = t['elementCount'], t['uniformStride'] + t = t['elementType'] + components = 1 + if t['kind'] == 'vector': + components = t['elementCount'] + t = t['elementType'] + if t['kind'] != 'scalar' or t['scalarType'] not in _PARAM_SCALARS: + raise ValueError(f'The shader parameter {name} has unsupported type: {t}') + return { + 'name': name, + 'offset': offset, + 'scalar': _PARAM_SCALARS[t['scalarType']], + 'components': components, + 'count': count, + 'stride': stride or 4 * components, + } + + 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 @@ -1364,10 +1391,12 @@ def program_metadata(target: ShaderTarget, reflections: Sequence[dict[str, Any]] compiled code, for targets that look up parameters by identifier. """ 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]]] = {} + # Parameter blocks are the uniform parameters of each entry point and the + # pipeline constant blocks. kitty sets their contents with set_params() + # rather than via uniform buffers. + param_blocks: dict[str, dict[str, Any]] = {} # Uniform blocks that are emitted as uniform blocks rather than as loose uniforms by the target real_blocks = set(native.uniform_struct_names) for refl in reflections: @@ -1379,22 +1408,31 @@ def program_metadata(target: ShaderTarget, reflections: Sequence[dict[str, Any]] et = evl['type'] tname = et['name'] slot = check_binding_slot(tname, p['binding']['index']) - if tname in real_blocks: + if tname in PIPELINE_CONSTANT_BLOCKS: + param_blocks[tname] = { + 'name': tname, + 'stage': '', + 'slot': slot, + 'size': evl['binding']['size'], + 'fields': [param_field(f['name'], f['binding']['offset'], f['type']) for f in et['fields']], + } + elif 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, 'slot': slot} - else: - uniforms.update(f['name'] for f in et['fields']) + # Otherwise the block is declared in an imported module but not + # used by this program, so it is not present in the compiled code case 'resource': 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']: stage = ep['stage'] - fields: dict[str, dict[str, int]] = {} + fields: list[dict[str, Any]] = [] + size = 0 for p in ep.get('parameters', ()): b = p.get('binding') or {} match b.get('kind'): @@ -1402,19 +1440,19 @@ def program_metadata(target: ShaderTarget, reflections: Sequence[dict[str, Any]] if stage == 'vertex': inputs[p['name']] = b['index'] case 'uniform': - uniforms.add(p['name']) - fields[p['name']] = {'offset': b['offset'], 'size': b['size']} - params[stage] = fields + fields.append(param_field(p['name'], b['offset'], p['type'])) + size = max(size, b['offset'] + b['size']) + if fields: + param_blocks[stage] = {'name': '', 'stage': stage, 'slot': -1, 'size': size, 'fields': fields} native_names = {k: v.partition('[')[0] for k, v in native.loose_uniforms.items()} # ] native_names.update(native.uniform_struct_names) native_names.update(native.native_inputs) return { 'target': target.id, 'inputs': inputs, - 'uniforms': sorted(uniforms), 'textures': textures, 'blocks': blocks, - 'params': params, + 'param_blocks': [param_blocks[k] for k in sorted(param_blocks)], 'native_names': native_names, }