image_compress_betsy.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**************************************************************************/
  2. /* image_compress_betsy.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef IMAGE_COMPRESS_BETSY_H
  31. #define IMAGE_COMPRESS_BETSY_H
  32. #include "core/io/image.h"
  33. #include "core/object/worker_thread_pool.h"
  34. #include "core/os/thread.h"
  35. #include "core/templates/command_queue_mt.h"
  36. #include "servers/rendering/rendering_device_binds.h"
  37. #include "servers/rendering/rendering_server_default.h"
  38. #if defined(VULKAN_ENABLED)
  39. #include "drivers/vulkan/rendering_context_driver_vulkan.h"
  40. #endif
  41. #if defined(METAL_ENABLED)
  42. #include "drivers/metal/rendering_context_driver_metal.h"
  43. #endif
  44. enum BetsyFormat {
  45. BETSY_FORMAT_BC1,
  46. BETSY_FORMAT_BC1_DITHER,
  47. BETSY_FORMAT_BC3,
  48. BETSY_FORMAT_BC4_SIGNED,
  49. BETSY_FORMAT_BC4_UNSIGNED,
  50. BETSY_FORMAT_BC6_SIGNED,
  51. BETSY_FORMAT_BC6_UNSIGNED,
  52. };
  53. struct BC6PushConstant {
  54. float sizeX;
  55. float sizeY;
  56. uint32_t padding[2];
  57. };
  58. struct BC1PushConstant {
  59. uint32_t num_refines;
  60. uint32_t padding[3];
  61. };
  62. struct BC4PushConstant {
  63. uint32_t channel_idx;
  64. uint32_t padding[3];
  65. };
  66. void free_device();
  67. Error _betsy_compress_bptc(Image *r_img, Image::UsedChannels p_channels);
  68. Error _betsy_compress_s3tc(Image *r_img, Image::UsedChannels p_channels);
  69. class BetsyCompressor : public Object {
  70. mutable CommandQueueMT command_queue;
  71. bool exit = false;
  72. WorkerThreadPool::TaskID task_id = WorkerThreadPool::INVALID_TASK_ID;
  73. struct BetsyShader {
  74. RID compiled;
  75. RID pipeline;
  76. };
  77. // Resources shared by all compression formats.
  78. RenderingDevice *compress_rd = nullptr;
  79. RenderingContextDriver *compress_rcd = nullptr;
  80. HashMap<String, BetsyShader> cached_shaders;
  81. RID src_sampler;
  82. // Format-specific resources.
  83. RID dxt1_encoding_table_buffer;
  84. void _init();
  85. void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);
  86. void _thread_loop();
  87. void _thread_exit();
  88. Error _compress(BetsyFormat p_format, Image *r_img);
  89. public:
  90. void init();
  91. void finish();
  92. Error compress(BetsyFormat p_format, Image *r_img) {
  93. Error err;
  94. command_queue.push_and_ret(this, &BetsyCompressor::_compress, p_format, r_img, &err);
  95. return err;
  96. }
  97. };
  98. #endif // IMAGE_COMPRESS_BETSY_H