textures.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. static vk_error init_texture_mem(struct vk_physical_device *phy_dev, struct vk_device *dev, struct vk_render_essentials *essentials,
  2. struct vk_image *image, uint8_t *texture, int width, int height, const char *name, bool mipmaps, bool linear)
  3. {
  4. vk_error retval = VK_ERROR_NONE;
  5. VkFormat img_format=VK_FORMAT_R8G8B8A8_UNORM; //VK_FORMAT_R8G8B8A8_SRGB
  6. *image = (struct vk_image) {
  7. .format = img_format,
  8. .extent = { .width = width, .height = height },
  9. .usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
  10. .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
  11. .make_view = true,
  12. .host_visible = false,
  13. .anisotropyEnable = true,
  14. .repeat_mode = VK_SAMPLER_ADDRESS_MODE_REPEAT, //VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER //VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
  15. .mipmaps = mipmaps,
  16. .linear = linear||mipmaps,
  17. };
  18. retval = vk_create_images(phy_dev, dev, image, 1);
  19. if (!vk_error_is_success(&retval))
  20. {
  21. retval.error.type=VK_ERROR_ERRNO;
  22. vk_error_printf(&retval, "Failed to create texture images\n");
  23. return retval;
  24. }
  25. retval = vk_render_init_texture(phy_dev, dev, essentials, image, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, texture, name);
  26. return retval;
  27. }
  28. #ifdef USE_stb_image
  29. static vk_error init_texture_file(struct vk_physical_device *phy_dev, struct vk_device *dev, struct vk_render_essentials *essentials,
  30. struct vk_image *image, const char *name, bool mipmaps)
  31. {
  32. vk_error retval = VK_ERROR_NONE;
  33. int width, height, channels;
  34. stbi_set_flip_vertically_on_load(true); //flip image Y
  35. uint8_t *generated_texture = stbi_load(name, &width, &height, &channels, STBI_rgb_alpha);
  36. if(generated_texture == NULL) {
  37. retval.error.type=VK_ERROR_ERRNO;
  38. printf("Error in loading image %s\n", name);
  39. return retval;
  40. }
  41. retval = init_texture_mem(phy_dev, dev, essentials, image, generated_texture, width, height, name, mipmaps, true);
  42. stbi_image_free(generated_texture);
  43. return retval;
  44. }
  45. #endif
  46. static vk_error texture_empty(struct vk_physical_device *phy_dev, struct vk_device *dev, struct vk_render_essentials *essentials, struct vk_image *image, int width, int height)
  47. {
  48. vk_error retval = VK_ERROR_NONE;
  49. size_t texture_size = width * height * 4 * sizeof(uint8_t);
  50. uint8_t *generated_texture = malloc(texture_size);
  51. if(generated_texture == NULL) {
  52. retval.error.type=VK_ERROR_ERRNO;
  53. printf("Error in allocating memory\n");
  54. return retval;
  55. }
  56. for (unsigned int i = 0; i < height; ++i){
  57. for (unsigned int j = 0; j < width; ++j){
  58. size_t pixel = (i * width + j) * 4 * sizeof(uint8_t);
  59. generated_texture[pixel + 0] = 0x00;
  60. generated_texture[pixel + 1] = 0x00;
  61. generated_texture[pixel + 2] = 0x00;
  62. generated_texture[pixel + 3] = 0x00;
  63. }
  64. }
  65. retval = init_texture_mem(phy_dev, dev, essentials, image, generated_texture, width, height, "empty", false, false);
  66. free(generated_texture);
  67. return retval;
  68. }