0.2.0-dev Download ZIP
134 lines ·4.5 KB Raw
| 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 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "core/io/image.h" |
| 34 | #include "core/object/worker_thread_pool.h" |
| 35 | #include "core/templates/command_queue_mt.h" |
| 36 | |
| 37 | class RDShaderFile; |
| 38 | class RenderingDevice; |
| 39 | class RenderingContextDriver; |
| 40 | |
| 41 | enum BetsyFormat { |
| 42 | BETSY_FORMAT_BC1, |
| 43 | BETSY_FORMAT_BC1_DITHER, |
| 44 | BETSY_FORMAT_BC3, |
| 45 | BETSY_FORMAT_BC4_SIGNED, |
| 46 | BETSY_FORMAT_BC4_UNSIGNED, |
| 47 | BETSY_FORMAT_BC5_SIGNED, |
| 48 | BETSY_FORMAT_BC5_UNSIGNED, |
| 49 | BETSY_FORMAT_BC6_SIGNED, |
| 50 | BETSY_FORMAT_BC6_UNSIGNED, |
| 51 | BETSY_FORMAT_MAX, |
| 52 | }; |
| 53 | |
| 54 | enum BetsyShaderType { |
| 55 | BETSY_SHADER_BC1_STANDARD, |
| 56 | BETSY_SHADER_BC1_DITHER, |
| 57 | BETSY_SHADER_BC4_SIGNED, |
| 58 | BETSY_SHADER_BC4_UNSIGNED, |
| 59 | BETSY_SHADER_BC6_SIGNED, |
| 60 | BETSY_SHADER_BC6_UNSIGNED, |
| 61 | BETSY_SHADER_ALPHA_STITCH, |
| 62 | BETSY_SHADER_RGB_TO_RGBA_FLOAT, |
| 63 | BETSY_SHADER_RGB_TO_RGBA_HALF, |
| 64 | BETSY_SHADER_RGB_TO_RGBA_UNORM8, |
| 65 | BETSY_SHADER_RGB_TO_RGBA_UNORM16, |
| 66 | BETSY_SHADER_MAX, |
| 67 | }; |
| 68 | |
| 69 | struct BC6PushConstant { |
| 70 | float sizeX; |
| 71 | float sizeY; |
| 72 | uint32_t padding[2] = { 0 }; |
| 73 | }; |
| 74 | |
| 75 | struct BC1PushConstant { |
| 76 | uint32_t num_refines; |
| 77 | uint32_t padding[3] = { 0 }; |
| 78 | }; |
| 79 | |
| 80 | struct BC4PushConstant { |
| 81 | uint32_t channel_idx; |
| 82 | uint32_t padding[3] = { 0 }; |
| 83 | }; |
| 84 | |
| 85 | struct RGBToRGBAPushConstant { |
| 86 | uint32_t width; |
| 87 | uint32_t height; |
| 88 | uint32_t padding[2]; |
| 89 | }; |
| 90 | |
| 91 | void free_device(); |
| 92 | |
| 93 | Error _betsy_compress_bptc(Image *r_img, Image::UsedChannels p_channels, Image::BPTCFormat p_bptc_format); |
| 94 | Error _betsy_compress_s3tc(Image *r_img, Image::UsedChannels p_channels); |
| 95 | |
| 96 | class BetsyCompressor : public Object { |
| 97 | GDSOFTCLASS(BetsyCompressor, Object); |
| 98 | |
| 99 | mutable CommandQueueMT command_queue; |
| 100 | bool exit = false; |
| 101 | WorkerThreadPool::TaskID task_id = WorkerThreadPool::INVALID_TASK_ID; |
| 102 | |
| 103 | struct BetsyShader { |
| 104 | RID compiled; |
| 105 | RID pipeline; |
| 106 | }; |
| 107 | |
| 108 | // Resources shared by all compression formats. |
| 109 | RenderingDevice *compress_rd = nullptr; |
| 110 | RenderingContextDriver *compress_rcd = nullptr; |
| 111 | BetsyShader cached_shaders[BETSY_SHADER_MAX]; |
| 112 | RID src_sampler; |
| 113 | |
| 114 | // Format-specific resources. |
| 115 | RID dxt1_encoding_table_buffer; |
| 116 | |
| 117 | void _init(); |
| 118 | void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id); |
| 119 | void _thread_loop(); |
| 120 | void _thread_exit(); |
| 121 | |
| 122 | Error _get_shader(BetsyFormat p_format, const String &p_version, BetsyShader &r_shader); |
| 123 | Error _compress(BetsyFormat p_format, Image *r_img); |
| 124 | |
| 125 | public: |
| 126 | void init(); |
| 127 | void finish(); |
| 128 | |
| 129 | Error compress(BetsyFormat p_format, Image *r_img) { |
| 130 | Error err; |
| 131 | command_queue.push_and_ret(this, &BetsyCompressor::_compress, &err, p_format, r_img); |
| 132 | return err; |
| 133 | } |
| 134 | }; |