forked from megayuchi/D3D12Lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureManager.cpp
223 lines (194 loc) · 6.03 KB
/
TextureManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "pch.h"
#include <Windows.h>
#include <d3d12.h>
#include "typedef.h"
#include "../Util/HashTable.h"
#include "SingleDescriptorAllocator.h"
#include "D3D12Renderer.h"
#include "D3D12ResourceManager.h"
#include "TextureManager.h"
CTextureManager::CTextureManager()
{
}
BOOL CTextureManager::Initialize(CD3D12Renderer* pRenderer, DWORD dwMaxBucketNum, DWORD dwMaxFileNum)
{
m_pRenderer = pRenderer;
m_pResourceManager = pRenderer->INL_GetResourceManager();
m_pHashTable = new CHashTable;
m_pHashTable->Initialize(dwMaxBucketNum, _MAX_PATH, dwMaxFileNum);
return TRUE;
}
TEXTURE_HANDLE* CTextureManager::CreateTextureFromFile(const WCHAR* wchFileName)
{
ID3D12Device* pD3DDevice = m_pRenderer->INL_GetD3DDevice();
CSingleDescriptorAllocator* pSingleDescriptorAllocator = m_pRenderer->INL_GetSingleDescriptorAllocator();
ID3D12Resource* pTexResource = nullptr;
D3D12_CPU_DESCRIPTOR_HANDLE srv = {};
D3D12_RESOURCE_DESC desc = {};
TEXTURE_HANDLE* pTexHandle = nullptr;
DWORD dwFileNameLen = (DWORD)wcslen(wchFileName);
DWORD dwKeySize = dwFileNameLen * sizeof(WCHAR);
if (m_pHashTable->Select((void**)&pTexHandle, 1, wchFileName, dwKeySize))
{
pTexHandle->dwRefCount++;
}
else
{
if (m_pResourceManager->CreateTextureFromFile(&pTexResource, &desc, wchFileName))
{
D3D12_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.Format = desc.Format;
SRVDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = desc.MipLevels;
if (pSingleDescriptorAllocator->AllocDescriptorHandle(&srv))
{
pD3DDevice->CreateShaderResourceView(pTexResource, &SRVDesc, srv);
pTexHandle = AllocTextureHandle();
pTexHandle->pTexResource = pTexResource;
pTexHandle->bFromFile = TRUE;
pTexHandle->srv = srv;
pTexHandle->pSearchHandle = m_pHashTable->Insert((void*)pTexHandle, wchFileName, dwKeySize);
if (!pTexHandle->pSearchHandle)
__debugbreak();
}
else
{
pTexResource->Release();
pTexResource = nullptr;
}
}
}
return pTexHandle;
}
TEXTURE_HANDLE* CTextureManager::CreateDynamicTexture(UINT TexWidth, UINT TexHeight)
{
ID3D12Device* pD3DDevice = m_pRenderer->INL_GetD3DDevice();
CSingleDescriptorAllocator* pSingleDescriptorAllocator = m_pRenderer->INL_GetSingleDescriptorAllocator();
TEXTURE_HANDLE* pTexHandle = nullptr;
ID3D12Resource* pTexResource = nullptr;
ID3D12Resource* pUploadBuffer = nullptr;
D3D12_CPU_DESCRIPTOR_HANDLE srv = {};
DXGI_FORMAT TexFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
if (m_pResourceManager->CreateTexturePair(&pTexResource, &pUploadBuffer, TexWidth, TexHeight, TexFormat))
{
D3D12_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.Format = TexFormat;
SRVDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = 1;
if (pSingleDescriptorAllocator->AllocDescriptorHandle(&srv))
{
pD3DDevice->CreateShaderResourceView(pTexResource, &SRVDesc, srv);
pTexHandle = AllocTextureHandle();
pTexHandle->pTexResource = pTexResource;
pTexHandle->pUploadBuffer = pUploadBuffer;
pTexHandle->srv = srv;
}
else
{
pTexResource->Release();
pTexResource = nullptr;
pUploadBuffer->Release();
pUploadBuffer = nullptr;
}
}
return pTexHandle;
}
TEXTURE_HANDLE* CTextureManager::CreateImmutableTexture(UINT TexWidth, UINT TexHeight, DXGI_FORMAT format, const BYTE* pInitImage)
{
ID3D12Device* pD3DDevice = m_pRenderer->INL_GetD3DDevice();
CSingleDescriptorAllocator* pSingleDescriptorAllocator = m_pRenderer->INL_GetSingleDescriptorAllocator();
TEXTURE_HANDLE* pTexHandle = nullptr;
ID3D12Resource* pTexResource = nullptr;
D3D12_CPU_DESCRIPTOR_HANDLE srv = {};
if (m_pResourceManager->CreateTexture(&pTexResource, TexWidth, TexHeight, format, pInitImage))
{
D3D12_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.Format = format;
SRVDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = 1;
if (pSingleDescriptorAllocator->AllocDescriptorHandle(&srv))
{
pD3DDevice->CreateShaderResourceView(pTexResource, &SRVDesc, srv);
pTexHandle = AllocTextureHandle();
pTexHandle->pTexResource = pTexResource;
pTexHandle->srv = srv;
}
else
{
pTexResource->Release();
pTexResource = nullptr;
}
}
return pTexHandle;
}
void CTextureManager::DeleteTexture(TEXTURE_HANDLE* pTexHandle)
{
FreeTextureHandle(pTexHandle);
}
TEXTURE_HANDLE* CTextureManager::AllocTextureHandle()
{
TEXTURE_HANDLE* pTexHandle = new TEXTURE_HANDLE;
memset(pTexHandle, 0, sizeof(TEXTURE_HANDLE));
pTexHandle->Link.pItem = pTexHandle;
LinkToLinkedListFIFO(&m_pTexLinkHead, &m_pTexLinkTail, &pTexHandle->Link);
pTexHandle->dwRefCount = 1;
return pTexHandle;
}
DWORD CTextureManager::FreeTextureHandle(TEXTURE_HANDLE* pTexHandle)
{
ID3D12Device* pD3DDevice = m_pRenderer->INL_GetD3DDevice();
CSingleDescriptorAllocator* pSingleDescriptorAllocator = m_pRenderer->INL_GetSingleDescriptorAllocator();
if (!pTexHandle->dwRefCount)
__debugbreak();
DWORD ref_count = --pTexHandle->dwRefCount;
if (!ref_count)
{
if (pTexHandle->pTexResource)
{
pTexHandle->pTexResource->Release();
pTexHandle->pTexResource = nullptr;
}
if (pTexHandle->pUploadBuffer)
{
pTexHandle->pUploadBuffer->Release();
pTexHandle->pUploadBuffer = nullptr;
}
if (pTexHandle->srv.ptr)
{
pSingleDescriptorAllocator->FreeDescriptorHandle(pTexHandle->srv);
pTexHandle->srv = {};
}
if (pTexHandle->pSearchHandle)
{
m_pHashTable->Delete(pTexHandle->pSearchHandle);
pTexHandle->pSearchHandle = nullptr;
}
UnLinkFromLinkedList(&m_pTexLinkHead, &m_pTexLinkTail, &pTexHandle->Link);
delete pTexHandle;
}
else
{
int a = 0;
}
return ref_count;
}
void CTextureManager::Cleanup()
{
if (m_pTexLinkHead)
{
// texture resource leak!!!
__debugbreak();
}
if (m_pHashTable)
{
delete m_pHashTable;
m_pHashTable = nullptr;
}
}
CTextureManager::~CTextureManager()
{
Cleanup();
}