| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/io/image.h" |
| 34 |
#include "core/object/worker_thread_pool.h" |
| 35 |
#include "core/os/thread.h" |
| 36 |
#include "core/templates/command_queue_mt.h" |
| 37 |
|
| 38 |
#include "servers/rendering/rendering_device_binds.h" |
| 39 |
#include "servers/rendering/rendering_server_default.h" |
| 40 |
|
| 41 |
#if defined(VULKAN_ENABLED) |
| 42 |
#include "drivers/vulkan/rendering_context_driver_vulkan.h" |
| 43 |
#endif |
| 44 |
#if defined(METAL_ENABLED) |
| 45 |
#include "drivers/metal/rendering_context_driver_metal.h" |
| 46 |
#endif |
| 47 |
|
| 48 |
enum BetsyFormat { |
| 49 |
BETSY_FORMAT_BC1, |
| 50 |
BETSY_FORMAT_BC1_DITHER, |
| 51 |
BETSY_FORMAT_BC3, |
| 52 |
BETSY_FORMAT_BC4_SIGNED, |
| 53 |
BETSY_FORMAT_BC4_UNSIGNED, |
| 54 |
BETSY_FORMAT_BC5_SIGNED, |
| 55 |
BETSY_FORMAT_BC5_UNSIGNED, |
| 56 |
BETSY_FORMAT_BC6_SIGNED, |
| 57 |
BETSY_FORMAT_BC6_UNSIGNED, |
| 58 |
BETSY_FORMAT_MAX, |
| 59 |
}; |
| 60 |
|
| 61 |
enum BetsyShaderType { |
| 62 |
BETSY_SHADER_BC1_STANDARD, |
| 63 |
BETSY_SHADER_BC1_DITHER, |
| 64 |
BETSY_SHADER_BC4_SIGNED, |
| 65 |
BETSY_SHADER_BC4_UNSIGNED, |
| 66 |
BETSY_SHADER_BC6_SIGNED, |
| 67 |
BETSY_SHADER_BC6_UNSIGNED, |
| 68 |
BETSY_SHADER_ALPHA_STITCH, |
| 69 |
BETSY_SHADER_RGB_TO_RGBA_FLOAT, |
| 70 |
BETSY_SHADER_RGB_TO_RGBA_HALF, |
| 71 |
BETSY_SHADER_RGB_TO_RGBA_UNORM8, |
| 72 |
BETSY_SHADER_RGB_TO_RGBA_UNORM16, |
| 73 |
BETSY_SHADER_MAX, |
| 74 |
}; |
| 75 |
|
| 76 |
struct BC6PushConstant { |
| 77 |
float sizeX; |
| 78 |
float sizeY; |
| 79 |
uint32_t padding[2] = { 0 }; |
| 80 |
}; |
| 81 |
|
| 82 |
struct BC1PushConstant { |
| 83 |
uint32_t num_refines; |
| 84 |
uint32_t padding[3] = { 0 }; |
| 85 |
}; |
| 86 |
|
| 87 |
struct BC4PushConstant { |
| 88 |
uint32_t channel_idx; |
| 89 |
uint32_t padding[3] = { 0 }; |
| 90 |
}; |
| 91 |
|
| 92 |
struct RGBToRGBAPushConstant { |
| 93 |
uint32_t width; |
| 94 |
uint32_t height; |
| 95 |
uint32_t padding[2]; |
| 96 |
}; |
| 97 |
|
| 98 |
void free_device(); |
| 99 |
|
| 100 |
Error _betsy_compress_bptc(Image *r_img, Image::UsedChannels p_channels); |
| 101 |
Error _betsy_compress_s3tc(Image *r_img, Image::UsedChannels p_channels); |
| 102 |
|
| 103 |
class BetsyCompressor : public Object { |
| 104 |
GDSOFTCLASS(BetsyCompressor, Object); |
| 105 |
|
| 106 |
mutable CommandQueueMT command_queue; |
| 107 |
bool exit = false; |
| 108 |
WorkerThreadPool::TaskID task_id = WorkerThreadPool::INVALID_TASK_ID; |
| 109 |
|
| 110 |
struct BetsyShader { |
| 111 |
RID compiled; |
| 112 |
RID pipeline; |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
RenderingDevice *compress_rd = nullptr; |
| 117 |
RenderingContextDriver *compress_rcd = nullptr; |
| 118 |
BetsyShader cached_shaders[BETSY_SHADER_MAX]; |
| 119 |
RID src_sampler; |
| 120 |
|
| 121 |
|
| 122 |
RID dxt1_encoding_table_buffer; |
| 123 |
|
| 124 |
void _init(); |
| 125 |
void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id); |
| 126 |
void _thread_loop(); |
| 127 |
void _thread_exit(); |
| 128 |
|
| 129 |
Error _get_shader(BetsyFormat p_format, const String &p_version, BetsyShader &r_shader); |
| 130 |
Error _compress(BetsyFormat p_format, Image *r_img); |
| 131 |
|
| 132 |
public: |
| 133 |
void init(); |
| 134 |
void finish(); |
| 135 |
|
| 136 |
Error compress(BetsyFormat p_format, Image *r_img) { |
| 137 |
Error err; |
| 138 |
command_queue.push_and_ret(this, &BetsyCompressor::_compress, &err, p_format, r_img); |
| 139 |
return err; |
| 140 |
} |
| 141 |
}; |