@@ -70,25 +70,39 @@ pub fn allocateCommandBuffers(self: *Self, info: *const vk.CommandBufferAllocate
7070
7171pub fn freeCommandBuffers (self : * Self , cmds : []* Dispatchable (CommandBuffer )) VkError ! void {
7272 // Ugly method but it works well
73+ var len : usize = 0 ;
7374 for (cmds ) | cmd | {
7475 if (std .mem .indexOf (* Dispatchable (CommandBuffer ), self .buffers .items , &[_ ]* Dispatchable (CommandBuffer ){cmd })) | i | {
7576 const save = self .buffers .orderedRemove (i );
7677 // Append the now free command buffer at the end of the pool
7778 self .buffers .appendAssumeCapacity (save );
79+ len += 1 ;
7880 }
7981 }
80- self .first_free_buffer_index -= cmds .len ;
82+ const new_first_free_buffer_index , const has_overflown = @subWithOverflow (self .first_free_buffer_index , len );
83+ if (has_overflown == 0 ) {
84+ self .first_free_buffer_index = new_first_free_buffer_index ;
85+ } else {
86+ std .log .scoped (.CommandPool ).warn ("Avoided an underflow. This should not happen, please fill an issue." , .{});
87+ }
8188}
8289
8390pub fn destroy (self : * Self , allocator : std.mem.Allocator ) void {
8491 for (self .buffers .items ) | non_dis_cmd | {
85- non_dis_cmd .object .destroy (allocator );
86- non_dis_cmd .destroy (allocator );
92+ non_dis_cmd .intrusiveDestroy (allocator );
8793 }
8894 self .buffers .deinit (allocator );
8995 self .vtable .destroy (self , allocator );
9096}
9197
9298pub inline fn reset (self : * Self , flags : vk.CommandPoolResetFlags ) VkError ! void {
9399 try self .vtable .reset (self , flags );
100+ if (flags .release_resources_bit ) {
101+ const allocator = self .host_allocator .allocator ();
102+ for (self .buffers .items ) | non_dis_cmd | {
103+ non_dis_cmd .intrusiveDestroy (allocator );
104+ }
105+ self .buffers .shrinkAndFree (allocator , BUFFER_POOL_BASE_CAPACITY );
106+ self .buffers .clearRetainingCapacity ();
107+ }
94108}
0 commit comments