fixed dumb MargaretImgAllocator bug

This commit is contained in:
Андреев Григорий 2026-01-01 02:09:03 +03:00
parent 238a3653e8
commit dd2d3c6575
4 changed files with 51 additions and 26 deletions

View File

@ -13,12 +13,4 @@ typedef struct{
U32 len;
} U32Segment;
U64 U64Segment_get_length_resp_alignment(U64Segment self, U8 alignment_exp) {
if (self.start & ((1ull << alignment_exp) - 1)) {
U64 pad_left = (1ull << alignment_exp) - (self.start & ((1ull << alignment_exp) - 1));
return self.len >= pad_left ? self.len - pad_left : 0;
}
return self.len;
}
#endif

View File

@ -1965,12 +1965,10 @@ Alice* Alice_new(){
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
/* | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT */,
mem_type_id_device_local, 6, false, 16);
mem_type_id_device_local, 6, false, 10000);
alice->dev_local_images = MargaretImgAllocator_new(alice->device, alice->physical_device,
mem_type_id_device_local, 16);
// todo: aAAAAND. We have some bug related to allocating stuff on block. This is very bad
// todo: hope will fix this night. Because this is VERY BAD
mem_type_id_device_local, 16000000);
alice->jane = Jane_alice_create(alice->device);
/* Luckily, swapchain image allocation is not managed by me */

View File

@ -172,12 +172,6 @@
* const VkAllocationCallbacks* pAllocator)
*/
// todo: get rid of this whole VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT crap. MargaretMA is for non-host-visible
// todo: for staging buffers you better use MargaretBufferAllocator. Ou, yeah, I have yet to write them
// todo: chucking rewrite all of this. Yes, I want all of this crrp rewritten
#include "../../l1/core/util.h"
#include "../../l1_5/core/buff_rb_tree_node.h"
#include "../../../gen/l1_5/BufRBTree_MapU64ToU64.h"
@ -189,10 +183,18 @@ typedef struct{
} MargaretIAFreeSegment;
#include "../../l1/core/uint_segments.h"
// todo: substitute U64Segment_get_length_resp_alignment by my own function
U64 margaret_get_length_resp_alignment(U64 start, U64 full_len, U8 alignment_exp) {
if (start & ((1ull << alignment_exp) - 1)) {
U64 pad_left = (1ull << alignment_exp) - (start & ((1ull << alignment_exp) - 1));
return full_len >= pad_left ? full_len - pad_left : 0;
}
return full_len;
}
bool MargaretIAFreeSegment_less_resp_align(const MargaretIAFreeSegment* A, const MargaretIAFreeSegment* B, U8 alignment_exp){
U64 A_len = U64Segment_get_length_resp_alignment((U64Segment){A->start, A->len}, alignment_exp);
U64 B_len = U64Segment_get_length_resp_alignment((U64Segment){B->start, B->len}, alignment_exp);
U64 A_len = margaret_get_length_resp_alignment(A->start, A->len, alignment_exp);
U64 B_len = margaret_get_length_resp_alignment(B->start, B->len, alignment_exp);
if (A_len == B_len) {
if (A->block == B->block) {
return A->start < B->start;
@ -291,6 +293,7 @@ OptionMargaretIAFreeSegment MargaretMemFreeSpaceManager_search(
if (man->free_space_in_memory[alignment_exp].variant == Option_None) {
assert(man->set_present.len > 0);
assert(man->free_space_in_memory[man->set_present.buf[0]].variant == Option_Some);
assert(man->free_space_in_memory[alignment_exp].variant == Option_None);
BufRBTreeByLenRespAlign_SetMargaretIAFreeSegment* have = &man->free_space_in_memory[man->set_present.buf[0]].some;
man->free_space_in_memory[alignment_exp] = Some_BufRBTreeByLenRespAlign_SetMargaretIAFreeSegment(
BufRBTreeByLenRespAlign_SetMargaretIAFreeSegment_new_reserved(alignment_exp, have->el.len));
@ -298,6 +301,7 @@ OptionMargaretIAFreeSegment MargaretMemFreeSpaceManager_search(
BufRBTreeByLenRespAlign_SetMargaretIAFreeSegment_insert(
&man->free_space_in_memory[alignment_exp].some, *VecMargaretIAFreeSegment_at(&have->el, i));
}
VecU8_append(&man->set_present, alignment_exp);
}
assert(man->free_space_in_memory[alignment_exp].variant == Option_Some);
U64 sit = BufRBTreeByLenRespAlign_SetMargaretIAFreeSegment_find_min_grtr_or_eq(
@ -341,11 +345,33 @@ void MargaretImgAllocator__add_block(MargaretImgAllocator* self, U64 capacity){
VecMargaretImgAllocatorOneBlock_append(&self->blocks, (MargaretImgAllocatorOneBlock){
.images = BufRBTree_MapU64ToU64_new_reserved(1),
.capacity = capacity,
.occupation_counter = capacity,
.occupation_counter = capacity, // sounds sus
.mem_hand = memory,
.mapped_memory = NULL /* not supported */});
}
/* Idk where to put it */
void MargaretImgAllocator__debug(const MargaretImgAllocator* self){
printf("=============================== MargaretImgAllocator ============\n"
"All blocks: { ");
for (size_t i = 0; i < self->blocks.len; i++) {
printf(" %lu/%lu ", self->blocks.buf[i].occupation_counter, self->blocks.buf[i].capacity);
}
printf("}\n");
for (size_t ai = 0; ai < self->mem_free_space.set_present.len; ai++) {
U8 alignment_exp = self->mem_free_space.set_present.buf[ai];
printf("Free spaces at alignment_exp = %d:\n", (int)alignment_exp);
assert(self->mem_free_space.free_space_in_memory[alignment_exp].variant == Option_Some);
const BufRBTreeByLenRespAlign_SetMargaretIAFreeSegment* set =
&self->mem_free_space.free_space_in_memory[alignment_exp].some;
assert(set->guest == alignment_exp);
for (size_t i = 0; i < set->el.len; i++) {
const MargaretIAFreeSegment *free_seg = &set->el.buf[i];
printf(" Block %lu, start %lu, len %lu\n", free_seg->block, free_seg->start, free_seg->len);
}
}
}
MargaretImgAllocator MargaretImgAllocator_new(
VkDevice device, VkPhysicalDevice physical_device, U8 memory_type_id, U64 initial_block_size
){
@ -358,6 +384,7 @@ MargaretImgAllocator MargaretImgAllocator_new(
};
MargaretImgAllocator__add_block(&self, initial_block_size);
MargaretImgAllocator__insert_gap(&self, 0, 0, initial_block_size);
// MargaretImgAllocator__debug(&self);
return self;
}
@ -487,13 +514,11 @@ NODISCARD MargaretImgAllocation MargaretImgAllocator__alloc(
MargaretMemFreeSpaceManager_search(&self->mem_free_space, alignment_exp, mem_requirements.size);
if (free_gap.variant == Option_None) {
// todo: there is clearly a bug that prevents free segments from appearing. I have to find it later
assert(self->blocks.len > 0);
U64 pitch = self->blocks.buf[self->blocks.len - 1].capacity;
// Old blocks remain intact
// todo: return back when done. Doing dumb things. I have a bug that prevents things from making sense
// U64 new_capacity = MAX_U64(mem_requirements.size, MIN_U64(2 * pitch, maintenance3_properties.maxMemoryAllocationSize));
U64 new_capacity = MAX_U64(mem_requirements.size, MIN_U64(pitch, maintenance3_properties.maxMemoryAllocationSize));
U64 new_capacity = MAX_U64(mem_requirements.size, MIN_U64(2 * pitch, maintenance3_properties.maxMemoryAllocationSize));
// U64 new_capacity = MAX_U64(mem_requirements.size, MIN_U64(pitch, maintenance3_properties.maxMemoryAllocationSize));
MargaretImgAllocator__add_block(self, new_capacity);
U64 bid = self->blocks.len - 1;
MargaretImgAllocator__insert_gap(self, bid, mem_requirements.size, new_capacity - mem_requirements.size);
@ -502,11 +527,13 @@ NODISCARD MargaretImgAllocation MargaretImgAllocator__alloc(
bool iret = BufRBTree_MapU64ToU64_insert(&block->images, 0, mem_requirements.size);
assert(iret);
check(vkBindImageMemory(self->device, fresh_img, block->mem_hand, 0) == VK_SUCCESS);
// MargaretImgAllocator__debug(self);
return (MargaretImgAllocation){.block = bid, fresh_img, 0};
}
U64 aligned_pos = MargaretImgAllocator__add_img_given_gap(self, free_gap.some, mem_requirements.size, alignment_exp);
VkDeviceMemory memory = VecMargaretImgAllocatorOneBlock_at(&self->blocks, free_gap.some.block)->mem_hand;
check(vkBindImageMemory(self->device, fresh_img, memory, aligned_pos) == VK_SUCCESS);
// MargaretImgAllocator__debug(self);
return (MargaretImgAllocation){.block = free_gap.some.block, .image = fresh_img, .start = aligned_pos};
}

View File

@ -66,11 +66,19 @@ main = do
(mat4Transit (Vec3 x puckLevitationHeight z )))
heroPos <- newIORef (Vec2 0 0)
puckRotation <- newIORef (0 :: Float)
-- Create the Callbacks structure.
let callbacks = Callbacks myonKeyboardKey myonAnotherFrame where
myonKeyboardKey keysym keyAction = do
putStrLn ("Got a keypress")
myonAnotherFrame fl = do
curPuckRotation <- readIORef puckRotation
writeIORef puckRotation (curPuckRotation + fl * 0.2)
--forM_ (zip[0..] puckSpots) $ \(i, (Vec2 x z)) -> aliceGenericMeshSetInst puck (fromIntegral i) (AliceGenericMeshInstance
-- (rot4) ^*^ (mat4Transit (Vec3 x puckLevitationHeight z )))
backDir <- aliceGetCamBack alice
let projBack = projectDirOntoPlane backDir
rightDir <- aliceGetCamRight alice