Update a bunch of stuff

This commit is contained in:
Luna 2025-02-16 17:16:16 +01:00
parent bf1ccd498e
commit f3b2fe4cdb
3 changed files with 67 additions and 41 deletions

84
kitty.c
View file

@ -46,6 +46,8 @@ struct Kitty {
const char *fragment_path;
struct VertexBuffer vertex_buffer;
struct IndexBuffer index_buffer;
struct da_Attatchment attatchments;
uint32_t push_constant_size;
@ -57,54 +59,59 @@ struct Kitty {
};
struct Kitty *kitty_make() {
struct Kitty *thingy = malloc(sizeof(struct Kitty));
memset(thingy, 0, sizeof(struct Kitty));
dyn_array_create_inplace(&thingy->attatchments);
dyn_array_create_inplace(&thingy->vertex_buffer.format);
meow("alloced a thingy at %p", thingy);
return thingy;
struct Kitty *kitty = malloc(sizeof(struct Kitty));
memset(kitty, 0, sizeof(struct Kitty));
dyn_array_create_inplace(&kitty->attatchments);
dyn_array_create_inplace(&kitty->vertex_buffer.format);
meow("alloced a kitty at %p", kitty);
return kitty;
}
void kitty_set_push_constant_size(struct Kitty *thingy, uint32_t size) {
thingy->push_constant_size = size;
void kitty_set_push_constant_size(struct Kitty *kitty, uint32_t size) {
kitty->push_constant_size = size;
}
void kitty_set_vertex_shader(struct Kitty *thingy, const char *path) {
thingy->vertex_path = path;
void kitty_set_vertex_shader(struct Kitty *kitty, const char *path) {
kitty->vertex_path = path;
meow("vertex path is %s", path);
}
void kitty_set_fragment_shader(struct Kitty *thingy, const char *path) {
thingy->fragment_path = path;
void kitty_set_fragment_shader(struct Kitty *kitty, const char *path) {
kitty->fragment_path = path;
meow("fragment path is %s", path);
}
void kitty_set_vertex_buffer(struct Kitty *thingy, void *data, uint32_t count,
void kitty_set_vertex_buffer(struct Kitty *kitty, void *data, uint32_t count,
int vertex_size) {
thingy->vertex_buffer.data = data;
thingy->vertex_buffer.count = count;
thingy->vertex_buffer.vertex_size = vertex_size;
kitty->vertex_buffer.data = data;
kitty->vertex_buffer.count = count;
kitty->vertex_buffer.vertex_size = vertex_size;
}
void kitty_add_vertex_buffer_format(struct Kitty *thingy,
enum VkFormat format) {
void kitty_add_index_buffer(struct Kitty *kitty, void *data, uint32_t count) {
kitty->index_buffer.data = data;
kitty->index_buffer.count = count;
}
void kitty_add_vertex_buffer_format(struct Kitty *kitty, enum VkFormat format) {
meow("attatched %d to the buffer", format);
dyn_array_append(&thingy->vertex_buffer.format, format);
dyn_array_append(&kitty->vertex_buffer.format, format);
}
void kitty_attatch_image(struct Kitty *thingy, const char *path) {
void kitty_attatch_image(struct Kitty *kitty, const char *path) {
struct Attatchment attatchment = {0};
attatchment.type = CAT_ATTATCH_IMAGE;
attatchment.image.path = path;
dyn_array_append(&thingy->attatchments, attatchment);
dyn_array_append(&kitty->attatchments, attatchment);
meow("image was attatched");
}
int kitty_attatch_ubo(struct Kitty *thingy, uint32_t size) {
int kitty_attatch_ubo(struct Kitty *kitty, uint32_t size) {
struct Attatchment attatchment = {0};
attatchment.type = CAT_ATTATCH_UBO;
attatchment.ubo.size = size;
dyn_array_append(&thingy->attatchments, attatchment);
dyn_array_append(&kitty->attatchments, attatchment);
meow("ubo of size %d was attatched", size);
return thingy->attatchments.count - 1;
return kitty->attatchments.count - 1;
}
void kitty_finalise(struct Vk *state, struct Kitty *kitty) {
meow("this thingy has %d ubos", kitty->attatchments.count);
meow("this kitty has %d attatchments", kitty->attatchments.count);
if (kitty->vertex_path == NULL) {
meow("a kitty has no vertex shader :c");
@ -241,7 +248,7 @@ void kitty_finalise(struct Vk *state, struct Kitty *kitty) {
/* VkPushConstantRange push_constant_range = {0}; */
/* push_constant_range.offset = 0; */
/* push_constant_range.size = thingy->push_constant_size; */
/* push_constant_range.size = kitty->push_constant_size; */
/* push_constant_range.stageFlags = VK_SHADER_STAGE_ALL; */
VkPipelineLayoutCreateInfo pipeline_layout_info = {0};
@ -483,6 +490,7 @@ void kitty_finalise(struct Vk *state, struct Kitty *kitty) {
vkDestroyBuffer(state->device, staging_buffer, NULL);
vkFreeMemory(state->device, staging_buffer_memory, NULL);
kitty->vertex_buffer.data = NULL;
vkDestroyShaderModule(state->device, vert_module, NULL);
vkDestroyShaderModule(state->device, frag_module, NULL);
@ -533,24 +541,22 @@ void free_kitty(struct Vk *state, struct Kitty *kitty) {
free(kitty);
}
void kitty_set_next_push_constant(struct Kitty *thingy, void *data) {
thingy->next_push_constant = data;
void kitty_set_next_push_constant(struct Kitty *kitty, void *data) {
kitty->next_push_constant = data;
}
void kitty_set_next_ubo(struct Vk *state, struct Kitty *thingy, int index,
void kitty_set_next_ubo(struct Vk *state, struct Kitty *kitty, int index,
void *data) {
void *dst =
thingy->attatchments.items[index].ubo.mapped[state->current_frame];
void *dst = kitty->attatchments.items[index].ubo.mapped[state->current_frame];
void *src = data;
int size = thingy->attatchments.items[index].ubo.size;
/* meow("index %d, number %f", index, *(float *)data); */
int size = kitty->attatchments.items[index].ubo.size;
memcpy(dst, src, size);
}
void kitty_draw(struct Vk *state, uint32_t image_index, struct Kitty *thingy) {
void kitty_draw(struct Vk *state, uint32_t image_index, struct Kitty *kitty) {
struct InFlightObjects flight = state->flights[state->current_frame];
vkCmdBindPipeline(flight.command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
thingy->pipeline);
kitty->pipeline);
VkViewport viewport = {0};
viewport.x = 0.0f;
@ -566,14 +572,14 @@ void kitty_draw(struct Vk *state, uint32_t image_index, struct Kitty *thingy) {
scissor.extent = (VkExtent2D){state->width, state->heigh};
vkCmdSetScissor(flight.command_buffer, 0, 1, &scissor);
VkBuffer vertex_buffer[] = {thingy->vertex_buffer.buffer};
VkBuffer vertex_buffer[] = {kitty->vertex_buffer.buffer};
VkDeviceSize offsets[] = {0};
vkCmdBindVertexBuffers(flight.command_buffer, 0, 1, vertex_buffer, offsets);
vkCmdBindDescriptorSets(
flight.command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
thingy->pipeline_layout, 0, 1,
&thingy->descriptor_sets[state->current_frame], 0, NULL);
kitty->pipeline_layout, 0, 1,
&kitty->descriptor_sets[state->current_frame], 0, NULL);
vkCmdDraw(flight.command_buffer, thingy->vertex_buffer.count, 1, 0, 0);
vkCmdDraw(flight.command_buffer, kitty->vertex_buffer.count, 1, 0, 0);
}

17
kitty.h
View file

@ -8,19 +8,32 @@ struct Kitty;
struct Kitty *kitty_make();
void kitty_set_vertex_shader(struct Kitty *thingy, const char *path);
void kitty_set_fragment_shader(struct Kitty *thingy, const char *path);
// 'data' can be freed after 'kitty_finalise' is called, ownership is not
// transferred
void kitty_set_vertex_buffer(struct Kitty *thingy, void *data, uint32_t count,
int vertex_size);
void kitty_add_vertex_buffer_format(struct Kitty *thingy, enum VkFormat format);
// 'data' can be freed after 'kitty_finalise' is called, ownership is not
// transferred
void kitty_add_index_buffer(struct Kitty *kitty, void *data, uint32_t count);
void kitty_set_push_constant_size(struct Kitty *thingy, uint32_t size);
// the returned number can be used in 'kitty_set_next_ubo' to update the content
// of the buffer
int kitty_attatch_ubo(struct Kitty *thingy, uint32_t size);
void kitty_attatch_image(struct Kitty *thingy, const char *path);
void kitty_finalise(struct Vk *state, struct Kitty *thingy);
void kitty_set_next_push_constant(struct Kitty *thingy, void *data);
void kitty_set_next_ubo(struct Vk *state, struct Kitty *thingy, int index,
void *data);
void kitty_draw(struct Vk *state, uint32_t image_index, struct Kitty *thingy);
void free_kitty(struct Vk *state, struct Kitty *kitty);
void kitty_attatch_image(struct Kitty *thingy, const char *path);
void kitty_set_push_constant_size(struct Kitty *thingy, uint32_t size);
#endif // INCLUDE_KITTY

View file

@ -32,6 +32,13 @@ struct VertexBuffer {
struct da_VK_FORMAT format;
};
struct IndexBuffer {
void *data;
VkBuffer buffer;
VkDeviceMemory memory;
uint32_t count;
};
struct InFlightObjects {
VkSemaphore image_available_semaphore;
VkSemaphore render_finished_semaphore;