diff --git a/gpu/api.h b/gpu/api.h index 7ec888476..e97bba1fa 100644 --- a/gpu/api.h +++ b/gpu/api.h @@ -232,7 +232,7 @@ typedef struct GPUShaderSource { typedef struct GPUUniformBlockInfo { uint32_t index; // identifies the block within the pipeline - uint32_t size; // size in bytes of the data in the block + uint32_t size; // size in bytes of the data in the block, zero if the driver cannot determine it } GPUUniformBlockInfo; // Shaders are written in Slang and compiled ahead of time, or for user diff --git a/gpu/vulkan/commands.c b/gpu/vulkan/commands.c new file mode 100644 index 000000000..0d5fadc5f --- /dev/null +++ b/gpu/vulkan/commands.c @@ -0,0 +1,657 @@ +/* + * commands.c + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// Recording and submitting commands, rendering and drawing + +#include "internal.h" + +#define AS_TEXTURE(handle) ((Texture *)(uintptr_t)(handle)) +#define AS_RENDER_TARGET(handle) ((RenderTarget *)(uintptr_t)(handle)) + +// Deferred destruction {{{ +typedef enum { GARBAGE_BUFFER, GARBAGE_IMAGE, GARBAGE_PIPELINE, GARBAGE_PIPELINE_OBJECTS } GarbageKind; + +struct Garbage { + GarbageKind kind; + union { + struct { + VkBuffer buffer; + Allocation alloc; + } buffer; + Image image; + VkPipeline pipeline; + struct { + VkPipelineLayout layout; + VkDescriptorSetLayout set_layout; + VkShaderModule modules[2]; + size_t num_modules; + } objects; + }; + Garbage *next; +}; + +static void +destroy_garbage(Driver *d, Garbage *g) { + switch (g->kind) { + case GARBAGE_BUFFER: + vkDestroyBuffer(d->device, g->buffer.buffer, NULL); + free_memory(d, &g->buffer.alloc); + break; + case GARBAGE_IMAGE: + destroy_image_views(d, &g->image); + vkDestroyImage(d->device, g->image.image, NULL); + free_memory(d, &g->image.alloc); + break; + case GARBAGE_PIPELINE: vkDestroyPipeline(d->device, g->pipeline, NULL); break; + case GARBAGE_PIPELINE_OBJECTS: + vkDestroyPipelineLayout(d->device, g->objects.layout, NULL); + vkDestroyDescriptorSetLayout(d->device, g->objects.set_layout, NULL); + for (size_t i = 0; i < g->objects.num_modules; i++) vkDestroyShaderModule(d->device, g->objects.modules[i], NULL); + break; + } +} + +static void +add_garbage(Driver *d, Garbage *g) { + // Attached to the context being recorded, which completes after every + // submission that could use the object + Context *c = current_context(d); + g->next = c->garbage; + c->garbage = g; +} + +static Garbage * +new_garbage(Driver *d, GarbageKind kind) { + Garbage *g = calloc(1, sizeof(Garbage)); + if (!g) fatal(d, "Out of memory"); + g->kind = kind; + return g; +} + +void +defer_destroy_buffer(Driver *d, VkBuffer buffer, Allocation *alloc) { + Garbage *g = new_garbage(d, GARBAGE_BUFFER); + g->buffer.buffer = buffer; + g->buffer.alloc = *alloc; + *alloc = (Allocation){0}; + add_garbage(d, g); +} + +void +defer_destroy_image(Driver *d, Image *image) { + if (d->rendering.image == image) end_rendering(d); + Garbage *g = new_garbage(d, GARBAGE_IMAGE); + g->image = *image; + *image = (Image){0}; + add_garbage(d, g); +} + +void +defer_destroy_pipeline(Driver *d, VkPipeline pipeline) { + if (d->cmd_state.pipeline == pipeline) d->cmd_state.pipeline = VK_NULL_HANDLE; + Garbage *g = new_garbage(d, GARBAGE_PIPELINE); + g->pipeline = pipeline; + add_garbage(d, g); +} + +void +defer_destroy_pipeline_objects(Driver *d, VkPipelineLayout layout, VkDescriptorSetLayout set_layout, VkShaderModule *modules, size_t num_modules) { + Garbage *g = new_garbage(d, GARBAGE_PIPELINE_OBJECTS); + g->objects.layout = layout; + g->objects.set_layout = set_layout; + g->objects.num_modules = MIN(num_modules, arraysz(g->objects.modules)); + memcpy(g->objects.modules, modules, g->objects.num_modules * sizeof(VkShaderModule)); + add_garbage(d, g); +} +// }}} + +// Contexts {{{ +// Release the resources of a context whose commands have completed +static void +context_completed(Driver *d, Context *c) { + c->submitted = false; + if (c->serial > d->completed_serial) d->completed_serial = c->serial; + release_staging(d, c); + while (c->garbage) { + Garbage *g = c->garbage; + c->garbage = g->next; + destroy_garbage(d, g); + free(g); + } + for (size_t i = 0; i < c->num_used_semaphores; i++) { + if (d->num_free_semaphores < arraysz(d->free_semaphores)) d->free_semaphores[d->num_free_semaphores++] = c->used_semaphores[i]; + else vkDestroySemaphore(d->device, c->used_semaphores[i], NULL); + } + c->num_used_semaphores = 0; +} + +static void +wait_for_context(Driver *d, Context *c) { + if (!c->submitted) return; + check_vk(d, vkWaitForFences(d->device, 1, &c->fence, VK_TRUE, UINT64_MAX), "waiting for GPU commands to complete"); + check_vk(d, vkResetFences(d->device, 1, &c->fence), "resetting a fence"); + context_completed(d, c); +} + +// Note which submissions have completed, without waiting, so that the +// objects they used can be reused sooner +static void +poll_completed(Driver *d) { + // Contexts complete in the order they were submitted + for (size_t i = 1; i <= NUM_CONTEXTS; i++) { + Context *c = d->contexts + (d->current_context + i) % NUM_CONTEXTS; + if (!c->submitted) continue; + if (vkGetFenceStatus(d->device, c->fence) != VK_SUCCESS) break; + wait_for_context(d, c); + } +} + +Context * +current_context(Driver *d) { + Context *c = d->contexts + d->current_context; + if (c->recording) return c; + poll_completed(d); + // Waiting on the previous submission of this context throttles the CPU + wait_for_context(d, c); + check_vk(d, vkResetCommandPool(d->device, c->pool, 0), "resetting a command pool"); + const VkCommandBufferBeginInfo info = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT}; + check_vk(d, vkBeginCommandBuffer(c->cmd, &info), "beginning a command buffer"); + c->serial = d->next_serial; + c->recording = true; + // Command buffer state does not carry over between command buffers + memset(&d->cmd_state, 0, sizeof(d->cmd_state)); + d->cmd_state.descriptors_dirty = true; + vkCmdBindIndexBuffer(c->cmd, d->quad_indices, 0, VK_INDEX_TYPE_UINT16); + return c; +} + +VkCommandBuffer +command_buffer(Driver *d) { + return current_context(d)->cmd; +} + +uint64_t +recording_serial(Driver *d) { + const Context *c = d->contexts + d->current_context; + return c->recording ? c->serial : d->next_serial; +} + +VkSemaphore +get_semaphore(Driver *d) { + Context *c = current_context(d); + VkSemaphore ans; + if (d->num_free_semaphores) ans = d->free_semaphores[--d->num_free_semaphores]; + else { + const VkSemaphoreCreateInfo info = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; + check_vk(d, vkCreateSemaphore(d->device, &info, NULL, &ans), "creating a semaphore"); + } + // Reusable once this context completes, as its submission waits on the semaphore + if (c->num_used_semaphores >= arraysz(c->used_semaphores)) fatal(d, "Too many semaphores used in a single submission"); + c->used_semaphores[c->num_used_semaphores++] = ans; + return ans; +} + +void +add_wait_semaphore(Driver *d, VkSemaphore s) { + Context *c = current_context(d); + if (c->num_waits >= arraysz(c->waits)) fatal(d, "Too many semaphores to wait on in a single submission"); + c->waits[c->num_waits++] = s; +} + +void +submit_commands(Driver *d, const VkSemaphore *signal, size_t num_signal, bool wait) { + Context *c = current_context(d); + end_rendering(d); + check_vk(d, vkEndCommandBuffer(c->cmd), "ending a command buffer"); + VkSemaphoreSubmitInfo waits[arraysz(c->waits)], signals[4]; + for (size_t i = 0; i < c->num_waits; i++) { + waits[i] = (VkSemaphoreSubmitInfo){ + .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO, .semaphore = c->waits[i], .stageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT}; + } + num_signal = MIN(num_signal, arraysz(signals)); + for (size_t i = 0; i < num_signal; i++) { + signals[i] = (VkSemaphoreSubmitInfo){ + .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO, .semaphore = signal[i], .stageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT}; + } + const VkCommandBufferSubmitInfo cmd_info = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = c->cmd}; + const VkSubmitInfo2 info = { + .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2, + .waitSemaphoreInfoCount = (uint32_t)c->num_waits, + .pWaitSemaphoreInfos = waits, + .commandBufferInfoCount = 1, + .pCommandBufferInfos = &cmd_info, + .signalSemaphoreInfoCount = (uint32_t)num_signal, + .pSignalSemaphoreInfos = signals, + }; + check_vk(d, vkQueueSubmit2(d->queue, 1, &info, c->fence), "submitting commands to the GPU"); + c->recording = false; + c->submitted = true; + c->num_waits = 0; + d->next_serial++; + d->current_context = (d->current_context + 1) % NUM_CONTEXTS; + if (wait) wait_for_serial(d, c->serial); +} + +void +wait_for_serial(Driver *d, uint64_t serial) { + if (serial <= d->completed_serial) return; + const Context *rc = d->contexts + d->current_context; + if (rc->recording && rc->serial <= serial) submit_commands(d, NULL, 0, false); + // Wait in submission order, oldest first + for (size_t i = 0; i < NUM_CONTEXTS && serial > d->completed_serial; i++) { + Context *c = d->contexts + (d->current_context + i) % NUM_CONTEXTS; + if (c->submitted) wait_for_context(d, c); + } +} + +void +destroy_contexts(Driver *d) { + for (size_t i = 0; i < NUM_CONTEXTS; i++) { + Context *c = d->contexts + i; + if (c->recording) { + // Commands that were never submitted, discard them + vkEndCommandBuffer(c->cmd); + c->recording = false; + } + wait_for_context(d, c); + // Releases unsubmitted resources too. Semaphores to wait on are also + // in used_semaphores, so they are recycled, and destroyed, with those. + context_completed(d, c); + c->num_waits = 0; + if (c->fence) vkDestroyFence(d->device, c->fence, NULL); + if (c->pool) vkDestroyCommandPool(d->device, c->pool, NULL); + *c = (Context){0}; + } +} +// }}} + +// Barriers and rendering {{{ +static bool +access_writes(VkAccessFlags2 a) { + return (a & (VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT | VK_ACCESS_2_SHADER_WRITE_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT)) != 0; +} + +void +transition_image(Driver *d, Image *image, VkImageLayout layout, VkPipelineStageFlags2 stage, VkAccessFlags2 access) { + image->last_use_serial = recording_serial(d); + const ImageState s = image->state; + // Reads after reads in the same layout need no barrier + if (s.layout == layout && !access_writes(s.access) && !access_writes(access)) { + image->state.stage |= stage; + image->state.access |= access; + return; + } + // Barriers cannot be recorded inside a render pass instance, except for self dependencies + end_rendering(d); + const VkImageMemoryBarrier2 barrier = { + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .srcStageMask = s.stage ? s.stage : VK_PIPELINE_STAGE_2_NONE, + .srcAccessMask = s.access, + .dstStageMask = stage, + .dstAccessMask = access, + .oldLayout = s.layout, + .newLayout = layout, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = image->image, + .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .levelCount = 1, .layerCount = VK_REMAINING_ARRAY_LAYERS}, + }; + const VkDependencyInfo dep = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1, .pImageMemoryBarriers = &barrier}; + vkCmdPipelineBarrier2(command_buffer(d), &dep); + image->state = (ImageState){.layout = layout, .stage = stage, .access = access}; +} + +void +end_rendering(Driver *d) { + if (!d->rendering.active) return; + vkCmdEndRendering(d->contexts[d->current_context].cmd); + d->rendering.active = false; + d->rendering.image = NULL; + d->rendering.view = VK_NULL_HANDLE; +} + +// The image the current render target renders into, NULL on failure +static Image * +target_image(Driver *d) { + if (d->render_target) return d->render_target->texture ? &d->render_target->texture->image : NULL; + return current_surface_image(d); +} + +// Begin a render pass instance for the current render target, if not +// already active. Returns false if there is nothing to render to. +static bool +begin_rendering(Driver *d) { + Image *img = target_image(d); + if (!img) return false; + const bool srgb = d->srgb_output && img->srgb_view; + const VkImageView view = srgb ? img->srgb_view : img->unorm_view; + if (!view) { + log_error(d, "Cannot render into a texture of this format"); + return false; + } + if (d->rendering.active && d->rendering.view == view) return true; + end_rendering(d); + transition_image( + d, + img, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT); + const VkRenderingAttachmentInfo attachment = { + .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, + .imageView = view, + .imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE, + }; + const VkRenderingInfo info = { + .sType = VK_STRUCTURE_TYPE_RENDERING_INFO, + .renderArea = {.extent = {img->width, img->height}}, + .layerCount = 1, + .colorAttachmentCount = 1, + .pColorAttachments = &attachment, + }; + vkCmdBeginRendering(command_buffer(d), &info); + d->rendering.active = true; + d->rendering.image = img; + d->rendering.view = view; + d->rendering.format = srgb ? img->srgb_format : img->unorm_format; + d->rendering.extent = (VkExtent2D){img->width, img->height}; + return true; +} + +// The scissor rectangle, clipped to the render target, in ans +static void +current_scissor(Driver *d, VkRect2D *ans) { + const int32_t w = (int32_t)d->rendering.extent.width, h = (int32_t)d->rendering.extent.height; + int32_t x0 = 0, y0 = 0, x1 = w, y1 = h; + if (d->scissor.enabled) { + x0 = MAX(0, d->scissor.rect[0]); + y0 = MAX(0, d->scissor.rect[1]); + x1 = MIN(w, d->scissor.rect[0] + d->scissor.rect[2]); + y1 = MIN(h, d->scissor.rect[1] + d->scissor.rect[3]); + } + *ans = (VkRect2D){.offset = {x0, y0}, .extent = {(uint32_t)MAX(0, x1 - x0), (uint32_t)MAX(0, y1 - y0)}}; +} + +void +clear(void *driver, float r, float g, float b, float a) { + Driver *d = driver; + if (!begin_rendering(d)) return; + VkRect2D rect; + current_scissor(d, &rect); + if (!rect.extent.width || !rect.extent.height) return; + const VkClearAttachment att = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .colorAttachment = 0, .clearValue = {.color = {.float32 = {r, g, b, a}}}}; + const VkClearRect cr = {.rect = rect, .baseArrayLayer = 0, .layerCount = 1}; + vkCmdClearAttachments(command_buffer(d), 1, &att, 1, &cr); +} + +void +set_viewport(void *driver, int32_t x, int32_t y, uint32_t width, uint32_t height) { + Driver *d = driver; + d->viewport[0] = x; + d->viewport[1] = y; + d->viewport[2] = (int32_t)width; + d->viewport[3] = (int32_t)height; +} + +void +get_viewport(void *driver, int32_t ans[4]) { + memcpy(ans, ((Driver *)driver)->viewport, sizeof(int32_t) * 4); +} + +void +set_scissor(void *driver, bool enabled, int32_t x, int32_t y, uint32_t width, uint32_t height) { + Driver *d = driver; + d->scissor.enabled = enabled; + d->scissor.rect[0] = x; + d->scissor.rect[1] = y; + d->scissor.rect[2] = (int32_t)width; + d->scissor.rect[3] = (int32_t)height; +} + +void +set_srgb_output(void *driver, bool enabled) { + // Takes effect at the next draw, which uses the corresponding view of the render target + ((Driver *)driver)->srgb_output = enabled; +} +// }}} + +// Drawing {{{ +// Make the textures used by the current pipeline ready for sampling. Must be +// done before beginning rendering, as layout transitions end it. +static bool +prepare_textures(Driver *d, const Pipeline *p) { + for (size_t i = 0; i < p->num_bindings; i++) { + const PipelineBinding *b = p->bindings + i; + if (b->kind != GPU_BINDING_TEXTURE) continue; + Texture *t = d->bound_textures[b->slot]; + if (!t) { + log_error(d, "No texture is bound to the slot: %u used by the texture: %s", b->slot, b->name); + return false; + } + if (d->rendering.active && d->rendering.image == &t->image) end_rendering(d); + if (target_image(d) == &t->image) { + log_error(d, "Cannot sample from the texture: %s while rendering into it", b->name); + return false; + } + transition_image( + d, + &t->image, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + VK_PIPELINE_STAGE_2_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT, + VK_ACCESS_2_SHADER_SAMPLED_READ_BIT); + } + return true; +} + +// The uniform buffer bound to a slot, either a parameter block of the pipeline or a buffer bound by kitty +static Buffer * +uniform_buffer_for_slot(Driver *d, const Pipeline *p, uint32_t slot) { + for (size_t i = 0; i < p->num_param_blocks; i++) { + if (p->param_blocks[i].slot == (int32_t)slot) return p->param_blocks[i].buffer; + } + return d->bound_buffers[slot]; +} + +static bool +push_descriptors(Driver *d, VkCommandBuffer cmd, const Pipeline *p) { + if (!p->num_bindings) return true; + VkWriteDescriptorSet writes[MAX_BINDING_SLOTS]; + VkDescriptorImageInfo images[MAX_BINDING_SLOTS]; + VkDescriptorBufferInfo buffers[MAX_BINDING_SLOTS]; + uint32_t n = 0; + for (size_t i = 0; i < p->num_bindings && n < MAX_BINDING_SLOTS; i++) { + const PipelineBinding *b = p->bindings + i; + VkWriteDescriptorSet *w = writes + n; + *w = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = b->slot, .descriptorCount = 1}; + if (b->kind == GPU_BINDING_TEXTURE) { + Texture *t = d->bound_textures[b->slot]; + images[n] = + (VkDescriptorImageInfo){.sampler = t->sampler, .imageView = t->image.sampled_view, .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL}; + w->descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + w->pImageInfo = images + n; + } else { + Buffer *buf = uniform_buffer_for_slot(d, p, b->slot); + if (!buf || !buf->storage.buffer) { + log_error(d, "No uniform buffer is bound to the slot: %u used by the uniform block: %s", b->slot, b->name); + return false; + } + use_buffer(d, buf); + buffers[n] = (VkDescriptorBufferInfo){.buffer = buf->storage.buffer, .offset = 0, .range = buf->storage.size}; + w->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + w->pBufferInfo = buffers + n; + } + n++; + } + vkCmdPushDescriptorSetKHR(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p->layout, 0, n, writes); + return true; +} + +static bool +bind_vertex_buffers(Driver *d, VkCommandBuffer cmd, const PipelineVariantKey *key) { + for (uint32_t loc = 0; loc < MAX_VERTEX_ATTRIBUTES; loc++) { + if (!key->attribute_formats[loc]) continue; + const VertexAttribute *a = d->vertex_layout->attributes + loc; + if (!a->buffer || !a->buffer->storage.buffer) { + log_error(d, "The vertex buffer for the vertex input at location: %u has no storage", loc); + return false; + } + use_buffer(d, a->buffer); + const VkDeviceSize offset = a->offset, stride = a->stride; + vkCmdBindVertexBuffers2(cmd, loc, 1, &a->buffer->storage.buffer, &offset, NULL, &stride); + } + return true; +} + +void +draw_quad(void *driver, uint32_t instance_count, GPUBlendMode blend) { + Driver *d = driver; + Pipeline *p = d->pipeline; + if (!p) return; + if (!prepare_textures(d, p) || !begin_rendering(d)) return; + const VkPipeline vp = pipeline_for_current_state(d, blend == GPU_BLEND_PREMULTIPLIED_ALPHA); + if (!vp) return; + VkCommandBuffer cmd = command_buffer(d); + if (d->cmd_state.pipeline != vp) { + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, vp); + d->cmd_state.pipeline = vp; + } + if (!d->cmd_state.viewport_valid || memcmp(d->cmd_state.viewport, d->viewport, sizeof(d->viewport)) != 0) { + const VkViewport v = { + .x = (float)d->viewport[0], .y = (float)d->viewport[1], .width = (float)d->viewport[2], .height = (float)d->viewport[3], .maxDepth = 1.f}; + vkCmdSetViewport(cmd, 0, 1, &v); + memcpy(d->cmd_state.viewport, d->viewport, sizeof(d->viewport)); + d->cmd_state.viewport_valid = true; + } + VkRect2D sc; + current_scissor(d, &sc); + if (!sc.extent.width || !sc.extent.height) return; + const int32_t sc_arr[4] = {sc.offset.x, sc.offset.y, (int32_t)sc.extent.width, (int32_t)sc.extent.height}; + if (!d->cmd_state.scissor_valid || memcmp(d->cmd_state.scissor, sc_arr, sizeof(sc_arr)) != 0) { + vkCmdSetScissor(cmd, 0, 1, &sc); + memcpy(d->cmd_state.scissor, sc_arr, sizeof(sc_arr)); + d->cmd_state.scissor_valid = true; + } + // The push constant range is the same for all pipeline layouts, so the + // pushed values survive changes of pipeline, but they are per pipeline in + // the API + if (d->cmd_state.push_constants_of != p || d->cmd_state.push_constants_dirty) { + vkCmdPushConstants(cmd, p->layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, PUSH_CONSTANTS_SIZE, p->push_constants); + d->cmd_state.push_constants_of = p; + d->cmd_state.push_constants_dirty = false; + } + if (!push_descriptors(d, cmd, p)) return; + const PipelineVariantKey *key = NULL; + for (size_t i = 0; i < p->num_variants; i++) { + if (p->variants[i].pipeline == vp) key = &p->variants[i].key; + } + if (!key || (d->vertex_layout && !bind_vertex_buffers(d, cmd, key))) return; + vkCmdDrawIndexed(cmd, 6, instance_count ? instance_count : 1, 0, 0, 0); +} +// }}} + +// Render targets and reading back {{{ +gpu_render_target_t +render_target_create(void *driver, gpu_texture_t color_texture) { + Driver *d = driver; + RenderTarget *rt = calloc(1, sizeof(RenderTarget)); + if (!rt) { + set_error(d, "Out of memory"); + return GPU_NULL_HANDLE; + } + rt->texture = AS_TEXTURE(color_texture); + return AS_HANDLE(rt); +} + +void +render_target_destroy(void *driver, gpu_render_target_t target) { + Driver *d = driver; + RenderTarget *rt = AS_RENDER_TARGET(target); + if (!rt) return; + if (d->render_target == rt) { + end_rendering(d); + d->render_target = NULL; + } + free(rt); +} + +void +set_render_target(void *driver, gpu_render_target_t target) { + // Rendering switches to the new target at the next draw + ((Driver *)driver)->render_target = AS_RENDER_TARGET(target); +} + +gpu_render_target_t +get_render_target(void *driver) { + return AS_HANDLE(((Driver *)driver)->render_target); +} + +const char * +render_target_status(void *driver UNUSED, gpu_render_target_t target) { + const RenderTarget *rt = AS_RENDER_TARGET(target); + if (!rt || !rt->texture) return "No texture"; + if (!rt->texture->image.unorm_view) return "The texture format cannot be rendered to"; + return NULL; +} + +void +read_pixels(void *driver, int32_t x, int32_t y, uint32_t width, uint32_t height, void *dst) { + Driver *d = driver; + Image *img = target_image(d); + if (!img || !width || !height) return; + if (x < 0 || y < 0 || (uint32_t)x + width > img->width || (uint32_t)y + height > img->height) { + log_error(d, "Reading pixels outside the render target"); + return; + } + const size_t bpp = img->format == VK_FORMAT_R16G16B16A16_UNORM ? 8 : 4; + const VkDeviceSize size = (VkDeviceSize)width * height * bpp; + VkBuffer buffer; + Allocation alloc; + if (!create_buffer_with_memory(d, size, VK_BUFFER_USAGE_TRANSFER_DST_BIT, MEMORY_READBACK, &buffer, &alloc)) { + log_error(d, "Failed to allocate memory for reading back pixels: %s", d->last_error); + return; + } + transition_image(d, img, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_READ_BIT); + const VkBufferImageCopy region = { + .imageSubresource = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .layerCount = 1}, + .imageOffset = {x, y, 0}, + .imageExtent = {width, height, 1}, + }; + vkCmdCopyImageToBuffer(command_buffer(d), img->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer, 1, ®ion); + submit_commands(d, NULL, 0, true); + convert_to_rgba8(img->format, alloc.mapped, dst, (size_t)width * height); + vkDestroyBuffer(d->device, buffer, NULL); + free_memory(d, &alloc); +} + +void +copy_to_texture(void *driver, gpu_texture_t dst, int32_t x, int32_t y, uint32_t width, uint32_t height) { + Driver *d = driver; + Texture *t = AS_TEXTURE(dst); + Image *img = target_image(d); + if (!img || !t || !width || !height) return; + transition_image(d, img, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_READ_BIT); + transition_image(d, &t->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); + // A blit converts between formats, such as from BGRA swapchain images + const VkImageBlit region = { + .srcSubresource = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .layerCount = 1}, + .srcOffsets = {{x, y, 0}, {x + (int32_t)width, y + (int32_t)height, 1}}, + .dstSubresource = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .layerCount = 1}, + .dstOffsets = {{0, 0, 0}, {(int32_t)width, (int32_t)height, 1}}, + }; + vkCmdBlitImage( + command_buffer(d), + img->image, + VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + t->image.image, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + 1, + ®ion, + VK_FILTER_NEAREST); +} +// }}} diff --git a/gpu/vulkan/driver.c b/gpu/vulkan/driver.c new file mode 100644 index 000000000..bdf8271da --- /dev/null +++ b/gpu/vulkan/driver.c @@ -0,0 +1,636 @@ +/* + * driver.c + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// The Vulkan implementation of the kitty GPU driver API, see ../api.h and +// internal.h for an overview. + +#include "internal.h" +#include "version.h" +#include + +#define VK_DEFINE_FUNCTION(name) PFN_##name name = NULL; +VK_GLOBAL_FUNCTIONS(VK_DEFINE_FUNCTION) +VK_INSTANCE_FUNCTIONS(VK_DEFINE_FUNCTION) +VK_XLIB_FUNCTIONS(VK_DEFINE_FUNCTION) +VK_WAYLAND_FUNCTIONS(VK_DEFINE_FUNCTION) +VK_DEBUG_FUNCTIONS(VK_DEFINE_FUNCTION) +VK_DEVICE_FUNCTIONS(VK_DEFINE_FUNCTION) +#undef VK_DEFINE_FUNCTION +PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; + +// There is only ever one driver per process +static Driver *the_driver = NULL; + +// Errors {{{ +void +set_error(Driver *d, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vsnprintf(d->last_error, sizeof(d->last_error), fmt, ap); + va_end(ap); +} + +void +log_error(Driver *d, const char *fmt, ...) { + char buf[4096]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + d->params.log_error(buf); +} + +void +debug_print(Driver *d, const char *fmt, ...) { + if (!d->params.debug_rendering) return; + char buf[4096]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + d->params.debug_print(buf); +} + +void +fatal(Driver *d, const char *fmt, ...) { + char buf[4096]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + d->params.fatal(buf); + abort(); +} + +const char * +vk_result_string(VkResult r) { + switch (r) { +#define C(x) \ + case x: return #x; + C(VK_SUCCESS) + C(VK_NOT_READY) + C(VK_TIMEOUT) + C(VK_INCOMPLETE) + C(VK_SUBOPTIMAL_KHR) + C(VK_ERROR_OUT_OF_HOST_MEMORY) + C(VK_ERROR_OUT_OF_DEVICE_MEMORY) + C(VK_ERROR_INITIALIZATION_FAILED) + C(VK_ERROR_DEVICE_LOST) + C(VK_ERROR_MEMORY_MAP_FAILED) + C(VK_ERROR_LAYER_NOT_PRESENT) + C(VK_ERROR_EXTENSION_NOT_PRESENT) + C(VK_ERROR_FEATURE_NOT_PRESENT) + C(VK_ERROR_INCOMPATIBLE_DRIVER) + C(VK_ERROR_TOO_MANY_OBJECTS) + C(VK_ERROR_FORMAT_NOT_SUPPORTED) + C(VK_ERROR_SURFACE_LOST_KHR) + C(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR) + C(VK_ERROR_OUT_OF_DATE_KHR) + C(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR) + C(VK_ERROR_INVALID_SHADER_NV) + C(VK_ERROR_OUT_OF_POOL_MEMORY) + C(VK_ERROR_UNKNOWN) +#undef C + default: return "unknown Vulkan error"; + } +} + +void +check_vk(Driver *d, VkResult r, const char *what) { + if (r != VK_SUCCESS) fatal(d, "Vulkan: %s failed with error: %s", what, vk_result_string(r)); +} + +static VKAPI_ATTR VkBool32 VKAPI_CALL +debug_messenger( + VkDebugUtilsMessageSeverityFlagBitsEXT severity, + VkDebugUtilsMessageTypeFlagsEXT type UNUSED, + const VkDebugUtilsMessengerCallbackDataEXT *data, + void *user_data) { + Driver *d = user_data; + if (severity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) log_error(d, "Vulkan: %s", data->pMessage); + else debug_print(d, "Vulkan: %s", data->pMessage); + return VK_FALSE; +} +// }}} + +// Loading {{{ +static bool +load_libvulkan(Driver *d) { + static const char *names[] = {"libvulkan.so.1", "libvulkan.so"}; + for (size_t i = 0; i < arraysz(names) && !d->libvulkan; i++) d->libvulkan = dlopen(names[i], RTLD_LAZY | RTLD_LOCAL); + if (!d->libvulkan) { + set_error(d, "Could not load the Vulkan library: %s", dlerror()); + return false; + } + *(void **)&vkGetInstanceProcAddr = dlsym(d->libvulkan, "vkGetInstanceProcAddr"); + if (!vkGetInstanceProcAddr) { + set_error(d, "The Vulkan library does not export vkGetInstanceProcAddr"); + return false; + } +#define LOAD(name) \ + if (!(name = (PFN_##name)vkGetInstanceProcAddr(NULL, #name))) { \ + set_error(d, "Could not load the Vulkan function: %s", #name); \ + return false; \ + } + VK_GLOBAL_FUNCTIONS(LOAD) +#undef LOAD + return true; +} + +static bool +load_instance_functions(Driver *d) { +#define LOAD(name) \ + if (!(name = (PFN_##name)vkGetInstanceProcAddr(d->instance, #name))) { \ + set_error(d, "Could not load the Vulkan function: %s", #name); \ + return false; \ + } + VK_INSTANCE_FUNCTIONS(LOAD) + if (d->platform == GPU_PLATFORM_WAYLAND) { + VK_WAYLAND_FUNCTIONS(LOAD) + } else { + VK_XLIB_FUNCTIONS(LOAD) + } +#undef LOAD + if (d->has_debug_utils) { +#define LOAD(name) name = (PFN_##name)vkGetInstanceProcAddr(d->instance, #name); + VK_DEBUG_FUNCTIONS(LOAD) +#undef LOAD + if (!vkCreateDebugUtilsMessengerEXT || !vkDestroyDebugUtilsMessengerEXT) d->has_debug_utils = false; + } + return true; +} + +static bool +load_device_functions(Driver *d) { +#define LOAD(name) \ + if (!(name = (PFN_##name)vkGetDeviceProcAddr(d->device, #name))) { \ + set_error(d, "Could not load the Vulkan device function: %s", #name); \ + return false; \ + } + VK_DEVICE_FUNCTIONS(LOAD) +#undef LOAD + return true; +} +// }}} + +// Instance and device {{{ +static bool +has_extension(const VkExtensionProperties *exts, uint32_t count, const char *name) { + for (uint32_t i = 0; i < count; i++) { + if (strcmp(exts[i].extensionName, name) == 0) return true; + } + return false; +} + +static bool +create_instance(Driver *d, GPUPlatform platform) { + uint32_t version = 0; + if (vkEnumerateInstanceVersion(&version) != VK_SUCCESS || + version < VK_MAKE_API_VERSION(0, VULKAN_REQUIRED_VERSION_MAJOR, VULKAN_REQUIRED_VERSION_MINOR, 0)) { + set_error( + d, + "The Vulkan loader supports version %u.%u, at least %d.%d is needed", + VK_API_VERSION_MAJOR(version), + VK_API_VERSION_MINOR(version), + VULKAN_REQUIRED_VERSION_MAJOR, + VULKAN_REQUIRED_VERSION_MINOR); + return false; + } + uint32_t num_exts = 0; + vkEnumerateInstanceExtensionProperties(NULL, &num_exts, NULL); + VkExtensionProperties *exts = calloc(MAX(1u, num_exts), sizeof(VkExtensionProperties)); + if (!exts) { + set_error(d, "Out of memory"); + return false; + } + vkEnumerateInstanceExtensionProperties(NULL, &num_exts, exts); + const char *platform_ext = platform == GPU_PLATFORM_WAYLAND ? VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME : VK_KHR_XLIB_SURFACE_EXTENSION_NAME; + const char *enabled[4]; + uint32_t num_enabled = 0; + const char *required[] = {VK_KHR_SURFACE_EXTENSION_NAME, platform_ext}; + for (size_t i = 0; i < arraysz(required); i++) { + if (!has_extension(exts, num_exts, required[i])) { + set_error(d, "The Vulkan instance does not support the required extension: %s", required[i]); + free(exts); + return false; + } + enabled[num_enabled++] = required[i]; + } + if (d->params.debug_rendering && has_extension(exts, num_exts, VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) { + enabled[num_enabled++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; + d->has_debug_utils = true; + } + free(exts); + // Enable the validation layer when debugging rendering, if it is installed + const char *layers[1]; + uint32_t num_layers = 0; + if (d->params.debug_rendering) { + uint32_t n = 0; + vkEnumerateInstanceLayerProperties(&n, NULL); + VkLayerProperties *props = calloc(MAX(1u, n), sizeof(VkLayerProperties)); + if (props) { + vkEnumerateInstanceLayerProperties(&n, props); + for (uint32_t i = 0; i < n; i++) { + if (strcmp(props[i].layerName, "VK_LAYER_KHRONOS_validation") == 0) layers[num_layers++] = "VK_LAYER_KHRONOS_validation"; + } + free(props); + } + debug_print(d, "Vulkan validation layer is %s", num_layers ? "enabled" : "not installed"); + } + const VkApplicationInfo app = { + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .pApplicationName = "kitty", + .pEngineName = "kitty", + .apiVersion = VK_MAKE_API_VERSION(0, VULKAN_REQUIRED_VERSION_MAJOR, VULKAN_REQUIRED_VERSION_MINOR, 0), + }; + const VkDebugUtilsMessengerCreateInfoEXT messenger_info = { + .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, + .messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT, + .messageType = + VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT, + .pfnUserCallback = debug_messenger, + .pUserData = d, + }; + const VkInstanceCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pNext = d->has_debug_utils ? &messenger_info : NULL, + .pApplicationInfo = &app, + .enabledExtensionCount = num_enabled, + .ppEnabledExtensionNames = enabled, + .enabledLayerCount = num_layers, + .ppEnabledLayerNames = layers, + }; + VkResult r = vkCreateInstance(&info, NULL, &d->instance); + if (r != VK_SUCCESS) { + set_error(d, "Failed to create the Vulkan instance with error: %s", vk_result_string(r)); + return false; + } + if (!load_instance_functions(d)) return false; + if (d->has_debug_utils) { + if (vkCreateDebugUtilsMessengerEXT(d->instance, &messenger_info, NULL, &d->messenger) != VK_SUCCESS) d->messenger = VK_NULL_HANDLE; + } + return true; +} + +static const char *required_device_extensions[] = { + VK_KHR_SWAPCHAIN_EXTENSION_NAME, + VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME, +}; + +// Why the physical device cannot be used, NULL if it can. Sets queue_family +// to a queue family that supports graphics and presentation. +static const char * +unsuitable_device(Driver *d, VkPhysicalDevice dev, uint32_t *queue_family, bool *has_mutable_format) { + static char reason[512]; + VkPhysicalDeviceProperties props; + vkGetPhysicalDeviceProperties(dev, &props); + if (props.apiVersion < VK_MAKE_API_VERSION(0, VULKAN_REQUIRED_VERSION_MAJOR, VULKAN_REQUIRED_VERSION_MINOR, 0)) { + snprintf(reason, sizeof(reason), "it supports only Vulkan %u.%u", VK_API_VERSION_MAJOR(props.apiVersion), VK_API_VERSION_MINOR(props.apiVersion)); + return reason; + } + uint32_t num_exts = 0; + vkEnumerateDeviceExtensionProperties(dev, NULL, &num_exts, NULL); + VkExtensionProperties *exts = calloc(MAX(1u, num_exts), sizeof(VkExtensionProperties)); + if (!exts) return "out of memory"; + vkEnumerateDeviceExtensionProperties(dev, NULL, &num_exts, exts); + for (size_t i = 0; i < arraysz(required_device_extensions); i++) { + if (!has_extension(exts, num_exts, required_device_extensions[i])) { + snprintf(reason, sizeof(reason), "it does not support the extension: %s", required_device_extensions[i]); + free(exts); + return reason; + } + } + *has_mutable_format = has_extension(exts, num_exts, VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME); + free(exts); + VkPhysicalDeviceVulkan13Features f13 = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES}; + VkPhysicalDeviceVulkan11Features f11 = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES, .pNext = &f13}; + VkPhysicalDeviceFeatures2 features = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, .pNext = &f11}; + vkGetPhysicalDeviceFeatures2(dev, &features); + if (!f13.dynamicRendering) return "it does not support dynamic rendering"; + if (!f13.synchronization2) return "it does not support synchronization2"; + if (!f11.shaderDrawParameters) return "it does not support shader draw parameters"; + uint32_t n = 0; + vkGetPhysicalDeviceQueueFamilyProperties(dev, &n, NULL); + VkQueueFamilyProperties *families = calloc(MAX(1u, n), sizeof(VkQueueFamilyProperties)); + if (!families) return "out of memory"; + vkGetPhysicalDeviceQueueFamilyProperties(dev, &n, families); + bool found = false; + for (uint32_t i = 0; i < n && !found; i++) { + if ((families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && presentation_supported(d, dev, i)) { + *queue_family = i; + found = true; + } + } + free(families); + if (!found) return "it has no queue that can render and present to windows on this display"; + return NULL; +} + +static bool +create_device(Driver *d) { + uint32_t n = 0; + VkResult r = vkEnumeratePhysicalDevices(d->instance, &n, NULL); + if (r != VK_SUCCESS || !n) { + set_error(d, "No Vulkan devices found"); + return false; + } + VkPhysicalDevice *devices = calloc(n, sizeof(VkPhysicalDevice)); + if (!devices) { + set_error(d, "Out of memory"); + return false; + } + vkEnumeratePhysicalDevices(d->instance, &n, devices); + // Use the first suitable device, in the order of the Vulkan loader, which + // puts the device driving the display first, honoring user preferences + // such as DRI_PRIME and MESA_VK_DEVICE_SELECT. + char reasons[2048] = {0}; + size_t pos = 0; + for (uint32_t i = 0; i < n && !d->physical_device; i++) { + uint32_t qf = 0; + bool mutable_format = false; + const char *reason = unsuitable_device(d, devices[i], &qf, &mutable_format); + VkPhysicalDeviceProperties props; + vkGetPhysicalDeviceProperties(devices[i], &props); + if (reason) { + debug_print(d, "Vulkan: Not using the device %s as %s", props.deviceName, reason); + if (pos < sizeof(reasons)) pos += snprintf(reasons + pos, sizeof(reasons) - pos, "%s%s: %s", pos ? "; " : "", props.deviceName, reason); + continue; + } + d->physical_device = devices[i]; + d->queue_family = qf; + d->has_swapchain_mutable_format = mutable_format; + d->properties = props; + } + free(devices); + if (!d->physical_device) { + set_error(d, "No suitable Vulkan device found: %s", reasons); + return false; + } + vkGetPhysicalDeviceMemoryProperties(d->physical_device, &d->memory_properties); + const float priority = 1.f; + const VkDeviceQueueCreateInfo queue_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueFamilyIndex = d->queue_family, + .queueCount = 1, + .pQueuePriorities = &priority, + }; + VkPhysicalDeviceVulkan13Features f13 = { + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES, + .dynamicRendering = VK_TRUE, + .synchronization2 = VK_TRUE, + }; + VkPhysicalDeviceVulkan11Features f11 = { + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES, + .pNext = &f13, + .shaderDrawParameters = VK_TRUE, + }; + const VkPhysicalDeviceFeatures2 features = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, .pNext = &f11}; + const char *exts[arraysz(required_device_extensions) + 1]; + uint32_t num_exts = 0; + for (size_t i = 0; i < arraysz(required_device_extensions); i++) exts[num_exts++] = required_device_extensions[i]; + if (d->has_swapchain_mutable_format) exts[num_exts++] = VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME; + const VkDeviceCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .pNext = &features, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &queue_info, + .enabledExtensionCount = num_exts, + .ppEnabledExtensionNames = exts, + }; + r = vkCreateDevice(d->physical_device, &info, NULL, &d->device); + if (r != VK_SUCCESS) { + set_error(d, "Failed to create the Vulkan device for %s with error: %s", d->properties.deviceName, vk_result_string(r)); + return false; + } + if (!load_device_functions(d)) return false; + vkGetDeviceQueue(d->device, d->queue_family, 0, &d->queue); + return true; +} + +static bool +create_shared_objects(Driver *d) { + // The indices of the two triangles covering a quad, see draw_quad() in ../api.h + static const uint16_t indices[6] = {0, 1, 2, 0, 2, 3}; + if (!create_buffer_with_memory(d, sizeof(indices), VK_BUFFER_USAGE_INDEX_BUFFER_BIT, MEMORY_UPLOAD, &d->quad_indices, &d->quad_indices_alloc)) return false; + memcpy(d->quad_indices_alloc.mapped, indices, sizeof(indices)); + for (size_t i = 0; i < NUM_CONTEXTS; i++) { + Context *c = d->contexts + i; + const VkCommandPoolCreateInfo pool_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + .flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, + .queueFamilyIndex = d->queue_family, + }; + if (vkCreateCommandPool(d->device, &pool_info, NULL, &c->pool) != VK_SUCCESS) { + set_error(d, "Failed to create a Vulkan command pool"); + return false; + } + const VkCommandBufferAllocateInfo alloc_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = c->pool, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1, + }; + if (vkAllocateCommandBuffers(d->device, &alloc_info, &c->cmd) != VK_SUCCESS) { + set_error(d, "Failed to allocate a Vulkan command buffer"); + return false; + } + const VkFenceCreateInfo fence_info = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; + if (vkCreateFence(d->device, &fence_info, NULL, &c->fence) != VK_SUCCESS) { + set_error(d, "Failed to create a Vulkan fence"); + return false; + } + } + d->next_serial = 1; + return create_samplers(d); +} +// }}} + +// Lifecycle {{{ +static const char * +shader_target(void *driver UNUSED) { +#define S(x) #x +#define TARGET(v) "spirv-" S(v) + return TARGET(SPIRV_VERSION); +#undef TARGET +#undef S +} + +static bool +connect_to_display(void *driver, const GPUNativeWindow *display) { + Driver *d = driver; + if (display->platform != GPU_PLATFORM_X11 && display->platform != GPU_PLATFORM_WAYLAND) { + set_error(d, "The Vulkan driver supports only X11 and Wayland"); + return false; + } + d->platform = display->platform; + d->native_display = display->display; + if (!load_libvulkan(d) || !create_instance(d, display->platform) || !create_device(d) || !create_shared_objects(d)) return false; + snprintf( + d->version_string, + sizeof(d->version_string), + "Vulkan %u.%u.%u on %s", + VK_API_VERSION_MAJOR(d->properties.apiVersion), + VK_API_VERSION_MINOR(d->properties.apiVersion), + VK_API_VERSION_PATCH(d->properties.apiVersion), + d->properties.deviceName); + debug_print(d, "Using %s", d->version_string); + return true; +} + +static bool +initialize(void *driver) { + Driver *d = driver; + if (!d->device) { + set_error(d, "The Vulkan driver was not connected to a display"); + return false; + } + d->initialized = true; + return true; +} + +static void +destroy(void *driver) { + Driver *d = driver; + if (d->device) { + vkDeviceWaitIdle(d->device); + release_native_resources(d); + destroy_contexts(d); + destroy_all_pipelines(d); + destroy_all_textures(d); + destroy_all_buffers(d); + while (d->vertex_layouts) vertex_layout_destroy(d, AS_HANDLE(d->vertex_layouts)); + destroy_samplers(d); + if (d->quad_indices) { + vkDestroyBuffer(d->device, d->quad_indices, NULL); + free_memory(d, &d->quad_indices_alloc); + } + for (size_t i = 0; i < d->num_free_semaphores; i++) vkDestroySemaphore(d->device, d->free_semaphores[i], NULL); + free_all_memory(d); + vkDestroyDevice(d->device, NULL); + } + if (d->instance) { + if (d->messenger) vkDestroyDebugUtilsMessengerEXT(d->instance, d->messenger, NULL); + vkDestroyInstance(d->instance, NULL); + } + // The library is not unloaded, as with the drivers themselves, see unload_gpu_driver() + if (the_driver == d) the_driver = NULL; + free(d); +} + +static const char * +last_error(void *driver) { + return ((Driver *)driver)->last_error; +} + +static const char * +version_string(void *driver) { + return ((Driver *)driver)->version_string; +} + +static void +get_capabilities(void *driver, GPUCapabilities *ans) { + Driver *d = driver; + ans->max_texture_size = d->properties.limits.maxImageDimension2D; + ans->max_array_texture_layers = d->properties.limits.maxImageArrayLayers; + // Encoding to sRGB needs both UNORM and SRGB views of swapchain images + ans->supports_srgb_output = d->has_swapchain_mutable_format; +} +// }}} + +__attribute__((visibility("default"))) bool +kitty_gpu_driver_load(uint32_t api_version, const GPUDriverInitParams *params, GPUDriverAPI *api, void **driver, char *errbuf, size_t errbuf_size) { + if (api_version != KITTY_GPU_API_VERSION) { + snprintf(errbuf, errbuf_size, "The Vulkan driver supports GPU API version %u but version %u was requested", KITTY_GPU_API_VERSION, api_version); + return false; + } + if (!params->log_error || !params->fatal || !params->debug_print) { + snprintf(errbuf, errbuf_size, "The Vulkan driver was not passed all required callbacks"); + return false; + } + if (the_driver) { + snprintf(errbuf, errbuf_size, "The Vulkan driver has already been loaded"); + return false; + } + Driver *d = calloc(1, sizeof(Driver)); + if (!d) { + snprintf(errbuf, errbuf_size, "Out of memory allocating the Vulkan driver"); + return false; + } + d->params = *params; + the_driver = d; + *api = (GPUDriverAPI){ + .api_version = KITTY_GPU_API_VERSION, + .name = "Vulkan", +#define F(name) .name = name + F(shader_target), + .connect = connect_to_display, + F(initialize), + F(destroy), + F(last_error), + F(version_string), + F(get_capabilities), + + F(texture_create), + F(texture_reset), + F(texture_destroy), + F(texture_upload), + F(texture_copy), + F(texture_read), + F(texture_get_desc), + F(bind_texture), + + F(buffer_create), + F(buffer_destroy), + F(buffer_allocate), + F(buffer_map), + F(buffer_unmap), + F(buffer_copy), + F(bind_uniform_buffer), + + F(pipeline_create), + F(pipeline_destroy), + F(use_pipeline), + F(pipeline_uniform_block), + F(pipeline_attribute_location), + F(set_params), + + F(vertex_layout_create), + F(vertex_layout_destroy), + F(bind_vertex_layout), + F(vertex_layout_set_attribute), + + F(choose_native_visual), + F(surface_create), + F(surface_destroy), + F(make_surface_current), + F(get_current_surface), + F(surface_resize), + F(surface_present), + F(surface_set_swap_interval), + F(surface_native_changed), + F(surface_recreate), + F(release_native_resources), + + F(render_target_create), + F(render_target_destroy), + F(set_render_target), + F(get_render_target), + F(render_target_status), + F(clear), + F(set_viewport), + F(get_viewport), + F(set_scissor), + F(set_srgb_output), + F(read_pixels), + F(copy_to_texture), + + F(draw_quad), +#undef F + }; + *driver = d; + return true; +} diff --git a/gpu/vulkan/functions.h b/gpu/vulkan/functions.h new file mode 100644 index 000000000..9e62bc89e --- /dev/null +++ b/gpu/vulkan/functions.h @@ -0,0 +1,131 @@ +/* + * functions.h + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// The Vulkan functions used by the driver. libvulkan is loaded at runtime, +// so that kitty has no link time dependency on it, and these are loaded from +// it with vkGetInstanceProcAddr() and vkGetDeviceProcAddr(). + +#pragma once + +#define VK_NO_PROTOTYPES +#define VK_USE_PLATFORM_XLIB_KHR +#define VK_USE_PLATFORM_WAYLAND_KHR +#include + +// Functions that need no instance +#define VK_GLOBAL_FUNCTIONS(X) \ + X(vkCreateInstance) \ + X(vkEnumerateInstanceExtensionProperties) \ + X(vkEnumerateInstanceLayerProperties) \ + X(vkEnumerateInstanceVersion) + +#define VK_INSTANCE_FUNCTIONS(X) \ + X(vkDestroyInstance) \ + X(vkEnumeratePhysicalDevices) \ + X(vkGetPhysicalDeviceProperties) \ + X(vkGetPhysicalDeviceProperties2) \ + X(vkGetPhysicalDeviceFeatures2) \ + X(vkGetPhysicalDeviceQueueFamilyProperties) \ + X(vkGetPhysicalDeviceMemoryProperties) \ + X(vkGetPhysicalDeviceFormatProperties) \ + X(vkEnumerateDeviceExtensionProperties) \ + X(vkCreateDevice) \ + X(vkGetDeviceProcAddr) \ + X(vkDestroySurfaceKHR) \ + X(vkGetPhysicalDeviceSurfaceSupportKHR) \ + X(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) \ + X(vkGetPhysicalDeviceSurfaceFormatsKHR) \ + X(vkGetPhysicalDeviceSurfacePresentModesKHR) + +// Only the functions of the windowing system in use are loaded +#define VK_XLIB_FUNCTIONS(X) \ + X(vkCreateXlibSurfaceKHR) \ + X(vkGetPhysicalDeviceXlibPresentationSupportKHR) + +#define VK_WAYLAND_FUNCTIONS(X) \ + X(vkCreateWaylandSurfaceKHR) \ + X(vkGetPhysicalDeviceWaylandPresentationSupportKHR) + +// Loaded only if the debug utils extension is enabled +#define VK_DEBUG_FUNCTIONS(X) \ + X(vkCreateDebugUtilsMessengerEXT) \ + X(vkDestroyDebugUtilsMessengerEXT) \ + X(vkSetDebugUtilsObjectNameEXT) + +#define VK_DEVICE_FUNCTIONS(X) \ + X(vkDestroyDevice) \ + X(vkGetDeviceQueue) \ + X(vkDeviceWaitIdle) \ + X(vkQueueWaitIdle) \ + X(vkQueueSubmit2) \ + X(vkQueuePresentKHR) \ + X(vkCreateSwapchainKHR) \ + X(vkDestroySwapchainKHR) \ + X(vkGetSwapchainImagesKHR) \ + X(vkAcquireNextImageKHR) \ + X(vkCreateCommandPool) \ + X(vkDestroyCommandPool) \ + X(vkResetCommandPool) \ + X(vkAllocateCommandBuffers) \ + X(vkBeginCommandBuffer) \ + X(vkEndCommandBuffer) \ + X(vkCreateFence) \ + X(vkDestroyFence) \ + X(vkWaitForFences) \ + X(vkGetFenceStatus) \ + X(vkResetFences) \ + X(vkCreateSemaphore) \ + X(vkDestroySemaphore) \ + X(vkAllocateMemory) \ + X(vkFreeMemory) \ + X(vkMapMemory) \ + X(vkUnmapMemory) \ + X(vkCreateBuffer) \ + X(vkDestroyBuffer) \ + X(vkGetBufferMemoryRequirements) \ + X(vkBindBufferMemory) \ + X(vkCreateImage) \ + X(vkDestroyImage) \ + X(vkGetImageMemoryRequirements) \ + X(vkBindImageMemory) \ + X(vkCreateImageView) \ + X(vkDestroyImageView) \ + X(vkCreateSampler) \ + X(vkDestroySampler) \ + X(vkCreateShaderModule) \ + X(vkDestroyShaderModule) \ + X(vkCreateDescriptorSetLayout) \ + X(vkDestroyDescriptorSetLayout) \ + X(vkCreatePipelineLayout) \ + X(vkDestroyPipelineLayout) \ + X(vkCreateGraphicsPipelines) \ + X(vkDestroyPipeline) \ + X(vkCmdPipelineBarrier2) \ + X(vkCmdBeginRendering) \ + X(vkCmdEndRendering) \ + X(vkCmdBindPipeline) \ + X(vkCmdSetViewport) \ + X(vkCmdSetScissor) \ + X(vkCmdBindVertexBuffers2) \ + X(vkCmdBindIndexBuffer) \ + X(vkCmdDrawIndexed) \ + X(vkCmdPushConstants) \ + X(vkCmdPushDescriptorSetKHR) \ + X(vkCmdCopyBufferToImage) \ + X(vkCmdCopyImageToBuffer) \ + X(vkCmdCopyImage) \ + X(vkCmdBlitImage) \ + X(vkCmdClearAttachments) + +#define VK_DECLARE_FUNCTION(name) extern PFN_##name name; +VK_GLOBAL_FUNCTIONS(VK_DECLARE_FUNCTION) +VK_INSTANCE_FUNCTIONS(VK_DECLARE_FUNCTION) +VK_XLIB_FUNCTIONS(VK_DECLARE_FUNCTION) +VK_WAYLAND_FUNCTIONS(VK_DECLARE_FUNCTION) +VK_DEBUG_FUNCTIONS(VK_DECLARE_FUNCTION) +VK_DEVICE_FUNCTIONS(VK_DECLARE_FUNCTION) +extern PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr; diff --git a/gpu/vulkan/internal.h b/gpu/vulkan/internal.h new file mode 100644 index 000000000..eb188f031 --- /dev/null +++ b/gpu/vulkan/internal.h @@ -0,0 +1,454 @@ +/* + * internal.h + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// Types and functions shared between the files of the Vulkan driver. +// +// Overview of how the driver implements the immediate mode API in ../api.h: +// +// * Commands are recorded into the command buffer of the current Context, one +// of a small ring of them. A Context is submitted when a surface is +// presented, or when the CPU needs the results, such as when reading back +// pixels. Before a Context is reused, its fence is waited on, which also +// throttles the CPU to at most a few submissions ahead of the GPU. +// * Every submission has a serial number. Objects that recorded commands +// might still use are destroyed, or reused, only once the serial of the +// last submission that could use them has completed. +// * Rendering uses dynamic rendering. A render pass instance is begun lazily, +// by the first draw or clear, and ended when the render target changes or +// commands that cannot be recorded inside render pass instances, such as +// copies, are recorded. Image layouts are tracked per image and transitions +// are recorded as needed. +// * Buffers live in memory the CPU can write to and are only ever written by +// the CPU. Writing to a buffer that recorded commands might still read +// gives it new storage, so that, as with OpenGL, commands use the contents +// the buffer had when they were recorded. +// * Textures and uniform buffers are bound with push descriptors and the +// uniform parameters of pipelines are push constants. + +#pragma once + +#define _GNU_SOURCE // for strdup() and strnlen() +#include "../api.h" +#include "functions.h" +#include +#include +#include +#include + +#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)) +#define AS_HANDLE(ptr) ((uint64_t)(uintptr_t)(ptr)) + +// The number of Contexts, see above +#define NUM_CONTEXTS 3 +// The maximum number of binding slots of textures and uniform buffers, the +// slots used by kitty are much smaller than this +#define MAX_BINDING_SLOTS 32 +// The maximum number of vertex inputs +#define MAX_VERTEX_ATTRIBUTES 16 +// The size of the push constant block guaranteed by Vulkan, see GPUParamBlock +#define PUSH_CONSTANTS_SIZE 128 + +// Memory {{{ +typedef enum { + MEMORY_DEVICE, // device local memory for images + MEMORY_UPLOAD, // host visible memory, preferably also device local, for buffers read by the GPU + MEMORY_READBACK, // host visible memory, preferably cached, for reading back from the GPU +} MemoryUsage; + +typedef struct MemoryBlock MemoryBlock; + +typedef struct Allocation { + VkDeviceMemory memory; + VkDeviceSize offset, size; + void *mapped; // NULL if not host visible + MemoryBlock *block; // NULL for dedicated allocations +} Allocation; +// }}} + +// Command recording {{{ +typedef struct Garbage Garbage; + +typedef struct Context { + VkCommandPool pool; + VkCommandBuffer cmd; + VkFence fence; + uint64_t serial; // the serial of the submission of this context + bool recording; // commands are being recorded into cmd + bool submitted; // submitted and the fence has not yet been waited on + Garbage *garbage; // objects to destroy once this context completes + // Staging memory for uploads and reads, released once the context completes + struct { + VkBuffer buffer; + Allocation alloc; + VkDeviceSize used; + } staging[64]; + size_t num_staging; + // Semaphores the submission must wait on, from acquiring swapchain images + VkSemaphore waits[8]; + size_t num_waits; + // Semaphores used for acquiring swapchain images, recycled once the context completes + VkSemaphore used_semaphores[8]; + size_t num_used_semaphores; +} Context; + +// The way an image was last accessed, for recording barriers +typedef struct ImageState { + VkImageLayout layout; + VkPipelineStageFlags2 stage; + VkAccessFlags2 access; +} ImageState; + +// A Vulkan image together with its views, used by textures and swapchains +typedef struct Image { + VkImage image; + Allocation alloc; // not used for swapchain images + VkFormat format; // the format of the image, for sampling + VkFormat unorm_format, srgb_format; // for render target views, srgb_format is zero if not supported + VkImageView sampled_view; + VkImageView unorm_view, srgb_view; // views for rendering, NULL if the image cannot be rendered to + uint32_t width, height, layers; + ImageState state; + uint64_t last_use_serial; +} Image; +// }}} + +// Resources {{{ +typedef struct Texture { + GPUTextureDesc desc; + Image image; + VkSampler sampler; + struct Texture *prev, *next; +} Texture; + +typedef struct BufferStorage { + VkBuffer buffer; + Allocation alloc; + VkDeviceSize size; + uint64_t last_use_serial; +} BufferStorage; + +typedef struct Buffer { + GPUBufferKind kind; + GPUUpdateFrequency frequency; + BufferStorage storage; // the current storage, size zero if not yet allocated + // Storage that commands might still use, reusable once the serial completes + BufferStorage retired[4]; + size_t num_retired; + struct Buffer *prev, *next; +} Buffer; + +typedef struct VertexAttribute { + bool enabled; + Buffer *buffer; + uint32_t components, stride, divisor; + GPUAttribType type; + VkDeviceSize offset; +} VertexAttribute; + +typedef struct VertexLayout { + VertexAttribute attributes[MAX_VERTEX_ATTRIBUTES]; + struct VertexLayout *prev, *next; +} VertexLayout; + +typedef struct PipelineVariantKey { + VkFormat color_format; + bool blend; + // The format and input rate of each vertex attribute, format is zero for unused attributes + VkFormat attribute_formats[MAX_VERTEX_ATTRIBUTES]; + bool attribute_per_instance[MAX_VERTEX_ATTRIBUTES]; +} PipelineVariantKey; + +typedef struct PipelineVariant { + PipelineVariantKey key; + VkPipeline pipeline; +} PipelineVariant; + +typedef struct PipelineBinding { + GPUBindingKind kind; + uint32_t slot; + char *name; +} PipelineBinding; + +typedef struct ParamBlock { + int32_t slot; // -1 for the push constant block + uint32_t size; + Buffer *buffer; // the uniform buffer holding the contents of blocks with a slot +} ParamBlock; + +typedef struct Pipeline { + VkShaderModule modules[2]; + size_t num_modules; + VkPipelineShaderStageCreateInfo stages[2]; + char *entry_points[2]; + VkDescriptorSetLayout set_layout; + VkPipelineLayout layout; + PipelineBinding *bindings; + size_t num_bindings; + ParamBlock *param_blocks; + size_t num_param_blocks; + uint8_t push_constants[PUSH_CONSTANTS_SIZE]; + uint32_t push_constants_size; + uint32_t vertex_inputs; // bit mask of the locations of the inputs of the vertex shader + PipelineVariant *variants; + size_t num_variants, variants_capacity; + struct Pipeline *prev, *next; +} Pipeline; + +typedef struct RenderTarget { + Texture *texture; +} RenderTarget; +// }}} + +// Surfaces {{{ +typedef struct Swapchain { + VkSwapchainKHR swapchain; + Image *images; + VkSemaphore *present_semaphores; // one per image, signalled when rendering to the image is done + uint32_t num_images; + VkExtent2D extent; + VkFormat format; + VkPresentModeKHR present_mode; +} Swapchain; + +typedef struct Surface { + GPUPlatform platform; + VkSurfaceKHR surface; + GPUSurfaceConfig config; + Swapchain swapchain; + uint32_t width, height; // the size of the window framebuffer + int swap_interval; + bool needs_recreate; // the swapchain must be recreated before it is next used + bool image_acquired; // an image has been acquired and not yet presented + uint32_t image_index; + struct Surface *prev, *next; +} Surface; +// }}} + +typedef struct Driver { + GPUDriverInitParams params; + char last_error[8192]; + char version_string[512]; + void *libvulkan; + + VkInstance instance; + VkDebugUtilsMessengerEXT messenger; + VkPhysicalDevice physical_device; + VkPhysicalDeviceProperties properties; + VkPhysicalDeviceMemoryProperties memory_properties; + VkDevice device; + uint32_t queue_family; + VkQueue queue; + GPUPlatform platform; + void *native_display; + bool has_debug_utils, has_swapchain_mutable_format; + bool initialized; + + // Command recording + Context contexts[NUM_CONTEXTS]; + size_t current_context; // index into contexts of the context being recorded or next to be recorded + uint64_t next_serial, completed_serial; + VkSemaphore free_semaphores[64]; + size_t num_free_semaphores; + MemoryBlock *memory_blocks; + + // Shared objects + VkBuffer quad_indices; + Allocation quad_indices_alloc; + VkSampler samplers[2][4][2]; // [filter][wrap][is_integer] + + // Live objects, destroyed with the driver + Texture *textures; + Buffer *buffers; + Pipeline *pipelines; + VertexLayout *vertex_layouts; + Surface *surfaces; + + // Current state, see "current" state in ../api.h + Surface *current_surface; + RenderTarget *render_target; // NULL for the window + int32_t viewport[4]; + struct { + bool enabled; + int32_t rect[4]; + } scissor; + bool srgb_output; + Pipeline *pipeline; + VertexLayout *vertex_layout; + Texture *bound_textures[MAX_BINDING_SLOTS]; + Buffer *bound_buffers[MAX_BINDING_SLOTS]; + + // State of the command buffer being recorded + struct { + bool active; // a render pass instance is active + Image *image; // the image being rendered to + VkImageView view; + VkFormat format; + VkExtent2D extent; + } rendering; + struct { + VkPipeline pipeline; // the bound pipeline + Pipeline *push_constants_of; // the pipeline whose push constants were last pushed + bool push_constants_dirty; + bool descriptors_dirty; + int32_t viewport[4]; + int32_t scissor[4]; + bool viewport_valid, scissor_valid; + } cmd_state; +} Driver; + +// driver.c +void set_error(Driver *d, const char *fmt, ...) __attribute__((format(printf, 2, 3))); +void log_error(Driver *d, const char *fmt, ...) __attribute__((format(printf, 2, 3))); +void debug_print(Driver *d, const char *fmt, ...) __attribute__((format(printf, 2, 3))); +void fatal(Driver *d, const char *fmt, ...) __attribute__((format(printf, 2, 3), noreturn)); +const char *vk_result_string(VkResult r); +// Calls fatal() if r is not VK_SUCCESS +void check_vk(Driver *d, VkResult r, const char *what); + +// memory.c +bool allocate_memory(Driver *d, const VkMemoryRequirements *req, MemoryUsage usage, Allocation *ans); +void free_memory(Driver *d, Allocation *a); +void free_all_memory(Driver *d); +bool create_buffer_with_memory(Driver *d, VkDeviceSize size, VkBufferUsageFlags usage, MemoryUsage mem_usage, VkBuffer *buffer, Allocation *alloc); +// Staging memory for uploads in the current context, valid until the context completes +bool allocate_staging(Driver *d, VkDeviceSize size, VkDeviceSize alignment, VkBuffer *buffer, VkDeviceSize *offset, void **mapped); +void release_staging(Driver *d, Context *c); +gpu_buffer_t buffer_create(void *driver, GPUBufferKind kind); +void buffer_destroy(void *driver, gpu_buffer_t buffer); +void buffer_allocate(void *driver, gpu_buffer_t buffer, size_t size, GPUUpdateFrequency frequency); +void *buffer_map(void *driver, gpu_buffer_t buffer, GPUMapMode mode, size_t offset, size_t size); +void buffer_unmap(void *driver, gpu_buffer_t buffer); +void buffer_copy(void *driver, gpu_buffer_t src, size_t src_offset, gpu_buffer_t dst, size_t dst_offset, size_t size); +void bind_uniform_buffer(void *driver, uint32_t slot, gpu_buffer_t buffer); +// Mark the current storage of the buffer as used by the commands being recorded +void use_buffer(Driver *d, Buffer *b); +// Write data to the start of the buffer, giving it new storage if commands might still use the current one +bool write_buffer(Driver *d, Buffer *b, const void *data, size_t size); +void destroy_all_buffers(Driver *d); + +// commands.c +// The context being recorded, beginning recording if needed +Context *current_context(Driver *d); +// The command buffer to record into, beginning recording if needed +VkCommandBuffer command_buffer(Driver *d); +// Submit the commands recorded so far. If wait is true, waits for them to complete. +void submit_commands(Driver *d, const VkSemaphore *signal, size_t num_signal, bool wait); +// Wait for submissions until serial has completed +void wait_for_serial(Driver *d, uint64_t serial); +// The serial of the commands being recorded +uint64_t recording_serial(Driver *d); +// Destroy objects once the commands being recorded have completed, see Garbage +void defer_destroy_buffer(Driver *d, VkBuffer buffer, Allocation *alloc); +void defer_destroy_image(Driver *d, Image *image); +void defer_destroy_pipeline(Driver *d, VkPipeline pipeline); +void defer_destroy_pipeline_objects(Driver *d, VkPipelineLayout layout, VkDescriptorSetLayout set_layout, VkShaderModule *modules, size_t num_modules); +void destroy_contexts(Driver *d); +VkSemaphore get_semaphore(Driver *d); +void add_wait_semaphore(Driver *d, VkSemaphore s); +void transition_image(Driver *d, Image *image, VkImageLayout layout, VkPipelineStageFlags2 stage, VkAccessFlags2 access); +void end_rendering(Driver *d); +void clear(void *driver, float r, float g, float b, float a); +void set_viewport(void *driver, int32_t x, int32_t y, uint32_t width, uint32_t height); +void get_viewport(void *driver, int32_t ans[4]); +void set_scissor(void *driver, bool enabled, int32_t x, int32_t y, uint32_t width, uint32_t height); +void set_srgb_output(void *driver, bool enabled); +void read_pixels(void *driver, int32_t x, int32_t y, uint32_t width, uint32_t height, void *dst); +void copy_to_texture(void *driver, gpu_texture_t dst, int32_t x, int32_t y, uint32_t width, uint32_t height); +gpu_render_target_t render_target_create(void *driver, gpu_texture_t color_texture); +void render_target_destroy(void *driver, gpu_render_target_t target); +void set_render_target(void *driver, gpu_render_target_t target); +gpu_render_target_t get_render_target(void *driver); +const char *render_target_status(void *driver, gpu_render_target_t target); +void draw_quad(void *driver, uint32_t instance_count, GPUBlendMode blend); + +// textures.c +bool create_samplers(Driver *d); +void destroy_samplers(Driver *d); +VkFormat vk_format_for(GPUTextureFormat f); +bool create_image_views(Driver *d, Image *img, VkImageViewType sampled_type); +void destroy_image_views(Driver *d, Image *img); +gpu_texture_t texture_create(void *driver, const GPUTextureDesc *desc, const GPUPixelData *initial); +bool texture_reset(void *driver, gpu_texture_t texture, const GPUTextureDesc *desc, const GPUPixelData *initial); +void texture_destroy(void *driver, gpu_texture_t texture); +void texture_upload(void *driver, gpu_texture_t texture, uint32_t x, uint32_t y, uint32_t layer, uint32_t width, uint32_t height, const GPUPixelData *pixels); +void texture_copy(void *driver, gpu_texture_t src, gpu_texture_t dst); +bool texture_read(void *driver, gpu_texture_t texture, GPUPixelLayout layout, void *dst, size_t dst_size); +void texture_get_desc(void *driver, gpu_texture_t texture, GPUTextureDesc *ans); +void bind_texture(void *driver, uint32_t slot, gpu_texture_t texture); +void destroy_all_textures(Driver *d); +// Convert pixels read back from an image of the specified format to tightly packed RGBA8 +void convert_to_rgba8(VkFormat format, const uint8_t *src, uint8_t *dst, size_t num_pixels); + +// pipelines.c +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, + size_t num_native_names); +void pipeline_destroy(void *driver, gpu_pipeline_t pipeline); +void use_pipeline(void *driver, gpu_pipeline_t pipeline); +bool pipeline_uniform_block(void *driver, gpu_pipeline_t pipeline, const char *name, GPUUniformBlockInfo *ans); +int32_t pipeline_attribute_location(void *driver, gpu_pipeline_t pipeline, const char *name); +void set_params(void *driver, gpu_pipeline_t pipeline, uint32_t block, const void *data, size_t size); +gpu_vertex_layout_t vertex_layout_create(void *driver); +void vertex_layout_destroy(void *driver, gpu_vertex_layout_t layout); +void bind_vertex_layout(void *driver, gpu_vertex_layout_t layout); +void vertex_layout_set_attribute( + void *driver, + gpu_vertex_layout_t layout, + uint32_t location, + gpu_buffer_t buffer, + uint32_t components, + GPUAttribType type, + uint32_t stride, + size_t offset, + uint32_t instance_divisor); +// The VkPipeline for drawing with the current state, NULL on failure +VkPipeline pipeline_for_current_state(Driver *d, bool blend); +VkFormat vertex_attribute_format(uint32_t components, GPUAttribType type); +void destroy_all_pipelines(Driver *d); + +// surfaces.c +bool choose_native_visual(void *driver, const GPUNativeWindow *native, const GPUSurfaceConfig *config, uint64_t *visual_id); +gpu_surface_t surface_create(void *driver, const GPUNativeWindow *native, const GPUSurfaceConfig *config, uint32_t width, uint32_t height); +void surface_destroy(void *driver, gpu_surface_t surface); +bool make_surface_current(void *driver, gpu_surface_t surface); +gpu_surface_t get_current_surface(void *driver); +void surface_resize(void *driver, gpu_surface_t surface, uint32_t width, uint32_t height); +bool surface_present(void *driver, gpu_surface_t surface); +void surface_set_swap_interval(void *driver, gpu_surface_t surface, int interval); +void surface_native_changed(void *driver, gpu_surface_t surface); +bool surface_recreate(void *driver, gpu_surface_t surface); +void release_native_resources(void *driver); +// The image of the current surface to render into, acquiring it if needed. NULL on failure. +Image *current_surface_image(Driver *d); +// Whether the physical device can present to windows on the display +bool presentation_supported(Driver *d, VkPhysicalDevice device, uint32_t queue_family); + +// Intrusive doubly linked lists of live objects +#define LIST_ADD(head, item) \ + do { \ + (item)->prev = NULL; \ + (item)->next = (head); \ + if (head) (head)->prev = (item); \ + (head) = (item); \ + } while (0) +#define LIST_REMOVE(head, item) \ + do { \ + if ((item)->prev) (item)->prev->next = (item)->next; \ + else (head) = (item)->next; \ + if ((item)->next) (item)->next->prev = (item)->prev; \ + (item)->prev = (item)->next = NULL; \ + } while (0) diff --git a/gpu/vulkan/memory.c b/gpu/vulkan/memory.c new file mode 100644 index 000000000..fd37ebdff --- /dev/null +++ b/gpu/vulkan/memory.c @@ -0,0 +1,484 @@ +/* + * memory.c + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// Device memory, staging memory and buffers + +#include "internal.h" + +// Memory allocation {{{ +// Vulkan implementations limit the number of device memory allocations, to +// as few as 4096, so small resources are sub-allocated from large blocks. +// Blocks are used either only for buffers or only for images, so that +// bufferImageGranularity never needs to be considered. + +#define BLOCK_SIZE (32u * 1024u * 1024u) +#define DEDICATED_THRESHOLD (BLOCK_SIZE / 4) + +typedef struct FreeRange { + VkDeviceSize offset, size; +} FreeRange; + +struct MemoryBlock { + VkDeviceMemory memory; + uint32_t memory_type; + MemoryUsage usage; + void *mapped; + VkDeviceSize size; + FreeRange *free_ranges; // sorted by offset, non-adjacent + size_t num_free, free_capacity; + MemoryBlock *next; +}; + +static int +find_memory_type(Driver *d, uint32_t type_bits, MemoryUsage usage) { + const VkPhysicalDeviceMemoryProperties *p = &d->memory_properties; + VkMemoryPropertyFlags required = 0, preferred = 0; + switch (usage) { + case MEMORY_DEVICE: preferred = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; break; + case MEMORY_UPLOAD: + required = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + preferred = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + break; + case MEMORY_READBACK: + required = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + preferred = VK_MEMORY_PROPERTY_HOST_CACHED_BIT; + break; + } + int fallback = -1; + for (uint32_t i = 0; i < p->memoryTypeCount; i++) { + if (!(type_bits & (1u << i))) continue; + const VkMemoryPropertyFlags f = p->memoryTypes[i].propertyFlags; + if ((f & required) != required) continue; + if ((f & preferred) == preferred) return (int)i; + if (fallback < 0) fallback = (int)i; + } + return fallback; +} + +static bool +allocate_device_memory(Driver *d, VkDeviceSize size, uint32_t memory_type, VkDeviceMemory *memory, void **mapped) { + const VkMemoryAllocateInfo info = {.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = size, .memoryTypeIndex = memory_type}; + VkResult r = vkAllocateMemory(d->device, &info, NULL, memory); + if (r != VK_SUCCESS) { + set_error(d, "Failed to allocate %llu bytes of GPU memory with error: %s", (unsigned long long)size, vk_result_string(r)); + return false; + } + *mapped = NULL; + if (d->memory_properties.memoryTypes[memory_type].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { + r = vkMapMemory(d->device, *memory, 0, VK_WHOLE_SIZE, 0, mapped); + if (r != VK_SUCCESS) { + vkFreeMemory(d->device, *memory, NULL); + set_error(d, "Failed to map GPU memory with error: %s", vk_result_string(r)); + return false; + } + } + return true; +} + +static bool +block_allocate(MemoryBlock *b, VkDeviceSize size, VkDeviceSize alignment, VkDeviceSize *offset) { + for (size_t i = 0; i < b->num_free; i++) { + FreeRange *r = b->free_ranges + i; + const VkDeviceSize start = (r->offset + alignment - 1) / alignment * alignment; + if (start + size > r->offset + r->size) continue; + const VkDeviceSize end = start + size, range_end = r->offset + r->size; + *offset = start; + if (start == r->offset && end == range_end) { + memmove(r, r + 1, (b->num_free - i - 1) * sizeof(FreeRange)); + b->num_free--; + } else if (start == r->offset) { + r->offset = end; + r->size = range_end - end; + } else if (end == range_end) { + r->size = start - r->offset; + } else { + // Split the range in two + if (b->num_free >= b->free_capacity) { + const size_t cap = MAX(16u, b->free_capacity * 2); + FreeRange *n = realloc(b->free_ranges, cap * sizeof(FreeRange)); + if (!n) return false; + b->free_ranges = n; + b->free_capacity = cap; + r = b->free_ranges + i; + } + memmove(r + 2, r + 1, (b->num_free - i - 1) * sizeof(FreeRange)); + b->num_free++; + r[1] = (FreeRange){.offset = end, .size = range_end - end}; + r->size = start - r->offset; + } + return true; + } + return false; +} + +static void +block_free(MemoryBlock *b, VkDeviceSize offset, VkDeviceSize size) { + size_t i = 0; + while (i < b->num_free && b->free_ranges[i].offset < offset) i++; + // Merge with the previous and next ranges if adjacent + const bool merge_prev = i > 0 && b->free_ranges[i - 1].offset + b->free_ranges[i - 1].size == offset; + const bool merge_next = i < b->num_free && offset + size == b->free_ranges[i].offset; + if (merge_prev && merge_next) { + b->free_ranges[i - 1].size += size + b->free_ranges[i].size; + memmove(b->free_ranges + i, b->free_ranges + i + 1, (b->num_free - i - 1) * sizeof(FreeRange)); + b->num_free--; + } else if (merge_prev) { + b->free_ranges[i - 1].size += size; + } else if (merge_next) { + b->free_ranges[i].offset = offset; + b->free_ranges[i].size += size; + } else { + if (b->num_free >= b->free_capacity) { + const size_t cap = MAX(16u, b->free_capacity * 2); + FreeRange *n = realloc(b->free_ranges, cap * sizeof(FreeRange)); + if (!n) return; // the range is leaked, harmless + b->free_ranges = n; + b->free_capacity = cap; + } + memmove(b->free_ranges + i + 1, b->free_ranges + i, (b->num_free - i) * sizeof(FreeRange)); + b->free_ranges[i] = (FreeRange){.offset = offset, .size = size}; + b->num_free++; + } +} + +static bool +allocate_from_block(MemoryBlock *b, const VkMemoryRequirements *req, Allocation *ans) { + VkDeviceSize offset; + if (!block_allocate(b, req->size, MAX(1u, req->alignment), &offset)) return false; + *ans = (Allocation){.memory = b->memory, .offset = offset, .size = req->size, .block = b, .mapped = b->mapped ? (uint8_t *)b->mapped + offset : NULL}; + return true; +} + +bool +allocate_memory(Driver *d, const VkMemoryRequirements *req, MemoryUsage usage, Allocation *ans) { + *ans = (Allocation){0}; + const int type = find_memory_type(d, req->memoryTypeBits, usage); + if (type < 0) { + set_error(d, "No suitable GPU memory type found"); + return false; + } + if (req->size >= DEDICATED_THRESHOLD) { + if (!allocate_device_memory(d, req->size, (uint32_t)type, &ans->memory, &ans->mapped)) return false; + ans->size = req->size; + return true; + } + for (MemoryBlock *b = d->memory_blocks; b; b = b->next) { + if (b->memory_type == (uint32_t)type && b->usage == usage && allocate_from_block(b, req, ans)) return true; + } + MemoryBlock *b = calloc(1, sizeof(MemoryBlock)); + if (!b) { + set_error(d, "Out of memory"); + return false; + } + if (!allocate_device_memory(d, BLOCK_SIZE, (uint32_t)type, &b->memory, &b->mapped)) { + free(b); + return false; + } + b->memory_type = (uint32_t)type; + b->usage = usage; + b->size = BLOCK_SIZE; + b->free_capacity = 16; + b->free_ranges = calloc(b->free_capacity, sizeof(FreeRange)); + if (!b->free_ranges) { + vkFreeMemory(d->device, b->memory, NULL); + free(b); + set_error(d, "Out of memory"); + return false; + } + b->free_ranges[0] = (FreeRange){.offset = 0, .size = BLOCK_SIZE}; + b->num_free = 1; + b->next = d->memory_blocks; + d->memory_blocks = b; + if (!allocate_from_block(b, req, ans)) { + set_error(d, "Failed to sub-allocate %llu bytes of GPU memory", (unsigned long long)req->size); + return false; + } + return true; +} + +void +free_memory(Driver *d, Allocation *a) { + if (!a->memory) return; + if (a->block) block_free(a->block, a->offset, a->size); + else vkFreeMemory(d->device, a->memory, NULL); + *a = (Allocation){0}; +} + +void +free_all_memory(Driver *d) { + while (d->memory_blocks) { + MemoryBlock *b = d->memory_blocks; + d->memory_blocks = b->next; + vkFreeMemory(d->device, b->memory, NULL); + free(b->free_ranges); + free(b); + } +} + +bool +create_buffer_with_memory(Driver *d, VkDeviceSize size, VkBufferUsageFlags usage, MemoryUsage mem_usage, VkBuffer *buffer, Allocation *alloc) { + const VkBufferCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = MAX(16u, size), .usage = usage, .sharingMode = VK_SHARING_MODE_EXCLUSIVE}; + VkResult r = vkCreateBuffer(d->device, &info, NULL, buffer); + if (r != VK_SUCCESS) { + set_error(d, "Failed to create a GPU buffer with error: %s", vk_result_string(r)); + return false; + } + VkMemoryRequirements req; + vkGetBufferMemoryRequirements(d->device, *buffer, &req); + if (!allocate_memory(d, &req, mem_usage, alloc)) { + vkDestroyBuffer(d->device, *buffer, NULL); + *buffer = VK_NULL_HANDLE; + return false; + } + r = vkBindBufferMemory(d->device, *buffer, alloc->memory, alloc->offset); + if (r != VK_SUCCESS) { + free_memory(d, alloc); + vkDestroyBuffer(d->device, *buffer, NULL); + *buffer = VK_NULL_HANDLE; + set_error(d, "Failed to bind GPU buffer memory with error: %s", vk_result_string(r)); + return false; + } + return true; +} +// }}} + +// Staging {{{ +#define STAGING_CHUNK_SIZE (4u * 1024u * 1024u) +// The value of used for staging buffers that cannot be shared +#define STAGING_NOT_SHAREABLE ((VkDeviceSize) - 1) + +bool +allocate_staging(Driver *d, VkDeviceSize size, VkDeviceSize alignment, VkBuffer *buffer, VkDeviceSize *offset, void **mapped) { + Context *c = current_context(d); + alignment = MAX(16u, alignment); + // Small uploads are sub-allocated from shared chunks + for (size_t i = 0; i < c->num_staging; i++) { + if (c->staging[i].used == STAGING_NOT_SHAREABLE) continue; + const VkDeviceSize start = (c->staging[i].used + alignment - 1) / alignment * alignment; + if (start + size <= c->staging[i].alloc.size) { + c->staging[i].used = start + size; + *buffer = c->staging[i].buffer; + *offset = start; + *mapped = (uint8_t *)c->staging[i].alloc.mapped + start; + return true; + } + } + if (c->num_staging >= arraysz(c->staging)) { + // Wait for the commands recorded so far to complete, freeing all staging memory + submit_commands(d, NULL, 0, true); + c = current_context(d); + } + // Uploads too large for a chunk get a buffer of their own + const bool shareable = size < STAGING_CHUNK_SIZE; + const size_t idx = c->num_staging; + if (!create_buffer_with_memory( + d, shareable ? STAGING_CHUNK_SIZE : size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, MEMORY_UPLOAD, &c->staging[idx].buffer, &c->staging[idx].alloc)) + return false; + c->num_staging++; + c->staging[idx].used = shareable ? size : STAGING_NOT_SHAREABLE; + *buffer = c->staging[idx].buffer; + *offset = 0; + *mapped = c->staging[idx].alloc.mapped; + return true; +} + +void +release_staging(Driver *d, Context *c) { + for (size_t i = 0; i < c->num_staging; i++) { + vkDestroyBuffer(d->device, c->staging[i].buffer, NULL); + free_memory(d, &c->staging[i].alloc); + } + c->num_staging = 0; +} +// }}} + +// Buffers {{{ +#define AS_BUFFER(handle) ((Buffer *)(uintptr_t)(handle)) + +static VkBufferUsageFlags +buffer_usage(GPUBufferKind kind) { + return kind == GPU_BUFFER_UNIFORM ? VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT : VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; +} + +gpu_buffer_t +buffer_create(void *driver, GPUBufferKind kind) { + Driver *d = driver; + Buffer *b = calloc(1, sizeof(Buffer)); + if (!b) { + set_error(d, "Out of memory"); + return GPU_NULL_HANDLE; + } + b->kind = kind; + b->frequency = GPU_UPDATE_RARELY; + LIST_ADD(d->buffers, b); + return AS_HANDLE(b); +} + +static void +destroy_storage(Driver *d, BufferStorage *s, bool deferred) { + if (!s->buffer) return; + if (deferred && s->last_use_serial > d->completed_serial) defer_destroy_buffer(d, s->buffer, &s->alloc); + else { + vkDestroyBuffer(d->device, s->buffer, NULL); + free_memory(d, &s->alloc); + } + *s = (BufferStorage){0}; +} + +static void +free_buffer(Driver *d, Buffer *b, bool deferred) { + destroy_storage(d, &b->storage, deferred); + for (size_t i = 0; i < b->num_retired; i++) destroy_storage(d, b->retired + i, deferred); + b->num_retired = 0; +} + +void +buffer_destroy(void *driver, gpu_buffer_t buffer) { + Driver *d = driver; + Buffer *b = AS_BUFFER(buffer); + if (!b) return; + for (size_t i = 0; i < arraysz(d->bound_buffers); i++) { + if (d->bound_buffers[i] == b) d->bound_buffers[i] = NULL; + } + free_buffer(d, b, true); + LIST_REMOVE(d->buffers, b); + free(b); +} + +void +destroy_all_buffers(Driver *d) { + while (d->buffers) { + Buffer *b = d->buffers; + free_buffer(d, b, false); + LIST_REMOVE(d->buffers, b); + free(b); + } +} + +// Get storage of the same size as the current storage that no commands use, +// reusing retired storage if possible +static bool +fresh_storage(Driver *d, Buffer *b, VkDeviceSize size, BufferStorage *ans) { + for (size_t i = 0; i < b->num_retired; i++) { + BufferStorage *s = b->retired + i; + if (s->size == size && s->last_use_serial <= d->completed_serial) { + *ans = *s; + b->retired[i] = b->retired[--b->num_retired]; + return true; + } + } + *ans = (BufferStorage){.size = size}; + return create_buffer_with_memory(d, size, buffer_usage(b->kind), MEMORY_UPLOAD, &ans->buffer, &ans->alloc); +} + +static void +retire_storage(Driver *d, Buffer *b, BufferStorage *s) { + if (!s->buffer) return; + if (s->last_use_serial <= d->completed_serial && b->num_retired >= arraysz(b->retired)) { + destroy_storage(d, s, false); + return; + } + if (b->num_retired >= arraysz(b->retired)) { + // Drop the oldest retired storage + destroy_storage(d, b->retired, true); + memmove(b->retired, b->retired + 1, (b->num_retired - 1) * sizeof(BufferStorage)); + b->num_retired--; + } + b->retired[b->num_retired++] = *s; + *s = (BufferStorage){0}; +} + +void +buffer_allocate(void *driver, gpu_buffer_t buffer, size_t size, GPUUpdateFrequency frequency) { + Driver *d = driver; + Buffer *b = AS_BUFFER(buffer); + b->frequency = frequency; + if (b->storage.buffer && b->storage.size == size) return; + // Retired storage of a different size is useless + for (size_t i = 0; i < b->num_retired; i++) destroy_storage(d, b->retired + i, true); + b->num_retired = 0; + destroy_storage(d, &b->storage, true); + if (!size) return; + BufferStorage s; + if (!fresh_storage(d, b, size, &s)) fatal(d, "Failed to allocate a GPU buffer of size %zu with error: %s", size, d->last_error); + b->storage = s; +} + +// Ensure the current storage of the buffer is not used by any commands, +// preserving its contents if preserve is true +static void +make_writable(Driver *d, Buffer *b, bool preserve) { + if (b->storage.last_use_serial <= d->completed_serial) return; + BufferStorage s; + if (!fresh_storage(d, b, b->storage.size, &s)) fatal(d, "Failed to allocate a GPU buffer with error: %s", d->last_error); + // Only the CPU writes to buffers, so the old contents are current + if (preserve) memcpy(s.alloc.mapped, b->storage.alloc.mapped, b->storage.size); + retire_storage(d, b, &b->storage); + b->storage = s; +} + +void * +buffer_map(void *driver, gpu_buffer_t buffer, GPUMapMode mode, size_t offset, size_t size) { + Driver *d = driver; + Buffer *b = AS_BUFFER(buffer); + if (!b->storage.buffer) return NULL; + switch (mode) { + case GPU_MAP_READ: return b->storage.alloc.mapped; + case GPU_MAP_WRITE: make_writable(d, b, true); return b->storage.alloc.mapped; + case GPU_MAP_WRITE_DISCARD_ALL: make_writable(d, b, false); return (uint8_t *)b->storage.alloc.mapped + offset; + case GPU_MAP_WRITE_DISCARD_RANGE: + // The contents outside the range must be preserved + make_writable(d, b, offset > 0 || size < b->storage.size); + return (uint8_t *)b->storage.alloc.mapped + offset; + } + return NULL; +} + +void +buffer_unmap(void *driver UNUSED, gpu_buffer_t buffer UNUSED) { + // Buffers are persistently mapped in coherent memory +} + +void +buffer_copy(void *driver, gpu_buffer_t src, size_t src_offset, gpu_buffer_t dst, size_t dst_offset, size_t size) { + Driver *d = driver; + Buffer *s = AS_BUFFER(src), *t = AS_BUFFER(dst); + if (!size || !s->storage.buffer || !t->storage.buffer) return; + if (src_offset + size > s->storage.size || dst_offset + size > t->storage.size) { + log_error(d, "Out of bounds GPU buffer copy"); + return; + } + // Only the CPU writes to buffers, so copies are done by the CPU too + make_writable(d, t, true); + memmove((uint8_t *)t->storage.alloc.mapped + dst_offset, (uint8_t *)s->storage.alloc.mapped + src_offset, size); +} + +bool +write_buffer(Driver *d, Buffer *b, const void *data, size_t size) { + if (!b->storage.buffer || b->storage.size < size) return false; + make_writable(d, b, size < b->storage.size); + memcpy(b->storage.alloc.mapped, data, size); + return true; +} + +void +bind_uniform_buffer(void *driver, uint32_t slot, gpu_buffer_t buffer) { + Driver *d = driver; + if (slot >= MAX_BINDING_SLOTS) { + log_error(d, "Invalid uniform buffer binding slot: %u", slot); + return; + } + d->bound_buffers[slot] = AS_BUFFER(buffer); + d->cmd_state.descriptors_dirty = true; +} + +void +use_buffer(Driver *d, Buffer *b) { + b->storage.last_use_serial = recording_serial(d); +} +// }}} diff --git a/gpu/vulkan/pipelines.c b/gpu/vulkan/pipelines.c new file mode 100644 index 000000000..87b15cb19 --- /dev/null +++ b/gpu/vulkan/pipelines.c @@ -0,0 +1,533 @@ +/* + * pipelines.c + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// Pipelines and vertex layouts. +// +// A GPU API pipeline corresponds to several Vulkan pipelines, its variants, +// as Vulkan pipelines also depend on the format of the render target, the +// blend mode and the formats of the vertex inputs. Variants are created when +// they are first drawn with. The strides of vertex inputs are dynamic state. + +#include "internal.h" + +#define AS_PIPELINE(handle) ((Pipeline *)(uintptr_t)(handle)) +#define AS_LAYOUT(handle) ((VertexLayout *)(uintptr_t)(handle)) +#define AS_BUFFER(handle) ((Buffer *)(uintptr_t)(handle)) + +// SPIR-V {{{ +// The locations of the inputs of the vertex entry point named entry in the +// SPIR-V module, as a bit mask. Only the inputs actually used by the shader +// need to be supplied by vertex layouts. +static bool +vertex_input_locations(Driver *d, const uint32_t *words, size_t num_words, const char *entry, uint32_t *ans) { + enum { OP_ENTRY_POINT = 15, OP_DECORATE = 71, OP_VARIABLE = 59, DECORATION_LOCATION = 30, STORAGE_INPUT = 1, MODEL_VERTEX = 0 }; + *ans = 0; + if (num_words < 5 || words[0] != 0x07230203) { + set_error(d, "Not a valid SPIR-V module"); + return false; + } + const uint32_t bound = words[3]; + int32_t *locations = malloc(sizeof(int32_t) * bound); + uint8_t *is_input = calloc(bound, 1); + if (!locations || !is_input) { + free(locations); + free(is_input); + set_error(d, "Out of memory"); + return false; + } + for (uint32_t i = 0; i < bound; i++) locations[i] = -1; + const uint32_t *interface = NULL; + size_t num_interface = 0; + for (size_t i = 5; i < num_words;) { + const uint32_t count = words[i] >> 16, opcode = words[i] & 0xffff; + if (!count || i + count > num_words) break; + const uint32_t *op = words + i; + switch (opcode) { + case OP_ENTRY_POINT: + if (count > 3 && op[1] == MODEL_VERTEX) { + const char *name = (const char *)(op + 3); + const size_t max_len = (count - 3) * 4, len = strnlen(name, max_len); + if (len < max_len && strcmp(name, entry) == 0) { + const size_t name_words = len / 4 + 1; + interface = op + 3 + name_words; + num_interface = count - 3 - name_words; + } + } + break; + case OP_DECORATE: + if (count >= 4 && op[2] == DECORATION_LOCATION && op[1] < bound) locations[op[1]] = (int32_t)op[3]; + break; + case OP_VARIABLE: + if (count >= 4 && op[2] < bound && op[3] == STORAGE_INPUT) is_input[op[2]] = 1; + break; + } + i += count; + } + for (size_t i = 0; interface && i < num_interface; i++) { + const uint32_t id = interface[i]; + if (id < bound && is_input[id] && locations[id] >= 0 && locations[id] < MAX_VERTEX_ATTRIBUTES) *ans |= 1u << locations[id]; + } + free(locations); + free(is_input); + if (!interface) { + set_error(d, "The SPIR-V module has no vertex entry point named: %s", entry); + return false; + } + return true; +} +// }}} + +// Pipelines {{{ +static void +free_pipeline(Driver *d, Pipeline *p, bool deferred) { + for (size_t i = 0; i < p->num_variants; i++) { + if (deferred) defer_destroy_pipeline(d, p->variants[i].pipeline); + else vkDestroyPipeline(d->device, p->variants[i].pipeline, NULL); + } + free(p->variants); + if (deferred) defer_destroy_pipeline_objects(d, p->layout, p->set_layout, p->modules, p->num_modules); + else { + if (p->layout) vkDestroyPipelineLayout(d->device, p->layout, NULL); + if (p->set_layout) vkDestroyDescriptorSetLayout(d->device, p->set_layout, NULL); + for (size_t i = 0; i < p->num_modules; i++) vkDestroyShaderModule(d->device, p->modules[i], NULL); + } + for (size_t i = 0; i < p->num_param_blocks; i++) { + if (p->param_blocks[i].buffer) buffer_destroy(d, AS_HANDLE(p->param_blocks[i].buffer)); + } + free(p->param_blocks); + for (size_t i = 0; i < p->num_bindings; i++) free(p->bindings[i].name); + free(p->bindings); + for (size_t i = 0; i < arraysz(p->entry_points); i++) free(p->entry_points[i]); + free(p); +} + +static bool +add_binding(Driver *d, Pipeline *p, GPUBindingKind kind, uint32_t slot, const char *name) { + if (slot >= MAX_BINDING_SLOTS) { + set_error(d, "The binding slot %u of %s is too large", slot, name); + return false; + } + for (size_t i = 0; i < p->num_bindings; i++) { + if (p->bindings[i].slot == slot) { + set_error(d, "The binding slot %u is used by both %s and %s", slot, p->bindings[i].name, name); + return false; + } + } + PipelineBinding *b = p->bindings + p->num_bindings; + if (!(b->name = strdup(name ? name : ""))) { + set_error(d, "Out of memory"); + return false; + } + b->kind = kind; + b->slot = slot; + p->num_bindings++; + return true; +} + +static bool +create_layouts(Driver *d, Pipeline *p) { + VkDescriptorSetLayoutBinding bindings[MAX_BINDING_SLOTS]; + for (size_t i = 0; i < p->num_bindings; i++) { + bindings[i] = (VkDescriptorSetLayoutBinding){ + .binding = p->bindings[i].slot, + .descriptorType = p->bindings[i].kind == GPU_BINDING_TEXTURE ? VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER : VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, + }; + } + const VkDescriptorSetLayoutCreateInfo set_info = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR, + .bindingCount = (uint32_t)p->num_bindings, + .pBindings = bindings, + }; + VkResult r = vkCreateDescriptorSetLayout(d->device, &set_info, NULL, &p->set_layout); + if (r != VK_SUCCESS) { + set_error(d, "Failed to create a descriptor set layout with error: %s", vk_result_string(r)); + return false; + } + // Every pipeline layout has the same push constant range, see draw_quad() + const VkPushConstantRange range = {.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, .size = PUSH_CONSTANTS_SIZE}; + const VkPipelineLayoutCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 1, + .pSetLayouts = &p->set_layout, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &range, + }; + if ((r = vkCreatePipelineLayout(d->device, &info, NULL, &p->layout)) != VK_SUCCESS) { + set_error(d, "Failed to create a pipeline layout with error: %s", vk_result_string(r)); + return false; + } + return true; +} + +static bool +create_modules(Driver *d, Pipeline *p, const GPUShaderSource *sources, size_t num_sources) { + if (num_sources != 2) { + set_error(d, "A pipeline needs a vertex and a fragment shader"); + return false; + } + for (size_t i = 0; i < num_sources; i++) { + const GPUShaderSource *s = sources + i; + if (s->language != GPU_SHADER_LANGUAGE_SPIRV) { + set_error(d, "The Vulkan driver supports only SPIR-V shaders"); + return false; + } + if (!s->size || s->size % 4 || !s->entry_point) { + set_error(d, "Invalid SPIR-V shader"); + return false; + } + // Modules containing the entry points of several stages are passed for each stage + VkShaderModule module = VK_NULL_HANDLE; + if (i > 0 && sources[0].code == s->code && sources[0].size == s->size) module = p->modules[0]; + else { + const VkShaderModuleCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = s->size, .pCode = (const uint32_t *)s->code}; + VkResult r = vkCreateShaderModule(d->device, &info, NULL, &module); + if (r != VK_SUCCESS) { + set_error(d, "Failed to create a shader module with error: %s", vk_result_string(r)); + return false; + } + p->modules[p->num_modules++] = module; + } + if (!(p->entry_points[i] = strdup(s->entry_point))) { + set_error(d, "Out of memory"); + return false; + } + p->stages[i] = (VkPipelineShaderStageCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = s->stage == GPU_SHADER_VERTEX ? VK_SHADER_STAGE_VERTEX_BIT : VK_SHADER_STAGE_FRAGMENT_BIT, + .module = module, + .pName = p->entry_points[i], + }; + if (s->stage == GPU_SHADER_VERTEX && !vertex_input_locations(d, (const uint32_t *)s->code, s->size / 4, s->entry_point, &p->vertex_inputs)) + return false; + } + return true; +} + +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 UNUSED, + size_t num_native_names UNUSED) { + Driver *d = driver; + Pipeline *p = calloc(1, sizeof(Pipeline)); + if (!p) goto oom; + if (!(p->bindings = calloc(MAX(1u, num_bindings + num_param_blocks), sizeof(PipelineBinding)))) goto oom; + if (!(p->param_blocks = calloc(MAX(1u, num_param_blocks), sizeof(ParamBlock)))) goto oom; + if (!create_modules(d, p, sources, num_sources)) goto fail; + for (size_t i = 0; i < num_bindings; i++) { + if (!add_binding(d, p, bindings[i].kind, bindings[i].slot, bindings[i].name)) goto fail; + } + for (size_t i = 0; i < num_param_blocks; i++) { + const GPUParamBlock *src = param_blocks + i; + ParamBlock *pb = p->param_blocks + p->num_param_blocks++; + pb->slot = src->slot; + pb->size = src->size; + if (src->slot < 0) { + if (src->size > PUSH_CONSTANTS_SIZE) { + set_error(d, "The push constant block of size %u is larger than the maximum of %d", src->size, PUSH_CONSTANTS_SIZE); + goto fail; + } + p->push_constants_size = src->size; + } else { + // Uniform blocks whose contents are constant for the lifetime of + // the pipeline are kept in a uniform buffer owned by the pipeline + if (!add_binding(d, p, GPU_BINDING_UNIFORM_BLOCK, (uint32_t)src->slot, src->name)) goto fail; + pb->buffer = AS_BUFFER(buffer_create(d, GPU_BUFFER_UNIFORM)); + if (!pb->buffer) goto fail; + buffer_allocate(d, AS_HANDLE(pb->buffer), src->size, GPU_UPDATE_RARELY); + memset(pb->buffer->storage.alloc.mapped, 0, src->size); + } + } + if (!create_layouts(d, p)) goto fail; + LIST_ADD(d->pipelines, p); + return AS_HANDLE(p); +oom: + set_error(d, "Out of memory"); +fail: + if (p) free_pipeline(d, p, false); + return GPU_NULL_HANDLE; +} + +void +pipeline_destroy(void *driver, gpu_pipeline_t pipeline) { + Driver *d = driver; + Pipeline *p = AS_PIPELINE(pipeline); + if (!p) return; + if (d->pipeline == p) d->pipeline = NULL; + if (d->cmd_state.push_constants_of == p) d->cmd_state.push_constants_of = NULL; + LIST_REMOVE(d->pipelines, p); + free_pipeline(d, p, true); +} + +void +destroy_all_pipelines(Driver *d) { + while (d->pipelines) { + Pipeline *p = d->pipelines; + LIST_REMOVE(d->pipelines, p); + free_pipeline(d, p, false); + } +} + +void +use_pipeline(void *driver, gpu_pipeline_t pipeline) { + ((Driver *)driver)->pipeline = AS_PIPELINE(pipeline); +} + +bool +pipeline_uniform_block(void *driver, gpu_pipeline_t pipeline, const char *name, GPUUniformBlockInfo *ans) { + Driver *d = driver; + const Pipeline *p = AS_PIPELINE(pipeline); + for (size_t i = 0; i < p->num_bindings; i++) { + if (p->bindings[i].kind == GPU_BINDING_UNIFORM_BLOCK && strcmp(p->bindings[i].name, name) == 0) { + ans->index = p->bindings[i].slot; + // The size is not known without reflecting the SPIR-V + ans->size = 0; + return true; + } + } + set_error(d, "The pipeline has no uniform block named: %s", name); + return false; +} + +int32_t +pipeline_attribute_location(void *driver UNUSED, gpu_pipeline_t pipeline UNUSED, const char *name UNUSED) { + // SPIR-V does not reliably preserve the names of inputs, the locations + // come from the reflection data in the program metadata + return -1; +} + +void +set_params(void *driver, gpu_pipeline_t pipeline, uint32_t block, const void *data, size_t size) { + Driver *d = driver; + Pipeline *p = AS_PIPELINE(pipeline); + if (block >= p->num_param_blocks) { + log_error(d, "Invalid parameter block index: %u", block); + return; + } + ParamBlock *pb = p->param_blocks + block; + if (size != pb->size) { + log_error(d, "The data for parameter block %u is %zu bytes instead of %u", block, size, pb->size); + return; + } + if (pb->slot < 0) { + memcpy(p->push_constants, data, size); + if (d->cmd_state.push_constants_of == p) d->cmd_state.push_constants_dirty = true; + } else if (!write_buffer(d, pb->buffer, data, size)) log_error(d, "Failed to write the parameter block %u", block); +} + +VkFormat +vertex_attribute_format(uint32_t components, GPUAttribType type) { + static const VkFormat formats[4][4] = { + {VK_FORMAT_R8_UINT, VK_FORMAT_R8G8_UINT, VK_FORMAT_R8G8B8_UINT, VK_FORMAT_R8G8B8A8_UINT}, + {VK_FORMAT_R16_UINT, VK_FORMAT_R16G16_UINT, VK_FORMAT_R16G16B16_UINT, VK_FORMAT_R16G16B16A16_UINT}, + {VK_FORMAT_R32_UINT, VK_FORMAT_R32G32_UINT, VK_FORMAT_R32G32B32_UINT, VK_FORMAT_R32G32B32A32_UINT}, + {VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT}, + }; + if (components < 1 || components > 4) return VK_FORMAT_UNDEFINED; + switch (type) { + case GPU_ATTRIB_U8: return formats[0][components - 1]; + case GPU_ATTRIB_U16: return formats[1][components - 1]; + case GPU_ATTRIB_U32: return formats[2][components - 1]; + case GPU_ATTRIB_F32: return formats[3][components - 1]; + } + return VK_FORMAT_UNDEFINED; +} + +static VkPipeline +create_variant(Driver *d, Pipeline *p, const PipelineVariantKey *key) { + VkVertexInputBindingDescription vbindings[MAX_VERTEX_ATTRIBUTES]; + VkVertexInputAttributeDescription vattrs[MAX_VERTEX_ATTRIBUTES]; + uint32_t n = 0; + for (uint32_t loc = 0; loc < MAX_VERTEX_ATTRIBUTES; loc++) { + if (!key->attribute_formats[loc]) continue; + // Each input has a binding of its own, its offset is applied when binding the buffer + vbindings[n] = (VkVertexInputBindingDescription){ + .binding = loc, .inputRate = key->attribute_per_instance[loc] ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX}; + vattrs[n] = (VkVertexInputAttributeDescription){.location = loc, .binding = loc, .format = key->attribute_formats[loc]}; + n++; + } + const VkPipelineVertexInputStateCreateInfo vertex_input = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, + .vertexBindingDescriptionCount = n, + .pVertexBindingDescriptions = vbindings, + .vertexAttributeDescriptionCount = n, + .pVertexAttributeDescriptions = vattrs, + }; + const VkPipelineInputAssemblyStateCreateInfo input_assembly = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST}; + const VkPipelineViewportStateCreateInfo viewport = {.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, .viewportCount = 1, .scissorCount = 1}; + const VkPipelineRasterizationStateCreateInfo raster = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, + .polygonMode = VK_POLYGON_MODE_FILL, + .cullMode = VK_CULL_MODE_NONE, + .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE, + .lineWidth = 1.f, + }; + const VkPipelineMultisampleStateCreateInfo multisample = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT}; + const VkPipelineColorBlendAttachmentState blend = { + .blendEnable = key->blend, + // Pre-multiplied alpha, see GPU_BLEND_PREMULTIPLIED_ALPHA + .srcColorBlendFactor = VK_BLEND_FACTOR_ONE, + .dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, + .colorBlendOp = VK_BLEND_OP_ADD, + .srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE, + .dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, + .alphaBlendOp = VK_BLEND_OP_ADD, + .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT, + }; + const VkPipelineColorBlendStateCreateInfo color_blend = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, .attachmentCount = 1, .pAttachments = &blend}; + const VkDynamicState dynamic_states[] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE}; + const VkPipelineDynamicStateCreateInfo dynamic = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, + .dynamicStateCount = n ? 3 : 2, + .pDynamicStates = dynamic_states, + }; + const VkPipelineRenderingCreateInfo rendering = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, .colorAttachmentCount = 1, .pColorAttachmentFormats = &key->color_format}; + const VkGraphicsPipelineCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, + .pNext = &rendering, + .stageCount = 2, + .pStages = p->stages, + .pVertexInputState = &vertex_input, + .pInputAssemblyState = &input_assembly, + .pViewportState = &viewport, + .pRasterizationState = &raster, + .pMultisampleState = &multisample, + .pColorBlendState = &color_blend, + .pDynamicState = &dynamic, + .layout = p->layout, + }; + VkPipeline ans = VK_NULL_HANDLE; + VkResult r = vkCreateGraphicsPipelines(d->device, VK_NULL_HANDLE, 1, &info, NULL, &ans); + if (r != VK_SUCCESS) { + log_error(d, "Failed to create a Vulkan pipeline with error: %s", vk_result_string(r)); + return VK_NULL_HANDLE; + } + return ans; +} + +VkPipeline +pipeline_for_current_state(Driver *d, bool blend) { + Pipeline *p = d->pipeline; + PipelineVariantKey key; + memset(&key, 0, sizeof(key)); // the key is compared with memcmp() + key.color_format = d->rendering.format; + key.blend = blend; + for (uint32_t loc = 0; loc < MAX_VERTEX_ATTRIBUTES; loc++) { + if (!(p->vertex_inputs & (1u << loc))) continue; + const VertexAttribute *a = d->vertex_layout ? d->vertex_layout->attributes + loc : NULL; + if (!a || !a->enabled) { + log_error(d, "The vertex layout has no input at location: %u used by the shader", loc); + return VK_NULL_HANDLE; + } + if (a->divisor > 1) { + log_error(d, "Instance divisors greater than one are not supported"); + return VK_NULL_HANDLE; + } + key.attribute_formats[loc] = vertex_attribute_format(a->components, a->type); + key.attribute_per_instance[loc] = a->divisor > 0; + } + for (size_t i = 0; i < p->num_variants; i++) { + if (memcmp(&p->variants[i].key, &key, sizeof(key)) == 0) return p->variants[i].pipeline; + } + for (uint32_t loc = 0; loc < MAX_VERTEX_ATTRIBUTES; loc++) { + if (!key.attribute_formats[loc]) continue; + VkFormatProperties fp; + vkGetPhysicalDeviceFormatProperties(d->physical_device, key.attribute_formats[loc], &fp); + if (!(fp.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT)) { + log_error(d, "The GPU does not support the vertex input format: %d", (int)key.attribute_formats[loc]); + return VK_NULL_HANDLE; + } + } + if (p->num_variants >= p->variants_capacity) { + const size_t cap = MAX(4u, p->variants_capacity * 2); + PipelineVariant *v = realloc(p->variants, cap * sizeof(PipelineVariant)); + if (!v) fatal(d, "Out of memory"); + p->variants = v; + p->variants_capacity = cap; + } + const VkPipeline vp = create_variant(d, p, &key); + if (!vp) return VK_NULL_HANDLE; + p->variants[p->num_variants++] = (PipelineVariant){.key = key, .pipeline = vp}; + return vp; +} +// }}} + +// Vertex layouts {{{ +gpu_vertex_layout_t +vertex_layout_create(void *driver) { + Driver *d = driver; + VertexLayout *l = calloc(1, sizeof(VertexLayout)); + if (!l) { + set_error(d, "Out of memory"); + return GPU_NULL_HANDLE; + } + LIST_ADD(d->vertex_layouts, l); + return AS_HANDLE(l); +} + +void +vertex_layout_destroy(void *driver, gpu_vertex_layout_t layout) { + Driver *d = driver; + VertexLayout *l = AS_LAYOUT(layout); + if (!l) return; + if (d->vertex_layout == l) d->vertex_layout = NULL; + LIST_REMOVE(d->vertex_layouts, l); + free(l); +} + +void +bind_vertex_layout(void *driver, gpu_vertex_layout_t layout) { + ((Driver *)driver)->vertex_layout = AS_LAYOUT(layout); +} + +void +vertex_layout_set_attribute( + void *driver, + gpu_vertex_layout_t layout, + uint32_t location, + gpu_buffer_t buffer, + uint32_t components, + GPUAttribType type, + uint32_t stride, + size_t offset, + uint32_t instance_divisor) { + Driver *d = driver; + VertexLayout *l = AS_LAYOUT(layout); + if (location >= MAX_VERTEX_ATTRIBUTES) { + log_error(d, "Invalid vertex input location: %u", location); + return; + } + l->attributes[location] = (VertexAttribute){ + .enabled = true, + .buffer = AS_BUFFER(buffer), + .components = components, + .type = type, + // A stride of zero means tightly packed, as in OpenGL + .stride = stride ? stride + : components * (type == GPU_ATTRIB_U8 ? 1 + : type == GPU_ATTRIB_U16 ? 2 + : 4), + .offset = offset, + .divisor = instance_divisor, + }; + d->vertex_layout = l; +} +// }}} diff --git a/gpu/vulkan/surfaces.c b/gpu/vulkan/surfaces.c new file mode 100644 index 000000000..25d74e44f --- /dev/null +++ b/gpu/vulkan/surfaces.c @@ -0,0 +1,464 @@ +/* + * surfaces.c + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// Surfaces and swapchains for X11 and Wayland windows + +#include "internal.h" +#include +#include + +#define AS_SURFACE(handle) ((Surface *)(uintptr_t)(handle)) + +// libX11 {{{ +// libX11 is already loaded by the X11 backend of glfw, it is used here only +// to find visuals. +static struct { + bool loaded; + int (*default_screen)(Display *); + Visual *(*default_visual)(Display *, int); + VisualID (*visual_id_from_visual)(Visual *); + Status (*match_visual_info)(Display *, int, int, int, XVisualInfo *); +} x11; + +static bool +load_x11(Driver *d) { + if (x11.loaded) return true; + void *lib = dlopen("libX11.so.6", RTLD_LAZY | RTLD_LOCAL); + if (!lib) lib = dlopen("libX11.so", RTLD_LAZY | RTLD_LOCAL); + if (!lib) { + set_error(d, "Could not load libX11: %s", dlerror()); + return false; + } + *(void **)&x11.default_screen = dlsym(lib, "XDefaultScreen"); + *(void **)&x11.default_visual = dlsym(lib, "XDefaultVisual"); + *(void **)&x11.visual_id_from_visual = dlsym(lib, "XVisualIDFromVisual"); + *(void **)&x11.match_visual_info = dlsym(lib, "XMatchVisualInfo"); + if (!x11.default_screen || !x11.default_visual || !x11.visual_id_from_visual || !x11.match_visual_info) { + set_error(d, "libX11 is missing required functions"); + return false; + } + x11.loaded = true; + return true; +} +// }}} + +bool +presentation_supported(Driver *d, VkPhysicalDevice device, uint32_t queue_family) { + if (d->platform == GPU_PLATFORM_WAYLAND) return vkGetPhysicalDeviceWaylandPresentationSupportKHR(device, queue_family, d->native_display); + if (!load_x11(d)) return false; + Display *dpy = d->native_display; + const VisualID visual = x11.visual_id_from_visual(x11.default_visual(dpy, x11.default_screen(dpy))); + return vkGetPhysicalDeviceXlibPresentationSupportKHR(device, queue_family, dpy, visual); +} + +bool +choose_native_visual(void *driver, const GPUNativeWindow *native, const GPUSurfaceConfig *config, uint64_t *visual_id) { + Driver *d = driver; + *visual_id = 0; + if (native->platform != GPU_PLATFORM_X11 || !config->transparent) return true; + // Semi-transparent windows need a visual with an alpha channel + if (!load_x11(d)) return false; + Display *dpy = native->display; + const int screen = native->screen < 0 ? x11.default_screen(dpy) : native->screen; + XVisualInfo vi; + if (x11.match_visual_info(dpy, screen, 32, TrueColor, &vi) && + vkGetPhysicalDeviceXlibPresentationSupportKHR(d->physical_device, d->queue_family, dpy, vi.visualid)) { + *visual_id = vi.visualid; + } + return true; +} + +// Swapchains {{{ +static void +destroy_swapchain_objects(Driver *d, Swapchain *sc) { + for (uint32_t i = 0; i < sc->num_images; i++) { + destroy_image_views(d, sc->images + i); + if (sc->present_semaphores[i]) vkDestroySemaphore(d->device, sc->present_semaphores[i], NULL); + } + free(sc->images); + free(sc->present_semaphores); + sc->images = NULL; + sc->present_semaphores = NULL; + sc->num_images = 0; +} + +// Wait until the GPU no longer uses the swapchain of the surface +static void +finish_using_swapchain(Driver *d, Surface *s) { + if (d->rendering.active && d->current_surface == s && !d->render_target) end_rendering(d); + // Commands recorded for an acquired image must be submitted, as they wait on the acquire semaphore + if (s->image_acquired) submit_commands(d, NULL, 0, false); + s->image_acquired = false; + vkQueueWaitIdle(d->queue); + wait_for_serial(d, d->next_serial - 1); +} + +static VkPresentModeKHR +choose_present_mode(Driver *d, Surface *s) { + if (s->swap_interval > 0) return VK_PRESENT_MODE_FIFO_KHR; // always supported + uint32_t n = 0; + vkGetPhysicalDeviceSurfacePresentModesKHR(d->physical_device, s->surface, &n, NULL); + VkPresentModeKHR modes[16]; + n = MIN(n, (uint32_t)arraysz(modes)); + vkGetPhysicalDeviceSurfacePresentModesKHR(d->physical_device, s->surface, &n, modes); + // Mailbox never tears and, unlike FIFO, does not block when the window is hidden + static const VkPresentModeKHR preferred[] = {VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR}; + for (size_t p = 0; p < arraysz(preferred); p++) { + for (uint32_t i = 0; i < n; i++) { + if (modes[i] == preferred[p]) return modes[i]; + } + } + return VK_PRESENT_MODE_FIFO_KHR; +} + +static bool +choose_surface_format(Driver *d, Surface *s, VkFormat *format, VkFormat *srgb_format, VkColorSpaceKHR *color_space) { + uint32_t n = 0; + vkGetPhysicalDeviceSurfaceFormatsKHR(d->physical_device, s->surface, &n, NULL); + VkSurfaceFormatKHR *formats = calloc(MAX(1u, n), sizeof(VkSurfaceFormatKHR)); + if (!formats) { + set_error(d, "Out of memory"); + return false; + } + vkGetPhysicalDeviceSurfaceFormatsKHR(d->physical_device, s->surface, &n, formats); + static const VkFormat preferred[][2] = { + {VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_SRGB}, + {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB}, + }; + bool found = false; + for (size_t p = 0; p < arraysz(preferred) && !found; p++) { + for (uint32_t i = 0; i < n && !found; i++) { + if (formats[i].format == preferred[p][0] && formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { + *format = preferred[p][0]; + *srgb_format = preferred[p][1]; + *color_space = formats[i].colorSpace; + found = true; + } + } + } + free(formats); + if (!found) set_error(d, "The window surface supports no suitable format"); + return found; +} + +static VkCompositeAlphaFlagBitsKHR +choose_composite_alpha(const Surface *s, VkCompositeAlphaFlagsKHR supported) { + static const VkCompositeAlphaFlagBitsKHR transparent[] = {VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR, VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR}; + static const VkCompositeAlphaFlagBitsKHR opaque[] = {VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR}; + const VkCompositeAlphaFlagBitsKHR *order = s->config.transparent ? transparent : opaque; + for (size_t i = 0; i < 2; i++) { + if (supported & order[i]) return order[i]; + } + for (uint32_t bit = 1; bit; bit <<= 1) { + if (supported & bit) return (VkCompositeAlphaFlagBitsKHR)bit; + } + return VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; +} + +typedef enum { + SWAPCHAIN_CREATED, + SWAPCHAIN_EMPTY, // there is nothing to render to, such as when the window is minimized + SWAPCHAIN_FAILED, +} SwapchainResult; + +// (Re-)create the swapchain of the surface +static SwapchainResult +create_swapchain(Driver *d, Surface *s) { + VkSurfaceCapabilitiesKHR caps; + VkResult r = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(d->physical_device, s->surface, &caps); + if (r != VK_SUCCESS) { + set_error(d, "Failed to query the window surface with error: %s", vk_result_string(r)); + return SWAPCHAIN_FAILED; + } + VkExtent2D extent = caps.currentExtent; + if (extent.width == UINT32_MAX) { + // The size is decided by the swapchain, as on Wayland + extent.width = MAX(caps.minImageExtent.width, MIN(caps.maxImageExtent.width, s->width)); + extent.height = MAX(caps.minImageExtent.height, MIN(caps.maxImageExtent.height, s->height)); + } + if (!extent.width || !extent.height) return SWAPCHAIN_EMPTY; + VkFormat format, srgb_format; + VkColorSpaceKHR color_space; + if (!choose_surface_format(d, s, &format, &srgb_format, &color_space)) return SWAPCHAIN_FAILED; + uint32_t num_images = MAX(caps.minImageCount + 1, 3u); + if (caps.maxImageCount) num_images = MIN(num_images, caps.maxImageCount); + const VkPresentModeKHR present_mode = choose_present_mode(d, s); + const bool mutable_format = d->has_swapchain_mutable_format; + const VkFormat view_formats[2] = {format, srgb_format}; + const VkImageFormatListCreateInfo format_list = { + .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO, .viewFormatCount = 2, .pViewFormats = view_formats}; + Swapchain old = s->swapchain; + const VkSwapchainCreateInfoKHR info = { + .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, + .pNext = mutable_format ? &format_list : NULL, + .flags = mutable_format ? VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR : 0, + .surface = s->surface, + .minImageCount = num_images, + .imageFormat = format, + .imageColorSpace = color_space, + .imageExtent = extent, + .imageArrayLayers = 1, + // Transfers are used to read back the contents of the window + .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | (caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT), + .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE, + .preTransform = (caps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) ? VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR : caps.currentTransform, + .compositeAlpha = choose_composite_alpha(s, caps.supportedCompositeAlpha), + .presentMode = present_mode, + .clipped = VK_TRUE, + .oldSwapchain = old.swapchain, + }; + VkSwapchainKHR swapchain; + r = vkCreateSwapchainKHR(d->device, &info, NULL, &swapchain); + if (old.swapchain) { + destroy_swapchain_objects(d, &old); + vkDestroySwapchainKHR(d->device, old.swapchain, NULL); + } + s->swapchain = (Swapchain){0}; + if (r != VK_SUCCESS) { + set_error(d, "Failed to create the swapchain for the window with error: %s", vk_result_string(r)); + return SWAPCHAIN_FAILED; + } + Swapchain *sc = &s->swapchain; + sc->swapchain = swapchain; + sc->extent = extent; + sc->format = format; + sc->present_mode = present_mode; + uint32_t n = 0; + vkGetSwapchainImagesKHR(d->device, swapchain, &n, NULL); + VkImage *images = calloc(MAX(1u, n), sizeof(VkImage)); + sc->images = calloc(MAX(1u, n), sizeof(Image)); + sc->present_semaphores = calloc(MAX(1u, n), sizeof(VkSemaphore)); + if (!images || !sc->images || !sc->present_semaphores) { + free(images); + set_error(d, "Out of memory"); + return SWAPCHAIN_FAILED; + } + vkGetSwapchainImagesKHR(d->device, swapchain, &n, images); + sc->num_images = n; + for (uint32_t i = 0; i < n; i++) { + Image *img = sc->images + i; + img->image = images[i]; + img->format = format; + img->unorm_format = format; + img->srgb_format = mutable_format ? srgb_format : VK_FORMAT_UNDEFINED; + img->width = extent.width; + img->height = extent.height; + img->layers = 1; + const VkSemaphoreCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; + if (!create_image_views(d, img, VK_IMAGE_VIEW_TYPE_MAX_ENUM) || vkCreateSemaphore(d->device, &si, NULL, sc->present_semaphores + i) != VK_SUCCESS) { + free(images); + set_error(d, "Failed to create the views of swapchain images: %s", d->last_error); + return SWAPCHAIN_FAILED; + } + } + free(images); + s->needs_recreate = false; + debug_print(d, "Created a Vulkan swapchain of %u images of size %ux%u with present mode: %d", n, extent.width, extent.height, (int)present_mode); + return SWAPCHAIN_CREATED; +} + +static void +destroy_swapchain(Driver *d, Surface *s) { + if (!s->swapchain.swapchain) return; + finish_using_swapchain(d, s); + destroy_swapchain_objects(d, &s->swapchain); + vkDestroySwapchainKHR(d->device, s->swapchain.swapchain, NULL); + s->swapchain = (Swapchain){0}; +} + +static bool +recreate_swapchain(Driver *d, Surface *s) { + if (s->swapchain.swapchain) finish_using_swapchain(d, s); + switch (create_swapchain(d, s)) { + case SWAPCHAIN_CREATED: return true; + case SWAPCHAIN_EMPTY: return false; + case SWAPCHAIN_FAILED: log_error(d, "Vulkan: %s", d->last_error); return false; + } + return false; +} + +Image * +current_surface_image(Driver *d) { + Surface *s = d->current_surface; + if (!s) return NULL; + if (s->image_acquired) return s->swapchain.images + s->image_index; + for (int attempt = 0; attempt < 2; attempt++) { + if ((s->needs_recreate || !s->swapchain.swapchain) && !recreate_swapchain(d, s)) return NULL; + VkSemaphore sem = get_semaphore(d); + uint32_t idx; + // Do not block forever if the compositor never releases an image + VkResult r = vkAcquireNextImageKHR(d->device, s->swapchain.swapchain, 1000000000ull, sem, VK_NULL_HANDLE, &idx); + switch (r) { + case VK_SUCCESS: + case VK_SUBOPTIMAL_KHR: { + if (r == VK_SUBOPTIMAL_KHR) s->needs_recreate = true; // at the next frame + add_wait_semaphore(d, sem); + s->image_acquired = true; + s->image_index = idx; + Image *img = s->swapchain.images + idx; + // The previous contents are not needed, and the first access must + // wait on the acquire semaphore, which submissions wait on at all stages + img->state = (ImageState){.layout = VK_IMAGE_LAYOUT_UNDEFINED, .stage = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT}; + return img; + } + case VK_ERROR_OUT_OF_DATE_KHR: s->needs_recreate = true; break; + case VK_TIMEOUT: + case VK_NOT_READY: debug_print(d, "Vulkan: Timed out acquiring a swapchain image"); return NULL; + default: log_error(d, "Vulkan: Failed to acquire a swapchain image with error: %s", vk_result_string(r)); return NULL; + } + } + return NULL; +} +// }}} + +// Surfaces {{{ +gpu_surface_t +surface_create(void *driver, const GPUNativeWindow *native, const GPUSurfaceConfig *config, uint32_t width, uint32_t height) { + Driver *d = driver; + Surface *s = calloc(1, sizeof(Surface)); + if (!s) { + set_error(d, "Out of memory"); + return GPU_NULL_HANDLE; + } + s->platform = native->platform; + s->config = *config; + s->width = width; + s->height = height; + s->swap_interval = 1; + VkResult r; + if (native->platform == GPU_PLATFORM_WAYLAND) { + const VkWaylandSurfaceCreateInfoKHR info = { + .sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR, .display = native->display, .surface = native->handle}; + r = vkCreateWaylandSurfaceKHR(d->instance, &info, NULL, &s->surface); + } else if (native->platform == GPU_PLATFORM_X11) { + const VkXlibSurfaceCreateInfoKHR info = { + .sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR, .dpy = native->display, .window = (Window)native->x11_window}; + r = vkCreateXlibSurfaceKHR(d->instance, &info, NULL, &s->surface); + } else { + set_error(d, "The Vulkan driver supports only X11 and Wayland windows"); + free(s); + return GPU_NULL_HANDLE; + } + if (r != VK_SUCCESS) { + set_error(d, "Failed to create a Vulkan surface for the window with error: %s", vk_result_string(r)); + free(s); + return GPU_NULL_HANDLE; + } + VkBool32 supported = VK_FALSE; + vkGetPhysicalDeviceSurfaceSupportKHR(d->physical_device, d->queue_family, s->surface, &supported); + if (!supported) { + set_error(d, "The GPU cannot present to the window"); + vkDestroySurfaceKHR(d->instance, s->surface, NULL); + free(s); + return GPU_NULL_HANDLE; + } + // Create the swapchain now, so that failures happen while kitty can still + // fall back to another GPU driver. Windows of size zero get a swapchain later. + if (create_swapchain(d, s) == SWAPCHAIN_FAILED) { + vkDestroySurfaceKHR(d->instance, s->surface, NULL); + free(s); + return GPU_NULL_HANDLE; + } + LIST_ADD(d->surfaces, s); + debug_print(d, "Created Vulkan surface for window of size: %ux%u", width, height); + return AS_HANDLE(s); +} + +void +surface_destroy(void *driver, gpu_surface_t surface) { + Driver *d = driver; + Surface *s = AS_SURFACE(surface); + if (!s) return; + destroy_swapchain(d, s); + if (d->current_surface == s) d->current_surface = NULL; + vkDestroySurfaceKHR(d->instance, s->surface, NULL); + LIST_REMOVE(d->surfaces, s); + free(s); +} + +bool +make_surface_current(void *driver, gpu_surface_t surface) { + Driver *d = driver; + Surface *s = AS_SURFACE(surface); + if (s != d->current_surface && !d->render_target) end_rendering(d); + d->current_surface = s; + return true; +} + +gpu_surface_t +get_current_surface(void *driver) { + return AS_HANDLE(((Driver *)driver)->current_surface); +} + +void +surface_resize(void *driver UNUSED, gpu_surface_t surface, uint32_t width, uint32_t height) { + Surface *s = AS_SURFACE(surface); + if (!s) return; + s->width = width; + s->height = height; + if (width != s->swapchain.extent.width || height != s->swapchain.extent.height) s->needs_recreate = true; +} + +bool +surface_present(void *driver, gpu_surface_t surface) { + Driver *d = driver; + Surface *s = AS_SURFACE(surface); + if (!s) return false; + if (!s->image_acquired) return true; // nothing was rendered + if (d->rendering.active && d->rendering.image == s->swapchain.images + s->image_index) end_rendering(d); + Image *img = s->swapchain.images + s->image_index; + transition_image(d, img, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, 0); + VkSemaphore done = s->swapchain.present_semaphores[s->image_index]; + submit_commands(d, &done, 1, false); + s->image_acquired = false; + const VkPresentInfoKHR info = { + .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, + .waitSemaphoreCount = 1, + .pWaitSemaphores = &done, + .swapchainCount = 1, + .pSwapchains = &s->swapchain.swapchain, + .pImageIndices = &s->image_index, + }; + VkResult r = vkQueuePresentKHR(d->queue, &info); + switch (r) { + case VK_SUCCESS: return true; + case VK_SUBOPTIMAL_KHR: + case VK_ERROR_OUT_OF_DATE_KHR: s->needs_recreate = true; return true; + case VK_ERROR_DEVICE_LOST: fatal(d, "Vulkan: The GPU device was lost"); + default: set_error(d, "Failed to present the window with error: %s", vk_result_string(r)); return false; + } +} + +void +surface_set_swap_interval(void *driver, gpu_surface_t surface, int interval) { + Driver *d = driver; + Surface *s = AS_SURFACE(surface); + if (!s || s->swap_interval == interval) return; + s->swap_interval = interval; + if (s->swapchain.swapchain && choose_present_mode(d, s) != s->swapchain.present_mode) s->needs_recreate = true; +} + +void +surface_native_changed(void *driver UNUSED, gpu_surface_t surface UNUSED) { + // Only needed on macOS +} + +bool +surface_recreate(void *driver, gpu_surface_t surface) { + Driver *d = driver; + Surface *s = AS_SURFACE(surface); + if (!s) return false; + s->needs_recreate = true; + return make_surface_current(d, surface); +} + +void +release_native_resources(void *driver) { + Driver *d = driver; + while (d->surfaces) surface_destroy(d, AS_HANDLE(d->surfaces)); +} +// }}} diff --git a/gpu/vulkan/textures.c b/gpu/vulkan/textures.c new file mode 100644 index 000000000..048243aa3 --- /dev/null +++ b/gpu/vulkan/textures.c @@ -0,0 +1,507 @@ +/* + * textures.c + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// Textures, images and samplers + +#include "internal.h" + +#define AS_TEXTURE(handle) ((Texture *)(uintptr_t)(handle)) + +// Formats {{{ +VkFormat +vk_format_for(GPUTextureFormat f) { + switch (f) { + case GPU_FORMAT_R8: return VK_FORMAT_R8_UNORM; + case GPU_FORMAT_R8UI: return VK_FORMAT_R8_UINT; + case GPU_FORMAT_RG8UI: return VK_FORMAT_R8G8_UINT; + case GPU_FORMAT_R32UI: return VK_FORMAT_R32_UINT; + case GPU_FORMAT_RGBA8: return VK_FORMAT_R8G8B8A8_UNORM; + case GPU_FORMAT_SRGB8_ALPHA8: return VK_FORMAT_R8G8B8A8_SRGB; + case GPU_FORMAT_RGBA16: return VK_FORMAT_R16G16B16A16_UNORM; + } + return VK_FORMAT_UNDEFINED; +} + +static bool +is_integer_format(VkFormat f) { + return f == VK_FORMAT_R8_UINT || f == VK_FORMAT_R8G8_UINT || f == VK_FORMAT_R32_UINT; +} + +static size_t +texel_size(VkFormat f) { + switch (f) { + case VK_FORMAT_R8_UNORM: + case VK_FORMAT_R8_UINT: return 1; + case VK_FORMAT_R8G8_UINT: return 2; + case VK_FORMAT_R16G16B16A16_UNORM: return 8; + default: return 4; + } +} + +// The number of 8 bit channels in a texel, zero for formats that are not made of 8 bit channels +static unsigned +channels_8bit(VkFormat f) { + switch (f) { + case VK_FORMAT_R8_UNORM: + case VK_FORMAT_R8_UINT: return 1; + case VK_FORMAT_R8G8_UINT: return 2; + case VK_FORMAT_R8G8B8A8_UNORM: + case VK_FORMAT_R8G8B8A8_SRGB: + case VK_FORMAT_B8G8R8A8_UNORM: + case VK_FORMAT_B8G8R8A8_SRGB: return 4; + default: return 0; + } +} + +static size_t +pixel_layout_size(GPUPixelLayout l) { + switch (l) { + case GPU_PIXELS_R8: return 1; + case GPU_PIXELS_RG8: return 2; + case GPU_PIXELS_RGB8: return 3; + case GPU_PIXELS_RGBA8: + case GPU_PIXELS_RGBA8_PACKED: + case GPU_PIXELS_R32UI: return 4; + } + return 4; +} + +static size_t +row_stride(size_t row_bytes, uint32_t alignment) { + if (alignment < 1) alignment = 1; + return (row_bytes + alignment - 1) / alignment * alignment; +} + +// Convert pixels from CPU memory into tightly packed texels of the format +static bool +convert_pixels(const GPUPixelData *src, uint32_t width, uint32_t height, VkFormat format, uint8_t *dst) { + const size_t sbpp = pixel_layout_size(src->layout), dbpp = texel_size(format); + const size_t sstride = row_stride(sbpp * width, src->row_alignment), dstride = dbpp * width; + const uint8_t *s = src->data; + if (src->layout == GPU_PIXELS_R32UI || format == VK_FORMAT_R32_UINT) { + if (src->layout != GPU_PIXELS_R32UI || format != VK_FORMAT_R32_UINT) return false; + for (uint32_t y = 0; y < height; y++) memcpy(dst + y * dstride, s + y * sstride, dstride); + return true; + } + const unsigned dch = channels_8bit(format); + // Fast path, the layout of the pixels is the same as that of the texels + if (dch && src->layout != GPU_PIXELS_RGBA8_PACKED && dch == sbpp) { + for (uint32_t y = 0; y < height; y++) memcpy(dst + y * dstride, s + y * sstride, dstride); + return true; + } + for (uint32_t y = 0; y < height; y++) { + const uint8_t *row = s + y * sstride; + uint8_t *out = dst + y * dstride; + for (uint32_t x = 0; x < width; x++) { + uint8_t c[4] = {0, 0, 0, 255}; + const uint8_t *p = row + x * sbpp; + switch (src->layout) { + case GPU_PIXELS_R8: c[0] = p[0]; break; + case GPU_PIXELS_RG8: + c[0] = p[0]; + c[1] = p[1]; + break; + case GPU_PIXELS_RGB8: memcpy(c, p, 3); break; + case GPU_PIXELS_RGBA8: memcpy(c, p, 4); break; + case GPU_PIXELS_RGBA8_PACKED: { + uint32_t v; + memcpy(&v, p, 4); + c[0] = (uint8_t)(v >> 24); + c[1] = (uint8_t)(v >> 16); + c[2] = (uint8_t)(v >> 8); + c[3] = (uint8_t)v; + } break; + case GPU_PIXELS_R32UI: break; + } + if (dch) memcpy(out + x * dch, c, dch); + else if (format == VK_FORMAT_R16G16B16A16_UNORM) { + uint16_t w[4]; + for (int i = 0; i < 4; i++) w[i] = (uint16_t)(c[i] * 257u); + memcpy(out + x * 8, w, 8); + } else return false; + } + } + return true; +} + +void +convert_to_rgba8(VkFormat format, const uint8_t *src, uint8_t *dst, size_t num_pixels) { + switch (format) { + case VK_FORMAT_B8G8R8A8_UNORM: + case VK_FORMAT_B8G8R8A8_SRGB: + for (size_t i = 0; i < num_pixels; i++, src += 4, dst += 4) { + dst[0] = src[2]; + dst[1] = src[1]; + dst[2] = src[0]; + dst[3] = src[3]; + } + break; + case VK_FORMAT_R16G16B16A16_UNORM: + for (size_t i = 0; i < num_pixels * 4; i++) { + uint16_t v; + memcpy(&v, src + 2 * i, 2); + dst[i] = (uint8_t)(v >> 8); + } + break; + default: memcpy(dst, src, num_pixels * 4); break; + } +} +// }}} + +// Samplers {{{ +static VkSamplerAddressMode +address_mode(int wrap) { + switch (wrap) { + case 0: return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; + case 1: return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER; + case 2: return VK_SAMPLER_ADDRESS_MODE_REPEAT; + default: return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT; + } +} + +bool +create_samplers(Driver *d) { + for (int filter = 0; filter < 2; filter++) { + for (int wrap = 0; wrap < 4; wrap++) { + for (int integer = 0; integer < 2; integer++) { + // Integer textures cannot be filtered, they are only fetched from + const VkFilter f = filter && !integer ? VK_FILTER_LINEAR : VK_FILTER_NEAREST; + const VkSamplerCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, + .magFilter = f, + .minFilter = f, + .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST, + .addressModeU = address_mode(wrap), + .addressModeV = address_mode(wrap), + .addressModeW = address_mode(wrap), + .borderColor = integer ? VK_BORDER_COLOR_INT_TRANSPARENT_BLACK : VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK, + .maxLod = 0.f, + }; + VkResult r = vkCreateSampler(d->device, &info, NULL, &d->samplers[filter][wrap][integer]); + if (r != VK_SUCCESS) { + set_error(d, "Failed to create a Vulkan sampler with error: %s", vk_result_string(r)); + return false; + } + } + } + } + return true; +} + +void +destroy_samplers(Driver *d) { + for (int filter = 0; filter < 2; filter++) { + for (int wrap = 0; wrap < 4; wrap++) { + for (int integer = 0; integer < 2; integer++) { + if (d->samplers[filter][wrap][integer]) vkDestroySampler(d->device, d->samplers[filter][wrap][integer], NULL); + d->samplers[filter][wrap][integer] = VK_NULL_HANDLE; + } + } + } +} + +static VkSampler +sampler_for(Driver *d, const GPUTextureDesc *desc) { + const int filter = desc->filter == GPU_FILTER_LINEAR ? 1 : 0; + int wrap = 0; + switch (desc->wrap) { + case GPU_WRAP_CLAMP_TO_EDGE: wrap = 0; break; + case GPU_WRAP_CLAMP_TO_BORDER: wrap = 1; break; + case GPU_WRAP_REPEAT: wrap = 2; break; + case GPU_WRAP_MIRRORED_REPEAT: wrap = 3; break; + } + return d->samplers[filter][wrap][is_integer_format(vk_format_for(desc->format)) ? 1 : 0]; +} +// }}} + +// Images {{{ +static VkImageView +create_view(Driver *d, VkImage image, VkFormat format, VkImageViewType type, uint32_t layers) { + const VkImageViewCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, + .image = image, + .viewType = type, + .format = format, + .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .levelCount = 1, .layerCount = layers}, + }; + VkImageView ans = VK_NULL_HANDLE; + VkResult r = vkCreateImageView(d->device, &info, NULL, &ans); + if (r != VK_SUCCESS) { + set_error(d, "Failed to create a Vulkan image view with error: %s", vk_result_string(r)); + return VK_NULL_HANDLE; + } + return ans; +} + +bool +create_image_views(Driver *d, Image *img, VkImageViewType sampled_type) { + // Views for sampling use all layers, views for rendering only the first + if (sampled_type != VK_IMAGE_VIEW_TYPE_MAX_ENUM) { + if (!(img->sampled_view = create_view(d, img->image, img->format, sampled_type, img->layers))) return false; + } + if (img->unorm_format && !(img->unorm_view = create_view(d, img->image, img->unorm_format, VK_IMAGE_VIEW_TYPE_2D, 1))) return false; + if (img->srgb_format && !(img->srgb_view = create_view(d, img->image, img->srgb_format, VK_IMAGE_VIEW_TYPE_2D, 1))) return false; + return true; +} + +void +destroy_image_views(Driver *d, Image *img) { + if (img->sampled_view) vkDestroyImageView(d->device, img->sampled_view, NULL); + if (img->unorm_view) vkDestroyImageView(d->device, img->unorm_view, NULL); + if (img->srgb_view) vkDestroyImageView(d->device, img->srgb_view, NULL); + img->sampled_view = img->unorm_view = img->srgb_view = VK_NULL_HANDLE; +} + +static bool +create_texture_image(Driver *d, Image *img, const GPUTextureDesc *desc) { + *img = (Image){0}; + const VkFormat format = vk_format_for(desc->format); + if (format == VK_FORMAT_UNDEFINED) { + set_error(d, "Unknown texture format: %d", (int)desc->format); + return false; + } + VkFormatProperties fp; + vkGetPhysicalDeviceFormatProperties(d->physical_device, format, &fp); + const VkFormatFeatureFlags needed = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT; + if ((fp.optimalTilingFeatures & needed) != needed) { + set_error(d, "The GPU does not support textures of the format: %d", (int)format); + return false; + } + const bool renderable = !is_integer_format(format) && (fp.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT); + img->format = format; + img->width = MAX(1u, desc->width); + img->height = MAX(1u, desc->height); + img->layers = desc->kind == GPU_TEXTURE_2D_ARRAY ? MAX(1u, desc->layers) : 1; + const bool is_srgb = format == VK_FORMAT_R8G8B8A8_SRGB; + if (renderable) { + // Rendering into sRGB textures encodes to sRGB only when sRGB output + // is enabled, see set_srgb_output(), so they need both kinds of views + img->unorm_format = is_srgb ? VK_FORMAT_R8G8B8A8_UNORM : format; + img->srgb_format = is_srgb ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_UNDEFINED; + } + const VkFormat view_formats[2] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB}; + const VkImageFormatListCreateInfo format_list = { + .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO, .viewFormatCount = 2, .pViewFormats = view_formats}; + const VkImageCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .pNext = is_srgb && renderable ? &format_list : NULL, + .flags = is_srgb && renderable ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0, + .imageType = VK_IMAGE_TYPE_2D, + .format = format, + .extent = {img->width, img->height, 1}, + .mipLevels = 1, + .arrayLayers = img->layers, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | + (renderable ? VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT : 0), + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, + }; + VkResult r = vkCreateImage(d->device, &info, NULL, &img->image); + if (r != VK_SUCCESS) { + set_error(d, "Failed to create a Vulkan image of size %ux%ux%u with error: %s", img->width, img->height, img->layers, vk_result_string(r)); + return false; + } + VkMemoryRequirements req; + vkGetImageMemoryRequirements(d->device, img->image, &req); + if (!allocate_memory(d, &req, MEMORY_DEVICE, &img->alloc)) goto fail; + if ((r = vkBindImageMemory(d->device, img->image, img->alloc.memory, img->alloc.offset)) != VK_SUCCESS) { + set_error(d, "Failed to bind image memory with error: %s", vk_result_string(r)); + goto fail; + } + if (!create_image_views(d, img, desc->kind == GPU_TEXTURE_2D_ARRAY ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D)) goto fail; + img->state = (ImageState){.layout = VK_IMAGE_LAYOUT_UNDEFINED}; + return true; +fail: + destroy_image_views(d, img); + vkDestroyImage(d->device, img->image, NULL); + free_memory(d, &img->alloc); + *img = (Image){0}; + return false; +} +// }}} + +// Textures {{{ +static bool +upload(Driver *d, Texture *t, uint32_t x, uint32_t y, uint32_t layer, uint32_t width, uint32_t height, const GPUPixelData *pixels) { + Image *img = &t->image; + if (!width || !height) return true; + if ((uint64_t)x + width > img->width || (uint64_t)y + height > img->height || layer >= img->layers) { + set_error(d, "Texture upload out of bounds"); + return false; + } + const VkDeviceSize size = (VkDeviceSize)width * height * texel_size(img->format); + VkBuffer buffer; + VkDeviceSize offset; + void *mapped; + if (!allocate_staging(d, size, MAX(16u, texel_size(img->format)), &buffer, &offset, &mapped)) return false; + if (!convert_pixels(pixels, width, height, img->format, mapped)) { + set_error(d, "Cannot upload pixels of layout: %d to a texture of format: %d", (int)pixels->layout, (int)img->format); + return false; + } + transition_image(d, img, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); + const VkBufferImageCopy region = { + .bufferOffset = offset, + .imageSubresource = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseArrayLayer = layer, .layerCount = 1}, + .imageOffset = {(int32_t)x, (int32_t)y, 0}, + .imageExtent = {width, height, 1}, + }; + vkCmdCopyBufferToImage(command_buffer(d), buffer, img->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); + return true; +} + +static bool +init_texture(Driver *d, Texture *t, const GPUTextureDesc *desc, const GPUPixelData *initial) { + if (!create_texture_image(d, &t->image, desc)) return false; + t->desc = *desc; + t->sampler = sampler_for(d, desc); + if (initial) { + // The pixels of all layers, one after the other + const size_t layer_size = row_stride(pixel_layout_size(initial->layout) * t->image.width, initial->row_alignment) * t->image.height; + for (uint32_t layer = 0; layer < t->image.layers; layer++) { + GPUPixelData p = *initial; + p.data = (const uint8_t *)initial->data + layer * layer_size; + if (!upload(d, t, 0, 0, layer, t->image.width, t->image.height, &p)) return false; + } + } + return true; +} + +gpu_texture_t +texture_create(void *driver, const GPUTextureDesc *desc, const GPUPixelData *initial) { + Driver *d = driver; + Texture *t = calloc(1, sizeof(Texture)); + if (!t) { + set_error(d, "Out of memory"); + return GPU_NULL_HANDLE; + } + if (!init_texture(d, t, desc, initial)) { + if (t->image.image) defer_destroy_image(d, &t->image); + free(t); + return GPU_NULL_HANDLE; + } + LIST_ADD(d->textures, t); + return AS_HANDLE(t); +} + +bool +texture_reset(void *driver, gpu_texture_t texture, const GPUTextureDesc *desc, const GPUPixelData *initial) { + Driver *d = driver; + Texture *t = AS_TEXTURE(texture); + defer_destroy_image(d, &t->image); + if (!init_texture(d, t, desc, initial)) { + if (t->image.image) defer_destroy_image(d, &t->image); + return false; + } + return true; +} + +void +texture_destroy(void *driver, gpu_texture_t texture) { + Driver *d = driver; + Texture *t = AS_TEXTURE(texture); + if (!t) return; + for (size_t i = 0; i < arraysz(d->bound_textures); i++) { + if (d->bound_textures[i] == t) d->bound_textures[i] = NULL; + } + if (t->image.image) defer_destroy_image(d, &t->image); + LIST_REMOVE(d->textures, t); + free(t); +} + +void +destroy_all_textures(Driver *d) { + while (d->textures) { + Texture *t = d->textures; + destroy_image_views(d, &t->image); + if (t->image.image) vkDestroyImage(d->device, t->image.image, NULL); + free_memory(d, &t->image.alloc); + LIST_REMOVE(d->textures, t); + free(t); + } +} + +void +texture_upload(void *driver, gpu_texture_t texture, uint32_t x, uint32_t y, uint32_t layer, uint32_t width, uint32_t height, const GPUPixelData *pixels) { + Driver *d = driver; + if (!upload(d, AS_TEXTURE(texture), x, y, layer, width, height, pixels)) log_error(d, "Failed to upload texture: %s", d->last_error); +} + +void +texture_copy(void *driver, gpu_texture_t src, gpu_texture_t dst) { + Driver *d = driver; + Texture *s = AS_TEXTURE(src), *t = AS_TEXTURE(dst); + if (s->image.format != t->image.format || s->image.width > t->image.width || s->image.height > t->image.height) { + log_error(d, "Cannot copy between incompatible textures"); + return; + } + transition_image(d, &s->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_READ_BIT); + transition_image(d, &t->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); + const uint32_t layers = MIN(s->image.layers, t->image.layers); + const VkImageCopy region = { + .srcSubresource = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .layerCount = layers}, + .dstSubresource = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .layerCount = layers}, + .extent = {s->image.width, s->image.height, 1}, + }; + vkCmdCopyImage(command_buffer(d), s->image.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, t->image.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); +} + +bool +texture_read(void *driver, gpu_texture_t texture, GPUPixelLayout layout, void *dst, size_t dst_size) { + Driver *d = driver; + Texture *t = AS_TEXTURE(texture); + Image *img = &t->image; + const size_t tsz = texel_size(img->format), npx = (size_t)img->width * img->height; + const size_t out_bpp = pixel_layout_size(layout), out_stride = row_stride(out_bpp * img->width, 4); + if (dst_size < out_stride * img->height) { + set_error(d, "The buffer for reading the texture is too small"); + return false; + } + const bool converts_to_rgba8 = channels_8bit(img->format) == 4 || img->format == VK_FORMAT_R16G16B16A16_UNORM; + if (layout == GPU_PIXELS_RGBA8 ? !converts_to_rgba8 : out_bpp != tsz) { + set_error(d, "Cannot read a texture of format %d as pixel layout %d", (int)img->format, (int)layout); + return false; + } + VkBuffer buffer; + Allocation alloc; + if (!create_buffer_with_memory(d, npx * tsz, VK_BUFFER_USAGE_TRANSFER_DST_BIT, MEMORY_READBACK, &buffer, &alloc)) return false; + transition_image(d, img, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_READ_BIT); + const VkBufferImageCopy region = { + .imageSubresource = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .layerCount = 1}, + .imageExtent = {img->width, img->height, 1}, + }; + vkCmdCopyImageToBuffer(command_buffer(d), img->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer, 1, ®ion); + submit_commands(d, NULL, 0, true); + const uint8_t *src = alloc.mapped; + for (uint32_t y = 0; y < img->height; y++) { + uint8_t *out = (uint8_t *)dst + y * out_stride; + const uint8_t *in = src + (size_t)y * img->width * tsz; + if (layout == GPU_PIXELS_RGBA8) convert_to_rgba8(img->format, in, out, img->width); + else memcpy(out, in, (size_t)img->width * tsz); + } + vkDestroyBuffer(d->device, buffer, NULL); + free_memory(d, &alloc); + return true; +} + +void +texture_get_desc(void *driver UNUSED, gpu_texture_t texture, GPUTextureDesc *ans) { + *ans = AS_TEXTURE(texture)->desc; +} + +void +bind_texture(void *driver, uint32_t slot, gpu_texture_t texture) { + Driver *d = driver; + if (slot >= MAX_BINDING_SLOTS) { + log_error(d, "Invalid texture binding slot: %u", slot); + return; + } + d->bound_textures[slot] = AS_TEXTURE(texture); + d->cmd_state.descriptors_dirty = true; +} +// }}} diff --git a/gpu/vulkan/version.h b/gpu/vulkan/version.h new file mode 100644 index 000000000..fa7a5ab94 --- /dev/null +++ b/gpu/vulkan/version.h @@ -0,0 +1,15 @@ +/* + * version.h + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#pragma once + +// Required minimum Vulkan version +#define VULKAN_REQUIRED_VERSION_MAJOR 1 +#define VULKAN_REQUIRED_VERSION_MINOR 3 +// The SPIR-V version shaders are compiled to, as major * 10 + minor. Vulkan +// 1.3 guarantees support for SPIR-V 1.6. +#define SPIRV_VERSION 16 diff --git a/kitty/gpu.c b/kitty/gpu.c index 3bda973a2..8692dc8c1 100644 --- a/kitty/gpu.c +++ b/kitty/gpu.c @@ -586,8 +586,8 @@ set_program_layout(int program, PyObject *metadata) { return false; } // The layout of the block is taken from the reflection data, so - // verify that it matches what the driver actually uses - if (size != info.size) { + // verify that it matches what the driver actually uses, if the driver knows + if (info.size && size != info.size) { PyErr_Format( PyExc_ValueError, "The uniform block: %s in GPU program: %d has size %u according to the GPU driver but %u according to the shader reflection data", diff --git a/kitty/shaders/cell.slang b/kitty/shaders/cell.slang index bcc7d5b2a..958ca9317 100644 --- a/kitty/shaders/cell.slang +++ b/kitty/shaders/cell.slang @@ -24,7 +24,7 @@ static const bool ONLY_BACKGROUND = RENDER_MODE == 1; // that they can be shared with the padding shader. [[vk::binding(2)]] -uniform Sampler2D sprite_decorations_map; // binding slots: see SHADER_BINDING_SLOTS in slang.py +uniform Sampler2D sprite_decorations_map; // a single channel R32UI texture // binding slots: see SHADER_BINDING_SLOTS in slang.py static const uint SPRITE_INDEX_MASK = 0x7fffffff; static const uint SPRITE_COLORED_MASK = 0x80000000; @@ -98,7 +98,7 @@ read_sprite_decorations_idx(uint2 sprite_idx) { int2 sz = int2(width, height); int y = idx / sz[0]; int x = idx - y * sz[0]; - return sprite_decorations_map[int2(x, y)].r; + return sprite_decorations_map[int2(x, y)]; } uint2 diff --git a/kitty/shaders/indexed_graphics.slang b/kitty/shaders/indexed_graphics.slang index a73afa208..b5fc12871 100644 --- a/kitty/shaders/indexed_graphics.slang +++ b/kitty/shaders/indexed_graphics.slang @@ -40,11 +40,11 @@ vertex_main(uint vertex_id: SV_VertexID) { // The red channel is the palette index and, when has_alpha is non-zero, the // green channel is the straight alpha. [[vk::binding(1)]] -uniform Sampler2D image; // binding slots: see SHADER_BINDING_SLOTS in slang.py +uniform Sampler2D image; // a two channel RG8UI texture // binding slots: see SHADER_BINDING_SLOTS in slang.py float4 premult_color_at(int2 pos, uint has_alpha) { - uint4 px = image[pos]; + uint2 px = image[pos]; float alpha = has_alpha != 0 ? float(px.g) / 255.0 : 1.0; return float4(indexed_image_color(px.r) * alpha, alpha); } diff --git a/kitty_tests/check_build.py b/kitty_tests/check_build.py index d42efe8f5..cb3aab516 100644 --- a/kitty_tests/check_build.py +++ b/kitty_tests/check_build.py @@ -151,15 +151,25 @@ class TestBuild(BaseTest): self.assertTrue(os.access(path, os.X_OK), f'{path} is not executable') def test_gpu_drivers(self) -> None: - from kitty.constants import gpu_driver_path - - path = gpu_driver_path('opengl') - self.assertTrue(os.path.isfile(path), f'{path} is not a file') - self.assertTrue(os.access(path, os.X_OK), f'{path} is not executable') import ctypes + import glob - lib = ctypes.CDLL(path) - self.assertTrue(hasattr(lib, 'kitty_gpu_driver_load'), f'{path} does not export kitty_gpu_driver_load') + from kitty.constants import gpu_driver_path, is_macos, shaders_dir + + def check(name: str) -> None: + path = gpu_driver_path(name) + self.assertTrue(os.path.isfile(path), f'{path} is not a file') + self.assertTrue(os.access(path, os.X_OK), f'{path} is not executable') + lib = ctypes.CDLL(path) + self.assertTrue(hasattr(lib, 'kitty_gpu_driver_load'), f'{path} does not export kitty_gpu_driver_load') + + check('opengl') + # The Vulkan driver is built only when the Vulkan headers are available + if not is_macos and os.path.exists(gpu_driver_path('vulkan')): + check('vulkan') + self.assertTrue( + glob.glob(os.path.join(shaders_dir, 'spirv-*', 'cell.json')), 'The built-in shaders have not been compiled for the Vulkan GPU driver' + ) def test_gpu_driver_shaders(self) -> None: # The built-in shaders must have been compiled for the shader target of the driver diff --git a/setup.py b/setup.py index 72e4f6103..d46319da0 100755 --- a/setup.py +++ b/setup.py @@ -1303,7 +1303,35 @@ def build_wayland_protocols( def gpu_api_headers() -> List[str]: - return ['gpu/api.h', 'gpu/opengl/version.h'] + return ['gpu/api.h', 'gpu/opengl/version.h', 'gpu/vulkan/version.h'] + + +@lru_cache(maxsize=2) +def vulkan_headers_available() -> bool: + # The Vulkan driver needs only the Vulkan headers to build, libvulkan is loaded at runtime + cflags = env.cflags + pkg_config('x11', '--cflags-only-I', fatal=False) + src = '\n'.join( + ( + '#define VK_NO_PROTOTYPES', + '#define VK_USE_PLATFORM_XLIB_KHR', + '#define VK_USE_PLATFORM_WAYLAND_KHR', + '#include ', + 'int main(void) { return VK_API_VERSION_1_3 ? 0 : 1; }', + ) + ) + return bool(test_compile(env.cc, *cflags, src=src, link_also=False)) + + +def gpu_drivers_to_build() -> List[str]: + # The GPU drivers built for this platform. OpenGL is built on all platforms + # and Vulkan on platforms other than macOS, when its headers are available. + ans = ['opengl'] + if not is_macos: + if vulkan_headers_available(): + ans.insert(0, 'vulkan') + else: + print(error('The Vulkan headers were not found, the Vulkan GPU driver will not be built'), file=sys.stderr) + return ans def gpu_driver_init_env(env: Env, driver: str = 'opengl') -> Env: @@ -1323,6 +1351,11 @@ def gpu_driver_init_env(env: Env, driver: str = 'opengl') -> Env: for dep in ('x11', 'xrender'): ans.cflags.extend(pkg_config(dep, '--cflags-only-I')) ans.ldpaths.append('-ldl') + elif driver == 'vulkan': + # The X11 headers are needed for the Xlib surface functions. libvulkan + # and libX11 are loaded at runtime. + ans.cflags.extend(pkg_config('x11', '--cflags-only-I')) + ans.ldpaths.append('-ldl') return ans @@ -1336,6 +1369,12 @@ def gpu_driver_shader_targets(driver: str = 'opengl') -> List[str]: if m is None: raise SystemExit('Could not find GLSL_VERSION in gpu/opengl/version.h') return [f'glsl-{m.group(1)}'] + if driver == 'vulkan': + with open('gpu/vulkan/version.h') as f: + m = re.search(r'^#define SPIRV_VERSION (\d+)', f.read(), flags=re.MULTILINE) + if m is None: + raise SystemExit('Could not find SPIRV_VERSION in gpu/vulkan/version.h') + return [f'spirv-{m.group(1)}'] raise KeyError(f'Unknown GPU driver: {driver}') @@ -1344,11 +1383,13 @@ def gpu_driver_sources(driver: str) -> List[str]: common = ['driver.c', 'gl-wrapper.c'] platform = ['nsgl.m'] if is_macos else ['egl.c', 'glx.c'] return [f'gpu/{driver}/{x}' for x in common + platform] + if driver == 'vulkan': + return [f'gpu/{driver}/{x}' for x in ('driver.c', 'memory.c', 'commands.c', 'textures.c', 'pipelines.c', 'surfaces.c')] raise KeyError(f'Unknown GPU driver: {driver}') def compile_gpu_drivers(compilation_database: CompilationDatabase, build_dsym: bool = False) -> None: - for driver in ('opengl',): + for driver in gpu_drivers_to_build(): denv = gpu_driver_init_env(env, driver) sources = gpu_driver_sources(driver) headers = sorted(set(glob.glob(f'gpu/{driver}/*.h') + gpu_api_headers())) @@ -1504,7 +1545,7 @@ def generate_shaders(kitty_exe: str) -> None: '+launch', os.path.join(src_base, 'kitty/shaders/slang.py'), 'verbose' if verbose else '', - '--targets=' + ','.join(t for driver in ('opengl',) for t in gpu_driver_shader_targets(driver)), + '--targets=' + ','.join(t for driver in gpu_drivers_to_build() for t in gpu_driver_shader_targets(driver)), 'build/shaders', 'shaders', ],