-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescriptor_set_allocator.odin
205 lines (163 loc) · 6.8 KB
/
descriptor_set_allocator.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package justvk
import "core:fmt"
import vk "vendor:vulkan"
//
// Basically a block allocator but for descriptor sets of a certain
// layout instead of memory.
//
// #Sync #Race
// Problem for multithreading. It might be the case that we can disallow
// this to be shared over threads but since the initial idea is to use
// this per shader program that seems like quite the limitation. But it
// might be fine.
// #Speed #Tweak #Limitation
DESCRIPTOR_SETS_PER_POOL :: 4
Descriptor_Set_Handle :: int
Descriptor_Set_Block :: struct {
set : vk.DescriptorSet,
allocated : bool,
next_free : Maybe(int),
busy_fence : vk.Fence,
}
Descriptor_Set_Allocator :: struct {
dc : ^Device_Context,
layout : vk.DescriptorSetLayout,
num_buffers : int,
num_samplers : int,
num_storage_buffers : int,
// #Memory #Fragmentation #Speed
pools : [dynamic]vk.DescriptorPool,
set_blocks : [dynamic]Descriptor_Set_Block,
busy_blocks : [dynamic]Descriptor_Set_Handle,
first_free : Descriptor_Set_Handle,
}
make_descriptor_pool :: proc(alloc : ^Descriptor_Set_Allocator) -> vk.DescriptorPool {
using alloc.dc;
buffers_size : vk.DescriptorPoolSize;
buffers_size.descriptorCount = cast(u32)alloc.num_buffers * DESCRIPTOR_SETS_PER_POOL;
buffers_size.type = .UNIFORM_BUFFER;
samplers_size : vk.DescriptorPoolSize;
samplers_size.descriptorCount = cast(u32)alloc.num_samplers * DESCRIPTOR_SETS_PER_POOL;
samplers_size.type = .COMBINED_IMAGE_SAMPLER;
storage_buffers_size : vk.DescriptorPoolSize;
storage_buffers_size.descriptorCount = cast(u32)alloc.num_storage_buffers * DESCRIPTOR_SETS_PER_POOL;
storage_buffers_size.type = .STORAGE_BUFFER;
sizes : [3]vk.DescriptorPoolSize;
next : int;
if alloc.num_buffers > 0 {
sizes[next] = buffers_size;
next += 1;
}
if alloc.num_samplers > 0 {
sizes[next] = samplers_size;
next += 1;
}
if alloc.num_storage_buffers > 0 {
sizes[next] = storage_buffers_size;
next += 1;
}
the_pool : vk.DescriptorPool;
pool_create : vk.DescriptorPoolCreateInfo;
pool_create.sType = .DESCRIPTOR_POOL_CREATE_INFO;
pool_create.maxSets = DESCRIPTOR_SETS_PER_POOL;
pool_create.pPoolSizes = slice_to_multi_ptr(sizes[:next]);
pool_create.poolSizeCount = cast(u32)next;
if vk.CreateDescriptorPool(vk_device, &pool_create, nil, &the_pool) != .SUCCESS {
panic("Failed creating descriptor pool");
}
return the_pool;
}
init_descriptor_set_allocator :: proc(alloc : ^Descriptor_Set_Allocator, set_layout : vk.DescriptorSetLayout, layout : Glsl_Layout, using dc : ^Device_Context) {
alloc.dc = dc;
alloc.layout = set_layout;
alloc.num_buffers = layout.num_ubos;
alloc.num_samplers = layout.num_samplers;
alloc.num_storage_buffers = layout.num_sbos;
alloc.pools = make([dynamic]vk.DescriptorPool);
alloc.set_blocks = make([dynamic]Descriptor_Set_Block);
alloc.busy_blocks = make([dynamic]Descriptor_Set_Handle);
append(&alloc.pools, make_descriptor_pool(alloc));
resize(&alloc.set_blocks, DESCRIPTOR_SETS_PER_POOL);
alloc.first_free = 0;
}
destroy_descriptor_set_allocator :: proc(alloc : Descriptor_Set_Allocator) {
using alloc.dc;
for pool in alloc.pools {
vk.DestroyDescriptorPool(vk_device, pool, nil)
}
delete(alloc.pools);
delete(alloc.set_blocks);
delete(alloc.busy_blocks);
}
allocate_descriptor_set :: proc(alloc : ^Descriptor_Set_Allocator) -> (Descriptor_Set_Handle, vk.DescriptorSet) {
using alloc.dc;
// #Speed #Sync #Try
// This might be slow but it might be trivial. Needs testing.
// An alternative could be to dispatch fence waiting on as jobs
// but that would require syncing which may very well make things
// slower than this..
// I have no idea how much of a slowdown vkGetFenceStatus is
check_busy_blocks(alloc); // !!This may change first_free!!
consumed := &alloc.set_blocks[alloc.first_free];
handle := alloc.first_free;
if consumed.next_free != nil {
alloc.first_free = consumed.next_free.(int);
} else {
alloc.first_free += 1;
}
if alloc.first_free >= len(alloc.set_blocks) {
resize(&alloc.set_blocks, len(alloc.set_blocks) + DESCRIPTOR_SETS_PER_POOL);
append(&alloc.pools, make_descriptor_pool(alloc));
}
// Update adress, array might have resized
consumed = &alloc.set_blocks[handle];
assert(!consumed.allocated);
consumed.allocated = true;
if consumed.set == 0 {
alloc_info : vk.DescriptorSetAllocateInfo;
alloc_info.sType = .DESCRIPTOR_SET_ALLOCATE_INFO;
alloc_info.descriptorPool = alloc.pools[handle / DESCRIPTOR_SETS_PER_POOL];
alloc_info.descriptorSetCount = 1;
alloc_info.pSetLayouts = &alloc.layout;
check_vk_result(vk.AllocateDescriptorSets(vk_device, &alloc_info, &consumed.set));
}
return handle, consumed.set;
}
release_descriptor_set :: proc(alloc : ^Descriptor_Set_Allocator, handle : Descriptor_Set_Handle, busy_fence : vk.Fence = 0) {
assert(check_descriptor_set_handle(alloc, handle), "Invalid descriptor set handle");
assert(!alloc.set_blocks[alloc.first_free].allocated);
if busy_fence == 0 {
alloc.set_blocks[handle].allocated = false;
alloc.set_blocks[handle].next_free = alloc.first_free;
alloc.first_free = handle;
assert(!alloc.set_blocks[alloc.first_free].allocated);
} else {
alloc.set_blocks[handle].busy_fence = busy_fence;
append(&alloc.busy_blocks, handle);
}
}
check_busy_blocks :: proc(alloc : ^Descriptor_Set_Allocator) {
using alloc.dc;
for i := len(alloc.busy_blocks)-1; i >= 0; i -= 1 {
handle := alloc.busy_blocks[i];
block := &alloc.set_blocks[handle];
assert(block.busy_fence != 0, "Descriptor set block was marked as busy but no valid fence was set");
fence_status := vk.GetFenceStatus(vk_device, block.busy_fence);
if fence_status == .SUCCESS { // Signaled, ready
unordered_remove(&alloc.busy_blocks, i);
release_descriptor_set(alloc, handle);
continue;
} else if fence_status == .NOT_READY { // Unsignaled, not ready
continue;
} else {
panic(fmt.tprint("GetFenceStatus failed", fence_status));
}
}
}
get_descriptor_set :: proc(alloc : ^Descriptor_Set_Allocator, handle : Descriptor_Set_Handle) -> vk.DescriptorSet {
assert(check_descriptor_set_handle(alloc, handle), "Invalid descriptor set handle");
return alloc.set_blocks[handle].set;
}
check_descriptor_set_handle :: proc(alloc : ^Descriptor_Set_Allocator, handle : Descriptor_Set_Handle) -> bool {
return handle >= 0 && handle < len(alloc.set_blocks) && alloc.set_blocks[handle].set != 0 && alloc.set_blocks[handle].allocated;
}