-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathImageLoader.cpp
232 lines (196 loc) · 7.1 KB
/
ImageLoader.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
#include "stdafx.h"
#include "AssetLoaders.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
namespace happy
{
class Image
{
public:
Image(fs::path &path)
{
int n;
m_Data = stbi_load(path.string().c_str(), &m_Width, &m_Height, &n, 4);
}
~Image()
{
stbi_image_free(m_Data);
}
unsigned getWidth() const { return (unsigned)m_Width; }
unsigned getHeight() const { return (unsigned)m_Height; }
unsigned char* getData() const { return m_Data; }
private:
int m_Width;
int m_Height;
unsigned char* m_Data;
};
ComPtr<ID3D11ShaderResourceView> loadCubemap(RenderingContext *pRenderContext, fs::path filePath[6])
{
ComPtr<ID3D11Texture2D> pCubemap;
{
Image faces[6] = { filePath[0], filePath[1], filePath[2], filePath[3], filePath[4], filePath[5] };
for (int i = 1; i < 6; ++i)
if (faces[i].getWidth() != faces[0].getWidth() ||
faces[i].getHeight() != faces[0].getHeight())
throw exception("All faces of a cubemap must be the same size");
D3D11_TEXTURE2D_DESC desc;
desc.Width = faces[0].getWidth();
desc.Height = faces[0].getHeight();
desc.MipLevels = 1;
desc.ArraySize = 6;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.CPUAccessFlags = 0;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
D3D11_SUBRESOURCE_DATA data[6];
for (unsigned int i = 0; i < 6; ++i)
{
data[i].pSysMem = faces[i].getData();
data[i].SysMemPitch = faces[i].getWidth() * 4;
data[i].SysMemSlicePitch = 0;
}
THROW_ON_FAIL(pRenderContext->getDevice()->CreateTexture2D(&desc, data, &pCubemap));
#ifdef DEBUG
std::string tag = filePath[0].filename().string();
pCubemap->SetPrivateData(WKPDID_D3DDebugObjectName, tag.length(), tag.data());
#endif
}
ComPtr<ID3D11ShaderResourceView> pSRV;
{
D3D11_SHADER_RESOURCE_VIEW_DESC desc;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
desc.TextureCube.MipLevels = 1;
desc.TextureCube.MostDetailedMip = 0;
THROW_ON_FAIL(pRenderContext->getDevice()->CreateShaderResourceView(pCubemap.Get(), &desc, &pSRV));
}
return pSRV;
}
ComPtr<ID3D11ShaderResourceView> loadCubemapFolder(RenderingContext *pRenderContext, fs::path folderPath, std::string format)
{
fs::path files[] =
{
folderPath / ("posx." + format),
folderPath / ("negx." + format),
folderPath / ("posy." + format),
folderPath / ("negy." + format),
folderPath / ("posz." + format),
folderPath / ("negz." + format),
};
return loadCubemap(pRenderContext, files);
}
ComPtr<ID3D11ShaderResourceView> loadTexture(RenderingContext *pRenderContext, fs::path filePath)
{
Image image = filePath;
ComPtr<ID3D11Texture2D> pTexture;
{
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = image.getWidth();
desc.Height = image.getHeight();
desc.MipLevels = 0;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET;
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
THROW_ON_FAIL(pRenderContext->getDevice()->CreateTexture2D(&desc, nullptr, &pTexture));
#ifdef DEBUG
std::string tag = filePath.filename().string();
pTexture->SetPrivateData(WKPDID_D3DDebugObjectName, tag.length(), tag.data());
#endif
}
ComPtr<ID3D11ShaderResourceView> pSRV;
{
D3D11_SHADER_RESOURCE_VIEW_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
desc.Texture2D.MipLevels = -1;
THROW_ON_FAIL(pRenderContext->getDevice()->CreateShaderResourceView(pTexture.Get(), &desc, &pSRV));
}
pRenderContext->getContext()->UpdateSubresource(pTexture.Get(), 0, nullptr, image.getData(), image.getWidth() * 4, image.getWidth()*image.getHeight()*4);
pRenderContext->getContext()->GenerateMips(pSRV.Get());
return pSRV;
}
ComPtr<ID3D11ShaderResourceView> loadCombinedTexture(RenderingContext *pRenderContext, unsigned defaultPixel, vector<TextureLayer> files)
{
unsigned width = 1;
unsigned height = 1;
vector<unsigned> combinedImageData;
combinedImageData.resize(width * height, bb::swap_endian(defaultPixel));
for (unsigned i = 0; i < files.size(); ++i)
{
Image image = files[i].m_path;
if (i == 0)
{
width = image.getWidth();
height = image.getHeight();
combinedImageData.resize(width * height, bb::swap_endian(defaultPixel));
}
if (image.getWidth() != width || image.getHeight() != height)
throw exception("images must be same size");
unsigned *srcdata = reinterpret_cast<unsigned*>(image.getData());
unsigned *dstdata = combinedImageData.data();
unsigned length = (unsigned)combinedImageData.size();
switch (files[i].type)
{
case TextureLayer::rgb:
for (unsigned pixel = 0; pixel < length; ++pixel)
{
dstdata[pixel] |= (srcdata[pixel] & 0x00ffffff);
}
break;
case TextureLayer::gray:
switch (files[i].target)
{
case TextureLayer::r: for (unsigned pixel = 0; pixel < length; ++pixel) dstdata[pixel] |= (srcdata[pixel] & 0x000000ff); break;
case TextureLayer::g: for (unsigned pixel = 0; pixel < length; ++pixel) dstdata[pixel] |= (srcdata[pixel] & 0x000000ff) << 8; break;
case TextureLayer::b: for (unsigned pixel = 0; pixel < length; ++pixel) dstdata[pixel] |= (srcdata[pixel] & 0x000000ff) << 16; break;
case TextureLayer::a: for (unsigned pixel = 0; pixel < length; ++pixel) dstdata[pixel] |= (srcdata[pixel] & 0x000000ff) << 24; break;
}
break;
}
}
ComPtr<ID3D11Texture2D> pTexture;
{
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = width;
desc.Height = height;
desc.MipLevels = 0;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
THROW_ON_FAIL(pRenderContext->getDevice()->CreateTexture2D(&desc, nullptr, &pTexture));
#ifdef DEBUG
std::string tag = filesWithMapping[0].first.filename().string();
pTexture->SetPrivateData(WKPDID_D3DDebugObjectName, tag.length(), tag.data());
#endif
}
ComPtr<ID3D11ShaderResourceView> pSRV;
{
D3D11_SHADER_RESOURCE_VIEW_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
desc.Texture2D.MipLevels = -1;
THROW_ON_FAIL(pRenderContext->getDevice()->CreateShaderResourceView(pTexture.Get(), &desc, &pSRV));
}
pRenderContext->getContext()->UpdateSubresource(pTexture.Get(), 0, nullptr, (void*)combinedImageData.data(), width*4, width*height*4);
pRenderContext->getContext()->GenerateMips(pSRV.Get());
return pSRV;
}
}