| 1 | /**************************************************************************/ |
| 2 | /* register_types.cpp */ |
| 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 | #include "register_types.h" |
| 32 | |
| 33 | #include "core/os/os.h" |
| 34 | #include "servers/rendering_server.h" |
| 35 | |
| 36 | #include <transcoder/basisu_transcoder.h> |
| 37 | |
| 38 | #ifdef TOOLS_ENABLED |
| 39 | #include <encoder/basisu_comp.h> |
| 40 | #endif |
| 41 | |
| 42 | enum BasisDecompressFormat { |
| 43 | BASIS_DECOMPRESS_RG, |
| 44 | BASIS_DECOMPRESS_RGB, |
| 45 | BASIS_DECOMPRESS_RGBA, |
| 46 | BASIS_DECOMPRESS_RG_AS_RA |
| 47 | }; |
| 48 | |
| 49 | //workaround for lack of ETC2 RG |
| 50 | #define USE_RG_AS_RGBA |
| 51 | |
| 52 | #ifdef TOOLS_ENABLED |
| 53 | static Vector<uint8_t> basis_universal_packer(const Ref<Image> &p_image, Image::UsedChannels p_channels) { |
| 54 | Vector<uint8_t> budata; |
| 55 | { |
| 56 | basisu::basis_compressor_params params; |
| 57 | Ref<Image> image = p_image->duplicate(); |
| 58 | if (image->get_format() != Image::FORMAT_RGBA8) { |
| 59 | image->convert(Image::FORMAT_RGBA8); |
| 60 | } |
| 61 | |
| 62 | params.m_uastc = true; |
| 63 | params.m_quality_level = basisu::BASISU_QUALITY_MIN; |
| 64 | |
| 65 | params.m_pack_uastc_flags &= ~basisu::cPackUASTCLevelMask; |
| 66 | |
| 67 | static const uint32_t s_level_flags[basisu::TOTAL_PACK_UASTC_LEVELS] = { basisu::cPackUASTCLevelFastest, basisu::cPackUASTCLevelFaster, basisu::cPackUASTCLevelDefault, basisu::cPackUASTCLevelSlower, basisu::cPackUASTCLevelVerySlow }; |
| 68 | params.m_pack_uastc_flags |= s_level_flags[0]; |
| 69 | params.m_rdo_uastc = 0.0f; |
| 70 | params.m_rdo_uastc_quality_scalar = 0.0f; |
| 71 | params.m_rdo_uastc_dict_size = 1024; |
| 72 | |
| 73 | params.m_mip_fast = true; |
| 74 | params.m_multithreading = true; |
| 75 | |
| 76 | basisu::job_pool jpool(OS::get_singleton()->get_processor_count()); |
| 77 | params.m_pJob_pool = &jpool; |
| 78 | |
| 79 | BasisDecompressFormat decompress_format = BASIS_DECOMPRESS_RG; |
| 80 | params.m_check_for_alpha = false; |
| 81 | |
| 82 | switch (p_channels) { |
| 83 | case Image::USED_CHANNELS_L: { |
| 84 | decompress_format = BASIS_DECOMPRESS_RGB; |
| 85 | } break; |
| 86 | case Image::USED_CHANNELS_LA: { |
| 87 | params.m_force_alpha = true; |
| 88 | decompress_format = BASIS_DECOMPRESS_RGBA; |
| 89 | } break; |
| 90 | case Image::USED_CHANNELS_R: { |
| 91 | decompress_format = BASIS_DECOMPRESS_RGB; |
| 92 | } break; |
| 93 | case Image::USED_CHANNELS_RG: { |
| 94 | #ifdef USE_RG_AS_RGBA |
| 95 | params.m_force_alpha = true; |
| 96 | image->convert_rg_to_ra_rgba8(); |
| 97 | decompress_format = BASIS_DECOMPRESS_RG_AS_RA; |
| 98 | #else |
| 99 | params.m_seperate_rg_to_color_alpha = true; |
| 100 | decompress_format = BASIS_DECOMPRESS_RG; |
| 101 | #endif |
| 102 | } break; |
| 103 | case Image::USED_CHANNELS_RGB: { |
| 104 | decompress_format = BASIS_DECOMPRESS_RGB; |
| 105 | } break; |
| 106 | case Image::USED_CHANNELS_RGBA: { |
| 107 | params.m_force_alpha = true; |
| 108 | decompress_format = BASIS_DECOMPRESS_RGBA; |
| 109 | } break; |
| 110 | } |
| 111 | |
| 112 | if (!image->has_mipmaps()) { |
| 113 | basisu::image buimg(image->get_width(), image->get_height()); |
| 114 | Vector<uint8_t> vec = image->get_data(); |
| 115 | const uint8_t *r = vec.ptr(); |
| 116 | memcpy(buimg.get_ptr(), r, vec.size()); |
| 117 | params.m_source_images.push_back(buimg); |
| 118 | } else { |
| 119 | { |
| 120 | Ref<Image> base_image = image->get_image_from_mipmap(0); |
| 121 | Vector<uint8_t> image_vec = base_image->get_data(); |
| 122 | basisu::image buimg_image(base_image->get_width(), base_image->get_height()); |
| 123 | const uint8_t *r = image_vec.ptr(); |
| 124 | memcpy(buimg_image.get_ptr(), r, image_vec.size()); |
| 125 | params.m_source_images.push_back(buimg_image); |
| 126 | } |
| 127 | basisu::vector<basisu::image> images; |
| 128 | for (int32_t mip_map_i = 1; mip_map_i <= image->get_mipmap_count(); mip_map_i++) { |
| 129 | Ref<Image> mip_map = image->get_image_from_mipmap(mip_map_i); |
| 130 | Vector<uint8_t> mip_map_vec = mip_map->get_data(); |
| 131 | basisu::image buimg_mipmap(mip_map->get_width(), mip_map->get_height()); |
| 132 | const uint8_t *r = mip_map_vec.ptr(); |
| 133 | memcpy(buimg_mipmap.get_ptr(), r, mip_map_vec.size()); |
| 134 | images.push_back(buimg_mipmap); |
| 135 | } |
| 136 | params.m_source_mipmap_images.push_back(images); |
| 137 | } |
| 138 | |
| 139 | basisu::basis_compressor c; |
| 140 | c.init(params); |
| 141 | |
| 142 | int buerr = c.process(); |
| 143 | ERR_FAIL_COND_V(buerr != basisu::basis_compressor::cECSuccess, budata); |
| 144 | |
| 145 | const basisu::uint8_vec &buvec = c.get_output_basis_file(); |
| 146 | budata.resize(buvec.size() + 4); |
| 147 | |
| 148 | { |
| 149 | uint8_t *w = budata.ptrw(); |
| 150 | uint32_t *decf = (uint32_t *)w; |
| 151 | *decf = decompress_format; |
| 152 | memcpy(w + 4, &buvec[0], buvec.size()); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return budata; |
| 157 | } |
| 158 | #endif // TOOLS_ENABLED |
| 159 | |
| 160 | static Ref<Image> basis_universal_unpacker_ptr(const uint8_t *p_data, int p_size) { |
| 161 | Ref<Image> image; |
| 162 | |
| 163 | const uint8_t *ptr = p_data; |
| 164 | int size = p_size; |
| 165 | ERR_FAIL_COND_V_MSG(p_data == nullptr, image, "Cannot unpack invalid basis universal data." ); |
| 166 | |
| 167 | basist::transcoder_texture_format format = basist::transcoder_texture_format::cTFTotalTextureFormats; |
| 168 | Image::Format imgfmt = Image::FORMAT_MAX; |
| 169 | |
| 170 | switch (*(uint32_t *)(ptr)) { |
| 171 | case BASIS_DECOMPRESS_RG: { |
| 172 | if (RS::get_singleton()->has_os_feature("rgtc" )) { |
| 173 | format = basist::transcoder_texture_format::cTFBC5; // get this from renderer |
| 174 | imgfmt = Image::FORMAT_RGTC_RG; |
| 175 | } else if (RS::get_singleton()->has_os_feature("etc2" )) { |
| 176 | //unfortunately, basis universal does not support |
| 177 | // |
| 178 | ERR_FAIL_V(image); //unimplemented here |
| 179 | //format = basist::transcoder_texture_format::cTFETC1; // get this from renderer |
| 180 | //imgfmt = Image::FORMAT_RGTC_RG; |
| 181 | } else { |
| 182 | // FIXME: There wasn't anything here, but then imgformat is used uninitialized. |
| 183 | ERR_FAIL_V(image); |
| 184 | } |
| 185 | } break; |
| 186 | case BASIS_DECOMPRESS_RGB: { |
| 187 | if (RS::get_singleton()->has_os_feature("bptc" )) { |
| 188 | format = basist::transcoder_texture_format::cTFBC7_M6_OPAQUE_ONLY; // get this from renderer |
| 189 | imgfmt = Image::FORMAT_BPTC_RGBA; |
| 190 | } else if (RS::get_singleton()->has_os_feature("s3tc" )) { |
| 191 | format = basist::transcoder_texture_format::cTFBC1; // get this from renderer |
| 192 | imgfmt = Image::FORMAT_DXT1; |
| 193 | } else if (RS::get_singleton()->has_os_feature("etc" )) { |
| 194 | format = basist::transcoder_texture_format::cTFETC1; // get this from renderer |
| 195 | imgfmt = Image::FORMAT_ETC; |
| 196 | } else { |
| 197 | format = basist::transcoder_texture_format::cTFBGR565; // get this from renderer |
| 198 | imgfmt = Image::FORMAT_RGB565; |
| 199 | } |
| 200 | |
| 201 | } break; |
| 202 | case BASIS_DECOMPRESS_RGBA: { |
| 203 | if (RS::get_singleton()->has_os_feature("bptc" )) { |
| 204 | format = basist::transcoder_texture_format::cTFBC7_M5; // get this from renderer |
| 205 | imgfmt = Image::FORMAT_BPTC_RGBA; |
| 206 | } else if (RS::get_singleton()->has_os_feature("s3tc" )) { |
| 207 | format = basist::transcoder_texture_format::cTFBC3; // get this from renderer |
| 208 | imgfmt = Image::FORMAT_DXT5; |
| 209 | } else if (RS::get_singleton()->has_os_feature("etc2" )) { |
| 210 | format = basist::transcoder_texture_format::cTFETC2; // get this from renderer |
| 211 | imgfmt = Image::FORMAT_ETC2_RGBA8; |
| 212 | } else { |
| 213 | //opengl most likely |
| 214 | format = basist::transcoder_texture_format::cTFRGBA4444; // get this from renderer |
| 215 | imgfmt = Image::FORMAT_RGBA4444; |
| 216 | } |
| 217 | } break; |
| 218 | case BASIS_DECOMPRESS_RG_AS_RA: { |
| 219 | if (RS::get_singleton()->has_os_feature("s3tc" )) { |
| 220 | format = basist::transcoder_texture_format::cTFBC3; // get this from renderer |
| 221 | imgfmt = Image::FORMAT_DXT5_RA_AS_RG; |
| 222 | } else if (RS::get_singleton()->has_os_feature("etc2" )) { |
| 223 | format = basist::transcoder_texture_format::cTFETC2; // get this from renderer |
| 224 | imgfmt = Image::FORMAT_ETC2_RGBA8; |
| 225 | } else { |
| 226 | //opengl most likely, bad for normal maps, nothing to do about this. |
| 227 | format = basist::transcoder_texture_format::cTFRGBA32; |
| 228 | imgfmt = Image::FORMAT_RGBA8; |
| 229 | } |
| 230 | } break; |
| 231 | } |
| 232 | |
| 233 | ptr += 4; |
| 234 | size -= 4; |
| 235 | |
| 236 | basist::basisu_transcoder tr; |
| 237 | |
| 238 | ERR_FAIL_COND_V(!tr.validate_header(ptr, size), image); |
| 239 | |
| 240 | tr.start_transcoding(ptr, size); |
| 241 | |
| 242 | basist::basisu_image_info info; |
| 243 | tr.get_image_info(ptr, size, info, 0); |
| 244 | Vector<uint8_t> gpudata; |
| 245 | gpudata.resize(Image::get_image_data_size(info.m_width, info.m_height, imgfmt, info.m_total_levels > 1)); |
| 246 | |
| 247 | uint8_t *w = gpudata.ptrw(); |
| 248 | uint8_t *dst = w; |
| 249 | for (int i = 0; i < gpudata.size(); i++) { |
| 250 | dst[i] = 0x00; |
| 251 | } |
| 252 | uint32_t mip_count = Image::get_image_required_mipmaps(info.m_orig_width, info.m_orig_height, imgfmt); |
| 253 | for (uint32_t level_i = 0; level_i <= mip_count; level_i++) { |
| 254 | basist::basisu_image_level_info level; |
| 255 | tr.get_image_level_info(ptr, size, level, 0, level_i); |
| 256 | int ofs = Image::get_image_mipmap_offset(info.m_width, info.m_height, imgfmt, level_i); |
| 257 | bool ret = tr.transcode_image_level(ptr, size, 0, level_i, dst + ofs, level.m_total_blocks, format); |
| 258 | if (!ret) { |
| 259 | print_line(vformat("Basis universal cannot unpack level %d." , level_i)); |
| 260 | break; |
| 261 | }; |
| 262 | } |
| 263 | |
| 264 | image = Image::create_from_data(info.m_width, info.m_height, info.m_total_levels > 1, imgfmt, gpudata); |
| 265 | |
| 266 | return image; |
| 267 | } |
| 268 | |
| 269 | static Ref<Image> basis_universal_unpacker(const Vector<uint8_t> &p_buffer) { |
| 270 | Ref<Image> image; |
| 271 | |
| 272 | const uint8_t *r = p_buffer.ptr(); |
| 273 | int size = p_buffer.size(); |
| 274 | return basis_universal_unpacker_ptr(r, size); |
| 275 | } |
| 276 | |
| 277 | void initialize_basis_universal_module(ModuleInitializationLevel p_level) { |
| 278 | if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | #ifdef TOOLS_ENABLED |
| 283 | using namespace basisu; |
| 284 | using namespace basist; |
| 285 | basisu_encoder_init(); |
| 286 | Image::basis_universal_packer = basis_universal_packer; |
| 287 | #endif |
| 288 | basist::basisu_transcoder_init(); |
| 289 | Image::basis_universal_unpacker = basis_universal_unpacker; |
| 290 | Image::basis_universal_unpacker_ptr = basis_universal_unpacker_ptr; |
| 291 | } |
| 292 | |
| 293 | void uninitialize_basis_universal_module(ModuleInitializationLevel p_level) { |
| 294 | if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | #ifdef TOOLS_ENABLED |
| 299 | Image::basis_universal_packer = nullptr; |
| 300 | #endif |
| 301 | Image::basis_universal_unpacker = nullptr; |
| 302 | Image::basis_universal_unpacker_ptr = nullptr; |
| 303 | } |
| 304 | |