Debian Codegraph @

Source/Core/VideoBackends/D3D12/DX12Texture.h

raw symbols SCIP
1 // Copyright 2019 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <memory>
7 #include <string>
8 #include <string_view>
9
10 #include "Common/CommonTypes.h"
11 #include "VideoBackends/D3D12/Common.h"
12 #include "VideoBackends/D3D12/DescriptorHeapManager.h"
13 #include "VideoCommon/AbstractFramebuffer.h"
14 #include "VideoCommon/AbstractGfx.h"
15 #include "VideoCommon/AbstractStagingTexture.h"
16 #include "VideoCommon/AbstractTexture.h"
17
18 namespace DX12
19 {
20 class DXTexture final : public AbstractTexture
21 {
22 public:
23 ~DXTexture() override;
24
25 static std::unique_ptr<DXTexture> Create(const TextureConfig& config, std::string_view name);
26 static std::unique_ptr<DXTexture> CreateAdopted(ID3D12Resource* resource);
27
28 void Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer, size_t buffer_size,
29 u32 layer) override;
30 void CopyRectangleFromTexture(const AbstractTexture* src,
31 const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
32 u32 src_level, const MathUtil::Rectangle<int>& dst_rect,
33 u32 dst_layer, u32 dst_level) override;
34 void ResolveFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& rect,
35 u32 layer, u32 level) override;
36 void FinishedRendering() override;
37
38 ID3D12Resource* GetResource() const { return m_resource.Get(); }
39 const DescriptorHandle& GetSRVDescriptor() const { return m_srv_descriptor; }
40 const DescriptorHandle& GetUAVDescriptor() const { return m_uav_descriptor; }
41 D3D12_RESOURCE_STATES GetState() const { return m_state; }
42 u32 CalcSubresource(u32 level, u32 layer) const { return level + layer * m_config.levels; }
43
44 void TransitionToState(D3D12_RESOURCE_STATES state) const;
45
46 // Destroys the resource backing this texture. The resource must not be in use by the GPU.
47 void DestroyResource();
48
49 private:
50 DXTexture(const TextureConfig& config, ID3D12Resource* resource, D3D12_RESOURCE_STATES state,
51 std::string_view name);
52
53 bool CreateSRVDescriptor();
54 bool CreateUAVDescriptor();
55
56 ComPtr<ID3D12Resource> m_resource;
57 DescriptorHandle m_srv_descriptor = {};
58 DescriptorHandle m_uav_descriptor = {};
59
60 std::wstring m_name;
61
62 mutable D3D12_RESOURCE_STATES m_state;
63 };
64
65 class DXFramebuffer final : public AbstractFramebuffer
66 {
67 public:
68 ~DXFramebuffer() override;
69
70 const DescriptorHandle& GetIntRTVDescriptor() const { return m_int_rtv_descriptor; }
71 const DescriptorHandle& GetDSVDescriptor() const { return m_dsv_descriptor; }
72
73 UINT GetRTVDescriptorCount() const { return static_cast<UINT>(m_render_targets.size()); }
74 const D3D12_CPU_DESCRIPTOR_HANDLE* GetRTVDescriptorArray() const
75 {
76 return m_render_targets_raw.data();
77 }
78 const D3D12_CPU_DESCRIPTOR_HANDLE* GetIntRTVDescriptorArray() const;
79 const D3D12_CPU_DESCRIPTOR_HANDLE* GetDSVDescriptorArray() const
80 {
81 return m_depth_attachment ? &m_dsv_descriptor.cpu_handle : nullptr;
82 }
83
84 void Unbind();
85 void ClearRenderTargets(const ClearColor& color_value, const D3D12_RECT* rectangle) const;
86 void ClearDepth(float depth_value, const D3D12_RECT* rectangle) const;
87 void TransitionRenderTargets() const;
88
89 static std::unique_ptr<DXFramebuffer>
90 Create(DXTexture* color_attachment, DXTexture* depth_attachment,
91 std::vector<AbstractTexture*> additional_color_attachments);
92
93 private:
94 DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
95 std::vector<AbstractTexture*> additional_color_attachments,
96 AbstractTextureFormat color_format, AbstractTextureFormat depth_format, u32 width,
97 u32 height, u32 layers, u32 samples);
98
99 bool CreateRTVDescriptors();
100 bool CreateRTVDescriptor(u32 layers, AbstractTexture* attachment);
101 bool CreateIRTVDescriptor();
102 bool CreateDSVDescriptor();
103
104 std::vector<DescriptorHandle> m_render_targets;
105 std::vector<D3D12_CPU_DESCRIPTOR_HANDLE> m_render_targets_raw;
106 DescriptorHandle m_int_rtv_descriptor = {};
107 DescriptorHandle m_dsv_descriptor = {};
108 };
109
110 class DXStagingTexture final : public AbstractStagingTexture
111 {
112 public:
113 ~DXStagingTexture() override;
114
115 void CopyFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& src_rect,
116 u32 src_layer, u32 src_level,
117 const MathUtil::Rectangle<int>& dst_rect) override;
118 void CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst,
119 const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer,
120 u32 dst_level) override;
121
122 bool Map() override;
123 void Unmap() override;
124 void Flush() override;
125
126 static std::unique_ptr<DXStagingTexture> Create(StagingTextureType type,
127 const TextureConfig& config);
128
129 private:
130 DXStagingTexture(StagingTextureType type, const TextureConfig& config, ID3D12Resource* resource,
131 u32 stride, u32 buffer_size);
132
133 ComPtr<ID3D12Resource> m_resource;
134 u64 m_completed_fence = 0;
135 u32 m_buffer_size;
136 };
137
138 } // namespace DX12