Skip to content

Commit 53faa4f

Browse files
author
hongqingwan
committed
code clean
1 parent 073dcad commit 53faa4f

16 files changed

+156
-317
lines changed

libobs-d3d12/d3d12-command-context.cpp

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
/******************************************************************************
2-
Copyright (C) 2023 by Lain Bailey <[email protected]>
3-
4-
This program is free software: you can redistribute it and/or modify
5-
it under the terms of the GNU General Public License as published by
6-
the Free Software Foundation, either version 2 of the License, or
7-
(at your option) any later version.
8-
9-
This program is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
GNU General Public License for more details.
13-
14-
You should have received a copy of the GNU General Public License
15-
along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
******************************************************************************/
17-
181
#include "d3d12-command-context.hpp"
192

203
#include <util/base.h>
@@ -264,12 +247,30 @@ D3D12_CPU_DESCRIPTOR_HANDLE DescriptorAllocator::Allocate(uint32_t Count)
264247
m_DescriptorSize = m_DeviceInstance->GetDevice()->GetDescriptorHandleIncrementSize(m_Type);
265248
}
266249

250+
if (m_RemainingFreeHandles <= 0) {
251+
throw HRError("DescriptorAllocator: No remaining free handles");
252+
}
253+
267254
D3D12_CPU_DESCRIPTOR_HANDLE ret = m_CurrentHandle;
268255
m_CurrentHandle.ptr += Count * m_DescriptorSize;
269256
m_RemainingFreeHandles -= Count;
270257
return ret;
271258
}
272259

260+
size_t DescriptorAllocator::RemainingFreeCount() {
261+
return m_RemainingFreeHandles;
262+
}
263+
264+
void DescriptorAllocator::DiscardAll()
265+
{
266+
if (m_CurrentHeap == nullptr) {
267+
return;
268+
}
269+
270+
m_CurrentHandle = m_CurrentHeap->GetCPUDescriptorHandleForHeapStart();
271+
m_RemainingFreeHandles = sm_NumDescriptorsPerHeap;
272+
}
273+
273274
DescriptorHandle::DescriptorHandle()
274275
{
275276
m_CpuHandle.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
@@ -743,6 +744,19 @@ GpuBuffer::~GpuBuffer()
743744
Destroy();
744745
}
745746

747+
void GpuBuffer::Destroy()
748+
{
749+
if (m_UAV.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
750+
m_UAV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
751+
}
752+
753+
if (m_SRV.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
754+
m_SRV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
755+
}
756+
757+
GpuResource::Destroy();
758+
}
759+
746760
void GpuBuffer::Create(const std::wstring &name, uint32_t NumElements, uint32_t ElementSize, const void *initialData)
747761
{
748762
Destroy();
@@ -1544,6 +1558,11 @@ DepthBuffer::DepthBuffer(D3D12DeviceInstance *DeviceInstance, float ClearDepth,
15441558
m_hStencilSRV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
15451559
}
15461560

1561+
DepthBuffer::~DepthBuffer()
1562+
{
1563+
Destroy();
1564+
}
1565+
15471566
void DepthBuffer::Create(const std::wstring &Name, uint32_t Width, uint32_t Height, DXGI_FORMAT Format,
15481567
D3D12_GPU_VIRTUAL_ADDRESS VidMemPtr)
15491568
{
@@ -1563,6 +1582,35 @@ void DepthBuffer::Create(const std::wstring &Name, uint32_t Width, uint32_t Heig
15631582
CreateDerivedViews(m_DeviceInstance->GetDevice());
15641583
}
15651584

1585+
void DepthBuffer::Destroy()
1586+
{
1587+
if (m_hDSV[0].ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1588+
m_hDSV[0].ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1589+
}
1590+
1591+
if (m_hDSV[1].ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1592+
m_hDSV[1].ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1593+
}
1594+
1595+
if (m_hDSV[2].ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1596+
m_hDSV[2].ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1597+
}
1598+
1599+
if (m_hDSV[3].ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1600+
m_hDSV[3].ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1601+
}
1602+
1603+
if (m_hDepthSRV.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1604+
m_hDepthSRV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1605+
}
1606+
1607+
if (m_hStencilSRV.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1608+
m_hStencilSRV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1609+
}
1610+
1611+
GpuResource::Destroy();
1612+
}
1613+
15661614
const D3D12_CPU_DESCRIPTOR_HANDLE &DepthBuffer::GetDSV() const
15671615
{
15681616
return m_hDSV[0];
@@ -3625,6 +3673,7 @@ void EngineProfiling::DisplayFrameRate()
36253673

36263674
SamplerDesc::SamplerDesc(D3D12DeviceInstance *DeviceInstance) : m_DeviceInstance(DeviceInstance)
36273675
{
3676+
Sampler.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
36283677
Filter = D3D12_FILTER_ANISOTROPIC;
36293678
AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
36303679
AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
@@ -3640,6 +3689,13 @@ SamplerDesc::SamplerDesc(D3D12DeviceInstance *DeviceInstance) : m_DeviceInstance
36403689
MaxLOD = D3D12_FLOAT32_MAX;
36413690
}
36423691

3692+
SamplerDesc::~SamplerDesc()
3693+
{
3694+
if (Sampler.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
3695+
Sampler.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
3696+
}
3697+
}
3698+
36433699
void SamplerDesc::SetTextureAddressMode(D3D12_TEXTURE_ADDRESS_MODE AddressMode)
36443700
{
36453701
AddressU = AddressMode;
@@ -3655,17 +3711,10 @@ void SamplerDesc::SetBorderColor(Color Border)
36553711
BorderColor[3] = Border.w;
36563712
}
36573713

3658-
D3D12_CPU_DESCRIPTOR_HANDLE SamplerDesc::CreateDescriptor(void)
3714+
void SamplerDesc::CreateDescriptor(void)
36593715
{
3660-
size_t hashValue = Utility::HashState(this);
3661-
auto iter = m_DeviceInstance->GetSamplerCache().find(hashValue);
3662-
if (iter != m_DeviceInstance->GetSamplerCache().end()) {
3663-
return iter->second;
3664-
}
3665-
3666-
D3D12_CPU_DESCRIPTOR_HANDLE Handle = m_DeviceInstance->AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
3667-
m_DeviceInstance->GetDevice()->CreateSampler(this, Handle);
3668-
return Handle;
3716+
Sampler = m_DeviceInstance->AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
3717+
m_DeviceInstance->GetDevice()->CreateSampler(this, Sampler);
36693718
}
36703719

36713720
void SamplerDesc::CreateDescriptor(D3D12_CPU_DESCRIPTOR_HANDLE Handle)
@@ -3862,11 +3911,6 @@ DescriptorAllocator *D3D12DeviceInstance::GetDescriptorAllocator()
38623911
return m_DescriptorAllocator;
38633912
}
38643913

3865-
std::map<size_t, D3D12_CPU_DESCRIPTOR_HANDLE> &D3D12DeviceInstance::GetSamplerCache()
3866-
{
3867-
return m_SamplerCache;
3868-
}
3869-
38703914
ID3D12DescriptorHeap *D3D12DeviceInstance::RequestCommonHeap(D3D12_DESCRIPTOR_HEAP_TYPE Type)
38713915
{
38723916
D3D12_DESCRIPTOR_HEAP_DESC Desc;
@@ -3930,6 +3974,11 @@ D3D12_CPU_DESCRIPTOR_HANDLE D3D12DeviceInstance::AllocateDescriptor(D3D12_DESCRI
39303974
return m_DescriptorAllocator[Type].Allocate(Count);
39313975
}
39323976

3977+
void D3D12DeviceInstance::DiscardDescriptorAll(D3D12_DESCRIPTOR_HEAP_TYPE Type, UINT Count)
3978+
{
3979+
m_DescriptorAllocator[Type].DiscardAll();
3980+
}
3981+
39333982
GraphicsContext *D3D12DeviceInstance::GetNewGraphicsContext(const std::wstring &ID)
39343983
{
39353984
CommandContext *NewContext = m_ContextManager->AllocateContext(D3D12_COMMAND_LIST_TYPE_DIRECT);

libobs-d3d12/d3d12-command-context.hpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ class DescriptorAllocator {
591591
public:
592592
DescriptorAllocator(D3D12DeviceInstance *DeviceInstance, D3D12_DESCRIPTOR_HEAP_TYPE Type);
593593
D3D12_CPU_DESCRIPTOR_HANDLE Allocate(uint32_t Count);
594+
void DiscardAll();
595+
size_t RemainingFreeCount();
594596

595597
protected:
596598
D3D12DeviceInstance *m_DeviceInstance = nullptr;
@@ -810,6 +812,8 @@ class GpuBuffer : public GpuResource {
810812
GpuBuffer(D3D12DeviceInstance *DeviceInstance);
811813
virtual ~GpuBuffer();
812814

815+
virtual void Destroy() override;
816+
813817
// Create a buffer. If initial data is provided, it will be copied into the buffer using the default command context.
814818
void Create(const std::wstring &name, uint32_t NumElements, uint32_t ElementSize,
815819
const void *initialData = nullptr);
@@ -944,6 +948,7 @@ struct Color {
944948
class DepthBuffer : public PixelBuffer {
945949
public:
946950
DepthBuffer(D3D12DeviceInstance *DeviceInstance, float ClearDepth = 0.0f, uint8_t ClearStencil = 0);
951+
virtual ~DepthBuffer();
947952
// Create a depth buffer. If an address is supplied, memory will not be allocated.
948953
// The vmem address allows you to alias buffers (which can be especially useful for
949954
// reusing ESRAM across a frame.)
@@ -952,6 +957,7 @@ class DepthBuffer : public PixelBuffer {
952957

953958
void Create(const std::wstring &Name, uint32_t Width, uint32_t Height, uint32_t NumSamples, DXGI_FORMAT Format,
954959
D3D12_GPU_VIRTUAL_ADDRESS VidMemPtr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN);
960+
virtual void Destroy() override;
955961

956962
// Get pre-created CPU-visible descriptor handles
957963
const D3D12_CPU_DESCRIPTOR_HANDLE &GetDSV() const;
@@ -1332,6 +1338,10 @@ class CommandContext : NonCopyable {
13321338
D3D12_COMMAND_LIST_TYPE m_Type;
13331339
};
13341340

1341+
class GraphicsCopyContext {
1342+
1343+
};
1344+
13351345
class GraphicsContext : public CommandContext {
13361346
public:
13371347
void ClearUAV(GpuBuffer &Target);
@@ -1410,8 +1420,6 @@ class GraphicsContext : public CommandContext {
14101420

14111421
class ComputeContext : public CommandContext {
14121422
public:
1413-
static ComputeContext &Begin(const std::wstring &ID = L"", bool Async = false);
1414-
14151423
void ClearUAV(GpuBuffer &Target);
14161424

14171425
void SetRootSignature(const RootSignature &RootSig);
@@ -1538,13 +1546,15 @@ class EngineProfiling {
15381546
class SamplerDesc : public D3D12_SAMPLER_DESC {
15391547
public:
15401548
SamplerDesc(D3D12DeviceInstance *DeviceInstance);
1549+
~SamplerDesc();
15411550
void SetTextureAddressMode(D3D12_TEXTURE_ADDRESS_MODE AddressMode);
15421551
void SetBorderColor(Color Border);
15431552

1544-
D3D12_CPU_DESCRIPTOR_HANDLE CreateDescriptor(void);
1553+
void CreateDescriptor(void);
15451554
void CreateDescriptor(D3D12_CPU_DESCRIPTOR_HANDLE Handle);
15461555

15471556
D3D12DeviceInstance *m_DeviceInstance = nullptr;
1557+
D3D12_CPU_DESCRIPTOR_HANDLE Sampler;
15481558
};
15491559

15501560
struct MonitorColorInfo {
@@ -1589,14 +1599,15 @@ class D3D12DeviceInstance {
15891599
CommandSignature &GetDispatchIndirectCommandSignature();
15901600
CommandSignature &GetDrawIndirectCommandSignature();
15911601
DescriptorAllocator *GetDescriptorAllocator();
1592-
std::map<size_t, D3D12_CPU_DESCRIPTOR_HANDLE> &GetSamplerCache();
15931602

15941603
DescriptorAllocator m_DescriptorAllocator[D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES] = {
15951604
{this, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV},
15961605
{this, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER},
15971606
{this, D3D12_DESCRIPTOR_HEAP_TYPE_RTV},
15981607
{this, D3D12_DESCRIPTOR_HEAP_TYPE_DSV}};
15991608
D3D12_CPU_DESCRIPTOR_HANDLE AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE Type, UINT Count = 1);
1609+
void DiscardDescriptorAll(D3D12_DESCRIPTOR_HEAP_TYPE Type, UINT Count = 1);
1610+
16001611
ID3D12DescriptorHeap *RequestCommonHeap(D3D12_DESCRIPTOR_HEAP_TYPE Type);
16011612

16021613
ID3D12DescriptorHeap *RequestDynamicDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE HeapType);
@@ -1668,8 +1679,6 @@ class D3D12DeviceInstance {
16681679
std::map<size_t, ComPtr<ID3D12PipelineState>> m_GraphicsPSOHashMap;
16691680
std::map<size_t, ComPtr<ID3D12PipelineState>> m_ComputePSOHashMap;
16701681

1671-
std::map<size_t, D3D12_CPU_DESCRIPTOR_HANDLE> m_SamplerCache;
1672-
16731682
bool m_TypedUAVLoadSupport_R11G11B10_FLOAT = false;
16741683
bool m_TypedUAVLoadSupport_R16G16B16A16_FLOAT = false;
16751684
bool m_NV12Supported = false;

libobs-d3d12/d3d12-duplicator.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
/******************************************************************************
2-
Copyright (C) 2023 by Lain Bailey <[email protected]>
3-
4-
This program is free software: you can redistribute it and/or modify
5-
it under the terms of the GNU General Public License as published by
6-
the Free Software Foundation, either version 2 of the License, or
7-
(at your option) any later version.
8-
9-
This program is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
GNU General Public License for more details.
13-
14-
You should have received a copy of the GNU General Public License
15-
along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
******************************************************************************/
17-
181
#include "d3d12-subsystem.hpp"
192
#include <unordered_map>
203

libobs-d3d12/d3d12-indexbuffer.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
/******************************************************************************
2-
Copyright (C) 2023 by Lain Bailey <[email protected]>
3-
4-
This program is free software: you can redistribute it and/or modify
5-
it under the terms of the GNU General Public License as published by
6-
the Free Software Foundation, either version 2 of the License, or
7-
(at your option) any later version.
8-
9-
This program is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
GNU General Public License for more details.
13-
14-
You should have received a copy of the GNU General Public License
15-
along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
******************************************************************************/
17-
181
#include "d3d12-subsystem.hpp"
192

203
void gs_index_buffer::InitBuffer()

libobs-d3d12/d3d12-samplerstate.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
/******************************************************************************
2-
Copyright (C) 2023 by Lain Bailey <[email protected]>
3-
4-
This program is free software: you can redistribute it and/or modify
5-
it under the terms of the GNU General Public License as published by
6-
the Free Software Foundation, either version 2 of the License, or
7-
(at your option) any later version.
8-
9-
This program is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
GNU General Public License for more details.
13-
14-
You should have received a copy of the GNU General Public License
15-
along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
******************************************************************************/
17-
181
#include <graphics/vec4.h>
192
#include <float.h>
203

@@ -83,5 +66,5 @@ gs_sampler_state::gs_sampler_state(gs_device_t *device, const gs_sampler_info *i
8366
sampleDesc.AddressW = ConvertGSAddressMode(info->address_w);
8467
sampleDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
8568
sampleDesc.MaxAnisotropy = 0;
86-
sampler = sampleDesc.CreateDescriptor();
69+
sampleDesc.CreateDescriptor();
8770
}

libobs-d3d12/d3d12-shader.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
/******************************************************************************
2-
Copyright (C) 2023 by Lain Bailey <[email protected]>
3-
4-
This program is free software: you can redistribute it and/or modify
5-
it under the terms of the GNU General Public License as published by
6-
the Free Software Foundation, either version 2 of the License, or
7-
(at your option) any later version.
8-
9-
This program is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
GNU General Public License for more details.
13-
14-
You should have received a copy of the GNU General Public License
15-
along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
******************************************************************************/
17-
181
#include "d3d12-subsystem.hpp"
192
#include "d3d12-shaderprocessor.hpp"
203
#include <graphics/vec2.h>
@@ -253,7 +236,8 @@ inline void gs_shader::UpdateParam(std::vector<uint8_t> &constData, gs_shader_pa
253236
device_load_texture(device, shader_tex.tex, param.textureID);
254237

255238
if (param.nextSampler) {
256-
device->context->SetDynamicSampler(samplerRootParameterIndex, 0, param.nextSampler->sampler);
239+
device->context->SetDynamicSampler(samplerRootParameterIndex, 0,
240+
param.nextSampler->sampleDesc.Sampler);
257241
param.nextSampler = nullptr;
258242
}
259243
}

0 commit comments

Comments
 (0)