-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTexture.cpp
More file actions
416 lines (369 loc) · 10.7 KB
/
Copy pathTexture.cpp
File metadata and controls
416 lines (369 loc) · 10.7 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "Texture.h"
#include "PathFinder.h"
#include "Core.Memory.hpp"
// Win32::ThrowIfFailed가 여기 있다. 유니티 빌드에서는 같은 블롭의 앞선
// 파일이 공급했다.
#include "DirectXHelper.h"
#include <DirectXTex.h>
// 유니티 빌드에서는 같은 블롭의 앞선 파일이 이 using을 공급했다.
// ASan 구성은 블롭을 끄므로 직접 받는다(PHASE 9-9).
using namespace DirectX;
//static functions
Texture* Texture::CreateFromPixels(_In_ uint32 width, _In_ uint32 height,
_In_ std::string_view name, _In_ DXGI_FORMAT textureFormat,
_In_reads_bytes_(rowPitch* height) const void* pixels, _In_opt_ size_t rowPitch)
{
if (0 == width || 0 == height || nullptr == pixels) return nullptr;
DirectX::ScratchImage image;
if (FAILED(image.Initialize2D(textureFormat, width, height, 1, 1))) return nullptr;
const DirectX::Image* destination = image.GetImage(0, 0, 0);
if (nullptr == destination) return nullptr;
// 원본 행 간격을 안 주면 빈틈없이 채워진 것으로 본다.
//
// ★ 행 단위로 옮긴다. ScratchImage의 행 간격은 정렬 때문에 원본보다 클 수
// 있고, 그때 통째로 memcpy하면 그림이 한 행씩 밀려 비스듬해진다 —
// 1x1에서는 안 드러나고 폭이 커지는 순간 나타나는 부류다.
const size_t bitsPerPixel = DirectX::BitsPerPixel(textureFormat);
if (0 == bitsPerPixel) return nullptr;
const size_t packedPitch = (static_cast<size_t>(width) * bitsPerPixel + 7u) / 8u;
const size_t sourcePitch = (0 != rowPitch) ? rowPitch : packedPitch;
const size_t copyBytes = (std::min)(sourcePitch, destination->rowPitch);
const auto* source = static_cast<const uint8_t*>(pixels);
for (uint32 y = 0; y < height; ++y)
{
std::memcpy(destination->pixels + static_cast<size_t>(y) * destination->rowPitch,
source + static_cast<size_t>(y) * sourcePitch, copyBytes);
}
Texture* texture = new Texture();
texture->m_name = std::string(name);
texture->m_textureType = TextureType::ImageTexture;
texture->m_size = { float(width), float(height) };
// 파일 로더가 남기는 자리와 같다 — DX12 캐시가 여기서 가져간다(T1·T4).
texture->m_cpuPixels = std::make_shared<DirectX::ScratchImage>(std::move(image));
return texture;
}
Texture* Texture::LoadFormPath(_In_ const file::path& path, bool isCompress)
{
file::path matPath = PathFinder::RelativeToMaterial(path.string());
if (!file::exists(path) && !file::exists(matPath))
{
return nullptr;
}
file::path preparePath{};
if (file::exists(matPath))
{
preparePath = matPath;
}
else
{
preparePath = path;
}
ScratchImage image{};
TexMetadata metadata{};
Benchmark banch3;
if (path.extension() == ".dds")
{
//load dds
Win32::ThrowIfFailed(
LoadFromDDSFile(
preparePath.c_str(),
DDS_FLAGS_FORCE_RGB,
&metadata,
image
)
);
}
else if (path.extension() == ".tga")
{
//load tga
Win32::ThrowIfFailed(
LoadFromTGAFile(
preparePath.c_str(),
&metadata,
image
)
);
}
else if (path.extension() == ".hdr")
{
//load hdr
Win32::ThrowIfFailed(
LoadFromHDRFile(
preparePath.c_str(),
&metadata,
image
)
);
}
else
{
//load wic
Win32::ThrowIfFailed(
LoadFromWICFile(
preparePath.c_str(),
WIC_FLAGS_IGNORE_SRGB,
&metadata,
image
)
);
}
if(isCompress)
{
ScratchImage compressedImage{};
if (!IsCompressed(metadata.format) && path.extension() != ".hdr" && path.extension() != ".dds")
{
DirectX::TexMetadata tempMetadata = metadata;
// DXGI_FORMAT_BC1_UNORM (== DXT1)
Win32::ThrowIfFailed(
DirectX::Compress(
image.GetImages(),
image.GetImageCount(),
metadata,
DXGI_FORMAT_BC1_UNORM_SRGB,
TEX_COMPRESS_PARALLEL,
0.5f,
compressedImage
)
);
metadata = compressedImage.GetMetadata(); // ��Ÿ������ ����
image = std::move(compressedImage); // ����� �̹����� ��ü
}
}
Texture* texture = new Texture();
// ★ 여기 있던 DX11 SRV 생성을 걷었다 (T6, 2026-08-08).
//
// 이 로더가 만들던 것은 둘이었다 - CPU 픽셀(m_cpuPixels)과 DX11
// SRV. 앞의 것은 DX12TextureCache가 읽어 올리고, 뒤의 것은
// 에디터 썸네일의 폴백 하나가 마지막 소비자였다(EditorImGuiTexture).
// 그 폴백이 사라지면서 소비자가 0이 됐다.
texture->m_textureType = TextureType::ImageTexture;
texture->m_size = { float(metadata.width),float(metadata.height) };
texture->m_isTextureAlpha = !image.IsAlphaAllOpaque();
// 압축까지 끝난 최종 이미지를 DX12가 가져가도록 남긴다(T1).
// 예전에는 여기서 버렸고 DX12는 방금 만든 DX11 텍스처에서 되읽었다.
texture->m_cpuPixels = std::make_shared<DirectX::ScratchImage>(std::move(image));
return texture;
}
std::shared_ptr<Texture> Texture::LoadSharedFromPath(const file::path& path, bool isCompress)
{
file::path matPath = PathFinder::RelativeToMaterial(path.string());
if (!file::exists(path) && !file::exists(matPath))
{
return nullptr;
}
file::path preparePath{};
if (file::exists(matPath))
{
preparePath = matPath;
}
else
{
preparePath = path;
}
ScratchImage image{};
TexMetadata metadata{};
Benchmark banch3;
if (path.extension() == ".dds")
{
//load dds
Win32::ThrowIfFailed(
LoadFromDDSFile(
preparePath.c_str(),
DDS_FLAGS_FORCE_RGB,
&metadata,
image
)
);
}
else if (path.extension() == ".tga")
{
//load tga
Win32::ThrowIfFailed(
LoadFromTGAFile(
preparePath.c_str(),
&metadata,
image
)
);
}
else if (path.extension() == ".hdr")
{
//load hdr
Win32::ThrowIfFailed(
LoadFromHDRFile(
preparePath.c_str(),
&metadata,
image
)
);
}
else
{
//load wic
Win32::ThrowIfFailed(
LoadFromWICFile(
preparePath.c_str(),
WIC_FLAGS_IGNORE_SRGB,
&metadata,
image
)
);
}
if (isCompress)
{
ScratchImage compressedImage{};
if (!IsCompressed(metadata.format) && path.extension() != ".hdr" && path.extension() != ".dds")
{
DirectX::TexMetadata tempMetadata = metadata;
// DXGI_FORMAT_BC1_UNORM (== DXT1)
Win32::ThrowIfFailed(
DirectX::Compress(
image.GetImages(),
image.GetImageCount(),
image.GetMetadata(),
DXGI_FORMAT_BC1_UNORM,
TEX_COMPRESS_SRGB | TEX_COMPRESS_DITHER | TEX_COMPRESS_UNIFORM,
0.5f,
compressedImage
)
);
metadata = compressedImage.GetMetadata(); // ��Ÿ������ ����
image = std::move(compressedImage); // ����� �̹����� ��ü
}
}
auto texture = std::make_shared<Texture>();
// ★ DX11 SRV 생성 제거 (T6) - 위 LoadFormPath의 주석과 같은 이유다.
texture->m_textureType = TextureType::ImageTexture;
texture->m_size = { float(image.GetMetadata().width),float(image.GetMetadata().height) };
texture->m_isTextureAlpha = !image.IsAlphaAllOpaque();
// 압축까지 끝난 최종 이미지를 DX12가 가져가도록 남긴다(T1).
// 예전에는 여기서 버렸고 DX12는 방금 만든 DX11 텍스처에서 되읽었다.
texture->m_cpuPixels = std::make_shared<DirectX::ScratchImage>(std::move(image));
return texture;
}
std::unique_ptr<Texture> Texture::LoadManagedFromPath(const file::path& path, bool isCompress)
{
file::path matPath = PathFinder::RelativeToMaterial(path.string());
if (!file::exists(path) && !file::exists(matPath))
{
return nullptr;
}
file::path preparePath{};
if (file::exists(matPath))
{
preparePath = matPath;
}
else
{
preparePath = path;
}
ScratchImage image{};
TexMetadata metadata{};
Benchmark banch3;
if (path.extension() == ".dds")
{
//load dds
Win32::ThrowIfFailed(
LoadFromDDSFile(
preparePath.c_str(),
DDS_FLAGS_FORCE_RGB,
&metadata,
image
)
);
}
else if (path.extension() == ".tga")
{
//load tga
Win32::ThrowIfFailed(
LoadFromTGAFile(
preparePath.c_str(),
&metadata,
image
)
);
}
else if (path.extension() == ".hdr")
{
//load hdr
Win32::ThrowIfFailed(
LoadFromHDRFile(
preparePath.c_str(),
&metadata,
image
)
);
}
else
{
//load wic
Win32::ThrowIfFailed(
LoadFromWICFile(
preparePath.c_str(),
WIC_FLAGS_IGNORE_SRGB,
&metadata,
image
)
);
}
if (isCompress)
{
ScratchImage compressedImage{};
if (!IsCompressed(metadata.format) && path.extension() != ".hdr" && path.extension() != ".dds")
{
DirectX::TexMetadata tempMetadata = metadata;
// DXGI_FORMAT_BC1_UNORM (== DXT1)
Win32::ThrowIfFailed(
DirectX::Compress(
image.GetImages(),
image.GetImageCount(),
metadata,
DXGI_FORMAT_BC1_UNORM,
TEX_COMPRESS_PARALLEL,
0.5f,
compressedImage
)
);
metadata = compressedImage.GetMetadata(); // ��Ÿ������ ����
image = std::move(compressedImage); // ����� �̹����� ��ü
}
}
auto texture = std::make_unique<Texture>();
// ★ DX11 SRV 생성 제거 (T6) - 위 LoadFormPath의 주석과 같은 이유다.
texture->m_textureType = TextureType::ImageTexture;
texture->m_size = { float(metadata.width),float(metadata.height) };
texture->m_isTextureAlpha = !image.IsAlphaAllOpaque();
// 압축까지 끝난 최종 이미지를 DX12가 가져가도록 남긴다(T1).
// 예전에는 여기서 버렸고 DX12는 방금 만든 DX11 텍스처에서 되읽었다.
texture->m_cpuPixels = std::make_shared<DirectX::ScratchImage>(std::move(image));
return texture;
}
// ★ DX11 생성자·뷰 이관을 걷어낸 뒤의 수명 (T6, 2026-08-08).
//
// 예전 이동 생성자는 ID3D11 핸들 다섯과 desc를 옮기고, 화면 리사이즈
// 델리게이트 둘을 해제했다가 새 객체로 다시 걸었다. 그 전부가 DX11
// 자원과 화면 추종 정책의 것이라 함께 사라졌다.
//
// 남은 것은 CPU 자료뿐이고, 그것은 shared_ptr과 값이라 옮기기만 하면 된다.
Texture::Texture(Texture&& texture) noexcept
{
m_cpuPixels = std::move(texture.m_cpuPixels);
m_assetId = texture.m_assetId;
m_textureType = texture.m_textureType;
m_name = std::move(texture.m_name);
m_extension = std::move(texture.m_extension);
m_size = texture.m_size;
m_isTextureAlpha = texture.m_isTextureAlpha;
texture.m_textureType = TextureType::Unknown;
texture.m_size = {};
texture.m_isTextureAlpha = false;
}
Texture::~Texture() = default;
math::vector2 Texture::GetImageSize() const
{
// ★ 예전에는 m_sizeRatio로 나눴다 (T6에서 정리).
// 그 비율은 '화면의 1/N 해상도로 따라가는 렌더 타깃'을 위한 것이었고,
// 화면 추종 정책과 함께 사라졌다. 파일에서 읽은 텍스처의 비율은 늘
// 1이었으므로 이 함수가 돌려주던 값은 그대로다.
return m_size;
}