-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXTextureManager.cpp
256 lines (206 loc) · 7.11 KB
/
XTextureManager.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "XTextureManager.h"
XTextureManager* Singleton<XTextureManager>::ms_singleton = new XTextureManager(XD3DRenderer::GetSingletonPointer());
bool XTextureManager::GetTextureData(const std::string name, unsigned char** pData, unsigned int & width, unsigned int & height, unsigned int & rowPitch)
{
HRESULT hr;
ID3D11ShaderResourceView* pResourceView;;
pResourceView = m_textures[name]->pTexture;
if (pResourceView == nullptr)
{
pData = nullptr;
return false;
}
ID3D11Resource* pRes;
ID3D11Texture2D* pTex;
D3D11_TEXTURE2D_DESC tex2DDesc;
pResourceView->GetResource(&pRes);
pRes->QueryInterface(&pTex);
pTex->GetDesc(&tex2DDesc);
width = tex2DDesc.Width;
height = tex2DDesc.Height;
ID3D11Texture2D* pTex2 = nullptr;
D3D11_TEXTURE2D_DESC texDesc;
memset(&texDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
texDesc.Width = tex2DDesc.Width;
texDesc.Height = tex2DDesc.Height;
texDesc.MipLevels = tex2DDesc.MipLevels;
texDesc.ArraySize = tex2DDesc.ArraySize;
texDesc.Format = tex2DDesc.Format;
texDesc.SampleDesc.Count = tex2DDesc.SampleDesc.Count;
texDesc.SampleDesc.Quality = tex2DDesc.SampleDesc.Quality;
texDesc.Usage = D3D11_USAGE_STAGING;
//texDesc.BindFlags = tex2DDesc.BindFlags;
texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
texDesc.MiscFlags = tex2DDesc.MiscFlags;
hr = m_pD3D->GetD3DDevice()->CreateTexture2D(&texDesc, nullptr, &pTex2);
if (FAILED(hr))
return false;
m_pD3D->GetDeviceContext()->CopyResource(pTex2, pTex);
D3D11_MAPPED_SUBRESOURCE mappedTex;
m_pD3D->GetDeviceContext()->Map(pTex2, 0, D3D11_MAP_READ, 0, &mappedTex);
*pData = new unsigned char[mappedTex.RowPitch * height];
memcpy(*pData, (UCHAR*)mappedTex.pData, mappedTex.RowPitch * height * sizeof(unsigned char));
rowPitch = mappedTex.RowPitch;
m_pD3D->GetDeviceContext()->Unmap(pTex2, D3D11CalcSubresource(0, 0, 1));
return true;
}
bool XTextureManager::CreateTexture(unsigned char* imgData, const std::string& name, unsigned int height, unsigned int width, bool isLightmap)
{
HRESULT hr;
ID3D11Texture2D *pTex2D = nullptr;
D3D11_TEXTURE2D_DESC texDesc;
memset(&texDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
texDesc.Width = width;
texDesc.Height = height;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_DYNAMIC;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
texDesc.MiscFlags = 0;
hr = m_pD3D->GetD3DDevice()->CreateTexture2D(&texDesc, nullptr, &pTex2D);
if (FAILED(hr))
return false;
D3D11_MAPPED_SUBRESOURCE mappedTex;
m_pD3D->GetDeviceContext()->Map(pTex2D, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);
UCHAR* pTexels = (UCHAR*)mappedTex.pData;
int j = 0;
for (UINT row = 0; row < texDesc.Height; row++)
{
UINT rowStart = row * mappedTex.RowPitch;
for (UINT col = 0; col < texDesc.Width; col++)
{
UINT colStart = col * 4;
pTexels[rowStart + colStart + 0] = imgData[j++];
pTexels[rowStart + colStart + 1] = imgData[j++];
pTexels[rowStart + colStart + 2] = imgData[j++];
pTexels[rowStart + colStart + 3] = imgData[j++];
}
}
m_pD3D->GetDeviceContext()->Unmap(pTex2D, D3D11CalcSubresource(0, 0, 1));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
memset(&srvDesc, 0, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;
XTextureContainer* texture= new XTextureContainer();
hr = m_pD3D->GetD3DDevice()->CreateShaderResourceView(pTex2D, &srvDesc, &texture->pTexture);
if (FAILED(hr))
return false;
if (!isLightmap)
{
m_textures[name] = texture;
m_numTextures++;
}
else
{
m_lightmaps.push_back(texture);
m_numLightmaps++;
}
return true;
}
bool XTextureManager::CreateWhiteTexture(int width, int height, bool isLightMap)
{
HRESULT hr;
ID3D11Texture2D *pTex2D = nullptr;
D3D11_TEXTURE2D_DESC texDesc;
memset(&texDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
texDesc.Width = width;
texDesc.Height = height;
if (isLightMap)
{
texDesc.MipLevels = 0;
texDesc.ArraySize = 1;
}
else
{
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
}
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_DYNAMIC;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
texDesc.MiscFlags = 0;
hr = m_pD3D->GetD3DDevice()->CreateTexture2D(&texDesc, nullptr, &pTex2D);
if (FAILED(hr))
return false;
D3D11_MAPPED_SUBRESOURCE mappedTex;
m_pD3D->GetDeviceContext()->Map(pTex2D, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);
UCHAR* pTexels = (UCHAR*)mappedTex.pData;
memset(pTexels, 255, width*height * 4);
m_pD3D->GetDeviceContext()->Unmap(pTex2D, D3D11CalcSubresource(0, 0, 1));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
memset(&srvDesc, 0, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;
XTextureContainer* texture = new XTextureContainer();
hr = m_pD3D->GetD3DDevice()->CreateShaderResourceView(pTex2D, &srvDesc, &texture->pTexture);
if (FAILED(hr))
return false;
if (!isLightMap)
{
m_textures["white_texture"] = texture;
m_numTextures++;
}
else
{
m_lightmaps.push_back(texture);
m_numLightmaps++;
}
return true;
}
bool XTextureManager::Load(const std::string & filePath, const std::string& name, bool isLightmap)
{
HRESULT hr;
std::wstring wideFileName = std::wstring(filePath.begin(), filePath.end());
XTextureContainer* pTex = new XTextureContainer();
hr = D3DX11CreateShaderResourceViewFromFile(m_pD3D->GetD3DDevice(), wideFileName.c_str(), NULL, NULL, &pTex->pTexture, NULL);
if (FAILED(hr))
return false;
if (!isLightmap)
{
m_textures[name] = pTex;
m_numTextures++;
}
else
{
m_lightmaps.push_back(pTex);
m_numLightmaps++;
}
return true;
}
bool XTextureManager::Save(const std::string & filePath, const std::string& name)
{
return true;
}
bool XTextureManager::CreateSamplerState()
{
HRESULT hr;
D3D11_SAMPLER_DESC samplerDesc;
// Create a texture sampler state description.
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.BorderColor[0] = 0;
samplerDesc.BorderColor[1] = 0;
samplerDesc.BorderColor[2] = 0;
samplerDesc.BorderColor[3] = 0;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
// Create the texture sampler state.
hr = m_pD3D->GetD3DDevice()->CreateSamplerState(&samplerDesc, &m_pSampleState);
if (FAILED(hr))
return false;
return true;
}