-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiGraphicsDevice_Metal.h
More file actions
521 lines (471 loc) · 22.2 KB
/
Copy pathwiGraphicsDevice_Metal.h
File metadata and controls
521 lines (471 loc) · 22.2 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#pragma once
#ifdef __APPLE__
#include "CommonInclude.h"
#include "wiPlatform.h"
#include "wiAllocator.h"
#include "wiGraphicsDevice.h"
#include "wiUnorderedMap.h"
#include <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>
#define IR_RUNTIME_METALCPP
#include <metal_irconverter_runtime/metal_irconverter_runtime.h>
#include <mutex>
#include <deque>
// There are crashes with this in graphics debugger, so this can be disabled:
//#define USE_TEXTURE_VIEW_POOL
namespace wi::graphics
{
class GraphicsDevice_Metal final : public GraphicsDevice
{
public:
struct RootLayout
{
uint32_t constants[PUSH_CONSTANT_COUNT];
MTL::GPUAddress root_cbvs[ROOT_CBV_COUNT];
MTL::GPUAddress resource_table_ptr;
MTL::GPUAddress sampler_table_ptr;
};
struct ResourceTable
{
IRDescriptorTableEntry cbvs[arraysize(DescriptorBindingTable::CBV) - arraysize(RootLayout::root_cbvs)];
IRDescriptorTableEntry srvs[arraysize(DescriptorBindingTable::SRV)];
IRDescriptorTableEntry uavs[arraysize(DescriptorBindingTable::UAV)];
};
NS::SharedPtr<MTL::SamplerState> static_samplers[STATIC_SAMPLER_COUNT];
struct StaticSamplerDescriptors
{
IRDescriptorTableEntry samplers[STATIC_SAMPLER_COUNT]; // workaround for static sampler, they are not supported by Metal Shader Converter
} static_sampler_descriptors;
struct SamplerTable
{
IRDescriptorTableEntry samplers[arraysize(DescriptorBindingTable::SAM)];
StaticSamplerDescriptors static_samplers;
};
union ShaderAdditionalData
{
MTL::Size numthreads; // compute, mesh, amplification
struct // vertex
{
uint32_t vertex_output_size_in_bytes;
bool needs_draw_params;
};
uint32_t max_input_primitives_per_mesh_threadgroup; // geometry
};
private:
NS::SharedPtr<MTL::Device> device;
NS::SharedPtr<MTL4::CommandQueue> uploadqueue;
bool textureUlongAtomics = true;
struct Semaphore
{
NS::SharedPtr<MTL::Event> event;
uint64_t fenceValue = 0;
};
struct CommandQueue
{
NS::SharedPtr<MTL4::CommandQueue> queue;
wi::vector<MTL4::CommandBuffer*> submit_cmds;
void signal(const Semaphore& sema)
{
if (queue.get() == nullptr)
return;
queue->signalEvent(sema.event.get(), sema.fenceValue);
}
void wait(const Semaphore& sema)
{
if (queue.get() == nullptr)
return;
queue->wait(sema.event.get(), sema.fenceValue);
}
void submit()
{
if (queue.get() == nullptr)
return;
if (submit_cmds.empty())
return;
queue->commit(submit_cmds.data(), submit_cmds.size());
submit_cmds.clear();
}
} queues[QUEUE_COUNT];
uint64_t frame_fence_values[BUFFERCOUNT] = {};
NS::SharedPtr<MTL::SharedEvent> frame_fence[BUFFERCOUNT][QUEUE_COUNT];
NS::SharedPtr<MTL4::ArgumentTableDescriptor> argument_table_desc;
wi::vector<Semaphore> semaphore_pool;
std::mutex semaphore_pool_locker;
Semaphore new_semaphore()
{
std::scoped_lock lck(semaphore_pool_locker);
if (semaphore_pool.empty())
{
Semaphore& sema = semaphore_pool.emplace_back();
sema.event = NS::TransferPtr(device->newEvent());
}
Semaphore sema = semaphore_pool.back();
semaphore_pool.pop_back();
sema.fenceValue++;
return sema;
}
void free_semaphore(const Semaphore& sema)
{
std::scoped_lock lck(semaphore_pool_locker);
semaphore_pool.push_back(sema);
}
struct JustInTimePSO
{
NS::SharedPtr<MTL::RenderPipelineState> pipeline;
NS::SharedPtr<MTL::DepthStencilState> depth_stencil_state;
};
struct CommandList_Metal
{
NS::SharedPtr<MTL4::CommandAllocator> commandallocators[BUFFERCOUNT];
NS::SharedPtr<MTL4::CommandBuffer> commandbuffer;
NS::SharedPtr<MTL4::ArgumentTable> argument_table;
GPULinearAllocator frame_allocators[BUFFERCOUNT];
RenderPassInfo renderpass_info;
uint32_t id = 0;
QUEUE_TYPE queue = QUEUE_COUNT;
const PipelineState* active_pso = nullptr;
bool dirty_pso = false;
bool dirty_cs = false;
const Shader* active_cs = nullptr;
wi::vector<NS::SharedPtr<CA::MetalDrawable>> presents;
NS::SharedPtr<MTL4::RenderCommandEncoder> render_encoder;
NS::SharedPtr<MTL4::ComputeCommandEncoder> compute_encoder;
MTL::PrimitiveType primitive_type = MTL::PrimitiveTypeTriangle;
IRRuntimePrimitiveType ir_primitive_type = IRRuntimePrimitiveTypeTriangle;
MTL4::BufferRange index_buffer = {};
MTL::IndexType index_type = MTL::IndexTypeUInt32;
wi::vector<std::pair<PipelineHash, JustInTimePSO>> pipelines_worker;
PipelineHash pipeline_hash;
DescriptorBindingTable binding_table;
bool dirty_root = false;
bool dirty_resource = false;
bool dirty_sampler = false;
RootLayout root = {};
uint32_t render_width = 0;
uint32_t render_height = 0;
bool dirty_scissor = false;
uint32_t scissor_count = 0;
MTL::ScissorRect scissors[16] = {};
bool dirty_viewport = false;
uint32_t viewport_count = 0;
MTL::Viewport viewports[16] = {};
wi::vector<Semaphore> waits;
wi::vector<Semaphore> signals;
bool drawargs_required = false;
bool dirty_drawargs = false;
MTL::Size numthreads_as = {};
MTL::Size numthreads_ms = {};
IRGeometryEmulationPipelineDescriptor gs_desc = {};
wi::vector<std::pair<NS::SharedPtr<MTL::Texture>, uint32_t>> texture_clears;
struct VertexBufferBinding
{
MTL::GPUAddress gpu_address = 0;
uint32_t stride = 0;
};
VertexBufferBinding vertex_buffers[16];
bool dirty_vb = false;
wi::vector<GPUBarrier> barriers;
void reset(uint32_t bufferindex)
{
frame_allocators[bufferindex].reset();
renderpass_info = {};
id = 0;
queue = QUEUE_COUNT;
active_pso = nullptr;
active_cs = nullptr;
dirty_pso = false;
dirty_cs = false;
presents.clear();
render_encoder.reset();
compute_encoder.reset();
primitive_type = MTL::PrimitiveTypeTriangle;
ir_primitive_type = IRRuntimePrimitiveTypeTriangle;
index_buffer = {};
index_type = MTL::IndexTypeUInt32;
pipelines_worker.clear();
pipeline_hash = {};
binding_table = {};
dirty_root = true;
dirty_resource = true;
dirty_sampler = true;
root = {};
for (auto& x : vertex_buffers)
{
x = {};
}
dirty_vb = false;
render_width = 0;
render_height = 0;
dirty_viewport = false;
dirty_scissor = false;
scissor_count = 0;
viewport_count = 0;
for (auto& x : scissors)
{
x = {};
}
for (auto& x : viewports)
{
x = {};
}
assert(barriers.empty());
assert(waits.empty());
assert(signals.empty());
drawargs_required = false;
dirty_drawargs = false;
numthreads_as = {};
numthreads_ms = {};
}
};
wi::allocator::BlockAllocator<CommandList_Metal, 64> cmd_allocator;
wi::vector<CommandList_Metal*> commandlists;
uint32_t cmd_count = 0;
wi::SpinLock cmd_locker;
wi::unordered_map<PipelineHash, JustInTimePSO> pipelines_global;
NS::SharedPtr<MTL::Buffer> descriptor_heap_res;
NS::SharedPtr<MTL::Buffer> descriptor_heap_sam;
IRDescriptorTableEntry* descriptor_heap_res_data = nullptr;
IRDescriptorTableEntry* descriptor_heap_sam_data = nullptr;
#ifdef USE_TEXTURE_VIEW_POOL
NS::SharedPtr<MTL::TextureViewPool> texture_view_pool;
#endif // USE_TEXTURE_VIEW_POOL
void binder_flush(CommandList cmd);
void barrier_flush(CommandList cmd);
void clear_flush(CommandList cmd);
constexpr CommandList_Metal& GetCommandList(CommandList cmd) const
{
assert(cmd.IsValid());
return *(CommandList_Metal*)cmd.internal_state;
}
void pso_validate(CommandList cmd);
void predraw(CommandList cmd);
void predispatch(CommandList cmd);
void precopy(CommandList cmd);
public:
GraphicsDevice_Metal(ValidationMode validationMode = ValidationMode::Disabled, GPUPreference preference = GPUPreference::Discrete);
~GraphicsDevice_Metal() override;
bool CreateSwapChain(const SwapChainDesc* desc, wi::platform::window_type window, SwapChain* swapchain) const override;
bool CreateBuffer2(const GPUBufferDesc* desc, const std::function<void(void*)>& init_callback, GPUBuffer* buffer, const GPUResource* alias = nullptr, uint64_t alias_offset = 0ull) const override;
bool CreateTexture(const TextureDesc* desc, const SubresourceData* initial_data, Texture* texture, const GPUResource* alias = nullptr, uint64_t alias_offset = 0ull) const override;
bool CreateShader(ShaderStage stage, const void* shadercode, size_t shadercode_size, Shader* shader, const char* entrypoint = "main") const override;
bool CreateSampler(const SamplerDesc* desc, Sampler* sampler) const override;
bool CreateQueryHeap(const GPUQueryHeapDesc* desc, GPUQueryHeap* queryheap) const override;
bool CreatePipelineState(const PipelineStateDesc* desc, PipelineState* pso, const RenderPassInfo* renderpass_info = nullptr) const override;
bool CreateRaytracingAccelerationStructure(const RaytracingAccelerationStructureDesc* desc, RaytracingAccelerationStructure* bvh) const override;
bool CreateRaytracingPipelineState(const RaytracingPipelineStateDesc* desc, RaytracingPipelineState* rtpso) const override;
bool CreateVideoDecoder(const VideoDesc* desc, VideoDecoder* video_decoder) const override;
int CreateSubresource(Texture* texture, SubresourceType type, uint32_t firstSlice, uint32_t sliceCount, uint32_t firstMip, uint32_t mipCount, const Format* format_change = nullptr, const ImageAspect* aspect = nullptr, const Swizzle* swizzle = nullptr, float min_lod_clamp = 0) const override;
int CreateSubresource(GPUBuffer* buffer, SubresourceType type, uint64_t offset, uint64_t size = ~0, const Format* format_change = nullptr, const uint32_t* structuredbuffer_stride_change = nullptr) const override;
void DeleteSubresources(GPUResource* resource) override;
int GetDescriptorIndex(const GPUResource* resource, SubresourceType type, int subresource = -1) const override;
int GetDescriptorIndex(const Sampler* sampler) const override;
void WriteShadingRateValue(ShadingRate rate, void* dest) const override;
void WriteTopLevelAccelerationStructureInstance(const RaytracingAccelerationStructureDesc::TopLevel::Instance* instance, void* dest) const override;
void WriteShaderIdentifier(const RaytracingPipelineState* rtpso, uint32_t group_index, void* dest) const override;
void SetName(GPUResource* pResource, const char* name) const override;
void SetName(Shader* shader, const char* name) const override;
CommandList BeginCommandList(QUEUE_TYPE queue = QUEUE_GRAPHICS) override;
void SubmitCommandLists() override;
void WaitForGPU() const override;
void ClearPipelineStateCache() override;
size_t GetActivePipelineCount() const override { return 0; }
ShaderFormat GetShaderFormat() const override { return ShaderFormat::METAL; }
Texture GetBackBuffer(const SwapChain* swapchain) const override;
ColorSpace GetSwapChainColorSpace(const SwapChain* swapchain) const override;
bool IsSwapChainSupportsHDR(const SwapChain* swapchain) const override;
uint32_t GetMinOffsetAlignment(const GPUBufferDesc* desc) const override
{
uint32_t alignment = 256u;
if (has_flag(desc->misc_flags, ResourceMiscFlag::ALIASING_BUFFER) || has_flag(desc->misc_flags, ResourceMiscFlag::ALIASING_TEXTURE_NON_RT_DS) || has_flag(desc->misc_flags, ResourceMiscFlag::ALIASING_TEXTURE_RT_DS))
{
alignment = std::max(alignment, uint32_t(64 * 1024)); // 64KB safety to match DX12
}
return alignment;
}
MemoryUsage GetMemoryUsage() const override
{
MemoryUsage mem;
mem.budget = device->recommendedMaxWorkingSetSize();
mem.usage = device->currentAllocatedSize();
return mem;
}
uint32_t GetMaxViewportCount() const override { return 16; };
void SparseUpdate(QUEUE_TYPE queue, const SparseUpdateCommand* commands, uint32_t command_count) override;
const char* GetTag() const override { return "Metal"; }
///////////////Thread-sensitive////////////////////////
void WaitCommandList(CommandList cmd, CommandList wait_for) override;
void RenderPassBegin(const SwapChain* swapchain, CommandList cmd) override;
void RenderPassBegin(const RenderPassImage* images, uint32_t image_count, CommandList cmd, RenderPassFlags flags = RenderPassFlags::NONE) override { RenderPassBegin(images, image_count, nullptr, cmd, flags); };
void RenderPassBegin(const RenderPassImage* images, uint32_t image_count, const GPUQueryHeap* occlusionqueries, CommandList cmd, RenderPassFlags flags = RenderPassFlags::NONE) override;
void RenderPassEnd(CommandList cmd) override;
void BindScissorRects(uint32_t numRects, const Rect* rects, CommandList cmd) override;
void BindViewports(uint32_t NumViewports, const Viewport *pViewports, CommandList cmd) override;
void BindResource(const GPUResource* resource, uint32_t slot, CommandList cmd, int subresource = -1) override;
void BindResources(const GPUResource *const* resources, uint32_t slot, uint32_t count, CommandList cmd) override;
void BindUAV(const GPUResource* resource, uint32_t slot, CommandList cmd, int subresource = -1) override;
void BindUAVs(const GPUResource *const* resources, uint32_t slot, uint32_t count, CommandList cmd) override;
void BindSampler(const Sampler* sampler, uint32_t slot, CommandList cmd) override;
void BindConstantBuffer(const GPUBuffer* buffer, uint32_t slot, CommandList cmd, uint64_t offset = 0ull) override;
void BindVertexBuffers(const GPUBuffer *const* vertexBuffers, uint32_t slot, uint32_t count, const uint32_t* strides, const uint64_t* offsets, CommandList cmd) override;
void BindIndexBuffer(const GPUBuffer* indexBuffer, const IndexBufferFormat format, uint64_t offset, CommandList cmd) override;
void BindStencilRef(uint32_t value, CommandList cmd) override;
void BindBlendFactor(float r, float g, float b, float a, CommandList cmd) override;
void BindShadingRate(ShadingRate rate, CommandList cmd) override;
void BindPipelineState(const PipelineState* pso, CommandList cmd) override;
void BindComputeShader(const Shader* cs, CommandList cmd) override;
void BindDepthBounds(float min_bounds, float max_bounds, CommandList cmd) override;
void Draw(uint32_t vertexCount, uint32_t startVertexLocation, CommandList cmd) override;
void DrawIndexed(uint32_t indexCount, uint32_t startIndexLocation, int32_t baseVertexLocation, CommandList cmd) override;
void DrawInstanced(uint32_t vertexCount, uint32_t instanceCount, uint32_t startVertexLocation, uint32_t startInstanceLocation, CommandList cmd) override;
void DrawIndexedInstanced(uint32_t indexCount, uint32_t instanceCount, uint32_t startIndexLocation, int32_t baseVertexLocation, uint32_t startInstanceLocation, CommandList cmd) override;
void DrawInstancedIndirect(const GPUBuffer* args, uint64_t args_offset, CommandList cmd) override;
void DrawIndexedInstancedIndirect(const GPUBuffer* args, uint64_t args_offset, CommandList cmd) override;
void DrawInstancedIndirectCount(const GPUBuffer* args, uint64_t args_offset, const GPUBuffer* count, uint64_t count_offset, uint32_t max_count, CommandList cmd) override;
void DrawIndexedInstancedIndirectCount(const GPUBuffer* args, uint64_t args_offset, const GPUBuffer* count, uint64_t count_offset, uint32_t max_count, CommandList cmd) override;
void Dispatch(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ, CommandList cmd) override;
void DispatchIndirect(const GPUBuffer* args, uint64_t args_offset, CommandList cmd) override;
void DispatchMesh(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ, CommandList cmd) override;
void DispatchMeshIndirect(const GPUBuffer* args, uint64_t args_offset, CommandList cmd) override;
void DispatchMeshIndirectCount(const GPUBuffer* args, uint64_t args_offset, const GPUBuffer* count, uint64_t count_offset, uint32_t max_count, CommandList cmd) override;
void CopyResource(const GPUResource* pDst, const GPUResource* pSrc, CommandList cmd) override;
void CopyBuffer(const GPUBuffer* pDst, uint64_t dst_offset, const GPUBuffer* pSrc, uint64_t src_offset, uint64_t size, CommandList cmd) override;
void CopyTexture(const Texture* dst, uint32_t dstX, uint32_t dstY, uint32_t dstZ, uint32_t dstMip, uint32_t dstSlice, const Texture* src, uint32_t srcMip, uint32_t srcSlice, CommandList cmd, const Box* srcbox, ImageAspect dst_aspect, ImageAspect src_aspect) override;
void QueryBegin(const GPUQueryHeap* heap, uint32_t index, CommandList cmd) override;
void QueryEnd(const GPUQueryHeap* heap, uint32_t index, CommandList cmd) override;
void QueryResolve(const GPUQueryHeap* heap, uint32_t index, uint32_t count, const GPUBuffer* dest, uint64_t dest_offset, CommandList cmd) override;
void QueryReset(const GPUQueryHeap* heap, uint32_t index, uint32_t count, CommandList cmd) override {};
void Barrier(const GPUBarrier* barriers, uint32_t numBarriers, CommandList cmd) override;
void BuildRaytracingAccelerationStructure(const RaytracingAccelerationStructure* dst, CommandList cmd, const RaytracingAccelerationStructure* src = nullptr) override;
void BindRaytracingPipelineState(const RaytracingPipelineState* rtpso, CommandList cmd) override;
void DispatchRays(const DispatchRaysDesc* desc, CommandList cmd) override;
void PushConstants(const void* data, uint32_t size, CommandList cmd, uint32_t offset = 0) override;
void PredicationBegin(const GPUBuffer* buffer, uint64_t offset, PredicationOp op, CommandList cmd) override;
void PredicationEnd(CommandList cmd) override;
void ClearUAV(const GPUResource* resource, uint32_t value, CommandList cmd) override;
void VideoDecode(const VideoDecoder* video_decoder, const VideoDecodeOperation* op, CommandList cmd) override;
void EventBegin(const char* name, CommandList cmd) override;
void EventEnd(CommandList cmd) override;
void SetMarker(const char* name, CommandList cmd) override;
RenderPassInfo GetRenderPassInfo(CommandList cmd) override
{
return GetCommandList(cmd).renderpass_info;
}
GPULinearAllocator& GetFrameAllocator(CommandList cmd) override
{
return GetCommandList(cmd).frame_allocators[GetBufferIndex()];
}
struct AllocationHandler
{
std::mutex destroylocker;
uint64_t framecount = 0;
std::deque<std::pair<NS::SharedPtr<MTL::Resource>, uint64_t>> destroyer_resources;
std::deque<std::pair<NS::SharedPtr<MTL::SamplerState>, uint64_t>> destroyer_samplers;
std::deque<std::pair<NS::SharedPtr<MTL::Library>, uint64_t>> destroyer_libraries;
std::deque<std::pair<NS::SharedPtr<MTL::Function>, uint64_t>> destroyer_functions;
std::deque<std::pair<NS::SharedPtr<MTL::RenderPipelineState>, uint64_t>> destroyer_render_pipelines;
std::deque<std::pair<NS::SharedPtr<MTL::ComputePipelineState>, uint64_t>> destroyer_compute_pipelines;
std::deque<std::pair<NS::SharedPtr<MTL::DepthStencilState>, uint64_t>> destroyer_depth_stencil_states;
std::deque<std::pair<NS::SharedPtr<MTL4::CounterHeap>, uint64_t>> destroyer_counter_heaps;
std::deque<std::pair<NS::SharedPtr<CA::MetalDrawable>, uint64_t>> destroyer_drawables;
std::deque<std::pair<int, uint64_t>> destroyer_bindless_res;
std::deque<std::pair<int, uint64_t>> destroyer_bindless_sam;
wi::vector<int> free_bindless_res;
wi::vector<int> free_bindless_sam;
void Update(uint64_t FRAMECOUNT, uint32_t BUFFERCOUNT)
{
std::scoped_lock lck(destroylocker);
framecount = FRAMECOUNT;
while (!destroyer_resources.empty() && destroyer_resources.front().second + BUFFERCOUNT < FRAMECOUNT)
{
remove_resident(destroyer_resources.front().first.get());
destroyer_resources.pop_front();
// SharedPtr auto delete
}
while (!destroyer_samplers.empty() && destroyer_samplers.front().second + BUFFERCOUNT < FRAMECOUNT)
{
destroyer_samplers.pop_front();
// SharedPtr auto delete
}
while (!destroyer_libraries.empty() && destroyer_libraries.front().second + BUFFERCOUNT < FRAMECOUNT)
{
destroyer_libraries.pop_front();
// SharedPtr auto delete
}
while (!destroyer_functions.empty() && destroyer_functions.front().second + BUFFERCOUNT < FRAMECOUNT)
{
destroyer_functions.pop_front();
// SharedPtr auto delete
}
while (!destroyer_render_pipelines.empty() && destroyer_render_pipelines.front().second + BUFFERCOUNT < FRAMECOUNT)
{
destroyer_render_pipelines.pop_front();
// SharedPtr auto delete
}
while (!destroyer_compute_pipelines.empty() && destroyer_compute_pipelines.front().second + BUFFERCOUNT < FRAMECOUNT)
{
destroyer_compute_pipelines.pop_front();
// SharedPtr auto delete
}
while (!destroyer_depth_stencil_states.empty() && destroyer_depth_stencil_states.front().second + BUFFERCOUNT < FRAMECOUNT)
{
destroyer_depth_stencil_states.pop_front();
// SharedPtr auto delete
}
while (!destroyer_counter_heaps.empty() && destroyer_counter_heaps.front().second + BUFFERCOUNT < FRAMECOUNT)
{
destroyer_counter_heaps.pop_front();
// SharedPtr auto delete
}
while (!destroyer_drawables.empty() && destroyer_drawables.front().second + BUFFERCOUNT < FRAMECOUNT)
{
destroyer_drawables.pop_front();
// SharedPtr auto delete
}
while (!destroyer_bindless_res.empty() && destroyer_bindless_res.front().second + BUFFERCOUNT < FRAMECOUNT)
{
free_bindless_res.push_back(destroyer_bindless_res.front().first);
destroyer_bindless_res.pop_front();
}
while (!destroyer_bindless_sam.empty() && destroyer_bindless_sam.front().second + BUFFERCOUNT < FRAMECOUNT)
{
free_bindless_sam.push_back(destroyer_bindless_sam.front().first);
destroyer_bindless_sam.pop_front();
}
}
int allocate_resource_index()
{
std::scoped_lock lck(destroylocker);
if (free_bindless_res.empty())
return -1;
int index = free_bindless_res.back();
free_bindless_res.pop_back();
return index;
}
int allocate_sampler_index()
{
std::scoped_lock lck(destroylocker);
if (free_bindless_sam.empty())
return -1;
int index = free_bindless_sam.back();
free_bindless_sam.pop_back();
return index;
}
NS::SharedPtr<MTL::ResidencySet> residency_set;
void make_resident(const MTL::Resource* allocation)
{
if (allocation == nullptr)
return;
std::scoped_lock locker(destroylocker);
residency_set->addAllocation(allocation);
}
void remove_resident(const MTL::Allocation* allocation)
{
if (allocation == nullptr)
return;
residency_set->removeAllocation(allocation);
}
};
wi::allocator::shared_ptr<AllocationHandler> allocationhandler;
};
}
#endif // __APPLE__