Fixed r0.c problems. Finally, now r0.c runs on MargaretMemAllocator!!!!! And everything is wroking!!!

This commit is contained in:
Андреев Григорий 2025-12-02 20:09:29 +03:00
parent fac2fde22b
commit cadde8714b
7 changed files with 146 additions and 109 deletions

View File

@ -367,7 +367,7 @@ RBTreeNode_KVPU64ToMargaretMAOccupation* MargaretMemAllocatorRequests_alloc_imag
VecMargaretMemAllocatorRequestAllocImage_append(&self->alloc_image, (MargaretMemAllocatorRequestAllocImage){
.width = width, .height = height, .format = format, .usage_flags = usage, .current_layout = current_layout,
.current_dest_stage_mask = current_dest_stage_mask, .current_dest_access_mask = current_dest_access_mask,
.preserve_at_quiet = preserve_at_quiet
.preserve_at_quiet = preserve_at_quiet, .new_node = new_node
});
return new_node;
}
@ -547,13 +547,15 @@ void MargaretMemAllocator__insert_gap(
/* didn't generate it, so I am doing it myself */
void RBTree_MapU64ToMargaretMAOccupation_insert_node(
RBTree_MapU64ToMargaretMAOccupation* self, RBTreeNode_KVPU64ToMargaretMAOccupation* node){
RBTree_MapU64ToMargaretMAOccupation* self, RBTreeNode_KVPU64ToMargaretMAOccupation* node
){
node->base.left = node->base.right = self->NIL;
node->base.color = RBTREE_RED;
if (self->root == self->NIL) {
self->root = &node->base;
node->base.parent = self->NIL;
node->base.color = RBTREE_BLACK;
return;
}
RBTreeNode_KVPU64ToMargaretMAOccupation* cur = (RBTreeNode_KVPU64ToMargaretMAOccupation*)self->root;
while (true) {
@ -562,6 +564,7 @@ void RBTree_MapU64ToMargaretMAOccupation_insert_node(
node->base.parent = &cur->base;
cur->base.left = &node->base;
RBTree_fix_after_insert(&self->root, self->NIL, &node->base);
return;
} else {
cur = (RBTreeNode_KVPU64ToMargaretMAOccupation*)cur->base.left;
}
@ -570,6 +573,7 @@ void RBTree_MapU64ToMargaretMAOccupation_insert_node(
node->base.parent = &cur->base;
cur->base.right = &node->base;
RBTree_fix_after_insert(&self->root, self->NIL, &node->base);
return;
} else {
cur = (RBTreeNode_KVPU64ToMargaretMAOccupation*)cur->base.right;
}
@ -760,6 +764,41 @@ void MargaretMemAllocator__shrink_some_buffer(
occ_me->buf.capacity = smaller_size;
}
void MargaretMemAllocator__bind_buffer_memory(const MargaretMemAllocator* self, RBTreeNode_KVPU64ToMargaretMAOccupation* occ_it){
assert(occ_it->value.me.variant == MargaretMemoryOccupation_Buffer);
check(vkBindBufferMemory(self->device, occ_it->value.me.buf.buffer, occ_it->value.block->el.mem_hand, occ_it->key) == VK_SUCCESS);
}
void MargaretMemAllocator__bind_image_memory(const MargaretMemAllocator* self, RBTreeNode_KVPU64ToMargaretMAOccupation* occ_it){
assert(occ_it->value.me.variant == MargaretMemoryOccupation_Image);
check(vkBindImageMemory(self->device, occ_it->value.me.img.image, occ_it->value.block->el.mem_hand, occ_it->key) == VK_SUCCESS);
}
void MargaretMemAllocator__thrust_a_block(
MargaretMemAllocator* self, ListNodeMargaretMemAllocatorOneBlock* cur_block, U64 capacity
){
check(vkAllocateMemory(self->device, &(VkMemoryAllocateInfo){
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.memoryTypeIndex = self->memory_type_id,
.allocationSize = capacity}, NULL, &cur_block->el.mem_hand) == VK_SUCCESS);
cur_block->el.capacity = capacity;
// todo: when I write MargaretBufAllocator and rewrite r0.c to use it, I will remove this whole "void* mapped_memory" crap
if (self->mem_properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
check(vkMapMemory(self->device, cur_block->el.mem_hand, 0, VK_WHOLE_SIZE,
0, &cur_block->el.mapped_memory) == VK_SUCCESS);
}
RBTreeNode_KVPU64ToMargaretMAOccupation* occ_it = RBTree_MapU64ToMargaretMAOccupation_find_min(&cur_block->el.occupied_memory);
while (occ_it) {
if (occ_it->value.me.variant == MargaretMemoryOccupation_Buffer) {
MargaretMemAllocator__bind_buffer_memory(self, occ_it);
} else {
MargaretMemAllocator__bind_image_memory(self, occ_it);
}
occ_it = RBTree_MapU64ToMargaretMAOccupation_find_next(&cur_block->el.occupied_memory, occ_it);
}
}
/* Helper function for MargaretMemAllocator_request_needs_defragmentation */
void MargaretMemAllocator__keep_building_up_cur_block(
MargaretMemAllocator* self, U64* cur_block_size_needed, ListNodeMargaretMemAllocatorOneBlock** cur_block,
@ -767,22 +806,21 @@ void MargaretMemAllocator__keep_building_up_cur_block(
VkMemoryRequirements mem_requirements, RBTreeNode_KVPU64ToMargaretMAOccupation* occ_it,
U64 maxMemoryAllocationSize
){
assert(*cur_block_size_needed < maxMemoryAllocationSize);
check(U64_is_2pow(mem_requirements.alignment));
if (mem_requirements.size > maxMemoryAllocationSize)
abortf("Your object asks too much :(\n");
U64 af = margaret_get_alignment_left_padding(*cur_block_size_needed, U64_2pow_log(mem_requirements.alignment));
if (mem_requirements.size > maxMemoryAllocationSize) {
abortf("Your life sucks\n");
}
if (*cur_block_size_needed + af + mem_requirements.size > maxMemoryAllocationSize) {
MargaretMemAllocator__insert_gap(self, *cur_block,
/* We use mem man method here directly because MMA__insert_gap has a side effect of requiring valid block */
MargaretMemFreeSpaceManager_insert(&self->mem_free_space, *cur_block,
*cur_block_size_needed, maxMemoryAllocationSize - *cur_block_size_needed);
vkAllocateMemory(self->device, &(VkMemoryAllocateInfo){
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.memoryTypeIndex = self->memory_type_id,
.allocationSize = maxMemoryAllocationSize}, NULL, &((*cur_block)->el.mem_hand));
(*cur_block)->el.capacity = maxMemoryAllocationSize;
MargaretMemAllocator__thrust_a_block(self, *cur_block, maxMemoryAllocationSize);
*updated_total_capacity += maxMemoryAllocationSize;
if (self->mem_properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
vkMapMemory(self->device, (*cur_block)->el.mem_hand, 0, VK_WHOLE_SIZE, 0, &(*cur_block)->el.mapped_memory);
}
*cur_block = (ListNodeMargaretMemAllocatorOneBlock*)safe_calloc(1, sizeof(ListNodeMargaretMemAllocatorOneBlock));
(*cur_block)->el.occupied_memory = RBTree_MapU64ToMargaretMAOccupation_new();
@ -791,7 +829,9 @@ void MargaretMemAllocator__keep_building_up_cur_block(
*cur_block_size_needed = 0;
af = 0;
}
MargaretMemAllocator__insert_gap(self, *cur_block, *cur_block_size_needed, af);
/* We use mem man method here directly because MMA__insert_gap has a side effect of requiring valid block,
* which is just not the case */
MargaretMemFreeSpaceManager_insert(&self->mem_free_space, *cur_block, *cur_block_size_needed, af);
occ_it->key = *cur_block_size_needed + af;
occ_it->value.block = *cur_block;
occ_it->value.taken_size = mem_requirements.size;
@ -801,16 +841,6 @@ void MargaretMemAllocator__keep_building_up_cur_block(
*cur_block_size_needed = *cur_block_size_needed + af + mem_requirements.size;
}
void MargaretMemAllocator__bind_buffer_memory(const MargaretMemAllocator* self, RBTreeNode_KVPU64ToMargaretMAOccupation* occ_it){
assert(occ_it->value.me.variant == MargaretMemoryOccupation_Buffer);
check(vkBindImageMemory(self->device, occ_it->value.me.img.image, occ_it->value.block->el.mem_hand, occ_it->key) == VK_SUCCESS);
}
void MargaretMemAllocator__bind_image_memory(const MargaretMemAllocator* self, RBTreeNode_KVPU64ToMargaretMAOccupation* occ_it){
assert(occ_it->value.me.variant == MargaretMemoryOccupation_Image);
check(vkBindImageMemory(self->device, occ_it->value.me.img.image, occ_it->value.block->el.mem_hand, occ_it->key) == VK_SUCCESS);
}
MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
MargaretMemAllocator* self, MargaretMemAllocatorRequests* requests,
VecMargaretMABufferExpansionRecord buffer_expansion_record,
@ -877,12 +907,12 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
for (size_t i = 0; i < alloc_buf_requests_require_cancel; i++) {
RBTreeNode_KVPU64ToMargaretMAOccupation* given_occ_it = requests->alloc_buf.buf[i].new_node;
assert(given_occ_it && given_occ_it->value.me.variant == MargaretMemoryOccupation_Buffer);
MargaretMemAllocator__get_rid_of_memory_occupant_and_node(self, given_occ_it);
MargaretMemAllocator__get_rid_of_memory_occupant_but_not_node(self, given_occ_it);
}
for (size_t i = 0; i < alloc_img_requests_require_cancel; i++) {
RBTreeNode_KVPU64ToMargaretMAOccupation* given_occ_it = requests->alloc_image.buf[i].new_node;
assert(given_occ_it && given_occ_it->value.me.variant == MargaretMemoryOccupation_Image);
MargaretMemAllocator__get_rid_of_memory_occupant_and_node(self, given_occ_it);
MargaretMemAllocator__get_rid_of_memory_occupant_but_not_node(self, given_occ_it);
}
/* END OF REVERTING. WE REVERTED EVERYTHING */
@ -934,7 +964,7 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
VkBuffer fresh_buf;
check(vkCreateBuffer(self->device, &(VkBufferCreateInfo){
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.flags = replacer->value.me.buf.usage_flags,
.usage = replacer->value.me.buf.usage_flags,
.size = needed_buf_capacity,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE
}, NULL, &fresh_buf) == VK_SUCCESS);
@ -947,7 +977,6 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
MargaretMemAllocator__keep_building_up_cur_block(self,
&cur_block_size_needed, &cur_block, &updated_total_capacity, mem_requirements,
occ_it, maintenance3_properties.maxMemoryAllocationSize);
MargaretMemAllocator__bind_buffer_memory(self, occ_it);
if (occ_it->value.me.buf.preserve_at_quiet) {
vkCmdCopyBuffer(self->command_buffer, replacer->value.me.buf.buffer, occ_it->value.me.buf.buffer, 1, &(VkBufferCopy){
@ -962,7 +991,7 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
VkBuffer fresh_buf;
check(vkCreateBuffer(self->device, &(VkBufferCreateInfo){
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.flags = req->usage,
.usage = req->usage,
.size = needed_buf_capacity,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE
}, NULL, &fresh_buf) == VK_SUCCESS);
@ -975,11 +1004,11 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
occ_it->value.me.buf.capacity = needed_buf_capacity;
occ_it->value.me.buf.usage_flags = req->usage;
occ_it->value.me.buf.preserve_at_quiet = req->preserve_at_quiet;
occ_it->value.me.buf.moved_already = false;
MargaretMemAllocator__keep_building_up_cur_block(self, &cur_block_size_needed,
&cur_block, &updated_total_capacity, mem_requirements,
occ_it, maintenance3_properties.maxMemoryAllocationSize);
MargaretMemAllocator__bind_buffer_memory(self, occ_it);
}
for (size_t i = 0; i < requests->alloc_image.len; i++) {
@ -1014,7 +1043,6 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
MargaretMemAllocator__keep_building_up_cur_block(self, &cur_block_size_needed,
&cur_block, &updated_total_capacity, mem_requirements,
occ_it, maintenance3_properties.maxMemoryAllocationSize);
MargaretMemAllocator__bind_image_memory(self, occ_it);
}
/* We move blocks here, but not because some request asked, no, we migrate all unmoved blocks from */
@ -1037,7 +1065,7 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
VkBuffer fresh_buf;
check(vkCreateBuffer(self->device, &(VkBufferCreateInfo){
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.flags = replacer->value.me.buf.usage_flags,
.usage = replacer->value.me.buf.usage_flags,
.size = replacer->value.me.buf.capacity,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE
}, NULL, &fresh_buf) == VK_SUCCESS);
@ -1050,7 +1078,6 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
MargaretMemAllocator__keep_building_up_cur_block(self, &cur_block_size_needed,
&cur_block, &updated_total_capacity, mem_requirements,
occ_it, maintenance3_properties.maxMemoryAllocationSize);
MargaretMemAllocator__bind_buffer_memory(self, occ_it);
if (occ_it->value.me.buf.preserve_at_quiet) {
vkCmdCopyBuffer(self->command_buffer, replacer->value.me.buf.buffer, occ_it->value.me.buf.buffer, 1, &(VkBufferCopy){
@ -1080,7 +1107,6 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
MargaretMemAllocator__keep_building_up_cur_block(self, &cur_block_size_needed,
&cur_block, &updated_total_capacity, mem_requirements,
occ_it, maintenance3_properties.maxMemoryAllocationSize);
MargaretMemAllocator__bind_image_memory(self, occ_it);
if (occ_it->value.me.img.preserve_at_quiet) {
VkImageMemoryBarrier first_barriers[2] = {
@ -1163,15 +1189,10 @@ MargaretMemAllocatorDemands MargaretMemAllocator_request_needs_defragmentation(
U64 shizhlyazhki = desirable_gain > updated_total_capacity ? desirable_gain - updated_total_capacity : 0;
U64 fin_block_capacity = MIN_U64(maintenance3_properties.maxMemoryAllocationSize,
MAX_U64(cur_block_size_needed, shizhlyazhki));
cur_block->el.capacity = fin_block_capacity;
MargaretMemAllocator__thrust_a_block(self, cur_block, fin_block_capacity);
updated_total_capacity += fin_block_capacity;
vkAllocateMemory(self->device, &(VkMemoryAllocateInfo){
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.memoryTypeIndex = self->memory_type_id,
.allocationSize = fin_block_capacity}, NULL, &cur_block->el.mem_hand);
if (self->mem_properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
vkMapMemory(self->device, cur_block->el.mem_hand, 0, VK_WHOLE_SIZE, 0, &cur_block->el.mapped_memory);
}
self->total_capacity = updated_total_capacity;
MargaretMemAllocatorRequests_sink(requests);
assert(self->old_moved_buffers.len == 0);

View File

@ -399,8 +399,8 @@ OptionVkExtent2D margaret_choose_image_extent(const VkSurfaceCapabilitiesKHR* ca
if (capabilities->minImageExtent.width > sane_limits.width ||
capabilities->minImageExtent.height > sane_limits.height)
return None_VkExtent2D();
return Some_VkExtent2D((VkExtent2D) { MIN_U32(sane_limits.width, sane_limits.width),
MIN_U32(sane_limits.height, sane_limits.height) });
return Some_VkExtent2D((VkExtent2D) { MIN_U32(sane_limits.width, capabilities->maxImageExtent.width),
MIN_U32(sane_limits.height, capabilities->maxImageExtent.height) });
}
/* May be bigger, than a sane limit */
return Some_VkExtent2D(capabilities->currentExtent);
@ -735,6 +735,7 @@ VecVkFramebuffer margaret_create_swapchain_framebuffers(
VecVkFramebuffer swapchain_framebuffers = VecVkFramebuffer_new_zeroinit(swapchain_image_views->len);
for (uint32_t i = 0; i < swapchain_image_views->len; i++) {
VkImageView attachments[1] = {*VecVkImageView_at(swapchain_image_views, i)};
printf("CREATING FRAMEBUFFER: %u %u\n", image_extent.width, image_extent.height);
VkFramebufferCreateInfo framebuffer_crinfo = {
.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
.renderPass = render_pass,

View File

@ -395,13 +395,13 @@ PipelineHands create_graphics_pipeline_0_b(
{
.location = 6,
.binding = 1,
.format = VK_FORMAT_R32G32B32A32_SFLOAT,
.format = VK_FORMAT_R32G32B32_SFLOAT,
.offset = offsetof(ShinyMeshInstance, color_off)
},
{
.location = 7,
.binding = 1,
.format = VK_FORMAT_R32G32B32A32_SFLOAT,
.format = VK_FORMAT_R32G32B32_SFLOAT,
.offset = offsetof(ShinyMeshInstance, color_on)
},
};
@ -940,27 +940,31 @@ void record_copying_entire_scene_from_staging_to_device_local(VkCommandBuffer co
const GenericModelOnSceneMem* model = VecGenericModelOnSceneMem_at(&scene->generic_models, mi);
assert(model->instance_vec_len <= model->instance_vec_capacity);
assert(model->instance_vec_capacity * sizeof(GenericMeshInstance) == model->instance_attr_buf->value.me.buf.capacity);
vkCmdCopyBuffer(command_buffer,
model->staging_instance_attr_buf->value.me.buf.buffer, model->instance_attr_buf->value.me.buf.buffer,
1, &(VkBufferCopy){.srcOffset = 0, .dstOffset = 0,
.size = model->instance_vec_len * sizeof(GenericMeshInstance)});
if (model->instance_vec_len) {
vkCmdCopyBuffer(command_buffer,
model->staging_instance_attr_buf->value.me.buf.buffer, model->instance_attr_buf->value.me.buf.buffer,
1, &(VkBufferCopy){.srcOffset = 0, .dstOffset = 0,
.size = model->instance_vec_len * sizeof(GenericMeshInstance)});
}
}
for (size_t mi = 0; mi < scene->shiny_models.len; mi++) {
const ShinyModelOnSceneMem* model = VecShinyModelOnSceneMem_at(&scene->shiny_models, mi);
assert(model->instance_vec_len <= model->instance_vec_capacity);
assert(model->instance_vec_capacity * sizeof(ShinyMeshInstance) == model->instance_attr_buf->value.me.buf.capacity);
vkCmdCopyBuffer(command_buffer,
model->staging_instance_attr_buf->value.me.buf.buffer, model->instance_attr_buf->value.me.buf.buffer,
1, &(VkBufferCopy){.srcOffset = 0, .dstOffset = 0,
.size = model->instance_vec_len * sizeof(ShinyMeshInstance)});
if (model->instance_vec_len) {
vkCmdCopyBuffer(command_buffer,
model->staging_instance_attr_buf->value.me.buf.buffer, model->instance_attr_buf->value.me.buf.buffer,
1, &(VkBufferCopy){.srcOffset = 0, .dstOffset = 0,
.size = model->instance_vec_len * sizeof(ShinyMeshInstance)});
}
}
Pipeline0UBO* pipeline_0_ubo = (Pipeline0UBO*)MargaretMAIterator_get_mapped(scene->pipeline0_ubo);
Pipeline0UBO* pipeline_0_ubo = (Pipeline0UBO*)MargaretMAIterator_get_mapped(scene->pipeline0_staging_ubo);
assert(pipeline_0_ubo->point_light_count <= pipeline_0_ubo_point_light_max_count);
assert(pipeline_0_ubo->spotlight_count <= pipeline_0_ubo_spotlight_max_count);
VkBufferCopy regions_to_copy[4] = {
{
.srcOffset = offsetof(Pipeline0UBO, point_light_arr), .dstOffset = offsetof(Pipeline0UBO, point_light_arr),
.srcOffset = offsetof(Pipeline0UBO, point_light_count), .dstOffset = offsetof(Pipeline0UBO, point_light_count),
.size = sizeof(int)
},
{
@ -1074,7 +1078,7 @@ typedef struct {
void recreate_swapchain(state_r0 *state) {
// We are stopping program and rebuilding our sem+sem+fence synchronization mechanism
vkDeviceWaitIdle(state->vk.device);
vkDeviceWaitIdle(state->vk.device); // todo: AAAAAAAAAAAAAAA this is all so so wrong AAAAAAAA
Jane_r0_destroy(state->vk.device, state->vk.jane);
state->vk.jane = Jane_r0_create(state->vk.device);
VkSwapchainKHR old_swapchain = MargaretSwapchainBundle_pop_swapchain_drop_rest(
@ -1115,7 +1119,7 @@ void update_state(state_r0* state, uint32_t dur) {
{
GenericModelOnSceneMem* model = VecGenericModelOnSceneMem_mat(&state->vk.scene.generic_models, 0);
assert(model->instance_vec_len >= 1);
GenericMeshInstance* instances = (GenericMeshInstance*)MargaretMAIterator_get_mapped(model->instance_attr_buf);
GenericMeshInstance* instances = (GenericMeshInstance*)MargaretMAIterator_get_mapped(model->staging_instance_attr_buf);
if (state->first_0x80_keys[XKB_KEY_j]) {
state->vk.scene.funny_vector.x -= fl;
instances[0].model_t = marie_translation_mat4(state->vk.scene.funny_vector);
@ -1447,7 +1451,7 @@ void copying_buffer_to_image_color_aspect_record_cmd_buf (
},
};
vkCmdCopyBufferToImage(command_buffer, pair.staging_buffer->value.me.buf.buffer,
pair.staging_buffer->value.me.img.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
pair.image->value.me.img.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
}
/* filling buffers Vec again */
@ -1582,10 +1586,14 @@ static void main_h_wl_keyboard_key(
if (keysym == XKB_KEY_1) {
vec3 p = state->vk.scene.cam.pos;
ShinyModelOnSceneMem* model = VecShinyModelOnSceneMem_mat(&state->vk.scene.shiny_models, 0);
assert(model->instance_vec_len == 1);
ShinyMeshInstance* instances = (ShinyMeshInstance*)MargaretMAIterator_get_mapped(model->instance_attr_buf);
assert(model->instance_vec_len >= 1);
ShinyMeshInstance* instances = (ShinyMeshInstance*)MargaretMAIterator_get_mapped(model->staging_instance_attr_buf);
instances[0].model_t = marie_translation_mat4(p);
state->vk.dt_transfer_required = true;
Pipeline0UBO* ubo = (Pipeline0UBO*)MargaretMAIterator_get_mapped(state->vk.scene.pipeline0_staging_ubo);
assert(ubo->point_light_count >= 1);
ubo->point_light_arr[0].pos = p;
printf("Point light source pos set to %f %f %f\n", p.x, p.y, p.z);
state->vk.dt_transfer_required = true;
} else if (keysym == XKB_KEY_2) {
@ -1771,10 +1779,9 @@ int main() {
bool ENABLE_VALIDATION_LAYERS = true;
const U32 MAX_WIN_WIDTH = 1920;
const U32 MAX_WIN_HEIGHT = 1080;
VkExtent2D sane_image_extent_limit = {MAX_WIN_WIDTH, MAX_WIN_HEIGHT};
state_r0 state = {0};
state.sane_image_extent_limit = (VkExtent2D){1000, 700};
state.sane_image_extent_limit = (VkExtent2D){MAX_WIN_WIDTH, MAX_WIN_HEIGHT};
vulkan_ctx_r0 *vk = &state.vk;
state.wl_display = wl_display_connect(NULL);
@ -1822,7 +1829,7 @@ int main() {
vk->surface = margaret_create_surface(instance, state.wl_display, state.wl_surface);
vk->physical_device = margaret_select_one_physical_device(
instance, vk->surface, GPU, bugged_GPU, sane_image_extent_limit);
instance, vk->surface, GPU, bugged_GPU, state.sane_image_extent_limit);
{ /* Funny vibe check */
VecU8 txt = margaret_stringify_device_memory_properties_2(vk->physical_device);
@ -1843,7 +1850,7 @@ int main() {
vkGetDeviceQueue(vk->device, vk->queue_fam.for_presentation, 0, &vk->queues.presentation_queue);
ResultMargaretChosenSwapchainDetailsOrSpanU8 swapchain_details_res = margaret_choose_swapchain_details(
vk->physical_device, vk->surface, sane_image_extent_limit);
vk->physical_device, vk->surface, state.sane_image_extent_limit);
if (swapchain_details_res.variant != Result_Ok)
abortf("swapchain_details_res.variant != Result_Ok");
@ -1879,21 +1886,33 @@ int main() {
vkGetPhysicalDeviceMemoryProperties(vk->physical_device, &mem_properties);
const VkMemoryPropertyFlags host_visible_coherent = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
const VkMemoryPropertyFlags device_local = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
assert(mem_properties.memoryTypeCount < 32);
int ham_sandwich = 0;
for (U32 i = 0; i < mem_properties.memoryTypeCount; i++) {
int mem_type_id_host_visible_coherent = -1;
for (int i = 0; i < (int)mem_properties.memoryTypeCount; i++) {
if ((mem_properties.memoryTypes[i].propertyFlags & host_visible_coherent) == host_visible_coherent) {
// todo: replace ths MargaretMemAllocator with MargaretBufferAllocator
vk->host_visible_coherent_mem = MargaretMemAllocator_new(vk->device, vk->physical_device,
vk->host_visible_mem_mv_command_buf, host_visible_coherent, i);
}
if ((mem_properties.memoryTypes[i].propertyFlags & device_local) == device_local) {
vk->device_local_mem = MargaretMemAllocator_new(vk->device, vk->physical_device,
vk->device_local_mem_mv_command_buf, host_visible_coherent, i);
mem_type_id_host_visible_coherent = i;
break;
}
}
if (ham_sandwich != 2)
abortf("Can't find memory types\n");
if (mem_type_id_host_visible_coherent == -1) {
abortf("Can't find host visible + host coherent memory\n");
}
int mem_type_id_device_local = -1;
for (int i = 0; i < (int)mem_properties.memoryTypeCount; i++) {
if ((mem_properties.memoryTypes[i].propertyFlags & device_local) == device_local) {
mem_type_id_device_local = i;
break;
}
}
if (mem_type_id_device_local == -1) {
abortf("Can't find device local memory\n");
}
// todo: replace staging MMA with MBA
vk->host_visible_coherent_mem = MargaretMemAllocator_new(vk->device, vk->physical_device,
vk->host_visible_mem_mv_command_buf, host_visible_coherent, mem_type_id_host_visible_coherent);
vk->device_local_mem = MargaretMemAllocator_new(vk->device, vk->physical_device,
vk->device_local_mem_mv_command_buf, device_local, mem_type_id_device_local);
vk->jane = Jane_r0_create(vk->device);
/* Luckily, swapchain image allocation is not managed by me */
@ -1919,12 +1938,6 @@ int main() {
// .topology = generate_shiny_cube(0.5f), .max_instance_count = 5
// });
// todo: continue from here
// vk->my_cam_control_info = CamControlInfo_new();
// vk->Buba_control_info = (vec3){0};
MargaretMemAllocatorRequests initial_req_for_staging = MargaretMemAllocatorRequests_new();
MargaretMemAllocatorRequests initial_req_for_device_local = MargaretMemAllocatorRequests_new();
@ -1977,8 +1990,8 @@ int main() {
VK_FORMAT_R8G8B8A8_UNORM,
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_IMAGE_LAYOUT_UNDEFINED, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT, true);
mem.normal_texture = MargaretMemAllocatorRequests_alloc_image(&initial_req_for_device_local,
mem.pixels_normal.width, mem.pixels_normal.height,
mem.specular_texture = MargaretMemAllocatorRequests_alloc_image(&initial_req_for_device_local,
mem.pixels_specular.width, mem.pixels_specular.height,
VK_FORMAT_R8_UNORM,
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_IMAGE_LAYOUT_UNDEFINED, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT, true);
@ -2021,7 +2034,7 @@ int main() {
vk->scene = Scene_new(generic_model_mem, shiny_model_mem, pipeline0_staging_ubo, pipeline0_ubo);
vk->device_IT1_image = MargaretMemAllocatorRequests_alloc_image(&initial_req_for_device_local,
MAX_WIN_WIDTH, MAX_WIN_HEIGHT, vk->IT1_format, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
MAX_WIN_WIDTH, MAX_WIN_HEIGHT, vk->IT1_format, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
/* We do layout transitions in renderpass (the easy way) + we don't copy this image */
VK_IMAGE_LAYOUT_UNDEFINED, 0, 0, false);
vk->device_zbuffer_image = MargaretMemAllocatorRequests_alloc_image(&initial_req_for_device_local,
@ -2044,7 +2057,7 @@ int main() {
.model_t = marie_translation_mat4((vec3){11.f * (float)X, -6, 4.f * (float)Z}) };
}
}
model_g->instance_vec_len = 0;
model_g->instance_vec_len = 100;
ShinyModelOnSceneMem* model_sh = VecShinyModelOnSceneMem_mat(&vk->scene.shiny_models, 0);
ShinyMeshInstance* sh_instances = (ShinyMeshInstance*)MargaretMAIterator_get_mapped(model_sh->staging_instance_attr_buf);
assert(model_sh->instance_vec_capacity == 100);
@ -2055,7 +2068,7 @@ int main() {
.color_on = {0, 1, 0}, .color_off = {1, 0.4f, 0.5f} };
}
}
model_sh->instance_vec_len = 0;
model_sh->instance_vec_len = 100;
Pipeline0UBO* ubo = (Pipeline0UBO*)MargaretMAIterator_get_mapped(vk->scene.pipeline0_staging_ubo);
assert(pipeline_0_ubo_point_light_max_count >= 100);
@ -2065,10 +2078,11 @@ int main() {
for (int Z = 0; Z < 10; Z++) {
ubo->point_light_arr[X * 10 + Z] = (Pipeline0PointLight){
.pos = (vec3){11.f * (float)X, 10, 4.f * (float)Z},
.color = {0, 1, 0}
.color = {5, 5, 5}
};
}
}
ubo->point_light_arr[0].color = (vec3){100, 100, 100};
// todo: synchronize them with my cool light sources)
}
@ -2102,8 +2116,8 @@ int main() {
margaret_end_command_buffer(vk->transfer_command_buf);
check(vkQueueSubmit(vk->queues.graphics_queue, 1, &(VkSubmitInfo){
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, .pCommandBuffers = &vk->transfer_command_buf,
}, NULL) == VK_SUCCESS);
check(vkWaitForFences(vk->device, 1, &vk->jane.roxy, VK_TRUE, UINT64_MAX));
}, vk->jane.roxy) == VK_SUCCESS);
check(vkWaitForFences(vk->device, 1, &vk->jane.roxy, VK_TRUE, UINT64_MAX) == VK_SUCCESS);
recreate_vulkan_references_objects(&state);
@ -2112,10 +2126,14 @@ int main() {
/* Will happen mid-frame */
vk->dt_transfer_required = true;
printf("ENTERING WAAYLAND MAINLOOP\n");
while (wl_display_dispatch(state.wl_display) >= 0) {
if (state.closed)
break;
}
printf("CLOSING\n");
vkDeviceWaitIdle(vk->device);
// The End

View File

@ -124,8 +124,8 @@ void SceneTemplate_drop(SceneTemplate self) {
VecGenericMeshInSceneTemplate_drop(self.generic_models);
}
#define pipeline_0_ubo_point_light_max_count 20
#define pipeline_0_ubo_spotlight_max_count 120
#define pipeline_0_ubo_point_light_max_count 120
#define pipeline_0_ubo_spotlight_max_count 20
typedef struct {
int point_light_count;

View File

@ -192,8 +192,6 @@ void SceneTemplate_copy_initial_model_topology_cmd_buf_recording(
vkCmdCopyBuffer(command_buffer, mm->staging_ebo->value.me.buf.buffer, mm->ebo->value.me.buf.buffer,
1, &(VkBufferCopy){.srcOffset = 0, .dstOffset = 0, .size = ebo_len});
}
margaret_end_command_buffer(command_buffer);
}
#endif

View File

@ -14,6 +14,11 @@ layout(push_constant, std430) uniform pc {
layout(offset = 64) vec3 camera_pos;
};
struct Pipeline0PointLight {
vec3 pos;
vec3 color;
};
struct Pipeline0Spotlight {
vec3 pos;
vec3 dir;
@ -21,16 +26,11 @@ struct Pipeline0Spotlight {
float range;
};
struct Pipeline0PointLight {
vec3 pos;
vec3 color;
};
layout(std140, binding = 0) uniform Pipeline0UBO {
int point_light_count;
int spotlight_count;
Pipeline0PointLight point_light_arr[20];
Pipeline0Spotlight spotlight_arr [120];
Pipeline0PointLight point_light_arr[120];
Pipeline0Spotlight spotlight_arr [20];
};
float get_intensity(float dist){
@ -61,5 +61,4 @@ void main(){
float specular_c = texture(specular_map, fsin_tex).x;
vec3 color = natural_color * diffuse_illumination + specular_c * specular_illumination;
fin_color = vec4(color, 1);
// fin_color = vec4(specular_c, 0, 0, 1);
}

View File

@ -11,6 +11,11 @@ layout(push_constant, std430) uniform pc {
layout(offset = 64) vec3 camera_pos;
};
struct Pipeline0PointLight {
vec3 pos;
vec3 color;
};
struct Pipeline0Spotlight {
vec3 pos;
vec3 dir;
@ -18,16 +23,11 @@ struct Pipeline0Spotlight {
float range;
};
struct Pipeline0PointLight {
vec3 pos;
vec3 color;
};
layout(std140, binding = 0) uniform Pipeline0UBO {
int point_light_count;
int spotlight_count;
Pipeline0PointLight point_light_arr[20];
Pipeline0Spotlight spotlight_arr [120];
Pipeline0PointLight point_light_arr[120];
Pipeline0Spotlight spotlight_arr [20];
};
float get_intensity(float dist){