2019-11-05 11:01:00 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* texture_basisu.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2019-11-05 11:01:00 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
#include "texture_basisu.h"
|
2019-09-27 02:16:44 +00:00
|
|
|
#if 0
|
|
|
|
#include "core/os/os.h"
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2021-05-07 15:00:41 +00:00
|
|
|
#include <encoder/basisu_comp.h>
|
2019-09-27 02:16:44 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
#include <transcoder/basisu_transcoder.h>
|
2019-09-27 02:16:44 +00:00
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
void TextureBasisU::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_basisu_data", "data"), &TextureBasisU::set_basisu_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_basisu_data"), &TextureBasisU::get_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("import"), &TextureBasisU::import);
|
2019-09-27 02:16:44 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "basisu_data"), "set_basisu_data", "get_basisu_data");
|
2019-09-27 02:16:44 +00:00
|
|
|
};
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
int TextureBasisU::get_width() const {
|
2019-09-27 02:16:44 +00:00
|
|
|
return tex_size.x;
|
|
|
|
};
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
int TextureBasisU::get_height() const {
|
2019-09-27 02:16:44 +00:00
|
|
|
return tex_size.y;
|
|
|
|
};
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
RID TextureBasisU::get_rid() const {
|
2019-09-27 02:16:44 +00:00
|
|
|
return texture;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
bool TextureBasisU::has_alpha() const {
|
2019-09-27 02:16:44 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
void TextureBasisU::set_flags(uint32_t p_flags) {
|
2019-09-27 02:16:44 +00:00
|
|
|
flags = p_flags;
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->texture_set_flags(texture, p_flags);
|
2019-09-27 02:16:44 +00:00
|
|
|
};
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
uint32_t TextureBasisU::get_flags() const {
|
2019-09-27 02:16:44 +00:00
|
|
|
return flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
void TextureBasisU::set_basisu_data(const Vector<uint8_t>& p_data) {
|
2019-09-27 02:16:44 +00:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
data = p_data;
|
|
|
|
#endif
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
const uint8_t* r = p_data.ptr();
|
2019-09-27 02:16:44 +00:00
|
|
|
const void* ptr = r.ptr();
|
|
|
|
int size = p_data.size();
|
|
|
|
|
|
|
|
basist::transcoder_texture_format format;
|
|
|
|
Image::Format imgfmt;
|
|
|
|
|
|
|
|
if (OS::get_singleton()->has_feature("s3tc")) {
|
|
|
|
format = basist::cTFBC3; // get this from renderer
|
|
|
|
imgfmt = Image::FORMAT_DXT5;
|
|
|
|
|
|
|
|
} else if (OS::get_singleton()->has_feature("etc2")) {
|
|
|
|
format = basist::cTFETC2;
|
|
|
|
imgfmt = Image::FORMAT_ETC2_RGBA8;
|
|
|
|
};
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
basist::basisu_transcoder tr(nullptr);
|
2019-09-27 02:16:44 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!tr.validate_header(ptr, size));
|
|
|
|
|
|
|
|
basist::basisu_image_info info;
|
|
|
|
tr.get_image_info(ptr, size, info, 0);
|
|
|
|
tex_size = Size2(info.m_width, info.m_height);
|
|
|
|
|
|
|
|
int block_size = basist::basis_get_bytes_per_block(format);
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> gpudata;
|
2019-09-27 02:16:44 +00:00
|
|
|
gpudata.resize(info.m_total_blocks * block_size);
|
|
|
|
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t* w = gpudata.ptrw();
|
2019-09-27 02:16:44 +00:00
|
|
|
uint8_t* dst = w.ptr();
|
|
|
|
for (int i=0; i<gpudata.size(); i++)
|
|
|
|
dst[i] = 0x00;
|
|
|
|
|
|
|
|
int ofs = 0;
|
|
|
|
tr.start_transcoding(ptr, size);
|
|
|
|
for (int i=0; i<info.m_total_levels; i++) {
|
|
|
|
basist::basisu_image_level_info level;
|
|
|
|
tr.get_image_level_info(ptr, size, level, 0, i);
|
|
|
|
|
|
|
|
bool ret = tr.transcode_image_level(ptr, size, 0, i, dst + ofs, level.m_total_blocks - i, format);
|
|
|
|
if (!ret) {
|
|
|
|
printf("failed! on level %i\n", i);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
ofs += level.m_total_blocks * block_size;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Ref<Image> img;
|
2021-06-17 22:03:09 +00:00
|
|
|
img.instantiate();
|
2019-09-27 02:16:44 +00:00
|
|
|
img->create(info.m_width, info.m_height, info.m_total_levels > 1, imgfmt, gpudata);
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->texture_allocate(texture, tex_size.x, tex_size.y, 0, img->get_format(), RS::TEXTURE_TYPE_2D, flags);
|
|
|
|
RenderingServer::get_singleton()->texture_set_data(texture, img);
|
2019-09-27 02:16:44 +00:00
|
|
|
};
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
Error TextureBasisU::import(const Ref<Image>& p_img) {
|
2019-09-27 02:16:44 +00:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> budata;
|
2019-09-27 02:16:44 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
Image::Format format = p_img->get_format();
|
|
|
|
if (format != Image::FORMAT_RGB8 && format != Image::FORMAT_RGBA8) {
|
|
|
|
ERR_FAIL_V(ERR_INVALID_PARAMETER);
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
};
|
|
|
|
|
|
|
|
Ref<Image> copy = p_img->duplicate();
|
|
|
|
if (format == Image::FORMAT_RGB8)
|
|
|
|
copy->convert(Image::FORMAT_RGBA8);
|
|
|
|
|
|
|
|
basisu::image buimg(p_img->get_width(), p_img->get_height());
|
|
|
|
int size = p_img->get_width() * p_img->get_height() * 4;
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> vec = copy->get_data();
|
2019-09-27 02:16:44 +00:00
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
const uint8_t* r = vec.ptr();
|
2019-09-27 02:16:44 +00:00
|
|
|
memcpy(buimg.get_ptr(), r.ptr(), size);
|
|
|
|
};
|
|
|
|
|
|
|
|
basisu::basis_compressor_params params;
|
|
|
|
params.m_max_endpoint_clusters = 512;
|
|
|
|
params.m_max_selector_clusters = 512;
|
|
|
|
params.m_multithreading = true;
|
|
|
|
|
|
|
|
basisu::job_pool jpool(1);
|
|
|
|
params.m_pJob_pool = &jpool;
|
|
|
|
|
|
|
|
params.m_mip_gen = p_img->get_mipmap_count() > 0;
|
|
|
|
params.m_source_images.push_back(buimg);
|
|
|
|
|
|
|
|
basisu::basis_compressor c;
|
|
|
|
c.init(params);
|
|
|
|
|
|
|
|
int buerr = c.process();
|
|
|
|
if (buerr != basisu::basis_compressor::cECSuccess) {
|
|
|
|
ERR_FAIL_V(ERR_INVALID_PARAMETER);
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
};
|
|
|
|
|
|
|
|
const basisu::uint8_vec& buvec = c.get_output_basis_file();
|
|
|
|
budata.resize(buvec.size());
|
|
|
|
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t* w = budata.ptrw();
|
2019-09-27 02:16:44 +00:00
|
|
|
memcpy(w.ptr(), &buvec[0], budata.size());
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
set_basisu_data(budata);
|
2019-09-27 02:16:44 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
#else
|
|
|
|
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> TextureBasisU::get_basisu_data() const {
|
2019-09-27 02:16:44 +00:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
TextureBasisU::TextureBasisU() {
|
2020-03-27 18:21:27 +00:00
|
|
|
texture = RenderingServer::get_singleton()->texture_create();
|
2019-09-27 02:16:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-09-27 12:43:04 +00:00
|
|
|
TextureBasisU::~TextureBasisU() {
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->free(texture);
|
2019-09-27 02:16:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|