SaschaWillems / Vulkan-glTF-PBR

Physical based rendering with Vulkan using glTF 2.0 models
MIT License
915 stars 124 forks source link

Segmentation fault when failed to load a texture. #37

Open syoyo opened 4 years ago

syoyo commented 4 years ago

https://github.com/SaschaWillems/Vulkan-glTF-PBR/blob/master/base/VulkanTexture.hpp#L89

Creating gli::texture2d or gli::texture_cube directly from gli::load triggers assertion(debug build) or gives undefined behaviour(usually this results to a Vulkan error) in the constructor when failed to load a texture file. (It does not create empty gli texture)

Here is an example when failed to load a texture.

Fatal : VkResult is "-2" in /mnt/data/work/Vulkan-glTF-PBR/base/VulkanglTFModel.hpp at line 184
Vulkan-glTF-PBR: /mnt/data/work/Vulkan-glTF-PBR/base/VulkanglTFModel.hpp:184: void vkglTF::Texture::fromglTfImage(tinygltf::Image&, vkglTF::TextureSampler, vks::VulkanDevice*, VkQueue): Assertion `res == VK_SUCCESS' failed.
Aborted (core dumped)

Tthis is an issue of gli, so it should be fixed in gli library. For a while, we can use the following workaround:

  gli::texture tex(gli::load(filename.c_str());
  // will create empty texture when failed to load a texture.

  if (!tex.empty()) {
    // trigger assertion or return false
  }
  gli::texture2d tex2D(tex);

  ...

Related to: https://github.com/g-truc/gli/issues/174

SaschaWillems commented 4 years ago

Thanks for reporting this. I'll take a look into this.

SaschaWillems commented 4 years ago

As with my Vulkan samples, I'll replace gli with libktx. This gives more control over how to handle texture loading failures.

What would be your favorite way of handling texture loading failures? Right now the ktx loading will also just assert, and I think showing some kind of message or gracefully exiting with a proper output would be sufficient.

syoyo commented 4 years ago

I'll replace gli with libktx

Awesome!

What would be your favorite way of handling texture loading failures?

Less assertion as much as possible(so we may better to check if a texture file exists before libktx API call). Simply print an error when failed to load a texture(and/or file not found), then exit an app is appreciated.