Skip to content

Commit

Permalink
Rework D3D12 descriptor heap management again to leave GPU handles un…
Browse files Browse the repository at this point in the history
…touched

This is necessary since with D3D12 raytracing one can put descriptor handles into shader binding tables, which ReShade cannot easily fix up
  • Loading branch information
crosire committed Jan 29, 2022
1 parent 792dd45 commit 2d659b6
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 194 deletions.
50 changes: 20 additions & 30 deletions source/d3d12/d3d12_command_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,13 @@ void STDMETHODCALLTYPE D3D12GraphicsCommandList::SetGraphicsRootSignature(ID3D12
}
void STDMETHODCALLTYPE D3D12GraphicsCommandList::SetComputeRootDescriptorTable(UINT RootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE BaseDescriptor)
{
#if RESHADE_ADDON
_orig->SetComputeRootDescriptorTable(RootParameterIndex, _device->convert_to_original_gpu_descriptor_handle(BaseDescriptor));
#else
_orig->SetComputeRootDescriptorTable(RootParameterIndex, BaseDescriptor);
#endif

#if RESHADE_ADDON && !RESHADE_ADDON_LITE
const reshade::api::descriptor_set set = { BaseDescriptor.ptr };
if (!reshade::has_addon_event<reshade::addon_event::bind_descriptor_sets>())
return;

const reshade::api::descriptor_set set = _device_impl->convert_to_descriptor_set(BaseDescriptor);
reshade::invoke_addon_event<reshade::addon_event::bind_descriptor_sets>(
this,
reshade::api::shader_stage::all_compute,
Expand All @@ -467,14 +466,13 @@ void STDMETHODCALLTYPE D3D12GraphicsCommandList::SetComputeRootDescriptorTable(U
}
void STDMETHODCALLTYPE D3D12GraphicsCommandList::SetGraphicsRootDescriptorTable(UINT RootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE BaseDescriptor)
{
#if RESHADE_ADDON
_orig->SetGraphicsRootDescriptorTable(RootParameterIndex, _device->convert_to_original_gpu_descriptor_handle(BaseDescriptor));
#else
_orig->SetGraphicsRootDescriptorTable(RootParameterIndex, BaseDescriptor);
#endif

#if RESHADE_ADDON && !RESHADE_ADDON_LITE
const reshade::api::descriptor_set set = { BaseDescriptor.ptr };
if (!reshade::has_addon_event<reshade::addon_event::bind_descriptor_sets>())
return;

const reshade::api::descriptor_set set = _device_impl->convert_to_descriptor_set(BaseDescriptor);
reshade::invoke_addon_event<reshade::addon_event::bind_descriptor_sets>(
this,
reshade::api::shader_stage::all_graphics,
Expand Down Expand Up @@ -702,35 +700,27 @@ void STDMETHODCALLTYPE D3D12GraphicsCommandList::ClearRenderTargetView(D3D12_CPU
}
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)
{
#if RESHADE_ADDON
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = _device_impl->convert_to_original_cpu_descriptor_handle(ViewCPUHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
ViewCPUHandle = _device_impl->convert_to_original_cpu_descriptor_handle(ViewCPUHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
#endif

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)))
#if RESHADE_ADDON
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)))
return;

_orig->ClearUnorderedAccessViewUint(
_device_impl->convert_to_original_gpu_descriptor_handle(ViewGPUHandleInCurrentHeap),
original_descriptor_handle,
pResource, Values, NumRects, pRects);
#else
_orig->ClearUnorderedAccessViewUint(ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects);
#endif
_orig->ClearUnorderedAccessViewUint(ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects);
}
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)
{
#if RESHADE_ADDON
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = _device_impl->convert_to_original_cpu_descriptor_handle(ViewCPUHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
ViewCPUHandle = _device_impl->convert_to_original_cpu_descriptor_handle(ViewCPUHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
#endif

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)))
#if RESHADE_ADDON
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)))
return;

_orig->ClearUnorderedAccessViewFloat(
_device_impl->convert_to_original_gpu_descriptor_handle(ViewGPUHandleInCurrentHeap),
original_descriptor_handle,
pResource, Values, NumRects, pRects);
#else
_orig->ClearUnorderedAccessViewFloat(ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects);
#endif
_orig->ClearUnorderedAccessViewFloat(ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects);
}
void STDMETHODCALLTYPE D3D12GraphicsCommandList::DiscardResource(ID3D12Resource *pResource, const D3D12_DISCARD_REGION *pRegion)
{
Expand Down
35 changes: 3 additions & 32 deletions source/d3d12/d3d12_descriptor_heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* License: https://github.com/crosire/reshade#license
*/

#if RESHADE_ADDON
#if RESHADE_ADDON && !RESHADE_ADDON_LITE

#include "d3d12_device.hpp"
#include "d3d12_descriptor_heap.hpp"
#include "dll_log.hpp"

D3D12DescriptorHeap::D3D12DescriptorHeap(D3D12Device *device, ID3D12DescriptorHeap *original) :
D3D12DescriptorHeap::D3D12DescriptorHeap(ID3D12Device *device, ID3D12DescriptorHeap *original) :
_orig(original),
_device(device)
{
Expand Down Expand Up @@ -102,36 +102,7 @@ D3D12_CPU_DESCRIPTOR_HANDLE STDMETHODCALLTYPE D3D12DescriptorHeap::GetCPUDescrip
}
D3D12_GPU_DESCRIPTOR_HANDLE STDMETHODCALLTYPE D3D12DescriptorHeap::GetGPUDescriptorHandleForHeapStart()
{
return _internal_base_gpu_handle;
}

void D3D12DescriptorHeap::initialize_descriptor_base_handle(size_t heap_index)
{
// Generate a descriptor handle of the following format:
// Bit 0 - 19: Descriptor Index
// Bit 20 - 22: Heap Type
// Bit 23 - 23: Heap Flags
// Bit 24 - 55: Heap Index

_orig_base_cpu_handle = _orig->GetCPUDescriptorHandleForHeapStart();
_internal_base_cpu_handle = { 0 };

static_assert(D3D12_MAX_SHADER_VISIBLE_DESCRIPTOR_HEAP_SIZE_TIER_2 < (1 << 20));

assert(heap_index < (1ull << std::min(sizeof(SIZE_T) * 8 - 24ull, 32ull)));
_internal_base_cpu_handle.ptr |= static_cast<SIZE_T>(heap_index) << 24;

const D3D12_DESCRIPTOR_HEAP_DESC heap_desc = _orig->GetDesc();
assert(heap_desc.Type <= 0x3);
_internal_base_cpu_handle.ptr |= heap_desc.Type << 20;
assert(heap_desc.Flags <= 0x1);
_internal_base_cpu_handle.ptr |= heap_desc.Flags << 23;

if (heap_desc.Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)
{
_orig_base_gpu_handle = _orig->GetGPUDescriptorHandleForHeapStart();
_internal_base_gpu_handle.ptr = _internal_base_cpu_handle.ptr;
}
return _orig->GetGPUDescriptorHandleForHeapStart();
}

#endif
11 changes: 6 additions & 5 deletions source/d3d12/d3d12_descriptor_heap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

#pragma once

#if RESHADE_ADDON
#if RESHADE_ADDON && !RESHADE_ADDON_LITE

#include <d3d12.h>

struct D3D12Device;

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

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

ID3D12DescriptorHeap *_orig;
ULONG _ref = 1;
D3D12Device *const _device;
D3D12_CPU_DESCRIPTOR_HANDLE _internal_base_cpu_handle = { 0 }, _orig_base_cpu_handle = { 0 };
D3D12_GPU_DESCRIPTOR_HANDLE _internal_base_gpu_handle = { 0 }, _orig_base_gpu_handle = { 0 };
ID3D12Device *const _device;
D3D12_CPU_DESCRIPTOR_HANDLE _orig_base_cpu_handle = { 0 };
D3D12_GPU_DESCRIPTOR_HANDLE _orig_base_gpu_handle = { 0 };
D3D12_CPU_DESCRIPTOR_HANDLE _internal_base_cpu_handle = { 0 };
};

#endif
45 changes: 25 additions & 20 deletions source/d3d12/d3d12_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,6 @@ HRESULT STDMETHODCALLTYPE D3D12Device::CreateDescriptorHeap(const D3D12_DESCRIPT
}
UINT STDMETHODCALLTYPE D3D12Device::GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE DescriptorHeapType)
{
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
if (DescriptorHeapType <= D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER)
return 0x1; // See 'D3D12DescriptorHeap::initialize_descriptor_base_handle'
#endif

return _orig->GetDescriptorHandleIncrementSize(DescriptorHeapType);
}
HRESULT STDMETHODCALLTYPE D3D12Device::CreateRootSignature(UINT nodeMask, const void *pBlobWithRootSignature, SIZE_T blobLengthInBytes, REFIID riid, void **ppvRootSignature)
Expand Down Expand Up @@ -708,13 +703,9 @@ HRESULT STDMETHODCALLTYPE D3D12Device::CreateRootSignature(UINT nodeMask, const
}
void STDMETHODCALLTYPE D3D12Device::CreateConstantBufferView(const D3D12_CONSTANT_BUFFER_VIEW_DESC *pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor)
{
#if RESHADE_ADDON
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
_orig->CreateConstantBufferView(pDesc, convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
#else
_orig->CreateConstantBufferView(pDesc, DestDescriptor);
#endif

#if RESHADE_ADDON && !RESHADE_ADDON_LITE
if (!reshade::has_addon_event<reshade::addon_event::update_descriptor_sets>())
return;

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

reshade::api::descriptor_set_update update;
update.set = { DestDescriptor.ptr };
update.set = convert_to_descriptor_set(DestDescriptor);
update.binding = 0;
update.array_offset = 0;
update.type = reshade::api::descriptor_type::constant_buffer;
update.count = 1;
update.descriptors = &buffer_range;

reshade::invoke_addon_event<reshade::addon_event::update_descriptor_sets>(this, 1, &update);
#else
_orig->CreateConstantBufferView(pDesc, DestDescriptor);
#endif
}
void STDMETHODCALLTYPE D3D12Device::CreateShaderResourceView(ID3D12Resource *pResource, const D3D12_SHADER_RESOURCE_VIEW_DESC *pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor)
Expand All @@ -748,7 +741,11 @@ void STDMETHODCALLTYPE D3D12Device::CreateShaderResourceView(ID3D12Resource *
pDesc = &internal_desc;
}

#if RESHADE_ADDON && !RESHADE_ADDON_LITE
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
#else
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = DestDescriptor;
#endif
_orig->CreateShaderResourceView(pResource, pDesc, original_descriptor_handle);

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

reshade::api::descriptor_set_update update;
update.set = { DestDescriptor.ptr };
update.set = convert_to_descriptor_set(DestDescriptor);
update.binding = 0;
update.array_offset = 0;
update.type = reshade::api::descriptor_type::shader_resource_view;
Expand All @@ -788,8 +785,12 @@ void STDMETHODCALLTYPE D3D12Device::CreateUnorderedAccessView(ID3D12Resource
pDesc = &internal_desc;
}

#if RESHADE_ADDON && !RESHADE_ADDON_LITE
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
_orig->CreateUnorderedAccessView(pResource, pCounterResource, pDesc, convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
#else
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = DestDescriptor;
#endif
_orig->CreateUnorderedAccessView(pResource, pCounterResource, pDesc, original_descriptor_handle);

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

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

reshade::api::descriptor_set_update update;
update.set = { DestDescriptor.ptr };
update.set = convert_to_descriptor_set(DestDescriptor);
update.binding = 0;
update.array_offset = 0;
update.type = reshade::api::descriptor_type::unordered_access_view;
Expand Down Expand Up @@ -873,7 +874,11 @@ void STDMETHODCALLTYPE D3D12Device::CreateSampler(const D3D12_SAMPLER_DESC *p
pDesc = &internal_desc;
}

#if RESHADE_ADDON && !RESHADE_ADDON_LITE
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
#else
const D3D12_CPU_DESCRIPTOR_HANDLE original_descriptor_handle = DestDescriptor;
#endif
_orig->CreateSampler(pDesc, original_descriptor_handle);

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

reshade::api::descriptor_set_update update;
update.set = { DestDescriptor.ptr };
update.set = convert_to_descriptor_set(DestDescriptor);
update.binding = 0;
update.array_offset = 0;
update.type = reshade::api::descriptor_type::sampler;
Expand Down Expand Up @@ -920,10 +925,10 @@ void STDMETHODCALLTYPE D3D12Device::CopyDescriptors(UINT NumDestDescriptorRan
{
const UINT src_count = (pSrcDescriptorRangeSizes != nullptr ? pSrcDescriptorRangeSizes[src_range] : 1);

copies[num_copies].dest_set = { pDestDescriptorRangeStarts[dst_range].ptr };
copies[num_copies].dest_set = convert_to_descriptor_set(pDestDescriptorRangeStarts[dst_range]);
copies[num_copies].dest_binding = 0;
copies[num_copies].dest_array_offset = 0;
copies[num_copies].source_set = { pSrcDescriptorRangeStarts[src_range].ptr };
copies[num_copies].source_set = convert_to_descriptor_set(pSrcDescriptorRangeStarts[src_range]);
copies[num_copies].source_binding = 0;
copies[num_copies].source_array_offset = 0;

Expand Down Expand Up @@ -975,8 +980,8 @@ void STDMETHODCALLTYPE D3D12Device::CopyDescriptorsSimple(UINT NumDescriptors
reshade::has_addon_event<reshade::addon_event::copy_descriptor_sets>())
{
reshade::api::descriptor_set_copy copy;
copy.dest_set = { DestDescriptorRangeStart.ptr };
copy.source_set = { SrcDescriptorRangeStart.ptr };
copy.dest_set = convert_to_descriptor_set(DestDescriptorRangeStart);
copy.source_set = convert_to_descriptor_set(SrcDescriptorRangeStart);
copy.count = NumDescriptors;

if (reshade::invoke_addon_event<reshade::addon_event::copy_descriptor_sets>(this, 1, &copy))
Expand Down Expand Up @@ -1872,7 +1877,7 @@ HRESULT STDMETHODCALLTYPE D3D12Device::CreatePlacedResource1(ID3D12Heap *pHeap,
void STDMETHODCALLTYPE D3D12Device::CreateSamplerFeedbackUnorderedAccessView(ID3D12Resource *pTargetedResource, ID3D12Resource *pFeedbackResource, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor)
{
assert(_interface_version >= 8);
#if RESHADE_ADDON
#if RESHADE_ADDON && !RESHADE_ADDON_LITE
static_cast<ID3D12Device8 *>(_orig)->CreateSamplerFeedbackUnorderedAccessView(pTargetedResource, pFeedbackResource, convert_to_original_cpu_descriptor_handle(DestDescriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
#else
static_cast<ID3D12Device8 *>(_orig)->CreateSamplerFeedbackUnorderedAccessView(pTargetedResource, pFeedbackResource, DestDescriptor);
Expand Down
Loading

0 comments on commit 2d659b6

Please sign in to comment.