Skip to content

Commit d577257

Browse files
committed
fixes
1 parent 7d800a5 commit d577257

File tree

20 files changed

+117
-1147
lines changed

20 files changed

+117
-1147
lines changed

sources/DirectXFramework/DX12/CommandList.cpp

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,28 @@ namespace DX12
187187
m_commandList->EndQuery(pQueryHeap.get_native(), D3D12_QUERY_TYPE_TIMESTAMP, QueryIdx);
188188
}
189189

190-
void Eventer::resolve_times(Resource* resource, QueryHeap& pQueryHeap, uint32_t NumQueries, std::function<void()> f)
191-
{
192-
190+
void Eventer::resolve_times(QueryHeap& pQueryHeap, uint32_t NumQueries, std::function<void(std::span<UINT64>)> f)
191+
{
193192
CommandList* list = static_cast<CommandList*>(this); // :(
194-
list->transition(resource, ResourceState::COPY_DEST);
193+
auto info = list->read_data(NumQueries* sizeof(UINT64));
194+
195195
list->flush_transitions();
196-
m_commandList->ResolveQueryData(pQueryHeap.get_native(), D3D12_QUERY_TYPE_TIMESTAMP, 0, NumQueries, resource->get_native().Get(), 0);
197-
on_execute_funcs.emplace_back(f);
196+
m_commandList->ResolveQueryData(pQueryHeap.get_native(), D3D12_QUERY_TYPE_TIMESTAMP, 0, NumQueries, info.resource->get_native().Get(), info.offset);
197+
198+
199+
on_execute_funcs.emplace_back([info, f, NumQueries]() {
200+
201+
UINT64* data;
202+
D3D12_RANGE range;
203+
range.Begin = info.offset;
204+
range.End = info.offset + info.size;
205+
info.resource->get_native()->Map(0, &range, reinterpret_cast<void**>(&data));
206+
207+
208+
f(std::span(data, NumQueries));
209+
info.resource->get_native()->Unmap(0, nullptr);
210+
211+
});
198212
}
199213

200214
void GraphicsContext::set_signature(const RootSignature::ptr& s)
@@ -385,7 +399,7 @@ namespace DX12
385399
desc.SizeInBytes = (UINT)Math::AlignUp(size, D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
386400

387401
assert(desc.SizeInBytes < 65536);
388-
Handle h = list.frame_resources->srv_uav_cbv_cpu.place();
402+
Handle h = list.frame_resources->get_cpu_heap(Render::DescriptorHeapType::CBV_SRV_UAV).place();
389403

390404
Device::get().create_cbv(h, resource.get(), desc);
391405

@@ -405,39 +419,20 @@ namespace DX12
405419
UploadInfo info;
406420
info.resource = upload_resources.back();
407421
info.offset = resource_offset;
408-
resource_offset += AlignedSize;
409-
// UINT64 uploadBufferSize = GetRequiredIntermediateSize(resource->get_native().Get(), first_subresource, sub_count);
410-
/* while (resource_index < upload_resources.size() && resource_offset + uploadBufferSize > upload_resources[resource_index]->get_size())
411-
{
412-
resource_index++; resource_offset = 0;
413-
}
414-
415-
if (upload_resources.empty() || upload_resources.size() == resource_index)
416-
{
417-
upload_resources.push_back(BufferCache::get().get_upload(std::max(heap_size, uploadBufferSize)));
418-
resource_offset = 0;
419-
resource_index = upload_resources.size() - 1;
420-
}
421-
422-
assert(!upload_resources.empty());
423-
UploadInfo info;
424-
info.resource = upload_resources[resource_index];
425-
info.offset = resource_offset;
426-
resource_offset += uploadBufferSize;
427-
428-
if (resource_offset >= upload_resources.back()->get_size())
429-
resource_index++;
430-
*/
431422
info.size = AlignedSize;
423+
424+
resource_offset += AlignedSize;
432425
return info;
433426
}
427+
434428
Readbacker::ReadBackInfo Readbacker::read_data(UINT64 uploadBufferSize)
435429
{
436430
auto buff = BufferCache::get().get_readback(uploadBufferSize);//std::make_shared<BufferBase>(uploadBufferSize, HeapType::READBACK, ResourceState::COPY_DEST);
437431
read_back_resources.push_back(buff);
438432
ReadBackInfo info;
439433
info.resource = buff;//read_back_resources.back();
440434
info.offset = 0;
435+
info.size = uploadBufferSize;
441436
return info;
442437
}
443438
void CopyContext::update_resource(Resource::ptr resource, UINT first_subresource, UINT sub_count, D3D12_SUBRESOURCE_DATA* data)
@@ -555,7 +550,11 @@ namespace DX12
555550
}
556551
std::future<bool> CopyContext::read_texture(const Resource* resource, ivec3 offset, ivec3 box, UINT sub_resource, std::function<void(const char*, UINT64, UINT64, UINT64)> f)
557552
{
558-
base.transition(resource, Render::ResourceState::COPY_SOURCE);
553+
if (base.type != CommandListType::COPY)
554+
base.transition(resource, Render::ResourceState::COPY_SOURCE);
555+
else
556+
base.transition(resource, Render::ResourceState::COMMON);
557+
559558
base.flush_transitions();
560559
UINT64 RequiredSize = 0;
561560
D3D12_PLACED_SUBRESOURCE_FOOTPRINT Layouts;

sources/DirectXFramework/DX12/CommandList.h

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ namespace DX12
3333
std::mutex upload;
3434
std::mutex read_back;
3535

36+
37+
ResourceHeapAllocator allocator;
3638
public:
3739

3840
std::shared_ptr<UploadBuffer> get_upload(UINT64 size);
@@ -168,31 +170,46 @@ namespace DX12
168170
template<class LockPolicy = Free>
169171
class GPUCompiledManager : public Uploader
170172
{
171-
// using cb_buffer = virtual_gpu_buffer<std::byte>;
173+
enum_array<DescriptorHeapType, typename DynamicDescriptor<LockPolicy>::ptr> cpu_heaps;
174+
enum_array<DescriptorHeapType, typename DynamicDescriptor<LockPolicy>::ptr> gpu_heaps;
172175

173-
// static Cache<std::shared_ptr<cb_buffer>> cbv_cache;
174176
public:
175-
DynamicDescriptor<DescriptorHeapType::CBV_SRV_UAV, LockPolicy, DescriptorHeapFlags::SHADER_VISIBLE> srv;
176-
DynamicDescriptor<DescriptorHeapType::SAMPLER, LockPolicy, DescriptorHeapFlags::SHADER_VISIBLE> smp;
177177

178-
DynamicDescriptor<DescriptorHeapType::CBV_SRV_UAV, LockPolicy> srv_uav_cbv_cpu;
179-
DynamicDescriptor<DescriptorHeapType::RTV, LockPolicy> rtv_cpu;
180-
DynamicDescriptor<DescriptorHeapType::DSV, LockPolicy> dsv_cpu;
181-
DynamicDescriptor<DescriptorHeapType::SAMPLER, LockPolicy> smp_cpu;
178+
DynamicDescriptor<LockPolicy>& get_cpu_heap(DescriptorHeapType type)
179+
{
180+
assert(cpu_heaps[type]);
181+
return *cpu_heaps[type];
182+
}
182183

183-
// std::shared_ptr<cb_buffer> cb;
184+
DynamicDescriptor<LockPolicy>& get_gpu_heap(DescriptorHeapType type)
185+
{
186+
assert(cpu_heaps[type]);
187+
return *gpu_heaps[type];
188+
}
189+
190+
GPUCompiledManager()
191+
{
192+
gpu_heaps[DescriptorHeapType::CBV_SRV_UAV] = std::make_shared<DynamicDescriptor<LockPolicy>>(DescriptorHeapType::CBV_SRV_UAV, DescriptorHeapFlags::SHADER_VISIBLE);
193+
gpu_heaps[DescriptorHeapType::SAMPLER] = std::make_shared<DynamicDescriptor<LockPolicy>>(DescriptorHeapType::SAMPLER, DescriptorHeapFlags::SHADER_VISIBLE);
194+
195+
cpu_heaps[DescriptorHeapType::CBV_SRV_UAV] = std::make_shared<DynamicDescriptor<LockPolicy>>(DescriptorHeapType::CBV_SRV_UAV, DescriptorHeapFlags::NONE);
196+
cpu_heaps[DescriptorHeapType::RTV] = std::make_shared<DynamicDescriptor<LockPolicy>>(DescriptorHeapType::RTV, DescriptorHeapFlags::NONE);
197+
cpu_heaps[DescriptorHeapType::DSV] = std::make_shared<DynamicDescriptor<LockPolicy>>(DescriptorHeapType::DSV, DescriptorHeapFlags::NONE);
198+
cpu_heaps[DescriptorHeapType::SAMPLER] = std::make_shared<DynamicDescriptor<LockPolicy>>(DescriptorHeapType::SAMPLER, DescriptorHeapFlags::NONE);
199+
}
184200

185201
void reset()
186202
{
187-
srv.reset();
188-
smp.reset();
189203

190-
srv_uav_cbv_cpu.reset();
191-
rtv_cpu.reset();
192-
dsv_cpu.reset();
193-
smp_cpu.reset();
194-
Uploader::reset();
204+
for (auto& h : gpu_heaps)
205+
if (h)
206+
h->reset();
195207

208+
for (auto& h : cpu_heaps)
209+
if (h)
210+
h->reset();
211+
212+
Uploader::reset();
196213
}
197214

198215
};
@@ -556,6 +573,7 @@ namespace DX12
556573
{
557574
std::shared_ptr<CPUBuffer> resource;
558575
UINT64 offset;
576+
UINT64 size;
559577
};
560578

561579
ReadBackInfo read_data(UINT64 uploadBufferSize);
@@ -641,7 +659,7 @@ namespace DX12
641659

642660
// timers
643661
void insert_time(QueryHeap& pQueryHeap, uint32_t QueryIdx);
644-
void resolve_times(Resource* resource, QueryHeap& pQueryHeap, uint32_t NumQueries, std::function<void()>);
662+
void resolve_times(QueryHeap& pQueryHeap, uint32_t NumQueries, std::function<void(std::span<UINT64>)>);
645663

646664
};
647665

@@ -856,7 +874,7 @@ namespace DX12
856874
transition_uav(h.resource_info);
857875

858876
flush_transitions();
859-
auto handle = srv.place(h);
877+
auto handle = get_cpu_heap(DescriptorHeapType::CBV_SRV_UAV).place(h);
860878
get_native_list()->ClearUnorderedAccessViewUint(handle.gpu, h.cpu, resource->get_native().Get(), reinterpret_cast<UINT*>(ClearColor.data()), 0, nullptr);
861879
}
862880

@@ -866,7 +884,7 @@ namespace DX12
866884
transition_uav(h.resource_info);
867885

868886
flush_transitions();
869-
auto handle = srv.place(h);
887+
auto handle = get_cpu_heap(DescriptorHeapType::CBV_SRV_UAV).place(h);
870888
get_native_list()->ClearUnorderedAccessViewFloat(handle.gpu, h.cpu, resource->get_native().Get(), reinterpret_cast<FLOAT*>(ClearColor.data()), 0, nullptr);
871889
}
872890

@@ -1124,22 +1142,22 @@ namespace DX12
11241142

11251143
HandleTableLight place_rtv(UINT count)
11261144
{
1127-
return get_base().rtv_cpu.place(count);
1145+
return get_base().get_cpu_heap(DescriptorHeapType::RTV).place(count);
11281146
}
11291147

11301148
HandleTableLight place_dsv(UINT count)
11311149
{
1132-
return get_base().dsv_cpu.place(count);
1150+
return get_base().get_cpu_heap(DescriptorHeapType::DSV).place(count);
11331151
}
11341152

11351153
HandleTableLight place_rtv(std::initializer_list<Handle> list)
11361154
{
1137-
return get_base().rtv_cpu.place(list);
1155+
return get_base().get_cpu_heap(DescriptorHeapType::RTV).place(list);
11381156
}
11391157

11401158
HandleTableLight place_dsv(std::initializer_list<Handle> list)
11411159
{
1142-
return get_base().dsv_cpu.place(list);
1160+
return get_base().get_cpu_heap(DescriptorHeapType::DSV).place(list);
11431161
}
11441162

11451163
void set_rtvs_internal(D3D12_CPU_DESCRIPTOR_HANDLE* t, Handle h)

sources/DirectXFramework/DX12/Descriptors.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,12 @@ namespace DX12
766766
using mutex = std::atomic<std::thread::id>;
767767
};
768768

769-
template<DescriptorHeapType type, class LockPolicy = Free, DescriptorHeapFlags flags = DescriptorHeapFlags::NONE>
769+
template<class LockPolicy = Free>
770770
class DynamicDescriptor
771771
{
772+
DescriptorHeapType type;
773+
DescriptorHeapFlags flags = DescriptorHeapFlags::NONE;
774+
772775
friend class CommandList;
773776
friend class GraphicsContext;
774777
friend class ComputeContext;
@@ -779,7 +782,7 @@ namespace DX12
779782
typename LockPolicy::mutex m;
780783
void create_heap(UINT count)
781784
{
782-
if constexpr (flags == DescriptorHeapFlags::SHADER_VISIBLE)
785+
if (flags == DescriptorHeapFlags::SHADER_VISIBLE)
783786
{
784787
pages.push_back(DescriptorHeapManager::get().get_gpu_heap(type)->create_page(count));
785788
}
@@ -815,6 +818,13 @@ namespace DX12
815818
}
816819

817820
public:
821+
822+
using ptr = std::shared_ptr<DynamicDescriptor<LockPolicy>>;
823+
DynamicDescriptor(DescriptorHeapType type, DescriptorHeapFlags flags):type(type),flags(flags)
824+
{
825+
826+
}
827+
818828
~DynamicDescriptor()
819829
{
820830
reset();

sources/DirectXFramework/DX12/GPUTimer.h

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ namespace DX12
99

1010
QueryHeap heap;
1111
std::mutex buffer_lock;
12-
std::queue<std::shared_ptr<CPUBuffer>> buffers;
13-
14-
1512
static const int MAX_TIMERS = 1024;
1613
IdGenerator ids;
1714
std::array<UINT64, MAX_TIMERS*2> read_back_data;
@@ -47,70 +44,14 @@ namespace DX12
4744
return static_cast<double>(gpu_start) / frequency;
4845
}
4946

50-
51-
/* void start_read()
52-
{
53-
if (fence != -1)
54-
DX12::Device::get().get_queue(CommandListType::DIRECT)->wait(fence);
55-
56-
read_back_data = buffer.map<UINT64>(0, MAX_TIMERS * 2);
57-
}
58-
59-
void end_read()
60-
{
61-
buffer.unmap();
62-
auto list = DX12::Device::get().get_queue(CommandListType::DIRECT)->get_free_list();
63-
list->begin();
64-
list->resolve_times(&buffer, heap, MAX_TIMERS * 2);
65-
list->end();
66-
fence = list->execute();
67-
}
68-
*/
6947
void read_buffer(CommandList::ptr &list, std::function<void()> f)
7048
{
71-
std::shared_ptr<CPUBuffer> current_buffer;
72-
{
73-
std::lock_guard<std::mutex> g(buffer_lock);
74-
75-
if(buffers.empty())
76-
{
77-
current_buffer.reset(new CPUBuffer(MAX_TIMERS * 2, sizeof(UINT64)));
78-
}
79-
else {
80-
current_buffer = buffers.front();
81-
buffers.pop();
82-
}
83-
}
84-
85-
list->resolve_times(current_buffer.get(), heap, MAX_TIMERS * 2, [this, f, current_buffer]() {
86-
memcpy(read_back_data.data(), current_buffer->map<UINT64>(0, MAX_TIMERS * 2), sizeof(UINT64)*MAX_TIMERS*2);
87-
current_buffer->unmap();
88-
{
89-
std::lock_guard<std::mutex> g(buffer_lock);
90-
buffers.push(current_buffer);
91-
}
49+
list->resolve_times( heap, MAX_TIMERS * 2, [this, f](std::span<UINT64> data) {
50+
std::copy(data.begin(), data.end(), read_back_data.begin());
9251
f();
93-
9452
});
9553
}
9654

97-
/*
98-
void read_buffer(std::function<void()> f)
99-
{
100-
auto list = DX12::Device::get().get_queue(CommandListType::DIRECT)->get_free_list();
101-
list->begin();
102-
list->resolve_times(&buffer, heap, MAX_TIMERS * 2, [this, f]() {
103-
read_back_data = buffer.map<UINT64>(0, MAX_TIMERS * 2);
104-
f();
105-
buffer.unmap();
106-
});
107-
108-
list->end();
109-
list->execute();
110-
111-
112-
113-
}*/
11455
};
11556

11657

sources/DirectXFramework/DX12/Memory.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace DX12
1414

1515
class ResourceHeap
1616
{
17+
std::shared_ptr<Resource> cpu_buffer;
18+
1719
public:
1820
ComPtr<ID3D12Heap > heap;
1921
HeapType type;
@@ -176,12 +178,6 @@ namespace DX12
176178
UINT offset;
177179
UINT count = 1;
178180
};
179-
enum class TileState : int
180-
{
181-
LOADED,
182-
FREED,
183-
NONE
184-
};
185181

186182
struct ResourceTile
187183
{

sources/DirectXFramework/DX12/PipelineState.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ namespace DX12
470470
//Log::get() << desc << Log::endl;
471471
auto state= PipelineState::ptr(new PipelineState(desc, binary));
472472

473-
if (!desc.name.empty() && binary.empty())
473+
if (!desc.name.empty())
474474
{
475475
binary_cache[desc.name] = state->get_cache();
476476
}
@@ -485,7 +485,7 @@ namespace DX12
485485
//Log::get() << desc << Log::endl;
486486
auto state = ComputePipelineState::ptr(new ComputePipelineState(desc,binary));
487487

488-
if (!desc.name.empty() &&binary.empty())
488+
if (!desc.name.empty())
489489
{
490490
binary_cache[desc.name] = state->get_cache();
491491
}

0 commit comments

Comments
 (0)