Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/util/d3d11_texture.h
4214 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
6
#include "d3d11_stream_buffer.h"
7
#include "gpu_device.h"
8
9
#include "common/windows_headers.h"
10
11
#include <d3d11_1.h>
12
#include <wrl/client.h>
13
14
class D3D11Device;
15
16
class D3D11Sampler final : public GPUSampler
17
{
18
friend D3D11Device;
19
20
template<typename T>
21
using ComPtr = Microsoft::WRL::ComPtr<T>;
22
23
public:
24
~D3D11Sampler() override;
25
26
ALWAYS_INLINE ID3D11SamplerState* GetSamplerState() const { return m_ss.Get(); }
27
ALWAYS_INLINE ID3D11SamplerState* const* GetSamplerStateArray() const { return m_ss.GetAddressOf(); }
28
29
#ifdef ENABLE_GPU_OBJECT_NAMES
30
void SetDebugName(std::string_view name) override;
31
#endif
32
33
private:
34
D3D11Sampler(ComPtr<ID3D11SamplerState> ss);
35
36
ComPtr<ID3D11SamplerState> m_ss;
37
};
38
39
class D3D11Texture final : public GPUTexture
40
{
41
friend D3D11Device;
42
43
public:
44
template<typename T>
45
using ComPtr = Microsoft::WRL::ComPtr<T>;
46
47
~D3D11Texture();
48
49
ALWAYS_INLINE ID3D11Texture2D* GetD3DTexture() const { return m_texture.Get(); }
50
ALWAYS_INLINE ID3D11ShaderResourceView* GetD3DSRV() const { return m_srv.Get(); }
51
ALWAYS_INLINE ID3D11View* GetRTVOrDSV() const { return m_rtv_dsv.Get(); }
52
ALWAYS_INLINE ID3D11RenderTargetView* GetD3DRTV() const
53
{
54
return static_cast<ID3D11RenderTargetView*>(m_rtv_dsv.Get());
55
}
56
ALWAYS_INLINE ID3D11DepthStencilView* GetD3DDSV() const
57
{
58
return static_cast<ID3D11DepthStencilView*>(m_rtv_dsv.Get());
59
}
60
ALWAYS_INLINE ID3D11ShaderResourceView* const* GetD3DSRVArray() const { return m_srv.GetAddressOf(); }
61
ALWAYS_INLINE ID3D11RenderTargetView* const* GetD3DRTVArray() const
62
{
63
return reinterpret_cast<ID3D11RenderTargetView* const*>(m_rtv_dsv.GetAddressOf());
64
}
65
ALWAYS_INLINE ID3D11UnorderedAccessView* GetD3DUAV() const { return m_uav.Get(); }
66
DXGI_FORMAT GetDXGIFormat() const;
67
68
ALWAYS_INLINE operator ID3D11Texture2D*() const { return m_texture.Get(); }
69
ALWAYS_INLINE operator ID3D11ShaderResourceView*() const { return m_srv.Get(); }
70
ALWAYS_INLINE operator ID3D11RenderTargetView*() const
71
{
72
return static_cast<ID3D11RenderTargetView*>(m_rtv_dsv.Get());
73
}
74
ALWAYS_INLINE operator ID3D11DepthStencilView*() const
75
{
76
return static_cast<ID3D11DepthStencilView*>(m_rtv_dsv.Get());
77
}
78
ALWAYS_INLINE operator ID3D11UnorderedAccessView*() const { return m_uav.Get(); }
79
ALWAYS_INLINE operator bool() const { return static_cast<bool>(m_texture); }
80
81
static std::unique_ptr<D3D11Texture> Create(ID3D11Device* device, u32 width, u32 height, u32 layers, u32 levels,
82
u32 samples, Type type, Format format, Flags flags,
83
const void* initial_data, u32 initial_data_stride, Error* error);
84
85
D3D11_TEXTURE2D_DESC GetDesc() const;
86
void CommitClear(ID3D11DeviceContext1* context);
87
88
bool Update(u32 x, u32 y, u32 width, u32 height, const void* data, u32 pitch, u32 layer = 0, u32 level = 0) override;
89
bool Map(void** map, u32* map_stride, u32 x, u32 y, u32 width, u32 height, u32 layer = 0, u32 level = 0) override;
90
void Unmap() override;
91
void GenerateMipmaps() override;
92
93
#ifdef ENABLE_GPU_OBJECT_NAMES
94
void SetDebugName(std::string_view name) override;
95
#endif
96
97
private:
98
D3D11Texture(u32 width, u32 height, u32 layers, u32 levels, u32 samples, Type type, Format format, Flags flags,
99
ComPtr<ID3D11Texture2D> texture, ComPtr<ID3D11ShaderResourceView> srv, ComPtr<ID3D11View> rtv_dsv,
100
ComPtr<ID3D11UnorderedAccessView> uav);
101
102
ComPtr<ID3D11Texture2D> m_texture;
103
ComPtr<ID3D11ShaderResourceView> m_srv;
104
ComPtr<ID3D11View> m_rtv_dsv;
105
ComPtr<ID3D11UnorderedAccessView> m_uav;
106
u32 m_mapped_subresource = 0;
107
};
108
109
class D3D11TextureBuffer final : public GPUTextureBuffer
110
{
111
public:
112
D3D11TextureBuffer(Format format, u32 size_in_elements);
113
~D3D11TextureBuffer() override;
114
115
ALWAYS_INLINE ID3D11Buffer* GetBuffer() const { return m_buffer.GetD3DBuffer(); }
116
ALWAYS_INLINE ID3D11ShaderResourceView* GetSRV() const { return m_srv.Get(); }
117
ALWAYS_INLINE ID3D11ShaderResourceView* const* GetSRVArray() const { return m_srv.GetAddressOf(); }
118
119
bool CreateBuffer(Error* error);
120
121
// Inherited via GPUTextureBuffer
122
void* Map(u32 required_elements) override;
123
void Unmap(u32 used_elements) override;
124
125
#ifdef ENABLE_GPU_OBJECT_NAMES
126
void SetDebugName(std::string_view name) override;
127
#endif
128
129
private:
130
D3D11StreamBuffer m_buffer;
131
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_srv;
132
};
133
134
class D3D11DownloadTexture final : public GPUDownloadTexture
135
{
136
public:
137
~D3D11DownloadTexture() override;
138
139
static std::unique_ptr<D3D11DownloadTexture> Create(u32 width, u32 height, GPUTexture::Format format, Error* error);
140
141
void CopyFromTexture(u32 dst_x, u32 dst_y, GPUTexture* src, u32 src_x, u32 src_y, u32 width, u32 height,
142
u32 src_layer, u32 src_level, bool use_transfer_pitch) override;
143
144
bool Map(u32 x, u32 y, u32 width, u32 height) override;
145
void Unmap() override;
146
147
void Flush() override;
148
149
#ifdef ENABLE_GPU_OBJECT_NAMES
150
void SetDebugName(std::string_view name) override;
151
#endif
152
153
private:
154
D3D11DownloadTexture(Microsoft::WRL::ComPtr<ID3D11Texture2D> tex, u32 width, u32 height, GPUTexture::Format format);
155
156
Microsoft::WRL::ComPtr<ID3D11Texture2D> m_texture;
157
};
158
159