feat: modules moved and engine moved to submodule
This commit is contained in:
parent
dfb5e645cd
commit
c33d2130cc
5136 changed files with 225275 additions and 64485 deletions
|
|
@ -89,12 +89,36 @@ float CalcMSLE(float3 a, float3 b) {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Adapt the log function to make sense when a < 0
|
||||
float3 customLog2(float3 a) {
|
||||
return float3(
|
||||
a.x >= 0 ? log2(a.x + 1.0f) : -log2(-a.x + 1.0f),
|
||||
a.y >= 0 ? log2(a.y + 1.0f) : -log2(-a.y + 1.0f),
|
||||
a.z >= 0 ? log2(a.z + 1.0f) : -log2(-a.z + 1.0f));
|
||||
}
|
||||
|
||||
// Inverse of customLog2()
|
||||
float3 customExp2(float3 a) {
|
||||
return float3(
|
||||
a.x >= 0 ? exp2(a.x) - 1.0f : -(exp2(-a.x) - 1.0f),
|
||||
a.y >= 0 ? exp2(a.y) - 1.0f : -(exp2(-a.y) - 1.0f),
|
||||
a.z >= 0 ? exp2(a.z) - 1.0f : -(exp2(-a.z) - 1.0f));
|
||||
}
|
||||
#else
|
||||
float CalcMSLE(float3 a, float3 b) {
|
||||
float3 err = log2((b + 1.0f) / (a + 1.0f));
|
||||
err = err * err;
|
||||
return err.x + err.y + err.z;
|
||||
}
|
||||
|
||||
float3 customLog2(float3 a) {
|
||||
return log2(a + 1.0f);
|
||||
}
|
||||
|
||||
float3 customExp2(float3 a) {
|
||||
return exp2(a) - 1.0f;
|
||||
}
|
||||
#endif
|
||||
|
||||
uint PatternFixupID(uint i) {
|
||||
|
|
@ -272,15 +296,15 @@ void EncodeP1(inout uint4 block, inout float blockMSLE, float3 texels[16]) {
|
|||
refinedBlockMax = max(refinedBlockMax, texels[i] == blockMax ? refinedBlockMax : texels[i]);
|
||||
}
|
||||
|
||||
float3 logBlockMax = log2(blockMax + 1.0f);
|
||||
float3 logBlockMin = log2(blockMin + 1.0f);
|
||||
float3 logRefinedBlockMax = log2(refinedBlockMax + 1.0f);
|
||||
float3 logRefinedBlockMin = log2(refinedBlockMin + 1.0f);
|
||||
float3 logBlockMax = customLog2(blockMax);
|
||||
float3 logBlockMin = customLog2(blockMin);
|
||||
float3 logRefinedBlockMax = customLog2(refinedBlockMax);
|
||||
float3 logRefinedBlockMin = customLog2(refinedBlockMin);
|
||||
float3 logBlockMaxExt = (logBlockMax - logBlockMin) * (1.0f / 32.0f);
|
||||
logBlockMin += min(logRefinedBlockMin - logBlockMin, logBlockMaxExt);
|
||||
logBlockMax -= min(logBlockMax - logRefinedBlockMax, logBlockMaxExt);
|
||||
blockMin = exp2(logBlockMin) - 1.0f;
|
||||
blockMax = exp2(logBlockMax) - 1.0f;
|
||||
blockMin = customExp2(logBlockMin);
|
||||
blockMax = customExp2(logBlockMax);
|
||||
|
||||
float3 blockDir = blockMax - blockMin;
|
||||
blockDir = blockDir / (blockDir.x + blockDir.y + blockDir.z);
|
||||
|
|
|
|||
|
|
@ -28,8 +28,7 @@
|
|||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef BETSY_BC1_H
|
||||
#define BETSY_BC1_H
|
||||
#pragma once
|
||||
|
||||
constexpr const float dxt1_encoding_table[1024] = {
|
||||
0,
|
||||
|
|
@ -1057,5 +1056,3 @@ constexpr const float dxt1_encoding_table[1024] = {
|
|||
63,
|
||||
63,
|
||||
};
|
||||
|
||||
#endif // BETSY_BC1_H
|
||||
|
|
|
|||
|
|
@ -259,6 +259,14 @@ void BetsyCompressor::_thread_exit() {
|
|||
compress_rd->free(cached_shaders[i].compiled);
|
||||
}
|
||||
}
|
||||
|
||||
// Free the RD (and RCD if necessary).
|
||||
memdelete(compress_rd);
|
||||
compress_rd = nullptr;
|
||||
if (compress_rcd != nullptr) {
|
||||
memdelete(compress_rcd);
|
||||
compress_rcd = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -268,16 +276,6 @@ void BetsyCompressor::finish() {
|
|||
WorkerThreadPool::get_singleton()->wait_for_task_completion(task_id);
|
||||
task_id = WorkerThreadPool::INVALID_TASK_ID;
|
||||
}
|
||||
|
||||
if (compress_rd != nullptr) {
|
||||
// Free the RD (and RCD if necessary).
|
||||
memdelete(compress_rd);
|
||||
compress_rd = nullptr;
|
||||
if (compress_rcd != nullptr) {
|
||||
memdelete(compress_rcd);
|
||||
compress_rcd = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper functions.
|
||||
|
|
@ -373,6 +371,13 @@ Error BetsyCompressor::_compress(BetsyFormat p_format, Image *r_img) {
|
|||
return ERR_INVALID_DATA;
|
||||
}
|
||||
|
||||
int img_width = r_img->get_width();
|
||||
int img_height = r_img->get_height();
|
||||
if (img_width % 4 != 0 || img_height % 4 != 0) {
|
||||
img_width = img_width <= 2 ? img_width : (img_width + 3) & ~3;
|
||||
img_height = img_height <= 2 ? img_height : (img_height + 3) & ~3;
|
||||
}
|
||||
|
||||
Error err = OK;
|
||||
|
||||
// Destination format.
|
||||
|
|
@ -443,7 +448,7 @@ Error BetsyCompressor::_compress(BetsyFormat p_format, Image *r_img) {
|
|||
|
||||
// Container for the compressed data.
|
||||
Vector<uint8_t> dst_data;
|
||||
dst_data.resize(Image::get_image_data_size(r_img->get_width(), r_img->get_height(), dest_format, r_img->has_mipmaps()));
|
||||
dst_data.resize(Image::get_image_data_size(img_width, img_height, dest_format, r_img->has_mipmaps()));
|
||||
uint8_t *dst_data_ptr = dst_data.ptrw();
|
||||
|
||||
Vector<Vector<uint8_t>> src_images;
|
||||
|
|
@ -452,9 +457,12 @@ Error BetsyCompressor::_compress(BetsyFormat p_format, Image *r_img) {
|
|||
|
||||
// Compress each mipmap.
|
||||
for (int i = 0; i < mip_count; i++) {
|
||||
int64_t ofs, size;
|
||||
int width, height;
|
||||
r_img->get_mipmap_offset_size_and_dimensions(i, ofs, size, width, height);
|
||||
Image::get_image_mipmap_offset_and_dimensions(img_width, img_height, dest_format, i, width, height);
|
||||
|
||||
int64_t src_mip_ofs, src_mip_size;
|
||||
int src_mip_w, src_mip_h;
|
||||
r_img->get_mipmap_offset_size_and_dimensions(i, src_mip_ofs, src_mip_size, src_mip_w, src_mip_h);
|
||||
|
||||
// Set the source texture width and size.
|
||||
src_texture_format.height = height;
|
||||
|
|
@ -464,9 +472,38 @@ Error BetsyCompressor::_compress(BetsyFormat p_format, Image *r_img) {
|
|||
dst_texture_format.height = (height + 3) >> 2;
|
||||
dst_texture_format.width = (width + 3) >> 2;
|
||||
|
||||
// Create a buffer filled with the source mip layer data.
|
||||
src_image_ptr[0].resize(size);
|
||||
memcpy(src_image_ptr[0].ptrw(), r_img->ptr() + ofs, size);
|
||||
// Pad textures to nearest block by smearing.
|
||||
if (width != src_mip_w || height != src_mip_h) {
|
||||
const uint8_t *src_mip_read = r_img->ptr() + src_mip_ofs;
|
||||
|
||||
// Reserve the buffer for padded image data.
|
||||
int px_size = Image::get_format_pixel_size(r_img->get_format());
|
||||
src_image_ptr[0].resize(width * height * px_size);
|
||||
uint8_t *ptrw = src_image_ptr[0].ptrw();
|
||||
|
||||
int x = 0, y = 0;
|
||||
for (y = 0; y < src_mip_h; y++) {
|
||||
for (x = 0; x < src_mip_w; x++) {
|
||||
memcpy(ptrw + (width * y + x) * px_size, src_mip_read + (src_mip_w * y + x) * px_size, px_size);
|
||||
}
|
||||
|
||||
// First, smear in x.
|
||||
for (; x < width; x++) {
|
||||
memcpy(ptrw + (width * y + x) * px_size, ptrw + (width * y + x - 1) * px_size, px_size);
|
||||
}
|
||||
}
|
||||
|
||||
// Then, smear in y.
|
||||
for (; y < height; y++) {
|
||||
for (x = 0; x < width; x++) {
|
||||
memcpy(ptrw + (width * y + x) * px_size, ptrw + (width * y + x - width) * px_size, px_size);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Create a buffer filled with the source mip layer data.
|
||||
src_image_ptr[0].resize(src_mip_size);
|
||||
memcpy(src_image_ptr[0].ptrw(), r_img->ptr() + src_mip_ofs, src_mip_size);
|
||||
}
|
||||
|
||||
// Create the textures on the GPU.
|
||||
RID src_texture = compress_rd->texture_create(src_texture_format, RD::TextureView(), src_images);
|
||||
|
|
@ -646,7 +683,7 @@ Error BetsyCompressor::_compress(BetsyFormat p_format, Image *r_img) {
|
|||
|
||||
// Copy data from the GPU to the buffer.
|
||||
const Vector<uint8_t> texture_data = compress_rd->texture_get_data(dst_texture_rid, 0);
|
||||
int64_t dst_ofs = Image::get_image_mipmap_offset(r_img->get_width(), r_img->get_height(), dest_format, i);
|
||||
int64_t dst_ofs = Image::get_image_mipmap_offset(img_width, img_height, dest_format, i);
|
||||
|
||||
memcpy(dst_data_ptr + dst_ofs, texture_data.ptr(), texture_data.size());
|
||||
|
||||
|
|
@ -658,12 +695,12 @@ Error BetsyCompressor::_compress(BetsyFormat p_format, Image *r_img) {
|
|||
src_images.clear();
|
||||
|
||||
// Set the compressed data to the image.
|
||||
r_img->set_data(r_img->get_width(), r_img->get_height(), r_img->has_mipmaps(), dest_format, dst_data);
|
||||
r_img->set_data(img_width, img_height, r_img->has_mipmaps(), dest_format, dst_data);
|
||||
|
||||
print_verbose(
|
||||
vformat("Betsy: Encoding a %dx%d image with %d mipmaps as %s took %d ms.",
|
||||
r_img->get_width(),
|
||||
r_img->get_height(),
|
||||
img_width,
|
||||
img_height,
|
||||
r_img->get_mipmap_count(),
|
||||
Image::get_format_name(dest_format),
|
||||
OS::get_singleton()->get_ticks_msec() - start_time));
|
||||
|
|
|
|||
|
|
@ -28,8 +28,7 @@
|
|||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef IMAGE_COMPRESS_BETSY_H
|
||||
#define IMAGE_COMPRESS_BETSY_H
|
||||
#pragma once
|
||||
|
||||
#include "core/io/image.h"
|
||||
#include "core/object/worker_thread_pool.h"
|
||||
|
|
@ -128,5 +127,3 @@ public:
|
|||
return err;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // IMAGE_COMPRESS_BETSY_H
|
||||
|
|
|
|||
|
|
@ -28,12 +28,9 @@
|
|||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef BETSY_REGISTER_TYPES_H
|
||||
#define BETSY_REGISTER_TYPES_H
|
||||
#pragma once
|
||||
|
||||
#include "modules/register_module_types.h"
|
||||
|
||||
void initialize_betsy_module(ModuleInitializationLevel p_level);
|
||||
void uninitialize_betsy_module(ModuleInitializationLevel p_level);
|
||||
|
||||
#endif // BETSY_REGISTER_TYPES_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue