-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureLoader.cpp
302 lines (270 loc) · 12.5 KB
/
TextureLoader.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "stdafx.h"
#include "TextureLoader.h"
int TextureLoader::LoadImageDataFromFile(BYTE** pp_image_data, D3D12_RESOURCE_DESC& resource_description, LPCWSTR filename,
int& bytes_per_row) {
HRESULT hr;
// reset decoder, frame and converter since these will be different for each image we load
IWICBitmapDecoder* wicDecoder = nullptr;
IWICBitmapFrameDecode* wicFrame = nullptr;
IWICFormatConverter* wicConverter = nullptr;
bool imageConverted = false;
static IWICImagingFactory* m_wicFactory;
if (m_wicFactory == nullptr) {
// Initialize the COM library
CoInitialize(nullptr);
// create the WIC factory
hr = CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&m_wicFactory)
);
if (FAILED(hr))
return 0;
}
// load a decoder for the image
hr = m_wicFactory->CreateDecoderFromFilename(
filename, // Image we want to load in
nullptr, // This is a vendor ID, we do not prefer a specific one so set to null
GENERIC_READ, // We want to read from this file
WICDecodeMetadataCacheOnLoad,
// We will cache the metadata right away, rather than when needed, which might be unknown
&wicDecoder // the wic decoder to be created
);
if (FAILED(hr))
return 0;
// get image from decoder (this will decode the "frame")
hr = wicDecoder->GetFrame(0, &wicFrame);
if (FAILED(hr))
return 0;
// get wic pixel format of image
WICPixelFormatGUID pixelFormat;
hr = wicFrame->GetPixelFormat(&pixelFormat);
if (FAILED(hr))
return 0;
// get size of image
UINT textureWidth, textureHeight;
hr = wicFrame->GetSize(&textureWidth, &textureHeight);
if (FAILED(hr))
return 0;
// we are not handling sRGB types in this tutorial, so if you need that support, you'll have to figure
// out how to implement the support yourself
// convert wic pixel format to dxgi pixel format
DXGI_FORMAT dxgiFormat = GetDXGIFormatFromWICFormat(pixelFormat);
// if the format of the image is not a supported dxgi format, try to convert it
if (dxgiFormat == DXGI_FORMAT_UNKNOWN) {
// get a dxgi compatible wic format from the current image format
WICPixelFormatGUID convertToPixelFormat = GetConvertToWICFormat(pixelFormat);
// return if no dxgi compatible format was found
if (convertToPixelFormat == GUID_WICPixelFormatDontCare)
return 0;
// set the dxgi format
dxgiFormat = GetDXGIFormatFromWICFormat(convertToPixelFormat);
// create the format converter
hr = m_wicFactory->CreateFormatConverter(&wicConverter);
if (FAILED(hr))
return 0;
// make sure we can convert to the dxgi compatible format
BOOL canConvert = FALSE;
hr = wicConverter->CanConvert(pixelFormat, convertToPixelFormat, &canConvert);
if (FAILED(hr) || !canConvert)
return 0;
// do the conversion (wicConverter will contain the converted image)
hr = wicConverter->Initialize(wicFrame, convertToPixelFormat, WICBitmapDitherTypeErrorDiffusion, nullptr, 0,
WICBitmapPaletteTypeCustom);
if (FAILED(hr))
return 0;
// this is so we know to get the image data from the wicConverter (otherwise we will get from wicFrame)
imageConverted = true;
}
int bitsPerPixel = GetDXGIFormatBitsPerPixel(dxgiFormat); // number of bits per pixel
bytes_per_row = (textureWidth * bitsPerPixel) / 8; // number of bytes in each row of the image data
int imageSize = bytes_per_row * textureHeight; // total image size in bytes
// allocate enough memory for the raw image data, and set pp_image_data to point to that memory
*pp_image_data = (BYTE*)malloc(imageSize);
// copy (decoded) raw image data into the newly allocated memory (pp_image_data)
if (imageConverted) {
// if image format needed to be converted, the wic converter will contain the converted image
hr = wicConverter->CopyPixels(nullptr, bytes_per_row, imageSize, *pp_image_data);
if (FAILED(hr))
return 0;
} else {
// no need to convert, just copy data from the wic frame
hr = wicFrame->CopyPixels(nullptr, bytes_per_row, imageSize, *pp_image_data);
if (FAILED(hr))
return 0;
}
// now describe the texture with the information we have obtained from the image
resource_description = {};
resource_description.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
resource_description.Alignment = 0;
// may be 0, 4KB, 64KB, or 4MB. 0 will let runtime decide between 64KB and 4MB (4MB for multi-sampled textures)
resource_description.Width = textureWidth; // width of the texture
resource_description.Height = textureHeight; // height of the texture
resource_description.DepthOrArraySize = 1;
// if 3d image, depth of 3d image. Otherwise an array of 1D or 2D textures (we only have one image, so we set 1)
resource_description.MipLevels = 0;
// Number of mipmaps. We are not generating default mipmaps for this texture, so 0
resource_description.Format = dxgiFormat; // This is the dxgi format of the image (format of the pixels)
resource_description.SampleDesc.Count = 1; // This is the number of samples per pixel, we just want 1 sample
resource_description.SampleDesc.Quality = 0;
// The quality level of the samples. Higher is better quality, but worse performance
resource_description.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
// The arrangement of the pixels. Setting to unknown lets the driver choose the most efficient one
resource_description.Flags = D3D12_RESOURCE_FLAG_NONE; // no flags
// return the size of the image. remember to delete the image once your done with it (in this tutorial once its uploaded to the gpu)
return imageSize;
}
// get the dxgi format equivilent of a wic format
DXGI_FORMAT TextureLoader::GetDXGIFormatFromWICFormat(WICPixelFormatGUID& wic_format_guid) {
if (wic_format_guid == GUID_WICPixelFormat128bppRGBAFloat)
return DXGI_FORMAT_R32G32B32A32_FLOAT;
if (wic_format_guid == GUID_WICPixelFormat64bppRGBAHalf)
return DXGI_FORMAT_R16G16B16A16_FLOAT;
if (wic_format_guid == GUID_WICPixelFormat64bppRGBA)
return DXGI_FORMAT_R16G16B16A16_UNORM;
if (wic_format_guid == GUID_WICPixelFormat32bppRGBA)
return DXGI_FORMAT_R8G8B8A8_UNORM;
if (wic_format_guid == GUID_WICPixelFormat32bppBGRA)
return DXGI_FORMAT_B8G8R8A8_UNORM;
if (wic_format_guid == GUID_WICPixelFormat32bppBGR)
return DXGI_FORMAT_B8G8R8X8_UNORM;
if (wic_format_guid == GUID_WICPixelFormat32bppRGBA1010102XR)
return DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM;
if (wic_format_guid == GUID_WICPixelFormat32bppRGBA1010102)
return DXGI_FORMAT_R10G10B10A2_UNORM;
if (wic_format_guid == GUID_WICPixelFormat16bppBGRA5551)
return DXGI_FORMAT_B5G5R5A1_UNORM;
if (wic_format_guid == GUID_WICPixelFormat16bppBGR565)
return DXGI_FORMAT_B5G6R5_UNORM;
if (wic_format_guid == GUID_WICPixelFormat32bppGrayFloat)
return DXGI_FORMAT_R32_FLOAT;
if (wic_format_guid == GUID_WICPixelFormat16bppGrayHalf)
return DXGI_FORMAT_R16_FLOAT;
if (wic_format_guid == GUID_WICPixelFormat16bppGray)
return DXGI_FORMAT_R16_UNORM;
if (wic_format_guid == GUID_WICPixelFormat8bppGray)
return DXGI_FORMAT_R8_UNORM;
if (wic_format_guid == GUID_WICPixelFormat8bppAlpha)
return DXGI_FORMAT_A8_UNORM;
return DXGI_FORMAT_UNKNOWN;
}
// get a dxgi compatible wic format from another wic format
WICPixelFormatGUID TextureLoader::GetConvertToWICFormat(WICPixelFormatGUID& wic_format_guid) {
if (wic_format_guid == GUID_WICPixelFormatBlackWhite)
return GUID_WICPixelFormat8bppGray;
if (wic_format_guid == GUID_WICPixelFormat1bppIndexed)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat2bppIndexed)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat4bppIndexed)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat8bppIndexed)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat2bppGray)
return GUID_WICPixelFormat8bppGray;
if (wic_format_guid == GUID_WICPixelFormat4bppGray)
return GUID_WICPixelFormat8bppGray;
if (wic_format_guid == GUID_WICPixelFormat16bppGrayFixedPoint)
return GUID_WICPixelFormat16bppGrayHalf;
if (wic_format_guid == GUID_WICPixelFormat32bppGrayFixedPoint)
return GUID_WICPixelFormat32bppGrayFloat;
if (wic_format_guid == GUID_WICPixelFormat16bppBGR555)
return GUID_WICPixelFormat16bppBGRA5551;
if (wic_format_guid == GUID_WICPixelFormat32bppBGR101010)
return GUID_WICPixelFormat32bppRGBA1010102;
if (wic_format_guid == GUID_WICPixelFormat24bppBGR)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat24bppRGB)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat32bppPBGRA)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat32bppPRGBA)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat48bppRGB)
return GUID_WICPixelFormat64bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat48bppBGR)
return GUID_WICPixelFormat64bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat64bppBGRA)
return GUID_WICPixelFormat64bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat64bppPRGBA)
return GUID_WICPixelFormat64bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat64bppPBGRA)
return GUID_WICPixelFormat64bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat48bppRGBFixedPoint)
return GUID_WICPixelFormat64bppRGBAHalf;
if (wic_format_guid == GUID_WICPixelFormat48bppBGRFixedPoint)
return GUID_WICPixelFormat64bppRGBAHalf;
if (wic_format_guid == GUID_WICPixelFormat64bppRGBAFixedPoint)
return GUID_WICPixelFormat64bppRGBAHalf;
if (wic_format_guid == GUID_WICPixelFormat64bppBGRAFixedPoint)
return GUID_WICPixelFormat64bppRGBAHalf;
if (wic_format_guid == GUID_WICPixelFormat64bppRGBFixedPoint)
return GUID_WICPixelFormat64bppRGBAHalf;
if (wic_format_guid == GUID_WICPixelFormat64bppRGBHalf)
return GUID_WICPixelFormat64bppRGBAHalf;
if (wic_format_guid == GUID_WICPixelFormat48bppRGBHalf)
return GUID_WICPixelFormat64bppRGBAHalf;
if (wic_format_guid == GUID_WICPixelFormat128bppPRGBAFloat)
return GUID_WICPixelFormat128bppRGBAFloat;
if (wic_format_guid == GUID_WICPixelFormat128bppRGBFloat)
return GUID_WICPixelFormat128bppRGBAFloat;
if (wic_format_guid == GUID_WICPixelFormat128bppRGBAFixedPoint)
return GUID_WICPixelFormat128bppRGBAFloat;
if (wic_format_guid == GUID_WICPixelFormat128bppRGBFixedPoint)
return GUID_WICPixelFormat128bppRGBAFloat;
if (wic_format_guid == GUID_WICPixelFormat32bppRGBE)
return GUID_WICPixelFormat128bppRGBAFloat;
if (wic_format_guid == GUID_WICPixelFormat32bppCMYK)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat64bppCMYK)
return GUID_WICPixelFormat64bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat40bppCMYKAlpha)
return GUID_WICPixelFormat64bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat80bppCMYKAlpha)
return GUID_WICPixelFormat64bppRGBA;
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE)
if (wic_format_guid == GUID_WICPixelFormat32bppRGB)
return GUID_WICPixelFormat32bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat64bppRGB)
return GUID_WICPixelFormat64bppRGBA;
if (wic_format_guid == GUID_WICPixelFormat64bppPRGBAHalf)
return GUID_WICPixelFormat64bppRGBAHalf;
#endif
return GUID_WICPixelFormatDontCare;
}
// get the number of bits per pixel for a dxgi format
int TextureLoader::GetDXGIFormatBitsPerPixel(DXGI_FORMAT& dxgi_format) {
if (dxgi_format == DXGI_FORMAT_R32G32B32A32_FLOAT)
return 128;
if (dxgi_format == DXGI_FORMAT_R16G16B16A16_FLOAT)
return 64;
if (dxgi_format == DXGI_FORMAT_R16G16B16A16_UNORM)
return 64;
if (dxgi_format == DXGI_FORMAT_R8G8B8A8_UNORM)
return 32;
if (dxgi_format == DXGI_FORMAT_B8G8R8A8_UNORM)
return 32;
if (dxgi_format == DXGI_FORMAT_B8G8R8X8_UNORM)
return 32;
if (dxgi_format == DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM)
return 32;
if (dxgi_format == DXGI_FORMAT_R10G10B10A2_UNORM)
return 32;
if (dxgi_format == DXGI_FORMAT_B5G5R5A1_UNORM)
return 16;
if (dxgi_format == DXGI_FORMAT_B5G6R5_UNORM)
return 16;
else {
if (dxgi_format == DXGI_FORMAT_R32_FLOAT)
return 32;
if (dxgi_format == DXGI_FORMAT_R16_FLOAT)
return 16;
if (dxgi_format == DXGI_FORMAT_R16_UNORM)
return 16;
if (dxgi_format == DXGI_FORMAT_R8_UNORM)
return 8;
if (dxgi_format == DXGI_FORMAT_A8_UNORM)
return 8;
}
}