texture_basisu.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*************************************************************************/
  2. /* texture_basisu.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "texture_basisu.h"
  31. #if 0
  32. #include "core/os/os.h"
  33. #ifdef TOOLS_ENABLED
  34. #include <basisu_comp.h>
  35. #endif
  36. #include <transcoder/basisu_transcoder.h>
  37. void TextureBasisU::_bind_methods() {
  38. ClassDB::bind_method(D_METHOD("set_basisu_data", "data"), &TextureBasisU::set_basisu_data);
  39. ClassDB::bind_method(D_METHOD("get_basisu_data"), &TextureBasisU::get_data);
  40. ClassDB::bind_method(D_METHOD("import"), &TextureBasisU::import);
  41. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "basisu_data"), "set_basisu_data", "get_basisu_data");
  42. };
  43. int TextureBasisU::get_width() const {
  44. return tex_size.x;
  45. };
  46. int TextureBasisU::get_height() const {
  47. return tex_size.y;
  48. };
  49. RID TextureBasisU::get_rid() const {
  50. return texture;
  51. };
  52. bool TextureBasisU::has_alpha() const {
  53. return false;
  54. };
  55. void TextureBasisU::set_flags(uint32_t p_flags) {
  56. flags = p_flags;
  57. RenderingServer::get_singleton()->texture_set_flags(texture, p_flags);
  58. };
  59. uint32_t TextureBasisU::get_flags() const {
  60. return flags;
  61. };
  62. void TextureBasisU::set_basisu_data(const Vector<uint8_t>& p_data) {
  63. #ifdef TOOLS_ENABLED
  64. data = p_data;
  65. #endif
  66. const uint8_t* r = p_data.ptr();
  67. const void* ptr = r.ptr();
  68. int size = p_data.size();
  69. basist::transcoder_texture_format format;
  70. Image::Format imgfmt;
  71. if (OS::get_singleton()->has_feature("s3tc")) {
  72. format = basist::cTFBC3; // get this from renderer
  73. imgfmt = Image::FORMAT_DXT5;
  74. } else if (OS::get_singleton()->has_feature("etc2")) {
  75. format = basist::cTFETC2;
  76. imgfmt = Image::FORMAT_ETC2_RGBA8;
  77. };
  78. basist::basisu_transcoder tr(nullptr);
  79. ERR_FAIL_COND(!tr.validate_header(ptr, size));
  80. basist::basisu_image_info info;
  81. tr.get_image_info(ptr, size, info, 0);
  82. tex_size = Size2(info.m_width, info.m_height);
  83. int block_size = basist::basis_get_bytes_per_block(format);
  84. Vector<uint8_t> gpudata;
  85. gpudata.resize(info.m_total_blocks * block_size);
  86. {
  87. uint8_t* w = gpudata.ptrw();
  88. uint8_t* dst = w.ptr();
  89. for (int i=0; i<gpudata.size(); i++)
  90. dst[i] = 0x00;
  91. int ofs = 0;
  92. tr.start_transcoding(ptr, size);
  93. for (int i=0; i<info.m_total_levels; i++) {
  94. basist::basisu_image_level_info level;
  95. tr.get_image_level_info(ptr, size, level, 0, i);
  96. bool ret = tr.transcode_image_level(ptr, size, 0, i, dst + ofs, level.m_total_blocks - i, format);
  97. if (!ret) {
  98. printf("failed! on level %i\n", i);
  99. break;
  100. };
  101. ofs += level.m_total_blocks * block_size;
  102. };
  103. };
  104. Ref<Image> img;
  105. img.instance();
  106. img->create(info.m_width, info.m_height, info.m_total_levels > 1, imgfmt, gpudata);
  107. RenderingServer::get_singleton()->texture_allocate(texture, tex_size.x, tex_size.y, 0, img->get_format(), RS::TEXTURE_TYPE_2D, flags);
  108. RenderingServer::get_singleton()->texture_set_data(texture, img);
  109. };
  110. Error TextureBasisU::import(const Ref<Image>& p_img) {
  111. #ifdef TOOLS_ENABLED
  112. Vector<uint8_t> budata;
  113. {
  114. Image::Format format = p_img->get_format();
  115. if (format != Image::FORMAT_RGB8 && format != Image::FORMAT_RGBA8) {
  116. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  117. return ERR_INVALID_PARAMETER;
  118. };
  119. Ref<Image> copy = p_img->duplicate();
  120. if (format == Image::FORMAT_RGB8)
  121. copy->convert(Image::FORMAT_RGBA8);
  122. basisu::image buimg(p_img->get_width(), p_img->get_height());
  123. int size = p_img->get_width() * p_img->get_height() * 4;
  124. Vector<uint8_t> vec = copy->get_data();
  125. {
  126. const uint8_t* r = vec.ptr();
  127. memcpy(buimg.get_ptr(), r.ptr(), size);
  128. };
  129. basisu::basis_compressor_params params;
  130. params.m_max_endpoint_clusters = 512;
  131. params.m_max_selector_clusters = 512;
  132. params.m_multithreading = true;
  133. basisu::job_pool jpool(1);
  134. params.m_pJob_pool = &jpool;
  135. params.m_mip_gen = p_img->get_mipmap_count() > 0;
  136. params.m_source_images.push_back(buimg);
  137. basisu::basis_compressor c;
  138. c.init(params);
  139. int buerr = c.process();
  140. if (buerr != basisu::basis_compressor::cECSuccess) {
  141. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  142. return ERR_INVALID_PARAMETER;
  143. };
  144. const basisu::uint8_vec& buvec = c.get_output_basis_file();
  145. budata.resize(buvec.size());
  146. {
  147. uint8_t* w = budata.ptrw();
  148. memcpy(w.ptr(), &buvec[0], budata.size());
  149. };
  150. };
  151. set_basisu_data(budata);
  152. return OK;
  153. #else
  154. return ERR_UNAVAILABLE;
  155. #endif
  156. };
  157. Vector<uint8_t> TextureBasisU::get_basisu_data() const {
  158. return data;
  159. };
  160. TextureBasisU::TextureBasisU() {
  161. texture = RenderingServer::get_singleton()->texture_create();
  162. };
  163. TextureBasisU::~TextureBasisU() {
  164. RenderingServer::get_singleton()->free(texture);
  165. };
  166. #endif