image_compress_betsy.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_BC5_SIGNED,
  51. BETSY_FORMAT_BC5_UNSIGNED,
  52. BETSY_FORMAT_BC6_SIGNED,
  53. BETSY_FORMAT_BC6_UNSIGNED,
  54. BETSY_FORMAT_MAX,
  55. };
  56. enum BetsyShaderType {
  57. BETSY_SHADER_BC1_STANDARD,
  58. BETSY_SHADER_BC1_DITHER,
  59. BETSY_SHADER_BC4_SIGNED,
  60. BETSY_SHADER_BC4_UNSIGNED,
  61. BETSY_SHADER_BC6_SIGNED,
  62. BETSY_SHADER_BC6_UNSIGNED,
  63. BETSY_SHADER_ALPHA_STITCH,
  64. BETSY_SHADER_MAX,
  65. };
  66. struct BC6PushConstant {
  67. float sizeX;
  68. float sizeY;
  69. uint32_t padding[2] = { 0 };
  70. };
  71. struct BC1PushConstant {
  72. uint32_t num_refines;
  73. uint32_t padding[3] = { 0 };
  74. };
  75. struct BC4PushConstant {
  76. uint32_t channel_idx;
  77. uint32_t padding[3] = { 0 };
  78. };
  79. void free_device();
  80. Error _betsy_compress_bptc(Image *r_img, Image::UsedChannels p_channels);
  81. Error _betsy_compress_s3tc(Image *r_img, Image::UsedChannels p_channels);
  82. class BetsyCompressor : public Object {
  83. mutable CommandQueueMT command_queue;
  84. bool exit = false;
  85. WorkerThreadPool::TaskID task_id = WorkerThreadPool::INVALID_TASK_ID;
  86. struct BetsyShader {
  87. RID compiled;
  88. RID pipeline;
  89. };
  90. // Resources shared by all compression formats.
  91. RenderingDevice *compress_rd = nullptr;
  92. RenderingContextDriver *compress_rcd = nullptr;
  93. BetsyShader cached_shaders[BETSY_SHADER_MAX];
  94. RID src_sampler;
  95. // Format-specific resources.
  96. RID dxt1_encoding_table_buffer;
  97. void _init();
  98. void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);
  99. void _thread_loop();
  100. void _thread_exit();
  101. Error _get_shader(BetsyFormat p_format, const String &p_version, BetsyShader &r_shader);
  102. Error _compress(BetsyFormat p_format, Image *r_img);
  103. public:
  104. void init();
  105. void finish();
  106. Error compress(BetsyFormat p_format, Image *r_img) {
  107. Error err;
  108. command_queue.push_and_ret(this, &BetsyCompressor::_compress, p_format, r_img, &err);
  109. return err;
  110. }
  111. };
  112. #endif // IMAGE_COMPRESS_BETSY_H