Skip to content

Commit 2d659b6

Browse files
committed
Rework D3D12 descriptor heap management again to leave GPU handles untouched
This is necessary since with D3D12 raytracing one can put descriptor handles into shader binding tables, which ReShade cannot easily fix up
1 parent 792dd45 commit 2d659b6

File tree

6 files changed

+197
-194
lines changed

6 files changed

+197
-194
lines changed

source/d3d12/d3d12_command_list.cpp

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,13 @@ void STDMETHODCALLTYPE D3D12GraphicsCommandList::SetGraphicsRootSignature(ID3D12
449449
}
450450
void STDMETHODCALLTYPE D3D12GraphicsCommandList::SetComputeRootDescriptorTable(UINT RootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE BaseDescriptor)
451451
{
452-
#if RESHADE_ADDON
453-
_orig->SetComputeRootDescriptorTable(RootParameterIndex, _device->convert_to_original_gpu_descriptor_handle(BaseDescriptor));
454-
#else
455452
_orig->SetComputeRootDescriptorTable(RootParameterIndex, BaseDescriptor);
456-
#endif
457453

458454
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
459-
const reshade::api::descriptor_set set = { BaseDescriptor.ptr };
455+
if (!reshade::has_addon_event<reshade::addon_event::bind_descriptor_sets>())
456+
return;
457+
458+
const reshade::api::descriptor_set set = _device_impl->convert_to_descriptor_set(BaseDescriptor);
460459
reshade::invoke_addon_event<reshade::addon_event::bind_descriptor_sets>(
461460
this,
462461
reshade::api::shader_stage::all_compute,
@@ -467,14 +466,13 @@ void STDMETHODCALLTYPE D3D12GraphicsCommandList::SetComputeRootDescriptorTable(U
467466
}
468467
void STDMETHODCALLTYPE D3D12GraphicsCommandList::SetGraphicsRootDescriptorTable(UINT RootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE BaseDescriptor)
469468
{
470-
#if RESHADE_ADDON
471-
_orig->SetGraphicsRootDescriptorTable(RootParameterIndex, _device->convert_to_original_gpu_descriptor_handle(BaseDescriptor));
472-
#else
473469
_orig->SetGraphicsRootDescriptorTable(RootParameterIndex, BaseDescriptor);
474-
#endif
475470

476471
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
477-
const reshade::api::descriptor_set set = { BaseDescriptor.ptr };
472+
if (!reshade::has_addon_event<reshade::addon_event::bind_descriptor_sets>())
473+
return;
474+
475+
const reshade::api::descriptor_set set = _device_impl->convert_to_descriptor_set(BaseDescriptor);
478476
reshade::invoke_addon_event<reshade::addon_event::bind_descriptor_sets>(
479477
this,
480478
reshade::api::shader_stage::all_graphics,
@@ -702,35 +700,27 @@ void STDMETHODCALLTYPE D3D12GraphicsCommandList::ClearRenderTargetView(D3D12_CPU
702700
}
703701
void STDMETHODCALLTYPE D3D12GraphicsCommandList::ClearUnorderedAccessViewUint(D3D12_GPU_DESCRIPTOR_HANDLE ViewGPUHandleInCurrentHeap, D3D12_CPU_DESCRIPTOR_HANDLE ViewCPUHandle, ID3D12Resource *pResource, const UINT Values[4], UINT NumRects, const D3D12_RECT *pRects)
704702
{
705-
#if RESHADE_ADDON
706-
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = _device_impl->convert_to_original_cpu_descriptor_handle(ViewCPUHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
703+
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
704+
ViewCPUHandle = _device_impl->convert_to_original_cpu_descriptor_handle(ViewCPUHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
705+
#endif
707706

708-
if (reshade::invoke_addon_event<reshade::addon_event::clear_unordered_access_view_uint>(this, to_handle(original_descriptor_handle), Values, NumRects, reinterpret_cast<const reshade::api::rect *>(pRects)))
707+
#if RESHADE_ADDON
708+
if (reshade::invoke_addon_event<reshade::addon_event::clear_unordered_access_view_uint>(this, to_handle(ViewCPUHandle), Values, NumRects, reinterpret_cast<const reshade::api::rect *>(pRects)))
709709
return;
710-
711-
_orig->ClearUnorderedAccessViewUint(
712-
_device_impl->convert_to_original_gpu_descriptor_handle(ViewGPUHandleInCurrentHeap),
713-
original_descriptor_handle,
714-
pResource, Values, NumRects, pRects);
715-
#else
716-
_orig->ClearUnorderedAccessViewUint(ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects);
717710
#endif
711+
_orig->ClearUnorderedAccessViewUint(ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects);
718712
}
719713
void STDMETHODCALLTYPE D3D12GraphicsCommandList::ClearUnorderedAccessViewFloat(D3D12_GPU_DESCRIPTOR_HANDLE ViewGPUHandleInCurrentHeap, D3D12_CPU_DESCRIPTOR_HANDLE ViewCPUHandle, ID3D12Resource *pResource, const FLOAT Values[4], UINT NumRects, const D3D12_RECT *pRects)
720714
{
721-
#if RESHADE_ADDON
722-
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = _device_impl->convert_to_original_cpu_descriptor_handle(ViewCPUHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
715+
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
716+
ViewCPUHandle = _device_impl->convert_to_original_cpu_descriptor_handle(ViewCPUHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
717+
#endif
723718

724-
if (reshade::invoke_addon_event<reshade::addon_event::clear_unordered_access_view_float>(this, to_handle(original_descriptor_handle), Values, NumRects, reinterpret_cast<const reshade::api::rect *>(pRects)))
719+
#if RESHADE_ADDON
720+
if (reshade::invoke_addon_event<reshade::addon_event::clear_unordered_access_view_float>(this, to_handle(ViewCPUHandle), Values, NumRects, reinterpret_cast<const reshade::api::rect *>(pRects)))
725721
return;
726-
727-
_orig->ClearUnorderedAccessViewFloat(
728-
_device_impl->convert_to_original_gpu_descriptor_handle(ViewGPUHandleInCurrentHeap),
729-
original_descriptor_handle,
730-
pResource, Values, NumRects, pRects);
731-
#else
732-
_orig->ClearUnorderedAccessViewFloat(ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects);
733722
#endif
723+
_orig->ClearUnorderedAccessViewFloat(ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects);
734724
}
735725
void STDMETHODCALLTYPE D3D12GraphicsCommandList::DiscardResource(ID3D12Resource *pResource, const D3D12_DISCARD_REGION *pRegion)
736726
{

source/d3d12/d3d12_descriptor_heap.cpp

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* License: https://github.com/crosire/reshade#license
44
*/
55

6-
#if RESHADE_ADDON
6+
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
77

88
#include "d3d12_device.hpp"
99
#include "d3d12_descriptor_heap.hpp"
1010
#include "dll_log.hpp"
1111

12-
D3D12DescriptorHeap::D3D12DescriptorHeap(D3D12Device *device, ID3D12DescriptorHeap *original) :
12+
D3D12DescriptorHeap::D3D12DescriptorHeap(ID3D12Device *device, ID3D12DescriptorHeap *original) :
1313
_orig(original),
1414
_device(device)
1515
{
@@ -102,36 +102,7 @@ D3D12_CPU_DESCRIPTOR_HANDLE STDMETHODCALLTYPE D3D12DescriptorHeap::GetCPUDescrip
102102
}
103103
D3D12_GPU_DESCRIPTOR_HANDLE STDMETHODCALLTYPE D3D12DescriptorHeap::GetGPUDescriptorHandleForHeapStart()
104104
{
105-
return _internal_base_gpu_handle;
106-
}
107-
108-
void D3D12DescriptorHeap::initialize_descriptor_base_handle(size_t heap_index)
109-
{
110-
// Generate a descriptor handle of the following format:
111-
// Bit 0 - 19: Descriptor Index
112-
// Bit 20 - 22: Heap Type
113-
// Bit 23 - 23: Heap Flags
114-
// Bit 24 - 55: Heap Index
115-
116-
_orig_base_cpu_handle = _orig->GetCPUDescriptorHandleForHeapStart();
117-
_internal_base_cpu_handle = { 0 };
118-
119-
static_assert(D3D12_MAX_SHADER_VISIBLE_DESCRIPTOR_HEAP_SIZE_TIER_2 < (1 << 20));
120-
121-
assert(heap_index < (1ull << std::min(sizeof(SIZE_T) * 8 - 24ull, 32ull)));
122-
_internal_base_cpu_handle.ptr |= static_cast<SIZE_T>(heap_index) << 24;
123-
124-
const D3D12_DESCRIPTOR_HEAP_DESC heap_desc = _orig->GetDesc();
125-
assert(heap_desc.Type <= 0x3);
126-
_internal_base_cpu_handle.ptr |= heap_desc.Type << 20;
127-
assert(heap_desc.Flags <= 0x1);
128-
_internal_base_cpu_handle.ptr |= heap_desc.Flags << 23;
129-
130-
if (heap_desc.Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)
131-
{
132-
_orig_base_gpu_handle = _orig->GetGPUDescriptorHandleForHeapStart();
133-
_internal_base_gpu_handle.ptr = _internal_base_cpu_handle.ptr;
134-
}
105+
return _orig->GetGPUDescriptorHandleForHeapStart();
135106
}
136107

137108
#endif

source/d3d12/d3d12_descriptor_heap.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
#pragma once
77

8-
#if RESHADE_ADDON
8+
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
99

1010
#include <d3d12.h>
1111

1212
struct D3D12Device;
1313

1414
struct DECLSPEC_UUID("8628AD68-6047-4D27-9D87-3E5F386E0231") D3D12DescriptorHeap final : ID3D12DescriptorHeap
1515
{
16-
D3D12DescriptorHeap(D3D12Device *device, ID3D12DescriptorHeap *original);
16+
D3D12DescriptorHeap(ID3D12Device *device, ID3D12DescriptorHeap *original);
1717

1818
#pragma region IUnknown
1919
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObj) override;
@@ -41,9 +41,10 @@ struct DECLSPEC_UUID("8628AD68-6047-4D27-9D87-3E5F386E0231") D3D12DescriptorHeap
4141

4242
ID3D12DescriptorHeap *_orig;
4343
ULONG _ref = 1;
44-
D3D12Device *const _device;
45-
D3D12_CPU_DESCRIPTOR_HANDLE _internal_base_cpu_handle = { 0 }, _orig_base_cpu_handle = { 0 };
46-
D3D12_GPU_DESCRIPTOR_HANDLE _internal_base_gpu_handle = { 0 }, _orig_base_gpu_handle = { 0 };
44+
ID3D12Device *const _device;
45+
D3D12_CPU_DESCRIPTOR_HANDLE _orig_base_cpu_handle = { 0 };
46+
D3D12_GPU_DESCRIPTOR_HANDLE _orig_base_gpu_handle = { 0 };
47+
D3D12_CPU_DESCRIPTOR_HANDLE _internal_base_cpu_handle = { 0 };
4748
};
4849

4950
#endif

source/d3d12/d3d12_device.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,6 @@ HRESULT STDMETHODCALLTYPE D3D12Device::CreateDescriptorHeap(const D3D12_DESCRIPT
521521
}
522522
UINT STDMETHODCALLTYPE D3D12Device::GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE DescriptorHeapType)
523523
{
524-
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
525-
if (DescriptorHeapType <= D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER)
526-
return 0x1; // See 'D3D12DescriptorHeap::initialize_descriptor_base_handle'
527-
#endif
528-
529524
return _orig->GetDescriptorHandleIncrementSize(DescriptorHeapType);
530525
}
531526
HRESULT STDMETHODCALLTYPE D3D12Device::CreateRootSignature(UINT nodeMask, const void *pBlobWithRootSignature, SIZE_T blobLengthInBytes, REFIID riid, void **ppvRootSignature)
@@ -708,13 +703,9 @@ HRESULT STDMETHODCALLTYPE D3D12Device::CreateRootSignature(UINT nodeMask, const
708703
}
709704
void STDMETHODCALLTYPE D3D12Device::CreateConstantBufferView(const D3D12_CONSTANT_BUFFER_VIEW_DESC *pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor)
710705
{
711-
#if RESHADE_ADDON
706+
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
712707
_orig->CreateConstantBufferView(pDesc, convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
713-
#else
714-
_orig->CreateConstantBufferView(pDesc, DestDescriptor);
715-
#endif
716708

717-
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
718709
if (!reshade::has_addon_event<reshade::addon_event::update_descriptor_sets>())
719710
return;
720711

@@ -724,14 +715,16 @@ void STDMETHODCALLTYPE D3D12Device::CreateConstantBufferView(const D3D12_CONS
724715
buffer_range.size = pDesc->SizeInBytes;
725716

726717
reshade::api::descriptor_set_update update;
727-
update.set = { DestDescriptor.ptr };
718+
update.set = convert_to_descriptor_set(DestDescriptor);
728719
update.binding = 0;
729720
update.array_offset = 0;
730721
update.type = reshade::api::descriptor_type::constant_buffer;
731722
update.count = 1;
732723
update.descriptors = &buffer_range;
733724

734725
reshade::invoke_addon_event<reshade::addon_event::update_descriptor_sets>(this, 1, &update);
726+
#else
727+
_orig->CreateConstantBufferView(pDesc, DestDescriptor);
735728
#endif
736729
}
737730
void STDMETHODCALLTYPE D3D12Device::CreateShaderResourceView(ID3D12Resource *pResource, const D3D12_SHADER_RESOURCE_VIEW_DESC *pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor)
@@ -748,7 +741,11 @@ void STDMETHODCALLTYPE D3D12Device::CreateShaderResourceView(ID3D12Resource *
748741
pDesc = &internal_desc;
749742
}
750743

744+
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
751745
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
746+
#else
747+
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = DestDescriptor;
748+
#endif
752749
_orig->CreateShaderResourceView(pResource, pDesc, original_descriptor_handle);
753750

754751
const reshade::api::resource_view descriptor_value = to_handle(original_descriptor_handle);
@@ -764,7 +761,7 @@ void STDMETHODCALLTYPE D3D12Device::CreateShaderResourceView(ID3D12Resource *
764761
return;
765762

766763
reshade::api::descriptor_set_update update;
767-
update.set = { DestDescriptor.ptr };
764+
update.set = convert_to_descriptor_set(DestDescriptor);
768765
update.binding = 0;
769766
update.array_offset = 0;
770767
update.type = reshade::api::descriptor_type::shader_resource_view;
@@ -788,8 +785,12 @@ void STDMETHODCALLTYPE D3D12Device::CreateUnorderedAccessView(ID3D12Resource
788785
pDesc = &internal_desc;
789786
}
790787

788+
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
791789
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
792-
_orig->CreateUnorderedAccessView(pResource, pCounterResource, pDesc, convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
790+
#else
791+
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = DestDescriptor;
792+
#endif
793+
_orig->CreateUnorderedAccessView(pResource, pCounterResource, pDesc, original_descriptor_handle);
793794

794795
const reshade::api::resource_view descriptor_value = to_handle(original_descriptor_handle);
795796

@@ -804,7 +805,7 @@ void STDMETHODCALLTYPE D3D12Device::CreateUnorderedAccessView(ID3D12Resource
804805
return;
805806

806807
reshade::api::descriptor_set_update update;
807-
update.set = { DestDescriptor.ptr };
808+
update.set = convert_to_descriptor_set(DestDescriptor);
808809
update.binding = 0;
809810
update.array_offset = 0;
810811
update.type = reshade::api::descriptor_type::unordered_access_view;
@@ -873,7 +874,11 @@ void STDMETHODCALLTYPE D3D12Device::CreateSampler(const D3D12_SAMPLER_DESC *p
873874
pDesc = &internal_desc;
874875
}
875876

877+
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
876878
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
879+
#else
880+
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = DestDescriptor;
881+
#endif
877882
_orig->CreateSampler(pDesc, original_descriptor_handle);
878883

879884
const reshade::api::sampler descriptor_value = { original_descriptor_handle.ptr };
@@ -888,7 +893,7 @@ void STDMETHODCALLTYPE D3D12Device::CreateSampler(const D3D12_SAMPLER_DESC *p
888893
return;
889894

890895
reshade::api::descriptor_set_update update;
891-
update.set = { DestDescriptor.ptr };
896+
update.set = convert_to_descriptor_set(DestDescriptor);
892897
update.binding = 0;
893898
update.array_offset = 0;
894899
update.type = reshade::api::descriptor_type::sampler;
@@ -920,10 +925,10 @@ void STDMETHODCALLTYPE D3D12Device::CopyDescriptors(UINT NumDestDescriptorRan
920925
{
921926
const UINT src_count = (pSrcDescriptorRangeSizes != nullptr ? pSrcDescriptorRangeSizes[src_range] : 1);
922927

923-
copies[num_copies].dest_set = { pDestDescriptorRangeStarts[dst_range].ptr };
928+
copies[num_copies].dest_set = convert_to_descriptor_set(pDestDescriptorRangeStarts[dst_range]);
924929
copies[num_copies].dest_binding = 0;
925930
copies[num_copies].dest_array_offset = 0;
926-
copies[num_copies].source_set = { pSrcDescriptorRangeStarts[src_range].ptr };
931+
copies[num_copies].source_set = convert_to_descriptor_set(pSrcDescriptorRangeStarts[src_range]);
927932
copies[num_copies].source_binding = 0;
928933
copies[num_copies].source_array_offset = 0;
929934

@@ -975,8 +980,8 @@ void STDMETHODCALLTYPE D3D12Device::CopyDescriptorsSimple(UINT NumDescriptors
975980
reshade::has_addon_event<reshade::addon_event::copy_descriptor_sets>())
976981
{
977982
reshade::api::descriptor_set_copy copy;
978-
copy.dest_set = { DestDescriptorRangeStart.ptr };
979-
copy.source_set = { SrcDescriptorRangeStart.ptr };
983+
copy.dest_set = convert_to_descriptor_set(DestDescriptorRangeStart);
984+
copy.source_set = convert_to_descriptor_set(SrcDescriptorRangeStart);
980985
copy.count = NumDescriptors;
981986

982987
if (reshade::invoke_addon_event<reshade::addon_event::copy_descriptor_sets>(this, 1, &copy))
@@ -1872,7 +1877,7 @@ HRESULT STDMETHODCALLTYPE D3D12Device::CreatePlacedResource1(ID3D12Heap *pHeap,
18721877
void STDMETHODCALLTYPE D3D12Device::CreateSamplerFeedbackUnorderedAccessView(ID3D12Resource *pTargetedResource, ID3D12Resource *pFeedbackResource, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor)
18731878
{
18741879
assert(_interface_version >= 8);
1875-
#if RESHADE_ADDON
1880+
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
18761881
static_cast<ID3D12Device8 *>(_orig)->CreateSamplerFeedbackUnorderedAccessView(pTargetedResource, pFeedbackResource, convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
18771882
#else
18781883
static_cast<ID3D12Device8 *>(_orig)->CreateSamplerFeedbackUnorderedAccessView(pTargetedResource, pFeedbackResource, DestDescriptor);

0 commit comments

Comments
 (0)