| 362 | } |
| 363 | |
| 364 | ref<Texture> Texture::createFromFile( |
| 365 | ref<Device> pDevice, |
| 366 | const std::filesystem::path& path, |
| 367 | bool generateMipLevels, |
| 368 | bool loadAsSrgb, |
| 369 | ResourceBindFlags bindFlags, |
| 370 | Bitmap::ImportFlags importFlags |
| 371 | ) |
| 372 | { |
| 373 | if (!std::filesystem::exists(path)) |
| 374 | { |
| 375 | logWarning("Error when loading image file. File '{}' does not exist.", path); |
| 376 | return nullptr; |
| 377 | } |
| 378 | |
| 379 | ref<Texture> pTex; |
| 380 | if (hasExtension(path, "dds")) |
| 381 | { |
| 382 | try |
| 383 | { |
| 384 | pTex = ImageIO::loadTextureFromDDS(pDevice, path, loadAsSrgb); |
| 385 | } |
| 386 | catch (const std::exception& e) |
| 387 | { |
| 388 | logWarning("Error loading '{}': {}", path, e.what()); |
| 389 | } |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | Bitmap::UniqueConstPtr pBitmap = Bitmap::createFromFile(path, kTopDown, importFlags); |
| 394 | if (pBitmap) |
| 395 | { |
| 396 | ResourceFormat texFormat = pBitmap->getFormat(); |
| 397 | if (loadAsSrgb) |
| 398 | { |
| 399 | texFormat = linearToSrgbFormat(texFormat); |
| 400 | } |
| 401 | |
| 402 | pTex = pDevice->createTexture2D( |
| 403 | pBitmap->getWidth(), |
| 404 | pBitmap->getHeight(), |
| 405 | texFormat, |
| 406 | 1, |
| 407 | generateMipLevels ? Texture::kMaxPossible : 1, |
| 408 | pBitmap->getData(), |
| 409 | bindFlags |
| 410 | ); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (pTex != nullptr) |
| 415 | { |
| 416 | pTex->setSourcePath(path); |
| 417 | pTex->mImportFlags = importFlags; |
| 418 | |
| 419 | // Log debug info. |
| 420 | std::string str = fmt::format( |
| 421 | "Loaded texture: size={}x{} mips={} format={} path={}", |
nothing calls this directly
no test coverage detected