diff options
295 files changed, 23178 insertions, 3765 deletions
diff --git a/.gitignore b/.gitignore index 537ed7d32a..84aaefd736 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ core/version_generated.h drivers/gles2/shaders/*.h drivers/gles3/shaders/*.h drivers/unix/os_unix_global_settings_path.cpp +editor/authors.h editor/builtin_fonts.h editor/certs_compressed.h editor/doc_data_compressed.h diff --git a/core/SCsub b/core/SCsub index fd3f57dd7c..da2403f1d3 100644 --- a/core/SCsub +++ b/core/SCsub @@ -83,6 +83,25 @@ thirdparty_minizip_sources = [ thirdparty_minizip_sources = [thirdparty_minizip_dir + file for file in thirdparty_minizip_sources] env.add_source_files(env.core_sources, thirdparty_minizip_sources) +thirdparty_zstd_dir = "#thirdparty/zstd/" +thirdparty_zstd_sources = [ + "common/entropy_common.c", + "common/error_private.c", + "common/fse_decompress.c", + "common/pool.c", + "common/threading.c", + "common/xxhash.c", + "common/zstd_common.c", + "compress/fse_compress.c", + "compress/huf_compress.c", + "compress/zstd_compress.c", + "compress/zstdmt_compress.c", + "decompress/huf_decompress.c", + "decompress/zstd_decompress.c", +] +thirdparty_zstd_sources = [thirdparty_zstd_dir + file for file in thirdparty_zstd_sources] +env.add_source_files(env.core_sources, thirdparty_zstd_sources) + # Godot's own sources env.add_source_files(env.core_sources, "*.cpp") @@ -104,5 +123,5 @@ SConscript('helper/SCsub') # Build it all as a library lib = env.Library("core", env.core_sources) env.Prepend(LIBS=[lib]) - +env.Append(CPPPATH=["#thirdparty/zstd", "#thirdparty/zstd/common"]) Export('env') diff --git a/core/command_queue_mt.h b/core/command_queue_mt.h index bae8cf7fca..83c90a925c 100644 --- a/core/command_queue_mt.h +++ b/core/command_queue_mt.h @@ -170,6 +170,43 @@ class CommandQueueMT { virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8); } }; + template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9> + struct Command9 : public CommandBase { + + T *instance; + M method; + typename GetSimpleTypeT<P1>::type_t p1; + typename GetSimpleTypeT<P2>::type_t p2; + typename GetSimpleTypeT<P3>::type_t p3; + typename GetSimpleTypeT<P4>::type_t p4; + typename GetSimpleTypeT<P5>::type_t p5; + typename GetSimpleTypeT<P6>::type_t p6; + typename GetSimpleTypeT<P7>::type_t p7; + typename GetSimpleTypeT<P8>::type_t p8; + typename GetSimpleTypeT<P9>::type_t p9; + + virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9); } + }; + + template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10> + struct Command10 : public CommandBase { + + T *instance; + M method; + typename GetSimpleTypeT<P1>::type_t p1; + typename GetSimpleTypeT<P2>::type_t p2; + typename GetSimpleTypeT<P3>::type_t p3; + typename GetSimpleTypeT<P4>::type_t p4; + typename GetSimpleTypeT<P5>::type_t p5; + typename GetSimpleTypeT<P6>::type_t p6; + typename GetSimpleTypeT<P7>::type_t p7; + typename GetSimpleTypeT<P8>::type_t p8; + typename GetSimpleTypeT<P9>::type_t p9; + typename GetSimpleTypeT<P10>::type_t p10; + + virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); } + }; + /* comands that return */ template <class T, class M, class R> @@ -779,6 +816,52 @@ public: if (sync) sync->post(); } + + template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9> + void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9) { + + Command9<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9> *cmd = allocate_and_lock<Command9<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9> >(); + + cmd->instance = p_instance; + cmd->method = p_method; + cmd->p1 = p1; + cmd->p2 = p2; + cmd->p3 = p3; + cmd->p4 = p4; + cmd->p5 = p5; + cmd->p6 = p6; + cmd->p7 = p7; + cmd->p8 = p8; + cmd->p9 = p9; + + unlock(); + + if (sync) sync->post(); + } + + template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10> + void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10) { + + Command10<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10> *cmd = allocate_and_lock<Command10<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10> >(); + + cmd->instance = p_instance; + cmd->method = p_method; + cmd->p1 = p1; + cmd->p2 = p2; + cmd->p3 = p3; + cmd->p4 = p4; + cmd->p5 = p5; + cmd->p6 = p6; + cmd->p7 = p7; + cmd->p8 = p8; + cmd->p9 = p9; + cmd->p10 = p10; + + unlock(); + + if (sync) sync->post(); + } + /*** PUSH AND RET COMMANDS ***/ template <class T, class M, class R> diff --git a/core/global_config.cpp b/core/global_config.cpp index 896384d9c8..0729d4c482 100644 --- a/core/global_config.cpp +++ b/core/global_config.cpp @@ -39,6 +39,8 @@ #include "os/os.h" #include "variant_parser.h" +#include <zlib.h> + #define FORMAT_VERSION 3 GlobalConfig *GlobalConfig::singleton = NULL; @@ -967,6 +969,12 @@ GlobalConfig::GlobalConfig() { custom_prop_info["physics/2d/thread_model"] = PropertyInfo(Variant::INT, "physics/2d/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded"); GLOBAL_DEF("debug/profiler/max_functions", 16384); + + GLOBAL_DEF("compression/zstd_compression_level", 3); + custom_prop_info["compression/zstd_compression_level"] = PropertyInfo(Variant::INT, "compression/zstd_compression_level", PROPERTY_HINT_RANGE, "1,22,1"); + GLOBAL_DEF("compression/zlib_compression_level", Z_DEFAULT_COMPRESSION); + custom_prop_info["compression/zlib_compression_level"] = PropertyInfo(Variant::INT, "compression/zlib_compression_level", PROPERTY_HINT_RANGE, "-1,9,1"); + using_datapack = false; } diff --git a/core/helper/math_fieldwise.cpp b/core/helper/math_fieldwise.cpp index 2f176fb9bf..5545c2d642 100644 --- a/core/helper/math_fieldwise.cpp +++ b/core/helper/math_fieldwise.cpp @@ -63,8 +63,8 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const SETUP_TYPE(Rect2) - /**/ TRY_TRANSFER_FIELD("x", pos.x) - else TRY_TRANSFER_FIELD("y", pos.y) + /**/ TRY_TRANSFER_FIELD("x", position.x) + else TRY_TRANSFER_FIELD("y", position.y) else TRY_TRANSFER_FIELD("w", size.x) else TRY_TRANSFER_FIELD("h", size.y) @@ -110,9 +110,9 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const SETUP_TYPE(Rect3) - /**/ TRY_TRANSFER_FIELD("px", pos.x) - else TRY_TRANSFER_FIELD("py", pos.y) - else TRY_TRANSFER_FIELD("pz", pos.z) + /**/ TRY_TRANSFER_FIELD("px", position.x) + else TRY_TRANSFER_FIELD("py", position.y) + else TRY_TRANSFER_FIELD("pz", position.z) else TRY_TRANSFER_FIELD("sx", size.x) else TRY_TRANSFER_FIELD("sy", size.y) else TRY_TRANSFER_FIELD("sz", size.z) diff --git a/core/image.cpp b/core/image.cpp index 8d38846a58..94b01d9332 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -81,21 +81,21 @@ const char *Image::format_names[Image::FORMAT_MAX] = { SavePNGFunc Image::save_png_func = NULL; -void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_dst, const uint8_t *p_src) { +void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel) { uint32_t ofs = (p_y * width + p_x) * p_pixelsize; for (uint32_t i = 0; i < p_pixelsize; i++) { - p_dst[ofs + i] = p_src[i]; + p_data[ofs + i] = p_pixel[i]; } } -void Image::_get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_src, uint8_t *p_dst) { +void Image::_get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel) { uint32_t ofs = (p_y * width + p_x) * p_pixelsize; for (uint32_t i = 0; i < p_pixelsize; i++) { - p_dst[i] = p_src[ofs + i]; + p_pixel[i] = p_data[ofs + i]; } } @@ -818,7 +818,7 @@ void Image::flip_y() { uint8_t down[16]; uint32_t pixel_size = get_format_pixel_size(format); - for (int y = 0; y < height; y++) { + for (int y = 0; y < height / 2; y++) { for (int x = 0; x < width; x++) { @@ -854,7 +854,7 @@ void Image::flip_x() { for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { + for (int x = 0; x < width / 2; x++) { _get_pixelb(x, y, pixel_size, w.ptr(), up); _get_pixelb(width - x - 1, y, pixel_size, w.ptr(), down); @@ -1457,7 +1457,7 @@ Error Image::save_png(const String &p_path) const { if (save_png_func == NULL) return ERR_UNAVAILABLE; - return save_png_func(p_path, Ref<Image>(this)); + return save_png_func(p_path, Ref<Image>((Image *)this)); } int Image::get_image_data_size(int p_width, int p_height, Format p_format, int p_mipmaps) { @@ -1599,7 +1599,7 @@ Rect2 Image::get_used_rect() const { Ref<Image> Image::get_rect(const Rect2 &p_area) const { Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format)); - img->blit_rect(Ref<Image>(this), p_area, Point2(0, 0)); + img->blit_rect(Ref<Image>((Image *)this), p_area, Point2(0, 0)); return img; } @@ -1612,11 +1612,11 @@ void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Po ERR_FAIL_COND(srcdsize == 0); ERR_FAIL_COND(format != p_src->format); - Rect2i local_src_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest + p_src_rect.pos, p_src_rect.size)); + Rect2i local_src_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest + p_src_rect.position, p_src_rect.size)); if (local_src_rect.size.x <= 0 || local_src_rect.size.y <= 0) return; - Rect2i src_rect(p_src_rect.pos + (local_src_rect.pos - p_dest), local_src_rect.size); + Rect2i src_rect(p_src_rect.position + (local_src_rect.position - p_dest), local_src_rect.size); PoolVector<uint8_t>::Write wp = data.write(); uint8_t *dst_data_ptr = wp.ptr(); @@ -1630,11 +1630,11 @@ void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Po for (int j = 0; j < src_rect.size.x; j++) { - int src_x = src_rect.pos.x + j; - int src_y = src_rect.pos.y + i; + int src_x = src_rect.position.x + j; + int src_y = src_rect.position.y + i; - int dst_x = local_src_rect.pos.x + j; - int dst_y = local_src_rect.pos.y + i; + int dst_x = local_src_rect.position.x + j; + int dst_y = local_src_rect.position.y + i; const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size]; uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size]; diff --git a/core/image.h b/core/image.h index cd75dbfcfa..790c5de9f6 100644 --- a/core/image.h +++ b/core/image.h @@ -162,8 +162,8 @@ private: static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1); bool _can_modify(Format p_format) const; - _FORCE_INLINE_ void _put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_dst, const uint8_t *p_src); - _FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_src, uint8_t *p_dst); + _FORCE_INLINE_ void _put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel); + _FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel); void _set_data(const Dictionary &p_data); Dictionary _get_data() const; diff --git a/core/io/compression.cpp b/core/io/compression.cpp index 662411a62e..9ae54c38af 100644 --- a/core/io/compression.cpp +++ b/core/io/compression.cpp @@ -28,11 +28,12 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "compression.h" - +#include "global_config.h" #include "os/copymem.h" #include "zip_io.h" #include "thirdparty/misc/fastlz.h" +#include "thirdparty/zstd/zstd.h" #include <zlib.h> @@ -57,7 +58,8 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, strm.zalloc = zipio_alloc; strm.zfree = zipio_free; strm.opaque = Z_NULL; - int err = deflateInit(&strm, Z_DEFAULT_COMPRESSION); + int level = GLOBAL_GET("compression/zlib_compression_level"); + int err = deflateInit(&strm, level); if (err != Z_OK) return -1; @@ -76,6 +78,12 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, return aout; } break; + case MODE_ZSTD: { + + int max_dst_size = get_max_compressed_buffer_size(p_src_size, MODE_ZSTD); + int level = GLOBAL_GET("compression/zstd_compression_level"); + return ZSTD_compress(p_dst, max_dst_size, p_src, p_src_size, level); + } break; } ERR_FAIL_V(-1); @@ -105,6 +113,10 @@ int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) { deflateEnd(&strm); return aout; } break; + case MODE_ZSTD: { + + return ZSTD_compressBound(p_src_size); + } break; } ERR_FAIL_V(-1); @@ -148,6 +160,10 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p ERR_FAIL_COND_V(err != Z_STREAM_END, -1); return total; } break; + case MODE_ZSTD: { + + return ZSTD_decompress(p_dst, p_dst_max_size, p_src, p_src_size); + } break; } ERR_FAIL_V(-1); diff --git a/core/io/compression.h b/core/io/compression.h index a982a074b1..742f0f4d68 100644 --- a/core/io/compression.h +++ b/core/io/compression.h @@ -36,12 +36,13 @@ class Compression { public: enum Mode { MODE_FASTLZ, - MODE_DEFLATE + MODE_DEFLATE, + MODE_ZSTD }; - static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_FASTLZ); - static int get_max_compressed_buffer_size(int p_src_size, Mode p_mode = MODE_FASTLZ); - static int decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_FASTLZ); + static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD); + static int get_max_compressed_buffer_size(int p_src_size, Mode p_mode = MODE_ZSTD); + static int decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD); Compression(); }; diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index 4c6e8f5507..4e802579c6 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -369,7 +369,7 @@ FileAccessCompressed::FileAccessCompressed() { f = NULL; magic = "GCMP"; block_size = 16384; - cmode = Compression::MODE_DEFLATE; + cmode = Compression::MODE_ZSTD; writing = false; write_ptr = 0; write_buffer_size = 0; diff --git a/core/io/file_access_compressed.h b/core/io/file_access_compressed.h index 6af9d9b6ca..340c298a0f 100644 --- a/core/io/file_access_compressed.h +++ b/core/io/file_access_compressed.h @@ -64,7 +64,7 @@ class FileAccessCompressed : public FileAccess { FileAccess *f; public: - void configure(const String &p_magic, Compression::Mode p_mode = Compression::MODE_FASTLZ, int p_block_size = 4096); + void configure(const String &p_magic, Compression::Mode p_mode = Compression::MODE_ZSTD, int p_block_size = 4096); Error open_after_magic(FileAccess *p_base); diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index dccf70ad7a..75dfd563dd 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -139,8 +139,8 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int ERR_FAIL_COND_V(len < (int)4 * 4, ERR_INVALID_DATA); Rect2 val; - val.pos.x = decode_float(&buf[0]); - val.pos.y = decode_float(&buf[4]); + val.position.x = decode_float(&buf[0]); + val.position.y = decode_float(&buf[4]); val.size.x = decode_float(&buf[8]); val.size.y = decode_float(&buf[12]); r_variant = val; @@ -211,9 +211,9 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int ERR_FAIL_COND_V(len < (int)4 * 6, ERR_INVALID_DATA); Rect3 val; - val.pos.x = decode_float(&buf[0]); - val.pos.y = decode_float(&buf[4]); - val.pos.z = decode_float(&buf[8]); + val.position.x = decode_float(&buf[0]); + val.position.y = decode_float(&buf[4]); + val.position.z = decode_float(&buf[8]); val.size.x = decode_float(&buf[12]); val.size.y = decode_float(&buf[16]); val.size.z = decode_float(&buf[20]); @@ -861,8 +861,8 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len) { if (buf) { Rect2 r2 = p_variant; - encode_float(r2.pos.x, &buf[0]); - encode_float(r2.pos.y, &buf[4]); + encode_float(r2.position.x, &buf[0]); + encode_float(r2.position.y, &buf[4]); encode_float(r2.size.x, &buf[8]); encode_float(r2.size.y, &buf[12]); } @@ -926,9 +926,9 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len) { if (buf) { Rect3 aabb = p_variant; - encode_float(aabb.pos.x, &buf[0]); - encode_float(aabb.pos.y, &buf[4]); - encode_float(aabb.pos.z, &buf[8]); + encode_float(aabb.position.x, &buf[0]); + encode_float(aabb.position.y, &buf[4]); + encode_float(aabb.position.z, &buf[8]); encode_float(aabb.size.x, &buf[12]); encode_float(aabb.size.y, &buf[16]); encode_float(aabb.size.z, &buf[20]); diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 26b53c2a31..0373176cd2 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -155,8 +155,8 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { case VARIANT_RECT2: { Rect2 v; - v.pos.x = f->get_real(); - v.pos.y = f->get_real(); + v.position.x = f->get_real(); + v.position.y = f->get_real(); v.size.x = f->get_real(); v.size.y = f->get_real(); r_v = v; @@ -191,9 +191,9 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { case VARIANT_RECT3: { Rect3 v; - v.pos.x = f->get_real(); - v.pos.y = f->get_real(); - v.pos.z = f->get_real(); + v.position.x = f->get_real(); + v.position.y = f->get_real(); + v.position.z = f->get_real(); v.size.x = f->get_real(); v.size.y = f->get_real(); v.size.z = f->get_real(); @@ -1288,8 +1288,8 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property, f->store_32(VARIANT_RECT2); Rect2 val = p_property; - f->store_real(val.pos.x); - f->store_real(val.pos.y); + f->store_real(val.position.x); + f->store_real(val.position.y); f->store_real(val.size.x); f->store_real(val.size.y); @@ -1327,9 +1327,9 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property, f->store_32(VARIANT_RECT3); Rect3 val = p_property; - f->store_real(val.pos.x); - f->store_real(val.pos.y); - f->store_real(val.pos.z); + f->store_real(val.position.x); + f->store_real(val.position.y); + f->store_real(val.position.z); f->store_real(val.size.x); f->store_real(val.size.y); f->store_real(val.size.z); diff --git a/core/io/resource_import.h b/core/io/resource_import.h index 25b7a534b2..9d2a5180dc 100644 --- a/core/io/resource_import.h +++ b/core/io/resource_import.h @@ -61,6 +61,7 @@ public: String get_internal_resource_path(const String &p_path) const; void add_importer(const Ref<ResourceImporter> &p_importer) { importers.insert(p_importer); } + void remove_importer(const Ref<ResourceImporter> &p_importer) { importers.erase(p_importer); } Ref<ResourceImporter> get_importer_by_name(const String &p_name); Ref<ResourceImporter> get_importer_by_extension(const String &p_extension); void get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter> > *r_importers); diff --git a/core/math/bsp_tree.cpp b/core/math/bsp_tree.cpp index 9bbce752c0..327a6017c3 100644 --- a/core/math/bsp_tree.cpp +++ b/core/math/bsp_tree.cpp @@ -39,8 +39,8 @@ void BSP_Tree::from_aabb(const Rect3 &p_aabb) { Vector3 n; n[i] = 1; - planes.push_back(Plane(n, p_aabb.pos[i] + p_aabb.size[i])); - planes.push_back(Plane(-n, -p_aabb.pos[i])); + planes.push_back(Plane(n, p_aabb.position[i] + p_aabb.size[i])); + planes.push_back(Plane(-n, -p_aabb.position[i])); } nodes.clear(); @@ -552,7 +552,7 @@ BSP_Tree::BSP_Tree(const PoolVector<Face3> &p_faces, real_t p_error_radius) { if (first) { - aabb.pos = f.vertex[0]; + aabb.position = f.vertex[0]; first = false; } else { diff --git a/core/math/camera_matrix.cpp b/core/math/camera_matrix.cpp index 5b072b7c53..33ad522315 100644 --- a/core/math/camera_matrix.cpp +++ b/core/math/camera_matrix.cpp @@ -507,8 +507,8 @@ void CameraMatrix::set_light_atlas_rect(const Rect2 &p_rect) { m[9] = 0.0, m[10] = 1.0, m[11] = 0.0, - m[12] = p_rect.pos.x, - m[13] = p_rect.pos.y, + m[12] = p_rect.position.x, + m[13] = p_rect.position.y, m[14] = 0.0, m[15] = 1.0; } @@ -559,8 +559,8 @@ void CameraMatrix::make_scale(const Vector3 &p_scale) { void CameraMatrix::scale_translate_to_fit(const Rect3 &p_aabb) { - Vector3 min = p_aabb.pos; - Vector3 max = p_aabb.pos + p_aabb.size; + Vector3 min = p_aabb.position; + Vector3 max = p_aabb.position + p_aabb.size; matrix[0][0] = 2 / (max.x - min.x); matrix[1][0] = 0; diff --git a/core/math/face3.cpp b/core/math/face3.cpp index 6d772cf08c..5b66e1999a 100644 --- a/core/math/face3.cpp +++ b/core/math/face3.cpp @@ -197,20 +197,20 @@ bool Face3::intersects_aabb(const Rect3 &p_aabb) const { /** TEST FACE AXIS */ -#define TEST_AXIS(m_ax) \ - { \ - real_t aabb_min = p_aabb.pos.m_ax; \ - real_t aabb_max = p_aabb.pos.m_ax + p_aabb.size.m_ax; \ - real_t tri_min, tri_max; \ - for (int i = 0; i < 3; i++) { \ - if (i == 0 || vertex[i].m_ax > tri_max) \ - tri_max = vertex[i].m_ax; \ - if (i == 0 || vertex[i].m_ax < tri_min) \ - tri_min = vertex[i].m_ax; \ - } \ - \ - if (tri_max < aabb_min || aabb_max < tri_min) \ - return false; \ +#define TEST_AXIS(m_ax) \ + { \ + real_t aabb_min = p_aabb.position.m_ax; \ + real_t aabb_max = p_aabb.position.m_ax + p_aabb.size.m_ax; \ + real_t tri_min, tri_max; \ + for (int i = 0; i < 3; i++) { \ + if (i == 0 || vertex[i].m_ax > tri_max) \ + tri_max = vertex[i].m_ax; \ + if (i == 0 || vertex[i].m_ax < tri_min) \ + tri_min = vertex[i].m_ax; \ + } \ + \ + if (tri_max < aabb_min || aabb_max < tri_min) \ + return false; \ } TEST_AXIS(x); diff --git a/core/math/face3.h b/core/math/face3.h index 31ab72b925..1cc94321c3 100644 --- a/core/math/face3.h +++ b/core/math/face3.h @@ -101,7 +101,7 @@ bool Face3::intersects_aabb2(const Rect3 &p_aabb) const { Vector3 perp = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]); Vector3 half_extents = p_aabb.size * 0.5; - Vector3 ofs = p_aabb.pos + half_extents; + Vector3 ofs = p_aabb.position + half_extents; Vector3 sup = Vector3( (perp.x > 0) ? -half_extents.x : half_extents.x, @@ -117,8 +117,8 @@ bool Face3::intersects_aabb2(const Rect3 &p_aabb) const { #define TEST_AXIS(m_ax) \ { \ - real_t aabb_min = p_aabb.pos.m_ax; \ - real_t aabb_max = p_aabb.pos.m_ax + p_aabb.size.m_ax; \ + real_t aabb_min = p_aabb.position.m_ax; \ + real_t aabb_max = p_aabb.position.m_ax + p_aabb.size.m_ax; \ real_t tri_min, tri_max; \ for (int i = 0; i < 3; i++) { \ if (i == 0 || vertex[i].m_ax > tri_max) \ @@ -150,68 +150,68 @@ bool Face3::intersects_aabb2(const Rect3 &p_aabb) const { case 0: { - from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z); - to = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z); + from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z); + to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z); } break; case 1: { - from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z); - to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z); + from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); + to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z); } break; case 2: { - from = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z); - to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z); + from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); + to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); } break; case 3: { - from = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z); - to = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z); + from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z); + to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); } break; case 4: { - from = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z); - to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z); + from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); + to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); } break; case 5: { - from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z); - to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z); + from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); + to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); } break; case 6: { - from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z); - to = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z); + from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); + to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); } break; case 7: { - from = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z); - to = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z); + from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); + to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); } break; case 8: { - from = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z); - to = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z); + from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); + to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); } break; case 9: { - from = Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z); - to = Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z); + from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z); + to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); } break; case 10: { - from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z); - to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z); + from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z); + to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); } break; case 11: { - from = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z); - to = Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z); + from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); + to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); } break; } diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index 618017f8b3..2bea514d37 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -301,7 +301,7 @@ enum _CellFlags { static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, const Vector3 &voxelsize, const Face3 &p_face) { Rect3 aabb(Vector3(x, y, z), Vector3(len_x, len_y, len_z)); - aabb.pos = aabb.pos * voxelsize; + aabb.position = aabb.position * voxelsize; aabb.size = aabb.size * voxelsize; if (!p_face.intersects_aabb(aabb)) @@ -640,7 +640,7 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e Face3 f = faces[i]; for (int j = 0; j < 3; j++) { - f.vertex[j] -= global_aabb.pos; + f.vertex[j] -= global_aabb.position; } _plot_face(cell_status, 0, 0, 0, div_x, div_y, div_z, voxelsize, f); } @@ -707,7 +707,7 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e Vector3 &v = wrapped_faces_ptr[i].vertex[j]; v = v * voxelsize; - v += global_aabb.pos; + v += global_aabb.position; } } diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp index 962a42acb9..52e240ed47 100644 --- a/core/math/math_2d.cpp +++ b/core/math/math_2d.cpp @@ -308,7 +308,7 @@ bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 for (int i = 0; i < 2; i++) { real_t seg_from = p_from[i]; real_t seg_to = p_to[i]; - real_t box_begin = pos[i]; + real_t box_begin = position[i]; real_t box_end = box_begin + size[i]; real_t cmin, cmax; real_t csign; diff --git a/core/math/math_2d.h b/core/math/math_2d.h index 128b74baf6..780bb532d7 100644 --- a/core/math/math_2d.h +++ b/core/math/math_2d.h @@ -207,24 +207,24 @@ struct Transform2D; struct Rect2 { - Point2 pos; + Point2 position; Size2 size; - const Vector2 &get_pos() const { return pos; } - void set_pos(const Vector2 &p_pos) { pos = p_pos; } + const Vector2 &get_position() const { return position; } + void set_position(const Vector2 &p_pos) { position = p_pos; } const Vector2 &get_size() const { return size; } void set_size(const Vector2 &p_size) { size = p_size; } real_t get_area() const { return size.width * size.height; } inline bool intersects(const Rect2 &p_rect) const { - if (pos.x >= (p_rect.pos.x + p_rect.size.width)) + if (position.x >= (p_rect.position.x + p_rect.size.width)) return false; - if ((pos.x + size.width) <= p_rect.pos.x) + if ((position.x + size.width) <= p_rect.position.x) return false; - if (pos.y >= (p_rect.pos.y + p_rect.size.height)) + if (position.y >= (p_rect.position.y + p_rect.size.height)) return false; - if ((pos.y + size.height) <= p_rect.pos.y) + if ((position.y + size.height) <= p_rect.position.y) return false; return true; @@ -234,17 +234,17 @@ struct Rect2 { real_t dist = 1e20; - if (p_point.x < pos.x) { - dist = MIN(dist, pos.x - p_point.x); + if (p_point.x < position.x) { + dist = MIN(dist, position.x - p_point.x); } - if (p_point.y < pos.y) { - dist = MIN(dist, pos.y - p_point.y); + if (p_point.y < position.y) { + dist = MIN(dist, position.y - p_point.y); } - if (p_point.x >= (pos.x + size.x)) { - dist = MIN(p_point.x - (pos.x + size.x), dist); + if (p_point.x >= (position.x + size.x)) { + dist = MIN(p_point.x - (position.x + size.x), dist); } - if (p_point.y >= (pos.y + size.y)) { - dist = MIN(p_point.y - (pos.y + size.y), dist); + if (p_point.y >= (position.y + size.y)) { + dist = MIN(p_point.y - (position.y + size.y), dist); } if (dist == 1e20) @@ -259,9 +259,9 @@ struct Rect2 { inline bool encloses(const Rect2 &p_rect) const { - return (p_rect.pos.x >= pos.x) && (p_rect.pos.y >= pos.y) && - ((p_rect.pos.x + p_rect.size.x) < (pos.x + size.x)) && - ((p_rect.pos.y + p_rect.size.y) < (pos.y + size.y)); + return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) && + ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) && + ((p_rect.position.y + p_rect.size.y) < (position.y + size.y)); } inline bool has_no_area() const { @@ -275,14 +275,14 @@ struct Rect2 { if (!intersects(new_rect)) return Rect2(); - new_rect.pos.x = MAX(p_rect.pos.x, pos.x); - new_rect.pos.y = MAX(p_rect.pos.y, pos.y); + new_rect.position.x = MAX(p_rect.position.x, position.x); + new_rect.position.y = MAX(p_rect.position.y, position.y); - Point2 p_rect_end = p_rect.pos + p_rect.size; - Point2 end = pos + size; + Point2 p_rect_end = p_rect.position + p_rect.size; + Point2 end = position + size; - new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.pos.x; - new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.pos.y; + new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x; + new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y; return new_rect; } @@ -291,25 +291,25 @@ struct Rect2 { Rect2 new_rect; - new_rect.pos.x = MIN(p_rect.pos.x, pos.x); - new_rect.pos.y = MIN(p_rect.pos.y, pos.y); + new_rect.position.x = MIN(p_rect.position.x, position.x); + new_rect.position.y = MIN(p_rect.position.y, position.y); - new_rect.size.x = MAX(p_rect.pos.x + p_rect.size.x, pos.x + size.x); - new_rect.size.y = MAX(p_rect.pos.y + p_rect.size.y, pos.y + size.y); + new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x); + new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y); - new_rect.size = new_rect.size - new_rect.pos; //make relative again + new_rect.size = new_rect.size - new_rect.position; //make relative again return new_rect; }; inline bool has_point(const Point2 &p_point) const { - if (p_point.x < pos.x) + if (p_point.x < position.x) return false; - if (p_point.y < pos.y) + if (p_point.y < position.y) return false; - if (p_point.x >= (pos.x + size.x)) + if (p_point.x >= (position.x + size.x)) return false; - if (p_point.y >= (pos.y + size.y)) + if (p_point.y >= (position.y + size.y)) return false; return true; @@ -317,18 +317,37 @@ struct Rect2 { inline bool no_area() const { return (size.width <= 0 || size.height <= 0); } - bool operator==(const Rect2 &p_rect) const { return pos == p_rect.pos && size == p_rect.size; } - bool operator!=(const Rect2 &p_rect) const { return pos != p_rect.pos || size != p_rect.size; } + bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; } + bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; } inline Rect2 grow(real_t p_by) const { Rect2 g = *this; - g.pos.x -= p_by; - g.pos.y -= p_by; + g.position.x -= p_by; + g.position.y -= p_by; g.size.width += p_by * 2; g.size.height += p_by * 2; return g; } + inline Rect2 grow_margin(Margin p_margin, real_t p_amount) const { + Rect2 g = *this; + g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0, + (MARGIN_TOP == p_margin) ? p_amount : 0, + (MARGIN_RIGHT == p_margin) ? p_amount : 0, + (MARGIN_BOTTOM == p_margin) ? p_amount : 0); + return g; + } + + inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const { + + Rect2 g = *this; + g.position.x -= p_left; + g.position.y -= p_top; + g.size.width += p_left + p_right; + g.size.height += p_top + p_bottom; + + return g; + } inline Rect2 expand(const Vector2 &p_vector) const { @@ -339,8 +358,8 @@ struct Rect2 { inline void expand_to(const Vector2 &p_vector) { //in place function for speed - Vector2 begin = pos; - Vector2 end = pos + size; + Vector2 begin = position; + Vector2 end = position + size; if (p_vector.x < begin.x) begin.x = p_vector.x; @@ -352,19 +371,19 @@ struct Rect2 { if (p_vector.y > end.y) end.y = p_vector.y; - pos = begin; + position = begin; size = end - begin; } - operator String() const { return String(pos) + ", " + String(size); } + operator String() const { return String(position) + ", " + String(size); } Rect2() {} Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) { - pos = Point2(p_x, p_y); + position = Point2(p_x, p_y); size = Size2(p_width, p_height); } Rect2(const Point2 &p_pos, const Size2 &p_size) { - pos = p_pos; + position = p_pos; size = p_size; } }; @@ -434,24 +453,24 @@ typedef Point2i Size2i; struct Rect2i { - Point2i pos; + Point2i position; Size2i size; - const Point2i &get_pos() const { return pos; } - void set_pos(const Point2i &p_pos) { pos = p_pos; } + const Point2i &get_position() const { return position; } + void set_position(const Point2i &p_pos) { position = p_pos; } const Point2i &get_size() const { return size; } void set_size(const Point2i &p_size) { size = p_size; } int get_area() const { return size.width * size.height; } inline bool intersects(const Rect2i &p_rect) const { - if (pos.x > (p_rect.pos.x + p_rect.size.width)) + if (position.x > (p_rect.position.x + p_rect.size.width)) return false; - if ((pos.x + size.width) < p_rect.pos.x) + if ((position.x + size.width) < p_rect.position.x) return false; - if (pos.y > (p_rect.pos.y + p_rect.size.height)) + if (position.y > (p_rect.position.y + p_rect.size.height)) return false; - if ((pos.y + size.height) < p_rect.pos.y) + if ((position.y + size.height) < p_rect.position.y) return false; return true; @@ -459,9 +478,9 @@ struct Rect2i { inline bool encloses(const Rect2i &p_rect) const { - return (p_rect.pos.x >= pos.x) && (p_rect.pos.y >= pos.y) && - ((p_rect.pos.x + p_rect.size.x) < (pos.x + size.x)) && - ((p_rect.pos.y + p_rect.size.y) < (pos.y + size.y)); + return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) && + ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) && + ((p_rect.position.y + p_rect.size.y) < (position.y + size.y)); } inline bool has_no_area() const { @@ -475,14 +494,14 @@ struct Rect2i { if (!intersects(new_rect)) return Rect2i(); - new_rect.pos.x = MAX(p_rect.pos.x, pos.x); - new_rect.pos.y = MAX(p_rect.pos.y, pos.y); + new_rect.position.x = MAX(p_rect.position.x, position.x); + new_rect.position.y = MAX(p_rect.position.y, position.y); - Point2 p_rect_end = p_rect.pos + p_rect.size; - Point2 end = pos + size; + Point2 p_rect_end = p_rect.position + p_rect.size; + Point2 end = position + size; - new_rect.size.x = (int)(MIN(p_rect_end.x, end.x) - new_rect.pos.x); - new_rect.size.y = (int)(MIN(p_rect_end.y, end.y) - new_rect.pos.y); + new_rect.size.x = (int)(MIN(p_rect_end.x, end.x) - new_rect.position.x); + new_rect.size.y = (int)(MIN(p_rect_end.y, end.y) - new_rect.position.y); return new_rect; } @@ -491,25 +510,25 @@ struct Rect2i { Rect2i new_rect; - new_rect.pos.x = MIN(p_rect.pos.x, pos.x); - new_rect.pos.y = MIN(p_rect.pos.y, pos.y); + new_rect.position.x = MIN(p_rect.position.x, position.x); + new_rect.position.y = MIN(p_rect.position.y, position.y); - new_rect.size.x = MAX(p_rect.pos.x + p_rect.size.x, pos.x + size.x); - new_rect.size.y = MAX(p_rect.pos.y + p_rect.size.y, pos.y + size.y); + new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x); + new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y); - new_rect.size = new_rect.size - new_rect.pos; //make relative again + new_rect.size = new_rect.size - new_rect.position; //make relative again return new_rect; }; bool has_point(const Point2 &p_point) const { - if (p_point.x < pos.x) + if (p_point.x < position.x) return false; - if (p_point.y < pos.y) + if (p_point.y < position.y) return false; - if (p_point.x >= (pos.x + size.x)) + if (p_point.x >= (position.x + size.x)) return false; - if (p_point.y >= (pos.y + size.y)) + if (p_point.y >= (position.y + size.y)) return false; return true; @@ -517,14 +536,14 @@ struct Rect2i { bool no_area() { return (size.width <= 0 || size.height <= 0); } - bool operator==(const Rect2i &p_rect) const { return pos == p_rect.pos && size == p_rect.size; } - bool operator!=(const Rect2i &p_rect) const { return pos != p_rect.pos || size != p_rect.size; } + bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; } + bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; } Rect2i grow(int p_by) const { Rect2i g = *this; - g.pos.x -= p_by; - g.pos.y -= p_by; + g.position.x -= p_by; + g.position.y -= p_by; g.size.width += p_by * 2; g.size.height += p_by * 2; return g; @@ -532,8 +551,8 @@ struct Rect2i { inline void expand_to(const Point2i &p_vector) { - Point2i begin = pos; - Point2i end = pos + size; + Point2i begin = position; + Point2i end = position + size; if (p_vector.x < begin.x) begin.x = p_vector.x; @@ -545,24 +564,24 @@ struct Rect2i { if (p_vector.y > end.y) end.y = p_vector.y; - pos = begin; + position = begin; size = end - begin; } - operator String() const { return String(pos) + ", " + String(size); } + operator String() const { return String(position) + ", " + String(size); } - operator Rect2() const { return Rect2(pos, size); } + operator Rect2() const { return Rect2(position, size); } Rect2i(const Rect2 &p_r2) { - pos = p_r2.pos; + position = p_r2.position; size = p_r2.size; } Rect2i() {} Rect2i(int p_x, int p_y, int p_width, int p_height) { - pos = Point2(p_x, p_y); + position = Point2(p_x, p_y); size = Size2(p_width, p_height); } Rect2i(const Point2 &p_pos, const Size2 &p_size) { - pos = p_pos; + position = p_pos; size = p_size; } }; @@ -668,30 +687,30 @@ bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_re //SAT intersection between local and transformed rect2 Vector2 xf_points[4] = { - p_xform.xform(p_rect.pos), - p_xform.xform(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y)), - p_xform.xform(Vector2(p_rect.pos.x, p_rect.pos.y + p_rect.size.y)), - p_xform.xform(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y + p_rect.size.y)), + p_xform.xform(p_rect.position), + p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)), + p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)), + p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)), }; real_t low_limit; //base rect2 first (faster) - if (xf_points[0].y > pos.y) + if (xf_points[0].y > position.y) goto next1; - if (xf_points[1].y > pos.y) + if (xf_points[1].y > position.y) goto next1; - if (xf_points[2].y > pos.y) + if (xf_points[2].y > position.y) goto next1; - if (xf_points[3].y > pos.y) + if (xf_points[3].y > position.y) goto next1; return false; next1: - low_limit = pos.y + size.y; + low_limit = position.y + size.y; if (xf_points[0].y < low_limit) goto next2; @@ -706,20 +725,20 @@ next1: next2: - if (xf_points[0].x > pos.x) + if (xf_points[0].x > position.x) goto next3; - if (xf_points[1].x > pos.x) + if (xf_points[1].x > position.x) goto next3; - if (xf_points[2].x > pos.x) + if (xf_points[2].x > position.x) goto next3; - if (xf_points[3].x > pos.x) + if (xf_points[3].x > position.x) goto next3; return false; next3: - low_limit = pos.x + size.x; + low_limit = position.x + size.x; if (xf_points[0].x < low_limit) goto next4; @@ -735,10 +754,10 @@ next3: next4: Vector2 xf_points2[4] = { - pos, - Vector2(pos.x + size.x, pos.y), - Vector2(pos.x, pos.y + size.y), - Vector2(pos.x + size.x, pos.y + size.y), + position, + Vector2(position.x + size.x, position.y), + Vector2(position.x, position.y + size.y), + Vector2(position.x + size.x, position.y + size.y), }; real_t maxa = p_xform.elements[0].dot(xf_points2[0]); @@ -847,10 +866,10 @@ Rect2 Transform2D::xform(const Rect2 &p_rect) const { Vector2 x = elements[0] * p_rect.size.x; Vector2 y = elements[1] * p_rect.size.y; - Vector2 pos = xform(p_rect.pos); + Vector2 pos = xform(p_rect.position); Rect2 new_rect; - new_rect.pos = pos; + new_rect.position = pos; new_rect.expand_to(pos + x); new_rect.expand_to(pos + y); new_rect.expand_to(pos + x + y); @@ -868,14 +887,14 @@ void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) { Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const { Vector2 ends[4] = { - xform_inv(p_rect.pos), - xform_inv(Vector2(p_rect.pos.x, p_rect.pos.y + p_rect.size.y)), - xform_inv(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y + p_rect.size.y)), - xform_inv(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y)) + xform_inv(p_rect.position), + xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)), + xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)), + xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)) }; Rect2 new_rect; - new_rect.pos = ends[0]; + new_rect.position = ends[0]; new_rect.expand_to(ends[1]); new_rect.expand_to(ends[2]); new_rect.expand_to(ends[3]); diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 440d989705..dd64b10f88 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -51,9 +51,7 @@ class Math { public: Math() {} // useless to instance - enum { - RANDOM_MAX = 4294967295L - }; + static const uint64_t RANDOM_MAX = 4294967295; static _ALWAYS_INLINE_ double sin(double p_x) { return ::sin(p_x); } static _ALWAYS_INLINE_ float sin(float p_x) { return ::sinf(p_x); } diff --git a/core/math/octree.h b/core/math/octree.h index 4cc046ddf4..010c1b18f7 100644 --- a/core/math/octree.h +++ b/core/math/octree.h @@ -483,11 +483,11 @@ void Octree<T, use_pairs, AL>::_insert_element(Element *p_element, Octant *p_oct aabb.size *= 0.5; if (i & 1) - aabb.pos.x += aabb.size.x; + aabb.position.x += aabb.size.x; if (i & 2) - aabb.pos.y += aabb.size.y; + aabb.position.y += aabb.size.y; if (i & 4) - aabb.pos.z += aabb.size.z; + aabb.position.z += aabb.size.z; if (aabb.intersects_inclusive(p_element->aabb)) { /* if actually intersects, create the child */ @@ -544,11 +544,11 @@ void Octree<T, use_pairs, AL>::_ensure_valid_root(const Rect3 &p_aabb) { while (!base.encloses(p_aabb)) { - if (ABS(base.pos.x + base.size.x) <= ABS(base.pos.x)) { + if (ABS(base.position.x + base.size.x) <= ABS(base.position.x)) { /* grow towards positive */ base.size *= 2.0; } else { - base.pos -= base.size; + base.position -= base.size; base.size *= 2.0; } } @@ -576,14 +576,14 @@ void Octree<T, use_pairs, AL>::_ensure_valid_root(const Rect3 &p_aabb) { octant_count++; root->parent = gp; - if (ABS(base.pos.x + base.size.x) <= ABS(base.pos.x)) { + if (ABS(base.position.x + base.size.x) <= ABS(base.position.x)) { /* grow towards positive */ base.size *= 2.0; gp->aabb = base; gp->children[0] = root; root->parent_index = 0; } else { - base.pos -= base.size; + base.position -= base.size; base.size *= 2.0; gp->aabb = base; gp->children[(1 << 0) | (1 << 1) | (1 << 2)] = root; // add at all-positive @@ -797,9 +797,9 @@ OctreeElementID Octree<T, use_pairs, AL>::create(T *p_userdata, const Rect3 &p_a // check for AABB validity #ifdef DEBUG_ENABLED - ERR_FAIL_COND_V(p_aabb.pos.x > 1e15 || p_aabb.pos.x < -1e15, 0); - ERR_FAIL_COND_V(p_aabb.pos.y > 1e15 || p_aabb.pos.y < -1e15, 0); - ERR_FAIL_COND_V(p_aabb.pos.z > 1e15 || p_aabb.pos.z < -1e15, 0); + ERR_FAIL_COND_V(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15, 0); + ERR_FAIL_COND_V(p_aabb.position.y > 1e15 || p_aabb.position.y < -1e15, 0); + ERR_FAIL_COND_V(p_aabb.position.z > 1e15 || p_aabb.position.z < -1e15, 0); ERR_FAIL_COND_V(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0, 0); ERR_FAIL_COND_V(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0, 0); ERR_FAIL_COND_V(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0, 0); @@ -837,9 +837,9 @@ void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const Rect3 &p_aabb) { #ifdef DEBUG_ENABLED // check for AABB validity - ERR_FAIL_COND(p_aabb.pos.x > 1e15 || p_aabb.pos.x < -1e15); - ERR_FAIL_COND(p_aabb.pos.y > 1e15 || p_aabb.pos.y < -1e15); - ERR_FAIL_COND(p_aabb.pos.z > 1e15 || p_aabb.pos.z < -1e15); + ERR_FAIL_COND(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15); + ERR_FAIL_COND(p_aabb.position.y > 1e15 || p_aabb.position.y < -1e15); + ERR_FAIL_COND(p_aabb.position.z > 1e15 || p_aabb.position.z < -1e15); ERR_FAIL_COND(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0); ERR_FAIL_COND(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0); ERR_FAIL_COND(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0); diff --git a/core/math/quick_hull.cpp b/core/math/quick_hull.cpp index ce93720067..9f594ba4fa 100644 --- a/core/math/quick_hull.cpp +++ b/core/math/quick_hull.cpp @@ -42,7 +42,7 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me for (int i = 0; i < p_points.size(); i++) { if (i == 0) { - aabb.pos = p_points[i]; + aabb.position = p_points[i]; } else { aabb.expand_to(p_points[i]); } diff --git a/core/math/rect3.cpp b/core/math/rect3.cpp index 39b0beb071..973607f565 100644 --- a/core/math/rect3.cpp +++ b/core/math/rect3.cpp @@ -38,11 +38,11 @@ real_t Rect3::get_area() const { bool Rect3::operator==(const Rect3 &p_rval) const { - return ((pos == p_rval.pos) && (size == p_rval.size)); + return ((position == p_rval.position) && (size == p_rval.size)); } bool Rect3::operator!=(const Rect3 &p_rval) const { - return ((pos != p_rval.pos) || (size != p_rval.size)); + return ((position != p_rval.position) || (size != p_rval.size)); } void Rect3::merge_with(const Rect3 &p_aabb) { @@ -51,8 +51,8 @@ void Rect3::merge_with(const Rect3 &p_aabb) { Vector3 end_1, end_2; Vector3 min, max; - beg_1 = pos; - beg_2 = p_aabb.pos; + beg_1 = position; + beg_2 = p_aabb.position; end_1 = Vector3(size.x, size.y, size.z) + beg_1; end_2 = Vector3(p_aabb.size.x, p_aabb.size.y, p_aabb.size.z) + beg_2; @@ -64,16 +64,16 @@ void Rect3::merge_with(const Rect3 &p_aabb) { max.y = (end_1.y > end_2.y) ? end_1.y : end_2.y; max.z = (end_1.z > end_2.z) ? end_1.z : end_2.z; - pos = min; + position = min; size = max - min; } Rect3 Rect3::intersection(const Rect3 &p_aabb) const { - Vector3 src_min = pos; - Vector3 src_max = pos + size; - Vector3 dst_min = p_aabb.pos; - Vector3 dst_max = p_aabb.pos + p_aabb.size; + Vector3 src_min = position; + Vector3 src_max = position + size; + Vector3 dst_min = p_aabb.position; + Vector3 dst_max = p_aabb.position + p_aabb.size; Vector3 min, max; @@ -107,18 +107,18 @@ Rect3 Rect3::intersection(const Rect3 &p_aabb) const { bool Rect3::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const { Vector3 c1, c2; - Vector3 end = pos + size; + Vector3 end = position + size; real_t near = -1e20; real_t far = 1e20; int axis = 0; for (int i = 0; i < 3; i++) { if (p_dir[i] == 0) { - if ((p_from[i] < pos[i]) || (p_from[i] > end[i])) { + if ((p_from[i] < position[i]) || (p_from[i] > end[i])) { return false; } } else { // ray not parallel to planes in this direction - c1[i] = (pos[i] - p_from[i]) / p_dir[i]; + c1[i] = (position[i] - p_from[i]) / p_dir[i]; c2[i] = (end[i] - p_from[i]) / p_dir[i]; if (c1[i] > c2[i]) { @@ -156,7 +156,7 @@ bool Rect3::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vecto for (int i = 0; i < 3; i++) { real_t seg_from = p_from[i]; real_t seg_to = p_to[i]; - real_t box_begin = pos[i]; + real_t box_begin = position[i]; real_t box_end = box_begin + size[i]; real_t cmin, cmax; real_t csign; @@ -208,14 +208,14 @@ bool Rect3::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vecto bool Rect3::intersects_plane(const Plane &p_plane) const { Vector3 points[8] = { - Vector3(pos.x, pos.y, pos.z), - Vector3(pos.x, pos.y, pos.z + size.z), - Vector3(pos.x, pos.y + size.y, pos.z), - Vector3(pos.x, pos.y + size.y, pos.z + size.z), - Vector3(pos.x + size.x, pos.y, pos.z), - Vector3(pos.x + size.x, pos.y, pos.z + size.z), - Vector3(pos.x + size.x, pos.y + size.y, pos.z), - Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z), + Vector3(position.x, position.y, position.z), + Vector3(position.x, position.y, position.z + size.z), + Vector3(position.x, position.y + size.y, position.z), + Vector3(position.x, position.y + size.y, position.z + size.z), + Vector3(position.x + size.x, position.y, position.z), + Vector3(position.x + size.x, position.y, position.z + size.z), + Vector3(position.x + size.x, position.y + size.y, position.z), + Vector3(position.x + size.x, position.y + size.y, position.z + size.z), }; bool over = false; @@ -327,68 +327,68 @@ void Rect3::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { case 0: { - r_from = Vector3(pos.x + size.x, pos.y, pos.z); - r_to = Vector3(pos.x, pos.y, pos.z); + r_from = Vector3(position.x + size.x, position.y, position.z); + r_to = Vector3(position.x, position.y, position.z); } break; case 1: { - r_from = Vector3(pos.x + size.x, pos.y, pos.z + size.z); - r_to = Vector3(pos.x + size.x, pos.y, pos.z); + r_from = Vector3(position.x + size.x, position.y, position.z + size.z); + r_to = Vector3(position.x + size.x, position.y, position.z); } break; case 2: { - r_from = Vector3(pos.x, pos.y, pos.z + size.z); - r_to = Vector3(pos.x + size.x, pos.y, pos.z + size.z); + r_from = Vector3(position.x, position.y, position.z + size.z); + r_to = Vector3(position.x + size.x, position.y, position.z + size.z); } break; case 3: { - r_from = Vector3(pos.x, pos.y, pos.z); - r_to = Vector3(pos.x, pos.y, pos.z + size.z); + r_from = Vector3(position.x, position.y, position.z); + r_to = Vector3(position.x, position.y, position.z + size.z); } break; case 4: { - r_from = Vector3(pos.x, pos.y + size.y, pos.z); - r_to = Vector3(pos.x + size.x, pos.y + size.y, pos.z); + r_from = Vector3(position.x, position.y + size.y, position.z); + r_to = Vector3(position.x + size.x, position.y + size.y, position.z); } break; case 5: { - r_from = Vector3(pos.x + size.x, pos.y + size.y, pos.z); - r_to = Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z); + r_from = Vector3(position.x + size.x, position.y + size.y, position.z); + r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); } break; case 6: { - r_from = Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z); - r_to = Vector3(pos.x, pos.y + size.y, pos.z + size.z); + r_from = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); + r_to = Vector3(position.x, position.y + size.y, position.z + size.z); } break; case 7: { - r_from = Vector3(pos.x, pos.y + size.y, pos.z + size.z); - r_to = Vector3(pos.x, pos.y + size.y, pos.z); + r_from = Vector3(position.x, position.y + size.y, position.z + size.z); + r_to = Vector3(position.x, position.y + size.y, position.z); } break; case 8: { - r_from = Vector3(pos.x, pos.y, pos.z + size.z); - r_to = Vector3(pos.x, pos.y + size.y, pos.z + size.z); + r_from = Vector3(position.x, position.y, position.z + size.z); + r_to = Vector3(position.x, position.y + size.y, position.z + size.z); } break; case 9: { - r_from = Vector3(pos.x, pos.y, pos.z); - r_to = Vector3(pos.x, pos.y + size.y, pos.z); + r_from = Vector3(position.x, position.y, position.z); + r_to = Vector3(position.x, position.y + size.y, position.z); } break; case 10: { - r_from = Vector3(pos.x + size.x, pos.y, pos.z); - r_to = Vector3(pos.x + size.x, pos.y + size.y, pos.z); + r_from = Vector3(position.x + size.x, position.y, position.z); + r_to = Vector3(position.x + size.x, position.y + size.y, position.z); } break; case 11: { - r_from = Vector3(pos.x + size.x, pos.y, pos.z + size.z); - r_to = Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z); + r_from = Vector3(position.x + size.x, position.y, position.z + size.z); + r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); } break; } @@ -396,5 +396,5 @@ void Rect3::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { Rect3::operator String() const { - return String() + pos + " - " + size; + return String() + position + " - " + size; } diff --git a/core/math/rect3.h b/core/math/rect3.h index 136a156151..b7c94ad9f7 100644 --- a/core/math/rect3.h +++ b/core/math/rect3.h @@ -36,12 +36,12 @@ /** * AABB / AABB (Axis Aligned Bounding Box) - * This is implemented by a point (pos) and the box size + * This is implemented by a point (position) and the box size */ class Rect3 { public: - Vector3 pos; + Vector3 position; Vector3 size; real_t get_area() const; /// get area @@ -55,8 +55,8 @@ public: return (size.x <= CMP_EPSILON && size.y <= CMP_EPSILON && size.z <= CMP_EPSILON); } - const Vector3 &get_pos() const { return pos; } - void set_pos(const Vector3 &p_pos) { pos = p_pos; } + const Vector3 &get_position() const { return position; } + void set_position(const Vector3 &p_pos) { position = p_pos; } const Vector3 &get_size() const { return size; } void set_size(const Vector3 &p_size) { size = p_size; } @@ -102,24 +102,24 @@ public: _FORCE_INLINE_ Rect3() {} inline Rect3(const Vector3 &p_pos, const Vector3 &p_size) { - pos = p_pos; + position = p_pos; size = p_size; } }; inline bool Rect3::intersects(const Rect3 &p_aabb) const { - if (pos.x >= (p_aabb.pos.x + p_aabb.size.x)) + if (position.x >= (p_aabb.position.x + p_aabb.size.x)) return false; - if ((pos.x + size.x) <= p_aabb.pos.x) + if ((position.x + size.x) <= p_aabb.position.x) return false; - if (pos.y >= (p_aabb.pos.y + p_aabb.size.y)) + if (position.y >= (p_aabb.position.y + p_aabb.size.y)) return false; - if ((pos.y + size.y) <= p_aabb.pos.y) + if ((position.y + size.y) <= p_aabb.position.y) return false; - if (pos.z >= (p_aabb.pos.z + p_aabb.size.z)) + if (position.z >= (p_aabb.position.z + p_aabb.size.z)) return false; - if ((pos.z + size.z) <= p_aabb.pos.z) + if ((position.z + size.z) <= p_aabb.position.z) return false; return true; @@ -127,17 +127,17 @@ inline bool Rect3::intersects(const Rect3 &p_aabb) const { inline bool Rect3::intersects_inclusive(const Rect3 &p_aabb) const { - if (pos.x > (p_aabb.pos.x + p_aabb.size.x)) + if (position.x > (p_aabb.position.x + p_aabb.size.x)) return false; - if ((pos.x + size.x) < p_aabb.pos.x) + if ((position.x + size.x) < p_aabb.position.x) return false; - if (pos.y > (p_aabb.pos.y + p_aabb.size.y)) + if (position.y > (p_aabb.position.y + p_aabb.size.y)) return false; - if ((pos.y + size.y) < p_aabb.pos.y) + if ((position.y + size.y) < p_aabb.position.y) return false; - if (pos.z > (p_aabb.pos.z + p_aabb.size.z)) + if (position.z > (p_aabb.position.z + p_aabb.size.z)) return false; - if ((pos.z + size.z) < p_aabb.pos.z) + if ((position.z + size.z) < p_aabb.position.z) return false; return true; @@ -145,10 +145,10 @@ inline bool Rect3::intersects_inclusive(const Rect3 &p_aabb) const { inline bool Rect3::encloses(const Rect3 &p_aabb) const { - Vector3 src_min = pos; - Vector3 src_max = pos + size; - Vector3 dst_min = p_aabb.pos; - Vector3 dst_max = p_aabb.pos + p_aabb.size; + Vector3 src_min = position; + Vector3 src_max = position + size; + Vector3 dst_min = p_aabb.position; + Vector3 dst_max = p_aabb.position + p_aabb.size; return ( (src_min.x <= dst_min.x) && @@ -162,7 +162,7 @@ inline bool Rect3::encloses(const Rect3 &p_aabb) const { Vector3 Rect3::get_support(const Vector3 &p_normal) const { Vector3 half_extents = size * 0.5; - Vector3 ofs = pos + half_extents; + Vector3 ofs = position + half_extents; return Vector3( (p_normal.x > 0) ? -half_extents.x : half_extents.x, @@ -174,14 +174,14 @@ Vector3 Rect3::get_support(const Vector3 &p_normal) const { Vector3 Rect3::get_endpoint(int p_point) const { switch (p_point) { - case 0: return Vector3(pos.x, pos.y, pos.z); - case 1: return Vector3(pos.x, pos.y, pos.z + size.z); - case 2: return Vector3(pos.x, pos.y + size.y, pos.z); - case 3: return Vector3(pos.x, pos.y + size.y, pos.z + size.z); - case 4: return Vector3(pos.x + size.x, pos.y, pos.z); - case 5: return Vector3(pos.x + size.x, pos.y, pos.z + size.z); - case 6: return Vector3(pos.x + size.x, pos.y + size.y, pos.z); - case 7: return Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z); + case 0: return Vector3(position.x, position.y, position.z); + case 1: return Vector3(position.x, position.y, position.z + size.z); + case 2: return Vector3(position.x, position.y + size.y, position.z); + case 3: return Vector3(position.x, position.y + size.y, position.z + size.z); + case 4: return Vector3(position.x + size.x, position.y, position.z); + case 5: return Vector3(position.x + size.x, position.y, position.z + size.z); + case 6: return Vector3(position.x + size.x, position.y + size.y, position.z); + case 7: return Vector3(position.x + size.x, position.y + size.y, position.z + size.z); }; ERR_FAIL_V(Vector3()); @@ -192,7 +192,7 @@ bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) co #if 1 Vector3 half_extents = size * 0.5; - Vector3 ofs = pos + half_extents; + Vector3 ofs = position + half_extents; for (int i = 0; i < p_plane_count; i++) { const Plane &p = p_planes[i]; @@ -210,14 +210,14 @@ bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) co //cache all points to check against! // #warning should be easy to optimize, just use the same as when taking the support and use only that point Vector3 points[8] = { - Vector3(pos.x, pos.y, pos.z), - Vector3(pos.x, pos.y, pos.z + size.z), - Vector3(pos.x, pos.y + size.y, pos.z), - Vector3(pos.x, pos.y + size.y, pos.z + size.z), - Vector3(pos.x + size.x, pos.y, pos.z), - Vector3(pos.x + size.x, pos.y, pos.z + size.z), - Vector3(pos.x + size.x, pos.y + size.y, pos.z), - Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z), + Vector3(position.x, position.y, position.z), + Vector3(position.x, position.y, position.z + size.z), + Vector3(position.x, position.y + size.y, position.z), + Vector3(position.x, position.y + size.y, position.z + size.z), + Vector3(position.x + size.x, position.y, position.z), + Vector3(position.x + size.x, position.y, position.z + size.z), + Vector3(position.x + size.x, position.y + size.y, position.z), + Vector3(position.x + size.x, position.y + size.y, position.z + size.z), }; for (int i = 0; i < p_plane_count; i++) { //for each plane @@ -246,17 +246,17 @@ bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) co bool Rect3::has_point(const Vector3 &p_point) const { - if (p_point.x < pos.x) + if (p_point.x < position.x) return false; - if (p_point.y < pos.y) + if (p_point.y < position.y) return false; - if (p_point.z < pos.z) + if (p_point.z < position.z) return false; - if (p_point.x > pos.x + size.x) + if (p_point.x > position.x + size.x) return false; - if (p_point.y > pos.y + size.y) + if (p_point.y > position.y + size.y) return false; - if (p_point.z > pos.z + size.z) + if (p_point.z > position.z + size.z) return false; return true; @@ -264,8 +264,8 @@ bool Rect3::has_point(const Vector3 &p_point) const { inline void Rect3::expand_to(const Vector3 &p_vector) { - Vector3 begin = pos; - Vector3 end = pos + size; + Vector3 begin = position; + Vector3 end = position + size; if (p_vector.x < begin.x) begin.x = p_vector.x; @@ -281,14 +281,14 @@ inline void Rect3::expand_to(const Vector3 &p_vector) { if (p_vector.z > end.z) end.z = p_vector.z; - pos = begin; + position = begin; size = end - begin; } void Rect3::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const { Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5); - Vector3 center(pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z); + Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z); real_t length = p_plane.normal.abs().dot(half_extents); real_t distance = p_plane.distance_to(center); @@ -332,21 +332,21 @@ bool Rect3::smits_intersect_ray(const Vector3 &from, const Vector3 &dir, real_t real_t divy = 1.0 / dir.y; real_t divz = 1.0 / dir.z; - Vector3 upbound = pos + size; + Vector3 upbound = position + size; real_t tmin, tmax, tymin, tymax, tzmin, tzmax; if (dir.x >= 0) { - tmin = (pos.x - from.x) * divx; + tmin = (position.x - from.x) * divx; tmax = (upbound.x - from.x) * divx; } else { tmin = (upbound.x - from.x) * divx; - tmax = (pos.x - from.x) * divx; + tmax = (position.x - from.x) * divx; } if (dir.y >= 0) { - tymin = (pos.y - from.y) * divy; + tymin = (position.y - from.y) * divy; tymax = (upbound.y - from.y) * divy; } else { tymin = (upbound.y - from.y) * divy; - tymax = (pos.y - from.y) * divy; + tymax = (position.y - from.y) * divy; } if ((tmin > tymax) || (tymin > tmax)) return false; @@ -355,11 +355,11 @@ bool Rect3::smits_intersect_ray(const Vector3 &from, const Vector3 &dir, real_t if (tymax < tmax) tmax = tymax; if (dir.z >= 0) { - tzmin = (pos.z - from.z) * divz; + tzmin = (position.z - from.z) * divz; tzmax = (upbound.z - from.z) * divz; } else { tzmin = (upbound.z - from.z) * divz; - tzmax = (pos.z - from.z) * divz; + tzmax = (position.z - from.z) * divz; } if ((tmin > tzmax) || (tzmin > tmax)) return false; @@ -372,9 +372,9 @@ bool Rect3::smits_intersect_ray(const Vector3 &from, const Vector3 &dir, real_t void Rect3::grow_by(real_t p_amount) { - pos.x -= p_amount; - pos.y -= p_amount; - pos.z -= p_amount; + position.x -= p_amount; + position.y -= p_amount; + position.z -= p_amount; size.x += 2.0 * p_amount; size.y += 2.0 * p_amount; size.z += 2.0 * p_amount; diff --git a/core/math/transform.h b/core/math/transform.h index 4731496bf3..c39ea7826f 100644 --- a/core/math/transform.h +++ b/core/math/transform.h @@ -167,10 +167,10 @@ _FORCE_INLINE_ Rect3 Transform::xform(const Rect3 &p_aabb) const { Vector3 x = basis.get_axis(0) * p_aabb.size.x; Vector3 y = basis.get_axis(1) * p_aabb.size.y; Vector3 z = basis.get_axis(2) * p_aabb.size.z; - Vector3 pos = xform(p_aabb.pos); + Vector3 pos = xform(p_aabb.position); //could be even further optimized Rect3 new_aabb; - new_aabb.pos = pos; + new_aabb.position = pos; new_aabb.expand_to(pos + x); new_aabb.expand_to(pos + y); new_aabb.expand_to(pos + z); @@ -182,14 +182,14 @@ _FORCE_INLINE_ Rect3 Transform::xform(const Rect3 &p_aabb) const { #else Vector3 vertices[8] = { - Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z), - Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z), - Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z), - Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z), - Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z), - Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z), - Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z), - Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z) + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z), + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z), + Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z), + Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z) }; AABB ret; @@ -208,19 +208,19 @@ _FORCE_INLINE_ Rect3 Transform::xform_inv(const Rect3 &p_aabb) const { /* define vertices */ Vector3 vertices[8] = { - Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z), - Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z), - Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z), - Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z), - Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z), - Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z), - Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z), - Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z) + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z), + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z), + Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z), + Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z) }; Rect3 ret; - ret.pos = xform_inv(vertices[0]); + ret.position = xform_inv(vertices[0]); for (int i = 1; i < 8; i++) { diff --git a/core/math/triangle_mesh.cpp b/core/math/triangle_mesh.cpp index 1cf1351646..08ac08d776 100644 --- a/core/math/triangle_mesh.cpp +++ b/core/math/triangle_mesh.cpp @@ -79,7 +79,7 @@ int TriangleMesh::_create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, in int index = max_alloc++; BVH *_new = &p_bvh[index]; _new->aabb = aabb; - _new->center = aabb.pos + aabb.size * 0.5; + _new->center = aabb.position + aabb.size * 0.5; _new->face_index = -1; _new->left = left; _new->right = right; @@ -128,7 +128,7 @@ void TriangleMesh::create(const PoolVector<Vector3> &p_faces) { f.indices[j] = vidx; if (j == 0) - bw[i].aabb.pos = vs; + bw[i].aabb.position = vs; else bw[i].aabb.expand_to(vs); } @@ -138,7 +138,7 @@ void TriangleMesh::create(const PoolVector<Vector3> &p_faces) { bw[i].left = -1; bw[i].right = -1; bw[i].face_index = i; - bw[i].center = bw[i].aabb.pos + bw[i].aabb.size * 0.5; + bw[i].center = bw[i].aabb.position + bw[i].aabb.size * 0.5; } vertices.resize(db.size()); diff --git a/core/method_ptrcall.h b/core/method_ptrcall.h index bcbf2e4531..c6dbfc2a7a 100644 --- a/core/method_ptrcall.h +++ b/core/method_ptrcall.h @@ -115,7 +115,6 @@ MAKE_PTRARG(PoolVector2Array); MAKE_PTRARG(PoolVector3Array); MAKE_PTRARG(PoolColorArray); MAKE_PTRARG(Variant); -MAKE_PTRARG(PowerState); //this is for Object diff --git a/core/object.cpp b/core/object.cpp index 7aee936a2d..f20e93f9d7 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -1831,10 +1831,10 @@ void postinitialize_handler(Object *p_object) { p_object->_postinitialize(); } -HashMap<uint32_t, Object *> ObjectDB::instances; -uint32_t ObjectDB::instance_counter = 1; +HashMap<ObjectID, Object *> ObjectDB::instances; +ObjectID ObjectDB::instance_counter = 1; HashMap<Object *, ObjectID, ObjectDB::ObjectPtrHash> ObjectDB::instance_checks; -uint32_t ObjectDB::add_instance(Object *p_object) { +ObjectID ObjectDB::add_instance(Object *p_object) { ERR_FAIL_COND_V(p_object->get_instance_ID() != 0, 0); @@ -1859,7 +1859,7 @@ void ObjectDB::remove_instance(Object *p_object) { rw_lock->write_unlock(); } -Object *ObjectDB::get_instance(uint32_t p_instance_ID) { +Object *ObjectDB::get_instance(ObjectID p_instance_ID) { rw_lock->read_lock(); Object **obj = instances.getptr(p_instance_ID); @@ -1874,7 +1874,7 @@ void ObjectDB::debug_objects(DebugFunc p_func) { rw_lock->read_lock(); - const uint32_t *K = NULL; + const ObjectID *K = NULL; while ((K = instances.next(K))) { p_func(instances[*K]); @@ -1909,7 +1909,7 @@ void ObjectDB::cleanup() { WARN_PRINT("ObjectDB Instances still exist!"); if (OS::get_singleton()->is_stdout_verbose()) { - const uint32_t *K = NULL; + const ObjectID *K = NULL; while ((K = instances.next(K))) { String node_name; diff --git a/core/object.h b/core/object.h index 3b39224af0..83b03b9239 100644 --- a/core/object.h +++ b/core/object.h @@ -350,7 +350,7 @@ public: \ private: class ScriptInstance; -typedef uint32_t ObjectID; +typedef uint64_t ObjectID; class Object { public: @@ -423,7 +423,7 @@ private: bool _block_signals; int _predelete_ok; Set<Object *> change_receptors; - uint32_t _instance_ID; + ObjectID _instance_ID; bool _predelete(); void _postinitialize(); bool _can_translate; @@ -690,16 +690,16 @@ class ObjectDB { } }; - static HashMap<uint32_t, Object *> instances; + static HashMap<ObjectID, Object *> instances; static HashMap<Object *, ObjectID, ObjectPtrHash> instance_checks; - static uint32_t instance_counter; + static ObjectID instance_counter; friend class Object; friend void unregister_core_types(); static RWLock *rw_lock; static void cleanup(); - static uint32_t add_instance(Object *p_object); + static ObjectID add_instance(Object *p_object); static void remove_instance(Object *p_object); friend void register_core_types(); static void setup(); @@ -707,7 +707,7 @@ class ObjectDB { public: typedef void (*DebugFunc)(Object *p_obj); - static Object *get_instance(uint32_t p_instance_ID); + static Object *get_instance(ObjectID p_instance_ID); static void debug_objects(DebugFunc p_func); static int get_object_count(); diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp index 7ec76c1eed..dbdf9628e3 100644 --- a/core/os/input_event.cpp +++ b/core/os/input_event.cpp @@ -282,7 +282,10 @@ bool InputEventKey::action_match(const Ref<InputEvent> &p_event) const { if (key.is_null()) return false; - return get_scancode_with_modifiers() == key->get_scancode_with_modifiers(); + uint32_t code = get_scancode_with_modifiers(); + uint32_t event_code = key->get_scancode_with_modifiers(); + + return get_scancode() == key->get_scancode() && (!key->is_pressed() || (code & event_code) == code); } void InputEventKey::_bind_methods() { @@ -324,20 +327,20 @@ int InputEventMouse::get_button_mask() const { return button_mask; } -void InputEventMouse::set_pos(const Vector2 &p_pos) { +void InputEventMouse::set_position(const Vector2 &p_pos) { pos = p_pos; } -Vector2 InputEventMouse::get_pos() const { +Vector2 InputEventMouse::get_position() const { return pos; } -void InputEventMouse::set_global_pos(const Vector2 &p_global_pos) { +void InputEventMouse::set_global_position(const Vector2 &p_global_pos) { global_pos = p_global_pos; } -Vector2 InputEventMouse::get_global_pos() const { +Vector2 InputEventMouse::get_global_position() const { return global_pos; } @@ -347,15 +350,15 @@ void InputEventMouse::_bind_methods() { ClassDB::bind_method(D_METHOD("set_button_mask", "button_mask"), &InputEventMouse::set_button_mask); ClassDB::bind_method(D_METHOD("get_button_mask"), &InputEventMouse::get_button_mask); - ClassDB::bind_method(D_METHOD("set_pos", "pos"), &InputEventMouse::set_pos); - ClassDB::bind_method(D_METHOD("get_pos"), &InputEventMouse::get_pos); + ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventMouse::set_position); + ClassDB::bind_method(D_METHOD("get_position"), &InputEventMouse::get_position); - ClassDB::bind_method(D_METHOD("set_global_pos", "global_pos"), &InputEventMouse::set_global_pos); - ClassDB::bind_method(D_METHOD("get_global_pos"), &InputEventMouse::get_global_pos); + ClassDB::bind_method(D_METHOD("set_global_position", "global_position"), &InputEventMouse::set_global_position); + ClassDB::bind_method(D_METHOD("get_global_position"), &InputEventMouse::get_global_position); ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask"), "set_button_mask", "get_button_mask"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pos"), "set_pos", "get_pos"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_pos"), "set_global_pos", "get_global_pos"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position"), "set_global_position", "get_global_position"); } InputEventMouse::InputEventMouse() { @@ -404,8 +407,8 @@ bool InputEventMouseButton::is_doubleclick() const { Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const { - Vector2 g = p_xform.xform(get_global_pos()); - Vector2 l = p_xform.xform(get_pos() + p_local_ofs); + Vector2 g = p_xform.xform(get_global_position()); + Vector2 l = p_xform.xform(get_position() + p_local_ofs); Ref<InputEventMouseButton> mb; mb.instance(); @@ -418,8 +421,8 @@ Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, co mb->set_control(get_control()); mb->set_metakey(get_metakey()); - mb->set_pos(l); - mb->set_global_pos(g); + mb->set_position(l); + mb->set_global_position(g); mb->set_button_mask(get_button_mask()); mb->set_pressed(pressed); @@ -489,8 +492,8 @@ Vector2 InputEventMouseMotion::get_speed() const { Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const { - Vector2 g = p_xform.xform(get_global_pos()); - Vector2 l = p_xform.xform(get_pos() + p_local_ofs); + Vector2 g = p_xform.xform(get_global_position()); + Vector2 l = p_xform.xform(get_position() + p_local_ofs); Vector2 r = p_xform.basis_xform(get_relative()); Vector2 s = p_xform.basis_xform(get_speed()); @@ -505,8 +508,8 @@ Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, co mm->set_control(get_control()); mm->set_metakey(get_metakey()); - mm->set_pos(l); - mm->set_global_pos(g); + mm->set_position(l); + mm->set_global_position(g); mm->set_button_mask(get_button_mask()); mm->set_relative(r); @@ -650,11 +653,11 @@ int InputEventScreenTouch::get_index() const { return index; } -void InputEventScreenTouch::set_pos(const Vector2 &p_pos) { +void InputEventScreenTouch::set_position(const Vector2 &p_pos) { pos = p_pos; } -Vector2 InputEventScreenTouch::get_pos() const { +Vector2 InputEventScreenTouch::get_position() const { return pos; } @@ -675,7 +678,7 @@ Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, co st->set_id(get_id()); st->set_device(get_device()); st->set_index(index); - st->set_pos(p_xform.xform(pos + p_local_ofs)); + st->set_position(p_xform.xform(pos + p_local_ofs)); st->set_pressed(pressed); return st; @@ -686,14 +689,14 @@ void InputEventScreenTouch::_bind_methods() { ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenTouch::set_index); ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenTouch::get_index); - ClassDB::bind_method(D_METHOD("set_pos", "pos"), &InputEventScreenTouch::set_pos); - ClassDB::bind_method(D_METHOD("get_pos"), &InputEventScreenTouch::get_pos); + ClassDB::bind_method(D_METHOD("set_position", "pos"), &InputEventScreenTouch::set_position); + ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenTouch::get_position); ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed); //ClassDB::bind_method(D_METHOD("is_pressed"),&InputEventScreenTouch::is_pressed); ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pos"), "set_pos", "get_pos"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed"); } @@ -715,11 +718,11 @@ int InputEventScreenDrag::get_index() const { return index; } -void InputEventScreenDrag::set_pos(const Vector2 &p_pos) { +void InputEventScreenDrag::set_position(const Vector2 &p_pos) { pos = p_pos; } -Vector2 InputEventScreenDrag::get_pos() const { +Vector2 InputEventScreenDrag::get_position() const { return pos; } @@ -752,7 +755,7 @@ Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, con sd->set_device(get_device()); sd->set_index(index); - sd->set_pos(p_xform.xform(pos + p_local_ofs)); + sd->set_position(p_xform.xform(pos + p_local_ofs)); sd->set_relative(p_xform.basis_xform(relative)); sd->set_speed(p_xform.basis_xform(speed)); @@ -764,8 +767,8 @@ void InputEventScreenDrag::_bind_methods() { ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index); ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index); - ClassDB::bind_method(D_METHOD("set_pos", "pos"), &InputEventScreenDrag::set_pos); - ClassDB::bind_method(D_METHOD("get_pos"), &InputEventScreenDrag::get_pos); + ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenDrag::set_position); + ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenDrag::get_position); ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventScreenDrag::set_relative); ClassDB::bind_method(D_METHOD("get_relative"), &InputEventScreenDrag::get_relative); @@ -774,7 +777,7 @@ void InputEventScreenDrag::_bind_methods() { ClassDB::bind_method(D_METHOD("get_speed"), &InputEventScreenDrag::get_speed); ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pos"), "set_pos", "get_pos"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed"); } diff --git a/core/os/input_event.h b/core/os/input_event.h index 31f88b295b..6a694df345 100644 --- a/core/os/input_event.h +++ b/core/os/input_event.h @@ -265,11 +265,11 @@ public: void set_button_mask(int p_mask); int get_button_mask() const; - void set_pos(const Vector2 &p_pos); - Vector2 get_pos() const; + void set_position(const Vector2 &p_pos); + Vector2 get_position() const; - void set_global_pos(const Vector2 &p_global_pos); - Vector2 get_global_pos() const; + void set_global_position(const Vector2 &p_global_pos); + Vector2 get_global_position() const; InputEventMouse(); }; @@ -390,8 +390,8 @@ public: void set_index(int p_index); int get_index() const; - void set_pos(const Vector2 &p_pos); - Vector2 get_pos() const; + void set_position(const Vector2 &p_pos); + Vector2 get_position() const; void set_pressed(bool p_pressed); virtual bool is_pressed() const; @@ -416,8 +416,8 @@ public: void set_index(int p_index); int get_index() const; - void set_pos(const Vector2 &p_pos); - Vector2 get_pos() const; + void set_position(const Vector2 &p_pos); + Vector2 get_position() const; void set_relative(const Vector2 &p_relative); Vector2 get_relative() const; diff --git a/core/os/os.h b/core/os/os.h index 037ce436c1..11fe8b44e3 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -413,4 +413,6 @@ public: virtual ~OS(); }; +VARIANT_ENUM_CAST(PowerState); + #endif diff --git a/core/variant.cpp b/core/variant.cpp index ae5141b8bf..0807a33788 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -2549,8 +2549,8 @@ uint32_t Variant::hash() const { } break; case RECT2: { - uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->pos.x); - hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->pos.y, hash); + uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.x); + hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.y, hash); hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash); return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash); } break; @@ -2590,7 +2590,7 @@ uint32_t Variant::hash() const { uint32_t hash = 5831; for (int i = 0; i < 3; i++) { - hash = hash_djb2_one_float(_data._rect3->pos[i], hash); + hash = hash_djb2_one_float(_data._rect3->position[i], hash); hash = hash_djb2_one_float(_data._rect3->size[i], hash); } @@ -2820,7 +2820,7 @@ bool Variant::hash_compare(const Variant &p_variant) const { const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem); const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem); - return (hash_compare_vector2(l->pos, r->pos)) && + return (hash_compare_vector2(l->position, r->position)) && (hash_compare_vector2(l->size, r->size)); } break; @@ -2855,7 +2855,7 @@ bool Variant::hash_compare(const Variant &p_variant) const { const Rect3 *l = _data._rect3; const Rect3 *r = p_variant._data._rect3; - return (hash_compare_vector3(l->pos, r->pos) && + return (hash_compare_vector3(l->position, r->position) && (hash_compare_vector3(l->size, r->size))); } break; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 6568dc877e..9ead727a80 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -354,6 +354,8 @@ struct _VariantCall { VCALL_LOCALMEM1R(Rect2, merge); VCALL_LOCALMEM1R(Rect2, has_point); VCALL_LOCALMEM1R(Rect2, grow); + VCALL_LOCALMEM2R(Rect2, grow_margin); + VCALL_LOCALMEM4R(Rect2, grow_individual); VCALL_LOCALMEM1R(Rect2, expand); VCALL_LOCALMEM0R(Vector3, min_axis); @@ -1433,6 +1435,8 @@ void register_variant_methods() { ADDFUNC1(RECT2, RECT2, Rect2, merge, RECT2, "b", varray()); ADDFUNC1(RECT2, BOOL, Rect2, has_point, VECTOR2, "point", varray()); ADDFUNC1(RECT2, RECT2, Rect2, grow, REAL, "by", varray()); + ADDFUNC2(RECT2, RECT2, Rect2, grow_margin, INT, "margin", REAL, "by", varray()); + ADDFUNC4(RECT2, RECT2, Rect2, grow_individual, REAL, "left", REAL, "top", REAL, "right", REAL, " bottom", varray()); ADDFUNC1(RECT2, RECT2, Rect2, expand, VECTOR2, "to", varray()); ADDFUNC0(VECTOR3, INT, Vector3, min_axis, varray()); diff --git a/core/variant_op.cpp b/core/variant_op.cpp index 7b9b7abd9e..7b2d0f6886 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -1131,9 +1131,9 @@ void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid) const String *str = reinterpret_cast<const String *>(p_index._data._mem); Rect2 *v = reinterpret_cast<Rect2 *>(_data._mem); - if (*str == "pos") { + if (*str == "position") { valid = true; - v->pos = p_value; + v->position = p_value; return; } else if (*str == "size") { valid = true; @@ -1141,7 +1141,7 @@ void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid) return; } else if (*str == "end") { valid = true; - v->size = Vector2(p_value) - v->pos; + v->size = Vector2(p_value) - v->position; return; } } @@ -1304,9 +1304,9 @@ void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid) const String *str = reinterpret_cast<const String *>(p_index._data._mem); Rect3 *v = _data._rect3; - if (*str == "pos") { + if (*str == "position") { valid = true; - v->pos = p_value; + v->position = p_value; return; } else if (*str == "size") { valid = true; @@ -1314,7 +1314,7 @@ void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid) return; } else if (*str == "end") { valid = true; - v->size = Vector3(p_value) - v->pos; + v->size = Vector3(p_value) - v->position; return; } } @@ -1589,15 +1589,15 @@ Variant Variant::get(const Variant &p_index, bool *r_valid) const { const String *str = reinterpret_cast<const String *>(p_index._data._mem); const Rect2 *v = reinterpret_cast<const Rect2 *>(_data._mem); - if (*str == "pos") { + if (*str == "position") { valid = true; - return v->pos; + return v->position; } else if (*str == "size") { valid = true; return v->size; } else if (*str == "end") { valid = true; - return v->size + v->pos; + return v->size + v->position; } } } break; @@ -1718,15 +1718,15 @@ Variant Variant::get(const Variant &p_index, bool *r_valid) const { const String *str = reinterpret_cast<const String *>(p_index._data._mem); const Rect3 *v = _data._rect3; - if (*str == "pos") { + if (*str == "position") { valid = true; - return v->pos; + return v->position; } else if (*str == "size") { valid = true; return v->size; } else if (*str == "end") { valid = true; - return v->size + v->pos; + return v->size + v->position; } } } break; @@ -2111,7 +2111,7 @@ void Variant::get_property_list(List<PropertyInfo> *p_list) const { } break; // 5 case RECT2: { - p_list->push_back(PropertyInfo(Variant::VECTOR2, "pos")); + p_list->push_back(PropertyInfo(Variant::VECTOR2, "position")); p_list->push_back(PropertyInfo(Variant::VECTOR2, "size")); p_list->push_back(PropertyInfo(Variant::VECTOR2, "end")); @@ -2759,7 +2759,7 @@ void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst) case RECT2: { const Rect2 *ra = reinterpret_cast<const Rect2 *>(a._data._mem); const Rect2 *rb = reinterpret_cast<const Rect2 *>(b._data._mem); - r_dst = Rect2(ra->pos + rb->pos * c, ra->size + rb->size * c); + r_dst = Rect2(ra->position + rb->position * c, ra->size + rb->size * c); } return; case VECTOR3: { @@ -2769,7 +2769,7 @@ void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst) case RECT3: { const Rect3 *ra = reinterpret_cast<const Rect3 *>(a._data._mem); const Rect3 *rb = reinterpret_cast<const Rect3 *>(b._data._mem); - r_dst = Rect3(ra->pos + rb->pos * c, ra->size + rb->size * c); + r_dst = Rect3(ra->position + rb->position * c, ra->size + rb->size * c); } return; case QUAT: { @@ -2879,7 +2879,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & } return; case RECT2: { - r_dst = Rect2(reinterpret_cast<const Rect2 *>(a._data._mem)->pos.linear_interpolate(reinterpret_cast<const Rect2 *>(b._data._mem)->pos, c), reinterpret_cast<const Rect2 *>(a._data._mem)->size.linear_interpolate(reinterpret_cast<const Rect2 *>(b._data._mem)->size, c)); + r_dst = Rect2(reinterpret_cast<const Rect2 *>(a._data._mem)->position.linear_interpolate(reinterpret_cast<const Rect2 *>(b._data._mem)->position, c), reinterpret_cast<const Rect2 *>(a._data._mem)->size.linear_interpolate(reinterpret_cast<const Rect2 *>(b._data._mem)->size, c)); } return; case VECTOR3: { @@ -2899,7 +2899,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & } return; case RECT3: { - r_dst = Rect3(a._data._rect3->pos.linear_interpolate(b._data._rect3->pos, c), a._data._rect3->size.linear_interpolate(b._data._rect3->size, c)); + r_dst = Rect3(a._data._rect3->position.linear_interpolate(b._data._rect3->position, c), a._data._rect3->size.linear_interpolate(b._data._rect3->size, c)); } return; case BASIS: { diff --git a/core/variant_parser.cpp b/core/variant_parser.cpp index 55e2bb42e3..26a6a05a30 100644 --- a/core/variant_parser.cpp +++ b/core/variant_parser.cpp @@ -744,7 +744,12 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, return err; if (token.type == TK_PARENTHESIS_CLOSE) { - + Reference *reference = obj->cast_to<Reference>(); + if (reference) { + value = REF(reference); + } else { + value = obj; + } return OK; } @@ -760,7 +765,6 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, } } - get_token(p_stream, token, line, r_err_str); if (token.type != TK_STRING) { r_err_str = "Expected property name as string"; return ERR_PARSE_ERROR; @@ -1616,7 +1620,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str case Variant::RECT2: { Rect2 aabb = p_variant; - p_store_string_func(p_store_string_ud, "Rect2( " + rtosfix(aabb.pos.x) + ", " + rtosfix(aabb.pos.y) + ", " + rtosfix(aabb.size.x) + ", " + rtosfix(aabb.size.y) + " )"); + p_store_string_func(p_store_string_ud, "Rect2( " + rtosfix(aabb.position.x) + ", " + rtosfix(aabb.position.y) + ", " + rtosfix(aabb.size.x) + ", " + rtosfix(aabb.size.y) + " )"); } break; case Variant::VECTOR3: { @@ -1633,7 +1637,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str case Variant::RECT3: { Rect3 aabb = p_variant; - p_store_string_func(p_store_string_ud, "Rect3( " + rtosfix(aabb.pos.x) + ", " + rtosfix(aabb.pos.y) + ", " + rtosfix(aabb.pos.z) + ", " + rtosfix(aabb.size.x) + ", " + rtosfix(aabb.size.y) + ", " + rtosfix(aabb.size.z) + " )"); + p_store_string_func(p_store_string_ud, "Rect3( " + rtosfix(aabb.position.x) + ", " + rtosfix(aabb.position.y) + ", " + rtosfix(aabb.position.z) + ", " + rtosfix(aabb.size.x) + ", " + rtosfix(aabb.size.y) + ", " + rtosfix(aabb.size.z) + " )"); } break; case Variant::QUAT: { diff --git a/doc/base/classes.xml b/doc/base/classes.xml index dd827bd3e6..612ab42c17 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -24618,6 +24618,8 @@ </constant> <constant name="COMPRESS_ZLIB" value="3"> </constant> + <constant name="COMPRESS_ZSTD" value="4"> + </constant> </constants> </class> <class name="NetworkedMultiplayerPeer" inherits="PacketPeer" category="Core"> @@ -52006,7 +52008,7 @@ do_property]. Sets environment properties for the entire scene </brief_description> <description> - The [WorldEnvironment] node can be added to a scene in order to set default [Environment] variables for the scene. The [WorldEnvironment] can be overridden by an [Environment] node set on the current [Camera]. Additionally, only one [WorldEnvironment] may be instanced in a given scene at a time. The [WorldEnvironment] allows the user to specify default lighting parameters (e.g. ambient lighting), various post-processing effects (e.g. SSAO, DOF, Tonemapping), and how to draw the background (e.g. solid color, skybox). + The [WorldEnvironment] node can be added to a scene in order to set default [Environment] variables for the scene. The [WorldEnvironment] can be overridden by an [Environment] node set on the current [Camera]. Additionally, only one [WorldEnvironment] may be instanced in a given scene at a time. The [WorldEnvironment] allows the user to specify default lighting parameters (e.g. ambient lighting), various post-processing effects (e.g. SSAO, DOF, Tonemapping), and how to draw the background (e.g. solid color, skybox). </description> <methods> <method name="get_environment" qualifiers="const"> diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cpp index 4f6d68de43..8123feccdc 100644 --- a/drivers/gles3/rasterizer_canvas_gles3.cpp +++ b/drivers/gles3/rasterizer_canvas_gles3.cpp @@ -29,8 +29,8 @@ /*************************************************************************/ #include "rasterizer_canvas_gles3.h" +#include "global_config.h" #include "os/os.h" - #ifndef GLES_OVER_GL #define glClearDepth glClearDepthf #endif @@ -113,7 +113,7 @@ void RasterizerCanvasGLES3::light_internal_update(RID p_rid, Light *p_light) { li->ubo_data.light_pos[0] = p_light->light_shader_pos.x; li->ubo_data.light_pos[1] = p_light->light_shader_pos.y; - li->ubo_data.shadowpixel_size = 1.0 / p_light->shadow_buffer_size; + li->ubo_data.shadowpixel_size = (1.0 / p_light->shadow_buffer_size) * (1.0 + p_light->shadow_smooth); li->ubo_data.light_outside_alpha = p_light->mode == VS::CANVAS_LIGHT_MODE_MASK ? 1.0 : 0.0; li->ubo_data.light_height = p_light->height; if (p_light->radius_cache == 0) @@ -247,113 +247,56 @@ void RasterizerCanvasGLES3::_set_texture_rect_mode(bool p_enable) { state.using_texture_rect = p_enable; } -void RasterizerCanvasGLES3::_draw_polygon(int p_vertex_count, const int *p_indices, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, const RID &p_texture, bool p_singlecolor) { +void RasterizerCanvasGLES3::_draw_polygon(const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor) { - bool do_colors = false; - Color m; - if (p_singlecolor) { - m = *p_colors; - glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a); - } else if (!p_colors) { + glBindVertexArray(data.polygon_buffer_pointer_array); + glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer); - glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); - } else - do_colors = true; - - RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(p_texture); + uint32_t buffer_ofs = 0; -#ifndef GLES_NO_CLIENT_ARRAYS + //vertex + glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_vertices); glEnableVertexAttribArray(VS::ARRAY_VERTEX); - glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), p_vertices); - if (do_colors) { + glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), ((float *)0) + buffer_ofs); + buffer_ofs += sizeof(Vector2) * p_vertex_count; + //color - glEnableVertexAttribArray(VS::ARRAY_COLOR); - glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), p_colors); - } else { + if (p_singlecolor) { glDisableVertexAttribArray(VS::ARRAY_COLOR); - } - - if (texture && p_uvs) { - - glEnableVertexAttribArray(VS::ARRAY_TEX_UV); - glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), p_uvs); - } else { - glDisableVertexAttribArray(VS::ARRAY_TEX_UV); - } - - if (p_indices) { - glDrawElements(GL_TRIANGLES, p_vertex_count, GL_UNSIGNED_INT, p_indices); + Color m = *p_colors; + glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a); + } else if (!p_colors) { + glDisableVertexAttribArray(VS::ARRAY_COLOR); + glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); } else { - glDrawArrays(GL_TRIANGLES, 0, p_vertex_count); - } - -#else //WebGL specific impl. - glBindBuffer(GL_ARRAY_BUFFER, gui_quad_buffer); - float *b = GlobalVertexBuffer; - int ofs = 0; - if (p_vertex_count > MAX_POLYGON_VERTICES) { - print_line("Too many vertices to render"); - return; - } - glEnableVertexAttribArray(VS::ARRAY_VERTEX); - glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(float) * 2, ((float *)0) + ofs); - for (int i = 0; i < p_vertex_count; i++) { - b[ofs++] = p_vertices[i].x; - b[ofs++] = p_vertices[i].y; - } - - if (p_colors && do_colors) { + glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors); glEnableVertexAttribArray(VS::ARRAY_COLOR); - glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(float) * 4, ((float *)0) + ofs); - for (int i = 0; i < p_vertex_count; i++) { - b[ofs++] = p_colors[i].r; - b[ofs++] = p_colors[i].g; - b[ofs++] = p_colors[i].b; - b[ofs++] = p_colors[i].a; - } - - } else { - glDisableVertexAttribArray(VS::ARRAY_COLOR); + glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), ((float *)0) + buffer_ofs); + buffer_ofs += sizeof(Color) * p_vertex_count; } if (p_uvs) { + glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs); glEnableVertexAttribArray(VS::ARRAY_TEX_UV); - glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(float) * 2, ((float *)0) + ofs); - for (int i = 0; i < p_vertex_count; i++) { - b[ofs++] = p_uvs[i].x; - b[ofs++] = p_uvs[i].y; - } + glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), ((float *)0) + buffer_ofs); + buffer_ofs += sizeof(Vector2) * p_vertex_count; } else { glDisableVertexAttribArray(VS::ARRAY_TEX_UV); } - glBufferSubData(GL_ARRAY_BUFFER, 0, ofs * 4, &b[0]); - //bind the indices buffer. - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices_buffer); - - static const int _max_draw_poly_indices = 16 * 1024; // change this size if needed!!! - ERR_FAIL_COND(p_vertex_count > _max_draw_poly_indices); - static uint16_t _draw_poly_indices[_max_draw_poly_indices]; - for (int i = 0; i < p_vertex_count; i++) { - _draw_poly_indices[i] = p_indices[i]; - //OS::get_singleton()->print("ind: %d ", p_indices[i]); - }; - - //copy the data to GPU. - glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, p_vertex_count * sizeof(uint16_t), &_draw_poly_indices[0]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer); + glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(int) * p_index_count, p_indices); //draw the triangles. - glDrawElements(GL_TRIANGLES, p_vertex_count, GL_UNSIGNED_SHORT, 0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); -#endif + glDrawElements(GL_TRIANGLES, p_index_count, GL_UNSIGNED_INT, 0); storage->frame.canvas_draw_commands++; + + glBindVertexArray(0); } void RasterizerCanvasGLES3::_draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs) { @@ -404,9 +347,9 @@ void RasterizerCanvasGLES3::_draw_gui_primitive(int p_points, const Vector2 *p_v } } - glBindBuffer(GL_ARRAY_BUFFER, data.primitive_quad_buffer); + glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer); glBufferSubData(GL_ARRAY_BUFFER, 0, p_points * stride * 4, &b[0]); - glBindVertexArray(data.primitive_quad_buffer_arrays[version]); + glBindVertexArray(data.polygon_buffer_quad_arrays[version]); glDrawArrays(prim[p_points], 0, p_points); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -473,7 +416,7 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur } Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height); - Rect2 src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.pos * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1); + Rect2 src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1); if (rect->flags & CANVAS_RECT_FLIP_H) { src_rect.size.x *= -1; @@ -489,8 +432,8 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size); - glVertexAttrib4f(1, rect->rect.pos.x, rect->rect.pos.y, rect->rect.size.x, rect->rect.size.y); - glVertexAttrib4f(2, src_rect.pos.x, src_rect.pos.y, src_rect.size.x, src_rect.size.y); + glVertexAttrib4f(1, rect->rect.position.x, rect->rect.position.y, rect->rect.size.x, rect->rect.size.y); + glVertexAttrib4f(2, src_rect.position.x, src_rect.position.y, src_rect.size.x, src_rect.size.y); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); if (untile) { @@ -500,7 +443,7 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur } else { - glVertexAttrib4f(1, rect->rect.pos.x, rect->rect.pos.y, rect->rect.size.x, rect->rect.size.y); + glVertexAttrib4f(1, rect->rect.position.x, rect->rect.position.y, rect->rect.size.x, rect->rect.size.y); glVertexAttrib4f(2, 0, 0, 1, 1); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } @@ -521,7 +464,7 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur if (!texture) { - glVertexAttrib4f(1, np->rect.pos.x, np->rect.pos.y, np->rect.size.x, np->rect.size.y); + glVertexAttrib4f(1, np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y); glVertexAttrib4f(2, 0, 0, 1, 1); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); continue; @@ -535,50 +478,50 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur #define SRCRECT(m_x, m_y, m_w, m_h) glVertexAttrib4f(2, (m_x)*texpixel_size.x, (m_y)*texpixel_size.y, (m_w)*texpixel_size.x, (m_h)*texpixel_size.y) //top left - DSTRECT(np->rect.pos.x, np->rect.pos.y, np->margin[MARGIN_LEFT], np->margin[MARGIN_TOP]); - SRCRECT(np->source.pos.x, np->source.pos.y, np->margin[MARGIN_LEFT], np->margin[MARGIN_TOP]); + DSTRECT(np->rect.position.x, np->rect.position.y, np->margin[MARGIN_LEFT], np->margin[MARGIN_TOP]); + SRCRECT(np->source.position.x, np->source.position.y, np->margin[MARGIN_LEFT], np->margin[MARGIN_TOP]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); //top right - DSTRECT(np->rect.pos.x + np->rect.size.width - np->margin[MARGIN_RIGHT], np->rect.pos.y, np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); - SRCRECT(np->source.pos.x + np->source.size.width - np->margin[MARGIN_RIGHT], np->source.pos.y, np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); + DSTRECT(np->rect.position.x + np->rect.size.width - np->margin[MARGIN_RIGHT], np->rect.position.y, np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); + SRCRECT(np->source.position.x + np->source.size.width - np->margin[MARGIN_RIGHT], np->source.position.y, np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); //bottom right - DSTRECT(np->rect.pos.x + np->rect.size.width - np->margin[MARGIN_RIGHT], np->rect.pos.y + np->rect.size.height - np->margin[MARGIN_BOTTOM], np->margin[MARGIN_RIGHT], np->margin[MARGIN_BOTTOM]); - SRCRECT(np->source.pos.x + np->source.size.width - np->margin[MARGIN_RIGHT], np->source.pos.y + np->source.size.height - np->margin[MARGIN_BOTTOM], np->margin[MARGIN_RIGHT], np->margin[MARGIN_BOTTOM]); + DSTRECT(np->rect.position.x + np->rect.size.width - np->margin[MARGIN_RIGHT], np->rect.position.y + np->rect.size.height - np->margin[MARGIN_BOTTOM], np->margin[MARGIN_RIGHT], np->margin[MARGIN_BOTTOM]); + SRCRECT(np->source.position.x + np->source.size.width - np->margin[MARGIN_RIGHT], np->source.position.y + np->source.size.height - np->margin[MARGIN_BOTTOM], np->margin[MARGIN_RIGHT], np->margin[MARGIN_BOTTOM]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); //bottom left - DSTRECT(np->rect.pos.x, np->rect.pos.y + np->rect.size.height - np->margin[MARGIN_BOTTOM], np->margin[MARGIN_LEFT], np->margin[MARGIN_BOTTOM]); - SRCRECT(np->source.pos.x, np->source.pos.y + np->source.size.height - np->margin[MARGIN_BOTTOM], np->margin[MARGIN_LEFT], np->margin[MARGIN_BOTTOM]); + DSTRECT(np->rect.position.x, np->rect.position.y + np->rect.size.height - np->margin[MARGIN_BOTTOM], np->margin[MARGIN_LEFT], np->margin[MARGIN_BOTTOM]); + SRCRECT(np->source.position.x, np->source.position.y + np->source.size.height - np->margin[MARGIN_BOTTOM], np->margin[MARGIN_LEFT], np->margin[MARGIN_BOTTOM]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); //top - DSTRECT(np->rect.pos.x + np->margin[MARGIN_LEFT], np->rect.pos.y, np->rect.size.width - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); - SRCRECT(np->source.pos.x + np->margin[MARGIN_LEFT], np->source.pos.y, np->source.size.width - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); + DSTRECT(np->rect.position.x + np->margin[MARGIN_LEFT], np->rect.position.y, np->rect.size.width - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); + SRCRECT(np->source.position.x + np->margin[MARGIN_LEFT], np->source.position.y, np->source.size.width - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); //bottom - DSTRECT(np->rect.pos.x + np->margin[MARGIN_LEFT], np->rect.pos.y + np->rect.size.height - np->margin[MARGIN_BOTTOM], np->rect.size.width - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); - SRCRECT(np->source.pos.x + np->margin[MARGIN_LEFT], np->source.pos.y + np->source.size.height - np->margin[MARGIN_BOTTOM], np->source.size.width - np->margin[MARGIN_LEFT] - np->margin[MARGIN_LEFT], np->margin[MARGIN_TOP]); + DSTRECT(np->rect.position.x + np->margin[MARGIN_LEFT], np->rect.position.y + np->rect.size.height - np->margin[MARGIN_BOTTOM], np->rect.size.width - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->margin[MARGIN_TOP]); + SRCRECT(np->source.position.x + np->margin[MARGIN_LEFT], np->source.position.y + np->source.size.height - np->margin[MARGIN_BOTTOM], np->source.size.width - np->margin[MARGIN_LEFT] - np->margin[MARGIN_LEFT], np->margin[MARGIN_TOP]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); //left - DSTRECT(np->rect.pos.x, np->rect.pos.y + np->margin[MARGIN_TOP], np->margin[MARGIN_LEFT], np->rect.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); - SRCRECT(np->source.pos.x, np->source.pos.y + np->margin[MARGIN_TOP], np->margin[MARGIN_LEFT], np->source.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); + DSTRECT(np->rect.position.x, np->rect.position.y + np->margin[MARGIN_TOP], np->margin[MARGIN_LEFT], np->rect.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); + SRCRECT(np->source.position.x, np->source.position.y + np->margin[MARGIN_TOP], np->margin[MARGIN_LEFT], np->source.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); //right - DSTRECT(np->rect.pos.x + np->rect.size.width - np->margin[MARGIN_RIGHT], np->rect.pos.y + np->margin[MARGIN_TOP], np->margin[MARGIN_RIGHT], np->rect.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); - SRCRECT(np->source.pos.x + np->source.size.width - np->margin[MARGIN_RIGHT], np->source.pos.y + np->margin[MARGIN_TOP], np->margin[MARGIN_RIGHT], np->source.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); + DSTRECT(np->rect.position.x + np->rect.size.width - np->margin[MARGIN_RIGHT], np->rect.position.y + np->margin[MARGIN_TOP], np->margin[MARGIN_RIGHT], np->rect.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); + SRCRECT(np->source.position.x + np->source.size.width - np->margin[MARGIN_RIGHT], np->source.position.y + np->margin[MARGIN_TOP], np->margin[MARGIN_RIGHT], np->source.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); if (np->draw_center) { //center - DSTRECT(np->rect.pos.x + np->margin[MARGIN_LEFT], np->rect.pos.y + np->margin[MARGIN_TOP], np->rect.size.x - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->rect.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); - SRCRECT(np->source.pos.x + np->margin[MARGIN_LEFT], np->source.pos.y + np->margin[MARGIN_TOP], np->source.size.x - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->source.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); + DSTRECT(np->rect.position.x + np->margin[MARGIN_LEFT], np->rect.position.y + np->margin[MARGIN_TOP], np->rect.size.x - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->rect.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); + SRCRECT(np->source.position.x + np->margin[MARGIN_LEFT], np->source.position.y + np->margin[MARGIN_TOP], np->source.size.x - np->margin[MARGIN_LEFT] - np->margin[MARGIN_RIGHT], np->source.size.height - np->margin[MARGIN_TOP] - np->margin[MARGIN_BOTTOM]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } @@ -624,7 +567,7 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height); state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size); } - //_draw_polygon(polygon->count,polygon->indices.ptr(),polygon->points.ptr(),polygon->uvs.ptr(),polygon->colors.ptr(),polygon->texture,polygon->colors.size()==1); + _draw_polygon(polygon->indices.ptr(), polygon->count, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1); } break; case Item::Command::TYPE_CIRCLE: { @@ -644,6 +587,9 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur indices[i * 3 + 1] = (i + 1) % numpoints; indices[i * 3 + 2] = numpoints; } + + _draw_polygon(indices, numpoints * 3, numpoints + 1, points, NULL, &circle->color, true); + //_draw_polygon(numpoints*3,indices,points,NULL,&circle->color,RID(),true); //canvas_draw_circle(circle->indices.size(),circle->indices.ptr(),circle->points.ptr(),circle->uvs.ptr(),circle->colors.ptr(),circle->texture,circle->colors.size()==1); } break; @@ -670,8 +616,8 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur //glScissor(viewport.x+current_clip->final_clip_rect.pos.x,viewport.y+ (viewport.height-(current_clip->final_clip_rect.pos.y+current_clip->final_clip_rect.size.height)), //current_clip->final_clip_rect.size.width,current_clip->final_clip_rect.size.height); - int x = current_clip->final_clip_rect.pos.x; - int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.pos.y + current_clip->final_clip_rect.size.y); + int x = current_clip->final_clip_rect.position.x; + int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y); int w = current_clip->final_clip_rect.size.x; int h = current_clip->final_clip_rect.size.y; @@ -787,7 +733,7 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons if (current_clip) { glEnable(GL_SCISSOR_TEST); - glScissor(current_clip->final_clip_rect.pos.x, (rt_size.height - (current_clip->final_clip_rect.pos.y + current_clip->final_clip_rect.size.height)), current_clip->final_clip_rect.size.width, current_clip->final_clip_rect.size.height); + glScissor(current_clip->final_clip_rect.position.x, (rt_size.height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.height)), current_clip->final_clip_rect.size.width, current_clip->final_clip_rect.size.height); } else { @@ -1108,7 +1054,7 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons if (reclip) { glEnable(GL_SCISSOR_TEST); - glScissor(current_clip->final_clip_rect.pos.x, (rt_size.height - (current_clip->final_clip_rect.pos.y + current_clip->final_clip_rect.size.height)), current_clip->final_clip_rect.size.width, current_clip->final_clip_rect.size.height); + glScissor(current_clip->final_clip_rect.position.x, (rt_size.height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.height)), current_clip->final_clip_rect.size.width, current_clip->final_clip_rect.size.height); } p_item_list = p_item_list->next; @@ -1165,7 +1111,6 @@ void RasterizerCanvasGLES3::canvas_light_shadow_buffer_update(RID p_buffer, cons glBindFramebuffer(GL_FRAMEBUFFER, cls->fbo); - glEnableVertexAttribArray(VS::ARRAY_VERTEX); state.canvas_shadow_shader.bind(); glViewport(0, 0, cls->size, cls->height); @@ -1264,18 +1209,14 @@ void RasterizerCanvasGLES3::canvas_light_shadow_buffer_update(RID p_buffer, cons } } */ - glBindBuffer(GL_ARRAY_BUFFER, cc->vertex_id); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cc->index_id); - glVertexAttribPointer(VS::ARRAY_VERTEX, 3, GL_FLOAT, false, 0, 0); + glBindVertexArray(cc->array_id); glDrawElements(GL_TRIANGLES, cc->len * 3, GL_UNSIGNED_SHORT, 0); instance = instance->next; } } - glDisableVertexAttribArray(VS::ARRAY_VERTEX); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindVertexArray(0); } void RasterizerCanvasGLES3::reset_canvas() { @@ -1340,8 +1281,8 @@ void RasterizerCanvasGLES3::reset_canvas() { void RasterizerCanvasGLES3::draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src) { - glVertexAttrib4f(1, p_rect.pos.x, p_rect.pos.y, p_rect.size.x, p_rect.size.y); - glVertexAttrib4f(2, p_src.pos.x, p_src.pos.y, p_src.size.x, p_src.size.y); + glVertexAttrib4f(1, p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y); + glVertexAttrib4f(2, p_src.position.x, p_src.position.y, p_src.size.x, p_src.size.y); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } @@ -1376,15 +1317,19 @@ void RasterizerCanvasGLES3::initialize() { { - glGenBuffers(1, &data.primitive_quad_buffer); - glBindBuffer(GL_ARRAY_BUFFER, data.primitive_quad_buffer); - glBufferData(GL_ARRAY_BUFFER, (2 + 2 + 4) * 4 * sizeof(float), NULL, GL_DYNAMIC_DRAW); //allocate max size + uint32_t poly_size = GLOBAL_DEF("rendering/buffers/canvas_polygon_buffer_size_kb", 128); + poly_size *= 1024; //kb + poly_size = MAX(poly_size, (2 + 2 + 4) * 4 * sizeof(float)); + glGenBuffers(1, &data.polygon_buffer); + glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer); + glBufferData(GL_ARRAY_BUFFER, poly_size, NULL, GL_DYNAMIC_DRAW); //allocate max size glBindBuffer(GL_ARRAY_BUFFER, 0); + //quad arrays for (int i = 0; i < 4; i++) { - glGenVertexArrays(1, &data.primitive_quad_buffer_arrays[i]); - glBindVertexArray(data.primitive_quad_buffer_arrays[i]); - glBindBuffer(GL_ARRAY_BUFFER, data.primitive_quad_buffer); + glGenVertexArrays(1, &data.polygon_buffer_quad_arrays[i]); + glBindVertexArray(data.polygon_buffer_quad_arrays[i]); + glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer); int uv_ofs = 0; int color_ofs = 0; @@ -1415,6 +1360,15 @@ void RasterizerCanvasGLES3::initialize() { glBindVertexArray(0); } + + glGenVertexArrays(1, &data.polygon_buffer_pointer_array); + + uint32_t index_size = GLOBAL_DEF("rendering/buffers/canvas_polygon_index_buffer_size_kb", 128); + index_size *= 1024; //kb + glGenBuffers(1, &data.polygon_index_buffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_size, NULL, GL_DYNAMIC_DRAW); //allocate max size + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } store_transform(Transform(), state.canvas_item_ubo_data.projection_matrix); @@ -1436,6 +1390,8 @@ void RasterizerCanvasGLES3::finalize() { glDeleteBuffers(1, &data.canvas_quad_vertices); glDeleteVertexArrays(1, &data.canvas_quad_array); + + glDeleteVertexArrays(1, &data.polygon_buffer_pointer_array); } RasterizerCanvasGLES3::RasterizerCanvasGLES3() { diff --git a/drivers/gles3/rasterizer_canvas_gles3.h b/drivers/gles3/rasterizer_canvas_gles3.h index 95ef9ee443..2da0cc0afd 100644 --- a/drivers/gles3/rasterizer_canvas_gles3.h +++ b/drivers/gles3/rasterizer_canvas_gles3.h @@ -47,8 +47,11 @@ public: GLuint canvas_quad_vertices; GLuint canvas_quad_array; - GLuint primitive_quad_buffer; - GLuint primitive_quad_buffer_arrays[4]; + GLuint polygon_buffer; + GLuint polygon_buffer_quad_arrays[4]; + GLuint polygon_buffer_pointer_array; + GLuint polygon_index_buffer; + uint32_t polygon_buffer_size; } data; @@ -107,7 +110,7 @@ public: _FORCE_INLINE_ RasterizerStorageGLES3::Texture *_bind_canvas_texture(const RID &p_texture); _FORCE_INLINE_ void _draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs); - _FORCE_INLINE_ void _draw_polygon(int p_vertex_count, const int *p_indices, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, const RID &p_texture, bool p_singlecolor); + _FORCE_INLINE_ void _draw_polygon(const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor); _FORCE_INLINE_ void _canvas_item_render_commands(Item *p_item, Item *current_clip, bool &reclip); virtual void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light); diff --git a/drivers/gles3/rasterizer_gles3.cpp b/drivers/gles3/rasterizer_gles3.cpp index aa4150cbe4..53df7e9c3d 100644 --- a/drivers/gles3/rasterizer_gles3.cpp +++ b/drivers/gles3/rasterizer_gles3.cpp @@ -212,11 +212,8 @@ void RasterizerGLES3::begin_frame() { storage->update_dirty_resources(); - storage->info.render_object_count = 0; - storage->info.render_material_switch_count = 0; - storage->info.render_surface_switch_count = 0; - storage->info.render_shader_rebind_count = 0; - storage->info.render_vertices_count = 0; + storage->info.render_final = storage->info.render; + storage->info.render.reset(); scene->iteration(); } @@ -301,18 +298,18 @@ void RasterizerGLES3::set_boot_image(const Ref<Image> &p_image, const Color &p_c //scale horizontally screenrect.size.y = window_h; screenrect.size.x = imgrect.size.x * window_h / imgrect.size.y; - screenrect.pos.x = (window_w - screenrect.size.x) / 2; + screenrect.position.x = (window_w - screenrect.size.x) / 2; } else { //scale vertically screenrect.size.x = window_w; screenrect.size.y = imgrect.size.y * window_w / imgrect.size.x; - screenrect.pos.y = (window_h - screenrect.size.y) / 2; + screenrect.position.y = (window_h - screenrect.size.y) / 2; } } else { screenrect = imgrect; - screenrect.pos += ((Size2(window_w, window_h) - screenrect.size) / 2.0).floor(); + screenrect.position += ((Size2(window_w, window_h) - screenrect.size) / 2.0).floor(); } RasterizerStorageGLES3::Texture *t = storage->texture_owner.get(texture); diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cpp index 7138363796..87a2ca8bb9 100644 --- a/drivers/gles3/rasterizer_scene_gles3.cpp +++ b/drivers/gles3/rasterizer_scene_gles3.cpp @@ -933,6 +933,49 @@ void RasterizerSceneGLES3::environment_set_tonemap(RID p_env, VS::EnvironmentTon } void RasterizerSceneGLES3::environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) { + + Environment *env = environment_owner.getornull(p_env); + ERR_FAIL_COND(!env); + + env->adjustments_enabled = p_enable; + env->adjustments_brightness = p_brightness; + env->adjustments_contrast = p_contrast; + env->adjustments_saturation = p_saturation; + env->color_correction = p_ramp; +} + +void RasterizerSceneGLES3::environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) { + + Environment *env = environment_owner.getornull(p_env); + ERR_FAIL_COND(!env); + + env->fog_enabled = p_enable; + env->fog_color = p_color; + env->fog_sun_color = p_sun_color; + env->fog_sun_amount = p_sun_amount; +} + +void RasterizerSceneGLES3::environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_curve, bool p_transmit, float p_transmit_curve) { + + Environment *env = environment_owner.getornull(p_env); + ERR_FAIL_COND(!env); + + env->fog_depth_enabled = p_enable; + env->fog_depth_begin = p_depth_begin; + env->fog_depth_curve = p_depth_curve; + env->fog_transmit_enabled = p_transmit; + env->fog_transmit_curve = p_transmit_curve; +} + +void RasterizerSceneGLES3::environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve) { + + Environment *env = environment_owner.getornull(p_env); + ERR_FAIL_COND(!env); + + env->fog_height_enabled = p_enable; + env->fog_height_min = p_min_height; + env->fog_height_max = p_max_height; + env->fog_height_curve = p_height_curve; } RID RasterizerSceneGLES3::light_instance_create(RID p_light) { @@ -1247,8 +1290,11 @@ void RasterizerSceneGLES3::_setup_geometry(RenderList::Element *e, const Transfo storage->mesh_render_blend_shapes(s, e->instance->blend_values.ptr()); //rebind shader state.scene_shader.bind(); +#ifdef DEBUG_ENABLED + } else if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) { + glBindVertexArray(s->array_wireframe_id); // everything is so easy nowadays +#endif } else { - glBindVertexArray(s->array_id); // everything is so easy nowadays } @@ -1258,7 +1304,16 @@ void RasterizerSceneGLES3::_setup_geometry(RenderList::Element *e, const Transfo RasterizerStorageGLES3::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES3::MultiMesh *>(e->owner); RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry); - glBindVertexArray(s->instancing_array_id); // use the instancing array ID +#ifdef DEBUG_ENABLED + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) { + + glBindVertexArray(s->instancing_array_wireframe_id); // use the instancing array ID + } else +#endif + { + glBindVertexArray(s->instancing_array_id); // use the instancing array ID + } + glBindBuffer(GL_ARRAY_BUFFER, multi_mesh->buffer); //modify the buffer int stride = (multi_mesh->xform_floats + multi_mesh->color_floats) * 4; @@ -1323,13 +1378,26 @@ void RasterizerSceneGLES3::_setup_geometry(RenderList::Element *e, const Transfo sorter.sort(particle_array, particles->amount); glUnmapBuffer(GL_ARRAY_BUFFER); +#ifdef DEBUG_ENABLED + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) { + glBindVertexArray(s->instancing_array_wireframe_id); // use the wireframe instancing array ID + } else +#endif + { - glBindVertexArray(s->instancing_array_id); // use the instancing array ID + glBindVertexArray(s->instancing_array_id); // use the instancing array ID + } glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffer_histories[1]); //modify the buffer } else { - - glBindVertexArray(s->instancing_array_id); // use the instancing array ID +#ifdef DEBUG_ENABLED + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) { + glBindVertexArray(s->instancing_array_wireframe_id); // use the wireframe instancing array ID + } else +#endif + { + glBindVertexArray(s->instancing_array_id); // use the instancing array ID + } glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[0]); //modify the buffer } @@ -1378,17 +1446,25 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) { RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry); - if (s->index_array_len > 0) { +#ifdef DEBUG_ENABLED + + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) { + + glDrawElements(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0); + storage->info.render.vertices_count += s->index_array_len; + } else +#endif + if (s->index_array_len > 0) { glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0); - storage->info.render_vertices_count += s->index_array_len; + storage->info.render.vertices_count += s->index_array_len; } else { glDrawArrays(gl_primitive[s->primitive], 0, s->array_len); - storage->info.render_vertices_count += s->array_len; + storage->info.render.vertices_count += s->array_len; } } break; @@ -1399,17 +1475,25 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) { int amount = MAX(multi_mesh->size, multi_mesh->visible_instances); - if (s->index_array_len > 0) { +#ifdef DEBUG_ENABLED + + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) { + + glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0, amount); + storage->info.render.vertices_count += s->index_array_len * amount; + } else +#endif + if (s->index_array_len > 0) { glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount); - storage->info.render_vertices_count += s->index_array_len * amount; + storage->info.render.vertices_count += s->index_array_len * amount; } else { glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount); - storage->info.render_vertices_count += s->array_len * amount; + storage->info.render.vertices_count += s->array_len * amount; } } break; @@ -1435,7 +1519,7 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) { int vertices = c.vertices.size(); uint32_t buf_ofs = 0; - storage->info.render_vertices_count += vertices; + storage->info.render.vertices_count += vertices; if (c.texture.is_valid() && storage->texture_owner.owns(c.texture)) { @@ -1557,18 +1641,25 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) { glEnableVertexAttribArray(12); //custom glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + stride * split + sizeof(float) * 4 * 2); glVertexAttribDivisor(12, 1); +#ifdef DEBUG_ENABLED - if (s->index_array_len > 0) { + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) { + + glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0, amount - split); + storage->info.render.vertices_count += s->index_array_len * (amount - split); + } else +#endif + if (s->index_array_len > 0) { glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount - split); - storage->info.render_vertices_count += s->index_array_len * (amount - split); + storage->info.render.vertices_count += s->index_array_len * (amount - split); } else { glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount - split); - storage->info.render_vertices_count += s->array_len * (amount - split); + storage->info.render.vertices_count += s->array_len * (amount - split); } } @@ -1588,34 +1679,49 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) { glEnableVertexAttribArray(12); //custom glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + sizeof(float) * 4 * 2); glVertexAttribDivisor(12, 1); +#ifdef DEBUG_ENABLED - if (s->index_array_len > 0) { + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) { + + glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0, split); + storage->info.render.vertices_count += s->index_array_len * split; + } else +#endif + if (s->index_array_len > 0) { glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, split); - storage->info.render_vertices_count += s->index_array_len * split; + storage->info.render.vertices_count += s->index_array_len * split; } else { glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, split); - storage->info.render_vertices_count += s->array_len * split; + storage->info.render.vertices_count += s->array_len * split; } } } else { - if (s->index_array_len > 0) { +#ifdef DEBUG_ENABLED + + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) { + + glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0, amount); + storage->info.render.vertices_count += s->index_array_len * amount; + } else +#endif + if (s->index_array_len > 0) { glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount); - storage->info.render_vertices_count += s->index_array_len * amount; + storage->info.render.vertices_count += s->index_array_len * amount; } else { glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount); - storage->info.render_vertices_count += s->array_len * amount; + storage->info.render.vertices_count += s->array_len * amount; } } @@ -1697,7 +1803,7 @@ void RasterizerSceneGLES3::_setup_light(RenderList::Element *e, const Transform GIProbeInstance *gipi = gi_probe_instance_owner.getptr(ridp[0]); - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 10); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 9); glBindTexture(GL_TEXTURE_3D, gipi->tex_cache); state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_XFORM1, gipi->transform_to_data * p_view_transform); state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BOUNDS1, gipi->bounds); @@ -1709,7 +1815,7 @@ void RasterizerSceneGLES3::_setup_light(RenderList::Element *e, const Transform GIProbeInstance *gipi2 = gi_probe_instance_owner.getptr(ridp[1]); - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 11); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 10); glBindTexture(GL_TEXTURE_3D, gipi2->tex_cache); state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_XFORM2, gipi2->transform_to_data * p_view_transform); state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BOUNDS2, gipi2->bounds); @@ -1740,19 +1846,10 @@ void RasterizerSceneGLES3::_set_cull(bool p_front, bool p_reverse_cull) { void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements, int p_element_count, const Transform &p_view_transform, const CameraMatrix &p_projection, GLuint p_base_env, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add, bool p_directional_shadows) { - if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]) { - //p_reverse_cull=!p_reverse_cull; - glFrontFace(GL_CCW); - } else { - glFrontFace(GL_CW); - } - glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.scene_ubo); //bind globals ubo if (!p_shadow && !p_directional_add) { glBindBufferBase(GL_UNIFORM_BUFFER, 2, state.env_radiance_ubo); //bind environment radiance info - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1); - glBindTexture(GL_TEXTURE_2D, state.brdf_texture); if (p_base_env) { glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2); @@ -1793,7 +1890,7 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements, int p_ bool first = true; bool prev_use_instancing = false; - storage->info.render_object_count += p_element_count; + storage->info.render.draw_call_count += p_element_count; for (int i = 0; i < p_element_count; i++) { @@ -1934,19 +2031,19 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements, int p_ if (skeleton.is_valid()) { RasterizerStorageGLES3::Skeleton *sk = storage->skeleton_owner.getornull(skeleton); - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 6); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1); glBindTexture(GL_TEXTURE_2D, sk->texture); } } if (material != prev_material || rebind) { - storage->info.render_material_switch_count++; + storage->info.render.material_switch_count++; rebind = _setup_material(material, p_alpha_pass); if (rebind) { - storage->info.render_shader_rebind_count++; + storage->info.render.shader_rebind_count++; } } @@ -1957,7 +2054,7 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements, int p_ if (e->owner != prev_owner || prev_base_type != e->instance->base_type || prev_geometry != e->geometry) { _setup_geometry(e, p_view_transform); - storage->info.render_surface_switch_count++; + storage->info.render.surface_switch_count++; } _set_cull(e->sort_key & RenderList::SORT_KEY_MIRROR_FLAG, p_reverse_cull); @@ -2000,6 +2097,10 @@ void RasterizerSceneGLES3::_add_geometry(RasterizerStorageGLES3::Geometry *p_geo RasterizerStorageGLES3::Material *m = NULL; RID m_src = p_instance->material_override.is_valid() ? p_instance->material_override : (p_material >= 0 ? p_instance->materials[p_material] : p_geometry->material); + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) { + m_src = default_overdraw_material; + } + /* #ifdef DEBUG_ENABLED if (current_debug==VS::SCENARIO_DEBUG_OVERDRAW) { @@ -2012,7 +2113,7 @@ void RasterizerSceneGLES3::_add_geometry(RasterizerStorageGLES3::Geometry *p_geo if (m_src.is_valid()) { m = storage->material_owner.getornull(m_src); - if (!m->shader) { + if (!m->shader || !m->shader->valid) { m = NULL; } } @@ -2023,7 +2124,7 @@ void RasterizerSceneGLES3::_add_geometry(RasterizerStorageGLES3::Geometry *p_geo ERR_FAIL_COND(!m); - bool has_base_alpha = (m->shader->spatial.uses_alpha); + bool has_base_alpha = (m->shader->spatial.uses_alpha || m->shader->spatial.uses_screen_texture); bool has_blend_alpha = m->shader->spatial.blend_mode != RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MIX || m->shader->spatial.ontop; bool has_alpha = has_base_alpha || has_blend_alpha; bool shadow = false; @@ -2038,6 +2139,10 @@ void RasterizerSceneGLES3::_add_geometry(RasterizerStorageGLES3::Geometry *p_geo state.used_sss = true; } + if (m->shader->spatial.uses_screen_texture) { + state.used_screen_texture = true; + } + if (p_shadow) { if (has_blend_alpha || (has_base_alpha && m->shader->spatial.depth_draw_mode != RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS)) @@ -2114,7 +2219,7 @@ void RasterizerSceneGLES3::_add_geometry(RasterizerStorageGLES3::Geometry *p_geo //e->light_type=0xFF; // no lights! - if (shadow || m->shader->spatial.unshaded /*|| current_debug==VS::SCENARIO_DEBUG_SHADELESS*/) { + if (shadow || m->shader->spatial.unshaded || state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_UNSHADED) { e->sort_key |= RenderList::SORT_KEY_UNSHADED_FLAG; } @@ -2213,6 +2318,7 @@ void RasterizerSceneGLES3::_setup_environment(Environment *env, const CameraMatr state.ubo_data.time[i] = storage->frame.time[i]; } + state.ubo_data.z_far = p_cam_projection.get_z_far(); //bg and ambient if (env) { state.ubo_data.bg_energy = env->bg_energy; @@ -2244,6 +2350,30 @@ void RasterizerSceneGLES3::_setup_environment(Environment *env, const CameraMatr state.env_radiance_data.ambient_contribution = env->ambient_sky_contribution; state.ubo_data.ambient_occlusion_affect_light = env->ssao_light_affect; + + //fog + + Color linear_fog = env->fog_color.to_linear(); + state.ubo_data.fog_color_enabled[0] = linear_fog.r; + state.ubo_data.fog_color_enabled[1] = linear_fog.g; + state.ubo_data.fog_color_enabled[2] = linear_fog.b; + state.ubo_data.fog_color_enabled[3] = env->fog_enabled ? 1.0 : 0.0; + + Color linear_sun = env->fog_sun_color.to_linear(); + state.ubo_data.fog_sun_color_amount[0] = linear_sun.r; + state.ubo_data.fog_sun_color_amount[1] = linear_sun.g; + state.ubo_data.fog_sun_color_amount[2] = linear_sun.b; + state.ubo_data.fog_sun_color_amount[3] = env->fog_sun_amount; + state.ubo_data.fog_depth_enabled = env->fog_depth_enabled; + state.ubo_data.fog_depth_begin = env->fog_depth_begin; + state.ubo_data.fog_depth_curve = env->fog_depth_curve; + state.ubo_data.fog_transmit_enabled = env->fog_transmit_enabled; + state.ubo_data.fog_transmit_curve = env->fog_transmit_curve; + state.ubo_data.fog_height_enabled = env->fog_height_enabled; + state.ubo_data.fog_height_min = env->fog_height_min; + state.ubo_data.fog_height_max = env->fog_height_max; + state.ubo_data.fog_height_curve = env->fog_height_curve; + } else { state.ubo_data.bg_energy = 1.0; state.ubo_data.ambient_energy = 1.0; @@ -2261,6 +2391,8 @@ void RasterizerSceneGLES3::_setup_environment(Environment *env, const CameraMatr state.env_radiance_data.ambient_contribution = 0; state.ubo_data.ambient_occlusion_affect_light = 0; + + state.ubo_data.fog_color_enabled[3] = 0.0; } { @@ -2343,8 +2475,8 @@ void RasterizerSceneGLES3::_setup_directional_light(int p_index, const Transform for (int j = 0; j < shadow_count; j++) { - uint32_t x = li->directional_rect.pos.x; - uint32_t y = li->directional_rect.pos.y; + uint32_t x = li->directional_rect.position.x; + uint32_t y = li->directional_rect.position.y; uint32_t width = li->directional_rect.size.x; uint32_t height = li->directional_rect.size.y; @@ -2389,8 +2521,8 @@ void RasterizerSceneGLES3::_setup_directional_light(int p_index, const Transform store_camera(shadow_mtx, &ubo_data.shadow_matrix1[16 * j]); - ubo_data.light_clamp[0] = atlas_rect.pos.x; - ubo_data.light_clamp[1] = atlas_rect.pos.y; + ubo_data.light_clamp[0] = atlas_rect.position.x; + ubo_data.light_clamp[1] = atlas_rect.position.y; ubo_data.light_clamp[2] = atlas_rect.size.x; ubo_data.light_clamp[3] = atlas_rect.size.y; } @@ -2579,8 +2711,8 @@ void RasterizerSceneGLES3::_setup_lights(RID *p_light_cull_result, int p_light_c Rect2 rect(float(x) / atlas_size, float(y) / atlas_size, float(width) / atlas_size, float(height) / atlas_size); ubo_data.light_params[3] = 1.0; //means it has shadow - ubo_data.light_clamp[0] = rect.pos.x; - ubo_data.light_clamp[1] = rect.pos.y; + ubo_data.light_clamp[0] = rect.position.x; + ubo_data.light_clamp[1] = rect.position.y; ubo_data.light_clamp[2] = rect.size.x; ubo_data.light_clamp[3] = rect.size.y; @@ -2797,6 +2929,7 @@ void RasterizerSceneGLES3::_fill_render_list(InstanceBase **p_cull_result, int p current_geometry_index = 0; current_material_index = 0; state.used_sss = false; + state.used_screen_texture = false; //fill list @@ -2874,6 +3007,39 @@ void RasterizerSceneGLES3::_fill_render_list(InstanceBase **p_cull_result, int p } } +void RasterizerSceneGLES3::_blur_effect_buffer() { + + //blur diffuse into effect mipmaps using separatable convolution + //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true); + for (int i = 0; i < storage->frame.current_rt->effects.mip_maps[1].sizes.size(); i++) { + + int vp_w = storage->frame.current_rt->effects.mip_maps[1].sizes[i].width; + int vp_h = storage->frame.current_rt->effects.mip_maps[1].sizes[i].height; + glViewport(0, 0, vp_w, vp_h); + //horizontal pass + state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, true); + state.effect_blur_shader.bind(); + state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h)); + state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i)); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //previous level, since mipmaps[0] starts one level bigger + glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[i].fbo); + _copy_screen(); + state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, false); + + //vertical pass + state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, true); + state.effect_blur_shader.bind(); + state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h)); + state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i)); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color); + glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger + _copy_screen(); + state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, false); + } +} + void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_cam_projection) { glDepthMask(GL_FALSE); @@ -3028,8 +3194,8 @@ void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_ //copy normal and roughness to effect buffer glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo); glReadBuffer(GL_COLOR_ATTACHMENT3); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->buffers.effect_fbo); - glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT, GL_NEAREST); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->effects.ssao.blur_fbo[0]); + glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT, GL_LINEAR); state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_11_SAMPLES, subsurface_scatter_quality == SSS_QUALITY_LOW); state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_17_SAMPLES, subsurface_scatter_quality == SSS_QUALITY_MEDIUM); @@ -3045,8 +3211,11 @@ void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_ glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //disable filter (fixes bugs on AMD) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.effect); + glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.blur_red[0]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); @@ -3056,10 +3225,15 @@ void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_ _copy_screen(); glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color); state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::DIR, Vector2(0, 1)); glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); // copy to base level _copy_screen(); + + glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //restore filter + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); } if (env->ssr_enabled) { @@ -3072,33 +3246,7 @@ void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_ //blur diffuse into effect mipmaps using separatable convolution //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true); - for (int i = 0; i < storage->frame.current_rt->effects.mip_maps[1].sizes.size(); i++) { - - int vp_w = storage->frame.current_rt->effects.mip_maps[1].sizes[i].width; - int vp_h = storage->frame.current_rt->effects.mip_maps[1].sizes[i].height; - glViewport(0, 0, vp_w, vp_h); - //horizontal pass - state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, true); - state.effect_blur_shader.bind(); - state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h)); - state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i)); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //previous level, since mipmaps[0] starts one level bigger - glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[i].fbo); - _copy_screen(); - state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, false); - - //vertical pass - state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, true); - state.effect_blur_shader.bind(); - state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h)); - state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i)); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color); - glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger - _copy_screen(); - state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, false); - } + _blur_effect_buffer(); //perform SSR @@ -3151,6 +3299,8 @@ void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_ //copy reflection over diffuse, resolving SSR if needed state.resolve_shader.set_conditional(ResolveShaderGLES3::USE_SSR, env->ssr_enabled); state.resolve_shader.bind(); + state.resolve_shader.set_uniform(ResolveShaderGLES3::PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height)); + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color); if (env->ssr_enabled) { @@ -3167,6 +3317,13 @@ void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_ glDisable(GL_BLEND); //end additive + if (state.used_screen_texture) { + _blur_effect_buffer(); + //restored framebuffer + glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); + glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height); + } + state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::SIMPLE_COPY, true); state.effect_blur_shader.bind(); state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(0)); @@ -3209,19 +3366,21 @@ void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - if (!env) { - //no environment, simply return and convert to SRGB + if (!env || storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + //no environment or transparent render, simply return and convert to SRGB glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, true); - storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, true); + storage->shaders.copy.set_conditional(CopyShaderGLES3::V_FLIP, storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]); + storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]); storage->shaders.copy.bind(); _copy_screen(); storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, false); storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false); //compute luminance + storage->shaders.copy.set_conditional(CopyShaderGLES3::V_FLIP, false); return; } @@ -3584,6 +3743,18 @@ void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); } + if (env->adjustments_enabled) { + + state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_BCS, true); + RasterizerStorageGLES3::Texture *tex = storage->texture_owner.getornull(env->color_correction); + if (tex) { + state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_COLOR_CORRECTION, true); + glActiveTexture(GL_TEXTURE3); + glBindTexture(tex->target, tex->tex_id); + } + } + + state.tonemap_shader.set_conditional(TonemapShaderGLES3::V_FLIP, storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]); state.tonemap_shader.bind(); state.tonemap_shader.set_uniform(TonemapShaderGLES3::EXPOSURE, env->tone_mapper_exposure); @@ -3606,6 +3777,11 @@ void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p state.tonemap_shader.set_uniform(TonemapShaderGLES3::AUTO_EXPOSURE_GREY, env->auto_exposure_grey); } + if (env->adjustments_enabled) { + + state.tonemap_shader.set_uniform(TonemapShaderGLES3::BCS, Vector3(env->adjustments_brightness, env->adjustments_contrast, env->adjustments_saturation)); + } + _copy_screen(); //turn off everything used @@ -3624,6 +3800,9 @@ void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SCREEN, false); state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SOFTLIGHT, false); state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_FILTER_BICUBIC, false); + state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_BCS, false); + state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_COLOR_CORRECTION, false); + state.tonemap_shader.set_conditional(TonemapShaderGLES3::V_FLIP, false); } void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID p_environment, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) { @@ -3633,12 +3812,14 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const //fill up ubo + storage->info.render.object_count += p_cull_count; + Environment *env = environment_owner.getornull(p_environment); ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas); ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_reflection_atlas); if (shadow_atlas && shadow_atlas->size) { - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 5); glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS); @@ -3647,7 +3828,7 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const } if (reflection_atlas && reflection_atlas->size) { - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 5); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3); glBindTexture(GL_TEXTURE_2D, reflection_atlas->color); } @@ -3659,8 +3840,8 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const state.ubo_data.subsurface_scatter_width = subsurface_scatter_size; - state.ubo_data.shadow_z_offset = 0; - state.ubo_data.shadow_slope_scale = 0; + state.ubo_data.z_offset = 0; + state.ubo_data.z_slope_scale = 0; state.ubo_data.shadow_dual_paraboloid_render_side = 0; state.ubo_data.shadow_dual_paraboloid_render_zfar = 0; @@ -3677,7 +3858,7 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const state.used_contact_shadows = true; - if (storage->frame.current_rt && true) { //detect with state.used_contact_shadows too + if (storage->frame.current_rt && state.debug_draw != VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) { //detect with state.used_contact_shadows too //pre z pass glDisable(GL_BLEND); @@ -3711,7 +3892,7 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); //bind depth for read - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 9); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 8); glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth); } @@ -3764,7 +3945,11 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const } else { - use_mrt = state.used_sss || (env && (env->ssao_enabled || env->ssr_enabled)); //only enable MRT rendering if any of these is enabled + use_mrt = env && (state.used_screen_texture || state.used_sss || env->ssao_enabled || env->ssr_enabled); //only enable MRT rendering if any of these is enabled + //effects disabled and transparency also prevent using MRTs + use_mrt = use_mrt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]; + use_mrt = use_mrt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS]; + use_mrt = use_mrt && state.debug_draw != VS::VIEWPORT_DEBUG_DRAW_OVERDRAW; glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height); @@ -3813,7 +3998,12 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const RasterizerStorageGLES3::Sky *sky = NULL; GLuint env_radiance_tex = 0; - if (!env || env->bg_mode == VS::ENV_BG_CLEAR_COLOR) { + if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) { + clear_color = Color(0, 0, 0, 0); + storage->frame.clear_request = false; + } else if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + clear_color = Color(0, 0, 0, 0); + } else if (!env || env->bg_mode == VS::ENV_BG_CLEAR_COLOR) { if (storage->frame.clear_request) { @@ -3846,20 +4036,14 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); } else { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_BLEND); } - glDisable(GL_BLEND); - render_list.sort_by_key(false); - if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { - glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - } else { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - } - if (state.directional_light_count == 0) { directional_light = NULL; _render_list(render_list.elements, render_list.element_count, p_cam_transform, p_cam_projection, env_radiance_tex, false, false, false, false, shadow_atlas != NULL); @@ -3881,14 +4065,14 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const glDrawBuffers(1, &gldb); } - if (env && env->bg_mode == VS::ENV_BG_SKY) { + if (env && env->bg_mode == VS::ENV_BG_SKY && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT] && state.debug_draw != VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) { /* if (use_mrt) { glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.fbo); //switch to alpha fbo for sky, only diffuse/ambient matters */ - _draw_sky(sky, p_cam_projection, p_cam_transform, storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP], env->sky_scale, env->bg_energy); + _draw_sky(sky, p_cam_projection, p_cam_transform, false, env->sky_scale, env->bg_energy); } //_render_list_forward(&alpha_render_list,camera_transform,camera_transform_inverse,camera_projection,false,fragment_lighting,true); @@ -3900,6 +4084,11 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const _render_mrts(env, p_cam_projection); } + if (state.used_screen_texture) { + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 7); + glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); + } + glEnable(GL_BLEND); glDepthMask(GL_TRUE); glEnable(GL_DEPTH_TEST); @@ -4157,15 +4346,15 @@ void RasterizerSceneGLES3::render_shadow(RID p_light, RID p_shadow_atlas, int p_ } else if (directional_shadow.light_count == 2) { light_instance->directional_rect = Rect2(0, 0, directional_shadow.size, directional_shadow.size / 2); if (light_instance->light_directional_index == 1) { - light_instance->directional_rect.pos.x += light_instance->directional_rect.size.x; + light_instance->directional_rect.position.x += light_instance->directional_rect.size.x; } } else { //3 and 4 light_instance->directional_rect = Rect2(0, 0, directional_shadow.size / 2, directional_shadow.size / 2); if (light_instance->light_directional_index & 1) { - light_instance->directional_rect.pos.x += light_instance->directional_rect.size.x; + light_instance->directional_rect.position.x += light_instance->directional_rect.size.x; } if (light_instance->light_directional_index / 2) { - light_instance->directional_rect.pos.y += light_instance->directional_rect.size.y; + light_instance->directional_rect.position.y += light_instance->directional_rect.size.y; } } } @@ -4173,8 +4362,8 @@ void RasterizerSceneGLES3::render_shadow(RID p_light, RID p_shadow_atlas, int p_ light_projection = light_instance->shadow_transform[p_pass].camera; light_transform = light_instance->shadow_transform[p_pass].transform; - x = light_instance->directional_rect.pos.x; - y = light_instance->directional_rect.pos.y; + x = light_instance->directional_rect.position.x; + y = light_instance->directional_rect.position.y; width = light_instance->directional_rect.size.x; height = light_instance->directional_rect.size.y; @@ -4325,8 +4514,8 @@ void RasterizerSceneGLES3::render_shadow(RID p_light, RID p_shadow_atlas, int p_ glClear(GL_DEPTH_BUFFER_BIT); glDisable(GL_SCISSOR_TEST); - state.ubo_data.shadow_z_offset = bias; - state.ubo_data.shadow_slope_scale = normal_bias; + state.ubo_data.z_offset = bias; + state.ubo_data.z_slope_scale = normal_bias; state.ubo_data.shadow_dual_paraboloid_render_side = dp_direction; state.ubo_data.shadow_dual_paraboloid_render_zfar = zfar; @@ -4438,136 +4627,39 @@ bool RasterizerSceneGLES3::free(RID p_rid) { return true; } -// http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html -static _FORCE_INLINE_ float radicalInverse_VdC(uint32_t bits) { - bits = (bits << 16u) | (bits >> 16u); - bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); - bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); - bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); - bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); - return float(bits) * 2.3283064365386963e-10f; // / 0x100000000 -} - -static _FORCE_INLINE_ Vector2 Hammersley(uint32_t i, uint32_t N) { - return Vector2(float(i) / float(N), radicalInverse_VdC(i)); -} - -static _FORCE_INLINE_ Vector3 ImportanceSampleGGX(Vector2 Xi, float Roughness, Vector3 N) { - float a = Roughness * Roughness; // DISNEY'S ROUGHNESS [see Burley'12 siggraph] - - // Compute distribution direction - float Phi = 2.0f * Math_PI * Xi.x; - float CosTheta = Math::sqrt((float)(1.0f - Xi.y) / (1.0f + (a * a - 1.0f) * Xi.y)); - float SinTheta = Math::sqrt((float)Math::abs(1.0f - CosTheta * CosTheta)); - - // Convert to spherical direction - Vector3 H; - H.x = SinTheta * Math::cos(Phi); - H.y = SinTheta * Math::sin(Phi); - H.z = CosTheta; - - Vector3 UpVector = Math::abs(N.z) < 0.999 ? Vector3(0.0, 0.0, 1.0) : Vector3(1.0, 0.0, 0.0); - Vector3 TangentX = UpVector.cross(N); - TangentX.normalize(); - Vector3 TangentY = N.cross(TangentX); - - // Tangent to world space - return TangentX * H.x + TangentY * H.y + N * H.z; -} - -static _FORCE_INLINE_ float GGX(float NdotV, float a) { - float k = a / 2.0; - return NdotV / (NdotV * (1.0 - k) + k); -} +void RasterizerSceneGLES3::set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw) { -// http://graphicrants.blogspot.com.au/2013/08/specular-brdf-reference.html -float _FORCE_INLINE_ G_Smith(float a, float nDotV, float nDotL) { - return GGX(nDotL, a * a) * GGX(nDotV, a * a); + state.debug_draw = p_debug_draw; } -void RasterizerSceneGLES3::_generate_brdf() { - - int brdf_size = GLOBAL_DEF("rendering/gles3/brdf_texture_size", 64); - - PoolVector<uint8_t> brdf; - brdf.resize(brdf_size * brdf_size * 2); - - PoolVector<uint8_t>::Write w = brdf.write(); - - for (int i = 0; i < brdf_size; i++) { - for (int j = 0; j < brdf_size; j++) { - - float Roughness = float(j) / (brdf_size - 1); - float NoV = float(i + 1) / (brdf_size); //avoid storing nov0 - - Vector3 V; - V.x = Math::sqrt(1.0f - NoV * NoV); - V.y = 0.0; - V.z = NoV; - - Vector3 N = Vector3(0.0, 0.0, 1.0); - - float A = 0; - float B = 0; - - for (int s = 0; s < 512; s++) { - - Vector2 xi = Hammersley(s, 512); - Vector3 H = ImportanceSampleGGX(xi, Roughness, N); - Vector3 L = 2.0 * V.dot(H) * H - V; +void RasterizerSceneGLES3::initialize() { - float NoL = CLAMP(L.z, 0.0, 1.0); - float NoH = CLAMP(H.z, 0.0, 1.0); - float VoH = CLAMP(V.dot(H), 0.0, 1.0); + render_pass = 0; - if (NoL > 0.0) { - float G = G_Smith(Roughness, NoV, NoL); - float G_Vis = G * VoH / (NoH * NoV); - float Fc = pow(1.0 - VoH, 5.0); + state.scene_shader.init(); - A += (1.0 - Fc) * G_Vis; - B += Fc * G_Vis; - } - } + { + //default material and shader - A /= 512.0; - B /= 512.0; + default_shader = storage->shader_create(); + storage->shader_set_code(default_shader, "shader_type spatial;\n"); + default_material = storage->material_create(); + storage->material_set_shader(default_material, default_shader); - int tofs = ((brdf_size - j - 1) * brdf_size + i) * 2; - w[tofs + 0] = CLAMP(A * 255, 0, 255); - w[tofs + 1] = CLAMP(B * 255, 0, 255); - } + default_shader_twosided = storage->shader_create(); + default_material_twosided = storage->material_create(); + storage->shader_set_code(default_shader_twosided, "shader_type spatial; render_mode cull_disabled;\n"); + storage->material_set_shader(default_material_twosided, default_shader_twosided); } - //set up brdf texture - - glGenTextures(1, &state.brdf_texture); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, state.brdf_texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, brdf_size, brdf_size, 0, GL_RG, GL_UNSIGNED_BYTE, w.ptr()); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glBindTexture(GL_TEXTURE_2D, 0); -} - -void RasterizerSceneGLES3::initialize() { - - render_pass = 0; - - state.scene_shader.init(); - - default_shader = storage->shader_create(); - storage->shader_set_code(default_shader, "shader_type spatial;\n"); - default_material = storage->material_create(); - storage->material_set_shader(default_material, default_shader); + { + //default material and shader - default_shader_twosided = storage->shader_create(); - default_material_twosided = storage->material_create(); - storage->shader_set_code(default_shader_twosided, "shader_type spatial; render_mode cull_disabled;\n"); - storage->material_set_shader(default_material_twosided, default_shader_twosided); + default_overdraw_shader = storage->shader_create(); + storage->shader_set_code(default_overdraw_shader, "shader_type spatial;\nrender_mode blend_add,unshaded;\n void fragment() { ALBEDO=vec3(0.4,0.8,0.8); ALPHA=0.2; }"); + default_overdraw_material = storage->material_create(); + storage->material_set_shader(default_overdraw_material, default_overdraw_shader); + } glGenBuffers(1, &state.scene_ubo); glBindBuffer(GL_UNIFORM_BUFFER, state.scene_ubo); @@ -4605,7 +4697,6 @@ void RasterizerSceneGLES3::initialize() { } render_list.init(); state.cube_to_dp_shader.init(); - _generate_brdf(); shadow_atlas_realloc_tolerance_msec = 500; @@ -4852,6 +4943,8 @@ void RasterizerSceneGLES3::initialize() { GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE); } + + state.debug_draw = VS::VIEWPORT_DEBUG_DRAW_DISABLED; } void RasterizerSceneGLES3::iteration() { diff --git a/drivers/gles3/rasterizer_scene_gles3.h b/drivers/gles3/rasterizer_scene_gles3.h index 322343bc89..f9953f83db 100644 --- a/drivers/gles3/rasterizer_scene_gles3.h +++ b/drivers/gles3/rasterizer_scene_gles3.h @@ -77,6 +77,9 @@ public: RID default_shader; RID default_shader_twosided; + RID default_overdraw_material; + RID default_overdraw_shader; + RasterizerStorageGLES3 *storage; Vector<RasterizerStorageGLES3::RenderTarget::Exposure> exposure_shrink; @@ -111,19 +114,34 @@ public: float time[4]; float ambient_light_color[4]; float bg_color[4]; + float fog_color_enabled[4]; + float fog_sun_color_amount[4]; + float ambient_energy; float bg_energy; - float shadow_z_offset; - float shadow_slope_scale; + float z_offset; + float z_slope_scale; float shadow_dual_paraboloid_render_zfar; float shadow_dual_paraboloid_render_side; float screen_pixel_size[2]; float shadow_atlas_pixel_size[2]; float shadow_directional_pixel_size[2]; + + float z_far; float reflection_multiplier; float subsurface_scatter_width; float ambient_occlusion_affect_light; + bool fog_depth_enabled; + float fog_depth_begin; + float fog_depth_curve; + bool fog_transmit_enabled; + float fog_transmit_curve; + bool fog_height_enabled; + float fog_height_min; + float fog_height_max; + float fog_height_curve; + } ubo_data; GLuint scene_ubo; @@ -137,8 +155,6 @@ public: GLuint env_radiance_ubo; - GLuint brdf_texture; - GLuint sky_verts; GLuint sky_array; @@ -170,7 +186,9 @@ public: bool cull_front; bool used_sss; + bool used_screen_texture; + VS::ViewportDebugDraw debug_draw; } state; /* SHADOW ATLAS API */ @@ -389,6 +407,27 @@ public: float dof_blur_near_amount; VS::EnvironmentDOFBlurQuality dof_blur_near_quality; + bool adjustments_enabled; + float adjustments_brightness; + float adjustments_contrast; + float adjustments_saturation; + RID color_correction; + + bool fog_enabled; + Color fog_color; + Color fog_sun_color; + float fog_sun_amount; + + bool fog_depth_enabled; + float fog_depth_begin; + float fog_depth_curve; + bool fog_transmit_enabled; + float fog_transmit_curve; + bool fog_height_enabled; + float fog_height_min; + float fog_height_max; + float fog_height_curve; + Environment() { bg_mode = VS::ENV_BG_CLEAR_COLOR; sky_scale = 1.0; @@ -445,6 +484,29 @@ public: dof_blur_near_transition = 1; dof_blur_near_amount = 0.1; dof_blur_near_quality = VS::ENV_DOF_BLUR_QUALITY_MEDIUM; + + adjustments_enabled = false; + adjustments_brightness = 1.0; + adjustments_contrast = 1.0; + adjustments_saturation = 1.0; + + fog_enabled = false; + fog_color = Color(0.5, 0.5, 0.5); + fog_sun_color = Color(0.8, 0.8, 0.0); + fog_sun_amount = 0; + + fog_depth_enabled = true; + + fog_depth_begin = 10; + fog_depth_curve = 1; + + fog_transmit_enabled = true; + fog_transmit_curve = 1; + + fog_height_enabled = false; + fog_height_min = 0; + fog_height_max = 100; + fog_height_curve = 1; } }; @@ -472,6 +534,10 @@ public: virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp); + virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount); + virtual void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_curve, bool p_transmit, float p_transmit_curve); + virtual void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve); + /* LIGHT INSTANCE */ struct LightDataUBO { @@ -711,6 +777,7 @@ public: void _fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_shadow); + void _blur_effect_buffer(); void _render_mrts(Environment *env, const CameraMatrix &p_cam_projection); void _post_process(Environment *env, const CameraMatrix &p_cam_projection); @@ -718,9 +785,8 @@ public: virtual void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count); virtual bool free(RID p_rid); - void _generate_brdf(); - virtual void set_scene_pass(uint64_t p_pass); + virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw); void iteration(); void initialize(); diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp index a3f1873eae..cf540c1577 100644 --- a/drivers/gles3/rasterizer_storage_gles3.cpp +++ b/drivers/gles3/rasterizer_storage_gles3.cpp @@ -782,7 +782,6 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p int bh = h; glCompressedTexImage2D(blit_target, i, internal_format, bw, bh, 0, size, &read[ofs]); - print_line("format: " + Image::get_format_name(texture->format) + " size: " + Vector2(bw, bh) + " block: " + itos(block)); } else { glPixelStorei(GL_UNPACK_ALIGNMENT, 1); @@ -825,8 +824,7 @@ Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, VS::CubeMapSi ERR_FAIL_COND_V(!texture, Ref<Image>()); ERR_FAIL_COND_V(!texture->active, Ref<Image>()); - ERR_FAIL_COND_V(texture->data_size == 0, Ref<Image>()); - ERR_FAIL_COND_V(texture->render_target, Ref<Image>()); + ERR_FAIL_COND_V(texture->data_size == 0 && !texture->render_target, Ref<Image>()); if (!texture->images[p_cube_side].is_null()) { return texture->images[p_cube_side]; @@ -1427,6 +1425,7 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const { _shader_dirty_list.remove(&p_shader->dirty_list); p_shader->valid = false; + p_shader->ubo_size = 0; p_shader->uniforms.clear(); @@ -1463,6 +1462,7 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const { p_shader->spatial.unshaded = false; p_shader->spatial.ontop = false; p_shader->spatial.uses_sss = false; + p_shader->spatial.uses_screen_texture = false; p_shader->spatial.uses_vertex = false; p_shader->spatial.writes_modelview_or_projection = false; @@ -1488,6 +1488,7 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const { shaders.actions_scene.usage_flag_pointers["SSS_STRENGTH"] = &p_shader->spatial.uses_sss; shaders.actions_scene.usage_flag_pointers["DISCARD"] = &p_shader->spatial.uses_discard; + shaders.actions_scene.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->spatial.uses_screen_texture; shaders.actions_scene.write_flag_pointers["MODELVIEW_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection; shaders.actions_scene.write_flag_pointers["PROJECTION_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection; @@ -1961,8 +1962,8 @@ _FORCE_INLINE_ static void _fill_std140_variant_ubo_value(ShaderLanguage::DataTy } else if (value.get_type() == Variant::RECT2) { Rect2 v = value; - gui[0] = v.pos.x; - gui[1] = v.pos.y; + gui[0] = v.position.x; + gui[1] = v.position.y; gui[2] = v.size.x; gui[3] = v.size.y; } else if (value.get_type() == Variant::QUAT) { @@ -2244,6 +2245,10 @@ void RasterizerStorageGLES3::_update_material(Material *material) { if (material->shader && material->shader->dirty_list.in_list()) { _update_shader(material->shader); } + + if (material->shader && !material->shader->valid) + return; + //update caches { @@ -2658,6 +2663,7 @@ void RasterizerStorageGLES3::mesh_add_surface(RID p_mesh, uint32_t p_format, VS: surface->skeleton_bone_used.resize(surface->skeleton_bone_aabb.size()); surface->aabb = p_aabb; surface->max_bone = p_bone_aabbs.size(); + surface->total_data_size += surface->array_byte_size + surface->index_array_byte_size; for (int i = 0; i < surface->skeleton_bone_used.size(); i++) { if (surface->skeleton_bone_aabb[i].size.x < 0 || surface->skeleton_bone_aabb[i].size.y < 0 || surface->skeleton_bone_aabb[i].size.z < 0) { @@ -2727,6 +2733,112 @@ void RasterizerStorageGLES3::mesh_add_surface(RID p_mesh, uint32_t p_format, VS: glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } + +#ifdef DEBUG_ENABLED + + if (config.generate_wireframes && p_primitive == VS::PRIMITIVE_TRIANGLES) { + //generate wireframes, this is used mostly by editor + PoolVector<uint32_t> wf_indices; + int index_count; + + if (p_format & VS::ARRAY_FORMAT_INDEX) { + + index_count = p_index_count * 2; + wf_indices.resize(index_count); + + PoolVector<uint8_t>::Read ir = p_index_array.read(); + PoolVector<uint32_t>::Write wr = wf_indices.write(); + + if (p_vertex_count < (1 << 16)) { + //read 16 bit indices + const uint16_t *src_idx = (const uint16_t *)ir.ptr(); + for (int i = 0; i < index_count; i += 6) { + + wr[i + 0] = src_idx[i / 2]; + wr[i + 1] = src_idx[i / 2 + 1]; + wr[i + 2] = src_idx[i / 2 + 1]; + wr[i + 3] = src_idx[i / 2 + 2]; + wr[i + 4] = src_idx[i / 2 + 2]; + wr[i + 5] = src_idx[i / 2]; + } + + } else { + + //read 16 bit indices + const uint32_t *src_idx = (const uint32_t *)ir.ptr(); + for (int i = 0; i < index_count; i += 6) { + + wr[i + 0] = src_idx[i / 2]; + wr[i + 1] = src_idx[i / 2 + 1]; + wr[i + 2] = src_idx[i / 2 + 1]; + wr[i + 3] = src_idx[i / 2 + 2]; + wr[i + 4] = src_idx[i / 2 + 2]; + wr[i + 5] = src_idx[i / 2]; + } + } + + } else { + + index_count = p_vertex_count * 2; + wf_indices.resize(index_count); + PoolVector<uint32_t>::Write wr = wf_indices.write(); + for (int i = 0; i < index_count; i += 6) { + + wr[i + 0] = i / 2; + wr[i + 1] = i / 2 + 1; + wr[i + 2] = i / 2 + 1; + wr[i + 3] = i / 2 + 2; + wr[i + 4] = i / 2 + 2; + wr[i + 5] = i / 2; + } + } + { + PoolVector<uint32_t>::Read ir = wf_indices.read(); + + glGenBuffers(1, &surface->index_wireframe_id); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_wireframe_id); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_count * sizeof(uint32_t), ir.ptr(), GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind + + surface->index_wireframe_len = index_count; + } + + for (int ai = 0; ai < 2; ai++) { + + if (ai == 0) { + //for normal draw + glGenVertexArrays(1, &surface->array_wireframe_id); + glBindVertexArray(surface->array_wireframe_id); + glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id); + } else if (ai == 1) { + //for instancing draw (can be changed and no one cares) + glGenVertexArrays(1, &surface->instancing_array_wireframe_id); + glBindVertexArray(surface->instancing_array_wireframe_id); + glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id); + } + + for (int i = 0; i < VS::ARRAY_MAX - 1; i++) { + + if (!attribs[i].enabled) + continue; + + if (attribs[i].integer) { + glVertexAttribIPointer(attribs[i].index, attribs[i].size, attribs[i].type, attribs[i].stride, ((uint8_t *)0) + attribs[i].offset); + } else { + glVertexAttribPointer(attribs[i].index, attribs[i].size, attribs[i].type, attribs[i].normalized, attribs[i].stride, ((uint8_t *)0) + attribs[i].offset); + } + glEnableVertexAttribArray(attribs[i].index); + } + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_wireframe_id); + + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + } + +#endif } { @@ -2739,6 +2851,8 @@ void RasterizerStorageGLES3::mesh_add_surface(RID p_mesh, uint32_t p_format, VS: PoolVector<uint8_t>::Read vr = p_blend_shapes[i].read(); + surface->total_data_size += array_size; + glGenBuffers(1, &mt.vertex_id); glBindBuffer(GL_ARRAY_BUFFER, mt.vertex_id); glBufferData(GL_ARRAY_BUFFER, array_size, vr.ptr(), GL_STATIC_DRAW); @@ -2770,6 +2884,8 @@ void RasterizerStorageGLES3::mesh_add_surface(RID p_mesh, uint32_t p_format, VS: mesh->surfaces.push_back(surface); mesh->instance_change_notify(); + + info.vertex_mem += surface->total_data_size; } void RasterizerStorageGLES3::mesh_set_blend_shape_count(RID p_mesh, int p_amount) { @@ -2998,6 +3114,14 @@ void RasterizerStorageGLES3::mesh_remove_surface(RID p_mesh, int p_surface) { glDeleteVertexArrays(1, &surface->blend_shapes[i].array_id); } + if (surface->index_wireframe_id) { + glDeleteBuffers(1, &surface->index_wireframe_id); + glDeleteVertexArrays(1, &surface->array_wireframe_id); + glDeleteVertexArrays(1, &surface->instancing_array_wireframe_id); + } + + info.vertex_mem -= surface->total_data_size; + mesh->instance_material_change_notify(); memdelete(surface); @@ -3830,7 +3954,7 @@ void RasterizerStorageGLES3::immediate_vertex(RID p_immediate, const Vector3 &p_ if (c->vertices.empty() && im->chunks.size() == 1) { - im->aabb.pos = p_vertex; + im->aabb.position = p_vertex; im->aabb.size = Vector3(); } else { im->aabb.expand_to(p_vertex); @@ -4497,7 +4621,7 @@ Rect3 RasterizerStorageGLES3::reflection_probe_get_aabb(RID p_probe) const { ERR_FAIL_COND_V(!reflection_probe, Rect3()); Rect3 aabb; - aabb.pos = -reflection_probe->extents; + aabb.position = -reflection_probe->extents; aabb.size = reflection_probe->extents * 2.0; return aabb; @@ -5090,7 +5214,7 @@ Rect3 RasterizerStorageGLES3::particles_get_current_aabb(RID p_particles) { pos = inv.xform(pos); } if (i == 0) - aabb.pos = pos; + aabb.position = pos; else aabb.expand_to(pos); } @@ -5452,7 +5576,7 @@ void RasterizerStorageGLES3::_render_target_clear(RenderTarget *rt) { glDeleteRenderbuffers(1, &rt->buffers.diffuse); glDeleteRenderbuffers(1, &rt->buffers.specular); glDeleteRenderbuffers(1, &rt->buffers.normal_rough); - glDeleteRenderbuffers(1, &rt->buffers.motion_sss); + glDeleteRenderbuffers(1, &rt->buffers.sss); glDeleteFramebuffers(1, &rt->buffers.effect_fbo); glDeleteTextures(1, &rt->buffers.effect); @@ -5491,6 +5615,7 @@ void RasterizerStorageGLES3::_render_target_clear(RenderTarget *rt) { tex->alloc_width = 0; tex->width = 0; tex->height = 0; + tex->active = false; for (int i = 0; i < 2; i++) { for (int j = 0; j < rt->effects.mip_maps[i].sizes.size(); j++) { @@ -5587,13 +5712,14 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt) { tex->alloc_width = rt->width; tex->height = rt->height; tex->alloc_height = rt->height; + tex->active = true; texture_set_flags(rt->texture, tex->flags); } /* BACK FBO */ - if (config.render_arch == RENDER_ARCH_DESKTOP && !rt->flags[RENDER_TARGET_NO_3D]) { + if (!rt->flags[RENDER_TARGET_NO_3D]) { static const int msaa_value[] = { 0, 2, 4, 8, 16 }; int msaa = msaa_value[rt->msaa]; @@ -5621,75 +5747,155 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt) { glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rt->buffers.diffuse); - glGenRenderbuffers(1, &rt->buffers.specular); - glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.specular); + if (!rt->flags[RENDER_TARGET_NO_3D_EFFECTS]) { - if (msaa == 0) - glRenderbufferStorage(GL_RENDERBUFFER, color_internal_format, rt->width, rt->height); - else - glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, color_internal_format, rt->width, rt->height); + glGenRenderbuffers(1, &rt->buffers.specular); + glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.specular); + + if (msaa == 0) + glRenderbufferStorage(GL_RENDERBUFFER, color_internal_format, rt->width, rt->height); + else + glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, color_internal_format, rt->width, rt->height); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, rt->buffers.specular); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, rt->buffers.specular); - glGenRenderbuffers(1, &rt->buffers.normal_rough); - glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.normal_rough); + glGenRenderbuffers(1, &rt->buffers.normal_rough); + glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.normal_rough); - if (msaa == 0) - glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, rt->width, rt->height); - else - glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_RGBA8, rt->width, rt->height); + if (msaa == 0) + glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, rt->width, rt->height); + else + glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_RGBA8, rt->width, rt->height); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_RENDERBUFFER, rt->buffers.normal_rough); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_RENDERBUFFER, rt->buffers.normal_rough); - glGenRenderbuffers(1, &rt->buffers.motion_sss); - glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.motion_sss); + glGenRenderbuffers(1, &rt->buffers.sss); + glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.sss); - if (msaa == 0) - glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, rt->width, rt->height); - else - glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_RGBA8, rt->width, rt->height); + if (msaa == 0) + glRenderbufferStorage(GL_RENDERBUFFER, GL_R8, rt->width, rt->height); + else + glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_R8, rt->width, rt->height); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_RENDERBUFFER, rt->buffers.motion_sss); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_RENDERBUFFER, rt->buffers.sss); - GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo); + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo); - if (status != GL_FRAMEBUFFER_COMPLETE) { - printf("err status: %x\n", status); - _render_target_clear(rt); - ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); - } + if (status != GL_FRAMEBUFFER_COMPLETE) { + printf("err status: %x\n", status); + _render_target_clear(rt); + ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); + } - glBindRenderbuffer(GL_RENDERBUFFER, 0); + glBindRenderbuffer(GL_RENDERBUFFER, 0); - // effect resolver + // effect resolver - glGenFramebuffers(1, &rt->buffers.effect_fbo); - glBindFramebuffer(GL_FRAMEBUFFER, rt->buffers.effect_fbo); + glGenFramebuffers(1, &rt->buffers.effect_fbo); + glBindFramebuffer(GL_FRAMEBUFFER, rt->buffers.effect_fbo); - glGenTextures(1, &rt->buffers.effect); - glBindTexture(GL_TEXTURE_2D, rt->buffers.effect); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, rt->width, rt->height, 0, - GL_RGBA, GL_UNSIGNED_BYTE, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_TEXTURE_2D, rt->buffers.effect, 0); + glGenTextures(1, &rt->buffers.effect); + glBindTexture(GL_TEXTURE_2D, rt->buffers.effect); + glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, rt->width, rt->height, 0, + color_format, color_type, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_TEXTURE_2D, rt->buffers.effect, 0); - if (status != GL_FRAMEBUFFER_COMPLETE) { - printf("err status: %x\n", status); - _render_target_clear(rt); - ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); - } + if (status != GL_FRAMEBUFFER_COMPLETE) { + printf("err status: %x\n", status); + _render_target_clear(rt); + ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); + } - glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo); + glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo); - if (status != GL_FRAMEBUFFER_COMPLETE) { - _render_target_clear(rt); - ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); + if (status != GL_FRAMEBUFFER_COMPLETE) { + _render_target_clear(rt); + ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); + } + + ///////////////// ssao + + //AO strength textures + for (int i = 0; i < 2; i++) { + + glGenFramebuffers(1, &rt->effects.ssao.blur_fbo[i]); + glBindFramebuffer(GL_FRAMEBUFFER, rt->effects.ssao.blur_fbo[i]); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, + GL_TEXTURE_2D, rt->depth, 0); + + glGenTextures(1, &rt->effects.ssao.blur_red[i]); + glBindTexture(GL_TEXTURE_2D, rt->effects.ssao.blur_red[i]); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, rt->width, rt->height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->effects.ssao.blur_red[i], 0); + + status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if (status != GL_FRAMEBUFFER_COMPLETE) { + _render_target_clear(rt); + ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); + } + } + //5 mip levels for depth texture, but base is read separately + + glGenTextures(1, &rt->effects.ssao.linear_depth); + glBindTexture(GL_TEXTURE_2D, rt->effects.ssao.linear_depth); + + int ssao_w = rt->width / 2; + int ssao_h = rt->height / 2; + + for (int i = 0; i < 4; i++) { //5, but 4 mips, base is read directly to save bw + + glTexImage2D(GL_TEXTURE_2D, i, GL_R16UI, ssao_w, ssao_h, 0, GL_RED_INTEGER, GL_UNSIGNED_SHORT, NULL); + ssao_w >>= 1; + ssao_h >>= 1; + } + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3); + + for (int i = 0; i < 4; i++) { //5, but 4 mips, base is read directly to save bw + + GLuint fbo; + glGenFramebuffers(1, &fbo); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->effects.ssao.linear_depth, i); + rt->effects.ssao.depth_mipmap_fbos.push_back(fbo); + } + + //////Exposure + + glGenFramebuffers(1, &rt->exposure.fbo); + glBindFramebuffer(GL_FRAMEBUFFER, rt->exposure.fbo); + + glGenTextures(1, &rt->exposure.color); + glBindTexture(GL_TEXTURE_2D, rt->exposure.color); + glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 1, 1, 0, GL_RED, GL_FLOAT, NULL); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->exposure.color, 0); + + status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if (status != GL_FRAMEBUFFER_COMPLETE) { + _render_target_clear(rt); + ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); + } } + } + + if (!rt->flags[RENDER_TARGET_NO_SAMPLING]) { for (int i = 0; i < 2; i++) { @@ -5736,7 +5942,7 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt) { glBindFramebuffer(GL_FRAMEBUFFER, mm.fbo); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->effects.mip_maps[i].color, j); - status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (status != GL_FRAMEBUFFER_COMPLETE) { _render_target_clear(rt); ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); @@ -5756,79 +5962,6 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt) { glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } - ///////////////// ssao - - //AO strength textures - for (int i = 0; i < 2; i++) { - - glGenFramebuffers(1, &rt->effects.ssao.blur_fbo[i]); - glBindFramebuffer(GL_FRAMEBUFFER, rt->effects.ssao.blur_fbo[i]); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, - GL_TEXTURE_2D, rt->depth, 0); - - glGenTextures(1, &rt->effects.ssao.blur_red[i]); - glBindTexture(GL_TEXTURE_2D, rt->effects.ssao.blur_red[i]); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, rt->width, rt->height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->effects.ssao.blur_red[i], 0); - - status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - if (status != GL_FRAMEBUFFER_COMPLETE) { - _render_target_clear(rt); - ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); - } - } - //5 mip levels for depth texture, but base is read separately - - glGenTextures(1, &rt->effects.ssao.linear_depth); - glBindTexture(GL_TEXTURE_2D, rt->effects.ssao.linear_depth); - - int ssao_w = rt->width / 2; - int ssao_h = rt->height / 2; - - for (int i = 0; i < 4; i++) { //5, but 4 mips, base is read directly to save bw - - glTexImage2D(GL_TEXTURE_2D, i, GL_R16UI, ssao_w, ssao_h, 0, GL_RED_INTEGER, GL_UNSIGNED_SHORT, NULL); - ssao_w >>= 1; - ssao_h >>= 1; - } - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3); - - for (int i = 0; i < 4; i++) { //5, but 4 mips, base is read directly to save bw - - GLuint fbo; - glGenFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->effects.ssao.linear_depth, i); - rt->effects.ssao.depth_mipmap_fbos.push_back(fbo); - } - - //////Exposure - - glGenFramebuffers(1, &rt->exposure.fbo); - glBindFramebuffer(GL_FRAMEBUFFER, rt->exposure.fbo); - - glGenTextures(1, &rt->exposure.color); - glBindTexture(GL_TEXTURE_2D, rt->exposure.color); - glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 1, 1, 0, GL_RED, GL_FLOAT, NULL); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->exposure.color, 0); - - status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - if (status != GL_FRAMEBUFFER_COMPLETE) { - _render_target_clear(rt); - ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); - } } } @@ -5853,9 +5986,10 @@ RID RasterizerStorageGLES3::render_target_create() { t->srgb = false; t->total_data_size = 0; t->ignore_mipmaps = false; - t->mipmaps = 0; + t->mipmaps = 1; t->active = true; t->tex_id = 0; + t->render_target = rt; rt->texture = texture_owner.make_rid(t); @@ -5892,8 +6026,10 @@ void RasterizerStorageGLES3::render_target_set_flag(RID p_render_target, RenderT rt->flags[p_flag] = p_value; switch (p_flag) { + case RENDER_TARGET_HDR: case RENDER_TARGET_NO_3D: - case RENDER_TARGET_TRANSPARENT: { + case RENDER_TARGET_NO_SAMPLING: + case RENDER_TARGET_NO_3D_EFFECTS: { //must reset for these formats _render_target_clear(rt); _render_target_allocate(rt); @@ -5974,6 +6110,7 @@ RID RasterizerStorageGLES3::canvas_light_occluder_create() { co->index_id = 0; co->vertex_id = 0; co->len = 0; + glGenVertexArrays(1, &co->array_id); return canvas_occluder_owner.make_rid(co); } @@ -6045,7 +6182,7 @@ void RasterizerStorageGLES3::canvas_light_occluder_set_polylines(RID p_occluder, if (!co->vertex_id) { glGenBuffers(1, &co->vertex_id); glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id); - glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(real_t), vw.ptr(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(real_t), vw.ptr(), GL_DYNAMIC_DRAW); } else { glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id); @@ -6058,7 +6195,7 @@ void RasterizerStorageGLES3::canvas_light_occluder_set_polylines(RID p_occluder, glGenBuffers(1, &co->index_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, lc * 3 * sizeof(uint16_t), iw.ptr(), GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, lc * 3 * sizeof(uint16_t), iw.ptr(), GL_DYNAMIC_DRAW); } else { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id); @@ -6068,6 +6205,12 @@ void RasterizerStorageGLES3::canvas_light_occluder_set_polylines(RID p_occluder, glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind co->len = lc; + glBindVertexArray(co->array_id); + glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id); + glEnableVertexAttribArray(VS::ARRAY_VERTEX); + glVertexAttribPointer(VS::ARRAY_VERTEX, 3, GL_FLOAT, false, 0, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id); + glBindVertexArray(0); } } @@ -6296,6 +6439,8 @@ bool RasterizerStorageGLES3::free(RID p_rid) { if (co->vertex_id) glDeleteBuffers(1, &co->vertex_id); + glDeleteVertexArrays(1, &co->array_id); + canvas_occluder_owner.free(p_rid); memdelete(co); @@ -6333,10 +6478,98 @@ bool RasterizerStorageGLES3::has_os_feature(const String &p_feature) const { //////////////////////////////////////////// -void RasterizerStorageGLES3::initialize() { +void RasterizerStorageGLES3::set_debug_generate_wireframes(bool p_generate) { - config.render_arch = RENDER_ARCH_DESKTOP; - //config.fbo_deferred=int(Globals::get_singleton()->get("rendering/gles3/lighting_technique")); + config.generate_wireframes = p_generate; +} + +void RasterizerStorageGLES3::render_info_begin_capture() { + + info.snap = info.render; +} + +void RasterizerStorageGLES3::render_info_end_capture() { + + info.snap.object_count = info.render.object_count - info.snap.object_count; + info.snap.draw_call_count = info.render.draw_call_count - info.snap.draw_call_count; + info.snap.material_switch_count = info.render.material_switch_count - info.snap.material_switch_count; + info.snap.surface_switch_count = info.render.surface_switch_count - info.snap.surface_switch_count; + info.snap.shader_rebind_count = info.render.shader_rebind_count - info.snap.shader_rebind_count; + info.snap.vertices_count = info.render.vertices_count - info.snap.vertices_count; +} + +int RasterizerStorageGLES3::get_captured_render_info(VS::RenderInfo p_info) { + + switch (p_info) { + case VS::INFO_OBJECTS_IN_FRAME: { + + return info.snap.object_count; + } break; + case VS::INFO_VERTICES_IN_FRAME: { + + return info.snap.vertices_count; + } break; + case VS::INFO_MATERIAL_CHANGES_IN_FRAME: { + return info.snap.material_switch_count; + } break; + case VS::INFO_SHADER_CHANGES_IN_FRAME: { + return info.snap.shader_rebind_count; + } break; + case VS::INFO_SURFACE_CHANGES_IN_FRAME: { + return info.snap.surface_switch_count; + } break; + case VS::INFO_DRAW_CALLS_IN_FRAME: { + return info.snap.draw_call_count; + } break; + default: { + return get_render_info(p_info); + } + } +} + +int RasterizerStorageGLES3::get_render_info(VS::RenderInfo p_info) { + + switch (p_info) { + case VS::INFO_OBJECTS_IN_FRAME: { + + return info.render_final.object_count; + } break; + case VS::INFO_VERTICES_IN_FRAME: { + + return info.render_final.vertices_count; + } break; + case VS::INFO_MATERIAL_CHANGES_IN_FRAME: { + return info.render_final.material_switch_count; + } break; + case VS::INFO_SHADER_CHANGES_IN_FRAME: { + return info.render_final.shader_rebind_count; + } break; + case VS::INFO_SURFACE_CHANGES_IN_FRAME: { + return info.render_final.surface_switch_count; + } break; + case VS::INFO_DRAW_CALLS_IN_FRAME: { + return info.render_final.draw_call_count; + } break; + case VS::INFO_USAGE_VIDEO_MEM_TOTAL: { + + return 0; //no idea + } break; + case VS::INFO_VIDEO_MEM_USED: { + + return info.vertex_mem + info.texture_mem; + } break; + case VS::INFO_TEXTURE_MEM_USED: { + + return info.texture_mem; + } break; + case VS::INFO_VERTEX_MEM_USED: { + + return info.vertex_mem; + } break; + } +} + +void RasterizerStorageGLES3::initialize() { RasterizerStorageGLES3::system_fbo = 0; @@ -6517,6 +6750,7 @@ void RasterizerStorageGLES3::initialize() { frame.delta = 0; frame.current_rt = NULL; config.keep_original_textures = false; + config.generate_wireframes = false; } void RasterizerStorageGLES3::finalize() { diff --git a/drivers/gles3/rasterizer_storage_gles3.h b/drivers/gles3/rasterizer_storage_gles3.h index 7e107cfdf4..1f101b8b61 100644 --- a/drivers/gles3/rasterizer_storage_gles3.h +++ b/drivers/gles3/rasterizer_storage_gles3.h @@ -61,8 +61,6 @@ public: struct Config { - RenderArchitecture render_arch; - bool shrink_textures_x2; bool use_fast_texture_filter; bool use_anisotropic_filter; @@ -86,6 +84,8 @@ public: int max_texture_image_units; int max_texture_size; + bool generate_wireframes; + Set<String> extensions; bool keep_original_textures; @@ -126,12 +126,33 @@ public: struct Info { uint64_t texture_mem; + uint64_t vertex_mem; + + struct Render { + uint32_t object_count; + uint32_t draw_call_count; + uint32_t material_switch_count; + uint32_t surface_switch_count; + uint32_t shader_rebind_count; + uint32_t vertices_count; + + void reset() { + object_count = 0; + draw_call_count = 0; + material_switch_count = 0; + surface_switch_count = 0; + shader_rebind_count = 0; + vertices_count = 0; + } + } render, render_final, snap; - uint32_t render_object_count; - uint32_t render_material_switch_count; - uint32_t render_surface_switch_count; - uint32_t render_shader_rebind_count; - uint32_t render_vertices_count; + Info() { + + texture_mem = 0; + vertex_mem = 0; + render.reset(); + render_final.reset(); + } } info; @@ -411,6 +432,7 @@ public: bool uses_vertex; bool uses_discard; bool uses_sss; + bool uses_screen_texture; bool writes_modelview_or_projection; } spatial; @@ -538,6 +560,11 @@ public: GLuint vertex_id; GLuint index_id; + GLuint index_wireframe_id; + GLuint array_wireframe_id; + GLuint instancing_array_wireframe_id; + int index_wireframe_len; + Vector<Rect3> skeleton_bone_aabb; Vector<bool> skeleton_bone_used; @@ -568,6 +595,8 @@ public: mesh->update_multimeshes(); } + int total_data_size; + Surface() { array_byte_size = 0; @@ -582,6 +611,13 @@ public: primitive = VS::PRIMITIVE_POINTS; index_array_len = 0; active = false; + + total_data_size = 0; + + index_wireframe_id = 0; + array_wireframe_id = 0; + instancing_array_wireframe_id = 0; + index_wireframe_len = 0; } ~Surface() { @@ -1128,7 +1164,7 @@ public: GLuint specular; GLuint diffuse; GLuint normal_rough; - GLuint motion_sss; + GLuint sss; GLuint effect_fbo; GLuint effect; @@ -1208,9 +1244,10 @@ public: flags[RENDER_TARGET_VFLIP] = false; flags[RENDER_TARGET_TRANSPARENT] = false; + flags[RENDER_TARGET_NO_3D_EFFECTS] = false; flags[RENDER_TARGET_NO_3D] = false; - flags[RENDER_TARGET_HDR] = true; flags[RENDER_TARGET_NO_SAMPLING] = false; + flags[RENDER_TARGET_HDR] = true; last_exposure_tick = 0; } @@ -1248,6 +1285,7 @@ public: struct CanvasOccluder : public RID_Data { + GLuint array_id; // 0 means, unconfigured GLuint vertex_id; // 0 means, unconfigured GLuint index_id; // 0 means, unconfigured PoolVector<Vector2> lines; @@ -1274,6 +1312,7 @@ public: float delta; uint64_t prev_tick; uint64_t count; + } frame; void initialize(); @@ -1283,6 +1322,14 @@ public: virtual void update_dirty_resources(); + virtual void set_debug_generate_wireframes(bool p_generate); + + virtual void render_info_begin_capture(); + virtual void render_info_end_capture(); + virtual int get_captured_render_info(VS::RenderInfo p_info); + + virtual int get_render_info(VS::RenderInfo p_info); + RasterizerStorageGLES3(); }; diff --git a/drivers/gles3/shader_compiler_gles3.cpp b/drivers/gles3/shader_compiler_gles3.cpp index 147357bcd0..b1f7b4c9bd 100644 --- a/drivers/gles3/shader_compiler_gles3.cpp +++ b/drivers/gles3/shader_compiler_gles3.cpp @@ -574,6 +574,7 @@ String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, Gener code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions); } break; + default: { code = "(" + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions) + _opstr(onode->op) + _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions) + ")"; @@ -593,6 +594,10 @@ String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, Gener code += _mktab(p_level) + "else\n"; code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions); } + } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) { + + code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions) + ")\n"; + code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions); } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) { @@ -740,6 +745,10 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() { //actions[VS::SHADER_SPATIAL].renames["SCREEN_UV"]=ShaderLanguage::TYPE_VEC2; actions[VS::SHADER_SPATIAL].renames["POINT_COORD"] = "gl_PointCoord"; actions[VS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom"; + actions[VS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv"; + actions[VS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture"; + actions[VS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer"; + actions[VS::SHADER_SPATIAL].renames["SIDE"] = "side"; actions[VS::SHADER_SPATIAL].usage_defines["TANGENT"] = "#define ENABLE_TANGENT_INTERP\n"; actions[VS::SHADER_SPATIAL].usage_defines["BINORMAL"] = "@TANGENT"; @@ -757,12 +766,18 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() { actions[VS::SHADER_SPATIAL].usage_defines["COLOR"] = "#define ENABLE_COLOR_INTERP\n"; actions[VS::SHADER_SPATIAL].usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n"; - actions[VS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS_MOTION\n"; + actions[VS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n"; + actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n"; + actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n"; actions[VS::SHADER_SPATIAL].renames["SSS_STRENGTH"] = "sss_strength"; actions[VS::SHADER_SPATIAL].render_mode_defines["skip_default_transform"] = "#define SKIP_TRANSFORM_USED\n"; + actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n"; + actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_oren_nayar"] = "#define DIFFUSE_OREN_NAYAR\n"; + actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_half_lambert"] = "#define DIFFUSE_HALF_LAMBERT\n"; + /* PARTICLES SHADER */ actions[VS::SHADER_PARTICLES].renames["COLOR"] = "out_color"; diff --git a/drivers/gles3/shader_gles3.cpp b/drivers/gles3/shader_gles3.cpp index bebc006032..8c6d15c3b7 100644 --- a/drivers/gles3/shader_gles3.cpp +++ b/drivers/gles3/shader_gles3.cpp @@ -100,14 +100,14 @@ void ShaderGLES3::bind_uniforms() { }; uniforms_dirty = false; -}; +} GLint ShaderGLES3::get_uniform_location(int p_idx) const { ERR_FAIL_COND_V(!version, -1); return version->uniform_location[p_idx]; -}; +} bool ShaderGLES3::bind() { @@ -399,14 +399,14 @@ ShaderGLES3::Version *ShaderGLES3::get_current_version() { strings.push_back(fragment_code2.get_data()); if (cc) { - code_string = cc->fragment.ascii(); + code_string = cc->light.ascii(); strings.push_back(code_string.get_data()); } strings.push_back(fragment_code3.get_data()); if (cc) { - code_string2 = cc->light.ascii(); + code_string2 = cc->fragment.ascii(); strings.push_back(code_string2.get_data()); } @@ -666,7 +666,7 @@ void ShaderGLES3::setup(const char **p_conditional_defines, int p_conditional_co //print_line("CODE1:\n"+String(fragment_code1.get_data())); String code2 = code.substr(cpos + material_tag.length(), code.length()); - cpos = code2.find(code_tag); + cpos = code2.find(light_code_tag); if (cpos == -1) { fragment_code2 = code2.ascii(); @@ -675,16 +675,16 @@ void ShaderGLES3::setup(const char **p_conditional_defines, int p_conditional_co fragment_code2 = code2.substr(0, cpos).ascii(); //print_line("CODE2:\n"+String(fragment_code2.get_data())); - String code3 = code2.substr(cpos + code_tag.length(), code2.length()); + String code3 = code2.substr(cpos + light_code_tag.length(), code2.length()); - cpos = code3.find(light_code_tag); + cpos = code3.find(code_tag); if (cpos == -1) { fragment_code3 = code3.ascii(); } else { fragment_code3 = code3.substr(0, cpos).ascii(); //print_line("CODE3:\n"+String(fragment_code3.get_data())); - fragment_code4 = code3.substr(cpos + light_code_tag.length(), code3.length()).ascii(); + fragment_code4 = code3.substr(cpos + code_tag.length(), code3.length()).ascii(); //print_line("CODE4:\n"+String(fragment_code4.get_data())); } } diff --git a/drivers/gles3/shaders/canvas.glsl b/drivers/gles3/shaders/canvas.glsl index e6c72da8f1..d6850b535e 100644 --- a/drivers/gles3/shaders/canvas.glsl +++ b/drivers/gles3/shaders/canvas.glsl @@ -211,6 +211,18 @@ MATERIAL_UNIFORMS #endif + +void light_compute(inout vec3 light,vec3 light_vec,float light_height,vec4 light_color,vec2 light_uv,vec4 shadow,vec3 normal,vec2 uv,vec2 screen_uv,vec4 color) { + +#if defined(USE_LIGHT_SHADER_CODE) + +LIGHT_SHADER_CODE + +#endif + +} + + void main() { vec4 color = color_interp; @@ -285,11 +297,7 @@ FRAGMENT_SHADER_CODE #if defined(USE_LIGHT_SHADER_CODE) //light is written by the light shader - { - vec4 light_out=light*color; -LIGHT_SHADER_CODE - color=light_out; - } + light_compute(light,light_vec,light_height,light_color,light_uv,shadow,normal,uv,screen_uv,color); #else @@ -373,7 +381,7 @@ LIGHT_SHADER_CODE #ifdef SHADOW_FILTER_NEAREST - SHADOW_TEST(su+shadowpixel_size); + SHADOW_TEST(su); #endif diff --git a/drivers/gles3/shaders/copy.glsl b/drivers/gles3/shaders/copy.glsl index c8985e6902..4c8648903e 100644 --- a/drivers/gles3/shaders/copy.glsl +++ b/drivers/gles3/shaders/copy.glsl @@ -23,6 +23,10 @@ void main() { cube_interp = cube_in; #else uv_interp = uv_in; +#ifdef V_FLIP + uv_interp.y = 1.0-uv_interp.y; +#endif + #endif uv2_interp = uv2_in; gl_Position = vertex_attrib; @@ -84,8 +88,24 @@ uniform vec2 pixel_size; in vec2 uv2_interp; + +#ifdef USE_BCS + +uniform vec3 bcs; + +#endif + +#ifdef USE_COLOR_CORRECTION + +uniform sampler2D color_correction; //texunit:1 + +#endif + layout(location = 0) out vec4 frag_color; + + + void main() { //vec4 color = color_interp; @@ -135,6 +155,21 @@ void main() { color+=texture( source, uv_interp+vec2( 0.0,-2.0)*pixel_size )*0.06136; #endif +#ifdef USE_BCS + + color.rgb = mix(vec3(0.0),color.rgb,bcs.x); + color.rgb = mix(vec3(0.5),color.rgb,bcs.y); + color.rgb = mix(vec3(dot(vec3(1.0),color.rgb)*0.33333),color.rgb,bcs.z); + +#endif + +#ifdef USE_COLOR_CORRECTION + + color.r = texture(color_correction,vec2(color.r,0.0)).r; + color.g = texture(color_correction,vec2(color.g,0.0)).g; + color.b = texture(color_correction,vec2(color.b,0.0)).b; +#endif + #ifdef USE_MULTIPLIER color.rgb*=multiplier; #endif diff --git a/drivers/gles3/shaders/particles.glsl b/drivers/gles3/shaders/particles.glsl index fa12dd7408..c1356bb110 100644 --- a/drivers/gles3/shaders/particles.glsl +++ b/drivers/gles3/shaders/particles.glsl @@ -132,6 +132,7 @@ void main() { } uint particle_number = current_cycle * uint(total_particles) + uint(gl_VertexID); + int index = int(gl_VertexID); if (restart) { shader_active=emitting; @@ -244,6 +245,10 @@ MATERIAL_UNIFORMS void main() { { +LIGHT_SHADER_CODE + } + + { FRAGMENT_SHADER_CODE } } diff --git a/drivers/gles3/shaders/resolve.glsl b/drivers/gles3/shaders/resolve.glsl index 6acc712299..181a3c99ec 100644 --- a/drivers/gles3/shaders/resolve.glsl +++ b/drivers/gles3/shaders/resolve.glsl @@ -20,7 +20,7 @@ in vec2 uv_interp; uniform sampler2D source_specular; //texunit:0 uniform sampler2D source_ssr; //texunit:1 -uniform float stuff; +uniform vec2 pixel_size; in vec2 uv2_interp; diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl index 81166e84ff..0f1a7458d7 100644 --- a/drivers/gles3/shaders/scene.glsl +++ b/drivers/gles3/shaders/scene.glsl @@ -67,11 +67,15 @@ layout(std140) uniform SceneData { //ubo:0 highp vec4 ambient_light_color; highp vec4 bg_color; + + vec4 fog_color_enabled; + vec4 fog_sun_color_amount; + float ambient_energy; float bg_energy; - float shadow_z_offset; - float shadow_z_slope_scale; + float z_offset; + float z_slope_scale; float shadow_dual_paraboloid_render_zfar; float shadow_dual_paraboloid_render_side; @@ -79,10 +83,21 @@ layout(std140) uniform SceneData { //ubo:0 vec2 shadow_atlas_pixel_size; vec2 directional_shadow_pixel_size; + float z_far; float reflection_multiplier; float subsurface_scatter_width; float ambient_occlusion_affect_light; + bool fog_depth_enabled; + float fog_depth_begin; + float fog_depth_curve; + bool fog_transmit_enabled; + float fog_transmit_curve; + bool fog_height_enabled; + float fog_height_min; + float fog_height_max; + float fog_height_curve; + }; uniform highp mat4 world_transform; @@ -153,7 +168,7 @@ out highp float dp_clip; #define SKELETON_TEXTURE_WIDTH 256 #ifdef USE_SKELETON -uniform highp sampler2D skeleton_texture; //texunit:-6 +uniform highp sampler2D skeleton_texture; //texunit:-1 #endif out highp vec4 position_interp; @@ -230,7 +245,7 @@ void main() { normal = vec4(normal,0.0) * m; #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) - tangent.xyz = vec4(tangent.xyz,0.0) * mn; + tangent.xyz = vec4(tangent.xyz,0.0) * m; #endif } #endif @@ -304,7 +319,7 @@ VERTEX_SHADER_CODE //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges - highp vec3 vtx = vertex_interp+normalize(vertex_interp)*shadow_z_offset; + highp vec3 vtx = vertex_interp+normalize(vertex_interp)*z_offset; highp float distance = length(vtx); vtx = normalize(vtx); vtx.xy/=1.0-vtx.z; @@ -317,8 +332,8 @@ VERTEX_SHADER_CODE #else - float z_ofs = shadow_z_offset; - z_ofs += (1.0-abs(normal_interp.z))*shadow_z_slope_scale; + float z_ofs = z_offset; + z_ofs += (1.0-abs(normal_interp.z))*z_slope_scale; vertex_interp.z-=z_ofs; #endif //RENDER_DEPTH_DUAL_PARABOLOID @@ -338,7 +353,20 @@ VERTEX_SHADER_CODE [fragment] +/* texture unit usage, N is max_texture_unity-N +1-skeleton +2-radiance +3-reflection_atlas +4-directional_shadow +5-shadow_atlas +6-decal_atlas +7-screen +8-depth +9-probe1 +10-probe2 + +*/ #define M_PI 3.14159265359 @@ -370,7 +398,6 @@ in vec3 normal_interp; //used on forward mainly uniform bool no_ambient_light; -uniform sampler2D brdf_texture; //texunit:-1 #ifdef USE_RADIANCE_MAP @@ -413,12 +440,14 @@ layout(std140) uniform SceneData { highp vec4 ambient_light_color; highp vec4 bg_color; + vec4 fog_color_enabled; + vec4 fog_sun_color_amount; float ambient_energy; float bg_energy; - float shadow_z_offset; - float shadow_z_slope_scale; + float z_offset; + float z_slope_scale; float shadow_dual_paraboloid_render_zfar; float shadow_dual_paraboloid_render_side; @@ -426,10 +455,20 @@ layout(std140) uniform SceneData { vec2 shadow_atlas_pixel_size; vec2 directional_shadow_pixel_size; + float z_far; float reflection_multiplier; float subsurface_scatter_width; float ambient_occlusion_affect_light; + bool fog_depth_enabled; + float fog_depth_begin; + float fog_depth_curve; + bool fog_transmit_enabled; + float fog_transmit_curve; + bool fog_height_enabled; + float fog_height_min; + float fog_height_max; + float fog_height_curve; }; //directional light data @@ -482,7 +521,7 @@ layout(std140) uniform SpotLightData { //ubo:5 }; -uniform highp sampler2DShadow shadow_atlas; //texunit:-3 +uniform highp sampler2DShadow shadow_atlas; //texunit:-5 struct ReflectionData { @@ -500,7 +539,7 @@ layout(std140) uniform ReflectionProbeData { //ubo:6 ReflectionData reflections[MAX_REFLECTION_DATA_STRUCTS]; }; -uniform mediump sampler2D reflection_atlas; //texunit:-5 +uniform mediump sampler2D reflection_atlas; //texunit:-3 #ifdef USE_FORWARD_LIGHTING @@ -517,14 +556,19 @@ uniform int reflection_count; #endif +#if defined(SCREEN_TEXTURE_USED) + +uniform highp sampler2D screen_texture; //texunit:-7 + +#endif #ifdef USE_MULTIPLE_RENDER_TARGETS layout(location=0) out vec4 diffuse_buffer; layout(location=1) out vec4 specular_buffer; layout(location=2) out vec4 normal_mr_buffer; -#if defined (ENABLE_SSS_MOTION) -layout(location=3) out vec4 motion_ssr_buffer; +#if defined(ENABLE_SSS) +layout(location=3) out float sss_buffer; #endif #else @@ -534,7 +578,7 @@ layout(location=0) out vec4 frag_color; #endif in highp vec4 position_interp; -uniform highp sampler2D depth_buffer; //texunit:-9 +uniform highp sampler2D depth_buffer; //texunit:-8 float contact_shadow_compute(vec3 pos, vec3 dir, float max_distance) { @@ -623,15 +667,66 @@ float GTR1(float NdotH, float a) void light_compute(vec3 N, vec3 L,vec3 V,vec3 B, vec3 T,vec3 light_color,vec3 diffuse_color, float specular_blob_intensity, float roughness, float rim,float rim_tint, float clearcoat, float clearcoat_gloss,float anisotropy,inout vec3 diffuse, inout vec3 specular) { +#if defined(USE_LIGHT_SHADER_CODE) +//light is written by the light shader + + +LIGHT_SHADER_CODE + + +#else + float dotNL = max(dot(N,L), 0.0 ); - float dotNV = max(dot(N,V), 0.0 ); +#if defined(DIFFUSE_HALF_LAMBERT) + + float hl = dot(N,L) * 0.5 + 0.5; + diffuse += hl * light_color * diffuse_color; + +#elif defined(DIFFUSE_OREN_NAYAR) + + { + float LdotV = dot(L, V); + float NdotL = dot(L, N); + float NdotV = dot(N, V); + + float s = LdotV - NdotL * NdotV; + float t = mix(1.0, max(NdotL, NdotV), step(0.0, s)); + + float sigma2 = roughness * roughness; + vec3 A = 1.0 + sigma2 * (diffuse_color / (sigma2 + 0.13) + 0.5 / (sigma2 + 0.33)); + float B = 0.45 * sigma2 / (sigma2 + 0.09); + + diffuse += diffuse_color * max(0.0, NdotL) * (A + vec3(B) * s / t) / M_PI; + } + +#elif defined(DIFFUSE_BURLEY) + + { + float NdotL = dot(L, N); + float NdotV = dot(N, V); + float VdotH = dot(N, normalize(L+V)); + float energyBias = mix(roughness, 0.0, 0.5); + float energyFactor = mix(roughness, 1.0, 1.0 / 1.51); + float fd90 = energyBias + 2.0 * VdotH * VdotH * roughness; + float f0 = 1.0; + float lightScatter = f0 + (fd90 - f0) * pow(1.0 - NdotL, 5.0); + float viewScatter = f0 + (fd90 - f0) * pow(1.0 - NdotV, 5.0); + + diffuse+= light_color * diffuse_color * lightScatter * viewScatter * energyFactor; + } +#else + //lambert + diffuse += dotNL * light_color * diffuse_color; +#endif + + + float dotNV = max(dot(N,V), 0.0 ); #if defined(LIGHT_USE_RIM) float rim_light = pow(1.0-dotNV,(1.0-roughness)*16.0); diffuse += rim_light * rim * mix(vec3(1.0),diffuse_color,rim_tint) * light_color; #endif - diffuse += dotNL * light_color * diffuse_color; if (roughness > 0.0) { @@ -685,6 +780,7 @@ void light_compute(vec3 N, vec3 L,vec3 V,vec3 B, vec3 T,vec3 light_color,vec3 di } +#endif //defined(USE_LIGHT_SHADER_CODE) } @@ -968,7 +1064,7 @@ void reflection_process(int idx, vec3 vertex, vec3 normal,vec3 binormal, vec3 ta #ifdef USE_GI_PROBES -uniform mediump sampler3D gi_probe1; //texunit:-11 +uniform mediump sampler3D gi_probe1; //texunit:-9 uniform highp mat4 gi_probe_xform1; uniform highp vec3 gi_probe_bounds1; uniform highp vec3 gi_probe_cell_size1; @@ -1213,13 +1309,15 @@ void main() { float normaldepth=1.0; - +#if defined(SCREEN_UV_USED) + vec2 screen_uv = gl_FragCoord.xy*screen_pixel_size; +#endif #if defined(ENABLE_DISCARD) bool discard_=false; #endif -#if defined (ENABLE_SSS_MOTION) +#if defined (ENABLE_SSS) float sss_strength=0.0; #endif @@ -1293,7 +1391,7 @@ FRAGMENT_SHADER_CODE { //read radiance from dual paraboloid vec3 ref_vec = reflect(-eye_vec,normal); //2.0 * ndotv * normal - view; // reflect(v, n); - ref_vec=normalize((radiance_inverse_xform * vec4(ref_vec,0.0)).xyz); + ref_vec=normalize((radiance_inverse_xform * vec4(ref_vec,0.0)).xyz); vec3 radiance = textureDualParabolod(radiance_map,ref_vec,lod) * bg_energy; specular_light = radiance; @@ -1507,14 +1605,7 @@ FRAGMENT_SHADER_CODE -#if defined(USE_LIGHT_SHADER_CODE) -//light is written by the light shader -{ - -LIGHT_SHADER_CODE -} -#endif #ifdef RENDER_DEPTH //nothing happens, so a tree-ssa optimizer will result in no fragment shader :) @@ -1551,6 +1642,49 @@ LIGHT_SHADER_CODE specular_light *= min(1.0,50.0 * f0.g) * brdf.y + brdf.x * f0; } + if (fog_color_enabled.a > 0.5) { + + float fog_amount=0; + + + +#ifdef USE_LIGHT_DIRECTIONAL + + vec3 fog_color = mix( fog_color_enabled.rgb, fog_sun_color_amount.rgb,fog_sun_color_amount.a * pow(max( dot(normalize(vertex),-light_direction_attenuation.xyz), 0.0),8.0) ); +#else + + vec3 fog_color = fog_color_enabled.rgb; +#endif + + //apply fog + + if (fog_depth_enabled) { + + float fog_z = smoothstep(fog_depth_begin,z_far,-vertex.z); + + fog_amount = pow(fog_z,fog_depth_curve); + if (fog_transmit_enabled) { + vec3 total_light = emission + ambient_light + specular_light + diffuse_light; + float transmit = pow(fog_z,fog_transmit_curve); + fog_color = mix(max(total_light,fog_color),fog_color,transmit); + } + } + + if (fog_height_enabled) { + float y = (camera_matrix * vec4(vertex,1.0)).y; + fog_amount = max(fog_amount,pow(1.0-smoothstep(fog_height_min,fog_height_max,y),fog_height_curve)); + } + + float rev_amount = 1.0 - fog_amount; + + + emission = emission * rev_amount + fog_color * fog_amount; + ambient_light*=rev_amount; + specular_light*rev_amount; + diffuse_light*=rev_amount; + + } + #ifdef USE_MULTIPLE_RENDER_TARGETS #if defined(ENABLE_AO) @@ -1566,16 +1700,16 @@ LIGHT_SHADER_CODE #endif //ENABLE_AO diffuse_buffer=vec4(emission+diffuse_light+ambient_light,ambient_scale); - specular_buffer=vec4(specular_light,max(specular.r,max(specular.g,specular.b))); + specular_buffer=vec4(specular_light,metallic); normal_mr_buffer=vec4(normalize(normal)*0.5+0.5,roughness); -#if defined (ENABLE_SSS_MOTION) - motion_ssr_buffer = vec4(vec3(0.0),sss_strength); +#if defined (ENABLE_SSS) + sss_buffer = sss_strength; #endif -#else +#else //USE_MULTIPLE_RENDER_TARGETS #ifdef SHADELESS diff --git a/drivers/gles3/shaders/ssao_blur.glsl b/drivers/gles3/shaders/ssao_blur.glsl index ff852487c0..ce4154f50c 100644 --- a/drivers/gles3/shaders/ssao_blur.glsl +++ b/drivers/gles3/shaders/ssao_blur.glsl @@ -24,7 +24,7 @@ layout(location = 0) out float visibility; // Tunable Parameters: /** Increase to make depth edges crisper. Decrease to reduce flicker. */ -#define EDGE_SHARPNESS (1.0) +#define EDGE_SHARPNESS (4.0) /** Step in 2-pixel intervals since we already blurred against neighbors in the first AO pass. This constant can be increased while R decreases to improve diff --git a/drivers/gles3/shaders/subsurf_scattering.glsl b/drivers/gles3/shaders/subsurf_scattering.glsl index eb329dbaed..569be6c5fe 100644 --- a/drivers/gles3/shaders/subsurf_scattering.glsl +++ b/drivers/gles3/shaders/subsurf_scattering.glsl @@ -107,14 +107,14 @@ uniform vec2 dir; in vec2 uv_interp; uniform sampler2D source_diffuse; //texunit:0 -uniform sampler2D source_motion_ss; //texunit:1 +uniform sampler2D source_sss; //texunit:1 uniform sampler2D source_depth; //texunit:2 layout(location = 0) out vec4 frag_color; void main() { - float strength = texture(source_motion_ss,uv_interp).a; + float strength = texture(source_sss,uv_interp).r; strength*=strength; //stored as sqrt // Fetch color of current pixel: diff --git a/drivers/gles3/shaders/tonemap.glsl b/drivers/gles3/shaders/tonemap.glsl index 8f7e0c7be3..3ce2edf4e9 100644 --- a/drivers/gles3/shaders/tonemap.glsl +++ b/drivers/gles3/shaders/tonemap.glsl @@ -6,12 +6,13 @@ layout(location=4) in vec2 uv_in; out vec2 uv_interp; - - void main() { gl_Position = vertex_attrib; uv_interp = uv_in; +#ifdef V_FLIP + uv_interp.y = 1.0-uv_interp.y; +#endif } @@ -39,6 +40,19 @@ uniform highp float glow_intensity; #endif +#ifdef USE_BCS + +uniform vec3 bcs; + +#endif + +#ifdef USE_COLOR_CORRECTION + +uniform sampler2D color_correction; //texunit:3 + +#endif + + layout(location = 0) out vec4 frag_color; #ifdef USE_GLOW_FILTER_BICUBIC @@ -255,6 +269,20 @@ void main() { color.rgb = mix( (vec3(1.0)+a)*pow(color.rgb,vec3(1.0/2.4))-a , 12.92*color.rgb , lessThan(color.rgb,vec3(0.0031308))); +#ifdef USE_BCS + + color.rgb = mix(vec3(0.0),color.rgb,bcs.x); + color.rgb = mix(vec3(0.5),color.rgb,bcs.y); + color.rgb = mix(vec3(dot(vec3(1.0),color.rgb)*0.33333),color.rgb,bcs.z); + +#endif + +#ifdef USE_COLOR_CORRECTION + + color.r = texture(color_correction,vec2(color.r,0.0)).r; + color.g = texture(color_correction,vec2(color.g,0.0)).g; + color.b = texture(color_correction,vec2(color.b,0.0)).b; +#endif frag_color=vec4(color.rgb,1.0); diff --git a/drivers/png/image_loader_png.cpp b/drivers/png/image_loader_png.cpp index 25ab767bed..33d271248c 100644 --- a/drivers/png/image_loader_png.cpp +++ b/drivers/png/image_loader_png.cpp @@ -256,6 +256,7 @@ static Ref<Image> _load_mem_png(const uint8_t *p_png, int p_size) { static Ref<Image> _lossless_unpack_png(const PoolVector<uint8_t> &p_data) { int len = p_data.size(); + ERR_FAIL_COND_V(len < 4, Ref<Image>()); PoolVector<uint8_t>::Read r = p_data.read(); ERR_FAIL_COND_V(r[0] != 'P' || r[1] != 'N' || r[2] != 'G' || r[3] != ' ', Ref<Image>()); return _load_mem_png(&r[4], len - 4); diff --git a/drivers/png/resource_saver_png.cpp b/drivers/png/resource_saver_png.cpp index 1700603489..0d7e1d9d72 100644 --- a/drivers/png/resource_saver_png.cpp +++ b/drivers/png/resource_saver_png.cpp @@ -55,41 +55,6 @@ Error ResourceSaverPNG::save(const String &p_path, const RES &p_resource, uint32 Error err = save_image(p_path, img); if (err == OK) { - - bool global_filter = GlobalConfig::get_singleton()->get("image_loader/filter"); - bool global_mipmaps = GlobalConfig::get_singleton()->get("image_loader/gen_mipmaps"); - bool global_repeat = GlobalConfig::get_singleton()->get("image_loader/repeat"); - - String text; - - if (global_filter != bool(texture->get_flags() & Texture::FLAG_FILTER)) { - text += bool(texture->get_flags() & Texture::FLAG_FILTER) ? "filter=true\n" : "filter=false\n"; - } - if (global_mipmaps != bool(texture->get_flags() & Texture::FLAG_MIPMAPS)) { - text += bool(texture->get_flags() & Texture::FLAG_MIPMAPS) ? "gen_mipmaps=true\n" : "gen_mipmaps=false\n"; - } - if (global_repeat != bool(texture->get_flags() & Texture::FLAG_REPEAT)) { - text += bool(texture->get_flags() & Texture::FLAG_REPEAT) ? "repeat=true\n" : "repeat=false\n"; - } - if (bool(texture->get_flags() & Texture::FLAG_ANISOTROPIC_FILTER)) { - text += "anisotropic=true\n"; - } - if (bool(texture->get_flags() & Texture::FLAG_CONVERT_TO_LINEAR)) { - text += "tolinear=true\n"; - } - if (bool(texture->get_flags() & Texture::FLAG_MIRRORED_REPEAT)) { - text += "mirroredrepeat=true\n"; - } - - if (text != "" || FileAccess::exists(p_path + ".flags")) { - - FileAccess *f = FileAccess::open(p_path + ".flags", FileAccess::WRITE); - if (f) { - - f->store_string(text); - memdelete(f); - } - } } return err; diff --git a/editor/SCsub b/editor/SCsub index d7392f8249..c46e443534 100644 --- a/editor/SCsub +++ b/editor/SCsub @@ -141,6 +141,30 @@ def make_translations_header(target, source, env): g.write("#endif") +def make_authors_header(target, source, env): + + src = source[0].srcnode().abspath + dst = target[0].srcnode().abspath + f = open(src, "rb") + g = open(dst, "wb") + + g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") + g.write("#ifndef _EDITOR_AUTHORS_H\n") + g.write("#define _EDITOR_AUTHORS_H\n") + g.write("static const char *dev_names[] = {\n") + + name_count = -1 + for line in f: + if name_count >= 0: + if line.startswith(" "): + g.write("\t\"" + line.strip() + "\",\n") + name_count += 1 + elif line.strip() == "## Developers": + name_count = 0 + g.write("\t0\n") + g.write("};\n") + g.write("#define AUTHORS_COUNT " + str(name_count) + "\n") + g.write("#endif\n") if (env["tools"] == "yes"): @@ -188,6 +212,10 @@ if (env["tools"] == "yes"): env.Depends('#editor/builtin_fonts.h', flist) env.Command('#editor/builtin_fonts.h', flist, make_fonts_header) + # Authors + env.Depends('#editor/authors.h', "../AUTHORS.md") + env.Command('#editor/authors.h', "../AUTHORS.md", make_authors_header) + env.add_source_files(env.editor_sources, "*.cpp") diff --git a/editor/animation_editor.cpp b/editor/animation_editor.cpp index a5de4fee88..1798e66e8a 100644 --- a/editor/animation_editor.cpp +++ b/editor/animation_editor.cpp @@ -76,7 +76,7 @@ private: Ref<StyleBox> sb = get_stylebox("normal", "LineEdit"); sb->draw(ci, r); r.size -= sb->get_minimum_size(); - r.pos += sb->get_offset(); + r.position += sb->get_offset(); //VisualServer::get_singleton()->canvas_item_add Ref<Font> f = get_font("font", "Label"); @@ -111,7 +111,7 @@ private: iflp = 1.0 - iflp; } - VisualServer::get_singleton()->canvas_item_add_line(ci, r.pos + Point2(iflp * r.size.width, prev * r.size.height), r.pos + Point2(ifl * r.size.width, h * r.size.height), mcolor); + VisualServer::get_singleton()->canvas_item_add_line(ci, r.position + Point2(iflp * r.size.width, prev * r.size.height), r.position + Point2(ifl * r.size.width, h * r.size.height), mcolor); prev = h; } @@ -138,7 +138,7 @@ private: iflp = 1.0 - iflp; } - VisualServer::get_singleton()->canvas_item_add_line(ci, r.pos + Point2(iflp * r.size.width, prev * r.size.height), r.pos + Point2(ifl * r.size.width, h * r.size.height), color); + VisualServer::get_singleton()->canvas_item_add_line(ci, r.position + Point2(iflp * r.size.width, prev * r.size.height), r.position + Point2(ifl * r.size.width, h * r.size.height), color); prev = h; } } @@ -1900,13 +1900,13 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) { - Point2 mpos = mb->get_pos() - ofs; + Point2 mpos = mb->get_position() - ofs; if (selection.size() == 0) { // Auto-select on right-click if nothing is selected // Note: This code is pretty much duplicated from the left click code, // both codes could be moved into a function to avoid the duplicated code. - Point2 mpos = mb->get_pos() - ofs; + Point2 mpos = mb->get_position() - ofs; if (mpos.y < h) { return; @@ -1951,7 +1951,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) if (key == -1) { click.click = ClickOver::CLICK_SELECT_KEYS; - click.at = mb->get_pos(); + click.at = mb->get_position(); click.to = click.at; click.shift = mb->get_shift(); selected_track = idx; @@ -1974,7 +1974,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) selection.insert(sk, ki); click.click = ClickOver::CLICK_MOVE_KEYS; - click.at = mb->get_pos(); + click.at = mb->get_position(); click.to = click.at; update(); selected_track = idx; @@ -2009,14 +2009,14 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) if (mb->is_pressed()) { - Point2 mpos = mb->get_pos() - ofs; + Point2 mpos = mb->get_position() - ofs; if (mpos.y < h) { if (mpos.x < name_limit && mpos.x > (name_limit - hsep - hsize_icon->get_width())) { click.click = ClickOver::CLICK_RESIZE_NAMES; - click.at = mb->get_pos(); + click.at = mb->get_position(); click.to = click.at; click.at.y = name_limit; } @@ -2035,7 +2035,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) pos = animation->get_length(); timeline_pos = pos; click.click = ClickOver::CLICK_DRAG_TIMELINE; - click.at = mb->get_pos(); + click.at = mb->get_position(); click.to = click.at; emit_signal("timeline_changed", pos, false); } @@ -2055,7 +2055,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) if (mpos.x >= name_limit && mpos.x < settings_limit) { click.click = ClickOver::CLICK_SELECT_KEYS; - click.at = mb->get_pos(); + click.at = mb->get_position(); click.to = click.at; //drag select region } @@ -2076,7 +2076,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) Rect2 area(ofs.x, ofs.y + ((int(mpos.y) / h) + 1) * h, name_limit, h); track_name->set_text(animation->track_get_path(idx)); - track_name->set_position(te->get_global_position() + area.pos); + track_name->set_position(te->get_global_position() + area.position); track_name->set_size(area.size); track_name->show_modal(); track_name->grab_focus(); @@ -2115,7 +2115,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) if (key == -1) { click.click = ClickOver::CLICK_SELECT_KEYS; - click.at = mb->get_pos(); + click.at = mb->get_position(); click.to = click.at; click.shift = mb->get_shift(); selected_track = idx; @@ -2138,7 +2138,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) selection.insert(sk, ki); click.click = ClickOver::CLICK_MOVE_KEYS; - click.at = mb->get_pos(); + click.at = mb->get_position(); click.to = click.at; update(); selected_track = idx; @@ -2601,7 +2601,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) float clickp = click.at.x - ofs.x; float dif = base - clickp; - float target = mm->get_pos().x + dif - ofs.x; + float target = mm->get_position().x + dif - ofs.x; float ratio = target / settings_limit; @@ -2615,7 +2615,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) } break; case ClickOver::CLICK_DRAG_TIMELINE: { - Point2 mpos = mm->get_pos() - ofs; + Point2 mpos = mm->get_position() - ofs; /* if (mpos.x<name_limit) mpos.x=name_limit; @@ -2646,7 +2646,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) } break; case ClickOver::CLICK_SELECT_KEYS: { - click.to = mm->get_pos(); + click.to = mm->get_position(); if (click.to.y < h && click.at.y > h && mm->get_relative().y < 0) { float prev = v_scroll->get_value(); @@ -2665,7 +2665,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) } break; case ClickOver::CLICK_MOVE_KEYS: { - click.to = mm->get_pos(); + click.to = mm->get_position(); } break; default: {} } @@ -2680,7 +2680,7 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) if (mm->get_button_mask() == 0) { - Point2 mpos = mm->get_pos() - ofs; + Point2 mpos = mm->get_position() - ofs; if (mpos.y < h) { #if 0 diff --git a/editor/editor_audio_buses.cpp b/editor/editor_audio_buses.cpp index 26029261b2..e3e5793ec8 100644 --- a/editor/editor_audio_buses.cpp +++ b/editor/editor_audio_buses.cpp @@ -342,7 +342,7 @@ void EditorAudioBus::_effect_edited() { if (effect->get_metadata(0) == Variant()) { Rect2 area = effects->get_item_rect(effect); - effect_options->set_position(effects->get_global_position() + area.pos + Vector2(0, area.size.y)); + effect_options->set_position(effects->get_global_position() + area.position + Vector2(0, area.size.y)); effect_options->popup(); //add effect } else { @@ -396,7 +396,7 @@ void EditorAudioBus::_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> mb = p_event; if (mb.is_valid() && mb->get_button_index() == 2 && mb->is_pressed()) { - Vector2 pos = Vector2(mb->get_pos().x, mb->get_pos().y); + Vector2 pos = Vector2(mb->get_position().x, mb->get_position().y); delete_popup->set_position(get_global_position() + pos); delete_popup->popup(); } @@ -632,21 +632,24 @@ EditorAudioBus::EditorAudioBus(EditorAudioBuses *p_buses) { solo = memnew(ToolButton); solo->set_text("S"); solo->set_toggle_mode(true); - solo->set_modulate(Color(0.8, 1.2, 0.8)); + solo->add_color_override("font_color_pressed", Color(0.2, 0.9, 0.2)); + solo->add_color_override("font_color_hover", Color(0.6, 0.9, 0.6)); solo->set_focus_mode(FOCUS_NONE); solo->connect("pressed", this, "_solo_toggled"); hbc->add_child(solo); mute = memnew(ToolButton); mute->set_text("M"); mute->set_toggle_mode(true); - mute->set_modulate(Color(1.2, 0.8, 0.8)); + mute->add_color_override("font_color_pressed", Color(0.9, 0.2, 0.2)); + mute->add_color_override("font_color_hover", Color(0.9, 0.6, 0.6)); mute->set_focus_mode(FOCUS_NONE); mute->connect("pressed", this, "_mute_toggled"); hbc->add_child(mute); bypass = memnew(ToolButton); bypass->set_text("B"); bypass->set_toggle_mode(true); - bypass->set_modulate(Color(1.1, 1.1, 0.8)); + bypass->add_color_override("font_color_pressed", Color(0.9, 0.9, 0.2)); + bypass->add_color_override("font_color_hover", Color(0.9, 0.9, 0.6)); bypass->set_focus_mode(FOCUS_NONE); bypass->connect("pressed", this, "_bypass_toggled"); hbc->add_child(bypass); diff --git a/editor/editor_fonts.cpp b/editor/editor_fonts.cpp index dc4c2b21bc..0873b90f22 100644 --- a/editor/editor_fonts.cpp +++ b/editor/editor_fonts.cpp @@ -49,8 +49,8 @@ static Ref<BitmapFont> make_font(int p_height, int p_ascent, int p_valign, int p int chr = c[0]; Rect2 frect; - frect.pos.x = c[1]; - frect.pos.y = c[2]; + frect.position.x = c[1]; + frect.position.y = c[2]; frect.size.x = c[3]; frect.size.y = c[4]; Point2 align(c[5], c[6] + p_valign); diff --git a/editor/editor_help.cpp b/editor/editor_help.cpp index 8fb307a77d..11cb61370d 100644 --- a/editor/editor_help.cpp +++ b/editor/editor_help.cpp @@ -252,8 +252,8 @@ void EditorHelpSearch::_confirmed() { return; String mdata = ti->get_metadata(0); + EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT); emit_signal("go_to_help", mdata); - editor->call("_editor_select", EditorNode::EDITOR_SCRIPT); // in case EditorHelpSearch beeen invoked on top of other editor window // go to that hide(); } @@ -288,7 +288,6 @@ void EditorHelpSearch::_bind_methods() { EditorHelpSearch::EditorHelpSearch() { - editor = EditorNode::get_singleton(); VBoxContainer *vbc = memnew(VBoxContainer); add_child(vbc); @@ -362,8 +361,8 @@ void EditorHelpIndex::_tree_item_selected() { if (!s) return; + EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT); emit_signal("open_class", s->get_text(0)); - hide(); //_goto_desc(s->get_text(0)); @@ -371,12 +370,10 @@ void EditorHelpIndex::_tree_item_selected() { void EditorHelpIndex::select_class(const String &p_class) { - EditorNode *editor = EditorNode::get_singleton(); if (!tree_item_map.has(p_class)) return; tree_item_map[p_class]->select(0); class_list->ensure_cursor_is_visible(); - editor->call("_editor_select", EditorNode::EDITOR_SCRIPT); // in case EditorHelpIndex beeen invoked on top of other editor window } void EditorHelpIndex::popup() { @@ -1281,7 +1278,7 @@ Error EditorHelp::_goto_desc(const String &p_class, int p_vscr) { void EditorHelp::_request_help(const String &p_string) { Error err = _goto_desc(p_string); if (err == OK) { - editor->call("_editor_select", EditorNode::EDITOR_SCRIPT); + EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT); } //100 palabras } @@ -1691,8 +1688,6 @@ void EditorHelp::_bind_methods() { EditorHelp::EditorHelp() { - editor = EditorNode::get_singleton(); - VBoxContainer *vbc = this; EDITOR_DEF("text_editor/help/sort_functions_alphabetically", true); diff --git a/editor/editor_help.h b/editor/editor_help.h index d6fc0e3bf2..46d83490f4 100644 --- a/editor/editor_help.h +++ b/editor/editor_help.h @@ -49,7 +49,6 @@ class EditorHelpSearch : public ConfirmationDialog { GDCLASS(EditorHelpSearch, ConfirmationDialog) - EditorNode *editor; LineEdit *search_box; Tree *search_options; String base_type; @@ -119,7 +118,6 @@ class EditorHelp : public VBoxContainer { String edited_class; - EditorNode *editor; Map<String, int> method_line; Map<String, int> signal_line; Map<String, int> property_line; diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index c1f422cce3..6587e4fe09 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -30,6 +30,7 @@ #include "editor_node.h" #include "animation_editor.h" +#include "authors.h" #include "bind/core_bind.h" #include "class_db.h" #include "core/io/resource_loader.h" @@ -271,6 +272,8 @@ void EditorNode::_notification(int p_what) { } editor_selection->update(); + scene_root->set_size_override(true, Size2(GlobalConfig::get_singleton()->get("display/window/width"), GlobalConfig::get_singleton()->get("display/window/height"))); + ResourceImporterTexture::get_singleton()->update_imports(); } if (p_what == NOTIFICATION_ENTER_TREE) { @@ -822,56 +825,54 @@ void EditorNode::_save_scene_with_preview(String p_file) { } save.step(TTR("Creating Thumbnail"), 1); //current view? - int screen = -1; - for (int i = 0; i < editor_table.size(); i++) { - if (editor_plugin_screen == editor_table[i]) { - screen = i; - break; - } + + Ref<Image> img; + if (is2d) { + img = scene_root->get_texture()->get_data(); + } else { + img = SpatialEditor::get_singleton()->get_editor_viewport(0)->get_viewport_node()->get_texture()->get_data(); } - _editor_select(is2d ? EDITOR_2D : EDITOR_3D); + if (img.is_valid()) { + save.step(TTR("Creating Thumbnail"), 2); + save.step(TTR("Creating Thumbnail"), 3); - save.step(TTR("Creating Thumbnail"), 2); - save.step(TTR("Creating Thumbnail"), 3); -#if 0 - Image img = VS::get_singleton()->viewport_texture(scree_capture(viewport); - int preview_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); - preview_size*=EDSCALE; - int width,height; - if (img.get_width() > preview_size && img.get_width() >= img.get_height()) { + int preview_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); + preview_size *= EDSCALE; + int width, height; + if (img->get_width() > preview_size && img->get_width() >= img->get_height()) { - width=preview_size; - height = img.get_height() * preview_size / img.get_width(); - } else if (img.get_height() > preview_size && img.get_height() >= img.get_width()) { + width = preview_size; + height = img->get_height() * preview_size / img->get_width(); + } else if (img->get_height() > preview_size && img->get_height() >= img->get_width()) { - height=preview_size; - width = img.get_width() * preview_size / img.get_height(); - } else { + height = preview_size; + width = img->get_width() * preview_size / img->get_height(); + } else { - width=img.get_width(); - height=img.get_height(); - } + width = img->get_width(); + height = img->get_height(); + } - img.convert(Image::FORMAT_RGB8); - img.resize(width,height); + img->convert(Image::FORMAT_RGB8); + img->resize(width, height); + img->flip_y(); - String pfile = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp/last_scene_preview.png"); - img.save_png(pfile); - Vector<uint8_t> imgdata = FileAccess::get_file_as_array(pfile); + //save thumbnail directly, as thumbnailer may not update due to actual scene not changing md5 + String temp_path = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp"); + String cache_base = GlobalConfig::get_singleton()->globalize_path(p_file).md5_text(); + cache_base = temp_path.plus_file("resthumb-" + cache_base); - //print_line("img data is "+itos(imgdata.size())); + //does not have it, try to load a cached thumbnail - if (editor_data.get_edited_scene_import_metadata().is_null()) - editor_data.set_edited_scene_import_metadata(Ref<ResourceImportMetadata>( memnew( ResourceImportMetadata ) ) ); - editor_data.get_edited_scene_import_metadata()->set_option("thumbnail",imgdata); -#endif - //tamanio tel thumbnail - if (screen != -1) { - _editor_select(screen); + String file = cache_base + ".png"; + + img->save_png(file); } + save.step(TTR("Saving Scene"), 4); _save_scene(p_file); + EditorResourcePreview::get_singleton()->check_for_invalidation(p_file); } void EditorNode::_save_scene(String p_file, int idx) { @@ -1084,6 +1085,7 @@ void EditorNode::_dialog_action(String p_file) { GlobalConfig::get_singleton()->set("application/main_scene", p_file); GlobalConfig::get_singleton()->save(); //would be nice to show the project manager opened with the highlighted field.. + _run(false, ""); // automatically run the project } break; case FILE_SAVE_OPTIMIZED: { @@ -1982,7 +1984,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) { } _menu_option(FILE_SAVE_AS_SCENE); - _menu_option_confirm(FILE_SAVE_AND_RUN, true); + _menu_option_confirm(FILE_SAVE_AND_RUN, false); } break; case FILE_SAVE_OPTIMIZED: { @@ -3736,7 +3738,7 @@ void EditorNode::_dock_select_input(const Ref<InputEvent> &p_input) { if (me.is_valid()) { - Vector2 point = me->get_pos(); + Vector2 point = me->get_position(); int nrect = -1; for (int i = 0; i < DOCK_SLOT_MAX; i++) { @@ -3846,7 +3848,7 @@ void EditorNode::_dock_select_draw() { unusable.a = 0.1; Rect2 unr(s.x * 2, 0, s.x * 2, s.y * 2); - unr.pos += Vector2(2, 5); + unr.position += Vector2(2, 5); unr.size -= Vector2(4, 7); dock_select->draw_rect(unr, unusable); @@ -3899,7 +3901,7 @@ void EditorNode::_dock_select_draw() { Rect2 r(ofs, s); dock_select_rect[i] = r; - r.pos += Vector2(2, 5); + r.position += Vector2(2, 5); r.size -= Vector2(4, 7); if (i == dock_select_rect_over) { @@ -4878,6 +4880,7 @@ EditorNode::EditorNode() { Resource::_get_local_scene_func = _resource_get_edited_scene; VisualServer::get_singleton()->textures_keep_original(true); + VisualServer::get_singleton()->set_debug_generate_wireframes(true); EditorHelp::generate_doc(); //before any editor classes are crated SceneState::set_disable_placeholders(true); @@ -5874,14 +5877,49 @@ EditorNode::EditorNode() { about->get_ok()->set_text(TTR("Thanks!")); about->set_hide_on_ok(true); gui_base->add_child(about); + VBoxContainer *vbc = memnew(VBoxContainer); HBoxContainer *hbc = memnew(HBoxContainer); - about->add_child(hbc); + hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL); + hbc->set_alignment(BoxContainer::ALIGN_CENTER); + about->add_child(vbc); + vbc->add_child(hbc); Label *about_text = memnew(Label); - about_text->set_text(VERSION_FULL_NAME "\n(c) 2008-2017 Juan Linietsky, Ariel Manzur.\n"); + about_text->set_text(VERSION_FULL_NAME + String::utf8("\n\u00A9 2007-2017 Juan Linietsky, Ariel Manzur.\n\u00A9 2014-2017 ") + TTR("Godot Engine contributors") + "\n"); TextureRect *logo = memnew(TextureRect); logo->set_texture(gui_base->get_icon("Logo", "EditorIcons")); hbc->add_child(logo); hbc->add_child(about_text); + TabContainer *tc = memnew(TabContainer); + tc->set_custom_minimum_size(Vector2(740, 300)); + vbc->add_child(tc); + ScrollContainer *dev_base = memnew(ScrollContainer); + dev_base->set_name(TTR("Developers")); + tc->add_child(dev_base); + HBoxContainer *dev_hbc = memnew(HBoxContainer); + dev_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL); + dev_base->add_child(dev_hbc); + for (int i = 0; i < 3; i++) { + Label *dev_label = memnew(Label); + dev_label->set_h_size_flags(Control::SIZE_EXPAND); + dev_label->set_v_size_flags(Control::SIZE_FILL); + dev_hbc->add_child(dev_label); + } + int dev_name_index = 0; + int dev_name_column = 0; + const int dev_index_max = AUTHORS_COUNT / 3 + (AUTHORS_COUNT % 3 == 0 ? 0 : 1); + String dev_name = ""; + const char **dev_names_ptr = dev_names; + while (*dev_names_ptr) { + dev_name += String::utf8(*dev_names_ptr++); + if (++dev_name_index == dev_index_max || !*dev_names_ptr) { + dev_hbc->get_child(dev_name_column)->cast_to<Label>()->set_text(dev_name); + dev_name_column++; + dev_name = ""; + dev_name_index = 0; + } else { + dev_name += "\n"; + } + } warning = memnew(AcceptDialog); gui_base->add_child(warning); @@ -6020,14 +6058,13 @@ EditorNode::EditorNode() { plugin_init_callbacks[i](); } - /*resource_preview->add_preview_generator( Ref<EditorTexturePreviewPlugin>( memnew(EditorTexturePreviewPlugin ))); - resource_preview->add_preview_generator( Ref<EditorPackedScenePreviewPlugin>( memnew(EditorPackedScenePreviewPlugin ))); - resource_preview->add_preview_generator( Ref<EditorMaterialPreviewPlugin>( memnew(EditorMaterialPreviewPlugin ))); - resource_preview->add_preview_generator( Ref<EditorScriptPreviewPlugin>( memnew(EditorScriptPreviewPlugin ))); - resource_preview->add_preview_generator( Ref<EditorSamplePreviewPlugin>( memnew(EditorSamplePreviewPlugin ))); - resource_preview->add_preview_generator( Ref<EditorMeshPreviewPlugin>( memnew(EditorMeshPreviewPlugin ))); - resource_preview->add_preview_generator( Ref<EditorBitmapPreviewPlugin>( memnew(EditorBitmapPreviewPlugin ))); -*/ + resource_preview->add_preview_generator(Ref<EditorTexturePreviewPlugin>(memnew(EditorTexturePreviewPlugin))); + resource_preview->add_preview_generator(Ref<EditorPackedScenePreviewPlugin>(memnew(EditorPackedScenePreviewPlugin))); + resource_preview->add_preview_generator(Ref<EditorMaterialPreviewPlugin>(memnew(EditorMaterialPreviewPlugin))); + resource_preview->add_preview_generator(Ref<EditorScriptPreviewPlugin>(memnew(EditorScriptPreviewPlugin))); + //resource_preview->add_preview_generator( Ref<EditorSamplePreviewPlugin>( memnew(EditorSamplePreviewPlugin ))); + resource_preview->add_preview_generator(Ref<EditorMeshPreviewPlugin>(memnew(EditorMeshPreviewPlugin))); + resource_preview->add_preview_generator(Ref<EditorBitmapPreviewPlugin>(memnew(EditorBitmapPreviewPlugin))); circle_step_msec = OS::get_singleton()->get_ticks_msec(); circle_step_frame = Engine::get_singleton()->get_frames_drawn(); diff --git a/editor/editor_plugin.cpp b/editor/editor_plugin.cpp index 1b8d1b1677..6fcea98e32 100644 --- a/editor/editor_plugin.cpp +++ b/editor/editor_plugin.cpp @@ -281,6 +281,11 @@ void EditorPlugin::add_import_plugin(const Ref<EditorImportPlugin> &p_importer) EditorFileSystem::get_singleton()->scan_changes(); } +void EditorPlugin::remove_import_plugin(const Ref<EditorImportPlugin> &p_importer) { + ResourceFormatImporter::get_singleton()->remove_importer(p_importer); + EditorFileSystem::get_singleton()->scan_changes(); +} + void EditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) { if (get_script_instance() && get_script_instance()->has_method("set_window_layout")) { @@ -366,6 +371,7 @@ void EditorPlugin::_bind_methods() { ClassDB::bind_method(D_METHOD("queue_save_layout"), &EditorPlugin::queue_save_layout); ClassDB::bind_method(D_METHOD("edit_resource"), &EditorPlugin::edit_resource); ClassDB::bind_method(D_METHOD("add_import_plugin"), &EditorPlugin::add_import_plugin); + ClassDB::bind_method(D_METHOD("remove_import_plugin"), &EditorPlugin::remove_import_plugin); ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_canvas_gui_input", PropertyInfo(Variant::TRANSFORM2D, "canvas_xform"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_draw_over_canvas", PropertyInfo(Variant::TRANSFORM2D, "canvas_xform"), PropertyInfo(Variant::OBJECT, "canvas:Control"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_spatial_gui_input", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"))); diff --git a/editor/editor_plugin.h b/editor/editor_plugin.h index 1ef447a6b8..c607846ec9 100644 --- a/editor/editor_plugin.h +++ b/editor/editor_plugin.h @@ -148,6 +148,7 @@ public: virtual void save_global_state(); void add_import_plugin(const Ref<EditorImportPlugin> &p_importer); + void remove_import_plugin(const Ref<EditorImportPlugin> &p_importer); EditorPlugin(); virtual ~EditorPlugin(); diff --git a/editor/editor_profiler.cpp b/editor/editor_profiler.cpp index 5aad903485..64cf275c3a 100644 --- a/editor/editor_profiler.cpp +++ b/editor/editor_profiler.cpp @@ -496,7 +496,7 @@ void EditorProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) { (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) || (mm.is_valid())) { - int x = me->get_pos().x; + int x = me->get_position().x; x = x * frame_metrics.size() / graph->get_size().width; bool show_hover = x >= 0 && x < frame_metrics.size(); diff --git a/editor/editor_resource_preview.cpp b/editor/editor_resource_preview.cpp index 0b1887e8a2..5d68de3bb6 100644 --- a/editor/editor_resource_preview.cpp +++ b/editor/editor_resource_preview.cpp @@ -190,7 +190,7 @@ void EditorResourcePreview::_thread() { } else { preview_mutex->unlock(); - Ref<Texture> texture; + Ref<ImageTexture> texture; //print_line("pop from queue "+item.path); @@ -229,6 +229,7 @@ void EditorResourcePreview::_thread() { bool cache_valid = true; if (tsize != thumbnail_size) { + cache_valid = false; memdelete(f); } else if (last_modtime != modtime) { @@ -240,6 +241,7 @@ void EditorResourcePreview::_thread() { if (last_md5 != md5) { cache_valid = false; + } else { //update modified time @@ -252,14 +254,20 @@ void EditorResourcePreview::_thread() { memdelete(f); } - cache_valid = false; + //cache_valid = false; if (cache_valid) { - texture = ResourceLoader::load(cache_base + ".png", "ImageTexture", true); - if (!texture.is_valid()) { + Ref<Image> img; + img.instance(); + + if (img->load(cache_base + ".png") != OK) { //well fuck cache_valid = false; + } else { + + texture.instance(); + texture->create_from_image(img, Texture::FLAG_FILTER); } } diff --git a/editor/editor_run.cpp b/editor/editor_run.cpp index e0ebe985cd..c97dc9a88e 100644 --- a/editor/editor_run.cpp +++ b/editor/editor_run.cpp @@ -73,17 +73,17 @@ Error EditorRun::run(const String &p_scene, const String p_custom_args, const Li } Rect2 screen_rect; - screen_rect.pos = OS::get_singleton()->get_screen_position(screen); + screen_rect.position = OS::get_singleton()->get_screen_position(screen); screen_rect.size = OS::get_singleton()->get_screen_size(screen); Size2 desired_size; - desired_size.x = GlobalConfig::get_singleton()->get("display/width"); - desired_size.y = GlobalConfig::get_singleton()->get("display/height"); + desired_size.x = GlobalConfig::get_singleton()->get("display/window/width"); + desired_size.y = GlobalConfig::get_singleton()->get("display/window/height"); Size2 test_size; - test_size.x = GlobalConfig::get_singleton()->get("display/test_width"); - test_size.y = GlobalConfig::get_singleton()->get("display/test_height"); + test_size.x = GlobalConfig::get_singleton()->get("display/window/test_width"); + test_size.y = GlobalConfig::get_singleton()->get("display/window/test_height"); if (test_size.x > 0 && test_size.y > 0) { desired_size = test_size; @@ -95,21 +95,21 @@ Error EditorRun::run(const String &p_scene, const String p_custom_args, const Li case 0: { // default args.push_back("-p"); - args.push_back(itos(screen_rect.pos.x) + "x" + itos(screen_rect.pos.y)); + args.push_back(itos(screen_rect.position.x) + "x" + itos(screen_rect.position.y)); } break; case 1: { // centered - Vector2 pos = screen_rect.pos + ((screen_rect.size - desired_size) / 2).floor(); + Vector2 pos = screen_rect.position + ((screen_rect.size - desired_size) / 2).floor(); args.push_back("-p"); args.push_back(itos(pos.x) + "x" + itos(pos.y)); } break; case 2: { // custom pos Vector2 pos = EditorSettings::get_singleton()->get("run/window_placement/rect_custom_position"); - pos += screen_rect.pos; + pos += screen_rect.position; args.push_back("-p"); args.push_back(itos(pos.x) + "x" + itos(pos.y)); } break; case 3: { // force maximized - Vector2 pos = screen_rect.pos; + Vector2 pos = screen_rect.position; args.push_back("-p"); args.push_back(itos(pos.x) + "x" + itos(pos.y)); args.push_back("-mx"); @@ -117,7 +117,7 @@ Error EditorRun::run(const String &p_scene, const String p_custom_args, const Li } break; case 4: { // force fullscreen - Vector2 pos = screen_rect.pos; + Vector2 pos = screen_rect.position; args.push_back("-p"); args.push_back(itos(pos.x) + "x" + itos(pos.y)); args.push_back("-f"); diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index df12c7c75f..07c7fe97e4 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -556,6 +556,8 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { set("text_editor/line_numbers/line_length_guideline_column", 80); hints["text_editor/line_numbers/line_length_guideline_column"] = PropertyInfo(Variant::INT, "text_editor/line_numbers/line_length_guideline_column", PROPERTY_HINT_RANGE, "20, 160, 10"); + set("text_editor/open_scripts/show_members_overview", true); + set("text_editor/files/trim_trailing_whitespace_on_save", false); set("text_editor/completion/idle_parse_delay", 2); set("text_editor/tools/create_signal_callbacks", true); @@ -612,10 +614,12 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { set("editors/2d/bone_ik_color", Color(0.9, 0.9, 0.45, 0.9)); set("editors/2d/keep_margins_when_changing_anchors", false); set("editors/2d/warped_mouse_panning", true); + set("editors/2d/scroll_to_pan", false); + set("editors/2d/pan_speed", 20); set("editors/poly_editor/point_grab_radius", 8); - set("run/window_placement/rect", 0); + set("run/window_placement/rect", 1); hints["run/window_placement/rect"] = PropertyInfo(Variant::INT, "run/window_placement/rect", PROPERTY_HINT_ENUM, "Default,Centered,Custom Position,Force Maximized,Force Full Screen"); String screen_hints = TTR("Default (Same as Editor)"); for (int i = 0; i < OS::get_singleton()->get_screen_count(); i++) { diff --git a/editor/editor_themes.cpp b/editor/editor_themes.cpp index bf15f43d32..b6952c3024 100644 --- a/editor/editor_themes.cpp +++ b/editor/editor_themes.cpp @@ -145,8 +145,8 @@ Ref<Theme> create_editor_theme() { theme->set_icon("unchecked", "PopupMenu", theme->get_icon("Unchecked", "EditorIcons")); // Editor background - Ref<StyleBoxFlat> style_background = make_flat_stylebox(dark_color_2, 4, 4, 4, 4); - theme->set_stylebox("Background", "EditorStyles", style_background); + Ref<StyleBoxFlat> style_panel = make_flat_stylebox(dark_color_2, 4, 4, 4, 4); + theme->set_stylebox("Background", "EditorStyles", style_panel); // Focus Ref<StyleBoxFlat> focus_sbt = make_flat_stylebox(light_color_1, 4, 4, 4, 4); @@ -193,9 +193,9 @@ Ref<Theme> create_editor_theme() { theme->set_stylebox("MenuHover", "EditorStyles", style_menu_hover_border); // Content of each tab - Ref<StyleBoxFlat> style_panel = make_flat_stylebox(base_color, 1, 4, 1, 1); - theme->set_stylebox("panel", "TabContainer", style_panel); - theme->set_stylebox("Content", "EditorStyles", style_panel); + Ref<StyleBoxFlat> style_content_panel = make_flat_stylebox(base_color, 1, 4, 1, 1); + theme->set_stylebox("panel", "TabContainer", style_content_panel); + theme->set_stylebox("Content", "EditorStyles", style_content_panel); // Button Ref<StyleBoxFlat> style_button = make_flat_stylebox(dark_color_1, 4, 4, 4, 4); @@ -247,6 +247,10 @@ Ref<Theme> create_editor_theme() { theme->set_icon("arrow_collapsed", "Tree", theme->get_icon("TreeArrowRight", "EditorIcons")); theme->set_icon("select_arrow", "Tree", theme->get_icon("Dropdown", "EditorIcons")); theme->set_stylebox("bg_focus", "Tree", focus_sbt); + theme->set_stylebox("custom_button", "Tree", style_button); + theme->set_stylebox("custom_button_pressed", "Tree", style_button); + theme->set_stylebox("custom_button_hover", "Tree", style_button); + theme->set_color("custom_button_font_highlight", "Tree", HIGHLIGHT_COLOR_LIGHT); Ref<StyleBox> style_tree_btn = make_flat_stylebox(light_color_1, 2, 4, 2, 4); theme->set_stylebox("button_pressed", "Tree", style_tree_btn); @@ -277,7 +281,7 @@ Ref<Theme> create_editor_theme() { theme->set_color("drop_position_color", "Tree", highlight_color); // ItemList - Ref<StyleBoxFlat> style_itemlist_cursor = make_flat_stylebox(highlight_color, 8, 8, 8, 8); + Ref<StyleBoxFlat> style_itemlist_cursor = make_flat_stylebox(highlight_color, 4, 4, 4, 4); style_itemlist_cursor->set_draw_center(false); style_itemlist_cursor->set_border_size(1 * EDSCALE); style_itemlist_cursor->set_light_color(light_color_1); @@ -288,6 +292,7 @@ Ref<Theme> create_editor_theme() { theme->set_stylebox("selected", "ItemList", style_tree_selected); theme->set_stylebox("bg_focus", "ItemList", focus_sbt); theme->set_stylebox("bg", "ItemList", style_bg); + theme->set_constant("vseparation", "ItemList", 5 * EDSCALE); Ref<StyleBoxFlat> style_tab_fg = make_flat_stylebox(base_color, 15, 5, 15, 5); Ref<StyleBoxFlat> style_tab_bg = make_flat_stylebox(base_color, 15, 5, 15, 5); @@ -390,6 +395,9 @@ Ref<Theme> create_editor_theme() { theme->set_icon("grabber", "VSlider", theme->get_icon("SliderGrabber", "EditorIcons")); theme->set_icon("grabber_highlight", "VSlider", theme->get_icon("SliderGrabberHl", "EditorIcons")); + // Panel + theme->set_stylebox("panel", "Panel", style_panel); + // TooltipPanel Ref<StyleBoxFlat> style_tooltip = make_flat_stylebox(Color(1, 1, 1, 0.8), 8, 8, 8, 8); style_tooltip->set_border_size(2 * EDSCALE); diff --git a/editor/import/editor_import_collada.cpp b/editor/import/editor_import_collada.cpp index 907c7b045c..e0a2ea624e 100644 --- a/editor/import/editor_import_collada.cpp +++ b/editor/import/editor_import_collada.cpp @@ -72,7 +72,7 @@ struct ColladaImport { Map<String, NodeMap> node_map; //map from collada node to engine node Map<String, String> node_name_map; //map from collada node to engine node - Map<String, Ref<Mesh> > mesh_cache; + Map<String, Ref<ArrayMesh> > mesh_cache; Map<String, Ref<Curve3D> > curve_cache; Map<String, Ref<Material> > material_cache; Map<Collada::Node *, Skeleton *> skeleton_map; @@ -88,7 +88,7 @@ struct ColladaImport { Error _create_scene(Collada::Node *p_node, Spatial *p_parent); Error _create_resources(Collada::Node *p_node); Error _create_material(const String &p_material); - Error _create_mesh_surfaces(bool p_optimize, Ref<Mesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_data, const Collada::MorphControllerData *p_morph_data, Vector<Ref<Mesh> > p_morph_meshes = Vector<Ref<Mesh> >(), bool p_for_morph = false, bool p_use_mesh_material = false); + Error _create_mesh_surfaces(bool p_optimize, Ref<ArrayMesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_data, const Collada::MorphControllerData *p_morph_data, Vector<Ref<ArrayMesh> > p_morph_meshes = Vector<Ref<ArrayMesh> >(), bool p_for_morph = false, bool p_use_mesh_material = false); Error load(const String &p_path, int p_flags, bool p_force_make_tangents = false); void _fix_param_animation_tracks(); void create_animation(int p_clip, bool p_make_tracks_in_all_bones, bool p_import_value_tracks); @@ -591,7 +591,7 @@ static void _generate_tangents_and_binormals(const PoolVector<int> &p_indices, c } } -Error ColladaImport::_create_mesh_surfaces(bool p_optimize, Ref<Mesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<Mesh> > p_morph_meshes, bool p_for_morph, bool p_use_mesh_material) { +Error ColladaImport::_create_mesh_surfaces(bool p_optimize, Ref<ArrayMesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<ArrayMesh> > p_morph_meshes, bool p_for_morph, bool p_use_mesh_material) { bool local_xform_mirror = p_local_xform.basis.determinant() < 0; @@ -1530,7 +1530,7 @@ Error ColladaImport::_create_resources(Collada::Node *p_node) { String meshid; Transform apply_xform; Vector<int> bone_remap; - Vector<Ref<Mesh> > morphs; + Vector<Ref<ArrayMesh> > morphs; print_line("mesh: " + String(mi->get_name())); @@ -1621,9 +1621,9 @@ Error ColladaImport::_create_resources(Collada::Node *p_node) { String meshid = names[i]; if (collada.state.mesh_data_map.has(meshid)) { - Ref<Mesh> mesh = Ref<Mesh>(memnew(Mesh)); + Ref<ArrayMesh> mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid]; - Error err = _create_mesh_surfaces(false, mesh, ng->material_map, meshdata, apply_xform, bone_remap, skin, NULL, Vector<Ref<Mesh> >(), true); + Error err = _create_mesh_surfaces(false, mesh, ng->material_map, meshdata, apply_xform, bone_remap, skin, NULL, Vector<Ref<ArrayMesh> >(), true); ERR_FAIL_COND_V(err, err); morphs.push_back(mesh); @@ -1648,7 +1648,7 @@ Error ColladaImport::_create_resources(Collada::Node *p_node) { meshid = ng->source; } - Ref<Mesh> mesh; + Ref<ArrayMesh> mesh; if (mesh_cache.has(meshid)) { mesh = mesh_cache[meshid]; } else { @@ -1656,7 +1656,7 @@ Error ColladaImport::_create_resources(Collada::Node *p_node) { //bleh, must ignore invalid ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(meshid), ERR_INVALID_DATA); - mesh = Ref<Mesh>(memnew(Mesh)); + mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid]; mesh->set_name(meshdata.name); Error err = _create_mesh_surfaces(morphs.size() == 0, mesh, ng->material_map, meshdata, apply_xform, bone_remap, skin, morph, morphs, false, use_mesh_builtin_materials); diff --git a/editor/import/resource_importer_obj.cpp b/editor/import/resource_importer_obj.cpp index 21c2ae6eb3..342808f9e1 100644 --- a/editor/import/resource_importer_obj.cpp +++ b/editor/import/resource_importer_obj.cpp @@ -54,7 +54,7 @@ String ResourceImporterOBJ::get_save_extension() const { String ResourceImporterOBJ::get_resource_type() const { - return "Mesh"; + return "ArrayMesh"; } bool ResourceImporterOBJ::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const { @@ -89,7 +89,7 @@ Error ResourceImporterOBJ::import(const String &p_source_file, const String &p_s FileAccessRef f = FileAccess::open(p_source_file, FileAccess::READ); ERR_FAIL_COND_V(!f, ERR_CANT_OPEN); - Ref<Mesh> mesh = Ref<Mesh>(memnew(Mesh)); + Ref<ArrayMesh> mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); Map<String, Ref<Material> > name_map; bool generate_normals = p_options["generate/normals"]; diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index 755f4eb219..d2d2d45a47 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -137,7 +137,7 @@ static String _fixstr(const String &p_what, const String &p_str) { return p_what; } -Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh>, Ref<Shape> > &collision_map) { +Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<ArrayMesh>, Ref<Shape> > &collision_map) { // children first.. for (int i = 0; i < p_node->get_child_count(); i++) { @@ -175,7 +175,7 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> mi->set_flag(GeometryInstance::FLAG_BILLBOARD, true); if (mi->get_mesh().is_valid()) { - Ref<Mesh> m = mi->get_mesh(); + Ref<ArrayMesh> m = mi->get_mesh(); for (int i = 0; i < m->get_surface_count(); i++) { Ref<SpatialMaterial> fm = m->surface_get_material(i); @@ -194,7 +194,7 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> MeshInstance *mi = p_node->cast_to<MeshInstance>(); - Ref<Mesh> m = mi->get_mesh(); + Ref<ArrayMesh> m = mi->get_mesh(); if (m.is_valid()) { @@ -275,7 +275,7 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> if (mi->get_mesh().is_valid()) { - Ref<Mesh> m = mi->get_mesh(); + Ref<ArrayMesh> m = mi->get_mesh(); for (int i = 0; i < m->get_surface_count(); i++) { Ref<SpatialMaterial> fm = m->surface_get_material(i); @@ -325,7 +325,7 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> /*if (mi->get_mesh().is_valid()) { - Ref<Mesh> m = mi->get_mesh(); + Ref<ArrayMesh> m = mi->get_mesh(); for(int i=0;i<m->get_surface_count();i++) { Ref<SpatialMaterial> fm = m->surface_get_material(i); @@ -477,7 +477,7 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> MeshInstance *mi = p_node->cast_to<MeshInstance>(); - Ref<Mesh> mesh = mi->get_mesh(); + Ref<ArrayMesh> mesh = mi->get_mesh(); ERR_FAIL_COND_V(mesh.is_null(), NULL); NavigationMeshInstance *nmi = memnew(NavigationMeshInstance); @@ -655,7 +655,7 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> MeshInstance *mi = p_node->cast_to<MeshInstance>(); - Ref<Mesh> mesh = mi->get_mesh(); + Ref<ArrayMesh> mesh = mi->get_mesh(); if (!mesh.is_null()) { if (_teststr(mesh->get_name(), "col")) { @@ -972,7 +972,7 @@ static String _make_extname(const String &p_str) { return ext_name; } -void ResourceImporterScene::_make_external_resources(Node *p_node, const String &p_base_path, bool p_make_materials, bool p_make_meshes, Map<Ref<Material>, Ref<Material> > &p_materials, Map<Ref<Mesh>, Ref<Mesh> > &p_meshes) { +void ResourceImporterScene::_make_external_resources(Node *p_node, const String &p_base_path, bool p_make_materials, bool p_make_meshes, Map<Ref<Material>, Ref<Material> > &p_materials, Map<Ref<ArrayMesh>, Ref<ArrayMesh> > &p_meshes) { List<PropertyInfo> pi; @@ -1005,7 +1005,7 @@ void ResourceImporterScene::_make_external_resources(Node *p_node, const String } } else { - Ref<Mesh> mesh = p_node->get(E->get().name); + Ref<ArrayMesh> mesh = p_node->get(E->get().name); if (mesh.is_valid()) { @@ -1018,7 +1018,7 @@ void ResourceImporterScene::_make_external_resources(Node *p_node, const String String ext_name = p_base_path + "." + _make_extname(mesh->get_name()) + ".msh"; if (FileAccess::exists(ext_name)) { //if exists, use it - Ref<Mesh> existing = ResourceLoader::load(ext_name); + Ref<ArrayMesh> existing = ResourceLoader::load(ext_name); p_meshes[mesh] = existing; } else { @@ -1059,7 +1059,7 @@ void ResourceImporterScene::_make_external_resources(Node *p_node, const String } if (!p_make_meshes) { - p_meshes[mesh] = Ref<Mesh>(); //save it anyway, so it won't be checked again + p_meshes[mesh] = Ref<ArrayMesh>(); //save it anyway, so it won't be checked again } } } @@ -1192,7 +1192,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p float anim_optimizer_angerr = p_options["animation/optimizer/max_angular_error"]; float anim_optimizer_maxang = p_options["animation/optimizer/max_angle"]; - Map<Ref<Mesh>, Ref<Shape> > collision_map; + Map<Ref<ArrayMesh>, Ref<Shape> > collision_map; scene = _fix_node(scene, scene, collision_map); @@ -1230,7 +1230,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p if (external_materials || external_meshes) { Map<Ref<Material>, Ref<Material> > mat_map; - Map<Ref<Mesh>, Ref<Mesh> > mesh_map; + Map<Ref<ArrayMesh>, Ref<ArrayMesh> > mesh_map; _make_external_resources(scene, p_source_file.get_basename(), external_materials, external_meshes, mat_map, mesh_map); } diff --git a/editor/import/resource_importer_scene.h b/editor/import/resource_importer_scene.h index 9f7b1a84e6..ede3028b29 100644 --- a/editor/import/resource_importer_scene.h +++ b/editor/import/resource_importer_scene.h @@ -32,6 +32,7 @@ #include "io/resource_import.h" #include "scene/resources/animation.h" +#include "scene/resources/mesh.h" #include "scene/resources/shape.h" class Material; @@ -100,9 +101,9 @@ public: virtual void get_import_options(List<ImportOption> *r_options, int p_preset = 0) const; virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const; - void _make_external_resources(Node *p_node, const String &p_base_path, bool p_make_materials, bool p_make_meshes, Map<Ref<Material>, Ref<Material> > &p_materials, Map<Ref<Mesh>, Ref<Mesh> > &p_meshes); + void _make_external_resources(Node *p_node, const String &p_base_path, bool p_make_materials, bool p_make_meshes, Map<Ref<Material>, Ref<Material> > &p_materials, Map<Ref<ArrayMesh>, Ref<ArrayMesh> > &p_meshes); - Node *_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh>, Ref<Shape> > &collision_map); + Node *_fix_node(Node *p_node, Node *p_root, Map<Ref<ArrayMesh>, Ref<Shape> > &collision_map); void _create_clips(Node *scene, const Array &p_clips, bool p_bake_all); void _filter_anim_tracks(Ref<Animation> anim, Set<String> &keep); diff --git a/editor/pane_drag.h b/editor/pane_drag.h index 7bd9feb63b..0be017b8f7 100644 --- a/editor/pane_drag.h +++ b/editor/pane_drag.h @@ -34,7 +34,7 @@ class PaneDrag : public Control { - GDCLASS(PaneDrag, Control); + GDCLASS(PaneDrag, Control) bool mouse_over; diff --git a/editor/plugins/animation_tree_editor_plugin.cpp b/editor/plugins/animation_tree_editor_plugin.cpp index fccd527ed6..d67832e10b 100644 --- a/editor/plugins/animation_tree_editor_plugin.cpp +++ b/editor/plugins/animation_tree_editor_plugin.cpp @@ -744,7 +744,7 @@ void AnimationTreeEditor::_gui_input(Ref<InputEvent> p_event) { if (mb->is_pressed()) { if (mb->get_button_index() == 1) { - click_pos = Point2(mb->get_pos().x, mb->get_pos().y); + click_pos = Point2(mb->get_position().x, mb->get_position().y); click_motion = click_pos; click_type = _locate_click(click_pos, &click_node, &click_slot); if (click_type != CLICK_NONE) { @@ -780,7 +780,7 @@ void AnimationTreeEditor::_gui_input(Ref<InputEvent> p_event) { } else { // try to disconnect/remove - Point2 rclick_pos = Point2(mb->get_pos().x, mb->get_pos().y); + Point2 rclick_pos = Point2(mb->get_position().x, mb->get_position().y); rclick_type = _locate_click(rclick_pos, &rclick_node, &rclick_slot); if (rclick_type == CLICK_INPUT_SLOT || rclick_type == CLICK_OUTPUT_SLOT) { @@ -820,7 +820,7 @@ void AnimationTreeEditor::_gui_input(Ref<InputEvent> p_event) { case CLICK_INPUT_SLOT: case CLICK_OUTPUT_SLOT: { - Point2 dst_click_pos = Point2(mb->get_pos().x, mb->get_pos().y); + Point2 dst_click_pos = Point2(mb->get_position().x, mb->get_position().y); StringName id; int slot; ClickType dst_click_type = _locate_click(dst_click_pos, &id, &slot); @@ -859,7 +859,7 @@ void AnimationTreeEditor::_gui_input(Ref<InputEvent> p_event) { if (mm->get_button_mask() & 1 && click_type != CLICK_NONE) { - click_motion = Point2(mm->get_pos().x, mm->get_pos().y); + click_motion = Point2(mm->get_position().x, mm->get_position().y); update(); } if ((mm->get_button_mask() & 4 || Input::get_singleton()->is_key_pressed(KEY_SPACE))) { @@ -876,7 +876,7 @@ void AnimationTreeEditor::_draw_cos_line(const Vector2 &p_from, const Vector2 &p static const int steps = 20; Rect2 r; - r.pos = p_from; + r.position = p_from; r.expand_to(p_to); Vector2 sign = Vector2((p_from.x < p_to.x) ? 1 : -1, (p_from.y < p_to.y) ? 1 : -1); bool flip = sign.x * sign.y < 0; @@ -888,7 +888,7 @@ void AnimationTreeEditor::_draw_cos_line(const Vector2 &p_from, const Vector2 &p float c = -Math::cos(d * Math_PI) * 0.5 + 0.5; if (flip) c = 1.0 - c; - Vector2 p = r.pos + Vector2(d * r.size.width, c * r.size.height); + Vector2 p = r.position + Vector2(d * r.size.width, c * r.size.height); if (i > 0) { diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index a626dffc3c..803e59469c 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -536,10 +536,10 @@ void CanvasItemEditor::_find_canvas_items_at_rect(const Rect2 &p_rect, Node *p_n Rect2 rect = c->get_item_rect(); Transform2D xform = p_parent_xform * p_canvas_xform * c->get_transform(); - if (p_rect.has_point(xform.xform(rect.pos)) && - p_rect.has_point(xform.xform(rect.pos + Vector2(rect.size.x, 0))) && - p_rect.has_point(xform.xform(rect.pos + Vector2(rect.size.x, rect.size.y))) && - p_rect.has_point(xform.xform(rect.pos + Vector2(0, rect.size.y)))) { + if (p_rect.has_point(xform.xform(rect.position)) && + p_rect.has_point(xform.xform(rect.position + Vector2(rect.size.x, 0))) && + p_rect.has_point(xform.xform(rect.position + Vector2(rect.size.x, rect.size.y))) && + p_rect.has_point(xform.xform(rect.position + Vector2(0, rect.size.y)))) { r_items->push_back(c); } @@ -652,7 +652,7 @@ void CanvasItemEditor::_key_move(const Vector2 &p_dir, bool p_snap, KeyMoveMODE // drag = transform.affine_inverse().basis_xform(p_dir); // zoom sensitive drag = canvas_item->get_global_transform_with_canvas().affine_inverse().basis_xform(drag); Rect2 local_rect = canvas_item->get_item_rect(); - local_rect.pos += drag; + local_rect.position += drag; undo_redo->add_do_method(canvas_item, "edit_set_rect", local_rect); } else { // p_move_mode==MOVE_LOCAL_BASE || p_move_mode==MOVE_LOCAL_WITH_ROT @@ -680,7 +680,7 @@ Point2 CanvasItemEditor::_find_topleftmost_point() { Vector2 tl = Point2(1e10, 1e10); Rect2 r2; - r2.pos = tl; + r2.position = tl; List<Node *> &selection = editor_selection->get_selected_node_list(); @@ -695,13 +695,13 @@ Point2 CanvasItemEditor::_find_topleftmost_point() { Rect2 rect = canvas_item->get_item_rect(); Transform2D xform = canvas_item->get_global_transform_with_canvas(); - r2.expand_to(xform.xform(rect.pos)); - r2.expand_to(xform.xform(rect.pos + Vector2(rect.size.x, 0))); - r2.expand_to(xform.xform(rect.pos + rect.size)); - r2.expand_to(xform.xform(rect.pos + Vector2(0, rect.size.y))); + r2.expand_to(xform.xform(rect.position)); + r2.expand_to(xform.xform(rect.position + Vector2(rect.size.x, 0))); + r2.expand_to(xform.xform(rect.position + rect.size)); + r2.expand_to(xform.xform(rect.position + Vector2(0, rect.size.y))); } - return r2.pos; + return r2.position; } int CanvasItemEditor::get_item_count() { @@ -759,18 +759,18 @@ CanvasItemEditor::DragType CanvasItemEditor::_find_drag_type(const Transform2D & Vector2 endpoints[4] = { - xform.xform(rect.pos), - xform.xform(rect.pos + Vector2(rect.size.x, 0)), - xform.xform(rect.pos + rect.size), - xform.xform(rect.pos + Vector2(0, rect.size.y)) + xform.xform(rect.position), + xform.xform(rect.position + Vector2(rect.size.x, 0)), + xform.xform(rect.position + rect.size), + xform.xform(rect.position + Vector2(0, rect.size.y)) }; Vector2 endpointsl[4] = { - xforml.xform(rect.pos), - xforml.xform(rect.pos + Vector2(rect.size.x, 0)), - xforml.xform(rect.pos + rect.size), - xforml.xform(rect.pos + Vector2(0, rect.size.y)) + xforml.xform(rect.position), + xforml.xform(rect.position + Vector2(rect.size.x, 0)), + xforml.xform(rect.position + rect.size), + xforml.xform(rect.position + Vector2(0, rect.size.y)) }; DragType dragger[] = { @@ -962,7 +962,7 @@ bool CanvasItemEditor::get_remove_list(List<Node *> *p_list) { void CanvasItemEditor::_list_select(const Ref<InputEventMouseButton> &b) { - Point2 click = b->get_pos(); + Point2 click = b->get_position(); Node *scene = editor->get_edited_scene(); if (!scene) @@ -1010,13 +1010,12 @@ void CanvasItemEditor::_list_select(const Ref<InputEventMouseButton> &b) { selection_menu->add_item(item->get_name()); selection_menu->set_item_icon(i, icon); selection_menu->set_item_metadata(i, node_path); - selection_menu->set_item_tooltip(i, String(item->get_name()) + - "\nType: " + item->get_class() + "\nPath: " + node_path); + selection_menu->set_item_tooltip(i, String(item->get_name()) + "\nType: " + item->get_class() + "\nPath: " + node_path); } additive_selection = b->get_shift(); - selection_menu->set_global_position(b->get_global_pos()); + selection_menu->set_global_position(b->get_global_position()); selection_menu->popup(); selection_menu->call_deferred("grab_click_focus"); selection_menu->set_invalidate_click_until_motion(); @@ -1047,17 +1046,25 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { if (b->get_button_index() == BUTTON_WHEEL_DOWN) { - if (zoom < MIN_ZOOM) - return; + if (bool(EditorSettings::get_singleton()->get("editors/2d/scroll_to_pan"))) { + + v_scroll->set_value(v_scroll->get_value() + int(EditorSettings::get_singleton()->get("editors/2d/pan_speed")) / zoom * b->get_factor()); + + } else { + + if (zoom < MIN_ZOOM) + return; - float prev_zoom = zoom; - zoom = zoom * (1 - (0.05 * b->get_factor())); - { - Point2 ofs = b->get_pos(); - ofs = ofs / prev_zoom - ofs / zoom; - h_scroll->set_value(h_scroll->get_value() + ofs.x); - v_scroll->set_value(v_scroll->get_value() + ofs.y); + float prev_zoom = zoom; + zoom = zoom * (1 - (0.05 * b->get_factor())); + { + Point2 ofs = b->get_position(); + ofs = ofs / prev_zoom - ofs / zoom; + h_scroll->set_value(h_scroll->get_value() + ofs.x); + v_scroll->set_value(v_scroll->get_value() + ofs.y); + } } + _update_scroll(0); viewport->update(); return; @@ -1065,16 +1072,21 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { if (b->get_button_index() == BUTTON_WHEEL_UP) { - if (zoom > MAX_ZOOM) - return; + if (bool(EditorSettings::get_singleton()->get("editors/2d/scroll_to_pan"))) { + + v_scroll->set_value(v_scroll->get_value() - int(EditorSettings::get_singleton()->get("editors/2d/pan_speed")) / zoom * b->get_factor()); - float prev_zoom = zoom; - zoom = zoom * ((0.95 + (0.05 * b->get_factor())) / 0.95); - { - Point2 ofs = b->get_pos(); - ofs = ofs / prev_zoom - ofs / zoom; - h_scroll->set_value(h_scroll->get_value() + ofs.x); - v_scroll->set_value(v_scroll->get_value() + ofs.y); + } else { + if (zoom > MAX_ZOOM) return; + + float prev_zoom = zoom; + zoom = zoom * ((0.95 + (0.05 * b->get_factor())) / 0.95); + { + Point2 ofs = b->get_position(); + ofs = ofs / prev_zoom - ofs / zoom; + h_scroll->set_value(h_scroll->get_value() + ofs.x); + v_scroll->set_value(v_scroll->get_value() + ofs.y); + } } _update_scroll(0); @@ -1082,6 +1094,22 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { return; } + if (b->get_button_index() == BUTTON_WHEEL_LEFT) { + + if (bool(EditorSettings::get_singleton()->get("editors/2d/scroll_to_pan"))) { + + h_scroll->set_value(h_scroll->get_value() - int(EditorSettings::get_singleton()->get("editors/2d/pan_speed")) / zoom * b->get_factor()); + } + } + + if (b->get_button_index() == BUTTON_WHEEL_RIGHT) { + + if (bool(EditorSettings::get_singleton()->get("editors/2d/scroll_to_pan"))) { + + h_scroll->set_value(h_scroll->get_value() + int(EditorSettings::get_singleton()->get("editors/2d/pan_speed")) / zoom * b->get_factor()); + } + } + if (b->get_button_index() == BUTTON_RIGHT) { if (b->is_pressed() && (tool == TOOL_SELECT && b->get_alt())) { @@ -1156,7 +1184,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { if (b->get_button_index() == BUTTON_LEFT && tool == TOOL_EDIT_PIVOT) { if (b->is_pressed()) { - Point2 mouse_pos = b->get_pos(); + Point2 mouse_pos = b->get_position(); mouse_pos = transform.affine_inverse().xform(mouse_pos); mouse_pos = snap_point(mouse_pos); _edit_set_pivot(mouse_pos); @@ -1274,8 +1302,8 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { E->get().to }; - Vector2 p = Geometry::get_closest_point_to_segment_2d(b->get_pos(), s); - float d = p.distance_to(b->get_pos()); + Vector2 p = Geometry::get_closest_point_to_segment_2d(b->get_position(), s); + float d = p.distance_to(b->get_position()); if (d < bone_width && d < closest_dist) { Cbone = E; closest_dist = d; @@ -1342,7 +1370,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item); ERR_FAIL_COND(!se); - Point2 click = b->get_pos(); + Point2 click = b->get_position(); if ((b->get_control() && tool == TOOL_SELECT) || tool == TOOL_ROTATE) { @@ -1388,7 +1416,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { //multi canvas_item edit - Point2 click = b->get_pos(); + Point2 click = b->get_position(); if ((b->get_alt() || tool == TOOL_MOVE) && get_item_count()) { _prepare_drag(click); @@ -1451,7 +1479,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { if (box_selecting) { - box_selecting_to = transform.affine_inverse().xform(m->get_pos()); + box_selecting_to = transform.affine_inverse().xform(m->get_position()); viewport->update(); return; } @@ -1497,7 +1525,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { } Vector2 dfrom = drag_from; - Vector2 dto = transform.affine_inverse().xform(m->get_pos()); + Vector2 dto = transform.affine_inverse().xform(m->get_position()); if (canvas_item->has_meta("_edit_lock_")) continue; @@ -1552,8 +1580,8 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { canvas_item->get_global_transform_with_canvas().affine_inverse().xform(dfrom); Rect2 local_rect = canvas_item->get_item_rect(); - Vector2 begin = local_rect.pos; - Vector2 end = local_rect.pos + local_rect.size; + Vector2 begin = local_rect.position; + Vector2 end = local_rect.position + local_rect.size; Vector2 minsize = canvas_item->edit_get_minimum_size(); if (uniform) { @@ -1645,7 +1673,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { if (!dragging_bone) { - local_rect.pos = begin; + local_rect.position = begin; local_rect.size = end - begin; canvas_item->edit_set_rect(local_rect); @@ -1868,10 +1896,10 @@ void CanvasItemEditor::_viewport_draw() { Vector2 endpoints[4] = { - xform.xform(rect.pos), - xform.xform(rect.pos + Vector2(rect.size.x, 0)), - xform.xform(rect.pos + rect.size), - xform.xform(rect.pos + Vector2(0, rect.size.y)) + xform.xform(rect.position), + xform.xform(rect.position + Vector2(rect.size.x, 0)), + xform.xform(rect.position + rect.size), + xform.xform(rect.position + Vector2(0, rect.size.y)) }; Color c = Color(1, 0.6, 0.4, 0.7); @@ -2212,7 +2240,7 @@ void CanvasItemEditor::_find_canvas_items_span(Node *p_node, Rect2 &r_rect, cons lock.group = c->has_meta("_edit_group_"); if (lock.group || lock.lock) { - lock.pos = xform.xform(rect.pos); + lock.pos = xform.xform(rect.position); lock_list.push_back(lock); } @@ -2228,10 +2256,10 @@ void CanvasItemEditor::_find_canvas_items_span(Node *p_node, Rect2 &r_rect, cons bone_list[id].last_pass = bone_last_frame; } - r_rect.expand_to(xform.xform(rect.pos)); - r_rect.expand_to(xform.xform(rect.pos + Point2(rect.size.x, 0))); - r_rect.expand_to(xform.xform(rect.pos + Point2(0, rect.size.y))); - r_rect.expand_to(xform.xform(rect.pos + rect.size)); + r_rect.expand_to(xform.xform(rect.position)); + r_rect.expand_to(xform.xform(rect.position + Point2(rect.size.x, 0))); + r_rect.expand_to(xform.xform(rect.position + Point2(0, rect.size.y))); + r_rect.expand_to(xform.xform(rect.position + rect.size)); } } @@ -2277,19 +2305,19 @@ void CanvasItemEditor::_update_scrollbars() { //expand area so it's easier to do animations and stuff at 0,0 canvas_item_rect.size += screen_rect * 2; - canvas_item_rect.pos -= screen_rect; + canvas_item_rect.position -= screen_rect; Point2 ofs; if (canvas_item_rect.size.height <= (local_rect.size.y / zoom)) { v_scroll->hide(); - ofs.y = canvas_item_rect.pos.y; + ofs.y = canvas_item_rect.position.y; } else { v_scroll->show(); - v_scroll->set_min(canvas_item_rect.pos.y); - v_scroll->set_max(canvas_item_rect.pos.y + canvas_item_rect.size.y); + v_scroll->set_min(canvas_item_rect.position.y); + v_scroll->set_max(canvas_item_rect.position.y + canvas_item_rect.size.y); v_scroll->set_page(local_rect.size.y / zoom); if (first_update) { //so 0,0 is visible @@ -2304,12 +2332,12 @@ void CanvasItemEditor::_update_scrollbars() { if (canvas_item_rect.size.width <= (local_rect.size.x / zoom)) { h_scroll->hide(); - ofs.x = canvas_item_rect.pos.x; + ofs.x = canvas_item_rect.position.x; } else { h_scroll->show(); - h_scroll->set_min(canvas_item_rect.pos.x); - h_scroll->set_max(canvas_item_rect.pos.x + canvas_item_rect.size.x); + h_scroll->set_min(canvas_item_rect.position.x); + h_scroll->set_max(canvas_item_rect.position.x + canvas_item_rect.size.x); h_scroll->set_page(local_rect.size.x / zoom); ofs.x = h_scroll->get_value(); } @@ -2977,7 +3005,7 @@ void CanvasItemEditor::_focus_selection(int p_op) { Transform2D t(angle, Vector2(0.f, 0.f)); item_rect = t.xform(item_rect); - Rect2 canvas_item_rect(pos + scale * item_rect.pos, scale * item_rect.size); + Rect2 canvas_item_rect(pos + scale * item_rect.position, scale * item_rect.size); if (count == 1) { rect = canvas_item_rect; } else { @@ -2988,7 +3016,7 @@ void CanvasItemEditor::_focus_selection(int p_op) { if (p_op == VIEW_CENTER_TO_SELECTION) { - center = rect.pos + rect.size / 2; + center = rect.position + rect.size / 2; Vector2 offset = viewport->get_size() / 2 - editor->get_scene_root()->get_global_canvas_transform().xform(center); h_scroll->set_value(h_scroll->get_value() - offset.x / zoom); v_scroll->set_value(v_scroll->get_value() - offset.y / zoom); diff --git a/editor/plugins/collision_polygon_2d_editor_plugin.cpp b/editor/plugins/collision_polygon_2d_editor_plugin.cpp index b7cfcaae02..43abea0131 100644 --- a/editor/plugins/collision_polygon_2d_editor_plugin.cpp +++ b/editor/plugins/collision_polygon_2d_editor_plugin.cpp @@ -104,7 +104,7 @@ bool CollisionPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) if (mb.is_valid()) { Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); - Vector2 gpoint = mb->get_pos(); + Vector2 gpoint = mb->get_position(); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint = canvas_item_editor->snap_point(cpoint); cpoint = node->get_global_transform().affine_inverse().xform(cpoint); @@ -289,7 +289,7 @@ bool CollisionPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) if (edited_point != -1 && (wip_active || mm->get_button_mask() & BUTTON_MASK_LEFT)) { - Vector2 gpoint = mm->get_pos(); + Vector2 gpoint = mm->get_position(); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint = canvas_item_editor->snap_point(cpoint); edited_point_pos = node->get_global_transform().affine_inverse().xform(cpoint); diff --git a/editor/plugins/collision_shape_2d_editor_plugin.cpp b/editor/plugins/collision_shape_2d_editor_plugin.cpp index e2184c6158..9a6ee8153e 100644 --- a/editor/plugins/collision_shape_2d_editor_plugin.cpp +++ b/editor/plugins/collision_shape_2d_editor_plugin.cpp @@ -322,7 +322,7 @@ bool CollisionShape2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); - Point2 gpoint(mb->get_pos().x, mb->get_pos().y); + Point2 gpoint(mb->get_position().x, mb->get_position().y); if (mb->get_button_index() == BUTTON_LEFT) { if (mb->is_pressed()) { @@ -368,7 +368,7 @@ bool CollisionShape2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { return false; } - Point2 gpoint = mm->get_pos(); + Point2 gpoint = mm->get_position(); Point2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint = canvas_item_editor->snap_point(cpoint); cpoint = node->get_global_transform().affine_inverse().xform(cpoint); diff --git a/editor/plugins/curve_editor_plugin.cpp b/editor/plugins/curve_editor_plugin.cpp index 6dd94863a1..d869d703f1 100644 --- a/editor/plugins/curve_editor_plugin.cpp +++ b/editor/plugins/curve_editor_plugin.cpp @@ -57,7 +57,7 @@ void CurveTextureEdit::_gui_input(const Ref<InputEvent> &p_event) { Vector2 size = get_size(); size.y -= font_h; - Point2 p = Vector2(mb->get_pos().x, mb->get_pos().y) / size; + Point2 p = Vector2(mb->get_position().x, mb->get_position().y) / size; p.y = CLAMP(1.0 - p.y, 0, 1) * (max - min) + min; grabbed = -1; grabbing = true; @@ -111,7 +111,7 @@ void CurveTextureEdit::_gui_input(const Ref<InputEvent> &p_event) { Vector2 size = get_size(); size.y -= font_h; - Point2 p = mm->get_pos() / size; + Point2 p = mm->get_position() / size; p.y = CLAMP(1.0 - p.y, 0, 1) * (max - min) + min; p.x = CLAMP(p.x, 0.0, 1.0); diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp index db8060d591..7f8b98e1d3 100644 --- a/editor/plugins/editor_preview_plugins.cpp +++ b/editor/plugins/editor_preview_plugins.cpp @@ -39,239 +39,226 @@ #include "scene/resources/bit_mask.h" #include "scene/resources/mesh.h" -#if 0 -bool EditorTexturePreviewPlugin::handles(const String& p_type) const { +bool EditorTexturePreviewPlugin::handles(const String &p_type) const { - return (ClassDB::is_type(p_type,"ImageTexture") || ClassDB::is_type(p_type, "AtlasTexture")); + return ClassDB::is_parent_class(p_type, "Texture"); } -Ref<Texture> EditorTexturePreviewPlugin::generate(const RES& p_from) { +Ref<Texture> EditorTexturePreviewPlugin::generate(const RES &p_from) { - Image img; + Ref<Image> img; Ref<AtlasTexture> atex = p_from; if (atex.is_valid()) { - Ref<ImageTexture> tex = atex->get_atlas(); + Ref<Texture> tex = atex->get_atlas(); if (!tex.is_valid()) { return Ref<Texture>(); } - Image atlas = tex->get_data(); - img = atlas.get_rect(atex->get_region()); - } - else { - Ref<ImageTexture> tex = p_from; + Ref<Image> atlas = tex->get_data(); + img = atlas->get_rect(atex->get_region()); + } else { + Ref<Texture> tex = p_from; img = tex->get_data(); } - if (img.empty()) + if (img.is_null() || img->empty()) return Ref<Texture>(); - img.clear_mipmaps(); + img->clear_mipmaps(); int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); - thumbnail_size*=EDSCALE; - if (img.is_compressed()) { - if (img.decompress()!=OK) + thumbnail_size *= EDSCALE; + if (img->is_compressed()) { + if (img->decompress() != OK) return Ref<Texture>(); - } else if (img.get_format()!=Image::FORMAT_RGB8 && img.get_format()!=Image::FORMAT_RGBA8) { - img.convert(Image::FORMAT_RGBA8); + } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) { + img->convert(Image::FORMAT_RGBA8); } - int width,height; - if (img.get_width() > thumbnail_size && img.get_width() >= img.get_height()) { + int width, height; + if (img->get_width() > thumbnail_size && img->get_width() >= img->get_height()) { - width=thumbnail_size; - height = img.get_height() * thumbnail_size / img.get_width(); - } else if (img.get_height() > thumbnail_size && img.get_height() >= img.get_width()) { + width = thumbnail_size; + height = img->get_height() * thumbnail_size / img->get_width(); + } else if (img->get_height() > thumbnail_size && img->get_height() >= img->get_width()) { - height=thumbnail_size; - width = img.get_width() * thumbnail_size / img.get_height(); - } else { + height = thumbnail_size; + width = img->get_width() * thumbnail_size / img->get_height(); + } else { - width=img.get_width(); - height=img.get_height(); + width = img->get_width(); + height = img->get_height(); } - img.resize(width,height); + img->resize(width, height); - Ref<ImageTexture> ptex = Ref<ImageTexture>( memnew( ImageTexture )); + Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture)); - ptex->create_from_image(img,0); + ptex->create_from_image(img, 0); return ptex; - } EditorTexturePreviewPlugin::EditorTexturePreviewPlugin() { - - } //////////////////////////////////////////////////////////////////////////// -bool EditorBitmapPreviewPlugin::handles(const String& p_type) const { +bool EditorBitmapPreviewPlugin::handles(const String &p_type) const { - return ClassDB::is_type(p_type,"BitMap"); + return ClassDB::is_parent_class(p_type, "BitMap"); } -Ref<Texture> EditorBitmapPreviewPlugin::generate(const RES& p_from) { +Ref<Texture> EditorBitmapPreviewPlugin::generate(const RES &p_from) { - Ref<BitMap> bm =p_from; + Ref<BitMap> bm = p_from; - if (bm->get_size()==Size2()) { + if (bm->get_size() == Size2()) { return Ref<Texture>(); } PoolVector<uint8_t> data; - data.resize(bm->get_size().width*bm->get_size().height); + data.resize(bm->get_size().width * bm->get_size().height); { - PoolVector<uint8_t>::Write w=data.write(); + PoolVector<uint8_t>::Write w = data.write(); - for(int i=0;i<bm->get_size().width;i++) { - for(int j=0;j<bm->get_size().height;j++) { - if (bm->get_bit(Point2i(i,j))) { - w[j*bm->get_size().width+i]=255; + for (int i = 0; i < bm->get_size().width; i++) { + for (int j = 0; j < bm->get_size().height; j++) { + if (bm->get_bit(Point2i(i, j))) { + w[j * bm->get_size().width + i] = 255; } else { - w[j*bm->get_size().width+i]=0; - + w[j * bm->get_size().width + i] = 0; } } - } } - - Image img(bm->get_size().width,bm->get_size().height,0,Image::FORMAT_L8,data); + Ref<Image> img; + img.instance(); + img->create(bm->get_size().width, bm->get_size().height, 0, Image::FORMAT_L8, data); int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); - thumbnail_size*=EDSCALE; - if (img.is_compressed()) { - if (img.decompress()!=OK) + thumbnail_size *= EDSCALE; + if (img->is_compressed()) { + if (img->decompress() != OK) return Ref<Texture>(); - } else if (img.get_format()!=Image::FORMAT_RGB8 && img.get_format()!=Image::FORMAT_RGBA8) { - img.convert(Image::FORMAT_RGBA8); + } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) { + img->convert(Image::FORMAT_RGBA8); } - int width,height; - if (img.get_width() > thumbnail_size && img.get_width() >= img.get_height()) { + int width, height; + if (img->get_width() > thumbnail_size && img->get_width() >= img->get_height()) { - width=thumbnail_size; - height = img.get_height() * thumbnail_size / img.get_width(); - } else if (img.get_height() > thumbnail_size && img.get_height() >= img.get_width()) { + width = thumbnail_size; + height = img->get_height() * thumbnail_size / img->get_width(); + } else if (img->get_height() > thumbnail_size && img->get_height() >= img->get_width()) { - height=thumbnail_size; - width = img.get_width() * thumbnail_size / img.get_height(); - } else { + height = thumbnail_size; + width = img->get_width() * thumbnail_size / img->get_height(); + } else { - width=img.get_width(); - height=img.get_height(); + width = img->get_width(); + height = img->get_height(); } - img.resize(width,height); + img->resize(width, height); - Ref<ImageTexture> ptex = Ref<ImageTexture>( memnew( ImageTexture )); + Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture)); - ptex->create_from_image(img,0); + ptex->create_from_image(img, 0); return ptex; - } EditorBitmapPreviewPlugin::EditorBitmapPreviewPlugin() { - - } /////////////////////////////////////////////////////////////////////////// +bool EditorPackedScenePreviewPlugin::handles(const String &p_type) const { -Ref<Texture> EditorPackedScenePreviewPlugin::_gen_from_imd(Ref<ResourceImportMetadata> p_imd) { - - if (p_imd.is_null()) { - return Ref<Texture>(); - } + return ClassDB::is_parent_class(p_type, "PackedScene"); +} +Ref<Texture> EditorPackedScenePreviewPlugin::generate(const RES &p_from) { - if (!p_imd->has_option("thumbnail")) - return Ref<Texture>(); + return generate_from_path(p_from->get_path()); +} - Variant tn = p_imd->get_option("thumbnail"); - //print_line(Variant::get_type_name(tn.get_type())); - PoolVector<uint8_t> thumbnail = tn; +Ref<Texture> EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path) { - int len = thumbnail.size(); - if (len==0) - return Ref<Texture>(); + String temp_path = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp"); + String cache_base = GlobalConfig::get_singleton()->globalize_path(p_path).md5_text(); + cache_base = temp_path.plus_file("resthumb-" + cache_base); + //does not have it, try to load a cached thumbnail - PoolVector<uint8_t>::Read r = thumbnail.read(); + String path = cache_base + ".png"; - Image img(r.ptr(),len); - if (img.empty()) + if (!FileAccess::exists(path)) return Ref<Texture>(); - Ref<ImageTexture> ptex = Ref<ImageTexture>( memnew( ImageTexture )); - ptex->create_from_image(img,0); - return ptex; + Ref<Image> img; + img.instance(); + Error err = img->load(path); + if (err == OK) { -} + Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture)); -bool EditorPackedScenePreviewPlugin::handles(const String& p_type) const { + ptex->create_from_image(img, 0); + return ptex; - return ClassDB::is_type(p_type,"PackedScene"); + } else { + return Ref<Texture>(); + } } -Ref<Texture> EditorPackedScenePreviewPlugin::generate(const RES& p_from) { - Ref<ResourceImportMetadata> imd = p_from->get_import_metadata(); - return _gen_from_imd(imd); +EditorPackedScenePreviewPlugin::EditorPackedScenePreviewPlugin() { } -Ref<Texture> EditorPackedScenePreviewPlugin::generate_from_path(const String& p_path) { +////////////////////////////////////////////////////////////////// + +void EditorMaterialPreviewPlugin::_preview_done(const Variant &p_udata) { - Ref<ResourceImportMetadata> imd = ResourceLoader::load_import_metadata(p_path); - return _gen_from_imd(imd); + preview_done = true; } -EditorPackedScenePreviewPlugin::EditorPackedScenePreviewPlugin() { +void EditorMaterialPreviewPlugin::_bind_methods() { + ClassDB::bind_method("_preview_done", &EditorMaterialPreviewPlugin::_preview_done); } -////////////////////////////////////////////////////////////////// +bool EditorMaterialPreviewPlugin::handles(const String &p_type) const { -bool EditorMaterialPreviewPlugin::handles(const String& p_type) const { - - return ClassDB::is_type(p_type,"Material"); //any material + return ClassDB::is_parent_class(p_type, "Material"); //any material } -Ref<Texture> EditorMaterialPreviewPlugin::generate(const RES& p_from) { +Ref<Texture> EditorMaterialPreviewPlugin::generate(const RES &p_from) { Ref<Material> material = p_from; - ERR_FAIL_COND_V(material.is_null(),Ref<Texture>()); + ERR_FAIL_COND_V(material.is_null(), Ref<Texture>()); - VS::get_singleton()->mesh_surface_set_material(sphere,0,material->get_rid()); + VS::get_singleton()->mesh_surface_set_material(sphere, 0, material->get_rid()); - VS::get_singleton()->viewport_queue_screen_capture(viewport); - VS::get_singleton()->viewport_set_render_target_update_mode(viewport,VS::RENDER_TARGET_UPDATE_ONCE); //once used for capture + VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture //print_line("queue capture!"); - Image img; - int timeout=1000; - while(timeout) { - //print_line("try capture?"); + preview_done = false; + VS::get_singleton()->request_frame_drawn_callback(this, "_preview_done", Variant()); + + while (!preview_done) { OS::get_singleton()->delay_usec(10); - img = VS::get_singleton()->viewport_get_screen_capture(viewport); - if (!img.empty()) - break; - timeout--; } - //print_line("captured!"); - VS::get_singleton()->mesh_surface_set_material(sphere,0,RID()); + Ref<Image> img = VS::get_singleton()->VS::get_singleton()->texture_get_data(viewport_texture); + VS::get_singleton()->mesh_surface_set_material(sphere, 0, RID()); - int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); - thumbnail_size*=EDSCALE; - img.resize(thumbnail_size,thumbnail_size); + ERR_FAIL_COND_V(!img.is_valid(), Ref<ImageTexture>()); - Ref<ImageTexture> ptex = Ref<ImageTexture>( memnew( ImageTexture )); - ptex->create_from_image(img,0); + int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); + thumbnail_size *= EDSCALE; + img->convert(Image::FORMAT_RGBA8); + img->resize(thumbnail_size, thumbnail_size); + Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture)); + ptex->create_from_image(img, 0); return ptex; } @@ -280,70 +267,68 @@ EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() { scenario = VS::get_singleton()->scenario_create(); viewport = VS::get_singleton()->viewport_create(); - VS::get_singleton()->viewport_set_as_render_target(viewport,true); - VS::get_singleton()->viewport_set_render_target_update_mode(viewport,VS::RENDER_TARGET_UPDATE_DISABLED); - VS::get_singleton()->viewport_set_scenario(viewport,scenario); - VS::ViewportRect vr; - vr.x=0; - vr.y=0; - vr.width=128; - vr.height=128; - VS::get_singleton()->viewport_set_rect(viewport,vr); + VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_DISABLED); + VS::get_singleton()->viewport_set_scenario(viewport, scenario); + VS::get_singleton()->viewport_set_size(viewport, 128, 128); + VS::get_singleton()->viewport_set_transparent_background(viewport, true); + VS::get_singleton()->viewport_set_active(viewport, true); + VS::get_singleton()->viewport_set_vflip(viewport, true); + viewport_texture = VS::get_singleton()->viewport_get_texture(viewport); camera = VS::get_singleton()->camera_create(); - VS::get_singleton()->viewport_attach_camera(viewport,camera); - VS::get_singleton()->camera_set_transform(camera,Transform(Matrix3(),Vector3(0,0,3))); - VS::get_singleton()->camera_set_perspective(camera,45,0.1,10); + VS::get_singleton()->viewport_attach_camera(viewport, camera); + VS::get_singleton()->camera_set_transform(camera, Transform(Basis(), Vector3(0, 0, 3))); + VS::get_singleton()->camera_set_perspective(camera, 45, 0.1, 10); light = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); - light_instance = VS::get_singleton()->instance_create2(light,scenario); - VS::get_singleton()->instance_set_transform(light_instance,Transform().looking_at(Vector3(-1,-1,-1),Vector3(0,1,0))); + light_instance = VS::get_singleton()->instance_create2(light, scenario); + VS::get_singleton()->instance_set_transform(light_instance, Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0))); light2 = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); - VS::get_singleton()->light_set_color(light2,VS::LIGHT_COLOR_DIFFUSE,Color(0.7,0.7,0.7)); - VS::get_singleton()->light_set_color(light2,VS::LIGHT_COLOR_SPECULAR,Color(0.0,0.0,0.0)); - light_instance2 = VS::get_singleton()->instance_create2(light2,scenario); + VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7)); + //VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7)); + + light_instance2 = VS::get_singleton()->instance_create2(light2, scenario); - VS::get_singleton()->instance_set_transform(light_instance2,Transform().looking_at(Vector3(0,1,0),Vector3(0,0,1))); + VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1))); sphere = VS::get_singleton()->mesh_create(); - sphere_instance = VS::get_singleton()->instance_create2(sphere,scenario); + sphere_instance = VS::get_singleton()->instance_create2(sphere, scenario); - int lats=32; - int lons=32; - float radius=1.0; + int lats = 32; + int lons = 32; + float radius = 1.0; PoolVector<Vector3> vertices; PoolVector<Vector3> normals; PoolVector<Vector2> uvs; PoolVector<float> tangents; - Matrix3 tt = Matrix3(Vector3(0,1,0),Math_PI*0.5); + Basis tt = Basis(Vector3(0, 1, 0), Math_PI * 0.5); - for(int i = 1; i <= lats; i++) { - double lat0 = Math_PI * (-0.5 + (double) (i - 1) / lats); - double z0 = Math::sin(lat0); - double zr0 = Math::cos(lat0); + for (int i = 1; i <= lats; i++) { + double lat0 = Math_PI * (-0.5 + (double)(i - 1) / lats); + double z0 = Math::sin(lat0); + double zr0 = Math::cos(lat0); - double lat1 = Math_PI * (-0.5 + (double) i / lats); + double lat1 = Math_PI * (-0.5 + (double)i / lats); double z1 = Math::sin(lat1); double zr1 = Math::cos(lat1); - for(int j = lons; j >= 1; j--) { + for (int j = lons; j >= 1; j--) { - double lng0 = 2 * Math_PI * (double) (j - 1) / lons; + double lng0 = 2 * Math_PI * (double)(j - 1) / lons; double x0 = Math::cos(lng0); double y0 = Math::sin(lng0); - double lng1 = 2 * Math_PI * (double) (j) / lons; + double lng1 = 2 * Math_PI * (double)(j) / lons; double x1 = Math::cos(lng1); double y1 = Math::sin(lng1); - - Vector3 v[4]={ - Vector3(x1 * zr0, z0, y1 *zr0), - Vector3(x1 * zr1, z1, y1 *zr1), - Vector3(x0 * zr1, z1, y0 *zr1), - Vector3(x0 * zr0, z0, y0 *zr0) + Vector3 v[4] = { + Vector3(x1 * zr0, z0, y1 * zr0), + Vector3(x1 * zr1, z1, y1 * zr1), + Vector3(x0 * zr1, z1, y0 * zr1), + Vector3(x0 * zr0, z0, y0 * zr0) }; #define ADD_POINT(m_idx) \ @@ -364,8 +349,6 @@ EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() { tangents.push_back(1.0); \ } - - ADD_POINT(0); ADD_POINT(1); ADD_POINT(2); @@ -378,12 +361,11 @@ EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() { Array arr; arr.resize(VS::ARRAY_MAX); - arr[VS::ARRAY_VERTEX]=vertices; - arr[VS::ARRAY_NORMAL]=normals; - arr[VS::ARRAY_TANGENT]=tangents; - arr[VS::ARRAY_TEX_UV]=uvs; - VS::get_singleton()->mesh_add_surface(sphere,VS::PRIMITIVE_TRIANGLES,arr); - + arr[VS::ARRAY_VERTEX] = vertices; + arr[VS::ARRAY_NORMAL] = normals; + arr[VS::ARRAY_TANGENT] = tangents; + arr[VS::ARRAY_TEX_UV] = uvs; + VS::get_singleton()->mesh_add_surface_from_arrays(sphere, VS::PRIMITIVE_TRIANGLES, arr); } EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() { @@ -397,30 +379,28 @@ EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() { VS::get_singleton()->free(light_instance2); VS::get_singleton()->free(camera); VS::get_singleton()->free(scenario); - } /////////////////////////////////////////////////////////////////////////// static bool _is_text_char(CharType c) { - return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_'; + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; } -bool EditorScriptPreviewPlugin::handles(const String& p_type) const { +bool EditorScriptPreviewPlugin::handles(const String &p_type) const { - return ClassDB::is_type(p_type,"Script"); + return ClassDB::is_parent_class(p_type, "Script"); } -Ref<Texture> EditorScriptPreviewPlugin::generate(const RES& p_from) { - +Ref<Texture> EditorScriptPreviewPlugin::generate(const RES &p_from) { Ref<Script> scr = p_from; if (scr.is_null()) return Ref<Texture>(); String code = scr->get_source_code().strip_edges(); - if (code=="") + if (code == "") return Ref<Texture>(); List<String> kwors; @@ -428,107 +408,103 @@ Ref<Texture> EditorScriptPreviewPlugin::generate(const RES& p_from) { Set<String> keywords; - for(List<String>::Element *E=kwors.front();E;E=E->next()) { + for (List<String>::Element *E = kwors.front(); E; E = E->next()) { keywords.insert(E->get()); - } - int line = 0; - int col=0; + int col = 0; int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); - thumbnail_size*=EDSCALE; - Image img(thumbnail_size,thumbnail_size,0,Image::FORMAT_RGBA8); - - + thumbnail_size *= EDSCALE; + Ref<Image> img; + img.instance(); + img->create(thumbnail_size, thumbnail_size, 0, Image::FORMAT_RGBA8); Color bg_color = EditorSettings::get_singleton()->get("text_editor/highlighting/background_color"); - bg_color.a=1.0; + bg_color.a = 1.0; Color keyword_color = EditorSettings::get_singleton()->get("text_editor/highlighting/keyword_color"); Color text_color = EditorSettings::get_singleton()->get("text_editor/highlighting/text_color"); Color symbol_color = EditorSettings::get_singleton()->get("text_editor/highlighting/symbol_color"); - - for(int i=0;i<thumbnail_size;i++) { - for(int j=0;j<thumbnail_size;j++) { - img.put_pixel(i,j,bg_color); + for (int i = 0; i < thumbnail_size; i++) { + for (int j = 0; j < thumbnail_size; j++) { + img->put_pixel(i, j, bg_color); } - } - bool prev_is_text=false; - bool in_keyword=false; - for(int i=0;i<code.length();i++) { + img->lock(); + bool prev_is_text = false; + bool in_keyword = false; + for (int i = 0; i < code.length(); i++) { CharType c = code[i]; - if (c>32) { - if (col<thumbnail_size) { + if (c > 32) { + if (col < thumbnail_size) { Color color = text_color; - if (c!='_' && ((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~') || c=='\t')) { + if (c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t')) { //make symbol a little visible - color=symbol_color; - in_keyword=false; + color = symbol_color; + in_keyword = false; } else if (!prev_is_text && _is_text_char(c)) { int pos = i; - while(_is_text_char(code[pos])) { + while (_is_text_char(code[pos])) { pos++; } ///print_line("from "+itos(i)+" to "+itos(pos)); - String word = code.substr(i,pos-i); + String word = code.substr(i, pos - i); //print_line("found word: "+word); if (keywords.has(word)) - in_keyword=true; + in_keyword = true; } else if (!_is_text_char(c)) { - in_keyword=false; + in_keyword = false; } if (in_keyword) - color=keyword_color; + color = keyword_color; - Color ul=color; - ul.a*=0.5; - img.put_pixel(col,line*2,bg_color.blend(ul)); - img.put_pixel(col,line*2+1,color); + Color ul = color; + ul.a *= 0.5; + img->put_pixel(col, line * 2, bg_color.blend(ul)); + img->put_pixel(col, line * 2 + 1, color); - prev_is_text=_is_text_char(c); + prev_is_text = _is_text_char(c); } } else { - prev_is_text=false; - in_keyword=false; + prev_is_text = false; + in_keyword = false; - if (c=='\n') { - col=0; + if (c == '\n') { + col = 0; line++; - if (line>=thumbnail_size/2) + if (line >= thumbnail_size / 2) break; - } else if (c=='\t') { - col+=3; + } else if (c == '\t') { + col += 3; } } col++; } - Ref<ImageTexture> ptex = Ref<ImageTexture>( memnew( ImageTexture)); + img->unlock(); - ptex->create_from_image(img,0); - return ptex; + Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture)); + ptex->create_from_image(img, 0); + return ptex; } EditorScriptPreviewPlugin::EditorScriptPreviewPlugin() { - - } /////////////////////////////////////////////////////////////////// #if 0 bool EditorSamplePreviewPlugin::handles(const String& p_type) const { - return ClassDB::is_type(p_type,"Sample"); + return ClassDB::is_parent_class(p_type,"Sample"); } Ref<Texture> EditorSamplePreviewPlugin::generate(const RES& p_from) { @@ -795,105 +771,108 @@ EditorSamplePreviewPlugin::EditorSamplePreviewPlugin() { #endif /////////////////////////////////////////////////////////////////////////// -bool EditorMeshPreviewPlugin::handles(const String& p_type) const { +void EditorMeshPreviewPlugin::_preview_done(const Variant &p_udata) { - return ClassDB::is_type(p_type,"Mesh"); //any Mesh + preview_done = true; } -Ref<Texture> EditorMeshPreviewPlugin::generate(const RES& p_from) { +void EditorMeshPreviewPlugin::_bind_methods() { + + ClassDB::bind_method("_preview_done", &EditorMeshPreviewPlugin::_preview_done); +} +bool EditorMeshPreviewPlugin::handles(const String &p_type) const { + + return ClassDB::is_parent_class(p_type, "Mesh"); //any Mesh +} +Ref<Texture> EditorMeshPreviewPlugin::generate(const RES &p_from) { + + print_line("**Generating for mesh finally??"); Ref<Mesh> mesh = p_from; - ERR_FAIL_COND_V(mesh.is_null(),Ref<Texture>()); + ERR_FAIL_COND_V(mesh.is_null(), Ref<Texture>()); - VS::get_singleton()->instance_set_base(mesh_instance,mesh->get_rid()); + VS::get_singleton()->instance_set_base(mesh_instance, mesh->get_rid()); - AABB aabb= mesh->get_aabb(); - Vector3 ofs = aabb.pos + aabb.size*0.5; - aabb.pos-=ofs; + Rect3 aabb = mesh->get_aabb(); + print_line("mesh aabb: " + aabb); + Vector3 ofs = aabb.position + aabb.size * 0.5; + aabb.position -= ofs; Transform xform; - xform.basis=Matrix3().rotated(Vector3(0,1,0),-Math_PI*0.125); - xform.basis = Matrix3().rotated(Vector3(1,0,0),Math_PI*0.125)*xform.basis; - AABB rot_aabb = xform.xform(aabb); - float m = MAX(rot_aabb.size.x,rot_aabb.size.y)*0.5; - if (m==0) + xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI * 0.125); + xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI * 0.125) * xform.basis; + Rect3 rot_aabb = xform.xform(aabb); + float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5; + if (m == 0) return Ref<Texture>(); - m=1.0/m; - m*=0.5; + m = 1.0 / m; + m *= 0.5; //print_line("scale: "+rtos(m)); - xform.basis.scale(Vector3(m,m,m)); - xform.origin=-xform.basis.xform(ofs); //-ofs*m; - xform.origin.z-=rot_aabb.size.z*2; - VS::get_singleton()->instance_set_transform(mesh_instance,xform); - + xform.basis.scale(Vector3(m, m, m)); + xform.origin = -xform.basis.xform(ofs); //-ofs*m; + xform.origin.z -= rot_aabb.size.z * 2; + VS::get_singleton()->instance_set_transform(mesh_instance, xform); - - VS::get_singleton()->viewport_queue_screen_capture(viewport); - VS::get_singleton()->viewport_set_render_target_update_mode(viewport,VS::RENDER_TARGET_UPDATE_ONCE); //once used for capture + VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture //print_line("queue capture!"); - Image img; - int timeout=1000; - while(timeout) { - //print_line("try capture?"); + preview_done = false; + VS::get_singleton()->request_frame_drawn_callback(this, "_preview_done", Variant()); + + while (!preview_done) { OS::get_singleton()->delay_usec(10); - img = VS::get_singleton()->viewport_get_screen_capture(viewport); - if (!img.empty()) - break; - timeout--; } - //print_line("captured!"); - VS::get_singleton()->instance_set_base(mesh_instance,RID()); + Ref<Image> img = VS::get_singleton()->VS::get_singleton()->texture_get_data(viewport_texture); + ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>()); + + print_line("captured! " + itos(img->get_width()) + "x" + itos(img->get_height())); + VS::get_singleton()->instance_set_base(mesh_instance, RID()); int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); - thumbnail_size*=EDSCALE; - img.resize(thumbnail_size,thumbnail_size); + thumbnail_size *= EDSCALE; + img->convert(Image::FORMAT_RGBA8); + img->resize(thumbnail_size, thumbnail_size); - Ref<ImageTexture> ptex = Ref<ImageTexture>( memnew( ImageTexture )); - ptex->create_from_image(img,0); + Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture)); + ptex->create_from_image(img, 0); return ptex; } EditorMeshPreviewPlugin::EditorMeshPreviewPlugin() { scenario = VS::get_singleton()->scenario_create(); + viewport = VS::get_singleton()->viewport_create(); - VS::get_singleton()->viewport_set_as_render_target(viewport,true); - VS::get_singleton()->viewport_set_render_target_update_mode(viewport,VS::RENDER_TARGET_UPDATE_DISABLED); - VS::get_singleton()->viewport_set_scenario(viewport,scenario); - VS::ViewportRect vr; - vr.x=0; - vr.y=0; - vr.width=128; - vr.height=128; - VS::get_singleton()->viewport_set_rect(viewport,vr); + VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_DISABLED); + VS::get_singleton()->viewport_set_vflip(viewport, true); + VS::get_singleton()->viewport_set_scenario(viewport, scenario); + VS::get_singleton()->viewport_set_size(viewport, 128, 128); + VS::get_singleton()->viewport_set_transparent_background(viewport, true); + VS::get_singleton()->viewport_set_active(viewport, true); + viewport_texture = VS::get_singleton()->viewport_get_texture(viewport); camera = VS::get_singleton()->camera_create(); - VS::get_singleton()->viewport_attach_camera(viewport,camera); - VS::get_singleton()->camera_set_transform(camera,Transform(Matrix3(),Vector3(0,0,3))); + VS::get_singleton()->viewport_attach_camera(viewport, camera); + VS::get_singleton()->camera_set_transform(camera, Transform(Basis(), Vector3(0, 0, 3))); //VS::get_singleton()->camera_set_perspective(camera,45,0.1,10); - VS::get_singleton()->camera_set_orthogonal(camera,1.0,0.01,1000.0); + VS::get_singleton()->camera_set_orthogonal(camera, 1.0, 0.01, 1000.0); light = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); - light_instance = VS::get_singleton()->instance_create2(light,scenario); - VS::get_singleton()->instance_set_transform(light_instance,Transform().looking_at(Vector3(-1,-1,-1),Vector3(0,1,0))); + light_instance = VS::get_singleton()->instance_create2(light, scenario); + VS::get_singleton()->instance_set_transform(light_instance, Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0))); light2 = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); - VS::get_singleton()->light_set_color(light2,VS::LIGHT_COLOR_DIFFUSE,Color(0.7,0.7,0.7)); - VS::get_singleton()->light_set_color(light2,VS::LIGHT_COLOR_SPECULAR,Color(0.0,0.0,0.0)); - light_instance2 = VS::get_singleton()->instance_create2(light2,scenario); + VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7)); + //VS::get_singleton()->light_set_color(light2, VS::LIGHT_COLOR_SPECULAR, Color(0.0, 0.0, 0.0)); + light_instance2 = VS::get_singleton()->instance_create2(light2, scenario); - VS::get_singleton()->instance_set_transform(light_instance2,Transform().looking_at(Vector3(0,1,0),Vector3(0,0,1))); + VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1))); //sphere = VS::get_singleton()->mesh_create(); mesh_instance = VS::get_singleton()->instance_create(); - VS::get_singleton()->instance_set_scenario(mesh_instance,scenario); - - - + VS::get_singleton()->instance_set_scenario(mesh_instance, scenario); } - EditorMeshPreviewPlugin::~EditorMeshPreviewPlugin() { //VS::get_singleton()->free(sphere); @@ -905,6 +884,4 @@ EditorMeshPreviewPlugin::~EditorMeshPreviewPlugin() { VS::get_singleton()->free(light_instance2); VS::get_singleton()->free(camera); VS::get_singleton()->free(scenario); - } -#endif diff --git a/editor/plugins/editor_preview_plugins.h b/editor/plugins/editor_preview_plugins.h index 993e36df02..7e7d36eb1e 100644 --- a/editor/plugins/editor_preview_plugins.h +++ b/editor/plugins/editor_preview_plugins.h @@ -32,55 +32,58 @@ #include "editor/editor_resource_preview.h" -#if 0 class EditorTexturePreviewPlugin : public EditorResourcePreviewGenerator { + GDCLASS(EditorTexturePreviewPlugin, EditorResourcePreviewGenerator) public: - - virtual bool handles(const String& p_type) const; - virtual Ref<Texture> generate(const RES& p_from); + virtual bool handles(const String &p_type) const; + virtual Ref<Texture> generate(const RES &p_from); EditorTexturePreviewPlugin(); }; - class EditorBitmapPreviewPlugin : public EditorResourcePreviewGenerator { + GDCLASS(EditorBitmapPreviewPlugin, EditorResourcePreviewGenerator) public: - - virtual bool handles(const String& p_type) const; - virtual Ref<Texture> generate(const RES& p_from); + virtual bool handles(const String &p_type) const; + virtual Ref<Texture> generate(const RES &p_from); EditorBitmapPreviewPlugin(); }; - - class EditorPackedScenePreviewPlugin : public EditorResourcePreviewGenerator { - Ref<Texture> _gen_from_imd(Ref<ResourceImportMetadata> p_imd); public: - - virtual bool handles(const String& p_type) const; - virtual Ref<Texture> generate(const RES& p_from); - virtual Ref<Texture> generate_from_path(const String& p_path); + virtual bool handles(const String &p_type) const; + virtual Ref<Texture> generate(const RES &p_from); + virtual Ref<Texture> generate_from_path(const String &p_path); EditorPackedScenePreviewPlugin(); }; class EditorMaterialPreviewPlugin : public EditorResourcePreviewGenerator { + GDCLASS(EditorMaterialPreviewPlugin, EditorResourcePreviewGenerator) + RID scenario; RID sphere; RID sphere_instance; RID viewport; + RID viewport_texture; RID light; RID light_instance; RID light2; RID light_instance2; RID camera; -public: + volatile bool preview_done; - virtual bool handles(const String& p_type) const; - virtual Ref<Texture> generate(const RES& p_from); + void _preview_done(const Variant &p_udata); + +protected: + static void _bind_methods(); + +public: + virtual bool handles(const String &p_type) const; + virtual Ref<Texture> generate(const RES &p_from); EditorMaterialPreviewPlugin(); ~EditorMaterialPreviewPlugin(); @@ -88,9 +91,8 @@ public: class EditorScriptPreviewPlugin : public EditorResourcePreviewGenerator { public: - - virtual bool handles(const String& p_type) const; - virtual Ref<Texture> generate(const RES& p_from); + virtual bool handles(const String &p_type) const; + virtual Ref<Texture> generate(const RES &p_from); EditorScriptPreviewPlugin(); }; @@ -108,22 +110,30 @@ public: #endif class EditorMeshPreviewPlugin : public EditorResourcePreviewGenerator { + GDCLASS(EditorMeshPreviewPlugin, EditorResourcePreviewGenerator) + RID scenario; RID mesh_instance; RID viewport; + RID viewport_texture; RID light; RID light_instance; RID light2; RID light_instance2; RID camera; -public: + volatile bool preview_done; - virtual bool handles(const String& p_type) const; - virtual Ref<Texture> generate(const RES& p_from); + void _preview_done(const Variant &p_udata); + +protected: + static void _bind_methods(); + +public: + virtual bool handles(const String &p_type) const; + virtual Ref<Texture> generate(const RES &p_from); EditorMeshPreviewPlugin(); ~EditorMeshPreviewPlugin(); }; -#endif #endif // EDITORPREVIEWPLUGINS_H diff --git a/editor/plugins/gradient_texture_editor_plugin.cpp b/editor/plugins/gradient_texture_editor_plugin.cpp index 40f7de478d..bc985dcdf7 100644 --- a/editor/plugins/gradient_texture_editor_plugin.cpp +++ b/editor/plugins/gradient_texture_editor_plugin.cpp @@ -94,14 +94,14 @@ void GradientTextureEdit::_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> mb = p_event; //Show color picker on double click. if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_doubleclick() && mb->is_pressed()) { - grabbed = _get_point_from_pos(mb->get_pos().x); + grabbed = _get_point_from_pos(mb->get_position().x); _show_color_picker(); accept_event(); } //Delete point on right click if (mb.is_valid() && mb->get_button_index() == 2 && mb->is_pressed()) { - grabbed = _get_point_from_pos(mb->get_pos().x); + grabbed = _get_point_from_pos(mb->get_position().x); if (grabbed != -1) { points.remove(grabbed); grabbed = -1; @@ -115,7 +115,7 @@ void GradientTextureEdit::_gui_input(const Ref<InputEvent> &p_event) { //Hold alt key to duplicate selected color if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed() && mb->get_alt()) { - int x = mb->get_pos().x; + int x = mb->get_position().x; grabbed = _get_point_from_pos(x); if (grabbed != -1) { @@ -140,7 +140,7 @@ void GradientTextureEdit::_gui_input(const Ref<InputEvent> &p_event) { if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) { update(); - int x = mb->get_pos().x; + int x = mb->get_position().x; int total_w = get_size().width - get_size().height - 3; //Check if color selector was clicked. @@ -220,7 +220,7 @@ void GradientTextureEdit::_gui_input(const Ref<InputEvent> &p_event) { int total_w = get_size().width - get_size().height - 3; - int x = mm->get_pos().x; + int x = mm->get_position().x; float newofs = CLAMP(x / float(total_w), 0, 1); //Snap to nearest point if holding shift diff --git a/editor/plugins/light_occluder_2d_editor_plugin.cpp b/editor/plugins/light_occluder_2d_editor_plugin.cpp index 9c9e010b47..09021446dc 100644 --- a/editor/plugins/light_occluder_2d_editor_plugin.cpp +++ b/editor/plugins/light_occluder_2d_editor_plugin.cpp @@ -118,7 +118,7 @@ bool LightOccluder2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); - Vector2 gpoint = mb->get_pos(); + Vector2 gpoint = mb->get_position(); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint = canvas_item_editor->snap_point(cpoint); cpoint = node->get_global_transform().affine_inverse().xform(cpoint); @@ -308,7 +308,7 @@ bool LightOccluder2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (edited_point != -1 && (wip_active || mm->get_button_mask() & BUTTON_MASK_LEFT)) { - Vector2 gpoint = mm->get_pos(); + Vector2 gpoint = mm->get_position(); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint = canvas_item_editor->snap_point(cpoint); edited_point_pos = node->get_global_transform().affine_inverse().xform(cpoint); diff --git a/editor/plugins/line_2d_editor_plugin.cpp b/editor/plugins/line_2d_editor_plugin.cpp index 3497af3602..76906a5b93 100644 --- a/editor/plugins/line_2d_editor_plugin.cpp +++ b/editor/plugins/line_2d_editor_plugin.cpp @@ -87,7 +87,7 @@ bool Line2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (mb.is_valid()) { - Vector2 gpoint = mb->get_pos(); + Vector2 gpoint = mb->get_position(); Vector2 cpoint = mouse_to_local_pos(gpoint, mb->get_alt()); if (mb->is_pressed() && _dragging == false) { @@ -146,7 +146,7 @@ bool Line2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (mm.is_valid()) { if (_dragging) { - Vector2 cpoint = mouse_to_local_pos(mm->get_pos(), mm->get_alt()); + Vector2 cpoint = mouse_to_local_pos(mm->get_position(), mm->get_alt()); node->set_point_pos(action_point, cpoint); canvas_item_editor->get_viewport_control()->update(); return true; diff --git a/editor/plugins/navigation_polygon_editor_plugin.cpp b/editor/plugins/navigation_polygon_editor_plugin.cpp index a7fc1d5adb..1457b28ed1 100644 --- a/editor/plugins/navigation_polygon_editor_plugin.cpp +++ b/editor/plugins/navigation_polygon_editor_plugin.cpp @@ -134,7 +134,7 @@ bool NavigationPolygonEditor::forward_gui_input(const Ref<InputEvent> &p_event) Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); - Vector2 gpoint = mb->get_pos(); + Vector2 gpoint = mb->get_position(); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint = canvas_item_editor->snap_point(cpoint); cpoint = node->get_global_transform().affine_inverse().xform(cpoint); @@ -361,7 +361,7 @@ bool NavigationPolygonEditor::forward_gui_input(const Ref<InputEvent> &p_event) if (edited_point != -1 && (wip_active || mm->get_button_mask() & BUTTON_MASK_LEFT)) { - Vector2 gpoint = mm->get_pos(); + Vector2 gpoint = mm->get_position(); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint = canvas_item_editor->snap_point(cpoint); edited_point_pos = node->get_global_transform().affine_inverse().xform(cpoint); diff --git a/editor/plugins/particles_editor_plugin.cpp b/editor/plugins/particles_editor_plugin.cpp index dc2da80b06..d918a3e24e 100644 --- a/editor/plugins/particles_editor_plugin.cpp +++ b/editor/plugins/particles_editor_plugin.cpp @@ -254,7 +254,7 @@ void ParticlesEditor::_generate_emission_points() { for (int j = 0; j < 3; j++) { if (i == 0 && j == 0) - aabb.pos = r[i].vertex[j]; + aabb.position = r[i].vertex[j]; else aabb.expand_to(r[i].vertex[j]); } @@ -272,7 +272,7 @@ void ParticlesEditor::_generate_emission_points() { dir[Math::rand() % 3] = 1.0; Vector3 ofs = Vector3(1, 1, 1) - dir; ofs = (Vector3(1, 1, 1) - dir) * Vector3(Math::randf(), Math::randf(), Math::randf()) * aabb.size; - ofs += aabb.pos; + ofs += aabb.position; Vector3 ofsv = ofs + aabb.size * dir; diff --git a/editor/plugins/path_2d_editor_plugin.cpp b/editor/plugins/path_2d_editor_plugin.cpp index 73d5b28886..f9f16fea16 100644 --- a/editor/plugins/path_2d_editor_plugin.cpp +++ b/editor/plugins/path_2d_editor_plugin.cpp @@ -74,7 +74,7 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); - Vector2 gpoint = mb->get_pos(); + Vector2 gpoint = mb->get_position(); Vector2 cpoint = !mb->get_alt() ? canvas_item_editor->snap_point(xform.affine_inverse().xform(gpoint)) : @@ -431,7 +431,7 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (action != ACTION_NONE) { // Handle point/control movement. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); - Vector2 gpoint = mm->get_pos(); + Vector2 gpoint = mm->get_position(); Vector2 cpoint = !mm->get_alt() ? canvas_item_editor->snap_point(xform.affine_inverse().xform(gpoint)) : diff --git a/editor/plugins/path_editor_plugin.cpp b/editor/plugins/path_editor_plugin.cpp index 20535d796e..877707d77b 100644 --- a/editor/plugins/path_editor_plugin.cpp +++ b/editor/plugins/path_editor_plugin.cpp @@ -285,7 +285,7 @@ bool PathEditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<Inp if (mb.is_valid()) { - Point2 mbpos(mb->get_pos().x, mb->get_pos().y); + Point2 mbpos(mb->get_position().x, mb->get_position().y); if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->get_control()))) { //click into curve, break it down diff --git a/editor/plugins/polygon_2d_editor_plugin.cpp b/editor/plugins/polygon_2d_editor_plugin.cpp index c2edc608ab..dd13ca0e63 100644 --- a/editor/plugins/polygon_2d_editor_plugin.cpp +++ b/editor/plugins/polygon_2d_editor_plugin.cpp @@ -212,7 +212,7 @@ bool Polygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); - Vector2 gpoint = mb->get_pos(); + Vector2 gpoint = mb->get_position(); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint = canvas_item_editor->snap_point(cpoint); cpoint = node->get_global_transform().affine_inverse().xform(cpoint); @@ -397,7 +397,7 @@ bool Polygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (edited_point != -1 && (wip_active || mm->get_button_mask() & BUTTON_MASK_LEFT)) { - Vector2 gpoint = mm->get_pos(); + Vector2 gpoint = mm->get_position(); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint = canvas_item_editor->snap_point(cpoint); edited_point_pos = node->get_global_transform().affine_inverse().xform(cpoint); @@ -465,7 +465,7 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) { if (mb->is_pressed()) { - uv_drag_from = Vector2(mb->get_pos().x, mb->get_pos().y); + uv_drag_from = Vector2(mb->get_position().x, mb->get_position().y); uv_drag = true; uv_prev = node->get_uv(); uv_move_current = uv_mode; @@ -485,7 +485,7 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) { for (int i = 0; i < uv_prev.size(); i++) { Vector2 tuv = mtx.xform(uv_prev[i]); - if (tuv.distance_to(Vector2(mb->get_pos().x, mb->get_pos().y)) < 8) { + if (tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)) < 8) { uv_drag_from = tuv; uv_drag_index = i; } @@ -537,7 +537,7 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) { } else if (uv_drag) { - Vector2 uv_drag_to = mm->get_pos(); + Vector2 uv_drag_to = mm->get_position(); Vector2 drag = mtx.affine_inverse().xform(uv_drag_to) - mtx.affine_inverse().xform(uv_drag_from); switch (uv_move_current) { @@ -675,14 +675,14 @@ void Polygon2DEditor::_uv_draw() { rect = rect.grow(200); updating_uv_scroll = true; - uv_hscroll->set_min(rect.pos.x); - uv_hscroll->set_max(rect.pos.x + rect.size.x); + uv_hscroll->set_min(rect.position.x); + uv_hscroll->set_max(rect.position.x + rect.size.x); uv_hscroll->set_page(uv_edit_draw->get_size().x); uv_hscroll->set_value(uv_draw_ofs.x); uv_hscroll->set_step(0.001); - uv_vscroll->set_min(rect.pos.y); - uv_vscroll->set_max(rect.pos.y + rect.size.y); + uv_vscroll->set_min(rect.position.y); + uv_vscroll->set_max(rect.position.y + rect.size.y); uv_vscroll->set_page(uv_edit_draw->get_size().y); uv_vscroll->set_value(uv_draw_ofs.y); uv_vscroll->set_step(0.001); diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index fde6d83aa8..f3941d6a16 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -410,7 +410,9 @@ void ScriptEditor::_go_to_tab(int p_idx) { c->set_meta("__editor_pass", ++edit_pass); _update_history_arrows(); _update_script_colors(); + _update_members_overview(); _update_selected_editor_menu(); + _update_members_overview_visibility(); } void ScriptEditor::_add_recent_script(String p_path) { @@ -540,6 +542,7 @@ void ScriptEditor::_close_tab(int p_idx, bool p_save) { _update_history_arrows(); _update_script_names(); + _update_members_overview_visibility(); _save_layout(); } @@ -1025,6 +1028,7 @@ void ScriptEditor::_notification(int p_what) { editor->connect("script_add_function_request", this, "_add_callback"); editor->connect("resource_saved", this, "_res_saved_callback"); script_list->connect("item_selected", this, "_script_selected"); + members_overview->connect("item_selected", this, "_members_overview_selected"); script_split->connect("dragged", this, "_script_split_dragged"); autosave_timer->connect("timeout", this, "_autosave_scripts"); { @@ -1273,6 +1277,16 @@ void ScriptEditor::ensure_focus_current() { se->ensure_focus(); } +void ScriptEditor::_members_overview_selected(int p_idx) { + Node *current = tab_container->get_child(tab_container->get_current_tab()); + ScriptEditorBase *se = current->cast_to<ScriptEditorBase>(); + if (!se) { + return; + } + se->goto_line(members_overview->get_item_metadata(p_idx)); + se->ensure_focus(); +} + void ScriptEditor::_script_selected(int p_idx) { grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing @@ -1342,6 +1356,37 @@ struct _ScriptEditorItemData { } }; +void ScriptEditor::_update_members_overview_visibility() { + Node *current = tab_container->get_child(tab_container->get_current_tab()); + ScriptEditorBase *se = current->cast_to<ScriptEditorBase>(); + if (!se) { + members_overview->set_visible(false); + return; + } + + if (members_overview_enabled && se->show_members_overview()) { + members_overview->set_visible(true); + } else { + members_overview->set_visible(false); + } +} + +void ScriptEditor::_update_members_overview() { + members_overview->clear(); + + Node *current = tab_container->get_child(tab_container->get_current_tab()); + ScriptEditorBase *se = current->cast_to<ScriptEditorBase>(); + if (!se) { + return; + } + + Vector<String> functions = se->get_functions(); + for (int i = 0; i < functions.size(); i++) { + members_overview->add_item(functions[i].get_slice(":", 0)); + members_overview->set_item_metadata(i, functions[i].get_slice(":", 1).to_int() - 1); + } +} + void ScriptEditor::_update_script_colors() { bool script_temperature_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_enabled"); @@ -1485,6 +1530,7 @@ void ScriptEditor::_update_script_names() { } } + _update_members_overview(); _update_script_colors(); } @@ -1733,6 +1779,9 @@ void ScriptEditor::_editor_settings_changed() { convert_indent_on_save = EditorSettings::get_singleton()->get("text_editor/indent/convert_indent_on_save"); use_space_indentation = EditorSettings::get_singleton()->get("text_editor/indent/type"); + members_overview_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/show_members_overview"); + _update_members_overview_visibility(); + float autosave_time = EditorSettings::get_singleton()->get("text_editor/files/autosave_interval_secs"); if (autosave_time > 0) { autosave_timer->set_wait_time(autosave_time); @@ -2084,6 +2133,7 @@ void ScriptEditor::_bind_methods() { ClassDB::bind_method("_editor_settings_changed", &ScriptEditor::_editor_settings_changed); ClassDB::bind_method("_update_script_names", &ScriptEditor::_update_script_names); ClassDB::bind_method("_tree_changed", &ScriptEditor::_tree_changed); + ClassDB::bind_method("_members_overview_selected", &ScriptEditor::_members_overview_selected); ClassDB::bind_method("_script_selected", &ScriptEditor::_script_selected); ClassDB::bind_method("_script_created", &ScriptEditor::_script_created); ClassDB::bind_method("_script_split_dragged", &ScriptEditor::_script_split_dragged); @@ -2105,6 +2155,7 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) { waiting_update_names = false; pending_auto_reload = false; auto_reload_running_scripts = false; + members_overview_enabled = true; editor = p_editor; menu_hb = memnew(HBoxContainer); @@ -2114,10 +2165,19 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) { add_child(script_split); script_split->set_v_size_flags(SIZE_EXPAND_FILL); + list_split = memnew(VSplitContainer); + script_split->add_child(list_split); + list_split->set_v_size_flags(SIZE_EXPAND_FILL); + script_list = memnew(ItemList); - script_split->add_child(script_list); + list_split->add_child(script_list); script_list->set_custom_minimum_size(Size2(0, 0)); script_split->set_split_offset(140); + list_split->set_split_offset(500); + + members_overview = memnew(ItemList); + list_split->add_child(members_overview); + members_overview->set_custom_minimum_size(Size2(0, 0)); tab_container = memnew(TabContainer); tab_container->add_style_override("panel", p_editor->get_gui_base()->get_stylebox("ScriptPanel", "EditorStyles")); diff --git a/editor/plugins/script_editor_plugin.h b/editor/plugins/script_editor_plugin.h index 455a888f0e..e036d1ed9c 100644 --- a/editor/plugins/script_editor_plugin.h +++ b/editor/plugins/script_editor_plugin.h @@ -102,6 +102,8 @@ public: virtual void set_debugger_active(bool p_active) = 0; virtual bool can_lose_focus_on_node_selection() { return true; } + virtual bool show_members_overview() = 0; + virtual void set_tooltip_request_func(String p_method, Object *p_obj) = 0; virtual Control *get_edit_menu() = 0; @@ -179,6 +181,9 @@ class ScriptEditor : public VBoxContainer { ItemList *script_list; HSplitContainer *script_split; + ItemList *members_overview; + bool members_overview_enabled; + VSplitContainer *list_split; TabContainer *tab_container; EditorFileDialog *file_dialog; ConfirmationDialog *erase_tab_confirm; @@ -278,8 +283,11 @@ class ScriptEditor : public VBoxContainer { void _editor_settings_changed(); void _autosave_scripts(); + void _update_members_overview_visibility(); + void _update_members_overview(); void _update_script_names(); + void _members_overview_selected(int p_idx); void _script_selected(int p_idx); void _find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used); diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 2d3a14e525..9f76119374 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -218,6 +218,10 @@ void ScriptTextEditor::add_callback(const String &p_function, PoolStringArray p_ code_editor->get_text_edit()->cursor_set_column(1); } +bool ScriptTextEditor::show_members_overview() { + return true; +} + void ScriptTextEditor::update_settings() { code_editor->update_editor_settings(); @@ -1236,8 +1240,8 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) { int col, row; TextEdit *tx = code_editor->get_text_edit(); - tx->_get_mouse_pos(mb->get_global_pos() - tx->get_global_position(), row, col); - Vector2 mpos = mb->get_global_pos() - tx->get_global_position(); + tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col); + Vector2 mpos = mb->get_global_position() - tx->get_global_position(); bool have_selection = (tx->get_selection_text().length() > 0); bool have_color = (tx->get_word_at_pos(mpos) == "Color"); if (have_color) { diff --git a/editor/plugins/script_text_editor.h b/editor/plugins/script_text_editor.h index fdae03949c..ba40645161 100644 --- a/editor/plugins/script_text_editor.h +++ b/editor/plugins/script_text_editor.h @@ -149,6 +149,8 @@ public: virtual void add_callback(const String &p_function, PoolStringArray p_args); virtual void update_settings(); + virtual bool show_members_overview(); + virtual void set_tooltip_request_func(String p_method, Object *p_obj); virtual void set_debugger_active(bool p_active); diff --git a/editor/plugins/shader_editor_plugin.cpp b/editor/plugins/shader_editor_plugin.cpp index 8ae7d55bdd..7c8ee97f22 100644 --- a/editor/plugins/shader_editor_plugin.cpp +++ b/editor/plugins/shader_editor_plugin.cpp @@ -552,7 +552,8 @@ ShaderEditorPlugin::ShaderEditorPlugin(EditorNode *p_node) { shader_editor = memnew(ShaderEditor); shader_editor->set_custom_minimum_size(Size2(0, 300)); - button = editor->add_bottom_panel_item("Shader", shader_editor); + button = editor->add_bottom_panel_item(TTR("Shader"), shader_editor); + button->hide(); } ShaderEditorPlugin::~ShaderEditorPlugin() { diff --git a/editor/plugins/spatial_editor_plugin.cpp b/editor/plugins/spatial_editor_plugin.cpp index ff4b5e430e..a8d875a769 100644 --- a/editor/plugins/spatial_editor_plugin.cpp +++ b/editor/plugins/spatial_editor_plugin.cpp @@ -636,7 +636,7 @@ void SpatialEditorViewport::_smouseenter() { void SpatialEditorViewport::_list_select(Ref<InputEventMouseButton> b) { - _find_items_at_pos(b->get_pos(), clicked_includes_current, selection_results, b->get_shift()); + _find_items_at_pos(b->get_position(), clicked_includes_current, selection_results, b->get_shift()); Node *scene = editor->get_edited_scene(); @@ -684,7 +684,7 @@ void SpatialEditorViewport::_list_select(Ref<InputEventMouseButton> b) { selection_menu->set_item_tooltip(i, String(spat->get_name()) + "\nType: " + spat->get_class() + "\nPath: " + node_path); } - selection_menu->set_global_position(b->get_global_pos()); + selection_menu->set_global_position(b->get_global_position()); selection_menu->popup(); selection_menu->call_deferred("grab_click_focus"); selection_menu->set_invalidate_click_until_motion(); @@ -818,7 +818,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { break; } - _edit.mouse_pos = b->get_pos(); + _edit.mouse_pos = b->get_position(); _edit.snap = false; _edit.mode = TRANSFORM_NONE; @@ -863,7 +863,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { break; //bye //handle rotate _edit.mode = TRANSFORM_ROTATE; - _compute_edit(b->get_pos()); + _compute_edit(b->get_position()); break; } @@ -873,7 +873,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { break; //bye //handle rotate _edit.mode = TRANSFORM_TRANSLATE; - _compute_edit(b->get_pos()); + _compute_edit(b->get_position()); break; } @@ -883,7 +883,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { break; //bye //handle rotate _edit.mode = TRANSFORM_SCALE; - _compute_edit(b->get_pos()); + _compute_edit(b->get_position()); break; } @@ -891,7 +891,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { int gizmo_handle = -1; - clicked = _select_ray(b->get_pos(), b->get_shift(), clicked_includes_current, &gizmo_handle, b->get_shift()); + clicked = _select_ray(b->get_position(), b->get_shift(), clicked_includes_current, &gizmo_handle, b->get_shift()); //clicking is always deferred to either move or release @@ -904,8 +904,8 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { //default to regionselect cursor.region_select = true; - cursor.region_begin = b->get_pos(); - cursor.region_end = b->get_pos(); + cursor.region_begin = b->get_position(); + cursor.region_end = b->get_position(); } if (clicked && gizmo_handle >= 0) { @@ -989,7 +989,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { if (m.is_valid()) { - _edit.mouse_pos = m->get_pos(); + _edit.mouse_pos = m->get_position(); if (spatial_editor->get_selected()) { @@ -1026,7 +1026,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { if (_edit.gizmo.is_valid()) { - _edit.gizmo->set_handle(_edit.gizmo_handle, camera, m->get_pos()); + _edit.gizmo->set_handle(_edit.gizmo_handle, camera, m->get_position()); Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle); String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle); set_message(n + ": " + String(v)); @@ -1058,7 +1058,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { if (cursor.region_select && nav_mode == NAVIGATION_NONE) { - cursor.region_end = m->get_pos(); + cursor.region_end = m->get_position(); surface->update(); return; } @@ -1066,8 +1066,8 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { if (_edit.mode == TRANSFORM_NONE && nav_mode == NAVIGATION_NONE) return; - Vector3 ray_pos = _get_ray_pos(m->get_pos()); - Vector3 ray = _get_ray(m->get_pos()); + Vector3 ray_pos = _get_ray_pos(m->get_position()); + Vector3 ray = _get_ray(m->get_position()); switch (_edit.mode) { @@ -1676,7 +1676,7 @@ void SpatialEditorViewport::_notification(int p_what) { } Transform t = sp->get_global_transform(); - t.translate(se->aabb.pos); + t.translate(se->aabb.position); t.basis.scale(se->aabb.size); exist = true; @@ -1724,6 +1724,33 @@ void SpatialEditorViewport::_notification(int p_what) { bool hdr = GlobalConfig::get_singleton()->get("rendering/quality/hdr"); viewport->set_hdr(hdr); + + bool show_info = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION)); + if (show_info != info->is_visible()) { + if (show_info) + info->show(); + else + info->hide(); + } + + if (show_info) { + + String text; + text += TTR("Objects Drawn") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_OBJECTS_IN_FRAME)) + "\n"; + text += TTR("Material Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_MATERIAL_CHANGES_IN_FRAME)) + "\n"; + text += TTR("Shader Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SHADER_CHANGES_IN_FRAME)) + "\n"; + text += TTR("Surface Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SURFACE_CHANGES_IN_FRAME)) + "\n"; + text += TTR("Draw Calls") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_DRAW_CALLS_IN_FRAME)) + "\n"; + text += TTR("Vertices") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_VERTICES_IN_FRAME)); + + if (info_label->get_text() != text || surface->get_size() != prev_size) { + info_label->set_text(text); + Size2 ms = info->get_minimum_size(); + info->set_position(surface->get_size() - ms - Vector2(20, 20) * EDSCALE); + } + } + + prev_size = surface->get_size(); } if (p_what == NOTIFICATION_ENTER_TREE) { @@ -1731,6 +1758,7 @@ void SpatialEditorViewport::_notification(int p_what) { surface->connect("draw", this, "_draw"); surface->connect("gui_input", this, "_sinput"); surface->connect("mouse_entered", this, "_smouseenter"); + info->add_style_override("panel", get_stylebox("panel", "Panel")); preview_camera->set_icon(get_icon("Camera", "EditorIcons")); _init_gizmo_instance(index); } @@ -1752,10 +1780,10 @@ static void stroke_rect(CanvasItem *ci, Rect2 rect, Color color, real_t width = // a---b // | | // c---d - Vector2 a(rect.pos); - Vector2 b(rect.pos.x + rect.size.x, rect.pos.y); - Vector2 c(rect.pos.x, rect.pos.y + rect.size.y); - Vector2 d(rect.pos + rect.size); + Vector2 a(rect.position); + Vector2 b(rect.position.x + rect.size.x, rect.position.y); + Vector2 c(rect.position.x, rect.position.y + rect.size.y); + Vector2 d(rect.position + rect.size); ci->draw_line(a, b, color, width); ci->draw_line(b, d, color, width); @@ -1804,15 +1832,15 @@ void SpatialEditorViewport::_draw() { case Camera::KEEP_WIDTH: { draw_rect.size = Size2(s.width, s.width / aspect); - draw_rect.pos.x = 0; - draw_rect.pos.y = (s.height - draw_rect.size.y) * 0.5; + draw_rect.position.x = 0; + draw_rect.position.y = (s.height - draw_rect.size.y) * 0.5; } break; case Camera::KEEP_HEIGHT: { draw_rect.size = Size2(s.height * aspect, s.height); - draw_rect.pos.y = 0; - draw_rect.pos.x = (s.width - draw_rect.size.x) * 0.5; + draw_rect.position.y = 0; + draw_rect.position.x = (s.width - draw_rect.size.x) * 0.5; } break; } @@ -1846,7 +1874,7 @@ void SpatialEditorViewport::_draw() { real_t sy = r.size.y * logscale_t; surface->draw_rect(r, Color(1, 1, 1, 0.2)); - surface->draw_rect(Rect2(r.pos.x, r.pos.y + r.size.y - sy, r.size.x, sy), Color(1, 1, 1, 0.6)); + surface->draw_rect(Rect2(r.position.x, r.position.y + r.size.y - sy, r.size.x, sy), Color(1, 1, 1, 0.6)); stroke_rect(surface, r.grow(1), Color(0, 0, 0, 0.7)); } } @@ -1997,6 +2025,52 @@ void SpatialEditorViewport::_menu_option(int p_option) { view_menu->get_popup()->set_item_checked(idx, current); } break; + case VIEW_INFORMATION: { + + int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION); + bool current = view_menu->get_popup()->is_item_checked(idx); + view_menu->get_popup()->set_item_checked(idx, !current); + + } break; + case VIEW_DISPLAY_NORMAL: { + + viewport->set_debug_draw(Viewport::DEBUG_DRAW_DISABLED); + + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false); + + } break; + case VIEW_DISPLAY_WIREFRAME: { + + viewport->set_debug_draw(Viewport::DEBUG_DRAW_WIREFRAME); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), true); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false); + + } break; + case VIEW_DISPLAY_OVERDRAW: { + + viewport->set_debug_draw(Viewport::DEBUG_DRAW_OVERDRAW); + VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_OVERDRAW); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), true); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false); + + } break; + case VIEW_DISPLAY_SHADELESS: { + + viewport->set_debug_draw(Viewport::DEBUG_DRAW_UNSHADED); + VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_SHADELESS); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), true); + + } break; } } @@ -2273,6 +2347,7 @@ SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, Ed surface = memnew(Control); add_child(surface); surface->set_area_as_parent_rect(); + surface->set_clip_contents(true); camera = memnew(Camera); camera->set_disable_gizmo(true); camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + p_index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER)); @@ -2296,12 +2371,18 @@ SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, Ed view_menu->get_popup()->add_check_item(TTR("Orthogonal") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_ORTHOGONAL); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true); view_menu->get_popup()->add_separator(); - view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("Environment")), VIEW_ENVIRONMENT); + view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_normal", TTR("Display Normal")), VIEW_DISPLAY_NORMAL); + view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_wireframe", TTR("Display Wireframe")), VIEW_DISPLAY_WIREFRAME); + view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_overdraw", TTR("Display Overdraw")), VIEW_DISPLAY_OVERDRAW); + view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_unshaded", TTR("Display Unshaded")), VIEW_DISPLAY_SHADELESS); + view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true); + view_menu->get_popup()->add_separator(); + view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("View Environment")), VIEW_ENVIRONMENT); + view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("View Gizmos")), VIEW_GIZMOS); + view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_information", TTR("View Information")), VIEW_INFORMATION); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT), true); view_menu->get_popup()->add_separator(); view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener", TTR("Audio Listener")), VIEW_AUDIO_LISTENER); - view_menu->get_popup()->add_separator(); - view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("Gizmos")), VIEW_GIZMOS); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS), true); view_menu->get_popup()->add_separator(); @@ -2322,6 +2403,13 @@ SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, Ed preview = NULL; gizmo_scale = 1.0; + info = memnew(PanelContainer); + info->set_self_modulate(Color(1, 1, 1, 0.4)); + surface->add_child(info); + info_label = memnew(Label); + info->add_child(info_label); + info->hide(); + freelook_active = false; selection_menu = memnew(PopupMenu); @@ -2341,6 +2429,305 @@ SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, Ed EditorSettings::get_singleton()->connect("settings_changed", this, "update_transform_gizmo_view"); } +////////////////////////////////////////////////////////////// + +void SpatialEditorViewportContainer::_gui_input(const Ref<InputEvent> &p_event) { + + Ref<InputEventMouseButton> mb = p_event; + + if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { + + Vector2 size = get_size(); + + int h_sep = get_constant("separation", "HSplitContainer"); + int v_sep = get_constant("separation", "VSplitContainer"); + + int mid_w = size.width * ratio_h; + int mid_h = size.height * ratio_v; + + dragging_h = mb->get_position().x > (mid_w - h_sep / 2) && mb->get_position().x < (mid_w + h_sep / 2); + dragging_v = mb->get_position().y > (mid_h - v_sep / 2) && mb->get_position().y < (mid_h + v_sep / 2); + + drag_begin_pos = mb->get_position(); + drag_begin_ratio.x = ratio_h; + drag_begin_ratio.y = ratio_v; + + switch (view) { + case VIEW_USE_1_VIEWPORT: { + + dragging_h = false; + dragging_v = false; + + } break; + case VIEW_USE_2_VIEWPORTS: { + + dragging_h = false; + + } break; + case VIEW_USE_2_VIEWPORTS_ALT: { + + dragging_v = false; + + } break; + case VIEW_USE_3_VIEWPORTS: { + + if (dragging_v) + dragging_h = false; + else + dragging_v = false; + + } break; + case VIEW_USE_3_VIEWPORTS_ALT: { + + if (dragging_h) + dragging_v = false; + else + dragging_h = false; + } break; + case VIEW_USE_4_VIEWPORTS: { + + } break; + } + } + + if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { + dragging_h = false; + dragging_v = false; + } + + Ref<InputEventMouseMotion> mm = p_event; + + if (mm.is_valid() && (dragging_h || dragging_v)) { + + if (dragging_h) { + float new_ratio = drag_begin_ratio.x + (mm->get_position().x - drag_begin_pos.x) / get_size().width; + new_ratio = CLAMP(new_ratio, 40 / get_size().width, (get_size().width - 40) / get_size().width); + ratio_h = new_ratio; + queue_sort(); + update(); + } + if (dragging_v) { + float new_ratio = drag_begin_ratio.y + (mm->get_position().y - drag_begin_pos.y) / get_size().height; + new_ratio = CLAMP(new_ratio, 40 / get_size().height, (get_size().height - 40) / get_size().height); + ratio_v = new_ratio; + queue_sort(); + update(); + } + } +} + +void SpatialEditorViewportContainer::_notification(int p_what) { + + if (p_what == NOTIFICATION_MOUSE_ENTER || p_what == NOTIFICATION_MOUSE_EXIT) { + + mouseover = (p_what == NOTIFICATION_MOUSE_ENTER); + update(); + } + + if (p_what == NOTIFICATION_DRAW && mouseover) { + + Ref<Texture> h_grabber = get_icon("grabber", "HSplitContainer"); + + Ref<Texture> v_grabber = get_icon("grabber", "VSplitContainer"); + + Vector2 size = get_size(); + + int h_sep = get_constant("separation", "HSplitContainer"); + + int v_sep = get_constant("separation", "VSplitContainer"); + + int mid_w = size.width * ratio_h; + int mid_h = size.height * ratio_v; + + int size_left = mid_w - h_sep / 2; + int size_bottom = size.height - mid_h - v_sep / 2; + + switch (view) { + + case VIEW_USE_1_VIEWPORT: { + + //nothing to show + + } break; + case VIEW_USE_2_VIEWPORTS: { + + draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2)); + + } break; + case VIEW_USE_2_VIEWPORTS_ALT: { + + draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2)); + + } break; + case VIEW_USE_3_VIEWPORTS: { + + draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2)); + draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, mid_h + v_grabber->get_height() / 2 + (size_bottom - h_grabber->get_height()) / 2)); + + } break; + case VIEW_USE_3_VIEWPORTS_ALT: { + + draw_texture(v_grabber, Vector2((size_left - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2)); + draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2)); + } break; + case VIEW_USE_4_VIEWPORTS: { + + Vector2 half(mid_w, mid_h); + draw_texture(v_grabber, half - v_grabber->get_size() / 2.0); + draw_texture(h_grabber, half - h_grabber->get_size() / 2.0); + + } break; + } + } + + if (p_what == NOTIFICATION_SORT_CHILDREN) { + + SpatialEditorViewport *viewports[4]; + int vc = 0; + for (int i = 0; i < get_child_count(); i++) { + viewports[vc] = get_child(i)->cast_to<SpatialEditorViewport>(); + if (viewports[vc]) { + vc++; + } + } + + ERR_FAIL_COND(vc != 4); + + Size2 size = get_size(); + + if (size.x < 10 || size.y < 10) { + for (int i = 0; i < 4; i++) { + viewports[i]->hide(); + } + return; + } + int h_sep = get_constant("separation", "HSplitContainer"); + + int v_sep = get_constant("separation", "VSplitContainer"); + + int mid_w = size.width * ratio_h; + int mid_h = size.height * ratio_v; + + int size_left = mid_w - h_sep / 2; + int size_right = size.width - mid_w - h_sep / 2; + + int size_top = mid_h - v_sep / 2; + int size_bottom = size.height - mid_h - v_sep / 2; + + switch (view) { + + case VIEW_USE_1_VIEWPORT: { + + for (int i = 1; i < 4; i++) { + + viewports[i]->hide(); + } + + fit_child_in_rect(viewports[0], Rect2(Vector2(), size)); + + } break; + case VIEW_USE_2_VIEWPORTS: { + + for (int i = 1; i < 4; i++) { + + if (i == 1 || i == 3) + viewports[i]->hide(); + else + viewports[i]->show(); + } + + fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top))); + fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size.width, size_bottom))); + + } break; + case VIEW_USE_2_VIEWPORTS_ALT: { + + for (int i = 1; i < 4; i++) { + + if (i == 1 || i == 3) + viewports[i]->hide(); + else + viewports[i]->show(); + } + fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size.height))); + fit_child_in_rect(viewports[2], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height))); + + } break; + case VIEW_USE_3_VIEWPORTS: { + + for (int i = 1; i < 4; i++) { + + if (i == 1) + viewports[i]->hide(); + else + viewports[i]->show(); + } + + fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top))); + fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom))); + fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom))); + + } break; + case VIEW_USE_3_VIEWPORTS_ALT: { + + for (int i = 1; i < 4; i++) { + + if (i == 1) + viewports[i]->hide(); + else + viewports[i]->show(); + } + + fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top))); + fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom))); + fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height))); + + } break; + case VIEW_USE_4_VIEWPORTS: { + + for (int i = 1; i < 4; i++) { + + viewports[i]->show(); + } + + fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top))); + fit_child_in_rect(viewports[1], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size_top))); + fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom))); + fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom))); + + } break; + } + } +} + +void SpatialEditorViewportContainer::set_view(View p_view) { + + view = p_view; + queue_sort(); +} + +SpatialEditorViewportContainer::View SpatialEditorViewportContainer::get_view() { + + return view; +} + +void SpatialEditorViewportContainer::_bind_methods() { + + ClassDB::bind_method("_gui_input", &SpatialEditorViewportContainer::_gui_input); +} + +SpatialEditorViewportContainer::SpatialEditorViewportContainer() { + + view = VIEW_USE_1_VIEWPORT; + mouseover = false; + ratio_h = 0.5; + ratio_v = 0.5; + dragging_v = false; + dragging_h = false; +} + +/////////////////////////////////////////////////////////////////// + SpatialEditor *SpatialEditor::singleton = NULL; SpatialEditorSelectedItem::~SpatialEditorSelectedItem() { @@ -2379,7 +2766,7 @@ void SpatialEditor::update_transform_gizmo() { Transform xf = se->sp->get_global_transform(); if (first) { - center.pos = xf.origin; + center.position = xf.origin; first = false; if (local_gizmo_coords) { gizmo_basis = xf.basis; @@ -2392,7 +2779,7 @@ void SpatialEditor::update_transform_gizmo() { //count++; } - Vector3 pcenter = center.pos + center.size * 0.5; + Vector3 pcenter = center.position + center.size * 0.5; gizmo.visible = !first; gizmo.transform.origin = pcenter; gizmo.transform.basis = gizmo_basis; @@ -2719,12 +3106,7 @@ void SpatialEditor::_menu_item_pressed(int p_option) { } break; case MENU_VIEW_USE_1_VIEWPORT: { - for (int i = 1; i < 4; i++) { - - viewports[i]->hide(); - } - - viewports[0]->set_area_as_parent_rect(); + viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_1_VIEWPORT); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); @@ -2736,17 +3118,7 @@ void SpatialEditor::_menu_item_pressed(int p_option) { } break; case MENU_VIEW_USE_2_VIEWPORTS: { - for (int i = 1; i < 4; i++) { - - if (i == 1 || i == 3) - viewports[i]->hide(); - else - viewports[i]->show(); - } - viewports[0]->set_area_as_parent_rect(); - //viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5); - viewports[2]->set_area_as_parent_rect(); - //viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5); + viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true); @@ -2758,17 +3130,7 @@ void SpatialEditor::_menu_item_pressed(int p_option) { } break; case MENU_VIEW_USE_2_VIEWPORTS_ALT: { - for (int i = 1; i < 4; i++) { - - if (i == 1 || i == 3) - viewports[i]->hide(); - else - viewports[i]->show(); - } - viewports[0]->set_area_as_parent_rect(); - //viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5); - viewports[2]->set_area_as_parent_rect(); - //viewports[2]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5); + viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS_ALT); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); @@ -2780,21 +3142,7 @@ void SpatialEditor::_menu_item_pressed(int p_option) { } break; case MENU_VIEW_USE_3_VIEWPORTS: { - for (int i = 1; i < VIEWPORTS_COUNT; i++) { - - if (i == 1) - viewports[i]->hide(); - else - viewports[i]->show(); - } - viewports[0]->set_area_as_parent_rect(); - //viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5); - viewports[2]->set_area_as_parent_rect(); - //viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5); - //viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5); - viewports[3]->set_area_as_parent_rect(); - //viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5); - //viewports[3]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5); + viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); @@ -2806,21 +3154,7 @@ void SpatialEditor::_menu_item_pressed(int p_option) { } break; case MENU_VIEW_USE_3_VIEWPORTS_ALT: { - for (int i = 1; i < VIEWPORTS_COUNT; i++) { - - if (i == 1) - viewports[i]->hide(); - else - viewports[i]->show(); - } - viewports[0]->set_area_as_parent_rect(); - //viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5); - //viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5); - viewports[2]->set_area_as_parent_rect(); - //viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5); - //viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5); - viewports[3]->set_area_as_parent_rect(); - //viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5); + viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS_ALT); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); @@ -2832,22 +3166,7 @@ void SpatialEditor::_menu_item_pressed(int p_option) { } break; case MENU_VIEW_USE_4_VIEWPORTS: { - for (int i = 1; i < VIEWPORTS_COUNT; i++) { - - viewports[i]->show(); - } - viewports[0]->set_area_as_parent_rect(); - //viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5); - //viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5); - viewports[1]->set_area_as_parent_rect(); - //viewports[1]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5); - //viewports[1]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5); - viewports[2]->set_area_as_parent_rect(); - //viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5); - //viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5); - viewports[3]->set_area_as_parent_rect(); - //viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5); - //viewports[3]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5); + viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_4_VIEWPORTS); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); @@ -2857,43 +3176,6 @@ void SpatialEditor::_menu_item_pressed(int p_option) { view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false); } break; - case MENU_VIEW_DISPLAY_NORMAL: { - - VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_DISABLED); - - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), true); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false); - - } break; - case MENU_VIEW_DISPLAY_WIREFRAME: { - - VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_WIREFRAME); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), true); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false); - - } break; - case MENU_VIEW_DISPLAY_OVERDRAW: { - - VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_OVERDRAW); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), true); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false); - - } break; - case MENU_VIEW_DISPLAY_SHADELESS: { - - VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_SHADELESS); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false); - view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), true); - - } break; case MENU_VIEW_ORIGIN: { bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option)); @@ -3055,8 +3337,8 @@ void SpatialEditor::_init_indicators() { for (int i = 0; i < 3; i++) { - move_gizmo[i] = Ref<Mesh>(memnew(Mesh)); - rotate_gizmo[i] = Ref<Mesh>(memnew(Mesh)); + move_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh)); + rotate_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh)); Ref<SpatialMaterial> mat = memnew(SpatialMaterial); mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true); @@ -3242,17 +3524,6 @@ void SpatialEditor::_unhandled_key_input(Ref<InputEvent> p_event) { else if (ED_IS_SHORTCUT("spatial_editor/tool_scale", p_event)) _menu_item_pressed(MENU_TOOL_SCALE); - - else if (ED_IS_SHORTCUT("spatial_editor/display_wireframe", p_event)) { - if (k->get_shift() || k->get_control() || k->get_command()) - return; - - if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME))) { - _menu_item_pressed(MENU_VIEW_DISPLAY_NORMAL); - } else { - _menu_item_pressed(MENU_VIEW_DISPLAY_WIREFRAME); - } - } } } } @@ -3419,9 +3690,6 @@ void SpatialEditor::clear() { viewports[i]->reset(); } - _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT); - _menu_item_pressed(MENU_VIEW_DISPLAY_NORMAL); - VisualServer::get_singleton()->instance_set_visible(origin_instance, true); view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true); for (int i = 0; i < 3; ++i) { @@ -3566,17 +3834,11 @@ SpatialEditor::SpatialEditor(EditorNode *p_editor) { p->add_check_shortcut(ED_SHORTCUT("spatial_editor/4_viewports", TTR("4 Viewports"), KEY_MASK_CMD + KEY_4), MENU_VIEW_USE_4_VIEWPORTS); p->add_separator(); - p->add_check_shortcut(ED_SHORTCUT("spatial_editor/display_normal", TTR("Display Normal")), MENU_VIEW_DISPLAY_NORMAL); - p->add_check_shortcut(ED_SHORTCUT("spatial_editor/display_wireframe", TTR("Display Wireframe")), MENU_VIEW_DISPLAY_WIREFRAME); - p->add_check_shortcut(ED_SHORTCUT("spatial_editor/display_overdraw", TTR("Display Overdraw")), MENU_VIEW_DISPLAY_OVERDRAW); - p->add_check_shortcut(ED_SHORTCUT("spatial_editor/display_shadeless", TTR("Display Shadeless")), MENU_VIEW_DISPLAY_SHADELESS); - p->add_separator(); p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_origin", TTR("View Origin")), MENU_VIEW_ORIGIN); p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid", TTR("View Grid")), MENU_VIEW_GRID); p->add_separator(); p->add_shortcut(ED_SHORTCUT("spatial_editor/settings", TTR("Settings")), MENU_VIEW_CAMERA_SETTINGS); - p->set_item_checked(p->get_item_index(MENU_VIEW_DISPLAY_NORMAL), true); p->set_item_checked(p->get_item_index(MENU_VIEW_ORIGIN), true); p->set_item_checked(p->get_item_index(MENU_VIEW_GRID), true); @@ -3591,7 +3853,7 @@ SpatialEditor::SpatialEditor(EditorNode *p_editor) { shader_split = memnew(VSplitContainer); shader_split->set_h_size_flags(SIZE_EXPAND_FILL); palette_split->add_child(shader_split); - viewport_base = memnew(Control); + viewport_base = memnew(SpatialEditorViewportContainer); shader_split->add_child(viewport_base); viewport_base->set_v_size_flags(SIZE_EXPAND_FILL); for (int i = 0; i < VIEWPORTS_COUNT; i++) { diff --git a/editor/plugins/spatial_editor_plugin.h b/editor/plugins/spatial_editor_plugin.h index 88245ad0dc..6b05a8b370 100644 --- a/editor/plugins/spatial_editor_plugin.h +++ b/editor/plugins/spatial_editor_plugin.h @@ -84,6 +84,11 @@ class SpatialEditorViewport : public Control { VIEW_ORTHOGONAL, VIEW_AUDIO_LISTENER, VIEW_GIZMOS, + VIEW_INFORMATION, + VIEW_DISPLAY_NORMAL, + VIEW_DISPLAY_WIREFRAME, + VIEW_DISPLAY_OVERDRAW, + VIEW_DISPLAY_SHADELESS, }; public: @@ -116,6 +121,9 @@ private: bool freelook_active; + PanelContainer *info; + Label *info_label; + struct _RayResult { Spatial *item; @@ -287,6 +295,43 @@ public: ~SpatialEditorSelectedItem(); }; +class SpatialEditorViewportContainer : public Container { + + GDCLASS(SpatialEditorViewportContainer, Container) +public: + enum View { + VIEW_USE_1_VIEWPORT, + VIEW_USE_2_VIEWPORTS, + VIEW_USE_2_VIEWPORTS_ALT, + VIEW_USE_3_VIEWPORTS, + VIEW_USE_3_VIEWPORTS_ALT, + VIEW_USE_4_VIEWPORTS, + }; + +private: + View view; + bool mouseover; + float ratio_h; + float ratio_v; + + bool dragging_v; + bool dragging_h; + Vector2 drag_begin_pos; + Vector2 drag_begin_ratio; + + void _gui_input(const Ref<InputEvent> &p_event); + +protected: + void _notification(int p_what); + static void _bind_methods(); + +public: + void set_view(View p_view); + View get_view(); + + SpatialEditorViewportContainer(); +}; + class SpatialEditor : public VBoxContainer { GDCLASS(SpatialEditor, VBoxContainer); @@ -309,7 +354,7 @@ private: EditorNode *editor; EditorSelection *editor_selection; - Control *viewport_base; + SpatialEditorViewportContainer *viewport_base; SpatialEditorViewport *viewports[VIEWPORTS_COUNT]; VSplitContainer *shader_split; HSplitContainer *palette_split; @@ -330,13 +375,13 @@ private: bool grid_enable[3]; //should be always visible if true bool grid_enabled; - Ref<Mesh> move_gizmo[3], rotate_gizmo[3]; + Ref<ArrayMesh> move_gizmo[3], rotate_gizmo[3]; Ref<SpatialMaterial> gizmo_color[3]; Ref<SpatialMaterial> gizmo_hl; int over_gizmo_handle; - Ref<Mesh> selection_box; + Ref<ArrayMesh> selection_box; RID indicators; RID indicators_instance; RID cursor_mesh; @@ -379,10 +424,6 @@ private: MENU_VIEW_USE_3_VIEWPORTS, MENU_VIEW_USE_3_VIEWPORTS_ALT, MENU_VIEW_USE_4_VIEWPORTS, - MENU_VIEW_DISPLAY_NORMAL, - MENU_VIEW_DISPLAY_WIREFRAME, - MENU_VIEW_DISPLAY_OVERDRAW, - MENU_VIEW_DISPLAY_SHADELESS, MENU_VIEW_ORIGIN, MENU_VIEW_GRID, MENU_VIEW_CAMERA_SETTINGS, @@ -472,8 +513,8 @@ public: float get_rotate_snap() const { return snap_rotate->get_text().to_double(); } float get_scale_snap() const { return snap_scale->get_text().to_double(); } - Ref<Mesh> get_move_gizmo(int idx) const { return move_gizmo[idx]; } - Ref<Mesh> get_rotate_gizmo(int idx) const { return rotate_gizmo[idx]; } + Ref<ArrayMesh> get_move_gizmo(int idx) const { return move_gizmo[idx]; } + Ref<ArrayMesh> get_rotate_gizmo(int idx) const { return rotate_gizmo[idx]; } void update_transform_gizmo(); diff --git a/editor/plugins/texture_region_editor_plugin.cpp b/editor/plugins/texture_region_editor_plugin.cpp index 799bfbf358..976a7b6271 100644 --- a/editor/plugins/texture_region_editor_plugin.cpp +++ b/editor/plugins/texture_region_editor_plugin.cpp @@ -114,10 +114,10 @@ void TextureRegionEditor::_region_draw() { for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) { Rect2 r = E->get(); Vector2 endpoints[4] = { - mtx.basis_xform(r.pos), - mtx.basis_xform(r.pos + Vector2(r.size.x, 0)), - mtx.basis_xform(r.pos + r.size), - mtx.basis_xform(r.pos + Vector2(0, r.size.y)) + mtx.basis_xform(r.position), + mtx.basis_xform(r.position + Vector2(r.size.x, 0)), + mtx.basis_xform(r.position + r.size), + mtx.basis_xform(r.position + Vector2(0, r.size.y)) }; for (int i = 0; i < 4; i++) { int next = (i + 1) % 4; @@ -132,10 +132,10 @@ void TextureRegionEditor::_region_draw() { scroll_rect.expand_to(mtx.basis_xform(edit_draw->get_size())); Vector2 endpoints[4] = { - mtx.basis_xform(rect.pos), - mtx.basis_xform(rect.pos + Vector2(rect.size.x, 0)), - mtx.basis_xform(rect.pos + rect.size), - mtx.basis_xform(rect.pos + Vector2(0, rect.size.y)) + mtx.basis_xform(rect.position), + mtx.basis_xform(rect.position + Vector2(rect.size.x, 0)), + mtx.basis_xform(rect.position + rect.size), + mtx.basis_xform(rect.position + Vector2(0, rect.size.y)) }; Color color(0.9, 0.5, 0.5); for (int i = 0; i < 4; i++) { @@ -162,14 +162,14 @@ void TextureRegionEditor::_region_draw() { scroll_rect = scroll_rect.grow(200); updating_scroll = true; - hscroll->set_min(scroll_rect.pos.x); - hscroll->set_max(scroll_rect.pos.x + scroll_rect.size.x); + hscroll->set_min(scroll_rect.position.x); + hscroll->set_max(scroll_rect.position.x + scroll_rect.size.x); hscroll->set_page(edit_draw->get_size().x); hscroll->set_value(draw_ofs.x); hscroll->set_step(0.001); - vscroll->set_min(scroll_rect.pos.y); - vscroll->set_max(scroll_rect.pos.y + scroll_rect.size.y); + vscroll->set_min(scroll_rect.position.y); + vscroll->set_max(scroll_rect.position.y + scroll_rect.size.y); vscroll->set_page(edit_draw->get_size().y); vscroll->set_value(draw_ofs.y); vscroll->set_step(0.001); @@ -208,14 +208,14 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) { mtx.scale_basis(Vector2(draw_zoom, draw_zoom)); Vector2 endpoints[8] = { - mtx.xform(rect.pos) + Vector2(-4, -4), - mtx.xform(rect.pos + Vector2(rect.size.x / 2, 0)) + Vector2(0, -4), - mtx.xform(rect.pos + Vector2(rect.size.x, 0)) + Vector2(4, -4), - mtx.xform(rect.pos + Vector2(rect.size.x, rect.size.y / 2)) + Vector2(4, 0), - mtx.xform(rect.pos + rect.size) + Vector2(4, 4), - mtx.xform(rect.pos + Vector2(rect.size.x / 2, rect.size.y)) + Vector2(0, 4), - mtx.xform(rect.pos + Vector2(0, rect.size.y)) + Vector2(-4, 4), - mtx.xform(rect.pos + Vector2(0, rect.size.y / 2)) + Vector2(-4, 0) + mtx.xform(rect.position) + Vector2(-4, -4), + mtx.xform(rect.position + Vector2(rect.size.x / 2, 0)) + Vector2(0, -4), + mtx.xform(rect.position + Vector2(rect.size.x, 0)) + Vector2(4, -4), + mtx.xform(rect.position + Vector2(rect.size.x, rect.size.y / 2)) + Vector2(4, 0), + mtx.xform(rect.position + rect.size) + Vector2(4, 4), + mtx.xform(rect.position + Vector2(rect.size.x / 2, rect.size.y)) + Vector2(0, 4), + mtx.xform(rect.position + Vector2(0, rect.size.y)) + Vector2(-4, 4), + mtx.xform(rect.position + Vector2(0, rect.size.y / 2)) + Vector2(-4, 0) }; Ref<InputEventMouseButton> mb; @@ -239,31 +239,31 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) { margins[3] = obj_styleBox->get_margin_size(MARGIN_RIGHT); } Vector2 pos[4] = { - mtx.basis_xform(rect.pos + Vector2(0, margins[0])) - draw_ofs, - mtx.basis_xform(rect.pos + rect.size - Vector2(0, margins[1])) - draw_ofs, - mtx.basis_xform(rect.pos + Vector2(margins[2], 0)) - draw_ofs, - mtx.basis_xform(rect.pos + rect.size - Vector2(margins[3], 0)) - draw_ofs + mtx.basis_xform(rect.position + Vector2(0, margins[0])) - draw_ofs, + mtx.basis_xform(rect.position + rect.size - Vector2(0, margins[1])) - draw_ofs, + mtx.basis_xform(rect.position + Vector2(margins[2], 0)) - draw_ofs, + mtx.basis_xform(rect.position + rect.size - Vector2(margins[3], 0)) - draw_ofs }; - if (Math::abs(mb->get_pos().y - pos[0].y) < 8) { + if (Math::abs(mb->get_position().y - pos[0].y) < 8) { edited_margin = 0; prev_margin = margins[0]; - } else if (Math::abs(mb->get_pos().y - pos[1].y) < 8) { + } else if (Math::abs(mb->get_position().y - pos[1].y) < 8) { edited_margin = 1; prev_margin = margins[1]; - } else if (Math::abs(mb->get_pos().x - pos[2].x) < 8) { + } else if (Math::abs(mb->get_position().x - pos[2].x) < 8) { edited_margin = 2; prev_margin = margins[2]; - } else if (Math::abs(mb->get_pos().x - pos[3].x) < 8) { + } else if (Math::abs(mb->get_position().x - pos[3].x) < 8) { edited_margin = 3; prev_margin = margins[3]; } if (edited_margin >= 0) { - drag_from = Vector2(mb->get_pos().x, mb->get_pos().y); + drag_from = Vector2(mb->get_position().x, mb->get_position().y); drag = true; } } if (edited_margin < 0 && snap_mode == SNAP_AUTOSLICE) { - Vector2 point = mtx.affine_inverse().xform(Vector2(mb->get_pos().x, mb->get_pos().y)); + Vector2 point = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y)); for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) { if (E->get().has_point(point)) { rect = E->get(); @@ -277,8 +277,8 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) { r = obj_styleBox->get_region_rect(); else if (atlas_tex.is_valid()) r = atlas_tex->get_region(); - rect.expand_to(r.pos); - rect.expand_to(r.pos + r.size); + rect.expand_to(r.position); + rect.expand_to(r.position + r.size); } undo_redo->create_action("Set Region Rect"); if (node_sprite) { @@ -301,7 +301,7 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) { } } } else if (edited_margin < 0) { - drag_from = mtx.affine_inverse().xform(Vector2(mb->get_pos().x, mb->get_pos().y)); + drag_from = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y)); if (snap_mode == SNAP_PIXEL) drag_from = drag_from.snapped(Vector2(1, 1)); else if (snap_mode == SNAP_GRID) @@ -318,7 +318,7 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) { for (int i = 0; i < 8; i++) { Vector2 tuv = endpoints[i]; - if (tuv.distance_to(Vector2(mb->get_pos().x, mb->get_pos().y)) < 8) { + if (tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)) < 8) { drag_index = i; } } @@ -408,13 +408,13 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) { if (edited_margin >= 0) { float new_margin; if (edited_margin == 0) - new_margin = prev_margin + (mm->get_pos().y - drag_from.y) / draw_zoom; + new_margin = prev_margin + (mm->get_position().y - drag_from.y) / draw_zoom; else if (edited_margin == 1) - new_margin = prev_margin - (mm->get_pos().y - drag_from.y) / draw_zoom; + new_margin = prev_margin - (mm->get_position().y - drag_from.y) / draw_zoom; else if (edited_margin == 2) - new_margin = prev_margin + (mm->get_pos().x - drag_from.x) / draw_zoom; + new_margin = prev_margin + (mm->get_position().x - drag_from.x) / draw_zoom; else if (edited_margin == 3) - new_margin = prev_margin - (mm->get_pos().x - drag_from.x) / draw_zoom; + new_margin = prev_margin - (mm->get_position().x - drag_from.x) / draw_zoom; if (new_margin < 0) new_margin = 0; static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT }; @@ -423,7 +423,7 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) { if (obj_styleBox.is_valid()) obj_styleBox->set_margin_size(m[edited_margin], new_margin); } else { - Vector2 new_pos = mtx.affine_inverse().xform(mm->get_pos()); + Vector2 new_pos = mtx.affine_inverse().xform(mm->get_position()); if (snap_mode == SNAP_PIXEL) new_pos = new_pos.snapped(Vector2(1, 1)); else if (snap_mode == SNAP_GRID) @@ -439,49 +439,49 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) { switch (drag_index) { case 0: { - Vector2 p = rect_prev.pos + rect_prev.size; + Vector2 p = rect_prev.position + rect_prev.size; rect = Rect2(p, Size2()); rect.expand_to(new_pos); apply_rect(rect); } break; case 1: { - Vector2 p = rect_prev.pos + Vector2(0, rect_prev.size.y); + Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y); rect = Rect2(p, Size2(rect_prev.size.x, 0)); rect.expand_to(new_pos); apply_rect(rect); } break; case 2: { - Vector2 p = rect_prev.pos + Vector2(0, rect_prev.size.y); + Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y); rect = Rect2(p, Size2()); rect.expand_to(new_pos); apply_rect(rect); } break; case 3: { - Vector2 p = rect_prev.pos; + Vector2 p = rect_prev.position; rect = Rect2(p, Size2(0, rect_prev.size.y)); rect.expand_to(new_pos); apply_rect(rect); } break; case 4: { - Vector2 p = rect_prev.pos; + Vector2 p = rect_prev.position; rect = Rect2(p, Size2()); rect.expand_to(new_pos); apply_rect(rect); } break; case 5: { - Vector2 p = rect_prev.pos; + Vector2 p = rect_prev.position; rect = Rect2(p, Size2(rect_prev.size.x, 0)); rect.expand_to(new_pos); apply_rect(rect); } break; case 6: { - Vector2 p = rect_prev.pos + Vector2(rect_prev.size.x, 0); + Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0); rect = Rect2(p, Size2()); rect.expand_to(new_pos); apply_rect(rect); } break; case 7: { - Vector2 p = rect_prev.pos + Vector2(rect_prev.size.x, 0); + Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0); rect = Rect2(p, Size2(0, rect_prev.size.y)); rect.expand_to(new_pos); apply_rect(rect); @@ -693,7 +693,7 @@ void TextureRegionEditor::_edit_region() { if (grown.has_point(Point2(x, y))) { E->get().expand_to(Point2(x, y)); E->get().expand_to(Point2(x + 1, y + 1)); - x = E->get().pos.x + E->get().size.x - 1; + x = E->get().position.x + E->get().size.x - 1; bool merged = true; while (merged) { merged = false; @@ -706,8 +706,8 @@ void TextureRegionEditor::_edit_region() { if (F == E) continue; if (E->get().grow(1).intersects(F->get())) { - E->get().expand_to(F->get().pos); - E->get().expand_to(F->get().pos + F->get().size); + E->get().expand_to(F->get().position); + E->get().expand_to(F->get().position + F->get().size); if (F->prev()) { F = F->prev(); autoslice_cache.erase(F->next()); diff --git a/editor/plugins/tile_map_editor_plugin.cpp b/editor/plugins/tile_map_editor_plugin.cpp index 9f7a41b8b6..d12b979121 100644 --- a/editor/plugins/tile_map_editor_plugin.cpp +++ b/editor/plugins/tile_map_editor_plugin.cpp @@ -98,8 +98,8 @@ void TileMapEditor::_menu_option(int p_option) { return; undo_redo->create_action("Erase Selection"); - for (int i = rectangle.pos.y; i <= rectangle.pos.y + rectangle.size.y; i++) { - for (int j = rectangle.pos.x; j <= rectangle.pos.x + rectangle.size.x; j++) { + for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) { + for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) { _set_cell(Point2i(j, i), TileMap::INVALID_CELL, false, false, false, true); } @@ -333,7 +333,7 @@ PoolVector<Vector2> TileMapEditor::_bucket_fill(const Point2i &p_start, bool era } Rect2i r = node->get_item_rect(); - r.pos = r.pos / node->get_cell_size(); + r.position = r.position / node->get_cell_size(); r.size = r.size / node->get_cell_size(); int area = r.get_area(); @@ -349,7 +349,7 @@ PoolVector<Vector2> TileMapEditor::_bucket_fill(const Point2i &p_start, bool era invalidate_cache = true; } // Tile ID changed or position wasn't visited by the previous fill - int loc = (p_start.x - r.get_pos().x) + (p_start.y - r.get_pos().y) * r.get_size().x; + int loc = (p_start.x - r.position.x) + (p_start.y - r.position.y) * r.get_size().x; if (prev_id != bucket_cache_tile || !bucket_cache_visited[loc]) { invalidate_cache = true; } @@ -380,7 +380,7 @@ PoolVector<Vector2> TileMapEditor::_bucket_fill(const Point2i &p_start, bool era if (node->get_cell(n.x, n.y) == prev_id) { if (preview) { - int loc = (n.x - r.get_pos().x) + (n.y - r.get_pos().y) * r.get_size().x; + int loc = (n.x - r.position.x) + (n.y - r.position.y) * r.get_size().x; if (bucket_cache_visited[loc]) continue; bucket_cache_visited[loc] = true; @@ -441,7 +441,7 @@ void TileMapEditor::_select(const Point2i &p_from, const Point2i &p_to) { SWAP(begin.y, end.y); } - rectangle.pos = begin; + rectangle.position = begin; rectangle.size = end - begin; canvas_item_editor->update(); @@ -460,7 +460,7 @@ void TileMapEditor::_draw_cell(int p_cell, const Point2i &p_point, bool p_flip_h Size2 sc = p_xform.get_scale(); Rect2 rect = Rect2(); - rect.pos = node->map_to_world(p_point) + node->get_cell_draw_offset(); + rect.position = node->map_to_world(p_point) + node->get_cell_draw_offset(); if (r.has_no_area()) { rect.size = t->get_size(); @@ -490,42 +490,42 @@ void TileMapEditor::_draw_cell(int p_cell, const Point2i &p_point, bool p_flip_h if (node->get_tile_origin() == TileMap::TILE_ORIGIN_TOP_LEFT) { - rect.pos += tile_ofs; + rect.position += tile_ofs; } else if (node->get_tile_origin() == TileMap::TILE_ORIGIN_BOTTOM_LEFT) { Size2 cell_size = node->get_cell_size(); - rect.pos += tile_ofs; + rect.position += tile_ofs; if (p_transpose) { if (p_flip_h) - rect.pos.x -= cell_size.x; + rect.position.x -= cell_size.x; else - rect.pos.x += cell_size.x; + rect.position.x += cell_size.x; } else { if (p_flip_v) - rect.pos.y -= cell_size.y; + rect.position.y -= cell_size.y; else - rect.pos.y += cell_size.y; + rect.position.y += cell_size.y; } } else if (node->get_tile_origin() == TileMap::TILE_ORIGIN_CENTER) { - rect.pos += node->get_cell_size() / 2; + rect.position += node->get_cell_size() / 2; Vector2 s = r.size; Vector2 center = (s / 2) - tile_ofs; if (p_flip_h) - rect.pos.x -= s.x - center.x; + rect.position.x -= s.x - center.x; else - rect.pos.x -= center.x; + rect.position.x -= center.x; if (p_flip_v) - rect.pos.y -= s.y - center.y; + rect.position.y -= s.y - center.y; else - rect.pos.y -= center.y; + rect.position.y -= center.y; } - rect.pos = p_xform.xform(rect.pos); + rect.position = p_xform.xform(rect.position); rect.size *= sc; if (r.has_no_area()) @@ -560,9 +560,9 @@ void TileMapEditor::_update_copydata() { if (!selection_active) return; - for (int i = rectangle.pos.y; i <= rectangle.pos.y + rectangle.size.y; i++) { + for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) { - for (int j = rectangle.pos.x; j <= rectangle.pos.x + rectangle.size.x; j++) { + for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) { TileData tcd; @@ -737,8 +737,8 @@ bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (id != TileMap::INVALID_CELL) { undo_redo->create_action("Rectangle Paint"); - for (int i = rectangle.pos.y; i <= rectangle.pos.y + rectangle.size.y; i++) { - for (int j = rectangle.pos.x; j <= rectangle.pos.x + rectangle.size.x; j++) { + for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) { + for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) { _set_cell(Point2i(j, i), id, flip_h, flip_v, transpose, true); } @@ -749,7 +749,7 @@ bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) { } } else if (tool == TOOL_DUPLICATING) { - Point2 ofs = over_tile - rectangle.pos; + Point2 ofs = over_tile - rectangle.position; undo_redo->create_action(TTR("Duplicate")); for (List<TileData>::Element *E = copydata.front(); E; E = E->next()) { @@ -826,7 +826,7 @@ bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) { paint_undo.clear(); - Point2 local = node->world_to_map(xform_inv.xform(mb->get_pos())); + Point2 local = node->world_to_map(xform_inv.xform(mb->get_position())); if (mb->get_shift()) { @@ -900,7 +900,7 @@ bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (mm.is_valid()) { - Point2i new_over_tile = node->world_to_map(xform_inv.xform(mm->get_pos())); + Point2i new_over_tile = node->world_to_map(xform_inv.xform(mm->get_position())); if (new_over_tile != over_tile) { @@ -985,8 +985,8 @@ bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) { paint_undo.clear(); - for (int i = rectangle.pos.y; i <= rectangle.pos.y + rectangle.size.y; i++) { - for (int j = rectangle.pos.x; j <= rectangle.pos.x + rectangle.size.x; j++) { + for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) { + for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) { Point2i tile = Point2i(j, i); paint_undo[tile] = _get_op_from_cell(tile); @@ -1103,7 +1103,7 @@ void TileMapEditor::_canvas_draw() { Size2 screen_size = canvas_item_editor->get_size(); { Rect2 aabb; - aabb.pos = node->world_to_map(xform_inv.xform(Vector2())); + aabb.position = node->world_to_map(xform_inv.xform(Vector2())); aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(0, screen_size.height)))); aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(screen_size.width, 0)))); aabb.expand_to(node->world_to_map(xform_inv.xform(screen_size))); @@ -1113,10 +1113,10 @@ void TileMapEditor::_canvas_draw() { int max_lines = 2000; //avoid crash if size too smal - for (int i = (si.pos.x) - 1; i <= (si.pos.x + si.size.x); i++) { + for (int i = (si.position.x) - 1; i <= (si.position.x + si.size.x); i++) { - Vector2 from = xform.xform(node->map_to_world(Vector2(i, si.pos.y))); - Vector2 to = xform.xform(node->map_to_world(Vector2(i, si.pos.y + si.size.y + 1))); + Vector2 from = xform.xform(node->map_to_world(Vector2(i, si.position.y))); + Vector2 to = xform.xform(node->map_to_world(Vector2(i, si.position.y + si.size.y + 1))); Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2); canvas_item_editor->draw_line(from, to, col, 1); @@ -1127,9 +1127,9 @@ void TileMapEditor::_canvas_draw() { int max_lines = 10000; //avoid crash if size too smal - for (int i = (si.pos.x) - 1; i <= (si.pos.x + si.size.x); i++) { + for (int i = (si.position.x) - 1; i <= (si.position.x + si.size.x); i++) { - for (int j = (si.pos.y) - 1; j <= (si.pos.y + si.size.y); j++) { + for (int j = (si.position.y) - 1; j <= (si.position.y + si.size.y); j++) { Vector2 ofs; if (ABS(j) & 1) { @@ -1151,10 +1151,10 @@ void TileMapEditor::_canvas_draw() { if (node->get_half_offset() != TileMap::HALF_OFFSET_Y) { - for (int i = (si.pos.y) - 1; i <= (si.pos.y + si.size.y); i++) { + for (int i = (si.position.y) - 1; i <= (si.position.y + si.size.y); i++) { - Vector2 from = xform.xform(node->map_to_world(Vector2(si.pos.x, i))); - Vector2 to = xform.xform(node->map_to_world(Vector2(si.pos.x + si.size.x + 1, i))); + Vector2 from = xform.xform(node->map_to_world(Vector2(si.position.x, i))); + Vector2 to = xform.xform(node->map_to_world(Vector2(si.position.x + si.size.x + 1, i))); Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2); canvas_item_editor->draw_line(from, to, col, 1); @@ -1164,9 +1164,9 @@ void TileMapEditor::_canvas_draw() { } } else { - for (int i = (si.pos.y) - 1; i <= (si.pos.y + si.size.y); i++) { + for (int i = (si.position.y) - 1; i <= (si.position.y + si.size.y); i++) { - for (int j = (si.pos.x) - 1; j <= (si.pos.x + si.size.x); j++) { + for (int j = (si.position.x) - 1; j <= (si.position.x + si.size.x); j++) { Vector2 ofs; if (ABS(j) & 1) { @@ -1188,10 +1188,10 @@ void TileMapEditor::_canvas_draw() { if (selection_active) { Vector<Vector2> points; - points.push_back(xform.xform(node->map_to_world((rectangle.pos)))); - points.push_back(xform.xform(node->map_to_world((rectangle.pos + Point2(rectangle.size.x + 1, 0))))); - points.push_back(xform.xform(node->map_to_world((rectangle.pos + Point2(rectangle.size.x + 1, rectangle.size.y + 1))))); - points.push_back(xform.xform(node->map_to_world((rectangle.pos + Point2(0, rectangle.size.y + 1))))); + points.push_back(xform.xform(node->map_to_world((rectangle.position)))); + points.push_back(xform.xform(node->map_to_world((rectangle.position + Point2(rectangle.size.x + 1, 0))))); + points.push_back(xform.xform(node->map_to_world((rectangle.position + Point2(rectangle.size.x + 1, rectangle.size.y + 1))))); + points.push_back(xform.xform(node->map_to_world((rectangle.position + Point2(0, rectangle.size.y + 1))))); canvas_item_editor->draw_colored_polygon(points, Color(0.2, 0.8, 1, 0.4)); } @@ -1248,8 +1248,8 @@ void TileMapEditor::_canvas_draw() { if (id == TileMap::INVALID_CELL) return; - for (int i = rectangle.pos.y; i <= rectangle.pos.y + rectangle.size.y; i++) { - for (int j = rectangle.pos.x; j <= rectangle.pos.x + rectangle.size.x; j++) { + for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) { + for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) { _draw_cell(id, Point2i(j, i), flip_h, flip_v, transpose, xform); } @@ -1264,7 +1264,7 @@ void TileMapEditor::_canvas_draw() { if (ts.is_null()) return; - Point2 ofs = over_tile - rectangle.pos; + Point2 ofs = over_tile - rectangle.position; for (List<TileData>::Element *E = copydata.front(); E; E = E->next()) { @@ -1277,13 +1277,13 @@ void TileMapEditor::_canvas_draw() { } Rect2i duplicate = rectangle; - duplicate.pos = over_tile; + duplicate.position = over_tile; Vector<Vector2> points; - points.push_back(xform.xform(node->map_to_world(duplicate.pos))); - points.push_back(xform.xform(node->map_to_world((duplicate.pos + Point2(duplicate.size.x + 1, 0))))); - points.push_back(xform.xform(node->map_to_world((duplicate.pos + Point2(duplicate.size.x + 1, duplicate.size.y + 1))))); - points.push_back(xform.xform(node->map_to_world((duplicate.pos + Point2(0, duplicate.size.y + 1))))); + points.push_back(xform.xform(node->map_to_world(duplicate.position))); + points.push_back(xform.xform(node->map_to_world((duplicate.position + Point2(duplicate.size.x + 1, 0))))); + points.push_back(xform.xform(node->map_to_world((duplicate.position + Point2(duplicate.size.x + 1, duplicate.size.y + 1))))); + points.push_back(xform.xform(node->map_to_world((duplicate.position + Point2(0, duplicate.size.y + 1))))); canvas_item_editor->draw_colored_polygon(points, Color(0.2, 1.0, 0.8, 0.2)); diff --git a/editor/project_manager.cpp b/editor/project_manager.cpp index a4e8ef70ce..a3d3d42110 100644 --- a/editor/project_manager.cpp +++ b/editor/project_manager.cpp @@ -914,8 +914,9 @@ void ProjectManager::_on_project_created(const String &dir) { _update_scroll_pos(dir); } else { _load_recent_projects(); - scroll->connect("draw", this, "_update_scroll_pos", varray(dir), CONNECT_ONESHOT); + _update_scroll_pos(dir); } + _open_project(); } void ProjectManager::_update_scroll_pos(const String &dir) { @@ -1469,7 +1470,7 @@ ProjectListFilter::ProjectListFilter() { _current_filter = FILTER_NAME; filter_option = memnew(OptionButton); - filter_option->set_custom_minimum_size(Size2(80, 10)); + filter_option->set_custom_minimum_size(Size2(80 * EDSCALE, 10 * EDSCALE)); filter_option->set_clip_text(true); filter_option->connect("item_selected", this, "_filter_option_selected"); add_child(filter_option); diff --git a/editor/project_settings.cpp b/editor/project_settings.cpp index 1c4ca3cb58..8ef7bd427f 100644 --- a/editor/project_settings.cpp +++ b/editor/project_settings.cpp @@ -430,8 +430,8 @@ void ProjectSettings::_action_button_pressed(Object *p_obj, int p_column, int p_ if (p_id == 1) { Point2 ofs = input_editor->get_global_position(); Rect2 ir = input_editor->get_item_rect(ti); - ir.pos.y -= input_editor->get_scroll().y; - ofs += ir.pos + ir.size; + ir.position.y -= input_editor->get_scroll().y; + ofs += ir.position + ir.size; ofs.x -= 100; popup_add->set_position(ofs); popup_add->popup(); diff --git a/editor/property_editor.cpp b/editor/property_editor.cpp index 1c8a1c0ee0..3d3fecc8f9 100644 --- a/editor/property_editor.cpp +++ b/editor/property_editor.cpp @@ -678,8 +678,8 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: field_names.push_back("h"); config_value_editors(4, 4, 10, field_names); Rect2 r = v; - value_editor[0]->set_text(String::num(r.pos.x)); - value_editor[1]->set_text(String::num(r.pos.y)); + value_editor[0]->set_text(String::num(r.position.x)); + value_editor[1]->set_text(String::num(r.position.y)); value_editor[2]->set_text(String::num(r.size.x)); value_editor[3]->set_text(String::num(r.size.y)); } break; @@ -733,9 +733,9 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: config_value_editors(6, 3, 16, field_names); Rect3 aabb = v; - value_editor[0]->set_text(String::num(aabb.pos.x)); - value_editor[1]->set_text(String::num(aabb.pos.y)); - value_editor[2]->set_text(String::num(aabb.pos.z)); + value_editor[0]->set_text(String::num(aabb.position.x)); + value_editor[1]->set_text(String::num(aabb.position.y)); + value_editor[2]->set_text(String::num(aabb.position.z)); value_editor[3]->set_text(String::num(aabb.size.x)); value_editor[4]->set_text(String::num(aabb.size.y)); value_editor[5]->set_text(String::num(aabb.size.z)); @@ -1539,13 +1539,13 @@ void CustomPropertyEditor::_modified(String p_string) { Rect2 r2; if (evaluator) { - r2.pos.x = evaluator->eval(value_editor[0]->get_text()); - r2.pos.y = evaluator->eval(value_editor[1]->get_text()); + r2.position.x = evaluator->eval(value_editor[0]->get_text()); + r2.position.y = evaluator->eval(value_editor[1]->get_text()); r2.size.x = evaluator->eval(value_editor[2]->get_text()); r2.size.y = evaluator->eval(value_editor[3]->get_text()); } else { - r2.pos.x = value_editor[0]->get_text().to_double(); - r2.pos.y = value_editor[1]->get_text().to_double(); + r2.position.x = value_editor[0]->get_text().to_double(); + r2.position.y = value_editor[1]->get_text().to_double(); r2.size.x = value_editor[2]->get_text().to_double(); r2.size.y = value_editor[3]->get_text().to_double(); } @@ -2310,6 +2310,7 @@ void PropertyEditor::set_item_text(TreeItem *p_item, int p_type, const String &p if (obj->get(p_name).get_type() == Variant::NIL || obj->get(p_name).operator RefPtr().is_null()) { p_item->set_text(1, "<null>"); p_item->set_icon(1, Ref<Texture>()); + p_item->set_custom_as_button(1, false); Dictionary d = p_item->get_metadata(0); int hint = d.has("hint") ? d["hint"].operator int() : -1; @@ -2319,6 +2320,7 @@ void PropertyEditor::set_item_text(TreeItem *p_item, int p_type, const String &p } } else { + p_item->set_custom_as_button(1, true); RES res = obj->get(p_name).operator RefPtr(); if (res->is_class("Texture")) { int tw = EditorSettings::get_singleton()->get("docks/property_editor/texture_preview_width"); @@ -3540,17 +3542,21 @@ void PropertyEditor::update_tree() { item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM); item->set_editable(1, !read_only); - item->add_button(1, get_icon("EditResource", "EditorIcons")); + //item->add_button(1, get_icon("EditResource", "EditorIcons")); String type; if (p.hint == PROPERTY_HINT_RESOURCE_TYPE) type = p.hint_string; - if (obj->get(p.name).get_type() == Variant::NIL || obj->get(p.name).operator RefPtr().is_null()) { + RES res = obj->get(p.name).operator RefPtr(); + + if (obj->get(p.name).get_type() == Variant::NIL || res.is_null()) { item->set_text(1, "<null>"); item->set_icon(1, Ref<Texture>()); + item->set_custom_as_button(1, false); - } else { - RES res = obj->get(p.name).operator RefPtr(); + } else if (res.is_valid()) { + + item->set_custom_as_button(1, true); if (res->is_class("Texture")) { int tw = EditorSettings::get_singleton()->get("docks/property_editor/texture_preview_width"); @@ -3668,7 +3674,7 @@ void PropertyEditor::_draw_transparency(Object *t, const Rect2 &p_rect) { // make a little space between consecutive color fields Rect2 area = p_rect; - area.pos.y += 1; + area.position.y += 1; area.size.height -= 2; area.size.width -= arrow->get_size().width + 5; tree->draw_texture_rect(get_icon("Transparent", "EditorIcons"), area, true); @@ -3854,6 +3860,16 @@ void PropertyEditor::_item_edited() { _edit_set(name, NodePath(item->get_text(1)), refresh_all); } break; + case Variant::OBJECT: { + if (!item->is_custom_set_as_button(1)) + break; + + RES res = obj->get(name); + if (res.is_valid()) { + emit_signal("resource_selected", res.get_ref_ptr(), name); + } + + } break; case Variant::DICTIONARY: { @@ -3926,7 +3942,7 @@ void PropertyEditor::_custom_editor_request(bool p_arrow) { int hint = d.has("hint") ? d["hint"].operator int() : -1; String hint_text = d.has("hint_text") ? d["hint_text"] : ""; Rect2 where = tree->get_custom_popup_rect(); - custom_editor->set_position(where.pos); + custom_editor->set_position(where.position); if (custom_editor->edit(obj, name, type, v, hint, hint_text)) { custom_editor->popup(); @@ -4033,9 +4049,9 @@ void PropertyEditor::_edit_button(Object *p_item, int p_column, int p_button) { Variant v = obj->get(n); custom_editor->edit(obj, n, (Variant::Type)t, v, h, ht); Rect2 where = tree->get_item_rect(ti, 1); - where.pos -= tree->get_scroll(); - where.pos += tree->get_global_position(); - custom_editor->set_position(where.pos); + where.position -= tree->get_scroll(); + where.position += tree->get_global_position(); + custom_editor->set_position(where.position); custom_editor->popup(); } else if (t == Variant::STRING) { @@ -4046,9 +4062,9 @@ void PropertyEditor::_edit_button(Object *p_item, int p_column, int p_button) { if (h == PROPERTY_HINT_FILE || h == PROPERTY_HINT_DIR || h == PROPERTY_HINT_GLOBAL_DIR || h == PROPERTY_HINT_GLOBAL_FILE) { Rect2 where = tree->get_item_rect(ti, 1); - where.pos -= tree->get_scroll(); - where.pos += tree->get_global_position(); - custom_editor->set_position(where.pos); + where.position -= tree->get_scroll(); + where.position += tree->get_global_position(); + custom_editor->set_position(where.position); custom_editor->popup(); } else { custom_editor->popup_centered_ratio(); @@ -4124,7 +4140,7 @@ void PropertyEditor::_draw_flags(Object *t, const Rect2 &p_rect) { if (i == 1) ofs.y += bsize + 1; - ofs += p_rect.pos; + ofs += p_rect.position; for (int j = 0; j < 10; j++) { Point2 o = ofs + Point2(j * (bsize + 1), 0); diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index 0f05cc79ff..da2d22b900 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -658,6 +658,21 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { } } } break; + + default: { + + if (p_tool >= EDIT_SUBRESOURCE_BASE) { + + int idx = p_tool - EDIT_SUBRESOURCE_BASE; + + ERR_FAIL_INDEX(idx, subresources.size()); + + Object *obj = ObjectDB::get_instance(subresources[idx]); + ERR_FAIL_COND(!obj); + + editor->push_item(obj); + } + } } } @@ -1662,6 +1677,47 @@ void SceneTreeDock::_nodes_dragged(Array p_nodes, NodePath p_to, int p_type) { _do_reparent(to_node, to_pos, nodes, true); } +void SceneTreeDock::_add_children_to_popup(Object *p_obj, int p_depth) { + + if (p_depth > 8) + return; + + List<PropertyInfo> pinfo; + p_obj->get_property_list(&pinfo); + for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) { + + if (!(E->get().usage & PROPERTY_USAGE_EDITOR)) + continue; + if (E->get().hint != PROPERTY_HINT_RESOURCE_TYPE) + continue; + + Variant value = p_obj->get(E->get().name); + if (value.get_type() != Variant::OBJECT) + continue; + Object *obj = value; + if (!obj) + continue; + + Ref<Texture> icon; + + if (has_icon(obj->get_class(), "EditorIcons")) + icon = get_icon(obj->get_class(), "EditorIcons"); + else + icon = get_icon("Object", "EditorIcons"); + + if (menu->get_item_count() == 0) { + menu->add_item(TTR("Sub-Resources:")); + menu->set_item_disabled(0, true); + } + int index = menu->get_item_count(); + menu->add_icon_item(icon, E->get().name.capitalize(), EDIT_SUBRESOURCE_BASE + subresources.size()); + menu->set_item_h_offset(index, p_depth * 10 * EDSCALE); + subresources.push_back(obj->get_instance_ID()); + + _add_children_to_popup(obj, p_depth + 1); + } +} + void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) { if (!EditorNode::get_singleton()->get_edited_scene()) { @@ -1683,6 +1739,12 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) { menu->clear(); if (selection.size() == 1) { + + subresources.clear(); + _add_children_to_popup(selection.front()->get(), 0); + if (menu->get_item_count() > 0) + menu->add_separator(); + menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW); menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE); menu->add_separator(); diff --git a/editor/scene_tree_dock.h b/editor/scene_tree_dock.h index f190025dd6..79e01e7571 100644 --- a/editor/scene_tree_dock.h +++ b/editor/scene_tree_dock.h @@ -73,6 +73,12 @@ class SceneTreeDock : public VBoxContainer { TOOL_BUTTON_MAX }; + enum { + EDIT_SUBRESOURCE_BASE = 100 + }; + + Vector<ObjectID> subresources; + bool restore_script_editor_on_drag; int current_option; @@ -114,6 +120,8 @@ class SceneTreeDock : public VBoxContainer { Node *edited_scene; EditorNode *editor; + void _add_children_to_popup(Object *p_obj, int p_depth); + Node *_duplicate(Node *p_node, Map<Node *, Node *> &duplimap); void _node_reparent(NodePath p_path, bool p_keep_global_xform); void _do_reparent(Node *p_new_parent, int p_position_in_parent, Vector<Node *> p_nodes, bool p_keep_global_xform); diff --git a/editor/scene_tree_editor.cpp b/editor/scene_tree_editor.cpp index 2945abbd8c..d4e5714c0d 100644 --- a/editor/scene_tree_editor.cpp +++ b/editor/scene_tree_editor.cpp @@ -161,17 +161,17 @@ void SceneTreeEditor::_cell_button_pressed(Object *p_item, int p_column, int p_i if (p_id == BUTTON_SUBSCENE) { //open scene request Rect2 item_rect = tree->get_item_rect(item, 0); - item_rect.pos.y -= tree->get_scroll().y; - item_rect.pos += tree->get_global_position(); + item_rect.position.y -= tree->get_scroll().y; + item_rect.position += tree->get_global_position(); if (n == get_scene_node()) { - inheritance_menu->set_position(item_rect.pos + Vector2(0, item_rect.size.y)); + inheritance_menu->set_position(item_rect.position + Vector2(0, item_rect.size.y)); inheritance_menu->set_size(Vector2(item_rect.size.x, 0)); inheritance_menu->popup(); instance_node = n->get_instance_ID(); } else { - instance_menu->set_position(item_rect.pos + Vector2(0, item_rect.size.y)); + instance_menu->set_position(item_rect.position + Vector2(0, item_rect.size.y)); instance_menu->set_size(Vector2(item_rect.size.x, 0)); if (EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(n)) instance_menu->set_item_checked(0, true); diff --git a/editor/script_editor_debugger.cpp b/editor/script_editor_debugger.cpp index a2bb3a0879..7f1e6023bf 100644 --- a/editor/script_editor_debugger.cpp +++ b/editor/script_editor_debugger.cpp @@ -790,19 +790,19 @@ void ScriptEditorDebugger::_performance_draw() { Point2i p(i % cols, i / cols); Rect2i r(p * s, s); - r.pos += Point2(margin, margin); + r.position += Point2(margin, margin); r.size -= Point2(margin, margin) * 2.0; perf_draw->draw_style_box(graph_sb, r); - r.pos += graph_sb->get_offset(); + r.position += graph_sb->get_offset(); r.size -= graph_sb->get_minimum_size(); int pi = which[i]; Color c = Color(0.7, 0.9, 0.5); c.set_hsv(Math::fmod(c.get_h() + pi * 0.7654, 1), c.get_s(), c.get_v()); c.a = 0.8; - perf_draw->draw_string(graph_font, r.pos + Point2(0, graph_font->get_ascent()), perf_items[pi]->get_text(0), c, r.size.x); + perf_draw->draw_string(graph_font, r.position + Point2(0, graph_font->get_ascent()), perf_items[pi]->get_text(0), c, r.size.x); c.a = 0.6; - perf_draw->draw_string(graph_font, r.pos + Point2(graph_font->get_char_size('X').width, graph_font->get_ascent() + graph_font->get_height()), perf_items[pi]->get_text(1), c, r.size.y); + perf_draw->draw_string(graph_font, r.position + Point2(graph_font->get_char_size('X').width, graph_font->get_ascent() + graph_font->get_height()), perf_items[pi]->get_text(1), c, r.size.y); float spacing = point_sep / float(cols); float from = r.size.width; @@ -819,7 +819,7 @@ void ScriptEditorDebugger::_performance_draw() { c.a = 0.7; if (E != perf_history.front()) - perf_draw->draw_line(r.pos + Point2(from, h), r.pos + Point2(from + spacing, prev), c, 2.0); + perf_draw->draw_line(r.position + Point2(from, h), r.position + Point2(from + spacing, prev), c, 2.0); prev = h; E = E->next(); from -= spacing; diff --git a/editor/spatial_editor_gizmos.cpp b/editor/spatial_editor_gizmos.cpp index 4781bb6a3b..1b7afd3c43 100644 --- a/editor/spatial_editor_gizmos.cpp +++ b/editor/spatial_editor_gizmos.cpp @@ -79,7 +79,7 @@ void EditorSpatialGizmo::Instance::create_instance(Spatial *p_base) { VS::get_singleton()->instance_set_layer_mask(instance, 1 << SpatialEditorViewport::GIZMO_EDIT_LAYER); //gizmos are 26 } -void EditorSpatialGizmo::add_mesh(const Ref<Mesh> &p_mesh, bool p_billboard, const RID &p_skeleton) { +void EditorSpatialGizmo::add_mesh(const Ref<ArrayMesh> &p_mesh, bool p_billboard, const RID &p_skeleton) { ERR_FAIL_COND(!spatial_node); Instance ins; @@ -100,7 +100,7 @@ void EditorSpatialGizmo::add_lines(const Vector<Vector3> &p_lines, const Ref<Mat ERR_FAIL_COND(!spatial_node); Instance ins; - Ref<Mesh> mesh = memnew(Mesh); + Ref<ArrayMesh> mesh = memnew(ArrayMesh); Array a; a.resize(Mesh::ARRAY_MAX); @@ -162,7 +162,7 @@ void EditorSpatialGizmo::add_unscaled_billboard(const Ref<Material> &p_material, uv.push_back(Vector2(0, 1)); uv.push_back(Vector2(1, 1)); - Ref<Mesh> mesh = memnew(Mesh); + Ref<ArrayMesh> mesh = memnew(ArrayMesh); Array a; a.resize(Mesh::ARRAY_MAX); a[Mesh::ARRAY_VERTEX] = vs; @@ -219,7 +219,7 @@ void EditorSpatialGizmo::add_handles(const Vector<Vector3> &p_handles, bool p_bi ERR_FAIL_COND(!spatial_node); Instance ins; - Ref<Mesh> mesh = memnew(Mesh); + Ref<ArrayMesh> mesh = memnew(ArrayMesh); #if 1 Array a; @@ -1029,7 +1029,7 @@ CameraSpatialGizmo::CameraSpatialGizmo(Camera *p_camera) { void MeshInstanceSpatialGizmo::redraw() { - Ref<Mesh> m = mesh->get_mesh(); + Ref<ArrayMesh> m = mesh->get_mesh(); if (!m.is_valid()) return; //none @@ -1248,7 +1248,7 @@ void SkeletonSpatialGizmo::redraw() { */ } - Ref<Mesh> m = surface_tool->commit(); + Ref<ArrayMesh> m = surface_tool->commit(); add_mesh(m, false, skel->get_skeleton()); } @@ -1722,8 +1722,8 @@ void CollisionShapeSpatialGizmo::redraw() { Ref<BoxShape> bs = s; Vector<Vector3> lines; Rect3 aabb; - aabb.pos = -bs->get_extents(); - aabb.size = aabb.pos * -2; + aabb.position = -bs->get_extents(); + aabb.size = aabb.position * -2; for (int i = 0; i < 12; i++) { Vector3 a, b; @@ -1953,7 +1953,7 @@ void VisibilityNotifierGizmo::set_handle(int p_idx, Camera *p_camera, const Poin Vector3 ray_dir = p_camera->project_ray_normal(p_point); Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) }; - Vector3 ofs = aabb.pos + aabb.size * 0.5; + Vector3 ofs = aabb.position + aabb.size * 0.5; Vector3 axis; axis[p_idx] = 1.0; @@ -1964,7 +1964,7 @@ void VisibilityNotifierGizmo::set_handle(int p_idx, Camera *p_camera, const Poin if (d < 0.001) d = 0.001; - aabb.pos[p_idx] = (aabb.pos[p_idx] + aabb.size[p_idx] * 0.5) - d; + aabb.position[p_idx] = (aabb.position[p_idx] + aabb.size[p_idx] * 0.5) - d; aabb.size[p_idx] = d * 2; notifier->set_aabb(aabb); } @@ -2002,7 +2002,7 @@ void VisibilityNotifierGizmo::redraw() { for (int i = 0; i < 3; i++) { Vector3 ax; - ax[i] = aabb.pos[i] + aabb.size[i]; + ax[i] = aabb.position[i] + aabb.size[i]; handles.push_back(ax); } @@ -2053,7 +2053,7 @@ void ParticlesGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_poi Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) }; - Vector3 ofs = aabb.pos + aabb.size * 0.5; + Vector3 ofs = aabb.position + aabb.size * 0.5; Vector3 axis; axis[p_idx] = 1.0; @@ -2065,7 +2065,7 @@ void ParticlesGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_poi float d = ra[p_idx]; - aabb.pos[p_idx] = d - 1.0 - aabb.size[p_idx] * 0.5; + aabb.position[p_idx] = d - 1.0 - aabb.size[p_idx] * 0.5; particles->set_visibility_aabb(aabb); } else { @@ -2076,7 +2076,7 @@ void ParticlesGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_poi if (d < 0.001) d = 0.001; //resize - aabb.pos[p_idx] = (aabb.pos[p_idx] + aabb.size[p_idx] * 0.5) - d; + aabb.position[p_idx] = (aabb.position[p_idx] + aabb.size[p_idx] * 0.5) - d; aabb.size[p_idx] = d * 2; particles->set_visibility_aabb(aabb); } @@ -2115,13 +2115,13 @@ void ParticlesGizmo::redraw() { for (int i = 0; i < 3; i++) { Vector3 ax; - ax[i] = aabb.pos[i] + aabb.size[i]; - ax[(i + 1) % 3] = aabb.pos[(i + 1) % 3] + aabb.size[(i + 1) % 3] * 0.5; - ax[(i + 2) % 3] = aabb.pos[(i + 2) % 3] + aabb.size[(i + 2) % 3] * 0.5; + ax[i] = aabb.position[i] + aabb.size[i]; + ax[(i + 1) % 3] = aabb.position[(i + 1) % 3] + aabb.size[(i + 1) % 3] * 0.5; + ax[(i + 2) % 3] = aabb.position[(i + 2) % 3] + aabb.size[(i + 2) % 3] * 0.5; handles.push_back(ax); } - Vector3 center = aabb.pos + aabb.size * 0.5; + Vector3 center = aabb.position + aabb.size * 0.5; for (int i = 0; i < 3; i++) { Vector3 ax; @@ -2218,7 +2218,7 @@ void ReflectionProbeGizmo::commit_handle(int p_idx, const Variant &p_restore, bo Rect3 restore = p_restore; if (p_cancel) { - probe->set_extents(restore.pos); + probe->set_extents(restore.position); probe->set_origin_offset(restore.size); return; } @@ -2227,7 +2227,7 @@ void ReflectionProbeGizmo::commit_handle(int p_idx, const Variant &p_restore, bo ur->create_action(TTR("Change Probe Extents")); ur->add_do_method(probe, "set_extents", probe->get_extents()); ur->add_do_method(probe, "set_origin_offset", probe->get_origin_offset()); - ur->add_undo_method(probe, "set_extents", restore.pos); + ur->add_undo_method(probe, "set_extents", restore.position); ur->add_undo_method(probe, "set_origin_offset", restore.size); ur->commit_action(); } @@ -2241,7 +2241,7 @@ void ReflectionProbeGizmo::redraw() { Vector3 extents = probe->get_extents(); Rect3 aabb; - aabb.pos = -extents; + aabb.position = -extents; aabb.size = extents * 2; for (int i = 0; i < 12; i++) { @@ -2262,7 +2262,7 @@ void ReflectionProbeGizmo::redraw() { for (int i = 0; i < 3; i++) { Vector3 ax; - ax[i] = aabb.pos[i] + aabb.size[i]; + ax[i] = aabb.position[i] + aabb.size[i]; handles.push_back(ax); } @@ -2392,7 +2392,7 @@ void GIProbeGizmo::redraw() { for (int k = 0; k < 4; k++) { - Vector3 from = aabb.pos, to = aabb.pos; + Vector3 from = aabb.position, to = aabb.position; from[j] += cell_size * i; to[j] += cell_size * i; @@ -2421,7 +2421,7 @@ void GIProbeGizmo::redraw() { for (int i = 0; i < 3; i++) { Vector3 ax; - ax[i] = aabb.pos[i] + aabb.size[i]; + ax[i] = aabb.position[i] + aabb.size[i]; handles.push_back(ax); } @@ -2511,7 +2511,7 @@ void NavigationMeshSpatialGizmo::redraw() { if (lines.size()) add_lines(lines, navmesh->is_enabled() ? SpatialEditorGizmos::singleton->navmesh_edge_material : SpatialEditorGizmos::singleton->navmesh_edge_material_disabled); add_collision_triangles(tmesh); - Ref<Mesh> m = memnew(Mesh); + Ref<ArrayMesh> m = memnew(ArrayMesh); Array a; a.resize(Mesh::ARRAY_MAX); a[0] = tmeshfaces; @@ -3213,7 +3213,7 @@ SpatialEditorGizmos::SpatialEditorGizmos() { //position 3D Shared mesh - pos3d_mesh = Ref<Mesh>(memnew(Mesh)); + pos3d_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); { PoolVector<Vector3> cursor_points; @@ -3246,7 +3246,7 @@ SpatialEditorGizmos::SpatialEditorGizmos() { pos3d_mesh->surface_set_material(0, mat); } - listener_line_mesh = Ref<Mesh>(memnew(Mesh)); + listener_line_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); { PoolVector<Vector3> cursor_points; diff --git a/editor/spatial_editor_gizmos.h b/editor/spatial_editor_gizmos.h index 095586ab91..6a77e91425 100644 --- a/editor/spatial_editor_gizmos.h +++ b/editor/spatial_editor_gizmos.h @@ -59,7 +59,7 @@ class EditorSpatialGizmo : public SpatialEditorGizmo { struct Instance { RID instance; - Ref<Mesh> mesh; + Ref<ArrayMesh> mesh; RID skeleton; bool billboard; bool unscaled; @@ -97,7 +97,7 @@ class EditorSpatialGizmo : public SpatialEditorGizmo { protected: void add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard = false); - void add_mesh(const Ref<Mesh> &p_mesh, bool p_billboard = false, const RID &p_skeleton = RID()); + void add_mesh(const Ref<ArrayMesh> &p_mesh, bool p_billboard = false, const RID &p_skeleton = RID()); void add_collision_segments(const Vector<Vector3> &p_lines); void add_collision_triangles(const Ref<TriangleMesh> &p_tmesh); void add_unscaled_billboard(const Ref<Material> &p_material, float p_scale = 1); @@ -454,8 +454,8 @@ public: Ref<SpatialMaterial> shape_material; Ref<Texture> handle_t; - Ref<Mesh> pos3d_mesh; - Ref<Mesh> listener_line_mesh; + Ref<ArrayMesh> pos3d_mesh; + Ref<ArrayMesh> listener_line_mesh; static SpatialEditorGizmos *singleton; Ref<TriangleMesh> test_cube_tm; diff --git a/main/input_default.cpp b/main/input_default.cpp index 0de30e5e9e..e488438059 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -325,11 +325,11 @@ void InputDefault::parse_input_event(const Ref<InputEvent> &p_event) { Ref<InputEventScreenTouch> touch_event; touch_event.instance(); touch_event->set_pressed(mb->is_pressed()); - touch_event->set_pos(mb->get_pos()); + touch_event->set_position(mb->get_position()); main_loop->input_event(touch_event); } - Point2 pos = mb->get_global_pos(); + Point2 pos = mb->get_global_position(); if (mouse_pos != pos) { set_mouse_position(pos); } @@ -343,7 +343,7 @@ void InputDefault::parse_input_event(const Ref<InputEvent> &p_event) { Ref<InputEventScreenDrag> drag_event; drag_event.instance(); - drag_event->set_pos(mm->get_pos()); + drag_event->set_position(mm->get_position()); drag_event->set_relative(mm->get_relative()); drag_event->set_speed(mm->get_speed()); @@ -493,10 +493,10 @@ Point2i InputDefault::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_moti Math::fmod(p_motion->get_relative().x + rel_sgn.x * warp_margin.x, p_rect.size.x) - rel_sgn.x * warp_margin.x, Math::fmod(p_motion->get_relative().y + rel_sgn.y * warp_margin.y, p_rect.size.y) - rel_sgn.y * warp_margin.y); - const Point2i pos_local = p_motion->get_global_pos() - p_rect.pos; + const Point2i pos_local = p_motion->get_global_position() - p_rect.position; const Point2i pos_warped(Math::fposmod(pos_local.x, p_rect.size.x), Math::fposmod(pos_local.y, p_rect.size.y)); if (pos_warped != pos_local) { - OS::get_singleton()->warp_mouse_pos(pos_warped + p_rect.pos); + OS::get_singleton()->warp_mouse_pos(pos_warped + p_rect.position); } return rel_warped; diff --git a/main/tests/test_physics_2d.cpp b/main/tests/test_physics_2d.cpp index 5f57275503..2c9b51aadb 100644 --- a/main/tests/test_physics_2d.cpp +++ b/main/tests/test_physics_2d.cpp @@ -216,7 +216,7 @@ protected: if (mb->is_pressed()) { - Point2 p(mb->get_pos().x, mb->get_pos().y); + Point2 p(mb->get_position().x, mb->get_position().y); if (mb->get_button_index() == 1) { ray_to = p; @@ -232,7 +232,7 @@ protected: if (mm.is_valid()) { - Point2 p = mm->get_pos(); + Point2 p = mm->get_position(); if (mm->get_button_mask() & BUTTON_MASK_LEFT) { ray_to = p; diff --git a/modules/enet/networked_multiplayer_enet.cpp b/modules/enet/networked_multiplayer_enet.cpp index c05c86d9ae..738bd27ed3 100644 --- a/modules/enet/networked_multiplayer_enet.cpp +++ b/modules/enet/networked_multiplayer_enet.cpp @@ -575,6 +575,9 @@ size_t NetworkedMultiplayerENet::enet_compress(void *context, const ENetBuffer * case COMPRESS_ZLIB: { mode = Compression::MODE_DEFLATE; } break; + case COMPRESS_ZSTD: { + mode = Compression::MODE_ZSTD; + } break; default: { ERR_FAIL_V(0); } } @@ -608,6 +611,10 @@ size_t NetworkedMultiplayerENet::enet_decompress(void *context, const enet_uint8 ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_DEFLATE); } break; + case COMPRESS_ZSTD: { + + ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_ZSTD); + } break; default: {} } if (ret < 0) { @@ -629,7 +636,8 @@ void NetworkedMultiplayerENet::_setup_compressor() { enet_host_compress_with_range_coder(host); } break; case COMPRESS_FASTLZ: - case COMPRESS_ZLIB: { + case COMPRESS_ZLIB: + case COMPRESS_ZSTD: { enet_host_compress(host, &enet_compressor); } break; @@ -654,6 +662,7 @@ void NetworkedMultiplayerENet::_bind_methods() { BIND_CONSTANT(COMPRESS_RANGE_CODER); BIND_CONSTANT(COMPRESS_FASTLZ); BIND_CONSTANT(COMPRESS_ZLIB); + BIND_CONSTANT(COMPRESS_ZSTD); } NetworkedMultiplayerENet::NetworkedMultiplayerENet() { diff --git a/modules/enet/networked_multiplayer_enet.h b/modules/enet/networked_multiplayer_enet.h index c20c1af68e..8b971adf55 100644 --- a/modules/enet/networked_multiplayer_enet.h +++ b/modules/enet/networked_multiplayer_enet.h @@ -43,7 +43,8 @@ public: COMPRESS_NONE, COMPRESS_RANGE_CODER, COMPRESS_FASTLZ, - COMPRESS_ZLIB + COMPRESS_ZLIB, + COMPRESS_ZSTD }; private: diff --git a/modules/gdnative/godot/godot_array.cpp b/modules/gdnative/godot/godot_array.cpp index 8cf6d1b8ef..5497dde520 100644 --- a/modules/gdnative/godot/godot_array.cpp +++ b/modules/gdnative/godot/godot_array.cpp @@ -44,256 +44,264 @@ extern "C" { void _array_api_anchor() { } -void GDAPI godot_array_new(godot_array *p_arr) { - Array *a = (Array *)p_arr; - memnew_placement(a, Array); +void GDAPI godot_array_new(godot_array *r_dest) { + Array *dest = (Array *)r_dest; + memnew_placement(dest, Array); } -void GDAPI godot_array_new_copy(godot_array *p_dest, const godot_array *p_src) { - Array *dest = (Array *)p_dest; +void GDAPI godot_array_new_copy(godot_array *r_dest, const godot_array *p_src) { + Array *dest = (Array *)r_dest; const Array *src = (const Array *)p_src; memnew_placement(dest, Array(*src)); } -void GDAPI godot_array_new_pool_color_array(godot_array *p_arr, const godot_pool_color_array *p_pca) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_new_pool_color_array(godot_array *r_dest, const godot_pool_color_array *p_pca) { + Array *dest = (Array *)r_dest; PoolVector<Color> *pca = (PoolVector<Color> *)p_pca; - memnew_placement(a, Array); - a->resize(pca->size()); + memnew_placement(dest, Array); + dest->resize(pca->size()); - for (size_t i = 0; i < a->size(); i++) { + for (size_t i = 0; i < dest->size(); i++) { Variant v = pca->operator[](i); - a->operator[](i) = v; + dest->operator[](i) = v; } } -void GDAPI godot_array_new_pool_vector3_array(godot_array *p_arr, const godot_pool_vector3_array *p_pv3a) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_new_pool_vector3_array(godot_array *r_dest, const godot_pool_vector3_array *p_pv3a) { + Array *dest = (Array *)r_dest; PoolVector<Vector3> *pca = (PoolVector<Vector3> *)p_pv3a; - memnew_placement(a, Array); - a->resize(pca->size()); + memnew_placement(dest, Array); + dest->resize(pca->size()); - for (size_t i = 0; i < a->size(); i++) { + for (size_t i = 0; i < dest->size(); i++) { Variant v = pca->operator[](i); - a->operator[](i) = v; + dest->operator[](i) = v; } } -void GDAPI godot_array_new_pool_vector2_array(godot_array *p_arr, const godot_pool_vector2_array *p_pv2a) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_new_pool_vector2_array(godot_array *r_dest, const godot_pool_vector2_array *p_pv2a) { + Array *dest = (Array *)r_dest; PoolVector<Vector2> *pca = (PoolVector<Vector2> *)p_pv2a; - memnew_placement(a, Array); - a->resize(pca->size()); + memnew_placement(dest, Array); + dest->resize(pca->size()); - for (size_t i = 0; i < a->size(); i++) { + for (size_t i = 0; i < dest->size(); i++) { Variant v = pca->operator[](i); - a->operator[](i) = v; + dest->operator[](i) = v; } } -void GDAPI godot_array_new_pool_string_array(godot_array *p_arr, const godot_pool_string_array *p_psa) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_new_pool_string_array(godot_array *r_dest, const godot_pool_string_array *p_psa) { + Array *dest = (Array *)r_dest; PoolVector<String> *pca = (PoolVector<String> *)p_psa; - memnew_placement(a, Array); - a->resize(pca->size()); + memnew_placement(dest, Array); + dest->resize(pca->size()); - for (size_t i = 0; i < a->size(); i++) { + for (size_t i = 0; i < dest->size(); i++) { Variant v = pca->operator[](i); - a->operator[](i) = v; + dest->operator[](i) = v; } } -void GDAPI godot_array_new_pool_real_array(godot_array *p_arr, const godot_pool_real_array *p_pra) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_new_pool_real_array(godot_array *r_dest, const godot_pool_real_array *p_pra) { + Array *dest = (Array *)r_dest; PoolVector<godot_real> *pca = (PoolVector<godot_real> *)p_pra; - memnew_placement(a, Array); - a->resize(pca->size()); + memnew_placement(dest, Array); + dest->resize(pca->size()); - for (size_t i = 0; i < a->size(); i++) { + for (size_t i = 0; i < dest->size(); i++) { Variant v = pca->operator[](i); - a->operator[](i) = v; + dest->operator[](i) = v; } } -void GDAPI godot_array_new_pool_int_array(godot_array *p_arr, const godot_pool_int_array *p_pia) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_new_pool_int_array(godot_array *r_dest, const godot_pool_int_array *p_pia) { + Array *dest = (Array *)r_dest; PoolVector<godot_int> *pca = (PoolVector<godot_int> *)p_pia; - memnew_placement(a, Array); - a->resize(pca->size()); + memnew_placement(dest, Array); + dest->resize(pca->size()); - for (size_t i = 0; i < a->size(); i++) { + for (size_t i = 0; i < dest->size(); i++) { Variant v = pca->operator[](i); - a->operator[](i) = v; + dest->operator[](i) = v; } } -void GDAPI godot_array_new_pool_byte_array(godot_array *p_arr, const godot_pool_byte_array *p_pba) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_new_pool_byte_array(godot_array *r_dest, const godot_pool_byte_array *p_pba) { + Array *dest = (Array *)r_dest; PoolVector<uint8_t> *pca = (PoolVector<uint8_t> *)p_pba; - memnew_placement(a, Array); - a->resize(pca->size()); + memnew_placement(dest, Array); + dest->resize(pca->size()); - for (size_t i = 0; i < a->size(); i++) { + for (size_t i = 0; i < dest->size(); i++) { Variant v = pca->operator[](i); - a->operator[](i) = v; + dest->operator[](i) = v; } } -void GDAPI godot_array_set(godot_array *p_arr, const godot_int p_idx, const godot_variant *p_value) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_set(godot_array *p_self, const godot_int p_idx, const godot_variant *p_value) { + Array *self = (Array *)p_self; Variant *val = (Variant *)p_value; - a->operator[](p_idx) = *val; + self->operator[](p_idx) = *val; } -godot_variant GDAPI *godot_array_get(const godot_array *p_arr, const godot_int p_idx) { - Array *a = (Array *)p_arr; - return (godot_variant *)&a->operator[](p_idx); +godot_variant GDAPI godot_array_get(const godot_array *p_self, const godot_int p_idx) { + godot_variant raw_dest; + Variant *dest = (Variant *)&raw_dest; + const Array *self = (const Array *)p_self; + memnew_placement(dest, Variant(self->operator[](p_idx))); + return raw_dest; } -void GDAPI godot_array_append(godot_array *p_arr, const godot_variant *p_value) { - Array *a = (Array *)p_arr; +godot_variant GDAPI *godot_array_operator_index(godot_array *p_self, const godot_int p_idx) { + Array *self = (Array *)p_self; + return (godot_variant *)&self->operator[](p_idx); +} + +void GDAPI godot_array_append(godot_array *p_self, const godot_variant *p_value) { + Array *self = (Array *)p_self; Variant *val = (Variant *)p_value; - a->append(*val); + self->append(*val); } -void GDAPI godot_array_clear(godot_array *p_arr) { - Array *a = (Array *)p_arr; - a->clear(); +void GDAPI godot_array_clear(godot_array *p_self) { + Array *self = (Array *)p_self; + self->clear(); } -godot_int GDAPI godot_array_count(const godot_array *p_arr, const godot_variant *p_value) { - const Array *a = (const Array *)p_arr; +godot_int GDAPI godot_array_count(const godot_array *p_self, const godot_variant *p_value) { + const Array *self = (const Array *)p_self; const Variant *val = (const Variant *)p_value; - return a->count(*val); + return self->count(*val); } -godot_bool GDAPI godot_array_empty(const godot_array *p_arr) { - const Array *a = (const Array *)p_arr; - return a->empty(); +godot_bool GDAPI godot_array_empty(const godot_array *p_self) { + const Array *self = (const Array *)p_self; + return self->empty(); } -void GDAPI godot_array_erase(godot_array *p_arr, const godot_variant *p_value) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_erase(godot_array *p_self, const godot_variant *p_value) { + Array *self = (Array *)p_self; const Variant *val = (const Variant *)p_value; - a->erase(*val); + self->erase(*val); } -godot_variant GDAPI godot_array_front(const godot_array *p_arr) { - const Array *a = (const Array *)p_arr; +godot_variant GDAPI godot_array_front(const godot_array *p_self) { + const Array *self = (const Array *)p_self; godot_variant v; Variant *val = (Variant *)&v; memnew_placement(val, Variant); - *val = a->front(); + *val = self->front(); return v; } -godot_variant GDAPI godot_array_back(const godot_array *p_arr) { - const Array *a = (const Array *)p_arr; +godot_variant GDAPI godot_array_back(const godot_array *p_self) { + const Array *self = (const Array *)p_self; godot_variant v; Variant *val = (Variant *)&v; memnew_placement(val, Variant); - *val = a->back(); + *val = self->back(); return v; } -godot_int GDAPI godot_array_find(const godot_array *p_arr, const godot_variant *p_what, const godot_int p_from) { - const Array *a = (const Array *)p_arr; +godot_int GDAPI godot_array_find(const godot_array *p_self, const godot_variant *p_what, const godot_int p_from) { + const Array *self = (const Array *)p_self; const Variant *val = (const Variant *)p_what; - return a->find(*val, p_from); + return self->find(*val, p_from); } -godot_int GDAPI godot_array_find_last(const godot_array *p_arr, const godot_variant *p_what) { - const Array *a = (const Array *)p_arr; +godot_int GDAPI godot_array_find_last(const godot_array *p_self, const godot_variant *p_what) { + const Array *self = (const Array *)p_self; const Variant *val = (const Variant *)p_what; - return a->find_last(*val); + return self->find_last(*val); } -godot_bool GDAPI godot_array_has(const godot_array *p_arr, const godot_variant *p_value) { - const Array *a = (const Array *)p_arr; +godot_bool GDAPI godot_array_has(const godot_array *p_self, const godot_variant *p_value) { + const Array *self = (const Array *)p_self; const Variant *val = (const Variant *)p_value; - return a->has(*val); + return self->has(*val); } -uint32_t GDAPI godot_array_hash(const godot_array *p_arr) { - const Array *a = (const Array *)p_arr; - return a->hash(); +godot_int GDAPI godot_array_hash(const godot_array *p_self) { + const Array *self = (const Array *)p_self; + return self->hash(); } -void GDAPI godot_array_insert(godot_array *p_arr, const godot_int p_pos, const godot_variant *p_value) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_insert(godot_array *p_self, const godot_int p_pos, const godot_variant *p_value) { + Array *self = (Array *)p_self; const Variant *val = (const Variant *)p_value; - a->insert(p_pos, *val); + self->insert(p_pos, *val); } -void GDAPI godot_array_invert(godot_array *p_arr) { - Array *a = (Array *)p_arr; - a->invert(); +void GDAPI godot_array_invert(godot_array *p_self) { + Array *self = (Array *)p_self; + self->invert(); } -godot_variant GDAPI godot_array_pop_back(godot_array *p_arr) { - Array *a = (Array *)p_arr; +godot_variant GDAPI godot_array_pop_back(godot_array *p_self) { + Array *self = (Array *)p_self; godot_variant v; Variant *val = (Variant *)&v; memnew_placement(val, Variant); - *val = a->pop_back(); + *val = self->pop_back(); return v; } -godot_variant GDAPI godot_array_pop_front(godot_array *p_arr) { - Array *a = (Array *)p_arr; +godot_variant GDAPI godot_array_pop_front(godot_array *p_self) { + Array *self = (Array *)p_self; godot_variant v; Variant *val = (Variant *)&v; memnew_placement(val, Variant); - *val = a->pop_front(); + *val = self->pop_front(); return v; } -void GDAPI godot_array_push_back(godot_array *p_arr, const godot_variant *p_value) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_push_back(godot_array *p_self, const godot_variant *p_value) { + Array *self = (Array *)p_self; const Variant *val = (const Variant *)p_value; - a->push_back(*val); + self->push_back(*val); } -void GDAPI godot_array_push_front(godot_array *p_arr, const godot_variant *p_value) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_push_front(godot_array *p_self, const godot_variant *p_value) { + Array *self = (Array *)p_self; const Variant *val = (const Variant *)p_value; - a->push_front(*val); + self->push_front(*val); } -void GDAPI godot_array_remove(godot_array *p_arr, const godot_int p_idx) { - Array *a = (Array *)p_arr; - a->remove(p_idx); +void GDAPI godot_array_remove(godot_array *p_self, const godot_int p_idx) { + Array *self = (Array *)p_self; + self->remove(p_idx); } -void GDAPI godot_array_resize(godot_array *p_arr, const godot_int p_size) { - Array *a = (Array *)p_arr; - a->resize(p_size); +void GDAPI godot_array_resize(godot_array *p_self, const godot_int p_size) { + Array *self = (Array *)p_self; + self->resize(p_size); } -godot_int GDAPI godot_array_rfind(const godot_array *p_arr, const godot_variant *p_what, const godot_int p_from) { - const Array *a = (const Array *)p_arr; +godot_int GDAPI godot_array_rfind(const godot_array *p_self, const godot_variant *p_what, const godot_int p_from) { + const Array *self = (const Array *)p_self; const Variant *val = (const Variant *)p_what; - return a->rfind(*val, p_from); + return self->rfind(*val, p_from); } -godot_int GDAPI godot_array_size(const godot_array *p_arr) { - const Array *a = (const Array *)p_arr; - return a->size(); +godot_int GDAPI godot_array_size(const godot_array *p_self) { + const Array *self = (const Array *)p_self; + return self->size(); } -void GDAPI godot_array_sort(godot_array *p_arr) { - Array *a = (Array *)p_arr; - a->sort(); +void GDAPI godot_array_sort(godot_array *p_self) { + Array *self = (Array *)p_self; + self->sort(); } -void GDAPI godot_array_sort_custom(godot_array *p_arr, godot_object *p_obj, const godot_string *p_func) { - Array *a = (Array *)p_arr; +void GDAPI godot_array_sort_custom(godot_array *p_self, godot_object *p_obj, const godot_string *p_func) { + Array *self = (Array *)p_self; const String *func = (const String *)p_func; - a->sort_custom((Object *)p_obj, *func); + self->sort_custom((Object *)p_obj, *func); } -void GDAPI godot_array_destroy(godot_array *p_arr) { - ((Array *)p_arr)->~Array(); +void GDAPI godot_array_destroy(godot_array *p_self) { + ((Array *)p_self)->~Array(); } #ifdef __cplusplus diff --git a/modules/gdnative/godot/godot_array.h b/modules/gdnative/godot/godot_array.h index 5db0031b8c..bf8bc61977 100644 --- a/modules/gdnative/godot/godot_array.h +++ b/modules/gdnative/godot/godot_array.h @@ -48,67 +48,69 @@ typedef struct godot_array { #include "../godot.h" -void GDAPI godot_array_new(godot_array *p_arr); -void GDAPI godot_array_new_copy(godot_array *p_dest, const godot_array *p_src); -void GDAPI godot_array_new_pool_color_array(godot_array *p_arr, const godot_pool_color_array *p_pca); -void GDAPI godot_array_new_pool_vector3_array(godot_array *p_arr, const godot_pool_vector3_array *p_pv3a); -void GDAPI godot_array_new_pool_vector2_array(godot_array *p_arr, const godot_pool_vector2_array *p_pv2a); -void GDAPI godot_array_new_pool_string_array(godot_array *p_arr, const godot_pool_string_array *p_psa); -void GDAPI godot_array_new_pool_real_array(godot_array *p_arr, const godot_pool_real_array *p_pra); -void GDAPI godot_array_new_pool_int_array(godot_array *p_arr, const godot_pool_int_array *p_pia); -void GDAPI godot_array_new_pool_byte_array(godot_array *p_arr, const godot_pool_byte_array *p_pba); +void GDAPI godot_array_new(godot_array *r_dest); +void GDAPI godot_array_new_copy(godot_array *r_dest, const godot_array *p_src); +void GDAPI godot_array_new_pool_color_array(godot_array *r_dest, const godot_pool_color_array *p_pca); +void GDAPI godot_array_new_pool_vector3_array(godot_array *r_dest, const godot_pool_vector3_array *p_pv3a); +void GDAPI godot_array_new_pool_vector2_array(godot_array *r_dest, const godot_pool_vector2_array *p_pv2a); +void GDAPI godot_array_new_pool_string_array(godot_array *r_dest, const godot_pool_string_array *p_psa); +void GDAPI godot_array_new_pool_real_array(godot_array *r_dest, const godot_pool_real_array *p_pra); +void GDAPI godot_array_new_pool_int_array(godot_array *r_dest, const godot_pool_int_array *p_pia); +void GDAPI godot_array_new_pool_byte_array(godot_array *r_dest, const godot_pool_byte_array *p_pba); -void GDAPI godot_array_set(godot_array *p_arr, const godot_int p_idx, const godot_variant *p_value); +void GDAPI godot_array_set(godot_array *p_self, const godot_int p_idx, const godot_variant *p_value); -godot_variant GDAPI *godot_array_get(const godot_array *p_arr, const godot_int p_idx); +godot_variant GDAPI godot_array_get(const godot_array *p_self, const godot_int p_idx); -void GDAPI godot_array_append(godot_array *p_arr, const godot_variant *p_value); +godot_variant GDAPI *godot_array_operator_index(godot_array *p_self, const godot_int p_idx); -void GDAPI godot_array_clear(godot_array *p_arr); +void GDAPI godot_array_append(godot_array *p_self, const godot_variant *p_value); -godot_int GDAPI godot_array_count(const godot_array *p_arr, const godot_variant *p_value); +void GDAPI godot_array_clear(godot_array *p_self); -godot_bool GDAPI godot_array_empty(const godot_array *p_arr); +godot_int GDAPI godot_array_count(const godot_array *p_self, const godot_variant *p_value); -void GDAPI godot_array_erase(godot_array *p_arr, const godot_variant *p_value); +godot_bool GDAPI godot_array_empty(const godot_array *p_self); -godot_variant GDAPI godot_array_front(const godot_array *p_arr); +void GDAPI godot_array_erase(godot_array *p_self, const godot_variant *p_value); -godot_variant GDAPI godot_array_back(const godot_array *p_arr); +godot_variant GDAPI godot_array_front(const godot_array *p_self); -godot_int GDAPI godot_array_find(const godot_array *p_arr, const godot_variant *p_what, const godot_int p_from); +godot_variant GDAPI godot_array_back(const godot_array *p_self); -godot_int GDAPI godot_array_find_last(const godot_array *p_arr, const godot_variant *p_what); +godot_int GDAPI godot_array_find(const godot_array *p_self, const godot_variant *p_what, const godot_int p_from); -godot_bool GDAPI godot_array_has(const godot_array *p_arr, const godot_variant *p_value); +godot_int GDAPI godot_array_find_last(const godot_array *p_self, const godot_variant *p_what); -uint32_t GDAPI godot_array_hash(const godot_array *p_arr); +godot_bool GDAPI godot_array_has(const godot_array *p_self, const godot_variant *p_value); -void GDAPI godot_array_insert(godot_array *p_arr, const godot_int p_pos, const godot_variant *p_value); +godot_int GDAPI godot_array_hash(const godot_array *p_self); -void GDAPI godot_array_invert(godot_array *p_arr); +void GDAPI godot_array_insert(godot_array *p_self, const godot_int p_pos, const godot_variant *p_value); -godot_variant GDAPI godot_array_pop_back(godot_array *p_arr); +void GDAPI godot_array_invert(godot_array *p_self); -godot_variant GDAPI godot_array_pop_front(godot_array *p_arr); +godot_variant GDAPI godot_array_pop_back(godot_array *p_self); -void GDAPI godot_array_push_back(godot_array *p_arr, const godot_variant *p_value); +godot_variant GDAPI godot_array_pop_front(godot_array *p_self); -void GDAPI godot_array_push_front(godot_array *p_arr, const godot_variant *p_value); +void GDAPI godot_array_push_back(godot_array *p_self, const godot_variant *p_value); -void GDAPI godot_array_remove(godot_array *p_arr, const godot_int p_idx); +void GDAPI godot_array_push_front(godot_array *p_self, const godot_variant *p_value); -void GDAPI godot_array_resize(godot_array *p_arr, const godot_int p_size); +void GDAPI godot_array_remove(godot_array *p_self, const godot_int p_idx); -godot_int GDAPI godot_array_rfind(const godot_array *p_arr, const godot_variant *p_what, const godot_int p_from); +void GDAPI godot_array_resize(godot_array *p_self, const godot_int p_size); -godot_int GDAPI godot_array_size(const godot_array *p_arr); +godot_int GDAPI godot_array_rfind(const godot_array *p_self, const godot_variant *p_what, const godot_int p_from); -void GDAPI godot_array_sort(godot_array *p_arr); +godot_int GDAPI godot_array_size(const godot_array *p_self); -void GDAPI godot_array_sort_custom(godot_array *p_arr, godot_object *p_obj, const godot_string *p_func); +void GDAPI godot_array_sort(godot_array *p_self); -void GDAPI godot_array_destroy(godot_array *p_arr); +void GDAPI godot_array_sort_custom(godot_array *p_self, godot_object *p_obj, const godot_string *p_func); + +void GDAPI godot_array_destroy(godot_array *p_self); #ifdef __cplusplus } diff --git a/modules/gdnative/godot/godot_color.cpp b/modules/gdnative/godot/godot_color.cpp index 0417a828ab..6dedf2ab10 100644 --- a/modules/gdnative/godot/godot_color.cpp +++ b/modules/gdnative/godot/godot_color.cpp @@ -50,6 +50,61 @@ void GDAPI godot_color_new_rgb(godot_color *r_dest, const godot_real p_r, const *dest = Color(p_r, p_g, p_b); } +godot_real godot_color_get_r(const godot_color *p_self) { + const Color *self = (const Color *)p_self; + return self->r; +} + +void godot_color_set_r(godot_color *p_self, const godot_real val) { + Color *self = (Color *)p_self; + self->r = val; +} + +godot_real godot_color_get_g(const godot_color *p_self) { + const Color *self = (const Color *)p_self; + return self->g; +} + +void godot_color_set_g(godot_color *p_self, const godot_real val) { + Color *self = (Color *)p_self; + self->g = val; +} + +godot_real godot_color_get_b(const godot_color *p_self) { + const Color *self = (const Color *)p_self; + return self->b; +} + +void godot_color_set_b(godot_color *p_self, const godot_real val) { + Color *self = (Color *)p_self; + self->b = val; +} + +godot_real godot_color_get_a(const godot_color *p_self) { + const Color *self = (const Color *)p_self; + return self->a; +} + +void godot_color_set_a(godot_color *p_self, const godot_real val) { + Color *self = (Color *)p_self; + self->a = val; +} + +godot_real godot_color_get_h(const godot_color *p_self) { + const Color *self = (const Color *)p_self; + return self->get_h(); +} + +godot_real godot_color_get_s(const godot_color *p_self) { + const Color *self = (const Color *)p_self; + return self->get_s(); +} + +godot_real godot_color_get_v(const godot_color *p_self) { + const Color *self = (const Color *)p_self; + return self->get_v(); +} + godot_string GDAPI godot_color_as_string(const godot_color *p_self) { godot_string ret; const Color *self = (const Color *)p_self; @@ -106,7 +161,7 @@ godot_string GDAPI godot_color_to_html(const godot_color *p_self, const godot_bo godot_string dest; const Color *self = (const Color *)p_self; - *((String *)&dest) = self->to_html(p_with_alpha); + memnew_placement(&dest, String(self->to_html(p_with_alpha))); return dest; } diff --git a/modules/gdnative/godot/godot_color.h b/modules/gdnative/godot/godot_color.h index 8588c997ea..10dc228b1c 100644 --- a/modules/gdnative/godot/godot_color.h +++ b/modules/gdnative/godot/godot_color.h @@ -49,6 +49,22 @@ typedef struct godot_color { void GDAPI godot_color_new_rgba(godot_color *r_dest, const godot_real p_r, const godot_real p_g, const godot_real p_b, const godot_real p_a); void GDAPI godot_color_new_rgb(godot_color *r_dest, const godot_real p_r, const godot_real p_g, const godot_real p_b); +godot_real godot_color_get_r(const godot_color *p_self); +void godot_color_set_r(godot_color *p_self, const godot_real r); + +godot_real godot_color_get_g(const godot_color *p_self); +void godot_color_set_g(godot_color *p_self, const godot_real g); + +godot_real godot_color_get_b(const godot_color *p_self); +void godot_color_set_b(godot_color *p_self, const godot_real b); + +godot_real godot_color_get_a(const godot_color *p_self); +void godot_color_set_a(godot_color *p_self, const godot_real a); + +godot_real godot_color_get_h(const godot_color *p_self); +godot_real godot_color_get_s(const godot_color *p_self); +godot_real godot_color_get_v(const godot_color *p_self); + godot_string GDAPI godot_color_as_string(const godot_color *p_self); godot_int GDAPI godot_color_to_32(const godot_color *p_self); diff --git a/modules/gdnative/godot/godot_dictionary.cpp b/modules/gdnative/godot/godot_dictionary.cpp index deec5f8ffb..12c40f0564 100644 --- a/modules/gdnative/godot/godot_dictionary.cpp +++ b/modules/gdnative/godot/godot_dictionary.cpp @@ -44,9 +44,9 @@ void GDAPI godot_dictionary_new(godot_dictionary *r_dest) { memnew_placement(dest, Dictionary); } -void GDAPI godot_dictionary_new_copy(godot_dictionary *r_dest, const godot_dictionary *r_src) { +void GDAPI godot_dictionary_new_copy(godot_dictionary *r_dest, const godot_dictionary *p_src) { Dictionary *dest = (Dictionary *)r_dest; - const Dictionary *src = (const Dictionary *)r_src; + const Dictionary *src = (const Dictionary *)p_src; memnew_placement(dest, Dictionary(*src)); } @@ -107,10 +107,26 @@ godot_array GDAPI godot_dictionary_values(const godot_dictionary *p_self) { return dest; } -godot_variant GDAPI *godot_dictionary_operator_index(godot_dictionary *p_dict, const godot_variant *p_key) { - Dictionary *dict = (Dictionary *)p_dict; +godot_variant GDAPI godot_dictionary_get(const godot_dictionary *p_self, const godot_variant *p_key) { + godot_variant raw_dest; + Variant *dest = (Variant *)&raw_dest; + const Dictionary *self = (const Dictionary *)p_self; + const Variant *key = (const Variant *)p_key; + memnew_placement(dest, Variant(self->operator[](*key))); + return raw_dest; +} + +void GDAPI godot_dictionary_set(godot_dictionary *p_self, const godot_variant *p_key, const godot_variant *p_value) { + Dictionary *self = (Dictionary *)p_self; const Variant *key = (const Variant *)p_key; - return (godot_variant *)&dict->operator[](*key); + const Variant *value = (const Variant *)p_value; + self->operator[](*key) = *value; +} + +godot_variant GDAPI *godot_dictionary_operator_index(godot_dictionary *p_self, const godot_variant *p_key) { + Array *self = (Array *)p_self; + const Variant *key = (const Variant *)p_key; + return (godot_variant *)&self->operator[](*key); } godot_bool GDAPI godot_dictionary_operator_equal(const godot_dictionary *p_self, const godot_dictionary *p_b) { @@ -119,11 +135,11 @@ godot_bool GDAPI godot_dictionary_operator_equal(const godot_dictionary *p_self, return *self == *b; } -godot_string GDAPI godot_dictionary_to_json(const godot_dictionary *p_dict) { +godot_string GDAPI godot_dictionary_to_json(const godot_dictionary *p_self) { godot_string raw_dest; String *dest = (String *)&raw_dest; - const Dictionary *dict = (const Dictionary *)p_dict; - memnew_placement(dest, String(JSON::print(Variant(*dict)))); + const Dictionary *self = (const Dictionary *)p_self; + memnew_placement(dest, String(JSON::print(Variant(*self)))); return raw_dest; } diff --git a/modules/gdnative/godot/godot_dictionary.h b/modules/gdnative/godot/godot_dictionary.h index a89bd4bba1..0325670b15 100644 --- a/modules/gdnative/godot/godot_dictionary.h +++ b/modules/gdnative/godot/godot_dictionary.h @@ -48,7 +48,7 @@ typedef struct godot_dictionary { #include "godot_variant.h" void GDAPI godot_dictionary_new(godot_dictionary *r_dest); -void GDAPI godot_dictionary_new_copy(godot_dictionary *r_dest, const godot_dictionary *r_src); +void GDAPI godot_dictionary_new_copy(godot_dictionary *r_dest, const godot_dictionary *p_src); void GDAPI godot_dictionary_destroy(godot_dictionary *p_self); godot_int GDAPI godot_dictionary_size(const godot_dictionary *p_self); @@ -69,11 +69,14 @@ godot_array GDAPI godot_dictionary_keys(const godot_dictionary *p_self); godot_array GDAPI godot_dictionary_values(const godot_dictionary *p_self); -godot_variant GDAPI *godot_dictionary_operator_index(godot_dictionary *p_dict, const godot_variant *p_key); +godot_variant GDAPI godot_dictionary_get(const godot_dictionary *p_self, const godot_variant *p_key); +void GDAPI godot_dictionary_set(godot_dictionary *p_self, const godot_variant *p_key, const godot_variant *p_value); + +godot_variant GDAPI *godot_dictionary_operator_index(godot_dictionary *p_self, const godot_variant *p_key); godot_bool GDAPI godot_dictionary_operator_equal(const godot_dictionary *p_self, const godot_dictionary *p_b); -godot_string GDAPI godot_dictionary_to_json(const godot_dictionary *p_dict); +godot_string GDAPI godot_dictionary_to_json(const godot_dictionary *p_self); #ifdef __cplusplus } diff --git a/modules/gdnative/godot/godot_node_path.cpp b/modules/gdnative/godot/godot_node_path.cpp index 165688a340..c8eacd05af 100644 --- a/modules/gdnative/godot/godot_node_path.cpp +++ b/modules/gdnative/godot/godot_node_path.cpp @@ -44,6 +44,12 @@ void GDAPI godot_node_path_new(godot_node_path *r_dest, const godot_string *p_fr memnew_placement(dest, NodePath(*from)); } +void GDAPI godot_node_path_new_copy(godot_node_path *r_dest, const godot_node_path *p_src) { + NodePath *dest = (NodePath *)r_dest; + const NodePath *src = (const NodePath *)p_src; + memnew_placement(dest, NodePath(*src)); +} + void GDAPI godot_node_path_destroy(godot_node_path *p_self) { NodePath *self = (NodePath *)p_self; self->~NodePath(); diff --git a/modules/gdnative/godot/godot_node_path.h b/modules/gdnative/godot/godot_node_path.h index fb94bd3822..b0c9d44859 100644 --- a/modules/gdnative/godot/godot_node_path.h +++ b/modules/gdnative/godot/godot_node_path.h @@ -47,6 +47,7 @@ typedef struct godot_node_path { #include "godot_string.h" void GDAPI godot_node_path_new(godot_node_path *r_dest, const godot_string *p_from); +void GDAPI godot_node_path_new_copy(godot_node_path *r_dest, const godot_node_path *p_src); void GDAPI godot_node_path_destroy(godot_node_path *p_self); godot_string GDAPI godot_node_path_as_string(const godot_node_path *p_self); diff --git a/modules/gdnative/godot/godot_pool_arrays.cpp b/modules/gdnative/godot/godot_pool_arrays.cpp index ff4586ebe7..ea9aceea81 100644 --- a/modules/gdnative/godot/godot_pool_arrays.cpp +++ b/modules/gdnative/godot/godot_pool_arrays.cpp @@ -44,584 +44,584 @@ void _pool_arrays_api_anchor() { // byte -void GDAPI godot_pool_byte_array_new(godot_pool_byte_array *p_pba) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; - memnew_placement(pba, PoolVector<uint8_t>); +void GDAPI godot_pool_byte_array_new(godot_pool_byte_array *r_dest) { + PoolVector<uint8_t> *dest = (PoolVector<uint8_t> *)r_dest; + memnew_placement(dest, PoolVector<uint8_t>); } -void GDAPI godot_pool_byte_array_new_copy(godot_pool_byte_array *p_dest, const godot_pool_byte_array *p_src) { - PoolVector<uint8_t> *dest = (PoolVector<uint8_t> *)p_dest; +void GDAPI godot_pool_byte_array_new_copy(godot_pool_byte_array *r_dest, const godot_pool_byte_array *p_src) { + PoolVector<uint8_t> *dest = (PoolVector<uint8_t> *)r_dest; const PoolVector<uint8_t> *src = (const PoolVector<uint8_t> *)p_src; memnew_placement(dest, PoolVector<uint8_t>(*src)); } -void GDAPI godot_pool_byte_array_new_with_array(godot_pool_byte_array *p_pba, const godot_array *p_a) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; +void GDAPI godot_pool_byte_array_new_with_array(godot_pool_byte_array *r_dest, const godot_array *p_a) { + PoolVector<uint8_t> *dest = (PoolVector<uint8_t> *)r_dest; Array *a = (Array *)p_a; - memnew_placement(pba, PoolVector<uint8_t>); + memnew_placement(dest, PoolVector<uint8_t>); - pba->resize(a->size()); + dest->resize(a->size()); for (size_t i = 0; i < a->size(); i++) { - pba->set(i, (*a)[i]); + dest->set(i, (*a)[i]); } } -void GDAPI godot_pool_byte_array_append(godot_pool_byte_array *p_pba, const uint8_t p_data) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; - pba->append(p_data); +void GDAPI godot_pool_byte_array_append(godot_pool_byte_array *p_self, const uint8_t p_data) { + PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self; + self->append(p_data); } -void GDAPI godot_pool_byte_array_append_array(godot_pool_byte_array *p_pba, const godot_pool_byte_array *p_array) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; +void GDAPI godot_pool_byte_array_append_array(godot_pool_byte_array *p_self, const godot_pool_byte_array *p_array) { + PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self; PoolVector<uint8_t> *array = (PoolVector<uint8_t> *)p_array; - pba->append_array(*array); + self->append_array(*array); } -int GDAPI godot_pool_byte_array_insert(godot_pool_byte_array *p_pba, const godot_int p_idx, const uint8_t p_data) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; - return pba->insert(p_idx, p_data); +godot_error GDAPI godot_pool_byte_array_insert(godot_pool_byte_array *p_self, const godot_int p_idx, const uint8_t p_data) { + PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self; + return (godot_error)self->insert(p_idx, p_data); } -void GDAPI godot_pool_byte_array_invert(godot_pool_byte_array *p_pba) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; - pba->invert(); +void GDAPI godot_pool_byte_array_invert(godot_pool_byte_array *p_self) { + PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self; + self->invert(); } -void GDAPI godot_pool_byte_array_push_back(godot_pool_byte_array *p_pba, const uint8_t p_data) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; - pba->push_back(p_data); +void GDAPI godot_pool_byte_array_push_back(godot_pool_byte_array *p_self, const uint8_t p_data) { + PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self; + self->push_back(p_data); } -void GDAPI godot_pool_byte_array_remove(godot_pool_byte_array *p_pba, const godot_int p_idx) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; - pba->remove(p_idx); +void GDAPI godot_pool_byte_array_remove(godot_pool_byte_array *p_self, const godot_int p_idx) { + PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self; + self->remove(p_idx); } -void GDAPI godot_pool_byte_array_resize(godot_pool_byte_array *p_pba, const godot_int p_size) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; - pba->resize(p_size); +void GDAPI godot_pool_byte_array_resize(godot_pool_byte_array *p_self, const godot_int p_size) { + PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self; + self->resize(p_size); } -void GDAPI godot_pool_byte_array_set(godot_pool_byte_array *p_pba, const godot_int p_idx, const uint8_t p_data) { - PoolVector<uint8_t> *pba = (PoolVector<uint8_t> *)p_pba; - pba->set(p_idx, p_data); +void GDAPI godot_pool_byte_array_set(godot_pool_byte_array *p_self, const godot_int p_idx, const uint8_t p_data) { + PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self; + self->set(p_idx, p_data); } -uint8_t GDAPI godot_pool_byte_array_get(const godot_pool_byte_array *p_pba, const godot_int p_idx) { - const PoolVector<uint8_t> *pba = (const PoolVector<uint8_t> *)p_pba; - return pba->get(p_idx); +uint8_t GDAPI godot_pool_byte_array_get(const godot_pool_byte_array *p_self, const godot_int p_idx) { + const PoolVector<uint8_t> *self = (const PoolVector<uint8_t> *)p_self; + return self->get(p_idx); } -godot_int GDAPI godot_pool_byte_array_size(const godot_pool_byte_array *p_pba) { - const PoolVector<uint8_t> *pba = (const PoolVector<uint8_t> *)p_pba; - return pba->size(); +godot_int GDAPI godot_pool_byte_array_size(const godot_pool_byte_array *p_self) { + const PoolVector<uint8_t> *self = (const PoolVector<uint8_t> *)p_self; + return self->size(); } -void GDAPI godot_pool_byte_array_destroy(godot_pool_byte_array *p_pba) { - ((PoolVector<uint8_t> *)p_pba)->~PoolVector(); +void GDAPI godot_pool_byte_array_destroy(godot_pool_byte_array *p_self) { + ((PoolVector<uint8_t> *)p_self)->~PoolVector(); } // int -void GDAPI godot_pool_int_array_new(godot_pool_int_array *p_pba) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; - memnew_placement(pba, PoolVector<godot_int>); +void GDAPI godot_pool_int_array_new(godot_pool_int_array *r_dest) { + PoolVector<godot_int> *dest = (PoolVector<godot_int> *)r_dest; + memnew_placement(dest, PoolVector<godot_int>); } -void GDAPI godot_pool_int_array_new_copy(godot_pool_int_array *p_dest, const godot_pool_int_array *p_src) { - PoolVector<godot_int> *dest = (PoolVector<godot_int> *)p_dest; +void GDAPI godot_pool_int_array_new_copy(godot_pool_int_array *r_dest, const godot_pool_int_array *p_src) { + PoolVector<godot_int> *dest = (PoolVector<godot_int> *)r_dest; const PoolVector<godot_int> *src = (const PoolVector<godot_int> *)p_src; memnew_placement(dest, PoolVector<godot_int>(*src)); } -void GDAPI godot_pool_int_array_new_with_array(godot_pool_int_array *p_pba, const godot_array *p_a) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; +void GDAPI godot_pool_int_array_new_with_array(godot_pool_int_array *r_dest, const godot_array *p_a) { + PoolVector<godot_int> *dest = (PoolVector<godot_int> *)r_dest; Array *a = (Array *)p_a; - memnew_placement(pba, PoolVector<godot_int>); + memnew_placement(dest, PoolVector<godot_int>); - pba->resize(a->size()); + dest->resize(a->size()); for (size_t i = 0; i < a->size(); i++) { - pba->set(i, (*a)[i]); + dest->set(i, (*a)[i]); } } -void GDAPI godot_pool_int_array_append(godot_pool_int_array *p_pba, const godot_int p_data) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; - pba->append(p_data); +void GDAPI godot_pool_int_array_append(godot_pool_int_array *p_self, const godot_int p_data) { + PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self; + self->append(p_data); } -void GDAPI godot_pool_int_array_append_array(godot_pool_int_array *p_pba, const godot_pool_int_array *p_array) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; +void GDAPI godot_pool_int_array_append_array(godot_pool_int_array *p_self, const godot_pool_int_array *p_array) { + PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self; PoolVector<godot_int> *array = (PoolVector<godot_int> *)p_array; - pba->append_array(*array); + self->append_array(*array); } -int GDAPI godot_pool_int_array_insert(godot_pool_int_array *p_pba, const godot_int p_idx, const godot_int p_data) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; - return pba->insert(p_idx, p_data); +godot_error GDAPI godot_pool_int_array_insert(godot_pool_int_array *p_self, const godot_int p_idx, const godot_int p_data) { + PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self; + return (godot_error)self->insert(p_idx, p_data); } -void GDAPI godot_pool_int_array_invert(godot_pool_int_array *p_pba) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; - pba->invert(); +void GDAPI godot_pool_int_array_invert(godot_pool_int_array *p_self) { + PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self; + self->invert(); } -void GDAPI godot_pool_int_array_push_back(godot_pool_int_array *p_pba, const godot_int p_data) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; - pba->push_back(p_data); +void GDAPI godot_pool_int_array_push_back(godot_pool_int_array *p_self, const godot_int p_data) { + PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self; + self->push_back(p_data); } -void GDAPI godot_pool_int_array_remove(godot_pool_int_array *p_pba, const godot_int p_idx) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; - pba->remove(p_idx); +void GDAPI godot_pool_int_array_remove(godot_pool_int_array *p_self, const godot_int p_idx) { + PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self; + self->remove(p_idx); } -void GDAPI godot_pool_int_array_resize(godot_pool_int_array *p_pba, const godot_int p_size) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; - pba->resize(p_size); +void GDAPI godot_pool_int_array_resize(godot_pool_int_array *p_self, const godot_int p_size) { + PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self; + self->resize(p_size); } -void GDAPI godot_pool_int_array_set(godot_pool_int_array *p_pba, const godot_int p_idx, const godot_int p_data) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; - pba->set(p_idx, p_data); +void GDAPI godot_pool_int_array_set(godot_pool_int_array *p_self, const godot_int p_idx, const godot_int p_data) { + PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self; + self->set(p_idx, p_data); } -godot_int GDAPI godot_pool_int_array_get(const godot_pool_int_array *p_pba, const godot_int p_idx) { - const PoolVector<godot_int> *pba = (const PoolVector<godot_int> *)p_pba; - return pba->get(p_idx); +godot_int GDAPI godot_pool_int_array_get(const godot_pool_int_array *p_self, const godot_int p_idx) { + const PoolVector<godot_int> *self = (const PoolVector<godot_int> *)p_self; + return self->get(p_idx); } -godot_int GDAPI godot_pool_int_array_size(const godot_pool_int_array *p_pba) { - const PoolVector<godot_int> *pba = (const PoolVector<godot_int> *)p_pba; - return pba->size(); +godot_int GDAPI godot_pool_int_array_size(const godot_pool_int_array *p_self) { + const PoolVector<godot_int> *self = (const PoolVector<godot_int> *)p_self; + return self->size(); } -void GDAPI godot_pool_int_array_destroy(godot_pool_int_array *p_pba) { - ((PoolVector<godot_int> *)p_pba)->~PoolVector(); +void GDAPI godot_pool_int_array_destroy(godot_pool_int_array *p_self) { + ((PoolVector<godot_int> *)p_self)->~PoolVector(); } // real -void GDAPI godot_pool_real_array_new(godot_pool_real_array *p_pba) { - PoolVector<godot_real> *pba = (PoolVector<godot_real> *)p_pba; - memnew_placement(pba, PoolVector<godot_real>); +void GDAPI godot_pool_real_array_new(godot_pool_real_array *r_dest) { + PoolVector<godot_real> *dest = (PoolVector<godot_real> *)r_dest; + memnew_placement(dest, PoolVector<godot_real>); } -void GDAPI godot_pool_real_array_new_copy(godot_pool_real_array *p_dest, const godot_pool_real_array *p_src) { - PoolVector<godot_real> *dest = (PoolVector<godot_real> *)p_dest; +void GDAPI godot_pool_real_array_new_copy(godot_pool_real_array *r_dest, const godot_pool_real_array *p_src) { + PoolVector<godot_real> *dest = (PoolVector<godot_real> *)r_dest; const PoolVector<godot_real> *src = (const PoolVector<godot_real> *)p_src; memnew_placement(dest, PoolVector<godot_real>(*src)); } -void GDAPI godot_pool_real_array_new_with_array(godot_pool_real_array *p_pba, const godot_array *p_a) { - PoolVector<godot_real> *pba = (PoolVector<godot_real> *)p_pba; +void GDAPI godot_pool_real_array_new_with_array(godot_pool_real_array *r_dest, const godot_array *p_a) { + PoolVector<godot_real> *dest = (PoolVector<godot_real> *)r_dest; Array *a = (Array *)p_a; - memnew_placement(pba, PoolVector<godot_real>); + memnew_placement(dest, PoolVector<godot_real>); - pba->resize(a->size()); + dest->resize(a->size()); for (size_t i = 0; i < a->size(); i++) { - pba->set(i, (*a)[i]); + dest->set(i, (*a)[i]); } } -void GDAPI godot_pool_real_array_append(godot_pool_real_array *p_pba, const godot_real p_data) { - PoolVector<godot_real> *pba = (PoolVector<godot_real> *)p_pba; - pba->append(p_data); +void GDAPI godot_pool_real_array_append(godot_pool_real_array *p_self, const godot_real p_data) { + PoolVector<godot_real> *self = (PoolVector<godot_real> *)p_self; + self->append(p_data); } -void GDAPI godot_pool_real_array_append_array(godot_pool_real_array *p_pba, const godot_pool_real_array *p_array) { - PoolVector<godot_real> *pba = (PoolVector<godot_real> *)p_pba; +void GDAPI godot_pool_real_array_append_array(godot_pool_real_array *p_self, const godot_pool_real_array *p_array) { + PoolVector<godot_real> *self = (PoolVector<godot_real> *)p_self; PoolVector<godot_real> *array = (PoolVector<godot_real> *)p_array; - pba->append_array(*array); + self->append_array(*array); } -int GDAPI godot_pool_real_array_insert(godot_pool_real_array *p_pba, const godot_int p_idx, const godot_real p_data) { - PoolVector<godot_real> *pba = (PoolVector<godot_real> *)p_pba; - return pba->insert(p_idx, p_data); +godot_error GDAPI godot_pool_real_array_insert(godot_pool_real_array *p_self, const godot_int p_idx, const godot_real p_data) { + PoolVector<godot_real> *self = (PoolVector<godot_real> *)p_self; + return (godot_error)self->insert(p_idx, p_data); } -void GDAPI godot_pool_real_array_invert(godot_pool_real_array *p_pba) { - PoolVector<godot_real> *pba = (PoolVector<godot_real> *)p_pba; - pba->invert(); +void GDAPI godot_pool_real_array_invert(godot_pool_real_array *p_self) { + PoolVector<godot_real> *self = (PoolVector<godot_real> *)p_self; + self->invert(); } -void GDAPI godot_pool_real_array_push_back(godot_pool_real_array *p_pba, const godot_real p_data) { - PoolVector<godot_real> *pba = (PoolVector<godot_real> *)p_pba; - pba->push_back(p_data); +void GDAPI godot_pool_real_array_push_back(godot_pool_real_array *p_self, const godot_real p_data) { + PoolVector<godot_real> *self = (PoolVector<godot_real> *)p_self; + self->push_back(p_data); } -void GDAPI godot_pool_real_array_remove(godot_pool_real_array *p_pba, const godot_int p_idx) { - PoolVector<godot_real> *pba = (PoolVector<godot_real> *)p_pba; - pba->remove(p_idx); +void GDAPI godot_pool_real_array_remove(godot_pool_real_array *p_self, const godot_int p_idx) { + PoolVector<godot_real> *self = (PoolVector<godot_real> *)p_self; + self->remove(p_idx); } -void GDAPI godot_pool_real_array_resize(godot_pool_real_array *p_pba, const godot_int p_size) { - PoolVector<godot_int> *pba = (PoolVector<godot_int> *)p_pba; - pba->resize(p_size); +void GDAPI godot_pool_real_array_resize(godot_pool_real_array *p_self, const godot_int p_size) { + PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self; + self->resize(p_size); } -void GDAPI godot_pool_real_array_set(godot_pool_real_array *p_pba, const godot_int p_idx, const godot_real p_data) { - PoolVector<godot_real> *pba = (PoolVector<godot_real> *)p_pba; - pba->set(p_idx, p_data); +void GDAPI godot_pool_real_array_set(godot_pool_real_array *p_self, const godot_int p_idx, const godot_real p_data) { + PoolVector<godot_real> *self = (PoolVector<godot_real> *)p_self; + self->set(p_idx, p_data); } -godot_real GDAPI godot_pool_real_array_get(const godot_pool_real_array *p_pba, const godot_int p_idx) { - const PoolVector<godot_real> *pba = (const PoolVector<godot_real> *)p_pba; - return pba->get(p_idx); +godot_real GDAPI godot_pool_real_array_get(const godot_pool_real_array *p_self, const godot_int p_idx) { + const PoolVector<godot_real> *self = (const PoolVector<godot_real> *)p_self; + return self->get(p_idx); } -godot_int GDAPI godot_pool_real_array_size(const godot_pool_real_array *p_pba) { - const PoolVector<godot_real> *pba = (const PoolVector<godot_real> *)p_pba; - return pba->size(); +godot_int GDAPI godot_pool_real_array_size(const godot_pool_real_array *p_self) { + const PoolVector<godot_real> *self = (const PoolVector<godot_real> *)p_self; + return self->size(); } -void GDAPI godot_pool_real_array_destroy(godot_pool_real_array *p_pba) { - ((PoolVector<godot_real> *)p_pba)->~PoolVector(); +void GDAPI godot_pool_real_array_destroy(godot_pool_real_array *p_self) { + ((PoolVector<godot_real> *)p_self)->~PoolVector(); } // string -void GDAPI godot_pool_string_array_new(godot_pool_string_array *p_pba) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; - memnew_placement(pba, PoolVector<String>); +void GDAPI godot_pool_string_array_new(godot_pool_string_array *r_dest) { + PoolVector<String> *dest = (PoolVector<String> *)r_dest; + memnew_placement(dest, PoolVector<String>); } -void GDAPI godot_pool_string_array_new_copy(godot_pool_string_array *p_dest, const godot_pool_string_array *p_src) { - PoolVector<String> *dest = (PoolVector<String> *)p_dest; +void GDAPI godot_pool_string_array_new_copy(godot_pool_string_array *r_dest, const godot_pool_string_array *p_src) { + PoolVector<String> *dest = (PoolVector<String> *)r_dest; const PoolVector<String> *src = (const PoolVector<String> *)p_src; memnew_placement(dest, PoolVector<String>(*src)); } -void GDAPI godot_pool_string_array_new_with_array(godot_pool_string_array *p_pba, const godot_array *p_a) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; +void GDAPI godot_pool_string_array_new_with_array(godot_pool_string_array *r_dest, const godot_array *p_a) { + PoolVector<String> *dest = (PoolVector<String> *)r_dest; Array *a = (Array *)p_a; - memnew_placement(pba, PoolVector<String>); + memnew_placement(dest, PoolVector<String>); - pba->resize(a->size()); + dest->resize(a->size()); for (size_t i = 0; i < a->size(); i++) { - pba->set(i, (*a)[i]); + dest->set(i, (*a)[i]); } } -void GDAPI godot_pool_string_array_append(godot_pool_string_array *p_pba, const godot_string *p_data) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; +void GDAPI godot_pool_string_array_append(godot_pool_string_array *p_self, const godot_string *p_data) { + PoolVector<String> *self = (PoolVector<String> *)p_self; String &s = *(String *)p_data; - pba->append(s); + self->append(s); } -void GDAPI godot_pool_string_array_append_array(godot_pool_string_array *p_pba, const godot_pool_string_array *p_array) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; +void GDAPI godot_pool_string_array_append_array(godot_pool_string_array *p_self, const godot_pool_string_array *p_array) { + PoolVector<String> *self = (PoolVector<String> *)p_self; PoolVector<String> *array = (PoolVector<String> *)p_array; - pba->append_array(*array); + self->append_array(*array); } -int GDAPI godot_pool_string_array_insert(godot_pool_string_array *p_pba, const godot_int p_idx, const godot_string *p_data) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; +godot_error GDAPI godot_pool_string_array_insert(godot_pool_string_array *p_self, const godot_int p_idx, const godot_string *p_data) { + PoolVector<String> *self = (PoolVector<String> *)p_self; String &s = *(String *)p_data; - return pba->insert(p_idx, s); + return (godot_error)self->insert(p_idx, s); } -void GDAPI godot_pool_string_array_invert(godot_pool_string_array *p_pba) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; - pba->invert(); +void GDAPI godot_pool_string_array_invert(godot_pool_string_array *p_self) { + PoolVector<String> *self = (PoolVector<String> *)p_self; + self->invert(); } -void GDAPI godot_pool_string_array_push_back(godot_pool_string_array *p_pba, const godot_string *p_data) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; +void GDAPI godot_pool_string_array_push_back(godot_pool_string_array *p_self, const godot_string *p_data) { + PoolVector<String> *self = (PoolVector<String> *)p_self; String &s = *(String *)p_data; - pba->push_back(s); + self->push_back(s); } -void GDAPI godot_pool_string_array_remove(godot_pool_string_array *p_pba, const godot_int p_idx) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; - pba->remove(p_idx); +void GDAPI godot_pool_string_array_remove(godot_pool_string_array *p_self, const godot_int p_idx) { + PoolVector<String> *self = (PoolVector<String> *)p_self; + self->remove(p_idx); } -void GDAPI godot_pool_string_array_resize(godot_pool_string_array *p_pba, const godot_int p_size) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; - pba->resize(p_size); +void GDAPI godot_pool_string_array_resize(godot_pool_string_array *p_self, const godot_int p_size) { + PoolVector<String> *self = (PoolVector<String> *)p_self; + self->resize(p_size); } -void GDAPI godot_pool_string_array_set(godot_pool_string_array *p_pba, const godot_int p_idx, const godot_string *p_data) { - PoolVector<String> *pba = (PoolVector<String> *)p_pba; +void GDAPI godot_pool_string_array_set(godot_pool_string_array *p_self, const godot_int p_idx, const godot_string *p_data) { + PoolVector<String> *self = (PoolVector<String> *)p_self; String &s = *(String *)p_data; - pba->set(p_idx, s); + self->set(p_idx, s); } -godot_string GDAPI godot_pool_string_array_get(const godot_pool_string_array *p_pba, const godot_int p_idx) { - const PoolVector<String> *pba = (const PoolVector<String> *)p_pba; +godot_string GDAPI godot_pool_string_array_get(const godot_pool_string_array *p_self, const godot_int p_idx) { + const PoolVector<String> *self = (const PoolVector<String> *)p_self; godot_string str; String *s = (String *)&str; memnew_placement(s, String); - *s = pba->get(p_idx); + *s = self->get(p_idx); return str; } -godot_int GDAPI godot_pool_string_array_size(const godot_pool_string_array *p_pba) { - const PoolVector<String> *pba = (const PoolVector<String> *)p_pba; - return pba->size(); +godot_int GDAPI godot_pool_string_array_size(const godot_pool_string_array *p_self) { + const PoolVector<String> *self = (const PoolVector<String> *)p_self; + return self->size(); } -void GDAPI godot_pool_string_array_destroy(godot_pool_string_array *p_pba) { - ((PoolVector<String> *)p_pba)->~PoolVector(); +void GDAPI godot_pool_string_array_destroy(godot_pool_string_array *p_self) { + ((PoolVector<String> *)p_self)->~PoolVector(); } // vector2 -void GDAPI godot_pool_vector2_array_new(godot_pool_vector2_array *p_pba) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; - memnew_placement(pba, PoolVector<Vector2>); +void GDAPI godot_pool_vector2_array_new(godot_pool_vector2_array *r_dest) { + PoolVector<Vector2> *dest = (PoolVector<Vector2> *)r_dest; + memnew_placement(dest, PoolVector<Vector2>); } -void GDAPI godot_pool_vector2_array_new_copy(godot_pool_vector2_array *p_dest, const godot_pool_vector2_array *p_src) { - PoolVector<Vector2> *dest = (PoolVector<Vector2> *)p_dest; +void GDAPI godot_pool_vector2_array_new_copy(godot_pool_vector2_array *r_dest, const godot_pool_vector2_array *p_src) { + PoolVector<Vector2> *dest = (PoolVector<Vector2> *)r_dest; const PoolVector<Vector2> *src = (const PoolVector<Vector2> *)p_src; memnew_placement(dest, PoolVector<Vector2>(*src)); } -void GDAPI godot_pool_vector2_array_new_with_array(godot_pool_vector2_array *p_pba, const godot_array *p_a) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; +void GDAPI godot_pool_vector2_array_new_with_array(godot_pool_vector2_array *r_dest, const godot_array *p_a) { + PoolVector<Vector2> *dest = (PoolVector<Vector2> *)r_dest; Array *a = (Array *)p_a; - memnew_placement(pba, PoolVector<Vector2>); + memnew_placement(dest, PoolVector<Vector2>); - pba->resize(a->size()); + dest->resize(a->size()); for (size_t i = 0; i < a->size(); i++) { - pba->set(i, (*a)[i]); + dest->set(i, (*a)[i]); } } -void GDAPI godot_pool_vector2_array_append(godot_pool_vector2_array *p_pba, const godot_vector2 *p_data) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; +void GDAPI godot_pool_vector2_array_append(godot_pool_vector2_array *p_self, const godot_vector2 *p_data) { + PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self; Vector2 &s = *(Vector2 *)p_data; - pba->append(s); + self->append(s); } -void GDAPI godot_pool_vector2_array_append_array(godot_pool_vector2_array *p_pba, const godot_pool_vector2_array *p_array) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; +void GDAPI godot_pool_vector2_array_append_array(godot_pool_vector2_array *p_self, const godot_pool_vector2_array *p_array) { + PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self; PoolVector<Vector2> *array = (PoolVector<Vector2> *)p_array; - pba->append_array(*array); + self->append_array(*array); } -int GDAPI godot_pool_vector2_array_insert(godot_pool_vector2_array *p_pba, const godot_int p_idx, const godot_vector2 *p_data) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; +godot_error GDAPI godot_pool_vector2_array_insert(godot_pool_vector2_array *p_self, const godot_int p_idx, const godot_vector2 *p_data) { + PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self; Vector2 &s = *(Vector2 *)p_data; - return pba->insert(p_idx, s); + return (godot_error)self->insert(p_idx, s); } -void GDAPI godot_pool_vector2_array_invert(godot_pool_vector2_array *p_pba) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; - pba->invert(); +void GDAPI godot_pool_vector2_array_invert(godot_pool_vector2_array *p_self) { + PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self; + self->invert(); } -void GDAPI godot_pool_vector2_array_push_back(godot_pool_vector2_array *p_pba, const godot_vector2 *p_data) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; +void GDAPI godot_pool_vector2_array_push_back(godot_pool_vector2_array *p_self, const godot_vector2 *p_data) { + PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self; Vector2 &s = *(Vector2 *)p_data; - pba->push_back(s); + self->push_back(s); } -void GDAPI godot_pool_vector2_array_remove(godot_pool_vector2_array *p_pba, const godot_int p_idx) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; - pba->remove(p_idx); +void GDAPI godot_pool_vector2_array_remove(godot_pool_vector2_array *p_self, const godot_int p_idx) { + PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self; + self->remove(p_idx); } -void GDAPI godot_pool_vector2_array_resize(godot_pool_vector2_array *p_pba, const godot_int p_size) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; - pba->resize(p_size); +void GDAPI godot_pool_vector2_array_resize(godot_pool_vector2_array *p_self, const godot_int p_size) { + PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self; + self->resize(p_size); } -void GDAPI godot_pool_vector2_array_set(godot_pool_vector2_array *p_pba, const godot_int p_idx, const godot_vector2 *p_data) { - PoolVector<Vector2> *pba = (PoolVector<Vector2> *)p_pba; +void GDAPI godot_pool_vector2_array_set(godot_pool_vector2_array *p_self, const godot_int p_idx, const godot_vector2 *p_data) { + PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self; Vector2 &s = *(Vector2 *)p_data; - pba->set(p_idx, s); + self->set(p_idx, s); } -godot_vector2 GDAPI godot_pool_vector2_array_get(const godot_pool_vector2_array *p_pba, const godot_int p_idx) { - const PoolVector<Vector2> *pba = (const PoolVector<Vector2> *)p_pba; +godot_vector2 GDAPI godot_pool_vector2_array_get(const godot_pool_vector2_array *p_self, const godot_int p_idx) { + const PoolVector<Vector2> *self = (const PoolVector<Vector2> *)p_self; godot_vector2 v; Vector2 *s = (Vector2 *)&v; - *s = pba->get(p_idx); + *s = self->get(p_idx); return v; } -godot_int GDAPI godot_pool_vector2_array_size(const godot_pool_vector2_array *p_pba) { - const PoolVector<Vector2> *pba = (const PoolVector<Vector2> *)p_pba; - return pba->size(); +godot_int GDAPI godot_pool_vector2_array_size(const godot_pool_vector2_array *p_self) { + const PoolVector<Vector2> *self = (const PoolVector<Vector2> *)p_self; + return self->size(); } -void GDAPI godot_pool_vector2_array_destroy(godot_pool_vector2_array *p_pba) { - ((PoolVector<Vector2> *)p_pba)->~PoolVector(); +void GDAPI godot_pool_vector2_array_destroy(godot_pool_vector2_array *p_self) { + ((PoolVector<Vector2> *)p_self)->~PoolVector(); } // vector3 -void GDAPI godot_pool_vector3_array_new(godot_pool_vector3_array *p_pba) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; - memnew_placement(pba, PoolVector<Vector3>); +void GDAPI godot_pool_vector3_array_new(godot_pool_vector3_array *r_dest) { + PoolVector<Vector3> *dest = (PoolVector<Vector3> *)r_dest; + memnew_placement(dest, PoolVector<Vector3>); } -void GDAPI godot_pool_vector3_array_new_copy(godot_pool_vector3_array *p_dest, const godot_pool_vector3_array *p_src) { - PoolVector<Vector3> *dest = (PoolVector<Vector3> *)p_dest; +void GDAPI godot_pool_vector3_array_new_copy(godot_pool_vector3_array *r_dest, const godot_pool_vector3_array *p_src) { + PoolVector<Vector3> *dest = (PoolVector<Vector3> *)r_dest; const PoolVector<Vector3> *src = (const PoolVector<Vector3> *)p_src; memnew_placement(dest, PoolVector<Vector3>(*src)); } -void GDAPI godot_pool_vector3_array_new_with_array(godot_pool_vector3_array *p_pba, const godot_array *p_a) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; +void GDAPI godot_pool_vector3_array_new_with_array(godot_pool_vector3_array *r_dest, const godot_array *p_a) { + PoolVector<Vector3> *dest = (PoolVector<Vector3> *)r_dest; Array *a = (Array *)p_a; - memnew_placement(pba, PoolVector<Vector3>); + memnew_placement(dest, PoolVector<Vector3>); - pba->resize(a->size()); + dest->resize(a->size()); for (size_t i = 0; i < a->size(); i++) { - pba->set(i, (*a)[i]); + dest->set(i, (*a)[i]); } } -void GDAPI godot_pool_vector3_array_append(godot_pool_vector3_array *p_pba, const godot_vector3 *p_data) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; +void GDAPI godot_pool_vector3_array_append(godot_pool_vector3_array *p_self, const godot_vector3 *p_data) { + PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self; Vector3 &s = *(Vector3 *)p_data; - pba->append(s); + self->append(s); } -void GDAPI godot_pool_vector3_array_append_array(godot_pool_vector3_array *p_pba, const godot_pool_vector3_array *p_array) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; +void GDAPI godot_pool_vector3_array_append_array(godot_pool_vector3_array *p_self, const godot_pool_vector3_array *p_array) { + PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self; PoolVector<Vector3> *array = (PoolVector<Vector3> *)p_array; - pba->append_array(*array); + self->append_array(*array); } -int GDAPI godot_pool_vector3_array_insert(godot_pool_vector3_array *p_pba, const godot_int p_idx, const godot_vector3 *p_data) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; +godot_error GDAPI godot_pool_vector3_array_insert(godot_pool_vector3_array *p_self, const godot_int p_idx, const godot_vector3 *p_data) { + PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self; Vector3 &s = *(Vector3 *)p_data; - return pba->insert(p_idx, s); + return (godot_error)self->insert(p_idx, s); } -void GDAPI godot_pool_vector3_array_invert(godot_pool_vector3_array *p_pba) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; - pba->invert(); +void GDAPI godot_pool_vector3_array_invert(godot_pool_vector3_array *p_self) { + PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self; + self->invert(); } -void GDAPI godot_pool_vector3_array_push_back(godot_pool_vector3_array *p_pba, const godot_vector3 *p_data) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; +void GDAPI godot_pool_vector3_array_push_back(godot_pool_vector3_array *p_self, const godot_vector3 *p_data) { + PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self; Vector3 &s = *(Vector3 *)p_data; - pba->push_back(s); + self->push_back(s); } -void GDAPI godot_pool_vector3_array_remove(godot_pool_vector3_array *p_pba, const godot_int p_idx) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; - pba->remove(p_idx); +void GDAPI godot_pool_vector3_array_remove(godot_pool_vector3_array *p_self, const godot_int p_idx) { + PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self; + self->remove(p_idx); } -void GDAPI godot_pool_vector3_array_resize(godot_pool_vector3_array *p_pba, const godot_int p_size) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; - pba->resize(p_size); +void GDAPI godot_pool_vector3_array_resize(godot_pool_vector3_array *p_self, const godot_int p_size) { + PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self; + self->resize(p_size); } -void GDAPI godot_pool_vector3_array_set(godot_pool_vector3_array *p_pba, const godot_int p_idx, const godot_vector3 *p_data) { - PoolVector<Vector3> *pba = (PoolVector<Vector3> *)p_pba; +void GDAPI godot_pool_vector3_array_set(godot_pool_vector3_array *p_self, const godot_int p_idx, const godot_vector3 *p_data) { + PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self; Vector3 &s = *(Vector3 *)p_data; - pba->set(p_idx, s); + self->set(p_idx, s); } -godot_vector3 GDAPI godot_pool_vector3_array_get(const godot_pool_vector3_array *p_pba, const godot_int p_idx) { - const PoolVector<Vector3> *pba = (const PoolVector<Vector3> *)p_pba; +godot_vector3 GDAPI godot_pool_vector3_array_get(const godot_pool_vector3_array *p_self, const godot_int p_idx) { + const PoolVector<Vector3> *self = (const PoolVector<Vector3> *)p_self; godot_vector3 v; Vector3 *s = (Vector3 *)&v; - *s = pba->get(p_idx); + *s = self->get(p_idx); return v; } -godot_int GDAPI godot_pool_vector3_array_size(const godot_pool_vector3_array *p_pba) { - const PoolVector<Vector3> *pba = (const PoolVector<Vector3> *)p_pba; - return pba->size(); +godot_int GDAPI godot_pool_vector3_array_size(const godot_pool_vector3_array *p_self) { + const PoolVector<Vector3> *self = (const PoolVector<Vector3> *)p_self; + return self->size(); } -void GDAPI godot_pool_vector3_array_destroy(godot_pool_vector3_array *p_pba) { - ((PoolVector<Vector3> *)p_pba)->~PoolVector(); +void GDAPI godot_pool_vector3_array_destroy(godot_pool_vector3_array *p_self) { + ((PoolVector<Vector3> *)p_self)->~PoolVector(); } // color -void GDAPI godot_pool_color_array_new(godot_pool_color_array *p_pba) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; - memnew_placement(pba, PoolVector<Color>); +void GDAPI godot_pool_color_array_new(godot_pool_color_array *r_dest) { + PoolVector<Color> *dest = (PoolVector<Color> *)r_dest; + memnew_placement(dest, PoolVector<Color>); } -void GDAPI godot_pool_color_array_new_copy(godot_pool_color_array *p_dest, const godot_pool_color_array *p_src) { - PoolVector<Color> *dest = (PoolVector<Color> *)p_dest; +void GDAPI godot_pool_color_array_new_copy(godot_pool_color_array *r_dest, const godot_pool_color_array *p_src) { + PoolVector<Color> *dest = (PoolVector<Color> *)r_dest; const PoolVector<Color> *src = (const PoolVector<Color> *)p_src; memnew_placement(dest, PoolVector<Color>(*src)); } -void GDAPI godot_pool_color_array_new_with_array(godot_pool_color_array *p_pba, const godot_array *p_a) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; +void GDAPI godot_pool_color_array_new_with_array(godot_pool_color_array *r_dest, const godot_array *p_a) { + PoolVector<Color> *dest = (PoolVector<Color> *)r_dest; Array *a = (Array *)p_a; - memnew_placement(pba, PoolVector<Color>); + memnew_placement(dest, PoolVector<Color>); - pba->resize(a->size()); + dest->resize(a->size()); for (size_t i = 0; i < a->size(); i++) { - pba->set(i, (*a)[i]); + dest->set(i, (*a)[i]); } } -void GDAPI godot_pool_color_array_append(godot_pool_color_array *p_pba, const godot_color *p_data) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; +void GDAPI godot_pool_color_array_append(godot_pool_color_array *p_self, const godot_color *p_data) { + PoolVector<Color> *self = (PoolVector<Color> *)p_self; Color &s = *(Color *)p_data; - pba->append(s); + self->append(s); } -void GDAPI godot_pool_color_array_append_array(godot_pool_color_array *p_pba, const godot_pool_color_array *p_array) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; +void GDAPI godot_pool_color_array_append_array(godot_pool_color_array *p_self, const godot_pool_color_array *p_array) { + PoolVector<Color> *self = (PoolVector<Color> *)p_self; PoolVector<Color> *array = (PoolVector<Color> *)p_array; - pba->append_array(*array); + self->append_array(*array); } -int GDAPI godot_pool_color_array_insert(godot_pool_color_array *p_pba, const godot_int p_idx, const godot_color *p_data) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; +godot_error GDAPI godot_pool_color_array_insert(godot_pool_color_array *p_self, const godot_int p_idx, const godot_color *p_data) { + PoolVector<Color> *self = (PoolVector<Color> *)p_self; Color &s = *(Color *)p_data; - return pba->insert(p_idx, s); + return (godot_error)self->insert(p_idx, s); } -void GDAPI godot_pool_color_array_invert(godot_pool_color_array *p_pba) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; - pba->invert(); +void GDAPI godot_pool_color_array_invert(godot_pool_color_array *p_self) { + PoolVector<Color> *self = (PoolVector<Color> *)p_self; + self->invert(); } -void GDAPI godot_pool_color_array_push_back(godot_pool_color_array *p_pba, const godot_color *p_data) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; +void GDAPI godot_pool_color_array_push_back(godot_pool_color_array *p_self, const godot_color *p_data) { + PoolVector<Color> *self = (PoolVector<Color> *)p_self; Color &s = *(Color *)p_data; - pba->push_back(s); + self->push_back(s); } -void GDAPI godot_pool_color_array_remove(godot_pool_color_array *p_pba, const godot_int p_idx) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; - pba->remove(p_idx); +void GDAPI godot_pool_color_array_remove(godot_pool_color_array *p_self, const godot_int p_idx) { + PoolVector<Color> *self = (PoolVector<Color> *)p_self; + self->remove(p_idx); } -void GDAPI godot_pool_color_array_resize(godot_pool_color_array *p_pba, const godot_int p_size) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; - pba->resize(p_size); +void GDAPI godot_pool_color_array_resize(godot_pool_color_array *p_self, const godot_int p_size) { + PoolVector<Color> *self = (PoolVector<Color> *)p_self; + self->resize(p_size); } -void GDAPI godot_pool_color_array_set(godot_pool_color_array *p_pba, const godot_int p_idx, const godot_color *p_data) { - PoolVector<Color> *pba = (PoolVector<Color> *)p_pba; +void GDAPI godot_pool_color_array_set(godot_pool_color_array *p_self, const godot_int p_idx, const godot_color *p_data) { + PoolVector<Color> *self = (PoolVector<Color> *)p_self; Color &s = *(Color *)p_data; - pba->set(p_idx, s); + self->set(p_idx, s); } -godot_color GDAPI godot_pool_color_array_get(const godot_pool_color_array *p_pba, const godot_int p_idx) { - const PoolVector<Color> *pba = (const PoolVector<Color> *)p_pba; +godot_color GDAPI godot_pool_color_array_get(const godot_pool_color_array *p_self, const godot_int p_idx) { + const PoolVector<Color> *self = (const PoolVector<Color> *)p_self; godot_color v; Color *s = (Color *)&v; - *s = pba->get(p_idx); + *s = self->get(p_idx); return v; } -godot_int GDAPI godot_pool_color_array_size(const godot_pool_color_array *p_pba) { - const PoolVector<Color> *pba = (const PoolVector<Color> *)p_pba; - return pba->size(); +godot_int GDAPI godot_pool_color_array_size(const godot_pool_color_array *p_self) { + const PoolVector<Color> *self = (const PoolVector<Color> *)p_self; + return self->size(); } -void GDAPI godot_pool_color_array_destroy(godot_pool_color_array *p_pba) { - ((PoolVector<Color> *)p_pba)->~PoolVector(); +void GDAPI godot_pool_color_array_destroy(godot_pool_color_array *p_self) { + ((PoolVector<Color> *)p_self)->~PoolVector(); } #ifdef __cplusplus diff --git a/modules/gdnative/godot/godot_pool_arrays.h b/modules/gdnative/godot/godot_pool_arrays.h index 8b0d0137fd..a794d03f01 100644 --- a/modules/gdnative/godot/godot_pool_arrays.h +++ b/modules/gdnative/godot/godot_pool_arrays.h @@ -101,192 +101,192 @@ typedef struct godot_pool_color_array { // byte -void GDAPI godot_pool_byte_array_new(godot_pool_byte_array *p_pba); -void GDAPI godot_pool_byte_array_new_copy(godot_pool_byte_array *p_dest, const godot_pool_byte_array *p_src); -void GDAPI godot_pool_byte_array_new_with_array(godot_pool_byte_array *p_pba, const godot_array *p_a); +void GDAPI godot_pool_byte_array_new(godot_pool_byte_array *r_dest); +void GDAPI godot_pool_byte_array_new_copy(godot_pool_byte_array *r_dest, const godot_pool_byte_array *p_src); +void GDAPI godot_pool_byte_array_new_with_array(godot_pool_byte_array *r_dest, const godot_array *p_a); -void GDAPI godot_pool_byte_array_append(godot_pool_byte_array *p_pba, const uint8_t p_data); +void GDAPI godot_pool_byte_array_append(godot_pool_byte_array *p_self, const uint8_t p_data); -void GDAPI godot_pool_byte_array_append_array(godot_pool_byte_array *p_pba, const godot_pool_byte_array *p_array); +void GDAPI godot_pool_byte_array_append_array(godot_pool_byte_array *p_self, const godot_pool_byte_array *p_array); -int GDAPI godot_pool_byte_array_insert(godot_pool_byte_array *p_pba, const godot_int p_idx, const uint8_t p_data); +godot_error GDAPI godot_pool_byte_array_insert(godot_pool_byte_array *p_self, const godot_int p_idx, const uint8_t p_data); -void GDAPI godot_pool_byte_array_invert(godot_pool_byte_array *p_pba); +void GDAPI godot_pool_byte_array_invert(godot_pool_byte_array *p_self); -void GDAPI godot_pool_byte_array_push_back(godot_pool_byte_array *p_pba, const uint8_t p_data); +void GDAPI godot_pool_byte_array_push_back(godot_pool_byte_array *p_self, const uint8_t p_data); -void GDAPI godot_pool_byte_array_remove(godot_pool_byte_array *p_pba, const godot_int p_idx); +void GDAPI godot_pool_byte_array_remove(godot_pool_byte_array *p_self, const godot_int p_idx); -void GDAPI godot_pool_byte_array_resize(godot_pool_byte_array *p_pba, const godot_int p_size); +void GDAPI godot_pool_byte_array_resize(godot_pool_byte_array *p_self, const godot_int p_size); -void GDAPI godot_pool_byte_array_set(godot_pool_byte_array *p_pba, const godot_int p_idx, const uint8_t p_data); -uint8_t GDAPI godot_pool_byte_array_get(const godot_pool_byte_array *p_pba, const godot_int p_idx); +void GDAPI godot_pool_byte_array_set(godot_pool_byte_array *p_self, const godot_int p_idx, const uint8_t p_data); +uint8_t GDAPI godot_pool_byte_array_get(const godot_pool_byte_array *p_self, const godot_int p_idx); -godot_int GDAPI godot_pool_byte_array_size(const godot_pool_byte_array *p_pba); +godot_int GDAPI godot_pool_byte_array_size(const godot_pool_byte_array *p_self); -void GDAPI godot_pool_byte_array_destroy(godot_pool_byte_array *p_pba); +void GDAPI godot_pool_byte_array_destroy(godot_pool_byte_array *p_self); // int -void GDAPI godot_pool_int_array_new(godot_pool_int_array *p_pia); -void GDAPI godot_pool_int_array_new_copy(godot_pool_int_array *p_dest, const godot_pool_int_array *p_src); -void GDAPI godot_pool_int_array_new_with_array(godot_pool_int_array *p_pia, const godot_array *p_a); +void GDAPI godot_pool_int_array_new(godot_pool_int_array *r_dest); +void GDAPI godot_pool_int_array_new_copy(godot_pool_int_array *r_dest, const godot_pool_int_array *p_src); +void GDAPI godot_pool_int_array_new_with_array(godot_pool_int_array *r_dest, const godot_array *p_a); -void GDAPI godot_pool_int_array_append(godot_pool_int_array *p_pia, const godot_int p_data); +void GDAPI godot_pool_int_array_append(godot_pool_int_array *p_self, const godot_int p_data); -void GDAPI godot_pool_int_array_append_array(godot_pool_int_array *p_pia, const godot_pool_int_array *p_array); +void GDAPI godot_pool_int_array_append_array(godot_pool_int_array *p_self, const godot_pool_int_array *p_array); -int GDAPI godot_pool_int_array_insert(godot_pool_int_array *p_pia, const godot_int p_idx, const godot_int p_data); +godot_error GDAPI godot_pool_int_array_insert(godot_pool_int_array *p_self, const godot_int p_idx, const godot_int p_data); -void GDAPI godot_pool_int_array_invert(godot_pool_int_array *p_pia); +void GDAPI godot_pool_int_array_invert(godot_pool_int_array *p_self); -void GDAPI godot_pool_int_array_push_back(godot_pool_int_array *p_pia, const godot_int p_data); +void GDAPI godot_pool_int_array_push_back(godot_pool_int_array *p_self, const godot_int p_data); -void GDAPI godot_pool_int_array_remove(godot_pool_int_array *p_pia, const godot_int p_idx); +void GDAPI godot_pool_int_array_remove(godot_pool_int_array *p_self, const godot_int p_idx); -void GDAPI godot_pool_int_array_resize(godot_pool_int_array *p_pia, const godot_int p_size); +void GDAPI godot_pool_int_array_resize(godot_pool_int_array *p_self, const godot_int p_size); -void GDAPI godot_pool_int_array_set(godot_pool_int_array *p_pia, const godot_int p_idx, const godot_int p_data); -godot_int GDAPI godot_pool_int_array_get(const godot_pool_int_array *p_pia, const godot_int p_idx); +void GDAPI godot_pool_int_array_set(godot_pool_int_array *p_self, const godot_int p_idx, const godot_int p_data); +godot_int GDAPI godot_pool_int_array_get(const godot_pool_int_array *p_self, const godot_int p_idx); -godot_int GDAPI godot_pool_int_array_size(const godot_pool_int_array *p_pia); +godot_int GDAPI godot_pool_int_array_size(const godot_pool_int_array *p_self); -void GDAPI godot_pool_int_array_destroy(godot_pool_int_array *p_pia); +void GDAPI godot_pool_int_array_destroy(godot_pool_int_array *p_self); // real -void GDAPI godot_pool_real_array_new(godot_pool_real_array *p_pra); -void GDAPI godot_pool_real_array_new_copy(godot_pool_real_array *p_dest, const godot_pool_real_array *p_src); -void GDAPI godot_pool_real_array_new_with_array(godot_pool_real_array *p_pra, const godot_array *p_a); +void GDAPI godot_pool_real_array_new(godot_pool_real_array *r_dest); +void GDAPI godot_pool_real_array_new_copy(godot_pool_real_array *r_dest, const godot_pool_real_array *p_src); +void GDAPI godot_pool_real_array_new_with_array(godot_pool_real_array *r_dest, const godot_array *p_a); -void GDAPI godot_pool_real_array_append(godot_pool_real_array *p_pra, const godot_real p_data); +void GDAPI godot_pool_real_array_append(godot_pool_real_array *p_self, const godot_real p_data); -void GDAPI godot_pool_real_array_append_array(godot_pool_real_array *p_pra, const godot_pool_real_array *p_array); +void GDAPI godot_pool_real_array_append_array(godot_pool_real_array *p_self, const godot_pool_real_array *p_array); -int GDAPI godot_pool_real_array_insert(godot_pool_real_array *p_pra, const godot_int p_idx, const godot_real p_data); +godot_error GDAPI godot_pool_real_array_insert(godot_pool_real_array *p_self, const godot_int p_idx, const godot_real p_data); -void GDAPI godot_pool_real_array_invert(godot_pool_real_array *p_pra); +void GDAPI godot_pool_real_array_invert(godot_pool_real_array *p_self); -void GDAPI godot_pool_real_array_push_back(godot_pool_real_array *p_pra, const godot_real p_data); +void GDAPI godot_pool_real_array_push_back(godot_pool_real_array *p_self, const godot_real p_data); -void GDAPI godot_pool_real_array_remove(godot_pool_real_array *p_pra, const godot_int p_idx); +void GDAPI godot_pool_real_array_remove(godot_pool_real_array *p_self, const godot_int p_idx); -void GDAPI godot_pool_real_array_resize(godot_pool_real_array *p_pra, const godot_int p_size); +void GDAPI godot_pool_real_array_resize(godot_pool_real_array *p_self, const godot_int p_size); -void GDAPI godot_pool_real_array_set(godot_pool_real_array *p_pra, const godot_int p_idx, const godot_real p_data); -godot_real GDAPI godot_pool_real_array_get(const godot_pool_real_array *p_pra, const godot_int p_idx); +void GDAPI godot_pool_real_array_set(godot_pool_real_array *p_self, const godot_int p_idx, const godot_real p_data); +godot_real GDAPI godot_pool_real_array_get(const godot_pool_real_array *p_self, const godot_int p_idx); -godot_int GDAPI godot_pool_real_array_size(const godot_pool_real_array *p_pra); +godot_int GDAPI godot_pool_real_array_size(const godot_pool_real_array *p_self); -void GDAPI godot_pool_real_array_destroy(godot_pool_real_array *p_pra); +void GDAPI godot_pool_real_array_destroy(godot_pool_real_array *p_self); // string -void GDAPI godot_pool_string_array_new(godot_pool_string_array *p_psa); -void GDAPI godot_pool_string_array_new_copy(godot_pool_string_array *p_dest, const godot_pool_string_array *p_src); -void GDAPI godot_pool_string_array_new_with_array(godot_pool_string_array *p_psa, const godot_array *p_a); +void GDAPI godot_pool_string_array_new(godot_pool_string_array *r_dest); +void GDAPI godot_pool_string_array_new_copy(godot_pool_string_array *r_dest, const godot_pool_string_array *p_src); +void GDAPI godot_pool_string_array_new_with_array(godot_pool_string_array *r_dest, const godot_array *p_a); -void GDAPI godot_pool_string_array_append(godot_pool_string_array *p_psa, const godot_string *p_data); +void GDAPI godot_pool_string_array_append(godot_pool_string_array *p_self, const godot_string *p_data); -void GDAPI godot_pool_string_array_append_array(godot_pool_string_array *p_psa, const godot_pool_string_array *p_array); +void GDAPI godot_pool_string_array_append_array(godot_pool_string_array *p_self, const godot_pool_string_array *p_array); -int GDAPI godot_pool_string_array_insert(godot_pool_string_array *p_psa, const godot_int p_idx, const godot_string *p_data); +godot_error GDAPI godot_pool_string_array_insert(godot_pool_string_array *p_self, const godot_int p_idx, const godot_string *p_data); -void GDAPI godot_pool_string_array_invert(godot_pool_string_array *p_psa); +void GDAPI godot_pool_string_array_invert(godot_pool_string_array *p_self); -void GDAPI godot_pool_string_array_push_back(godot_pool_string_array *p_psa, const godot_string *p_data); +void GDAPI godot_pool_string_array_push_back(godot_pool_string_array *p_self, const godot_string *p_data); -void GDAPI godot_pool_string_array_remove(godot_pool_string_array *p_psa, const godot_int p_idx); +void GDAPI godot_pool_string_array_remove(godot_pool_string_array *p_self, const godot_int p_idx); -void GDAPI godot_pool_string_array_resize(godot_pool_string_array *p_psa, const godot_int p_size); +void GDAPI godot_pool_string_array_resize(godot_pool_string_array *p_self, const godot_int p_size); -void GDAPI godot_pool_string_array_set(godot_pool_string_array *p_psa, const godot_int p_idx, const godot_string *p_data); -godot_string GDAPI godot_pool_string_array_get(const godot_pool_string_array *p_psa, const godot_int p_idx); +void GDAPI godot_pool_string_array_set(godot_pool_string_array *p_self, const godot_int p_idx, const godot_string *p_data); +godot_string GDAPI godot_pool_string_array_get(const godot_pool_string_array *p_self, const godot_int p_idx); -godot_int GDAPI godot_pool_string_array_size(const godot_pool_string_array *p_psa); +godot_int GDAPI godot_pool_string_array_size(const godot_pool_string_array *p_self); -void GDAPI godot_pool_string_array_destroy(godot_pool_string_array *p_psa); +void GDAPI godot_pool_string_array_destroy(godot_pool_string_array *p_self); // vector2 -void GDAPI godot_pool_vector2_array_new(godot_pool_vector2_array *p_pv2a); -void GDAPI godot_pool_vector2_array_new_copy(godot_pool_vector2_array *p_dest, const godot_pool_vector2_array *p_src); -void GDAPI godot_pool_vector2_array_new_with_array(godot_pool_vector2_array *p_pv2a, const godot_array *p_a); +void GDAPI godot_pool_vector2_array_new(godot_pool_vector2_array *r_dest); +void GDAPI godot_pool_vector2_array_new_copy(godot_pool_vector2_array *r_dest, const godot_pool_vector2_array *p_src); +void GDAPI godot_pool_vector2_array_new_with_array(godot_pool_vector2_array *r_dest, const godot_array *p_a); -void GDAPI godot_pool_vector2_array_append(godot_pool_vector2_array *p_pv2a, const godot_vector2 *p_data); +void GDAPI godot_pool_vector2_array_append(godot_pool_vector2_array *p_self, const godot_vector2 *p_data); -void GDAPI godot_pool_vector2_array_append_array(godot_pool_vector2_array *p_pv2a, const godot_pool_vector2_array *p_array); +void GDAPI godot_pool_vector2_array_append_array(godot_pool_vector2_array *p_self, const godot_pool_vector2_array *p_array); -int GDAPI godot_pool_vector2_array_insert(godot_pool_vector2_array *p_pv2a, const godot_int p_idx, const godot_vector2 *p_data); +godot_error GDAPI godot_pool_vector2_array_insert(godot_pool_vector2_array *p_self, const godot_int p_idx, const godot_vector2 *p_data); -void GDAPI godot_pool_vector2_array_invert(godot_pool_vector2_array *p_pv2a); +void GDAPI godot_pool_vector2_array_invert(godot_pool_vector2_array *p_self); -void GDAPI godot_pool_vector2_array_push_back(godot_pool_vector2_array *p_pv2a, const godot_vector2 *p_data); +void GDAPI godot_pool_vector2_array_push_back(godot_pool_vector2_array *p_self, const godot_vector2 *p_data); -void GDAPI godot_pool_vector2_array_remove(godot_pool_vector2_array *p_pv2a, const godot_int p_idx); +void GDAPI godot_pool_vector2_array_remove(godot_pool_vector2_array *p_self, const godot_int p_idx); -void GDAPI godot_pool_vector2_array_resize(godot_pool_vector2_array *p_pv2a, const godot_int p_size); +void GDAPI godot_pool_vector2_array_resize(godot_pool_vector2_array *p_self, const godot_int p_size); -void GDAPI godot_pool_vector2_array_set(godot_pool_vector2_array *p_pv2a, const godot_int p_idx, const godot_vector2 *p_data); -godot_vector2 GDAPI godot_pool_vector2_array_get(const godot_pool_vector2_array *p_pv2a, const godot_int p_idx); +void GDAPI godot_pool_vector2_array_set(godot_pool_vector2_array *p_self, const godot_int p_idx, const godot_vector2 *p_data); +godot_vector2 GDAPI godot_pool_vector2_array_get(const godot_pool_vector2_array *p_self, const godot_int p_idx); -godot_int GDAPI godot_pool_vector2_array_size(const godot_pool_vector2_array *p_pv2a); +godot_int GDAPI godot_pool_vector2_array_size(const godot_pool_vector2_array *p_self); -void GDAPI godot_pool_vector2_array_destroy(godot_pool_vector2_array *p_pv2a); +void GDAPI godot_pool_vector2_array_destroy(godot_pool_vector2_array *p_self); // vector3 -void GDAPI godot_pool_vector3_array_new(godot_pool_vector3_array *p_pv3a); -void GDAPI godot_pool_vector3_array_new_copy(godot_pool_vector3_array *p_dest, const godot_pool_vector3_array *p_src); -void GDAPI godot_pool_vector3_array_new_with_array(godot_pool_vector3_array *p_pv3a, const godot_array *p_a); +void GDAPI godot_pool_vector3_array_new(godot_pool_vector3_array *r_dest); +void GDAPI godot_pool_vector3_array_new_copy(godot_pool_vector3_array *r_dest, const godot_pool_vector3_array *p_src); +void GDAPI godot_pool_vector3_array_new_with_array(godot_pool_vector3_array *r_dest, const godot_array *p_a); -void GDAPI godot_pool_vector3_array_append(godot_pool_vector3_array *p_pv3a, const godot_vector3 *p_data); +void GDAPI godot_pool_vector3_array_append(godot_pool_vector3_array *p_self, const godot_vector3 *p_data); -void GDAPI godot_pool_vector3_array_append_array(godot_pool_vector3_array *p_pv3a, const godot_pool_vector3_array *p_array); +void GDAPI godot_pool_vector3_array_append_array(godot_pool_vector3_array *p_self, const godot_pool_vector3_array *p_array); -int GDAPI godot_pool_vector3_array_insert(godot_pool_vector3_array *p_pv3a, const godot_int p_idx, const godot_vector3 *p_data); +godot_error GDAPI godot_pool_vector3_array_insert(godot_pool_vector3_array *p_self, const godot_int p_idx, const godot_vector3 *p_data); -void GDAPI godot_pool_vector3_array_invert(godot_pool_vector3_array *p_pv3a); +void GDAPI godot_pool_vector3_array_invert(godot_pool_vector3_array *p_self); -void GDAPI godot_pool_vector3_array_push_back(godot_pool_vector3_array *p_pv3a, const godot_vector3 *p_data); +void GDAPI godot_pool_vector3_array_push_back(godot_pool_vector3_array *p_self, const godot_vector3 *p_data); -void GDAPI godot_pool_vector3_array_remove(godot_pool_vector3_array *p_pv3a, const godot_int p_idx); +void GDAPI godot_pool_vector3_array_remove(godot_pool_vector3_array *p_self, const godot_int p_idx); -void GDAPI godot_pool_vector3_array_resize(godot_pool_vector3_array *p_pv3a, const godot_int p_size); +void GDAPI godot_pool_vector3_array_resize(godot_pool_vector3_array *p_self, const godot_int p_size); -void GDAPI godot_pool_vector3_array_set(godot_pool_vector3_array *p_pv3a, const godot_int p_idx, const godot_vector3 *p_data); -godot_vector3 GDAPI godot_pool_vector3_array_get(const godot_pool_vector3_array *p_pv3a, const godot_int p_idx); +void GDAPI godot_pool_vector3_array_set(godot_pool_vector3_array *p_self, const godot_int p_idx, const godot_vector3 *p_data); +godot_vector3 GDAPI godot_pool_vector3_array_get(const godot_pool_vector3_array *p_self, const godot_int p_idx); -godot_int GDAPI godot_pool_vector3_array_size(const godot_pool_vector3_array *p_pv3a); +godot_int GDAPI godot_pool_vector3_array_size(const godot_pool_vector3_array *p_self); -void GDAPI godot_pool_vector3_array_destroy(godot_pool_vector3_array *p_pv3a); +void GDAPI godot_pool_vector3_array_destroy(godot_pool_vector3_array *p_self); // color -void GDAPI godot_pool_color_array_new(godot_pool_color_array *p_pca); -void GDAPI godot_pool_color_array_new_copy(godot_pool_color_array *p_dest, const godot_pool_color_array *p_src); -void GDAPI godot_pool_color_array_new_with_array(godot_pool_color_array *p_pca, const godot_array *p_a); +void GDAPI godot_pool_color_array_new(godot_pool_color_array *r_dest); +void GDAPI godot_pool_color_array_new_copy(godot_pool_color_array *r_dest, const godot_pool_color_array *p_src); +void GDAPI godot_pool_color_array_new_with_array(godot_pool_color_array *r_dest, const godot_array *p_a); -void GDAPI godot_pool_color_array_append(godot_pool_color_array *p_pca, const godot_color *p_data); +void GDAPI godot_pool_color_array_append(godot_pool_color_array *p_self, const godot_color *p_data); -void GDAPI godot_pool_color_array_append_array(godot_pool_color_array *p_pca, const godot_pool_color_array *p_array); +void GDAPI godot_pool_color_array_append_array(godot_pool_color_array *p_self, const godot_pool_color_array *p_array); -int GDAPI godot_pool_color_array_insert(godot_pool_color_array *p_pca, const godot_int p_idx, const godot_color *p_data); +godot_error GDAPI godot_pool_color_array_insert(godot_pool_color_array *p_self, const godot_int p_idx, const godot_color *p_data); -void GDAPI godot_pool_color_array_invert(godot_pool_color_array *p_pca); +void GDAPI godot_pool_color_array_invert(godot_pool_color_array *p_self); -void GDAPI godot_pool_color_array_push_back(godot_pool_color_array *p_pca, const godot_color *p_data); +void GDAPI godot_pool_color_array_push_back(godot_pool_color_array *p_self, const godot_color *p_data); -void GDAPI godot_pool_color_array_remove(godot_pool_color_array *p_pca, const godot_int p_idx); +void GDAPI godot_pool_color_array_remove(godot_pool_color_array *p_self, const godot_int p_idx); -void GDAPI godot_pool_color_array_resize(godot_pool_color_array *p_pca, const godot_int p_size); +void GDAPI godot_pool_color_array_resize(godot_pool_color_array *p_self, const godot_int p_size); -void GDAPI godot_pool_color_array_set(godot_pool_color_array *p_pca, const godot_int p_idx, const godot_color *p_data); -godot_color GDAPI godot_pool_color_array_get(const godot_pool_color_array *p_pca, const godot_int p_idx); +void GDAPI godot_pool_color_array_set(godot_pool_color_array *p_self, const godot_int p_idx, const godot_color *p_data); +godot_color GDAPI godot_pool_color_array_get(const godot_pool_color_array *p_self, const godot_int p_idx); -godot_int GDAPI godot_pool_color_array_size(const godot_pool_color_array *p_pca); +godot_int GDAPI godot_pool_color_array_size(const godot_pool_color_array *p_self); -void GDAPI godot_pool_color_array_destroy(godot_pool_color_array *p_pca); +void GDAPI godot_pool_color_array_destroy(godot_pool_color_array *p_self); #ifdef __cplusplus } diff --git a/modules/gdnative/godot/godot_quat.cpp b/modules/gdnative/godot/godot_quat.cpp index 4d38c4987c..7235e4fcec 100644 --- a/modules/gdnative/godot/godot_quat.cpp +++ b/modules/gdnative/godot/godot_quat.cpp @@ -50,6 +50,46 @@ void GDAPI godot_quat_new_with_axis_angle(godot_quat *r_dest, const godot_vector *dest = Quat(*axis, p_angle); } +godot_real GDAPI godot_quat_get_x(const godot_quat *p_self) { + const Quat *self = (const Quat *)p_self; + return self->x; +} + +void GDAPI godot_quat_set_x(godot_quat *p_self, const godot_real val) { + Quat *self = (Quat *)p_self; + self->x = val; +} + +godot_real GDAPI godot_quat_get_y(const godot_quat *p_self) { + const Quat *self = (const Quat *)p_self; + return self->y; +} + +void GDAPI godot_quat_set_y(godot_quat *p_self, const godot_real val) { + Quat *self = (Quat *)p_self; + self->y = val; +} + +godot_real GDAPI godot_quat_get_z(const godot_quat *p_self) { + const Quat *self = (const Quat *)p_self; + return self->z; +} + +void GDAPI godot_quat_set_z(godot_quat *p_self, const godot_real val) { + Quat *self = (Quat *)p_self; + self->z = val; +} + +godot_real GDAPI godot_quat_get_w(const godot_quat *p_self) { + const Quat *self = (const Quat *)p_self; + return self->w; +} + +void GDAPI godot_quat_set_w(godot_quat *p_self, const godot_real val) { + Quat *self = (Quat *)p_self; + self->w = val; +} + godot_string GDAPI godot_quat_as_string(const godot_quat *p_self) { godot_string ret; const Quat *self = (const Quat *)p_self; diff --git a/modules/gdnative/godot/godot_quat.h b/modules/gdnative/godot/godot_quat.h index 6bdc33accf..2289b6cbab 100644 --- a/modules/gdnative/godot/godot_quat.h +++ b/modules/gdnative/godot/godot_quat.h @@ -49,6 +49,18 @@ typedef struct godot_quat { void GDAPI godot_quat_new(godot_quat *r_dest, const godot_real p_x, const godot_real p_y, const godot_real p_z, const godot_real p_w); void GDAPI godot_quat_new_with_axis_angle(godot_quat *r_dest, const godot_vector3 *p_axis, const godot_real p_angle); +godot_real GDAPI godot_quat_get_x(const godot_quat *p_self); +void GDAPI godot_quat_set_x(godot_quat *p_self, const godot_real val); + +godot_real GDAPI godot_quat_get_y(const godot_quat *p_self); +void GDAPI godot_quat_set_y(godot_quat *p_self, const godot_real val); + +godot_real GDAPI godot_quat_get_z(const godot_quat *p_self); +void GDAPI godot_quat_set_z(godot_quat *p_self, const godot_real val); + +godot_real GDAPI godot_quat_get_w(const godot_quat *p_self); +void GDAPI godot_quat_set_w(godot_quat *p_self, const godot_real val); + godot_string GDAPI godot_quat_as_string(const godot_quat *p_self); godot_real GDAPI godot_quat_length(const godot_quat *p_self); diff --git a/modules/gdnative/godot/godot_rect2.cpp b/modules/gdnative/godot/godot_rect2.cpp index eea95ca6fe..0e456ea3ba 100644 --- a/modules/gdnative/godot/godot_rect2.cpp +++ b/modules/gdnative/godot/godot_rect2.cpp @@ -38,11 +38,11 @@ extern "C" { void _rect2_api_anchor() {} -void GDAPI godot_rect2_new_with_pos_and_size(godot_rect2 *r_dest, const godot_vector2 *p_pos, const godot_vector2 *p_size) { - const Vector2 *pos = (const Vector2 *)p_pos; +void GDAPI godot_rect2_new_with_position_and_size(godot_rect2 *r_dest, const godot_vector2 *p_pos, const godot_vector2 *p_size) { + const Vector2 *position = (const Vector2 *)p_pos; const Vector2 *size = (const Vector2 *)p_size; Rect2 *dest = (Rect2 *)r_dest; - *dest = Rect2(*pos, *size); + *dest = Rect2(*position, *size); } void GDAPI godot_rect2_new(godot_rect2 *r_dest, const godot_real p_x, const godot_real p_y, const godot_real p_width, const godot_real p_height) { @@ -124,11 +124,11 @@ godot_bool GDAPI godot_rect2_operator_equal(const godot_rect2 *p_self, const god return *self == *b; } -godot_vector2 GDAPI godot_rect2_get_pos(const godot_rect2 *p_self) { +godot_vector2 GDAPI godot_rect2_get_position(const godot_rect2 *p_self) { godot_vector2 dest; Vector2 *d = (Vector2 *)&dest; const Rect2 *self = (const Rect2 *)p_self; - *d = self->get_pos(); + *d = self->get_position(); return dest; } @@ -140,10 +140,10 @@ godot_vector2 GDAPI godot_rect2_get_size(const godot_rect2 *p_self) { return dest; } -void GDAPI godot_rect2_set_pos(godot_rect2 *p_self, const godot_vector2 *p_pos) { +void GDAPI godot_rect2_set_position(godot_rect2 *p_self, const godot_vector2 *p_pos) { Rect2 *self = (Rect2 *)p_self; - const Vector2 *pos = (const Vector2 *)p_pos; - self->set_pos(*pos); + const Vector2 *position = (const Vector2 *)p_pos; + self->set_position(*position); } void GDAPI godot_rect2_set_size(godot_rect2 *p_self, const godot_vector2 *p_size) { diff --git a/modules/gdnative/godot/godot_rect2.h b/modules/gdnative/godot/godot_rect2.h index 9743321a3b..488a1204f7 100644 --- a/modules/gdnative/godot/godot_rect2.h +++ b/modules/gdnative/godot/godot_rect2.h @@ -46,7 +46,7 @@ typedef struct godot_rect2 { #include "../godot.h" #include "godot_vector2.h" -void GDAPI godot_rect2_new_with_pos_and_size(godot_rect2 *r_dest, const godot_vector2 *p_pos, const godot_vector2 *p_size); +void GDAPI godot_rect2_new_with_position_and_size(godot_rect2 *r_dest, const godot_vector2 *p_pos, const godot_vector2 *p_size); void GDAPI godot_rect2_new(godot_rect2 *r_dest, const godot_real p_x, const godot_real p_y, const godot_real p_width, const godot_real p_height); godot_string GDAPI godot_rect2_as_string(const godot_rect2 *p_self); @@ -71,11 +71,11 @@ godot_rect2 GDAPI godot_rect2_expand(const godot_rect2 *p_self, const godot_vect godot_bool GDAPI godot_rect2_operator_equal(const godot_rect2 *p_self, const godot_rect2 *p_b); -godot_vector2 GDAPI godot_rect2_get_pos(const godot_rect2 *p_self); +godot_vector2 GDAPI godot_rect2_get_position(const godot_rect2 *p_self); godot_vector2 GDAPI godot_rect2_get_size(const godot_rect2 *p_self); -void GDAPI godot_rect2_set_pos(godot_rect2 *p_self, const godot_vector2 *p_pos); +void GDAPI godot_rect2_set_position(godot_rect2 *p_self, const godot_vector2 *p_pos); void GDAPI godot_rect2_set_size(godot_rect2 *p_self, const godot_vector2 *p_size); diff --git a/modules/gdnative/godot/godot_rect3.cpp b/modules/gdnative/godot/godot_rect3.cpp index c4f8a853c2..e524fa8463 100644 --- a/modules/gdnative/godot/godot_rect3.cpp +++ b/modules/gdnative/godot/godot_rect3.cpp @@ -45,6 +45,34 @@ void GDAPI godot_rect3_new(godot_rect3 *r_dest, const godot_vector3 *p_pos, cons *dest = Rect3(*pos, *size); } +godot_vector3 GDAPI godot_rect3_get_position(const godot_rect3 *p_self) { + godot_vector3 raw_ret; + const Rect3 *self = (const Rect3 *)p_self; + Vector3 *ret = (Vector3 *)&raw_ret; + *ret = self->position; + return raw_ret; +} + +void GDAPI godot_rect3_set_position(const godot_rect3 *p_self, const godot_vector3 *p_v) { + Rect3 *self = (Rect3 *)p_self; + const Vector3 *v = (const Vector3 *)p_v; + self->position = *v; +} + +godot_vector3 GDAPI godot_rect3_get_size(const godot_rect3 *p_self) { + godot_vector3 raw_ret; + const Rect3 *self = (const Rect3 *)p_self; + Vector3 *ret = (Vector3 *)&raw_ret; + *ret = self->size; + return raw_ret; +} + +void GDAPI godot_rect3_set_size(const godot_rect3 *p_self, const godot_vector3 *p_v) { + Rect3 *self = (Rect3 *)p_self; + const Vector3 *v = (const Vector3 *)p_v; + self->size = *v; +} + godot_string GDAPI godot_rect3_as_string(const godot_rect3 *p_self) { godot_string ret; const Rect3 *self = (const Rect3 *)p_self; diff --git a/modules/gdnative/godot/godot_rect3.h b/modules/gdnative/godot/godot_rect3.h index 95969ab20e..9e9a49ac27 100644 --- a/modules/gdnative/godot/godot_rect3.h +++ b/modules/gdnative/godot/godot_rect3.h @@ -49,6 +49,12 @@ typedef struct godot_rect3 { void GDAPI godot_rect3_new(godot_rect3 *r_dest, const godot_vector3 *p_pos, const godot_vector3 *p_size); +godot_vector3 GDAPI godot_rect3_get_position(const godot_rect3 *p_self); +void GDAPI godot_rect3_set_position(const godot_rect3 *p_self, const godot_vector3 *p_v); + +godot_vector3 GDAPI godot_rect3_get_size(const godot_rect3 *p_self); +void GDAPI godot_rect3_set_size(const godot_rect3 *p_self, const godot_vector3 *p_v); + godot_string GDAPI godot_rect3_as_string(const godot_rect3 *p_self); godot_real GDAPI godot_rect3_get_area(const godot_rect3 *p_self); diff --git a/modules/gdnative/godot/godot_string.cpp b/modules/gdnative/godot/godot_string.cpp index 59d20c6d23..679011e715 100644 --- a/modules/gdnative/godot/godot_string.cpp +++ b/modules/gdnative/godot/godot_string.cpp @@ -41,81 +41,75 @@ extern "C" { void _string_api_anchor() { } -void GDAPI godot_string_new(godot_string *p_str) { - String *p = (String *)p_str; - memnew_placement(p, String); - // *p = String(); // useless here +void GDAPI godot_string_new(godot_string *r_dest) { + String *dest = (String *)r_dest; + memnew_placement(dest, String); } -void GDAPI godot_string_new_data(godot_string *p_str, const char *p_contents, const int p_size) { - String *p = (String *)p_str; - memnew_placement(p, String); - *p = String::utf8(p_contents, p_size); +void GDAPI godot_string_new_copy(godot_string *r_dest, const godot_string *p_src) { + String *dest = (String *)r_dest; + const String *src = (const String *)p_src; + memnew_placement(dest, String(*src)); } -void GDAPI godot_string_new_unicode_data(godot_string *p_str, const wchar_t *p_contents, const int p_size) { - String *p = (String *)p_str; - memnew_placement(p, String); - *p = String(p_contents, p_size); +void GDAPI godot_string_new_data(godot_string *r_dest, const char *p_contents, const int p_size) { + String *dest = (String *)r_dest; + memnew_placement(dest, String(String::utf8(p_contents, p_size))); } -void GDAPI godot_string_get_data(const godot_string *p_str, char *p_dest, int *p_size) { - String *p = (String *)p_str; +void GDAPI godot_string_new_unicode_data(godot_string *r_dest, const wchar_t *p_contents, const int p_size) { + String *dest = (String *)r_dest; + memnew_placement(dest, String(p_contents, p_size)); +} + +void GDAPI godot_string_get_data(const godot_string *p_self, char *r_dest, int *p_size) { + String *self = (String *)p_self; if (p_size != NULL) { - *p_size = p->utf8().length(); + *p_size = self->utf8().length(); } - if (p_dest != NULL) { - memcpy(p_dest, p->utf8().get_data(), *p_size); + if (r_dest != NULL) { + memcpy(r_dest, self->utf8().get_data(), *p_size); } } -void GDAPI godot_string_copy_string(const godot_string *p_dest, const godot_string *p_src) { - String *dest = (String *)p_dest; - String *src = (String *)p_src; - - *dest = *src; -} - -wchar_t GDAPI *godot_string_operator_index(godot_string *p_str, const godot_int p_idx) { - String *s = (String *)p_str; - return &(s->operator[](p_idx)); +wchar_t GDAPI *godot_string_operator_index(godot_string *p_self, const godot_int p_idx) { + String *self = (String *)p_self; + return &(self->operator[](p_idx)); } -const char GDAPI *godot_string_c_str(const godot_string *p_str) { - const String *s = (const String *)p_str; - return s->utf8().get_data(); +const char GDAPI *godot_string_c_str(const godot_string *p_self) { + const String *self = (const String *)p_self; + return self->utf8().get_data(); } -const wchar_t GDAPI *godot_string_unicode_str(const godot_string *p_str) { - const String *s = (const String *)p_str; - return s->c_str(); +const wchar_t GDAPI *godot_string_unicode_str(const godot_string *p_self) { + const String *self = (const String *)p_self; + return self->c_str(); } -godot_bool GDAPI godot_string_operator_equal(const godot_string *p_a, const godot_string *p_b) { - String *a = (String *)p_a; - String *b = (String *)p_b; - return *a == *b; +godot_bool GDAPI godot_string_operator_equal(const godot_string *p_self, const godot_string *p_b) { + const String *self = (const String *)p_self; + const String *b = (const String *)p_b; + return *self == *b; } -godot_bool GDAPI godot_string_operator_less(const godot_string *p_a, const godot_string *p_b) { - String *a = (String *)p_a; - String *b = (String *)p_b; - return *a < *b; +godot_bool GDAPI godot_string_operator_less(const godot_string *p_self, const godot_string *p_b) { + const String *self = (const String *)p_self; + const String *b = (const String *)p_b; + return *self < *b; } -void GDAPI godot_string_operator_plus(godot_string *p_dest, const godot_string *p_a, const godot_string *p_b) { - String *dest = (String *)p_dest; - const String *a = (String *)p_a; - const String *b = (String *)p_b; - - String tmp = *a + *b; - godot_string_new(p_dest); - *dest = tmp; +godot_string GDAPI godot_string_operator_plus(const godot_string *p_self, const godot_string *p_b) { + godot_string ret; + const String *self = (const String *)p_self; + const String *b = (const String *)p_b; + memnew_placement(&ret, String(*self + *b)); + return ret; } -void GDAPI godot_string_destroy(godot_string *p_str) { - String *p = (String *)p_str; - p->~String(); +void GDAPI godot_string_destroy(godot_string *p_self) { + String *self = (String *)p_self; + self->~String(); } #ifdef __cplusplus diff --git a/modules/gdnative/godot/godot_string.h b/modules/gdnative/godot/godot_string.h index e0ba298a9c..df848abb76 100644 --- a/modules/gdnative/godot/godot_string.h +++ b/modules/gdnative/godot/godot_string.h @@ -45,27 +45,26 @@ typedef struct godot_string { #include "../godot.h" -void GDAPI godot_string_new(godot_string *p_str); -void GDAPI godot_string_new_data(godot_string *p_str, const char *p_contents, const int p_size); -void GDAPI godot_string_new_unicode_data(godot_string *p_str, const wchar_t *p_contents, const int p_size); +void GDAPI godot_string_new(godot_string *r_dest); +void GDAPI godot_string_new_copy(godot_string *r_dest, const godot_string *p_src); +void GDAPI godot_string_new_data(godot_string *r_dest, const char *p_contents, const int p_size); +void GDAPI godot_string_new_unicode_data(godot_string *r_dest, const wchar_t *p_contents, const int p_size); -void GDAPI godot_string_get_data(const godot_string *p_str, char *p_dest, int *p_size); +void GDAPI godot_string_get_data(const godot_string *p_self, char *p_dest, int *p_size); -void GDAPI godot_string_copy_string(const godot_string *p_dest, const godot_string *p_src); +wchar_t GDAPI *godot_string_operator_index(godot_string *p_self, const godot_int p_idx); +const char GDAPI *godot_string_c_str(const godot_string *p_self); +const wchar_t GDAPI *godot_string_unicode_str(const godot_string *p_self); -wchar_t GDAPI *godot_string_operator_index(godot_string *p_str, const godot_int p_idx); -const char GDAPI *godot_string_c_str(const godot_string *p_str); -const wchar_t GDAPI *godot_string_unicode_str(const godot_string *p_str); - -godot_bool GDAPI godot_string_operator_equal(const godot_string *p_a, const godot_string *p_b); -godot_bool GDAPI godot_string_operator_less(const godot_string *p_a, const godot_string *p_b); -void GDAPI godot_string_operator_plus(godot_string *p_dest, const godot_string *p_a, const godot_string *p_b); +godot_bool GDAPI godot_string_operator_equal(const godot_string *p_self, const godot_string *p_b); +godot_bool GDAPI godot_string_operator_less(const godot_string *p_self, const godot_string *p_b); +godot_string GDAPI godot_string_operator_plus(const godot_string *p_self, const godot_string *p_b); // @Incomplete // hmm, I guess exposing the whole API doesn't make much sense // since the language used in the library has its own string funcs -void GDAPI godot_string_destroy(godot_string *p_str); +void GDAPI godot_string_destroy(godot_string *p_self); #ifdef __cplusplus } diff --git a/modules/gdnative/godot/godot_transform.cpp b/modules/gdnative/godot/godot_transform.cpp index f5a012f59c..eb9e1e207b 100644 --- a/modules/gdnative/godot/godot_transform.cpp +++ b/modules/gdnative/godot/godot_transform.cpp @@ -57,6 +57,32 @@ void GDAPI godot_transform_new(godot_transform *r_dest, const godot_basis *p_bas *dest = Transform(*basis, *origin); } +godot_basis GDAPI godot_transform_get_basis(const godot_transform *p_self) { + godot_basis dest; + const Transform *self = (const Transform *)p_self; + *((Basis *)&dest) = self->basis; + return dest; +} + +void GDAPI godot_transform_set_basis(godot_transform *p_self, godot_basis *p_v) { + Transform *self = (Transform *)p_self; + const Basis *v = (const Basis *)p_v; + self->basis = *v; +} + +godot_vector3 GDAPI godot_transform_get_origin(const godot_transform *p_self) { + godot_vector3 dest; + const Transform *self = (const Transform *)p_self; + *((Vector3 *)&dest) = self->origin; + return dest; +} + +void GDAPI godot_transform_set_origin(godot_transform *p_self, godot_vector3 *p_v) { + Transform *self = (Transform *)p_self; + const Vector3 *v = (const Vector3 *)p_v; + self->origin = *v; +} + godot_string GDAPI godot_transform_as_string(const godot_transform *p_self) { godot_string ret; const Transform *self = (const Transform *)p_self; diff --git a/modules/gdnative/godot/godot_transform.h b/modules/gdnative/godot/godot_transform.h index b15efc23b8..ee87e1d33f 100644 --- a/modules/gdnative/godot/godot_transform.h +++ b/modules/gdnative/godot/godot_transform.h @@ -51,6 +51,12 @@ typedef struct godot_transform { void GDAPI godot_transform_new_with_axis_origin(godot_transform *r_dest, const godot_vector3 *p_x_axis, const godot_vector3 *p_y_axis, const godot_vector3 *p_z_axis, const godot_vector3 *p_origin); void GDAPI godot_transform_new(godot_transform *r_dest, const godot_basis *p_basis, const godot_vector3 *p_origin); +godot_basis GDAPI godot_transform_get_basis(const godot_transform *p_self); +void GDAPI godot_transform_set_basis(godot_transform *p_self, godot_basis *p_v); + +godot_vector3 GDAPI godot_transform_get_origin(const godot_transform *p_self); +void GDAPI godot_transform_set_origin(godot_transform *p_self, godot_vector3 *p_v); + godot_string GDAPI godot_transform_as_string(const godot_transform *p_self); godot_transform GDAPI godot_transform_inverse(const godot_transform *p_self); diff --git a/modules/gdnative/godot/godot_variant.cpp b/modules/gdnative/godot/godot_variant.cpp index 9381fb86d3..c9607fb21a 100644 --- a/modules/gdnative/godot/godot_variant.cpp +++ b/modules/gdnative/godot/godot_variant.cpp @@ -45,10 +45,10 @@ godot_variant_type GDAPI godot_variant_get_type(const godot_variant *p_self) { return (godot_variant_type)self->get_type(); } -void GDAPI godot_variant_copy(godot_variant *p_dest, const godot_variant *p_src) { +void GDAPI godot_variant_new_copy(godot_variant *p_dest, const godot_variant *p_src) { Variant *dest = (Variant *)p_dest; Variant *src = (Variant *)p_src; - *dest = *src; + memnew_placement(dest, Variant(*src)); } void GDAPI godot_variant_new_nil(godot_variant *r_dest) { diff --git a/modules/gdnative/godot/godot_variant.h b/modules/gdnative/godot/godot_variant.h index d46b87c41b..9b6d287249 100644 --- a/modules/gdnative/godot/godot_variant.h +++ b/modules/gdnative/godot/godot_variant.h @@ -68,7 +68,6 @@ typedef enum godot_variant_type { GODOT_VARIANT_TYPE_NODE_PATH, // 15 GODOT_VARIANT_TYPE_RID, GODOT_VARIANT_TYPE_OBJECT, - GODOT_VARIANT_TYPE_INPUT_EVENT, // TODO: remove me once input_event is removed from main Godot codebase GODOT_VARIANT_TYPE_DICTIONARY, GODOT_VARIANT_TYPE_ARRAY, // 20 @@ -119,7 +118,7 @@ typedef struct godot_variant_call_error { godot_variant_type GDAPI godot_variant_get_type(const godot_variant *p_v); -void GDAPI godot_variant_copy(godot_variant *r_dest, const godot_variant *p_src); +void GDAPI godot_variant_new_copy(godot_variant *r_dest, const godot_variant *p_src); void GDAPI godot_variant_new_nil(godot_variant *r_dest); diff --git a/modules/gridmap/grid_map.cpp b/modules/gridmap/grid_map.cpp index d1f54e021f..9d3da8227c 100644 --- a/modules/gridmap/grid_map.cpp +++ b/modules/gridmap/grid_map.cpp @@ -1057,12 +1057,12 @@ Error GridMap::create_area(int p_id, const Rect3 &p_bounds) { // FIRST VALIDATE AREA IndexKey from, to; - from.x = p_bounds.pos.x; - from.y = p_bounds.pos.y; - from.z = p_bounds.pos.z; - to.x = p_bounds.pos.x + p_bounds.size.x; - to.y = p_bounds.pos.y + p_bounds.size.y; - to.z = p_bounds.pos.z + p_bounds.size.z; + from.x = p_bounds.position.x; + from.y = p_bounds.position.y; + from.z = p_bounds.position.z; + to.x = p_bounds.position.x + p_bounds.size.x; + to.y = p_bounds.position.y + p_bounds.size.y; + to.z = p_bounds.position.z + p_bounds.size.z; for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) { //this should somehow be faster... @@ -1101,8 +1101,8 @@ Rect3 GridMap::area_get_bounds(int p_area) const { const Area *a = area_map[p_area]; Rect3 aabb; - aabb.pos = Vector3(a->from.x, a->from.y, a->from.z); - aabb.size = Vector3(a->to.x, a->to.y, a->to.z) - aabb.pos; + aabb.position = Vector3(a->from.x, a->from.y, a->from.z); + aabb.size = Vector3(a->to.x, a->to.y, a->to.z) - aabb.position; return aabb; } diff --git a/modules/gridmap/grid_map_editor_plugin.cpp b/modules/gridmap/grid_map_editor_plugin.cpp index e567e08c79..954e865bcd 100644 --- a/modules/gridmap/grid_map_editor_plugin.cpp +++ b/modules/gridmap/grid_map_editor_plugin.cpp @@ -560,7 +560,7 @@ bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const Ref<Inpu else return false; - return do_input_action(p_camera, Point2(mb->get_pos().x, mb->get_pos().y), true); + return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true); } else { if ( @@ -604,7 +604,7 @@ bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const Ref<Inpu if (mm.is_valid()) { - return do_input_action(p_camera, mm->get_pos(), false); + return do_input_action(p_camera, mm->get_position(), false); } } else if (edit_mode->get_selected() == 1) { @@ -616,7 +616,7 @@ bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const Ref<Inpu if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) { - Point2 point = mb->get_pos(); + Point2 point = mb->get_position(); Camera *camera = p_camera; Vector3 from = camera->project_ray_origin(point); @@ -635,7 +635,7 @@ bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const Ref<Inpu int area = E->get(); Rect3 aabb = node->area_get_bounds(area); - aabb.pos *= node->get_cell_size(); + aabb.position *= node->get_cell_size(); aabb.size *= node->get_cell_size(); Vector3 rclip, rnormal; diff --git a/modules/hdr/image_loader_hdr.cpp b/modules/hdr/image_loader_hdr.cpp index 85819104cf..19df27b962 100644 --- a/modules/hdr/image_loader_hdr.cpp +++ b/modules/hdr/image_loader_hdr.cpp @@ -131,7 +131,7 @@ Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force //convert for (int i = 0; i < width * height; i++) { - float exp = pow(2, ptr[3] - 128); + float exp = pow(2.0f, ptr[3] - 128.0f); Color c( ptr[0] * exp / 255.0, diff --git a/modules/hdr/image_loader_hdr.h b/modules/hdr/image_loader_hdr.h index 9bc1fadd13..127833ebd0 100644 --- a/modules/hdr/image_loader_hdr.h +++ b/modules/hdr/image_loader_hdr.h @@ -27,8 +27,8 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef IMAGE_LOADER_TINYEXR_H -#define IMAGE_LOADER_TINYEXR_H +#ifndef IMAGE_LOADER_HDR_H +#define IMAGE_LOADER_HDR_H #include "io/image_loader.h" diff --git a/modules/squish/image_compress_squish.cpp b/modules/squish/image_compress_squish.cpp index efed0b8665..32772e0cd2 100644 --- a/modules/squish/image_compress_squish.cpp +++ b/modules/squish/image_compress_squish.cpp @@ -64,6 +64,7 @@ void image_decompress_squish(Image *p_image) { } else if (p_image->get_format() == Image::FORMAT_RGTC_RG) { squish_flags = squish::kBc5; } else { + print_line("wtf askd to decompress.. " + itos(p_image->get_format())); ERR_FAIL_COND(true); return; } @@ -153,8 +154,8 @@ void image_compress_squish(Image *p_image, bool p_srgb) { int bh = h % 4 != 0 ? h + (4 - h % 4) : h; int src_ofs = p_image->get_mipmap_offset(i); - squish::CompressImage(&rb[src_ofs], bw, bh, &wb[dst_ofs], squish_comp); - dst_ofs += (MAX(4, w) * MAX(4, h)) >> shift; + squish::CompressImage(&rb[src_ofs], w, h, &wb[dst_ofs], squish_comp); + dst_ofs += (MAX(4, bw) * MAX(4, bh)) >> shift; w >>= 1; h >>= 1; } diff --git a/modules/tga/SCsub b/modules/tga/SCsub new file mode 100644 index 0000000000..7e405f405c --- /dev/null +++ b/modules/tga/SCsub @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +Import('env') +Import('env_modules') + +env_tga = env_modules.Clone() + +# Godot's own source files +env_tga.add_source_files(env.modules_sources, "*.cpp") diff --git a/modules/tga/config.py b/modules/tga/config.py new file mode 100644 index 0000000000..fb920482f5 --- /dev/null +++ b/modules/tga/config.py @@ -0,0 +1,7 @@ + +def can_build(platform): + return True + + +def configure(env): + pass diff --git a/modules/tga/image_loader_tga.cpp b/modules/tga/image_loader_tga.cpp new file mode 100644 index 0000000000..5b8610b975 --- /dev/null +++ b/modules/tga/image_loader_tga.cpp @@ -0,0 +1,314 @@ +/*************************************************************************/ +/* image_loader_jpegd.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#include "image_loader_tga.h" + +#include "os/os.h" +#include "print_string.h" + +Error ImageLoaderTGA::decode_tga_rle(const uint8_t *p_compressed_buffer, size_t p_pixel_size, uint8_t *p_uncompressed_buffer, size_t p_output_size) { + Error error; + + PoolVector<uint8_t> pixels; + error = pixels.resize(p_pixel_size); + if (error != OK) + return error; + + PoolVector<uint8_t>::Write pixels_w = pixels.write(); + + size_t compressed_pos = 0; + size_t output_pos = 0; + size_t c = 0; + size_t count = 0; + + while (output_pos < p_output_size) { + c = p_compressed_buffer[compressed_pos]; + compressed_pos += 1; + count = (c & 0x7f) + 1; + + if (c & 0x80) { + for (int i = 0; i < p_pixel_size; i++) { + pixels_w.ptr()[i] = p_compressed_buffer[compressed_pos]; + compressed_pos += 1; + } + for (int i = 0; i < count; i++) { + for (int j = 0; j < p_pixel_size; j++) { + p_uncompressed_buffer[output_pos + j] = pixels_w.ptr()[j]; + } + output_pos += p_pixel_size; + } + } else { + count *= p_pixel_size; + for (int i = 0; i < count; i++) { + p_uncompressed_buffer[output_pos] = p_compressed_buffer[compressed_pos]; + compressed_pos += 1; + output_pos += 1; + } + } + } + return OK; +} + +Error ImageLoaderTGA::convert_to_image(Ref<Image> p_image, const uint8_t *p_buffer, const tga_header_s &p_header, const uint8_t *p_palette, const bool p_is_monochrome) { + +#define TGA_PUT_PIXEL(r, g, b, a) \ + int image_data_ofs = ((y * width) + x); \ + image_data_w[image_data_ofs * 4 + 0] = r; \ + image_data_w[image_data_ofs * 4 + 1] = g; \ + image_data_w[image_data_ofs * 4 + 2] = b; \ + image_data_w[image_data_ofs * 4 + 3] = a; + + uint32_t width = p_header.image_width; + uint32_t height = p_header.image_height; + tga_origin_e origin = static_cast<tga_origin_e>((p_header.image_descriptor & TGA_ORIGIN_MASK) >> TGA_ORIGIN_SHIFT); + + uint32_t x_start; + int32_t x_step; + uint32_t x_end; + uint32_t y_start; + int32_t y_step; + uint32_t y_end; + + if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_TOP_RIGHT) { + y_start = 0; + y_step = 1; + y_end = height; + } else { + y_start = height - 1; + y_step = -1; + y_end = -1; + } + + if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_BOTTOM_LEFT) { + x_start = 0; + x_step = 1; + x_end = width; + } else { + x_start = width - 1; + x_step = -1; + x_end = -1; + } + + PoolVector<uint8_t> image_data; + image_data.resize(width * height * sizeof(uint32_t)); + PoolVector<uint8_t>::Write image_data_w = image_data.write(); + + size_t i = 0; + uint32_t x = x_start; + uint32_t y = y_start; + + if (p_header.pixel_depth == 8) { + if (p_is_monochrome) { + while (y != y_end) { + while (x != x_end) { + uint8_t shade = p_buffer[i]; + + TGA_PUT_PIXEL(shade, shade, shade, 0xff) + + x += x_step; + i += 1; + } + x = x_start; + y += y_step; + } + } else { + while (y != y_end) { + while (x != x_end) { + uint8_t index = p_buffer[i]; + uint8_t r = 0x00; + uint8_t g = 0x00; + uint8_t b = 0x00; + uint8_t a = 0xff; + + if (p_header.color_map_depth == 24) { + r = (p_palette[(index * 3) + 0]); + g = (p_palette[(index * 3) + 1]); + b = (p_palette[(index * 3) + 2]); + } else { + return ERR_INVALID_DATA; + } + + TGA_PUT_PIXEL(r, g, b, a) + + x += x_step; + i += 1; + } + x = x_start; + y += y_step; + } + } + } else if (p_header.pixel_depth == 24) { + while (y != y_end) { + while (x != x_end) { + uint8_t r = p_buffer[i + 2]; + uint8_t g = p_buffer[i + 1]; + uint8_t b = p_buffer[i + 0]; + + TGA_PUT_PIXEL(r, g, b, 0xff) + + x += x_step; + i += 3; + } + x = x_start; + y += y_step; + } + } else if (p_header.pixel_depth == 32) { + while (y != y_end) { + while (x != x_end) { + uint8_t a = p_buffer[i + 3]; + uint8_t r = p_buffer[i + 2]; + uint8_t g = p_buffer[i + 1]; + uint8_t b = p_buffer[i + 0]; + + TGA_PUT_PIXEL(r, g, b, a) + + x += x_step; + i += 4; + } + x = x_start; + y += y_step; + } + } + + image_data_w = PoolVector<uint8_t>::Write(); + + p_image->create(width, height, 0, Image::FORMAT_RGBA8, image_data); + + return OK; +} + +Error ImageLoaderTGA::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) { + + PoolVector<uint8_t> src_image; + int src_image_len = f->get_len(); + ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT); + ERR_FAIL_COND_V(src_image_len < sizeof(tga_header_s), ERR_FILE_CORRUPT); + src_image.resize(src_image_len); + + Error err = OK; + + tga_header_s tga_header; + tga_header.id_length = f->get_8(); + tga_header.color_map_type = f->get_8(); + tga_header.image_type = static_cast<tga_type_e>(f->get_8()); + + tga_header.first_color_entry = f->get_16(); + tga_header.color_map_length = f->get_16(); + tga_header.color_map_depth = f->get_8(); + + tga_header.x_origin = f->get_16(); + tga_header.y_origin = f->get_16(); + tga_header.image_width = f->get_16(); + tga_header.image_height = f->get_16(); + tga_header.pixel_depth = f->get_8(); + tga_header.image_descriptor = f->get_8(); + + bool is_encoded = (tga_header.image_type == TGA_TYPE_RLE_INDEXED || tga_header.image_type == TGA_TYPE_RLE_RGB || tga_header.image_type == TGA_TYPE_RLE_MONOCHROME); + bool has_color_map = (tga_header.image_type == TGA_TYPE_RLE_INDEXED || tga_header.image_type == TGA_TYPE_INDEXED); + bool is_monochrome = (tga_header.image_type == TGA_TYPE_RLE_MONOCHROME || tga_header.image_type == TGA_TYPE_MONOCHROME); + + if (tga_header.image_type == TGA_TYPE_NO_DATA) + err = FAILED; + + if (has_color_map) { + if (tga_header.color_map_length > 256 || (tga_header.color_map_depth != 24) || tga_header.color_map_type != 1) { + err = FAILED; + } + } else { + if (tga_header.color_map_type) { + err = FAILED; + } + } + + if (tga_header.image_width <= 0 || tga_header.image_height <= 0) + err = FAILED; + + if (tga_header.pixel_depth != 8 && tga_header.pixel_depth != 24 && tga_header.pixel_depth != 32) + err = FAILED; + + if (err == OK) { + f->seek(f->get_pos() + tga_header.id_length); + + PoolVector<uint8_t> palette; + + if (has_color_map) { + size_t color_map_size = tga_header.color_map_length * (tga_header.color_map_depth >> 3); + err = palette.resize(color_map_size); + if (err == OK) { + PoolVector<uint8_t>::Write palette_w = palette.write(); + f->get_buffer(&palette_w[0], color_map_size); + } else { + return OK; + } + } + + PoolVector<uint8_t>::Write src_image_w = src_image.write(); + f->get_buffer(&src_image_w[0], src_image_len - f->get_pos()); + + PoolVector<uint8_t>::Read src_image_r = src_image.read(); + + const size_t pixel_size = tga_header.pixel_depth >> 3; + const size_t buffer_size = (tga_header.image_width * tga_header.image_height) * pixel_size; + + PoolVector<uint8_t> uncompressed_buffer; + uncompressed_buffer.resize(buffer_size); + PoolVector<uint8_t>::Write uncompressed_buffer_w = uncompressed_buffer.write(); + PoolVector<uint8_t>::Read uncompressed_buffer_r; + + const uint8_t *buffer = NULL; + + if (is_encoded) { + + err = decode_tga_rle(src_image_r.ptr(), pixel_size, uncompressed_buffer_w.ptr(), buffer_size); + + if (err == OK) { + uncompressed_buffer_r = uncompressed_buffer.read(); + buffer = uncompressed_buffer_r.ptr(); + } + } else { + buffer = src_image_r.ptr(); + }; + + if (err == OK) { + PoolVector<uint8_t>::Read palette_r = palette.read(); + err = convert_to_image(p_image, buffer, tga_header, palette_r.ptr(), is_monochrome); + } + } + + f->close(); + return err; +} + +void ImageLoaderTGA::get_recognized_extensions(List<String> *p_extensions) const { + + p_extensions->push_back("tga"); +} + +ImageLoaderTGA::ImageLoaderTGA() { +} diff --git a/modules/tga/image_loader_tga.h b/modules/tga/image_loader_tga.h new file mode 100644 index 0000000000..11329ec68a --- /dev/null +++ b/modules/tga/image_loader_tga.h @@ -0,0 +1,83 @@ +/*************************************************************************/ +/* image_loader_jpegd.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#ifndef IMAGE_LOADER_TGA_H +#define IMAGE_LOADER_TGA_H + +#include "io/image_loader.h" + +/** + @author SaracenOne +*/ +class ImageLoaderTGA : public ImageFormatLoader { + enum tga_type_e { + TGA_TYPE_NO_DATA = 0, + TGA_TYPE_INDEXED = 1, + TGA_TYPE_RGB = 2, + TGA_TYPE_MONOCHROME = 3, + TGA_TYPE_RLE_INDEXED = 9, + TGA_TYPE_RLE_RGB = 10, + TGA_TYPE_RLE_MONOCHROME = 11 + }; + + enum tga_origin_e { + TGA_ORIGIN_BOTTOM_LEFT = 0x00, + TGA_ORIGIN_BOTTOM_RIGHT = 0x01, + TGA_ORIGIN_TOP_LEFT = 0x02, + TGA_ORIGIN_TOP_RIGHT = 0x03, + TGA_ORIGIN_SHIFT = 0x04, + TGA_ORIGIN_MASK = 0x30 + }; + + struct tga_header_s { + uint8_t id_length; + uint8_t color_map_type; + tga_type_e image_type; + + uint16_t first_color_entry; + uint16_t color_map_length; + uint8_t color_map_depth; + + uint16_t x_origin; + uint16_t y_origin; + uint16_t image_width; + uint16_t image_height; + uint8_t pixel_depth; + uint8_t image_descriptor; + }; + static Error decode_tga_rle(const uint8_t *p_compressed_buffer, size_t p_pixel_size, uint8_t *p_uncompressed_buffer, size_t p_output_size); + static Error convert_to_image(Ref<Image> p_image, const uint8_t *p_buffer, const tga_header_s &p_header, const uint8_t *p_palette, const bool p_is_monochrome); + +public: + virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear); + virtual void get_recognized_extensions(List<String> *p_extensions) const; + ImageLoaderTGA(); +}; + +#endif diff --git a/modules/tga/register_types.cpp b/modules/tga/register_types.cpp new file mode 100644 index 0000000000..6e120fa3bf --- /dev/null +++ b/modules/tga/register_types.cpp @@ -0,0 +1,45 @@ +/*************************************************************************/ +/* register_types.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#include "register_types.h" + +#include "image_loader_tga.h" + +static ImageLoaderTGA *image_loader_tga = NULL; + +void register_tga_types() { + + image_loader_tga = memnew(ImageLoaderTGA); + ImageLoader::add_image_format_loader(image_loader_tga); +} + +void unregister_tga_types() { + + memdelete(image_loader_tga); +} diff --git a/modules/tga/register_types.h b/modules/tga/register_types.h new file mode 100644 index 0000000000..079b7bf291 --- /dev/null +++ b/modules/tga/register_types.h @@ -0,0 +1,31 @@ +/*************************************************************************/ +/* register_types.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +void register_tga_types(); +void unregister_tga_types(); diff --git a/modules/visual_script/visual_script_editor.cpp b/modules/visual_script/visual_script_editor.cpp index 5839bc9243..941668d474 100644 --- a/modules/visual_script/visual_script_editor.cpp +++ b/modules/visual_script/visual_script_editor.cpp @@ -1011,7 +1011,7 @@ void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_butt } Rect2 pos = members->get_item_rect(ti); - new_function_menu->set_position(members->get_global_position() + pos.pos + Vector2(0, pos.size.y)); + new_function_menu->set_position(members->get_global_position() + pos.position + Vector2(0, pos.size.y)); new_function_menu->popup(); return; } else if (p_button == 0) { @@ -2240,6 +2240,10 @@ void VisualScriptEditor::add_callback(const String &p_function, PoolStringArray //undo_redo->clear_history(); } +bool VisualScriptEditor::show_members_overview() { + return false; +} + void VisualScriptEditor::update_settings() { _update_graph(); diff --git a/modules/visual_script/visual_script_editor.h b/modules/visual_script/visual_script_editor.h index bb832431a0..92f31f20da 100644 --- a/modules/visual_script/visual_script_editor.h +++ b/modules/visual_script/visual_script_editor.h @@ -248,6 +248,7 @@ public: virtual void get_breakpoints(List<int> *p_breakpoints); virtual void add_callback(const String &p_function, PoolStringArray p_args); virtual void update_settings(); + virtual bool show_members_overview(); virtual void set_debugger_active(bool p_active); virtual void set_tooltip_request_func(String p_method, Object *p_obj); virtual Control *get_edit_menu(); diff --git a/platform/android/detect.py b/platform/android/detect.py index 8d2ed59f17..ce44ffbf74 100644 --- a/platform/android/detect.py +++ b/platform/android/detect.py @@ -232,7 +232,7 @@ def configure(env): elif (env["target"] == "debug"): env.Append(LINKFLAGS=['-O0']) env.Append(CPPFLAGS=['-O0', '-D_DEBUG', '-UNDEBUG', '-DDEBUG_ENABLED', - '-DDEBUG_MEMORY_ALLOC', '-g', '-fno-limit-debug-info']) + '-DDEBUG_MEMORY_ENABLED', '-g', '-fno-limit-debug-info']) env.Append(CPPFLAGS=['-DANDROID_ENABLED', '-DUNIX_ENABLED', '-DNO_FCNTL', '-DMPC_FIXED_POINT']) diff --git a/platform/android/java_glue.cpp b/platform/android/java_glue.cpp index 37f53a2478..d4bd443689 100644 --- a/platform/android/java_glue.cpp +++ b/platform/android/java_glue.cpp @@ -1367,7 +1367,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env jevent.device = p_device; jevent.type = OS_Android::JOY_EVENT_BUTTON; jevent.index = p_button; - jevent->is_pressed() = p_pressed; + jevent.pressed = p_pressed; input_mutex->lock(); joy_events.push_back(jevent); diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index 1c721c645c..9010b9e7da 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -344,7 +344,7 @@ void OS_Android::process_joy_event(OS_Android::JoypadEvent p_event) { switch (p_event.type) { case JOY_EVENT_BUTTON: - input->joy_button(p_event.device, p_event.index, p_event->is_pressed()); + input->joy_button(p_event.device, p_event.index, p_event.pressed); break; case JOY_EVENT_AXIS: InputDefault::JoyAxis value; @@ -380,8 +380,8 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> ev->set_button_index(BUTTON_LEFT); ev->set_button_mask(BUTTON_MASK_LEFT); ev->set_pressed(false); - ev->set_pos(touch[0].pos); - ev->set_global_pos(touch[0].pos); + ev->set_position(touch[0].pos); + ev->set_global_position(touch[0].pos); input->parse_input_event(ev); } @@ -391,7 +391,7 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> ev.instance(); ev->set_index(touch[i].id); ev->set_pressed(false); - ev->set_pos(touch[i].pos); + ev->set_position(touch[i].pos); input->parse_input_event(ev); } } @@ -406,12 +406,12 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> //send mouse Ref<InputEventMouseButton> ev; ev.instance(); - ev.type = Ref<InputEvent>::MOUSE_BUTTON; + // ev.type = Ref<InputEvent>::MOUSE_BUTTON; ev->set_button_index(BUTTON_LEFT); ev->set_button_mask(BUTTON_MASK_LEFT); ev->set_pressed(true); - ev->set_pos(touch[0].pos); - ev->set_global_pos(touch[0].pos); + ev->set_position(touch[0].pos); + ev->set_global_position(touch[0].pos); input->set_mouse_position(Point2(touch[0].pos.x, touch[0].pos.y)); last_mouse = touch[0].pos; input->parse_input_event(ev); @@ -424,7 +424,7 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> ev.instance(); ev->set_index(touch[i].id); ev->set_pressed(true); - ev->set_pos(touch[i].pos.x); + ev->set_position(touch[i].pos); input->parse_input_event(ev); } @@ -436,8 +436,8 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> Ref<InputEventMouseMotion> ev; ev.instance(); ev->set_button_mask(BUTTON_MASK_LEFT); - ev->set_pos(p_points[0].pos.x); - input->set_mouse_position(Point2(ev.mouse_motion.x, ev.mouse_motion.y)); + ev->set_position(p_points[0].pos); + input->set_mouse_position(Point2(ev->get_position().x, ev->get_position().y)); ev->set_speed(input->get_last_mouse_speed()); ev->set_relative(p_points[0].pos - last_mouse); last_mouse = p_points[0].pos; @@ -465,7 +465,7 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> Ref<InputEventScreenDrag> ev; ev.instance(); ev->set_index(touch[i].id); - ev->set_pos(p_points[idx].pos.x); + ev->set_position(p_points[idx].pos); ev->set_relative(p_points[idx].pos - touch[i].pos); input->parse_input_event(ev); touch[i].pos = p_points[idx].pos; @@ -481,8 +481,8 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> ev->set_button_index(BUTTON_LEFT); ev->set_button_mask(BUTTON_MASK_LEFT); ev->set_pressed(false); - ev->set_pos(touch[0].pos.x); - ev->set_global_pos(touch[0].pos.x); + ev->set_position(touch[0].pos); + ev->set_global_position(touch[0].pos); input->set_mouse_position(Point2(touch[0].pos.x, touch[0].pos.y)); input->parse_input_event(ev); @@ -492,7 +492,7 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> ev.instance(); ev->set_index(touch[i].id); ev->set_pressed(false); - ev->set_pos(touch[i].pos); + ev->set_position(touch[i].pos); input->parse_input_event(ev); } touch.clear(); @@ -511,7 +511,7 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> ev->set_index(tp.id); ev->set_pressed(true); - ev->set_pos(tp.pos); + ev->set_position(tp.pos); input->parse_input_event(ev); } break; @@ -524,7 +524,7 @@ void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> ev.instance(); ev->set_index(touch[i].id); ev->set_pressed(false); - ev->set_pos(touch[i].pos); + ev->set_position(touch[i].pos); input->parse_input_event(ev); touch.remove(i); i--; diff --git a/platform/iphone/detect.py b/platform/iphone/detect.py index 2dcd6dd7e5..c20d8e90f4 100644 --- a/platform/iphone/detect.py +++ b/platform/iphone/detect.py @@ -58,15 +58,15 @@ def configure(env): if (env["ios_sim"] == "yes" or env["arch"] == "x86"): # i386, simulator env["arch"] = "x86" env["bits"] = "32" - env.Append(CCFLAGS=string.split('-arch i386 -fobjc-abi-version=2 -fobjc-legacy-dispatch -fmessage-length=0 -fpascal-strings -fasm-blocks -D__IPHONE_OS_VERSION_MIN_REQUIRED=40100 -isysroot $IPHONESDK -mios-simulator-version-min=4.3 -DCUSTOM_MATRIX_TRANSFORM_H=\\\"build/iphone/matrix4_iphone.h\\\" -DCUSTOM_VECTOR3_TRANSFORM_H=\\\"build/iphone/vector3_iphone.h\\\"')) + env.Append(CCFLAGS=string.split('-arch i386 -fobjc-abi-version=2 -fobjc-legacy-dispatch -fmessage-length=0 -fpascal-strings -fblocks -fasm-blocks -D__IPHONE_OS_VERSION_MIN_REQUIRED=40100 -isysroot $IPHONESDK -mios-simulator-version-min=4.3 -DCUSTOM_MATRIX_TRANSFORM_H=\\\"build/iphone/matrix4_iphone.h\\\" -DCUSTOM_VECTOR3_TRANSFORM_H=\\\"build/iphone/vector3_iphone.h\\\"')) elif (env["arch"] == "arm" or env["arch"] == "arm32" or env["arch"] == "armv7" or env["bits"] == "32"): # arm env["arch"] = "arm" env["bits"] = "32" - env.Append(CCFLAGS=string.split('-fno-objc-arc -arch armv7 -fmessage-length=0 -fno-strict-aliasing -fdiagnostics-print-source-range-info -fdiagnostics-show-category=id -fdiagnostics-parseable-fixits -fpascal-strings -isysroot $IPHONESDK -fvisibility=hidden -mthumb "-DIBOutlet=__attribute__((iboutlet))" "-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection(ClassName)))" "-DIBAction=void)__attribute__((ibaction)" -miphoneos-version-min=9.0 -MMD -MT dependencies -isysroot $IPHONESDK')) + env.Append(CCFLAGS=string.split('-fno-objc-arc -arch armv7 -fmessage-length=0 -fno-strict-aliasing -fdiagnostics-print-source-range-info -fdiagnostics-show-category=id -fdiagnostics-parseable-fixits -fpascal-strings -fblocks -isysroot $IPHONESDK -fvisibility=hidden -mthumb "-DIBOutlet=__attribute__((iboutlet))" "-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection(ClassName)))" "-DIBAction=void)__attribute__((ibaction)" -miphoneos-version-min=9.0 -MMD -MT dependencies')) else: # armv64 env["arch"] = "arm64" env["bits"] = "64" - env.Append(CCFLAGS=string.split('-fno-objc-arc -arch arm64 -fmessage-length=0 -fno-strict-aliasing -fdiagnostics-print-source-range-info -fdiagnostics-show-category=id -fdiagnostics-parseable-fixits -fpascal-strings -fvisibility=hidden -MMD -MT dependencies -miphoneos-version-min=9.0 -isysroot $IPHONESDK')) + env.Append(CCFLAGS=string.split('-fno-objc-arc -arch arm64 -fmessage-length=0 -fno-strict-aliasing -fdiagnostics-print-source-range-info -fdiagnostics-show-category=id -fdiagnostics-parseable-fixits -fpascal-strings -fblocks -fvisibility=hidden -MMD -MT dependencies -miphoneos-version-min=9.0 -isysroot $IPHONESDK')) env.Append(CPPFLAGS=['-DNEED_LONG_INT']) env.Append(CPPFLAGS=['-DLIBYUV_DISABLE_NEON']) @@ -91,6 +91,7 @@ def configure(env): '-framework', 'OpenGLES', '-framework', 'QuartzCore', '-framework', 'SystemConfiguration', + '-framework', 'GameController', '-F$IPHONESDK', ]) elif (env["arch"] == "arm64"): @@ -111,6 +112,7 @@ def configure(env): '-framework', 'AVFoundation', '-framework', 'CoreMedia', '-framework', 'CoreMotion', + '-framework', 'GameController', ]) else: env.Append(LINKFLAGS=['-arch', 'armv7', '-Wl,-dead_strip', '-miphoneos-version-min=9.0', @@ -129,10 +131,11 @@ def configure(env): '-framework', 'AVFoundation', '-framework', 'CoreMedia', '-framework', 'CoreMotion', + '-framework', 'GameController', ]) if env['game_center'] == 'yes': - env.Append(CPPFLAGS=['-fblocks', '-DGAME_CENTER_ENABLED']) + env.Append(CPPFLAGS=['-DGAME_CENTER_ENABLED']) env.Append(LINKFLAGS=['-framework', 'GameKit']) if env['store_kit'] == 'yes': @@ -144,26 +147,20 @@ def configure(env): env.Append(CPPPATH=['$IPHONESDK/usr/include', '$IPHONESDK/System/Library/Frameworks/OpenGLES.framework/Headers', '$IPHONESDK/System/Library/Frameworks/AudioUnit.framework/Headers']) - if (env["target"] == "release"): + if (env["target"].startswith("release")): - env.Append(CCFLAGS=['-O3', '-DNS_BLOCK_ASSERTIONS=1', '-gdwarf-2']) # removed -ffast-math - env.Append(LINKFLAGS=['-O3']) + env.Append(CPPFLAGS=['-DNDEBUG', '-DNS_BLOCK_ASSERTIONS=1']) + env.Append(CPPFLAGS=['-O2', '-flto', '-ftree-vectorize', '-fomit-frame-pointer', '-ffast-math', '-funsafe-math-optimizations']) + env.Append(LINKFLAGS=['-O2', '-flto']) - elif env["target"] == "release_debug": - env.Append(CCFLAGS=['-Os', '-DNS_BLOCK_ASSERTIONS=1', '-DDEBUG_ENABLED']) - env.Append(LINKFLAGS=['-Os']) - env.Append(CPPFLAGS=['-DDEBUG_MEMORY_ENABLED']) + if env["target"] == "release_debug": + env.Append(CPPFLAGS=['-DDEBUG_ENABLED']) elif (env["target"] == "debug"): - env.Append(CCFLAGS=['-D_DEBUG', '-DDEBUG=1', '-gdwarf-2', '-O0', '-DDEBUG_ENABLED']) + env.Append(CPPFLAGS=['-D_DEBUG', '-DDEBUG=1', '-gdwarf-2', '-O0', '-DDEBUG_ENABLED']) env.Append(CPPFLAGS=['-DDEBUG_MEMORY_ENABLED']) - elif (env["target"] == "profile"): - - env.Append(CCFLAGS=['-g', '-pg', '-Os']) - env.Append(LINKFLAGS=['-pg']) - if (env["ios_sim"] == "yes"): # TODO: Check if needed? env['ENV']['MACOSX_DEPLOYMENT_TARGET'] = '10.6' env['ENV']['CODESIGN_ALLOCATE'] = '/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate' diff --git a/platform/iphone/os_iphone.cpp b/platform/iphone/os_iphone.cpp index 0d7913446e..b244edbff6 100644 --- a/platform/iphone/os_iphone.cpp +++ b/platform/iphone/os_iphone.cpp @@ -212,7 +212,7 @@ void OSIPhone::mouse_button(int p_idx, int p_x, int p_y, bool p_pressed, bool p_ ev->set_index(p_idx); ev->set_pressed(p_pressed); - ev->set_pos(Vector2(p_x, p_y)); + ev->set_position(Vector2(p_x, p_y)); queue_event(ev); }; @@ -225,12 +225,12 @@ void OSIPhone::mouse_button(int p_idx, int p_x, int p_y, bool p_pressed, bool p_ // swaped it for tilted screen //ev->get_pos().x = ev.mouse_button.global_x = video_mode.height - p_y; //ev->get_pos().y = ev.mouse_button.global_y = p_x; - ev->set_pos(Vector2(video_mode.height - p_y, p_x)); - ev->set_global_pos(Vector2(video_mode.height - p_y, p_x)); + ev->set_position(Vector2(video_mode.height - p_y, p_x)); + ev->set_global_position(Vector2(video_mode.height - p_y, p_x)); //mouse_list.pressed[p_idx] = p_pressed; - input->set_mouse_position(ev->get_pos()); + input->set_mouse_position(ev->get_position()); ev->set_button_index(BUTTON_LEFT); ev->set_doubleclick(p_doubleclick); ev->set_pressed(p_pressed); @@ -246,7 +246,7 @@ void OSIPhone::mouse_move(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_ Ref<InputEventScreenDrag> ev; ev.instance(); ev->set_index(p_idx); - ev->set_pos(Vector2(p_x, p_y)); + ev->set_position(Vector2(p_x, p_y)); ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y)); queue_event(ev); }; @@ -255,11 +255,11 @@ void OSIPhone::mouse_move(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_ Ref<InputEventMouseMotion> ev; ev.instance(); - ev->set_pos(Vector2(p_x, p_y)); - ev->set_global_pos(Vector2(p_x, p_y)); + ev->set_position(Vector2(p_x, p_y)); + ev->set_global_position(Vector2(p_x, p_y)); ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y)); - input->set_mouse_position(ev->get_pos()); + input->set_mouse_position(ev->get_position()); ev->set_speed(input->get_last_mouse_speed()); ev->set_button_mask(BUTTON_LEFT); // pressed diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index 3a37d663ad..123816b22d 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -30,21 +30,21 @@ #ifndef OS_OSX_H #define OS_OSX_H +#include "drivers/alsa/audio_driver_alsa.h" +#include "drivers/rtaudio/audio_driver_rtaudio.h" #include "drivers/unix/os_unix.h" #include "joypad_osx.h" #include "main/input_default.h" #include "os/input.h" -#include "power_osx.h" -#include "servers/visual_server.h" -// #include "servers/visual/visual_server_wrap_mt.h" -#include "drivers/alsa/audio_driver_alsa.h" -#include "drivers/rtaudio/audio_driver_rtaudio.h" #include "platform/osx/audio_driver_osx.h" +#include "power_osx.h" #include "servers/audio_server.h" #include "servers/physics_2d/physics_2d_server_sw.h" #include "servers/physics_2d/physics_2d_server_wrap_mt.h" #include "servers/physics_server.h" #include "servers/visual/rasterizer.h" +#include "servers/visual/visual_server_wrap_mt.h" +#include "servers/visual_server.h" #include <ApplicationServices/ApplicationServices.h> //bitch diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index 13bc2226f4..72fba39e33 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -291,8 +291,8 @@ static int button_mask = 0; get_key_modifier_state([event modifierFlags], mb); mb->set_button_index(BUTTON_LEFT); mb->set_pressed(true); - mb->set_pos(Vector2(mouse_x, mouse_y)); - mb->set_global_pos(Vector2(mouse_x, mouse_y)); + mb->set_position(Vector2(mouse_x, mouse_y)); + mb->set_global_position(Vector2(mouse_x, mouse_y)); mb->set_button_mask(button_mask); mb->set_doubleclick([event clickCount] == 2); OS_OSX::singleton->push_input(mb); @@ -311,8 +311,8 @@ static int button_mask = 0; get_key_modifier_state([event modifierFlags], mb); mb->set_button_index(BUTTON_LEFT); mb->set_pressed(false); - mb->set_pos(Vector2(mouse_x, mouse_y)); - mb->set_global_pos(Vector2(mouse_x, mouse_y)); + mb->set_position(Vector2(mouse_x, mouse_y)); + mb->set_global_position(Vector2(mouse_x, mouse_y)); mb->set_button_mask(button_mask); mb->set_doubleclick([event clickCount] == 2); OS_OSX::singleton->push_input(mb); @@ -330,8 +330,8 @@ static int button_mask = 0; const NSPoint p = [event locationInWindow]; mouse_x = p.x * OS_OSX::singleton->_mouse_scale([[event window] backingScaleFactor]); mouse_y = (contentRect.size.height - p.y) * OS_OSX::singleton->_mouse_scale([[event window] backingScaleFactor]); - mm->set_pos(Vector2(mouse_x, mouse_y)); - mm->set_global_pos(Vector2(mouse_x, mouse_y)); + mm->set_position(Vector2(mouse_x, mouse_y)); + mm->set_global_position(Vector2(mouse_x, mouse_y)); Vector2 relativeMotion = Vector2(); relativeMotion.x = [event deltaX] * OS_OSX::singleton->_mouse_scale([[event window] backingScaleFactor]); relativeMotion.y = [event deltaY] * OS_OSX::singleton->_mouse_scale([[event window] backingScaleFactor]); @@ -352,8 +352,8 @@ static int button_mask = 0; get_key_modifier_state([event modifierFlags], mb); mb->set_button_index(BUTTON_RIGHT); mb->set_pressed(true); - mb->set_pos(Vector2(mouse_x, mouse_y)); - mb->set_global_pos(Vector2(mouse_x, mouse_y)); + mb->set_position(Vector2(mouse_x, mouse_y)); + mb->set_global_position(Vector2(mouse_x, mouse_y)); mb->set_button_mask(button_mask); mb->set_doubleclick([event clickCount] == 2); OS_OSX::singleton->push_input(mb); @@ -365,7 +365,7 @@ static int button_mask = 0; - (void)rightMouseUp:(NSEvent *)event { - button_mask |= BUTTON_MASK_RIGHT; + button_mask &= ~BUTTON_MASK_RIGHT; Ref<InputEventMouseButton> mb; mb.instance(); @@ -373,8 +373,8 @@ static int button_mask = 0; get_key_modifier_state([event modifierFlags], mb); mb->set_button_index(BUTTON_RIGHT); mb->set_pressed(false); - mb->set_pos(Vector2(mouse_x, mouse_y)); - mb->set_global_pos(Vector2(mouse_x, mouse_y)); + mb->set_position(Vector2(mouse_x, mouse_y)); + mb->set_global_position(Vector2(mouse_x, mouse_y)); mb->set_button_mask(button_mask); mb->set_doubleclick([event clickCount] == 2); OS_OSX::singleton->push_input(mb); @@ -393,8 +393,8 @@ static int button_mask = 0; get_key_modifier_state([event modifierFlags], mb); mb->set_button_index(BUTTON_MIDDLE); mb->set_pressed(true); - mb->set_pos(Vector2(mouse_x, mouse_y)); - mb->set_global_pos(Vector2(mouse_x, mouse_y)); + mb->set_position(Vector2(mouse_x, mouse_y)); + mb->set_global_position(Vector2(mouse_x, mouse_y)); mb->set_button_mask(button_mask); mb->set_doubleclick([event clickCount] == 2); OS_OSX::singleton->push_input(mb); @@ -417,8 +417,8 @@ static int button_mask = 0; get_key_modifier_state([event modifierFlags], mb); mb->set_button_index(BUTTON_MIDDLE); mb->set_pressed(true); - mb->set_pos(Vector2(mouse_x, mouse_y)); - mb->set_global_pos(Vector2(mouse_x, mouse_y)); + mb->set_position(Vector2(mouse_x, mouse_y)); + mb->set_global_position(Vector2(mouse_x, mouse_y)); mb->set_button_mask(button_mask); mb->set_doubleclick([event clickCount] == 2); OS_OSX::singleton->push_input(mb); @@ -701,8 +701,8 @@ inline void sendScrollEvent(int button, double factor, int modifierFlags) { sc->set_factor(factor); sc->set_pressed(true); Vector2 mouse_pos = Vector2(mouse_x, mouse_y); - sc->set_pos(mouse_pos); - sc->set_global_pos(mouse_pos); + sc->set_position(mouse_pos); + sc->set_global_position(mouse_pos); sc->set_button_mask(button_mask); OS_OSX::singleton->push_input(sc); sc->set_pressed(false); @@ -932,12 +932,10 @@ void OS_OSX::initialize(const VideoMode &p_desired, int p_video_driver, int p_au //visual_server = memnew( VisualServerRaster(rasterizer) ); visual_server = memnew(VisualServerRaster); - // FIXME: Reimplement threaded rendering? Or remove? - /* - if (get_render_thread_mode()!=RENDER_THREAD_UNSAFE) { - visual_server =memnew(VisualServerWrapMT(visual_server,get_render_thread_mode()==RENDER_SEPARATE_THREAD)); + if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) { + + visual_server = memnew(VisualServerWrapMT(visual_server, get_render_thread_mode() == RENDER_SEPARATE_THREAD)); } -*/ visual_server->init(); visual_server->cursor_set_visible(false, 0); @@ -974,7 +972,7 @@ void OS_OSX::initialize(const VideoMode &p_desired, int p_video_driver, int p_au NSRect nsrect = [[screenArray objectAtIndex:i] visibleFrame]; Rect2 rect = Rect2(nsrect.origin.x, nsrect.origin.y, nsrect.size.width, nsrect.size.height); - rect.pos *= displayScale; + rect.position *= displayScale; rect.size *= displayScale; screens.push_back(rect); @@ -1275,7 +1273,7 @@ void OS_OSX::set_current_screen(int p_screen) { Point2 OS_OSX::get_screen_position(int p_screen) const { ERR_FAIL_INDEX_V(p_screen, screens.size(), Point2()); - return screens[p_screen].pos; + return screens[p_screen].position; }; int OS_OSX::get_screen_dpi(int p_screen) const { @@ -1384,7 +1382,7 @@ void OS_OSX::set_window_maximized(bool p_enabled) { [window_object setFrame:[[[NSScreen screens] objectAtIndex:current_screen] visibleFrame] display:YES]; } else { set_window_size(restore_rect.size); - set_window_position(restore_rect.pos); + set_window_position(restore_rect.position); }; maximized = p_enabled; }; diff --git a/platform/uwp/app.cpp b/platform/uwp/app.cpp index 51aba9b7fd..c773c0b746 100644 --- a/platform/uwp/app.cpp +++ b/platform/uwp/app.cpp @@ -263,7 +263,7 @@ void App::pointer_event(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Cor screen_touch.instance(); screen_touch->set_device(0); screen_touch->set_pressed(p_pressed); - screen_touch->set_pos(Vector2(pos.X, pos.Y)); + screen_touch->set_position(Vector2(pos.X, pos.Y)); screen_touch->set_index(_get_finger(point->PointerId)); last_touch_x[screen_touch->get_index()] = pos.X; @@ -280,8 +280,8 @@ void App::pointer_event(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Cor mouse_button->set_device(0); mouse_button->set_pressed(p_pressed); mouse_button->set_button_index(but); - mouse_button->set_pos(Vector2(pos.X, pos.Y)); - mouse_button->set_global_pos(Vector2(pos.X, pos.Y)); + mouse_button->set_position(Vector2(pos.X, pos.Y)); + mouse_button->set_global_position(Vector2(pos.X, pos.Y)); if (p_is_wheel) { if (point->Properties->MouseWheelDelta > 0) { @@ -355,9 +355,9 @@ void App::OnPointerMoved(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Co Ref<InputEventScreenDrag> screen_drag; screen_drag.instance(); screen_drag->set_device(0); - screen_drag->set_pos(Vector2(pos.X, pos.Y)); + screen_drag->set_position(Vector2(pos.X, pos.Y)); screen_drag->set_index(_get_finger(point->PointerId)); - screen_drag->set_relative(Vector2(screen_drag->get_pos().x - last_touch_x[screen_drag->get_index()], screen_drag->get_pos().y - last_touch_y[screen_drag->get_index()])); + screen_drag->set_relative(Vector2(screen_drag->get_position().x - last_touch_x[screen_drag->get_index()], screen_drag->get_position().y - last_touch_y[screen_drag->get_index()])); os->input_event(screen_drag); if (number_of_contacts > 1) @@ -372,8 +372,8 @@ void App::OnPointerMoved(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Co Ref<InputEventMouseMotion> mouse_motion; mouse_motion.instance(); mouse_motion->set_device(0); - mouse_motion->set_pos(Vector2(pos.X, pos.Y)); - mouse_motion->set_global_pos(Vector2(pos.X, pos.Y)); + mouse_motion->set_position(Vector2(pos.X, pos.Y)); + mouse_motion->set_global_position(Vector2(pos.X, pos.Y)); mouse_motion->set_relative(Vector2(pos.X - last_touch_x[31], pos.Y - last_touch_y[31])); last_mouse_pos = pos; @@ -394,8 +394,8 @@ void App::OnMouseMoved(MouseDevice ^ mouse_device, MouseEventArgs ^ args) { Ref<InputEventMouseMotion> mouse_motion; mouse_motion.instance(); mouse_motion->set_device(0); - mouse_motion->set_pos(Vector2(pos.X, pos.Y)); - mouse_motion->set_global_pos(Vector2(pos.X, pos.Y)); + mouse_motion->set_position(Vector2(pos.X, pos.Y)); + mouse_motion->set_global_position(Vector2(pos.X, pos.Y)); mouse_motion->set_relative(Vector2(args->MouseDelta.X, args->MouseDelta.Y)); last_mouse_pos = pos; diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 830aae5515..4393cad3bf 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -37,15 +37,15 @@ #include "drivers/windows/rw_lock_windows.h" #include "drivers/windows/semaphore_windows.h" #include "drivers/windows/thread_windows.h" -#include "servers/audio_server.h" -#include "servers/visual/visual_server_raster.h" -//#include "servers/visual/visual_server_wrap_mt.h" #include "global_config.h" #include "io/marshalls.h" #include "joypad.h" #include "lang_table.h" #include "main/main.h" #include "packet_peer_udp_winsock.h" +#include "servers/audio_server.h" +#include "servers/visual/visual_server_raster.h" +#include "servers/visual/visual_server_wrap_mt.h" #include "stream_peer_winsock.h" #include "tcp_server_winsock.h" @@ -219,7 +219,7 @@ void OS_Windows::_touch_event(bool p_pressed, int p_x, int p_y, int idx) { event.instance(); event->set_index(idx); event->set_pressed(p_pressed); - event->set_pos(Vector2(p_x, p_y)); + event->set_position(Vector2(p_x, p_y)); if (main_loop) { input->parse_input_event(event); @@ -231,7 +231,7 @@ void OS_Windows::_drag_event(int p_x, int p_y, int idx) { Ref<InputEventScreenDrag> event; event.instance(); event->set_index(idx); - event->set_pos(Vector2(p_x, p_y)); + event->set_position(Vector2(p_x, p_y)); if (main_loop) input->parse_input_event(event); @@ -378,7 +378,7 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) last_button_state = mm->get_button_mask(); /*mm->get_button_mask()|=(wParam&MK_XBUTTON1)?(1<<5):0; mm->get_button_mask()|=(wParam&MK_XBUTTON2)?(1<<6):0;*/ - mm->set_pos(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); + mm->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); if (mouse_mode == MOUSE_MODE_CAPTURED) { @@ -386,31 +386,31 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) old_x = c.x; old_y = c.y; - if (mm->get_pos() == c) { + if (mm->get_position() == c) { center = c; return 0; } - Point2i ncenter = mm->get_pos(); + Point2i ncenter = mm->get_position(); center = ncenter; POINT pos = { (int)c.x, (int)c.y }; ClientToScreen(hWnd, &pos); SetCursorPos(pos.x, pos.y); } - input->set_mouse_position(mm->get_pos()); + input->set_mouse_position(mm->get_position()); mm->set_speed(input->get_last_mouse_speed()); if (old_invalid) { - old_x = mm->get_pos().x; - old_y = mm->get_pos().y; + old_x = mm->get_position().x; + old_y = mm->get_position().y; old_invalid = false; } - mm->set_relative(Vector2(mm->get_pos() - Vector2(old_x, old_y))); - old_x = mm->get_pos().x; - old_y = mm->get_pos().y; + mm->set_relative(Vector2(mm->get_position() - Vector2(old_x, old_y))); + old_x = mm->get_position().x; + old_y = mm->get_position().y; if (window_has_focus && main_loop) input->parse_input_event(mm); @@ -536,14 +536,14 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) /* mb->get_button_mask()|=(wParam&MK_XBUTTON1)?(1<<5):0; mb->get_button_mask()|=(wParam&MK_XBUTTON2)?(1<<6):0;*/ - mb->set_pos(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); + mb->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); if (mouse_mode == MOUSE_MODE_CAPTURED) { - mb->set_pos(Vector2(old_x, old_y)); + mb->set_position(Vector2(old_x, old_y)); } - mb->set_global_pos(mb->get_pos()); + mb->set_global_position(mb->get_position()); if (uMsg != WM_MOUSEWHEEL) { if (mb->is_pressed()) { @@ -560,12 +560,12 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) } else if (mouse_mode != MOUSE_MODE_CAPTURED) { // for reasons unknown to mankind, wheel comes in screen cordinates POINT coords; - coords.x = mb->get_pos().x; - coords.y = mb->get_pos().y; + coords.x = mb->get_position().x; + coords.y = mb->get_position().y; ScreenToClient(hWnd, &coords); - mb->set_pos(Vector2(coords.x, coords.y)); + mb->set_position(Vector2(coords.x, coords.y)); } if (main_loop) { @@ -889,8 +889,8 @@ BOOL CALLBACK OS_Windows::MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPR MonitorInfo minfo; minfo.hMonitor = hMonitor; minfo.hdcMonitor = hdcMonitor; - minfo.rect.pos.x = lprcMonitor->left; - minfo.rect.pos.y = lprcMonitor->top; + minfo.rect.position.x = lprcMonitor->left; + minfo.rect.position.y = lprcMonitor->top; minfo.rect.size.x = lprcMonitor->right - lprcMonitor->left; minfo.rect.size.y = lprcMonitor->bottom - lprcMonitor->top; @@ -1029,12 +1029,10 @@ void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int #endif visual_server = memnew(VisualServerRaster); - // FIXME: Reimplement threaded rendering? Or remove? - /* - if (get_render_thread_mode()!=RENDER_THREAD_UNSAFE) { - visual_server =memnew(VisualServerWrapMT(visual_server,get_render_thread_mode()==RENDER_SEPARATE_THREAD)); + if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) { + + visual_server = memnew(VisualServerWrapMT(visual_server, get_render_thread_mode() == RENDER_SEPARATE_THREAD)); } - */ physics_server = memnew(PhysicsServerSW); physics_server->init(); @@ -1370,7 +1368,7 @@ void OS_Windows::set_current_screen(int p_screen) { Point2 OS_Windows::get_screen_position(int p_screen) const { ERR_FAIL_INDEX_V(p_screen, monitor_info.size(), Point2()); - return Vector2(monitor_info[p_screen].rect.pos); + return Vector2(monitor_info[p_screen].rect.position); } Size2 OS_Windows::get_screen_size(int p_screen) const { diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 2a912a7ba2..2eebc96d2c 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -34,6 +34,7 @@ #include "print_string.h" #include "servers/physics/physics_server_sw.h" #include "servers/visual/visual_server_raster.h" +#include "servers/visual/visual_server_wrap_mt.h" #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -206,12 +207,12 @@ void OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_au #endif visual_server = memnew(VisualServerRaster); -#if 0 - if (get_render_thread_mode()!=RENDER_THREAD_UNSAFE) { - visual_server =memnew(VisualServerWrapMT(visual_server,get_render_thread_mode()==RENDER_SEPARATE_THREAD)); + if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) { + + visual_server = memnew(VisualServerWrapMT(visual_server, get_render_thread_mode() == RENDER_SEPARATE_THREAD)); } -#endif + // borderless fullscreen window mode if (current_videomode.fullscreen) { // needed for lxde/openbox, possibly others @@ -1334,8 +1335,8 @@ void OS_X11::process_xevents() { get_key_modifier_state(event.xbutton.state, mb); mb->set_button_mask(get_mouse_button_state(event.xbutton.state)); - mb->set_pos(Vector2(event.xbutton.x, event.xbutton.y)); - mb->set_global_pos(mb->get_pos()); + mb->set_position(Vector2(event.xbutton.x, event.xbutton.y)); + mb->set_global_position(mb->get_position()); mb->set_button_index(event.xbutton.button); if (mb->get_button_index() == 2) mb->set_button_index(3); @@ -1443,8 +1444,8 @@ void OS_X11::process_xevents() { get_key_modifier_state(event.xmotion.state, mm); mm->set_button_mask(get_mouse_button_state(event.xmotion.state)); - mm->set_pos(pos); - mm->set_global_pos(pos); + mm->set_position(pos); + mm->set_global_position(pos); input->set_mouse_position(pos); mm->set_speed(input->get_last_mouse_speed()); mm->set_relative(rel); diff --git a/scene/2d/camera_2d.cpp b/scene/2d/camera_2d.cpp index 71f651b0a4..9d8b5da366 100644 --- a/scene/2d/camera_2d.cpp +++ b/scene/2d/camera_2d.cpp @@ -120,20 +120,20 @@ Transform2D Camera2D::get_camera_transform() { Rect2 screen_rect(-screen_offset + camera_pos, screen_size * zoom); if (offset != Vector2()) - screen_rect.pos += offset; + screen_rect.position += offset; if (limit_smoothing_enabled) { - if (screen_rect.pos.x < limit[MARGIN_LEFT]) - camera_pos.x -= screen_rect.pos.x - limit[MARGIN_LEFT]; + if (screen_rect.position.x < limit[MARGIN_LEFT]) + camera_pos.x -= screen_rect.position.x - limit[MARGIN_LEFT]; - if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT]) - camera_pos.x -= screen_rect.pos.x + screen_rect.size.x - limit[MARGIN_RIGHT]; + if (screen_rect.position.x + screen_rect.size.x > limit[MARGIN_RIGHT]) + camera_pos.x -= screen_rect.position.x + screen_rect.size.x - limit[MARGIN_RIGHT]; - if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) - camera_pos.y -= screen_rect.pos.y + screen_rect.size.y - limit[MARGIN_BOTTOM]; + if (screen_rect.position.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) + camera_pos.y -= screen_rect.position.y + screen_rect.size.y - limit[MARGIN_BOTTOM]; - if (screen_rect.pos.y < limit[MARGIN_TOP]) - camera_pos.y -= screen_rect.pos.y - limit[MARGIN_TOP]; + if (screen_rect.position.y < limit[MARGIN_TOP]) + camera_pos.y -= screen_rect.position.y - limit[MARGIN_TOP]; } if (smoothing_enabled && !get_tree()->is_editor_hint()) { @@ -160,42 +160,42 @@ Transform2D Camera2D::get_camera_transform() { } Rect2 screen_rect(-screen_offset + ret_camera_pos, screen_size * zoom); - if (screen_rect.pos.x < limit[MARGIN_LEFT]) - screen_rect.pos.x = limit[MARGIN_LEFT]; + if (screen_rect.position.x < limit[MARGIN_LEFT]) + screen_rect.position.x = limit[MARGIN_LEFT]; - if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT]) - screen_rect.pos.x = limit[MARGIN_RIGHT] - screen_rect.size.x; + if (screen_rect.position.x + screen_rect.size.x > limit[MARGIN_RIGHT]) + screen_rect.position.x = limit[MARGIN_RIGHT] - screen_rect.size.x; - if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) - screen_rect.pos.y = limit[MARGIN_BOTTOM] - screen_rect.size.y; + if (screen_rect.position.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) + screen_rect.position.y = limit[MARGIN_BOTTOM] - screen_rect.size.y; - if (screen_rect.pos.y < limit[MARGIN_TOP]) - screen_rect.pos.y = limit[MARGIN_TOP]; + if (screen_rect.position.y < limit[MARGIN_TOP]) + screen_rect.position.y = limit[MARGIN_TOP]; if (offset != Vector2()) { - screen_rect.pos += offset; - if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT]) - screen_rect.pos.x = limit[MARGIN_RIGHT] - screen_rect.size.x; + screen_rect.position += offset; + if (screen_rect.position.x + screen_rect.size.x > limit[MARGIN_RIGHT]) + screen_rect.position.x = limit[MARGIN_RIGHT] - screen_rect.size.x; - if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) - screen_rect.pos.y = limit[MARGIN_BOTTOM] - screen_rect.size.y; + if (screen_rect.position.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) + screen_rect.position.y = limit[MARGIN_BOTTOM] - screen_rect.size.y; - if (screen_rect.pos.x < limit[MARGIN_LEFT]) - screen_rect.pos.x = limit[MARGIN_LEFT]; + if (screen_rect.position.x < limit[MARGIN_LEFT]) + screen_rect.position.x = limit[MARGIN_LEFT]; - if (screen_rect.pos.y < limit[MARGIN_TOP]) - screen_rect.pos.y = limit[MARGIN_TOP]; + if (screen_rect.position.y < limit[MARGIN_TOP]) + screen_rect.position.y = limit[MARGIN_TOP]; } - camera_screen_center = screen_rect.pos + screen_rect.size * 0.5; + camera_screen_center = screen_rect.position + screen_rect.size * 0.5; Transform2D xform; if (rotating) { xform.set_rotation(angle); } xform.scale_basis(zoom); - xform.set_origin(screen_rect.pos /*.floor()*/); + xform.set_origin(screen_rect.position /*.floor()*/); /* if (0) { diff --git a/scene/2d/collision_polygon_2d.cpp b/scene/2d/collision_polygon_2d.cpp index 39eef89274..6b603c6473 100644 --- a/scene/2d/collision_polygon_2d.cpp +++ b/scene/2d/collision_polygon_2d.cpp @@ -233,7 +233,7 @@ void CollisionPolygon2D::set_polygon(const Vector<Point2> &p_polygon) { aabb = Rect2(-10, -10, 20, 20); } else { - aabb.pos -= aabb.size * 0.3; + aabb.position -= aabb.size * 0.3; aabb.size += aabb.size * 0.6; } _update_parent(); diff --git a/scene/2d/light_2d.cpp b/scene/2d/light_2d.cpp index 34413074c1..e8c2122bd1 100644 --- a/scene/2d/light_2d.cpp +++ b/scene/2d/light_2d.cpp @@ -336,6 +336,17 @@ String Light2D::get_configuration_warning() const { return String(); } +void Light2D::set_shadow_smooth(float p_amount) { + + shadow_smooth = p_amount; + VS::get_singleton()->canvas_light_set_shadow_smooth(canvas_light, shadow_smooth); +} + +float Light2D::get_shadow_smooth() const { + + return shadow_smooth; +} + void Light2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Light2D::set_enabled); @@ -389,6 +400,9 @@ void Light2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_shadow_buffer_size", "size"), &Light2D::set_shadow_buffer_size); ClassDB::bind_method(D_METHOD("get_shadow_buffer_size"), &Light2D::get_shadow_buffer_size); + ClassDB::bind_method(D_METHOD("set_shadow_smooth", "smooth"), &Light2D::set_shadow_smooth); + ClassDB::bind_method(D_METHOD("get_shadow_smooth"), &Light2D::get_shadow_smooth); + ClassDB::bind_method(D_METHOD("set_shadow_gradient_length", "multiplier"), &Light2D::set_shadow_gradient_length); ClassDB::bind_method(D_METHOD("get_shadow_gradient_length"), &Light2D::get_shadow_gradient_length); @@ -420,6 +434,7 @@ void Light2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_buffer_size", PROPERTY_HINT_RANGE, "32,16384,1"), "set_shadow_buffer_size", "get_shadow_buffer_size"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "shadow_gradient_length", PROPERTY_HINT_RANGE, "1,4096,0.1"), "set_shadow_gradient_length", "get_shadow_gradient_length"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "shadow_filter", PROPERTY_HINT_ENUM, "None,PCF3,PCF5,PCF9,PCF13"), "set_shadow_filter", "get_shadow_filter"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "shadow_filter_smooth", PROPERTY_HINT_RANGE, "0,64,0.1"), "set_shadow_smooth", "get_shadow_smooth"); ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_item_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_item_shadow_cull_mask", "get_item_shadow_cull_mask"); BIND_CONSTANT(MODE_ADD); @@ -449,6 +464,7 @@ Light2D::Light2D() { energy = 1.0; shadow_color = Color(0, 0, 0, 0); shadow_filter = SHADOW_FILTER_NONE; + shadow_smooth = 0; set_notify_transform(true); } diff --git a/scene/2d/light_2d.h b/scene/2d/light_2d.h index 9b09d54dd8..90e55aeda4 100644 --- a/scene/2d/light_2d.h +++ b/scene/2d/light_2d.h @@ -69,6 +69,7 @@ private: int item_mask; int item_shadow_mask; int shadow_buffer_size; + float shadow_smooth; float shadow_gradient_length; Mode mode; Ref<Texture> texture; @@ -146,6 +147,9 @@ public: void set_shadow_color(const Color &p_shadow_color); Color get_shadow_color() const; + void set_shadow_smooth(float p_amount); + float get_shadow_smooth() const; + virtual Rect2 get_item_rect() const; String get_configuration_warning() const; diff --git a/scene/2d/line_builder.cpp b/scene/2d/line_builder.cpp index add476ae0f..ea059d133d 100644 --- a/scene/2d/line_builder.cpp +++ b/scene/2d/line_builder.cpp @@ -83,8 +83,8 @@ static inline Vector2 rotate90(const Vector2 &v) { static inline Vector2 interpolate(const Rect2 &r, const Vector2 &v) { return Vector2( - Math::lerp(r.get_pos().x, r.get_pos().x + r.get_size().x, v.x), - Math::lerp(r.get_pos().y, r.get_pos().y + r.get_size().y, v.y)); + Math::lerp(r.position.x, r.position.x + r.get_size().x, v.x), + Math::lerp(r.position.y, r.position.y + r.get_size().y, v.y)); } //---------------------------------------------------------------------------- diff --git a/scene/2d/node_2d.cpp b/scene/2d/node_2d.cpp index ec286b1d54..6fe1dd73fe 100644 --- a/scene/2d/node_2d.cpp +++ b/scene/2d/node_2d.cpp @@ -76,9 +76,9 @@ void Node2D::edit_set_rect(const Rect2 &p_edit_rect) { Vector2 zero_offset; if (r.size.x != 0) - zero_offset.x = -r.pos.x / r.size.x; + zero_offset.x = -r.position.x / r.size.x; if (r.size.y != 0) - zero_offset.y = -r.pos.y / r.size.y; + zero_offset.y = -r.position.y / r.size.y; Size2 new_scale(1, 1); @@ -87,7 +87,7 @@ void Node2D::edit_set_rect(const Rect2 &p_edit_rect) { if (r.size.y != 0) new_scale.y = p_edit_rect.size.y / r.size.y; - Point2 new_pos = p_edit_rect.pos + p_edit_rect.size * zero_offset; //p_edit_rect.pos - r.pos; + Point2 new_pos = p_edit_rect.position + p_edit_rect.size * zero_offset; //p_edit_rect.pos - r.pos; Transform2D postxf; postxf.set_rotation_and_scale(angle, _scale); diff --git a/scene/2d/particles_2d.cpp b/scene/2d/particles_2d.cpp index 951861256e..27d0d07c0f 100644 --- a/scene/2d/particles_2d.cpp +++ b/scene/2d/particles_2d.cpp @@ -568,8 +568,8 @@ void Particles2D::_notification(int p_what) { if (total_frames > 1) { int frame = Math::fast_ftoi(Math::floor(p.frame * total_frames)) % total_frames; - src_rect.pos.x = size.x * (frame % h_frames); - src_rect.pos.y = size.y * (frame / h_frames); + src_rect.position.x = size.x * (frame % h_frames); + src_rect.position.y = size.y * (frame / h_frames); } Rect2 dst_rect(Point2(), size); if (flip_h) diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp index cb3e67f895..5c1c953a37 100644 --- a/scene/2d/polygon_2d.cpp +++ b/scene/2d/polygon_2d.cpp @@ -38,7 +38,7 @@ Rect2 Polygon2D::get_item_rect() const { for (int i = 0; i < l; i++) { Vector2 pos = r[i] + offset; if (i == 0) - item_rect.pos = pos; + item_rect.position = pos; else item_rect.expand_to(pos); } @@ -95,7 +95,7 @@ void Polygon2D::_notification(int p_what) { for (int i = 0; i < len; i++) { if (i == 0) - bounds.pos = points[i]; + bounds.position = points[i]; else bounds.expand_to(points[i]); if (points[i].y > highest_y) { @@ -110,10 +110,10 @@ void Polygon2D::_notification(int p_what) { Vector2 ep[7] = { Vector2(points[highest_idx].x, points[highest_idx].y + invert_border), - Vector2(bounds.pos + bounds.size), - Vector2(bounds.pos + Vector2(bounds.size.x, 0)), - Vector2(bounds.pos), - Vector2(bounds.pos + Vector2(0, bounds.size.y)), + Vector2(bounds.position + bounds.size), + Vector2(bounds.position + Vector2(bounds.size.x, 0)), + Vector2(bounds.position), + Vector2(bounds.position + Vector2(0, bounds.size.y)), Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y + invert_border), Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y), }; diff --git a/scene/2d/screen_button.cpp b/scene/2d/screen_button.cpp index f503a66208..ea68a111af 100644 --- a/scene/2d/screen_button.cpp +++ b/scene/2d/screen_button.cpp @@ -207,7 +207,7 @@ void TouchScreenButton::_input(const Ref<InputEvent> &p_event) { if ((st.is_valid() && st->is_pressed()) || sd.is_valid()) { int index = st.is_valid() ? st->get_index() : sd->get_index(); - Vector2 coord = st.is_valid() ? st->get_pos() : sd->get_pos(); + Vector2 coord = st.is_valid() ? st->get_position() : sd->get_position(); if (finger_pressed == -1 || index == finger_pressed) { @@ -254,7 +254,7 @@ void TouchScreenButton::_input(const Ref<InputEvent> &p_event) { if (!can_press) return; //already fingering - Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(st->get_pos()); + Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(st->get_position()); Rect2 item_rect = get_item_rect(); bool touched = false; diff --git a/scene/2d/sprite.cpp b/scene/2d/sprite.cpp index 1735bf3b91..c1eb90e52a 100644 --- a/scene/2d/sprite.cpp +++ b/scene/2d/sprite.cpp @@ -75,8 +75,8 @@ void Sprite::_notification(int p_what) { s = s / Size2(hframes, vframes); src_rect.size = s; - src_rect.pos.x += float(frame % hframes) * s.x; - src_rect.pos.y += float(frame / hframes) * s.y; + src_rect.position.x += float(frame % hframes) * s.x; + src_rect.position.y += float(frame / hframes) * s.y; } Point2 ofs = offset; diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index 080276eb25..eebf0efd1e 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -370,14 +370,14 @@ void TileMap::_update_dirty_quadrants() { s = tex->get_size(); else { s = r.size; - r.pos.x += fp_adjust; - r.pos.y += fp_adjust; + r.position.x += fp_adjust; + r.position.y += fp_adjust; r.size.x -= fp_adjust * 2.0; r.size.y -= fp_adjust * 2.0; } Rect2 rect; - rect.pos = offset.floor(); + rect.position = offset.floor(); rect.size = s; if (rect.size.y > rect.size.x) { @@ -406,39 +406,39 @@ void TileMap::_update_dirty_quadrants() { Vector2 center_ofs; if (tile_origin == TILE_ORIGIN_TOP_LEFT) { - rect.pos += tile_ofs; + rect.position += tile_ofs; } else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) { - rect.pos += tile_ofs; + rect.position += tile_ofs; if (c.transpose) { if (c.flip_h) - rect.pos.x -= cell_size.x; + rect.position.x -= cell_size.x; else - rect.pos.x += cell_size.x; + rect.position.x += cell_size.x; } else { if (c.flip_v) - rect.pos.y -= cell_size.y; + rect.position.y -= cell_size.y; else - rect.pos.y += cell_size.y; + rect.position.y += cell_size.y; } } else if (tile_origin == TILE_ORIGIN_CENTER) { - rect.pos += tcenter; + rect.position += tcenter; Vector2 center = (s / 2) - tile_ofs; center_ofs = tcenter - (s / 2); if (c.flip_h) - rect.pos.x -= s.x - center.x; + rect.position.x -= s.x - center.x; else - rect.pos.x -= center.x; + rect.position.x -= center.x; if (c.flip_v) - rect.pos.y -= s.y - center.y; + rect.position.y -= s.y - center.y; else - rect.pos.y -= center.y; + rect.position.y -= center.y; } Color modulate = tile_set->tile_get_modulate(c.id); @@ -549,7 +549,7 @@ void TileMap::_recompute_rect_cache() { for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) { Rect2 r; - r.pos = _map_to_world(E->key().x * _get_quadrant_size(), E->key().y * _get_quadrant_size()); + r.position = _map_to_world(E->key().x * _get_quadrant_size(), E->key().y * _get_quadrant_size()); r.expand_to(_map_to_world(E->key().x * _get_quadrant_size() + _get_quadrant_size(), E->key().y * _get_quadrant_size())); r.expand_to(_map_to_world(E->key().x * _get_quadrant_size() + _get_quadrant_size(), E->key().y * _get_quadrant_size() + _get_quadrant_size())); r.expand_to(_map_to_world(E->key().x * _get_quadrant_size(), E->key().y * _get_quadrant_size() + _get_quadrant_size())); diff --git a/scene/3d/collision_polygon.cpp b/scene/3d/collision_polygon.cpp index a9a693f370..d9321f7134 100644 --- a/scene/3d/collision_polygon.cpp +++ b/scene/3d/collision_polygon.cpp @@ -208,7 +208,7 @@ void CollisionPolygon::set_polygon(const Vector<Point2> &p_polygon) { aabb = Rect3(Vector3(-1, -1, -1), Vector3(2, 2, 2)); } else { - aabb.pos -= aabb.size * 0.3; + aabb.position -= aabb.size * 0.3; aabb.size += aabb.size * 0.6; } _update_parent(); diff --git a/scene/3d/gi_probe.cpp b/scene/3d/gi_probe.cpp index 96311236ef..adca1492c3 100644 --- a/scene/3d/gi_probe.cpp +++ b/scene/3d/gi_probe.cpp @@ -564,7 +564,7 @@ void GIProbe::_plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, cons Vector3 ofs_j = float(j) * t2; - Vector3 from = p_aabb.pos + ofs_i + ofs_j; + Vector3 from = p_aabb.position + ofs_i + ofs_j; Vector3 to = from + t1 + t2 + axis * p_aabb.size[closest_axis]; Vector3 half = (to - from) * 0.5; @@ -619,7 +619,7 @@ void GIProbe::_plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, cons //could not in any way get texture information.. so use closest point to center Face3 f(p_vtx[0], p_vtx[1], p_vtx[2]); - Vector3 inters = f.get_closest_point_to(p_aabb.pos + p_aabb.size * 0.5); + Vector3 inters = f.get_closest_point_to(p_aabb.position + p_aabb.size * 0.5); Vector2 uv = get_uv(inters, p_vtx, p_uv); @@ -700,15 +700,15 @@ void GIProbe::_plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, cons int nz = p_z; if (i & 1) { - aabb.pos.x += aabb.size.x; + aabb.position.x += aabb.size.x; nx += half; } if (i & 2) { - aabb.pos.y += aabb.size.y; + aabb.position.y += aabb.size.y; ny += half; } if (i & 4) { - aabb.pos.z += aabb.size.z; + aabb.position.z += aabb.size.z; nz += half; } //make sure to not plot beyond limits @@ -720,7 +720,7 @@ void GIProbe::_plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, cons //test_aabb.grow_by(test_aabb.get_longest_axis_size()*0.05); //grow a bit to avoid numerical error in real-time Vector3 qsize = test_aabb.size * 0.5; //quarter size, for fast aabb test - if (!fast_tri_box_overlap(test_aabb.pos + qsize, qsize, p_vtx)) { + if (!fast_tri_box_overlap(test_aabb.position + qsize, qsize, p_vtx)) { //if (!Face3(p_vtx[0],p_vtx[1],p_vtx[2]).intersects_aabb2(aabb)) { //does not fit in child, go on continue; @@ -885,7 +885,7 @@ Vector<Color> GIProbe::_get_bake_texture(Ref<Image> p_image, const Color &p_colo Vector<Color> ret; - if (p_image.is_null()) { + if (p_image.is_null() || p_image->empty()) { ret.resize(bake_texture_size * bake_texture_size); for (int i = 0; i < bake_texture_size * bake_texture_size; i++) { @@ -894,9 +894,11 @@ Vector<Color> GIProbe::_get_bake_texture(Ref<Image> p_image, const Color &p_colo return ret; } + p_image = p_image->duplicate(); if (p_image->is_compressed()) { print_line("DECOMPRESSING!!!!"); + p_image->decompress(); } p_image->convert(Image::FORMAT_RGBA8); @@ -907,10 +909,11 @@ Vector<Color> GIProbe::_get_bake_texture(Ref<Image> p_image, const Color &p_colo for (int i = 0; i < bake_texture_size * bake_texture_size; i++) { Color c; - c.r = r[i * 4 + 0] / 255.0; - c.g = r[i * 4 + 1] / 255.0; - c.b = r[i * 4 + 2] / 255.0; + c.r = (r[i * 4 + 0] / 255.0) * p_color.r; + c.g = (r[i * 4 + 1] / 255.0) * p_color.g; + c.b = (r[i * 4 + 2] / 255.0) * p_color.b; c.a = r[i * 4 + 3] / 255.0; + ret[i] = c; } @@ -970,7 +973,7 @@ GIProbe::Baker::MaterialCache GIProbe::_get_material_cache(Ref<Material> p_mater return mc; } -void GIProbe::_plot_mesh(const Transform &p_xform, Ref<Mesh> &p_mesh, Baker *p_baker, const Vector<Ref<Material> > &p_materials, const Ref<Material> &p_override_material) { +void GIProbe::_plot_mesh(const Transform &p_xform, Ref<ArrayMesh> &p_mesh, Baker *p_baker, const Vector<Ref<Material> > &p_materials, const Ref<Material> &p_override_material) { for (int i = 0; i < p_mesh->get_surface_count(); i++) { @@ -1064,7 +1067,7 @@ void GIProbe::_find_meshes(Node *p_at_node, Baker *p_baker) { MeshInstance *mi = p_at_node->cast_to<MeshInstance>(); if (mi && mi->get_flag(GeometryInstance::FLAG_USE_BAKED_LIGHT)) { - Ref<Mesh> mesh = mi->get_mesh(); + Ref<ArrayMesh> mesh = mi->get_mesh(); if (mesh.is_valid()) { Rect3 aabb = mesh->get_aabb(); @@ -1091,7 +1094,7 @@ void GIProbe::_find_meshes(Node *p_at_node, Baker *p_baker) { for (int i = 0; i < meshes.size(); i += 2) { Transform mxf = meshes[i]; - Ref<Mesh> mesh = meshes[i + 1]; + Ref<ArrayMesh> mesh = meshes[i + 1]; if (!mesh.is_valid()) continue; @@ -1152,7 +1155,7 @@ void GIProbe::bake(Node *p_from_node, bool p_create_visual_debug) { Transform to_bounds; to_bounds.basis.scale(Vector3(baker.po2_bounds.size[longest_axis], baker.po2_bounds.size[longest_axis], baker.po2_bounds.size[longest_axis])); - to_bounds.origin = baker.po2_bounds.pos; + to_bounds.origin = baker.po2_bounds.position; Transform to_grid; to_grid.basis.scale(Vector3(baker.axis_cell_size[longest_axis], baker.axis_cell_size[longest_axis], baker.axis_cell_size[longest_axis])); @@ -1271,12 +1274,13 @@ void GIProbe::_debug_mesh(int p_idx, int p_level, const Rect3 &p_aabb, Ref<Multi if (p_level == p_baker->cell_subdiv - 1) { - Vector3 center = p_aabb.pos + p_aabb.size * 0.5; + Vector3 center = p_aabb.position + p_aabb.size * 0.5; Transform xform; xform.origin = center; xform.basis.scale(p_aabb.size * 0.5); p_multimesh->set_instance_transform(idx, xform); Color col = Color(p_baker->bake_cells[p_idx].albedo[0], p_baker->bake_cells[p_idx].albedo[1], p_baker->bake_cells[p_idx].albedo[2]); + //Color col = Color(p_baker->bake_cells[p_idx].emission[0], p_baker->bake_cells[p_idx].emission[1], p_baker->bake_cells[p_idx].emission[2]); p_multimesh->set_instance_color(idx, col); idx++; @@ -1292,11 +1296,11 @@ void GIProbe::_debug_mesh(int p_idx, int p_level, const Rect3 &p_aabb, Ref<Multi aabb.size *= 0.5; if (i & 1) - aabb.pos.x += aabb.size.x; + aabb.position.x += aabb.size.x; if (i & 2) - aabb.pos.y += aabb.size.y; + aabb.position.y += aabb.size.y; if (i & 4) - aabb.pos.z += aabb.size.z; + aabb.position.z += aabb.size.z; _debug_mesh(p_baker->bake_cells[p_idx].childs[i], p_level + 1, aabb, p_multimesh, idx, p_baker); } @@ -1313,7 +1317,7 @@ void GIProbe::_create_debug_mesh(Baker *p_baker) { print_line("leaf voxels: " + itos(p_baker->leaf_voxel_count)); mm->set_instance_count(p_baker->leaf_voxel_count); - Ref<Mesh> mesh; + Ref<ArrayMesh> mesh; mesh.instance(); { @@ -1464,7 +1468,7 @@ GIProbe::GIProbe() { subdiv = SUBDIV_128; dynamic_range = 4; energy = 1.0; - bias = 0.4; + bias = 1.8; propagation = 1.0; extents = Vector3(10, 10, 10); color_scan_cell_width = 4; diff --git a/scene/3d/gi_probe.h b/scene/3d/gi_probe.h index 3b05d9952b..b5ee86455e 100644 --- a/scene/3d/gi_probe.h +++ b/scene/3d/gi_probe.h @@ -145,7 +145,7 @@ private: struct PlotMesh { Ref<Material> override_material; Vector<Ref<Material> > instance_materials; - Ref<Mesh> mesh; + Ref<ArrayMesh> mesh; Transform local_xform; }; @@ -173,7 +173,7 @@ private: Vector<Color> _get_bake_texture(Ref<Image> p_image, const Color &p_color); Baker::MaterialCache _get_material_cache(Ref<Material> p_material, Baker *p_baker); void _plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, const Vector3 *p_vtx, const Vector2 *p_uv, const Baker::MaterialCache &p_material, const Rect3 &p_aabb, Baker *p_baker); - void _plot_mesh(const Transform &p_xform, Ref<Mesh> &p_mesh, Baker *p_baker, const Vector<Ref<Material> > &p_materials, const Ref<Material> &p_override_material); + void _plot_mesh(const Transform &p_xform, Ref<ArrayMesh> &p_mesh, Baker *p_baker, const Vector<Ref<Material> > &p_materials, const Ref<Material> &p_override_material); void _find_meshes(Node *p_at_node, Baker *p_baker); void _fixup_plot(int p_idx, int p_level, int p_x, int p_y, int p_z, Baker *p_baker); diff --git a/scene/3d/immediate_geometry.cpp b/scene/3d/immediate_geometry.cpp index 8c1ad1d052..128c8b44c3 100644 --- a/scene/3d/immediate_geometry.cpp +++ b/scene/3d/immediate_geometry.cpp @@ -65,7 +65,7 @@ void ImmediateGeometry::add_vertex(const Vector3 &p_vertex) { VS::get_singleton()->immediate_vertex(im, p_vertex); if (empty) { - aabb.pos = p_vertex; + aabb.position = p_vertex; aabb.size = Vector3(); empty = false; } else { diff --git a/scene/3d/light.cpp b/scene/3d/light.cpp index 6ab65d3994..b4a4cf70bb 100644 --- a/scene/3d/light.cpp +++ b/scene/3d/light.cpp @@ -261,8 +261,8 @@ Light::Light(VisualServer::LightType p_type) { set_param(PARAM_SHADOW_SPLIT_1_OFFSET, 0.1); set_param(PARAM_SHADOW_SPLIT_2_OFFSET, 0.2); set_param(PARAM_SHADOW_SPLIT_3_OFFSET, 0.5); - set_param(PARAM_SHADOW_NORMAL_BIAS, 0.1); - set_param(PARAM_SHADOW_BIAS, 0.1); + set_param(PARAM_SHADOW_NORMAL_BIAS, 0.0); + set_param(PARAM_SHADOW_BIAS, 0.); set_param(PARAM_SHADOW_BIAS_SPLIT_SCALE, 0.1); } @@ -328,6 +328,7 @@ void DirectionalLight::_bind_methods() { DirectionalLight::DirectionalLight() : Light(VisualServer::LIGHT_DIRECTIONAL) { + set_param(PARAM_SHADOW_NORMAL_BIAS, 0.1); set_shadow_mode(SHADOW_PARALLEL_4_SPLITS); blend_splits = false; } @@ -371,7 +372,7 @@ void OmniLight::_bind_methods() { OmniLight::OmniLight() : Light(VisualServer::LIGHT_OMNI) { - set_shadow_mode(SHADOW_DUAL_PARABOLOID); + set_shadow_mode(SHADOW_CUBE); set_shadow_detail(SHADOW_DETAIL_HORIZONTAL); } diff --git a/scene/3d/listener.cpp b/scene/3d/listener.cpp index 148afbffa2..c7d3bac2f8 100644 --- a/scene/3d/listener.cpp +++ b/scene/3d/listener.cpp @@ -152,7 +152,7 @@ bool Listener::_can_gizmo_scale() const { } RES Listener::_get_gizmo_geometry() const { - Ref<Mesh> mesh = memnew(Mesh); + Ref<ArrayMesh> mesh = memnew(ArrayMesh); return mesh; } diff --git a/scene/3d/navigation_mesh.cpp b/scene/3d/navigation_mesh.cpp index 13fd852fe7..82f6f665db 100644 --- a/scene/3d/navigation_mesh.cpp +++ b/scene/3d/navigation_mesh.cpp @@ -187,7 +187,7 @@ Ref<Mesh> NavigationMesh::get_debug_mesh() { } } - debug_mesh = Ref<Mesh>(memnew(Mesh)); + debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); Array arr; arr.resize(Mesh::ARRAY_MAX); diff --git a/scene/3d/navigation_mesh.h b/scene/3d/navigation_mesh.h index c8f6d936aa..e5a3dc7b43 100644 --- a/scene/3d/navigation_mesh.h +++ b/scene/3d/navigation_mesh.h @@ -44,7 +44,7 @@ class NavigationMesh : public Resource { Vector<int> indices; }; Vector<Polygon> polygons; - Ref<Mesh> debug_mesh; + Ref<ArrayMesh> debug_mesh; struct _EdgeKey { diff --git a/scene/3d/ray_cast.cpp b/scene/3d/ray_cast.cpp index d24aa6ae2a..345afd3edf 100644 --- a/scene/3d/ray_cast.cpp +++ b/scene/3d/ray_cast.cpp @@ -266,7 +266,7 @@ void RayCast::_create_debug_shape() { line_material->set_albedo(Color(1.0, 0.8, 0.6)); } - Ref<Mesh> mesh = memnew(Mesh); + Ref<ArrayMesh> mesh = memnew(ArrayMesh); MeshInstance *mi = memnew(MeshInstance); mi->set_mesh(mesh); @@ -287,7 +287,7 @@ void RayCast::_update_debug_shape() { if (!mi->get_mesh().is_valid()) return; - Ref<Mesh> mesh = mi->get_mesh(); + Ref<ArrayMesh> mesh = mi->get_mesh(); if (mesh->get_surface_count() > 0) mesh->surface_remove(0); diff --git a/scene/3d/reflection_probe.cpp b/scene/3d/reflection_probe.cpp index 845245b090..86a62bec97 100644 --- a/scene/3d/reflection_probe.cpp +++ b/scene/3d/reflection_probe.cpp @@ -181,7 +181,7 @@ ReflectionProbe::UpdateMode ReflectionProbe::get_update_mode() const { Rect3 ReflectionProbe::get_aabb() const { Rect3 aabb; - aabb.pos = -origin_offset; + aabb.position = -origin_offset; aabb.size = origin_offset + extents; return aabb; } diff --git a/scene/3d/sprite_3d.cpp b/scene/3d/sprite_3d.cpp index e97b22f9e0..a95bb4a67f 100644 --- a/scene/3d/sprite_3d.cpp +++ b/scene/3d/sprite_3d.cpp @@ -335,8 +335,8 @@ void Sprite3D::_draw() { s = s / Size2i(hframes, vframes); src_rect.size = s; - src_rect.pos.x += (frame % hframes) * s.x; - src_rect.pos.y += (frame / hframes) * s.y; + src_rect.position.x += (frame % hframes) * s.x; + src_rect.position.y += (frame / hframes) * s.y; } Point2i ofs = get_offset(); @@ -360,17 +360,17 @@ void Sprite3D::_draw() { Vector2 vertices[4] = { - (final_rect.pos + Vector2(0, final_rect.size.y)) * pixel_size, - (final_rect.pos + final_rect.size) * pixel_size, - (final_rect.pos + Vector2(final_rect.size.x, 0)) * pixel_size, - final_rect.pos * pixel_size, + (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size, + (final_rect.position + final_rect.size) * pixel_size, + (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size, + final_rect.position * pixel_size, }; Vector2 uvs[4] = { - final_src_rect.pos / tsize, - (final_src_rect.pos + Vector2(final_src_rect.size.x, 0)) / tsize, - (final_src_rect.pos + final_src_rect.size) / tsize, - (final_src_rect.pos + Vector2(0, final_src_rect.size.y)) / tsize, + final_src_rect.position / tsize, + (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / tsize, + (final_src_rect.position + final_src_rect.size) / tsize, + (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / tsize, }; if (is_flipped_h()) { @@ -421,7 +421,7 @@ void Sprite3D::_draw() { vtx[y_axis] = vertices[i][1]; VS::get_singleton()->immediate_vertex(immediate, vtx); if (i == 0) { - aabb.pos = vtx; + aabb.position = vtx; aabb.size = Vector3(); } else { aabb.expand_to(vtx); @@ -861,17 +861,17 @@ void AnimatedSprite3D::_draw() { Vector2 vertices[4] = { - (final_rect.pos + Vector2(0, final_rect.size.y)) * pixel_size, - (final_rect.pos + final_rect.size) * pixel_size, - (final_rect.pos + Vector2(final_rect.size.x, 0)) * pixel_size, - final_rect.pos * pixel_size, + (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size, + (final_rect.position + final_rect.size) * pixel_size, + (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size, + final_rect.position * pixel_size, }; Vector2 uvs[4] = { - final_src_rect.pos / tsize, - (final_src_rect.pos + Vector2(final_src_rect.size.x, 0)) / tsize, - (final_src_rect.pos + final_src_rect.size) / tsize, - (final_src_rect.pos + Vector2(0, final_src_rect.size.y)) / tsize, + final_src_rect.position / tsize, + (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / tsize, + (final_src_rect.position + final_src_rect.size) / tsize, + (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / tsize, }; if (is_flipped_h()) { @@ -922,7 +922,7 @@ void AnimatedSprite3D::_draw() { vtx[y_axis] = vertices[i][1]; VS::get_singleton()->immediate_vertex(immediate, vtx); if (i == 0) { - aabb.pos = vtx; + aabb.position = vtx; aabb.size = Vector3(); } else { aabb.expand_to(vtx); diff --git a/scene/animation/animation_player.cpp b/scene/animation/animation_player.cpp index c4ea42d461..543b64bd15 100644 --- a/scene/animation/animation_player.cpp +++ b/scene/animation/animation_player.cpp @@ -202,6 +202,7 @@ void AnimationPlayer::_notification(int p_what) { if (!get_tree()->is_editor_hint() && animation_set.has(autoplay)) { play(autoplay); set_autoplay(""); //this line is the fix for autoplay issues with animatio + _animation_process(0); } } break; case NOTIFICATION_INTERNAL_PROCESS: { diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp index 2a6ffb5628..67920d177e 100644 --- a/scene/animation/tween.cpp +++ b/scene/animation/tween.cpp @@ -421,9 +421,9 @@ Variant Tween::_run_equation(InterpolateData &p_data) { Rect3 d = delta_val; Rect3 r; - APPLY_EQUATION(pos.x); - APPLY_EQUATION(pos.y); - APPLY_EQUATION(pos.z); + APPLY_EQUATION(position.x); + APPLY_EQUATION(position.y); + APPLY_EQUATION(position.z); APPLY_EQUATION(size.x); APPLY_EQUATION(size.y); APPLY_EQUATION(size.z); @@ -963,7 +963,7 @@ bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final case Variant::RECT3: { Rect3 i = initial_val; Rect3 f = final_val; - delta_val = Rect3(f.pos - i.pos, f.size - i.size); + delta_val = Rect3(f.position - i.position, f.size - i.size); } break; case Variant::TRANSFORM: { Transform i = initial_val; diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp index 318db8458b..c1dbc82f3c 100644 --- a/scene/gui/base_button.cpp +++ b/scene/gui/base_button.cpp @@ -162,7 +162,7 @@ void BaseButton::_gui_input(Ref<InputEvent> p_event) { if (mm.is_valid()) { if (status.press_attempt && status.pressing_button == 0) { bool last_press_inside = status.pressing_inside; - status.pressing_inside = has_point(mm->get_pos()); + status.pressing_inside = has_point(mm->get_position()); if (last_press_inside != status.pressing_inside) update(); } diff --git a/scene/gui/button_array.cpp b/scene/gui/button_array.cpp index 0d518059c8..1616272e66 100644 --- a/scene/gui/button_array.cpp +++ b/scene/gui/button_array.cpp @@ -236,8 +236,8 @@ void ButtonArray::_notification(int p_what) { } Rect2 r; - r.pos[orientation] = ofs; - r.pos[!orientation] = 0; + r.position[orientation] = ofs; + r.position[!orientation] = 0; r.size[orientation] = s; r.size[!orientation] = op_size; @@ -272,10 +272,10 @@ void ButtonArray::_notification(int p_what) { Point2 text_ofs = ((r.size - ssize - sbsize) / 2.0 + Point2(0, f->get_ascent())).floor() + sbofs; if (buttons[i].icon.is_valid()) { - draw_texture(buttons[i].icon, r.pos + Point2(text_ofs.x, Math::floor((r.size.height - buttons[i].icon->get_height()) / 2.0))); + draw_texture(buttons[i].icon, r.position + Point2(text_ofs.x, Math::floor((r.size.height - buttons[i].icon->get_height()) / 2.0))); text_ofs.x += buttons[i].icon->get_width() + icon_sep; } - draw_string(f, text_ofs + r.pos, buttons[i].xl_text, c); + draw_string(f, text_ofs + r.position, buttons[i].xl_text, c); buttons[i]._pos_cache = ofs; buttons[i]._size_cache = s; @@ -313,7 +313,7 @@ void ButtonArray::_gui_input(const Ref<InputEvent> &p_event) { if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { - int ofs = orientation == HORIZONTAL ? mb->get_pos().x : mb->get_pos().y; + int ofs = orientation == HORIZONTAL ? mb->get_position().x : mb->get_position().y; for (int i = 0; i < buttons.size(); i++) { @@ -330,7 +330,7 @@ void ButtonArray::_gui_input(const Ref<InputEvent> &p_event) { if (mm.is_valid()) { - int ofs = orientation == HORIZONTAL ? mm->get_pos().x : mm->get_pos().y; + int ofs = orientation == HORIZONTAL ? mm->get_position().x : mm->get_position().y; int new_hover = -1; for (int i = 0; i < buttons.size(); i++) { diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp index 662ce63946..f0e486aa50 100644 --- a/scene/gui/color_picker.cpp +++ b/scene/gui/color_picker.cpp @@ -304,8 +304,8 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &ev) { if (bev.is_valid()) { if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) { changing_color = true; - float x = CLAMP((float)bev->get_pos().x, 0, 256); - float y = CLAMP((float)bev->get_pos().y, 0, 256); + float x = CLAMP((float)bev->get_position().x, 0, 256); + float y = CLAMP((float)bev->get_position().y, 0, 256); s = x / 256; v = 1.0 - y / 256.0; color.set_hsv(h, s, v, color.a); @@ -323,8 +323,8 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &ev) { if (mev.is_valid()) { if (!changing_color) return; - float x = CLAMP((float)mev->get_pos().x, 0, 256); - float y = CLAMP((float)mev->get_pos().y, 0, 256); + float x = CLAMP((float)mev->get_position().x, 0, 256); + float y = CLAMP((float)mev->get_position().y, 0, 256); s = x / 256; v = 1.0 - y / 256.0; color.set_hsv(h, s, v, color.a); @@ -343,7 +343,7 @@ void ColorPicker::_w_input(const Ref<InputEvent> &ev) { if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) { changing_color = true; - h = 1 - ((float)bev->get_pos().y) / 256.0; + h = 1 - ((float)bev->get_position().y) / 256.0; } else { changing_color = false; @@ -361,7 +361,7 @@ void ColorPicker::_w_input(const Ref<InputEvent> &ev) { if (!changing_color) return; - float y = CLAMP((float)mev->get_pos().y, 0, 256); + float y = CLAMP((float)mev->get_position().y, 0, 256); h = 1.0 - y / 256.0; color.set_hsv(h, s, v, color.a); last_hsv = color; @@ -378,10 +378,10 @@ void ColorPicker::_preset_input(const Ref<InputEvent> &ev) { if (bev.is_valid()) { if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) { - int index = bev->get_pos().x / (preset->get_size().x / presets.size()); + int index = bev->get_position().x / (preset->get_size().x / presets.size()); set_pick_color(presets[index]); } else if (bev->is_pressed() && bev->get_button_index() == BUTTON_RIGHT) { - int index = bev->get_pos().x / (preset->get_size().x / presets.size()); + int index = bev->get_position().x / (preset->get_size().x / presets.size()); presets.erase(presets[index]); _update_presets(); bt_add_preset->show(); @@ -394,7 +394,7 @@ void ColorPicker::_preset_input(const Ref<InputEvent> &ev) { if (mev.is_valid()) { - int index = mev->get_pos().x * presets.size(); + int index = mev->get_position().x * presets.size(); if (preset->get_size().x != 0) { index /= preset->get_size().x; } @@ -422,7 +422,7 @@ void ColorPicker::_screen_input(const Ref<InputEvent> &ev) { if (mev.is_valid()) { Viewport *r = get_tree()->get_root(); - if (!r->get_visible_rect().has_point(Point2(mev->get_global_pos().x, mev->get_global_pos().y))) + if (!r->get_visible_rect().has_point(Point2(mev->get_global_position().x, mev->get_global_position().y))) return; Ref<Image> img = r->get_screen_capture(); if (!img.is_null()) { @@ -431,7 +431,7 @@ void ColorPicker::_screen_input(const Ref<InputEvent> &ev) { } if (last_capture.is_valid() && !last_capture->empty()) { int pw = last_capture->get_format() == Image::FORMAT_RGBA8 ? 4 : 3; - int ofs = (mev->get_global_pos().y * last_capture->get_width() + mev->get_global_pos().x) * pw; + int ofs = (mev->get_global_position().y * last_capture->get_width() + mev->get_global_position().x) * pw; PoolVector<uint8_t>::Read r = last_capture->get_data().read(); diff --git a/scene/gui/color_ramp_edit.cpp b/scene/gui/color_ramp_edit.cpp index 8c47f8559a..33e4ce0403 100644 --- a/scene/gui/color_ramp_edit.cpp +++ b/scene/gui/color_ramp_edit.cpp @@ -88,14 +88,14 @@ void ColorRampEdit::_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> mb = p_event; //Show color picker on double click. if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_doubleclick() && mb->is_pressed()) { - grabbed = _get_point_from_pos(mb->get_pos().x); + grabbed = _get_point_from_pos(mb->get_position().x); _show_color_picker(); accept_event(); } //Delete point on right click if (mb.is_valid() && mb->get_button_index() == 2 && mb->is_pressed()) { - grabbed = _get_point_from_pos(mb->get_pos().x); + grabbed = _get_point_from_pos(mb->get_position().x); if (grabbed != -1) { points.remove(grabbed); grabbed = -1; @@ -109,7 +109,7 @@ void ColorRampEdit::_gui_input(const Ref<InputEvent> &p_event) { //Hold alt key to duplicate selected color if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed() && mb->get_alt()) { - int x = mb->get_pos().x; + int x = mb->get_position().x; grabbed = _get_point_from_pos(x); if (grabbed != -1) { @@ -134,7 +134,7 @@ void ColorRampEdit::_gui_input(const Ref<InputEvent> &p_event) { if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) { update(); - int x = mb->get_pos().x; + int x = mb->get_position().x; int total_w = get_size().width - get_size().height - 3; //Check if color selector was clicked. @@ -214,7 +214,7 @@ void ColorRampEdit::_gui_input(const Ref<InputEvent> &p_event) { int total_w = get_size().width - get_size().height - 3; - int x = mm->get_pos().x; + int x = mm->get_position().x; float newofs = CLAMP(x / float(total_w), 0, 1); diff --git a/scene/gui/container.cpp b/scene/gui/container.cpp index c428d524a4..92bb5d43a7 100644 --- a/scene/gui/container.cpp +++ b/scene/gui/container.cpp @@ -96,18 +96,18 @@ void Container::fit_child_in_rect(Control *p_child, const Rect2 &p_rect) { if (!(p_child->get_h_size_flags() & SIZE_FILL)) { r.size.x = minsize.x; - r.pos.x += Math::floor((p_rect.size.x - minsize.x) / 2); + r.position.x += Math::floor((p_rect.size.x - minsize.x) / 2); } if (!(p_child->get_v_size_flags() & SIZE_FILL)) { r.size.y = minsize.y; - r.pos.y += Math::floor((p_rect.size.y - minsize.y) / 2); + r.position.y += Math::floor((p_rect.size.y - minsize.y) / 2); } for (int i = 0; i < 4; i++) p_child->set_anchor(Margin(i), ANCHOR_BEGIN); - p_child->set_position(r.pos); + p_child->set_position(r.position); p_child->set_size(r.size); p_child->set_rotation(0); p_child->set_scale(Vector2(1, 1)); diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 83fa20bee5..45485b768e 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -58,7 +58,7 @@ void Control::edit_set_state(const Variant &p_state) { Dictionary s = p_state; Rect2 state = s["rect"]; - set_position(state.pos); + set_position(state.position); set_size(state.size); set_rotation(s["rot"]); set_scale(s["scale"]); @@ -94,15 +94,15 @@ void Control::edit_set_rect(const Rect2 &p_edit_rect) { Transform2D postxf; postxf.set_rotation_and_scale(data.rotation, data.scale); - Vector2 new_pos = postxf.xform(p_edit_rect.pos); + Vector2 new_pos = postxf.xform(p_edit_rect.position); Vector2 pos = get_position() + new_pos; Rect2 new_rect = get_rect(); - new_rect.pos = pos.snapped(Vector2(1, 1)); + new_rect.position = pos.snapped(Vector2(1, 1)); new_rect.size = p_edit_rect.size.snapped(Vector2(1, 1)); - set_position(new_rect.pos); + set_position(new_rect.position); set_size(new_rect.size); } @@ -1184,6 +1184,7 @@ Size2 Control::get_parent_area_size() const { parent_size = get_viewport()->get_visible_rect().size; } + return parent_size; } @@ -1477,7 +1478,7 @@ Rect2 Control::get_global_rect() const { Rect2 Control::get_window_rect() const { ERR_FAIL_COND_V(!is_inside_tree(), Rect2()); Rect2 gr = get_global_rect(); - gr.pos += get_viewport()->get_visible_rect().pos; + gr.position += get_viewport()->get_visible_rect().position; return gr; } @@ -1949,10 +1950,10 @@ Control *Control::_get_focus_neighbour(Margin p_margin, int p_count) { Transform2D xform = get_global_transform(); Rect2 rect = get_item_rect(); - points[0] = xform.xform(rect.pos); - points[1] = xform.xform(rect.pos + Point2(rect.size.x, 0)); - points[2] = xform.xform(rect.pos + rect.size); - points[3] = xform.xform(rect.pos + Point2(0, rect.size.y)); + points[0] = xform.xform(rect.position); + points[1] = xform.xform(rect.position + Point2(rect.size.x, 0)); + points[2] = xform.xform(rect.position + rect.size); + points[3] = xform.xform(rect.position + Point2(0, rect.size.y)); const Vector2 dir[4] = { Vector2(-1, 0), @@ -2008,10 +2009,10 @@ void Control::_window_find_focus_neighbour(const Vector2 &p_dir, Node *p_at, con Transform2D xform = c->get_global_transform(); Rect2 rect = c->get_item_rect(); - points[0] = xform.xform(rect.pos); - points[1] = xform.xform(rect.pos + Point2(rect.size.x, 0)); - points[2] = xform.xform(rect.pos + rect.size); - points[3] = xform.xform(rect.pos + Point2(0, rect.size.y)); + points[0] = xform.xform(rect.position); + points[1] = xform.xform(rect.position + Point2(rect.size.x, 0)); + points[2] = xform.xform(rect.position + rect.size); + points[3] = xform.xform(rect.position + Point2(0, rect.size.y)); float min = 1e7; diff --git a/scene/gui/dialogs.cpp b/scene/gui/dialogs.cpp index 627bc96fb1..60d1350405 100644 --- a/scene/gui/dialogs.cpp +++ b/scene/gui/dialogs.cpp @@ -90,15 +90,15 @@ bool WindowDialog::has_point(const Point2 &p_point) const { // Enlarge upwards for title bar. int title_height = get_constant("title_height", "WindowDialog"); - r.pos.y -= title_height; + r.position.y -= title_height; r.size.y += title_height; // Inflate by the resizable border thickness. if (resizable) { int scaleborder_size = get_constant("scaleborder_size", "WindowDialog"); - r.pos.x -= scaleborder_size; + r.position.x -= scaleborder_size; r.size.width += scaleborder_size * 2; - r.pos.y -= scaleborder_size; + r.position.y -= scaleborder_size; r.size.height += scaleborder_size * 2; } @@ -113,7 +113,7 @@ void WindowDialog::_gui_input(const Ref<InputEvent> &p_event) { if (mb->is_pressed()) { // Begin a possible dragging operation. - drag_type = _drag_hit_test(Point2(mb->get_pos().x, mb->get_pos().y)); + drag_type = _drag_hit_test(Point2(mb->get_position().x, mb->get_position().y)); if (drag_type != DRAG_NONE) drag_offset = get_global_mouse_position() - get_position(); drag_offset_far = get_position() + get_size() - get_global_mouse_position(); @@ -131,7 +131,7 @@ void WindowDialog::_gui_input(const Ref<InputEvent> &p_event) { // Update the cursor while moving along the borders. CursorShape cursor = CURSOR_ARROW; if (resizable) { - int preview_drag_type = _drag_hit_test(Point2(mm->get_pos().x, mm->get_pos().y)); + int preview_drag_type = _drag_hit_test(Point2(mm->get_position().x, mm->get_position().y)); switch (preview_drag_type) { case DRAG_RESIZE_TOP: case DRAG_RESIZE_BOTTOM: @@ -162,28 +162,28 @@ void WindowDialog::_gui_input(const Ref<InputEvent> &p_event) { Size2 min_size = get_minimum_size(); if (drag_type == DRAG_MOVE) { - rect.pos = global_pos - drag_offset; + rect.position = global_pos - drag_offset; } else { if (drag_type & DRAG_RESIZE_TOP) { - int bottom = rect.pos.y + rect.size.height; + int bottom = rect.position.y + rect.size.height; int max_y = bottom - min_size.height; - rect.pos.y = MIN(global_pos.y - drag_offset.y, max_y); - rect.size.height = bottom - rect.pos.y; + rect.position.y = MIN(global_pos.y - drag_offset.y, max_y); + rect.size.height = bottom - rect.position.y; } else if (drag_type & DRAG_RESIZE_BOTTOM) { - rect.size.height = global_pos.y - rect.pos.y + drag_offset_far.y; + rect.size.height = global_pos.y - rect.position.y + drag_offset_far.y; } if (drag_type & DRAG_RESIZE_LEFT) { - int right = rect.pos.x + rect.size.width; + int right = rect.position.x + rect.size.width; int max_x = right - min_size.width; - rect.pos.x = MIN(global_pos.x - drag_offset.x, max_x); - rect.size.width = right - rect.pos.x; + rect.position.x = MIN(global_pos.x - drag_offset.x, max_x); + rect.size.width = right - rect.position.x; } else if (drag_type & DRAG_RESIZE_RIGHT) { - rect.size.width = global_pos.x - rect.pos.x + drag_offset_far.x; + rect.size.width = global_pos.x - rect.position.x + drag_offset_far.x; } } set_size(rect.size); - set_position(rect.pos); + set_position(rect.position); } } } diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index c52cdd9325..8d085d5399 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -168,24 +168,24 @@ void GraphEdit::_update_scroll() { continue; Rect2 r; - r.pos = gn->get_offset() * zoom; + r.position = gn->get_offset() * zoom; r.size = gn->get_size() * zoom; screen = screen.merge(r); } - screen.pos -= get_size(); + screen.position -= get_size(); screen.size += get_size() * 2.0; - h_scroll->set_min(screen.pos.x); - h_scroll->set_max(screen.pos.x + screen.size.x); + h_scroll->set_min(screen.position.x); + h_scroll->set_max(screen.position.x + screen.size.x); h_scroll->set_page(get_size().x); if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) h_scroll->hide(); else h_scroll->show(); - v_scroll->set_min(screen.pos.y); - v_scroll->set_max(screen.pos.y + screen.size.y); + v_scroll->set_min(screen.position.y); + v_scroll->set_max(screen.position.y + screen.size.y); v_scroll->set_page(get_size().y); if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) @@ -374,7 +374,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) { Ref<Texture> port = get_icon("port", "GraphNode"); - Vector2 mpos(mb->get_pos().x, mb->get_pos().y); + Vector2 mpos(mb->get_position().x, mb->get_position().y); float grab_r = port->get_width() * 0.5 * grab_r_extend; for (int i = get_child_count() - 1; i >= 0; i--) { @@ -483,12 +483,12 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { Ref<InputEventMouseMotion> mm = p_ev; if (mm.is_valid() && connecting) { - connecting_to = mm->get_pos(); + connecting_to = mm->get_position(); connecting_target = false; top_layer->update(); Ref<Texture> port = get_icon("port", "GraphNode"); - Vector2 mpos = mm->get_pos(); + Vector2 mpos = mm->get_position(); float grab_r = port->get_width() * 0.5 * grab_r_extend; for (int i = get_child_count() - 1; i >= 0; i--) { @@ -546,7 +546,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { } else if (!just_disconected) { String from = connecting_from; int from_slot = connecting_index; - Vector2 ofs = Vector2(mb->get_pos().x, mb->get_pos().y); + Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y); emit_signal("connection_to_empty", from, from_slot, ofs); } connecting = false; @@ -817,7 +817,7 @@ void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) { connecting = false; top_layer->update(); } else { - emit_signal("popup_request", b->get_global_pos()); + emit_signal("popup_request", b->get_global_position()); } } } @@ -880,7 +880,7 @@ void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) { if (gn) { - if (_filter_input(b->get_pos())) + if (_filter_input(b->get_position())) return; dragging = true; @@ -905,7 +905,7 @@ void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) { } } else { - if (_filter_input(b->get_pos())) + if (_filter_input(b->get_position())) return; if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) return; diff --git a/scene/gui/graph_node.cpp b/scene/gui/graph_node.cpp index fb0ff4f320..95f65f31d6 100644 --- a/scene/gui/graph_node.cpp +++ b/scene/gui/graph_node.cpp @@ -239,7 +239,7 @@ void GraphNode::_notification(int p_what) { if (show_close) { Vector2 cpos = Point2(w + sb->get_margin(MARGIN_LEFT), -close->get_height() + close_offset); draw_texture(close, cpos); - close_rect.pos = cpos; + close_rect.position = cpos; close_rect.size = close->get_size(); } else { close_rect = Rect2(); @@ -582,11 +582,9 @@ void GraphNode::_gui_input(const Ref<InputEvent> &p_ev) { ERR_EXPLAIN("GraphNode must be the child of a GraphEdit node."); ERR_FAIL_COND(get_parent_control() == NULL); - print_line("INPUT EVENT BUTTON"); - if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { - Vector2 mpos = Vector2(mb->get_pos().x, mb->get_pos().y); + Vector2 mpos = Vector2(mb->get_position().x, mb->get_position().y); if (close_rect.size != Size2() && close_rect.has_point(mpos)) { emit_signal("close_request"); accept_event(); @@ -616,7 +614,7 @@ void GraphNode::_gui_input(const Ref<InputEvent> &p_ev) { Ref<InputEventMouseMotion> mm = p_ev; if (resizing && mm.is_valid()) { - Vector2 mpos = mm->get_pos(); + Vector2 mpos = mm->get_position(); Vector2 diff = mpos - resizing_from; diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp index a9f063280b..2f0c7b9aaf 100644 --- a/scene/gui/item_list.cpp +++ b/scene/gui/item_list.cpp @@ -431,7 +431,7 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { if (mb.is_valid() && (mb->get_button_index() == BUTTON_LEFT || (allow_rmb_select && mb->get_button_index() == BUTTON_RIGHT)) && mb->is_pressed()) { search_string = ""; //any mousepress cancels - Vector2 pos(mb->get_pos().x, mb->get_pos().y); + Vector2 pos(mb->get_position().x, mb->get_position().y); Ref<StyleBox> bg = get_stylebox("bg"); pos -= bg->get_offset(); pos.y += scroll_bar->get_value(); @@ -475,7 +475,7 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { if (mb->get_button_index() == BUTTON_RIGHT) { - emit_signal("item_rmb_selected", i, Vector2(mb->get_pos().x, mb->get_pos().y)); + emit_signal("item_rmb_selected", i, Vector2(mb->get_position().x, mb->get_position().y)); } } else { @@ -486,7 +486,7 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { if (items[i].selected && mb->get_button_index() == BUTTON_RIGHT) { - emit_signal("item_rmb_selected", i, Vector2(mb->get_pos().x, mb->get_pos().y)); + emit_signal("item_rmb_selected", i, Vector2(mb->get_position().x, mb->get_position().y)); } else { bool selected = !items[i].selected; @@ -501,7 +501,7 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { if (mb->get_button_index() == BUTTON_RIGHT) { - emit_signal("item_rmb_selected", i, Vector2(mb->get_pos().x, mb->get_pos().y)); + emit_signal("item_rmb_selected", i, Vector2(mb->get_position().x, mb->get_position().y)); } else if (/*select_mode==SELECT_SINGLE &&*/ mb->is_doubleclick()) { emit_signal("item_activated", i); @@ -857,7 +857,7 @@ void ItemList::_notification(int p_what) { items[i].rect_cache = items[i].min_rect_cache; if (same_column_width) items[i].rect_cache.size.x = max_column_width; - items[i].rect_cache.pos = ofs; + items[i].rect_cache.position = ofs; max_h = MAX(max_h, items[i].rect_cache.size.y); ofs.x += items[i].rect_cache.size.x + hseparation; //print_line("item "+itos(i)+" ofs "+rtos(items[i].rect_cache.size.x)); @@ -906,10 +906,10 @@ void ItemList::_notification(int p_what) { int from = scroll_bar->get_value(); int to = from + scroll_bar->get_page(); - if (r.pos.y < from) { - scroll_bar->set_value(r.pos.y); - } else if (r.pos.y + r.size.y > to) { - scroll_bar->set_value(r.pos.y + r.size.y - (to - from)); + if (r.position.y < from) { + scroll_bar->set_value(r.position.y); + } else if (r.position.y + r.size.y > to) { + scroll_bar->set_value(r.position.y + r.size.y - (to - from)); } } @@ -928,26 +928,29 @@ void ItemList::_notification(int p_what) { continue; if (current_columns == 1) { - rcache.size.width = width - rcache.pos.x; + rcache.size.width = width - rcache.position.x; } - Rect2 r = rcache; - r.pos += base_ofs; - - // Use stylebox to dimension potential bg color, even if not selected - r.pos.x -= sbsel->get_margin(MARGIN_LEFT); - r.size.x += sbsel->get_margin(MARGIN_LEFT) + sbsel->get_margin(MARGIN_RIGHT); - r.pos.y -= sbsel->get_margin(MARGIN_TOP); - r.size.y += sbsel->get_margin(MARGIN_TOP) + sbsel->get_margin(MARGIN_BOTTOM); - if (items[i].selected) { + Rect2 r = rcache; + r.position += base_ofs; + + // Use stylebox to dimension potential bg color + r.position.x -= sbsel->get_margin(MARGIN_LEFT); + r.size.x += sbsel->get_margin(MARGIN_LEFT) + sbsel->get_margin(MARGIN_RIGHT); + r.position.y -= sbsel->get_margin(MARGIN_TOP); + r.size.y += sbsel->get_margin(MARGIN_TOP) + sbsel->get_margin(MARGIN_BOTTOM); draw_style_box(sbsel, r); } + if (items[i].custom_bg.a > 0.001) { - r.pos.x += 2; - r.size.x -= 4; - r.pos.y += 2; - r.size.y -= 4; + + Rect2 r = rcache; + r.position += base_ofs; + + // Size rect to make the align the temperature colors + r.position.y -= vseparation / 2; + r.size.y += vseparation; draw_rect(r, items[i].custom_bg); } @@ -965,7 +968,7 @@ void ItemList::_notification(int p_what) { Vector2 icon_ofs; - Point2 pos = items[i].rect_cache.pos + icon_ofs + base_ofs; + Point2 pos = items[i].rect_cache.position + icon_ofs + base_ofs; if (icon_mode == ICON_MODE_TOP) { @@ -985,7 +988,7 @@ void ItemList::_notification(int p_what) { if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) { Rect2 adj = _adjust_to_max_size(items[i].get_icon_size() * icon_scale, icon_size); - draw_rect.pos += adj.pos; + draw_rect.position += adj.position; draw_rect.size = adj.size; } @@ -1001,7 +1004,7 @@ void ItemList::_notification(int p_what) { if (items[i].tag_icon.is_valid()) { - draw_texture(items[i].tag_icon, items[i].rect_cache.pos + base_ofs); + draw_texture(items[i].tag_icon, items[i].rect_cache.position + base_ofs); } if (items[i].text != "") { @@ -1046,7 +1049,7 @@ void ItemList::_notification(int p_what) { text_ofs.y += font->get_ascent(); text_ofs = text_ofs.floor(); text_ofs += base_ofs; - text_ofs += items[i].rect_cache.pos; + text_ofs += items[i].rect_cache.position; for (int j = 0; j < ss; j++) { @@ -1074,7 +1077,7 @@ void ItemList::_notification(int p_what) { text_ofs.y += font->get_ascent(); text_ofs = text_ofs.floor(); text_ofs += base_ofs; - text_ofs += items[i].rect_cache.pos; + text_ofs += items[i].rect_cache.position; draw_string(font, text_ofs, items[i].text, modulate, max_len + 1); } @@ -1083,7 +1086,7 @@ void ItemList::_notification(int p_what) { if (select_mode == SELECT_MULTI && i == current) { Rect2 r = rcache; - r.pos += base_ofs; + r.position += base_ofs; draw_style_box(cursor, r); } } @@ -1141,7 +1144,7 @@ bool ItemList::is_pos_at_end_of_items(const Point2 &p_pos) const { pos.y += scroll_bar->get_value(); Rect2 endrect = items[items.size() - 1].rect_cache; - return (pos.y > endrect.pos.y + endrect.size.y); + return (pos.y > endrect.position.y + endrect.size.y); } String ItemList::get_tooltip(const Point2 &p_pos) const { diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 38debe8a77..bc579020bd 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -64,7 +64,7 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { shift_selection_check_pre(b->get_shift()); - set_cursor_at_pixel_pos(b->get_pos().x); + set_cursor_at_pixel_pos(b->get_position().x); if (b->get_shift()) { @@ -118,7 +118,7 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { if (m->get_button_mask() & BUTTON_LEFT) { if (selection.creating) { - set_cursor_at_pixel_pos(m->get_pos().x); + set_cursor_at_pixel_pos(m->get_position().x); selection_fill_at_cursor(); } } diff --git a/scene/gui/popup.cpp b/scene/gui/popup.cpp index 655d8ed6f6..8979e0f111 100644 --- a/scene/gui/popup.cpp +++ b/scene/gui/popup.cpp @@ -170,8 +170,8 @@ void Popup::popup_centered(const Size2 &p_size) { Rect2 rect; rect.size = p_size == Size2() ? get_size() : p_size; - rect.pos = ((window_size - rect.size) / 2.0).floor(); - set_position(rect.pos); + rect.position = ((window_size - rect.size) / 2.0).floor(); + set_position(rect.position); set_size(rect.size); show_modal(exclusive); @@ -193,8 +193,8 @@ void Popup::popup_centered_ratio(float p_screen_ratio) { Rect2 rect; Point2 window_size = get_viewport_rect().size; rect.size = (window_size * p_screen_ratio).floor(); - rect.pos = ((window_size - rect.size) / 2.0).floor(); - set_position(rect.pos); + rect.position = ((window_size - rect.size) / 2.0).floor(); + set_position(rect.position); set_size(rect.size); show_modal(exclusive); @@ -216,7 +216,7 @@ void Popup::popup(const Rect2 &bounds) { // Fit the popup into the optionally provided bounds. if (!bounds.has_no_area()) { - set_position(bounds.pos); + set_position(bounds.position); set_size(bounds.size); } _fix_size(); diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp index 985d9addc9..c4991700aa 100644 --- a/scene/gui/popup_menu.cpp +++ b/scene/gui/popup_menu.cpp @@ -190,12 +190,12 @@ void PopupMenu::_activate_submenu(int over) { PopupMenu *pum = pm->cast_to<PopupMenu>(); if (pum) { - pr.pos -= pum->get_global_position(); + pr.position -= pum->get_global_position(); pum->clear_autohide_areas(); - pum->add_autohide_area(Rect2(pr.pos.x, pr.pos.y, pr.size.x, items[over]._ofs_cache)); + pum->add_autohide_area(Rect2(pr.position.x, pr.position.y, pr.size.x, items[over]._ofs_cache)); if (over < items.size() - 1) { int from = items[over + 1]._ofs_cache; - pum->add_autohide_area(Rect2(pr.pos.x, pr.pos.y + from, pr.size.x, pr.size.y - from)); + pum->add_autohide_area(Rect2(pr.position.x, pr.position.y + from, pr.size.x, pr.size.y - from)); } } } @@ -284,7 +284,7 @@ void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { //update hover Ref<InputEventMouseMotion> ie; ie.instance(); - ie->set_pos(b->get_pos() + Vector2(0, s)); + ie->set_position(b->get_position() + Vector2(0, s)); _gui_input(ie); } } break; @@ -303,13 +303,13 @@ void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { //update hover Ref<InputEventMouseMotion> ie; ie.instance(); - ie->set_pos(b->get_pos() - Vector2(0, s)); + ie->set_position(b->get_position() - Vector2(0, s)); _gui_input(ie); } } break; case BUTTON_LEFT: { - int over = _get_mouse_over(b->get_pos()); + int over = _get_mouse_over(b->get_position()); if (invalidated_click) { invalidated_click = false; @@ -348,13 +348,13 @@ void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { for (List<Rect2>::Element *E = autohide_areas.front(); E; E = E->next()) { - if (!Rect2(Point2(), get_size()).has_point(m->get_pos()) && E->get().has_point(m->get_pos())) { + if (!Rect2(Point2(), get_size()).has_point(m->get_position()) && E->get().has_point(m->get_position())) { call_deferred("hide"); return; } } - int over = _get_mouse_over(m->get_pos()); + int over = _get_mouse_over(m->get_position()); int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].ID >= 0 ? items[over].ID : over); if (id < 0) { diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp index a618810c11..0b8595de42 100644 --- a/scene/gui/rich_text_label.cpp +++ b/scene/gui/rich_text_label.cpp @@ -264,7 +264,7 @@ void RichTextLabel::_process_line(ItemFrame *p_frame, const Vector2 &p_ofs, int cw = tab_size * font->get_char_size(' ').width; } - if (end > 0 && w + cw + wofs > p_width) { + if (end > 0 && w + cw + begin > p_width) { break; //don't allow lines longer than assigned width } @@ -751,7 +751,7 @@ void RichTextLabel::_gui_input(Ref<InputEvent> p_event) { Item *item = NULL; bool outside; - _find_click(main, b->get_pos(), &item, &line, &outside); + _find_click(main, b->get_position(), &item, &line, &outside); if (item) { @@ -852,7 +852,7 @@ void RichTextLabel::_gui_input(Ref<InputEvent> p_event) { int line = 0; Item *item = NULL; - _find_click(main, m->get_pos(), &item, &line); + _find_click(main, m->get_position(), &item, &line); if (!item) return; // do not update diff --git a/scene/gui/scroll_bar.cpp b/scene/gui/scroll_bar.cpp index 27a16ccc33..bd66cd2745 100644 --- a/scene/gui/scroll_bar.cpp +++ b/scene/gui/scroll_bar.cpp @@ -72,7 +72,7 @@ void ScrollBar::_gui_input(Ref<InputEvent> p_event) { if (b->is_pressed()) { - double ofs = orientation == VERTICAL ? b->get_pos().y : b->get_pos().x; + double ofs = orientation == VERTICAL ? b->get_position().y : b->get_position().x; Ref<Texture> decr = get_icon("decrement"); Ref<Texture> incr = get_icon("increment"); @@ -130,7 +130,7 @@ void ScrollBar::_gui_input(Ref<InputEvent> p_event) { if (drag.active) { - double ofs = orientation == VERTICAL ? m->get_pos().y : m->get_pos().x; + double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x; Ref<Texture> decr = get_icon("decrement"); double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width(); @@ -141,7 +141,7 @@ void ScrollBar::_gui_input(Ref<InputEvent> p_event) { set_as_ratio(drag.value_at_click + diff); } else { - double ofs = orientation == VERTICAL ? m->get_pos().y : m->get_pos().x; + double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x; Ref<Texture> decr = get_icon("decrement"); Ref<Texture> incr = get_icon("increment"); @@ -267,14 +267,14 @@ void ScrollBar::_notification(int p_what) { grabber_rect.size.width = get_grabber_size(); grabber_rect.size.height = get_size().height; - grabber_rect.pos.y = 0; - grabber_rect.pos.x = get_grabber_offset() + decr->get_width() + bg->get_margin(MARGIN_LEFT); + grabber_rect.position.y = 0; + grabber_rect.position.x = get_grabber_offset() + decr->get_width() + bg->get_margin(MARGIN_LEFT); } else { grabber_rect.size.width = get_size().width; grabber_rect.size.height = get_grabber_size(); - grabber_rect.pos.y = get_grabber_offset() + decr->get_height() + bg->get_margin(MARGIN_TOP); - grabber_rect.pos.x = 0; + grabber_rect.position.y = get_grabber_offset() + decr->get_height() + bg->get_margin(MARGIN_TOP); + grabber_rect.position.x = 0; } grabber->draw(ci, grabber_rect); diff --git a/scene/gui/scroll_container.cpp b/scene/gui/scroll_container.cpp index 70b4ac47f8..cc1a637d2b 100644 --- a/scene/gui/scroll_container.cpp +++ b/scene/gui/scroll_container.cpp @@ -233,14 +233,14 @@ void ScrollContainer::_notification(int p_what) { Rect2 r = Rect2(-scroll, minsize); if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) { - r.pos.x = 0; + r.position.x = 0; if (c->get_h_size_flags() & SIZE_EXPAND) r.size.width = MAX(size.width, minsize.width); else r.size.width = minsize.width; } if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) { - r.pos.y = 0; + r.position.y = 0; r.size.height = size.height; if (c->get_v_size_flags() & SIZE_EXPAND) r.size.height = MAX(size.height, minsize.height); diff --git a/scene/gui/slider.cpp b/scene/gui/slider.cpp index ae2bf0999e..d0e708bf73 100644 --- a/scene/gui/slider.cpp +++ b/scene/gui/slider.cpp @@ -46,7 +46,7 @@ void Slider::_gui_input(Ref<InputEvent> p_event) { if (mb->is_pressed()) { Ref<Texture> grabber = get_icon(mouse_inside || has_focus() ? "grabber_highlight" : "grabber"); - grab.pos = orientation == VERTICAL ? mb->get_pos().y : mb->get_pos().x; + grab.pos = orientation == VERTICAL ? mb->get_position().y : mb->get_position().x; double grab_width = (double)grabber->get_size().width; double grab_height = (double)grabber->get_size().height; @@ -75,7 +75,7 @@ void Slider::_gui_input(Ref<InputEvent> p_event) { Size2i size = get_size(); Ref<Texture> grabber = get_icon("grabber"); - float motion = (orientation == VERTICAL ? mm->get_pos().y : mm->get_pos().x) - grab.pos; + float motion = (orientation == VERTICAL ? mm->get_position().y : mm->get_position().x) - grab.pos; if (orientation == VERTICAL) motion = -motion; float areasize = orientation == VERTICAL ? size.height - grabber->get_size().height : size.width - grabber->get_size().width; diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp index 2eb2028391..c5b9df15b9 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -96,7 +96,7 @@ void SpinBox::_gui_input(const Ref<InputEvent> &p_event) { if (mb.is_valid() && mb->is_pressed()) { - bool up = mb->get_pos().y < (get_size().height / 2); + bool up = mb->get_position().y < (get_size().height / 2); switch (mb->get_button_index()) { @@ -135,7 +135,7 @@ void SpinBox::_gui_input(const Ref<InputEvent> &p_event) { if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == 1) { //set_default_cursor_shape(CURSOR_VSIZE); - Vector2 cpos = Vector2(mb->get_pos().x, mb->get_pos().y); + Vector2 cpos = Vector2(mb->get_position().x, mb->get_position().y); drag.mouse_pos = cpos; } @@ -155,7 +155,7 @@ void SpinBox::_gui_input(const Ref<InputEvent> &p_event) { if (mm.is_valid() && mm->get_button_mask() & 1) { - Vector2 cpos = mm->get_pos(); + Vector2 cpos = mm->get_position(); if (drag.enabled) { diff --git a/scene/gui/split_container.cpp b/scene/gui/split_container.cpp index 91fd18e2ed..31236fa277 100644 --- a/scene/gui/split_container.cpp +++ b/scene/gui/split_container.cpp @@ -286,16 +286,16 @@ void SplitContainer::_gui_input(const Ref<InputEvent> &p_event) { if (vertical) { - if (mb->get_pos().y > middle_sep && mb->get_pos().y < middle_sep + sep) { + if (mb->get_position().y > middle_sep && mb->get_position().y < middle_sep + sep) { dragging = true; - drag_from = mb->get_pos().y; + drag_from = mb->get_position().y; drag_ofs = expand_ofs; } } else { - if (mb->get_pos().x > middle_sep && mb->get_pos().x < middle_sep + sep) { + if (mb->get_position().x > middle_sep && mb->get_position().x < middle_sep + sep) { dragging = true; - drag_from = mb->get_pos().x; + drag_from = mb->get_position().x; drag_ofs = expand_ofs; } } @@ -312,7 +312,7 @@ void SplitContainer::_gui_input(const Ref<InputEvent> &p_event) { if (dragging) { - expand_ofs = drag_ofs + ((vertical ? mm->get_pos().y : mm->get_pos().x) - drag_from); + expand_ofs = drag_ofs + ((vertical ? mm->get_position().y : mm->get_position().x) - drag_from); queue_sort(); emit_signal("dragged", get_split_offset()); } diff --git a/scene/gui/split_container.h b/scene/gui/split_container.h index 94b80cf279..87a210f24c 100644 --- a/scene/gui/split_container.h +++ b/scene/gui/split_container.h @@ -34,7 +34,7 @@ class SplitContainer : public Container { - GDCLASS(SplitContainer, Container); + GDCLASS(SplitContainer, Container) public: enum DraggerVisibility { diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp index 6bbfa1aeb0..5ebcef3e8d 100644 --- a/scene/gui/tab_container.cpp +++ b/scene/gui/tab_container.cpp @@ -69,7 +69,7 @@ void TabContainer::_gui_input(const Ref<InputEvent> &p_event) { if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { - Point2 pos(mb->get_pos().x, mb->get_pos().y); + Point2 pos(mb->get_position().x, mb->get_position().y); Size2 size = get_size(); // Click must be on tabs in the tab header area. @@ -233,7 +233,7 @@ void TabContainer::_notification(int p_what) { Control *control = tabs[i + first_tab_cache]->cast_to<Control>(); String text = control->has_meta("_tab_name") ? String(XL_MESSAGE(String(control->get_meta("_tab_name")))) : String(control->get_name()); - int x_content = tab_rect.pos.x + tab_style->get_margin(MARGIN_LEFT); + int x_content = tab_rect.position.x + tab_style->get_margin(MARGIN_LEFT); int top_margin = tab_style->get_margin(MARGIN_TOP); int y_center = top_margin + (tab_rect.size.y - tab_style->get_minimum_size().y) / 2; diff --git a/scene/gui/tabs.cpp b/scene/gui/tabs.cpp index d94c33e408..cdb7965444 100644 --- a/scene/gui/tabs.cpp +++ b/scene/gui/tabs.cpp @@ -85,7 +85,7 @@ void Tabs::_gui_input(const Ref<InputEvent> &p_event) { if (mm.is_valid()) { - Point2 pos = mm->get_pos(); + Point2 pos = mm->get_position(); highlight_arrow = -1; if (buttons_visible) { @@ -165,7 +165,7 @@ void Tabs::_gui_input(const Ref<InputEvent> &p_event) { mb->get_button_index() == BUTTON_LEFT) { // clicks - Point2 pos(mb->get_pos().x, mb->get_pos().y); + Point2 pos(mb->get_position().x, mb->get_position().y); if (buttons_visible) { @@ -340,8 +340,8 @@ void Tabs::_notification(int p_what) { Rect2 rb_rect; rb_rect.size = style->get_minimum_size() + rb->get_size(); - rb_rect.pos.x = w; - rb_rect.pos.y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2; + rb_rect.position.x = w; + rb_rect.position.y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2; if (rb_hover == i) { if (rb_pressing) @@ -350,7 +350,7 @@ void Tabs::_notification(int p_what) { style->draw(ci, rb_rect); } - rb->draw(ci, Point2i(w + style->get_margin(MARGIN_LEFT), rb_rect.pos.y + style->get_margin(MARGIN_TOP))); + rb->draw(ci, Point2i(w + style->get_margin(MARGIN_LEFT), rb_rect.position.y + style->get_margin(MARGIN_TOP))); w += rb->get_width(); tabs[i].rb_rect = rb_rect; } @@ -364,8 +364,8 @@ void Tabs::_notification(int p_what) { Rect2 cb_rect; cb_rect.size = style->get_minimum_size() + cb->get_size(); - cb_rect.pos.x = w; - cb_rect.pos.y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2; + cb_rect.position.x = w; + cb_rect.position.y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2; if (!tabs[i].disabled && cb_hover == i) { if (cb_pressing) @@ -374,7 +374,7 @@ void Tabs::_notification(int p_what) { style->draw(ci, cb_rect); } - cb->draw(ci, Point2i(w + style->get_margin(MARGIN_LEFT), cb_rect.pos.y + style->get_margin(MARGIN_TOP))); + cb->draw(ci, Point2i(w + style->get_margin(MARGIN_LEFT), cb_rect.position.y + style->get_margin(MARGIN_TOP))); w += cb->get_width(); tabs[i].cb_rect = cb_rect; } diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 4989a3d863..936a9b77f8 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -1069,16 +1069,16 @@ void TextEdit::_notification(int p_what) { int th = h + csb->get_minimum_size().y; if (cursor_pos.y + get_row_height() + th > get_size().height) { - completion_rect.pos.y = cursor_pos.y - th; + completion_rect.position.y = cursor_pos.y - th; } else { - completion_rect.pos.y = cursor_pos.y + get_row_height() + csb->get_offset().y; + completion_rect.position.y = cursor_pos.y + get_row_height() + csb->get_offset().y; completion_below = true; } if (cursor_pos.x - nofs + w + scrollw > get_size().width) { - completion_rect.pos.x = get_size().width - w - scrollw; + completion_rect.position.x = get_size().width - w - scrollw; } else { - completion_rect.pos.x = cursor_pos.x - nofs; + completion_rect.position.x = cursor_pos.x - nofs; } completion_rect.size.width = w + 2; @@ -1086,14 +1086,14 @@ void TextEdit::_notification(int p_what) { if (completion_options.size() <= maxlines) scrollw = 0; - draw_style_box(csb, Rect2(completion_rect.pos - csb->get_offset(), completion_rect.size + csb->get_minimum_size() + Size2(scrollw, 0))); + draw_style_box(csb, Rect2(completion_rect.position - csb->get_offset(), completion_rect.size + csb->get_minimum_size() + Size2(scrollw, 0))); if (cache.completion_background_color.a > 0.01) { - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(completion_rect.pos, completion_rect.size + Size2(scrollw, 0)), cache.completion_background_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(completion_rect.position, completion_rect.size + Size2(scrollw, 0)), cache.completion_background_color); } int line_from = CLAMP(completion_index - lines / 2, 0, completion_options.size() - lines); - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(completion_rect.pos.x, completion_rect.pos.y + (completion_index - line_from) * get_row_height()), Size2(completion_rect.size.width, get_row_height())), cache.completion_selected_color); - draw_rect(Rect2(completion_rect.pos, Size2(nofs, completion_rect.size.height)), cache.completion_existing_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(completion_rect.position.x, completion_rect.position.y + (completion_index - line_from) * get_row_height()), Size2(completion_rect.size.width, get_row_height())), cache.completion_selected_color); + draw_rect(Rect2(completion_rect.position, Size2(nofs, completion_rect.size.height)), cache.completion_existing_color); for (int i = 0; i < lines; i++) { @@ -1105,14 +1105,14 @@ void TextEdit::_notification(int p_what) { text_color = color_regions[j].color; } } - draw_string(cache.font, Point2(completion_rect.pos.x, completion_rect.pos.y + i * get_row_height() + cache.font->get_ascent()), completion_options[l], text_color, completion_rect.size.width); + draw_string(cache.font, Point2(completion_rect.position.x, completion_rect.position.y + i * get_row_height() + cache.font->get_ascent()), completion_options[l], text_color, completion_rect.size.width); } if (scrollw) { //draw a small scroll rectangle to show a position in the options float r = maxlines / (float)completion_options.size(); float o = line_from / (float)completion_options.size(); - draw_rect(Rect2(completion_rect.pos.x + completion_rect.size.width, completion_rect.pos.y + o * completion_rect.size.y, scrollw, completion_rect.size.y * r), scrollc); + draw_rect(Rect2(completion_rect.position.x + completion_rect.size.width, completion_rect.position.y + o * completion_rect.size.y, scrollw, completion_rect.size.y * r), scrollc); } completion_line_ofs = line_from; @@ -1445,7 +1445,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { Ref<InputEventMouseButton> mb = p_gui_input; if (mb.is_valid()) { - if (completion_active && completion_rect.has_point(mb->get_pos())) { + if (completion_active && completion_rect.has_point(mb->get_position())) { if (!mb->is_pressed()) return; @@ -1468,7 +1468,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (mb->get_button_index() == BUTTON_LEFT) { - completion_index = CLAMP(completion_line_ofs + (mb->get_pos().y - completion_rect.pos.y) / get_row_height(), 0, completion_options.size() - 1); + completion_index = CLAMP(completion_line_ofs + (mb->get_position().y - completion_rect.position.y) / get_row_height(), 0, completion_options.size() - 1); completion_current = completion_options[completion_index]; update(); @@ -1500,7 +1500,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { _reset_caret_blink_timer(); int row, col; - _get_mouse_pos(Point2i(mb->get_pos().x, mb->get_pos().y), row, col); + _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col); if (mb->get_command() && highlighted_word != String()) { @@ -1511,7 +1511,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { // toggle breakpoint on gutter click if (draw_breakpoint_gutter) { int gutter = cache.style_normal->get_margin(MARGIN_LEFT); - if (mb->get_pos().x > gutter && mb->get_pos().x <= gutter + cache.breakpoint_gutter_width + 3) { + if (mb->get_position().x > gutter && mb->get_position().x <= gutter + cache.breakpoint_gutter_width + 3) { set_line_as_breakpoint(row, !is_line_set_as_breakpoint(row)); emit_signal("breakpoint_toggled", row); return; @@ -1646,7 +1646,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (select_identifiers_enabled) { if (mm->get_command() && mm->get_button_mask() == 0) { - String new_word = get_word_at_pos(mm->get_pos()); + String new_word = get_word_at_pos(mm->get_position()); if (new_word != highlighted_word) { highlighted_word = new_word; update(); @@ -1666,7 +1666,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { _reset_caret_blink_timer(); int row, col; - _get_mouse_pos(mm->get_pos(), row, col); + _get_mouse_pos(mm->get_position(), row, col); select(selection.selecting_line, selection.selecting_column, row, col); diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index b17192fe4f..d864d9fce7 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -610,6 +610,18 @@ Color TreeItem::get_custom_bg_color(int p_column) const { return cells[p_column].bg_color; } +void TreeItem::set_custom_as_button(int p_column, bool p_button) { + + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].custom_button = p_button; +} + +bool TreeItem::is_custom_set_as_button(int p_column) const { + + ERR_FAIL_INDEX_V(p_column, cells.size(), false); + return cells[p_column].custom_button; +} + void TreeItem::_bind_methods() { ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode); @@ -670,6 +682,9 @@ void TreeItem::_bind_methods() { ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color); ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color); + ClassDB::bind_method(D_METHOD("set_custom_as_button", "column", "enable"), &TreeItem::set_custom_as_button); + ClassDB::bind_method(D_METHOD("is_custom_set_as_button", "column"), &TreeItem::is_custom_set_as_button); + ClassDB::bind_method(D_METHOD("add_button", "column", "button:Texture", "button_idx", "disabled", "tooltip"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false), DEFVAL("")); ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count); ClassDB::bind_method(D_METHOD("get_button:Texture", "column", "button_idx"), &TreeItem::get_button); @@ -732,6 +747,10 @@ TreeItem::~TreeItem() { tree->pressing_for_editor = false; } + if (tree && tree->cache.hover_item == this) { + tree->cache.hover_item = NULL; + } + if (tree && tree->selected_item == this) tree->selected_item = NULL; @@ -772,6 +791,11 @@ void Tree::update_cache() { cache.select_arrow = get_icon("select_arrow"); cache.updown = get_icon("updown"); + cache.custom_button = get_stylebox("custom_button"); + cache.custom_button_hover = get_stylebox("custom_button_hover"); + cache.custom_button_pressed = get_stylebox("custom_button_pressed"); + cache.custom_button_font_highlight = get_color("custom_button_font_highlight"); + cache.font_color = get_color("font_color"); cache.font_color_selected = get_color("font_color_selected"); cache.guide_color = get_color("guide_color"); @@ -833,6 +857,9 @@ int Tree::compute_item_height(TreeItem *p_item) const { if (s.height > height) height = s.height; } + if (p_item->cells[i].mode == TreeItem::CELL_MODE_CUSTOM && p_item->cells[i].custom_button) { + height += cache.custom_button->get_minimum_size().height; + } } break; default: {} @@ -877,8 +904,8 @@ void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, co bmsize.width = p_cell.icon_max_w; } - p_cell.draw_icon(ci, rect.pos + Size2i(0, Math::floor((real_t)(rect.size.y - bmsize.y) / 2)), bmsize); - rect.pos.x += bmsize.x + cache.hseparation; + p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - bmsize.y) / 2)), bmsize); + rect.position.x += bmsize.x + cache.hseparation; rect.size.x -= bmsize.x + cache.hseparation; } @@ -893,8 +920,8 @@ void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, co if (p_cell.suffix != String()) text += " " + p_cell.suffix; - rect.pos.y += Math::floor((rect.size.y - font->get_height()) / 2.0) + font->get_ascent(); - font->draw(ci, rect.pos, text, p_color, rect.size.x); + rect.position.y += Math::floor((rect.size.y - font->get_height()) / 2.0) + font->get_ascent(); + font->draw(ci, rect.position, text, p_color, rect.size.x); } #if 0 @@ -1007,16 +1034,16 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - cache.offset + p_draw_ofs, Size2i(w, label_h)); Rect2i cell_rect = item_rect; if (i != 0) { - cell_rect.pos.x -= cache.hseparation; + cell_rect.position.x -= cache.hseparation; cell_rect.size.x += cache.hseparation; } - VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(cell_rect.pos.x, cell_rect.pos.y + cell_rect.size.height), cell_rect.pos + cell_rect.size, cache.guide_color, 1); + VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(cell_rect.position.x, cell_rect.position.y + cell_rect.size.height), cell_rect.position + cell_rect.size, cache.guide_color, 1); if (i == 0) { if (p_item->cells[0].selected && select_mode == SELECT_ROW) { - Rect2i row_rect = Rect2i(Point2i(cache.bg->get_margin(MARGIN_LEFT), item_rect.pos.y), Size2i(get_size().width - cache.bg->get_minimum_size().width, item_rect.size.y)); + Rect2i row_rect = Rect2i(Point2i(cache.bg->get_margin(MARGIN_LEFT), item_rect.position.y), Size2i(get_size().width - cache.bg->get_minimum_size().width, item_rect.size.y)); //Rect2 r = Rect2i(row_rect.pos,row_rect.size); //r.grow(cache.selected->get_margin(MARGIN_LEFT)); if (has_focus()) @@ -1028,34 +1055,34 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 if (p_item->cells[i].selected && select_mode != SELECT_ROW) { - Rect2i r(item_rect.pos, item_rect.size); + Rect2i r(item_rect.position, item_rect.size); if (p_item->cells[i].text.size() > 0) { float icon_width = p_item->cells[i].get_icon_size().width; - r.pos.x += icon_width; + r.position.x += icon_width; r.size.x -= icon_width; } //r.grow(cache.selected->get_margin(MARGIN_LEFT)); if (has_focus()) { cache.selected_focus->draw(ci, r); - p_item->set_meta("__focus_rect", Rect2(r.pos, r.size)); + p_item->set_meta("__focus_rect", Rect2(r.position, r.size)); } else { cache.selected->draw(ci, r); } if (text_editor->is_visible_in_tree()) { - text_editor->set_position(get_global_position() + r.pos); + text_editor->set_position(get_global_position() + r.position); } } if (p_item->cells[i].custom_bg_color) { Rect2 r = cell_rect; - r.pos.x -= cache.hseparation; + r.position.x -= cache.hseparation; r.size.x += cache.hseparation; if (p_item->cells[i].custom_bg_outline) { - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.pos.x, r.pos.y, r.size.x, 1), p_item->cells[i].bg_color); - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.pos.x, r.pos.y + r.size.y - 1, r.size.x, 1), p_item->cells[i].bg_color); - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.pos.x, r.pos.y, 1, r.size.y), p_item->cells[i].bg_color); - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.pos.x + r.size.x - 1, r.pos.y, 1, r.size.y), p_item->cells[i].bg_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), p_item->cells[i].bg_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y - 1, r.size.x, 1), p_item->cells[i].bg_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), p_item->cells[i].bg_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), p_item->cells[i].bg_color); } else { VisualServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color); } @@ -1066,22 +1093,22 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 Rect2 r = cell_rect; if (drop_mode_section == -1 || drop_mode_section == 0) { - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.pos.x, r.pos.y, r.size.x, 1), cache.drop_position_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), cache.drop_position_color); } if (drop_mode_section == 0) { - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.pos.x, r.pos.y, 1, r.size.y), cache.drop_position_color); - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.pos.x + r.size.x - 1, r.pos.y, 1, r.size.y), cache.drop_position_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), cache.drop_position_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), cache.drop_position_color); } if (drop_mode_section == 1 || drop_mode_section == 0) { - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.pos.x, r.pos.y + r.size.y, r.size.x, 1), cache.drop_position_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y, r.size.x, 1), cache.drop_position_color); } } Color col = p_item->cells[i].custom_color ? p_item->cells[i].color : get_color(p_item->cells[i].selected ? "font_color_selected" : "font_color"); - Point2i text_pos = item_rect.pos; + Point2i text_pos = item_rect.position; text_pos.y += Math::floor((item_rect.size.y - font->get_height()) / 2) + font_ascent; switch (p_item->cells[i].mode) { @@ -1094,7 +1121,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 Ref<Texture> checked = cache.checked; Ref<Texture> unchecked = cache.unchecked; - Point2i check_ofs = item_rect.pos; + Point2i check_ofs = item_rect.position; check_ofs.y += Math::floor((real_t)(item_rect.size.y - checked->get_height()) / 2); if (p_item->cells[i].checked) { @@ -1109,7 +1136,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 text_pos.x += check_w; item_rect.size.x -= check_w; - item_rect.pos.x += check_w; + item_rect.position.x += check_w; draw_item_rect(p_item->cells[i], item_rect, col); @@ -1137,7 +1164,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 font->draw(ci, text_pos, s, col, item_rect.size.x - downarrow->get_width()); //? - Point2i arrow_pos = item_rect.pos; + Point2i arrow_pos = item_rect.position; arrow_pos.x += item_rect.size.x - downarrow->get_width(); arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0); @@ -1157,7 +1184,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 if (!p_item->cells[i].editable) break; - Point2i updown_pos = item_rect.pos; + Point2i updown_pos = item_rect.position; updown_pos.x += item_rect.size.x - updown->get_width(); updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0); @@ -1176,7 +1203,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 } Point2i icon_ofs = (item_rect.size - icon_size) / 2; - icon_ofs += item_rect.pos; + icon_ofs += item_rect.position; draw_texture_rect(p_item->cells[i].icon, Rect2(icon_ofs, icon_size)); //p_item->cells[i].icon->draw(ci, icon_ofs); @@ -1202,12 +1229,28 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 Ref<Texture> downarrow = cache.select_arrow; Rect2i ir = item_rect; - ir.size.width -= downarrow->get_width(); - draw_item_rect(p_item->cells[i], ir, col); - Point2i arrow_pos = item_rect.pos; + Point2i arrow_pos = item_rect.position; arrow_pos.x += item_rect.size.x - downarrow->get_width(); arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0); + ir.size.width -= downarrow->get_width(); + + if (p_item->cells[i].custom_button) { + if (cache.hover_item == p_item && cache.hover_cell == i) { + if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) { + draw_style_box(cache.custom_button_pressed, ir); + } else { + draw_style_box(cache.custom_button_hover, ir); + col = cache.custom_button_font_highlight; + } + } else { + draw_style_box(cache.custom_button, ir); + } + ir.size -= cache.custom_button->get_minimum_size(); + ir.position += cache.custom_button->get_offset(); + } + + draw_item_rect(p_item->cells[i], ir, col); downarrow->draw(ci, arrow_pos); @@ -1697,11 +1740,18 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, bool case TreeItem::CELL_MODE_CUSTOM: { edited_item = p_item; edited_col = col; - custom_popup_rect = Rect2i(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h - cache.offset.y), Size2(get_column_width(col), item_h)); - emit_signal("custom_popup_edited", ((bool)(x >= (col_width - item_h / 2)))); + bool on_arrow = x > col_width - cache.select_arrow->get_width(); bring_up_editor = false; - item_edited(col, p_item); + + if (on_arrow || !p_item->cells[col].custom_button) { + custom_popup_rect = Rect2i(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h - cache.offset.y), Size2(get_column_width(col), item_h)); + emit_signal("custom_popup_edited", ((bool)(x >= (col_width - item_h / 2)))); + } + + if (!p_item->cells[col].custom_button || !on_arrow) { + item_edited(col, p_item); + } click_handled = true; return -1; } break; @@ -2122,7 +2172,7 @@ void Tree::_gui_input(Ref<InputEvent> p_event) { Ref<StyleBox> bg = cache.bg; - Point2 pos = mm->get_pos() - bg->get_offset(); + Point2 pos = mm->get_position() - bg->get_offset(); Cache::ClickType old_hover = cache.hover_type; int old_index = cache.hover_index; @@ -2148,9 +2198,9 @@ void Tree::_gui_input(Ref<InputEvent> p_event) { } } - if (drop_mode_flags && root) { + if (root) { - Point2 mpos = mm->get_pos(); + Point2 mpos = mm->get_position(); mpos -= cache.bg->get_offset(); mpos.y -= _get_title_button_height(); if (mpos.y >= 0) { @@ -2163,11 +2213,17 @@ void Tree::_gui_input(Ref<InputEvent> p_event) { int col, h, section; TreeItem *it = _find_item_at_pos(root, mpos, col, h, section); - if (it != drop_mode_over || section != drop_mode_section) { + if (drop_mode_flags && it != drop_mode_over || section != drop_mode_section) { drop_mode_over = it; drop_mode_section = section; update(); } + + if (it != cache.hover_item || col != cache.hover_cell) { + cache.hover_item = it; + cache.hover_cell = col; + update(); + } } } @@ -2180,7 +2236,7 @@ void Tree::_gui_input(Ref<InputEvent> p_event) { if (!range_drag_enabled) { - Vector2 cpos = mm->get_pos(); + Vector2 cpos = mm->get_position(); if (cpos.distance_to(pressing_pos) > 2) { range_drag_enabled = true; range_drag_capture_pos = cpos; @@ -2217,7 +2273,7 @@ void Tree::_gui_input(Ref<InputEvent> p_event) { if (b->get_button_index() == BUTTON_LEFT) { - Point2 pos = b->get_pos() - cache.bg->get_offset(); + Point2 pos = b->get_position() - cache.bg->get_offset(); if (show_column_titles) { pos.y -= _get_title_button_height(); @@ -2251,7 +2307,7 @@ void Tree::_gui_input(Ref<InputEvent> p_event) { warp_mouse(range_drag_capture_pos); } else { Rect2 rect = get_selected()->get_meta("__focus_rect"); - if (rect.has_point(Point2(b->get_pos().x, b->get_pos().y))) { + if (rect.has_point(Point2(b->get_position().x, b->get_position().y))) { edit_selected(); } else { emit_signal("item_double_clicked"); @@ -2295,7 +2351,7 @@ void Tree::_gui_input(Ref<InputEvent> p_event) { case BUTTON_LEFT: { Ref<StyleBox> bg = cache.bg; - Point2 pos = b->get_pos() - bg->get_offset(); + Point2 pos = b->get_position() - bg->get_offset(); cache.click_type = Cache::CLICK_NONE; if (show_column_titles && b->get_button_index() == BUTTON_LEFT) { pos.y -= _get_title_button_height(); @@ -2333,7 +2389,7 @@ void Tree::_gui_input(Ref<InputEvent> p_event) { blocked--; if (pressing_for_editor) { - pressing_pos = b->get_pos(); + pressing_pos = b->get_position(); } if (b->get_button_index() == BUTTON_RIGHT) @@ -2401,7 +2457,7 @@ bool Tree::edit_selected() { edited_item = s; edited_col = col; - custom_popup_rect = Rect2i(get_global_position() + rect.pos, rect.size); + custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size); emit_signal("custom_popup_edited", false); item_edited(col, s); @@ -2416,7 +2472,7 @@ bool Tree::edit_selected() { } popup_menu->set_size(Size2(rect.size.width, 0)); - popup_menu->set_position(get_global_position() + rect.pos + Point2i(0, rect.size.height)); + popup_menu->set_position(get_global_position() + rect.position + Point2i(0, rect.size.height)); popup_menu->popup(); popup_edited_item = s; popup_edited_item_col = col; @@ -2424,7 +2480,7 @@ bool Tree::edit_selected() { } else if (c.mode == TreeItem::CELL_MODE_STRING || c.mode == TreeItem::CELL_MODE_RANGE || c.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) { - Point2i textedpos = get_global_position() + rect.pos; + Point2i textedpos = get_global_position() + rect.position; text_editor->set_position(textedpos); text_editor->set_size(rect.size); text_editor->clear(); @@ -2671,7 +2727,7 @@ void Tree::_notification(int p_what) { ofs += tbrect.size.width; //text int clip_w = tbrect.size.width - sb->get_minimum_size().width; - f->draw_halign(ci, tbrect.pos + Point2i(sb->get_offset().x, (tbrect.size.height - f->get_height()) / 2 + f->get_ascent()), HALIGN_CENTER, clip_w, columns[i].title, cache.title_button_color); + f->draw_halign(ci, tbrect.position + Point2i(sb->get_offset().x, (tbrect.size.height - f->get_height()) / 2 + f->get_ascent()), HALIGN_CENTER, clip_w, columns[i].title, cache.title_button_color); } } } @@ -3053,11 +3109,11 @@ Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column) const { int ofs = get_item_offset(p_item); int height = compute_item_height(p_item); Rect2 r; - r.pos.y = ofs; + r.position.y = ofs; r.size.height = height; if (p_column == -1) { - r.pos.x = 0; + r.position.x = 0; r.size.x = get_size().width; } else { @@ -3065,7 +3121,7 @@ Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column) const { for (int i = 0; i < p_column; i++) { accum += get_column_width(i); } - r.pos.x = accum; + r.position.x = accum; r.size.x = get_column_width(p_column); } @@ -3469,6 +3525,7 @@ void Tree::_bind_methods() { ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::VECTOR2, "pos"))); ADD_SIGNAL(MethodInfo("empty_tree_rmb_selected", PropertyInfo(Variant::VECTOR2, "pos"))); ADD_SIGNAL(MethodInfo("item_edited")); + ADD_SIGNAL(MethodInfo("item_custom_button_pressed")); ADD_SIGNAL(MethodInfo("item_double_clicked")); ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item"))); //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) ); @@ -3575,6 +3632,9 @@ Tree::Tree() { force_edit_checkbox_only_on_checkbox = false; set_clip_contents(true); + + cache.hover_item = NULL; + cache.hover_cell = -1; } Tree::~Tree() { diff --git a/scene/gui/tree.h b/scene/gui/tree.h index 0c07109e7d..097e0110e8 100644 --- a/scene/gui/tree.h +++ b/scene/gui/tree.h @@ -81,6 +81,7 @@ private: bool custom_bg_color; bool custom_bg_outline; Color bg_color; + bool custom_button; Variant meta; String tooltip; @@ -107,6 +108,7 @@ private: Cell() { custom_draw_obj = 0; + custom_button = false; mode = TreeItem::CELL_MODE_STRING; min = 0; max = 100; @@ -238,6 +240,9 @@ public: void clear_custom_bg_color(int p_column); Color get_custom_bg_color(int p_column) const; + void set_custom_as_button(int p_column, bool p_button); + bool is_custom_set_as_button(int p_column) const; + void set_tooltip(int p_column, const String &p_tooltip); String get_tooltip(int p_column) const; @@ -369,6 +374,10 @@ private: Ref<StyleBox> title_button; Ref<StyleBox> title_button_hover; Ref<StyleBox> title_button_pressed; + Ref<StyleBox> custom_button; + Ref<StyleBox> custom_button_hover; + Ref<StyleBox> custom_button_pressed; + Color title_button_color; Ref<Texture> checked; @@ -383,6 +392,7 @@ private: Color guide_color; Color drop_position_color; Color relationship_line_color; + Color custom_button_font_highlight; int hseparation; int vseparation; @@ -410,6 +420,9 @@ private: int hover_index; Point2 click_pos; + TreeItem *hover_item; + int hover_cell; + } cache; int _get_title_button_height() const; diff --git a/scene/main/scene_main_loop.cpp b/scene/main/scene_main_loop.cpp index 96a3519840..f7a255cd33 100644 --- a/scene/main/scene_main_loop.cpp +++ b/scene/main/scene_main_loop.cpp @@ -839,12 +839,12 @@ Ref<Material> SceneTree::get_debug_collision_material() { return collision_material; } -Ref<Mesh> SceneTree::get_debug_contact_mesh() { +Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() { if (debug_contact_mesh.is_valid()) return debug_contact_mesh; - debug_contact_mesh = Ref<Mesh>(memnew(Mesh)); + debug_contact_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); Ref<SpatialMaterial> mat = memnew(SpatialMaterial); /*mat->set_flag(Material::FLAG_UNSHADED,true); diff --git a/scene/main/scene_main_loop.h b/scene/main/scene_main_loop.h index 5d42c66652..2ea79bf945 100644 --- a/scene/main/scene_main_loop.h +++ b/scene/main/scene_main_loop.h @@ -33,6 +33,7 @@ #include "io/networked_multiplayer_peer.h" #include "os/main_loop.h" #include "os/thread_safe.h" +#include "scene/resources/mesh.h" #include "scene/resources/world.h" #include "scene/resources/world_2d.h" #include "self_list.h" @@ -169,7 +170,7 @@ private: Color debug_collision_contact_color; Color debug_navigation_color; Color debug_navigation_disabled_color; - Ref<Mesh> debug_contact_mesh; + Ref<ArrayMesh> debug_contact_mesh; Ref<Material> navigation_material; Ref<Material> navigation_disabled_material; Ref<Material> collision_material; @@ -406,7 +407,7 @@ public: Ref<Material> get_debug_navigation_material(); Ref<Material> get_debug_navigation_disabled_material(); Ref<Material> get_debug_collision_material(); - Ref<Mesh> get_debug_contact_mesh(); + Ref<ArrayMesh> get_debug_contact_mesh(); int get_collision_debug_contact_count() { return collision_debug_contacts; } diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index f62cebe042..d8ec8b9991 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -117,7 +117,11 @@ bool ViewportTexture::has_alpha() const { return false; } +Ref<Image> ViewportTexture::get_data() const { + ERR_FAIL_COND_V(!vp, Ref<Image>()); + return VS::get_singleton()->texture_get_data(vp->texture_rid); +} void ViewportTexture::set_flags(uint32_t p_flags) { if (!vp) @@ -512,7 +516,7 @@ void Viewport::_notification(int p_what) { if (mm.is_valid()) { - pos = mm->get_pos(); + pos = mm->get_position(); motion_tested = true; physics_last_mousepos = pos; } @@ -520,19 +524,19 @@ void Viewport::_notification(int p_what) { Ref<InputEventMouseButton> mb = ev; if (mb.is_valid()) { - pos = mb->get_pos(); + pos = mb->get_position(); } Ref<InputEventScreenDrag> sd = ev; if (sd.is_valid()) { - pos = sd->get_pos(); + pos = sd->get_position(); } Ref<InputEventScreenTouch> st = ev; if (st.is_valid()) { - pos = st->get_pos(); + pos = st->get_position(); } if (ss2d) { @@ -1152,6 +1156,7 @@ void Viewport::set_size_override(bool p_enable, const Size2 &p_size, const Vecto size_override_margin = p_margin; _update_rect(); _update_stretch_transform(); + emit_signal("size_changed"); } Size2 Viewport::get_size_override() const { @@ -1310,7 +1315,7 @@ Transform2D Viewport::_get_input_pre_xform() const { if (to_screen_rect != Rect2()) { - pre_xf.elements[2] = -to_screen_rect.pos; + pre_xf.elements[2] = -to_screen_rect.position; pre_xf.scale(size / to_screen_rect.size); } @@ -1473,17 +1478,17 @@ void Viewport::_gui_show_tooltip() { gui.tooltip_label->set_text(tooltip); Rect2 r(gui.tooltip_pos + Point2(10, 10), gui.tooltip_label->get_combined_minimum_size() + ttp->get_minimum_size()); Rect2 vr = gui.tooltip_label->get_viewport_rect(); - if (r.size.x + r.pos.x > vr.size.x) - r.pos.x = vr.size.x - r.size.x; - else if (r.pos.x < 0) - r.pos.x = 0; + if (r.size.x + r.position.x > vr.size.x) + r.position.x = vr.size.x - r.size.x; + else if (r.position.x < 0) + r.position.x = 0; - if (r.size.y + r.pos.y > vr.size.y) - r.pos.y = vr.size.y - r.size.y; - else if (r.pos.y < 0) - r.pos.y = 0; + if (r.size.y + r.position.y > vr.size.y) + r.position.y = vr.size.y - r.size.y; + else if (r.position.y < 0) + r.position.y = 0; - gui.tooltip_popup->set_global_position(r.pos); + gui.tooltip_popup->set_global_position(r.position); gui.tooltip_popup->set_size(r.size); gui.tooltip_popup->raise(); @@ -1685,7 +1690,7 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { gui.key_event_accepted = false; - Point2 mpos = mb->get_pos(); + Point2 mpos = mb->get_position(); if (mb->is_pressed()) { Size2 pos = mpos; @@ -1740,11 +1745,11 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { mb = mb->xformed_by(Transform2D()); // make a copy of the event - mb->set_global_pos(pos); + mb->set_global_position(pos); pos = gui.focus_inv_xform.xform(pos); - mb->set_pos(pos); + mb->set_position(pos); #ifdef DEBUG_ENABLED if (ScriptDebugger::get_singleton()) { @@ -1841,9 +1846,9 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { Size2 pos = mpos; mb = mb->xformed_by(Transform2D()); //make a copy - mb->set_global_pos(pos); + mb->set_global_position(pos); pos = gui.focus_inv_xform.xform(pos); - mb->set_pos(pos); + mb->set_position(pos); if (gui.mouse_focus->can_process()) { _gui_call_input(gui.mouse_focus, mb); @@ -1869,7 +1874,7 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { if (mm.is_valid()) { gui.key_event_accepted = false; - Point2 mpos = mm->get_pos(); + Point2 mpos = mm->get_position(); gui.last_mouse_pos = mpos; @@ -1961,7 +1966,7 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { mm = mm->xformed_by(Transform2D()); //make a copy - mm->set_global_pos(mpos); + mm->set_global_position(mpos); mm->set_speed(speed); mm->set_relative(rel); @@ -1999,7 +2004,7 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { //pos = gui.focus_inv_xform.xform(pos); - mm->set_pos(pos); + mm->set_position(pos); Control::CursorShape cursor_shape = over->get_cursor_shape(pos); OS::get_singleton()->set_cursor_shape((OS::CursorShape)cursor_shape); @@ -2318,7 +2323,7 @@ void Viewport::_gui_grab_click_focus(Control *p_control) { //send unclic Point2 click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos); - mb->set_pos(click); + mb->set_position(click); mb->set_button_index(gui.mouse_focus_button); mb->set_pressed(false); gui.mouse_focus->call_deferred(SceneStringNames::get_singleton()->_gui_input, mb); @@ -2326,7 +2331,7 @@ void Viewport::_gui_grab_click_focus(Control *p_control) { gui.mouse_focus = p_control; gui.focus_inv_xform = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse(); click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos); - mb->set_pos(click); + mb->set_position(click); mb->set_button_index(gui.mouse_focus_button); mb->set_pressed(true); gui.mouse_focus->call_deferred(SceneStringNames::get_singleton()->_gui_input, mb); @@ -2516,6 +2521,32 @@ bool Viewport::get_hdr() const { return hdr; } +void Viewport::set_usage(Usage p_usage) { + + usage = p_usage; + VS::get_singleton()->viewport_set_usage(viewport, VS::ViewportUsage(p_usage)); +} + +Viewport::Usage Viewport::get_usage() const { + return usage; +} + +void Viewport::set_debug_draw(DebugDraw p_debug_draw) { + + debug_draw = p_debug_draw; + VS::get_singleton()->viewport_set_debug_draw(viewport, VS::ViewportDebugDraw(p_debug_draw)); +} + +Viewport::DebugDraw Viewport::get_debug_draw() const { + + return debug_draw; +} + +int Viewport::get_render_info(RenderInfo p_info) { + + return VS::get_singleton()->viewport_get_render_info(viewport, VS::ViewportRenderInfo(p_info)); +} + void Viewport::_bind_methods() { ClassDB::bind_method(D_METHOD("set_size", "size"), &Viewport::set_size); @@ -2569,6 +2600,14 @@ void Viewport::_bind_methods() { ClassDB::bind_method(D_METHOD("set_hdr", "enable"), &Viewport::set_hdr); ClassDB::bind_method(D_METHOD("get_hdr"), &Viewport::get_hdr); + ClassDB::bind_method(D_METHOD("set_usage", "usage"), &Viewport::set_usage); + ClassDB::bind_method(D_METHOD("get_usage"), &Viewport::get_usage); + + ClassDB::bind_method(D_METHOD("set_debug_draw", "debug_draw"), &Viewport::set_debug_draw); + ClassDB::bind_method(D_METHOD("get_debug_draw"), &Viewport::get_debug_draw); + + ClassDB::bind_method(D_METHOD("get_render_info", "info"), &Viewport::get_render_info); + ClassDB::bind_method(D_METHOD("get_texture:ViewportTexture"), &Viewport::get_texture); ClassDB::bind_method(D_METHOD("set_physics_object_picking", "enable"), &Viewport::set_physics_object_picking); @@ -2622,6 +2661,8 @@ void Viewport::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x"), "set_msaa", "get_msaa"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hdr"), "set_hdr", "get_hdr"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_3d"), "set_disable_3d", "is_3d_disabled"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "usage", PROPERTY_HINT_ENUM, "2D,2D No-Sampling,3D,3D No-Effects"), "set_usage", "get_usage"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_draw", PROPERTY_HINT_ENUM, "Disabled,Unshaded,Overdraw,Wireframe"), "set_debug_draw", "get_debug_draw"); ADD_GROUP("Render Target", "render_target_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "render_target_v_flip"), "set_vflip", "get_vflip"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "render_target_clear_on_new_frame"), "set_clear_on_new_frame", "get_clear_on_new_frame"); @@ -2656,6 +2697,19 @@ void Viewport::_bind_methods() { BIND_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1024); BIND_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_MAX); + BIND_CONSTANT(RENDER_INFO_OBJECTS_IN_FRAME); + BIND_CONSTANT(RENDER_INFO_VERTICES_IN_FRAME); + BIND_CONSTANT(RENDER_INFO_MATERIAL_CHANGES_IN_FRAME); + BIND_CONSTANT(RENDER_INFO_SHADER_CHANGES_IN_FRAME); + BIND_CONSTANT(RENDER_INFO_SURFACE_CHANGES_IN_FRAME); + BIND_CONSTANT(RENDER_INFO_DRAW_CALLS_IN_FRAME); + BIND_CONSTANT(RENDER_INFO_MAX); + + BIND_CONSTANT(DEBUG_DRAW_DISABLED); + BIND_CONSTANT(DEBUG_DRAW_UNSHADED); + BIND_CONSTANT(DEBUG_DRAW_OVERDRAW); + BIND_CONSTANT(DEBUG_DRAW_WIREFRAME); + BIND_CONSTANT(MSAA_DISABLED); BIND_CONSTANT(MSAA_2X); BIND_CONSTANT(MSAA_4X); @@ -2730,6 +2784,9 @@ Viewport::Viewport() { msaa = MSAA_DISABLED; hdr = false; + + usage = USAGE_3D; + debug_draw = DEBUG_DRAW_DISABLED; } Viewport::~Viewport() { diff --git a/scene/main/viewport.h b/scene/main/viewport.h index 7470cefb49..378055bef5 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -77,6 +77,8 @@ public: virtual void set_flags(uint32_t p_flags); virtual uint32_t get_flags() const; + virtual Ref<Image> get_data() const; + ViewportTexture(); ~ViewportTexture(); }; @@ -113,6 +115,31 @@ public: MSAA_16X, }; + enum Usage { + USAGE_2D, + USAGE_2D_NO_SAMPLING, + USAGE_3D, + USAGE_3D_NO_EFFECTS, + }; + + enum RenderInfo { + + RENDER_INFO_OBJECTS_IN_FRAME, + RENDER_INFO_VERTICES_IN_FRAME, + RENDER_INFO_MATERIAL_CHANGES_IN_FRAME, + RENDER_INFO_SHADER_CHANGES_IN_FRAME, + RENDER_INFO_SURFACE_CHANGES_IN_FRAME, + RENDER_INFO_DRAW_CALLS_IN_FRAME, + RENDER_INFO_MAX + }; + + enum DebugDraw { + DEBUG_DRAW_DISABLED, + DEBUG_DRAW_UNSHADED, + DEBUG_DRAW_OVERDRAW, + DEBUG_DRAW_WIREFRAME, + }; + private: friend class ViewportTexture; @@ -195,6 +222,10 @@ private: RID texture_rid; uint32_t texture_flags; + DebugDraw debug_draw; + + Usage usage; + int shadow_atlas_size; ShadowAtlasQuadrantSubdiv shadow_atlas_quadrant_subdiv[4]; @@ -416,6 +447,14 @@ public: virtual String get_configuration_warning() const; + void set_usage(Usage p_usage); + Usage get_usage() const; + + void set_debug_draw(DebugDraw p_debug_draw); + DebugDraw get_debug_draw() const; + + int get_render_info(RenderInfo p_info); + Viewport(); ~Viewport(); }; @@ -423,5 +462,8 @@ public: VARIANT_ENUM_CAST(Viewport::UpdateMode); VARIANT_ENUM_CAST(Viewport::ShadowAtlasQuadrantSubdiv); VARIANT_ENUM_CAST(Viewport::MSAA); +VARIANT_ENUM_CAST(Viewport::Usage); +VARIANT_ENUM_CAST(Viewport::DebugDraw); +VARIANT_ENUM_CAST(Viewport::RenderInfo); #endif diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 0a3f64eacd..9f078072d7 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -523,11 +523,12 @@ void register_scene_types() { ClassDB::register_virtual_class<Shader>(); #ifndef _3D_DISABLED - ClassDB::register_class<Mesh>(); - ClassDB::register_class<QuadMesh>(); + ClassDB::register_virtual_class<Mesh>(); + ClassDB::register_class<ArrayMesh>(); ClassDB::register_virtual_class<Material>(); ClassDB::register_class<SpatialMaterial>(); ClassDB::add_compatibility_class("FixedSpatialMaterial", "SpatialMaterial"); + ClassDB::add_compatibility_class("Mesh", "ArrayMesh"); SceneTree::add_idle_callback(SpatialMaterial::flush_changes); SpatialMaterial::init_shaders(); diff --git a/scene/resources/animation.cpp b/scene/resources/animation.cpp index 545c700354..459d3ffe41 100644 --- a/scene/resources/animation.cpp +++ b/scene/resources/animation.cpp @@ -1133,7 +1133,7 @@ Variant Animation::_cubic_interpolate(const Variant &p_pre_a, const Variant &p_a Rect2 pb = p_post_b; return Rect2( - a.pos.cubic_interpolate(b.pos, pa.pos, pb.pos, p_c), + a.position.cubic_interpolate(b.position, pa.position, pb.position, p_c), a.size.cubic_interpolate(b.size, pa.size, pb.size, p_c)); } break; @@ -1165,7 +1165,7 @@ Variant Animation::_cubic_interpolate(const Variant &p_pre_a, const Variant &p_a Rect3 pb = p_post_b; return Rect3( - a.pos.cubic_interpolate(b.pos, pa.pos, pb.pos, p_c), + a.position.cubic_interpolate(b.position, pa.position, pb.position, p_c), a.size.cubic_interpolate(b.size, pa.size, pb.size, p_c)); } break; default: { diff --git a/scene/resources/bit_mask.cpp b/scene/resources/bit_mask.cpp index e512f8a968..a473067937 100644 --- a/scene/resources/bit_mask.cpp +++ b/scene/resources/bit_mask.cpp @@ -67,9 +67,9 @@ void BitMap::set_bit_rect(const Rect2 &p_rect, bool p_value) { Rect2i current = Rect2i(0, 0, width, height).clip(p_rect); uint8_t *data = bitmask.ptr(); - for (int i = current.pos.x; i < current.pos.x + current.size.x; i++) { + for (int i = current.position.x; i < current.position.x + current.size.x; i++) { - for (int j = current.pos.y; j < current.pos.y + current.size.y; j++) { + for (int j = current.position.y; j < current.position.y + current.size.y; j++) { int ofs = width * j + i; int bbyte = ofs / 8; diff --git a/scene/resources/box_shape.cpp b/scene/resources/box_shape.cpp index fedfc71313..d5c25b718e 100644 --- a/scene/resources/box_shape.cpp +++ b/scene/resources/box_shape.cpp @@ -34,8 +34,8 @@ Vector<Vector3> BoxShape::_gen_debug_mesh_lines() { Vector<Vector3> lines; Rect3 aabb; - aabb.pos = -get_extents(); - aabb.size = aabb.pos * -2; + aabb.position = -get_extents(); + aabb.size = aabb.position * -2; for (int i = 0; i < 12; i++) { Vector3 a, b; diff --git a/scene/resources/capsule_shape_2d.cpp b/scene/resources/capsule_shape_2d.cpp index 3f41526bcb..20177692a1 100644 --- a/scene/resources/capsule_shape_2d.cpp +++ b/scene/resources/capsule_shape_2d.cpp @@ -80,7 +80,7 @@ Rect2 CapsuleShape2D::get_rect() const { Vector2 he = Point2(get_radius(), get_radius() + get_height() * 0.5); Rect2 rect; - rect.pos = -he; + rect.position = -he; rect.size = he * 2.0; return rect; } diff --git a/scene/resources/circle_shape_2d.cpp b/scene/resources/circle_shape_2d.cpp index 4512e1c4a9..1b7e09ddfb 100644 --- a/scene/resources/circle_shape_2d.cpp +++ b/scene/resources/circle_shape_2d.cpp @@ -58,7 +58,7 @@ void CircleShape2D::_bind_methods() { Rect2 CircleShape2D::get_rect() const { Rect2 rect; - rect.pos = -Point2(get_radius(), get_radius()); + rect.position = -Point2(get_radius(), get_radius()); rect.size = Point2(get_radius(), get_radius()) * 2.0; return rect; } diff --git a/scene/resources/concave_polygon_shape_2d.cpp b/scene/resources/concave_polygon_shape_2d.cpp index 653540a663..b8c931421e 100644 --- a/scene/resources/concave_polygon_shape_2d.cpp +++ b/scene/resources/concave_polygon_shape_2d.cpp @@ -68,7 +68,7 @@ Rect2 ConcavePolygonShape2D::get_rect() const { PoolVector<Vector2>::Read r = s.read(); for (int i = 0; i < len; i++) { if (i == 0) - rect.pos = r[i]; + rect.position = r[i]; else rect.expand_to(r[i]); } diff --git a/scene/resources/convex_polygon_shape_2d.cpp b/scene/resources/convex_polygon_shape_2d.cpp index fcbf66c8cd..e13f7faf70 100644 --- a/scene/resources/convex_polygon_shape_2d.cpp +++ b/scene/resources/convex_polygon_shape_2d.cpp @@ -78,7 +78,7 @@ Rect2 ConvexPolygonShape2D::get_rect() const { Rect2 rect; for (int i = 0; i < points.size(); i++) { if (i == 0) - rect.pos = points[i]; + rect.position = points[i]; else rect.expand_to(points[i]); } diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 7e5065a03b..668a8ff66f 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -138,8 +138,8 @@ static Ref<BitmapFont> make_font(int p_height, int p_ascent, int p_valign, int p int chr = c[0]; Rect2 frect; - frect.pos.x = c[1]; - frect.pos.y = c[2]; + frect.position.x = c[1]; + frect.position.y = c[2]; frect.size.x = c[3]; frect.size.y = c[4]; Point2 align(c[5], c[6] + p_valign); @@ -170,8 +170,8 @@ static Ref<BitmapFont> make_font2(int p_height, int p_ascent, int p_charcount, c int chr = c[0]; Rect2 frect; - frect.pos.x = c[1]; - frect.pos.y = c[2]; + frect.position.x = c[1]; + frect.position.y = c[2]; frect.size.x = c[3]; frect.size.y = c[4]; Point2 align(c[6], c[5]); @@ -626,6 +626,9 @@ void fill_default_theme(Ref<Theme> &t, const Ref<Font> &default_font, const Ref< t->set_stylebox("title_button_normal", "Tree", make_stylebox(tree_title_png, 4, 4, 4, 4)); t->set_stylebox("title_button_pressed", "Tree", make_stylebox(tree_title_pressed_png, 4, 4, 4, 4)); t->set_stylebox("title_button_hover", "Tree", make_stylebox(tree_title_png, 4, 4, 4, 4)); + t->set_stylebox("custom_button", "Tree", sb_button_normal); + t->set_stylebox("custom_button_pressed", "Tree", sb_button_pressed); + t->set_stylebox("custom_button_hover", "Tree", sb_button_hover); t->set_icon("checked", "Tree", make_icon(checked_png)); t->set_icon("unchecked", "Tree", make_icon(unchecked_png)); @@ -645,6 +648,7 @@ void fill_default_theme(Ref<Theme> &t, const Ref<Font> &default_font, const Ref< t->set_color("guide_color", "Tree", Color(0, 0, 0, 0.1)); t->set_color("drop_position_color", "Tree", Color(1, 0.3, 0.2)); t->set_color("relationship_line_color", "Tree", Color::html("464646")); + t->set_color("custom_button_font_highlight", "Tree", control_font_color_hover); t->set_constant("hseparation", "Tree", 4 * scale); t->set_constant("vseparation", "Tree", 4 * scale); diff --git a/scene/resources/environment.cpp b/scene/resources/environment.cpp index 66f913f347..e40fc0d4be 100644 --- a/scene/resources/environment.cpp +++ b/scene/resources/environment.cpp @@ -217,6 +217,7 @@ void Environment::set_adjustment_enable(bool p_enable) { adjustment_enabled = p_enable; VS::get_singleton()->environment_set_adjustment(environment, adjustment_enabled, adjustment_brightness, adjustment_contrast, adjustment_saturation, adjustment_color_correction.is_valid() ? adjustment_color_correction->get_rid() : RID()); + _change_notify(); } bool Environment::is_adjustment_enabled() const { @@ -283,12 +284,39 @@ void Environment::_validate_property(PropertyInfo &property) const { property.usage = PROPERTY_USAGE_NOEDITOR; } } + + static const char *hide_prefixes[] = { + "fog_", + "auto_exposure_", + "ss_reflections_", + "ssao_", + "dof_blur_far_", + "dof_blur_near_", + "glow_", + "adjustment_", + NULL + + }; + + const char **prefixes = hide_prefixes; + while (*prefixes) { + String prefix = String(*prefixes); + + String enabled = prefix + "enabled"; + if (property.name.begins_with(prefix) && property.name != enabled && !bool(get(enabled))) { + property.usage = PROPERTY_USAGE_NOEDITOR; + return; + } + + prefixes++; + } } void Environment::set_ssr_enabled(bool p_enable) { ssr_enabled = p_enable; VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_accel, ssr_fade, ssr_depth_tolerance, ssr_smooth, ssr_roughness); + _change_notify(); } bool Environment::is_ssr_enabled() const { @@ -360,6 +388,7 @@ void Environment::set_ssao_enabled(bool p_enable) { ssao_enabled = p_enable; VS::get_singleton()->environment_set_ssao(environment, ssao_enabled, ssao_radius, ssao_intensity, ssao_radius2, ssao_intensity2, ssao_bias, ssao_direct_light_affect, ssao_color, ssao_blur); + _change_notify(); } bool Environment::is_ssao_enabled() const { @@ -453,6 +482,7 @@ void Environment::set_glow_enabled(bool p_enabled) { glow_enabled = p_enabled; VS::get_singleton()->environment_set_glow(environment, glow_enabled, glow_levels, glow_intensity, glow_strength, glow_bloom, VS::EnvironmentGlowBlendMode(glow_blend_mode), glow_hdr_bleed_treshold, glow_hdr_bleed_treshold, glow_bicubic_upscale); + _change_notify(); } bool Environment::is_glow_enabled() const { @@ -558,6 +588,7 @@ void Environment::set_dof_blur_far_enabled(bool p_enable) { dof_blur_far_enabled = p_enable; VS::get_singleton()->environment_set_dof_blur_far(environment, dof_blur_far_enabled, dof_blur_far_distance, dof_blur_far_transition, dof_blur_far_amount, VS::EnvironmentDOFBlurQuality(dof_blur_far_quality)); + _change_notify(); } bool Environment::is_dof_blur_far_enabled() const { @@ -610,6 +641,7 @@ void Environment::set_dof_blur_near_enabled(bool p_enable) { dof_blur_near_enabled = p_enable; VS::get_singleton()->environment_set_dof_blur_near(environment, dof_blur_near_enabled, dof_blur_near_distance, dof_blur_near_transition, dof_blur_near_amount, VS::EnvironmentDOFBlurQuality(dof_blur_near_quality)); + _change_notify(); } bool Environment::is_dof_blur_near_enabled() const { @@ -661,6 +693,138 @@ Environment::DOFBlurQuality Environment::get_dof_blur_near_quality() const { return dof_blur_near_quality; } +void Environment::set_fog_enabled(bool p_enabled) { + + fog_enabled = p_enabled; + VS::get_singleton()->environment_set_fog(environment, fog_enabled, fog_color, fog_sun_color, fog_sun_amount); + _change_notify(); +} + +bool Environment::is_fog_enabled() const { + + return fog_enabled; +} + +void Environment::set_fog_color(const Color &p_color) { + + fog_color = p_color; + VS::get_singleton()->environment_set_fog(environment, fog_enabled, fog_color, fog_sun_color, fog_sun_amount); +} +Color Environment::get_fog_color() const { + + return fog_color; +} + +void Environment::set_fog_sun_color(const Color &p_color) { + + fog_sun_color = p_color; + VS::get_singleton()->environment_set_fog(environment, fog_enabled, fog_color, fog_sun_color, fog_sun_amount); +} +Color Environment::get_fog_sun_color() const { + + return fog_sun_color; +} + +void Environment::set_fog_sun_amount(float p_amount) { + + fog_sun_amount = p_amount; + VS::get_singleton()->environment_set_fog(environment, fog_enabled, fog_color, fog_sun_color, fog_sun_amount); +} +float Environment::get_fog_sun_amount() const { + + return fog_sun_amount; +} + +void Environment::set_fog_depth_enabled(bool p_enabled) { + + fog_depth_enabled = p_enabled; + VS::get_singleton()->environment_set_fog_depth(environment, fog_depth_enabled, fog_depth_begin, fog_depth_curve, fog_transmit_enabled, fog_transmit_curve); +} +bool Environment::is_fog_depth_enabled() const { + + return fog_depth_enabled; +} + +void Environment::set_fog_depth_begin(float p_distance) { + + fog_depth_begin = p_distance; + VS::get_singleton()->environment_set_fog_depth(environment, fog_depth_enabled, fog_depth_begin, fog_depth_curve, fog_transmit_enabled, fog_transmit_curve); +} +float Environment::get_fog_depth_begin() const { + + return fog_depth_begin; +} + +void Environment::set_fog_depth_curve(float p_curve) { + + fog_depth_curve = p_curve; + VS::get_singleton()->environment_set_fog_depth(environment, fog_depth_enabled, fog_depth_begin, fog_depth_curve, fog_transmit_enabled, fog_transmit_curve); +} +float Environment::get_fog_depth_curve() const { + + return fog_depth_curve; +} + +void Environment::set_fog_transmit_enabled(bool p_enabled) { + + fog_transmit_enabled = p_enabled; + VS::get_singleton()->environment_set_fog_depth(environment, fog_depth_enabled, fog_depth_begin, fog_depth_curve, fog_transmit_enabled, fog_transmit_curve); +} +bool Environment::is_fog_transmit_enabled() const { + + return fog_transmit_enabled; +} + +void Environment::set_fog_transmit_curve(float p_curve) { + + fog_transmit_curve = p_curve; + VS::get_singleton()->environment_set_fog_depth(environment, fog_depth_enabled, fog_depth_begin, fog_depth_curve, fog_transmit_enabled, fog_transmit_curve); +} +float Environment::get_fog_transmit_curve() const { + + return fog_transmit_curve; +} + +void Environment::set_fog_height_enabled(bool p_enabled) { + + fog_height_enabled = p_enabled; + VS::get_singleton()->environment_set_fog_height(environment, fog_height_enabled, fog_height_min, fog_height_max, fog_height_curve); +} +bool Environment::is_fog_height_enabled() const { + + return fog_height_enabled; +} + +void Environment::set_fog_height_min(float p_distance) { + + fog_height_min = p_distance; + VS::get_singleton()->environment_set_fog_height(environment, fog_height_enabled, fog_height_min, fog_height_max, fog_height_curve); +} +float Environment::get_fog_height_min() const { + + return fog_height_min; +} + +void Environment::set_fog_height_max(float p_distance) { + + fog_height_max = p_distance; + VS::get_singleton()->environment_set_fog_height(environment, fog_height_enabled, fog_height_min, fog_height_max, fog_height_curve); +} +float Environment::get_fog_height_max() const { + + return fog_height_max; +} + +void Environment::set_fog_height_curve(float p_distance) { + + fog_height_curve = p_distance; + VS::get_singleton()->environment_set_fog_height(environment, fog_height_enabled, fog_height_min, fog_height_max, fog_height_curve); +} +float Environment::get_fog_height_curve() const { + + return fog_height_curve; +} + void Environment::_bind_methods() { ClassDB::bind_method(D_METHOD("set_background", "mode"), &Environment::set_background); @@ -695,6 +859,60 @@ void Environment::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::REAL, "ambient_light_energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_ambient_light_energy", "get_ambient_light_energy"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "ambient_light_sky_contribution", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_ambient_light_sky_contribution", "get_ambient_light_sky_contribution"); + ClassDB::bind_method(D_METHOD("set_fog_enabled", "enabled"), &Environment::set_fog_enabled); + ClassDB::bind_method(D_METHOD("is_fog_enabled"), &Environment::is_fog_enabled); + + ClassDB::bind_method(D_METHOD("set_fog_color", "color"), &Environment::set_fog_color); + ClassDB::bind_method(D_METHOD("get_fog_color"), &Environment::get_fog_color); + + ClassDB::bind_method(D_METHOD("set_fog_sun_color", "color"), &Environment::set_fog_sun_color); + ClassDB::bind_method(D_METHOD("get_fog_sun_color"), &Environment::get_fog_sun_color); + + ClassDB::bind_method(D_METHOD("set_fog_sun_amount", "amount"), &Environment::set_fog_sun_amount); + ClassDB::bind_method(D_METHOD("get_fog_sun_amount"), &Environment::get_fog_sun_amount); + + ClassDB::bind_method(D_METHOD("set_fog_depth_enabled", "enabled"), &Environment::set_fog_depth_enabled); + ClassDB::bind_method(D_METHOD("is_fog_depth_enabled"), &Environment::is_fog_depth_enabled); + + ClassDB::bind_method(D_METHOD("set_fog_depth_begin", "distance"), &Environment::set_fog_depth_begin); + ClassDB::bind_method(D_METHOD("get_fog_depth_begin"), &Environment::get_fog_depth_begin); + + ClassDB::bind_method(D_METHOD("set_fog_depth_curve", "curve"), &Environment::set_fog_depth_curve); + ClassDB::bind_method(D_METHOD("get_fog_depth_curve"), &Environment::get_fog_depth_curve); + + ClassDB::bind_method(D_METHOD("set_fog_transmit_enabled", "enabled"), &Environment::set_fog_transmit_enabled); + ClassDB::bind_method(D_METHOD("is_fog_transmit_enabled"), &Environment::is_fog_transmit_enabled); + + ClassDB::bind_method(D_METHOD("set_fog_transmit_curve", "curve"), &Environment::set_fog_transmit_curve); + ClassDB::bind_method(D_METHOD("get_fog_transmit_curve"), &Environment::get_fog_transmit_curve); + + ClassDB::bind_method(D_METHOD("set_fog_height_enabled", "enabled"), &Environment::set_fog_height_enabled); + ClassDB::bind_method(D_METHOD("is_fog_height_enabled"), &Environment::is_fog_height_enabled); + + ClassDB::bind_method(D_METHOD("set_fog_height_min", "height"), &Environment::set_fog_height_min); + ClassDB::bind_method(D_METHOD("get_fog_height_min"), &Environment::get_fog_height_min); + + ClassDB::bind_method(D_METHOD("set_fog_height_max", "height"), &Environment::set_fog_height_max); + ClassDB::bind_method(D_METHOD("get_fog_height_max"), &Environment::get_fog_height_max); + + ClassDB::bind_method(D_METHOD("set_fog_height_curve", "curve"), &Environment::set_fog_height_curve); + ClassDB::bind_method(D_METHOD("get_fog_height_curve"), &Environment::get_fog_height_curve); + + ADD_GROUP("Fog", "fog_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fog_enabled"), "set_fog_enabled", "is_fog_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "fog_color"), "set_fog_color", "get_fog_color"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "fog_sun_color"), "set_fog_sun_color", "get_fog_sun_color"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "fog_sun_amount", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_fog_sun_amount", "get_fog_sun_amount"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fog_depth_enabled"), "set_fog_depth_enabled", "is_fog_depth_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "fog_depth_begin", PROPERTY_HINT_RANGE, "0,4000,0.1"), "set_fog_depth_begin", "get_fog_depth_begin"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "fog_depth_curve", PROPERTY_HINT_EXP_EASING), "set_fog_depth_curve", "get_fog_depth_curve"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fog_transmit_enabled"), "set_fog_transmit_enabled", "is_fog_transmit_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "fog_transmit_curve", PROPERTY_HINT_EXP_EASING), "set_fog_transmit_curve", "get_fog_transmit_curve"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fog_height_enabled"), "set_fog_height_enabled", "is_fog_height_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "fog_height_min", PROPERTY_HINT_RANGE, "-4000,4000,0.1"), "set_fog_height_min", "get_fog_height_min"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "fog_height_max", PROPERTY_HINT_RANGE, "-4000,4000,0.1"), "set_fog_height_max", "get_fog_height_max"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "fog_height_curve", PROPERTY_HINT_EXP_EASING), "set_fog_height_curve", "get_fog_height_curve"); + ClassDB::bind_method(D_METHOD("set_tonemapper", "mode"), &Environment::set_tonemapper); ClassDB::bind_method(D_METHOD("get_tonemapper"), &Environment::get_tonemapper); @@ -997,6 +1215,27 @@ Environment::Environment() { dof_blur_near_transition = 1; dof_blur_near_amount = 0.1; dof_blur_near_quality = DOF_BLUR_QUALITY_MEDIUM; + + fog_enabled = false; + fog_color = Color(0.5, 0.5, 0.5); + fog_sun_color = Color(0.8, 0.8, 0.0); + fog_sun_amount = 0; + + fog_depth_enabled = true; + + fog_depth_begin = 10; + fog_depth_curve = 1; + + fog_transmit_enabled = false; + fog_transmit_curve = 1; + + fog_height_enabled = false; + fog_height_min = 0; + fog_height_max = 100; + fog_height_curve = 1; + + set_fog_color(Color(0.5, 0.6, 0.7)); + set_fog_sun_color(Color(1.0, 0.9, 0.7)); } Environment::~Environment() { diff --git a/scene/resources/environment.h b/scene/resources/environment.h index d9141ccd9c..7df3458231 100644 --- a/scene/resources/environment.h +++ b/scene/resources/environment.h @@ -138,6 +138,23 @@ private: float dof_blur_near_amount; DOFBlurQuality dof_blur_near_quality; + bool fog_enabled; + Color fog_color; + Color fog_sun_color; + float fog_sun_amount; + + bool fog_depth_enabled; + float fog_depth_begin; + float fog_depth_curve; + + bool fog_transmit_enabled; + float fog_transmit_curve; + + bool fog_height_enabled; + float fog_height_min; + float fog_height_max; + float fog_height_curve; + protected: static void _bind_methods(); virtual void _validate_property(PropertyInfo &property) const; @@ -307,6 +324,45 @@ public: void set_dof_blur_near_quality(DOFBlurQuality p_quality); DOFBlurQuality get_dof_blur_near_quality() const; + void set_fog_enabled(bool p_enabled); + bool is_fog_enabled() const; + + void set_fog_color(const Color &p_color); + Color get_fog_color() const; + + void set_fog_sun_color(const Color &p_color); + Color get_fog_sun_color() const; + + void set_fog_sun_amount(float p_amount); + float get_fog_sun_amount() const; + + void set_fog_depth_enabled(bool p_enabled); + bool is_fog_depth_enabled() const; + + void set_fog_depth_begin(float p_distance); + float get_fog_depth_begin() const; + + void set_fog_depth_curve(float p_curve); + float get_fog_depth_curve() const; + + void set_fog_transmit_enabled(bool p_enabled); + bool is_fog_transmit_enabled() const; + + void set_fog_transmit_curve(float p_curve); + float get_fog_transmit_curve() const; + + void set_fog_height_enabled(bool p_enabled); + bool is_fog_height_enabled() const; + + void set_fog_height_min(float p_distance); + float get_fog_height_min() const; + + void set_fog_height_max(float p_distance); + float get_fog_height_max() const; + + void set_fog_height_curve(float p_distance); + float get_fog_height_curve() const; + virtual RID get_rid() const; Environment(); diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp index 692dc47677..225a42f651 100644 --- a/scene/resources/font.cpp +++ b/scene/resources/font.cpp @@ -120,8 +120,8 @@ PoolVector<int> BitmapFont::_get_chars() const { const Character *c = char_map.getptr(*key); chars.push_back(*key); chars.push_back(c->texture_idx); - chars.push_back(c->rect.pos.x); - chars.push_back(c->rect.pos.y); + chars.push_back(c->rect.position.x); + chars.push_back(c->rect.position.y); chars.push_back(c->rect.size.x); chars.push_back(c->rect.size.y); @@ -272,9 +272,9 @@ Error BitmapFont::create_from_fnt(const String &p_string) { Rect2 rect; if (keys.has("x")) - rect.pos.x = keys["x"].to_int(); + rect.position.x = keys["x"].to_int(); if (keys.has("y")) - rect.pos.y = keys["y"].to_int(); + rect.position.y = keys["y"].to_int(); if (keys.has("width")) rect.size.width = keys["width"].to_int(); if (keys.has("height")) diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index 9184b86237..ce88325539 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -190,10 +190,9 @@ void SpatialMaterial::init_shaders() { shader_names->clearcoat = "clearcoat"; shader_names->clearcoat_gloss = "clearcoat_gloss"; shader_names->anisotropy = "anisotropy_ratio"; - shader_names->height_scale = "height_scale"; + shader_names->depth_scale = "depth_scale"; shader_names->subsurface_scattering_strength = "subsurface_scattering_strength"; shader_names->refraction = "refraction"; - shader_names->refraction_roughness = "refraction_roughness"; shader_names->point_size = "point_size"; shader_names->uv1_scale = "uv1_scale"; shader_names->uv1_offset = "uv1_offset"; @@ -203,6 +202,8 @@ void SpatialMaterial::init_shaders() { shader_names->particle_h_frames = "particle_h_frames"; shader_names->particle_v_frames = "particle_v_frames"; shader_names->particles_anim_loop = "particles_anim_loop"; + shader_names->depth_min_layers = "depth_min_layers"; + shader_names->depth_max_layers = "depth_max_layers"; shader_names->texture_names[TEXTURE_ALBEDO] = "texture_albedo"; shader_names->texture_names[TEXTURE_METALLIC] = "texture_metallic"; @@ -213,7 +214,7 @@ void SpatialMaterial::init_shaders() { shader_names->texture_names[TEXTURE_CLEARCOAT] = "texture_clearcoat"; shader_names->texture_names[TEXTURE_FLOWMAP] = "texture_flowmap"; shader_names->texture_names[TEXTURE_AMBIENT_OCCLUSION] = "texture_ambient_occlusion"; - shader_names->texture_names[TEXTURE_HEIGHT] = "texture_height"; + shader_names->texture_names[TEXTURE_DEPTH] = "texture_depth"; shader_names->texture_names[TEXTURE_SUBSURFACE_SCATTERING] = "texture_subsurface_scattering"; shader_names->texture_names[TEXTURE_REFRACTION] = "texture_refraction"; shader_names->texture_names[TEXTURE_DETAIL_MASK] = "texture_detail_mask"; @@ -266,7 +267,12 @@ void SpatialMaterial::_update_shader() { case BLEND_MODE_MUL: code += "blend_mul"; break; } - switch (depth_draw_mode) { + DepthDrawMode ddm = depth_draw_mode; + if (features[FEATURE_REFRACTION]) { + ddm = DEPTH_DRAW_ALWAYS; + } + + switch (ddm) { case DEPTH_DRAW_OPAQUE_ONLY: code += ",depth_draw_opaque"; break; case DEPTH_DRAW_ALWAYS: code += ",depth_draw_always"; break; case DEPTH_DRAW_DISABLED: code += ",depth_draw_never"; break; @@ -278,6 +284,12 @@ void SpatialMaterial::_update_shader() { case CULL_FRONT: code += ",cull_front"; break; case CULL_DISABLED: code += ",cull_disabled"; break; } + switch (diffuse_mode) { + case DIFFUSE_LAMBERT: code += ",diffuse_lambert"; break; + case DIFFUSE_HALF_LAMBERT: code += ",diffuse_half_lambert"; break; + case DIFFUSE_OREN_NAYAR: code += ",diffuse_oren_nayar"; break; + case DIFFUSE_BURLEY: code += ",diffuse_burley"; break; + } if (flags[FLAG_UNSHADED]) { code += ",unshaded"; @@ -314,6 +326,11 @@ void SpatialMaterial::_update_shader() { code += "uniform float emission_energy;\n"; } + if (features[FEATURE_REFRACTION]) { + code += "uniform sampler2D texture_refraction;\n"; + code += "uniform float refraction : hint_range(-16,16);\n"; + } + if (features[FEATURE_NORMAL_MAPPING]) { code += "uniform sampler2D texture_normal : hint_normal;\n"; code += "uniform float normal_scale : hint_range(-16,16);\n"; @@ -348,6 +365,13 @@ void SpatialMaterial::_update_shader() { code += "uniform sampler2D texture_subsurface_scattering : hint_white;\n"; } + if (features[FEATURE_DEPTH_MAPPING]) { + code += "uniform sampler2D texture_depth : hint_black;\n"; + code += "uniform float depth_scale;\n"; + code += "uniform int depth_min_layers;\n"; + code += "uniform int depth_max_layers;\n"; + } + code += "\n\n"; code += "void vertex() {\n"; @@ -421,10 +445,52 @@ void SpatialMaterial::_update_shader() { code += "\n\n"; code += "void fragment() {\n"; + code += "\tvec2 base_uv = UV;\n"; + if (features[FEATURE_DETAIL] && detail_uv == DETAIL_UV_2) { + code += "\tvec2 base_uv2 = UV2;\n"; + } + + if (features[FEATURE_DEPTH_MAPPING]) { + code += "\t{\n"; + code += "\t\tvec3 view_dir = normalize(normalize(-VERTEX)*mat3(TANGENT,BINORMAL,NORMAL));\n"; + + if (deep_parallax) { + code += "\t\tfloat num_layers = mix(float(depth_max_layers),float(depth_min_layers), abs(dot(vec3(0.0, 0.0, 1.0), view_dir)));\n"; + code += "\t\tfloat layer_depth = 1.0 / num_layers;\n"; + code += "\t\tfloat current_layer_depth = 0.0;\n"; + code += "\t\tvec2 P = view_dir.xy * depth_scale;\n"; + code += "\t\tvec2 delta = P / num_layers;\n"; + code += "\t\tvec2 ofs = base_uv;\n"; + code += "\t\tfloat depth = texture(texture_depth, ofs).r;\n"; + code += "\t\tfloat current_depth = 0.0;\n"; + code += "\t\twhile(current_depth < depth) {\n"; + code += "\t\t\tofs -= delta;\n"; + code += "\t\t\tdepth = texture(texture_depth, ofs).r;\n"; + code += "\t\t\tcurrent_depth += layer_depth;\n"; + code += "\t\t}\n"; + code += "\t\tvec2 prev_ofs = ofs + delta;\n"; + code += "\t\tfloat after_depth = depth - current_depth;\n"; + code += "\t\tfloat before_depth = texture(texture_depth, prev_ofs).r - current_depth + layer_depth;\n"; + code += "\t\tfloat weight = after_depth / (after_depth - before_depth);\n"; + code += "\t\tofs = mix(ofs,prev_ofs,weight);\n"; + + } else { + code += "\t\tfloat depth = texture(texture_depth, base_uv).r;\n"; + code += "\t\tvec2 ofs = base_uv - view_dir.xy / view_dir.z * (depth * depth_scale);\n"; + } + + code += "\t\tbase_uv=ofs;\n"; + if (features[FEATURE_DETAIL] && detail_uv == DETAIL_UV_2) { + code += "\t\tbase_uv2-=ofs;\n"; + } + + code += "\t}\n"; + } + if (flags[FLAG_USE_POINT_SIZE]) { code += "\tvec4 albedo_tex = texture(texture_albedo,POINT_COORD);\n"; } else { - code += "\tvec4 albedo_tex = texture(texture_albedo,UV);\n"; + code += "\tvec4 albedo_tex = texture(texture_albedo,base_uv);\n"; } if (flags[FLAG_ALBEDO_FROM_VERTEX_COLOR]) { @@ -432,52 +498,72 @@ void SpatialMaterial::_update_shader() { } code += "\tALBEDO = albedo.rgb * albedo_tex.rgb;\n"; - if (features[FEATURE_TRANSPARENT]) { - code += "\tALPHA = albedo.a * albedo_tex.a;\n"; + code += "\tfloat metallic_tex = texture(texture_metallic,base_uv).r;\n"; + code += "\tMETALLIC = metallic_tex * metallic;\n"; + code += "\tfloat roughness_tex = texture(texture_roughness,base_uv).r;\n"; + code += "\tROUGHNESS = roughness_tex * roughness;\n"; + code += "\tSPECULAR = specular;\n"; + + if (features[FEATURE_NORMAL_MAPPING]) { + code += "\tNORMALMAP = texture(texture_normal,base_uv).rgb;\n"; + code += "\tNORMALMAP_DEPTH = normal_scale;\n"; } if (features[FEATURE_EMISSION]) { - code += "\tEMISSION = (emission.rgb+texture(texture_emission,UV).rgb)*emission_energy;\n"; + code += "\tEMISSION = (emission.rgb+texture(texture_emission,base_uv).rgb)*emission_energy;\n"; } - if (features[FEATURE_NORMAL_MAPPING]) { - code += "\tNORMALMAP = texture(texture_normal,UV).rgb;\n"; - code += "\tNORMALMAP_DEPTH = normal_scale;\n"; + if (features[FEATURE_REFRACTION]) { + + if (features[FEATURE_NORMAL_MAPPING]) { + code += "\tvec3 ref_normal = normalize( mix(NORMAL,TANGENT * NORMALMAP.x + BINORMAL * NORMALMAP.y + NORMAL * NORMALMAP.z,NORMALMAP_DEPTH) ) * SIDE;\n"; + } else { + code += "\tvec3 ref_normal = NORMAL;\n"; + } + + code += "\tvec2 ref_ofs = SCREEN_UV - ref_normal.xy * texture(texture_refraction,base_uv).r * refraction;\n"; + code += "\tfloat ref_amount = 1.0 - albedo.a * albedo_tex.a;\n"; + code += "\tEMISSION += textureLod(SCREEN_TEXTURE,ref_ofs,ROUGHNESS * 8.0).rgb * ref_amount;\n"; + code += "\tALBEDO *= 1.0 - ref_amount;\n"; + code += "\tALPHA = 1.0;\n"; + + } else if (features[FEATURE_TRANSPARENT]) { + code += "\tALPHA = albedo.a * albedo_tex.a;\n"; } if (features[FEATURE_RIM]) { - code += "\tvec2 rim_tex = texture(texture_rim,UV).xw;\n"; + code += "\tvec2 rim_tex = texture(texture_rim,base_uv).xw;\n"; code += "\tRIM = rim*rim_tex.x;"; code += "\tRIM_TINT = rim_tint*rim_tex.y;\n"; } if (features[FEATURE_CLEARCOAT]) { - code += "\tvec2 clearcoat_tex = texture(texture_clearcoat,UV).xw;\n"; + code += "\tvec2 clearcoat_tex = texture(texture_clearcoat,base_uv).xw;\n"; code += "\tCLEARCOAT = clearcoat*clearcoat_tex.x;"; code += "\tCLEARCOAT_GLOSS = clearcoat_gloss*clearcoat_tex.y;\n"; } if (features[FEATURE_ANISOTROPY]) { - code += "\tvec4 anisotropy_tex = texture(texture_flowmap,UV);\n"; + code += "\tvec4 anisotropy_tex = texture(texture_flowmap,base_uv);\n"; code += "\tANISOTROPY = anisotropy_ratio*anisotropy_tex.a;\n"; code += "\tANISOTROPY_FLOW = anisotropy_tex.rg*2.0-1.0;\n"; } if (features[FEATURE_AMBIENT_OCCLUSION]) { - code += "\tAO = texture(texture_ambient_occlusion,UV).r;\n"; + code += "\tAO = texture(texture_ambient_occlusion,base_uv).r;\n"; } if (features[FEATURE_SUBSURACE_SCATTERING]) { - code += "\tfloat sss_tex = texture(texture_subsurface_scattering,UV).r;\n"; + code += "\tfloat sss_tex = texture(texture_subsurface_scattering,base_uv).r;\n"; code += "\tSSS_STRENGTH=subsurface_scattering_strength*sss_tex;\n"; } if (features[FEATURE_DETAIL]) { - String det_uv = detail_uv == DETAIL_UV_1 ? "UV" : "UV2"; + String det_uv = detail_uv == DETAIL_UV_1 ? "base_uv" : "base_uv2"; code += "\tvec4 detail_tex = texture(texture_detail_albedo," + det_uv + ");\n"; code += "\tvec4 detail_norm_tex = texture(texture_detail_normal," + det_uv + ");\n"; - code += "\tvec4 detail_mask_tex = texture(texture_detail_mask,UV);\n"; + code += "\tvec4 detail_mask_tex = texture(texture_detail_mask,base_uv);\n"; switch (detail_blend_mode) { case BLEND_MODE_MIX: { @@ -500,12 +586,6 @@ void SpatialMaterial::_update_shader() { code += "\tALBEDO.rgb = mix(ALBEDO.rgb,detail,detail_mask_tex.r);\n"; } - code += "\tfloat metallic_tex = texture(texture_metallic,UV).r;\n"; - code += "\tMETALLIC = metallic_tex * metallic;\n"; - code += "\tfloat roughness_tex = texture(texture_roughness,UV).r;\n"; - code += "\tROUGHNESS = roughness_tex * roughness;\n"; - code += "\tSPECULAR = specular;\n"; - code += "}\n"; ShaderData shader_data; @@ -687,15 +767,15 @@ float SpatialMaterial::get_anisotropy() const { return anisotropy; } -void SpatialMaterial::set_height_scale(float p_height_scale) { +void SpatialMaterial::set_depth_scale(float p_depth_scale) { - height_scale = p_height_scale; - VS::get_singleton()->material_set_param(_get_material(), shader_names->height_scale, p_height_scale); + depth_scale = p_depth_scale; + VS::get_singleton()->material_set_param(_get_material(), shader_names->depth_scale, p_depth_scale); } -float SpatialMaterial::get_height_scale() const { +float SpatialMaterial::get_depth_scale() const { - return height_scale; + return depth_scale; } void SpatialMaterial::set_subsurface_scattering_strength(float p_subsurface_scattering_strength) { @@ -720,16 +800,6 @@ float SpatialMaterial::get_refraction() const { return refraction; } -void SpatialMaterial::set_refraction_roughness(float p_refraction_roughness) { - - refraction_roughness = p_refraction_roughness; - VS::get_singleton()->material_set_param(_get_material(), shader_names->refraction_roughness, refraction_roughness); -} -float SpatialMaterial::get_refraction_roughness() const { - - return refraction_roughness; -} - void SpatialMaterial::set_detail_uv(DetailUV p_detail_uv) { if (detail_uv == p_detail_uv) @@ -857,6 +927,9 @@ void SpatialMaterial::_validate_feature(const String &text, Feature feature, Pro if (property.name.begins_with(text) && property.name != text + "_enabled" && !features[feature]) { property.usage = 0; } + if ((property.name == "depth_min_layers" || property.name == "depth_max_layers") && !deep_parallax) { + property.usage = 0; + } } void SpatialMaterial::_validate_property(PropertyInfo &property) const { @@ -866,7 +939,7 @@ void SpatialMaterial::_validate_property(PropertyInfo &property) const { _validate_feature("clearcoat", FEATURE_CLEARCOAT, property); _validate_feature("anisotropy", FEATURE_ANISOTROPY, property); _validate_feature("ao", FEATURE_AMBIENT_OCCLUSION, property); - _validate_feature("height", FEATURE_HEIGHT_MAPPING, property); + _validate_feature("depth", FEATURE_DEPTH_MAPPING, property); _validate_feature("subsurf_scatter", FEATURE_SUBSURACE_SCATTERING, property); _validate_feature("refraction", FEATURE_REFRACTION, property); _validate_feature("detail", FEATURE_DETAIL, property); @@ -985,6 +1058,39 @@ int SpatialMaterial::get_particles_anim_loop() const { return particles_anim_loop; } +void SpatialMaterial::set_depth_deep_parallax(bool p_enable) { + + deep_parallax = p_enable; + _queue_shader_change(); + _change_notify(); + ; +} + +bool SpatialMaterial::is_depth_deep_parallax_enabled() const { + + return deep_parallax; +} + +void SpatialMaterial::set_depth_deep_parallax_min_layers(int p_layer) { + + deep_parallax_min_layers = p_layer; + VS::get_singleton()->material_set_param(_get_material(), shader_names->depth_min_layers, p_layer); +} +int SpatialMaterial::get_depth_deep_parallax_min_layers() const { + + return deep_parallax_min_layers; +} + +void SpatialMaterial::set_depth_deep_parallax_max_layers(int p_layer) { + + deep_parallax_max_layers = p_layer; + VS::get_singleton()->material_set_param(_get_material(), shader_names->depth_max_layers, p_layer); +} +int SpatialMaterial::get_depth_deep_parallax_max_layers() const { + + return deep_parallax_max_layers; +} + void SpatialMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_albedo", "albedo"), &SpatialMaterial::set_albedo); @@ -1023,8 +1129,8 @@ void SpatialMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_anisotropy", "anisotropy"), &SpatialMaterial::set_anisotropy); ClassDB::bind_method(D_METHOD("get_anisotropy"), &SpatialMaterial::get_anisotropy); - ClassDB::bind_method(D_METHOD("set_height_scale", "height_scale"), &SpatialMaterial::set_height_scale); - ClassDB::bind_method(D_METHOD("get_height_scale"), &SpatialMaterial::get_height_scale); + ClassDB::bind_method(D_METHOD("set_depth_scale", "depth_scale"), &SpatialMaterial::set_depth_scale); + ClassDB::bind_method(D_METHOD("get_depth_scale"), &SpatialMaterial::get_depth_scale); ClassDB::bind_method(D_METHOD("set_subsurface_scattering_strength", "strength"), &SpatialMaterial::set_subsurface_scattering_strength); ClassDB::bind_method(D_METHOD("get_subsurface_scattering_strength"), &SpatialMaterial::get_subsurface_scattering_strength); @@ -1032,9 +1138,6 @@ void SpatialMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_refraction", "refraction"), &SpatialMaterial::set_refraction); ClassDB::bind_method(D_METHOD("get_refraction"), &SpatialMaterial::get_refraction); - ClassDB::bind_method(D_METHOD("set_refraction_roughness", "refraction_roughness"), &SpatialMaterial::set_refraction_roughness); - ClassDB::bind_method(D_METHOD("get_refraction_roughness"), &SpatialMaterial::get_refraction_roughness); - ClassDB::bind_method(D_METHOD("set_line_width", "line_width"), &SpatialMaterial::set_line_width); ClassDB::bind_method(D_METHOD("get_line_width"), &SpatialMaterial::get_line_width); @@ -1092,6 +1195,15 @@ void SpatialMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_particles_anim_loop", "frames"), &SpatialMaterial::set_particles_anim_loop); ClassDB::bind_method(D_METHOD("get_particles_anim_loop"), &SpatialMaterial::get_particles_anim_loop); + ClassDB::bind_method(D_METHOD("set_depth_deep_parallax", "enable"), &SpatialMaterial::set_depth_deep_parallax); + ClassDB::bind_method(D_METHOD("is_depth_deep_parallax_enabled"), &SpatialMaterial::is_depth_deep_parallax_enabled); + + ClassDB::bind_method(D_METHOD("set_depth_deep_parallax_min_layers", "layer"), &SpatialMaterial::set_depth_deep_parallax_min_layers); + ClassDB::bind_method(D_METHOD("get_depth_deep_parallax_min_layers"), &SpatialMaterial::get_depth_deep_parallax_min_layers); + + ClassDB::bind_method(D_METHOD("set_depth_deep_parallax_max_layers", "layer"), &SpatialMaterial::set_depth_deep_parallax_max_layers); + ClassDB::bind_method(D_METHOD("get_depth_deep_parallax_max_layers"), &SpatialMaterial::get_depth_deep_parallax_max_layers); + ADD_GROUP("Flags", "flags_"); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_transparent"), "set_feature", "get_feature", FEATURE_TRANSPARENT); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_unshaded"), "set_flag", "get_flag", FLAG_UNSHADED); @@ -1160,10 +1272,13 @@ void SpatialMaterial::_bind_methods() { ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "ao_enabled"), "set_feature", "get_feature", FEATURE_AMBIENT_OCCLUSION); ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "ao_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_AMBIENT_OCCLUSION); - ADD_GROUP("Height", "height_"); - ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "height_enabled"), "set_feature", "get_feature", FEATURE_HEIGHT_MAPPING); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "height_scale", PROPERTY_HINT_RANGE, "-16,16,0.01"), "set_height_scale", "get_height_scale"); - ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "height_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_HEIGHT); + ADD_GROUP("Depth", "depth_"); + ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "depth_enabled"), "set_feature", "get_feature", FEATURE_DEPTH_MAPPING); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth_scale", PROPERTY_HINT_RANGE, "-16,16,0.01"), "set_depth_scale", "get_depth_scale"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "depth_deep_parallax"), "set_depth_deep_parallax", "is_depth_deep_parallax_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "depth_min_layers", PROPERTY_HINT_RANGE, "1,32,1"), "set_depth_deep_parallax_min_layers", "get_depth_deep_parallax_min_layers"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "depth_max_layers", PROPERTY_HINT_RANGE, "1,32,1"), "set_depth_deep_parallax_max_layers", "get_depth_deep_parallax_max_layers"); + ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "depth_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_DEPTH); ADD_GROUP("Subsurf Scatter", "subsurf_scatter_"); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "subsurf_scatter_enabled"), "set_feature", "get_feature", FEATURE_SUBSURACE_SCATTERING); @@ -1172,8 +1287,7 @@ void SpatialMaterial::_bind_methods() { ADD_GROUP("Refraction", "refraction_"); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "refraction_enabled"), "set_feature", "get_feature", FEATURE_REFRACTION); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "refraction_displacement", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_refraction", "get_refraction"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "refraction_roughness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_refraction_roughness", "get_refraction_roughness"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "refraction_scale", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_refraction", "get_refraction"); ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "refraction_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_REFRACTION); ADD_GROUP("Detail", "detail_"); @@ -1201,7 +1315,7 @@ void SpatialMaterial::_bind_methods() { BIND_CONSTANT(TEXTURE_CLEARCOAT); BIND_CONSTANT(TEXTURE_FLOWMAP); BIND_CONSTANT(TEXTURE_AMBIENT_OCCLUSION); - BIND_CONSTANT(TEXTURE_HEIGHT); + BIND_CONSTANT(TEXTURE_DEPTH); BIND_CONSTANT(TEXTURE_SUBSURFACE_SCATTERING); BIND_CONSTANT(TEXTURE_REFRACTION); BIND_CONSTANT(TEXTURE_DETAIL_MASK); @@ -1219,7 +1333,7 @@ void SpatialMaterial::_bind_methods() { BIND_CONSTANT(FEATURE_CLEARCOAT); BIND_CONSTANT(FEATURE_ANISOTROPY); BIND_CONSTANT(FEATURE_AMBIENT_OCCLUSION); - BIND_CONSTANT(FEATURE_HEIGHT_MAPPING); + BIND_CONSTANT(FEATURE_DEPTH_MAPPING); BIND_CONSTANT(FEATURE_SUBSURACE_SCATTERING); BIND_CONSTANT(FEATURE_REFRACTION); BIND_CONSTANT(FEATURE_DETAIL); @@ -1248,7 +1362,7 @@ void SpatialMaterial::_bind_methods() { BIND_CONSTANT(FLAG_MAX); BIND_CONSTANT(DIFFUSE_LAMBERT); - BIND_CONSTANT(DIFFUSE_LAMBERT_WRAP); + BIND_CONSTANT(DIFFUSE_HALF_LAMBERT); BIND_CONSTANT(DIFFUSE_OREN_NAYAR); BIND_CONSTANT(DIFFUSE_BURLEY); @@ -1274,10 +1388,9 @@ SpatialMaterial::SpatialMaterial() set_clearcoat(1); set_clearcoat_gloss(0.5); set_anisotropy(0); - set_height_scale(1); + set_depth_scale(0.05); set_subsurface_scattering_strength(0); - set_refraction(0); - set_refraction_roughness(0); + set_refraction(0.05); set_line_width(1); set_point_size(1); set_uv1_offset(Vector2(0, 0)); @@ -1289,6 +1402,10 @@ SpatialMaterial::SpatialMaterial() set_particles_anim_v_frames(1); set_particles_anim_loop(false); + deep_parallax = false; + set_depth_deep_parallax_min_layers(8); + set_depth_deep_parallax_max_layers(32); + detail_uv = DETAIL_UV_1; blend_mode = BLEND_MODE_MIX; detail_blend_mode = BLEND_MODE_MIX; diff --git a/scene/resources/material.h b/scene/resources/material.h index f974db0aa7..b6e85c8332 100644 --- a/scene/resources/material.h +++ b/scene/resources/material.h @@ -97,7 +97,7 @@ public: TEXTURE_CLEARCOAT, TEXTURE_FLOWMAP, TEXTURE_AMBIENT_OCCLUSION, - TEXTURE_HEIGHT, + TEXTURE_DEPTH, TEXTURE_SUBSURFACE_SCATTERING, TEXTURE_REFRACTION, TEXTURE_DETAIL_MASK, @@ -120,7 +120,7 @@ public: FEATURE_CLEARCOAT, FEATURE_ANISOTROPY, FEATURE_AMBIENT_OCCLUSION, - FEATURE_HEIGHT_MAPPING, + FEATURE_DEPTH_MAPPING, FEATURE_SUBSURACE_SCATTERING, FEATURE_REFRACTION, FEATURE_DETAIL, @@ -160,7 +160,7 @@ public: enum DiffuseMode { DIFFUSE_LAMBERT, - DIFFUSE_LAMBERT_WRAP, + DIFFUSE_HALF_LAMBERT, DIFFUSE_OREN_NAYAR, DIFFUSE_BURLEY, }; @@ -185,7 +185,7 @@ private: uint32_t detail_blend_mode : 2; uint32_t diffuse_mode : 2; uint32_t invalid_key : 1; - uint32_t specular_mode : 1; + uint32_t deep_parallax : 1; uint32_t billboard_mode : 2; }; @@ -226,6 +226,8 @@ private: mk.detail_blend_mode = detail_blend_mode; mk.diffuse_mode = diffuse_mode; mk.billboard_mode = billboard_mode; + mk.deep_parallax = deep_parallax ? 1 : 0; + ; return mk; } @@ -243,10 +245,9 @@ private: StringName clearcoat; StringName clearcoat_gloss; StringName anisotropy; - StringName height_scale; + StringName depth_scale; StringName subsurface_scattering_strength; StringName refraction; - StringName refraction_roughness; StringName point_size; StringName uv1_scale; StringName uv1_offset; @@ -255,6 +256,9 @@ private: StringName particle_h_frames; StringName particle_v_frames; StringName particles_anim_loop; + StringName depth_min_layers; + StringName depth_max_layers; + StringName texture_names[TEXTURE_MAX]; }; @@ -280,10 +284,9 @@ private: float clearcoat; float clearcoat_gloss; float anisotropy; - float height_scale; + float depth_scale; float subsurface_scattering_strength; float refraction; - float refraction_roughness; float line_width; float point_size; int particles_anim_h_frames; @@ -298,6 +301,10 @@ private: DetailUV detail_uv; + bool deep_parallax; + int deep_parallax_min_layers; + int deep_parallax_max_layers; + BlendMode blend_mode; BlendMode detail_blend_mode; DepthDrawMode depth_draw_mode; @@ -353,8 +360,17 @@ public: void set_anisotropy(float p_anisotropy); float get_anisotropy() const; - void set_height_scale(float p_height_scale); - float get_height_scale() const; + void set_depth_scale(float p_depth_scale); + float get_depth_scale() const; + + void set_depth_deep_parallax(bool p_enable); + bool is_depth_deep_parallax_enabled() const; + + void set_depth_deep_parallax_min_layers(int p_layer); + int get_depth_deep_parallax_min_layers() const; + + void set_depth_deep_parallax_max_layers(int p_layer); + int get_depth_deep_parallax_max_layers() const; void set_subsurface_scattering_strength(float p_strength); float get_subsurface_scattering_strength() const; @@ -362,9 +378,6 @@ public: void set_refraction(float p_refraction); float get_refraction() const; - void set_refraction_roughness(float p_refraction_roughness); - float get_refraction_roughness() const; - void set_line_width(float p_line_width); float get_line_width() const; diff --git a/scene/resources/mesh.cpp b/scene/resources/mesh.cpp index 4846d84b33..9f55f0c364 100644 --- a/scene/resources/mesh.cpp +++ b/scene/resources/mesh.cpp @@ -32,6 +32,390 @@ #include "scene/resources/convex_polygon_shape.h" #include "surface_tool.h" +void Mesh::_clear_triangle_mesh() { + + triangle_mesh.unref(); + ; +} + +Ref<TriangleMesh> Mesh::generate_triangle_mesh() const { + + if (triangle_mesh.is_valid()) + return triangle_mesh; + + int facecount = 0; + + for (int i = 0; i < get_surface_count(); i++) { + + if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) + continue; + + if (surface_get_format(i) & ARRAY_FORMAT_INDEX) { + + facecount += surface_get_array_index_len(i); + } else { + + facecount += surface_get_array_len(i); + } + } + + if (facecount == 0 || (facecount % 3) != 0) + return triangle_mesh; + + PoolVector<Vector3> faces; + faces.resize(facecount); + PoolVector<Vector3>::Write facesw = faces.write(); + + int widx = 0; + + for (int i = 0; i < get_surface_count(); i++) { + + if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) + continue; + + Array a = surface_get_arrays(i); + + int vc = surface_get_array_len(i); + PoolVector<Vector3> vertices = a[ARRAY_VERTEX]; + PoolVector<Vector3>::Read vr = vertices.read(); + + if (surface_get_format(i) & ARRAY_FORMAT_INDEX) { + + int ic = surface_get_array_index_len(i); + PoolVector<int> indices = a[ARRAY_INDEX]; + PoolVector<int>::Read ir = indices.read(); + + for (int i = 0; i < ic; i++) { + int index = ir[i]; + facesw[widx++] = vr[index]; + } + + } else { + + for (int i = 0; i < vc; i++) + facesw[widx++] = vr[i]; + } + } + + facesw = PoolVector<Vector3>::Write(); + + triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh)); + triangle_mesh->create(faces); + + return triangle_mesh; +} + +PoolVector<Face3> Mesh::get_faces() const { + + Ref<TriangleMesh> tm = generate_triangle_mesh(); + if (tm.is_valid()) + return tm->get_faces(); + return PoolVector<Face3>(); + /* + for (int i=0;i<surfaces.size();i++) { + + if (VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, i ) != VisualServer::PRIMITIVE_TRIANGLES ) + continue; + + PoolVector<int> indices; + PoolVector<Vector3> vertices; + + vertices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_VERTEX); + + int len=VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i); + bool has_indices; + + if (len>0) { + + indices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_INDEX); + has_indices=true; + + } else { + + len=vertices.size(); + has_indices=false; + } + + if (len<=0) + continue; + + PoolVector<int>::Read indicesr = indices.read(); + const int *indicesptr = indicesr.ptr(); + + PoolVector<Vector3>::Read verticesr = vertices.read(); + const Vector3 *verticesptr = verticesr.ptr(); + + int old_faces=faces.size(); + int new_faces=old_faces+(len/3); + + faces.resize(new_faces); + + PoolVector<Face3>::Write facesw = faces.write(); + Face3 *facesptr=facesw.ptr(); + + + for (int i=0;i<len/3;i++) { + + Face3 face; + + for (int j=0;j<3;j++) { + + int idx=i*3+j; + face.vertex[j] = has_indices ? verticesptr[ indicesptr[ idx ] ] : verticesptr[idx]; + } + + facesptr[i+old_faces]=face; + } + + } +*/ +} + +Ref<Shape> Mesh::create_convex_shape() const { + + PoolVector<Vector3> vertices; + + for (int i = 0; i < get_surface_count(); i++) { + + Array a = surface_get_arrays(i); + PoolVector<Vector3> v = a[ARRAY_VERTEX]; + vertices.append_array(v); + } + + Ref<ConvexPolygonShape> shape = memnew(ConvexPolygonShape); + shape->set_points(vertices); + return shape; +} + +Ref<Shape> Mesh::create_trimesh_shape() const { + + PoolVector<Face3> faces = get_faces(); + if (faces.size() == 0) + return Ref<Shape>(); + + PoolVector<Vector3> face_points; + face_points.resize(faces.size() * 3); + + for (int i = 0; i < face_points.size(); i++) { + + Face3 f = faces.get(i / 3); + face_points.set(i, f.vertex[i % 3]); + } + + Ref<ConcavePolygonShape> shape = memnew(ConcavePolygonShape); + shape->set_faces(face_points); + return shape; +} + +Ref<Mesh> Mesh::create_outline(float p_margin) const { + + Array arrays; + int index_accum = 0; + for (int i = 0; i < get_surface_count(); i++) { + + if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) + continue; + + Array a = surface_get_arrays(i); + int vcount = 0; + + if (i == 0) { + arrays = a; + PoolVector<Vector3> v = a[ARRAY_VERTEX]; + index_accum += v.size(); + } else { + + for (int j = 0; j < arrays.size(); j++) { + + if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) { + //mismatch, do not use + arrays[j] = Variant(); + continue; + } + + switch (j) { + + case ARRAY_VERTEX: + case ARRAY_NORMAL: { + + PoolVector<Vector3> dst = arrays[j]; + PoolVector<Vector3> src = a[j]; + if (j == ARRAY_VERTEX) + vcount = src.size(); + if (dst.size() == 0 || src.size() == 0) { + arrays[j] = Variant(); + continue; + } + dst.append_array(src); + arrays[j] = dst; + } break; + case ARRAY_TANGENT: + case ARRAY_BONES: + case ARRAY_WEIGHTS: { + + PoolVector<real_t> dst = arrays[j]; + PoolVector<real_t> src = a[j]; + if (dst.size() == 0 || src.size() == 0) { + arrays[j] = Variant(); + continue; + } + dst.append_array(src); + arrays[j] = dst; + + } break; + case ARRAY_COLOR: { + PoolVector<Color> dst = arrays[j]; + PoolVector<Color> src = a[j]; + if (dst.size() == 0 || src.size() == 0) { + arrays[j] = Variant(); + continue; + } + dst.append_array(src); + arrays[j] = dst; + + } break; + case ARRAY_TEX_UV: + case ARRAY_TEX_UV2: { + PoolVector<Vector2> dst = arrays[j]; + PoolVector<Vector2> src = a[j]; + if (dst.size() == 0 || src.size() == 0) { + arrays[j] = Variant(); + continue; + } + dst.append_array(src); + arrays[j] = dst; + + } break; + case ARRAY_INDEX: { + PoolVector<int> dst = arrays[j]; + PoolVector<int> src = a[j]; + if (dst.size() == 0 || src.size() == 0) { + arrays[j] = Variant(); + continue; + } + { + int ss = src.size(); + PoolVector<int>::Write w = src.write(); + for (int k = 0; k < ss; k++) { + w[k] += index_accum; + } + } + dst.append_array(src); + arrays[j] = dst; + index_accum += vcount; + + } break; + } + } + } + } + + { + PoolVector<int>::Write ir; + PoolVector<int> indices = arrays[ARRAY_INDEX]; + bool has_indices = false; + PoolVector<Vector3> vertices = arrays[ARRAY_VERTEX]; + int vc = vertices.size(); + ERR_FAIL_COND_V(!vc, Ref<ArrayMesh>()); + PoolVector<Vector3>::Write r = vertices.write(); + + if (indices.size()) { + vc = indices.size(); + ir = indices.write(); + has_indices = true; + } + + Map<Vector3, Vector3> normal_accum; + + //fill normals with triangle normals + for (int i = 0; i < vc; i += 3) { + + Vector3 t[3]; + + if (has_indices) { + t[0] = r[ir[i + 0]]; + t[1] = r[ir[i + 1]]; + t[2] = r[ir[i + 2]]; + } else { + t[0] = r[i + 0]; + t[1] = r[i + 1]; + t[2] = r[i + 2]; + } + + Vector3 n = Plane(t[0], t[1], t[2]).normal; + + for (int j = 0; j < 3; j++) { + + Map<Vector3, Vector3>::Element *E = normal_accum.find(t[j]); + if (!E) { + normal_accum[t[j]] = n; + } else { + float d = n.dot(E->get()); + if (d < 1.0) + E->get() += n * (1.0 - d); + //E->get()+=n; + } + } + } + + //normalize + + for (Map<Vector3, Vector3>::Element *E = normal_accum.front(); E; E = E->next()) { + E->get().normalize(); + } + + //displace normals + int vc2 = vertices.size(); + + for (int i = 0; i < vc2; i++) { + + Vector3 t = r[i]; + + Map<Vector3, Vector3>::Element *E = normal_accum.find(t); + ERR_CONTINUE(!E); + + t += E->get() * p_margin; + r[i] = t; + } + + r = PoolVector<Vector3>::Write(); + arrays[ARRAY_VERTEX] = vertices; + + if (!has_indices) { + + PoolVector<int> new_indices; + new_indices.resize(vertices.size()); + PoolVector<int>::Write iw = new_indices.write(); + + for (int j = 0; j < vc2; j += 3) { + + iw[j] = j; + iw[j + 1] = j + 2; + iw[j + 2] = j + 1; + } + + iw = PoolVector<int>::Write(); + arrays[ARRAY_INDEX] = new_indices; + + } else { + + for (int j = 0; j < vc; j += 3) { + + SWAP(ir[j + 1], ir[j + 2]); + } + ir = PoolVector<int>::Write(); + arrays[ARRAY_INDEX] = indices; + } + } + + Ref<ArrayMesh> newmesh = memnew(ArrayMesh); + newmesh->add_surface_from_arrays(PRIMITIVE_TRIANGLES, arrays); + return newmesh; +} + +Mesh::Mesh() { +} + static const char *_array_name[] = { "vertex_array", "normal_array", @@ -45,34 +429,34 @@ static const char *_array_name[] = { NULL }; -static const Mesh::ArrayType _array_types[] = { - - Mesh::ARRAY_VERTEX, - Mesh::ARRAY_NORMAL, - Mesh::ARRAY_TANGENT, - Mesh::ARRAY_COLOR, - Mesh::ARRAY_TEX_UV, - Mesh::ARRAY_TEX_UV2, - Mesh::ARRAY_BONES, - Mesh::ARRAY_WEIGHTS, - Mesh::ARRAY_INDEX +static const ArrayMesh::ArrayType _array_types[] = { + + ArrayMesh::ARRAY_VERTEX, + ArrayMesh::ARRAY_NORMAL, + ArrayMesh::ARRAY_TANGENT, + ArrayMesh::ARRAY_COLOR, + ArrayMesh::ARRAY_TEX_UV, + ArrayMesh::ARRAY_TEX_UV2, + ArrayMesh::ARRAY_BONES, + ArrayMesh::ARRAY_WEIGHTS, + ArrayMesh::ARRAY_INDEX }; /* compatibility */ static const int _format_translate[] = { - Mesh::ARRAY_FORMAT_VERTEX, - Mesh::ARRAY_FORMAT_NORMAL, - Mesh::ARRAY_FORMAT_TANGENT, - Mesh::ARRAY_FORMAT_COLOR, - Mesh::ARRAY_FORMAT_TEX_UV, - Mesh::ARRAY_FORMAT_TEX_UV2, - Mesh::ARRAY_FORMAT_BONES, - Mesh::ARRAY_FORMAT_WEIGHTS, - Mesh::ARRAY_FORMAT_INDEX, + ArrayMesh::ARRAY_FORMAT_VERTEX, + ArrayMesh::ARRAY_FORMAT_NORMAL, + ArrayMesh::ARRAY_FORMAT_TANGENT, + ArrayMesh::ARRAY_FORMAT_COLOR, + ArrayMesh::ARRAY_FORMAT_TEX_UV, + ArrayMesh::ARRAY_FORMAT_TEX_UV2, + ArrayMesh::ARRAY_FORMAT_BONES, + ArrayMesh::ARRAY_FORMAT_WEIGHTS, + ArrayMesh::ARRAY_FORMAT_INDEX, }; -bool Mesh::_set(const StringName &p_name, const Variant &p_value) { +bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) { String sname = p_name; @@ -191,7 +575,7 @@ bool Mesh::_set(const StringName &p_name, const Variant &p_value) { return false; } -bool Mesh::_get(const StringName &p_name, Variant &r_ret) const { +bool ArrayMesh::_get(const StringName &p_name, Variant &r_ret) const { if (_is_generated()) return false; @@ -270,7 +654,7 @@ bool Mesh::_get(const StringName &p_name, Variant &r_ret) const { return true; } -void Mesh::_get_property_list(List<PropertyInfo> *p_list) const { +void ArrayMesh::_get_property_list(List<PropertyInfo> *p_list) const { if (_is_generated()) return; @@ -290,7 +674,7 @@ void Mesh::_get_property_list(List<PropertyInfo> *p_list) const { p_list->push_back(PropertyInfo(Variant::RECT3, "custom_aabb/custom_aabb")); } -void Mesh::_recompute_aabb() { +void ArrayMesh::_recompute_aabb() { // regenerate AABB aabb = Rect3(); @@ -304,16 +688,17 @@ void Mesh::_recompute_aabb() { } } -void Mesh::add_surface(uint32_t p_format, PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const Rect3 &p_aabb, const Vector<PoolVector<uint8_t> > &p_blend_shapes, const Vector<Rect3> &p_bone_aabbs) { +void ArrayMesh::add_surface(uint32_t p_format, PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const Rect3 &p_aabb, const Vector<PoolVector<uint8_t> > &p_blend_shapes, const Vector<Rect3> &p_bone_aabbs) { Surface s; s.aabb = p_aabb; surfaces.push_back(s); + _recompute_aabb(); VisualServer::get_singleton()->mesh_add_surface(mesh, p_format, (VS::PrimitiveType)p_primitive, p_array, p_vertex_count, p_index_array, p_index_count, p_aabb, p_blend_shapes, p_bone_aabbs); } -void Mesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, uint32_t p_flags) { +void ArrayMesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, uint32_t p_flags) { ERR_FAIL_COND(p_arrays.size() != ARRAY_MAX); @@ -335,7 +720,7 @@ void Mesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arr for (int i = 0; i < len; i++) { if (i == 0) - aabb.pos = vtx[i]; + aabb.position = vtx[i]; else aabb.expand_to(vtx[i]); } @@ -345,28 +730,28 @@ void Mesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arr _recompute_aabb(); } - triangle_mesh = Ref<TriangleMesh>(); + _clear_triangle_mesh(); _change_notify(); emit_changed(); } -Array Mesh::surface_get_arrays(int p_surface) const { +Array ArrayMesh::surface_get_arrays(int p_surface) const { ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array()); return VisualServer::get_singleton()->mesh_surface_get_arrays(mesh, p_surface); } -Array Mesh::surface_get_blend_shape_arrays(int p_surface) const { +Array ArrayMesh::surface_get_blend_shape_arrays(int p_surface) const { ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array()); return Array(); } -int Mesh::get_surface_count() const { +int ArrayMesh::get_surface_count() const { return surfaces.size(); } -void Mesh::add_blend_shape(const StringName &p_name) { +void ArrayMesh::add_blend_shape(const StringName &p_name) { if (surfaces.size()) { ERR_EXPLAIN("Can't add a shape key count if surfaces are already created."); @@ -389,15 +774,15 @@ void Mesh::add_blend_shape(const StringName &p_name) { VS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size()); } -int Mesh::get_blend_shape_count() const { +int ArrayMesh::get_blend_shape_count() const { return blend_shapes.size(); } -StringName Mesh::get_blend_shape_name(int p_index) const { +StringName ArrayMesh::get_blend_shape_name(int p_index) const { ERR_FAIL_INDEX_V(p_index, blend_shapes.size(), StringName()); return blend_shapes[p_index]; } -void Mesh::clear_blend_shapes() { +void ArrayMesh::clear_blend_shapes() { if (surfaces.size()) { ERR_EXPLAIN("Can't set shape key count if surfaces are already created."); @@ -407,54 +792,54 @@ void Mesh::clear_blend_shapes() { blend_shapes.clear(); } -void Mesh::set_blend_shape_mode(BlendShapeMode p_mode) { +void ArrayMesh::set_blend_shape_mode(BlendShapeMode p_mode) { blend_shape_mode = p_mode; VS::get_singleton()->mesh_set_blend_shape_mode(mesh, (VS::BlendShapeMode)p_mode); } -Mesh::BlendShapeMode Mesh::get_blend_shape_mode() const { +ArrayMesh::BlendShapeMode ArrayMesh::get_blend_shape_mode() const { return blend_shape_mode; } -void Mesh::surface_remove(int p_idx) { +void ArrayMesh::surface_remove(int p_idx) { ERR_FAIL_INDEX(p_idx, surfaces.size()); VisualServer::get_singleton()->mesh_remove_surface(mesh, p_idx); surfaces.remove(p_idx); - triangle_mesh = Ref<TriangleMesh>(); + _clear_triangle_mesh(); _recompute_aabb(); _change_notify(); emit_changed(); } -int Mesh::surface_get_array_len(int p_idx) const { +int ArrayMesh::surface_get_array_len(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1); return VisualServer::get_singleton()->mesh_surface_get_array_len(mesh, p_idx); } -int Mesh::surface_get_array_index_len(int p_idx) const { +int ArrayMesh::surface_get_array_index_len(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1); return VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, p_idx); } -uint32_t Mesh::surface_get_format(int p_idx) const { +uint32_t ArrayMesh::surface_get_format(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, surfaces.size(), 0); return VisualServer::get_singleton()->mesh_surface_get_format(mesh, p_idx); } -Mesh::PrimitiveType Mesh::surface_get_primitive_type(int p_idx) const { +ArrayMesh::PrimitiveType ArrayMesh::surface_get_primitive_type(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, surfaces.size(), PRIMITIVE_LINES); return (PrimitiveType)VisualServer::get_singleton()->mesh_surface_get_primitive_type(mesh, p_idx); } -void Mesh::surface_set_material(int p_idx, const Ref<Material> &p_material) { +void ArrayMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) { ERR_FAIL_INDEX(p_idx, surfaces.size()); if (surfaces[p_idx].material == p_material) @@ -465,40 +850,40 @@ void Mesh::surface_set_material(int p_idx, const Ref<Material> &p_material) { _change_notify("material"); } -void Mesh::surface_set_name(int p_idx, const String &p_name) { +void ArrayMesh::surface_set_name(int p_idx, const String &p_name) { ERR_FAIL_INDEX(p_idx, surfaces.size()); surfaces[p_idx].name = p_name; } -String Mesh::surface_get_name(int p_idx) const { +String ArrayMesh::surface_get_name(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, surfaces.size(), String()); return surfaces[p_idx].name; } -void Mesh::surface_set_custom_aabb(int p_idx, const Rect3 &p_aabb) { +void ArrayMesh::surface_set_custom_aabb(int p_idx, const Rect3 &p_aabb) { ERR_FAIL_INDEX(p_idx, surfaces.size()); surfaces[p_idx].aabb = p_aabb; // set custom aabb too? } -Ref<Material> Mesh::surface_get_material(int p_idx) const { +Ref<Material> ArrayMesh::surface_get_material(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, surfaces.size(), Ref<Material>()); return surfaces[p_idx].material; } -void Mesh::add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data) { +void ArrayMesh::add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data) { VisualServer::get_singleton()->mesh_add_surface_from_mesh_data(mesh, p_mesh_data); Rect3 aabb; for (int i = 0; i < p_mesh_data.vertices.size(); i++) { if (i == 0) - aabb.pos = p_mesh_data.vertices[i]; + aabb.position = p_mesh_data.vertices[i]; else aabb.expand_to(p_mesh_data.vertices[i]); } @@ -510,7 +895,7 @@ void Mesh::add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data) { else aabb.merge_with(s.aabb); - triangle_mesh = Ref<TriangleMesh>(); + _clear_triangle_mesh(); surfaces.push_back(s); _change_notify(); @@ -518,129 +903,27 @@ void Mesh::add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data) { emit_changed(); } -RID Mesh::get_rid() const { +RID ArrayMesh::get_rid() const { return mesh; } -Rect3 Mesh::get_aabb() const { +Rect3 ArrayMesh::get_aabb() const { return aabb; } -void Mesh::set_custom_aabb(const Rect3 &p_custom) { +void ArrayMesh::set_custom_aabb(const Rect3 &p_custom) { custom_aabb = p_custom; VS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb); } -Rect3 Mesh::get_custom_aabb() const { +Rect3 ArrayMesh::get_custom_aabb() const { return custom_aabb; } -PoolVector<Face3> Mesh::get_faces() const { - - Ref<TriangleMesh> tm = generate_triangle_mesh(); - if (tm.is_valid()) - return tm->get_faces(); - return PoolVector<Face3>(); - /* - for (int i=0;i<surfaces.size();i++) { - - if (VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, i ) != VisualServer::PRIMITIVE_TRIANGLES ) - continue; - - PoolVector<int> indices; - PoolVector<Vector3> vertices; - - vertices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_VERTEX); - - int len=VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i); - bool has_indices; - - if (len>0) { - - indices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_INDEX); - has_indices=true; - - } else { - - len=vertices.size(); - has_indices=false; - } - - if (len<=0) - continue; - - PoolVector<int>::Read indicesr = indices.read(); - const int *indicesptr = indicesr.ptr(); - - PoolVector<Vector3>::Read verticesr = vertices.read(); - const Vector3 *verticesptr = verticesr.ptr(); - - int old_faces=faces.size(); - int new_faces=old_faces+(len/3); - - faces.resize(new_faces); - - PoolVector<Face3>::Write facesw = faces.write(); - Face3 *facesptr=facesw.ptr(); - - - for (int i=0;i<len/3;i++) { - - Face3 face; - - for (int j=0;j<3;j++) { - - int idx=i*3+j; - face.vertex[j] = has_indices ? verticesptr[ indicesptr[ idx ] ] : verticesptr[idx]; - } - - facesptr[i+old_faces]=face; - } - - } -*/ -} - -Ref<Shape> Mesh::create_convex_shape() const { - - PoolVector<Vector3> vertices; - - for (int i = 0; i < get_surface_count(); i++) { - - Array a = surface_get_arrays(i); - PoolVector<Vector3> v = a[ARRAY_VERTEX]; - vertices.append_array(v); - } - - Ref<ConvexPolygonShape> shape = memnew(ConvexPolygonShape); - shape->set_points(vertices); - return shape; -} - -Ref<Shape> Mesh::create_trimesh_shape() const { - - PoolVector<Face3> faces = get_faces(); - if (faces.size() == 0) - return Ref<Shape>(); - - PoolVector<Vector3> face_points; - face_points.resize(faces.size() * 3); - - for (int i = 0; i < face_points.size(); i++) { - - Face3 f = faces.get(i / 3); - face_points.set(i, f.vertex[i % 3]); - } - - Ref<ConcavePolygonShape> shape = memnew(ConcavePolygonShape); - shape->set_faces(face_points); - return shape; -} - -void Mesh::center_geometry() { +void ArrayMesh::center_geometry() { /* Vector3 ofs = aabb.pos+aabb.size*0.5; @@ -668,13 +951,13 @@ void Mesh::center_geometry() { */ } -void Mesh::regen_normalmaps() { +void ArrayMesh::regen_normalmaps() { Vector<Ref<SurfaceTool> > surfs; for (int i = 0; i < get_surface_count(); i++) { Ref<SurfaceTool> st = memnew(SurfaceTool); - st->create_from(Ref<Mesh>(this), i); + st->create_from(Ref<ArrayMesh>(this), i); surfs.push_back(st); } @@ -685,315 +968,42 @@ void Mesh::regen_normalmaps() { for (int i = 0; i < surfs.size(); i++) { surfs[i]->generate_tangents(); - surfs[i]->commit(Ref<Mesh>(this)); - } -} - -Ref<TriangleMesh> Mesh::generate_triangle_mesh() const { - - if (triangle_mesh.is_valid()) - return triangle_mesh; - - int facecount = 0; - - for (int i = 0; i < get_surface_count(); i++) { - - if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) - continue; - - if (surface_get_format(i) & ARRAY_FORMAT_INDEX) { - - facecount += surface_get_array_index_len(i); - } else { - - facecount += surface_get_array_len(i); - } - } - - if (facecount == 0 || (facecount % 3) != 0) - return triangle_mesh; - - PoolVector<Vector3> faces; - faces.resize(facecount); - PoolVector<Vector3>::Write facesw = faces.write(); - - int widx = 0; - - for (int i = 0; i < get_surface_count(); i++) { - - if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) - continue; - - Array a = surface_get_arrays(i); - - int vc = surface_get_array_len(i); - PoolVector<Vector3> vertices = a[ARRAY_VERTEX]; - PoolVector<Vector3>::Read vr = vertices.read(); - - if (surface_get_format(i) & ARRAY_FORMAT_INDEX) { - - int ic = surface_get_array_index_len(i); - PoolVector<int> indices = a[ARRAY_INDEX]; - PoolVector<int>::Read ir = indices.read(); - - for (int i = 0; i < ic; i++) { - int index = ir[i]; - facesw[widx++] = vr[index]; - } - - } else { - - for (int i = 0; i < vc; i++) - facesw[widx++] = vr[i]; - } + surfs[i]->commit(Ref<ArrayMesh>(this)); } - - facesw = PoolVector<Vector3>::Write(); - - triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh)); - triangle_mesh->create(faces); - - return triangle_mesh; } -Ref<Mesh> Mesh::create_outline(float p_margin) const { - - Array arrays; - int index_accum = 0; - for (int i = 0; i < get_surface_count(); i++) { - - if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) - continue; - - Array a = surface_get_arrays(i); - int vcount = 0; - - if (i == 0) { - arrays = a; - PoolVector<Vector3> v = a[ARRAY_VERTEX]; - index_accum += v.size(); - } else { - - for (int j = 0; j < arrays.size(); j++) { - - if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) { - //mismatch, do not use - arrays[j] = Variant(); - continue; - } - - switch (j) { - - case ARRAY_VERTEX: - case ARRAY_NORMAL: { - - PoolVector<Vector3> dst = arrays[j]; - PoolVector<Vector3> src = a[j]; - if (j == ARRAY_VERTEX) - vcount = src.size(); - if (dst.size() == 0 || src.size() == 0) { - arrays[j] = Variant(); - continue; - } - dst.append_array(src); - arrays[j] = dst; - } break; - case ARRAY_TANGENT: - case ARRAY_BONES: - case ARRAY_WEIGHTS: { - - PoolVector<real_t> dst = arrays[j]; - PoolVector<real_t> src = a[j]; - if (dst.size() == 0 || src.size() == 0) { - arrays[j] = Variant(); - continue; - } - dst.append_array(src); - arrays[j] = dst; - - } break; - case ARRAY_COLOR: { - PoolVector<Color> dst = arrays[j]; - PoolVector<Color> src = a[j]; - if (dst.size() == 0 || src.size() == 0) { - arrays[j] = Variant(); - continue; - } - dst.append_array(src); - arrays[j] = dst; - - } break; - case ARRAY_TEX_UV: - case ARRAY_TEX_UV2: { - PoolVector<Vector2> dst = arrays[j]; - PoolVector<Vector2> src = a[j]; - if (dst.size() == 0 || src.size() == 0) { - arrays[j] = Variant(); - continue; - } - dst.append_array(src); - arrays[j] = dst; - - } break; - case ARRAY_INDEX: { - PoolVector<int> dst = arrays[j]; - PoolVector<int> src = a[j]; - if (dst.size() == 0 || src.size() == 0) { - arrays[j] = Variant(); - continue; - } - { - int ss = src.size(); - PoolVector<int>::Write w = src.write(); - for (int k = 0; k < ss; k++) { - w[k] += index_accum; - } - } - dst.append_array(src); - arrays[j] = dst; - index_accum += vcount; - - } break; - } - } - } - } - - { - PoolVector<int>::Write ir; - PoolVector<int> indices = arrays[ARRAY_INDEX]; - bool has_indices = false; - PoolVector<Vector3> vertices = arrays[ARRAY_VERTEX]; - int vc = vertices.size(); - ERR_FAIL_COND_V(!vc, Ref<Mesh>()); - PoolVector<Vector3>::Write r = vertices.write(); - - if (indices.size()) { - vc = indices.size(); - ir = indices.write(); - has_indices = true; - } - - Map<Vector3, Vector3> normal_accum; - - //fill normals with triangle normals - for (int i = 0; i < vc; i += 3) { - - Vector3 t[3]; - - if (has_indices) { - t[0] = r[ir[i + 0]]; - t[1] = r[ir[i + 1]]; - t[2] = r[ir[i + 2]]; - } else { - t[0] = r[i + 0]; - t[1] = r[i + 1]; - t[2] = r[i + 2]; - } - - Vector3 n = Plane(t[0], t[1], t[2]).normal; - - for (int j = 0; j < 3; j++) { - - Map<Vector3, Vector3>::Element *E = normal_accum.find(t[j]); - if (!E) { - normal_accum[t[j]] = n; - } else { - float d = n.dot(E->get()); - if (d < 1.0) - E->get() += n * (1.0 - d); - //E->get()+=n; - } - } - } - - //normalize - - for (Map<Vector3, Vector3>::Element *E = normal_accum.front(); E; E = E->next()) { - E->get().normalize(); - } - - //displace normals - int vc2 = vertices.size(); - - for (int i = 0; i < vc2; i++) { - - Vector3 t = r[i]; - - Map<Vector3, Vector3>::Element *E = normal_accum.find(t); - ERR_CONTINUE(!E); - - t += E->get() * p_margin; - r[i] = t; - } - - r = PoolVector<Vector3>::Write(); - arrays[ARRAY_VERTEX] = vertices; - - if (!has_indices) { - - PoolVector<int> new_indices; - new_indices.resize(vertices.size()); - PoolVector<int>::Write iw = new_indices.write(); - - for (int j = 0; j < vc2; j += 3) { - - iw[j] = j; - iw[j + 1] = j + 2; - iw[j + 2] = j + 1; - } - - iw = PoolVector<int>::Write(); - arrays[ARRAY_INDEX] = new_indices; - - } else { - - for (int j = 0; j < vc; j += 3) { - - SWAP(ir[j + 1], ir[j + 2]); - } - ir = PoolVector<int>::Write(); - arrays[ARRAY_INDEX] = indices; - } - } - - Ref<Mesh> newmesh = memnew(Mesh); - newmesh->add_surface_from_arrays(PRIMITIVE_TRIANGLES, arrays); - return newmesh; -} - -void Mesh::_bind_methods() { - - ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &Mesh::add_blend_shape); - ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &Mesh::get_blend_shape_count); - ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &Mesh::get_blend_shape_name); - ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &Mesh::clear_blend_shapes); - ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &Mesh::set_blend_shape_mode); - ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &Mesh::get_blend_shape_mode); - - ClassDB::bind_method(D_METHOD("add_surface_from_arrays", "primitive", "arrays", "blend_shapes", "compress_flags"), &Mesh::add_surface_from_arrays, DEFVAL(Array()), DEFVAL(ARRAY_COMPRESS_DEFAULT)); - ClassDB::bind_method(D_METHOD("get_surface_count"), &Mesh::get_surface_count); - ClassDB::bind_method(D_METHOD("surface_remove", "surf_idx"), &Mesh::surface_remove); - ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &Mesh::surface_get_array_len); - ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &Mesh::surface_get_array_index_len); - ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &Mesh::surface_get_format); - ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &Mesh::surface_get_primitive_type); - ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material:Material"), &Mesh::surface_set_material); - ClassDB::bind_method(D_METHOD("surface_get_material:Material", "surf_idx"), &Mesh::surface_get_material); - ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &Mesh::surface_set_name); - ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &Mesh::surface_get_name); - ClassDB::bind_method(D_METHOD("create_trimesh_shape:Shape"), &Mesh::create_trimesh_shape); - ClassDB::bind_method(D_METHOD("create_convex_shape:Shape"), &Mesh::create_convex_shape); - ClassDB::bind_method(D_METHOD("create_outline:Mesh", "margin"), &Mesh::create_outline); - ClassDB::bind_method(D_METHOD("center_geometry"), &Mesh::center_geometry); +void ArrayMesh::_bind_methods() { + + ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ArrayMesh::add_blend_shape); + ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ArrayMesh::get_blend_shape_count); + ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &ArrayMesh::get_blend_shape_name); + ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &ArrayMesh::clear_blend_shapes); + ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ArrayMesh::set_blend_shape_mode); + ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ArrayMesh::get_blend_shape_mode); + + ClassDB::bind_method(D_METHOD("add_surface_from_arrays", "primitive", "arrays", "blend_shapes", "compress_flags"), &ArrayMesh::add_surface_from_arrays, DEFVAL(Array()), DEFVAL(ARRAY_COMPRESS_DEFAULT)); + ClassDB::bind_method(D_METHOD("get_surface_count"), &ArrayMesh::get_surface_count); + ClassDB::bind_method(D_METHOD("surface_remove", "surf_idx"), &ArrayMesh::surface_remove); + ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &ArrayMesh::surface_get_array_len); + ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &ArrayMesh::surface_get_array_index_len); + ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &ArrayMesh::surface_get_format); + ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &ArrayMesh::surface_get_primitive_type); + ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material:Material"), &ArrayMesh::surface_set_material); + ClassDB::bind_method(D_METHOD("surface_get_material:Material", "surf_idx"), &ArrayMesh::surface_get_material); + ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &ArrayMesh::surface_set_name); + ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &ArrayMesh::surface_get_name); + ClassDB::bind_method(D_METHOD("create_trimesh_shape:Shape"), &ArrayMesh::create_trimesh_shape); + ClassDB::bind_method(D_METHOD("create_convex_shape:Shape"), &ArrayMesh::create_convex_shape); + ClassDB::bind_method(D_METHOD("create_outline:ArrayMesh", "margin"), &ArrayMesh::create_outline); + ClassDB::bind_method(D_METHOD("center_geometry"), &ArrayMesh::center_geometry); ClassDB::set_method_flags(get_class_static(), _scs_create("center_geometry"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); - ClassDB::bind_method(D_METHOD("regen_normalmaps"), &Mesh::regen_normalmaps); + ClassDB::bind_method(D_METHOD("regen_normalmaps"), &ArrayMesh::regen_normalmaps); ClassDB::set_method_flags(get_class_static(), _scs_create("regen_normalmaps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); - ClassDB::bind_method(D_METHOD("get_faces"), &Mesh::get_faces); - ClassDB::bind_method(D_METHOD("generate_triangle_mesh:TriangleMesh"), &Mesh::generate_triangle_mesh); + ClassDB::bind_method(D_METHOD("get_faces"), &ArrayMesh::get_faces); + ClassDB::bind_method(D_METHOD("generate_triangle_mesh:TriangleMesh"), &ArrayMesh::generate_triangle_mesh); - ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &Mesh::set_custom_aabb); - ClassDB::bind_method(D_METHOD("get_custom_aabb"), &Mesh::get_custom_aabb); + ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ArrayMesh::set_custom_aabb); + ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ArrayMesh::get_custom_aabb); BIND_CONSTANT(NO_INDEX_ARRAY); BIND_CONSTANT(ARRAY_WEIGHTS_SIZE); @@ -1027,19 +1037,19 @@ void Mesh::_bind_methods() { BIND_CONSTANT(PRIMITIVE_TRIANGLE_FAN); } -Mesh::Mesh() { +ArrayMesh::ArrayMesh() { mesh = VisualServer::get_singleton()->mesh_create(); blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE; } -Mesh::~Mesh() { +ArrayMesh::~ArrayMesh() { VisualServer::get_singleton()->free(mesh); } //////////////////////// - +#if 0 void QuadMesh::_bind_methods() { ClassDB::bind_method(D_METHOD("set_material", "material:Material"), &QuadMesh::set_material); @@ -1105,3 +1115,4 @@ QuadMesh::QuadMesh() { add_surface_from_arrays(PRIMITIVE_TRIANGLE_FAN, arr); } +#endif diff --git a/scene/resources/mesh.h b/scene/resources/mesh.h index e441b4924a..7804a84f81 100644 --- a/scene/resources/mesh.h +++ b/scene/resources/mesh.h @@ -38,10 +38,13 @@ /** @author Juan Linietsky <reduzio@gmail.com> */ -class Mesh : public Resource { +class Mesh : public Resource { GDCLASS(Mesh, Resource); - RES_BASE_EXTENSION("msh"); + + mutable Ref<TriangleMesh> triangle_mesh; //cached +protected: + void _clear_triangle_mesh(); public: enum { @@ -111,6 +114,34 @@ public: BLEND_SHAPE_MODE_RELATIVE = VS::BLEND_SHAPE_MODE_RELATIVE, }; + virtual int get_surface_count() const = 0; + virtual int surface_get_array_len(int p_idx) const = 0; + virtual int surface_get_array_index_len(int p_idx) const = 0; + virtual Array surface_get_arrays(int p_surface) const = 0; + virtual uint32_t surface_get_format(int p_idx) const = 0; + virtual PrimitiveType surface_get_primitive_type(int p_idx) const = 0; + virtual Ref<Material> surface_get_material(int p_idx) const = 0; + virtual int get_blend_shape_count() const = 0; + virtual StringName get_blend_shape_name(int p_index) const = 0; + + PoolVector<Face3> get_faces() const; + Ref<TriangleMesh> generate_triangle_mesh() const; + + Ref<Shape> create_trimesh_shape() const; + Ref<Shape> create_convex_shape() const; + + Ref<Mesh> create_outline(float p_margin) const; + + virtual Rect3 get_aabb() const = 0; + + Mesh(); +}; + +class ArrayMesh : public Mesh { + + GDCLASS(ArrayMesh, Mesh); + RES_BASE_EXTENSION("msh"); + private: struct Surface { String name; @@ -124,8 +155,6 @@ private: Vector<StringName> blend_shapes; Rect3 custom_aabb; - mutable Ref<TriangleMesh> triangle_mesh; - void _recompute_aabb(); protected: @@ -177,21 +206,15 @@ public: Rect3 get_aabb() const; virtual RID get_rid() const; - Ref<Shape> create_trimesh_shape() const; - Ref<Shape> create_convex_shape() const; - - Ref<Mesh> create_outline(float p_margin) const; - void center_geometry(); void regen_normalmaps(); - PoolVector<Face3> get_faces() const; - Ref<TriangleMesh> generate_triangle_mesh() const; - Mesh(); + ArrayMesh(); - ~Mesh(); + ~ArrayMesh(); }; +#if 0 class QuadMesh : public Mesh { GDCLASS(QuadMesh, Mesh) @@ -206,6 +229,8 @@ public: QuadMesh(); }; +#endif + VARIANT_ENUM_CAST(Mesh::ArrayType); VARIANT_ENUM_CAST(Mesh::PrimitiveType); VARIANT_ENUM_CAST(Mesh::BlendShapeMode); diff --git a/scene/resources/mesh_data_tool.cpp b/scene/resources/mesh_data_tool.cpp index b6b47bf443..dc3713fb57 100644 --- a/scene/resources/mesh_data_tool.cpp +++ b/scene/resources/mesh_data_tool.cpp @@ -38,7 +38,7 @@ void MeshDataTool::clear() { format = 0; } -Error MeshDataTool::create_from_surface(const Ref<Mesh> &p_mesh, int p_surface) { +Error MeshDataTool::create_from_surface(const Ref<ArrayMesh> &p_mesh, int p_surface) { ERR_FAIL_COND_V(p_mesh.is_null(), ERR_INVALID_PARAMETER); @@ -179,7 +179,7 @@ Error MeshDataTool::create_from_surface(const Ref<Mesh> &p_mesh, int p_surface) return OK; } -Error MeshDataTool::commit_to_surface(const Ref<Mesh> &p_mesh) { +Error MeshDataTool::commit_to_surface(const Ref<ArrayMesh> &p_mesh) { ERR_FAIL_COND_V(p_mesh.is_null(), ERR_INVALID_PARAMETER); Array arr; @@ -309,7 +309,7 @@ Error MeshDataTool::commit_to_surface(const Ref<Mesh> &p_mesh) { if (w.size()) arr[Mesh::ARRAY_WEIGHTS] = w; - Ref<Mesh> ncmesh = p_mesh; + Ref<ArrayMesh> ncmesh = p_mesh; int sc = ncmesh->get_surface_count(); ncmesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr); ncmesh->surface_set_material(sc, material); diff --git a/scene/resources/mesh_data_tool.h b/scene/resources/mesh_data_tool.h index f6797d3e5e..ad771edbd1 100644 --- a/scene/resources/mesh_data_tool.h +++ b/scene/resources/mesh_data_tool.h @@ -78,8 +78,8 @@ protected: public: void clear(); - Error create_from_surface(const Ref<Mesh> &p_mesh, int p_surface); - Error commit_to_surface(const Ref<Mesh> &p_mesh); + Error create_from_surface(const Ref<ArrayMesh> &p_mesh, int p_surface); + Error commit_to_surface(const Ref<ArrayMesh> &p_mesh); int get_format() const; diff --git a/scene/resources/polygon_path_finder.cpp b/scene/resources/polygon_path_finder.cpp index 472031366f..80b413630a 100644 --- a/scene/resources/polygon_path_finder.cpp +++ b/scene/resources/polygon_path_finder.cpp @@ -71,7 +71,7 @@ void PolygonPathFinder::setup(const Vector<Vector2> &p_points, const Vector<int> outside_point.y = i == 0 ? p_points[0].y : (MAX(p_points[i].y, outside_point.y)); if (i == 0) { - bounds.pos = points[i].pos; + bounds.position = points[i].pos; } else { bounds.expand_to(points[i].pos); } diff --git a/scene/resources/segment_shape_2d.cpp b/scene/resources/segment_shape_2d.cpp index 145374ff05..99062d693b 100644 --- a/scene/resources/segment_shape_2d.cpp +++ b/scene/resources/segment_shape_2d.cpp @@ -35,7 +35,7 @@ void SegmentShape2D::_update_shape() { Rect2 r; - r.pos = a; + r.position = a; r.size = b; Physics2DServer::get_singleton()->shape_set_data(get_rid(), r); emit_changed(); @@ -69,7 +69,7 @@ void SegmentShape2D::draw(const RID &p_to_rid, const Color &p_color) { Rect2 SegmentShape2D::get_rect() const { Rect2 rect; - rect.pos = a; + rect.position = a; rect.expand_to(b); return rect; } @@ -121,7 +121,7 @@ void RayShape2D::draw(const RID &p_to_rid, const Color &p_color) { Rect2 RayShape2D::get_rect() const { Rect2 rect; - rect.pos = Vector2(); + rect.position = Vector2(); rect.expand_to(Vector2(0, length)); rect = rect.grow(0.707 * 4); return rect; diff --git a/scene/resources/shape.cpp b/scene/resources/shape.cpp index b449932b17..77f2096d9b 100644 --- a/scene/resources/shape.cpp +++ b/scene/resources/shape.cpp @@ -49,14 +49,14 @@ void Shape::add_vertices_to_array(PoolVector<Vector3> &array, const Transform &p } } -Ref<Mesh> Shape::get_debug_mesh() { +Ref<ArrayMesh> Shape::get_debug_mesh() { if (debug_mesh_cache.is_valid()) return debug_mesh_cache; Vector<Vector3> lines = _gen_debug_mesh_lines(); - debug_mesh_cache = Ref<Mesh>(memnew(Mesh)); + debug_mesh_cache = Ref<ArrayMesh>(memnew(ArrayMesh)); if (!lines.empty()) { //make mesh diff --git a/scene/resources/shape.h b/scene/resources/shape.h index 01b8db650e..ea3ba9ab0a 100644 --- a/scene/resources/shape.h +++ b/scene/resources/shape.h @@ -31,7 +31,7 @@ #define SHAPE_H #include "resource.h" -class Mesh; +class ArrayMesh; class Shape : public Resource { @@ -40,7 +40,7 @@ class Shape : public Resource { RES_BASE_EXTENSION("shp"); RID shape; - Ref<Mesh> debug_mesh_cache; + Ref<ArrayMesh> debug_mesh_cache; protected: _FORCE_INLINE_ RID get_shape() const { return shape; } @@ -50,7 +50,7 @@ protected: public: virtual RID get_rid() const { return shape; } - Ref<Mesh> get_debug_mesh(); + Ref<ArrayMesh> get_debug_mesh(); void add_vertices_to_array(PoolVector<Vector3> &array, const Transform &p_xform); diff --git a/scene/resources/shape_line_2d.cpp b/scene/resources/shape_line_2d.cpp index 89c1cea252..c38ae04eff 100644 --- a/scene/resources/shape_line_2d.cpp +++ b/scene/resources/shape_line_2d.cpp @@ -76,7 +76,7 @@ Rect2 LineShape2D::get_rect() const { Vector2 l1[2] = { point - get_normal().tangent() * 100, point + get_normal().tangent() * 100 }; Vector2 l2[2] = { point, point + get_normal() * 30 }; Rect2 rect; - rect.pos = l1[0]; + rect.position = l1[0]; rect.expand_to(l1[1]); rect.expand_to(l2[0]); rect.expand_to(l2[1]); diff --git a/scene/resources/style_box.cpp b/scene/resources/style_box.cpp index b665de2de1..8874cbc102 100644 --- a/scene/resources/style_box.cpp +++ b/scene/resources/style_box.cpp @@ -138,8 +138,8 @@ void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const { texture->get_rect_region(rect, src_rect, rect, src_rect); - rect.pos.x -= expand_margin[MARGIN_LEFT]; - rect.pos.y -= expand_margin[MARGIN_TOP]; + rect.position.x -= expand_margin[MARGIN_LEFT]; + rect.position.y -= expand_margin[MARGIN_TOP]; rect.size.x += expand_margin[MARGIN_LEFT] + expand_margin[MARGIN_RIGHT]; rect.size.y += expand_margin[MARGIN_TOP] + expand_margin[MARGIN_BOTTOM]; @@ -352,26 +352,26 @@ void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const { color_downright.b = (border_size - i) * color_downright.b / border_size + i * bg_color.b / border_size; } - vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r.pos.x, r.pos.y + r.size.y - 1), Size2(r.size.x, 1)), color_downright); - vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r.pos.x + r.size.x - 1, r.pos.y), Size2(1, r.size.y)), color_downright); + vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r.position.x, r.position.y + r.size.y - 1), Size2(r.size.x, 1)), color_downright); + vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r.position.x + r.size.x - 1, r.position.y), Size2(1, r.size.y)), color_downright); - vs->canvas_item_add_rect(p_canvas_item, Rect2(r.pos, Size2(r.size.x, 1)), color_upleft); - vs->canvas_item_add_rect(p_canvas_item, Rect2(r.pos, Size2(1, r.size.y)), color_upleft); + vs->canvas_item_add_rect(p_canvas_item, Rect2(r.position, Size2(r.size.x, 1)), color_upleft); + vs->canvas_item_add_rect(p_canvas_item, Rect2(r.position, Size2(1, r.size.y)), color_upleft); - r.pos.x++; - r.pos.y++; + r.position.x++; + r.position.y++; r.size.x -= 2; r.size.y -= 2; } if (draw_center) - vs->canvas_item_add_rect(p_canvas_item, Rect2(r.pos, r.size), bg_color); + vs->canvas_item_add_rect(p_canvas_item, Rect2(r.position, r.size), bg_color); Rect2i r_add = p_rect; - vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r_add.pos.x - additional_border_size[MARGIN_LEFT], r_add.pos.y - additional_border_size[MARGIN_TOP]), Size2(r_add.size.width + additional_border_size[MARGIN_LEFT] + additional_border_size[MARGIN_RIGHT], additional_border_size[MARGIN_TOP])), light_color); - vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r_add.pos.x - additional_border_size[MARGIN_LEFT], r_add.pos.y), Size2(additional_border_size[MARGIN_LEFT], r_add.size.height)), light_color); - vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r_add.pos.x + r_add.size.width, r_add.pos.y), Size2(additional_border_size[MARGIN_RIGHT], r_add.size.height)), dark_color); - vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r_add.pos.x - additional_border_size[MARGIN_LEFT], r_add.pos.y + r_add.size.height), Size2(r_add.size.width + additional_border_size[MARGIN_LEFT] + additional_border_size[MARGIN_RIGHT], additional_border_size[MARGIN_BOTTOM])), dark_color); + vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r_add.position.x - additional_border_size[MARGIN_LEFT], r_add.position.y - additional_border_size[MARGIN_TOP]), Size2(r_add.size.width + additional_border_size[MARGIN_LEFT] + additional_border_size[MARGIN_RIGHT], additional_border_size[MARGIN_TOP])), light_color); + vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r_add.position.x - additional_border_size[MARGIN_LEFT], r_add.position.y), Size2(additional_border_size[MARGIN_LEFT], r_add.size.height)), light_color); + vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r_add.position.x + r_add.size.width, r_add.position.y), Size2(additional_border_size[MARGIN_RIGHT], r_add.size.height)), dark_color); + vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r_add.position.x - additional_border_size[MARGIN_LEFT], r_add.position.y + r_add.size.height), Size2(r_add.size.width + additional_border_size[MARGIN_LEFT] + additional_border_size[MARGIN_RIGHT], additional_border_size[MARGIN_BOTTOM])), dark_color); } float StyleBoxFlat::get_style_margin(Margin p_margin) const { diff --git a/scene/resources/surface_tool.cpp b/scene/resources/surface_tool.cpp index 4c36d79a7a..60fb97c792 100644 --- a/scene/resources/surface_tool.cpp +++ b/scene/resources/surface_tool.cpp @@ -224,13 +224,13 @@ void SurfaceTool::add_index(int p_index) { index_array.push_back(p_index); } -Ref<Mesh> SurfaceTool::commit(const Ref<Mesh> &p_existing) { +Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { - Ref<Mesh> mesh; + Ref<ArrayMesh> mesh; if (p_existing.is_valid()) mesh = p_existing; else - mesh = Ref<Mesh>(memnew(Mesh)); + mesh.instance(); int varr_len = vertex_array.size(); diff --git a/scene/resources/surface_tool.h b/scene/resources/surface_tool.h index b143086e11..753c3626b8 100644 --- a/scene/resources/surface_tool.h +++ b/scene/resources/surface_tool.h @@ -125,7 +125,7 @@ public: void create_from(const Ref<Mesh> &p_existing, int p_surface); void append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform &p_xform); - Ref<Mesh> commit(const Ref<Mesh> &p_existing = Ref<Mesh>()); + Ref<ArrayMesh> commit(const Ref<ArrayMesh> &p_existing = Ref<ArrayMesh>()); SurfaceTool(); }; diff --git a/scene/resources/texture.cpp b/scene/resources/texture.cpp index bc8deb501e..0b8ad5536c 100644 --- a/scene/resources/texture.cpp +++ b/scene/resources/texture.cpp @@ -494,9 +494,9 @@ Error StreamTexture::_load_data(const String &p_path, int &tw, int &th, int &fla img = Image::lossy_unpacker(pv); } - if (img.is_null()) { + if (img.is_null() || img->empty()) { memdelete(f); - ERR_FAIL_COND_V(img->empty(), ERR_FILE_CORRUPT); + ERR_FAIL_COND_V(img.is_null() || img->empty(), ERR_FILE_CORRUPT); } total_size += img->get_data().size(); @@ -860,7 +860,7 @@ void AtlasTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_m rc.size.height = atlas->get_height(); } - VS::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, Rect2(p_pos + margin.pos, rc.size), atlas->get_rid(), rc, p_modulate, p_transpose); + VS::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, Rect2(p_pos + margin.position, rc.size), atlas->get_rid(), rc, p_modulate, p_transpose); } void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const { @@ -879,7 +879,7 @@ void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile } Vector2 scale = p_rect.size / (region.size + margin.size); - Rect2 dr(p_rect.pos + margin.pos * scale, rc.size * scale); + Rect2 dr(p_rect.position + margin.position * scale, rc.size * scale); VS::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, dr, atlas->get_rid(), rc, p_modulate, p_transpose); } @@ -892,24 +892,24 @@ void AtlasTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, cons return; Rect2 src = p_src_rect; - src.pos += (rc.pos - margin.pos); + src.position += (rc.position - margin.position); Rect2 src_c = rc.clip(src); if (src_c.size == Size2()) return; - Vector2 ofs = (src_c.pos - src.pos); + Vector2 ofs = (src_c.position - src.position); Vector2 scale = p_rect.size / p_src_rect.size; if (scale.x < 0) { - float mx = (margin.size.width - margin.pos.x); - mx -= margin.pos.x; + float mx = (margin.size.width - margin.position.x); + mx -= margin.position.x; ofs.x = -(ofs.x + mx); } if (scale.y < 0) { - float my = margin.size.height - margin.pos.y; - my -= margin.pos.y; + float my = margin.size.height - margin.position.y; + my -= margin.position.y; ofs.y = -(ofs.y + my); } - Rect2 dr(p_rect.pos + ofs * scale, src_c.size * scale); + Rect2 dr(p_rect.position + ofs * scale, src_c.size * scale); VS::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, dr, atlas->get_rid(), src_c, p_modulate, p_transpose); } @@ -922,24 +922,24 @@ bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, return false; Rect2 src = p_src_rect; - src.pos += (rc.pos - margin.pos); + src.position += (rc.position - margin.position); Rect2 src_c = rc.clip(src); if (src_c.size == Size2()) return false; - Vector2 ofs = (src_c.pos - src.pos); + Vector2 ofs = (src_c.position - src.position); Vector2 scale = p_rect.size / p_src_rect.size; if (scale.x < 0) { - float mx = (margin.size.width - margin.pos.x); - mx -= margin.pos.x; + float mx = (margin.size.width - margin.position.x); + mx -= margin.position.x; ofs.x = -(ofs.x + mx); } if (scale.y < 0) { - float my = margin.size.height - margin.pos.y; - my -= margin.pos.y; + float my = margin.size.height - margin.position.y; + my -= margin.position.y; ofs.y = -(ofs.y + my); } - Rect2 dr(p_rect.pos + ofs * scale, src_c.size * scale); + Rect2 dr(p_rect.position + ofs * scale, src_c.size * scale); r_rect = dr; r_src_rect = src_c; @@ -1096,7 +1096,7 @@ void LargeTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile for (int i = 0; i < pieces.size(); i++) { // TODO - pieces[i].texture->draw_rect(p_canvas_item, Rect2(pieces[i].offset * scale + p_rect.pos, pieces[i].texture->get_size() * scale), false, p_modulate, p_transpose); + pieces[i].texture->draw_rect(p_canvas_item, Rect2(pieces[i].offset * scale + p_rect.position, pieces[i].texture->get_size() * scale), false, p_modulate, p_transpose); } } void LargeTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose) const { @@ -1116,8 +1116,8 @@ void LargeTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, cons Rect2 local = p_src_rect.clip(rect); Rect2 target = local; target.size *= scale; - target.pos = p_rect.pos + (p_src_rect.pos + rect.pos) * scale; - local.pos -= rect.pos; + target.position = p_rect.position + (p_src_rect.position + rect.position) * scale; + local.position -= rect.position; pieces[i].texture->draw_rect_region(p_canvas_item, target, local, p_modulate, p_transpose); } } @@ -1781,6 +1781,10 @@ float GradientTexture::get_offset(int pos) const { return 0; //TODO: Maybe throw some error instead? } +Ref<Image> GradientTexture::get_data() const { + return VisualServer::get_singleton()->texture_get_data(texture); +} + void GradientTexture::set_color(int pos, const Color &color) { if (points.size() <= pos) { points.resize(pos + 1); diff --git a/scene/resources/texture.h b/scene/resources/texture.h index 07416529ae..9ee9588d2b 100644 --- a/scene/resources/texture.h +++ b/scene/resources/texture.h @@ -539,6 +539,8 @@ public: return pointFirst.color.linear_interpolate(pointSecond.color, (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset)); } + virtual Ref<Image> get_data() const; + int get_points_count() const; GradientTexture(); diff --git a/scene/resources/world_2d.cpp b/scene/resources/world_2d.cpp index db72ccd03c..ec9ec96e50 100644 --- a/scene/resources/world_2d.cpp +++ b/scene/resources/world_2d.cpp @@ -97,9 +97,9 @@ struct SpatialIndexer2D { void _notifier_update_cells(VisibilityNotifier2D *p_notifier, const Rect2 &p_rect, bool p_add) { - Point2i begin = p_rect.pos; + Point2i begin = p_rect.position; begin /= cell_size; - Point2i end = p_rect.pos + p_rect.size; + Point2i end = p_rect.position + p_rect.size; end /= cell_size; for (int i = begin.x; i <= end.x; i++) { @@ -220,9 +220,9 @@ struct SpatialIndexer2D { for (Map<Viewport *, ViewportData>::Element *E = viewports.front(); E; E = E->next()) { - Point2i begin = E->get().rect.pos; + Point2i begin = E->get().rect.position; begin /= cell_size; - Point2i end = E->get().rect.pos + E->get().rect.size; + Point2i end = E->get().rect.position + E->get().rect.size; end /= cell_size; pass++; List<VisibilityNotifier2D *> added; diff --git a/servers/physics/collision_object_sw.cpp b/servers/physics/collision_object_sw.cpp index 5eb10a4d09..dc0b2fc84f 100644 --- a/servers/physics/collision_object_sw.cpp +++ b/servers/physics/collision_object_sw.cpp @@ -164,7 +164,7 @@ void CollisionObjectSW::_update_shapes_with_motion(const Vector3 &p_motion) { Rect3 shape_aabb = s.shape->get_aabb(); Transform xform = transform * s.xform; shape_aabb = xform.xform(shape_aabb); - shape_aabb = shape_aabb.merge(Rect3(shape_aabb.pos + p_motion, shape_aabb.size)); //use motion + shape_aabb = shape_aabb.merge(Rect3(shape_aabb.position + p_motion, shape_aabb.size)); //use motion s.aabb_cache = shape_aabb; space->get_broadphase()->move(s.bpid, shape_aabb); diff --git a/servers/physics/collision_solver_sw.cpp b/servers/physics/collision_solver_sw.cpp index 38ce31ec78..32a42bcaf4 100644 --- a/servers/physics/collision_solver_sw.cpp +++ b/servers/physics/collision_solver_sw.cpp @@ -166,7 +166,7 @@ bool CollisionSolverSW::solve_concave(const ShapeSW *p_shape_A, const Transform smin *= axis_scale; smax *= axis_scale; - local_aabb.pos[i] = smin; + local_aabb.position[i] = smin; local_aabb.size[i] = smax - smin; } @@ -332,7 +332,7 @@ bool CollisionSolverSW::solve_distance(const ShapeSW *p_shape_A, const Transform Rect3 cc_hint_aabb; if (use_cc_hint) { cc_hint_aabb = p_concave_hint; - cc_hint_aabb.pos -= p_transform_B.origin; + cc_hint_aabb.position -= p_transform_B.origin; } Rect3 local_aabb; @@ -353,7 +353,7 @@ bool CollisionSolverSW::solve_distance(const ShapeSW *p_shape_A, const Transform smin *= axis_scale; smax *= axis_scale; - local_aabb.pos[i] = smin; + local_aabb.position[i] = smin; local_aabb.size[i] = smax - smin; } diff --git a/servers/physics/shape_sw.cpp b/servers/physics/shape_sw.cpp index ff7b442442..7b3df37a63 100644 --- a/servers/physics/shape_sw.cpp +++ b/servers/physics/shape_sw.cpp @@ -757,7 +757,7 @@ void ConvexPolygonShapeSW::_setup(const Vector<Vector3> &p_vertices) { for (int i = 0; i < mesh.vertices.size(); i++) { if (i == 0) - _aabb.pos = mesh.vertices[i]; + _aabb.position = mesh.vertices[i]; else _aabb.expand_to(mesh.vertices[i]); } @@ -1388,7 +1388,7 @@ void ConcavePolygonShapeSW::_setup(PoolVector<Vector3> p_faces) { Face3 face(facesr[i * 3 + 0], facesr[i * 3 + 1], facesr[i * 3 + 2]); bvh_arrayw[i].aabb = face.get_aabb(); - bvh_arrayw[i].center = bvh_arrayw[i].aabb.pos + bvh_arrayw[i].aabb.size * 0.5; + bvh_arrayw[i].center = bvh_arrayw[i].aabb.position + bvh_arrayw[i].aabb.size * 0.5; bvh_arrayw[i].face_index = i; facesw[i].indices[0] = i * 3 + 0; facesw[i].indices[1] = i * 3 + 1; @@ -1504,7 +1504,7 @@ void HeightMapShapeSW::_setup(PoolVector<real_t> p_heights, int p_width, int p_d Vector3 pos(j * cell_size, h, i * cell_size); if (i == 0 || j == 0) - aabb.pos = pos; + aabb.position = pos; else aabb.expand_to(pos); } diff --git a/servers/physics/space_sw.cpp b/servers/physics/space_sw.cpp index a4bf857bd8..16562dce6b 100644 --- a/servers/physics/space_sw.cpp +++ b/servers/physics/space_sw.cpp @@ -180,7 +180,7 @@ bool PhysicsDirectSpaceStateSW::cast_motion(const RID &p_shape, const Transform ERR_FAIL_COND_V(!shape, false); Rect3 aabb = p_xform.xform(shape->get_aabb()); - aabb = aabb.merge(Rect3(aabb.pos + p_motion, aabb.size)); //motion + aabb = aabb.merge(Rect3(aabb.position + p_motion, aabb.size)); //motion aabb = aabb.grow(p_margin); /* diff --git a/servers/physics_2d/broad_phase_2d_hash_grid.cpp b/servers/physics_2d/broad_phase_2d_hash_grid.cpp index e39a5b6df1..438cd416f6 100644 --- a/servers/physics_2d/broad_phase_2d_hash_grid.cpp +++ b/servers/physics_2d/broad_phase_2d_hash_grid.cpp @@ -116,8 +116,8 @@ void BroadPhase2DHashGrid::_enter_grid(Element *p_elem, const Rect2 &p_rect, boo return; } - Point2i from = (p_rect.pos / cell_size).floor(); - Point2i to = ((p_rect.pos + p_rect.size) / cell_size).floor(); + Point2i from = (p_rect.position / cell_size).floor(); + Point2i to = ((p_rect.position + p_rect.size) / cell_size).floor(); for (int i = from.x; i <= to.x; i++) { @@ -214,8 +214,8 @@ void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool return; } - Point2i from = (p_rect.pos / cell_size).floor(); - Point2i to = ((p_rect.pos + p_rect.size) / cell_size).floor(); + Point2i from = (p_rect.position / cell_size).floor(); + Point2i to = ((p_rect.position + p_rect.size) / cell_size).floor(); for (int i = from.x; i <= to.x; i++) { @@ -574,8 +574,8 @@ int BroadPhase2DHashGrid::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p pass++; - Point2i from = (p_aabb.pos / cell_size).floor(); - Point2i to = ((p_aabb.pos + p_aabb.size) / cell_size).floor(); + Point2i from = (p_aabb.position / cell_size).floor(); + Point2i to = ((p_aabb.position + p_aabb.size) / cell_size).floor(); int cullcount = 0; for (int i = from.x; i <= to.x; i++) { diff --git a/servers/physics_2d/collision_object_2d_sw.cpp b/servers/physics_2d/collision_object_2d_sw.cpp index 265b5bb836..b6c1d145df 100644 --- a/servers/physics_2d/collision_object_2d_sw.cpp +++ b/servers/physics_2d/collision_object_2d_sw.cpp @@ -169,7 +169,7 @@ void CollisionObject2DSW::_update_shapes_with_motion(const Vector2 &p_motion) { Rect2 shape_aabb = s.shape->get_aabb(); Transform2D xform = transform * s.xform; shape_aabb = xform.xform(shape_aabb); - shape_aabb = shape_aabb.merge(Rect2(shape_aabb.pos + p_motion, shape_aabb.size)); //use motion + shape_aabb = shape_aabb.merge(Rect2(shape_aabb.position + p_motion, shape_aabb.size)); //use motion s.aabb_cache = shape_aabb; space->get_broadphase()->move(s.bpid, shape_aabb); diff --git a/servers/physics_2d/collision_solver_2d_sw.cpp b/servers/physics_2d/collision_solver_2d_sw.cpp index e57d1b7044..b482f826c2 100644 --- a/servers/physics_2d/collision_solver_2d_sw.cpp +++ b/servers/physics_2d/collision_solver_2d_sw.cpp @@ -203,7 +203,7 @@ bool CollisionSolver2DSW::solve_concave(const Shape2DSW *p_shape_A, const Transf smin *= axis_scale; smax *= axis_scale; - local_aabb.pos[i] = smin; + local_aabb.position[i] = smin; local_aabb.size[i] = smax - smin; } diff --git a/servers/physics_2d/shape_2d_sw.cpp b/servers/physics_2d/shape_2d_sw.cpp index 203a1052b2..245b4e15bc 100644 --- a/servers/physics_2d/shape_2d_sw.cpp +++ b/servers/physics_2d/shape_2d_sw.cpp @@ -246,12 +246,12 @@ void SegmentShape2DSW::set_data(const Variant &p_data) { ERR_FAIL_COND(p_data.get_type() != Variant::RECT2); Rect2 r = p_data; - a = r.pos; + a = r.position; b = r.size; n = (b - a).tangent(); Rect2 aabb; - aabb.pos = a; + aabb.position = a; aabb.expand_to(b); if (aabb.size.x == 0) aabb.size.x = 0.001; @@ -263,7 +263,7 @@ void SegmentShape2DSW::set_data(const Variant &p_data) { Variant SegmentShape2DSW::get_data() const { Rect2 r; - r.pos = a; + r.position = a; r.size = b; return r; } @@ -621,13 +621,13 @@ bool ConvexPolygonShape2DSW::intersect_segment(const Vector2 &p_begin, const Vec real_t ConvexPolygonShape2DSW::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const { Rect2 aabb; - aabb.pos = points[0].pos * p_scale; + aabb.position = points[0].pos * p_scale; for (int i = 0; i < point_count; i++) { aabb.expand_to(points[i].pos * p_scale); } - return p_mass * aabb.size.dot(aabb.size) / 12.0 + p_mass * (aabb.pos + aabb.size * 0.5).length_squared(); + return p_mass * aabb.size.dot(aabb.size) / 12.0 + p_mass * (aabb.position + aabb.size * 0.5).length_squared(); } void ConvexPolygonShape2DSW::set_data(const Variant &p_data) { @@ -677,7 +677,7 @@ void ConvexPolygonShape2DSW::set_data(const Variant &p_data) { ERR_FAIL_COND(point_count == 0); Rect2 aabb; - aabb.pos = points[0].pos; + aabb.position = points[0].pos; for (int i = 1; i < point_count; i++) aabb.expand_to(points[i].pos); @@ -942,7 +942,7 @@ void ConcavePolygonShape2DSW::set_data(const Variant &p_data) { } points.resize(pointmap.size()); - aabb.pos = pointmap.front()->key(); + aabb.position = pointmap.front()->key(); for (Map<Point2, int>::Element *E = pointmap.front(); E; E = E->next()) { aabb.expand_to(E->key()); @@ -953,7 +953,7 @@ void ConcavePolygonShape2DSW::set_data(const Variant &p_data) { main_vbh.resize(segments.size()); for (int i = 0; i < main_vbh.size(); i++) { - main_vbh[i].aabb.pos = points[segments[i].points[0]]; + main_vbh[i].aabb.position = points[segments[i].points[0]]; main_vbh[i].aabb.expand_to(points[segments[i].points[1]]); main_vbh[i].left = -1; main_vbh[i].right = i; diff --git a/servers/physics_2d/shape_2d_sw.h b/servers/physics_2d/shape_2d_sw.h index 547ecdcd11..4cc98b7f1e 100644 --- a/servers/physics_2d/shape_2d_sw.h +++ b/servers/physics_2d/shape_2d_sw.h @@ -513,7 +513,7 @@ class ConcavePolygonShape2DSW : public ConcaveShape2DSW { _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const { - return (a.aabb.pos.x + a.aabb.size.x * 0.5) < (b.aabb.pos.x + b.aabb.size.x * 0.5); + return (a.aabb.position.x + a.aabb.size.x * 0.5) < (b.aabb.position.x + b.aabb.size.x * 0.5); } }; @@ -521,7 +521,7 @@ class ConcavePolygonShape2DSW : public ConcaveShape2DSW { _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const { - return (a.aabb.pos.y + a.aabb.size.y * 0.5) < (b.aabb.pos.y + b.aabb.size.y * 0.5); + return (a.aabb.position.y + a.aabb.size.y * 0.5) < (b.aabb.position.y + b.aabb.size.y * 0.5); } }; diff --git a/servers/physics_2d/space_2d_sw.cpp b/servers/physics_2d/space_2d_sw.cpp index ac479aab7a..78b1e84734 100644 --- a/servers/physics_2d/space_2d_sw.cpp +++ b/servers/physics_2d/space_2d_sw.cpp @@ -50,7 +50,7 @@ int Physics2DDirectSpaceStateSW::intersect_point(const Vector2 &p_point, ShapeRe return 0; Rect2 aabb; - aabb.pos = p_point - Vector2(0.00001, 0.00001); + aabb.position = p_point - Vector2(0.00001, 0.00001); aabb.size = Vector2(0.00002, 0.00002); int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space2DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results); @@ -223,7 +223,7 @@ bool Physics2DDirectSpaceStateSW::cast_motion(const RID &p_shape, const Transfor ERR_FAIL_COND_V(!shape, false); Rect2 aabb = p_xform.xform(shape->get_aabb()); - aabb = aabb.merge(Rect2(aabb.pos + p_motion, aabb.size)); //motion + aabb = aabb.merge(Rect2(aabb.position + p_motion, aabb.size)); //motion aabb = aabb.grow(p_margin); /* @@ -339,7 +339,7 @@ bool Physics2DDirectSpaceStateSW::collide_shape(RID p_shape, const Transform2D & ERR_FAIL_COND_V(!shape, 0); Rect2 aabb = p_shape_xform.xform(shape->get_aabb()); - aabb = aabb.merge(Rect2(aabb.pos + p_motion, aabb.size)); //motion + aabb = aabb.merge(Rect2(aabb.position + p_motion, aabb.size)); //motion aabb = aabb.grow(p_margin); int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space2DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results); @@ -434,7 +434,7 @@ bool Physics2DDirectSpaceStateSW::rest_info(RID p_shape, const Transform2D &p_sh ERR_FAIL_COND_V(!shape, 0); Rect2 aabb = p_shape_xform.xform(shape->get_aabb()); - aabb = aabb.merge(Rect2(aabb.pos + p_motion, aabb.size)); //motion + aabb = aabb.merge(Rect2(aabb.position + p_motion, aabb.size)); //motion aabb = aabb.grow(p_margin); int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space2DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results); @@ -656,7 +656,7 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co } body_transform.elements[2] += recover_motion; - body_aabb.pos += recover_motion; + body_aabb.position += recover_motion; recover_attempts--; @@ -671,7 +671,7 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co // STEP 2 ATTEMPT MOTION Rect2 motion_aabb = body_aabb; - motion_aabb.pos += p_motion; + motion_aabb.position += p_motion; motion_aabb = motion_aabb.merge(body_aabb); int amount = _cull_aabb_for_body(p_body, motion_aabb); @@ -807,7 +807,7 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co Transform2D body_shape_xform = ugt * p_body->get_shape_transform(best_shape); Shape2DSW *body_shape = p_body->get_shape(best_shape); - body_aabb.pos += p_motion * unsafe; + body_aabb.position += p_motion * unsafe; int amount = _cull_aabb_for_body(p_body, body_aabb); diff --git a/servers/server_wrap_mt_common.h b/servers/server_wrap_mt_common.h index f6b78e8f40..d434a6622e 100644 --- a/servers/server_wrap_mt_common.h +++ b/servers/server_wrap_mt_common.h @@ -41,8 +41,9 @@ } #define FUNCRID(m_type) \ + List<RID> m_type##_id_pool; \ int m_type##allocn() { \ - for (int i = 0; i < m_type##_pool_max_size; i++) { \ + for (int i = 0; i < pool_max_size; i++) { \ m_type##_id_pool.push_back(server_name->m_type##_create()); \ } \ return 0; \ @@ -747,3 +748,21 @@ server_name->m_type(p1, p2, p3, p4, p5, p6, p7, p8); \ } \ } + +#define FUNC9(m_type, m_arg1, m_arg2, m_arg3, m_arg4, m_arg5, m_arg6, m_arg7, m_arg8, m_arg9) \ + virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7, m_arg8 p8, m_arg9 p9) { \ + if (Thread::get_caller_ID() != server_thread) { \ + command_queue.push(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7, p8, p9); \ + } else { \ + server_name->m_type(p1, p2, p3, p4, p5, p6, p7, p8, p9); \ + } \ + } + +#define FUNC10(m_type, m_arg1, m_arg2, m_arg3, m_arg4, m_arg5, m_arg6, m_arg7, m_arg8, m_arg9, m_arg10) \ + virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7, m_arg8 p8, m_arg9 p9, m_arg10 p10) { \ + if (Thread::get_caller_ID() != server_thread) { \ + command_queue.push(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); \ + } else { \ + server_name->m_type(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); \ + } \ + } diff --git a/servers/visual/rasterizer.h b/servers/visual/rasterizer.h index 4b274acd9e..c5fc2fe8af 100644 --- a/servers/visual/rasterizer.h +++ b/servers/visual/rasterizer.h @@ -71,6 +71,10 @@ public: virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) = 0; + virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) = 0; + virtual void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_curve, bool p_transmit, float p_transmit_curve) = 0; + virtual void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve) = 0; + struct InstanceBase : RID_Data { VS::InstanceType base_type; @@ -151,6 +155,7 @@ public: virtual void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) = 0; virtual void set_scene_pass(uint64_t p_pass) = 0; + virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw) = 0; virtual bool free(RID p_rid) = 0; @@ -464,6 +469,7 @@ public: enum RenderTargetFlags { RENDER_TARGET_VFLIP, RENDER_TARGET_TRANSPARENT, + RENDER_TARGET_NO_3D_EFFECTS, RENDER_TARGET_NO_3D, RENDER_TARGET_NO_SAMPLING, RENDER_TARGET_HDR, @@ -493,6 +499,14 @@ public: virtual void update_dirty_resources() = 0; + virtual void set_debug_generate_wireframes(bool p_generate) = 0; + + virtual void render_info_begin_capture() = 0; + virtual void render_info_end_capture() = 0; + virtual int get_captured_render_info(VS::RenderInfo p_info) = 0; + + virtual int get_render_info(VS::RenderInfo p_info) = 0; + static RasterizerStorage *base_singleton; RasterizerStorage(); virtual ~RasterizerStorage() {} @@ -532,6 +546,7 @@ public: float shadow_gradient_length; VS::CanvasLightShadowFilter shadow_filter; Color shadow_color; + float shadow_smooth; void *texture_cache; // implementation dependent Rect2 rect_cache; @@ -570,6 +585,7 @@ public: shadow_buffer_size = 256; shadow_gradient_length = 0; shadow_filter = VS::CANVAS_LIGHT_FILTER_NONE; + shadow_smooth = 0.0; } }; @@ -769,7 +785,7 @@ public: case Item::Command::TYPE_LINE: { const Item::CommandLine *line = static_cast<const Item::CommandLine *>(c); - r.pos = line->from; + r.position = line->from; r.expand_to(line->to); } break; case Item::Command::TYPE_RECT: { @@ -786,7 +802,7 @@ public: case Item::Command::TYPE_PRIMITIVE: { const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c); - r.pos = primitive->points[0]; + r.position = primitive->points[0]; for (int i = 1; i < primitive->points.size(); i++) { r.expand_to(primitive->points[i]); @@ -797,7 +813,7 @@ public: const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c); int l = polygon->points.size(); const Point2 *pp = &polygon->points[0]; - r.pos = pp[0]; + r.position = pp[0]; for (int i = 1; i < l; i++) { r.expand_to(pp[i]); @@ -808,7 +824,7 @@ public: const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c); Rect3 aabb = RasterizerStorage::base_singleton->mesh_get_aabb(mesh->mesh, mesh->skeleton); - r = Rect2(aabb.pos.x, aabb.pos.y, aabb.size.x, aabb.size.y); + r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y); } break; case Item::Command::TYPE_MULTIMESH: { @@ -816,13 +832,13 @@ public: const Item::CommandMultiMesh *multimesh = static_cast<const Item::CommandMultiMesh *>(c); Rect3 aabb = RasterizerStorage::base_singleton->multimesh_get_aabb(multimesh->multimesh); - r = Rect2(aabb.pos.x, aabb.pos.y, aabb.size.x, aabb.size.y); + r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y); } break; case Item::Command::TYPE_CIRCLE: { const Item::CommandCircle *circle = static_cast<const Item::CommandCircle *>(c); - r.pos = Point2(-circle->radius, -circle->radius) + circle->pos; + r.position = Point2(-circle->radius, -circle->radius) + circle->pos; r.size = Point2(circle->radius * 2.0, circle->radius * 2.0); } break; case Item::Command::TYPE_TRANSFORM: { diff --git a/servers/visual/shader_language.cpp b/servers/visual/shader_language.cpp index 9fe92f0fec..319351f8ec 100644 --- a/servers/visual/shader_language.cpp +++ b/servers/visual/shader_language.cpp @@ -3185,7 +3185,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Dat tk = _get_token(); if (tk.type != TK_PARENTHESIS_CLOSE) { - _set_error("Expected '(' after expression"); + _set_error("Expected ')' after expression"); return ERR_PARSE_ERROR; } @@ -3196,6 +3196,8 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Dat p_block->statements.push_back(cf); Error err = _parse_block(block, p_builtin_types, true, p_can_break, p_can_continue); + if (err) + return err; pos = _get_tkpos(); tk = _get_token(); @@ -3209,6 +3211,35 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Dat } else { _set_tkpos(pos); //rollback } + } else if (tk.type == TK_CF_WHILE) { + //if () {} + tk = _get_token(); + if (tk.type != TK_PARENTHESIS_OPEN) { + _set_error("Expected '(' after if"); + return ERR_PARSE_ERROR; + } + + ControlFlowNode *cf = alloc_node<ControlFlowNode>(); + cf->flow_op = FLOW_OP_WHILE; + Node *n = _parse_and_reduce_expression(p_block, p_builtin_types); + if (!n) + return ERR_PARSE_ERROR; + + tk = _get_token(); + if (tk.type != TK_PARENTHESIS_CLOSE) { + _set_error("Expected ')' after expression"); + return ERR_PARSE_ERROR; + } + + BlockNode *block = alloc_node<BlockNode>(); + block->parent_block = p_block; + cf->expressions.push_back(n); + cf->blocks.push_back(block); + p_block->statements.push_back(cf); + + Error err = _parse_block(block, p_builtin_types, true, p_can_break, p_can_continue); + if (err) + return err; } else if (tk.type == TK_CF_RETURN) { diff --git a/servers/visual/shader_types.cpp b/servers/visual/shader_types.cpp index 5bacc2ec51..e0201420fe 100644 --- a/servers/visual/shader_types.cpp +++ b/servers/visual/shader_types.cpp @@ -52,7 +52,6 @@ ShaderTypes::ShaderTypes() { shader_modes[VS::SHADER_SPATIAL].functions["vertex"]["SRC_VERTEX"] = ShaderLanguage::TYPE_VEC3; shader_modes[VS::SHADER_SPATIAL].functions["vertex"]["SRC_NORMAL"] = ShaderLanguage::TYPE_VEC3; - shader_modes[VS::SHADER_SPATIAL].functions["vertex"]["SRC_TANGENT"] = ShaderLanguage::TYPE_VEC4; shader_modes[VS::SHADER_SPATIAL].functions["vertex"]["SRC_BONES"] = ShaderLanguage::TYPE_IVEC4; shader_modes[VS::SHADER_SPATIAL].functions["vertex"]["SRC_WEIGHTS"] = ShaderLanguage::TYPE_VEC4; @@ -105,8 +104,11 @@ ShaderTypes::ShaderTypes() { shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["EMISSION"] = ShaderLanguage::TYPE_VEC3; shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["SPECIAL"] = ShaderLanguage::TYPE_FLOAT; shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["DISCARD"] = ShaderLanguage::TYPE_BOOL; + shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["SCREEN_TEXTURE"] = ShaderLanguage::TYPE_SAMPLER2D; + shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["DEPTH_TEXTURE"] = ShaderLanguage::TYPE_SAMPLER2D; shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["SCREEN_UV"] = ShaderLanguage::TYPE_VEC2; shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["POINT_COORD"] = ShaderLanguage::TYPE_VEC2; + shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["SIDE"] = ShaderLanguage::TYPE_FLOAT; shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["WORLD_MATRIX"] = ShaderLanguage::TYPE_MAT4; shader_modes[VS::SHADER_SPATIAL].functions["fragment"]["INV_CAMERA_MATRIX"] = ShaderLanguage::TYPE_MAT4; @@ -131,6 +133,11 @@ ShaderTypes::ShaderTypes() { shader_modes[VS::SHADER_SPATIAL].modes.insert("unshaded"); shader_modes[VS::SHADER_SPATIAL].modes.insert("ontop"); + shader_modes[VS::SHADER_SPATIAL].modes.insert("diffuse_lambert"); + shader_modes[VS::SHADER_SPATIAL].modes.insert("diffuse_half_lambert"); + shader_modes[VS::SHADER_SPATIAL].modes.insert("diffuse_oren_nayar"); + shader_modes[VS::SHADER_SPATIAL].modes.insert("diffuse_burley"); + shader_modes[VS::SHADER_SPATIAL].modes.insert("skip_default_transform"); /************ CANVAS ITEM **************************/ @@ -166,8 +173,6 @@ ShaderTypes::ShaderTypes() { shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"]["COLOR"] = ShaderLanguage::TYPE_VEC4; shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"]["TEXTURE"] = ShaderLanguage::TYPE_SAMPLER2D; shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"]["TEXTURE_PIXEL_SIZE"] = ShaderLanguage::TYPE_VEC2; - shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"]["VAR1"] = ShaderLanguage::TYPE_VEC4; - shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"]["VAR2"] = ShaderLanguage::TYPE_VEC4; shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"]["SCREEN_UV"] = ShaderLanguage::TYPE_VEC2; shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"]["LIGHT_VEC"] = ShaderLanguage::TYPE_VEC2; shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"]["LIGHT_HEIGHT"] = ShaderLanguage::TYPE_FLOAT; diff --git a/servers/visual/visual_server_canvas.cpp b/servers/visual/visual_server_canvas.cpp index 3a83ba887d..48e6a3d006 100644 --- a/servers/visual/visual_server_canvas.cpp +++ b/servers/visual/visual_server_canvas.cpp @@ -61,7 +61,7 @@ void VisualServerCanvas::_render_canvas_item(Item *p_canvas_item, const Transfor Rect2 rect = ci->get_rect(); Transform2D xform = p_transform * ci->xform; Rect2 global_rect = xform.xform(rect); - global_rect.pos += p_clip_rect.pos; + global_rect.position += p_clip_rect.position; if (ci->use_parent_material && p_material_owner) ci->material_owner = p_material_owner; @@ -119,7 +119,7 @@ void VisualServerCanvas::_render_canvas_item(Item *p_canvas_item, const Transfor ci->final_transform = xform; ci->final_modulate = Color(modulate.r * ci->self_modulate.r, modulate.g * ci->self_modulate.g, modulate.b * ci->self_modulate.b, modulate.a * ci->self_modulate.a); ci->global_rect_cache = global_rect; - ci->global_rect_cache.pos -= p_clip_rect.pos; + ci->global_rect_cache.position -= p_clip_rect.position; ci->light_masked = false; int zidx = p_z - VS::CANVAS_ITEM_Z_MIN; @@ -233,7 +233,6 @@ void VisualServerCanvas::render_canvas(Canvas *p_canvas, const Transform2D &p_tr } VSG::canvas_render->canvas_end(); - } RID VisualServerCanvas::canvas_create() { @@ -917,6 +916,13 @@ void VisualServerCanvas::canvas_light_set_shadow_color(RID p_light, const Color clight->shadow_color = p_color; } +void VisualServerCanvas::canvas_light_set_shadow_smooth(RID p_light, float p_smooth) { + + RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light); + ERR_FAIL_COND(!clight); + clight->shadow_smooth = p_smooth; +} + RID VisualServerCanvas::canvas_light_occluder_create() { RasterizerCanvas::LightOccluderInstance *occluder = memnew(RasterizerCanvas::LightOccluderInstance); @@ -1041,7 +1047,7 @@ void VisualServerCanvas::canvas_occluder_polygon_set_shape_as_lines(RID p_occlud PoolVector<Vector2>::Read r = p_shape.read(); for (int i = 0; i < lc; i++) { if (i == 0) - occluder_poly->aabb.pos = r[i]; + occluder_poly->aabb.position = r[i]; else occluder_poly->aabb.expand_to(r[i]); } diff --git a/servers/visual/visual_server_canvas.h b/servers/visual/visual_server_canvas.h index 47b057f96a..b4ddf2dc8e 100644 --- a/servers/visual/visual_server_canvas.h +++ b/servers/visual/visual_server_canvas.h @@ -209,6 +209,7 @@ public: void canvas_light_set_shadow_gradient_length(RID p_light, float p_length); void canvas_light_set_shadow_filter(RID p_light, VS::CanvasLightShadowFilter p_filter); void canvas_light_set_shadow_color(RID p_light, const Color &p_color); + void canvas_light_set_shadow_smooth(RID p_light, float p_smooth); RID canvas_light_occluder_create(); void canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas); diff --git a/servers/visual/visual_server_raster.cpp b/servers/visual/visual_server_raster.cpp index 2fdff29f0a..8d332b3e6c 100644 --- a/servers/visual/visual_server_raster.cpp +++ b/servers/visual/visual_server_raster.cpp @@ -74,6 +74,19 @@ void VisualServerRaster::free(RID p_rid) { /* EVENT QUEUING */ +void VisualServerRaster::request_frame_drawn_callback(Object *p_where, const StringName &p_method, const Variant &p_userdata) { + + ERR_FAIL_NULL(p_where); + FrameDrawnCallbacks fdc; + fdc.object = p_where->get_instance_ID(); + fdc.method = p_method; + fdc.param = p_userdata; + + frame_drawn_callbacks.push_back(fdc); + + print_line("added callback to draw"); +} + void VisualServerRaster::draw() { /* @@ -92,6 +105,22 @@ void VisualServerRaster::draw() { //_draw_cursors_and_margins(); VSG::rasterizer->end_frame(); //draw_extra_frame=VS:rasterizer->needs_to_draw_next_frame(); + + while (frame_drawn_callbacks.front()) { + + Object *obj = ObjectDB::get_instance(frame_drawn_callbacks.front()->get().object); + if (obj) { + Variant::CallError ce; + const Variant *v = &frame_drawn_callbacks.front()->get().param; + obj->call(frame_drawn_callbacks.front()->get().method, &v, 1, ce); + if (ce.error != Variant::CallError::CALL_OK) { + String err = Variant::get_call_error_text(obj, frame_drawn_callbacks.front()->get().method, &v, 1, ce); + ERR_PRINTS("Error calling frame drawn function: " + err); + } + } + + frame_drawn_callbacks.pop_front(); + } } void VisualServerRaster::sync() { } @@ -116,7 +145,7 @@ void VisualServerRaster::finish() { int VisualServerRaster::get_render_info(RenderInfo p_info) { - return 0; + return VSG::storage->get_render_info(p_info); } /* TESTING */ @@ -146,6 +175,11 @@ bool VisualServerRaster::has_os_feature(const String &p_feature) const { return VSG::storage->has_os_feature(p_feature); } +void VisualServerRaster::set_debug_generate_wireframes(bool p_generate) { + + VSG::storage->set_debug_generate_wireframes(p_generate); +} + VisualServerRaster::VisualServerRaster() { VSG::canvas = memnew(VisualServerCanvas); diff --git a/servers/visual/visual_server_raster.h b/servers/visual/visual_server_raster.h index 957af7b9dd..dc44755726 100644 --- a/servers/visual/visual_server_raster.h +++ b/servers/visual/visual_server_raster.h @@ -61,6 +61,15 @@ class VisualServerRaster : public VisualServer { bool draw_extra_frame; RID test_cube; + struct FrameDrawnCallbacks { + + ObjectID object; + StringName method; + Variant param; + }; + + List<FrameDrawnCallbacks> frame_drawn_callbacks; + #if 0 struct Room { @@ -586,6 +595,8 @@ public: m_r m_name(m_type1 arg1) { return BINDBASE->m_name(arg1); } #define BIND1RC(m_r, m_name, m_type1) \ m_r m_name(m_type1 arg1) const { return BINDBASE->m_name(arg1); } +#define BIND2R(m_r, m_name, m_type1, m_type2) \ + m_r m_name(m_type1 arg1, m_type2 arg2) { return BINDBASE->m_name(arg1, arg2); } #define BIND2RC(m_r, m_name, m_type1, m_type2) \ m_r m_name(m_type1 arg1, m_type2 arg2) const { return BINDBASE->m_name(arg1, arg2); } #define BIND3RC(m_r, m_name, m_type1, m_type2, m_type3) \ @@ -632,7 +643,6 @@ public: BIND1RC(uint32_t, texture_get_width, RID) BIND1RC(uint32_t, texture_get_height, RID) BIND3(texture_set_size_override, RID, int, int) - BIND2RC(RID, texture_create_radiance_cubemap, RID, int) BIND3(texture_set_detect_3d_callback, RID, TextureDetectCallback, void *) BIND3(texture_set_detect_srgb_callback, RID, TextureDetectCallback, void *) @@ -922,6 +932,10 @@ public: BIND3(viewport_set_shadow_atlas_quadrant_subdivision, RID, int, int) BIND2(viewport_set_msaa, RID, ViewportMSAA) BIND2(viewport_set_hdr, RID, bool) + BIND2(viewport_set_usage, RID, ViewportUsage) + + BIND2R(int, viewport_get_render_info, RID, ViewportRenderInfo) + BIND2(viewport_set_debug_draw, RID, ViewportDebugDraw) /* ENVIRONMENT API */ @@ -944,12 +958,15 @@ public: BIND6(environment_set_dof_blur_near, RID, bool, float, float, float, EnvironmentDOFBlurQuality) BIND6(environment_set_dof_blur_far, RID, bool, float, float, float, EnvironmentDOFBlurQuality) BIND10(environment_set_glow, RID, bool, int, float, float, float, EnvironmentGlowBlendMode, float, float, bool) - BIND5(environment_set_fog, RID, bool, float, float, RID) BIND9(environment_set_tonemap, RID, EnvironmentToneMapper, float, float, bool, float, float, float, float) BIND6(environment_set_adjustment, RID, bool, float, float, float, RID) + BIND5(environment_set_fog, RID, bool, const Color &, const Color &, float) + BIND6(environment_set_fog_depth, RID, bool, float, float, bool, float) + BIND5(environment_set_fog_height, RID, bool, float, float, float) + /* SCENARIO API */ #undef BINDBASE @@ -1065,6 +1082,7 @@ public: BIND2(canvas_light_set_shadow_gradient_length, RID, float) BIND2(canvas_light_set_shadow_filter, RID, CanvasLightShadowFilter) BIND2(canvas_light_set_shadow_color, RID, const Color &) + BIND2(canvas_light_set_shadow_smooth, RID, float) BIND0R(RID, canvas_light_occluder_create) BIND2(canvas_light_occluder_attach_to_canvas, RID, RID) @@ -1096,6 +1114,8 @@ public: /* EVENT QUEUING */ + virtual void request_frame_drawn_callback(Object *p_where, const StringName &p_method, const Variant &p_userdata); + virtual void draw(); virtual void sync(); virtual bool has_changed() const; @@ -1116,6 +1136,7 @@ public: virtual bool has_feature(Features p_feature) const; virtual bool has_os_feature(const String &p_feature) const; + virtual void set_debug_generate_wireframes(bool p_generate); VisualServerRaster(); ~VisualServerRaster(); diff --git a/servers/visual/visual_server_viewport.cpp b/servers/visual/visual_server_viewport.cpp index 63ed0ac7c4..a99b890601 100644 --- a/servers/visual/visual_server_viewport.cpp +++ b/servers/visual/visual_server_viewport.cpp @@ -88,7 +88,7 @@ void VisualServerViewport::_draw_viewport(Viewport *p_viewport) { } } - if (!p_viewport->disable_3d && p_viewport->camera.is_valid()) { + if (!p_viewport->disable_3d && !p_viewport->disable_3d_by_usage && p_viewport->camera.is_valid()) { VSG::scene->render_camera(p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas); } @@ -133,7 +133,7 @@ void VisualServerViewport::_draw_viewport(Viewport *p_viewport) { cl->texture_cache = NULL; Transform2D scale; scale.scale(cl->rect_cache.size); - scale.elements[2] = cl->rect_cache.pos; + scale.elements[2] = cl->rect_cache.position; cl->light_shader_xform = (cl->xform_cache * scale).affine_inverse(); cl->light_shader_pos = cl->xform_cache[2]; if (cl->shadow_buffer.is_valid()) { @@ -272,8 +272,20 @@ void VisualServerViewport::draw_viewports() { continue; VSG::rasterizer->set_current_render_target(vp->render_target); + + VSG::scene_render->set_debug_draw_mode(vp->debug_draw); + VSG::storage->render_info_begin_capture(); + _draw_viewport(vp); + VSG::storage->render_info_end_capture(); + vp->render_info[VS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_OBJECTS_IN_FRAME); + vp->render_info[VS::VIEWPORT_RENDER_INFO_VERTICES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_VERTICES_IN_FRAME); + vp->render_info[VS::VIEWPORT_RENDER_INFO_MATERIAL_CHANGES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_MATERIAL_CHANGES_IN_FRAME); + vp->render_info[VS::VIEWPORT_RENDER_INFO_SHADER_CHANGES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_SHADER_CHANGES_IN_FRAME); + vp->render_info[VS::VIEWPORT_RENDER_INFO_SURFACE_CHANGES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_SURFACE_CHANGES_IN_FRAME); + vp->render_info[VS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_DRAW_CALLS_IN_FRAME); + if (vp->viewport_to_screen_rect != Rect2()) { //copy to screen if set as such VSG::rasterizer->set_current_render_target(RID()); @@ -283,6 +295,7 @@ void VisualServerViewport::draw_viewports() { if (vp->update_mode == VS::VIEWPORT_UPDATE_ONCE) { vp->update_mode = VS::VIEWPORT_UPDATE_DISABLED; } + VSG::scene_render->set_debug_draw_mode(VS::VIEWPORT_DEBUG_DRAW_DISABLED); } } @@ -409,7 +422,8 @@ void VisualServerViewport::viewport_set_disable_3d(RID p_viewport, bool p_disabl ERR_FAIL_COND(!viewport); viewport->disable_3d = p_disable; - VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, p_disable); + //VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, p_disable); + //this should be just for disabling rendering of 3D, to actually disable it, set usage } void VisualServerViewport::viewport_attach_camera(RID p_viewport, RID p_camera) { @@ -518,6 +532,63 @@ void VisualServerViewport::viewport_set_hdr(RID p_viewport, bool p_enabled) { VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_HDR, p_enabled); } +void VisualServerViewport::viewport_set_usage(RID p_viewport, VS::ViewportUsage p_usage) { + + Viewport *viewport = viewport_owner.getornull(p_viewport); + ERR_FAIL_COND(!viewport); + + switch (p_usage) { + case VS::VIEWPORT_USAGE_2D: { + + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, true); + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, true); + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, false); + + viewport->disable_3d_by_usage = true; + } break; + case VS::VIEWPORT_USAGE_2D_NO_SAMPLING: { + + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, true); + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, true); + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, true); + viewport->disable_3d_by_usage = true; + } break; + case VS::VIEWPORT_USAGE_3D: { + + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, false); + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, false); + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, false); + viewport->disable_3d_by_usage = false; + } break; + case VS::VIEWPORT_USAGE_3D_NO_EFFECTS: { + + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, false); + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, true); + VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, false); + viewport->disable_3d_by_usage = false; + } break; + } +} + +int VisualServerViewport::viewport_get_render_info(RID p_viewport, VS::ViewportRenderInfo p_info) { + + ERR_FAIL_INDEX_V(p_info, VS::VIEWPORT_RENDER_INFO_MAX, -1); + + Viewport *viewport = viewport_owner.getornull(p_viewport); + if (!viewport) + return 0; //there should be a lock here.. + + return viewport->render_info[p_info]; +} + +void VisualServerViewport::viewport_set_debug_draw(RID p_viewport, VS::ViewportDebugDraw p_draw) { + + Viewport *viewport = viewport_owner.getornull(p_viewport); + ERR_FAIL_COND(!viewport); + + viewport->debug_draw = p_draw; +} + bool VisualServerViewport::free(RID p_rid) { if (viewport_owner.owns(p_rid)) { diff --git a/servers/visual/visual_server_viewport.h b/servers/visual/visual_server_viewport.h index 118d11a111..4d46e39b77 100644 --- a/servers/visual/visual_server_viewport.h +++ b/servers/visual/visual_server_viewport.h @@ -59,10 +59,14 @@ public: bool hide_canvas; bool disable_environment; bool disable_3d; + bool disable_3d_by_usage; RID shadow_atlas; int shadow_atlas_size; + int render_info[VS::VIEWPORT_RENDER_INFO_MAX]; + VS::ViewportDebugDraw debug_draw; + VS::ViewportClearMode clear_mode; bool rendered_in_prev_frame; @@ -101,6 +105,11 @@ public: viewport_to_screen = 0; shadow_atlas_size = 0; disable_3d = false; + disable_3d_by_usage = false; + debug_draw = VS::VIEWPORT_DEBUG_DRAW_DISABLED; + for (int i = 0; i < VS::VIEWPORT_RENDER_INFO_MAX; i++) { + render_info[i] = 0; + } } }; @@ -164,6 +173,10 @@ public: void viewport_set_msaa(RID p_viewport, VS::ViewportMSAA p_msaa); void viewport_set_hdr(RID p_viewport, bool p_enabled); + void viewport_set_usage(RID p_viewport, VS::ViewportUsage p_usage); + + virtual int viewport_get_render_info(RID p_viewport, VS::ViewportRenderInfo p_info); + virtual void viewport_set_debug_draw(RID p_viewport, VS::ViewportDebugDraw p_draw); void draw_viewports(); diff --git a/servers/visual/visual_server_wrap_mt.cpp b/servers/visual/visual_server_wrap_mt.cpp new file mode 100644 index 0000000000..fd15633244 --- /dev/null +++ b/servers/visual/visual_server_wrap_mt.cpp @@ -0,0 +1,193 @@ +/*************************************************************************/ +/* visual_server_wrap_mt.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/*************************************************************************/ +#include "visual_server_wrap_mt.h" +#include "global_config.h" +#include "os/os.h" + +void VisualServerWrapMT::thread_exit() { + + exit = true; +} + +void VisualServerWrapMT::thread_draw() { + + draw_mutex->lock(); + + draw_pending--; + bool draw = (draw_pending == 0); // only draw when no more flushes are pending + + draw_mutex->unlock(); + + if (draw) { + + visual_server->draw(); + } +} + +void VisualServerWrapMT::thread_flush() { + + draw_mutex->lock(); + + draw_pending--; + + draw_mutex->unlock(); +} + +void VisualServerWrapMT::_thread_callback(void *_instance) { + + VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT *>(_instance); + + vsmt->thread_loop(); +} + +void VisualServerWrapMT::thread_loop() { + + server_thread = Thread::get_caller_ID(); + + OS::get_singleton()->make_rendering_thread(); + + visual_server->init(); + + exit = false; + draw_thread_up = true; + while (!exit) { + // flush commands one by one, until exit is requested + command_queue.wait_and_flush_one(); + } + + command_queue.flush_all(); // flush all + + visual_server->finish(); +} + +/* EVENT QUEUING */ + +void VisualServerWrapMT::sync() { + + if (create_thread) { + + /* TODO: sync with the thread */ + + /* + ERR_FAIL_COND(!draw_mutex); + draw_mutex->lock(); + draw_pending++; //cambiar por un saferefcount + draw_mutex->unlock(); + */ + //command_queue.push( this, &VisualServerWrapMT::thread_flush); + } else { + + command_queue.flush_all(); //flush all pending from other threads + } +} + +void VisualServerWrapMT::draw() { + + if (create_thread) { + + /* TODO: Make it draw + ERR_FAIL_COND(!draw_mutex); + draw_mutex->lock(); + draw_pending++; //cambiar por un saferefcount + draw_mutex->unlock(); + + command_queue.push( this, &VisualServerWrapMT::thread_draw); + */ + } else { + + visual_server->draw(); + } +} + +void VisualServerWrapMT::init() { + + if (create_thread) { + + draw_mutex = Mutex::create(); + print_line("CREATING RENDER THREAD"); + OS::get_singleton()->release_rendering_thread(); + if (create_thread) { + thread = Thread::create(_thread_callback, this); + print_line("STARTING RENDER THREAD"); + } + while (!draw_thread_up) { + OS::get_singleton()->delay_usec(1000); + } + print_line("DONE RENDER THREAD"); + } else { + + visual_server->init(); + } +} + +void VisualServerWrapMT::finish() { + + if (thread) { + + command_queue.push(this, &VisualServerWrapMT::thread_exit); + Thread::wait_to_finish(thread); + memdelete(thread); + + texture_free_cached_ids(); + //mesh_free_cached_ids(); + + thread = NULL; + } else { + visual_server->finish(); + } + + if (draw_mutex) + memdelete(draw_mutex); +} + +VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread) + : command_queue(p_create_thread) { + + visual_server = p_contained; + create_thread = p_create_thread; + thread = NULL; + draw_mutex = NULL; + draw_pending = 0; + draw_thread_up = false; + alloc_mutex = Mutex::create(); + pool_max_size = GLOBAL_DEF("memory/servers/thread_rid_prealloc_amount", 20); + + if (!p_create_thread) { + server_thread = Thread::get_caller_ID(); + } else { + server_thread = 0; + } +} + +VisualServerWrapMT::~VisualServerWrapMT() { + + memdelete(visual_server); + memdelete(alloc_mutex); + //finish(); +} diff --git a/servers/visual/visual_server_wrap_mt.h b/servers/visual/visual_server_wrap_mt.h new file mode 100644 index 0000000000..05d8fb3dcd --- /dev/null +++ b/servers/visual/visual_server_wrap_mt.h @@ -0,0 +1,584 @@ +/*************************************************************************/ +/* visual_server_wrap_mt.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/*************************************************************************/ +#ifndef VISUAL_SERVER_WRAP_MT_H +#define VISUAL_SERVER_WRAP_MT_H + +#include "command_queue_mt.h" +#include "os/thread.h" +#include "servers/visual_server.h" + +/** + @author Juan Linietsky <reduzio@gmail.com> +*/ +class VisualServerWrapMT : public VisualServer { + + // the real visual server + mutable VisualServer *visual_server; + + mutable CommandQueueMT command_queue; + + static void _thread_callback(void *_instance); + void thread_loop(); + + Thread::ID server_thread; + volatile bool exit; + Thread *thread; + volatile bool draw_thread_up; + bool create_thread; + + Mutex *draw_mutex; + int draw_pending; + void thread_draw(); + void thread_flush(); + + void thread_exit(); + + Mutex *alloc_mutex; + + int pool_max_size; + +//#define DEBUG_SYNC + +#ifdef DEBUG_SYNC +#define SYNC_DEBUG print_line("sync on: " + String(__FUNCTION__)); +#else +#define SYNC_DEBUG +#endif + +public: +#define ServerName VisualServer +#define ServerNameWrapMT VisualServerWrapMT +#define server_name visual_server +#include "servers/server_wrap_mt_common.h" + + /* EVENT QUEUING */ + FUNCRID(texture) + FUNC5(texture_allocate, RID, int, int, Image::Format, uint32_t) + FUNC3(texture_set_data, RID, const Ref<Image> &, CubeMapSide) + FUNC2RC(Ref<Image>, texture_get_data, RID, CubeMapSide) + FUNC2(texture_set_flags, RID, uint32_t) + FUNC1RC(uint32_t, texture_get_flags, RID) + FUNC1RC(Image::Format, texture_get_format, RID) + FUNC1RC(uint32_t, texture_get_texid, RID) + FUNC1RC(uint32_t, texture_get_width, RID) + FUNC1RC(uint32_t, texture_get_height, RID) + FUNC3(texture_set_size_override, RID, int, int) + + FUNC3(texture_set_detect_3d_callback, RID, TextureDetectCallback, void *) + FUNC3(texture_set_detect_srgb_callback, RID, TextureDetectCallback, void *) + + FUNC2(texture_set_path, RID, const String &) + FUNC1RC(String, texture_get_path, RID) + FUNC1(texture_set_shrink_all_x2_on_set_data, bool) + FUNC1(texture_debug_usage, List<TextureInfo> *) + + FUNC1(textures_keep_original, bool) + + /* SKY API */ + + FUNC0R(RID, sky_create) + FUNC3(sky_set_texture, RID, RID, int) + + /* SHADER API */ + + FUNC0R(RID, shader_create) + + FUNC2(shader_set_code, RID, const String &) + FUNC1RC(String, shader_get_code, RID) + + FUNC2C(shader_get_param_list, RID, List<PropertyInfo> *) + + FUNC3(shader_set_default_texture_param, RID, const StringName &, RID) + FUNC2RC(RID, shader_get_default_texture_param, RID, const StringName &) + + /* COMMON MATERIAL API */ + + FUNC0R(RID, material_create) + + FUNC2(material_set_shader, RID, RID) + FUNC1RC(RID, material_get_shader, RID) + + FUNC3(material_set_param, RID, const StringName &, const Variant &) + FUNC2RC(Variant, material_get_param, RID, const StringName &) + + FUNC2(material_set_line_width, RID, float) + + /* MESH API */ + + FUNC0R(RID, mesh_create) + + FUNC10(mesh_add_surface, RID, uint32_t, PrimitiveType, const PoolVector<uint8_t> &, int, const PoolVector<uint8_t> &, int, const Rect3 &, const Vector<PoolVector<uint8_t> > &, const Vector<Rect3> &) + + FUNC2(mesh_set_blend_shape_count, RID, int) + FUNC1RC(int, mesh_get_blend_shape_count, RID) + + FUNC2(mesh_set_blend_shape_mode, RID, BlendShapeMode) + FUNC1RC(BlendShapeMode, mesh_get_blend_shape_mode, RID) + + FUNC3(mesh_surface_set_material, RID, int, RID) + FUNC2RC(RID, mesh_surface_get_material, RID, int) + + FUNC2RC(int, mesh_surface_get_array_len, RID, int) + FUNC2RC(int, mesh_surface_get_array_index_len, RID, int) + + FUNC2RC(PoolVector<uint8_t>, mesh_surface_get_array, RID, int) + FUNC2RC(PoolVector<uint8_t>, mesh_surface_get_index_array, RID, int) + + FUNC2RC(uint32_t, mesh_surface_get_format, RID, int) + FUNC2RC(PrimitiveType, mesh_surface_get_primitive_type, RID, int) + + FUNC2RC(Rect3, mesh_surface_get_aabb, RID, int) + FUNC2RC(Vector<PoolVector<uint8_t> >, mesh_surface_get_blend_shapes, RID, int) + FUNC2RC(Vector<Rect3>, mesh_surface_get_skeleton_aabb, RID, int) + + FUNC2(mesh_remove_surface, RID, int) + FUNC1RC(int, mesh_get_surface_count, RID) + + FUNC2(mesh_set_custom_aabb, RID, const Rect3 &) + FUNC1RC(Rect3, mesh_get_custom_aabb, RID) + + FUNC1(mesh_clear, RID) + + /* MULTIMESH API */ + + FUNC0R(RID, multimesh_create) + + FUNC4(multimesh_allocate, RID, int, MultimeshTransformFormat, MultimeshColorFormat) + FUNC1RC(int, multimesh_get_instance_count, RID) + + FUNC2(multimesh_set_mesh, RID, RID) + FUNC3(multimesh_instance_set_transform, RID, int, const Transform &) + FUNC3(multimesh_instance_set_transform_2d, RID, int, const Transform2D &) + FUNC3(multimesh_instance_set_color, RID, int, const Color &) + + FUNC1RC(RID, multimesh_get_mesh, RID) + FUNC1RC(Rect3, multimesh_get_aabb, RID) + + FUNC2RC(Transform, multimesh_instance_get_transform, RID, int) + FUNC2RC(Transform2D, multimesh_instance_get_transform_2d, RID, int) + FUNC2RC(Color, multimesh_instance_get_color, RID, int) + + FUNC2(multimesh_set_visible_instances, RID, int) + FUNC1RC(int, multimesh_get_visible_instances, RID) + + /* IMMEDIATE API */ + + FUNC0R(RID, immediate_create) + FUNC3(immediate_begin, RID, PrimitiveType, RID) + FUNC2(immediate_vertex, RID, const Vector3 &) + FUNC2(immediate_normal, RID, const Vector3 &) + FUNC2(immediate_tangent, RID, const Plane &) + FUNC2(immediate_color, RID, const Color &) + FUNC2(immediate_uv, RID, const Vector2 &) + FUNC2(immediate_uv2, RID, const Vector2 &) + FUNC1(immediate_end, RID) + FUNC1(immediate_clear, RID) + FUNC2(immediate_set_material, RID, RID) + FUNC1RC(RID, immediate_get_material, RID) + + /* SKELETON API */ + + FUNC0R(RID, skeleton_create) + FUNC3(skeleton_allocate, RID, int, bool) + FUNC1RC(int, skeleton_get_bone_count, RID) + FUNC3(skeleton_bone_set_transform, RID, int, const Transform &) + FUNC2RC(Transform, skeleton_bone_get_transform, RID, int) + FUNC3(skeleton_bone_set_transform_2d, RID, int, const Transform2D &) + FUNC2RC(Transform2D, skeleton_bone_get_transform_2d, RID, int) + + /* Light API */ + + FUNC1R(RID, light_create, LightType) + + FUNC2(light_set_color, RID, const Color &) + FUNC3(light_set_param, RID, LightParam, float) + FUNC2(light_set_shadow, RID, bool) + FUNC2(light_set_shadow_color, RID, const Color &) + FUNC2(light_set_projector, RID, RID) + FUNC2(light_set_negative, RID, bool) + FUNC2(light_set_cull_mask, RID, uint32_t) + + FUNC2(light_omni_set_shadow_mode, RID, LightOmniShadowMode) + FUNC2(light_omni_set_shadow_detail, RID, LightOmniShadowDetail) + + FUNC2(light_directional_set_shadow_mode, RID, LightDirectionalShadowMode) + FUNC2(light_directional_set_blend_splits, RID, bool) + + /* PROBE API */ + + FUNC0R(RID, reflection_probe_create) + + FUNC2(reflection_probe_set_update_mode, RID, ReflectionProbeUpdateMode) + FUNC2(reflection_probe_set_intensity, RID, float) + FUNC2(reflection_probe_set_interior_ambient, RID, const Color &) + FUNC2(reflection_probe_set_interior_ambient_energy, RID, float) + FUNC2(reflection_probe_set_interior_ambient_probe_contribution, RID, float) + FUNC2(reflection_probe_set_max_distance, RID, float) + FUNC2(reflection_probe_set_extents, RID, const Vector3 &) + FUNC2(reflection_probe_set_origin_offset, RID, const Vector3 &) + FUNC2(reflection_probe_set_as_interior, RID, bool) + FUNC2(reflection_probe_set_enable_box_projection, RID, bool) + FUNC2(reflection_probe_set_enable_shadows, RID, bool) + FUNC2(reflection_probe_set_cull_mask, RID, uint32_t) + + /* ROOM API */ + + FUNC0R(RID, room_create) + FUNC4(room_add_bounds, RID, const PoolVector<Vector2> &, float, const Transform &) + FUNC1(room_clear_bounds, RID) + + /* PORTAL API */ + + // portals are only (x/y) points, forming a convex shape, which its clockwise + // order points outside. (z is 0); + + FUNC0R(RID, portal_create) + FUNC2(portal_set_shape, RID, const Vector<Point2> &) + FUNC2(portal_set_enabled, RID, bool) + FUNC2(portal_set_disable_distance, RID, float) + FUNC2(portal_set_disabled_color, RID, const Color &) + + /* BAKED LIGHT API */ + + FUNC0R(RID, gi_probe_create) + + FUNC2(gi_probe_set_bounds, RID, const Rect3 &) + FUNC1RC(Rect3, gi_probe_get_bounds, RID) + + FUNC2(gi_probe_set_cell_size, RID, float) + FUNC1RC(float, gi_probe_get_cell_size, RID) + + FUNC2(gi_probe_set_to_cell_xform, RID, const Transform &) + FUNC1RC(Transform, gi_probe_get_to_cell_xform, RID) + + FUNC2(gi_probe_set_dynamic_range, RID, int) + FUNC1RC(int, gi_probe_get_dynamic_range, RID) + + FUNC2(gi_probe_set_energy, RID, float) + FUNC1RC(float, gi_probe_get_energy, RID) + + FUNC2(gi_probe_set_bias, RID, float) + FUNC1RC(float, gi_probe_get_bias, RID) + + FUNC2(gi_probe_set_propagation, RID, float) + FUNC1RC(float, gi_probe_get_propagation, RID) + + FUNC2(gi_probe_set_interior, RID, bool) + FUNC1RC(bool, gi_probe_is_interior, RID) + + FUNC2(gi_probe_set_compress, RID, bool) + FUNC1RC(bool, gi_probe_is_compressed, RID) + + FUNC2(gi_probe_set_dynamic_data, RID, const PoolVector<int> &) + FUNC1RC(PoolVector<int>, gi_probe_get_dynamic_data, RID) + + /* PARTICLES */ + + FUNC0R(RID, particles_create) + + FUNC2(particles_set_emitting, RID, bool) + FUNC2(particles_set_amount, RID, int) + FUNC2(particles_set_lifetime, RID, float) + FUNC2(particles_set_pre_process_time, RID, float) + FUNC2(particles_set_explosiveness_ratio, RID, float) + FUNC2(particles_set_randomness_ratio, RID, float) + FUNC2(particles_set_custom_aabb, RID, const Rect3 &) + FUNC2(particles_set_speed_scale, RID, float) + FUNC2(particles_set_use_local_coordinates, RID, bool) + FUNC2(particles_set_process_material, RID, RID) + FUNC2(particles_set_fixed_fps, RID, int) + FUNC2(particles_set_fractional_delta, RID, bool) + + FUNC2(particles_set_draw_order, RID, VS::ParticlesDrawOrder) + + FUNC2(particles_set_draw_passes, RID, int) + FUNC3(particles_set_draw_pass_mesh, RID, int, RID) + + FUNC1R(Rect3, particles_get_current_aabb, RID) + + /* CAMERA API */ + + FUNC0R(RID, camera_create) + FUNC4(camera_set_perspective, RID, float, float, float) + FUNC4(camera_set_orthogonal, RID, float, float, float) + FUNC2(camera_set_transform, RID, const Transform &) + FUNC2(camera_set_cull_mask, RID, uint32_t) + FUNC2(camera_set_environment, RID, RID) + FUNC2(camera_set_use_vertical_aspect, RID, bool) + + /* VIEWPORT TARGET API */ + + FUNC0R(RID, viewport_create) + + FUNC3(viewport_set_size, RID, int, int) + + FUNC2(viewport_set_active, RID, bool) + FUNC2(viewport_set_parent_viewport, RID, RID) + + FUNC2(viewport_set_clear_mode, RID, ViewportClearMode) + + FUNC3(viewport_attach_to_screen, RID, const Rect2 &, int) + FUNC1(viewport_detach, RID) + + FUNC2(viewport_set_update_mode, RID, ViewportUpdateMode) + FUNC2(viewport_set_vflip, RID, bool) + + FUNC1RC(RID, viewport_get_texture, RID) + + FUNC2(viewport_set_hide_scenario, RID, bool) + FUNC2(viewport_set_hide_canvas, RID, bool) + FUNC2(viewport_set_disable_environment, RID, bool) + FUNC2(viewport_set_disable_3d, RID, bool) + + FUNC2(viewport_attach_camera, RID, RID) + FUNC2(viewport_set_scenario, RID, RID) + FUNC2(viewport_attach_canvas, RID, RID) + + FUNC2(viewport_remove_canvas, RID, RID) + FUNC3(viewport_set_canvas_transform, RID, RID, const Transform2D &) + FUNC2(viewport_set_transparent_background, RID, bool) + + FUNC2(viewport_set_global_canvas_transform, RID, const Transform2D &) + FUNC3(viewport_set_canvas_layer, RID, RID, int) + FUNC2(viewport_set_shadow_atlas_size, RID, int) + FUNC3(viewport_set_shadow_atlas_quadrant_subdivision, RID, int, int) + FUNC2(viewport_set_msaa, RID, ViewportMSAA) + FUNC2(viewport_set_hdr, RID, bool) + FUNC2(viewport_set_usage, RID, ViewportUsage) + + //this passes directly to avoid stalling, but it's pretty dangerous, so dont call after freeing a viewport + virtual int viewport_get_render_info(RID p_viewport, ViewportRenderInfo p_info) { + return visual_server->viewport_get_render_info(p_viewport, p_info); + } + + FUNC2(viewport_set_debug_draw, RID, ViewportDebugDraw) + + /* ENVIRONMENT API */ + + FUNC0R(RID, environment_create) + + FUNC2(environment_set_background, RID, EnvironmentBG) + FUNC2(environment_set_sky, RID, RID) + FUNC2(environment_set_sky_scale, RID, float) + FUNC2(environment_set_bg_color, RID, const Color &) + FUNC2(environment_set_bg_energy, RID, float) + FUNC2(environment_set_canvas_max_layer, RID, int) + FUNC4(environment_set_ambient_light, RID, const Color &, float, float) + FUNC8(environment_set_ssr, RID, bool, int, float, float, float, bool, bool) + FUNC10(environment_set_ssao, RID, bool, float, float, float, float, float, float, const Color &, bool) + + FUNC6(environment_set_dof_blur_near, RID, bool, float, float, float, EnvironmentDOFBlurQuality) + FUNC6(environment_set_dof_blur_far, RID, bool, float, float, float, EnvironmentDOFBlurQuality) + FUNC10(environment_set_glow, RID, bool, int, float, float, float, EnvironmentGlowBlendMode, float, float, bool) + + FUNC9(environment_set_tonemap, RID, EnvironmentToneMapper, float, float, bool, float, float, float, float) + + FUNC6(environment_set_adjustment, RID, bool, float, float, float, RID) + + FUNC5(environment_set_fog, RID, bool, const Color &, const Color &, float) + FUNC6(environment_set_fog_depth, RID, bool, float, float, bool, float) + FUNC5(environment_set_fog_height, RID, bool, float, float, float) + + FUNC0R(RID, scenario_create) + + FUNC2(scenario_set_debug, RID, ScenarioDebugMode) + FUNC2(scenario_set_environment, RID, RID) + FUNC3(scenario_set_reflection_atlas_size, RID, int, int) + FUNC2(scenario_set_fallback_environment, RID, RID) + + /* INSTANCING API */ + // from can be mesh, light, area and portal so far. + FUNC0R(RID, instance_create) + + FUNC2(instance_set_base, RID, RID) // from can be mesh, light, poly, area and portal so far. + FUNC2(instance_set_scenario, RID, RID) // from can be mesh, light, poly, area and portal so far. + FUNC2(instance_set_layer_mask, RID, uint32_t) + FUNC2(instance_set_transform, RID, const Transform &) + FUNC2(instance_attach_object_instance_ID, RID, ObjectID) + FUNC3(instance_set_blend_shape_weight, RID, int, float) + FUNC3(instance_set_surface_material, RID, int, RID) + FUNC2(instance_set_visible, RID, bool) + + FUNC2(instance_attach_skeleton, RID, RID) + FUNC2(instance_set_exterior, RID, bool) + FUNC2(instance_set_room, RID, RID) + + FUNC2(instance_set_extra_visibility_margin, RID, real_t) + + // don't use these in a game! + FUNC2RC(Vector<ObjectID>, instances_cull_aabb, const Rect3 &, RID) + FUNC3RC(Vector<ObjectID>, instances_cull_ray, const Vector3 &, const Vector3 &, RID) + FUNC2RC(Vector<ObjectID>, instances_cull_convex, const Vector<Plane> &, RID) + + FUNC3(instance_geometry_set_flag, RID, InstanceFlags, bool) + FUNC2(instance_geometry_set_cast_shadows_setting, RID, ShadowCastingSetting) + FUNC2(instance_geometry_set_material_override, RID, RID) + + FUNC5(instance_geometry_set_draw_range, RID, float, float, float, float) + FUNC2(instance_geometry_set_as_instance_lod, RID, RID) + + /* CANVAS (2D) */ + + FUNC0R(RID, canvas_create) + FUNC3(canvas_set_item_mirroring, RID, RID, const Point2 &) + FUNC2(canvas_set_modulate, RID, const Color &) + + FUNC0R(RID, canvas_item_create) + FUNC2(canvas_item_set_parent, RID, RID) + + FUNC2(canvas_item_set_visible, RID, bool) + FUNC2(canvas_item_set_light_mask, RID, int) + + FUNC2(canvas_item_set_transform, RID, const Transform2D &) + FUNC2(canvas_item_set_clip, RID, bool) + FUNC2(canvas_item_set_distance_field_mode, RID, bool) + FUNC3(canvas_item_set_custom_rect, RID, bool, const Rect2 &) + FUNC2(canvas_item_set_modulate, RID, const Color &) + FUNC2(canvas_item_set_self_modulate, RID, const Color &) + + FUNC2(canvas_item_set_draw_behind_parent, RID, bool) + + FUNC6(canvas_item_add_line, RID, const Point2 &, const Point2 &, const Color &, float, bool) + FUNC3(canvas_item_add_rect, RID, const Rect2 &, const Color &) + FUNC4(canvas_item_add_circle, RID, const Point2 &, float, const Color &) + FUNC6(canvas_item_add_texture_rect, RID, const Rect2 &, RID, bool, const Color &, bool) + FUNC6(canvas_item_add_texture_rect_region, RID, const Rect2 &, RID, const Rect2 &, const Color &, bool) + FUNC10(canvas_item_add_nine_patch, RID, const Rect2 &, const Rect2 &, RID, const Vector2 &, const Vector2 &, NinePatchAxisMode, NinePatchAxisMode, bool, const Color &) + FUNC6(canvas_item_add_primitive, RID, const Vector<Point2> &, const Vector<Color> &, const Vector<Point2> &, RID, float) + FUNC5(canvas_item_add_polygon, RID, const Vector<Point2> &, const Vector<Color> &, const Vector<Point2> &, RID) + FUNC7(canvas_item_add_triangle_array, RID, const Vector<int> &, const Vector<Point2> &, const Vector<Color> &, const Vector<Point2> &, RID, int) + FUNC3(canvas_item_add_mesh, RID, const RID &, RID) + FUNC3(canvas_item_add_multimesh, RID, RID, RID) + FUNC2(canvas_item_add_set_transform, RID, const Transform2D &) + FUNC2(canvas_item_add_clip_ignore, RID, bool) + FUNC2(canvas_item_set_sort_children_by_y, RID, bool) + FUNC2(canvas_item_set_z, RID, int) + FUNC2(canvas_item_set_z_as_relative_to_parent, RID, bool) + FUNC3(canvas_item_set_copy_to_backbuffer, RID, bool, const Rect2 &) + + FUNC1(canvas_item_clear, RID) + FUNC2(canvas_item_set_draw_index, RID, int) + + FUNC2(canvas_item_set_material, RID, RID) + + FUNC2(canvas_item_set_use_parent_material, RID, bool) + + FUNC0R(RID, canvas_light_create) + FUNC2(canvas_light_attach_to_canvas, RID, RID) + FUNC2(canvas_light_set_enabled, RID, bool) + FUNC2(canvas_light_set_scale, RID, float) + FUNC2(canvas_light_set_transform, RID, const Transform2D &) + FUNC2(canvas_light_set_texture, RID, RID) + FUNC2(canvas_light_set_texture_offset, RID, const Vector2 &) + FUNC2(canvas_light_set_color, RID, const Color &) + FUNC2(canvas_light_set_height, RID, float) + FUNC2(canvas_light_set_energy, RID, float) + FUNC3(canvas_light_set_z_range, RID, int, int) + FUNC3(canvas_light_set_layer_range, RID, int, int) + FUNC2(canvas_light_set_item_cull_mask, RID, int) + FUNC2(canvas_light_set_item_shadow_cull_mask, RID, int) + + FUNC2(canvas_light_set_mode, RID, CanvasLightMode) + + FUNC2(canvas_light_set_shadow_enabled, RID, bool) + FUNC2(canvas_light_set_shadow_buffer_size, RID, int) + FUNC2(canvas_light_set_shadow_gradient_length, RID, float) + FUNC2(canvas_light_set_shadow_filter, RID, CanvasLightShadowFilter) + FUNC2(canvas_light_set_shadow_color, RID, const Color &) + FUNC2(canvas_light_set_shadow_smooth, RID, float) + + FUNC0R(RID, canvas_light_occluder_create) + FUNC2(canvas_light_occluder_attach_to_canvas, RID, RID) + FUNC2(canvas_light_occluder_set_enabled, RID, bool) + FUNC2(canvas_light_occluder_set_polygon, RID, RID) + FUNC2(canvas_light_occluder_set_transform, RID, const Transform2D &) + FUNC2(canvas_light_occluder_set_light_mask, RID, int) + + FUNC0R(RID, canvas_occluder_polygon_create) + FUNC3(canvas_occluder_polygon_set_shape, RID, const PoolVector<Vector2> &, bool) + FUNC2(canvas_occluder_polygon_set_shape_as_lines, RID, const PoolVector<Vector2> &) + + FUNC2(canvas_occluder_polygon_set_cull_mode, RID, CanvasOccluderPolygonCullMode) + + /* CURSOR */ + FUNC2(cursor_set_rotation, float, int) // radians + FUNC4(cursor_set_texture, RID, const Point2 &, int, const Rect2 &) + FUNC2(cursor_set_visible, bool, int) + FUNC2(cursor_set_pos, const Point2 &, int) + + /* BLACK BARS */ + + FUNC4(black_bars_set_margins, int, int, int, int) + FUNC4(black_bars_set_images, RID, RID, RID, RID) + + /* FREE */ + + FUNC1(free, RID) + + /* EVENT QUEUING */ + + FUNC3(request_frame_drawn_callback, Object *, const StringName &, const Variant &) + + virtual void init(); + virtual void finish(); + virtual void draw(); + virtual void sync(); + FUNC0RC(bool, has_changed) + + /* RENDER INFO */ + + //this passes directly to avoid stalling + virtual int get_render_info(RenderInfo p_info) { + return visual_server->get_render_info(p_info); + } + + FUNC3(set_boot_image, const Ref<Image> &, const Color &, bool) + FUNC1(set_default_clear_color, const Color &) + + FUNC0R(RID, get_test_cube) + + FUNC1(set_debug_generate_wireframes, bool) + + virtual bool has_feature(Features p_feature) const { return visual_server->has_feature(p_feature); } + virtual bool has_os_feature(const String &p_feature) const { return visual_server->has_os_feature(p_feature); } + + VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread); + ~VisualServerWrapMT(); + +#undef ServerName +#undef ServerNameWrapMT +#undef server_name +}; + +#ifdef DEBUG_SYNC +#undef DEBUG_SYNC +#endif +#undef SYNC_DEBUG + +#endif diff --git a/servers/visual_server.cpp b/servers/visual_server.cpp index 5df1ca456b..b2dda455e9 100644 --- a/servers/visual_server.cpp +++ b/servers/visual_server.cpp @@ -338,8 +338,6 @@ Error VisualServer::_surface_set_data(Array p_arrays, uint32_t p_format, uint32_ PoolVector<uint8_t>::Write iw; if (r_index_array.size()) { - print_line("elements: " + itos(r_index_array.size())); - iw = r_index_array.write(); } @@ -399,7 +397,7 @@ Error VisualServer::_surface_set_data(Array p_arrays, uint32_t p_format, uint32_ } } - r_aabb = Rect3(Vector3(aabb.pos.x, aabb.pos.y, 0), Vector3(aabb.size.x, aabb.size.y, 0)); + r_aabb = Rect3(Vector3(aabb.position.x, aabb.position.y, 0), Vector3(aabb.size.x, aabb.size.y, 0)); } else { PoolVector<Vector3> array = p_arrays[ai]; @@ -770,7 +768,7 @@ Error VisualServer::_surface_set_data(Array p_arrays, uint32_t p_format, uint32_ if (bptr->size.x < 0) { //first bptr[idx] = Rect3(); - bptr[idx].pos = v; + bptr[idx].position = v; any_valid = true; } else { bptr[idx].expand_to(v); diff --git a/servers/visual_server.h b/servers/visual_server.h index aa98d47455..4f2f9d30fa 100644 --- a/servers/visual_server.h +++ b/servers/visual_server.h @@ -579,7 +579,38 @@ public: }; virtual void viewport_set_msaa(RID p_viewport, ViewportMSAA p_msaa) = 0; + + enum ViewportUsage { + VIEWPORT_USAGE_2D, + VIEWPORT_USAGE_2D_NO_SAMPLING, + VIEWPORT_USAGE_3D, + VIEWPORT_USAGE_3D_NO_EFFECTS, + }; + virtual void viewport_set_hdr(RID p_viewport, bool p_enabled) = 0; + virtual void viewport_set_usage(RID p_viewport, ViewportUsage p_usage) = 0; + + enum ViewportRenderInfo { + + VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME, + VIEWPORT_RENDER_INFO_VERTICES_IN_FRAME, + VIEWPORT_RENDER_INFO_MATERIAL_CHANGES_IN_FRAME, + VIEWPORT_RENDER_INFO_SHADER_CHANGES_IN_FRAME, + VIEWPORT_RENDER_INFO_SURFACE_CHANGES_IN_FRAME, + VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME, + VIEWPORT_RENDER_INFO_MAX + }; + + virtual int viewport_get_render_info(RID p_viewport, ViewportRenderInfo p_info) = 0; + + enum ViewportDebugDraw { + VIEWPORT_DEBUG_DRAW_DISABLED, + VIEWPORT_DEBUG_DRAW_UNSHADED, + VIEWPORT_DEBUG_DRAW_OVERDRAW, + VIEWPORT_DEBUG_DRAW_WIREFRAME, + }; + + virtual void viewport_set_debug_draw(RID p_viewport, ViewportDebugDraw p_draw) = 0; /* ENVIRONMENT API */ @@ -623,7 +654,6 @@ public: GLOW_BLEND_MODE_REPLACE, }; virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_treshold, EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_treshold, float p_hdr_bleed_scale, bool p_bicubic_upscale) = 0; - virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) = 0; enum EnvironmentToneMapper { ENV_TONE_MAPPER_LINEAR, @@ -638,6 +668,10 @@ public: virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_accel, float p_fade, float p_depth_tolerance, bool p_smooth, bool p_roughness) = 0; virtual void environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_radius2, float p_intensity2, float p_bias, float p_light_affect, const Color &p_color, bool p_blur) = 0; + virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) = 0; + virtual void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_curve, bool p_transmit, float p_transmit_curve) = 0; + virtual void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve) = 0; + /* SCENARIO API */ virtual RID scenario_create() = 0; @@ -810,6 +844,7 @@ public: virtual void canvas_light_set_shadow_gradient_length(RID p_light, float p_length) = 0; virtual void canvas_light_set_shadow_filter(RID p_light, CanvasLightShadowFilter p_filter) = 0; virtual void canvas_light_set_shadow_color(RID p_light, const Color &p_color) = 0; + virtual void canvas_light_set_shadow_smooth(RID p_light, float p_smooth) = 0; virtual RID canvas_light_occluder_create() = 0; virtual void canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas) = 0; @@ -844,6 +879,8 @@ public: virtual void free(RID p_rid) = 0; ///< free RIDs associated with the visual server + virtual void request_frame_drawn_callback(Object *p_where, const StringName &p_method, const Variant &p_userdata) = 0; + /* EVENT QUEUING */ virtual void draw() = 0; @@ -898,6 +935,8 @@ public: virtual bool has_os_feature(const String &p_feature) const = 0; + virtual void set_debug_generate_wireframes(bool p_generate) = 0; + VisualServer(); virtual ~VisualServer(); }; diff --git a/thirdparty/README.md b/thirdparty/README.md index 2830f6615a..c3dd1dff6e 100644 --- a/thirdparty/README.md +++ b/thirdparty/README.md @@ -367,3 +367,14 @@ https://github.com/godotengine/godot/commit/37f5e1dcd94611dd5b670f013abf0323e8b4 Files extracted from upstream source: - all .c and .h files + +## zstd + +- Upstream: https://github.com/facebook/zstd +- Version: 1.2.0 +- License: BSD-3-Clause + +Files extracted from upstream source: + +- all .c and .h under lib/ +- README.md, LICENSE, PATENTS diff --git a/thirdparty/zstd/LICENSE b/thirdparty/zstd/LICENSE new file mode 100644 index 0000000000..a793a80289 --- /dev/null +++ b/thirdparty/zstd/LICENSE @@ -0,0 +1,30 @@ +BSD License + +For Zstandard software + +Copyright (c) 2016-present, Facebook, Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name Facebook nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/thirdparty/zstd/PATENTS b/thirdparty/zstd/PATENTS new file mode 100644 index 0000000000..15b4a2ea5c --- /dev/null +++ b/thirdparty/zstd/PATENTS @@ -0,0 +1,33 @@ +Additional Grant of Patent Rights Version 2 + +"Software" means the Zstandard software distributed by Facebook, Inc. + +Facebook, Inc. ("Facebook") hereby grants to each recipient of the Software +("you") a perpetual, worldwide, royalty-free, non-exclusive, irrevocable +(subject to the termination provision below) license under any Necessary +Claims, to make, have made, use, sell, offer to sell, import, and otherwise +transfer the Software. For avoidance of doubt, no license is granted under +Facebook’s rights in any patent claims that are infringed by (i) modifications +to the Software made by you or any third party or (ii) the Software in +combination with any software or other technology. + +The license granted hereunder will terminate, automatically and without notice, +if you (or any of your subsidiaries, corporate affiliates or agents) initiate +directly or indirectly, or take a direct financial interest in, any Patent +Assertion: (i) against Facebook or any of its subsidiaries or corporate +affiliates, (ii) against any party if such Patent Assertion arises in whole or +in part from any software, technology, product or service of Facebook or any of +its subsidiaries or corporate affiliates, or (iii) against any party relating +to the Software. Notwithstanding the foregoing, if Facebook or any of its +subsidiaries or corporate affiliates files a lawsuit alleging patent +infringement against you in the first instance, and you respond by filing a +patent infringement counterclaim in that lawsuit against that party that is +unrelated to the Software, the license granted hereunder will not terminate +under section (i) of this paragraph due to such counterclaim. + +A "Necessary Claim" is a claim of a patent owned by Facebook that is +necessarily infringed by the Software standing alone. + +A "Patent Assertion" is any lawsuit or other action alleging direct, indirect, +or contributory infringement or inducement to infringe any patent, including a +cross-claim or counterclaim. diff --git a/thirdparty/zstd/README.md b/thirdparty/zstd/README.md new file mode 100644 index 0000000000..7caee5fd3f --- /dev/null +++ b/thirdparty/zstd/README.md @@ -0,0 +1,146 @@ + __Zstandard__, or `zstd` as short version, is a fast lossless compression algorithm, + targeting real-time compression scenarios at zlib-level and better compression ratios. + +It is provided as an open-source BSD-licensed **C** library, +and a command line utility producing and decoding `.zst` and `.gz` files. +For other programming languages, +you can consult a list of known ports on [Zstandard homepage](http://www.zstd.net/#other-languages). + +|Branch |Status | +|------------|---------| +|master | [](https://travis-ci.org/facebook/zstd) | +|dev | [](https://travis-ci.org/facebook/zstd) | + +As a reference, several fast compression algorithms were tested and compared +on a server running Linux Debian (`Linux version 4.8.0-1-amd64`), +with a Core i7-6700K CPU @ 4.0GHz, +using [lzbench], an open-source in-memory benchmark by @inikep +compiled with GCC 6.3.0, +on the [Silesia compression corpus]. + +[lzbench]: https://github.com/inikep/lzbench +[Silesia compression corpus]: http://sun.aei.polsl.pl/~sdeor/index.php?page=silesia + +| Compressor name | Ratio | Compression| Decompress.| +| --------------- | ------| -----------| ---------- | +| **zstd 1.1.3 -1** | 2.877 | 430 MB/s | 1110 MB/s | +| zlib 1.2.8 -1 | 2.743 | 110 MB/s | 400 MB/s | +| brotli 0.5.2 -0 | 2.708 | 400 MB/s | 430 MB/s | +| quicklz 1.5.0 -1 | 2.238 | 550 MB/s | 710 MB/s | +| lzo1x 2.09 -1 | 2.108 | 650 MB/s | 830 MB/s | +| lz4 1.7.5 | 2.101 | 720 MB/s | 3600 MB/s | +| snappy 1.1.3 | 2.091 | 500 MB/s | 1650 MB/s | +| lzf 3.6 -1 | 2.077 | 400 MB/s | 860 MB/s | + +[zlib]:http://www.zlib.net/ +[LZ4]: http://www.lz4.org/ + +Zstd can also offer stronger compression ratios at the cost of compression speed. +Speed vs Compression trade-off is configurable by small increments. Decompression speed is preserved and remains roughly the same at all settings, a property shared by most LZ compression algorithms, such as [zlib] or lzma. + +The following tests were run +on a server running Linux Debian (`Linux version 4.8.0-1-amd64`) +with a Core i7-6700K CPU @ 4.0GHz, +using [lzbench], an open-source in-memory benchmark by @inikep +compiled with GCC 6.3.0, +on the [Silesia compression corpus]. + +Compression Speed vs Ratio | Decompression Speed +---------------------------|-------------------- + |  + +Several algorithms can produce higher compression ratios, but at slower speeds, falling outside of the graph. +For a larger picture including very slow modes, [click on this link](doc/images/DCspeed5.png) . + + +### The case for Small Data compression + +Previous charts provide results applicable to typical file and stream scenarios (several MB). Small data comes with different perspectives. + +The smaller the amount of data to compress, the more difficult it is to compress. This problem is common to all compression algorithms, and reason is, compression algorithms learn from past data how to compress future data. But at the beginning of a new data set, there is no "past" to build upon. + +To solve this situation, Zstd offers a __training mode__, which can be used to tune the algorithm for a selected type of data. +Training Zstandard is achieved by provide it with a few samples (one file per sample). The result of this training is stored in a file called "dictionary", which must be loaded before compression and decompression. +Using this dictionary, the compression ratio achievable on small data improves dramatically. + +The following example uses the `github-users` [sample set](https://github.com/facebook/zstd/releases/tag/v1.1.3), created from [github public API](https://developer.github.com/v3/users/#get-all-users). +It consists of roughly 10K records weighting about 1KB each. + +Compression Ratio | Compression Speed | Decompression Speed +------------------|-------------------|-------------------- + |  |  + + +These compression gains are achieved while simultaneously providing _faster_ compression and decompression speeds. + +Training works if there is some correlation in a family of small data samples. The more data-specific a dictionary is, the more efficient it is (there is no _universal dictionary_). +Hence, deploying one dictionary per type of data will provide the greatest benefits. +Dictionary gains are mostly effective in the first few KB. Then, the compression algorithm will gradually use previously decoded content to better compress the rest of the file. + +#### Dictionary compression How To : + +1) Create the dictionary + +`zstd --train FullPathToTrainingSet/* -o dictionaryName` + +2) Compress with dictionary + +`zstd -D dictionaryName FILE` + +3) Decompress with dictionary + +`zstd -D dictionaryName --decompress FILE.zst` + + +### Build + +Once you have the repository cloned, there are multiple ways provided to build Zstandard. + +#### Makefile + +If your system is compatible with a standard `make` (or `gmake`) binary generator, +you can simply run it at the root directory. +It will generate `zstd` within root directory. + +Other available options include : +- `make install` : create and install zstd binary, library and man page +- `make test` : create and run `zstd` and test tools on local platform + +#### cmake + +A `cmake` project generator is provided within `build/cmake`. +It can generate Makefiles or other build scripts +to create `zstd` binary, and `libzstd` dynamic and static libraries. + +#### Meson + +A Meson project is provided within `contrib/meson`. + +#### Visual Studio (Windows) + +Going into `build` directory, you will find additional possibilities : +- Projects for Visual Studio 2005, 2008 and 2010 + + VS2010 project is compatible with VS2012, VS2013 and VS2015 +- Automated build scripts for Visual compiler by @KrzysFR , in `build/VS_scripts`, + which will build `zstd` cli and `libzstd` library without any need to open Visual Studio solution. + + +### Status + +Zstandard is currently deployed within Facebook. It is used daily to compress and decompress very large amounts of data in multiple formats and use cases. +Zstandard is considered safe for production environments. + +### License + +Zstandard is [BSD-licensed](LICENSE). We also provide an [additional patent grant](PATENTS). + +### Contributing + +The "dev" branch is the one where all contributions will be merged before reaching "master". +If you plan to propose a patch, please commit into the "dev" branch or its own feature branch. +Direct commit to "master" are not permitted. +For more information, please read [CONTRIBUTING](CONTRIBUTING.md). + +### Miscellaneous + +Zstd entropy stage is provided by [Huff0 and FSE, from Finite State Entropy library](https://github.com/Cyan4973/FiniteStateEntropy). diff --git a/thirdparty/zstd/common/bitstream.h b/thirdparty/zstd/common/bitstream.h new file mode 100644 index 0000000000..ca42850df3 --- /dev/null +++ b/thirdparty/zstd/common/bitstream.h @@ -0,0 +1,446 @@ +/* ****************************************************************** + bitstream + Part of FSE library + header file (to include) + Copyright (C) 2013-2017, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - Source repository : https://github.com/Cyan4973/FiniteStateEntropy +****************************************************************** */ +#ifndef BITSTREAM_H_MODULE +#define BITSTREAM_H_MODULE + +#if defined (__cplusplus) +extern "C" { +#endif + + +/* +* This API consists of small unitary functions, which must be inlined for best performance. +* Since link-time-optimization is not available for all compilers, +* these functions are defined into a .h to be included. +*/ + +/*-**************************************** +* Dependencies +******************************************/ +#include "mem.h" /* unaligned access routines */ +#include "error_private.h" /* error codes and messages */ + + +/*-************************************* +* Debug +***************************************/ +#if defined(BIT_DEBUG) && (BIT_DEBUG>=1) +# include <assert.h> +#else +# define assert(condition) ((void)0) +#endif + + +/*========================================= +* Target specific +=========================================*/ +#if defined(__BMI__) && defined(__GNUC__) +# include <immintrin.h> /* support for bextr (experimental) */ +#endif + +#define STREAM_ACCUMULATOR_MIN_32 25 +#define STREAM_ACCUMULATOR_MIN_64 57 +#define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64)) + +/*-****************************************** +* bitStream encoding API (write forward) +********************************************/ +/* bitStream can mix input from multiple sources. +* A critical property of these streams is that they encode and decode in **reverse** direction. +* So the first bit sequence you add will be the last to be read, like a LIFO stack. +*/ +typedef struct +{ + size_t bitContainer; + unsigned bitPos; + char* startPtr; + char* ptr; + char* endPtr; +} BIT_CStream_t; + +MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity); +MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits); +MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC); +MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC); + +/* Start with initCStream, providing the size of buffer to write into. +* bitStream will never write outside of this buffer. +* `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code. +* +* bits are first added to a local register. +* Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems. +* Writing data into memory is an explicit operation, performed by the flushBits function. +* Hence keep track how many bits are potentially stored into local register to avoid register overflow. +* After a flushBits, a maximum of 7 bits might still be stored into local register. +* +* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers. +* +* Last operation is to close the bitStream. +* The function returns the final size of CStream in bytes. +* If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable) +*/ + + +/*-******************************************** +* bitStream decoding API (read backward) +**********************************************/ +typedef struct +{ + size_t bitContainer; + unsigned bitsConsumed; + const char* ptr; + const char* start; + const char* limitPtr; +} BIT_DStream_t; + +typedef enum { BIT_DStream_unfinished = 0, + BIT_DStream_endOfBuffer = 1, + BIT_DStream_completed = 2, + BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */ + /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */ + +MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize); +MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits); +MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD); +MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD); + + +/* Start by invoking BIT_initDStream(). +* A chunk of the bitStream is then stored into a local register. +* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). +* You can then retrieve bitFields stored into the local register, **in reverse order**. +* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method. +* A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished. +* Otherwise, it can be less than that, so proceed accordingly. +* Checking if DStream has reached its end can be performed with BIT_endOfDStream(). +*/ + + +/*-**************************************** +* unsafe API +******************************************/ +MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits); +/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */ + +MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC); +/* unsafe version; does not check buffer overflow */ + +MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits); +/* faster, but works only if nbBits >= 1 */ + + + +/*-************************************************************** +* Internal functions +****************************************************************/ +MEM_STATIC unsigned BIT_highbit32 (register U32 val) +{ +# if defined(_MSC_VER) /* Visual */ + unsigned long r=0; + _BitScanReverse ( &r, val ); + return (unsigned) r; +# elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */ + return 31 - __builtin_clz (val); +# else /* Software version */ + static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, + 11, 14, 16, 18, 22, 25, 3, 30, + 8, 12, 20, 28, 15, 17, 24, 7, + 19, 27, 23, 6, 26, 5, 4, 31 }; + U32 v = val; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27]; +# endif +} + +/*===== Local Constants =====*/ +static const unsigned BIT_mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, + 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, + 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, + 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF }; /* up to 26 bits */ + + +/*-************************************************************** +* bitStream encoding +****************************************************************/ +/*! BIT_initCStream() : + * `dstCapacity` must be > sizeof(size_t) + * @return : 0 if success, + otherwise an error code (can be tested using ERR_isError() ) */ +MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, + void* startPtr, size_t dstCapacity) +{ + bitC->bitContainer = 0; + bitC->bitPos = 0; + bitC->startPtr = (char*)startPtr; + bitC->ptr = bitC->startPtr; + bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->bitContainer); + if (dstCapacity <= sizeof(bitC->bitContainer)) return ERROR(dstSize_tooSmall); + return 0; +} + +/*! BIT_addBits() : + can add up to 26 bits into `bitC`. + Does not check for register overflow ! */ +MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, + size_t value, unsigned nbBits) +{ + bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos; + bitC->bitPos += nbBits; +} + +/*! BIT_addBitsFast() : + * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */ +MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, + size_t value, unsigned nbBits) +{ + assert((value>>nbBits) == 0); + bitC->bitContainer |= value << bitC->bitPos; + bitC->bitPos += nbBits; +} + +/*! BIT_flushBitsFast() : + * assumption : bitContainer has not overflowed + * unsafe version; does not check buffer overflow */ +MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC) +{ + size_t const nbBytes = bitC->bitPos >> 3; + assert( bitC->bitPos <= (sizeof(bitC->bitContainer)*8) ); + MEM_writeLEST(bitC->ptr, bitC->bitContainer); + bitC->ptr += nbBytes; + assert(bitC->ptr <= bitC->endPtr); + bitC->bitPos &= 7; + bitC->bitContainer >>= nbBytes*8; +} + +/*! BIT_flushBits() : + * assumption : bitContainer has not overflowed + * safe version; check for buffer overflow, and prevents it. + * note : does not signal buffer overflow. + * overflow will be revealed later on using BIT_closeCStream() */ +MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC) +{ + size_t const nbBytes = bitC->bitPos >> 3; + assert( bitC->bitPos <= (sizeof(bitC->bitContainer)*8) ); + MEM_writeLEST(bitC->ptr, bitC->bitContainer); + bitC->ptr += nbBytes; + if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr; + bitC->bitPos &= 7; + bitC->bitContainer >>= nbBytes*8; +} + +/*! BIT_closeCStream() : + * @return : size of CStream, in bytes, + or 0 if it could not fit into dstBuffer */ +MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC) +{ + BIT_addBitsFast(bitC, 1, 1); /* endMark */ + BIT_flushBits(bitC); + if (bitC->ptr >= bitC->endPtr) return 0; /* overflow detected */ + return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0); +} + + +/*-******************************************************** +* bitStream decoding +**********************************************************/ +/*! BIT_initDStream() : +* Initialize a BIT_DStream_t. +* `bitD` : a pointer to an already allocated BIT_DStream_t structure. +* `srcSize` must be the *exact* size of the bitStream, in bytes. +* @return : size of stream (== srcSize) or an errorCode if a problem is detected +*/ +MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize) +{ + if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); } + + bitD->start = (const char*)srcBuffer; + bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer); + + if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ + bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer); + bitD->bitContainer = MEM_readLEST(bitD->ptr); + { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1]; + bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ + if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ } + } else { + bitD->ptr = bitD->start; + bitD->bitContainer = *(const BYTE*)(bitD->start); + switch(srcSize) + { + case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16); + case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24); + case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32); + case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24; + case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16; + case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8; + default:; + } + { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1]; + bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; + if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ } + bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8; + } + + return srcSize; +} + +MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start) +{ + return bitContainer >> start; +} + +MEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits) +{ +#if defined(__BMI__) && defined(__GNUC__) && __GNUC__*1000+__GNUC_MINOR__ >= 4008 /* experimental */ +# if defined(__x86_64__) + if (sizeof(bitContainer)==8) + return _bextr_u64(bitContainer, start, nbBits); + else +# endif + return _bextr_u32(bitContainer, start, nbBits); +#else + return (bitContainer >> start) & BIT_mask[nbBits]; +#endif +} + +MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) +{ + return bitContainer & BIT_mask[nbBits]; +} + +/*! BIT_lookBits() : + * Provides next n bits from local register. + * local register is not modified. + * On 32-bits, maxNbBits==24. + * On 64-bits, maxNbBits==56. + * @return : value extracted + */ + MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits) +{ +#if defined(__BMI__) && defined(__GNUC__) /* experimental; fails if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8 */ + return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits); +#else + U32 const regMask = sizeof(bitD->bitContainer)*8 - 1; + return ((bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> 1) >> ((regMask-nbBits) & regMask); +#endif +} + +/*! BIT_lookBitsFast() : + * unsafe version; only works if nbBits >= 1 */ +MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits) +{ + U32 const regMask = sizeof(bitD->bitContainer)*8 - 1; + assert(nbBits >= 1); + return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask); +} + +MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits) +{ + bitD->bitsConsumed += nbBits; +} + +/*! BIT_readBits() : + * Read (consume) next n bits from local register and update. + * Pay attention to not read more than nbBits contained into local register. + * @return : extracted value. + */ +MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits) +{ + size_t const value = BIT_lookBits(bitD, nbBits); + BIT_skipBits(bitD, nbBits); + return value; +} + +/*! BIT_readBitsFast() : +* unsafe version; only works only if nbBits >= 1 */ +MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits) +{ + size_t const value = BIT_lookBitsFast(bitD, nbBits); + assert(nbBits >= 1); + BIT_skipBits(bitD, nbBits); + return value; +} + +/*! BIT_reloadDStream() : +* Refill `bitD` from buffer previously set in BIT_initDStream() . +* This function is safe, it guarantees it will not read beyond src buffer. +* @return : status of `BIT_DStream_t` internal register. + if status == BIT_DStream_unfinished, internal register is filled with >= (sizeof(bitD->bitContainer)*8 - 7) bits */ +MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) +{ + if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* overflow detected, like end of stream */ + return BIT_DStream_overflow; + + if (bitD->ptr >= bitD->limitPtr) { + bitD->ptr -= bitD->bitsConsumed >> 3; + bitD->bitsConsumed &= 7; + bitD->bitContainer = MEM_readLEST(bitD->ptr); + return BIT_DStream_unfinished; + } + if (bitD->ptr == bitD->start) { + if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer; + return BIT_DStream_completed; + } + /* start < ptr < limitPtr */ + { U32 nbBytes = bitD->bitsConsumed >> 3; + BIT_DStream_status result = BIT_DStream_unfinished; + if (bitD->ptr - nbBytes < bitD->start) { + nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ + result = BIT_DStream_endOfBuffer; + } + bitD->ptr -= nbBytes; + bitD->bitsConsumed -= nbBytes*8; + bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD->bitContainer), otherwise bitD->ptr == bitD->start */ + return result; + } +} + +/*! BIT_endOfDStream() : +* @return Tells if DStream has exactly reached its end (all bits consumed). +*/ +MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream) +{ + return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8)); +} + +#if defined (__cplusplus) +} +#endif + +#endif /* BITSTREAM_H_MODULE */ diff --git a/thirdparty/zstd/common/entropy_common.c b/thirdparty/zstd/common/entropy_common.c new file mode 100644 index 0000000000..b37a082fee --- /dev/null +++ b/thirdparty/zstd/common/entropy_common.c @@ -0,0 +1,221 @@ +/* + Common functions of New Generation Entropy library + Copyright (C) 2016, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy + - Public forum : https://groups.google.com/forum/#!forum/lz4c +*************************************************************************** */ + +/* ************************************* +* Dependencies +***************************************/ +#include "mem.h" +#include "error_private.h" /* ERR_*, ERROR */ +#define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */ +#include "fse.h" +#define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */ +#include "huf.h" + + +/*=== Version ===*/ +unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; } + + +/*=== Error Management ===*/ +unsigned FSE_isError(size_t code) { return ERR_isError(code); } +const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); } + +unsigned HUF_isError(size_t code) { return ERR_isError(code); } +const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); } + + +/*-************************************************************** +* FSE NCount encoding-decoding +****************************************************************/ +size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, + const void* headerBuffer, size_t hbSize) +{ + const BYTE* const istart = (const BYTE*) headerBuffer; + const BYTE* const iend = istart + hbSize; + const BYTE* ip = istart; + int nbBits; + int remaining; + int threshold; + U32 bitStream; + int bitCount; + unsigned charnum = 0; + int previous0 = 0; + + if (hbSize < 4) return ERROR(srcSize_wrong); + bitStream = MEM_readLE32(ip); + nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */ + if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge); + bitStream >>= 4; + bitCount = 4; + *tableLogPtr = nbBits; + remaining = (1<<nbBits)+1; + threshold = 1<<nbBits; + nbBits++; + + while ((remaining>1) & (charnum<=*maxSVPtr)) { + if (previous0) { + unsigned n0 = charnum; + while ((bitStream & 0xFFFF) == 0xFFFF) { + n0 += 24; + if (ip < iend-5) { + ip += 2; + bitStream = MEM_readLE32(ip) >> bitCount; + } else { + bitStream >>= 16; + bitCount += 16; + } } + while ((bitStream & 3) == 3) { + n0 += 3; + bitStream >>= 2; + bitCount += 2; + } + n0 += bitStream & 3; + bitCount += 2; + if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall); + while (charnum < n0) normalizedCounter[charnum++] = 0; + if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) { + ip += bitCount>>3; + bitCount &= 7; + bitStream = MEM_readLE32(ip) >> bitCount; + } else { + bitStream >>= 2; + } } + { int const max = (2*threshold-1) - remaining; + int count; + + if ((bitStream & (threshold-1)) < (U32)max) { + count = bitStream & (threshold-1); + bitCount += nbBits-1; + } else { + count = bitStream & (2*threshold-1); + if (count >= threshold) count -= max; + bitCount += nbBits; + } + + count--; /* extra accuracy */ + remaining -= count < 0 ? -count : count; /* -1 means +1 */ + normalizedCounter[charnum++] = (short)count; + previous0 = !count; + while (remaining < threshold) { + nbBits--; + threshold >>= 1; + } + + if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) { + ip += bitCount>>3; + bitCount &= 7; + } else { + bitCount -= (int)(8 * (iend - 4 - ip)); + ip = iend - 4; + } + bitStream = MEM_readLE32(ip) >> (bitCount & 31); + } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */ + if (remaining != 1) return ERROR(corruption_detected); + if (bitCount > 32) return ERROR(corruption_detected); + *maxSVPtr = charnum-1; + + ip += (bitCount+7)>>3; + return ip-istart; +} + + +/*! HUF_readStats() : + Read compact Huffman tree, saved by HUF_writeCTable(). + `huffWeight` is destination buffer. + `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32. + @return : size read from `src` , or an error Code . + Note : Needed by HUF_readCTable() and HUF_readDTableX?() . +*/ +size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, + U32* nbSymbolsPtr, U32* tableLogPtr, + const void* src, size_t srcSize) +{ + U32 weightTotal; + const BYTE* ip = (const BYTE*) src; + size_t iSize; + size_t oSize; + + if (!srcSize) return ERROR(srcSize_wrong); + iSize = ip[0]; + /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */ + + if (iSize >= 128) { /* special header */ + oSize = iSize - 127; + iSize = ((oSize+1)/2); + if (iSize+1 > srcSize) return ERROR(srcSize_wrong); + if (oSize >= hwSize) return ERROR(corruption_detected); + ip += 1; + { U32 n; + for (n=0; n<oSize; n+=2) { + huffWeight[n] = ip[n/2] >> 4; + huffWeight[n+1] = ip[n/2] & 15; + } } } + else { /* header compressed with FSE (normal case) */ + FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */ + if (iSize+1 > srcSize) return ERROR(srcSize_wrong); + oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6); /* max (hwSize-1) values decoded, as last one is implied */ + if (FSE_isError(oSize)) return oSize; + } + + /* collect weight stats */ + memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32)); + weightTotal = 0; + { U32 n; for (n=0; n<oSize; n++) { + if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected); + rankStats[huffWeight[n]]++; + weightTotal += (1 << huffWeight[n]) >> 1; + } } + if (weightTotal == 0) return ERROR(corruption_detected); + + /* get last non-null symbol weight (implied, total must be 2^n) */ + { U32 const tableLog = BIT_highbit32(weightTotal) + 1; + if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected); + *tableLogPtr = tableLog; + /* determine last weight */ + { U32 const total = 1 << tableLog; + U32 const rest = total - weightTotal; + U32 const verif = 1 << BIT_highbit32(rest); + U32 const lastWeight = BIT_highbit32(rest) + 1; + if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */ + huffWeight[oSize] = (BYTE)lastWeight; + rankStats[lastWeight]++; + } } + + /* check tree construction validity */ + if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */ + + /* results */ + *nbSymbolsPtr = (U32)(oSize+1); + return iSize+1; +} diff --git a/thirdparty/zstd/common/error_private.c b/thirdparty/zstd/common/error_private.c new file mode 100644 index 0000000000..b3287245f1 --- /dev/null +++ b/thirdparty/zstd/common/error_private.c @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +/* The purpose of this file is to have a single list of error strings embedded in binary */ + +#include "error_private.h" + +const char* ERR_getErrorString(ERR_enum code) +{ + static const char* const notErrorCode = "Unspecified error code"; + switch( code ) + { + case PREFIX(no_error): return "No error detected"; + case PREFIX(GENERIC): return "Error (generic)"; + case PREFIX(prefix_unknown): return "Unknown frame descriptor"; + case PREFIX(version_unsupported): return "Version not supported"; + case PREFIX(parameter_unknown): return "Unknown parameter type"; + case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter"; + case PREFIX(frameParameter_unsupportedBy32bits): return "Frame parameter unsupported in 32-bits mode"; + case PREFIX(frameParameter_windowTooLarge): return "Frame requires too much memory for decoding"; + case PREFIX(compressionParameter_unsupported): return "Compression parameter is out of bound"; + case PREFIX(init_missing): return "Context should be init first"; + case PREFIX(memory_allocation): return "Allocation error : not enough memory"; + case PREFIX(stage_wrong): return "Operation not authorized at current processing stage"; + case PREFIX(dstSize_tooSmall): return "Destination buffer is too small"; + case PREFIX(srcSize_wrong): return "Src size is incorrect"; + case PREFIX(corruption_detected): return "Corrupted block detected"; + case PREFIX(checksum_wrong): return "Restored data doesn't match checksum"; + case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported"; + case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large"; + case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small"; + case PREFIX(dictionary_corrupted): return "Dictionary is corrupted"; + case PREFIX(dictionary_wrong): return "Dictionary mismatch"; + case PREFIX(dictionaryCreation_failed): return "Cannot create Dictionary from provided samples"; + case PREFIX(maxCode): + default: return notErrorCode; + } +} diff --git a/thirdparty/zstd/common/error_private.h b/thirdparty/zstd/common/error_private.h new file mode 100644 index 0000000000..1bc2e49548 --- /dev/null +++ b/thirdparty/zstd/common/error_private.h @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +/* Note : this module is expected to remain private, do not expose it */ + +#ifndef ERROR_H_MODULE +#define ERROR_H_MODULE + +#if defined (__cplusplus) +extern "C" { +#endif + + +/* **************************************** +* Dependencies +******************************************/ +#include <stddef.h> /* size_t */ +#include "zstd_errors.h" /* enum list */ + + +/* **************************************** +* Compiler-specific +******************************************/ +#if defined(__GNUC__) +# define ERR_STATIC static __attribute__((unused)) +#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) +# define ERR_STATIC static inline +#elif defined(_MSC_VER) +# define ERR_STATIC static __inline +#else +# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ +#endif + + +/*-**************************************** +* Customization (error_public.h) +******************************************/ +typedef ZSTD_ErrorCode ERR_enum; +#define PREFIX(name) ZSTD_error_##name + + +/*-**************************************** +* Error codes handling +******************************************/ +#ifdef ERROR +# undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */ +#endif +#define ERROR(name) ((size_t)-PREFIX(name)) + +ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } + +ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); } + + +/*-**************************************** +* Error Strings +******************************************/ + +const char* ERR_getErrorString(ERR_enum code); /* error_private.c */ + +ERR_STATIC const char* ERR_getErrorName(size_t code) +{ + return ERR_getErrorString(ERR_getErrorCode(code)); +} + +#if defined (__cplusplus) +} +#endif + +#endif /* ERROR_H_MODULE */ diff --git a/thirdparty/zstd/common/fse.h b/thirdparty/zstd/common/fse.h new file mode 100644 index 0000000000..6d5d41def1 --- /dev/null +++ b/thirdparty/zstd/common/fse.h @@ -0,0 +1,698 @@ +/* ****************************************************************** + FSE : Finite State Entropy codec + Public Prototypes declaration + Copyright (C) 2013-2016, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - Source repository : https://github.com/Cyan4973/FiniteStateEntropy +****************************************************************** */ +#ifndef FSE_H +#define FSE_H + +#if defined (__cplusplus) +extern "C" { +#endif + + +/*-***************************************** +* Dependencies +******************************************/ +#include <stddef.h> /* size_t, ptrdiff_t */ + + +/*-***************************************** +* FSE_PUBLIC_API : control library symbols visibility +******************************************/ +#if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4) +# define FSE_PUBLIC_API __attribute__ ((visibility ("default"))) +#elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */ +# define FSE_PUBLIC_API __declspec(dllexport) +#elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1) +# define FSE_PUBLIC_API __declspec(dllimport) /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ +#else +# define FSE_PUBLIC_API +#endif + +/*------ Version ------*/ +#define FSE_VERSION_MAJOR 0 +#define FSE_VERSION_MINOR 9 +#define FSE_VERSION_RELEASE 0 + +#define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE +#define FSE_QUOTE(str) #str +#define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str) +#define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION) + +#define FSE_VERSION_NUMBER (FSE_VERSION_MAJOR *100*100 + FSE_VERSION_MINOR *100 + FSE_VERSION_RELEASE) +FSE_PUBLIC_API unsigned FSE_versionNumber(void); /**< library version number; to be used when checking dll version */ + +/*-**************************************** +* FSE simple functions +******************************************/ +/*! FSE_compress() : + Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'. + 'dst' buffer must be already allocated. Compression runs faster is dstCapacity >= FSE_compressBound(srcSize). + @return : size of compressed data (<= dstCapacity). + Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! + if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead. + if FSE_isError(return), compression failed (more details using FSE_getErrorName()) +*/ +FSE_PUBLIC_API size_t FSE_compress(void* dst, size_t dstCapacity, + const void* src, size_t srcSize); + +/*! FSE_decompress(): + Decompress FSE data from buffer 'cSrc', of size 'cSrcSize', + into already allocated destination buffer 'dst', of size 'dstCapacity'. + @return : size of regenerated data (<= maxDstSize), + or an error code, which can be tested using FSE_isError() . + + ** Important ** : FSE_decompress() does not decompress non-compressible nor RLE data !!! + Why ? : making this distinction requires a header. + Header management is intentionally delegated to the user layer, which can better manage special cases. +*/ +FSE_PUBLIC_API size_t FSE_decompress(void* dst, size_t dstCapacity, + const void* cSrc, size_t cSrcSize); + + +/*-***************************************** +* Tool functions +******************************************/ +FSE_PUBLIC_API size_t FSE_compressBound(size_t size); /* maximum compressed size */ + +/* Error Management */ +FSE_PUBLIC_API unsigned FSE_isError(size_t code); /* tells if a return value is an error code */ +FSE_PUBLIC_API const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */ + + +/*-***************************************** +* FSE advanced functions +******************************************/ +/*! FSE_compress2() : + Same as FSE_compress(), but allows the selection of 'maxSymbolValue' and 'tableLog' + Both parameters can be defined as '0' to mean : use default value + @return : size of compressed data + Special values : if return == 0, srcData is not compressible => Nothing is stored within cSrc !!! + if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression. + if FSE_isError(return), it's an error code. +*/ +FSE_PUBLIC_API size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog); + + +/*-***************************************** +* FSE detailed API +******************************************/ +/*! +FSE_compress() does the following: +1. count symbol occurrence from source[] into table count[] +2. normalize counters so that sum(count[]) == Power_of_2 (2^tableLog) +3. save normalized counters to memory buffer using writeNCount() +4. build encoding table 'CTable' from normalized counters +5. encode the data stream using encoding table 'CTable' + +FSE_decompress() does the following: +1. read normalized counters with readNCount() +2. build decoding table 'DTable' from normalized counters +3. decode the data stream using decoding table 'DTable' + +The following API allows targeting specific sub-functions for advanced tasks. +For example, it's possible to compress several blocks using the same 'CTable', +or to save and provide normalized distribution using external method. +*/ + +/* *** COMPRESSION *** */ + +/*! FSE_count(): + Provides the precise count of each byte within a table 'count'. + 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1). + *maxSymbolValuePtr will be updated if detected smaller than initial value. + @return : the count of the most frequent symbol (which is not identified). + if return == srcSize, there is only one symbol. + Can also return an error code, which can be tested with FSE_isError(). */ +FSE_PUBLIC_API size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize); + +/*! FSE_optimalTableLog(): + dynamically downsize 'tableLog' when conditions are met. + It saves CPU time, by using smaller tables, while preserving or even improving compression ratio. + @return : recommended tableLog (necessarily <= 'maxTableLog') */ +FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue); + +/*! FSE_normalizeCount(): + normalize counts so that sum(count[]) == Power_of_2 (2^tableLog) + 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). + @return : tableLog, + or an errorCode, which can be tested using FSE_isError() */ +FSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, const unsigned* count, size_t srcSize, unsigned maxSymbolValue); + +/*! FSE_NCountWriteBound(): + Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'. + Typically useful for allocation purpose. */ +FSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog); + +/*! FSE_writeNCount(): + Compactly save 'normalizedCounter' into 'buffer'. + @return : size of the compressed table, + or an errorCode, which can be tested using FSE_isError(). */ +FSE_PUBLIC_API size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); + + +/*! Constructor and Destructor of FSE_CTable. + Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */ +typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */ +FSE_PUBLIC_API FSE_CTable* FSE_createCTable (unsigned tableLog, unsigned maxSymbolValue); +FSE_PUBLIC_API void FSE_freeCTable (FSE_CTable* ct); + +/*! FSE_buildCTable(): + Builds `ct`, which must be already allocated, using FSE_createCTable(). + @return : 0, or an errorCode, which can be tested using FSE_isError() */ +FSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); + +/*! FSE_compress_usingCTable(): + Compress `src` using `ct` into `dst` which must be already allocated. + @return : size of compressed data (<= `dstCapacity`), + or 0 if compressed data could not fit into `dst`, + or an errorCode, which can be tested using FSE_isError() */ +FSE_PUBLIC_API size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct); + +/*! +Tutorial : +---------- +The first step is to count all symbols. FSE_count() does this job very fast. +Result will be saved into 'count', a table of unsigned int, which must be already allocated, and have 'maxSymbolValuePtr[0]+1' cells. +'src' is a table of bytes of size 'srcSize'. All values within 'src' MUST be <= maxSymbolValuePtr[0] +maxSymbolValuePtr[0] will be updated, with its real value (necessarily <= original value) +FSE_count() will return the number of occurrence of the most frequent symbol. +This can be used to know if there is a single symbol within 'src', and to quickly evaluate its compressibility. +If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()). + +The next step is to normalize the frequencies. +FSE_normalizeCount() will ensure that sum of frequencies is == 2 ^'tableLog'. +It also guarantees a minimum of 1 to any Symbol with frequency >= 1. +You can use 'tableLog'==0 to mean "use default tableLog value". +If you are unsure of which tableLog value to use, you can ask FSE_optimalTableLog(), +which will provide the optimal valid tableLog given sourceSize, maxSymbolValue, and a user-defined maximum (0 means "default"). + +The result of FSE_normalizeCount() will be saved into a table, +called 'normalizedCounter', which is a table of signed short. +'normalizedCounter' must be already allocated, and have at least 'maxSymbolValue+1' cells. +The return value is tableLog if everything proceeded as expected. +It is 0 if there is a single symbol within distribution. +If there is an error (ex: invalid tableLog value), the function will return an ErrorCode (which can be tested using FSE_isError()). + +'normalizedCounter' can be saved in a compact manner to a memory area using FSE_writeNCount(). +'buffer' must be already allocated. +For guaranteed success, buffer size must be at least FSE_headerBound(). +The result of the function is the number of bytes written into 'buffer'. +If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError(); ex : buffer size too small). + +'normalizedCounter' can then be used to create the compression table 'CTable'. +The space required by 'CTable' must be already allocated, using FSE_createCTable(). +You can then use FSE_buildCTable() to fill 'CTable'. +If there is an error, both functions will return an ErrorCode (which can be tested using FSE_isError()). + +'CTable' can then be used to compress 'src', with FSE_compress_usingCTable(). +Similar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size 'srcSize' +The function returns the size of compressed data (without header), necessarily <= `dstCapacity`. +If it returns '0', compressed data could not fit into 'dst'. +If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()). +*/ + + +/* *** DECOMPRESSION *** */ + +/*! FSE_readNCount(): + Read compactly saved 'normalizedCounter' from 'rBuffer'. + @return : size read from 'rBuffer', + or an errorCode, which can be tested using FSE_isError(). + maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */ +FSE_PUBLIC_API size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize); + +/*! Constructor and Destructor of FSE_DTable. + Note that its size depends on 'tableLog' */ +typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ +FSE_PUBLIC_API FSE_DTable* FSE_createDTable(unsigned tableLog); +FSE_PUBLIC_API void FSE_freeDTable(FSE_DTable* dt); + +/*! FSE_buildDTable(): + Builds 'dt', which must be already allocated, using FSE_createDTable(). + return : 0, or an errorCode, which can be tested using FSE_isError() */ +FSE_PUBLIC_API size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); + +/*! FSE_decompress_usingDTable(): + Decompress compressed source `cSrc` of size `cSrcSize` using `dt` + into `dst` which must be already allocated. + @return : size of regenerated data (necessarily <= `dstCapacity`), + or an errorCode, which can be tested using FSE_isError() */ +FSE_PUBLIC_API size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt); + +/*! +Tutorial : +---------- +(Note : these functions only decompress FSE-compressed blocks. + If block is uncompressed, use memcpy() instead + If block is a single repeated byte, use memset() instead ) + +The first step is to obtain the normalized frequencies of symbols. +This can be performed by FSE_readNCount() if it was saved using FSE_writeNCount(). +'normalizedCounter' must be already allocated, and have at least 'maxSymbolValuePtr[0]+1' cells of signed short. +In practice, that means it's necessary to know 'maxSymbolValue' beforehand, +or size the table to handle worst case situations (typically 256). +FSE_readNCount() will provide 'tableLog' and 'maxSymbolValue'. +The result of FSE_readNCount() is the number of bytes read from 'rBuffer'. +Note that 'rBufferSize' must be at least 4 bytes, even if useful information is less than that. +If there is an error, the function will return an error code, which can be tested using FSE_isError(). + +The next step is to build the decompression tables 'FSE_DTable' from 'normalizedCounter'. +This is performed by the function FSE_buildDTable(). +The space required by 'FSE_DTable' must be already allocated using FSE_createDTable(). +If there is an error, the function will return an error code, which can be tested using FSE_isError(). + +`FSE_DTable` can then be used to decompress `cSrc`, with FSE_decompress_usingDTable(). +`cSrcSize` must be strictly correct, otherwise decompression will fail. +FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`). +If there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small) +*/ + + +#ifdef FSE_STATIC_LINKING_ONLY + +/* *** Dependency *** */ +#include "bitstream.h" + + +/* ***************************************** +* Static allocation +*******************************************/ +/* FSE buffer bounds */ +#define FSE_NCOUNTBOUND 512 +#define FSE_BLOCKBOUND(size) (size + (size>>7)) +#define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ + +/* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */ +#define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2)) +#define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog)) + +/* or use the size to malloc() space directly. Pay attention to alignment restrictions though */ +#define FSE_CTABLE_SIZE(maxTableLog, maxSymbolValue) (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(FSE_CTable)) +#define FSE_DTABLE_SIZE(maxTableLog) (FSE_DTABLE_SIZE_U32(maxTableLog) * sizeof(FSE_DTable)) + + +/* ***************************************** +* FSE advanced API +*******************************************/ +/* FSE_count_wksp() : + * Same as FSE_count(), but using an externally provided scratch buffer. + * `workSpace` size must be table of >= `1024` unsigned + */ +size_t FSE_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr, + const void* source, size_t sourceSize, unsigned* workSpace); + +/** FSE_countFast() : + * same as FSE_count(), but blindly trusts that all byte values within src are <= *maxSymbolValuePtr + */ +size_t FSE_countFast(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize); + +/* FSE_countFast_wksp() : + * Same as FSE_countFast(), but using an externally provided scratch buffer. + * `workSpace` must be a table of minimum `1024` unsigned + */ +size_t FSE_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned* workSpace); + +/*! FSE_count_simple + * Same as FSE_countFast(), but does not use any additional memory (not even on stack). + * This function is unsafe, and will segfault if any value within `src` is `> *maxSymbolValuePtr` (presuming it's also the size of `count`). +*/ +size_t FSE_count_simple(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize); + + + +unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus); +/**< same as FSE_optimalTableLog(), which used `minus==2` */ + +/* FSE_compress_wksp() : + * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`). + * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable. + */ +#define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024) ) +size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); + +size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits); +/**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */ + +size_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue); +/**< build a fake FSE_CTable, designed to compress always the same symbolValue */ + +/* FSE_buildCTable_wksp() : + * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`). + * `wkspSize` must be >= `(1<<tableLog)`. + */ +size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); + +size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits); +/**< build a fake FSE_DTable, designed to read a flat distribution where each symbol uses nbBits */ + +size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue); +/**< build a fake FSE_DTable, designed to always generate the same symbolValue */ + +size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog); +/**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DTABLE_SIZE_U32(maxLog)` */ + + +/* ***************************************** +* FSE symbol compression API +*******************************************/ +/*! + This API consists of small unitary functions, which highly benefit from being inlined. + Hence their body are included in next section. +*/ +typedef struct { + ptrdiff_t value; + const void* stateTable; + const void* symbolTT; + unsigned stateLog; +} FSE_CState_t; + +static void FSE_initCState(FSE_CState_t* CStatePtr, const FSE_CTable* ct); + +static void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* CStatePtr, unsigned symbol); + +static void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* CStatePtr); + +/**< +These functions are inner components of FSE_compress_usingCTable(). +They allow the creation of custom streams, mixing multiple tables and bit sources. + +A key property to keep in mind is that encoding and decoding are done **in reverse direction**. +So the first symbol you will encode is the last you will decode, like a LIFO stack. + +You will need a few variables to track your CStream. They are : + +FSE_CTable ct; // Provided by FSE_buildCTable() +BIT_CStream_t bitStream; // bitStream tracking structure +FSE_CState_t state; // State tracking structure (can have several) + + +The first thing to do is to init bitStream and state. + size_t errorCode = BIT_initCStream(&bitStream, dstBuffer, maxDstSize); + FSE_initCState(&state, ct); + +Note that BIT_initCStream() can produce an error code, so its result should be tested, using FSE_isError(); +You can then encode your input data, byte after byte. +FSE_encodeSymbol() outputs a maximum of 'tableLog' bits at a time. +Remember decoding will be done in reverse direction. + FSE_encodeByte(&bitStream, &state, symbol); + +At any time, you can also add any bit sequence. +Note : maximum allowed nbBits is 25, for compatibility with 32-bits decoders + BIT_addBits(&bitStream, bitField, nbBits); + +The above methods don't commit data to memory, they just store it into local register, for speed. +Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). +Writing data to memory is a manual operation, performed by the flushBits function. + BIT_flushBits(&bitStream); + +Your last FSE encoding operation shall be to flush your last state value(s). + FSE_flushState(&bitStream, &state); + +Finally, you must close the bitStream. +The function returns the size of CStream in bytes. +If data couldn't fit into dstBuffer, it will return a 0 ( == not compressible) +If there is an error, it returns an errorCode (which can be tested using FSE_isError()). + size_t size = BIT_closeCStream(&bitStream); +*/ + + +/* ***************************************** +* FSE symbol decompression API +*******************************************/ +typedef struct { + size_t state; + const void* table; /* precise table may vary, depending on U16 */ +} FSE_DState_t; + + +static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt); + +static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD); + +static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr); + +/**< +Let's now decompose FSE_decompress_usingDTable() into its unitary components. +You will decode FSE-encoded symbols from the bitStream, +and also any other bitFields you put in, **in reverse order**. + +You will need a few variables to track your bitStream. They are : + +BIT_DStream_t DStream; // Stream context +FSE_DState_t DState; // State context. Multiple ones are possible +FSE_DTable* DTablePtr; // Decoding table, provided by FSE_buildDTable() + +The first thing to do is to init the bitStream. + errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize); + +You should then retrieve your initial state(s) +(in reverse flushing order if you have several ones) : + errorCode = FSE_initDState(&DState, &DStream, DTablePtr); + +You can then decode your data, symbol after symbol. +For information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'. +Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out). + unsigned char symbol = FSE_decodeSymbol(&DState, &DStream); + +You can retrieve any bitfield you eventually stored into the bitStream (in reverse order) +Note : maximum allowed nbBits is 25, for 32-bits compatibility + size_t bitField = BIT_readBits(&DStream, nbBits); + +All above operations only read from local register (which size depends on size_t). +Refueling the register from memory is manually performed by the reload method. + endSignal = FSE_reloadDStream(&DStream); + +BIT_reloadDStream() result tells if there is still some more data to read from DStream. +BIT_DStream_unfinished : there is still some data left into the DStream. +BIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled. +BIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed. +BIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted. + +When reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop, +to properly detect the exact end of stream. +After each decoded symbol, check if DStream is fully consumed using this simple test : + BIT_reloadDStream(&DStream) >= BIT_DStream_completed + +When it's done, verify decompression is fully completed, by checking both DStream and the relevant states. +Checking if DStream has reached its end is performed by : + BIT_endOfDStream(&DStream); +Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible. + FSE_endOfDState(&DState); +*/ + + +/* ***************************************** +* FSE unsafe API +*******************************************/ +static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD); +/* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */ + + +/* ***************************************** +* Implementation of inlined functions +*******************************************/ +typedef struct { + int deltaFindState; + U32 deltaNbBits; +} FSE_symbolCompressionTransform; /* total 8 bytes */ + +MEM_STATIC void FSE_initCState(FSE_CState_t* statePtr, const FSE_CTable* ct) +{ + const void* ptr = ct; + const U16* u16ptr = (const U16*) ptr; + const U32 tableLog = MEM_read16(ptr); + statePtr->value = (ptrdiff_t)1<<tableLog; + statePtr->stateTable = u16ptr+2; + statePtr->symbolTT = ((const U32*)ct + 1 + (tableLog ? (1<<(tableLog-1)) : 1)); + statePtr->stateLog = tableLog; +} + + +/*! FSE_initCState2() : +* Same as FSE_initCState(), but the first symbol to include (which will be the last to be read) +* uses the smallest state value possible, saving the cost of this symbol */ +MEM_STATIC void FSE_initCState2(FSE_CState_t* statePtr, const FSE_CTable* ct, U32 symbol) +{ + FSE_initCState(statePtr, ct); + { const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol]; + const U16* stateTable = (const U16*)(statePtr->stateTable); + U32 nbBitsOut = (U32)((symbolTT.deltaNbBits + (1<<15)) >> 16); + statePtr->value = (nbBitsOut << 16) - symbolTT.deltaNbBits; + statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState]; + } +} + +MEM_STATIC void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* statePtr, U32 symbol) +{ + FSE_symbolCompressionTransform const symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol]; + const U16* const stateTable = (const U16*)(statePtr->stateTable); + U32 const nbBitsOut = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16); + BIT_addBits(bitC, statePtr->value, nbBitsOut); + statePtr->value = stateTable[ (statePtr->value >> nbBitsOut) + symbolTT.deltaFindState]; +} + +MEM_STATIC void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* statePtr) +{ + BIT_addBits(bitC, statePtr->value, statePtr->stateLog); + BIT_flushBits(bitC); +} + + +/* ====== Decompression ====== */ + +typedef struct { + U16 tableLog; + U16 fastMode; +} FSE_DTableHeader; /* sizeof U32 */ + +typedef struct +{ + unsigned short newState; + unsigned char symbol; + unsigned char nbBits; +} FSE_decode_t; /* size == U32 */ + +MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt) +{ + const void* ptr = dt; + const FSE_DTableHeader* const DTableH = (const FSE_DTableHeader*)ptr; + DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog); + BIT_reloadDStream(bitD); + DStatePtr->table = dt + 1; +} + +MEM_STATIC BYTE FSE_peekSymbol(const FSE_DState_t* DStatePtr) +{ + FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; + return DInfo.symbol; +} + +MEM_STATIC void FSE_updateState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) +{ + FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; + U32 const nbBits = DInfo.nbBits; + size_t const lowBits = BIT_readBits(bitD, nbBits); + DStatePtr->state = DInfo.newState + lowBits; +} + +MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) +{ + FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; + U32 const nbBits = DInfo.nbBits; + BYTE const symbol = DInfo.symbol; + size_t const lowBits = BIT_readBits(bitD, nbBits); + + DStatePtr->state = DInfo.newState + lowBits; + return symbol; +} + +/*! FSE_decodeSymbolFast() : + unsafe, only works if no symbol has a probability > 50% */ +MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) +{ + FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; + U32 const nbBits = DInfo.nbBits; + BYTE const symbol = DInfo.symbol; + size_t const lowBits = BIT_readBitsFast(bitD, nbBits); + + DStatePtr->state = DInfo.newState + lowBits; + return symbol; +} + +MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr) +{ + return DStatePtr->state == 0; +} + + + +#ifndef FSE_COMMONDEFS_ONLY + +/* ************************************************************** +* Tuning parameters +****************************************************************/ +/*!MEMORY_USAGE : +* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) +* Increasing memory usage improves compression ratio +* Reduced memory usage can improve speed, due to cache effect +* Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */ +#ifndef FSE_MAX_MEMORY_USAGE +# define FSE_MAX_MEMORY_USAGE 14 +#endif +#ifndef FSE_DEFAULT_MEMORY_USAGE +# define FSE_DEFAULT_MEMORY_USAGE 13 +#endif + +/*!FSE_MAX_SYMBOL_VALUE : +* Maximum symbol value authorized. +* Required for proper stack allocation */ +#ifndef FSE_MAX_SYMBOL_VALUE +# define FSE_MAX_SYMBOL_VALUE 255 +#endif + +/* ************************************************************** +* template functions type & suffix +****************************************************************/ +#define FSE_FUNCTION_TYPE BYTE +#define FSE_FUNCTION_EXTENSION +#define FSE_DECODE_TYPE FSE_decode_t + + +#endif /* !FSE_COMMONDEFS_ONLY */ + + +/* *************************************************************** +* Constants +*****************************************************************/ +#define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2) +#define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG) +#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1) +#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2) +#define FSE_MIN_TABLELOG 5 + +#define FSE_TABLELOG_ABSOLUTE_MAX 15 +#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX +# error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported" +#endif + +#define FSE_TABLESTEP(tableSize) ((tableSize>>1) + (tableSize>>3) + 3) + + +#endif /* FSE_STATIC_LINKING_ONLY */ + + +#if defined (__cplusplus) +} +#endif + +#endif /* FSE_H */ diff --git a/thirdparty/zstd/common/fse_decompress.c b/thirdparty/zstd/common/fse_decompress.c new file mode 100644 index 0000000000..8474a4c079 --- /dev/null +++ b/thirdparty/zstd/common/fse_decompress.c @@ -0,0 +1,328 @@ +/* ****************************************************************** + FSE : Finite State Entropy decoder + Copyright (C) 2013-2015, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy + - Public forum : https://groups.google.com/forum/#!forum/lz4c +****************************************************************** */ + + +/* ************************************************************** +* Compiler specifics +****************************************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# define FORCE_INLINE static __forceinline +# include <intrin.h> /* For Visual 2005 */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ +#else +# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +# else +# define FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +#endif + + +/* ************************************************************** +* Includes +****************************************************************/ +#include <stdlib.h> /* malloc, free, qsort */ +#include <string.h> /* memcpy, memset */ +#include "bitstream.h" +#define FSE_STATIC_LINKING_ONLY +#include "fse.h" + + +/* ************************************************************** +* Error Management +****************************************************************/ +#define FSE_isError ERR_isError +#define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ + +/* check and forward error code */ +#define CHECK_F(f) { size_t const e = f; if (FSE_isError(e)) return e; } + + +/* ************************************************************** +* Templates +****************************************************************/ +/* + designed to be included + for type-specific functions (template emulation in C) + Objective is to write these functions only once, for improved maintenance +*/ + +/* safety checks */ +#ifndef FSE_FUNCTION_EXTENSION +# error "FSE_FUNCTION_EXTENSION must be defined" +#endif +#ifndef FSE_FUNCTION_TYPE +# error "FSE_FUNCTION_TYPE must be defined" +#endif + +/* Function names */ +#define FSE_CAT(X,Y) X##Y +#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y) +#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y) + + +/* Function templates */ +FSE_DTable* FSE_createDTable (unsigned tableLog) +{ + if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX; + return (FSE_DTable*)malloc( FSE_DTABLE_SIZE_U32(tableLog) * sizeof (U32) ); +} + +void FSE_freeDTable (FSE_DTable* dt) +{ + free(dt); +} + +size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) +{ + void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */ + FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr); + U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1]; + + U32 const maxSV1 = maxSymbolValue + 1; + U32 const tableSize = 1 << tableLog; + U32 highThreshold = tableSize-1; + + /* Sanity Checks */ + if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge); + if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); + + /* Init, lay down lowprob symbols */ + { FSE_DTableHeader DTableH; + DTableH.tableLog = (U16)tableLog; + DTableH.fastMode = 1; + { S16 const largeLimit= (S16)(1 << (tableLog-1)); + U32 s; + for (s=0; s<maxSV1; s++) { + if (normalizedCounter[s]==-1) { + tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; + symbolNext[s] = 1; + } else { + if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0; + symbolNext[s] = normalizedCounter[s]; + } } } + memcpy(dt, &DTableH, sizeof(DTableH)); + } + + /* Spread symbols */ + { U32 const tableMask = tableSize-1; + U32 const step = FSE_TABLESTEP(tableSize); + U32 s, position = 0; + for (s=0; s<maxSV1; s++) { + int i; + for (i=0; i<normalizedCounter[s]; i++) { + tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; + position = (position + step) & tableMask; + while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */ + } } + if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ + } + + /* Build Decoding table */ + { U32 u; + for (u=0; u<tableSize; u++) { + FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol); + U16 nextState = symbolNext[symbol]++; + tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) ); + tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize); + } } + + return 0; +} + + +#ifndef FSE_COMMONDEFS_ONLY + +/*-******************************************************* +* Decompression (Byte symbols) +*********************************************************/ +size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue) +{ + void* ptr = dt; + FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; + void* dPtr = dt + 1; + FSE_decode_t* const cell = (FSE_decode_t*)dPtr; + + DTableH->tableLog = 0; + DTableH->fastMode = 0; + + cell->newState = 0; + cell->symbol = symbolValue; + cell->nbBits = 0; + + return 0; +} + + +size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits) +{ + void* ptr = dt; + FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; + void* dPtr = dt + 1; + FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr; + const unsigned tableSize = 1 << nbBits; + const unsigned tableMask = tableSize - 1; + const unsigned maxSV1 = tableMask+1; + unsigned s; + + /* Sanity checks */ + if (nbBits < 1) return ERROR(GENERIC); /* min size */ + + /* Build Decoding Table */ + DTableH->tableLog = (U16)nbBits; + DTableH->fastMode = 1; + for (s=0; s<maxSV1; s++) { + dinfo[s].newState = 0; + dinfo[s].symbol = (BYTE)s; + dinfo[s].nbBits = (BYTE)nbBits; + } + + return 0; +} + +FORCE_INLINE size_t FSE_decompress_usingDTable_generic( + void* dst, size_t maxDstSize, + const void* cSrc, size_t cSrcSize, + const FSE_DTable* dt, const unsigned fast) +{ + BYTE* const ostart = (BYTE*) dst; + BYTE* op = ostart; + BYTE* const omax = op + maxDstSize; + BYTE* const olimit = omax-3; + + BIT_DStream_t bitD; + FSE_DState_t state1; + FSE_DState_t state2; + + /* Init */ + CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize)); + + FSE_initDState(&state1, &bitD, dt); + FSE_initDState(&state2, &bitD, dt); + +#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) + + /* 4 symbols per loop */ + for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) { + op[0] = FSE_GETSYMBOL(&state1); + + if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ + BIT_reloadDStream(&bitD); + + op[1] = FSE_GETSYMBOL(&state2); + + if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ + { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } } + + op[2] = FSE_GETSYMBOL(&state1); + + if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ + BIT_reloadDStream(&bitD); + + op[3] = FSE_GETSYMBOL(&state2); + } + + /* tail */ + /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */ + while (1) { + if (op>(omax-2)) return ERROR(dstSize_tooSmall); + *op++ = FSE_GETSYMBOL(&state1); + if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) { + *op++ = FSE_GETSYMBOL(&state2); + break; + } + + if (op>(omax-2)) return ERROR(dstSize_tooSmall); + *op++ = FSE_GETSYMBOL(&state2); + if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) { + *op++ = FSE_GETSYMBOL(&state1); + break; + } } + + return op-ostart; +} + + +size_t FSE_decompress_usingDTable(void* dst, size_t originalSize, + const void* cSrc, size_t cSrcSize, + const FSE_DTable* dt) +{ + const void* ptr = dt; + const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr; + const U32 fastMode = DTableH->fastMode; + + /* select fast mode (static) */ + if (fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1); + return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0); +} + + +size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog) +{ + const BYTE* const istart = (const BYTE*)cSrc; + const BYTE* ip = istart; + short counting[FSE_MAX_SYMBOL_VALUE+1]; + unsigned tableLog; + unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE; + + /* normal FSE decoding mode */ + size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize); + if (FSE_isError(NCountLength)) return NCountLength; + //if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */ + if (tableLog > maxLog) return ERROR(tableLog_tooLarge); + ip += NCountLength; + cSrcSize -= NCountLength; + + CHECK_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) ); + + return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace); /* always return, even if it is an error code */ +} + + +typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)]; + +size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize) +{ + DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */ + return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG); +} + + + +#endif /* FSE_COMMONDEFS_ONLY */ diff --git a/thirdparty/zstd/common/huf.h b/thirdparty/zstd/common/huf.h new file mode 100644 index 0000000000..7873ca3d42 --- /dev/null +++ b/thirdparty/zstd/common/huf.h @@ -0,0 +1,283 @@ +/* ****************************************************************** + Huffman coder, part of New Generation Entropy library + header file + Copyright (C) 2013-2016, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - Source repository : https://github.com/Cyan4973/FiniteStateEntropy +****************************************************************** */ +#ifndef HUF_H_298734234 +#define HUF_H_298734234 + +#if defined (__cplusplus) +extern "C" { +#endif + + +/* *** Dependencies *** */ +#include <stddef.h> /* size_t */ + + +/* *** library symbols visibility *** */ +/* Note : when linking with -fvisibility=hidden on gcc, or by default on Visual, + * HUF symbols remain "private" (internal symbols for library only). + * Set macro FSE_DLL_EXPORT to 1 if you want HUF symbols visible on DLL interface */ +#if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4) +# define HUF_PUBLIC_API __attribute__ ((visibility ("default"))) +#elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */ +# define HUF_PUBLIC_API __declspec(dllexport) +#elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1) +# define HUF_PUBLIC_API __declspec(dllimport) /* not required, just to generate faster code (saves a function pointer load from IAT and an indirect jump) */ +#else +# define HUF_PUBLIC_API +#endif + + +/* *** simple functions *** */ +/** +HUF_compress() : + Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'. + 'dst' buffer must be already allocated. + Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize). + `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB. + @return : size of compressed data (<= `dstCapacity`). + Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! + if return == 1, srcData is a single repeated byte symbol (RLE compression). + if HUF_isError(return), compression failed (more details using HUF_getErrorName()) +*/ +HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity, + const void* src, size_t srcSize); + +/** +HUF_decompress() : + Decompress HUF data from buffer 'cSrc', of size 'cSrcSize', + into already allocated buffer 'dst', of minimum size 'dstSize'. + `originalSize` : **must** be the ***exact*** size of original (uncompressed) data. + Note : in contrast with FSE, HUF_decompress can regenerate + RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data, + because it knows size to regenerate. + @return : size of regenerated data (== originalSize), + or an error code, which can be tested using HUF_isError() +*/ +HUF_PUBLIC_API size_t HUF_decompress(void* dst, size_t originalSize, + const void* cSrc, size_t cSrcSize); + + +/* *** Tool functions *** */ +#define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */ +HUF_PUBLIC_API size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */ + +/* Error Management */ +HUF_PUBLIC_API unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */ +HUF_PUBLIC_API const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */ + + +/* *** Advanced function *** */ + +/** HUF_compress2() : + * Same as HUF_compress(), but offers direct control over `maxSymbolValue` and `tableLog`. + * `tableLog` must be `<= HUF_TABLELOG_MAX` . */ +HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog); + +/** HUF_compress4X_wksp() : + * Same as HUF_compress2(), but uses externally allocated `workSpace`. + * `workspace` must have minimum alignment of 4, and be at least as large as following macro */ +#define HUF_WORKSPACE_SIZE (6 << 10) +#define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32)) +HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); + + + +/* ****************************************************************** + * WARNING !! + * The following section contains advanced and experimental definitions + * which shall never be used in the context of dll + * because they are not guaranteed to remain stable in the future. + * Only consider them in association with static linking. + *******************************************************************/ +#ifdef HUF_STATIC_LINKING_ONLY + +/* *** Dependencies *** */ +#include "mem.h" /* U32 */ + + +/* *** Constants *** */ +#define HUF_TABLELOG_MAX 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */ +#define HUF_TABLELOG_DEFAULT 11 /* tableLog by default, when not specified */ +#define HUF_SYMBOLVALUE_MAX 255 + +#define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */ +#if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX) +# error "HUF_TABLELOG_MAX is too large !" +#endif + + +/* **************************************** +* Static allocation +******************************************/ +/* HUF buffer bounds */ +#define HUF_CTABLEBOUND 129 +#define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true when incompressible is pre-filtered with fast heuristic */ +#define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ + +/* static allocation of HUF's Compression Table */ +#define HUF_CTABLE_SIZE_U32(maxSymbolValue) ((maxSymbolValue)+1) /* Use tables of U32, for proper alignment */ +#define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_U32(maxSymbolValue) * sizeof(U32)) +#define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \ + U32 name##hb[HUF_CTABLE_SIZE_U32(maxSymbolValue)]; \ + void* name##hv = &(name##hb); \ + HUF_CElt* name = (HUF_CElt*)(name##hv) /* no final ; */ + +/* static allocation of HUF's DTable */ +typedef U32 HUF_DTable; +#define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog))) +#define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \ + HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) } +#define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \ + HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) } + + +/* **************************************** +* Advanced decompression functions +******************************************/ +size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */ +size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */ + +size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< decodes RLE and uncompressed */ +size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */ +size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */ +size_t HUF_decompress4X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */ + + +/* **************************************** +* HUF detailed API +******************************************/ +/*! +HUF_compress() does the following: +1. count symbol occurrence from source[] into table count[] using FSE_count() +2. (optional) refine tableLog using HUF_optimalTableLog() +3. build Huffman table from count using HUF_buildCTable() +4. save Huffman table to memory buffer using HUF_writeCTable() +5. encode the data stream using HUF_compress4X_usingCTable() + +The following API allows targeting specific sub-functions for advanced tasks. +For example, it's possible to compress several blocks using the same 'CTable', +or to save and regenerate 'CTable' using external methods. +*/ +/* FSE_count() : find it within "fse.h" */ +unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue); +typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */ +size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits); +size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog); +size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable); + +typedef enum { + HUF_repeat_none, /**< Cannot use the previous table */ + HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */ + HUF_repeat_valid /**< Can use the previous table and it is asumed to be valid */ + } HUF_repeat; +/** HUF_compress4X_repeat() : +* Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. +* If it uses hufTable it does not modify hufTable or repeat. +* If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. +* If preferRepeat then the old table will always be used if valid. */ +size_t HUF_compress4X_repeat(void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize, HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */ + +/** HUF_buildCTable_wksp() : + * Same as HUF_buildCTable(), but using externally allocated scratch buffer. + * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned. + */ +size_t HUF_buildCTable_wksp (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize); + +/*! HUF_readStats() : + Read compact Huffman tree, saved by HUF_writeCTable(). + `huffWeight` is destination buffer. + @return : size read from `src` , or an error Code . + Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */ +size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, + U32* nbSymbolsPtr, U32* tableLogPtr, + const void* src, size_t srcSize); + +/** HUF_readCTable() : +* Loading a CTable saved with HUF_writeCTable() */ +size_t HUF_readCTable (HUF_CElt* CTable, unsigned maxSymbolValue, const void* src, size_t srcSize); + + +/* +HUF_decompress() does the following: +1. select the decompression algorithm (X2, X4) based on pre-computed heuristics +2. build Huffman table from save, using HUF_readDTableXn() +3. decode 1 or 4 segments in parallel using HUF_decompressSXn_usingDTable +*/ + +/** HUF_selectDecoder() : +* Tells which decoder is likely to decode faster, +* based on a set of pre-determined metrics. +* @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 . +* Assumption : 0 < cSrcSize < dstSize <= 128 KB */ +U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize); + +size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize); +size_t HUF_readDTableX4 (HUF_DTable* DTable, const void* src, size_t srcSize); + +size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); +size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); +size_t HUF_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); + + +/* single stream variants */ + +size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog); +size_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */ +size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable); +/** HUF_compress1X_repeat() : +* Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. +* If it uses hufTable it does not modify hufTable or repeat. +* If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. +* If preferRepeat then the old table will always be used if valid. */ +size_t HUF_compress1X_repeat(void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize, HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */ + +size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */ +size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */ + +size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); +size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */ +size_t HUF_decompress1X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */ + +size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */ +size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); +size_t HUF_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); + +#endif /* HUF_STATIC_LINKING_ONLY */ + + +#if defined (__cplusplus) +} +#endif + +#endif /* HUF_H_298734234 */ diff --git a/thirdparty/zstd/common/mem.h b/thirdparty/zstd/common/mem.h new file mode 100644 index 0000000000..4773a8b930 --- /dev/null +++ b/thirdparty/zstd/common/mem.h @@ -0,0 +1,373 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#ifndef MEM_H_MODULE +#define MEM_H_MODULE + +#if defined (__cplusplus) +extern "C" { +#endif + +/*-**************************************** +* Dependencies +******************************************/ +#include <stddef.h> /* size_t, ptrdiff_t */ +#include <string.h> /* memcpy */ + + +/*-**************************************** +* Compiler specifics +******************************************/ +#if defined(_MSC_VER) /* Visual Studio */ +# include <stdlib.h> /* _byteswap_ulong */ +# include <intrin.h> /* _byteswap_* */ +#endif +#if defined(__GNUC__) +# define MEM_STATIC static __inline __attribute__((unused)) +#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) +# define MEM_STATIC static inline +#elif defined(_MSC_VER) +# define MEM_STATIC static __inline +#else +# define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ +#endif + +/* code only tested on 32 and 64 bits systems */ +#define MEM_STATIC_ASSERT(c) { enum { MEM_static_assert = 1/(int)(!!(c)) }; } +MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); } + + +/*-************************************************************** +* Basic Types +*****************************************************************/ +#if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) +# include <stdint.h> + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef int16_t S16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; + typedef int64_t S64; + typedef intptr_t iPtrDiff; + typedef uintptr_t uPtrDiff; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef signed short S16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; + typedef signed long long S64; + typedef ptrdiff_t iPtrDiff; + typedef size_t uPtrDiff; +#endif + + +/*-************************************************************** +* Memory I/O +*****************************************************************/ +/* MEM_FORCE_MEMORY_ACCESS : + * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. + * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. + * The below switch allow to select different access method for improved performance. + * Method 0 (default) : use `memcpy()`. Safe and portable. + * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable). + * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`. + * Method 2 : direct access. This method is portable but violate C standard. + * It can generate buggy code on targets depending on alignment. + * In some circumstances, it's the only known way to get the most performance (i.e. GCC + ARMv6) + * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details. + * Prefer these methods in priority order (0 > 1 > 2) + */ +#ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */ +# if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) ) +# define MEM_FORCE_MEMORY_ACCESS 2 +# elif defined(__INTEL_COMPILER) || defined(__GNUC__) +# define MEM_FORCE_MEMORY_ACCESS 1 +# endif +#endif + +MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; } +MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; } + +MEM_STATIC unsigned MEM_isLittleEndian(void) +{ + const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ + return one.c[0]; +} + +#if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2) + +/* violates C standard, by lying on structure alignment. +Only use if no other choice to achieve best performance on target platform */ +MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; } +MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; } +MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; } +MEM_STATIC U64 MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; } + +MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } +MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } +MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; } + +#elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1) + +/* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */ +/* currently only defined for gcc and icc */ +#if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32)) + __pragma( pack(push, 1) ) + typedef union { U16 u16; U32 u32; U64 u64; size_t st; } unalign; + __pragma( pack(pop) ) +#else + typedef union { U16 u16; U32 u32; U64 u64; size_t st; } __attribute__((packed)) unalign; +#endif + +MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; } +MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; } +MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; } +MEM_STATIC U64 MEM_readST(const void* ptr) { return ((const unalign*)ptr)->st; } + +MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; } +MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; } +MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign*)memPtr)->u64 = value; } + +#else + +/* default method, safe and standard. + can sometimes prove slower */ + +MEM_STATIC U16 MEM_read16(const void* memPtr) +{ + U16 val; memcpy(&val, memPtr, sizeof(val)); return val; +} + +MEM_STATIC U32 MEM_read32(const void* memPtr) +{ + U32 val; memcpy(&val, memPtr, sizeof(val)); return val; +} + +MEM_STATIC U64 MEM_read64(const void* memPtr) +{ + U64 val; memcpy(&val, memPtr, sizeof(val)); return val; +} + +MEM_STATIC size_t MEM_readST(const void* memPtr) +{ + size_t val; memcpy(&val, memPtr, sizeof(val)); return val; +} + +MEM_STATIC void MEM_write16(void* memPtr, U16 value) +{ + memcpy(memPtr, &value, sizeof(value)); +} + +MEM_STATIC void MEM_write32(void* memPtr, U32 value) +{ + memcpy(memPtr, &value, sizeof(value)); +} + +MEM_STATIC void MEM_write64(void* memPtr, U64 value) +{ + memcpy(memPtr, &value, sizeof(value)); +} + +#endif /* MEM_FORCE_MEMORY_ACCESS */ + +MEM_STATIC U32 MEM_swap32(U32 in) +{ +#if defined(_MSC_VER) /* Visual Studio */ + return _byteswap_ulong(in); +#elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403) + return __builtin_bswap32(in); +#else + return ((in << 24) & 0xff000000 ) | + ((in << 8) & 0x00ff0000 ) | + ((in >> 8) & 0x0000ff00 ) | + ((in >> 24) & 0x000000ff ); +#endif +} + +MEM_STATIC U64 MEM_swap64(U64 in) +{ +#if defined(_MSC_VER) /* Visual Studio */ + return _byteswap_uint64(in); +#elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403) + return __builtin_bswap64(in); +#else + return ((in << 56) & 0xff00000000000000ULL) | + ((in << 40) & 0x00ff000000000000ULL) | + ((in << 24) & 0x0000ff0000000000ULL) | + ((in << 8) & 0x000000ff00000000ULL) | + ((in >> 8) & 0x00000000ff000000ULL) | + ((in >> 24) & 0x0000000000ff0000ULL) | + ((in >> 40) & 0x000000000000ff00ULL) | + ((in >> 56) & 0x00000000000000ffULL); +#endif +} + +MEM_STATIC size_t MEM_swapST(size_t in) +{ + if (MEM_32bits()) + return (size_t)MEM_swap32((U32)in); + else + return (size_t)MEM_swap64((U64)in); +} + +/*=== Little endian r/w ===*/ + +MEM_STATIC U16 MEM_readLE16(const void* memPtr) +{ + if (MEM_isLittleEndian()) + return MEM_read16(memPtr); + else { + const BYTE* p = (const BYTE*)memPtr; + return (U16)(p[0] + (p[1]<<8)); + } +} + +MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val) +{ + if (MEM_isLittleEndian()) { + MEM_write16(memPtr, val); + } else { + BYTE* p = (BYTE*)memPtr; + p[0] = (BYTE)val; + p[1] = (BYTE)(val>>8); + } +} + +MEM_STATIC U32 MEM_readLE24(const void* memPtr) +{ + return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16); +} + +MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val) +{ + MEM_writeLE16(memPtr, (U16)val); + ((BYTE*)memPtr)[2] = (BYTE)(val>>16); +} + +MEM_STATIC U32 MEM_readLE32(const void* memPtr) +{ + if (MEM_isLittleEndian()) + return MEM_read32(memPtr); + else + return MEM_swap32(MEM_read32(memPtr)); +} + +MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32) +{ + if (MEM_isLittleEndian()) + MEM_write32(memPtr, val32); + else + MEM_write32(memPtr, MEM_swap32(val32)); +} + +MEM_STATIC U64 MEM_readLE64(const void* memPtr) +{ + if (MEM_isLittleEndian()) + return MEM_read64(memPtr); + else + return MEM_swap64(MEM_read64(memPtr)); +} + +MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64) +{ + if (MEM_isLittleEndian()) + MEM_write64(memPtr, val64); + else + MEM_write64(memPtr, MEM_swap64(val64)); +} + +MEM_STATIC size_t MEM_readLEST(const void* memPtr) +{ + if (MEM_32bits()) + return (size_t)MEM_readLE32(memPtr); + else + return (size_t)MEM_readLE64(memPtr); +} + +MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val) +{ + if (MEM_32bits()) + MEM_writeLE32(memPtr, (U32)val); + else + MEM_writeLE64(memPtr, (U64)val); +} + +/*=== Big endian r/w ===*/ + +MEM_STATIC U32 MEM_readBE32(const void* memPtr) +{ + if (MEM_isLittleEndian()) + return MEM_swap32(MEM_read32(memPtr)); + else + return MEM_read32(memPtr); +} + +MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32) +{ + if (MEM_isLittleEndian()) + MEM_write32(memPtr, MEM_swap32(val32)); + else + MEM_write32(memPtr, val32); +} + +MEM_STATIC U64 MEM_readBE64(const void* memPtr) +{ + if (MEM_isLittleEndian()) + return MEM_swap64(MEM_read64(memPtr)); + else + return MEM_read64(memPtr); +} + +MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64) +{ + if (MEM_isLittleEndian()) + MEM_write64(memPtr, MEM_swap64(val64)); + else + MEM_write64(memPtr, val64); +} + +MEM_STATIC size_t MEM_readBEST(const void* memPtr) +{ + if (MEM_32bits()) + return (size_t)MEM_readBE32(memPtr); + else + return (size_t)MEM_readBE64(memPtr); +} + +MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val) +{ + if (MEM_32bits()) + MEM_writeBE32(memPtr, (U32)val); + else + MEM_writeBE64(memPtr, (U64)val); +} + + +/* function safe only for comparisons */ +MEM_STATIC U32 MEM_readMINMATCH(const void* memPtr, U32 length) +{ + switch (length) + { + default : + case 4 : return MEM_read32(memPtr); + case 3 : if (MEM_isLittleEndian()) + return MEM_read32(memPtr)<<8; + else + return MEM_read32(memPtr)>>8; + } +} + +#if defined (__cplusplus) +} +#endif + +#endif /* MEM_H_MODULE */ diff --git a/thirdparty/zstd/common/pool.c b/thirdparty/zstd/common/pool.c new file mode 100644 index 0000000000..e439fe1b0d --- /dev/null +++ b/thirdparty/zstd/common/pool.c @@ -0,0 +1,194 @@ +/** + * Copyright (c) 2016-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + + +/* ====== Dependencies ======= */ +#include <stddef.h> /* size_t */ +#include <stdlib.h> /* malloc, calloc, free */ +#include "pool.h" + +/* ====== Compiler specifics ====== */ +#if defined(_MSC_VER) +# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ +#endif + + +#ifdef ZSTD_MULTITHREAD + +#include "threading.h" /* pthread adaptation */ + +/* A job is a function and an opaque argument */ +typedef struct POOL_job_s { + POOL_function function; + void *opaque; +} POOL_job; + +struct POOL_ctx_s { + /* Keep track of the threads */ + pthread_t *threads; + size_t numThreads; + + /* The queue is a circular buffer */ + POOL_job *queue; + size_t queueHead; + size_t queueTail; + size_t queueSize; + /* The mutex protects the queue */ + pthread_mutex_t queueMutex; + /* Condition variable for pushers to wait on when the queue is full */ + pthread_cond_t queuePushCond; + /* Condition variables for poppers to wait on when the queue is empty */ + pthread_cond_t queuePopCond; + /* Indicates if the queue is shutting down */ + int shutdown; +}; + +/* POOL_thread() : + Work thread for the thread pool. + Waits for jobs and executes them. + @returns : NULL on failure else non-null. +*/ +static void* POOL_thread(void* opaque) { + POOL_ctx* const ctx = (POOL_ctx*)opaque; + if (!ctx) { return NULL; } + for (;;) { + /* Lock the mutex and wait for a non-empty queue or until shutdown */ + pthread_mutex_lock(&ctx->queueMutex); + while (ctx->queueHead == ctx->queueTail && !ctx->shutdown) { + pthread_cond_wait(&ctx->queuePopCond, &ctx->queueMutex); + } + /* empty => shutting down: so stop */ + if (ctx->queueHead == ctx->queueTail) { + pthread_mutex_unlock(&ctx->queueMutex); + return opaque; + } + /* Pop a job off the queue */ + { POOL_job const job = ctx->queue[ctx->queueHead]; + ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize; + /* Unlock the mutex, signal a pusher, and run the job */ + pthread_mutex_unlock(&ctx->queueMutex); + pthread_cond_signal(&ctx->queuePushCond); + job.function(job.opaque); + } + } + /* Unreachable */ +} + +POOL_ctx *POOL_create(size_t numThreads, size_t queueSize) { + POOL_ctx *ctx; + /* Check the parameters */ + if (!numThreads || !queueSize) { return NULL; } + /* Allocate the context and zero initialize */ + ctx = (POOL_ctx *)calloc(1, sizeof(POOL_ctx)); + if (!ctx) { return NULL; } + /* Initialize the job queue. + * It needs one extra space since one space is wasted to differentiate empty + * and full queues. + */ + ctx->queueSize = queueSize + 1; + ctx->queue = (POOL_job *)malloc(ctx->queueSize * sizeof(POOL_job)); + ctx->queueHead = 0; + ctx->queueTail = 0; + pthread_mutex_init(&ctx->queueMutex, NULL); + pthread_cond_init(&ctx->queuePushCond, NULL); + pthread_cond_init(&ctx->queuePopCond, NULL); + ctx->shutdown = 0; + /* Allocate space for the thread handles */ + ctx->threads = (pthread_t *)malloc(numThreads * sizeof(pthread_t)); + ctx->numThreads = 0; + /* Check for errors */ + if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; } + /* Initialize the threads */ + { size_t i; + for (i = 0; i < numThreads; ++i) { + if (pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) { + ctx->numThreads = i; + POOL_free(ctx); + return NULL; + } } + ctx->numThreads = numThreads; + } + return ctx; +} + +/*! POOL_join() : + Shutdown the queue, wake any sleeping threads, and join all of the threads. +*/ +static void POOL_join(POOL_ctx *ctx) { + /* Shut down the queue */ + pthread_mutex_lock(&ctx->queueMutex); + ctx->shutdown = 1; + pthread_mutex_unlock(&ctx->queueMutex); + /* Wake up sleeping threads */ + pthread_cond_broadcast(&ctx->queuePushCond); + pthread_cond_broadcast(&ctx->queuePopCond); + /* Join all of the threads */ + { size_t i; + for (i = 0; i < ctx->numThreads; ++i) { + pthread_join(ctx->threads[i], NULL); + } } +} + +void POOL_free(POOL_ctx *ctx) { + if (!ctx) { return; } + POOL_join(ctx); + pthread_mutex_destroy(&ctx->queueMutex); + pthread_cond_destroy(&ctx->queuePushCond); + pthread_cond_destroy(&ctx->queuePopCond); + if (ctx->queue) free(ctx->queue); + if (ctx->threads) free(ctx->threads); + free(ctx); +} + +void POOL_add(void *ctxVoid, POOL_function function, void *opaque) { + POOL_ctx *ctx = (POOL_ctx *)ctxVoid; + if (!ctx) { return; } + + pthread_mutex_lock(&ctx->queueMutex); + { POOL_job const job = {function, opaque}; + /* Wait until there is space in the queue for the new job */ + size_t newTail = (ctx->queueTail + 1) % ctx->queueSize; + while (ctx->queueHead == newTail && !ctx->shutdown) { + pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex); + newTail = (ctx->queueTail + 1) % ctx->queueSize; + } + /* The queue is still going => there is space */ + if (!ctx->shutdown) { + ctx->queue[ctx->queueTail] = job; + ctx->queueTail = newTail; + } + } + pthread_mutex_unlock(&ctx->queueMutex); + pthread_cond_signal(&ctx->queuePopCond); +} + +#else /* ZSTD_MULTITHREAD not defined */ +/* No multi-threading support */ + +/* We don't need any data, but if it is empty malloc() might return NULL. */ +struct POOL_ctx_s { + int data; +}; + +POOL_ctx *POOL_create(size_t numThreads, size_t queueSize) { + (void)numThreads; + (void)queueSize; + return (POOL_ctx *)malloc(sizeof(POOL_ctx)); +} + +void POOL_free(POOL_ctx *ctx) { + if (ctx) free(ctx); +} + +void POOL_add(void *ctx, POOL_function function, void *opaque) { + (void)ctx; + function(opaque); +} + +#endif /* ZSTD_MULTITHREAD */ diff --git a/thirdparty/zstd/common/pool.h b/thirdparty/zstd/common/pool.h new file mode 100644 index 0000000000..50cb25b12c --- /dev/null +++ b/thirdparty/zstd/common/pool.h @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2016-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +#ifndef POOL_H +#define POOL_H + +#if defined (__cplusplus) +extern "C" { +#endif + + +#include <stddef.h> /* size_t */ + +typedef struct POOL_ctx_s POOL_ctx; + +/*! POOL_create() : + Create a thread pool with at most `numThreads` threads. + `numThreads` must be at least 1. + The maximum number of queued jobs before blocking is `queueSize`. + `queueSize` must be at least 1. + @return : The POOL_ctx pointer on success else NULL. +*/ +POOL_ctx *POOL_create(size_t numThreads, size_t queueSize); + +/*! POOL_free() : + Free a thread pool returned by POOL_create(). +*/ +void POOL_free(POOL_ctx *ctx); + +/*! POOL_function : + The function type that can be added to a thread pool. +*/ +typedef void (*POOL_function)(void *); +/*! POOL_add_function : + The function type for a generic thread pool add function. +*/ +typedef void (*POOL_add_function)(void *, POOL_function, void *); + +/*! POOL_add() : + Add the job `function(opaque)` to the thread pool. + Possibly blocks until there is room in the queue. + Note : The function may be executed asynchronously, so `opaque` must live until the function has been completed. +*/ +void POOL_add(void *ctx, POOL_function function, void *opaque); + + +#if defined (__cplusplus) +} +#endif + +#endif diff --git a/thirdparty/zstd/common/threading.c b/thirdparty/zstd/common/threading.c new file mode 100644 index 0000000000..32d58796a9 --- /dev/null +++ b/thirdparty/zstd/common/threading.c @@ -0,0 +1,80 @@ + +/** + * Copyright (c) 2016 Tino Reichardt + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * You can contact the author at: + * - zstdmt source repository: https://github.com/mcmilk/zstdmt + */ + +/** + * This file will hold wrapper for systems, which do not support pthreads + */ + +/* When ZSTD_MULTITHREAD is not defined, this file would become an empty translation unit. +* Include some ISO C header code to prevent this and portably avoid related warnings. +* (Visual C++: C4206 / GCC: -Wpedantic / Clang: -Wempty-translation-unit) +*/ +#include <stddef.h> + + +#if defined(ZSTD_MULTITHREAD) && defined(_WIN32) + +/** + * Windows minimalist Pthread Wrapper, based on : + * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html + */ + + +/* === Dependencies === */ +#include <process.h> +#include <errno.h> +#include "threading.h" + + +/* === Implementation === */ + +static unsigned __stdcall worker(void *arg) +{ + pthread_t* const thread = (pthread_t*) arg; + thread->arg = thread->start_routine(thread->arg); + return 0; +} + +int pthread_create(pthread_t* thread, const void* unused, + void* (*start_routine) (void*), void* arg) +{ + (void)unused; + thread->arg = arg; + thread->start_routine = start_routine; + thread->handle = (HANDLE) _beginthreadex(NULL, 0, worker, thread, 0, NULL); + + if (!thread->handle) + return errno; + else + return 0; +} + +int _pthread_join(pthread_t * thread, void **value_ptr) +{ + DWORD result; + + if (!thread->handle) return 0; + + result = WaitForSingleObject(thread->handle, INFINITE); + switch (result) { + case WAIT_OBJECT_0: + if (value_ptr) *value_ptr = thread->arg; + return 0; + case WAIT_ABANDONED: + return EINVAL; + default: + return GetLastError(); + } +} + +#endif /* ZSTD_MULTITHREAD */ diff --git a/thirdparty/zstd/common/threading.h b/thirdparty/zstd/common/threading.h new file mode 100644 index 0000000000..c0086139ea --- /dev/null +++ b/thirdparty/zstd/common/threading.h @@ -0,0 +1,104 @@ + +/** + * Copyright (c) 2016 Tino Reichardt + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * You can contact the author at: + * - zstdmt source repository: https://github.com/mcmilk/zstdmt + */ + +#ifndef THREADING_H_938743 +#define THREADING_H_938743 + +#if defined (__cplusplus) +extern "C" { +#endif + +#if defined(ZSTD_MULTITHREAD) && defined(_WIN32) + +/** + * Windows minimalist Pthread Wrapper, based on : + * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html + */ +#ifdef WINVER +# undef WINVER +#endif +#define WINVER 0x0600 + +#ifdef _WIN32_WINNT +# undef _WIN32_WINNT +#endif +#define _WIN32_WINNT 0x0600 + +#ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +#endif + +#include <windows.h> + +/* mutex */ +#define pthread_mutex_t CRITICAL_SECTION +#define pthread_mutex_init(a,b) InitializeCriticalSection((a)) +#define pthread_mutex_destroy(a) DeleteCriticalSection((a)) +#define pthread_mutex_lock(a) EnterCriticalSection((a)) +#define pthread_mutex_unlock(a) LeaveCriticalSection((a)) + +/* condition variable */ +#define pthread_cond_t CONDITION_VARIABLE +#define pthread_cond_init(a, b) InitializeConditionVariable((a)) +#define pthread_cond_destroy(a) /* No delete */ +#define pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE) +#define pthread_cond_signal(a) WakeConditionVariable((a)) +#define pthread_cond_broadcast(a) WakeAllConditionVariable((a)) + +/* pthread_create() and pthread_join() */ +typedef struct { + HANDLE handle; + void* (*start_routine)(void*); + void* arg; +} pthread_t; + +int pthread_create(pthread_t* thread, const void* unused, + void* (*start_routine) (void*), void* arg); + +#define pthread_join(a, b) _pthread_join(&(a), (b)) +int _pthread_join(pthread_t* thread, void** value_ptr); + +/** + * add here more wrappers as required + */ + + +#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */ +/* === POSIX Systems === */ +# include <pthread.h> + +#else /* ZSTD_MULTITHREAD not defined */ +/* No multithreading support */ + +#define pthread_mutex_t int /* #define rather than typedef, as sometimes pthread support is implicit, resulting in duplicated symbols */ +#define pthread_mutex_init(a,b) +#define pthread_mutex_destroy(a) +#define pthread_mutex_lock(a) +#define pthread_mutex_unlock(a) + +#define pthread_cond_t int +#define pthread_cond_init(a,b) +#define pthread_cond_destroy(a) +#define pthread_cond_wait(a,b) +#define pthread_cond_signal(a) +#define pthread_cond_broadcast(a) + +/* do not use pthread_t */ + +#endif /* ZSTD_MULTITHREAD */ + +#if defined (__cplusplus) +} +#endif + +#endif /* THREADING_H_938743 */ diff --git a/thirdparty/zstd/common/xxhash.c b/thirdparty/zstd/common/xxhash.c new file mode 100644 index 0000000000..eb44222c5f --- /dev/null +++ b/thirdparty/zstd/common/xxhash.c @@ -0,0 +1,869 @@ +/* +* xxHash - Fast Hash algorithm +* Copyright (C) 2012-2016, Yann Collet +* +* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following disclaimer +* in the documentation and/or other materials provided with the +* distribution. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +* You can contact the author at : +* - xxHash homepage: http://www.xxhash.com +* - xxHash source repository : https://github.com/Cyan4973/xxHash +*/ + + +/* ************************************* +* Tuning parameters +***************************************/ +/*!XXH_FORCE_MEMORY_ACCESS : + * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. + * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. + * The below switch allow to select different access method for improved performance. + * Method 0 (default) : use `memcpy()`. Safe and portable. + * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable). + * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`. + * Method 2 : direct access. This method doesn't depend on compiler but violate C standard. + * It can generate buggy code on targets which do not support unaligned memory accesses. + * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6) + * See http://stackoverflow.com/a/32095106/646947 for details. + * Prefer these methods in priority order (0 > 1 > 2) + */ +#ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */ +# if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) ) +# define XXH_FORCE_MEMORY_ACCESS 2 +# elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \ + (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) )) +# define XXH_FORCE_MEMORY_ACCESS 1 +# endif +#endif + +/*!XXH_ACCEPT_NULL_INPUT_POINTER : + * If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer. + * When this option is enabled, xxHash output for null input pointers will be the same as a null-length input. + * By default, this option is disabled. To enable it, uncomment below define : + */ +/* #define XXH_ACCEPT_NULL_INPUT_POINTER 1 */ + +/*!XXH_FORCE_NATIVE_FORMAT : + * By default, xxHash library provides endian-independant Hash values, based on little-endian convention. + * Results are therefore identical for little-endian and big-endian CPU. + * This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format. + * Should endian-independance be of no importance for your application, you may set the #define below to 1, + * to improve speed for Big-endian CPU. + * This option has no impact on Little_Endian CPU. + */ +#ifndef XXH_FORCE_NATIVE_FORMAT /* can be defined externally */ +# define XXH_FORCE_NATIVE_FORMAT 0 +#endif + +/*!XXH_FORCE_ALIGN_CHECK : + * This is a minor performance trick, only useful with lots of very small keys. + * It means : check for aligned/unaligned input. + * The check costs one initial branch per hash; set to 0 when the input data + * is guaranteed to be aligned. + */ +#ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */ +# if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) +# define XXH_FORCE_ALIGN_CHECK 0 +# else +# define XXH_FORCE_ALIGN_CHECK 1 +# endif +#endif + + +/* ************************************* +* Includes & Memory related functions +***************************************/ +/* Modify the local functions below should you wish to use some other memory routines */ +/* for malloc(), free() */ +#include <stdlib.h> +static void* XXH_malloc(size_t s) { return malloc(s); } +static void XXH_free (void* p) { free(p); } +/* for memcpy() */ +#include <string.h> +static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); } + +#ifndef XXH_STATIC_LINKING_ONLY +# define XXH_STATIC_LINKING_ONLY +#endif +#include "xxhash.h" + + +/* ************************************* +* Compiler Specific Options +***************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# define FORCE_INLINE static __forceinline +#else +# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +# else +# define FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +#endif + + +/* ************************************* +* Basic Types +***************************************/ +#ifndef MEM_MODULE +# define MEM_MODULE +# if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) +# include <stdint.h> + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +# else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; /* if your compiler doesn't support unsigned long long, replace by another 64-bit type here. Note that xxhash.h will also need to be updated. */ +# endif +#endif + + +#if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2)) + +/* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */ +static U32 XXH_read32(const void* memPtr) { return *(const U32*) memPtr; } +static U64 XXH_read64(const void* memPtr) { return *(const U64*) memPtr; } + +#elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1)) + +/* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */ +/* currently only defined for gcc and icc */ +typedef union { U32 u32; U64 u64; } __attribute__((packed)) unalign; + +static U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; } +static U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; } + +#else + +/* portable and safe solution. Generally efficient. + * see : http://stackoverflow.com/a/32095106/646947 + */ + +static U32 XXH_read32(const void* memPtr) +{ + U32 val; + memcpy(&val, memPtr, sizeof(val)); + return val; +} + +static U64 XXH_read64(const void* memPtr) +{ + U64 val; + memcpy(&val, memPtr, sizeof(val)); + return val; +} + +#endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */ + + +/* **************************************** +* Compiler-specific Functions and Macros +******************************************/ +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) + +/* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */ +#if defined(_MSC_VER) +# define XXH_rotl32(x,r) _rotl(x,r) +# define XXH_rotl64(x,r) _rotl64(x,r) +#else +# define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r))) +# define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r))) +#endif + +#if defined(_MSC_VER) /* Visual Studio */ +# define XXH_swap32 _byteswap_ulong +# define XXH_swap64 _byteswap_uint64 +#elif GCC_VERSION >= 403 +# define XXH_swap32 __builtin_bswap32 +# define XXH_swap64 __builtin_bswap64 +#else +static U32 XXH_swap32 (U32 x) +{ + return ((x << 24) & 0xff000000 ) | + ((x << 8) & 0x00ff0000 ) | + ((x >> 8) & 0x0000ff00 ) | + ((x >> 24) & 0x000000ff ); +} +static U64 XXH_swap64 (U64 x) +{ + return ((x << 56) & 0xff00000000000000ULL) | + ((x << 40) & 0x00ff000000000000ULL) | + ((x << 24) & 0x0000ff0000000000ULL) | + ((x << 8) & 0x000000ff00000000ULL) | + ((x >> 8) & 0x00000000ff000000ULL) | + ((x >> 24) & 0x0000000000ff0000ULL) | + ((x >> 40) & 0x000000000000ff00ULL) | + ((x >> 56) & 0x00000000000000ffULL); +} +#endif + + +/* ************************************* +* Architecture Macros +***************************************/ +typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess; + +/* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */ +#ifndef XXH_CPU_LITTLE_ENDIAN + static const int g_one = 1; +# define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&g_one)) +#endif + + +/* *************************** +* Memory reads +*****************************/ +typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment; + +FORCE_INLINE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align) +{ + if (align==XXH_unaligned) + return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr)); + else + return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr); +} + +FORCE_INLINE U32 XXH_readLE32(const void* ptr, XXH_endianess endian) +{ + return XXH_readLE32_align(ptr, endian, XXH_unaligned); +} + +static U32 XXH_readBE32(const void* ptr) +{ + return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr); +} + +FORCE_INLINE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align) +{ + if (align==XXH_unaligned) + return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr)); + else + return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr); +} + +FORCE_INLINE U64 XXH_readLE64(const void* ptr, XXH_endianess endian) +{ + return XXH_readLE64_align(ptr, endian, XXH_unaligned); +} + +static U64 XXH_readBE64(const void* ptr) +{ + return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr); +} + + +/* ************************************* +* Macros +***************************************/ +#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ + + +/* ************************************* +* Constants +***************************************/ +static const U32 PRIME32_1 = 2654435761U; +static const U32 PRIME32_2 = 2246822519U; +static const U32 PRIME32_3 = 3266489917U; +static const U32 PRIME32_4 = 668265263U; +static const U32 PRIME32_5 = 374761393U; + +static const U64 PRIME64_1 = 11400714785074694791ULL; +static const U64 PRIME64_2 = 14029467366897019727ULL; +static const U64 PRIME64_3 = 1609587929392839161ULL; +static const U64 PRIME64_4 = 9650029242287828579ULL; +static const U64 PRIME64_5 = 2870177450012600261ULL; + +XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; } + + +/* ************************** +* Utils +****************************/ +XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dstState, const XXH32_state_t* restrict srcState) +{ + memcpy(dstState, srcState, sizeof(*dstState)); +} + +XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dstState, const XXH64_state_t* restrict srcState) +{ + memcpy(dstState, srcState, sizeof(*dstState)); +} + + +/* *************************** +* Simple Hash Functions +*****************************/ + +static U32 XXH32_round(U32 seed, U32 input) +{ + seed += input * PRIME32_2; + seed = XXH_rotl32(seed, 13); + seed *= PRIME32_1; + return seed; +} + +FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align) +{ + const BYTE* p = (const BYTE*)input; + const BYTE* bEnd = p + len; + U32 h32; +#define XXH_get32bits(p) XXH_readLE32_align(p, endian, align) + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (p==NULL) { + len=0; + bEnd=p=(const BYTE*)(size_t)16; + } +#endif + + if (len>=16) { + const BYTE* const limit = bEnd - 16; + U32 v1 = seed + PRIME32_1 + PRIME32_2; + U32 v2 = seed + PRIME32_2; + U32 v3 = seed + 0; + U32 v4 = seed - PRIME32_1; + + do { + v1 = XXH32_round(v1, XXH_get32bits(p)); p+=4; + v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4; + v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4; + v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4; + } while (p<=limit); + + h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); + } else { + h32 = seed + PRIME32_5; + } + + h32 += (U32) len; + + while (p+4<=bEnd) { + h32 += XXH_get32bits(p) * PRIME32_3; + h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; + p+=4; + } + + while (p<bEnd) { + h32 += (*p) * PRIME32_5; + h32 = XXH_rotl32(h32, 11) * PRIME32_1 ; + p++; + } + + h32 ^= h32 >> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + + return h32; +} + + +XXH_PUBLIC_API unsigned int XXH32 (const void* input, size_t len, unsigned int seed) +{ +#if 0 + /* Simple version, good for code maintenance, but unfortunately slow for small inputs */ + XXH32_CREATESTATE_STATIC(state); + XXH32_reset(state, seed); + XXH32_update(state, input, len); + return XXH32_digest(state); +#else + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if (XXH_FORCE_ALIGN_CHECK) { + if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */ + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); + else + return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); + } } + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); + else + return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); +#endif +} + + +static U64 XXH64_round(U64 acc, U64 input) +{ + acc += input * PRIME64_2; + acc = XXH_rotl64(acc, 31); + acc *= PRIME64_1; + return acc; +} + +static U64 XXH64_mergeRound(U64 acc, U64 val) +{ + val = XXH64_round(0, val); + acc ^= val; + acc = acc * PRIME64_1 + PRIME64_4; + return acc; +} + +FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align) +{ + const BYTE* p = (const BYTE*)input; + const BYTE* const bEnd = p + len; + U64 h64; +#define XXH_get64bits(p) XXH_readLE64_align(p, endian, align) + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (p==NULL) { + len=0; + bEnd=p=(const BYTE*)(size_t)32; + } +#endif + + if (len>=32) { + const BYTE* const limit = bEnd - 32; + U64 v1 = seed + PRIME64_1 + PRIME64_2; + U64 v2 = seed + PRIME64_2; + U64 v3 = seed + 0; + U64 v4 = seed - PRIME64_1; + + do { + v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8; + v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8; + v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8; + v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8; + } while (p<=limit); + + h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); + h64 = XXH64_mergeRound(h64, v1); + h64 = XXH64_mergeRound(h64, v2); + h64 = XXH64_mergeRound(h64, v3); + h64 = XXH64_mergeRound(h64, v4); + + } else { + h64 = seed + PRIME64_5; + } + + h64 += (U64) len; + + while (p+8<=bEnd) { + U64 const k1 = XXH64_round(0, XXH_get64bits(p)); + h64 ^= k1; + h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; + p+=8; + } + + if (p+4<=bEnd) { + h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1; + h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; + p+=4; + } + + while (p<bEnd) { + h64 ^= (*p) * PRIME64_5; + h64 = XXH_rotl64(h64, 11) * PRIME64_1; + p++; + } + + h64 ^= h64 >> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + + return h64; +} + + +XXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed) +{ +#if 0 + /* Simple version, good for code maintenance, but unfortunately slow for small inputs */ + XXH64_CREATESTATE_STATIC(state); + XXH64_reset(state, seed); + XXH64_update(state, input, len); + return XXH64_digest(state); +#else + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if (XXH_FORCE_ALIGN_CHECK) { + if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */ + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); + else + return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); + } } + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); + else + return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); +#endif +} + + +/* ************************************************** +* Advanced Hash Functions +****************************************************/ + +XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void) +{ + return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t)); +} +XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr) +{ + XXH_free(statePtr); + return XXH_OK; +} + +XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void) +{ + return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t)); +} +XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr) +{ + XXH_free(statePtr); + return XXH_OK; +} + + +/*** Hash feed ***/ + +XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed) +{ + XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */ + memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */ + state.v1 = seed + PRIME32_1 + PRIME32_2; + state.v2 = seed + PRIME32_2; + state.v3 = seed + 0; + state.v4 = seed - PRIME32_1; + memcpy(statePtr, &state, sizeof(state)); + return XXH_OK; +} + + +XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed) +{ + XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */ + memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */ + state.v1 = seed + PRIME64_1 + PRIME64_2; + state.v2 = seed + PRIME64_2; + state.v3 = seed + 0; + state.v4 = seed - PRIME64_1; + memcpy(statePtr, &state, sizeof(state)); + return XXH_OK; +} + + +FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian) +{ + const BYTE* p = (const BYTE*)input; + const BYTE* const bEnd = p + len; + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (input==NULL) return XXH_ERROR; +#endif + + state->total_len_32 += (unsigned)len; + state->large_len |= (len>=16) | (state->total_len_32>=16); + + if (state->memsize + len < 16) { /* fill in tmp buffer */ + XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len); + state->memsize += (unsigned)len; + return XXH_OK; + } + + if (state->memsize) { /* some data left from previous update */ + XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize); + { const U32* p32 = state->mem32; + state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++; + state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++; + state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++; + state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); p32++; + } + p += 16-state->memsize; + state->memsize = 0; + } + + if (p <= bEnd-16) { + const BYTE* const limit = bEnd - 16; + U32 v1 = state->v1; + U32 v2 = state->v2; + U32 v3 = state->v3; + U32 v4 = state->v4; + + do { + v1 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4; + v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4; + v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4; + v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4; + } while (p<=limit); + + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } + + if (p < bEnd) { + XXH_memcpy(state->mem32, p, (size_t)(bEnd-p)); + state->memsize = (unsigned)(bEnd-p); + } + + return XXH_OK; +} + +XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_update_endian(state_in, input, len, XXH_littleEndian); + else + return XXH32_update_endian(state_in, input, len, XXH_bigEndian); +} + + + +FORCE_INLINE U32 XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian) +{ + const BYTE * p = (const BYTE*)state->mem32; + const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize; + U32 h32; + + if (state->large_len) { + h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); + } else { + h32 = state->v3 /* == seed */ + PRIME32_5; + } + + h32 += state->total_len_32; + + while (p+4<=bEnd) { + h32 += XXH_readLE32(p, endian) * PRIME32_3; + h32 = XXH_rotl32(h32, 17) * PRIME32_4; + p+=4; + } + + while (p<bEnd) { + h32 += (*p) * PRIME32_5; + h32 = XXH_rotl32(h32, 11) * PRIME32_1; + p++; + } + + h32 ^= h32 >> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + + return h32; +} + + +XXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_digest_endian(state_in, XXH_littleEndian); + else + return XXH32_digest_endian(state_in, XXH_bigEndian); +} + + + +/* **** XXH64 **** */ + +FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian) +{ + const BYTE* p = (const BYTE*)input; + const BYTE* const bEnd = p + len; + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (input==NULL) return XXH_ERROR; +#endif + + state->total_len += len; + + if (state->memsize + len < 32) { /* fill in tmp buffer */ + XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len); + state->memsize += (U32)len; + return XXH_OK; + } + + if (state->memsize) { /* tmp buffer is full */ + XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize); + state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian)); + state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian)); + state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian)); + state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian)); + p += 32-state->memsize; + state->memsize = 0; + } + + if (p+32 <= bEnd) { + const BYTE* const limit = bEnd - 32; + U64 v1 = state->v1; + U64 v2 = state->v2; + U64 v3 = state->v3; + U64 v4 = state->v4; + + do { + v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8; + v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8; + v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8; + v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8; + } while (p<=limit); + + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } + + if (p < bEnd) { + XXH_memcpy(state->mem64, p, (size_t)(bEnd-p)); + state->memsize = (unsigned)(bEnd-p); + } + + return XXH_OK; +} + +XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_update_endian(state_in, input, len, XXH_littleEndian); + else + return XXH64_update_endian(state_in, input, len, XXH_bigEndian); +} + + + +FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian) +{ + const BYTE * p = (const BYTE*)state->mem64; + const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize; + U64 h64; + + if (state->total_len >= 32) { + U64 const v1 = state->v1; + U64 const v2 = state->v2; + U64 const v3 = state->v3; + U64 const v4 = state->v4; + + h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); + h64 = XXH64_mergeRound(h64, v1); + h64 = XXH64_mergeRound(h64, v2); + h64 = XXH64_mergeRound(h64, v3); + h64 = XXH64_mergeRound(h64, v4); + } else { + h64 = state->v3 + PRIME64_5; + } + + h64 += (U64) state->total_len; + + while (p+8<=bEnd) { + U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian)); + h64 ^= k1; + h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; + p+=8; + } + + if (p+4<=bEnd) { + h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1; + h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; + p+=4; + } + + while (p<bEnd) { + h64 ^= (*p) * PRIME64_5; + h64 = XXH_rotl64(h64, 11) * PRIME64_1; + p++; + } + + h64 ^= h64 >> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + + return h64; +} + + +XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_digest_endian(state_in, XXH_littleEndian); + else + return XXH64_digest_endian(state_in, XXH_bigEndian); +} + + +/* ************************** +* Canonical representation +****************************/ + +/*! Default XXH result types are basic unsigned 32 and 64 bits. +* The canonical representation follows human-readable write convention, aka big-endian (large digits first). +* These functions allow transformation of hash result into and from its canonical format. +* This way, hash values can be written into a file or buffer, and remain comparable across different systems and programs. +*/ + +XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash) +{ + XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t)); + if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash); + memcpy(dst, &hash, sizeof(*dst)); +} + +XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash) +{ + XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t)); + if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash); + memcpy(dst, &hash, sizeof(*dst)); +} + +XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src) +{ + return XXH_readBE32(src); +} + +XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src) +{ + return XXH_readBE64(src); +} diff --git a/thirdparty/zstd/common/xxhash.h b/thirdparty/zstd/common/xxhash.h new file mode 100644 index 0000000000..9bad1f59f6 --- /dev/null +++ b/thirdparty/zstd/common/xxhash.h @@ -0,0 +1,305 @@ +/* + xxHash - Extremely Fast Hash algorithm + Header File + Copyright (C) 2012-2016, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - xxHash source repository : https://github.com/Cyan4973/xxHash +*/ + +/* Notice extracted from xxHash homepage : + +xxHash is an extremely fast Hash algorithm, running at RAM speed limits. +It also successfully passes all tests from the SMHasher suite. + +Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) + +Name Speed Q.Score Author +xxHash 5.4 GB/s 10 +CrapWow 3.2 GB/s 2 Andrew +MumurHash 3a 2.7 GB/s 10 Austin Appleby +SpookyHash 2.0 GB/s 10 Bob Jenkins +SBox 1.4 GB/s 9 Bret Mulvey +Lookup3 1.2 GB/s 9 Bob Jenkins +SuperFastHash 1.2 GB/s 1 Paul Hsieh +CityHash64 1.05 GB/s 10 Pike & Alakuijala +FNV 0.55 GB/s 5 Fowler, Noll, Vo +CRC32 0.43 GB/s 9 +MD5-32 0.33 GB/s 10 Ronald L. Rivest +SHA1-32 0.28 GB/s 10 + +Q.Score is a measure of quality of the hash function. +It depends on successfully passing SMHasher test set. +10 is a perfect score. + +A 64-bits version, named XXH64, is available since r35. +It offers much better speed, but for 64-bits applications only. +Name Speed on 64 bits Speed on 32 bits +XXH64 13.8 GB/s 1.9 GB/s +XXH32 6.8 GB/s 6.0 GB/s +*/ + +#if defined (__cplusplus) +extern "C" { +#endif + +#ifndef XXHASH_H_5627135585666179 +#define XXHASH_H_5627135585666179 1 + + +/* **************************** +* Definitions +******************************/ +#include <stddef.h> /* size_t */ +typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; + + +/* **************************** +* API modifier +******************************/ +/** XXH_PRIVATE_API +* This is useful if you want to include xxhash functions in `static` mode +* in order to inline them, and remove their symbol from the public list. +* Methodology : +* #define XXH_PRIVATE_API +* #include "xxhash.h" +* `xxhash.c` is automatically included. +* It's not useful to compile and link it as a separate module anymore. +*/ +#ifdef XXH_PRIVATE_API +# ifndef XXH_STATIC_LINKING_ONLY +# define XXH_STATIC_LINKING_ONLY +# endif +# if defined(__GNUC__) +# define XXH_PUBLIC_API static __inline __attribute__((unused)) +# elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) +# define XXH_PUBLIC_API static inline +# elif defined(_MSC_VER) +# define XXH_PUBLIC_API static __inline +# else +# define XXH_PUBLIC_API static /* this version may generate warnings for unused static functions; disable the relevant warning */ +# endif +#else +# define XXH_PUBLIC_API /* do nothing */ +#endif /* XXH_PRIVATE_API */ + +/*!XXH_NAMESPACE, aka Namespace Emulation : + +If you want to include _and expose_ xxHash functions from within your own library, +but also want to avoid symbol collisions with another library which also includes xxHash, + +you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library +with the value of XXH_NAMESPACE (so avoid to keep it NULL and avoid numeric values). + +Note that no change is required within the calling program as long as it includes `xxhash.h` : +regular symbol name will be automatically translated by this header. +*/ +#ifdef XXH_NAMESPACE +# define XXH_CAT(A,B) A##B +# define XXH_NAME2(A,B) XXH_CAT(A,B) +# define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32) +# define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64) +# define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber) +# define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState) +# define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState) +# define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState) +# define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState) +# define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset) +# define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset) +# define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update) +# define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update) +# define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest) +# define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest) +# define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState) +# define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState) +# define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash) +# define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash) +# define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical) +# define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical) +#endif + + +/* ************************************* +* Version +***************************************/ +#define XXH_VERSION_MAJOR 0 +#define XXH_VERSION_MINOR 6 +#define XXH_VERSION_RELEASE 2 +#define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE) +XXH_PUBLIC_API unsigned XXH_versionNumber (void); + + +/* **************************** +* Simple Hash Functions +******************************/ +typedef unsigned int XXH32_hash_t; +typedef unsigned long long XXH64_hash_t; + +XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed); +XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed); + +/*! +XXH32() : + Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input". + The memory between input & input+length must be valid (allocated and read-accessible). + "seed" can be used to alter the result predictably. + Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s +XXH64() : + Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". + "seed" can be used to alter the result predictably. + This function runs 2x faster on 64-bits systems, but slower on 32-bits systems (see benchmark). +*/ + + +/* **************************** +* Streaming Hash Functions +******************************/ +typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */ +typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */ + +/*! State allocation, compatible with dynamic libraries */ + +XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void); +XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr); + +XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void); +XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr); + + +/* hash streaming */ + +XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned int seed); +XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length); +XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr); + +XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed); +XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length); +XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr); + +/* +These functions generate the xxHash of an input provided in multiple segments. +Note that, for small input, they are slower than single-call functions, due to state management. +For small input, prefer `XXH32()` and `XXH64()` . + +XXH state must first be allocated, using XXH*_createState() . + +Start a new hash by initializing state with a seed, using XXH*_reset(). + +Then, feed the hash state by calling XXH*_update() as many times as necessary. +Obviously, input must be allocated and read accessible. +The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. + +Finally, a hash value can be produced anytime, by using XXH*_digest(). +This function returns the nn-bits hash as an int or long long. + +It's still possible to continue inserting input into the hash state after a digest, +and generate some new hashes later on, by calling again XXH*_digest(). + +When done, free XXH state space if it was allocated dynamically. +*/ + + +/* ************************** +* Utils +****************************/ +#if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* ! C99 */ +# define restrict /* disable restrict */ +#endif + +XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dst_state, const XXH32_state_t* restrict src_state); +XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dst_state, const XXH64_state_t* restrict src_state); + + +/* ************************** +* Canonical representation +****************************/ +/* Default result type for XXH functions are primitive unsigned 32 and 64 bits. +* The canonical representation uses human-readable write convention, aka big-endian (large digits first). +* These functions allow transformation of hash result into and from its canonical format. +* This way, hash values can be written into a file / memory, and remain comparable on different systems and programs. +*/ +typedef struct { unsigned char digest[4]; } XXH32_canonical_t; +typedef struct { unsigned char digest[8]; } XXH64_canonical_t; + +XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash); +XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash); + +XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src); +XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src); + +#endif /* XXHASH_H_5627135585666179 */ + + + +/* ================================================================================================ + This section contains definitions which are not guaranteed to remain stable. + They may change in future versions, becoming incompatible with a different version of the library. + They shall only be used with static linking. + Never use these definitions in association with dynamic linking ! +=================================================================================================== */ +#if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXH_STATIC_H_3543687687345) +#define XXH_STATIC_H_3543687687345 + +/* These definitions are only meant to allow allocation of XXH state + statically, on stack, or in a struct for example. + Do not use members directly. */ + + struct XXH32_state_s { + unsigned total_len_32; + unsigned large_len; + unsigned v1; + unsigned v2; + unsigned v3; + unsigned v4; + unsigned mem32[4]; /* buffer defined as U32 for alignment */ + unsigned memsize; + unsigned reserved; /* never read nor write, will be removed in a future version */ + }; /* typedef'd to XXH32_state_t */ + + struct XXH64_state_s { + unsigned long long total_len; + unsigned long long v1; + unsigned long long v2; + unsigned long long v3; + unsigned long long v4; + unsigned long long mem64[4]; /* buffer defined as U64 for alignment */ + unsigned memsize; + unsigned reserved[2]; /* never read nor write, will be removed in a future version */ + }; /* typedef'd to XXH64_state_t */ + + +# ifdef XXH_PRIVATE_API +# include "xxhash.c" /* include xxhash functions as `static`, for inlining */ +# endif + +#endif /* XXH_STATIC_LINKING_ONLY && XXH_STATIC_H_3543687687345 */ + + +#if defined (__cplusplus) +} +#endif diff --git a/thirdparty/zstd/common/zstd_common.c b/thirdparty/zstd/common/zstd_common.c new file mode 100644 index 0000000000..8408a589ae --- /dev/null +++ b/thirdparty/zstd/common/zstd_common.c @@ -0,0 +1,73 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + + + +/*-************************************* +* Dependencies +***************************************/ +#include <stdlib.h> /* malloc */ +#include "error_private.h" +#define ZSTD_STATIC_LINKING_ONLY +#include "zstd.h" /* declaration of ZSTD_isError, ZSTD_getErrorName, ZSTD_getErrorCode, ZSTD_getErrorString, ZSTD_versionNumber */ + + +/*-**************************************** +* Version +******************************************/ +unsigned ZSTD_versionNumber (void) { return ZSTD_VERSION_NUMBER; } + + +/*-**************************************** +* ZSTD Error Management +******************************************/ +/*! ZSTD_isError() : +* tells if a return value is an error code */ +unsigned ZSTD_isError(size_t code) { return ERR_isError(code); } + +/*! ZSTD_getErrorName() : +* provides error code string from function result (useful for debugging) */ +const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); } + +/*! ZSTD_getError() : +* convert a `size_t` function result into a proper ZSTD_errorCode enum */ +ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); } + +/*! ZSTD_getErrorString() : +* provides error code string from enum */ +const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); } + + +/*=************************************************************** +* Custom allocator +****************************************************************/ +/* default uses stdlib */ +void* ZSTD_defaultAllocFunction(void* opaque, size_t size) +{ + void* address = malloc(size); + (void)opaque; + return address; +} + +void ZSTD_defaultFreeFunction(void* opaque, void* address) +{ + (void)opaque; + free(address); +} + +void* ZSTD_malloc(size_t size, ZSTD_customMem customMem) +{ + return customMem.customAlloc(customMem.opaque, size); +} + +void ZSTD_free(void* ptr, ZSTD_customMem customMem) +{ + if (ptr!=NULL) + customMem.customFree(customMem.opaque, ptr); +} diff --git a/thirdparty/zstd/common/zstd_errors.h b/thirdparty/zstd/common/zstd_errors.h new file mode 100644 index 0000000000..3d579d9693 --- /dev/null +++ b/thirdparty/zstd/common/zstd_errors.h @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#ifndef ZSTD_ERRORS_H_398273423 +#define ZSTD_ERRORS_H_398273423 + +#if defined (__cplusplus) +extern "C" { +#endif + +/*===== dependency =====*/ +#include <stddef.h> /* size_t */ + + +/* ===== ZSTDERRORLIB_API : control library symbols visibility ===== */ +#if defined(__GNUC__) && (__GNUC__ >= 4) +# define ZSTDERRORLIB_VISIBILITY __attribute__ ((visibility ("default"))) +#else +# define ZSTDERRORLIB_VISIBILITY +#endif +#if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) +# define ZSTDERRORLIB_API __declspec(dllexport) ZSTDERRORLIB_VISIBILITY +#elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) +# define ZSTDERRORLIB_API __declspec(dllimport) ZSTDERRORLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ +#else +# define ZSTDERRORLIB_API ZSTDERRORLIB_VISIBILITY +#endif + +/*-**************************************** +* error codes list +******************************************/ +typedef enum { + ZSTD_error_no_error, + ZSTD_error_GENERIC, + ZSTD_error_prefix_unknown, + ZSTD_error_version_unsupported, + ZSTD_error_parameter_unknown, + ZSTD_error_frameParameter_unsupported, + ZSTD_error_frameParameter_unsupportedBy32bits, + ZSTD_error_frameParameter_windowTooLarge, + ZSTD_error_compressionParameter_unsupported, + ZSTD_error_init_missing, + ZSTD_error_memory_allocation, + ZSTD_error_stage_wrong, + ZSTD_error_dstSize_tooSmall, + ZSTD_error_srcSize_wrong, + ZSTD_error_corruption_detected, + ZSTD_error_checksum_wrong, + ZSTD_error_tableLog_tooLarge, + ZSTD_error_maxSymbolValue_tooLarge, + ZSTD_error_maxSymbolValue_tooSmall, + ZSTD_error_dictionary_corrupted, + ZSTD_error_dictionary_wrong, + ZSTD_error_dictionaryCreation_failed, + ZSTD_error_maxCode +} ZSTD_ErrorCode; + +/*! ZSTD_getErrorCode() : + convert a `size_t` function result into a `ZSTD_ErrorCode` enum type, + which can be used to compare directly with enum list published into "error_public.h" */ +ZSTDERRORLIB_API ZSTD_ErrorCode ZSTD_getErrorCode(size_t functionResult); +ZSTDERRORLIB_API const char* ZSTD_getErrorString(ZSTD_ErrorCode code); + + +#if defined (__cplusplus) +} +#endif + +#endif /* ZSTD_ERRORS_H_398273423 */ diff --git a/thirdparty/zstd/common/zstd_internal.h b/thirdparty/zstd/common/zstd_internal.h new file mode 100644 index 0000000000..2533333ba8 --- /dev/null +++ b/thirdparty/zstd/common/zstd_internal.h @@ -0,0 +1,284 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#ifndef ZSTD_CCOMMON_H_MODULE +#define ZSTD_CCOMMON_H_MODULE + +/*-******************************************************* +* Compiler specifics +*********************************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# define FORCE_INLINE static __forceinline +# include <intrin.h> /* For Visual 2005 */ +# pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4324) /* disable: C4324: padded structure */ +#else +# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +# else +# define FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +#endif + +#ifdef _MSC_VER +# define FORCE_NOINLINE static __declspec(noinline) +#else +# ifdef __GNUC__ +# define FORCE_NOINLINE static __attribute__((__noinline__)) +# else +# define FORCE_NOINLINE static +# endif +#endif + + +/*-************************************* +* Dependencies +***************************************/ +#include "mem.h" +#include "error_private.h" +#define ZSTD_STATIC_LINKING_ONLY +#include "zstd.h" +#ifndef XXH_STATIC_LINKING_ONLY +# define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */ +#endif +#include "xxhash.h" /* XXH_reset, update, digest */ + + +/*-************************************* +* shared macros +***************************************/ +#undef MIN +#undef MAX +#define MIN(a,b) ((a)<(b) ? (a) : (b)) +#define MAX(a,b) ((a)>(b) ? (a) : (b)) +#define CHECK_F(f) { size_t const errcod = f; if (ERR_isError(errcod)) return errcod; } /* check and Forward error code */ +#define CHECK_E(f, e) { size_t const errcod = f; if (ERR_isError(errcod)) return ERROR(e); } /* check and send Error code */ + + +/*-************************************* +* Common constants +***************************************/ +#define ZSTD_OPT_NUM (1<<12) +#define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7+ */ + +#define ZSTD_REP_NUM 3 /* number of repcodes */ +#define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */ +#define ZSTD_REP_MOVE (ZSTD_REP_NUM-1) +#define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM) +static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 }; + +#define KB *(1 <<10) +#define MB *(1 <<20) +#define GB *(1U<<30) + +#define BIT7 128 +#define BIT6 64 +#define BIT5 32 +#define BIT4 16 +#define BIT1 2 +#define BIT0 1 + +#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10 +static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 }; +static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 }; + +#define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */ +static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE; +typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e; + +#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */ +#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */ + +#define HufLog 12 +typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e; + +#define LONGNBSEQ 0x7F00 + +#define MINMATCH 3 + +#define Litbits 8 +#define MaxLit ((1<<Litbits) - 1) +#define MaxML 52 +#define MaxLL 35 +#define MaxOff 28 +#define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */ +#define MLFSELog 9 +#define LLFSELog 9 +#define OffFSELog 8 + +static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12, + 13,14,15,16 }; +static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1, + -1,-1,-1,-1 }; +#define LL_DEFAULTNORMLOG 6 /* for static allocation */ +static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG; + +static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11, + 12,13,14,15,16 }; +static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1, + -1,-1,-1,-1,-1 }; +#define ML_DEFAULTNORMLOG 6 /* for static allocation */ +static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG; + +static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 }; +#define OF_DEFAULTNORMLOG 5 /* for static allocation */ +static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG; + + +/*-******************************************* +* Shared functions to include for inlining +*********************************************/ +static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } +#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } + +/*! ZSTD_wildcopy() : +* custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */ +#define WILDCOPY_OVERLENGTH 8 +MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length) +{ + const BYTE* ip = (const BYTE*)src; + BYTE* op = (BYTE*)dst; + BYTE* const oend = op + length; + do + COPY8(op, ip) + while (op < oend); +} + +MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd) /* should be faster for decoding, but strangely, not verified on all platform */ +{ + const BYTE* ip = (const BYTE*)src; + BYTE* op = (BYTE*)dst; + BYTE* const oend = (BYTE*)dstEnd; + do + COPY8(op, ip) + while (op < oend); +} + + +/*-******************************************* +* Private interfaces +*********************************************/ +typedef struct ZSTD_stats_s ZSTD_stats_t; + +typedef struct { + U32 off; + U32 len; +} ZSTD_match_t; + +typedef struct { + U32 price; + U32 off; + U32 mlen; + U32 litlen; + U32 rep[ZSTD_REP_NUM]; +} ZSTD_optimal_t; + + +typedef struct seqDef_s { + U32 offset; + U16 litLength; + U16 matchLength; +} seqDef; + + +typedef struct { + seqDef* sequencesStart; + seqDef* sequences; + BYTE* litStart; + BYTE* lit; + BYTE* llCode; + BYTE* mlCode; + BYTE* ofCode; + U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */ + U32 longLengthPos; + /* opt */ + ZSTD_optimal_t* priceTable; + ZSTD_match_t* matchTable; + U32* matchLengthFreq; + U32* litLengthFreq; + U32* litFreq; + U32* offCodeFreq; + U32 matchLengthSum; + U32 matchSum; + U32 litLengthSum; + U32 litSum; + U32 offCodeSum; + U32 log2matchLengthSum; + U32 log2matchSum; + U32 log2litLengthSum; + U32 log2litSum; + U32 log2offCodeSum; + U32 factor; + U32 staticPrices; + U32 cachedPrice; + U32 cachedLitLength; + const BYTE* cachedLiterals; +} seqStore_t; + +const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); +void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); +int ZSTD_isSkipFrame(ZSTD_DCtx* dctx); + +/* custom memory allocation functions */ +void* ZSTD_defaultAllocFunction(void* opaque, size_t size); +void ZSTD_defaultFreeFunction(void* opaque, void* address); +#ifndef ZSTD_DLL_IMPORT +static const ZSTD_customMem defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL }; +#endif +void* ZSTD_malloc(size_t size, ZSTD_customMem customMem); +void ZSTD_free(void* ptr, ZSTD_customMem customMem); + + +/*====== common function ======*/ + +MEM_STATIC U32 ZSTD_highbit32(U32 val) +{ +# if defined(_MSC_VER) /* Visual */ + unsigned long r=0; + _BitScanReverse(&r, val); + return (unsigned)r; +# elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */ + return 31 - __builtin_clz(val); +# else /* Software version */ + static const int DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; + U32 v = val; + int r; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27]; + return r; +# endif +} + + +/* hidden functions */ + +/* ZSTD_invalidateRepCodes() : + * ensures next compression will not use repcodes from previous block. + * Note : only works with regular variant; + * do not use with extDict variant ! */ +void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); + + +#endif /* ZSTD_CCOMMON_H_MODULE */ diff --git a/thirdparty/zstd/compress/fse_compress.c b/thirdparty/zstd/compress/fse_compress.c new file mode 100644 index 0000000000..26e8052ddc --- /dev/null +++ b/thirdparty/zstd/compress/fse_compress.c @@ -0,0 +1,857 @@ +/* ****************************************************************** + FSE : Finite State Entropy encoder + Copyright (C) 2013-2015, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy + - Public forum : https://groups.google.com/forum/#!forum/lz4c +****************************************************************** */ + +/* ************************************************************** +* Compiler specifics +****************************************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# define FORCE_INLINE static __forceinline +# include <intrin.h> /* For Visual 2005 */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ +#else +# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +# else +# define FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +#endif + + +/* ************************************************************** +* Includes +****************************************************************/ +#include <stdlib.h> /* malloc, free, qsort */ +#include <string.h> /* memcpy, memset */ +#include <stdio.h> /* printf (debug) */ +#include "bitstream.h" +#define FSE_STATIC_LINKING_ONLY +#include "fse.h" + + +/* ************************************************************** +* Error Management +****************************************************************/ +#define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ + + +/* ************************************************************** +* Templates +****************************************************************/ +/* + designed to be included + for type-specific functions (template emulation in C) + Objective is to write these functions only once, for improved maintenance +*/ + +/* safety checks */ +#ifndef FSE_FUNCTION_EXTENSION +# error "FSE_FUNCTION_EXTENSION must be defined" +#endif +#ifndef FSE_FUNCTION_TYPE +# error "FSE_FUNCTION_TYPE must be defined" +#endif + +/* Function names */ +#define FSE_CAT(X,Y) X##Y +#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y) +#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y) + + +/* Function templates */ + +/* FSE_buildCTable_wksp() : + * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`). + * wkspSize should be sized to handle worst case situation, which is `1<<max_tableLog * sizeof(FSE_FUNCTION_TYPE)` + * workSpace must also be properly aligned with FSE_FUNCTION_TYPE requirements + */ +size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize) +{ + U32 const tableSize = 1 << tableLog; + U32 const tableMask = tableSize - 1; + void* const ptr = ct; + U16* const tableU16 = ( (U16*) ptr) + 2; + void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableLog ? tableSize>>1 : 1) ; + FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT); + U32 const step = FSE_TABLESTEP(tableSize); + U32 cumul[FSE_MAX_SYMBOL_VALUE+2]; + + FSE_FUNCTION_TYPE* const tableSymbol = (FSE_FUNCTION_TYPE*)workSpace; + U32 highThreshold = tableSize-1; + + /* CTable header */ + if (((size_t)1 << tableLog) * sizeof(FSE_FUNCTION_TYPE) > wkspSize) return ERROR(tableLog_tooLarge); + tableU16[-2] = (U16) tableLog; + tableU16[-1] = (U16) maxSymbolValue; + + /* For explanations on how to distribute symbol values over the table : + * http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */ + + /* symbol start positions */ + { U32 u; + cumul[0] = 0; + for (u=1; u<=maxSymbolValue+1; u++) { + if (normalizedCounter[u-1]==-1) { /* Low proba symbol */ + cumul[u] = cumul[u-1] + 1; + tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u-1); + } else { + cumul[u] = cumul[u-1] + normalizedCounter[u-1]; + } } + cumul[maxSymbolValue+1] = tableSize+1; + } + + /* Spread symbols */ + { U32 position = 0; + U32 symbol; + for (symbol=0; symbol<=maxSymbolValue; symbol++) { + int nbOccurences; + for (nbOccurences=0; nbOccurences<normalizedCounter[symbol]; nbOccurences++) { + tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol; + position = (position + step) & tableMask; + while (position > highThreshold) position = (position + step) & tableMask; /* Low proba area */ + } } + + if (position!=0) return ERROR(GENERIC); /* Must have gone through all positions */ + } + + /* Build table */ + { U32 u; for (u=0; u<tableSize; u++) { + FSE_FUNCTION_TYPE s = tableSymbol[u]; /* note : static analyzer may not understand tableSymbol is properly initialized */ + tableU16[cumul[s]++] = (U16) (tableSize+u); /* TableU16 : sorted by symbol order; gives next state value */ + } } + + /* Build Symbol Transformation Table */ + { unsigned total = 0; + unsigned s; + for (s=0; s<=maxSymbolValue; s++) { + switch (normalizedCounter[s]) + { + case 0: break; + + case -1: + case 1: + symbolTT[s].deltaNbBits = (tableLog << 16) - (1<<tableLog); + symbolTT[s].deltaFindState = total - 1; + total ++; + break; + default : + { + U32 const maxBitsOut = tableLog - BIT_highbit32 (normalizedCounter[s]-1); + U32 const minStatePlus = normalizedCounter[s] << maxBitsOut; + symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus; + symbolTT[s].deltaFindState = total - normalizedCounter[s]; + total += normalizedCounter[s]; + } } } } + + return 0; +} + + +size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) +{ + FSE_FUNCTION_TYPE tableSymbol[FSE_MAX_TABLESIZE]; /* memset() is not necessary, even if static analyzer complain about it */ + return FSE_buildCTable_wksp(ct, normalizedCounter, maxSymbolValue, tableLog, tableSymbol, sizeof(tableSymbol)); +} + + + +#ifndef FSE_COMMONDEFS_ONLY + +/*-************************************************************** +* FSE NCount encoding-decoding +****************************************************************/ +size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog) +{ + size_t const maxHeaderSize = (((maxSymbolValue+1) * tableLog) >> 3) + 3; + return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND; /* maxSymbolValue==0 ? use default */ +} + +static size_t FSE_writeNCount_generic (void* header, size_t headerBufferSize, + const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, + unsigned writeIsSafe) +{ + BYTE* const ostart = (BYTE*) header; + BYTE* out = ostart; + BYTE* const oend = ostart + headerBufferSize; + int nbBits; + const int tableSize = 1 << tableLog; + int remaining; + int threshold; + U32 bitStream; + int bitCount; + unsigned charnum = 0; + int previous0 = 0; + + bitStream = 0; + bitCount = 0; + /* Table Size */ + bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount; + bitCount += 4; + + /* Init */ + remaining = tableSize+1; /* +1 for extra accuracy */ + threshold = tableSize; + nbBits = tableLog+1; + + while (remaining>1) { /* stops at 1 */ + if (previous0) { + unsigned start = charnum; + while (!normalizedCounter[charnum]) charnum++; + while (charnum >= start+24) { + start+=24; + bitStream += 0xFFFFU << bitCount; + if ((!writeIsSafe) && (out > oend-2)) return ERROR(dstSize_tooSmall); /* Buffer overflow */ + out[0] = (BYTE) bitStream; + out[1] = (BYTE)(bitStream>>8); + out+=2; + bitStream>>=16; + } + while (charnum >= start+3) { + start+=3; + bitStream += 3 << bitCount; + bitCount += 2; + } + bitStream += (charnum-start) << bitCount; + bitCount += 2; + if (bitCount>16) { + if ((!writeIsSafe) && (out > oend - 2)) return ERROR(dstSize_tooSmall); /* Buffer overflow */ + out[0] = (BYTE)bitStream; + out[1] = (BYTE)(bitStream>>8); + out += 2; + bitStream >>= 16; + bitCount -= 16; + } } + { int count = normalizedCounter[charnum++]; + int const max = (2*threshold-1)-remaining; + remaining -= count < 0 ? -count : count; + count++; /* +1 for extra accuracy */ + if (count>=threshold) count += max; /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */ + bitStream += count << bitCount; + bitCount += nbBits; + bitCount -= (count<max); + previous0 = (count==1); + if (remaining<1) return ERROR(GENERIC); + while (remaining<threshold) nbBits--, threshold>>=1; + } + if (bitCount>16) { + if ((!writeIsSafe) && (out > oend - 2)) return ERROR(dstSize_tooSmall); /* Buffer overflow */ + out[0] = (BYTE)bitStream; + out[1] = (BYTE)(bitStream>>8); + out += 2; + bitStream >>= 16; + bitCount -= 16; + } } + + /* flush remaining bitStream */ + if ((!writeIsSafe) && (out > oend - 2)) return ERROR(dstSize_tooSmall); /* Buffer overflow */ + out[0] = (BYTE)bitStream; + out[1] = (BYTE)(bitStream>>8); + out+= (bitCount+7) /8; + + if (charnum > maxSymbolValue + 1) return ERROR(GENERIC); + + return (out-ostart); +} + + +size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) +{ + if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); /* Unsupported */ + if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC); /* Unsupported */ + + if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog)) + return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 0); + + return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 1); +} + + + +/*-************************************************************** +* Counting histogram +****************************************************************/ +/*! FSE_count_simple + This function counts byte values within `src`, and store the histogram into table `count`. + It doesn't use any additional memory. + But this function is unsafe : it doesn't check that all values within `src` can fit into `count`. + For this reason, prefer using a table `count` with 256 elements. + @return : count of most numerous element +*/ +size_t FSE_count_simple(unsigned* count, unsigned* maxSymbolValuePtr, + const void* src, size_t srcSize) +{ + const BYTE* ip = (const BYTE*)src; + const BYTE* const end = ip + srcSize; + unsigned maxSymbolValue = *maxSymbolValuePtr; + unsigned max=0; + + memset(count, 0, (maxSymbolValue+1)*sizeof(*count)); + if (srcSize==0) { *maxSymbolValuePtr = 0; return 0; } + + while (ip<end) count[*ip++]++; + + while (!count[maxSymbolValue]) maxSymbolValue--; + *maxSymbolValuePtr = maxSymbolValue; + + { U32 s; for (s=0; s<=maxSymbolValue; s++) if (count[s] > max) max = count[s]; } + + return (size_t)max; +} + + +/* FSE_count_parallel_wksp() : + * Same as FSE_count_parallel(), but using an externally provided scratch buffer. + * `workSpace` size must be a minimum of `1024 * sizeof(unsigned)`` */ +static size_t FSE_count_parallel_wksp( + unsigned* count, unsigned* maxSymbolValuePtr, + const void* source, size_t sourceSize, + unsigned checkMax, unsigned* const workSpace) +{ + const BYTE* ip = (const BYTE*)source; + const BYTE* const iend = ip+sourceSize; + unsigned maxSymbolValue = *maxSymbolValuePtr; + unsigned max=0; + U32* const Counting1 = workSpace; + U32* const Counting2 = Counting1 + 256; + U32* const Counting3 = Counting2 + 256; + U32* const Counting4 = Counting3 + 256; + + memset(Counting1, 0, 4*256*sizeof(unsigned)); + + /* safety checks */ + if (!sourceSize) { + memset(count, 0, maxSymbolValue + 1); + *maxSymbolValuePtr = 0; + return 0; + } + if (!maxSymbolValue) maxSymbolValue = 255; /* 0 == default */ + + /* by stripes of 16 bytes */ + { U32 cached = MEM_read32(ip); ip += 4; + while (ip < iend-15) { + U32 c = cached; cached = MEM_read32(ip); ip += 4; + Counting1[(BYTE) c ]++; + Counting2[(BYTE)(c>>8) ]++; + Counting3[(BYTE)(c>>16)]++; + Counting4[ c>>24 ]++; + c = cached; cached = MEM_read32(ip); ip += 4; + Counting1[(BYTE) c ]++; + Counting2[(BYTE)(c>>8) ]++; + Counting3[(BYTE)(c>>16)]++; + Counting4[ c>>24 ]++; + c = cached; cached = MEM_read32(ip); ip += 4; + Counting1[(BYTE) c ]++; + Counting2[(BYTE)(c>>8) ]++; + Counting3[(BYTE)(c>>16)]++; + Counting4[ c>>24 ]++; + c = cached; cached = MEM_read32(ip); ip += 4; + Counting1[(BYTE) c ]++; + Counting2[(BYTE)(c>>8) ]++; + Counting3[(BYTE)(c>>16)]++; + Counting4[ c>>24 ]++; + } + ip-=4; + } + + /* finish last symbols */ + while (ip<iend) Counting1[*ip++]++; + + if (checkMax) { /* verify stats will fit into destination table */ + U32 s; for (s=255; s>maxSymbolValue; s--) { + Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s]; + if (Counting1[s]) return ERROR(maxSymbolValue_tooSmall); + } } + + { U32 s; for (s=0; s<=maxSymbolValue; s++) { + count[s] = Counting1[s] + Counting2[s] + Counting3[s] + Counting4[s]; + if (count[s] > max) max = count[s]; + } } + + while (!count[maxSymbolValue]) maxSymbolValue--; + *maxSymbolValuePtr = maxSymbolValue; + return (size_t)max; +} + +/* FSE_countFast_wksp() : + * Same as FSE_countFast(), but using an externally provided scratch buffer. + * `workSpace` size must be table of >= `1024` unsigned */ +size_t FSE_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr, + const void* source, size_t sourceSize, unsigned* workSpace) +{ + if (sourceSize < 1500) return FSE_count_simple(count, maxSymbolValuePtr, source, sourceSize); + return FSE_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, 0, workSpace); +} + +/* fast variant (unsafe : won't check if src contains values beyond count[] limit) */ +size_t FSE_countFast(unsigned* count, unsigned* maxSymbolValuePtr, + const void* source, size_t sourceSize) +{ + unsigned tmpCounters[1024]; + return FSE_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, tmpCounters); +} + +/* FSE_count_wksp() : + * Same as FSE_count(), but using an externally provided scratch buffer. + * `workSpace` size must be table of >= `1024` unsigned */ +size_t FSE_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr, + const void* source, size_t sourceSize, unsigned* workSpace) +{ + if (*maxSymbolValuePtr < 255) + return FSE_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, 1, workSpace); + *maxSymbolValuePtr = 255; + return FSE_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace); +} + +size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, + const void* src, size_t srcSize) +{ + unsigned tmpCounters[1024]; + return FSE_count_wksp(count, maxSymbolValuePtr, src, srcSize, tmpCounters); +} + + + +/*-************************************************************** +* FSE Compression Code +****************************************************************/ +/*! FSE_sizeof_CTable() : + FSE_CTable is a variable size structure which contains : + `U16 tableLog;` + `U16 maxSymbolValue;` + `U16 nextStateNumber[1 << tableLog];` // This size is variable + `FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1];` // This size is variable +Allocation is manual (C standard does not support variable-size structures). +*/ +size_t FSE_sizeof_CTable (unsigned maxSymbolValue, unsigned tableLog) +{ + if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); + return FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32); +} + +FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog) +{ + size_t size; + if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX; + size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32); + return (FSE_CTable*)malloc(size); +} + +void FSE_freeCTable (FSE_CTable* ct) { free(ct); } + +/* provides the minimum logSize to safely represent a distribution */ +static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue) +{ + U32 minBitsSrc = BIT_highbit32((U32)(srcSize - 1)) + 1; + U32 minBitsSymbols = BIT_highbit32(maxSymbolValue) + 2; + U32 minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols; + return minBits; +} + +unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus) +{ + U32 maxBitsSrc = BIT_highbit32((U32)(srcSize - 1)) - minus; + U32 tableLog = maxTableLog; + U32 minBits = FSE_minTableLog(srcSize, maxSymbolValue); + if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG; + if (maxBitsSrc < tableLog) tableLog = maxBitsSrc; /* Accuracy can be reduced */ + if (minBits > tableLog) tableLog = minBits; /* Need a minimum to safely represent all symbol values */ + if (tableLog < FSE_MIN_TABLELOG) tableLog = FSE_MIN_TABLELOG; + if (tableLog > FSE_MAX_TABLELOG) tableLog = FSE_MAX_TABLELOG; + return tableLog; +} + +unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue) +{ + return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2); +} + + +/* Secondary normalization method. + To be used when primary method fails. */ + +static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue) +{ + short const NOT_YET_ASSIGNED = -2; + U32 s; + U32 distributed = 0; + U32 ToDistribute; + + /* Init */ + U32 const lowThreshold = (U32)(total >> tableLog); + U32 lowOne = (U32)((total * 3) >> (tableLog + 1)); + + for (s=0; s<=maxSymbolValue; s++) { + if (count[s] == 0) { + norm[s]=0; + continue; + } + if (count[s] <= lowThreshold) { + norm[s] = -1; + distributed++; + total -= count[s]; + continue; + } + if (count[s] <= lowOne) { + norm[s] = 1; + distributed++; + total -= count[s]; + continue; + } + + norm[s]=NOT_YET_ASSIGNED; + } + ToDistribute = (1 << tableLog) - distributed; + + if ((total / ToDistribute) > lowOne) { + /* risk of rounding to zero */ + lowOne = (U32)((total * 3) / (ToDistribute * 2)); + for (s=0; s<=maxSymbolValue; s++) { + if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) { + norm[s] = 1; + distributed++; + total -= count[s]; + continue; + } } + ToDistribute = (1 << tableLog) - distributed; + } + + if (distributed == maxSymbolValue+1) { + /* all values are pretty poor; + probably incompressible data (should have already been detected); + find max, then give all remaining points to max */ + U32 maxV = 0, maxC = 0; + for (s=0; s<=maxSymbolValue; s++) + if (count[s] > maxC) maxV=s, maxC=count[s]; + norm[maxV] += (short)ToDistribute; + return 0; + } + + if (total == 0) { + /* all of the symbols were low enough for the lowOne or lowThreshold */ + for (s=0; ToDistribute > 0; s = (s+1)%(maxSymbolValue+1)) + if (norm[s] > 0) ToDistribute--, norm[s]++; + return 0; + } + + { U64 const vStepLog = 62 - tableLog; + U64 const mid = (1ULL << (vStepLog-1)) - 1; + U64 const rStep = ((((U64)1<<vStepLog) * ToDistribute) + mid) / total; /* scale on remaining */ + U64 tmpTotal = mid; + for (s=0; s<=maxSymbolValue; s++) { + if (norm[s]==NOT_YET_ASSIGNED) { + U64 const end = tmpTotal + (count[s] * rStep); + U32 const sStart = (U32)(tmpTotal >> vStepLog); + U32 const sEnd = (U32)(end >> vStepLog); + U32 const weight = sEnd - sStart; + if (weight < 1) + return ERROR(GENERIC); + norm[s] = (short)weight; + tmpTotal = end; + } } } + + return 0; +} + + +size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog, + const unsigned* count, size_t total, + unsigned maxSymbolValue) +{ + /* Sanity checks */ + if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG; + if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC); /* Unsupported size */ + if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); /* Unsupported size */ + if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC); /* Too small tableLog, compression potentially impossible */ + + { U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 }; + U64 const scale = 62 - tableLog; + U64 const step = ((U64)1<<62) / total; /* <== here, one division ! */ + U64 const vStep = 1ULL<<(scale-20); + int stillToDistribute = 1<<tableLog; + unsigned s; + unsigned largest=0; + short largestP=0; + U32 lowThreshold = (U32)(total >> tableLog); + + for (s=0; s<=maxSymbolValue; s++) { + if (count[s] == total) return 0; /* rle special case */ + if (count[s] == 0) { normalizedCounter[s]=0; continue; } + if (count[s] <= lowThreshold) { + normalizedCounter[s] = -1; + stillToDistribute--; + } else { + short proba = (short)((count[s]*step) >> scale); + if (proba<8) { + U64 restToBeat = vStep * rtbTable[proba]; + proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat; + } + if (proba > largestP) largestP=proba, largest=s; + normalizedCounter[s] = proba; + stillToDistribute -= proba; + } } + if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) { + /* corner case, need another normalization method */ + size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue); + if (FSE_isError(errorCode)) return errorCode; + } + else normalizedCounter[largest] += (short)stillToDistribute; + } + +#if 0 + { /* Print Table (debug) */ + U32 s; + U32 nTotal = 0; + for (s=0; s<=maxSymbolValue; s++) + printf("%3i: %4i \n", s, normalizedCounter[s]); + for (s=0; s<=maxSymbolValue; s++) + nTotal += abs(normalizedCounter[s]); + if (nTotal != (1U<<tableLog)) + printf("Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog); + getchar(); + } +#endif + + return tableLog; +} + + +/* fake FSE_CTable, for raw (uncompressed) input */ +size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits) +{ + const unsigned tableSize = 1 << nbBits; + const unsigned tableMask = tableSize - 1; + const unsigned maxSymbolValue = tableMask; + void* const ptr = ct; + U16* const tableU16 = ( (U16*) ptr) + 2; + void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableSize>>1); /* assumption : tableLog >= 1 */ + FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT); + unsigned s; + + /* Sanity checks */ + if (nbBits < 1) return ERROR(GENERIC); /* min size */ + + /* header */ + tableU16[-2] = (U16) nbBits; + tableU16[-1] = (U16) maxSymbolValue; + + /* Build table */ + for (s=0; s<tableSize; s++) + tableU16[s] = (U16)(tableSize + s); + + /* Build Symbol Transformation Table */ + { const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits); + for (s=0; s<=maxSymbolValue; s++) { + symbolTT[s].deltaNbBits = deltaNbBits; + symbolTT[s].deltaFindState = s-1; + } } + + return 0; +} + +/* fake FSE_CTable, for rle input (always same symbol) */ +size_t FSE_buildCTable_rle (FSE_CTable* ct, BYTE symbolValue) +{ + void* ptr = ct; + U16* tableU16 = ( (U16*) ptr) + 2; + void* FSCTptr = (U32*)ptr + 2; + FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) FSCTptr; + + /* header */ + tableU16[-2] = (U16) 0; + tableU16[-1] = (U16) symbolValue; + + /* Build table */ + tableU16[0] = 0; + tableU16[1] = 0; /* just in case */ + + /* Build Symbol Transformation Table */ + symbolTT[symbolValue].deltaNbBits = 0; + symbolTT[symbolValue].deltaFindState = 0; + + return 0; +} + + +static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize, + const void* src, size_t srcSize, + const FSE_CTable* ct, const unsigned fast) +{ + const BYTE* const istart = (const BYTE*) src; + const BYTE* const iend = istart + srcSize; + const BYTE* ip=iend; + + BIT_CStream_t bitC; + FSE_CState_t CState1, CState2; + + /* init */ + if (srcSize <= 2) return 0; + { size_t const initError = BIT_initCStream(&bitC, dst, dstSize); + if (FSE_isError(initError)) return 0; /* not enough space available to write a bitstream */ } + +#define FSE_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s)) + + if (srcSize & 1) { + FSE_initCState2(&CState1, ct, *--ip); + FSE_initCState2(&CState2, ct, *--ip); + FSE_encodeSymbol(&bitC, &CState1, *--ip); + FSE_FLUSHBITS(&bitC); + } else { + FSE_initCState2(&CState2, ct, *--ip); + FSE_initCState2(&CState1, ct, *--ip); + } + + /* join to mod 4 */ + srcSize -= 2; + if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) { /* test bit 2 */ + FSE_encodeSymbol(&bitC, &CState2, *--ip); + FSE_encodeSymbol(&bitC, &CState1, *--ip); + FSE_FLUSHBITS(&bitC); + } + + /* 2 or 4 encoding per loop */ + while ( ip>istart ) { + + FSE_encodeSymbol(&bitC, &CState2, *--ip); + + if (sizeof(bitC.bitContainer)*8 < FSE_MAX_TABLELOG*2+7 ) /* this test must be static */ + FSE_FLUSHBITS(&bitC); + + FSE_encodeSymbol(&bitC, &CState1, *--ip); + + if (sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) { /* this test must be static */ + FSE_encodeSymbol(&bitC, &CState2, *--ip); + FSE_encodeSymbol(&bitC, &CState1, *--ip); + } + + FSE_FLUSHBITS(&bitC); + } + + FSE_flushCState(&bitC, &CState2); + FSE_flushCState(&bitC, &CState1); + return BIT_closeCStream(&bitC); +} + +size_t FSE_compress_usingCTable (void* dst, size_t dstSize, + const void* src, size_t srcSize, + const FSE_CTable* ct) +{ + unsigned const fast = (dstSize >= FSE_BLOCKBOUND(srcSize)); + + if (fast) + return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1); + else + return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0); +} + + +size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); } + +#define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return f +#define CHECK_F(f) { CHECK_V_F(_var_err__, f); } + +/* FSE_compress_wksp() : + * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`). + * `wkspSize` size must be `(1<<tableLog)`. + */ +size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize) +{ + BYTE* const ostart = (BYTE*) dst; + BYTE* op = ostart; + BYTE* const oend = ostart + dstSize; + + U32 count[FSE_MAX_SYMBOL_VALUE+1]; + S16 norm[FSE_MAX_SYMBOL_VALUE+1]; + FSE_CTable* CTable = (FSE_CTable*)workSpace; + size_t const CTableSize = FSE_CTABLE_SIZE_U32(tableLog, maxSymbolValue); + void* scratchBuffer = (void*)(CTable + CTableSize); + size_t const scratchBufferSize = wkspSize - (CTableSize * sizeof(FSE_CTable)); + + /* init conditions */ + if (wkspSize < FSE_WKSP_SIZE_U32(tableLog, maxSymbolValue)) return ERROR(tableLog_tooLarge); + if (srcSize <= 1) return 0; /* Not compressible */ + if (!maxSymbolValue) maxSymbolValue = FSE_MAX_SYMBOL_VALUE; + if (!tableLog) tableLog = FSE_DEFAULT_TABLELOG; + + /* Scan input and build symbol stats */ + { CHECK_V_F(maxCount, FSE_count_wksp(count, &maxSymbolValue, src, srcSize, (unsigned*)scratchBuffer) ); + if (maxCount == srcSize) return 1; /* only a single symbol in src : rle */ + if (maxCount == 1) return 0; /* each symbol present maximum once => not compressible */ + if (maxCount < (srcSize >> 7)) return 0; /* Heuristic : not compressible enough */ + } + + tableLog = FSE_optimalTableLog(tableLog, srcSize, maxSymbolValue); + CHECK_F( FSE_normalizeCount(norm, tableLog, count, srcSize, maxSymbolValue) ); + + /* Write table description header */ + { CHECK_V_F(nc_err, FSE_writeNCount(op, oend-op, norm, maxSymbolValue, tableLog) ); + op += nc_err; + } + + /* Compress */ + CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, scratchBufferSize) ); + { CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, src, srcSize, CTable) ); + if (cSize == 0) return 0; /* not enough space for compressed data */ + op += cSize; + } + + /* check compressibility */ + if ( (size_t)(op-ostart) >= srcSize-1 ) return 0; + + return op-ostart; +} + +typedef struct { + FSE_CTable CTable_max[FSE_CTABLE_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)]; + BYTE scratchBuffer[1 << FSE_MAX_TABLELOG]; +} fseWkspMax_t; + +size_t FSE_compress2 (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog) +{ + fseWkspMax_t scratchBuffer; + FSE_STATIC_ASSERT(sizeof(scratchBuffer) >= FSE_WKSP_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)); /* compilation failures here means scratchBuffer is not large enough */ + if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); + return FSE_compress_wksp(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog, &scratchBuffer, sizeof(scratchBuffer)); +} + +size_t FSE_compress (void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + return FSE_compress2(dst, dstCapacity, src, srcSize, FSE_MAX_SYMBOL_VALUE, FSE_DEFAULT_TABLELOG); +} + + +#endif /* FSE_COMMONDEFS_ONLY */ diff --git a/thirdparty/zstd/compress/huf_compress.c b/thirdparty/zstd/compress/huf_compress.c new file mode 100644 index 0000000000..fe11aafb8f --- /dev/null +++ b/thirdparty/zstd/compress/huf_compress.c @@ -0,0 +1,684 @@ +/* ****************************************************************** + Huffman encoder, part of New Generation Entropy library + Copyright (C) 2013-2016, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy + - Public forum : https://groups.google.com/forum/#!forum/lz4c +****************************************************************** */ + +/* ************************************************************** +* Compiler specifics +****************************************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +#endif + + +/* ************************************************************** +* Includes +****************************************************************/ +#include <string.h> /* memcpy, memset */ +#include <stdio.h> /* printf (debug) */ +#include "bitstream.h" +#define FSE_STATIC_LINKING_ONLY /* FSE_optimalTableLog_internal */ +#include "fse.h" /* header compression */ +#define HUF_STATIC_LINKING_ONLY +#include "huf.h" + + +/* ************************************************************** +* Error Management +****************************************************************/ +#define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ +#define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return f +#define CHECK_F(f) { CHECK_V_F(_var_err__, f); } + + +/* ************************************************************** +* Utils +****************************************************************/ +unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue) +{ + return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 1); +} + + +/* ******************************************************* +* HUF : Huffman block compression +*********************************************************/ +/* HUF_compressWeights() : + * Same as FSE_compress(), but dedicated to huff0's weights compression. + * The use case needs much less stack memory. + * Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX. + */ +#define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6 +size_t HUF_compressWeights (void* dst, size_t dstSize, const void* weightTable, size_t wtSize) +{ + BYTE* const ostart = (BYTE*) dst; + BYTE* op = ostart; + BYTE* const oend = ostart + dstSize; + + U32 maxSymbolValue = HUF_TABLELOG_MAX; + U32 tableLog = MAX_FSE_TABLELOG_FOR_HUFF_HEADER; + + FSE_CTable CTable[FSE_CTABLE_SIZE_U32(MAX_FSE_TABLELOG_FOR_HUFF_HEADER, HUF_TABLELOG_MAX)]; + BYTE scratchBuffer[1<<MAX_FSE_TABLELOG_FOR_HUFF_HEADER]; + + U32 count[HUF_TABLELOG_MAX+1]; + S16 norm[HUF_TABLELOG_MAX+1]; + + /* init conditions */ + if (wtSize <= 1) return 0; /* Not compressible */ + + /* Scan input and build symbol stats */ + { CHECK_V_F(maxCount, FSE_count_simple(count, &maxSymbolValue, weightTable, wtSize) ); + if (maxCount == wtSize) return 1; /* only a single symbol in src : rle */ + if (maxCount == 1) return 0; /* each symbol present maximum once => not compressible */ + } + + tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue); + CHECK_F( FSE_normalizeCount(norm, tableLog, count, wtSize, maxSymbolValue) ); + + /* Write table description header */ + { CHECK_V_F(hSize, FSE_writeNCount(op, oend-op, norm, maxSymbolValue, tableLog) ); + op += hSize; + } + + /* Compress */ + CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, sizeof(scratchBuffer)) ); + { CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, weightTable, wtSize, CTable) ); + if (cSize == 0) return 0; /* not enough space for compressed data */ + op += cSize; + } + + return op-ostart; +} + + +struct HUF_CElt_s { + U16 val; + BYTE nbBits; +}; /* typedef'd to HUF_CElt within "huf.h" */ + +/*! HUF_writeCTable() : + `CTable` : Huffman tree to save, using huf representation. + @return : size of saved CTable */ +size_t HUF_writeCTable (void* dst, size_t maxDstSize, + const HUF_CElt* CTable, U32 maxSymbolValue, U32 huffLog) +{ + BYTE bitsToWeight[HUF_TABLELOG_MAX + 1]; /* precomputed conversion table */ + BYTE huffWeight[HUF_SYMBOLVALUE_MAX]; + BYTE* op = (BYTE*)dst; + U32 n; + + /* check conditions */ + if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(maxSymbolValue_tooLarge); + + /* convert to weight */ + bitsToWeight[0] = 0; + for (n=1; n<huffLog+1; n++) + bitsToWeight[n] = (BYTE)(huffLog + 1 - n); + for (n=0; n<maxSymbolValue; n++) + huffWeight[n] = bitsToWeight[CTable[n].nbBits]; + + /* attempt weights compression by FSE */ + { CHECK_V_F(hSize, HUF_compressWeights(op+1, maxDstSize-1, huffWeight, maxSymbolValue) ); + if ((hSize>1) & (hSize < maxSymbolValue/2)) { /* FSE compressed */ + op[0] = (BYTE)hSize; + return hSize+1; + } } + + /* write raw values as 4-bits (max : 15) */ + if (maxSymbolValue > (256-128)) return ERROR(GENERIC); /* should not happen : likely means source cannot be compressed */ + if (((maxSymbolValue+1)/2) + 1 > maxDstSize) return ERROR(dstSize_tooSmall); /* not enough space within dst buffer */ + op[0] = (BYTE)(128 /*special case*/ + (maxSymbolValue-1)); + huffWeight[maxSymbolValue] = 0; /* to be sure it doesn't cause msan issue in final combination */ + for (n=0; n<maxSymbolValue; n+=2) + op[(n/2)+1] = (BYTE)((huffWeight[n] << 4) + huffWeight[n+1]); + return ((maxSymbolValue+1)/2) + 1; +} + + +size_t HUF_readCTable (HUF_CElt* CTable, U32 maxSymbolValue, const void* src, size_t srcSize) +{ + BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1]; /* init not required, even though some static analyzer may complain */ + U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1]; /* large enough for values from 0 to 16 */ + U32 tableLog = 0; + U32 nbSymbols = 0; + + /* get symbol weights */ + CHECK_V_F(readSize, HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX+1, rankVal, &nbSymbols, &tableLog, src, srcSize)); + + /* check result */ + if (tableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge); + if (nbSymbols > maxSymbolValue+1) return ERROR(maxSymbolValue_tooSmall); + + /* Prepare base value per rank */ + { U32 n, nextRankStart = 0; + for (n=1; n<=tableLog; n++) { + U32 current = nextRankStart; + nextRankStart += (rankVal[n] << (n-1)); + rankVal[n] = current; + } } + + /* fill nbBits */ + { U32 n; for (n=0; n<nbSymbols; n++) { + const U32 w = huffWeight[n]; + CTable[n].nbBits = (BYTE)(tableLog + 1 - w); + } } + + /* fill val */ + { U16 nbPerRank[HUF_TABLELOG_MAX+2] = {0}; /* support w=0=>n=tableLog+1 */ + U16 valPerRank[HUF_TABLELOG_MAX+2] = {0}; + { U32 n; for (n=0; n<nbSymbols; n++) nbPerRank[CTable[n].nbBits]++; } + /* determine stating value per rank */ + valPerRank[tableLog+1] = 0; /* for w==0 */ + { U16 min = 0; + U32 n; for (n=tableLog; n>0; n--) { /* start at n=tablelog <-> w=1 */ + valPerRank[n] = min; /* get starting value within each rank */ + min += nbPerRank[n]; + min >>= 1; + } } + /* assign value within rank, symbol order */ + { U32 n; for (n=0; n<=maxSymbolValue; n++) CTable[n].val = valPerRank[CTable[n].nbBits]++; } + } + + return readSize; +} + + +typedef struct nodeElt_s { + U32 count; + U16 parent; + BYTE byte; + BYTE nbBits; +} nodeElt; + +static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits) +{ + const U32 largestBits = huffNode[lastNonNull].nbBits; + if (largestBits <= maxNbBits) return largestBits; /* early exit : no elt > maxNbBits */ + + /* there are several too large elements (at least >= 2) */ + { int totalCost = 0; + const U32 baseCost = 1 << (largestBits - maxNbBits); + U32 n = lastNonNull; + + while (huffNode[n].nbBits > maxNbBits) { + totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits)); + huffNode[n].nbBits = (BYTE)maxNbBits; + n --; + } /* n stops at huffNode[n].nbBits <= maxNbBits */ + while (huffNode[n].nbBits == maxNbBits) n--; /* n end at index of smallest symbol using < maxNbBits */ + + /* renorm totalCost */ + totalCost >>= (largestBits - maxNbBits); /* note : totalCost is necessarily a multiple of baseCost */ + + /* repay normalized cost */ + { U32 const noSymbol = 0xF0F0F0F0; + U32 rankLast[HUF_TABLELOG_MAX+2]; + int pos; + + /* Get pos of last (smallest) symbol per rank */ + memset(rankLast, 0xF0, sizeof(rankLast)); + { U32 currentNbBits = maxNbBits; + for (pos=n ; pos >= 0; pos--) { + if (huffNode[pos].nbBits >= currentNbBits) continue; + currentNbBits = huffNode[pos].nbBits; /* < maxNbBits */ + rankLast[maxNbBits-currentNbBits] = pos; + } } + + while (totalCost > 0) { + U32 nBitsToDecrease = BIT_highbit32(totalCost) + 1; + for ( ; nBitsToDecrease > 1; nBitsToDecrease--) { + U32 highPos = rankLast[nBitsToDecrease]; + U32 lowPos = rankLast[nBitsToDecrease-1]; + if (highPos == noSymbol) continue; + if (lowPos == noSymbol) break; + { U32 const highTotal = huffNode[highPos].count; + U32 const lowTotal = 2 * huffNode[lowPos].count; + if (highTotal <= lowTotal) break; + } } + /* only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !) */ + while ((nBitsToDecrease<=HUF_TABLELOG_MAX) && (rankLast[nBitsToDecrease] == noSymbol)) /* HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary */ + nBitsToDecrease ++; + totalCost -= 1 << (nBitsToDecrease-1); + if (rankLast[nBitsToDecrease-1] == noSymbol) + rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease]; /* this rank is no longer empty */ + huffNode[rankLast[nBitsToDecrease]].nbBits ++; + if (rankLast[nBitsToDecrease] == 0) /* special case, reached largest symbol */ + rankLast[nBitsToDecrease] = noSymbol; + else { + rankLast[nBitsToDecrease]--; + if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits-nBitsToDecrease) + rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */ + } } /* while (totalCost > 0) */ + + while (totalCost < 0) { /* Sometimes, cost correction overshoot */ + if (rankLast[1] == noSymbol) { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0 (using maxNbBits) */ + while (huffNode[n].nbBits == maxNbBits) n--; + huffNode[n+1].nbBits--; + rankLast[1] = n+1; + totalCost++; + continue; + } + huffNode[ rankLast[1] + 1 ].nbBits--; + rankLast[1]++; + totalCost ++; + } } } /* there are several too large elements (at least >= 2) */ + + return maxNbBits; +} + + +typedef struct { + U32 base; + U32 current; +} rankPos; + +static void HUF_sort(nodeElt* huffNode, const U32* count, U32 maxSymbolValue) +{ + rankPos rank[32]; + U32 n; + + memset(rank, 0, sizeof(rank)); + for (n=0; n<=maxSymbolValue; n++) { + U32 r = BIT_highbit32(count[n] + 1); + rank[r].base ++; + } + for (n=30; n>0; n--) rank[n-1].base += rank[n].base; + for (n=0; n<32; n++) rank[n].current = rank[n].base; + for (n=0; n<=maxSymbolValue; n++) { + U32 const c = count[n]; + U32 const r = BIT_highbit32(c+1) + 1; + U32 pos = rank[r].current++; + while ((pos > rank[r].base) && (c > huffNode[pos-1].count)) huffNode[pos]=huffNode[pos-1], pos--; + huffNode[pos].count = c; + huffNode[pos].byte = (BYTE)n; + } +} + + +/** HUF_buildCTable_wksp() : + * Same as HUF_buildCTable(), but using externally allocated scratch buffer. + * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned. + */ +#define STARTNODE (HUF_SYMBOLVALUE_MAX+1) +typedef nodeElt huffNodeTable[2*HUF_SYMBOLVALUE_MAX+1 +1]; +size_t HUF_buildCTable_wksp (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize) +{ + nodeElt* const huffNode0 = (nodeElt*)workSpace; + nodeElt* const huffNode = huffNode0+1; + U32 n, nonNullRank; + int lowS, lowN; + U16 nodeNb = STARTNODE; + U32 nodeRoot; + + /* safety checks */ + if (wkspSize < sizeof(huffNodeTable)) return ERROR(GENERIC); /* workSpace is not large enough */ + if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT; + if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(GENERIC); + memset(huffNode0, 0, sizeof(huffNodeTable)); + + /* sort, decreasing order */ + HUF_sort(huffNode, count, maxSymbolValue); + + /* init for parents */ + nonNullRank = maxSymbolValue; + while(huffNode[nonNullRank].count == 0) nonNullRank--; + lowS = nonNullRank; nodeRoot = nodeNb + lowS - 1; lowN = nodeNb; + huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS-1].count; + huffNode[lowS].parent = huffNode[lowS-1].parent = nodeNb; + nodeNb++; lowS-=2; + for (n=nodeNb; n<=nodeRoot; n++) huffNode[n].count = (U32)(1U<<30); + huffNode0[0].count = (U32)(1U<<31); /* fake entry, strong barrier */ + + /* create parents */ + while (nodeNb <= nodeRoot) { + U32 n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++; + U32 n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++; + huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count; + huffNode[n1].parent = huffNode[n2].parent = nodeNb; + nodeNb++; + } + + /* distribute weights (unlimited tree height) */ + huffNode[nodeRoot].nbBits = 0; + for (n=nodeRoot-1; n>=STARTNODE; n--) + huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1; + for (n=0; n<=nonNullRank; n++) + huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1; + + /* enforce maxTableLog */ + maxNbBits = HUF_setMaxHeight(huffNode, nonNullRank, maxNbBits); + + /* fill result into tree (val, nbBits) */ + { U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0}; + U16 valPerRank[HUF_TABLELOG_MAX+1] = {0}; + if (maxNbBits > HUF_TABLELOG_MAX) return ERROR(GENERIC); /* check fit into table */ + for (n=0; n<=nonNullRank; n++) + nbPerRank[huffNode[n].nbBits]++; + /* determine stating value per rank */ + { U16 min = 0; + for (n=maxNbBits; n>0; n--) { + valPerRank[n] = min; /* get starting value within each rank */ + min += nbPerRank[n]; + min >>= 1; + } } + for (n=0; n<=maxSymbolValue; n++) + tree[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */ + for (n=0; n<=maxSymbolValue; n++) + tree[n].val = valPerRank[tree[n].nbBits]++; /* assign value within rank, symbol order */ + } + + return maxNbBits; +} + +/** HUF_buildCTable() : + * Note : count is used before tree is written, so they can safely overlap + */ +size_t HUF_buildCTable (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits) +{ + huffNodeTable nodeTable; + return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, nodeTable, sizeof(nodeTable)); +} + +static size_t HUF_estimateCompressedSize(HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue) +{ + size_t nbBits = 0; + int s; + for (s = 0; s <= (int)maxSymbolValue; ++s) { + nbBits += CTable[s].nbBits * count[s]; + } + return nbBits >> 3; +} + +static int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue) { + int bad = 0; + int s; + for (s = 0; s <= (int)maxSymbolValue; ++s) { + bad |= (count[s] != 0) & (CTable[s].nbBits == 0); + } + return !bad; +} + +static void HUF_encodeSymbol(BIT_CStream_t* bitCPtr, U32 symbol, const HUF_CElt* CTable) +{ + BIT_addBitsFast(bitCPtr, CTable[symbol].val, CTable[symbol].nbBits); +} + +size_t HUF_compressBound(size_t size) { return HUF_COMPRESSBOUND(size); } + +#define HUF_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s)) + +#define HUF_FLUSHBITS_1(stream) \ + if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*2+7) HUF_FLUSHBITS(stream) + +#define HUF_FLUSHBITS_2(stream) \ + if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*4+7) HUF_FLUSHBITS(stream) + +size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable) +{ + const BYTE* ip = (const BYTE*) src; + BYTE* const ostart = (BYTE*)dst; + BYTE* const oend = ostart + dstSize; + BYTE* op = ostart; + size_t n; + const unsigned fast = (dstSize >= HUF_BLOCKBOUND(srcSize)); + BIT_CStream_t bitC; + + /* init */ + if (dstSize < 8) return 0; /* not enough space to compress */ + { size_t const initErr = BIT_initCStream(&bitC, op, oend-op); + if (HUF_isError(initErr)) return 0; } + + n = srcSize & ~3; /* join to mod 4 */ + switch (srcSize & 3) + { + case 3 : HUF_encodeSymbol(&bitC, ip[n+ 2], CTable); + HUF_FLUSHBITS_2(&bitC); + case 2 : HUF_encodeSymbol(&bitC, ip[n+ 1], CTable); + HUF_FLUSHBITS_1(&bitC); + case 1 : HUF_encodeSymbol(&bitC, ip[n+ 0], CTable); + HUF_FLUSHBITS(&bitC); + case 0 : + default: ; + } + + for (; n>0; n-=4) { /* note : n&3==0 at this stage */ + HUF_encodeSymbol(&bitC, ip[n- 1], CTable); + HUF_FLUSHBITS_1(&bitC); + HUF_encodeSymbol(&bitC, ip[n- 2], CTable); + HUF_FLUSHBITS_2(&bitC); + HUF_encodeSymbol(&bitC, ip[n- 3], CTable); + HUF_FLUSHBITS_1(&bitC); + HUF_encodeSymbol(&bitC, ip[n- 4], CTable); + HUF_FLUSHBITS(&bitC); + } + + return BIT_closeCStream(&bitC); +} + + +size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable) +{ + size_t const segmentSize = (srcSize+3)/4; /* first 3 segments */ + const BYTE* ip = (const BYTE*) src; + const BYTE* const iend = ip + srcSize; + BYTE* const ostart = (BYTE*) dst; + BYTE* const oend = ostart + dstSize; + BYTE* op = ostart; + + if (dstSize < 6 + 1 + 1 + 1 + 8) return 0; /* minimum space to compress successfully */ + if (srcSize < 12) return 0; /* no saving possible : too small input */ + op += 6; /* jumpTable */ + + { CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend-op, ip, segmentSize, CTable) ); + if (cSize==0) return 0; + MEM_writeLE16(ostart, (U16)cSize); + op += cSize; + } + + ip += segmentSize; + { CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend-op, ip, segmentSize, CTable) ); + if (cSize==0) return 0; + MEM_writeLE16(ostart+2, (U16)cSize); + op += cSize; + } + + ip += segmentSize; + { CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend-op, ip, segmentSize, CTable) ); + if (cSize==0) return 0; + MEM_writeLE16(ostart+4, (U16)cSize); + op += cSize; + } + + ip += segmentSize; + { CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend-op, ip, iend-ip, CTable) ); + if (cSize==0) return 0; + op += cSize; + } + + return op-ostart; +} + + +static size_t HUF_compressCTable_internal( + BYTE* const ostart, BYTE* op, BYTE* const oend, + const void* src, size_t srcSize, + unsigned singleStream, const HUF_CElt* CTable) +{ + size_t const cSize = singleStream ? + HUF_compress1X_usingCTable(op, oend - op, src, srcSize, CTable) : + HUF_compress4X_usingCTable(op, oend - op, src, srcSize, CTable); + if (HUF_isError(cSize)) { return cSize; } + if (cSize==0) { return 0; } /* uncompressible */ + op += cSize; + /* check compressibility */ + if ((size_t)(op-ostart) >= srcSize-1) { return 0; } + return op-ostart; +} + + +/* `workSpace` must a table of at least 1024 unsigned */ +static size_t HUF_compress_internal ( + void* dst, size_t dstSize, + const void* src, size_t srcSize, + unsigned maxSymbolValue, unsigned huffLog, + unsigned singleStream, + void* workSpace, size_t wkspSize, + HUF_CElt* oldHufTable, HUF_repeat* repeat, int preferRepeat) +{ + BYTE* const ostart = (BYTE*)dst; + BYTE* const oend = ostart + dstSize; + BYTE* op = ostart; + + U32* count; + size_t const countSize = sizeof(U32) * (HUF_SYMBOLVALUE_MAX + 1); + HUF_CElt* CTable; + size_t const CTableSize = sizeof(HUF_CElt) * (HUF_SYMBOLVALUE_MAX + 1); + + /* checks & inits */ + if (wkspSize < sizeof(huffNodeTable) + countSize + CTableSize) return ERROR(GENERIC); + if (!srcSize) return 0; /* Uncompressed (note : 1 means rle, so first byte must be correct) */ + if (!dstSize) return 0; /* cannot fit within dst budget */ + if (srcSize > HUF_BLOCKSIZE_MAX) return ERROR(srcSize_wrong); /* current block size limit */ + if (huffLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge); + if (!maxSymbolValue) maxSymbolValue = HUF_SYMBOLVALUE_MAX; + if (!huffLog) huffLog = HUF_TABLELOG_DEFAULT; + + count = (U32*)workSpace; + workSpace = (BYTE*)workSpace + countSize; + wkspSize -= countSize; + CTable = (HUF_CElt*)workSpace; + workSpace = (BYTE*)workSpace + CTableSize; + wkspSize -= CTableSize; + + /* Heuristic : If we don't need to check the validity of the old table use the old table for small inputs */ + if (preferRepeat && repeat && *repeat == HUF_repeat_valid) { + return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, singleStream, oldHufTable); + } + + /* Scan input and build symbol stats */ + { CHECK_V_F(largest, FSE_count_wksp (count, &maxSymbolValue, (const BYTE*)src, srcSize, (U32*)workSpace) ); + if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 1; } /* single symbol, rle */ + if (largest <= (srcSize >> 7)+1) return 0; /* Fast heuristic : not compressible enough */ + } + + /* Check validity of previous table */ + if (repeat && *repeat == HUF_repeat_check && !HUF_validateCTable(oldHufTable, count, maxSymbolValue)) { + *repeat = HUF_repeat_none; + } + /* Heuristic : use existing table for small inputs */ + if (preferRepeat && repeat && *repeat != HUF_repeat_none) { + return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, singleStream, oldHufTable); + } + + /* Build Huffman Tree */ + huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue); + { CHECK_V_F(maxBits, HUF_buildCTable_wksp (CTable, count, maxSymbolValue, huffLog, workSpace, wkspSize) ); + huffLog = (U32)maxBits; + /* Zero the unused symbols so we can check it for validity */ + memset(CTable + maxSymbolValue + 1, 0, CTableSize - (maxSymbolValue + 1) * sizeof(HUF_CElt)); + } + + /* Write table description header */ + { CHECK_V_F(hSize, HUF_writeCTable (op, dstSize, CTable, maxSymbolValue, huffLog) ); + /* Check if using the previous table will be beneficial */ + if (repeat && *repeat != HUF_repeat_none) { + size_t const oldSize = HUF_estimateCompressedSize(oldHufTable, count, maxSymbolValue); + size_t const newSize = HUF_estimateCompressedSize(CTable, count, maxSymbolValue); + if (oldSize <= hSize + newSize || hSize + 12 >= srcSize) { + return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, singleStream, oldHufTable); + } + } + /* Use the new table */ + if (hSize + 12ul >= srcSize) { return 0; } + op += hSize; + if (repeat) { *repeat = HUF_repeat_none; } + if (oldHufTable) { memcpy(oldHufTable, CTable, CTableSize); } /* Save the new table */ + } + return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, singleStream, CTable); +} + + +size_t HUF_compress1X_wksp (void* dst, size_t dstSize, + const void* src, size_t srcSize, + unsigned maxSymbolValue, unsigned huffLog, + void* workSpace, size_t wkspSize) +{ + return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 1 /* single stream */, workSpace, wkspSize, NULL, NULL, 0); +} + +size_t HUF_compress1X_repeat (void* dst, size_t dstSize, + const void* src, size_t srcSize, + unsigned maxSymbolValue, unsigned huffLog, + void* workSpace, size_t wkspSize, + HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat) +{ + return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 1 /* single stream */, workSpace, wkspSize, hufTable, repeat, preferRepeat); +} + +size_t HUF_compress1X (void* dst, size_t dstSize, + const void* src, size_t srcSize, + unsigned maxSymbolValue, unsigned huffLog) +{ + unsigned workSpace[1024]; + return HUF_compress1X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace)); +} + +size_t HUF_compress4X_wksp (void* dst, size_t dstSize, + const void* src, size_t srcSize, + unsigned maxSymbolValue, unsigned huffLog, + void* workSpace, size_t wkspSize) +{ + return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 0 /* 4 streams */, workSpace, wkspSize, NULL, NULL, 0); +} + +size_t HUF_compress4X_repeat (void* dst, size_t dstSize, + const void* src, size_t srcSize, + unsigned maxSymbolValue, unsigned huffLog, + void* workSpace, size_t wkspSize, + HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat) +{ + return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 0 /* 4 streams */, workSpace, wkspSize, hufTable, repeat, preferRepeat); +} + +size_t HUF_compress2 (void* dst, size_t dstSize, + const void* src, size_t srcSize, + unsigned maxSymbolValue, unsigned huffLog) +{ + unsigned workSpace[1024]; + return HUF_compress4X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace)); +} + +size_t HUF_compress (void* dst, size_t maxDstSize, const void* src, size_t srcSize) +{ + return HUF_compress2(dst, maxDstSize, src, (U32)srcSize, 255, HUF_TABLELOG_DEFAULT); +} diff --git a/thirdparty/zstd/compress/zstd_compress.c b/thirdparty/zstd/compress/zstd_compress.c new file mode 100644 index 0000000000..c08b315dab --- /dev/null +++ b/thirdparty/zstd/compress/zstd_compress.c @@ -0,0 +1,3598 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + + +/*-************************************* +* Dependencies +***************************************/ +#include <string.h> /* memset */ +#include "mem.h" +#define FSE_STATIC_LINKING_ONLY /* FSE_encodeSymbol */ +#include "fse.h" +#define HUF_STATIC_LINKING_ONLY +#include "huf.h" +#include "zstd_internal.h" /* includes zstd.h */ + + +/*-************************************* +* Debug +***************************************/ +#if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=1) +# include <assert.h> +#else +# define assert(condition) ((void)0) +#endif + +#define ZSTD_STATIC_ASSERT(c) { enum { ZSTD_static_assert = 1/(int)(!!(c)) }; } + +#if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=2) +# include <stdio.h> + static unsigned g_debugLevel = ZSTD_DEBUG; +# define DEBUGLOG(l, ...) if (l<=g_debugLevel) { fprintf(stderr, __FILE__ ": "); fprintf(stderr, __VA_ARGS__); fprintf(stderr, " \n"); } +#else +# define DEBUGLOG(l, ...) {} /* disabled */ +#endif + + +/*-************************************* +* Constants +***************************************/ +static const U32 g_searchStrength = 8; /* control skip over incompressible data */ +#define HASH_READ_SIZE 8 +typedef enum { ZSTDcs_created=0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e; + +/* entropy tables always have same size */ +static size_t const hufCTable_size = HUF_CTABLE_SIZE(255); +static size_t const litlengthCTable_size = FSE_CTABLE_SIZE(LLFSELog, MaxLL); +static size_t const offcodeCTable_size = FSE_CTABLE_SIZE(OffFSELog, MaxOff); +static size_t const matchlengthCTable_size = FSE_CTABLE_SIZE(MLFSELog, MaxML); +static size_t const entropyScratchSpace_size = HUF_WORKSPACE_SIZE; + + +/*-************************************* +* Helper functions +***************************************/ +size_t ZSTD_compressBound(size_t srcSize) { + size_t const lowLimit = 256 KB; + size_t const margin = (srcSize < lowLimit) ? (lowLimit-srcSize) >> 12 : 0; /* from 64 to 0 */ + return srcSize + (srcSize >> 8) + margin; +} + + +/*-************************************* +* Sequence storage +***************************************/ +static void ZSTD_resetSeqStore(seqStore_t* ssPtr) +{ + ssPtr->lit = ssPtr->litStart; + ssPtr->sequences = ssPtr->sequencesStart; + ssPtr->longLengthID = 0; +} + + +/*-************************************* +* Context memory management +***************************************/ +struct ZSTD_CCtx_s { + const BYTE* nextSrc; /* next block here to continue on current prefix */ + const BYTE* base; /* All regular indexes relative to this position */ + const BYTE* dictBase; /* extDict indexes relative to this position */ + U32 dictLimit; /* below that point, need extDict */ + U32 lowLimit; /* below that point, no more data */ + U32 nextToUpdate; /* index from which to continue dictionary update */ + U32 nextToUpdate3; /* index from which to continue dictionary update */ + U32 hashLog3; /* dispatch table : larger == faster, more memory */ + U32 loadedDictEnd; /* index of end of dictionary */ + U32 forceWindow; /* force back-references to respect limit of 1<<wLog, even for dictionary */ + U32 forceRawDict; /* Force loading dictionary in "content-only" mode (no header analysis) */ + ZSTD_compressionStage_e stage; + U32 rep[ZSTD_REP_NUM]; + U32 repToConfirm[ZSTD_REP_NUM]; + U32 dictID; + ZSTD_parameters params; + void* workSpace; + size_t workSpaceSize; + size_t blockSize; + U64 frameContentSize; + U64 consumedSrcSize; + XXH64_state_t xxhState; + ZSTD_customMem customMem; + + seqStore_t seqStore; /* sequences storage ptrs */ + U32* hashTable; + U32* hashTable3; + U32* chainTable; + HUF_repeat hufCTable_repeatMode; + HUF_CElt* hufCTable; + U32 fseCTables_ready; + FSE_CTable* offcodeCTable; + FSE_CTable* matchlengthCTable; + FSE_CTable* litlengthCTable; + unsigned* entropyScratchSpace; +}; + +ZSTD_CCtx* ZSTD_createCCtx(void) +{ + return ZSTD_createCCtx_advanced(defaultCustomMem); +} + +ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem) +{ + ZSTD_CCtx* cctx; + + if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem; + if (!customMem.customAlloc || !customMem.customFree) return NULL; + + cctx = (ZSTD_CCtx*) ZSTD_malloc(sizeof(ZSTD_CCtx), customMem); + if (!cctx) return NULL; + memset(cctx, 0, sizeof(ZSTD_CCtx)); + cctx->customMem = customMem; + return cctx; +} + +size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx) +{ + if (cctx==NULL) return 0; /* support free on NULL */ + ZSTD_free(cctx->workSpace, cctx->customMem); + ZSTD_free(cctx, cctx->customMem); + return 0; /* reserved as a potential error code in the future */ +} + +size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx) +{ + if (cctx==NULL) return 0; /* support sizeof on NULL */ + return sizeof(*cctx) + cctx->workSpaceSize; +} + +size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value) +{ + switch(param) + { + case ZSTD_p_forceWindow : cctx->forceWindow = value>0; cctx->loadedDictEnd = 0; return 0; + case ZSTD_p_forceRawDict : cctx->forceRawDict = value>0; return 0; + default: return ERROR(parameter_unknown); + } +} + +const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) /* hidden interface */ +{ + return &(ctx->seqStore); +} + +static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) +{ + return cctx->params; +} + + +/** ZSTD_checkParams() : + ensure param values remain within authorized range. + @return : 0, or an error code if one value is beyond authorized range */ +size_t ZSTD_checkCParams(ZSTD_compressionParameters cParams) +{ +# define CLAMPCHECK(val,min,max) { if ((val<min) | (val>max)) return ERROR(compressionParameter_unsupported); } + CLAMPCHECK(cParams.windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); + CLAMPCHECK(cParams.chainLog, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); + CLAMPCHECK(cParams.hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); + CLAMPCHECK(cParams.searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); + CLAMPCHECK(cParams.searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); + CLAMPCHECK(cParams.targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); + if ((U32)(cParams.strategy) > (U32)ZSTD_btopt2) return ERROR(compressionParameter_unsupported); + return 0; +} + + +/** ZSTD_cycleLog() : + * condition for correct operation : hashLog > 1 */ +static U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat) +{ + U32 const btScale = ((U32)strat >= (U32)ZSTD_btlazy2); + return hashLog - btScale; +} + +/** ZSTD_adjustCParams() : + optimize `cPar` for a given input (`srcSize` and `dictSize`). + mostly downsizing to reduce memory consumption and initialization. + Both `srcSize` and `dictSize` are optional (use 0 if unknown), + but if both are 0, no optimization can be done. + Note : cPar is considered validated at this stage. Use ZSTD_checkParams() to ensure that. */ +ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize) +{ + if (srcSize+dictSize == 0) return cPar; /* no size information available : no adjustment */ + + /* resize params, to use less memory when necessary */ + { U32 const minSrcSize = (srcSize==0) ? 500 : 0; + U64 const rSize = srcSize + dictSize + minSrcSize; + if (rSize < ((U64)1<<ZSTD_WINDOWLOG_MAX)) { + U32 const srcLog = MAX(ZSTD_HASHLOG_MIN, ZSTD_highbit32((U32)(rSize)-1) + 1); + if (cPar.windowLog > srcLog) cPar.windowLog = srcLog; + } } + if (cPar.hashLog > cPar.windowLog) cPar.hashLog = cPar.windowLog; + { U32 const cycleLog = ZSTD_cycleLog(cPar.chainLog, cPar.strategy); + if (cycleLog > cPar.windowLog) cPar.chainLog -= (cycleLog - cPar.windowLog); + } + + if (cPar.windowLog < ZSTD_WINDOWLOG_ABSOLUTEMIN) cPar.windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN; /* required for frame header */ + + return cPar; +} + + +size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams) +{ + size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, (size_t)1 << cParams.windowLog); + U32 const divider = (cParams.searchLength==3) ? 3 : 4; + size_t const maxNbSeq = blockSize / divider; + size_t const tokenSpace = blockSize + 11*maxNbSeq; + + size_t const chainSize = (cParams.strategy == ZSTD_fast) ? 0 : (1 << cParams.chainLog); + size_t const hSize = ((size_t)1) << cParams.hashLog; + U32 const hashLog3 = (cParams.searchLength>3) ? 0 : MIN(ZSTD_HASHLOG3_MAX, cParams.windowLog); + size_t const h3Size = ((size_t)1) << hashLog3; + size_t const entropySpace = hufCTable_size + litlengthCTable_size + + offcodeCTable_size + matchlengthCTable_size + + entropyScratchSpace_size; + size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); + + size_t const optSpace = ((MaxML+1) + (MaxLL+1) + (MaxOff+1) + (1<<Litbits))*sizeof(U32) + + (ZSTD_OPT_NUM+1)*(sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t)); + size_t const neededSpace = entropySpace + tableSpace + tokenSpace + + (((cParams.strategy == ZSTD_btopt) || (cParams.strategy == ZSTD_btopt2)) ? optSpace : 0); + + return sizeof(ZSTD_CCtx) + neededSpace; +} + + +static U32 ZSTD_equivalentParams(ZSTD_parameters param1, ZSTD_parameters param2) +{ + return (param1.cParams.hashLog == param2.cParams.hashLog) + & (param1.cParams.chainLog == param2.cParams.chainLog) + & (param1.cParams.strategy == param2.cParams.strategy) + & ((param1.cParams.searchLength==3) == (param2.cParams.searchLength==3)); +} + +/*! ZSTD_continueCCtx() : + reuse CCtx without reset (note : requires no dictionary) */ +static size_t ZSTD_continueCCtx(ZSTD_CCtx* cctx, ZSTD_parameters params, U64 frameContentSize) +{ + U32 const end = (U32)(cctx->nextSrc - cctx->base); + cctx->params = params; + cctx->frameContentSize = frameContentSize; + cctx->consumedSrcSize = 0; + cctx->lowLimit = end; + cctx->dictLimit = end; + cctx->nextToUpdate = end+1; + cctx->stage = ZSTDcs_init; + cctx->dictID = 0; + cctx->loadedDictEnd = 0; + { int i; for (i=0; i<ZSTD_REP_NUM; i++) cctx->rep[i] = repStartValue[i]; } + cctx->seqStore.litLengthSum = 0; /* force reset of btopt stats */ + XXH64_reset(&cctx->xxhState, 0); + return 0; +} + +typedef enum { ZSTDcrp_continue, ZSTDcrp_noMemset, ZSTDcrp_fullReset } ZSTD_compResetPolicy_e; + +/*! ZSTD_resetCCtx_internal() : + note : `params` must be validated */ +static size_t ZSTD_resetCCtx_internal (ZSTD_CCtx* zc, + ZSTD_parameters params, U64 frameContentSize, + ZSTD_compResetPolicy_e const crp) +{ + if (crp == ZSTDcrp_continue) + if (ZSTD_equivalentParams(params, zc->params)) { + zc->fseCTables_ready = 0; + zc->hufCTable_repeatMode = HUF_repeat_none; + return ZSTD_continueCCtx(zc, params, frameContentSize); + } + + { size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, (size_t)1 << params.cParams.windowLog); + U32 const divider = (params.cParams.searchLength==3) ? 3 : 4; + size_t const maxNbSeq = blockSize / divider; + size_t const tokenSpace = blockSize + 11*maxNbSeq; + size_t const chainSize = (params.cParams.strategy == ZSTD_fast) ? 0 : (1 << params.cParams.chainLog); + size_t const hSize = ((size_t)1) << params.cParams.hashLog; + U32 const hashLog3 = (params.cParams.searchLength>3) ? 0 : MIN(ZSTD_HASHLOG3_MAX, params.cParams.windowLog); + size_t const h3Size = ((size_t)1) << hashLog3; + size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); + void* ptr; + + /* Check if workSpace is large enough, alloc a new one if needed */ + { size_t const entropySpace = hufCTable_size + litlengthCTable_size + + offcodeCTable_size + matchlengthCTable_size + + entropyScratchSpace_size; + size_t const optPotentialSpace = ((MaxML+1) + (MaxLL+1) + (MaxOff+1) + (1<<Litbits)) * sizeof(U32) + + (ZSTD_OPT_NUM+1) * (sizeof(ZSTD_match_t)+sizeof(ZSTD_optimal_t)); + size_t const optSpace = ((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btopt2)) ? optPotentialSpace : 0; + size_t const neededSpace = entropySpace + optSpace + tableSpace + tokenSpace; + if (zc->workSpaceSize < neededSpace) { + zc->workSpaceSize = 0; + ZSTD_free(zc->workSpace, zc->customMem); + zc->workSpace = ZSTD_malloc(neededSpace, zc->customMem); + if (zc->workSpace == NULL) return ERROR(memory_allocation); + zc->workSpaceSize = neededSpace; + ptr = zc->workSpace; + + /* entropy space */ + zc->hufCTable = (HUF_CElt*)ptr; + ptr = (char*)zc->hufCTable + hufCTable_size; /* note : HUF_CElt* is incomplete type, size is estimated via macro */ + zc->offcodeCTable = (FSE_CTable*) ptr; + ptr = (char*)ptr + offcodeCTable_size; + zc->matchlengthCTable = (FSE_CTable*) ptr; + ptr = (char*)ptr + matchlengthCTable_size; + zc->litlengthCTable = (FSE_CTable*) ptr; + ptr = (char*)ptr + litlengthCTable_size; + assert(((size_t)ptr & 3) == 0); /* ensure correct alignment */ + zc->entropyScratchSpace = (unsigned*) ptr; + } } + + /* init params */ + zc->params = params; + zc->blockSize = blockSize; + zc->frameContentSize = frameContentSize; + zc->consumedSrcSize = 0; + + XXH64_reset(&zc->xxhState, 0); + zc->stage = ZSTDcs_init; + zc->dictID = 0; + zc->loadedDictEnd = 0; + zc->fseCTables_ready = 0; + zc->hufCTable_repeatMode = HUF_repeat_none; + zc->nextToUpdate = 1; + zc->nextSrc = NULL; + zc->base = NULL; + zc->dictBase = NULL; + zc->dictLimit = 0; + zc->lowLimit = 0; + { int i; for (i=0; i<ZSTD_REP_NUM; i++) zc->rep[i] = repStartValue[i]; } + zc->hashLog3 = hashLog3; + zc->seqStore.litLengthSum = 0; + + /* ensure entropy tables are close together at the beginning */ + assert((void*)zc->hufCTable == zc->workSpace); + assert((char*)zc->offcodeCTable == (char*)zc->hufCTable + hufCTable_size); + assert((char*)zc->matchlengthCTable == (char*)zc->offcodeCTable + offcodeCTable_size); + assert((char*)zc->litlengthCTable == (char*)zc->matchlengthCTable + matchlengthCTable_size); + assert((char*)zc->entropyScratchSpace == (char*)zc->litlengthCTable + litlengthCTable_size); + ptr = (char*)zc->entropyScratchSpace + entropyScratchSpace_size; + + /* opt parser space */ + if ((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btopt2)) { + assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */ + zc->seqStore.litFreq = (U32*)ptr; + zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<<Litbits); + zc->seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (MaxLL+1); + zc->seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (MaxML+1); + ptr = zc->seqStore.offCodeFreq + (MaxOff+1); + zc->seqStore.matchTable = (ZSTD_match_t*)ptr; + ptr = zc->seqStore.matchTable + ZSTD_OPT_NUM+1; + zc->seqStore.priceTable = (ZSTD_optimal_t*)ptr; + ptr = zc->seqStore.priceTable + ZSTD_OPT_NUM+1; + } + + /* table Space */ + if (crp!=ZSTDcrp_noMemset) memset(ptr, 0, tableSpace); /* reset tables only */ + assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */ + zc->hashTable = (U32*)(ptr); + zc->chainTable = zc->hashTable + hSize; + zc->hashTable3 = zc->chainTable + chainSize; + ptr = zc->hashTable3 + h3Size; + + /* sequences storage */ + zc->seqStore.sequencesStart = (seqDef*)ptr; + ptr = zc->seqStore.sequencesStart + maxNbSeq; + zc->seqStore.llCode = (BYTE*) ptr; + zc->seqStore.mlCode = zc->seqStore.llCode + maxNbSeq; + zc->seqStore.ofCode = zc->seqStore.mlCode + maxNbSeq; + zc->seqStore.litStart = zc->seqStore.ofCode + maxNbSeq; + + return 0; + } +} + +/* ZSTD_invalidateRepCodes() : + * ensures next compression will not use repcodes from previous block. + * Note : only works with regular variant; + * do not use with extDict variant ! */ +void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx) { + int i; + for (i=0; i<ZSTD_REP_NUM; i++) cctx->rep[i] = 0; +} + + +/*! ZSTD_copyCCtx_internal() : + * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. + * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). + * pledgedSrcSize=0 means "empty" if fParams.contentSizeFlag=1 + * @return : 0, or an error code */ +size_t ZSTD_copyCCtx_internal(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx, + ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize) +{ + if (srcCCtx->stage!=ZSTDcs_init) return ERROR(stage_wrong); + + memcpy(&dstCCtx->customMem, &srcCCtx->customMem, sizeof(ZSTD_customMem)); + { ZSTD_parameters params = srcCCtx->params; + params.fParams = fParams; + DEBUGLOG(5, "ZSTD_resetCCtx_internal : dictIDFlag : %u \n", !fParams.noDictIDFlag); + ZSTD_resetCCtx_internal(dstCCtx, params, pledgedSrcSize, ZSTDcrp_noMemset); + } + + /* copy tables */ + { size_t const chainSize = (srcCCtx->params.cParams.strategy == ZSTD_fast) ? 0 : (1 << srcCCtx->params.cParams.chainLog); + size_t const hSize = (size_t)1 << srcCCtx->params.cParams.hashLog; + size_t const h3Size = (size_t)1 << srcCCtx->hashLog3; + size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); + assert((U32*)dstCCtx->chainTable == (U32*)dstCCtx->hashTable + hSize); /* chainTable must follow hashTable */ + assert((U32*)dstCCtx->hashTable3 == (U32*)dstCCtx->chainTable + chainSize); + memcpy(dstCCtx->hashTable, srcCCtx->hashTable, tableSpace); /* presumes all tables follow each other */ + } + + /* copy dictionary offsets */ + dstCCtx->nextToUpdate = srcCCtx->nextToUpdate; + dstCCtx->nextToUpdate3= srcCCtx->nextToUpdate3; + dstCCtx->nextSrc = srcCCtx->nextSrc; + dstCCtx->base = srcCCtx->base; + dstCCtx->dictBase = srcCCtx->dictBase; + dstCCtx->dictLimit = srcCCtx->dictLimit; + dstCCtx->lowLimit = srcCCtx->lowLimit; + dstCCtx->loadedDictEnd= srcCCtx->loadedDictEnd; + dstCCtx->dictID = srcCCtx->dictID; + + /* copy entropy tables */ + dstCCtx->fseCTables_ready = srcCCtx->fseCTables_ready; + if (srcCCtx->fseCTables_ready) { + memcpy(dstCCtx->litlengthCTable, srcCCtx->litlengthCTable, litlengthCTable_size); + memcpy(dstCCtx->matchlengthCTable, srcCCtx->matchlengthCTable, matchlengthCTable_size); + memcpy(dstCCtx->offcodeCTable, srcCCtx->offcodeCTable, offcodeCTable_size); + } + dstCCtx->hufCTable_repeatMode = srcCCtx->hufCTable_repeatMode; + if (srcCCtx->hufCTable_repeatMode) { + memcpy(dstCCtx->hufCTable, srcCCtx->hufCTable, hufCTable_size); + } + + return 0; +} + +/*! ZSTD_copyCCtx() : + * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. + * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). + * pledgedSrcSize==0 means "unknown". +* @return : 0, or an error code */ +size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx, unsigned long long pledgedSrcSize) +{ + ZSTD_frameParameters fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; + fParams.contentSizeFlag = pledgedSrcSize>0; + + return ZSTD_copyCCtx_internal(dstCCtx, srcCCtx, fParams, pledgedSrcSize); +} + + +/*! ZSTD_reduceTable() : + * reduce table indexes by `reducerValue` */ +static void ZSTD_reduceTable (U32* const table, U32 const size, U32 const reducerValue) +{ + U32 u; + for (u=0 ; u < size ; u++) { + if (table[u] < reducerValue) table[u] = 0; + else table[u] -= reducerValue; + } +} + +/*! ZSTD_reduceIndex() : +* rescale all indexes to avoid future overflow (indexes are U32) */ +static void ZSTD_reduceIndex (ZSTD_CCtx* zc, const U32 reducerValue) +{ + { U32 const hSize = 1 << zc->params.cParams.hashLog; + ZSTD_reduceTable(zc->hashTable, hSize, reducerValue); } + + { U32 const chainSize = (zc->params.cParams.strategy == ZSTD_fast) ? 0 : (1 << zc->params.cParams.chainLog); + ZSTD_reduceTable(zc->chainTable, chainSize, reducerValue); } + + { U32 const h3Size = (zc->hashLog3) ? 1 << zc->hashLog3 : 0; + ZSTD_reduceTable(zc->hashTable3, h3Size, reducerValue); } +} + + +/*-******************************************************* +* Block entropic compression +*********************************************************/ + +/* See doc/zstd_compression_format.md for detailed format description */ + +size_t ZSTD_noCompressBlock (void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + if (srcSize + ZSTD_blockHeaderSize > dstCapacity) return ERROR(dstSize_tooSmall); + memcpy((BYTE*)dst + ZSTD_blockHeaderSize, src, srcSize); + MEM_writeLE24(dst, (U32)(srcSize << 2) + (U32)bt_raw); + return ZSTD_blockHeaderSize+srcSize; +} + + +static size_t ZSTD_noCompressLiterals (void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + BYTE* const ostart = (BYTE* const)dst; + U32 const flSize = 1 + (srcSize>31) + (srcSize>4095); + + if (srcSize + flSize > dstCapacity) return ERROR(dstSize_tooSmall); + + switch(flSize) + { + case 1: /* 2 - 1 - 5 */ + ostart[0] = (BYTE)((U32)set_basic + (srcSize<<3)); + break; + case 2: /* 2 - 2 - 12 */ + MEM_writeLE16(ostart, (U16)((U32)set_basic + (1<<2) + (srcSize<<4))); + break; + default: /*note : should not be necessary : flSize is within {1,2,3} */ + case 3: /* 2 - 2 - 20 */ + MEM_writeLE32(ostart, (U32)((U32)set_basic + (3<<2) + (srcSize<<4))); + break; + } + + memcpy(ostart + flSize, src, srcSize); + return srcSize + flSize; +} + +static size_t ZSTD_compressRleLiteralsBlock (void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + BYTE* const ostart = (BYTE* const)dst; + U32 const flSize = 1 + (srcSize>31) + (srcSize>4095); + + (void)dstCapacity; /* dstCapacity already guaranteed to be >=4, hence large enough */ + + switch(flSize) + { + case 1: /* 2 - 1 - 5 */ + ostart[0] = (BYTE)((U32)set_rle + (srcSize<<3)); + break; + case 2: /* 2 - 2 - 12 */ + MEM_writeLE16(ostart, (U16)((U32)set_rle + (1<<2) + (srcSize<<4))); + break; + default: /*note : should not be necessary : flSize is necessarily within {1,2,3} */ + case 3: /* 2 - 2 - 20 */ + MEM_writeLE32(ostart, (U32)((U32)set_rle + (3<<2) + (srcSize<<4))); + break; + } + + ostart[flSize] = *(const BYTE*)src; + return flSize+1; +} + + +static size_t ZSTD_minGain(size_t srcSize) { return (srcSize >> 6) + 2; } + +static size_t ZSTD_compressLiterals (ZSTD_CCtx* zc, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize) +{ + size_t const minGain = ZSTD_minGain(srcSize); + size_t const lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB); + BYTE* const ostart = (BYTE*)dst; + U32 singleStream = srcSize < 256; + symbolEncodingType_e hType = set_compressed; + size_t cLitSize; + + + /* small ? don't even attempt compression (speed opt) */ +# define LITERAL_NOENTROPY 63 + { size_t const minLitSize = zc->hufCTable_repeatMode == HUF_repeat_valid ? 6 : LITERAL_NOENTROPY; + if (srcSize <= minLitSize) return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); + } + + if (dstCapacity < lhSize+1) return ERROR(dstSize_tooSmall); /* not enough space for compression */ + { HUF_repeat repeat = zc->hufCTable_repeatMode; + int const preferRepeat = zc->params.cParams.strategy < ZSTD_lazy ? srcSize <= 1024 : 0; + if (repeat == HUF_repeat_valid && lhSize == 3) singleStream = 1; + cLitSize = singleStream ? HUF_compress1X_repeat(ostart+lhSize, dstCapacity-lhSize, src, srcSize, 255, 11, + zc->entropyScratchSpace, entropyScratchSpace_size, zc->hufCTable, &repeat, preferRepeat) + : HUF_compress4X_repeat(ostart+lhSize, dstCapacity-lhSize, src, srcSize, 255, 11, + zc->entropyScratchSpace, entropyScratchSpace_size, zc->hufCTable, &repeat, preferRepeat); + if (repeat != HUF_repeat_none) { hType = set_repeat; } /* reused the existing table */ + else { zc->hufCTable_repeatMode = HUF_repeat_check; } /* now have a table to reuse */ + } + + if ((cLitSize==0) | (cLitSize >= srcSize - minGain)) { + zc->hufCTable_repeatMode = HUF_repeat_none; + return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); + } + if (cLitSize==1) { + zc->hufCTable_repeatMode = HUF_repeat_none; + return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize); + } + + /* Build header */ + switch(lhSize) + { + case 3: /* 2 - 2 - 10 - 10 */ + { U32 const lhc = hType + ((!singleStream) << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<14); + MEM_writeLE24(ostart, lhc); + break; + } + case 4: /* 2 - 2 - 14 - 14 */ + { U32 const lhc = hType + (2 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<18); + MEM_writeLE32(ostart, lhc); + break; + } + default: /* should not be necessary, lhSize is only {3,4,5} */ + case 5: /* 2 - 2 - 18 - 18 */ + { U32 const lhc = hType + (3 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<22); + MEM_writeLE32(ostart, lhc); + ostart[4] = (BYTE)(cLitSize >> 10); + break; + } + } + return lhSize+cLitSize; +} + +static const BYTE LL_Code[64] = { 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + 16, 16, 17, 17, 18, 18, 19, 19, + 20, 20, 20, 20, 21, 21, 21, 21, + 22, 22, 22, 22, 22, 22, 22, 22, + 23, 23, 23, 23, 23, 23, 23, 23, + 24, 24, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 24, 24, 24 }; + +static const BYTE ML_Code[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, + 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, + 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, + 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, + 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, + 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 }; + + +void ZSTD_seqToCodes(const seqStore_t* seqStorePtr) +{ + BYTE const LL_deltaCode = 19; + BYTE const ML_deltaCode = 36; + const seqDef* const sequences = seqStorePtr->sequencesStart; + BYTE* const llCodeTable = seqStorePtr->llCode; + BYTE* const ofCodeTable = seqStorePtr->ofCode; + BYTE* const mlCodeTable = seqStorePtr->mlCode; + U32 const nbSeq = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); + U32 u; + for (u=0; u<nbSeq; u++) { + U32 const llv = sequences[u].litLength; + U32 const mlv = sequences[u].matchLength; + llCodeTable[u] = (llv> 63) ? (BYTE)ZSTD_highbit32(llv) + LL_deltaCode : LL_Code[llv]; + ofCodeTable[u] = (BYTE)ZSTD_highbit32(sequences[u].offset); + mlCodeTable[u] = (mlv>127) ? (BYTE)ZSTD_highbit32(mlv) + ML_deltaCode : ML_Code[mlv]; + } + if (seqStorePtr->longLengthID==1) + llCodeTable[seqStorePtr->longLengthPos] = MaxLL; + if (seqStorePtr->longLengthID==2) + mlCodeTable[seqStorePtr->longLengthPos] = MaxML; +} + +MEM_STATIC size_t ZSTD_compressSequences (ZSTD_CCtx* zc, + void* dst, size_t dstCapacity, + size_t srcSize) +{ + const int longOffsets = zc->params.cParams.windowLog > STREAM_ACCUMULATOR_MIN; + const seqStore_t* seqStorePtr = &(zc->seqStore); + U32 count[MaxSeq+1]; + S16 norm[MaxSeq+1]; + FSE_CTable* CTable_LitLength = zc->litlengthCTable; + FSE_CTable* CTable_OffsetBits = zc->offcodeCTable; + FSE_CTable* CTable_MatchLength = zc->matchlengthCTable; + U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */ + const seqDef* const sequences = seqStorePtr->sequencesStart; + const BYTE* const ofCodeTable = seqStorePtr->ofCode; + const BYTE* const llCodeTable = seqStorePtr->llCode; + const BYTE* const mlCodeTable = seqStorePtr->mlCode; + BYTE* const ostart = (BYTE*)dst; + BYTE* const oend = ostart + dstCapacity; + BYTE* op = ostart; + size_t const nbSeq = seqStorePtr->sequences - seqStorePtr->sequencesStart; + BYTE* seqHead; + BYTE scratchBuffer[1<<MAX(MLFSELog,LLFSELog)]; + + /* Compress literals */ + { const BYTE* const literals = seqStorePtr->litStart; + size_t const litSize = seqStorePtr->lit - literals; + size_t const cSize = ZSTD_compressLiterals(zc, op, dstCapacity, literals, litSize); + if (ZSTD_isError(cSize)) return cSize; + op += cSize; + } + + /* Sequences Header */ + if ((oend-op) < 3 /*max nbSeq Size*/ + 1 /*seqHead */) return ERROR(dstSize_tooSmall); + if (nbSeq < 0x7F) *op++ = (BYTE)nbSeq; + else if (nbSeq < LONGNBSEQ) op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2; + else op[0]=0xFF, MEM_writeLE16(op+1, (U16)(nbSeq - LONGNBSEQ)), op+=3; + if (nbSeq==0) goto _check_compressibility; + + /* seqHead : flags for FSE encoding type */ + seqHead = op++; + +#define MIN_SEQ_FOR_DYNAMIC_FSE 64 +#define MAX_SEQ_FOR_STATIC_FSE 1000 + + /* convert length/distances into codes */ + ZSTD_seqToCodes(seqStorePtr); + + /* CTable for Literal Lengths */ + { U32 max = MaxLL; + size_t const mostFrequent = FSE_countFast_wksp(count, &max, llCodeTable, nbSeq, zc->entropyScratchSpace); + if ((mostFrequent == nbSeq) && (nbSeq > 2)) { + *op++ = llCodeTable[0]; + FSE_buildCTable_rle(CTable_LitLength, (BYTE)max); + LLtype = set_rle; + } else if ((zc->fseCTables_ready) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { + LLtype = set_repeat; + } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (LL_defaultNormLog-1)))) { + FSE_buildCTable_wksp(CTable_LitLength, LL_defaultNorm, MaxLL, LL_defaultNormLog, scratchBuffer, sizeof(scratchBuffer)); + LLtype = set_basic; + } else { + size_t nbSeq_1 = nbSeq; + const U32 tableLog = FSE_optimalTableLog(LLFSELog, nbSeq, max); + if (count[llCodeTable[nbSeq-1]]>1) { count[llCodeTable[nbSeq-1]]--; nbSeq_1--; } + FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); + { size_t const NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */ + if (FSE_isError(NCountSize)) return NCountSize; + op += NCountSize; } + FSE_buildCTable_wksp(CTable_LitLength, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)); + LLtype = set_compressed; + } } + + /* CTable for Offsets */ + { U32 max = MaxOff; + size_t const mostFrequent = FSE_countFast_wksp(count, &max, ofCodeTable, nbSeq, zc->entropyScratchSpace); + if ((mostFrequent == nbSeq) && (nbSeq > 2)) { + *op++ = ofCodeTable[0]; + FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max); + Offtype = set_rle; + } else if ((zc->fseCTables_ready) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { + Offtype = set_repeat; + } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (OF_defaultNormLog-1)))) { + FSE_buildCTable_wksp(CTable_OffsetBits, OF_defaultNorm, MaxOff, OF_defaultNormLog, scratchBuffer, sizeof(scratchBuffer)); + Offtype = set_basic; + } else { + size_t nbSeq_1 = nbSeq; + const U32 tableLog = FSE_optimalTableLog(OffFSELog, nbSeq, max); + if (count[ofCodeTable[nbSeq-1]]>1) { count[ofCodeTable[nbSeq-1]]--; nbSeq_1--; } + FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); + { size_t const NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */ + if (FSE_isError(NCountSize)) return NCountSize; + op += NCountSize; } + FSE_buildCTable_wksp(CTable_OffsetBits, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)); + Offtype = set_compressed; + } } + + /* CTable for MatchLengths */ + { U32 max = MaxML; + size_t const mostFrequent = FSE_countFast_wksp(count, &max, mlCodeTable, nbSeq, zc->entropyScratchSpace); + if ((mostFrequent == nbSeq) && (nbSeq > 2)) { + *op++ = *mlCodeTable; + FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max); + MLtype = set_rle; + } else if ((zc->fseCTables_ready) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { + MLtype = set_repeat; + } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (ML_defaultNormLog-1)))) { + FSE_buildCTable_wksp(CTable_MatchLength, ML_defaultNorm, MaxML, ML_defaultNormLog, scratchBuffer, sizeof(scratchBuffer)); + MLtype = set_basic; + } else { + size_t nbSeq_1 = nbSeq; + const U32 tableLog = FSE_optimalTableLog(MLFSELog, nbSeq, max); + if (count[mlCodeTable[nbSeq-1]]>1) { count[mlCodeTable[nbSeq-1]]--; nbSeq_1--; } + FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); + { size_t const NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */ + if (FSE_isError(NCountSize)) return NCountSize; + op += NCountSize; } + FSE_buildCTable_wksp(CTable_MatchLength, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)); + MLtype = set_compressed; + } } + + *seqHead = (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2)); + zc->fseCTables_ready = 0; + + /* Encoding Sequences */ + { BIT_CStream_t blockStream; + FSE_CState_t stateMatchLength; + FSE_CState_t stateOffsetBits; + FSE_CState_t stateLitLength; + + CHECK_E(BIT_initCStream(&blockStream, op, oend-op), dstSize_tooSmall); /* not enough space remaining */ + + /* first symbols */ + FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq-1]); + FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq-1]); + FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq-1]); + BIT_addBits(&blockStream, sequences[nbSeq-1].litLength, LL_bits[llCodeTable[nbSeq-1]]); + if (MEM_32bits()) BIT_flushBits(&blockStream); + BIT_addBits(&blockStream, sequences[nbSeq-1].matchLength, ML_bits[mlCodeTable[nbSeq-1]]); + if (MEM_32bits()) BIT_flushBits(&blockStream); + if (longOffsets) { + U32 const ofBits = ofCodeTable[nbSeq-1]; + int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); + if (extraBits) { + BIT_addBits(&blockStream, sequences[nbSeq-1].offset, extraBits); + BIT_flushBits(&blockStream); + } + BIT_addBits(&blockStream, sequences[nbSeq-1].offset >> extraBits, + ofBits - extraBits); + } else { + BIT_addBits(&blockStream, sequences[nbSeq-1].offset, ofCodeTable[nbSeq-1]); + } + BIT_flushBits(&blockStream); + + { size_t n; + for (n=nbSeq-2 ; n<nbSeq ; n--) { /* intentional underflow */ + BYTE const llCode = llCodeTable[n]; + BYTE const ofCode = ofCodeTable[n]; + BYTE const mlCode = mlCodeTable[n]; + U32 const llBits = LL_bits[llCode]; + U32 const ofBits = ofCode; /* 32b*/ /* 64b*/ + U32 const mlBits = ML_bits[mlCode]; + /* (7)*/ /* (7)*/ + FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); /* 15 */ /* 15 */ + FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 24 */ /* 24 */ + if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/ + FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); /* 16 */ /* 33 */ + if (MEM_32bits() || (ofBits+mlBits+llBits >= 64-7-(LLFSELog+MLFSELog+OffFSELog))) + BIT_flushBits(&blockStream); /* (7)*/ + BIT_addBits(&blockStream, sequences[n].litLength, llBits); + if (MEM_32bits() && ((llBits+mlBits)>24)) BIT_flushBits(&blockStream); + BIT_addBits(&blockStream, sequences[n].matchLength, mlBits); + if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/ + if (longOffsets) { + int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); + if (extraBits) { + BIT_addBits(&blockStream, sequences[n].offset, extraBits); + BIT_flushBits(&blockStream); /* (7)*/ + } + BIT_addBits(&blockStream, sequences[n].offset >> extraBits, + ofBits - extraBits); /* 31 */ + } else { + BIT_addBits(&blockStream, sequences[n].offset, ofBits); /* 31 */ + } + BIT_flushBits(&blockStream); /* (7)*/ + } } + + FSE_flushCState(&blockStream, &stateMatchLength); + FSE_flushCState(&blockStream, &stateOffsetBits); + FSE_flushCState(&blockStream, &stateLitLength); + + { size_t const streamSize = BIT_closeCStream(&blockStream); + if (streamSize==0) return ERROR(dstSize_tooSmall); /* not enough space */ + op += streamSize; + } } + + /* check compressibility */ +_check_compressibility: + { size_t const minGain = ZSTD_minGain(srcSize); + size_t const maxCSize = srcSize - minGain; + if ((size_t)(op-ostart) >= maxCSize) { + zc->hufCTable_repeatMode = HUF_repeat_none; + return 0; + } } + + /* confirm repcodes */ + { int i; for (i=0; i<ZSTD_REP_NUM; i++) zc->rep[i] = zc->repToConfirm[i]; } + + return op - ostart; +} + +#if 0 /* for debug */ +# define STORESEQ_DEBUG +#include <stdio.h> /* fprintf */ +U32 g_startDebug = 0; +const BYTE* g_start = NULL; +#endif + +/*! ZSTD_storeSeq() : + Store a sequence (literal length, literals, offset code and match length code) into seqStore_t. + `offsetCode` : distance to match, or 0 == repCode. + `matchCode` : matchLength - MINMATCH +*/ +MEM_STATIC void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const void* literals, U32 offsetCode, size_t matchCode) +{ +#ifdef STORESEQ_DEBUG + if (g_startDebug) { + const U32 pos = (U32)((const BYTE*)literals - g_start); + if (g_start==NULL) g_start = (const BYTE*)literals; + if ((pos > 1895000) && (pos < 1895300)) + DEBUGLOG(5, "Cpos %6u :%5u literals & match %3u bytes at distance %6u \n", + pos, (U32)litLength, (U32)matchCode+MINMATCH, (U32)offsetCode); + } +#endif + /* copy Literals */ + ZSTD_wildcopy(seqStorePtr->lit, literals, litLength); + seqStorePtr->lit += litLength; + + /* literal Length */ + if (litLength>0xFFFF) { + seqStorePtr->longLengthID = 1; + seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); + } + seqStorePtr->sequences[0].litLength = (U16)litLength; + + /* match offset */ + seqStorePtr->sequences[0].offset = offsetCode + 1; + + /* match Length */ + if (matchCode>0xFFFF) { + seqStorePtr->longLengthID = 2; + seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); + } + seqStorePtr->sequences[0].matchLength = (U16)matchCode; + + seqStorePtr->sequences++; +} + + +/*-************************************* +* Match length counter +***************************************/ +static unsigned ZSTD_NbCommonBytes (register size_t val) +{ + if (MEM_isLittleEndian()) { + if (MEM_64bits()) { +# if defined(_MSC_VER) && defined(_WIN64) + unsigned long r = 0; + _BitScanForward64( &r, (U64)val ); + return (unsigned)(r>>3); +# elif defined(__GNUC__) && (__GNUC__ >= 3) + return (__builtin_ctzll((U64)val) >> 3); +# else + static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, + 0, 3, 1, 3, 1, 4, 2, 7, + 0, 2, 3, 6, 1, 5, 3, 5, + 1, 3, 4, 4, 2, 5, 6, 7, + 7, 0, 1, 2, 3, 3, 4, 6, + 2, 6, 5, 5, 3, 4, 5, 6, + 7, 1, 2, 4, 6, 4, 4, 5, + 7, 2, 6, 5, 7, 6, 7, 7 }; + return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; +# endif + } else { /* 32 bits */ +# if defined(_MSC_VER) + unsigned long r=0; + _BitScanForward( &r, (U32)val ); + return (unsigned)(r>>3); +# elif defined(__GNUC__) && (__GNUC__ >= 3) + return (__builtin_ctz((U32)val) >> 3); +# else + static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, + 3, 2, 2, 1, 3, 2, 0, 1, + 3, 3, 1, 2, 2, 2, 2, 0, + 3, 1, 2, 0, 1, 0, 1, 1 }; + return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; +# endif + } + } else { /* Big Endian CPU */ + if (MEM_64bits()) { +# if defined(_MSC_VER) && defined(_WIN64) + unsigned long r = 0; + _BitScanReverse64( &r, val ); + return (unsigned)(r>>3); +# elif defined(__GNUC__) && (__GNUC__ >= 3) + return (__builtin_clzll(val) >> 3); +# else + unsigned r; + const unsigned n32 = sizeof(size_t)*4; /* calculate this way due to compiler complaining in 32-bits mode */ + if (!(val>>n32)) { r=4; } else { r=0; val>>=n32; } + if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } + r += (!val); + return r; +# endif + } else { /* 32 bits */ +# if defined(_MSC_VER) + unsigned long r = 0; + _BitScanReverse( &r, (unsigned long)val ); + return (unsigned)(r>>3); +# elif defined(__GNUC__) && (__GNUC__ >= 3) + return (__builtin_clz((U32)val) >> 3); +# else + unsigned r; + if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } + r += (!val); + return r; +# endif + } } +} + + +static size_t ZSTD_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* const pInLimit) +{ + const BYTE* const pStart = pIn; + const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1); + + while (pIn < pInLoopLimit) { + size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn); + if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; } + pIn += ZSTD_NbCommonBytes(diff); + return (size_t)(pIn - pStart); + } + if (MEM_64bits()) if ((pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; } + if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; } + if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++; + return (size_t)(pIn - pStart); +} + +/** ZSTD_count_2segments() : +* can count match length with `ip` & `match` in 2 different segments. +* convention : on reaching mEnd, match count continue starting from iStart +*/ +static size_t ZSTD_count_2segments(const BYTE* ip, const BYTE* match, const BYTE* iEnd, const BYTE* mEnd, const BYTE* iStart) +{ + const BYTE* const vEnd = MIN( ip + (mEnd - match), iEnd); + size_t const matchLength = ZSTD_count(ip, match, vEnd); + if (match + matchLength != mEnd) return matchLength; + return matchLength + ZSTD_count(ip+matchLength, iStart, iEnd); +} + + +/*-************************************* +* Hashes +***************************************/ +static const U32 prime3bytes = 506832829U; +static U32 ZSTD_hash3(U32 u, U32 h) { return ((u << (32-24)) * prime3bytes) >> (32-h) ; } +MEM_STATIC size_t ZSTD_hash3Ptr(const void* ptr, U32 h) { return ZSTD_hash3(MEM_readLE32(ptr), h); } /* only in zstd_opt.h */ + +static const U32 prime4bytes = 2654435761U; +static U32 ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32-h) ; } +static size_t ZSTD_hash4Ptr(const void* ptr, U32 h) { return ZSTD_hash4(MEM_read32(ptr), h); } + +static const U64 prime5bytes = 889523592379ULL; +static size_t ZSTD_hash5(U64 u, U32 h) { return (size_t)(((u << (64-40)) * prime5bytes) >> (64-h)) ; } +static size_t ZSTD_hash5Ptr(const void* p, U32 h) { return ZSTD_hash5(MEM_readLE64(p), h); } + +static const U64 prime6bytes = 227718039650203ULL; +static size_t ZSTD_hash6(U64 u, U32 h) { return (size_t)(((u << (64-48)) * prime6bytes) >> (64-h)) ; } +static size_t ZSTD_hash6Ptr(const void* p, U32 h) { return ZSTD_hash6(MEM_readLE64(p), h); } + +static const U64 prime7bytes = 58295818150454627ULL; +static size_t ZSTD_hash7(U64 u, U32 h) { return (size_t)(((u << (64-56)) * prime7bytes) >> (64-h)) ; } +static size_t ZSTD_hash7Ptr(const void* p, U32 h) { return ZSTD_hash7(MEM_readLE64(p), h); } + +static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL; +static size_t ZSTD_hash8(U64 u, U32 h) { return (size_t)(((u) * prime8bytes) >> (64-h)) ; } +static size_t ZSTD_hash8Ptr(const void* p, U32 h) { return ZSTD_hash8(MEM_readLE64(p), h); } + +static size_t ZSTD_hashPtr(const void* p, U32 hBits, U32 mls) +{ + switch(mls) + { + default: + case 4: return ZSTD_hash4Ptr(p, hBits); + case 5: return ZSTD_hash5Ptr(p, hBits); + case 6: return ZSTD_hash6Ptr(p, hBits); + case 7: return ZSTD_hash7Ptr(p, hBits); + case 8: return ZSTD_hash8Ptr(p, hBits); + } +} + + +/*-************************************* +* Fast Scan +***************************************/ +static void ZSTD_fillHashTable (ZSTD_CCtx* zc, const void* end, const U32 mls) +{ + U32* const hashTable = zc->hashTable; + U32 const hBits = zc->params.cParams.hashLog; + const BYTE* const base = zc->base; + const BYTE* ip = base + zc->nextToUpdate; + const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; + const size_t fastHashFillStep = 3; + + while(ip <= iend) { + hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip - base); + ip += fastHashFillStep; + } +} + + +FORCE_INLINE +void ZSTD_compressBlock_fast_generic(ZSTD_CCtx* cctx, + const void* src, size_t srcSize, + const U32 mls) +{ + U32* const hashTable = cctx->hashTable; + U32 const hBits = cctx->params.cParams.hashLog; + seqStore_t* seqStorePtr = &(cctx->seqStore); + const BYTE* const base = cctx->base; + const BYTE* const istart = (const BYTE*)src; + const BYTE* ip = istart; + const BYTE* anchor = istart; + const U32 lowestIndex = cctx->dictLimit; + const BYTE* const lowest = base + lowestIndex; + const BYTE* const iend = istart + srcSize; + const BYTE* const ilimit = iend - HASH_READ_SIZE; + U32 offset_1=cctx->rep[0], offset_2=cctx->rep[1]; + U32 offsetSaved = 0; + + /* init */ + ip += (ip==lowest); + { U32 const maxRep = (U32)(ip-lowest); + if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0; + if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0; + } + + /* Main Search Loop */ + while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ + size_t mLength; + size_t const h = ZSTD_hashPtr(ip, hBits, mls); + U32 const current = (U32)(ip-base); + U32 const matchIndex = hashTable[h]; + const BYTE* match = base + matchIndex; + hashTable[h] = current; /* update hash table */ + + if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) { + mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; + ip++; + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); + } else { + U32 offset; + if ( (matchIndex <= lowestIndex) || (MEM_read32(match) != MEM_read32(ip)) ) { + ip += ((ip-anchor) >> g_searchStrength) + 1; + continue; + } + mLength = ZSTD_count(ip+4, match+4, iend) + 4; + offset = (U32)(ip-match); + while (((ip>anchor) & (match>lowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ + offset_2 = offset_1; + offset_1 = offset; + + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); + } + + /* match found */ + ip += mLength; + anchor = ip; + + if (ip <= ilimit) { + /* Fill Table */ + hashTable[ZSTD_hashPtr(base+current+2, hBits, mls)] = current+2; /* here because current+2 could be > iend-8 */ + hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); + /* check immediate repcode */ + while ( (ip <= ilimit) + && ( (offset_2>0) + & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { + /* store sequence */ + size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4; + { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */ + hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base); + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength-MINMATCH); + ip += rLength; + anchor = ip; + continue; /* faster when present ... (?) */ + } } } + + /* save reps for next block */ + cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved; + cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved; + + /* Last Literals */ + { size_t const lastLLSize = iend - anchor; + memcpy(seqStorePtr->lit, anchor, lastLLSize); + seqStorePtr->lit += lastLLSize; + } +} + + +static void ZSTD_compressBlock_fast(ZSTD_CCtx* ctx, + const void* src, size_t srcSize) +{ + const U32 mls = ctx->params.cParams.searchLength; + switch(mls) + { + default: /* includes case 3 */ + case 4 : + ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 4); return; + case 5 : + ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 5); return; + case 6 : + ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 6); return; + case 7 : + ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 7); return; + } +} + + +static void ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx* ctx, + const void* src, size_t srcSize, + const U32 mls) +{ + U32* hashTable = ctx->hashTable; + const U32 hBits = ctx->params.cParams.hashLog; + seqStore_t* seqStorePtr = &(ctx->seqStore); + const BYTE* const base = ctx->base; + const BYTE* const dictBase = ctx->dictBase; + const BYTE* const istart = (const BYTE*)src; + const BYTE* ip = istart; + const BYTE* anchor = istart; + const U32 lowestIndex = ctx->lowLimit; + const BYTE* const dictStart = dictBase + lowestIndex; + const U32 dictLimit = ctx->dictLimit; + const BYTE* const lowPrefixPtr = base + dictLimit; + const BYTE* const dictEnd = dictBase + dictLimit; + const BYTE* const iend = istart + srcSize; + const BYTE* const ilimit = iend - 8; + U32 offset_1=ctx->rep[0], offset_2=ctx->rep[1]; + + /* Search Loop */ + while (ip < ilimit) { /* < instead of <=, because (ip+1) */ + const size_t h = ZSTD_hashPtr(ip, hBits, mls); + const U32 matchIndex = hashTable[h]; + const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base; + const BYTE* match = matchBase + matchIndex; + const U32 current = (U32)(ip-base); + const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */ + const BYTE* repBase = repIndex < dictLimit ? dictBase : base; + const BYTE* repMatch = repBase + repIndex; + size_t mLength; + hashTable[h] = current; /* update hash table */ + + if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) + && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { + const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend; + mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4; + ip++; + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); + } else { + if ( (matchIndex < lowestIndex) || + (MEM_read32(match) != MEM_read32(ip)) ) { + ip += ((ip-anchor) >> g_searchStrength) + 1; + continue; + } + { const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend; + const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr; + U32 offset; + mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4; + while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ + offset = current - matchIndex; + offset_2 = offset_1; + offset_1 = offset; + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); + } } + + /* found a match : store it */ + ip += mLength; + anchor = ip; + + if (ip <= ilimit) { + /* Fill Table */ + hashTable[ZSTD_hashPtr(base+current+2, hBits, mls)] = current+2; + hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); + /* check immediate repcode */ + while (ip <= ilimit) { + U32 const current2 = (U32)(ip-base); + U32 const repIndex2 = current2 - offset_2; + const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2; + if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */ + && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { + const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend; + size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4; + U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2-MINMATCH); + hashTable[ZSTD_hashPtr(ip, hBits, mls)] = current2; + ip += repLength2; + anchor = ip; + continue; + } + break; + } } } + + /* save reps for next block */ + ctx->repToConfirm[0] = offset_1; ctx->repToConfirm[1] = offset_2; + + /* Last Literals */ + { size_t const lastLLSize = iend - anchor; + memcpy(seqStorePtr->lit, anchor, lastLLSize); + seqStorePtr->lit += lastLLSize; + } +} + + +static void ZSTD_compressBlock_fast_extDict(ZSTD_CCtx* ctx, + const void* src, size_t srcSize) +{ + U32 const mls = ctx->params.cParams.searchLength; + switch(mls) + { + default: /* includes case 3 */ + case 4 : + ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 4); return; + case 5 : + ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 5); return; + case 6 : + ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 6); return; + case 7 : + ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 7); return; + } +} + + +/*-************************************* +* Double Fast +***************************************/ +static void ZSTD_fillDoubleHashTable (ZSTD_CCtx* cctx, const void* end, const U32 mls) +{ + U32* const hashLarge = cctx->hashTable; + U32 const hBitsL = cctx->params.cParams.hashLog; + U32* const hashSmall = cctx->chainTable; + U32 const hBitsS = cctx->params.cParams.chainLog; + const BYTE* const base = cctx->base; + const BYTE* ip = base + cctx->nextToUpdate; + const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; + const size_t fastHashFillStep = 3; + + while(ip <= iend) { + hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base); + hashLarge[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base); + ip += fastHashFillStep; + } +} + + +FORCE_INLINE +void ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx* cctx, + const void* src, size_t srcSize, + const U32 mls) +{ + U32* const hashLong = cctx->hashTable; + const U32 hBitsL = cctx->params.cParams.hashLog; + U32* const hashSmall = cctx->chainTable; + const U32 hBitsS = cctx->params.cParams.chainLog; + seqStore_t* seqStorePtr = &(cctx->seqStore); + const BYTE* const base = cctx->base; + const BYTE* const istart = (const BYTE*)src; + const BYTE* ip = istart; + const BYTE* anchor = istart; + const U32 lowestIndex = cctx->dictLimit; + const BYTE* const lowest = base + lowestIndex; + const BYTE* const iend = istart + srcSize; + const BYTE* const ilimit = iend - HASH_READ_SIZE; + U32 offset_1=cctx->rep[0], offset_2=cctx->rep[1]; + U32 offsetSaved = 0; + + /* init */ + ip += (ip==lowest); + { U32 const maxRep = (U32)(ip-lowest); + if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0; + if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0; + } + + /* Main Search Loop */ + while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ + size_t mLength; + size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8); + size_t const h = ZSTD_hashPtr(ip, hBitsS, mls); + U32 const current = (U32)(ip-base); + U32 const matchIndexL = hashLong[h2]; + U32 const matchIndexS = hashSmall[h]; + const BYTE* matchLong = base + matchIndexL; + const BYTE* match = base + matchIndexS; + hashLong[h2] = hashSmall[h] = current; /* update hash tables */ + + assert(offset_1 <= current); /* supposed guaranteed by construction */ + if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) { + /* favor repcode */ + mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; + ip++; + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); + } else { + U32 offset; + if ( (matchIndexL > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip)) ) { + mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8; + offset = (U32)(ip-matchLong); + while (((ip>anchor) & (matchLong>lowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */ + } else if ( (matchIndexS > lowestIndex) && (MEM_read32(match) == MEM_read32(ip)) ) { + size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8); + U32 const matchIndexL3 = hashLong[hl3]; + const BYTE* matchL3 = base + matchIndexL3; + hashLong[hl3] = current + 1; + if ( (matchIndexL3 > lowestIndex) && (MEM_read64(matchL3) == MEM_read64(ip+1)) ) { + mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8; + ip++; + offset = (U32)(ip-matchL3); + while (((ip>anchor) & (matchL3>lowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */ + } else { + mLength = ZSTD_count(ip+4, match+4, iend) + 4; + offset = (U32)(ip-match); + while (((ip>anchor) & (match>lowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ + } + } else { + ip += ((ip-anchor) >> g_searchStrength) + 1; + continue; + } + + offset_2 = offset_1; + offset_1 = offset; + + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); + } + + /* match found */ + ip += mLength; + anchor = ip; + + if (ip <= ilimit) { + /* Fill Table */ + hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = + hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; /* here because current+2 could be > iend-8 */ + hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = + hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base); + + /* check immediate repcode */ + while ( (ip <= ilimit) + && ( (offset_2>0) + & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { + /* store sequence */ + size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4; + { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */ + hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base); + hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base); + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength-MINMATCH); + ip += rLength; + anchor = ip; + continue; /* faster when present ... (?) */ + } } } + + /* save reps for next block */ + cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved; + cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved; + + /* Last Literals */ + { size_t const lastLLSize = iend - anchor; + memcpy(seqStorePtr->lit, anchor, lastLLSize); + seqStorePtr->lit += lastLLSize; + } +} + + +static void ZSTD_compressBlock_doubleFast(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ + const U32 mls = ctx->params.cParams.searchLength; + switch(mls) + { + default: /* includes case 3 */ + case 4 : + ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 4); return; + case 5 : + ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 5); return; + case 6 : + ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 6); return; + case 7 : + ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 7); return; + } +} + + +static void ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx, + const void* src, size_t srcSize, + const U32 mls) +{ + U32* const hashLong = ctx->hashTable; + U32 const hBitsL = ctx->params.cParams.hashLog; + U32* const hashSmall = ctx->chainTable; + U32 const hBitsS = ctx->params.cParams.chainLog; + seqStore_t* seqStorePtr = &(ctx->seqStore); + const BYTE* const base = ctx->base; + const BYTE* const dictBase = ctx->dictBase; + const BYTE* const istart = (const BYTE*)src; + const BYTE* ip = istart; + const BYTE* anchor = istart; + const U32 lowestIndex = ctx->lowLimit; + const BYTE* const dictStart = dictBase + lowestIndex; + const U32 dictLimit = ctx->dictLimit; + const BYTE* const lowPrefixPtr = base + dictLimit; + const BYTE* const dictEnd = dictBase + dictLimit; + const BYTE* const iend = istart + srcSize; + const BYTE* const ilimit = iend - 8; + U32 offset_1=ctx->rep[0], offset_2=ctx->rep[1]; + + /* Search Loop */ + while (ip < ilimit) { /* < instead of <=, because (ip+1) */ + const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls); + const U32 matchIndex = hashSmall[hSmall]; + const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base; + const BYTE* match = matchBase + matchIndex; + + const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8); + const U32 matchLongIndex = hashLong[hLong]; + const BYTE* matchLongBase = matchLongIndex < dictLimit ? dictBase : base; + const BYTE* matchLong = matchLongBase + matchLongIndex; + + const U32 current = (U32)(ip-base); + const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */ + const BYTE* repBase = repIndex < dictLimit ? dictBase : base; + const BYTE* repMatch = repBase + repIndex; + size_t mLength; + hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */ + + if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) + && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { + const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend; + mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4; + ip++; + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); + } else { + if ((matchLongIndex > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) { + const BYTE* matchEnd = matchLongIndex < dictLimit ? dictEnd : iend; + const BYTE* lowMatchPtr = matchLongIndex < dictLimit ? dictStart : lowPrefixPtr; + U32 offset; + mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, lowPrefixPtr) + 8; + offset = current - matchLongIndex; + while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */ + offset_2 = offset_1; + offset_1 = offset; + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); + + } else if ((matchIndex > lowestIndex) && (MEM_read32(match) == MEM_read32(ip))) { + size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8); + U32 const matchIndex3 = hashLong[h3]; + const BYTE* const match3Base = matchIndex3 < dictLimit ? dictBase : base; + const BYTE* match3 = match3Base + matchIndex3; + U32 offset; + hashLong[h3] = current + 1; + if ( (matchIndex3 > lowestIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) { + const BYTE* matchEnd = matchIndex3 < dictLimit ? dictEnd : iend; + const BYTE* lowMatchPtr = matchIndex3 < dictLimit ? dictStart : lowPrefixPtr; + mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, lowPrefixPtr) + 8; + ip++; + offset = current+1 - matchIndex3; + while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */ + } else { + const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend; + const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr; + mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4; + offset = current - matchIndex; + while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ + } + offset_2 = offset_1; + offset_1 = offset; + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); + + } else { + ip += ((ip-anchor) >> g_searchStrength) + 1; + continue; + } } + + /* found a match : store it */ + ip += mLength; + anchor = ip; + + if (ip <= ilimit) { + /* Fill Table */ + hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; + hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = current+2; + hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base); + hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base); + /* check immediate repcode */ + while (ip <= ilimit) { + U32 const current2 = (U32)(ip-base); + U32 const repIndex2 = current2 - offset_2; + const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2; + if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */ + && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { + const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend; + size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4; + U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2-MINMATCH); + hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2; + hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2; + ip += repLength2; + anchor = ip; + continue; + } + break; + } } } + + /* save reps for next block */ + ctx->repToConfirm[0] = offset_1; ctx->repToConfirm[1] = offset_2; + + /* Last Literals */ + { size_t const lastLLSize = iend - anchor; + memcpy(seqStorePtr->lit, anchor, lastLLSize); + seqStorePtr->lit += lastLLSize; + } +} + + +static void ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx* ctx, + const void* src, size_t srcSize) +{ + U32 const mls = ctx->params.cParams.searchLength; + switch(mls) + { + default: /* includes case 3 */ + case 4 : + ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 4); return; + case 5 : + ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 5); return; + case 6 : + ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 6); return; + case 7 : + ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 7); return; + } +} + + +/*-************************************* +* Binary Tree search +***************************************/ +/** ZSTD_insertBt1() : add one or multiple positions to tree. +* ip : assumed <= iend-8 . +* @return : nb of positions added */ +static U32 ZSTD_insertBt1(ZSTD_CCtx* zc, const BYTE* const ip, const U32 mls, const BYTE* const iend, U32 nbCompares, + U32 extDict) +{ + U32* const hashTable = zc->hashTable; + U32 const hashLog = zc->params.cParams.hashLog; + size_t const h = ZSTD_hashPtr(ip, hashLog, mls); + U32* const bt = zc->chainTable; + U32 const btLog = zc->params.cParams.chainLog - 1; + U32 const btMask = (1 << btLog) - 1; + U32 matchIndex = hashTable[h]; + size_t commonLengthSmaller=0, commonLengthLarger=0; + const BYTE* const base = zc->base; + const BYTE* const dictBase = zc->dictBase; + const U32 dictLimit = zc->dictLimit; + const BYTE* const dictEnd = dictBase + dictLimit; + const BYTE* const prefixStart = base + dictLimit; + const BYTE* match; + const U32 current = (U32)(ip-base); + const U32 btLow = btMask >= current ? 0 : current - btMask; + U32* smallerPtr = bt + 2*(current&btMask); + U32* largerPtr = smallerPtr + 1; + U32 dummy32; /* to be nullified at the end */ + U32 const windowLow = zc->lowLimit; + U32 matchEndIdx = current+8; + size_t bestLength = 8; +#ifdef ZSTD_C_PREDICT + U32 predictedSmall = *(bt + 2*((current-1)&btMask) + 0); + U32 predictedLarge = *(bt + 2*((current-1)&btMask) + 1); + predictedSmall += (predictedSmall>0); + predictedLarge += (predictedLarge>0); +#endif /* ZSTD_C_PREDICT */ + + hashTable[h] = current; /* Update Hash Table */ + + while (nbCompares-- && (matchIndex > windowLow)) { + U32* const nextPtr = bt + 2*(matchIndex & btMask); + size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ + +#ifdef ZSTD_C_PREDICT /* note : can create issues when hlog small <= 11 */ + const U32* predictPtr = bt + 2*((matchIndex-1) & btMask); /* written this way, as bt is a roll buffer */ + if (matchIndex == predictedSmall) { + /* no need to check length, result known */ + *smallerPtr = matchIndex; + if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ + smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ + matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ + predictedSmall = predictPtr[1] + (predictPtr[1]>0); + continue; + } + if (matchIndex == predictedLarge) { + *largerPtr = matchIndex; + if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ + largerPtr = nextPtr; + matchIndex = nextPtr[0]; + predictedLarge = predictPtr[0] + (predictPtr[0]>0); + continue; + } +#endif + if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { + match = base + matchIndex; + if (match[matchLength] == ip[matchLength]) + matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iend) +1; + } else { + match = dictBase + matchIndex; + matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); + if (matchIndex+matchLength >= dictLimit) + match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ + } + + if (matchLength > bestLength) { + bestLength = matchLength; + if (matchLength > matchEndIdx - matchIndex) + matchEndIdx = matchIndex + (U32)matchLength; + } + + if (ip+matchLength == iend) /* equal : no way to know if inf or sup */ + break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt the tree */ + + if (match[matchLength] < ip[matchLength]) { /* necessarily within correct buffer */ + /* match is smaller than current */ + *smallerPtr = matchIndex; /* update smaller idx */ + commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ + if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ + smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ + matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ + } else { + /* match is larger than current */ + *largerPtr = matchIndex; + commonLengthLarger = matchLength; + if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ + largerPtr = nextPtr; + matchIndex = nextPtr[0]; + } } + + *smallerPtr = *largerPtr = 0; + if (bestLength > 384) return MIN(192, (U32)(bestLength - 384)); /* speed optimization */ + if (matchEndIdx > current + 8) return matchEndIdx - current - 8; + return 1; +} + + +static size_t ZSTD_insertBtAndFindBestMatch ( + ZSTD_CCtx* zc, + const BYTE* const ip, const BYTE* const iend, + size_t* offsetPtr, + U32 nbCompares, const U32 mls, + U32 extDict) +{ + U32* const hashTable = zc->hashTable; + U32 const hashLog = zc->params.cParams.hashLog; + size_t const h = ZSTD_hashPtr(ip, hashLog, mls); + U32* const bt = zc->chainTable; + U32 const btLog = zc->params.cParams.chainLog - 1; + U32 const btMask = (1 << btLog) - 1; + U32 matchIndex = hashTable[h]; + size_t commonLengthSmaller=0, commonLengthLarger=0; + const BYTE* const base = zc->base; + const BYTE* const dictBase = zc->dictBase; + const U32 dictLimit = zc->dictLimit; + const BYTE* const dictEnd = dictBase + dictLimit; + const BYTE* const prefixStart = base + dictLimit; + const U32 current = (U32)(ip-base); + const U32 btLow = btMask >= current ? 0 : current - btMask; + const U32 windowLow = zc->lowLimit; + U32* smallerPtr = bt + 2*(current&btMask); + U32* largerPtr = bt + 2*(current&btMask) + 1; + U32 matchEndIdx = current+8; + U32 dummy32; /* to be nullified at the end */ + size_t bestLength = 0; + + hashTable[h] = current; /* Update Hash Table */ + + while (nbCompares-- && (matchIndex > windowLow)) { + U32* const nextPtr = bt + 2*(matchIndex & btMask); + size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ + const BYTE* match; + + if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { + match = base + matchIndex; + if (match[matchLength] == ip[matchLength]) + matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iend) +1; + } else { + match = dictBase + matchIndex; + matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); + if (matchIndex+matchLength >= dictLimit) + match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ + } + + if (matchLength > bestLength) { + if (matchLength > matchEndIdx - matchIndex) + matchEndIdx = matchIndex + (U32)matchLength; + if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(current-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) ) + bestLength = matchLength, *offsetPtr = ZSTD_REP_MOVE + current - matchIndex; + if (ip+matchLength == iend) /* equal : no way to know if inf or sup */ + break; /* drop, to guarantee consistency (miss a little bit of compression) */ + } + + if (match[matchLength] < ip[matchLength]) { + /* match is smaller than current */ + *smallerPtr = matchIndex; /* update smaller idx */ + commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ + if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ + smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ + matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ + } else { + /* match is larger than current */ + *largerPtr = matchIndex; + commonLengthLarger = matchLength; + if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ + largerPtr = nextPtr; + matchIndex = nextPtr[0]; + } } + + *smallerPtr = *largerPtr = 0; + + zc->nextToUpdate = (matchEndIdx > current + 8) ? matchEndIdx - 8 : current+1; + return bestLength; +} + + +static void ZSTD_updateTree(ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iend, const U32 nbCompares, const U32 mls) +{ + const BYTE* const base = zc->base; + const U32 target = (U32)(ip - base); + U32 idx = zc->nextToUpdate; + + while(idx < target) + idx += ZSTD_insertBt1(zc, base+idx, mls, iend, nbCompares, 0); +} + +/** ZSTD_BtFindBestMatch() : Tree updater, providing best match */ +static size_t ZSTD_BtFindBestMatch ( + ZSTD_CCtx* zc, + const BYTE* const ip, const BYTE* const iLimit, + size_t* offsetPtr, + const U32 maxNbAttempts, const U32 mls) +{ + if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ + ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls); + return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 0); +} + + +static size_t ZSTD_BtFindBestMatch_selectMLS ( + ZSTD_CCtx* zc, /* Index table will be updated */ + const BYTE* ip, const BYTE* const iLimit, + size_t* offsetPtr, + const U32 maxNbAttempts, const U32 matchLengthSearch) +{ + switch(matchLengthSearch) + { + default : /* includes case 3 */ + case 4 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); + case 5 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); + case 7 : + case 6 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); + } +} + + +static void ZSTD_updateTree_extDict(ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iend, const U32 nbCompares, const U32 mls) +{ + const BYTE* const base = zc->base; + const U32 target = (U32)(ip - base); + U32 idx = zc->nextToUpdate; + + while (idx < target) idx += ZSTD_insertBt1(zc, base+idx, mls, iend, nbCompares, 1); +} + + +/** Tree updater, providing best match */ +static size_t ZSTD_BtFindBestMatch_extDict ( + ZSTD_CCtx* zc, + const BYTE* const ip, const BYTE* const iLimit, + size_t* offsetPtr, + const U32 maxNbAttempts, const U32 mls) +{ + if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ + ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls); + return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 1); +} + + +static size_t ZSTD_BtFindBestMatch_selectMLS_extDict ( + ZSTD_CCtx* zc, /* Index table will be updated */ + const BYTE* ip, const BYTE* const iLimit, + size_t* offsetPtr, + const U32 maxNbAttempts, const U32 matchLengthSearch) +{ + switch(matchLengthSearch) + { + default : /* includes case 3 */ + case 4 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); + case 5 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); + case 7 : + case 6 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); + } +} + + + +/* ********************************* +* Hash Chain +***********************************/ +#define NEXT_IN_CHAIN(d, mask) chainTable[(d) & mask] + +/* Update chains up to ip (excluded) + Assumption : always within prefix (i.e. not within extDict) */ +FORCE_INLINE +U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) +{ + U32* const hashTable = zc->hashTable; + const U32 hashLog = zc->params.cParams.hashLog; + U32* const chainTable = zc->chainTable; + const U32 chainMask = (1 << zc->params.cParams.chainLog) - 1; + const BYTE* const base = zc->base; + const U32 target = (U32)(ip - base); + U32 idx = zc->nextToUpdate; + + while(idx < target) { /* catch up */ + size_t const h = ZSTD_hashPtr(base+idx, hashLog, mls); + NEXT_IN_CHAIN(idx, chainMask) = hashTable[h]; + hashTable[h] = idx; + idx++; + } + + zc->nextToUpdate = target; + return hashTable[ZSTD_hashPtr(ip, hashLog, mls)]; +} + + + +FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ +size_t ZSTD_HcFindBestMatch_generic ( + ZSTD_CCtx* zc, /* Index table will be updated */ + const BYTE* const ip, const BYTE* const iLimit, + size_t* offsetPtr, + const U32 maxNbAttempts, const U32 mls, const U32 extDict) +{ + U32* const chainTable = zc->chainTable; + const U32 chainSize = (1 << zc->params.cParams.chainLog); + const U32 chainMask = chainSize-1; + const BYTE* const base = zc->base; + const BYTE* const dictBase = zc->dictBase; + const U32 dictLimit = zc->dictLimit; + const BYTE* const prefixStart = base + dictLimit; + const BYTE* const dictEnd = dictBase + dictLimit; + const U32 lowLimit = zc->lowLimit; + const U32 current = (U32)(ip-base); + const U32 minChain = current > chainSize ? current - chainSize : 0; + int nbAttempts=maxNbAttempts; + size_t ml=4-1; + + /* HC4 match finder */ + U32 matchIndex = ZSTD_insertAndFindFirstIndex (zc, ip, mls); + + for ( ; (matchIndex>lowLimit) & (nbAttempts>0) ; nbAttempts--) { + const BYTE* match; + size_t currentMl=0; + if ((!extDict) || matchIndex >= dictLimit) { + match = base + matchIndex; + if (match[ml] == ip[ml]) /* potentially better */ + currentMl = ZSTD_count(ip, match, iLimit); + } else { + match = dictBase + matchIndex; + if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */ + currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4; + } + + /* save best solution */ + if (currentMl > ml) { + ml = currentMl; + *offsetPtr = current - matchIndex + ZSTD_REP_MOVE; + if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */ + } + + if (matchIndex <= minChain) break; + matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask); + } + + return ml; +} + + +FORCE_INLINE size_t ZSTD_HcFindBestMatch_selectMLS ( + ZSTD_CCtx* zc, + const BYTE* ip, const BYTE* const iLimit, + size_t* offsetPtr, + const U32 maxNbAttempts, const U32 matchLengthSearch) +{ + switch(matchLengthSearch) + { + default : /* includes case 3 */ + case 4 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 0); + case 5 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 0); + case 7 : + case 6 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 0); + } +} + + +FORCE_INLINE size_t ZSTD_HcFindBestMatch_extDict_selectMLS ( + ZSTD_CCtx* zc, + const BYTE* ip, const BYTE* const iLimit, + size_t* offsetPtr, + const U32 maxNbAttempts, const U32 matchLengthSearch) +{ + switch(matchLengthSearch) + { + default : /* includes case 3 */ + case 4 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 1); + case 5 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 1); + case 7 : + case 6 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 1); + } +} + + +/* ******************************* +* Common parser - lazy strategy +*********************************/ +FORCE_INLINE +void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx, + const void* src, size_t srcSize, + const U32 searchMethod, const U32 depth) +{ + seqStore_t* seqStorePtr = &(ctx->seqStore); + const BYTE* const istart = (const BYTE*)src; + const BYTE* ip = istart; + const BYTE* anchor = istart; + const BYTE* const iend = istart + srcSize; + const BYTE* const ilimit = iend - 8; + const BYTE* const base = ctx->base + ctx->dictLimit; + + U32 const maxSearches = 1 << ctx->params.cParams.searchLog; + U32 const mls = ctx->params.cParams.searchLength; + + typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, + size_t* offsetPtr, + U32 maxNbAttempts, U32 matchLengthSearch); + searchMax_f const searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS; + U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1], savedOffset=0; + + /* init */ + ip += (ip==base); + ctx->nextToUpdate3 = ctx->nextToUpdate; + { U32 const maxRep = (U32)(ip-base); + if (offset_2 > maxRep) savedOffset = offset_2, offset_2 = 0; + if (offset_1 > maxRep) savedOffset = offset_1, offset_1 = 0; + } + + /* Match Loop */ + while (ip < ilimit) { + size_t matchLength=0; + size_t offset=0; + const BYTE* start=ip+1; + + /* check repCode */ + if ((offset_1>0) & (MEM_read32(ip+1) == MEM_read32(ip+1 - offset_1))) { + /* repcode : we take it */ + matchLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; + if (depth==0) goto _storeSequence; + } + + /* first search (depth 0) */ + { size_t offsetFound = 99999999; + size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls); + if (ml2 > matchLength) + matchLength = ml2, start = ip, offset=offsetFound; + } + + if (matchLength < 4) { + ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ + continue; + } + + /* let's try to find a better solution */ + if (depth>=1) + while (ip<ilimit) { + ip ++; + if ((offset) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) { + size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4; + int const gain2 = (int)(mlRep * 3); + int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offset+1) + 1); + if ((mlRep >= 4) && (gain2 > gain1)) + matchLength = mlRep, offset = 0, start = ip; + } + { size_t offset2=99999999; + size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); + int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ + int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 4); + if ((ml2 >= 4) && (gain2 > gain1)) { + matchLength = ml2, offset = offset2, start = ip; + continue; /* search a better one */ + } } + + /* let's find an even better one */ + if ((depth==2) && (ip<ilimit)) { + ip ++; + if ((offset) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) { + size_t const ml2 = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4; + int const gain2 = (int)(ml2 * 4); + int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 1); + if ((ml2 >= 4) && (gain2 > gain1)) + matchLength = ml2, offset = 0, start = ip; + } + { size_t offset2=99999999; + size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); + int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ + int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 7); + if ((ml2 >= 4) && (gain2 > gain1)) { + matchLength = ml2, offset = offset2, start = ip; + continue; + } } } + break; /* nothing found : store previous solution */ + } + + /* catch up */ + if (offset) { + while ( (start > anchor) + && (start > base+offset-ZSTD_REP_MOVE) + && (start[-1] == start[-1-offset+ZSTD_REP_MOVE]) ) /* only search for offset within prefix */ + { start--; matchLength++; } + offset_2 = offset_1; offset_1 = (U32)(offset - ZSTD_REP_MOVE); + } + + /* store sequence */ +_storeSequence: + { size_t const litLength = start - anchor; + ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength-MINMATCH); + anchor = ip = start + matchLength; + } + + /* check immediate repcode */ + while ( (ip <= ilimit) + && ((offset_2>0) + & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { + /* store sequence */ + matchLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4; + offset = offset_2; offset_2 = offset_1; offset_1 = (U32)offset; /* swap repcodes */ + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength-MINMATCH); + ip += matchLength; + anchor = ip; + continue; /* faster when present ... (?) */ + } } + + /* Save reps for next block */ + ctx->repToConfirm[0] = offset_1 ? offset_1 : savedOffset; + ctx->repToConfirm[1] = offset_2 ? offset_2 : savedOffset; + + /* Last Literals */ + { size_t const lastLLSize = iend - anchor; + memcpy(seqStorePtr->lit, anchor, lastLLSize); + seqStorePtr->lit += lastLLSize; + } +} + + +static void ZSTD_compressBlock_btlazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ + ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 1, 2); +} + +static void ZSTD_compressBlock_lazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ + ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 2); +} + +static void ZSTD_compressBlock_lazy(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ + ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 1); +} + +static void ZSTD_compressBlock_greedy(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ + ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 0); +} + + +FORCE_INLINE +void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx, + const void* src, size_t srcSize, + const U32 searchMethod, const U32 depth) +{ + seqStore_t* seqStorePtr = &(ctx->seqStore); + const BYTE* const istart = (const BYTE*)src; + const BYTE* ip = istart; + const BYTE* anchor = istart; + const BYTE* const iend = istart + srcSize; + const BYTE* const ilimit = iend - 8; + const BYTE* const base = ctx->base; + const U32 dictLimit = ctx->dictLimit; + const U32 lowestIndex = ctx->lowLimit; + const BYTE* const prefixStart = base + dictLimit; + const BYTE* const dictBase = ctx->dictBase; + const BYTE* const dictEnd = dictBase + dictLimit; + const BYTE* const dictStart = dictBase + ctx->lowLimit; + + const U32 maxSearches = 1 << ctx->params.cParams.searchLog; + const U32 mls = ctx->params.cParams.searchLength; + + typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, + size_t* offsetPtr, + U32 maxNbAttempts, U32 matchLengthSearch); + searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS_extDict : ZSTD_HcFindBestMatch_extDict_selectMLS; + + U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1]; + + /* init */ + ctx->nextToUpdate3 = ctx->nextToUpdate; + ip += (ip == prefixStart); + + /* Match Loop */ + while (ip < ilimit) { + size_t matchLength=0; + size_t offset=0; + const BYTE* start=ip+1; + U32 current = (U32)(ip-base); + + /* check repCode */ + { const U32 repIndex = (U32)(current+1 - offset_1); + const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; + const BYTE* const repMatch = repBase + repIndex; + if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ + if (MEM_read32(ip+1) == MEM_read32(repMatch)) { + /* repcode detected we should take it */ + const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; + matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repEnd, prefixStart) + 4; + if (depth==0) goto _storeSequence; + } } + + /* first search (depth 0) */ + { size_t offsetFound = 99999999; + size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls); + if (ml2 > matchLength) + matchLength = ml2, start = ip, offset=offsetFound; + } + + if (matchLength < 4) { + ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ + continue; + } + + /* let's try to find a better solution */ + if (depth>=1) + while (ip<ilimit) { + ip ++; + current++; + /* check repCode */ + if (offset) { + const U32 repIndex = (U32)(current - offset_1); + const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; + const BYTE* const repMatch = repBase + repIndex; + if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ + if (MEM_read32(ip) == MEM_read32(repMatch)) { + /* repcode detected */ + const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; + size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; + int const gain2 = (int)(repLength * 3); + int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offset+1) + 1); + if ((repLength >= 4) && (gain2 > gain1)) + matchLength = repLength, offset = 0, start = ip; + } } + + /* search match, depth 1 */ + { size_t offset2=99999999; + size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); + int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ + int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 4); + if ((ml2 >= 4) && (gain2 > gain1)) { + matchLength = ml2, offset = offset2, start = ip; + continue; /* search a better one */ + } } + + /* let's find an even better one */ + if ((depth==2) && (ip<ilimit)) { + ip ++; + current++; + /* check repCode */ + if (offset) { + const U32 repIndex = (U32)(current - offset_1); + const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; + const BYTE* const repMatch = repBase + repIndex; + if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ + if (MEM_read32(ip) == MEM_read32(repMatch)) { + /* repcode detected */ + const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; + size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; + int const gain2 = (int)(repLength * 4); + int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 1); + if ((repLength >= 4) && (gain2 > gain1)) + matchLength = repLength, offset = 0, start = ip; + } } + + /* search match, depth 2 */ + { size_t offset2=99999999; + size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); + int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ + int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 7); + if ((ml2 >= 4) && (gain2 > gain1)) { + matchLength = ml2, offset = offset2, start = ip; + continue; + } } } + break; /* nothing found : store previous solution */ + } + + /* catch up */ + if (offset) { + U32 const matchIndex = (U32)((start-base) - (offset - ZSTD_REP_MOVE)); + const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex; + const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart; + while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */ + offset_2 = offset_1; offset_1 = (U32)(offset - ZSTD_REP_MOVE); + } + + /* store sequence */ +_storeSequence: + { size_t const litLength = start - anchor; + ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength-MINMATCH); + anchor = ip = start + matchLength; + } + + /* check immediate repcode */ + while (ip <= ilimit) { + const U32 repIndex = (U32)((ip-base) - offset_2); + const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; + const BYTE* const repMatch = repBase + repIndex; + if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ + if (MEM_read32(ip) == MEM_read32(repMatch)) { + /* repcode detected we should take it */ + const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; + matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; + offset = offset_2; offset_2 = offset_1; offset_1 = (U32)offset; /* swap offset history */ + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength-MINMATCH); + ip += matchLength; + anchor = ip; + continue; /* faster when present ... (?) */ + } + break; + } } + + /* Save reps for next block */ + ctx->repToConfirm[0] = offset_1; ctx->repToConfirm[1] = offset_2; + + /* Last Literals */ + { size_t const lastLLSize = iend - anchor; + memcpy(seqStorePtr->lit, anchor, lastLLSize); + seqStorePtr->lit += lastLLSize; + } +} + + +void ZSTD_compressBlock_greedy_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ + ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 0); +} + +static void ZSTD_compressBlock_lazy_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ + ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 1); +} + +static void ZSTD_compressBlock_lazy2_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ + ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 2); +} + +static void ZSTD_compressBlock_btlazy2_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ + ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 1, 2); +} + + +/* The optimal parser */ +#include "zstd_opt.h" + +static void ZSTD_compressBlock_btopt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ +#ifdef ZSTD_OPT_H_91842398743 + ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 0); +#else + (void)ctx; (void)src; (void)srcSize; + return; +#endif +} + +static void ZSTD_compressBlock_btopt2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ +#ifdef ZSTD_OPT_H_91842398743 + ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 1); +#else + (void)ctx; (void)src; (void)srcSize; + return; +#endif +} + +static void ZSTD_compressBlock_btopt_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ +#ifdef ZSTD_OPT_H_91842398743 + ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 0); +#else + (void)ctx; (void)src; (void)srcSize; + return; +#endif +} + +static void ZSTD_compressBlock_btopt2_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +{ +#ifdef ZSTD_OPT_H_91842398743 + ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 1); +#else + (void)ctx; (void)src; (void)srcSize; + return; +#endif +} + + +typedef void (*ZSTD_blockCompressor) (ZSTD_CCtx* ctx, const void* src, size_t srcSize); + +static ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, int extDict) +{ + static const ZSTD_blockCompressor blockCompressor[2][8] = { + { ZSTD_compressBlock_fast, ZSTD_compressBlock_doubleFast, ZSTD_compressBlock_greedy, + ZSTD_compressBlock_lazy, ZSTD_compressBlock_lazy2, ZSTD_compressBlock_btlazy2, + ZSTD_compressBlock_btopt, ZSTD_compressBlock_btopt2 }, + { ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_doubleFast_extDict, ZSTD_compressBlock_greedy_extDict, + ZSTD_compressBlock_lazy_extDict,ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, + ZSTD_compressBlock_btopt_extDict, ZSTD_compressBlock_btopt2_extDict } + }; + + return blockCompressor[extDict][(U32)strat]; +} + + +static size_t ZSTD_compressBlock_internal(ZSTD_CCtx* zc, void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + ZSTD_blockCompressor const blockCompressor = ZSTD_selectBlockCompressor(zc->params.cParams.strategy, zc->lowLimit < zc->dictLimit); + const BYTE* const base = zc->base; + const BYTE* const istart = (const BYTE*)src; + const U32 current = (U32)(istart-base); + if (srcSize < MIN_CBLOCK_SIZE+ZSTD_blockHeaderSize+1) return 0; /* don't even attempt compression below a certain srcSize */ + ZSTD_resetSeqStore(&(zc->seqStore)); + if (current > zc->nextToUpdate + 384) + zc->nextToUpdate = current - MIN(192, (U32)(current - zc->nextToUpdate - 384)); /* limited update after finding a very long match */ + blockCompressor(zc, src, srcSize); + return ZSTD_compressSequences(zc, dst, dstCapacity, srcSize); +} + + +/*! ZSTD_compress_generic() : +* Compress a chunk of data into one or multiple blocks. +* All blocks will be terminated, all input will be consumed. +* Function will issue an error if there is not enough `dstCapacity` to hold the compressed content. +* Frame is supposed already started (header already produced) +* @return : compressed size, or an error code +*/ +static size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + U32 lastFrameChunk) +{ + size_t blockSize = cctx->blockSize; + size_t remaining = srcSize; + const BYTE* ip = (const BYTE*)src; + BYTE* const ostart = (BYTE*)dst; + BYTE* op = ostart; + U32 const maxDist = 1 << cctx->params.cParams.windowLog; + + if (cctx->params.fParams.checksumFlag && srcSize) + XXH64_update(&cctx->xxhState, src, srcSize); + + while (remaining) { + U32 const lastBlock = lastFrameChunk & (blockSize >= remaining); + size_t cSize; + + if (dstCapacity < ZSTD_blockHeaderSize + MIN_CBLOCK_SIZE) + return ERROR(dstSize_tooSmall); /* not enough space to store compressed block */ + if (remaining < blockSize) blockSize = remaining; + + /* preemptive overflow correction */ + if (cctx->lowLimit > (3U<<29)) { + U32 const cycleMask = (1 << ZSTD_cycleLog(cctx->params.cParams.hashLog, cctx->params.cParams.strategy)) - 1; + U32 const current = (U32)(ip - cctx->base); + U32 const newCurrent = (current & cycleMask) + (1 << cctx->params.cParams.windowLog); + U32 const correction = current - newCurrent; + ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX_64 <= 30); + ZSTD_reduceIndex(cctx, correction); + cctx->base += correction; + cctx->dictBase += correction; + cctx->lowLimit -= correction; + cctx->dictLimit -= correction; + if (cctx->nextToUpdate < correction) cctx->nextToUpdate = 0; + else cctx->nextToUpdate -= correction; + } + + if ((U32)(ip+blockSize - cctx->base) > cctx->loadedDictEnd + maxDist) { + /* enforce maxDist */ + U32 const newLowLimit = (U32)(ip+blockSize - cctx->base) - maxDist; + if (cctx->lowLimit < newLowLimit) cctx->lowLimit = newLowLimit; + if (cctx->dictLimit < cctx->lowLimit) cctx->dictLimit = cctx->lowLimit; + } + + cSize = ZSTD_compressBlock_internal(cctx, op+ZSTD_blockHeaderSize, dstCapacity-ZSTD_blockHeaderSize, ip, blockSize); + if (ZSTD_isError(cSize)) return cSize; + + if (cSize == 0) { /* block is not compressible */ + U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw)<<1) + (U32)(blockSize << 3); + if (blockSize + ZSTD_blockHeaderSize > dstCapacity) return ERROR(dstSize_tooSmall); + MEM_writeLE32(op, cBlockHeader24); /* no pb, 4th byte will be overwritten */ + memcpy(op + ZSTD_blockHeaderSize, ip, blockSize); + cSize = ZSTD_blockHeaderSize+blockSize; + } else { + U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed)<<1) + (U32)(cSize << 3); + MEM_writeLE24(op, cBlockHeader24); + cSize += ZSTD_blockHeaderSize; + } + + remaining -= blockSize; + dstCapacity -= cSize; + ip += blockSize; + op += cSize; + } + + if (lastFrameChunk && (op>ostart)) cctx->stage = ZSTDcs_ending; + return op-ostart; +} + + +static size_t ZSTD_writeFrameHeader(void* dst, size_t dstCapacity, + ZSTD_parameters params, U64 pledgedSrcSize, U32 dictID) +{ BYTE* const op = (BYTE*)dst; + U32 const dictIDSizeCodeLength = (dictID>0) + (dictID>=256) + (dictID>=65536); /* 0-3 */ + U32 const dictIDSizeCode = params.fParams.noDictIDFlag ? 0 : dictIDSizeCodeLength; /* 0-3 */ + U32 const checksumFlag = params.fParams.checksumFlag>0; + U32 const windowSize = 1U << params.cParams.windowLog; + U32 const singleSegment = params.fParams.contentSizeFlag && (windowSize >= pledgedSrcSize); + BYTE const windowLogByte = (BYTE)((params.cParams.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN) << 3); + U32 const fcsCode = params.fParams.contentSizeFlag ? + (pledgedSrcSize>=256) + (pledgedSrcSize>=65536+256) + (pledgedSrcSize>=0xFFFFFFFFU) : /* 0-3 */ + 0; + BYTE const frameHeaderDecriptionByte = (BYTE)(dictIDSizeCode + (checksumFlag<<2) + (singleSegment<<5) + (fcsCode<<6) ); + size_t pos; + + if (dstCapacity < ZSTD_frameHeaderSize_max) return ERROR(dstSize_tooSmall); + DEBUGLOG(5, "ZSTD_writeFrameHeader : dictIDFlag : %u \n", !params.fParams.noDictIDFlag); + DEBUGLOG(5, "ZSTD_writeFrameHeader : dictID : %u \n", dictID); + DEBUGLOG(5, "ZSTD_writeFrameHeader : dictIDSizeCode : %u \n", dictIDSizeCode); + + MEM_writeLE32(dst, ZSTD_MAGICNUMBER); + op[4] = frameHeaderDecriptionByte; pos=5; + if (!singleSegment) op[pos++] = windowLogByte; + switch(dictIDSizeCode) + { + default: /* impossible */ + case 0 : break; + case 1 : op[pos] = (BYTE)(dictID); pos++; break; + case 2 : MEM_writeLE16(op+pos, (U16)dictID); pos+=2; break; + case 3 : MEM_writeLE32(op+pos, dictID); pos+=4; break; + } + switch(fcsCode) + { + default: /* impossible */ + case 0 : if (singleSegment) op[pos++] = (BYTE)(pledgedSrcSize); break; + case 1 : MEM_writeLE16(op+pos, (U16)(pledgedSrcSize-256)); pos+=2; break; + case 2 : MEM_writeLE32(op+pos, (U32)(pledgedSrcSize)); pos+=4; break; + case 3 : MEM_writeLE64(op+pos, (U64)(pledgedSrcSize)); pos+=8; break; + } + return pos; +} + + +static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + U32 frame, U32 lastFrameChunk) +{ + const BYTE* const ip = (const BYTE*) src; + size_t fhSize = 0; + + if (cctx->stage==ZSTDcs_created) return ERROR(stage_wrong); /* missing init (ZSTD_compressBegin) */ + + if (frame && (cctx->stage==ZSTDcs_init)) { + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->params, cctx->frameContentSize, cctx->dictID); + if (ZSTD_isError(fhSize)) return fhSize; + dstCapacity -= fhSize; + dst = (char*)dst + fhSize; + cctx->stage = ZSTDcs_ongoing; + } + + /* Check if blocks follow each other */ + if (src != cctx->nextSrc) { + /* not contiguous */ + ptrdiff_t const delta = cctx->nextSrc - ip; + cctx->lowLimit = cctx->dictLimit; + cctx->dictLimit = (U32)(cctx->nextSrc - cctx->base); + cctx->dictBase = cctx->base; + cctx->base -= delta; + cctx->nextToUpdate = cctx->dictLimit; + if (cctx->dictLimit - cctx->lowLimit < HASH_READ_SIZE) cctx->lowLimit = cctx->dictLimit; /* too small extDict */ + } + + /* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */ + if ((ip+srcSize > cctx->dictBase + cctx->lowLimit) & (ip < cctx->dictBase + cctx->dictLimit)) { + ptrdiff_t const highInputIdx = (ip + srcSize) - cctx->dictBase; + U32 const lowLimitMax = (highInputIdx > (ptrdiff_t)cctx->dictLimit) ? cctx->dictLimit : (U32)highInputIdx; + cctx->lowLimit = lowLimitMax; + } + + cctx->nextSrc = ip + srcSize; + + if (srcSize) { + size_t const cSize = frame ? + ZSTD_compress_generic (cctx, dst, dstCapacity, src, srcSize, lastFrameChunk) : + ZSTD_compressBlock_internal (cctx, dst, dstCapacity, src, srcSize); + if (ZSTD_isError(cSize)) return cSize; + cctx->consumedSrcSize += srcSize; + return cSize + fhSize; + } else + return fhSize; +} + + +size_t ZSTD_compressContinue (ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize) +{ + return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1 /* frame mode */, 0 /* last chunk */); +} + + +size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx) +{ + return MIN (ZSTD_BLOCKSIZE_ABSOLUTEMAX, 1 << cctx->params.cParams.windowLog); +} + +size_t ZSTD_compressBlock(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + size_t const blockSizeMax = ZSTD_getBlockSizeMax(cctx); + if (srcSize > blockSizeMax) return ERROR(srcSize_wrong); + return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 0 /* frame mode */, 0 /* last chunk */); +} + +/*! ZSTD_loadDictionaryContent() : + * @return : 0, or an error code + */ +static size_t ZSTD_loadDictionaryContent(ZSTD_CCtx* zc, const void* src, size_t srcSize) +{ + const BYTE* const ip = (const BYTE*) src; + const BYTE* const iend = ip + srcSize; + + /* input becomes current prefix */ + zc->lowLimit = zc->dictLimit; + zc->dictLimit = (U32)(zc->nextSrc - zc->base); + zc->dictBase = zc->base; + zc->base += ip - zc->nextSrc; + zc->nextToUpdate = zc->dictLimit; + zc->loadedDictEnd = zc->forceWindow ? 0 : (U32)(iend - zc->base); + + zc->nextSrc = iend; + if (srcSize <= HASH_READ_SIZE) return 0; + + switch(zc->params.cParams.strategy) + { + case ZSTD_fast: + ZSTD_fillHashTable (zc, iend, zc->params.cParams.searchLength); + break; + + case ZSTD_dfast: + ZSTD_fillDoubleHashTable (zc, iend, zc->params.cParams.searchLength); + break; + + case ZSTD_greedy: + case ZSTD_lazy: + case ZSTD_lazy2: + if (srcSize >= HASH_READ_SIZE) + ZSTD_insertAndFindFirstIndex(zc, iend-HASH_READ_SIZE, zc->params.cParams.searchLength); + break; + + case ZSTD_btlazy2: + case ZSTD_btopt: + case ZSTD_btopt2: + if (srcSize >= HASH_READ_SIZE) + ZSTD_updateTree(zc, iend-HASH_READ_SIZE, iend, 1 << zc->params.cParams.searchLog, zc->params.cParams.searchLength); + break; + + default: + return ERROR(GENERIC); /* strategy doesn't exist; impossible */ + } + + zc->nextToUpdate = (U32)(iend - zc->base); + return 0; +} + + +/* Dictionaries that assign zero probability to symbols that show up causes problems + when FSE encoding. Refuse dictionaries that assign zero probability to symbols + that we may encounter during compression. + NOTE: This behavior is not standard and could be improved in the future. */ +static size_t ZSTD_checkDictNCount(short* normalizedCounter, unsigned dictMaxSymbolValue, unsigned maxSymbolValue) { + U32 s; + if (dictMaxSymbolValue < maxSymbolValue) return ERROR(dictionary_corrupted); + for (s = 0; s <= maxSymbolValue; ++s) { + if (normalizedCounter[s] == 0) return ERROR(dictionary_corrupted); + } + return 0; +} + + +/* Dictionary format : + * See : + * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#dictionary-format + */ +/*! ZSTD_loadZstdDictionary() : + * @return : 0, or an error code + * assumptions : magic number supposed already checked + * dictSize supposed > 8 + */ +static size_t ZSTD_loadZstdDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) +{ + const BYTE* dictPtr = (const BYTE*)dict; + const BYTE* const dictEnd = dictPtr + dictSize; + short offcodeNCount[MaxOff+1]; + unsigned offcodeMaxValue = MaxOff; + BYTE scratchBuffer[1<<MAX(MLFSELog,LLFSELog)]; + + dictPtr += 4; /* skip magic number */ + cctx->dictID = cctx->params.fParams.noDictIDFlag ? 0 : MEM_readLE32(dictPtr); + dictPtr += 4; + + { size_t const hufHeaderSize = HUF_readCTable(cctx->hufCTable, 255, dictPtr, dictEnd-dictPtr); + if (HUF_isError(hufHeaderSize)) return ERROR(dictionary_corrupted); + dictPtr += hufHeaderSize; + } + + { unsigned offcodeLog; + size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr); + if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted); + if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted); + /* Defer checking offcodeMaxValue because we need to know the size of the dictionary content */ + CHECK_E( FSE_buildCTable_wksp(cctx->offcodeCTable, offcodeNCount, offcodeMaxValue, offcodeLog, scratchBuffer, sizeof(scratchBuffer)), + dictionary_corrupted); + dictPtr += offcodeHeaderSize; + } + + { short matchlengthNCount[MaxML+1]; + unsigned matchlengthMaxValue = MaxML, matchlengthLog; + size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr); + if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted); + if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted); + /* Every match length code must have non-zero probability */ + CHECK_F( ZSTD_checkDictNCount(matchlengthNCount, matchlengthMaxValue, MaxML)); + CHECK_E( FSE_buildCTable_wksp(cctx->matchlengthCTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog, scratchBuffer, sizeof(scratchBuffer)), + dictionary_corrupted); + dictPtr += matchlengthHeaderSize; + } + + { short litlengthNCount[MaxLL+1]; + unsigned litlengthMaxValue = MaxLL, litlengthLog; + size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr); + if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted); + if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted); + /* Every literal length code must have non-zero probability */ + CHECK_F( ZSTD_checkDictNCount(litlengthNCount, litlengthMaxValue, MaxLL)); + CHECK_E( FSE_buildCTable_wksp(cctx->litlengthCTable, litlengthNCount, litlengthMaxValue, litlengthLog, scratchBuffer, sizeof(scratchBuffer)), + dictionary_corrupted); + dictPtr += litlengthHeaderSize; + } + + if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted); + cctx->rep[0] = MEM_readLE32(dictPtr+0); + cctx->rep[1] = MEM_readLE32(dictPtr+4); + cctx->rep[2] = MEM_readLE32(dictPtr+8); + dictPtr += 12; + + { size_t const dictContentSize = (size_t)(dictEnd - dictPtr); + U32 offcodeMax = MaxOff; + if (dictContentSize <= ((U32)-1) - 128 KB) { + U32 const maxOffset = (U32)dictContentSize + 128 KB; /* The maximum offset that must be supported */ + offcodeMax = ZSTD_highbit32(maxOffset); /* Calculate minimum offset code required to represent maxOffset */ + } + /* All offset values <= dictContentSize + 128 KB must be representable */ + CHECK_F (ZSTD_checkDictNCount(offcodeNCount, offcodeMaxValue, MIN(offcodeMax, MaxOff))); + /* All repCodes must be <= dictContentSize and != 0*/ + { U32 u; + for (u=0; u<3; u++) { + if (cctx->rep[u] == 0) return ERROR(dictionary_corrupted); + if (cctx->rep[u] > dictContentSize) return ERROR(dictionary_corrupted); + } } + + cctx->fseCTables_ready = 1; + cctx->hufCTable_repeatMode = HUF_repeat_valid; + return ZSTD_loadDictionaryContent(cctx, dictPtr, dictContentSize); + } +} + +/** ZSTD_compress_insertDictionary() : +* @return : 0, or an error code */ +static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) +{ + if ((dict==NULL) || (dictSize<=8)) return 0; + + /* dict as pure content */ + if ((MEM_readLE32(dict) != ZSTD_DICT_MAGIC) || (cctx->forceRawDict)) + return ZSTD_loadDictionaryContent(cctx, dict, dictSize); + + /* dict as zstd dictionary */ + return ZSTD_loadZstdDictionary(cctx, dict, dictSize); +} + +/*! ZSTD_compressBegin_internal() : +* @return : 0, or an error code */ +static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, + const void* dict, size_t dictSize, + ZSTD_parameters params, U64 pledgedSrcSize) +{ + ZSTD_compResetPolicy_e const crp = dictSize ? ZSTDcrp_fullReset : ZSTDcrp_continue; + assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); + CHECK_F(ZSTD_resetCCtx_internal(cctx, params, pledgedSrcSize, crp)); + return ZSTD_compress_insertDictionary(cctx, dict, dictSize); +} + + +/*! ZSTD_compressBegin_advanced() : +* @return : 0, or an error code */ +size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, + const void* dict, size_t dictSize, + ZSTD_parameters params, unsigned long long pledgedSrcSize) +{ + /* compression parameters verification and optimization */ + CHECK_F(ZSTD_checkCParams(params.cParams)); + return ZSTD_compressBegin_internal(cctx, dict, dictSize, params, pledgedSrcSize); +} + + +size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel) +{ + ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); + return ZSTD_compressBegin_internal(cctx, dict, dictSize, params, 0); +} + + +size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel) +{ + return ZSTD_compressBegin_usingDict(cctx, NULL, 0, compressionLevel); +} + + +/*! ZSTD_writeEpilogue() : +* Ends a frame. +* @return : nb of bytes written into dst (or an error code) */ +static size_t ZSTD_writeEpilogue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity) +{ + BYTE* const ostart = (BYTE*)dst; + BYTE* op = ostart; + size_t fhSize = 0; + + if (cctx->stage == ZSTDcs_created) return ERROR(stage_wrong); /* init missing */ + + /* special case : empty frame */ + if (cctx->stage == ZSTDcs_init) { + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->params, 0, 0); + if (ZSTD_isError(fhSize)) return fhSize; + dstCapacity -= fhSize; + op += fhSize; + cctx->stage = ZSTDcs_ongoing; + } + + if (cctx->stage != ZSTDcs_ending) { + /* write one last empty block, make it the "last" block */ + U32 const cBlockHeader24 = 1 /* last block */ + (((U32)bt_raw)<<1) + 0; + if (dstCapacity<4) return ERROR(dstSize_tooSmall); + MEM_writeLE32(op, cBlockHeader24); + op += ZSTD_blockHeaderSize; + dstCapacity -= ZSTD_blockHeaderSize; + } + + if (cctx->params.fParams.checksumFlag) { + U32 const checksum = (U32) XXH64_digest(&cctx->xxhState); + if (dstCapacity<4) return ERROR(dstSize_tooSmall); + MEM_writeLE32(op, checksum); + op += 4; + } + + cctx->stage = ZSTDcs_created; /* return to "created but no init" status */ + return op-ostart; +} + + +size_t ZSTD_compressEnd (ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize) +{ + size_t endResult; + size_t const cSize = ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1 /* frame mode */, 1 /* last chunk */); + if (ZSTD_isError(cSize)) return cSize; + endResult = ZSTD_writeEpilogue(cctx, (char*)dst + cSize, dstCapacity-cSize); + if (ZSTD_isError(endResult)) return endResult; + if (cctx->params.fParams.contentSizeFlag) { /* control src size */ + if (cctx->frameContentSize != cctx->consumedSrcSize) return ERROR(srcSize_wrong); + } + return cSize + endResult; +} + + +static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict,size_t dictSize, + ZSTD_parameters params) +{ + CHECK_F(ZSTD_compressBegin_internal(cctx, dict, dictSize, params, srcSize)); + return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); +} + +size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict,size_t dictSize, + ZSTD_parameters params) +{ + CHECK_F(ZSTD_checkCParams(params.cParams)); + return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); +} + +size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, + const void* dict, size_t dictSize, int compressionLevel) +{ + ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, dict ? dictSize : 0); + params.fParams.contentSizeFlag = 1; + return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); +} + +size_t ZSTD_compressCCtx (ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel) +{ + return ZSTD_compress_usingDict(ctx, dst, dstCapacity, src, srcSize, NULL, 0, compressionLevel); +} + +size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel) +{ + size_t result; + ZSTD_CCtx ctxBody; + memset(&ctxBody, 0, sizeof(ctxBody)); + memcpy(&ctxBody.customMem, &defaultCustomMem, sizeof(ZSTD_customMem)); + result = ZSTD_compressCCtx(&ctxBody, dst, dstCapacity, src, srcSize, compressionLevel); + ZSTD_free(ctxBody.workSpace, defaultCustomMem); /* can't free ctxBody itself, as it's on stack; free only heap content */ + return result; +} + + +/* ===== Dictionary API ===== */ + +struct ZSTD_CDict_s { + void* dictBuffer; + const void* dictContent; + size_t dictContentSize; + ZSTD_CCtx* refContext; +}; /* typedef'd tp ZSTD_CDict within "zstd.h" */ + +size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict) +{ + if (cdict==NULL) return 0; /* support sizeof on NULL */ + return ZSTD_sizeof_CCtx(cdict->refContext) + (cdict->dictBuffer ? cdict->dictContentSize : 0) + sizeof(*cdict); +} + +static ZSTD_parameters ZSTD_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams) +{ + ZSTD_parameters params; + params.cParams = cParams; + params.fParams = fParams; + return params; +} + +ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, unsigned byReference, + ZSTD_compressionParameters cParams, ZSTD_customMem customMem) +{ + if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem; + if (!customMem.customAlloc || !customMem.customFree) return NULL; + + { ZSTD_CDict* const cdict = (ZSTD_CDict*) ZSTD_malloc(sizeof(ZSTD_CDict), customMem); + ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); + + if (!cdict || !cctx) { + ZSTD_free(cdict, customMem); + ZSTD_freeCCtx(cctx); + return NULL; + } + + if ((byReference) || (!dictBuffer) || (!dictSize)) { + cdict->dictBuffer = NULL; + cdict->dictContent = dictBuffer; + } else { + void* const internalBuffer = ZSTD_malloc(dictSize, customMem); + if (!internalBuffer) { ZSTD_free(cctx, customMem); ZSTD_free(cdict, customMem); return NULL; } + memcpy(internalBuffer, dictBuffer, dictSize); + cdict->dictBuffer = internalBuffer; + cdict->dictContent = internalBuffer; + } + + { ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ + ZSTD_parameters const params = ZSTD_makeParams(cParams, fParams); + size_t const errorCode = ZSTD_compressBegin_advanced(cctx, cdict->dictContent, dictSize, params, 0); + if (ZSTD_isError(errorCode)) { + ZSTD_free(cdict->dictBuffer, customMem); + ZSTD_free(cdict, customMem); + ZSTD_freeCCtx(cctx); + return NULL; + } } + + cdict->refContext = cctx; + cdict->dictContentSize = dictSize; + return cdict; + } +} + +ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel) +{ + ZSTD_customMem const allocator = { NULL, NULL, NULL }; + ZSTD_compressionParameters cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); + return ZSTD_createCDict_advanced(dict, dictSize, 0, cParams, allocator); +} + +ZSTD_CDict* ZSTD_createCDict_byReference(const void* dict, size_t dictSize, int compressionLevel) +{ + ZSTD_customMem const allocator = { NULL, NULL, NULL }; + ZSTD_compressionParameters cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); + return ZSTD_createCDict_advanced(dict, dictSize, 1, cParams, allocator); +} + +size_t ZSTD_freeCDict(ZSTD_CDict* cdict) +{ + if (cdict==NULL) return 0; /* support free on NULL */ + { ZSTD_customMem const cMem = cdict->refContext->customMem; + ZSTD_freeCCtx(cdict->refContext); + ZSTD_free(cdict->dictBuffer, cMem); + ZSTD_free(cdict, cMem); + return 0; + } +} + +static ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { + return ZSTD_getParamsFromCCtx(cdict->refContext); +} + +/* ZSTD_compressBegin_usingCDict_advanced() : + * cdict must be != NULL */ +size_t ZSTD_compressBegin_usingCDict_advanced( + ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, + ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize) +{ + if (cdict==NULL) return ERROR(GENERIC); /* does not support NULL cdict */ + DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced : dictIDFlag == %u \n", !fParams.noDictIDFlag); + if (cdict->dictContentSize) + CHECK_F( ZSTD_copyCCtx_internal(cctx, cdict->refContext, fParams, pledgedSrcSize) ) + else { + ZSTD_parameters params = cdict->refContext->params; + params.fParams = fParams; + CHECK_F(ZSTD_compressBegin_internal(cctx, NULL, 0, params, pledgedSrcSize)); + } + return 0; +} + +/* ZSTD_compressBegin_usingCDict() : + * pledgedSrcSize=0 means "unknown" + * if pledgedSrcSize>0, it will enable contentSizeFlag */ +size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict) +{ + ZSTD_frameParameters const fParams = { 0 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; + DEBUGLOG(5, "ZSTD_compressBegin_usingCDict : dictIDFlag == %u \n", !fParams.noDictIDFlag); + return ZSTD_compressBegin_usingCDict_advanced(cctx, cdict, fParams, 0); +} + +size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const ZSTD_CDict* cdict, ZSTD_frameParameters fParams) +{ + CHECK_F (ZSTD_compressBegin_usingCDict_advanced(cctx, cdict, fParams, srcSize)); /* will check if cdict != NULL */ + return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); +} + +/*! ZSTD_compress_usingCDict() : + * Compression using a digested Dictionary. + * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. + * Note that compression parameters are decided at CDict creation time + * while frame parameters are hardcoded */ +size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const ZSTD_CDict* cdict) +{ + ZSTD_frameParameters const fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; + return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, fParams); +} + + + +/* ****************************************************************** +* Streaming +********************************************************************/ + +typedef enum { zcss_init, zcss_load, zcss_flush, zcss_final } ZSTD_cStreamStage; + +struct ZSTD_CStream_s { + ZSTD_CCtx* cctx; + ZSTD_CDict* cdictLocal; + const ZSTD_CDict* cdict; + char* inBuff; + size_t inBuffSize; + size_t inToCompress; + size_t inBuffPos; + size_t inBuffTarget; + size_t blockSize; + char* outBuff; + size_t outBuffSize; + size_t outBuffContentSize; + size_t outBuffFlushedSize; + ZSTD_cStreamStage stage; + U32 checksum; + U32 frameEnded; + U64 pledgedSrcSize; + ZSTD_parameters params; + ZSTD_customMem customMem; +}; /* typedef'd to ZSTD_CStream within "zstd.h" */ + +ZSTD_CStream* ZSTD_createCStream(void) +{ + return ZSTD_createCStream_advanced(defaultCustomMem); +} + +ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem) +{ + ZSTD_CStream* zcs; + + if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem; + if (!customMem.customAlloc || !customMem.customFree) return NULL; + + zcs = (ZSTD_CStream*)ZSTD_malloc(sizeof(ZSTD_CStream), customMem); + if (zcs==NULL) return NULL; + memset(zcs, 0, sizeof(ZSTD_CStream)); + memcpy(&zcs->customMem, &customMem, sizeof(ZSTD_customMem)); + zcs->cctx = ZSTD_createCCtx_advanced(customMem); + if (zcs->cctx == NULL) { ZSTD_freeCStream(zcs); return NULL; } + return zcs; +} + +size_t ZSTD_freeCStream(ZSTD_CStream* zcs) +{ + if (zcs==NULL) return 0; /* support free on NULL */ + { ZSTD_customMem const cMem = zcs->customMem; + ZSTD_freeCCtx(zcs->cctx); + zcs->cctx = NULL; + ZSTD_freeCDict(zcs->cdictLocal); + zcs->cdictLocal = NULL; + ZSTD_free(zcs->inBuff, cMem); + zcs->inBuff = NULL; + ZSTD_free(zcs->outBuff, cMem); + zcs->outBuff = NULL; + ZSTD_free(zcs, cMem); + return 0; + } +} + + +/*====== Initialization ======*/ + +size_t ZSTD_CStreamInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; } + +size_t ZSTD_CStreamOutSize(void) +{ + return ZSTD_compressBound(ZSTD_BLOCKSIZE_ABSOLUTEMAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */ ; +} + +static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) +{ + if (zcs->inBuffSize==0) return ERROR(stage_wrong); /* zcs has not been init at least once => can't reset */ + + DEBUGLOG(5, "ZSTD_resetCStream_internal : dictIDFlag == %u \n", !zcs->params.fParams.noDictIDFlag); + + if (zcs->cdict) CHECK_F(ZSTD_compressBegin_usingCDict_advanced(zcs->cctx, zcs->cdict, zcs->params.fParams, pledgedSrcSize)) + else CHECK_F(ZSTD_compressBegin_internal(zcs->cctx, NULL, 0, zcs->params, pledgedSrcSize)); + + zcs->inToCompress = 0; + zcs->inBuffPos = 0; + zcs->inBuffTarget = zcs->blockSize; + zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0; + zcs->stage = zcss_load; + zcs->frameEnded = 0; + zcs->pledgedSrcSize = pledgedSrcSize; + return 0; /* ready to go */ +} + +size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) +{ + + zcs->params.fParams.contentSizeFlag = (pledgedSrcSize > 0); + DEBUGLOG(5, "ZSTD_resetCStream : dictIDFlag == %u \n", !zcs->params.fParams.noDictIDFlag); + return ZSTD_resetCStream_internal(zcs, pledgedSrcSize); +} + +/* ZSTD_initCStream_internal() : + * params are supposed validated at this stage + * and zcs->cdict is supposed to be correct */ +static size_t ZSTD_initCStream_stage2(ZSTD_CStream* zcs, + const ZSTD_parameters params, + unsigned long long pledgedSrcSize) +{ + assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); + + /* allocate buffers */ + { size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog; + if (zcs->inBuffSize < neededInBuffSize) { + zcs->inBuffSize = 0; + ZSTD_free(zcs->inBuff, zcs->customMem); + zcs->inBuff = (char*) ZSTD_malloc(neededInBuffSize, zcs->customMem); + if (zcs->inBuff == NULL) return ERROR(memory_allocation); + zcs->inBuffSize = neededInBuffSize; + } + zcs->blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, neededInBuffSize); + } + if (zcs->outBuffSize < ZSTD_compressBound(zcs->blockSize)+1) { + size_t const outBuffSize = ZSTD_compressBound(zcs->blockSize)+1; + zcs->outBuffSize = 0; + ZSTD_free(zcs->outBuff, zcs->customMem); + zcs->outBuff = (char*) ZSTD_malloc(outBuffSize, zcs->customMem); + if (zcs->outBuff == NULL) return ERROR(memory_allocation); + zcs->outBuffSize = outBuffSize; + } + + zcs->checksum = params.fParams.checksumFlag > 0; + zcs->params = params; + + DEBUGLOG(5, "ZSTD_initCStream_stage2 : dictIDFlag == %u \n", !params.fParams.noDictIDFlag); + return ZSTD_resetCStream_internal(zcs, pledgedSrcSize); +} + +/* ZSTD_initCStream_usingCDict_advanced() : + * same as ZSTD_initCStream_usingCDict(), with control over frame parameters */ +size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, unsigned long long pledgedSrcSize, ZSTD_frameParameters fParams) +{ + if (!cdict) return ERROR(GENERIC); /* cannot handle NULL cdict (does not know what to do) */ + { ZSTD_parameters params = ZSTD_getParamsFromCDict(cdict); + params.fParams = fParams; + zcs->cdict = cdict; + return ZSTD_initCStream_stage2(zcs, params, pledgedSrcSize); + } +} + +/* note : cdict must outlive compression session */ +size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict) +{ + ZSTD_frameParameters const fParams = { 0 /* content */, 0 /* checksum */, 0 /* noDictID */ }; + return ZSTD_initCStream_usingCDict_advanced(zcs, cdict, 0, fParams); +} + +static size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, + const void* dict, size_t dictSize, + ZSTD_parameters params, unsigned long long pledgedSrcSize) +{ + assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); + zcs->cdict = NULL; + + if (dict && dictSize >= 8) { + ZSTD_freeCDict(zcs->cdictLocal); + zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, 0 /* copy */, params.cParams, zcs->customMem); + if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); + zcs->cdict = zcs->cdictLocal; + } + + DEBUGLOG(5, "ZSTD_initCStream_internal : dictIDFlag == %u \n", !params.fParams.noDictIDFlag); + return ZSTD_initCStream_stage2(zcs, params, pledgedSrcSize); +} + +size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, + const void* dict, size_t dictSize, + ZSTD_parameters params, unsigned long long pledgedSrcSize) +{ + CHECK_F( ZSTD_checkCParams(params.cParams) ); + DEBUGLOG(5, "ZSTD_initCStream_advanced : dictIDFlag == %u \n", !params.fParams.noDictIDFlag); + return ZSTD_initCStream_internal(zcs, dict, dictSize, params, pledgedSrcSize); +} + +size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel) +{ + ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); + return ZSTD_initCStream_internal(zcs, dict, dictSize, params, 0); +} + +size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize) +{ + ZSTD_parameters params = ZSTD_getParams(compressionLevel, pledgedSrcSize, 0); + params.fParams.contentSizeFlag = (pledgedSrcSize>0); + return ZSTD_initCStream_internal(zcs, NULL, 0, params, pledgedSrcSize); +} + +size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel) +{ + ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, 0); + return ZSTD_initCStream_internal(zcs, NULL, 0, params, 0); +} + +size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs) +{ + if (zcs==NULL) return 0; /* support sizeof on NULL */ + return sizeof(*zcs) + ZSTD_sizeof_CCtx(zcs->cctx) + ZSTD_sizeof_CDict(zcs->cdictLocal) + zcs->outBuffSize + zcs->inBuffSize; +} + +/*====== Compression ======*/ + +typedef enum { zsf_gather, zsf_flush, zsf_end } ZSTD_flush_e; + +MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + size_t const length = MIN(dstCapacity, srcSize); + memcpy(dst, src, length); + return length; +} + +static size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, + void* dst, size_t* dstCapacityPtr, + const void* src, size_t* srcSizePtr, + ZSTD_flush_e const flush) +{ + U32 someMoreWork = 1; + const char* const istart = (const char*)src; + const char* const iend = istart + *srcSizePtr; + const char* ip = istart; + char* const ostart = (char*)dst; + char* const oend = ostart + *dstCapacityPtr; + char* op = ostart; + + while (someMoreWork) { + switch(zcs->stage) + { + case zcss_init: return ERROR(init_missing); /* call ZBUFF_compressInit() first ! */ + + case zcss_load: + /* complete inBuffer */ + { size_t const toLoad = zcs->inBuffTarget - zcs->inBuffPos; + size_t const loaded = ZSTD_limitCopy(zcs->inBuff + zcs->inBuffPos, toLoad, ip, iend-ip); + zcs->inBuffPos += loaded; + ip += loaded; + if ( (zcs->inBuffPos==zcs->inToCompress) || (!flush && (toLoad != loaded)) ) { + someMoreWork = 0; break; /* not enough input to get a full block : stop there, wait for more */ + } } + /* compress current block (note : this stage cannot be stopped in the middle) */ + { void* cDst; + size_t cSize; + size_t const iSize = zcs->inBuffPos - zcs->inToCompress; + size_t oSize = oend-op; + if (oSize >= ZSTD_compressBound(iSize)) + cDst = op; /* compress directly into output buffer (avoid flush stage) */ + else + cDst = zcs->outBuff, oSize = zcs->outBuffSize; + cSize = (flush == zsf_end) ? + ZSTD_compressEnd(zcs->cctx, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize) : + ZSTD_compressContinue(zcs->cctx, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize); + if (ZSTD_isError(cSize)) return cSize; + if (flush == zsf_end) zcs->frameEnded = 1; + /* prepare next block */ + zcs->inBuffTarget = zcs->inBuffPos + zcs->blockSize; + if (zcs->inBuffTarget > zcs->inBuffSize) + zcs->inBuffPos = 0, zcs->inBuffTarget = zcs->blockSize; /* note : inBuffSize >= blockSize */ + zcs->inToCompress = zcs->inBuffPos; + if (cDst == op) { op += cSize; break; } /* no need to flush */ + zcs->outBuffContentSize = cSize; + zcs->outBuffFlushedSize = 0; + zcs->stage = zcss_flush; /* pass-through to flush stage */ + } + + case zcss_flush: + { size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; + size_t const flushed = ZSTD_limitCopy(op, oend-op, zcs->outBuff + zcs->outBuffFlushedSize, toFlush); + op += flushed; + zcs->outBuffFlushedSize += flushed; + if (toFlush!=flushed) { someMoreWork = 0; break; } /* dst too small to store flushed data : stop there */ + zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0; + zcs->stage = zcss_load; + break; + } + + case zcss_final: + someMoreWork = 0; /* do nothing */ + break; + + default: + return ERROR(GENERIC); /* impossible */ + } + } + + *srcSizePtr = ip - istart; + *dstCapacityPtr = op - ostart; + if (zcs->frameEnded) return 0; + { size_t hintInSize = zcs->inBuffTarget - zcs->inBuffPos; + if (hintInSize==0) hintInSize = zcs->blockSize; + return hintInSize; + } +} + +size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input) +{ + size_t sizeRead = input->size - input->pos; + size_t sizeWritten = output->size - output->pos; + size_t const result = ZSTD_compressStream_generic(zcs, + (char*)(output->dst) + output->pos, &sizeWritten, + (const char*)(input->src) + input->pos, &sizeRead, zsf_gather); + input->pos += sizeRead; + output->pos += sizeWritten; + return result; +} + + +/*====== Finalize ======*/ + +/*! ZSTD_flushStream() : +* @return : amount of data remaining to flush */ +size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output) +{ + size_t srcSize = 0; + size_t sizeWritten = output->size - output->pos; + size_t const result = ZSTD_compressStream_generic(zcs, + (char*)(output->dst) + output->pos, &sizeWritten, + &srcSize, &srcSize, /* use a valid src address instead of NULL */ + zsf_flush); + output->pos += sizeWritten; + if (ZSTD_isError(result)) return result; + return zcs->outBuffContentSize - zcs->outBuffFlushedSize; /* remaining to flush */ +} + + +size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output) +{ + BYTE* const ostart = (BYTE*)(output->dst) + output->pos; + BYTE* const oend = (BYTE*)(output->dst) + output->size; + BYTE* op = ostart; + + if (zcs->stage != zcss_final) { + /* flush whatever remains */ + size_t srcSize = 0; + size_t sizeWritten = output->size - output->pos; + size_t const notEnded = ZSTD_compressStream_generic(zcs, ostart, &sizeWritten, + &srcSize /* use a valid src address instead of NULL */, &srcSize, zsf_end); + size_t const remainingToFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; + op += sizeWritten; + if (remainingToFlush) { + output->pos += sizeWritten; + return remainingToFlush + ZSTD_BLOCKHEADERSIZE /* final empty block */ + (zcs->checksum * 4); + } + /* create epilogue */ + zcs->stage = zcss_final; + zcs->outBuffContentSize = !notEnded ? 0 : + /* write epilogue, including final empty block, into outBuff */ + ZSTD_compressEnd(zcs->cctx, zcs->outBuff, zcs->outBuffSize, NULL, 0); + if (ZSTD_isError(zcs->outBuffContentSize)) return zcs->outBuffContentSize; + } + + /* flush epilogue */ + { size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; + size_t const flushed = ZSTD_limitCopy(op, oend-op, zcs->outBuff + zcs->outBuffFlushedSize, toFlush); + op += flushed; + zcs->outBuffFlushedSize += flushed; + output->pos += op-ostart; + if (toFlush==flushed) zcs->stage = zcss_init; /* end reached */ + return toFlush - flushed; + } +} + + + +/*-===== Pre-defined compression levels =====-*/ + +#define ZSTD_DEFAULT_CLEVEL 1 +#define ZSTD_MAX_CLEVEL 22 +int ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; } + +static const ZSTD_compressionParameters ZSTD_defaultCParameters[4][ZSTD_MAX_CLEVEL+1] = { +{ /* "default" */ + /* W, C, H, S, L, TL, strat */ + { 18, 12, 12, 1, 7, 16, ZSTD_fast }, /* level 0 - never used */ + { 19, 13, 14, 1, 7, 16, ZSTD_fast }, /* level 1 */ + { 19, 15, 16, 1, 6, 16, ZSTD_fast }, /* level 2 */ + { 20, 16, 17, 1, 5, 16, ZSTD_dfast }, /* level 3.*/ + { 20, 18, 18, 1, 5, 16, ZSTD_dfast }, /* level 4.*/ + { 20, 15, 18, 3, 5, 16, ZSTD_greedy }, /* level 5 */ + { 21, 16, 19, 2, 5, 16, ZSTD_lazy }, /* level 6 */ + { 21, 17, 20, 3, 5, 16, ZSTD_lazy }, /* level 7 */ + { 21, 18, 20, 3, 5, 16, ZSTD_lazy2 }, /* level 8 */ + { 21, 20, 20, 3, 5, 16, ZSTD_lazy2 }, /* level 9 */ + { 21, 19, 21, 4, 5, 16, ZSTD_lazy2 }, /* level 10 */ + { 22, 20, 22, 4, 5, 16, ZSTD_lazy2 }, /* level 11 */ + { 22, 20, 22, 5, 5, 16, ZSTD_lazy2 }, /* level 12 */ + { 22, 21, 22, 5, 5, 16, ZSTD_lazy2 }, /* level 13 */ + { 22, 21, 22, 6, 5, 16, ZSTD_lazy2 }, /* level 14 */ + { 22, 21, 21, 5, 5, 16, ZSTD_btlazy2 }, /* level 15 */ + { 23, 22, 22, 5, 5, 16, ZSTD_btlazy2 }, /* level 16 */ + { 23, 21, 22, 4, 5, 24, ZSTD_btopt }, /* level 17 */ + { 23, 22, 22, 5, 4, 32, ZSTD_btopt }, /* level 18 */ + { 23, 23, 22, 6, 3, 48, ZSTD_btopt }, /* level 19 */ + { 25, 25, 23, 7, 3, 64, ZSTD_btopt2 }, /* level 20 */ + { 26, 26, 23, 7, 3,256, ZSTD_btopt2 }, /* level 21 */ + { 27, 27, 25, 9, 3,512, ZSTD_btopt2 }, /* level 22 */ +}, +{ /* for srcSize <= 256 KB */ + /* W, C, H, S, L, T, strat */ + { 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - not used */ + { 18, 13, 14, 1, 6, 8, ZSTD_fast }, /* level 1 */ + { 18, 14, 13, 1, 5, 8, ZSTD_dfast }, /* level 2 */ + { 18, 16, 15, 1, 5, 8, ZSTD_dfast }, /* level 3 */ + { 18, 15, 17, 1, 5, 8, ZSTD_greedy }, /* level 4.*/ + { 18, 16, 17, 4, 5, 8, ZSTD_greedy }, /* level 5.*/ + { 18, 16, 17, 3, 5, 8, ZSTD_lazy }, /* level 6.*/ + { 18, 17, 17, 4, 4, 8, ZSTD_lazy }, /* level 7 */ + { 18, 17, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 8 */ + { 18, 17, 17, 5, 4, 8, ZSTD_lazy2 }, /* level 9 */ + { 18, 17, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 10 */ + { 18, 18, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 11.*/ + { 18, 18, 17, 7, 4, 8, ZSTD_lazy2 }, /* level 12.*/ + { 18, 19, 17, 6, 4, 8, ZSTD_btlazy2 }, /* level 13 */ + { 18, 18, 18, 4, 4, 16, ZSTD_btopt }, /* level 14.*/ + { 18, 18, 18, 4, 3, 16, ZSTD_btopt }, /* level 15.*/ + { 18, 19, 18, 6, 3, 32, ZSTD_btopt }, /* level 16.*/ + { 18, 19, 18, 8, 3, 64, ZSTD_btopt }, /* level 17.*/ + { 18, 19, 18, 9, 3,128, ZSTD_btopt }, /* level 18.*/ + { 18, 19, 18, 10, 3,256, ZSTD_btopt }, /* level 19.*/ + { 18, 19, 18, 11, 3,512, ZSTD_btopt2 }, /* level 20.*/ + { 18, 19, 18, 12, 3,512, ZSTD_btopt2 }, /* level 21.*/ + { 18, 19, 18, 13, 3,512, ZSTD_btopt2 }, /* level 22.*/ +}, +{ /* for srcSize <= 128 KB */ + /* W, C, H, S, L, T, strat */ + { 17, 12, 12, 1, 7, 8, ZSTD_fast }, /* level 0 - not used */ + { 17, 12, 13, 1, 6, 8, ZSTD_fast }, /* level 1 */ + { 17, 13, 16, 1, 5, 8, ZSTD_fast }, /* level 2 */ + { 17, 16, 16, 2, 5, 8, ZSTD_dfast }, /* level 3 */ + { 17, 13, 15, 3, 4, 8, ZSTD_greedy }, /* level 4 */ + { 17, 15, 17, 4, 4, 8, ZSTD_greedy }, /* level 5 */ + { 17, 16, 17, 3, 4, 8, ZSTD_lazy }, /* level 6 */ + { 17, 15, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 7 */ + { 17, 17, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 8 */ + { 17, 17, 17, 5, 4, 8, ZSTD_lazy2 }, /* level 9 */ + { 17, 17, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 10 */ + { 17, 17, 17, 7, 4, 8, ZSTD_lazy2 }, /* level 11 */ + { 17, 17, 17, 8, 4, 8, ZSTD_lazy2 }, /* level 12 */ + { 17, 18, 17, 6, 4, 8, ZSTD_btlazy2 }, /* level 13.*/ + { 17, 17, 17, 7, 3, 8, ZSTD_btopt }, /* level 14.*/ + { 17, 17, 17, 7, 3, 16, ZSTD_btopt }, /* level 15.*/ + { 17, 18, 17, 7, 3, 32, ZSTD_btopt }, /* level 16.*/ + { 17, 18, 17, 7, 3, 64, ZSTD_btopt }, /* level 17.*/ + { 17, 18, 17, 7, 3,256, ZSTD_btopt }, /* level 18.*/ + { 17, 18, 17, 8, 3,256, ZSTD_btopt }, /* level 19.*/ + { 17, 18, 17, 9, 3,256, ZSTD_btopt2 }, /* level 20.*/ + { 17, 18, 17, 10, 3,256, ZSTD_btopt2 }, /* level 21.*/ + { 17, 18, 17, 11, 3,512, ZSTD_btopt2 }, /* level 22.*/ +}, +{ /* for srcSize <= 16 KB */ + /* W, C, H, S, L, T, strat */ + { 14, 12, 12, 1, 7, 6, ZSTD_fast }, /* level 0 - not used */ + { 14, 14, 14, 1, 6, 6, ZSTD_fast }, /* level 1 */ + { 14, 14, 14, 1, 4, 6, ZSTD_fast }, /* level 2 */ + { 14, 14, 14, 1, 4, 6, ZSTD_dfast }, /* level 3.*/ + { 14, 14, 14, 4, 4, 6, ZSTD_greedy }, /* level 4.*/ + { 14, 14, 14, 3, 4, 6, ZSTD_lazy }, /* level 5.*/ + { 14, 14, 14, 4, 4, 6, ZSTD_lazy2 }, /* level 6 */ + { 14, 14, 14, 5, 4, 6, ZSTD_lazy2 }, /* level 7 */ + { 14, 14, 14, 6, 4, 6, ZSTD_lazy2 }, /* level 8.*/ + { 14, 15, 14, 6, 4, 6, ZSTD_btlazy2 }, /* level 9.*/ + { 14, 15, 14, 3, 3, 6, ZSTD_btopt }, /* level 10.*/ + { 14, 15, 14, 6, 3, 8, ZSTD_btopt }, /* level 11.*/ + { 14, 15, 14, 6, 3, 16, ZSTD_btopt }, /* level 12.*/ + { 14, 15, 14, 6, 3, 24, ZSTD_btopt }, /* level 13.*/ + { 14, 15, 15, 6, 3, 48, ZSTD_btopt }, /* level 14.*/ + { 14, 15, 15, 6, 3, 64, ZSTD_btopt }, /* level 15.*/ + { 14, 15, 15, 6, 3, 96, ZSTD_btopt }, /* level 16.*/ + { 14, 15, 15, 6, 3,128, ZSTD_btopt }, /* level 17.*/ + { 14, 15, 15, 6, 3,256, ZSTD_btopt }, /* level 18.*/ + { 14, 15, 15, 7, 3,256, ZSTD_btopt }, /* level 19.*/ + { 14, 15, 15, 8, 3,256, ZSTD_btopt2 }, /* level 20.*/ + { 14, 15, 15, 9, 3,256, ZSTD_btopt2 }, /* level 21.*/ + { 14, 15, 15, 10, 3,256, ZSTD_btopt2 }, /* level 22.*/ +}, +}; + +/*! ZSTD_getCParams() : +* @return ZSTD_compressionParameters structure for a selected compression level, `srcSize` and `dictSize`. +* Size values are optional, provide 0 if not known or unused */ +ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long srcSize, size_t dictSize) +{ + ZSTD_compressionParameters cp; + size_t const addedSize = srcSize ? 0 : 500; + U64 const rSize = srcSize+dictSize ? srcSize+dictSize+addedSize : (U64)-1; + U32 const tableID = (rSize <= 256 KB) + (rSize <= 128 KB) + (rSize <= 16 KB); /* intentional underflow for srcSizeHint == 0 */ + if (compressionLevel <= 0) compressionLevel = ZSTD_DEFAULT_CLEVEL; /* 0 == default; no negative compressionLevel yet */ + if (compressionLevel > ZSTD_MAX_CLEVEL) compressionLevel = ZSTD_MAX_CLEVEL; + cp = ZSTD_defaultCParameters[tableID][compressionLevel]; + if (MEM_32bits()) { /* auto-correction, for 32-bits mode */ + if (cp.windowLog > ZSTD_WINDOWLOG_MAX) cp.windowLog = ZSTD_WINDOWLOG_MAX; + if (cp.chainLog > ZSTD_CHAINLOG_MAX) cp.chainLog = ZSTD_CHAINLOG_MAX; + if (cp.hashLog > ZSTD_HASHLOG_MAX) cp.hashLog = ZSTD_HASHLOG_MAX; + } + cp = ZSTD_adjustCParams(cp, srcSize, dictSize); + return cp; +} + +/*! ZSTD_getParams() : +* same as ZSTD_getCParams(), but @return a `ZSTD_parameters` object (instead of `ZSTD_compressionParameters`). +* All fields of `ZSTD_frameParameters` are set to default (0) */ +ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long srcSize, size_t dictSize) { + ZSTD_parameters params; + ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, srcSize, dictSize); + memset(¶ms, 0, sizeof(params)); + params.cParams = cParams; + return params; +} diff --git a/thirdparty/zstd/compress/zstd_opt.h b/thirdparty/zstd/compress/zstd_opt.h new file mode 100644 index 0000000000..5437611912 --- /dev/null +++ b/thirdparty/zstd/compress/zstd_opt.h @@ -0,0 +1,921 @@ +/** + * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + + +/* Note : this file is intended to be included within zstd_compress.c */ + + +#ifndef ZSTD_OPT_H_91842398743 +#define ZSTD_OPT_H_91842398743 + + +#define ZSTD_LITFREQ_ADD 2 +#define ZSTD_FREQ_DIV 4 +#define ZSTD_MAX_PRICE (1<<30) + +/*-************************************* +* Price functions for optimal parser +***************************************/ +FORCE_INLINE void ZSTD_setLog2Prices(seqStore_t* ssPtr) +{ + ssPtr->log2matchLengthSum = ZSTD_highbit32(ssPtr->matchLengthSum+1); + ssPtr->log2litLengthSum = ZSTD_highbit32(ssPtr->litLengthSum+1); + ssPtr->log2litSum = ZSTD_highbit32(ssPtr->litSum+1); + ssPtr->log2offCodeSum = ZSTD_highbit32(ssPtr->offCodeSum+1); + ssPtr->factor = 1 + ((ssPtr->litSum>>5) / ssPtr->litLengthSum) + ((ssPtr->litSum<<1) / (ssPtr->litSum + ssPtr->matchSum)); +} + + +MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr, const BYTE* src, size_t srcSize) +{ + unsigned u; + + ssPtr->cachedLiterals = NULL; + ssPtr->cachedPrice = ssPtr->cachedLitLength = 0; + ssPtr->staticPrices = 0; + + if (ssPtr->litLengthSum == 0) { + if (srcSize <= 1024) ssPtr->staticPrices = 1; + + for (u=0; u<=MaxLit; u++) + ssPtr->litFreq[u] = 0; + for (u=0; u<srcSize; u++) + ssPtr->litFreq[src[u]]++; + + ssPtr->litSum = 0; + ssPtr->litLengthSum = MaxLL+1; + ssPtr->matchLengthSum = MaxML+1; + ssPtr->offCodeSum = (MaxOff+1); + ssPtr->matchSum = (ZSTD_LITFREQ_ADD<<Litbits); + + for (u=0; u<=MaxLit; u++) { + ssPtr->litFreq[u] = 1 + (ssPtr->litFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->litSum += ssPtr->litFreq[u]; + } + for (u=0; u<=MaxLL; u++) + ssPtr->litLengthFreq[u] = 1; + for (u=0; u<=MaxML; u++) + ssPtr->matchLengthFreq[u] = 1; + for (u=0; u<=MaxOff; u++) + ssPtr->offCodeFreq[u] = 1; + } else { + ssPtr->matchLengthSum = 0; + ssPtr->litLengthSum = 0; + ssPtr->offCodeSum = 0; + ssPtr->matchSum = 0; + ssPtr->litSum = 0; + + for (u=0; u<=MaxLit; u++) { + ssPtr->litFreq[u] = 1 + (ssPtr->litFreq[u]>>(ZSTD_FREQ_DIV+1)); + ssPtr->litSum += ssPtr->litFreq[u]; + } + for (u=0; u<=MaxLL; u++) { + ssPtr->litLengthFreq[u] = 1 + (ssPtr->litLengthFreq[u]>>(ZSTD_FREQ_DIV+1)); + ssPtr->litLengthSum += ssPtr->litLengthFreq[u]; + } + for (u=0; u<=MaxML; u++) { + ssPtr->matchLengthFreq[u] = 1 + (ssPtr->matchLengthFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->matchLengthSum += ssPtr->matchLengthFreq[u]; + ssPtr->matchSum += ssPtr->matchLengthFreq[u] * (u + 3); + } + ssPtr->matchSum *= ZSTD_LITFREQ_ADD; + for (u=0; u<=MaxOff; u++) { + ssPtr->offCodeFreq[u] = 1 + (ssPtr->offCodeFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->offCodeSum += ssPtr->offCodeFreq[u]; + } + } + + ZSTD_setLog2Prices(ssPtr); +} + + +FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* ssPtr, U32 litLength, const BYTE* literals) +{ + U32 price, u; + + if (ssPtr->staticPrices) + return ZSTD_highbit32((U32)litLength+1) + (litLength*6); + + if (litLength == 0) + return ssPtr->log2litLengthSum - ZSTD_highbit32(ssPtr->litLengthFreq[0]+1); + + /* literals */ + if (ssPtr->cachedLiterals == literals) { + U32 const additional = litLength - ssPtr->cachedLitLength; + const BYTE* literals2 = ssPtr->cachedLiterals + ssPtr->cachedLitLength; + price = ssPtr->cachedPrice + additional * ssPtr->log2litSum; + for (u=0; u < additional; u++) + price -= ZSTD_highbit32(ssPtr->litFreq[literals2[u]]+1); + ssPtr->cachedPrice = price; + ssPtr->cachedLitLength = litLength; + } else { + price = litLength * ssPtr->log2litSum; + for (u=0; u < litLength; u++) + price -= ZSTD_highbit32(ssPtr->litFreq[literals[u]]+1); + + if (litLength >= 12) { + ssPtr->cachedLiterals = literals; + ssPtr->cachedPrice = price; + ssPtr->cachedLitLength = litLength; + } + } + + /* literal Length */ + { const BYTE LL_deltaCode = 19; + const BYTE llCode = (litLength>63) ? (BYTE)ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength]; + price += LL_bits[llCode] + ssPtr->log2litLengthSum - ZSTD_highbit32(ssPtr->litLengthFreq[llCode]+1); + } + + return price; +} + + +FORCE_INLINE U32 ZSTD_getPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength, const int ultra) +{ + /* offset */ + U32 price; + BYTE const offCode = (BYTE)ZSTD_highbit32(offset+1); + + if (seqStorePtr->staticPrices) + return ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit32((U32)matchLength+1) + 16 + offCode; + + price = offCode + seqStorePtr->log2offCodeSum - ZSTD_highbit32(seqStorePtr->offCodeFreq[offCode]+1); + if (!ultra && offCode >= 20) price += (offCode-19)*2; + + /* match Length */ + { const BYTE ML_deltaCode = 36; + const BYTE mlCode = (matchLength>127) ? (BYTE)ZSTD_highbit32(matchLength) + ML_deltaCode : ML_Code[matchLength]; + price += ML_bits[mlCode] + seqStorePtr->log2matchLengthSum - ZSTD_highbit32(seqStorePtr->matchLengthFreq[mlCode]+1); + } + + return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + seqStorePtr->factor; +} + + +MEM_STATIC void ZSTD_updatePrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) +{ + U32 u; + + /* literals */ + seqStorePtr->litSum += litLength*ZSTD_LITFREQ_ADD; + for (u=0; u < litLength; u++) + seqStorePtr->litFreq[literals[u]] += ZSTD_LITFREQ_ADD; + + /* literal Length */ + { const BYTE LL_deltaCode = 19; + const BYTE llCode = (litLength>63) ? (BYTE)ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength]; + seqStorePtr->litLengthFreq[llCode]++; + seqStorePtr->litLengthSum++; + } + + /* match offset */ + { BYTE const offCode = (BYTE)ZSTD_highbit32(offset+1); + seqStorePtr->offCodeSum++; + seqStorePtr->offCodeFreq[offCode]++; + } + + /* match Length */ + { const BYTE ML_deltaCode = 36; + const BYTE mlCode = (matchLength>127) ? (BYTE)ZSTD_highbit32(matchLength) + ML_deltaCode : ML_Code[matchLength]; + seqStorePtr->matchLengthFreq[mlCode]++; + seqStorePtr->matchLengthSum++; + } + + ZSTD_setLog2Prices(seqStorePtr); +} + + +#define SET_PRICE(pos, mlen_, offset_, litlen_, price_) \ + { \ + while (last_pos < pos) { opt[last_pos+1].price = ZSTD_MAX_PRICE; last_pos++; } \ + opt[pos].mlen = mlen_; \ + opt[pos].off = offset_; \ + opt[pos].litlen = litlen_; \ + opt[pos].price = price_; \ + } + + + +/* Update hashTable3 up to ip (excluded) + Assumption : always within prefix (i.e. not within extDict) */ +FORCE_INLINE +U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_CCtx* zc, const BYTE* ip) +{ + U32* const hashTable3 = zc->hashTable3; + U32 const hashLog3 = zc->hashLog3; + const BYTE* const base = zc->base; + U32 idx = zc->nextToUpdate3; + const U32 target = zc->nextToUpdate3 = (U32)(ip - base); + const size_t hash3 = ZSTD_hash3Ptr(ip, hashLog3); + + while(idx < target) { + hashTable3[ZSTD_hash3Ptr(base+idx, hashLog3)] = idx; + idx++; + } + + return hashTable3[hash3]; +} + + +/*-************************************* +* Binary Tree search +***************************************/ +static U32 ZSTD_insertBtAndGetAllMatches ( + ZSTD_CCtx* zc, + const BYTE* const ip, const BYTE* const iLimit, + U32 nbCompares, const U32 mls, + U32 extDict, ZSTD_match_t* matches, const U32 minMatchLen) +{ + const BYTE* const base = zc->base; + const U32 current = (U32)(ip-base); + const U32 hashLog = zc->params.cParams.hashLog; + const size_t h = ZSTD_hashPtr(ip, hashLog, mls); + U32* const hashTable = zc->hashTable; + U32 matchIndex = hashTable[h]; + U32* const bt = zc->chainTable; + const U32 btLog = zc->params.cParams.chainLog - 1; + const U32 btMask= (1U << btLog) - 1; + size_t commonLengthSmaller=0, commonLengthLarger=0; + const BYTE* const dictBase = zc->dictBase; + const U32 dictLimit = zc->dictLimit; + const BYTE* const dictEnd = dictBase + dictLimit; + const BYTE* const prefixStart = base + dictLimit; + const U32 btLow = btMask >= current ? 0 : current - btMask; + const U32 windowLow = zc->lowLimit; + U32* smallerPtr = bt + 2*(current&btMask); + U32* largerPtr = bt + 2*(current&btMask) + 1; + U32 matchEndIdx = current+8; + U32 dummy32; /* to be nullified at the end */ + U32 mnum = 0; + + const U32 minMatch = (mls == 3) ? 3 : 4; + size_t bestLength = minMatchLen-1; + + if (minMatch == 3) { /* HC3 match finder */ + U32 const matchIndex3 = ZSTD_insertAndFindFirstIndexHash3 (zc, ip); + if (matchIndex3>windowLow && (current - matchIndex3 < (1<<18))) { + const BYTE* match; + size_t currentMl=0; + if ((!extDict) || matchIndex3 >= dictLimit) { + match = base + matchIndex3; + if (match[bestLength] == ip[bestLength]) currentMl = ZSTD_count(ip, match, iLimit); + } else { + match = dictBase + matchIndex3; + if (MEM_readMINMATCH(match, MINMATCH) == MEM_readMINMATCH(ip, MINMATCH)) /* assumption : matchIndex3 <= dictLimit-4 (by table construction) */ + currentMl = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iLimit, dictEnd, prefixStart) + MINMATCH; + } + + /* save best solution */ + if (currentMl > bestLength) { + bestLength = currentMl; + matches[mnum].off = ZSTD_REP_MOVE_OPT + current - matchIndex3; + matches[mnum].len = (U32)currentMl; + mnum++; + if (currentMl > ZSTD_OPT_NUM) goto update; + if (ip+currentMl == iLimit) goto update; /* best possible, and avoid read overflow*/ + } + } + } + + hashTable[h] = current; /* Update Hash Table */ + + while (nbCompares-- && (matchIndex > windowLow)) { + U32* nextPtr = bt + 2*(matchIndex & btMask); + size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ + const BYTE* match; + + if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { + match = base + matchIndex; + if (match[matchLength] == ip[matchLength]) { + matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iLimit) +1; + } + } else { + match = dictBase + matchIndex; + matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iLimit, dictEnd, prefixStart); + if (matchIndex+matchLength >= dictLimit) + match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ + } + + if (matchLength > bestLength) { + if (matchLength > matchEndIdx - matchIndex) matchEndIdx = matchIndex + (U32)matchLength; + bestLength = matchLength; + matches[mnum].off = ZSTD_REP_MOVE_OPT + current - matchIndex; + matches[mnum].len = (U32)matchLength; + mnum++; + if (matchLength > ZSTD_OPT_NUM) break; + if (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */ + break; /* drop, to guarantee consistency (miss a little bit of compression) */ + } + + if (match[matchLength] < ip[matchLength]) { + /* match is smaller than current */ + *smallerPtr = matchIndex; /* update smaller idx */ + commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ + if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ + smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ + matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ + } else { + /* match is larger than current */ + *largerPtr = matchIndex; + commonLengthLarger = matchLength; + if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ + largerPtr = nextPtr; + matchIndex = nextPtr[0]; + } } + + *smallerPtr = *largerPtr = 0; + +update: + zc->nextToUpdate = (matchEndIdx > current + 8) ? matchEndIdx - 8 : current+1; + return mnum; +} + + +/** Tree updater, providing best match */ +static U32 ZSTD_BtGetAllMatches ( + ZSTD_CCtx* zc, + const BYTE* const ip, const BYTE* const iLimit, + const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches, const U32 minMatchLen) +{ + if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ + ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls); + return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 0, matches, minMatchLen); +} + + +static U32 ZSTD_BtGetAllMatches_selectMLS ( + ZSTD_CCtx* zc, /* Index table will be updated */ + const BYTE* ip, const BYTE* const iHighLimit, + const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches, const U32 minMatchLen) +{ + switch(matchLengthSearch) + { + case 3 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 3, matches, minMatchLen); + default : + case 4 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 4, matches, minMatchLen); + case 5 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 5, matches, minMatchLen); + case 7 : + case 6 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 6, matches, minMatchLen); + } +} + +/** Tree updater, providing best match */ +static U32 ZSTD_BtGetAllMatches_extDict ( + ZSTD_CCtx* zc, + const BYTE* const ip, const BYTE* const iLimit, + const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches, const U32 minMatchLen) +{ + if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ + ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls); + return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 1, matches, minMatchLen); +} + + +static U32 ZSTD_BtGetAllMatches_selectMLS_extDict ( + ZSTD_CCtx* zc, /* Index table will be updated */ + const BYTE* ip, const BYTE* const iHighLimit, + const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches, const U32 minMatchLen) +{ + switch(matchLengthSearch) + { + case 3 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 3, matches, minMatchLen); + default : + case 4 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 4, matches, minMatchLen); + case 5 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 5, matches, minMatchLen); + case 7 : + case 6 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 6, matches, minMatchLen); + } +} + + +/*-******************************* +* Optimal parser +*********************************/ +FORCE_INLINE +void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, + const void* src, size_t srcSize, const int ultra) +{ + seqStore_t* seqStorePtr = &(ctx->seqStore); + const BYTE* const istart = (const BYTE*)src; + const BYTE* ip = istart; + const BYTE* anchor = istart; + const BYTE* const iend = istart + srcSize; + const BYTE* const ilimit = iend - 8; + const BYTE* const base = ctx->base; + const BYTE* const prefixStart = base + ctx->dictLimit; + + const U32 maxSearches = 1U << ctx->params.cParams.searchLog; + const U32 sufficient_len = ctx->params.cParams.targetLength; + const U32 mls = ctx->params.cParams.searchLength; + const U32 minMatch = (ctx->params.cParams.searchLength == 3) ? 3 : 4; + + ZSTD_optimal_t* opt = seqStorePtr->priceTable; + ZSTD_match_t* matches = seqStorePtr->matchTable; + const BYTE* inr; + U32 offset, rep[ZSTD_REP_NUM]; + + /* init */ + ctx->nextToUpdate3 = ctx->nextToUpdate; + ZSTD_rescaleFreqs(seqStorePtr, (const BYTE*)src, srcSize); + ip += (ip==prefixStart); + { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) rep[i]=ctx->rep[i]; } + + /* Match Loop */ + while (ip < ilimit) { + U32 cur, match_num, last_pos, litlen, price; + U32 u, mlen, best_mlen, best_off, litLength; + memset(opt, 0, sizeof(ZSTD_optimal_t)); + last_pos = 0; + litlen = (U32)(ip - anchor); + + /* check repCode */ + { U32 i, last_i = ZSTD_REP_CHECK + (ip==anchor); + for (i=(ip == anchor); i<last_i; i++) { + const S32 repCur = (i==ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : rep[i]; + if ( (repCur > 0) && (repCur < (S32)(ip-prefixStart)) + && (MEM_readMINMATCH(ip, minMatch) == MEM_readMINMATCH(ip - repCur, minMatch))) { + mlen = (U32)ZSTD_count(ip+minMatch, ip+minMatch-repCur, iend) + minMatch; + if (mlen > sufficient_len || mlen >= ZSTD_OPT_NUM) { + best_mlen = mlen; best_off = i; cur = 0; last_pos = 1; + goto _storeSequence; + } + best_off = i - (ip == anchor); + do { + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra); + if (mlen > last_pos || price < opt[mlen].price) + SET_PRICE(mlen, mlen, i, litlen, price); /* note : macro modifies last_pos */ + mlen--; + } while (mlen >= minMatch); + } } } + + match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, ip, iend, maxSearches, mls, matches, minMatch); + + if (!last_pos && !match_num) { ip++; continue; } + + if (match_num && (matches[match_num-1].len > sufficient_len || matches[match_num-1].len >= ZSTD_OPT_NUM)) { + best_mlen = matches[match_num-1].len; + best_off = matches[match_num-1].off; + cur = 0; + last_pos = 1; + goto _storeSequence; + } + + /* set prices using matches at position = 0 */ + best_mlen = (last_pos) ? last_pos : minMatch; + for (u = 0; u < match_num; u++) { + mlen = (u>0) ? matches[u-1].len+1 : best_mlen; + best_mlen = matches[u].len; + while (mlen <= best_mlen) { + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off-1, mlen - MINMATCH, ultra); + if (mlen > last_pos || price < opt[mlen].price) + SET_PRICE(mlen, mlen, matches[u].off, litlen, price); /* note : macro modifies last_pos */ + mlen++; + } } + + if (last_pos < minMatch) { ip++; continue; } + + /* initialize opt[0] */ + { U32 i ; for (i=0; i<ZSTD_REP_NUM; i++) opt[0].rep[i] = rep[i]; } + opt[0].mlen = 1; + opt[0].litlen = litlen; + + /* check further positions */ + for (cur = 1; cur <= last_pos; cur++) { + inr = ip + cur; + + if (opt[cur-1].mlen == 1) { + litlen = opt[cur-1].litlen + 1; + if (cur > litlen) { + price = opt[cur - litlen].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-litlen); + } else + price = ZSTD_getLiteralPrice(seqStorePtr, litlen, anchor); + } else { + litlen = 1; + price = opt[cur - 1].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-1); + } + + if (cur > last_pos || price <= opt[cur].price) + SET_PRICE(cur, 1, 0, litlen, price); + + if (cur == last_pos) break; + + if (inr > ilimit) /* last match must start at a minimum distance of 8 from oend */ + continue; + + mlen = opt[cur].mlen; + if (opt[cur].off > ZSTD_REP_MOVE_OPT) { + opt[cur].rep[2] = opt[cur-mlen].rep[1]; + opt[cur].rep[1] = opt[cur-mlen].rep[0]; + opt[cur].rep[0] = opt[cur].off - ZSTD_REP_MOVE_OPT; + } else { + opt[cur].rep[2] = (opt[cur].off > 1) ? opt[cur-mlen].rep[1] : opt[cur-mlen].rep[2]; + opt[cur].rep[1] = (opt[cur].off > 0) ? opt[cur-mlen].rep[0] : opt[cur-mlen].rep[1]; + opt[cur].rep[0] = ((opt[cur].off==ZSTD_REP_MOVE_OPT) && (mlen != 1)) ? (opt[cur-mlen].rep[0] - 1) : (opt[cur-mlen].rep[opt[cur].off]); + } + + best_mlen = minMatch; + { U32 i, last_i = ZSTD_REP_CHECK + (mlen != 1); + for (i=(opt[cur].mlen != 1); i<last_i; i++) { /* check rep */ + const S32 repCur = (i==ZSTD_REP_MOVE_OPT) ? (opt[cur].rep[0] - 1) : opt[cur].rep[i]; + if ( (repCur > 0) && (repCur < (S32)(inr-prefixStart)) + && (MEM_readMINMATCH(inr, minMatch) == MEM_readMINMATCH(inr - repCur, minMatch))) { + mlen = (U32)ZSTD_count(inr+minMatch, inr+minMatch - repCur, iend) + minMatch; + + if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) { + best_mlen = mlen; best_off = i; last_pos = cur + 1; + goto _storeSequence; + } + + best_off = i - (opt[cur].mlen != 1); + if (mlen > best_mlen) best_mlen = mlen; + + do { + if (opt[cur].mlen == 1) { + litlen = opt[cur].litlen; + if (cur > litlen) { + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, best_off, mlen - MINMATCH, ultra); + } else + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra); + } else { + litlen = 0; + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, best_off, mlen - MINMATCH, ultra); + } + + if (cur + mlen > last_pos || price <= opt[cur + mlen].price) + SET_PRICE(cur + mlen, mlen, i, litlen, price); + mlen--; + } while (mlen >= minMatch); + } } } + + match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, inr, iend, maxSearches, mls, matches, best_mlen); + + if (match_num > 0 && (matches[match_num-1].len > sufficient_len || cur + matches[match_num-1].len >= ZSTD_OPT_NUM)) { + best_mlen = matches[match_num-1].len; + best_off = matches[match_num-1].off; + last_pos = cur + 1; + goto _storeSequence; + } + + /* set prices using matches at position = cur */ + for (u = 0; u < match_num; u++) { + mlen = (u>0) ? matches[u-1].len+1 : best_mlen; + best_mlen = matches[u].len; + + while (mlen <= best_mlen) { + if (opt[cur].mlen == 1) { + litlen = opt[cur].litlen; + if (cur > litlen) + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur-litlen, matches[u].off-1, mlen - MINMATCH, ultra); + else + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off-1, mlen - MINMATCH, ultra); + } else { + litlen = 0; + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off-1, mlen - MINMATCH, ultra); + } + + if (cur + mlen > last_pos || (price < opt[cur + mlen].price)) + SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price); + + mlen++; + } } } + + best_mlen = opt[last_pos].mlen; + best_off = opt[last_pos].off; + cur = last_pos - best_mlen; + + /* store sequence */ +_storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ + opt[0].mlen = 1; + + while (1) { + mlen = opt[cur].mlen; + offset = opt[cur].off; + opt[cur].mlen = best_mlen; + opt[cur].off = best_off; + best_mlen = mlen; + best_off = offset; + if (mlen > cur) break; + cur -= mlen; + } + + for (u = 0; u <= last_pos;) { + u += opt[u].mlen; + } + + for (cur=0; cur < last_pos; ) { + mlen = opt[cur].mlen; + if (mlen == 1) { ip++; cur++; continue; } + offset = opt[cur].off; + cur += mlen; + litLength = (U32)(ip - anchor); + + if (offset > ZSTD_REP_MOVE_OPT) { + rep[2] = rep[1]; + rep[1] = rep[0]; + rep[0] = offset - ZSTD_REP_MOVE_OPT; + offset--; + } else { + if (offset != 0) { + best_off = (offset==ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : (rep[offset]); + if (offset != 1) rep[2] = rep[1]; + rep[1] = rep[0]; + rep[0] = best_off; + } + if (litLength==0) offset--; + } + + ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH); + ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH); + anchor = ip = ip + mlen; + } } /* for (cur=0; cur < last_pos; ) */ + + /* Save reps for next block */ + { int i; for (i=0; i<ZSTD_REP_NUM; i++) ctx->repToConfirm[i] = rep[i]; } + + /* Last Literals */ + { size_t const lastLLSize = iend - anchor; + memcpy(seqStorePtr->lit, anchor, lastLLSize); + seqStorePtr->lit += lastLLSize; + } +} + + +FORCE_INLINE +void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, + const void* src, size_t srcSize, const int ultra) +{ + seqStore_t* seqStorePtr = &(ctx->seqStore); + const BYTE* const istart = (const BYTE*)src; + const BYTE* ip = istart; + const BYTE* anchor = istart; + const BYTE* const iend = istart + srcSize; + const BYTE* const ilimit = iend - 8; + const BYTE* const base = ctx->base; + const U32 lowestIndex = ctx->lowLimit; + const U32 dictLimit = ctx->dictLimit; + const BYTE* const prefixStart = base + dictLimit; + const BYTE* const dictBase = ctx->dictBase; + const BYTE* const dictEnd = dictBase + dictLimit; + + const U32 maxSearches = 1U << ctx->params.cParams.searchLog; + const U32 sufficient_len = ctx->params.cParams.targetLength; + const U32 mls = ctx->params.cParams.searchLength; + const U32 minMatch = (ctx->params.cParams.searchLength == 3) ? 3 : 4; + + ZSTD_optimal_t* opt = seqStorePtr->priceTable; + ZSTD_match_t* matches = seqStorePtr->matchTable; + const BYTE* inr; + + /* init */ + U32 offset, rep[ZSTD_REP_NUM]; + { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) rep[i]=ctx->rep[i]; } + + ctx->nextToUpdate3 = ctx->nextToUpdate; + ZSTD_rescaleFreqs(seqStorePtr, (const BYTE*)src, srcSize); + ip += (ip==prefixStart); + + /* Match Loop */ + while (ip < ilimit) { + U32 cur, match_num, last_pos, litlen, price; + U32 u, mlen, best_mlen, best_off, litLength; + U32 current = (U32)(ip-base); + memset(opt, 0, sizeof(ZSTD_optimal_t)); + last_pos = 0; + opt[0].litlen = (U32)(ip - anchor); + + /* check repCode */ + { U32 i, last_i = ZSTD_REP_CHECK + (ip==anchor); + for (i = (ip==anchor); i<last_i; i++) { + const S32 repCur = (i==ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : rep[i]; + const U32 repIndex = (U32)(current - repCur); + const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; + const BYTE* const repMatch = repBase + repIndex; + if ( (repCur > 0 && repCur <= (S32)current) + && (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex>lowestIndex)) /* intentional overflow */ + && (MEM_readMINMATCH(ip, minMatch) == MEM_readMINMATCH(repMatch, minMatch)) ) { + /* repcode detected we should take it */ + const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; + mlen = (U32)ZSTD_count_2segments(ip+minMatch, repMatch+minMatch, iend, repEnd, prefixStart) + minMatch; + + if (mlen > sufficient_len || mlen >= ZSTD_OPT_NUM) { + best_mlen = mlen; best_off = i; cur = 0; last_pos = 1; + goto _storeSequence; + } + + best_off = i - (ip==anchor); + litlen = opt[0].litlen; + do { + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra); + if (mlen > last_pos || price < opt[mlen].price) + SET_PRICE(mlen, mlen, i, litlen, price); /* note : macro modifies last_pos */ + mlen--; + } while (mlen >= minMatch); + } } } + + match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, ip, iend, maxSearches, mls, matches, minMatch); /* first search (depth 0) */ + + if (!last_pos && !match_num) { ip++; continue; } + + { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) opt[0].rep[i] = rep[i]; } + opt[0].mlen = 1; + + if (match_num && (matches[match_num-1].len > sufficient_len || matches[match_num-1].len >= ZSTD_OPT_NUM)) { + best_mlen = matches[match_num-1].len; + best_off = matches[match_num-1].off; + cur = 0; + last_pos = 1; + goto _storeSequence; + } + + best_mlen = (last_pos) ? last_pos : minMatch; + + /* set prices using matches at position = 0 */ + for (u = 0; u < match_num; u++) { + mlen = (u>0) ? matches[u-1].len+1 : best_mlen; + best_mlen = matches[u].len; + litlen = opt[0].litlen; + while (mlen <= best_mlen) { + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off-1, mlen - MINMATCH, ultra); + if (mlen > last_pos || price < opt[mlen].price) + SET_PRICE(mlen, mlen, matches[u].off, litlen, price); + mlen++; + } } + + if (last_pos < minMatch) { + ip++; continue; + } + + /* check further positions */ + for (cur = 1; cur <= last_pos; cur++) { + inr = ip + cur; + + if (opt[cur-1].mlen == 1) { + litlen = opt[cur-1].litlen + 1; + if (cur > litlen) { + price = opt[cur - litlen].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-litlen); + } else + price = ZSTD_getLiteralPrice(seqStorePtr, litlen, anchor); + } else { + litlen = 1; + price = opt[cur - 1].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-1); + } + + if (cur > last_pos || price <= opt[cur].price) + SET_PRICE(cur, 1, 0, litlen, price); + + if (cur == last_pos) break; + + if (inr > ilimit) /* last match must start at a minimum distance of 8 from oend */ + continue; + + mlen = opt[cur].mlen; + if (opt[cur].off > ZSTD_REP_MOVE_OPT) { + opt[cur].rep[2] = opt[cur-mlen].rep[1]; + opt[cur].rep[1] = opt[cur-mlen].rep[0]; + opt[cur].rep[0] = opt[cur].off - ZSTD_REP_MOVE_OPT; + } else { + opt[cur].rep[2] = (opt[cur].off > 1) ? opt[cur-mlen].rep[1] : opt[cur-mlen].rep[2]; + opt[cur].rep[1] = (opt[cur].off > 0) ? opt[cur-mlen].rep[0] : opt[cur-mlen].rep[1]; + opt[cur].rep[0] = ((opt[cur].off==ZSTD_REP_MOVE_OPT) && (mlen != 1)) ? (opt[cur-mlen].rep[0] - 1) : (opt[cur-mlen].rep[opt[cur].off]); + } + + best_mlen = minMatch; + { U32 i, last_i = ZSTD_REP_CHECK + (mlen != 1); + for (i = (mlen != 1); i<last_i; i++) { + const S32 repCur = (i==ZSTD_REP_MOVE_OPT) ? (opt[cur].rep[0] - 1) : opt[cur].rep[i]; + const U32 repIndex = (U32)(current+cur - repCur); + const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; + const BYTE* const repMatch = repBase + repIndex; + if ( (repCur > 0 && repCur <= (S32)(current+cur)) + && (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex>lowestIndex)) /* intentional overflow */ + && (MEM_readMINMATCH(inr, minMatch) == MEM_readMINMATCH(repMatch, minMatch)) ) { + /* repcode detected */ + const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; + mlen = (U32)ZSTD_count_2segments(inr+minMatch, repMatch+minMatch, iend, repEnd, prefixStart) + minMatch; + + if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) { + best_mlen = mlen; best_off = i; last_pos = cur + 1; + goto _storeSequence; + } + + best_off = i - (opt[cur].mlen != 1); + if (mlen > best_mlen) best_mlen = mlen; + + do { + if (opt[cur].mlen == 1) { + litlen = opt[cur].litlen; + if (cur > litlen) { + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, best_off, mlen - MINMATCH, ultra); + } else + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra); + } else { + litlen = 0; + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, best_off, mlen - MINMATCH, ultra); + } + + if (cur + mlen > last_pos || price <= opt[cur + mlen].price) + SET_PRICE(cur + mlen, mlen, i, litlen, price); + mlen--; + } while (mlen >= minMatch); + } } } + + match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, inr, iend, maxSearches, mls, matches, minMatch); + + if (match_num > 0 && (matches[match_num-1].len > sufficient_len || cur + matches[match_num-1].len >= ZSTD_OPT_NUM)) { + best_mlen = matches[match_num-1].len; + best_off = matches[match_num-1].off; + last_pos = cur + 1; + goto _storeSequence; + } + + /* set prices using matches at position = cur */ + for (u = 0; u < match_num; u++) { + mlen = (u>0) ? matches[u-1].len+1 : best_mlen; + best_mlen = matches[u].len; + + while (mlen <= best_mlen) { + if (opt[cur].mlen == 1) { + litlen = opt[cur].litlen; + if (cur > litlen) + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur-litlen, matches[u].off-1, mlen - MINMATCH, ultra); + else + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off-1, mlen - MINMATCH, ultra); + } else { + litlen = 0; + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off-1, mlen - MINMATCH, ultra); + } + + if (cur + mlen > last_pos || (price < opt[cur + mlen].price)) + SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price); + + mlen++; + } } } /* for (cur = 1; cur <= last_pos; cur++) */ + + best_mlen = opt[last_pos].mlen; + best_off = opt[last_pos].off; + cur = last_pos - best_mlen; + + /* store sequence */ +_storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ + opt[0].mlen = 1; + + while (1) { + mlen = opt[cur].mlen; + offset = opt[cur].off; + opt[cur].mlen = best_mlen; + opt[cur].off = best_off; + best_mlen = mlen; + best_off = offset; + if (mlen > cur) break; + cur -= mlen; + } + + for (u = 0; u <= last_pos; ) { + u += opt[u].mlen; + } + + for (cur=0; cur < last_pos; ) { + mlen = opt[cur].mlen; + if (mlen == 1) { ip++; cur++; continue; } + offset = opt[cur].off; + cur += mlen; + litLength = (U32)(ip - anchor); + + if (offset > ZSTD_REP_MOVE_OPT) { + rep[2] = rep[1]; + rep[1] = rep[0]; + rep[0] = offset - ZSTD_REP_MOVE_OPT; + offset--; + } else { + if (offset != 0) { + best_off = (offset==ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : (rep[offset]); + if (offset != 1) rep[2] = rep[1]; + rep[1] = rep[0]; + rep[0] = best_off; + } + + if (litLength==0) offset--; + } + + ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH); + ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH); + anchor = ip = ip + mlen; + } } /* for (cur=0; cur < last_pos; ) */ + + /* Save reps for next block */ + { int i; for (i=0; i<ZSTD_REP_NUM; i++) ctx->repToConfirm[i] = rep[i]; } + + /* Last Literals */ + { size_t lastLLSize = iend - anchor; + memcpy(seqStorePtr->lit, anchor, lastLLSize); + seqStorePtr->lit += lastLLSize; + } +} + +#endif /* ZSTD_OPT_H_91842398743 */ diff --git a/thirdparty/zstd/compress/zstdmt_compress.c b/thirdparty/zstd/compress/zstdmt_compress.c new file mode 100644 index 0000000000..fc7f52a290 --- /dev/null +++ b/thirdparty/zstd/compress/zstdmt_compress.c @@ -0,0 +1,751 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + + +/* ====== Tuning parameters ====== */ +#define ZSTDMT_NBTHREADS_MAX 128 + + +/* ====== Compiler specifics ====== */ +#if defined(_MSC_VER) +# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ +#endif + + +/* ====== Dependencies ====== */ +#include <stdlib.h> /* malloc */ +#include <string.h> /* memcpy */ +#include "pool.h" /* threadpool */ +#include "threading.h" /* mutex */ +#include "zstd_internal.h" /* MIN, ERROR, ZSTD_*, ZSTD_highbit32 */ +#include "zstdmt_compress.h" + + +/* ====== Debug ====== */ +#if 0 + +# include <stdio.h> +# include <unistd.h> +# include <sys/times.h> + static unsigned g_debugLevel = 5; +# define DEBUGLOGRAW(l, ...) if (l<=g_debugLevel) { fprintf(stderr, __VA_ARGS__); } +# define DEBUGLOG(l, ...) if (l<=g_debugLevel) { fprintf(stderr, __FILE__ ": "); fprintf(stderr, __VA_ARGS__); fprintf(stderr, " \n"); } + +# define DEBUG_PRINTHEX(l,p,n) { \ + unsigned debug_u; \ + for (debug_u=0; debug_u<(n); debug_u++) \ + DEBUGLOGRAW(l, "%02X ", ((const unsigned char*)(p))[debug_u]); \ + DEBUGLOGRAW(l, " \n"); \ +} + +static unsigned long long GetCurrentClockTimeMicroseconds(void) +{ + static clock_t _ticksPerSecond = 0; + if (_ticksPerSecond <= 0) _ticksPerSecond = sysconf(_SC_CLK_TCK); + + { struct tms junk; clock_t newTicks = (clock_t) times(&junk); + return ((((unsigned long long)newTicks)*(1000000))/_ticksPerSecond); } +} + +#define MUTEX_WAIT_TIME_DLEVEL 5 +#define PTHREAD_MUTEX_LOCK(mutex) \ +if (g_debugLevel>=MUTEX_WAIT_TIME_DLEVEL) { \ + unsigned long long const beforeTime = GetCurrentClockTimeMicroseconds(); \ + pthread_mutex_lock(mutex); \ + { unsigned long long const afterTime = GetCurrentClockTimeMicroseconds(); \ + unsigned long long const elapsedTime = (afterTime-beforeTime); \ + if (elapsedTime > 1000) { /* or whatever threshold you like; I'm using 1 millisecond here */ \ + DEBUGLOG(MUTEX_WAIT_TIME_DLEVEL, "Thread took %llu microseconds to acquire mutex %s \n", \ + elapsedTime, #mutex); \ + } } \ +} else pthread_mutex_lock(mutex); + +#else + +# define DEBUGLOG(l, ...) {} /* disabled */ +# define PTHREAD_MUTEX_LOCK(m) pthread_mutex_lock(m) +# define DEBUG_PRINTHEX(l,p,n) {} + +#endif + + +/* ===== Buffer Pool ===== */ + +typedef struct buffer_s { + void* start; + size_t size; +} buffer_t; + +static const buffer_t g_nullBuffer = { NULL, 0 }; + +typedef struct ZSTDMT_bufferPool_s { + unsigned totalBuffers; + unsigned nbBuffers; + buffer_t bTable[1]; /* variable size */ +} ZSTDMT_bufferPool; + +static ZSTDMT_bufferPool* ZSTDMT_createBufferPool(unsigned nbThreads) +{ + unsigned const maxNbBuffers = 2*nbThreads + 2; + ZSTDMT_bufferPool* const bufPool = (ZSTDMT_bufferPool*)calloc(1, sizeof(ZSTDMT_bufferPool) + (maxNbBuffers-1) * sizeof(buffer_t)); + if (bufPool==NULL) return NULL; + bufPool->totalBuffers = maxNbBuffers; + bufPool->nbBuffers = 0; + return bufPool; +} + +static void ZSTDMT_freeBufferPool(ZSTDMT_bufferPool* bufPool) +{ + unsigned u; + if (!bufPool) return; /* compatibility with free on NULL */ + for (u=0; u<bufPool->totalBuffers; u++) + free(bufPool->bTable[u].start); + free(bufPool); +} + +/* assumption : invocation from main thread only ! */ +static buffer_t ZSTDMT_getBuffer(ZSTDMT_bufferPool* pool, size_t bSize) +{ + if (pool->nbBuffers) { /* try to use an existing buffer */ + buffer_t const buf = pool->bTable[--(pool->nbBuffers)]; + size_t const availBufferSize = buf.size; + if ((availBufferSize >= bSize) & (availBufferSize <= 10*bSize)) /* large enough, but not too much */ + return buf; + free(buf.start); /* size conditions not respected : scratch this buffer and create a new one */ + } + /* create new buffer */ + { buffer_t buffer; + void* const start = malloc(bSize); + if (start==NULL) bSize = 0; + buffer.start = start; /* note : start can be NULL if malloc fails ! */ + buffer.size = bSize; + return buffer; + } +} + +/* store buffer for later re-use, up to pool capacity */ +static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* pool, buffer_t buf) +{ + if (buf.start == NULL) return; /* release on NULL */ + if (pool->nbBuffers < pool->totalBuffers) { + pool->bTable[pool->nbBuffers++] = buf; /* store for later re-use */ + return; + } + /* Reached bufferPool capacity (should not happen) */ + free(buf.start); +} + + +/* ===== CCtx Pool ===== */ + +typedef struct { + unsigned totalCCtx; + unsigned availCCtx; + ZSTD_CCtx* cctx[1]; /* variable size */ +} ZSTDMT_CCtxPool; + +/* assumption : CCtxPool invocation only from main thread */ + +/* note : all CCtx borrowed from the pool should be released back to the pool _before_ freeing the pool */ +static void ZSTDMT_freeCCtxPool(ZSTDMT_CCtxPool* pool) +{ + unsigned u; + for (u=0; u<pool->totalCCtx; u++) + ZSTD_freeCCtx(pool->cctx[u]); /* note : compatible with free on NULL */ + free(pool); +} + +/* ZSTDMT_createCCtxPool() : + * implies nbThreads >= 1 , checked by caller ZSTDMT_createCCtx() */ +static ZSTDMT_CCtxPool* ZSTDMT_createCCtxPool(unsigned nbThreads) +{ + ZSTDMT_CCtxPool* const cctxPool = (ZSTDMT_CCtxPool*) calloc(1, sizeof(ZSTDMT_CCtxPool) + (nbThreads-1)*sizeof(ZSTD_CCtx*)); + if (!cctxPool) return NULL; + cctxPool->totalCCtx = nbThreads; + cctxPool->availCCtx = 1; /* at least one cctx for single-thread mode */ + cctxPool->cctx[0] = ZSTD_createCCtx(); + if (!cctxPool->cctx[0]) { ZSTDMT_freeCCtxPool(cctxPool); return NULL; } + DEBUGLOG(1, "cctxPool created, with %u threads", nbThreads); + return cctxPool; +} + +static ZSTD_CCtx* ZSTDMT_getCCtx(ZSTDMT_CCtxPool* pool) +{ + if (pool->availCCtx) { + pool->availCCtx--; + return pool->cctx[pool->availCCtx]; + } + return ZSTD_createCCtx(); /* note : can be NULL, when creation fails ! */ +} + +static void ZSTDMT_releaseCCtx(ZSTDMT_CCtxPool* pool, ZSTD_CCtx* cctx) +{ + if (cctx==NULL) return; /* compatibility with release on NULL */ + if (pool->availCCtx < pool->totalCCtx) + pool->cctx[pool->availCCtx++] = cctx; + else + /* pool overflow : should not happen, since totalCCtx==nbThreads */ + ZSTD_freeCCtx(cctx); +} + + +/* ===== Thread worker ===== */ + +typedef struct { + buffer_t buffer; + size_t filled; +} inBuff_t; + +typedef struct { + ZSTD_CCtx* cctx; + buffer_t src; + const void* srcStart; + size_t srcSize; + size_t dictSize; + buffer_t dstBuff; + size_t cSize; + size_t dstFlushed; + unsigned firstChunk; + unsigned lastChunk; + unsigned jobCompleted; + unsigned jobScanned; + pthread_mutex_t* jobCompleted_mutex; + pthread_cond_t* jobCompleted_cond; + ZSTD_parameters params; + ZSTD_CDict* cdict; + unsigned long long fullFrameSize; +} ZSTDMT_jobDescription; + +/* ZSTDMT_compressChunk() : POOL_function type */ +void ZSTDMT_compressChunk(void* jobDescription) +{ + ZSTDMT_jobDescription* const job = (ZSTDMT_jobDescription*)jobDescription; + const void* const src = (const char*)job->srcStart + job->dictSize; + buffer_t const dstBuff = job->dstBuff; + DEBUGLOG(3, "job (first:%u) (last:%u) : dictSize %u, srcSize %u", + job->firstChunk, job->lastChunk, (U32)job->dictSize, (U32)job->srcSize); + if (job->cdict) { /* should only happen for first segment */ + size_t const initError = ZSTD_compressBegin_usingCDict_advanced(job->cctx, job->cdict, job->params.fParams, job->fullFrameSize); + if (job->cdict) DEBUGLOG(3, "using CDict "); + if (ZSTD_isError(initError)) { job->cSize = initError; goto _endJob; } + } else { /* srcStart points at reloaded section */ + if (!job->firstChunk) job->params.fParams.contentSizeFlag = 0; /* ensure no srcSize control */ + { size_t const dictModeError = ZSTD_setCCtxParameter(job->cctx, ZSTD_p_forceRawDict, 1); /* Force loading dictionary in "content-only" mode (no header analysis) */ + size_t const initError = ZSTD_compressBegin_advanced(job->cctx, job->srcStart, job->dictSize, job->params, job->fullFrameSize); + if (ZSTD_isError(initError) || ZSTD_isError(dictModeError)) { job->cSize = initError; goto _endJob; } + ZSTD_setCCtxParameter(job->cctx, ZSTD_p_forceWindow, 1); + } } + if (!job->firstChunk) { /* flush and overwrite frame header when it's not first segment */ + size_t const hSize = ZSTD_compressContinue(job->cctx, dstBuff.start, dstBuff.size, src, 0); + if (ZSTD_isError(hSize)) { job->cSize = hSize; goto _endJob; } + ZSTD_invalidateRepCodes(job->cctx); + } + + DEBUGLOG(4, "Compressing : "); + DEBUG_PRINTHEX(4, job->srcStart, 12); + job->cSize = (job->lastChunk) ? + ZSTD_compressEnd (job->cctx, dstBuff.start, dstBuff.size, src, job->srcSize) : + ZSTD_compressContinue(job->cctx, dstBuff.start, dstBuff.size, src, job->srcSize); + DEBUGLOG(3, "compressed %u bytes into %u bytes (first:%u) (last:%u)", + (unsigned)job->srcSize, (unsigned)job->cSize, job->firstChunk, job->lastChunk); + DEBUGLOG(5, "dstBuff.size : %u ; => %s", (U32)dstBuff.size, ZSTD_getErrorName(job->cSize)); + +_endJob: + PTHREAD_MUTEX_LOCK(job->jobCompleted_mutex); + job->jobCompleted = 1; + job->jobScanned = 0; + pthread_cond_signal(job->jobCompleted_cond); + pthread_mutex_unlock(job->jobCompleted_mutex); +} + + +/* ------------------------------------------ */ +/* ===== Multi-threaded compression ===== */ +/* ------------------------------------------ */ + +struct ZSTDMT_CCtx_s { + POOL_ctx* factory; + ZSTDMT_bufferPool* buffPool; + ZSTDMT_CCtxPool* cctxPool; + pthread_mutex_t jobCompleted_mutex; + pthread_cond_t jobCompleted_cond; + size_t targetSectionSize; + size_t marginSize; + size_t inBuffSize; + size_t dictSize; + size_t targetDictSize; + inBuff_t inBuff; + ZSTD_parameters params; + XXH64_state_t xxhState; + unsigned nbThreads; + unsigned jobIDMask; + unsigned doneJobID; + unsigned nextJobID; + unsigned frameEnded; + unsigned allJobsCompleted; + unsigned overlapRLog; + unsigned long long frameContentSize; + size_t sectionSize; + ZSTD_CDict* cdict; + ZSTD_CStream* cstream; + ZSTDMT_jobDescription jobs[1]; /* variable size (must lies at the end) */ +}; + +ZSTDMT_CCtx *ZSTDMT_createCCtx(unsigned nbThreads) +{ + ZSTDMT_CCtx* cctx; + U32 const minNbJobs = nbThreads + 2; + U32 const nbJobsLog2 = ZSTD_highbit32(minNbJobs) + 1; + U32 const nbJobs = 1 << nbJobsLog2; + DEBUGLOG(5, "nbThreads : %u ; minNbJobs : %u ; nbJobsLog2 : %u ; nbJobs : %u \n", + nbThreads, minNbJobs, nbJobsLog2, nbJobs); + if ((nbThreads < 1) | (nbThreads > ZSTDMT_NBTHREADS_MAX)) return NULL; + cctx = (ZSTDMT_CCtx*) calloc(1, sizeof(ZSTDMT_CCtx) + nbJobs*sizeof(ZSTDMT_jobDescription)); + if (!cctx) return NULL; + cctx->nbThreads = nbThreads; + cctx->jobIDMask = nbJobs - 1; + cctx->allJobsCompleted = 1; + cctx->sectionSize = 0; + cctx->overlapRLog = 3; + cctx->factory = POOL_create(nbThreads, 1); + cctx->buffPool = ZSTDMT_createBufferPool(nbThreads); + cctx->cctxPool = ZSTDMT_createCCtxPool(nbThreads); + if (!cctx->factory | !cctx->buffPool | !cctx->cctxPool) { /* one object was not created */ + ZSTDMT_freeCCtx(cctx); + return NULL; + } + if (nbThreads==1) { + cctx->cstream = ZSTD_createCStream(); + if (!cctx->cstream) { + ZSTDMT_freeCCtx(cctx); return NULL; + } } + pthread_mutex_init(&cctx->jobCompleted_mutex, NULL); /* Todo : check init function return */ + pthread_cond_init(&cctx->jobCompleted_cond, NULL); + DEBUGLOG(4, "mt_cctx created, for %u threads \n", nbThreads); + return cctx; +} + +/* ZSTDMT_releaseAllJobResources() : + * Ensure all workers are killed first. */ +static void ZSTDMT_releaseAllJobResources(ZSTDMT_CCtx* mtctx) +{ + unsigned jobID; + for (jobID=0; jobID <= mtctx->jobIDMask; jobID++) { + ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->jobs[jobID].dstBuff); + mtctx->jobs[jobID].dstBuff = g_nullBuffer; + ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->jobs[jobID].src); + mtctx->jobs[jobID].src = g_nullBuffer; + ZSTDMT_releaseCCtx(mtctx->cctxPool, mtctx->jobs[jobID].cctx); + mtctx->jobs[jobID].cctx = NULL; + } + memset(mtctx->jobs, 0, (mtctx->jobIDMask+1)*sizeof(ZSTDMT_jobDescription)); + ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->inBuff.buffer); + mtctx->inBuff.buffer = g_nullBuffer; + mtctx->allJobsCompleted = 1; +} + +size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx) +{ + if (mtctx==NULL) return 0; /* compatible with free on NULL */ + POOL_free(mtctx->factory); + if (!mtctx->allJobsCompleted) ZSTDMT_releaseAllJobResources(mtctx); /* stop workers first */ + ZSTDMT_freeBufferPool(mtctx->buffPool); /* release job resources into pools first */ + ZSTDMT_freeCCtxPool(mtctx->cctxPool); + ZSTD_freeCDict(mtctx->cdict); + ZSTD_freeCStream(mtctx->cstream); + pthread_mutex_destroy(&mtctx->jobCompleted_mutex); + pthread_cond_destroy(&mtctx->jobCompleted_cond); + free(mtctx); + return 0; +} + +size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSDTMT_parameter parameter, unsigned value) +{ + switch(parameter) + { + case ZSTDMT_p_sectionSize : + mtctx->sectionSize = value; + return 0; + case ZSTDMT_p_overlapSectionLog : + DEBUGLOG(4, "ZSTDMT_p_overlapSectionLog : %u", value); + mtctx->overlapRLog = (value >= 9) ? 0 : 9 - value; + return 0; + default : + return ERROR(compressionParameter_unsupported); + } +} + + +/* ------------------------------------------ */ +/* ===== Multi-threaded compression ===== */ +/* ------------------------------------------ */ + +size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* mtctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + int compressionLevel) +{ + ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, 0); + U32 const overlapLog = (compressionLevel >= ZSTD_maxCLevel()) ? 0 : 3; + size_t const overlapSize = (size_t)1 << (params.cParams.windowLog - overlapLog); + size_t const chunkTargetSize = (size_t)1 << (params.cParams.windowLog + 2); + unsigned const nbChunksMax = (unsigned)(srcSize / chunkTargetSize) + 1; + unsigned nbChunks = MIN(nbChunksMax, mtctx->nbThreads); + size_t const proposedChunkSize = (srcSize + (nbChunks-1)) / nbChunks; + size_t const avgChunkSize = ((proposedChunkSize & 0x1FFFF) < 0xFFFF) ? proposedChunkSize + 0xFFFF : proposedChunkSize; /* avoid too small last block */ + size_t remainingSrcSize = srcSize; + const char* const srcStart = (const char*)src; + unsigned const compressWithinDst = (dstCapacity >= ZSTD_compressBound(srcSize)) ? nbChunks : (unsigned)(dstCapacity / ZSTD_compressBound(avgChunkSize)); /* presumes avgChunkSize >= 256 KB, which should be the case */ + size_t frameStartPos = 0, dstBufferPos = 0; + + DEBUGLOG(3, "windowLog : %2u => chunkTargetSize : %u bytes ", params.cParams.windowLog, (U32)chunkTargetSize); + DEBUGLOG(2, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize); + params.fParams.contentSizeFlag = 1; + + if (nbChunks==1) { /* fallback to single-thread mode */ + ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0]; + return ZSTD_compressCCtx(cctx, dst, dstCapacity, src, srcSize, compressionLevel); + } + + { unsigned u; + for (u=0; u<nbChunks; u++) { + size_t const chunkSize = MIN(remainingSrcSize, avgChunkSize); + size_t const dstBufferCapacity = ZSTD_compressBound(chunkSize); + buffer_t const dstAsBuffer = { (char*)dst + dstBufferPos, dstBufferCapacity }; + buffer_t const dstBuffer = u < compressWithinDst ? dstAsBuffer : ZSTDMT_getBuffer(mtctx->buffPool, dstBufferCapacity); + ZSTD_CCtx* const cctx = ZSTDMT_getCCtx(mtctx->cctxPool); + size_t dictSize = u ? overlapSize : 0; + + if ((cctx==NULL) || (dstBuffer.start==NULL)) { + mtctx->jobs[u].cSize = ERROR(memory_allocation); /* job result */ + mtctx->jobs[u].jobCompleted = 1; + nbChunks = u+1; + break; /* let's wait for previous jobs to complete, but don't start new ones */ + } + + mtctx->jobs[u].srcStart = srcStart + frameStartPos - dictSize; + mtctx->jobs[u].dictSize = dictSize; + mtctx->jobs[u].srcSize = chunkSize; + mtctx->jobs[u].fullFrameSize = srcSize; + mtctx->jobs[u].params = params; + mtctx->jobs[u].dstBuff = dstBuffer; + mtctx->jobs[u].cctx = cctx; + mtctx->jobs[u].firstChunk = (u==0); + mtctx->jobs[u].lastChunk = (u==nbChunks-1); + mtctx->jobs[u].jobCompleted = 0; + mtctx->jobs[u].jobCompleted_mutex = &mtctx->jobCompleted_mutex; + mtctx->jobs[u].jobCompleted_cond = &mtctx->jobCompleted_cond; + + DEBUGLOG(3, "posting job %u (%u bytes)", u, (U32)chunkSize); + DEBUG_PRINTHEX(3, mtctx->jobs[u].srcStart, 12); + POOL_add(mtctx->factory, ZSTDMT_compressChunk, &mtctx->jobs[u]); + + frameStartPos += chunkSize; + dstBufferPos += dstBufferCapacity; + remainingSrcSize -= chunkSize; + } } + /* note : since nbChunks <= nbThreads, all jobs should be running immediately in parallel */ + + { unsigned chunkID; + size_t error = 0, dstPos = 0; + for (chunkID=0; chunkID<nbChunks; chunkID++) { + DEBUGLOG(3, "waiting for chunk %u ", chunkID); + PTHREAD_MUTEX_LOCK(&mtctx->jobCompleted_mutex); + while (mtctx->jobs[chunkID].jobCompleted==0) { + DEBUGLOG(4, "waiting for jobCompleted signal from chunk %u", chunkID); + pthread_cond_wait(&mtctx->jobCompleted_cond, &mtctx->jobCompleted_mutex); + } + pthread_mutex_unlock(&mtctx->jobCompleted_mutex); + DEBUGLOG(3, "ready to write chunk %u ", chunkID); + + ZSTDMT_releaseCCtx(mtctx->cctxPool, mtctx->jobs[chunkID].cctx); + mtctx->jobs[chunkID].cctx = NULL; + mtctx->jobs[chunkID].srcStart = NULL; + { size_t const cSize = mtctx->jobs[chunkID].cSize; + if (ZSTD_isError(cSize)) error = cSize; + if ((!error) && (dstPos + cSize > dstCapacity)) error = ERROR(dstSize_tooSmall); + if (chunkID) { /* note : chunk 0 is already written directly into dst */ + if (!error) + memmove((char*)dst + dstPos, mtctx->jobs[chunkID].dstBuff.start, cSize); /* may overlap if chunk decompressed within dst */ + if (chunkID >= compressWithinDst) /* otherwise, it decompresses within dst */ + ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->jobs[chunkID].dstBuff); + mtctx->jobs[chunkID].dstBuff = g_nullBuffer; + } + dstPos += cSize ; + } + } + if (!error) DEBUGLOG(3, "compressed size : %u ", (U32)dstPos); + return error ? error : dstPos; + } + +} + + +/* ====================================== */ +/* ======= Streaming API ======= */ +/* ====================================== */ + +static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* zcs) { + while (zcs->doneJobID < zcs->nextJobID) { + unsigned const jobID = zcs->doneJobID & zcs->jobIDMask; + PTHREAD_MUTEX_LOCK(&zcs->jobCompleted_mutex); + while (zcs->jobs[jobID].jobCompleted==0) { + DEBUGLOG(4, "waiting for jobCompleted signal from chunk %u", zcs->doneJobID); /* we want to block when waiting for data to flush */ + pthread_cond_wait(&zcs->jobCompleted_cond, &zcs->jobCompleted_mutex); + } + pthread_mutex_unlock(&zcs->jobCompleted_mutex); + zcs->doneJobID++; + } +} + + +static size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, + const void* dict, size_t dictSize, unsigned updateDict, + ZSTD_parameters params, unsigned long long pledgedSrcSize) +{ + ZSTD_customMem const cmem = { NULL, NULL, NULL }; + DEBUGLOG(3, "Started new compression, with windowLog : %u", params.cParams.windowLog); + if (zcs->nbThreads==1) return ZSTD_initCStream_advanced(zcs->cstream, dict, dictSize, params, pledgedSrcSize); + if (zcs->allJobsCompleted == 0) { /* previous job not correctly finished */ + ZSTDMT_waitForAllJobsCompleted(zcs); + ZSTDMT_releaseAllJobResources(zcs); + zcs->allJobsCompleted = 1; + } + zcs->params = params; + if (updateDict) { + ZSTD_freeCDict(zcs->cdict); zcs->cdict = NULL; + if (dict && dictSize) { + zcs->cdict = ZSTD_createCDict_advanced(dict, dictSize, 0, params.cParams, cmem); + if (zcs->cdict == NULL) return ERROR(memory_allocation); + } } + zcs->frameContentSize = pledgedSrcSize; + zcs->targetDictSize = (zcs->overlapRLog>=9) ? 0 : (size_t)1 << (zcs->params.cParams.windowLog - zcs->overlapRLog); + DEBUGLOG(4, "overlapRLog : %u ", zcs->overlapRLog); + DEBUGLOG(3, "overlap Size : %u KB", (U32)(zcs->targetDictSize>>10)); + zcs->targetSectionSize = zcs->sectionSize ? zcs->sectionSize : (size_t)1 << (zcs->params.cParams.windowLog + 2); + zcs->targetSectionSize = MAX(ZSTDMT_SECTION_SIZE_MIN, zcs->targetSectionSize); + zcs->targetSectionSize = MAX(zcs->targetDictSize, zcs->targetSectionSize); + DEBUGLOG(3, "Section Size : %u KB", (U32)(zcs->targetSectionSize>>10)); + zcs->marginSize = zcs->targetSectionSize >> 2; + zcs->inBuffSize = zcs->targetDictSize + zcs->targetSectionSize + zcs->marginSize; + zcs->inBuff.buffer = ZSTDMT_getBuffer(zcs->buffPool, zcs->inBuffSize); + if (zcs->inBuff.buffer.start == NULL) return ERROR(memory_allocation); + zcs->inBuff.filled = 0; + zcs->dictSize = 0; + zcs->doneJobID = 0; + zcs->nextJobID = 0; + zcs->frameEnded = 0; + zcs->allJobsCompleted = 0; + if (params.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0); + return 0; +} + +size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* zcs, + const void* dict, size_t dictSize, + ZSTD_parameters params, unsigned long long pledgedSrcSize) +{ + return ZSTDMT_initCStream_internal(zcs, dict, dictSize, 1, params, pledgedSrcSize); +} + +/* ZSTDMT_resetCStream() : + * pledgedSrcSize is optional and can be zero == unknown */ +size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* zcs, unsigned long long pledgedSrcSize) +{ + if (zcs->nbThreads==1) return ZSTD_resetCStream(zcs->cstream, pledgedSrcSize); + return ZSTDMT_initCStream_internal(zcs, NULL, 0, 0, zcs->params, pledgedSrcSize); +} + +size_t ZSTDMT_initCStream(ZSTDMT_CCtx* zcs, int compressionLevel) { + ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, 0); + return ZSTDMT_initCStream_internal(zcs, NULL, 0, 1, params, 0); +} + + +static size_t ZSTDMT_createCompressionJob(ZSTDMT_CCtx* zcs, size_t srcSize, unsigned endFrame) +{ + size_t const dstBufferCapacity = ZSTD_compressBound(srcSize); + buffer_t const dstBuffer = ZSTDMT_getBuffer(zcs->buffPool, dstBufferCapacity); + ZSTD_CCtx* const cctx = ZSTDMT_getCCtx(zcs->cctxPool); + unsigned const jobID = zcs->nextJobID & zcs->jobIDMask; + + if ((cctx==NULL) || (dstBuffer.start==NULL)) { + zcs->jobs[jobID].jobCompleted = 1; + zcs->nextJobID++; + ZSTDMT_waitForAllJobsCompleted(zcs); + ZSTDMT_releaseAllJobResources(zcs); + return ERROR(memory_allocation); + } + + DEBUGLOG(4, "preparing job %u to compress %u bytes with %u preload ", zcs->nextJobID, (U32)srcSize, (U32)zcs->dictSize); + zcs->jobs[jobID].src = zcs->inBuff.buffer; + zcs->jobs[jobID].srcStart = zcs->inBuff.buffer.start; + zcs->jobs[jobID].srcSize = srcSize; + zcs->jobs[jobID].dictSize = zcs->dictSize; /* note : zcs->inBuff.filled is presumed >= srcSize + dictSize */ + zcs->jobs[jobID].params = zcs->params; + if (zcs->nextJobID) zcs->jobs[jobID].params.fParams.checksumFlag = 0; /* do not calculate checksum within sections, just keep it in header for first section */ + zcs->jobs[jobID].cdict = zcs->nextJobID==0 ? zcs->cdict : NULL; + zcs->jobs[jobID].fullFrameSize = zcs->frameContentSize; + zcs->jobs[jobID].dstBuff = dstBuffer; + zcs->jobs[jobID].cctx = cctx; + zcs->jobs[jobID].firstChunk = (zcs->nextJobID==0); + zcs->jobs[jobID].lastChunk = endFrame; + zcs->jobs[jobID].jobCompleted = 0; + zcs->jobs[jobID].dstFlushed = 0; + zcs->jobs[jobID].jobCompleted_mutex = &zcs->jobCompleted_mutex; + zcs->jobs[jobID].jobCompleted_cond = &zcs->jobCompleted_cond; + + /* get a new buffer for next input */ + if (!endFrame) { + size_t const newDictSize = MIN(srcSize + zcs->dictSize, zcs->targetDictSize); + zcs->inBuff.buffer = ZSTDMT_getBuffer(zcs->buffPool, zcs->inBuffSize); + if (zcs->inBuff.buffer.start == NULL) { /* not enough memory to allocate next input buffer */ + zcs->jobs[jobID].jobCompleted = 1; + zcs->nextJobID++; + ZSTDMT_waitForAllJobsCompleted(zcs); + ZSTDMT_releaseAllJobResources(zcs); + return ERROR(memory_allocation); + } + DEBUGLOG(5, "inBuff filled to %u", (U32)zcs->inBuff.filled); + zcs->inBuff.filled -= srcSize + zcs->dictSize - newDictSize; + DEBUGLOG(5, "new job : filled to %u, with %u dict and %u src", (U32)zcs->inBuff.filled, (U32)newDictSize, (U32)(zcs->inBuff.filled - newDictSize)); + memmove(zcs->inBuff.buffer.start, (const char*)zcs->jobs[jobID].srcStart + zcs->dictSize + srcSize - newDictSize, zcs->inBuff.filled); + DEBUGLOG(5, "new inBuff pre-filled"); + zcs->dictSize = newDictSize; + } else { + zcs->inBuff.buffer = g_nullBuffer; + zcs->inBuff.filled = 0; + zcs->dictSize = 0; + zcs->frameEnded = 1; + if (zcs->nextJobID == 0) + zcs->params.fParams.checksumFlag = 0; /* single chunk : checksum is calculated directly within worker thread */ + } + + DEBUGLOG(3, "posting job %u : %u bytes (end:%u) (note : doneJob = %u=>%u)", zcs->nextJobID, (U32)zcs->jobs[jobID].srcSize, zcs->jobs[jobID].lastChunk, zcs->doneJobID, zcs->doneJobID & zcs->jobIDMask); + POOL_add(zcs->factory, ZSTDMT_compressChunk, &zcs->jobs[jobID]); /* this call is blocking when thread worker pool is exhausted */ + zcs->nextJobID++; + return 0; +} + + +/* ZSTDMT_flushNextJob() : + * output : will be updated with amount of data flushed . + * blockToFlush : if >0, the function will block and wait if there is no data available to flush . + * @return : amount of data remaining within internal buffer, 1 if unknown but > 0, 0 if no more, or an error code */ +static size_t ZSTDMT_flushNextJob(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output, unsigned blockToFlush) +{ + unsigned const wJobID = zcs->doneJobID & zcs->jobIDMask; + if (zcs->doneJobID == zcs->nextJobID) return 0; /* all flushed ! */ + PTHREAD_MUTEX_LOCK(&zcs->jobCompleted_mutex); + while (zcs->jobs[wJobID].jobCompleted==0) { + DEBUGLOG(5, "waiting for jobCompleted signal from job %u", zcs->doneJobID); + if (!blockToFlush) { pthread_mutex_unlock(&zcs->jobCompleted_mutex); return 0; } /* nothing ready to be flushed => skip */ + pthread_cond_wait(&zcs->jobCompleted_cond, &zcs->jobCompleted_mutex); /* block when nothing available to flush */ + } + pthread_mutex_unlock(&zcs->jobCompleted_mutex); + /* compression job completed : output can be flushed */ + { ZSTDMT_jobDescription job = zcs->jobs[wJobID]; + if (!job.jobScanned) { + if (ZSTD_isError(job.cSize)) { + DEBUGLOG(5, "compression error detected "); + ZSTDMT_waitForAllJobsCompleted(zcs); + ZSTDMT_releaseAllJobResources(zcs); + return job.cSize; + } + ZSTDMT_releaseCCtx(zcs->cctxPool, job.cctx); + zcs->jobs[wJobID].cctx = NULL; + DEBUGLOG(5, "zcs->params.fParams.checksumFlag : %u ", zcs->params.fParams.checksumFlag); + if (zcs->params.fParams.checksumFlag) { + XXH64_update(&zcs->xxhState, (const char*)job.srcStart + job.dictSize, job.srcSize); + if (zcs->frameEnded && (zcs->doneJobID+1 == zcs->nextJobID)) { /* write checksum at end of last section */ + U32 const checksum = (U32)XXH64_digest(&zcs->xxhState); + DEBUGLOG(4, "writing checksum : %08X \n", checksum); + MEM_writeLE32((char*)job.dstBuff.start + job.cSize, checksum); + job.cSize += 4; + zcs->jobs[wJobID].cSize += 4; + } } + ZSTDMT_releaseBuffer(zcs->buffPool, job.src); + zcs->jobs[wJobID].srcStart = NULL; + zcs->jobs[wJobID].src = g_nullBuffer; + zcs->jobs[wJobID].jobScanned = 1; + } + { size_t const toWrite = MIN(job.cSize - job.dstFlushed, output->size - output->pos); + DEBUGLOG(4, "Flushing %u bytes from job %u ", (U32)toWrite, zcs->doneJobID); + memcpy((char*)output->dst + output->pos, (const char*)job.dstBuff.start + job.dstFlushed, toWrite); + output->pos += toWrite; + job.dstFlushed += toWrite; + } + if (job.dstFlushed == job.cSize) { /* output buffer fully flushed => move to next one */ + ZSTDMT_releaseBuffer(zcs->buffPool, job.dstBuff); + zcs->jobs[wJobID].dstBuff = g_nullBuffer; + zcs->jobs[wJobID].jobCompleted = 0; + zcs->doneJobID++; + } else { + zcs->jobs[wJobID].dstFlushed = job.dstFlushed; + } + /* return value : how many bytes left in buffer ; fake it to 1 if unknown but >0 */ + if (job.cSize > job.dstFlushed) return (job.cSize - job.dstFlushed); + if (zcs->doneJobID < zcs->nextJobID) return 1; /* still some buffer to flush */ + zcs->allJobsCompleted = zcs->frameEnded; /* frame completed and entirely flushed */ + return 0; /* everything flushed */ +} } + + +size_t ZSTDMT_compressStream(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input) +{ + size_t const newJobThreshold = zcs->dictSize + zcs->targetSectionSize + zcs->marginSize; + if (zcs->frameEnded) return ERROR(stage_wrong); /* current frame being ended. Only flush is allowed. Restart with init */ + if (zcs->nbThreads==1) return ZSTD_compressStream(zcs->cstream, output, input); + + /* fill input buffer */ + { size_t const toLoad = MIN(input->size - input->pos, zcs->inBuffSize - zcs->inBuff.filled); + memcpy((char*)zcs->inBuff.buffer.start + zcs->inBuff.filled, input->src, toLoad); + input->pos += toLoad; + zcs->inBuff.filled += toLoad; + } + + if ( (zcs->inBuff.filled >= newJobThreshold) /* filled enough : let's compress */ + && (zcs->nextJobID <= zcs->doneJobID + zcs->jobIDMask) ) { /* avoid overwriting job round buffer */ + CHECK_F( ZSTDMT_createCompressionJob(zcs, zcs->targetSectionSize, 0) ); + } + + /* check for data to flush */ + CHECK_F( ZSTDMT_flushNextJob(zcs, output, (zcs->inBuff.filled == zcs->inBuffSize)) ); /* block if it wasn't possible to create new job due to saturation */ + + /* recommended next input size : fill current input buffer */ + return zcs->inBuffSize - zcs->inBuff.filled; /* note : could be zero when input buffer is fully filled and no more availability to create new job */ +} + + +static size_t ZSTDMT_flushStream_internal(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output, unsigned endFrame) +{ + size_t const srcSize = zcs->inBuff.filled - zcs->dictSize; + + if (srcSize) DEBUGLOG(4, "flushing : %u bytes left to compress", (U32)srcSize); + if ( ((srcSize > 0) || (endFrame && !zcs->frameEnded)) + && (zcs->nextJobID <= zcs->doneJobID + zcs->jobIDMask) ) { + CHECK_F( ZSTDMT_createCompressionJob(zcs, srcSize, endFrame) ); + } + + /* check if there is any data available to flush */ + DEBUGLOG(5, "zcs->doneJobID : %u ; zcs->nextJobID : %u ", zcs->doneJobID, zcs->nextJobID); + return ZSTDMT_flushNextJob(zcs, output, 1); +} + + +size_t ZSTDMT_flushStream(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output) +{ + if (zcs->nbThreads==1) return ZSTD_flushStream(zcs->cstream, output); + return ZSTDMT_flushStream_internal(zcs, output, 0); +} + +size_t ZSTDMT_endStream(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output) +{ + if (zcs->nbThreads==1) return ZSTD_endStream(zcs->cstream, output); + return ZSTDMT_flushStream_internal(zcs, output, 1); +} diff --git a/thirdparty/zstd/compress/zstdmt_compress.h b/thirdparty/zstd/compress/zstdmt_compress.h new file mode 100644 index 0000000000..27f78ee031 --- /dev/null +++ b/thirdparty/zstd/compress/zstdmt_compress.h @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + + #ifndef ZSTDMT_COMPRESS_H + #define ZSTDMT_COMPRESS_H + + #if defined (__cplusplus) + extern "C" { + #endif + + +/* Note : All prototypes defined in this file shall be considered experimental. + * There is no guarantee of API continuity (yet) on any of these prototypes */ + +/* === Dependencies === */ +#include <stddef.h> /* size_t */ +#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters */ +#include "zstd.h" /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTDLIB_API */ + + +/* === Simple one-pass functions === */ + +typedef struct ZSTDMT_CCtx_s ZSTDMT_CCtx; +ZSTDLIB_API ZSTDMT_CCtx* ZSTDMT_createCCtx(unsigned nbThreads); +ZSTDLIB_API size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* cctx); + +ZSTDLIB_API size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + int compressionLevel); + + +/* === Streaming functions === */ + +ZSTDLIB_API size_t ZSTDMT_initCStream(ZSTDMT_CCtx* mtctx, int compressionLevel); +ZSTDLIB_API size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* mtctx, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be zero == unknown */ + +ZSTDLIB_API size_t ZSTDMT_compressStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input); + +ZSTDLIB_API size_t ZSTDMT_flushStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output); /**< @return : 0 == all flushed; >0 : still some data to be flushed; or an error code (ZSTD_isError()) */ +ZSTDLIB_API size_t ZSTDMT_endStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output); /**< @return : 0 == all flushed; >0 : still some data to be flushed; or an error code (ZSTD_isError()) */ + + +/* === Advanced functions and parameters === */ + +#ifndef ZSTDMT_SECTION_SIZE_MIN +# define ZSTDMT_SECTION_SIZE_MIN (1U << 20) /* 1 MB - Minimum size of each compression job */ +#endif + +ZSTDLIB_API size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* mtctx, const void* dict, size_t dictSize, /**< dict can be released after init, a local copy is preserved within zcs */ + ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be zero == unknown */ + +/* ZSDTMT_parameter : + * List of parameters that can be set using ZSTDMT_setMTCtxParameter() */ +typedef enum { + ZSTDMT_p_sectionSize, /* size of input "section". Each section is compressed in parallel. 0 means default, which is dynamically determined within compression functions */ + ZSTDMT_p_overlapSectionLog /* Log of overlapped section; 0 == no overlap, 6(default) == use 1/8th of window, >=9 == use full window */ +} ZSDTMT_parameter; + +/* ZSTDMT_setMTCtxParameter() : + * allow setting individual parameters, one at a time, among a list of enums defined in ZSTDMT_parameter. + * The function must be called typically after ZSTD_createCCtx(). + * Parameters not explicitly reset by ZSTDMT_init*() remain the same in consecutive compression sessions. + * @return : 0, or an error code (which can be tested using ZSTD_isError()) */ +ZSTDLIB_API size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSDTMT_parameter parameter, unsigned value); + + +#if defined (__cplusplus) +} +#endif + +#endif /* ZSTDMT_COMPRESS_H */ diff --git a/thirdparty/zstd/decompress/huf_decompress.c b/thirdparty/zstd/decompress/huf_decompress.c new file mode 100644 index 0000000000..ea35c36201 --- /dev/null +++ b/thirdparty/zstd/decompress/huf_decompress.c @@ -0,0 +1,888 @@ +/* ****************************************************************** + Huffman decoder, part of New Generation Entropy library + Copyright (C) 2013-2016, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy + - Public forum : https://groups.google.com/forum/#!forum/lz4c +****************************************************************** */ + +/* ************************************************************** +* Compiler specifics +****************************************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# define FORCE_INLINE static __forceinline +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +#else +# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +# else +# define FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +#endif + + +/* ************************************************************** +* Dependencies +****************************************************************/ +#include <string.h> /* memcpy, memset */ +#include "bitstream.h" /* BIT_* */ +#include "fse.h" /* header compression */ +#define HUF_STATIC_LINKING_ONLY +#include "huf.h" + + +/* ************************************************************** +* Error Management +****************************************************************/ +#define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ + + +/*-***************************/ +/* generic DTableDesc */ +/*-***************************/ + +typedef struct { BYTE maxTableLog; BYTE tableType; BYTE tableLog; BYTE reserved; } DTableDesc; + +static DTableDesc HUF_getDTableDesc(const HUF_DTable* table) +{ + DTableDesc dtd; + memcpy(&dtd, table, sizeof(dtd)); + return dtd; +} + + +/*-***************************/ +/* single-symbol decoding */ +/*-***************************/ + +typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX2; /* single-symbol decoding */ + +size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize) +{ + BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1]; + U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1]; /* large enough for values from 0 to 16 */ + U32 tableLog = 0; + U32 nbSymbols = 0; + size_t iSize; + void* const dtPtr = DTable + 1; + HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr; + + HUF_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable)); + /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */ + + iSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize); + if (HUF_isError(iSize)) return iSize; + + /* Table header */ + { DTableDesc dtd = HUF_getDTableDesc(DTable); + if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */ + dtd.tableType = 0; + dtd.tableLog = (BYTE)tableLog; + memcpy(DTable, &dtd, sizeof(dtd)); + } + + /* Calculate starting value for each rank */ + { U32 n, nextRankStart = 0; + for (n=1; n<tableLog+1; n++) { + U32 const current = nextRankStart; + nextRankStart += (rankVal[n] << (n-1)); + rankVal[n] = current; + } } + + /* fill DTable */ + { U32 n; + for (n=0; n<nbSymbols; n++) { + U32 const w = huffWeight[n]; + U32 const length = (1 << w) >> 1; + U32 u; + HUF_DEltX2 D; + D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w); + for (u = rankVal[w]; u < rankVal[w] + length; u++) + dt[u] = D; + rankVal[w] += length; + } } + + return iSize; +} + + +static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog) +{ + size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */ + BYTE const c = dt[val].byte; + BIT_skipBits(Dstream, dt[val].nbBits); + return c; +} + +#define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \ + *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog) + +#define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \ + if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \ + HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) + +#define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \ + if (MEM_64bits()) \ + HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) + +FORCE_INLINE size_t HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX2* const dt, const U32 dtLog) +{ + BYTE* const pStart = p; + + /* up to 4 symbols at a time */ + while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4)) { + HUF_DECODE_SYMBOLX2_2(p, bitDPtr); + HUF_DECODE_SYMBOLX2_1(p, bitDPtr); + HUF_DECODE_SYMBOLX2_2(p, bitDPtr); + HUF_DECODE_SYMBOLX2_0(p, bitDPtr); + } + + /* closer to the end */ + while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd)) + HUF_DECODE_SYMBOLX2_0(p, bitDPtr); + + /* no more data to retrieve from bitstream, hence no need to reload */ + while (p < pEnd) + HUF_DECODE_SYMBOLX2_0(p, bitDPtr); + + return pEnd-pStart; +} + +static size_t HUF_decompress1X2_usingDTable_internal( + void* dst, size_t dstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + BYTE* op = (BYTE*)dst; + BYTE* const oend = op + dstSize; + const void* dtPtr = DTable + 1; + const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr; + BIT_DStream_t bitD; + DTableDesc const dtd = HUF_getDTableDesc(DTable); + U32 const dtLog = dtd.tableLog; + + { size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); + if (HUF_isError(errorCode)) return errorCode; } + + HUF_decodeStreamX2(op, &bitD, oend, dt, dtLog); + + /* check */ + if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected); + + return dstSize; +} + +size_t HUF_decompress1X2_usingDTable( + void* dst, size_t dstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + DTableDesc dtd = HUF_getDTableDesc(DTable); + if (dtd.tableType != 0) return ERROR(GENERIC); + return HUF_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable); +} + +size_t HUF_decompress1X2_DCtx (HUF_DTable* DCtx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + const BYTE* ip = (const BYTE*) cSrc; + + size_t const hSize = HUF_readDTableX2 (DCtx, cSrc, cSrcSize); + if (HUF_isError(hSize)) return hSize; + if (hSize >= cSrcSize) return ERROR(srcSize_wrong); + ip += hSize; cSrcSize -= hSize; + + return HUF_decompress1X2_usingDTable_internal (dst, dstSize, ip, cSrcSize, DCtx); +} + +size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX); + return HUF_decompress1X2_DCtx (DTable, dst, dstSize, cSrc, cSrcSize); +} + + +static size_t HUF_decompress4X2_usingDTable_internal( + void* dst, size_t dstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + /* Check */ + if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ + + { const BYTE* const istart = (const BYTE*) cSrc; + BYTE* const ostart = (BYTE*) dst; + BYTE* const oend = ostart + dstSize; + const void* const dtPtr = DTable + 1; + const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr; + + /* Init */ + BIT_DStream_t bitD1; + BIT_DStream_t bitD2; + BIT_DStream_t bitD3; + BIT_DStream_t bitD4; + size_t const length1 = MEM_readLE16(istart); + size_t const length2 = MEM_readLE16(istart+2); + size_t const length3 = MEM_readLE16(istart+4); + size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6); + const BYTE* const istart1 = istart + 6; /* jumpTable */ + const BYTE* const istart2 = istart1 + length1; + const BYTE* const istart3 = istart2 + length2; + const BYTE* const istart4 = istart3 + length3; + const size_t segmentSize = (dstSize+3) / 4; + BYTE* const opStart2 = ostart + segmentSize; + BYTE* const opStart3 = opStart2 + segmentSize; + BYTE* const opStart4 = opStart3 + segmentSize; + BYTE* op1 = ostart; + BYTE* op2 = opStart2; + BYTE* op3 = opStart3; + BYTE* op4 = opStart4; + U32 endSignal; + DTableDesc const dtd = HUF_getDTableDesc(DTable); + U32 const dtLog = dtd.tableLog; + + if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */ + { size_t const errorCode = BIT_initDStream(&bitD1, istart1, length1); + if (HUF_isError(errorCode)) return errorCode; } + { size_t const errorCode = BIT_initDStream(&bitD2, istart2, length2); + if (HUF_isError(errorCode)) return errorCode; } + { size_t const errorCode = BIT_initDStream(&bitD3, istart3, length3); + if (HUF_isError(errorCode)) return errorCode; } + { size_t const errorCode = BIT_initDStream(&bitD4, istart4, length4); + if (HUF_isError(errorCode)) return errorCode; } + + /* 16-32 symbols per loop (4-8 symbols per stream) */ + endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); + for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; ) { + HUF_DECODE_SYMBOLX2_2(op1, &bitD1); + HUF_DECODE_SYMBOLX2_2(op2, &bitD2); + HUF_DECODE_SYMBOLX2_2(op3, &bitD3); + HUF_DECODE_SYMBOLX2_2(op4, &bitD4); + HUF_DECODE_SYMBOLX2_1(op1, &bitD1); + HUF_DECODE_SYMBOLX2_1(op2, &bitD2); + HUF_DECODE_SYMBOLX2_1(op3, &bitD3); + HUF_DECODE_SYMBOLX2_1(op4, &bitD4); + HUF_DECODE_SYMBOLX2_2(op1, &bitD1); + HUF_DECODE_SYMBOLX2_2(op2, &bitD2); + HUF_DECODE_SYMBOLX2_2(op3, &bitD3); + HUF_DECODE_SYMBOLX2_2(op4, &bitD4); + HUF_DECODE_SYMBOLX2_0(op1, &bitD1); + HUF_DECODE_SYMBOLX2_0(op2, &bitD2); + HUF_DECODE_SYMBOLX2_0(op3, &bitD3); + HUF_DECODE_SYMBOLX2_0(op4, &bitD4); + endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); + } + + /* check corruption */ + if (op1 > opStart2) return ERROR(corruption_detected); + if (op2 > opStart3) return ERROR(corruption_detected); + if (op3 > opStart4) return ERROR(corruption_detected); + /* note : op4 supposed already verified within main loop */ + + /* finish bitStreams one by one */ + HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog); + HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog); + HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog); + HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog); + + /* check */ + endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); + if (!endSignal) return ERROR(corruption_detected); + + /* decoded size */ + return dstSize; + } +} + + +size_t HUF_decompress4X2_usingDTable( + void* dst, size_t dstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + DTableDesc dtd = HUF_getDTableDesc(DTable); + if (dtd.tableType != 0) return ERROR(GENERIC); + return HUF_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable); +} + + +size_t HUF_decompress4X2_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + const BYTE* ip = (const BYTE*) cSrc; + + size_t const hSize = HUF_readDTableX2 (dctx, cSrc, cSrcSize); + if (HUF_isError(hSize)) return hSize; + if (hSize >= cSrcSize) return ERROR(srcSize_wrong); + ip += hSize; cSrcSize -= hSize; + + return HUF_decompress4X2_usingDTable_internal (dst, dstSize, ip, cSrcSize, dctx); +} + +size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX); + return HUF_decompress4X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize); +} + + +/* *************************/ +/* double-symbols decoding */ +/* *************************/ +typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4; /* double-symbols decoding */ + +typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t; + +/* HUF_fillDTableX4Level2() : + * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */ +static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 consumed, + const U32* rankValOrigin, const int minWeight, + const sortedSymbol_t* sortedSymbols, const U32 sortedListSize, + U32 nbBitsBaseline, U16 baseSeq) +{ + HUF_DEltX4 DElt; + U32 rankVal[HUF_TABLELOG_MAX + 1]; + + /* get pre-calculated rankVal */ + memcpy(rankVal, rankValOrigin, sizeof(rankVal)); + + /* fill skipped values */ + if (minWeight>1) { + U32 i, skipSize = rankVal[minWeight]; + MEM_writeLE16(&(DElt.sequence), baseSeq); + DElt.nbBits = (BYTE)(consumed); + DElt.length = 1; + for (i = 0; i < skipSize; i++) + DTable[i] = DElt; + } + + /* fill DTable */ + { U32 s; for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */ + const U32 symbol = sortedSymbols[s].symbol; + const U32 weight = sortedSymbols[s].weight; + const U32 nbBits = nbBitsBaseline - weight; + const U32 length = 1 << (sizeLog-nbBits); + const U32 start = rankVal[weight]; + U32 i = start; + const U32 end = start + length; + + MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8))); + DElt.nbBits = (BYTE)(nbBits + consumed); + DElt.length = 2; + do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */ + + rankVal[weight] += length; + } } +} + +typedef U32 rankVal_t[HUF_TABLELOG_MAX][HUF_TABLELOG_MAX + 1]; + +static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog, + const sortedSymbol_t* sortedList, const U32 sortedListSize, + const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight, + const U32 nbBitsBaseline) +{ + U32 rankVal[HUF_TABLELOG_MAX + 1]; + const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */ + const U32 minBits = nbBitsBaseline - maxWeight; + U32 s; + + memcpy(rankVal, rankValOrigin, sizeof(rankVal)); + + /* fill DTable */ + for (s=0; s<sortedListSize; s++) { + const U16 symbol = sortedList[s].symbol; + const U32 weight = sortedList[s].weight; + const U32 nbBits = nbBitsBaseline - weight; + const U32 start = rankVal[weight]; + const U32 length = 1 << (targetLog-nbBits); + + if (targetLog-nbBits >= minBits) { /* enough room for a second symbol */ + U32 sortedRank; + int minWeight = nbBits + scaleLog; + if (minWeight < 1) minWeight = 1; + sortedRank = rankStart[minWeight]; + HUF_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits, + rankValOrigin[nbBits], minWeight, + sortedList+sortedRank, sortedListSize-sortedRank, + nbBitsBaseline, symbol); + } else { + HUF_DEltX4 DElt; + MEM_writeLE16(&(DElt.sequence), symbol); + DElt.nbBits = (BYTE)(nbBits); + DElt.length = 1; + { U32 const end = start + length; + U32 u; + for (u = start; u < end; u++) DTable[u] = DElt; + } } + rankVal[weight] += length; + } +} + +size_t HUF_readDTableX4 (HUF_DTable* DTable, const void* src, size_t srcSize) +{ + BYTE weightList[HUF_SYMBOLVALUE_MAX + 1]; + sortedSymbol_t sortedSymbol[HUF_SYMBOLVALUE_MAX + 1]; + U32 rankStats[HUF_TABLELOG_MAX + 1] = { 0 }; + U32 rankStart0[HUF_TABLELOG_MAX + 2] = { 0 }; + U32* const rankStart = rankStart0+1; + rankVal_t rankVal; + U32 tableLog, maxW, sizeOfSort, nbSymbols; + DTableDesc dtd = HUF_getDTableDesc(DTable); + U32 const maxTableLog = dtd.maxTableLog; + size_t iSize; + void* dtPtr = DTable+1; /* force compiler to avoid strict-aliasing */ + HUF_DEltX4* const dt = (HUF_DEltX4*)dtPtr; + + HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */ + if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge); + /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */ + + iSize = HUF_readStats(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize); + if (HUF_isError(iSize)) return iSize; + + /* check result */ + if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */ + + /* find maxWeight */ + for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */ + + /* Get start index of each weight */ + { U32 w, nextRankStart = 0; + for (w=1; w<maxW+1; w++) { + U32 current = nextRankStart; + nextRankStart += rankStats[w]; + rankStart[w] = current; + } + rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/ + sizeOfSort = nextRankStart; + } + + /* sort symbols by weight */ + { U32 s; + for (s=0; s<nbSymbols; s++) { + U32 const w = weightList[s]; + U32 const r = rankStart[w]++; + sortedSymbol[r].symbol = (BYTE)s; + sortedSymbol[r].weight = (BYTE)w; + } + rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */ + } + + /* Build rankVal */ + { U32* const rankVal0 = rankVal[0]; + { int const rescale = (maxTableLog-tableLog) - 1; /* tableLog <= maxTableLog */ + U32 nextRankVal = 0; + U32 w; + for (w=1; w<maxW+1; w++) { + U32 current = nextRankVal; + nextRankVal += rankStats[w] << (w+rescale); + rankVal0[w] = current; + } } + { U32 const minBits = tableLog+1 - maxW; + U32 consumed; + for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) { + U32* const rankValPtr = rankVal[consumed]; + U32 w; + for (w = 1; w < maxW+1; w++) { + rankValPtr[w] = rankVal0[w] >> consumed; + } } } } + + HUF_fillDTableX4(dt, maxTableLog, + sortedSymbol, sizeOfSort, + rankStart0, rankVal, maxW, + tableLog+1); + + dtd.tableLog = (BYTE)maxTableLog; + dtd.tableType = 1; + memcpy(DTable, &dtd, sizeof(dtd)); + return iSize; +} + + +static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog) +{ + size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */ + memcpy(op, dt+val, 2); + BIT_skipBits(DStream, dt[val].nbBits); + return dt[val].length; +} + +static U32 HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog) +{ + size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */ + memcpy(op, dt+val, 1); + if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits); + else { + if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) { + BIT_skipBits(DStream, dt[val].nbBits); + if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8)) + DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8); /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */ + } } + return 1; +} + + +#define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \ + ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog) + +#define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \ + if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \ + ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog) + +#define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \ + if (MEM_64bits()) \ + ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog) + +FORCE_INLINE size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const HUF_DEltX4* const dt, const U32 dtLog) +{ + BYTE* const pStart = p; + + /* up to 8 symbols at a time */ + while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-(sizeof(bitDPtr->bitContainer)-1))) { + HUF_DECODE_SYMBOLX4_2(p, bitDPtr); + HUF_DECODE_SYMBOLX4_1(p, bitDPtr); + HUF_DECODE_SYMBOLX4_2(p, bitDPtr); + HUF_DECODE_SYMBOLX4_0(p, bitDPtr); + } + + /* closer to end : up to 2 symbols at a time */ + while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd-2)) + HUF_DECODE_SYMBOLX4_0(p, bitDPtr); + + while (p <= pEnd-2) + HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */ + + if (p < pEnd) + p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog); + + return p-pStart; +} + + +static size_t HUF_decompress1X4_usingDTable_internal( + void* dst, size_t dstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + BIT_DStream_t bitD; + + /* Init */ + { size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); + if (HUF_isError(errorCode)) return errorCode; + } + + /* decode */ + { BYTE* const ostart = (BYTE*) dst; + BYTE* const oend = ostart + dstSize; + const void* const dtPtr = DTable+1; /* force compiler to not use strict-aliasing */ + const HUF_DEltX4* const dt = (const HUF_DEltX4*)dtPtr; + DTableDesc const dtd = HUF_getDTableDesc(DTable); + HUF_decodeStreamX4(ostart, &bitD, oend, dt, dtd.tableLog); + } + + /* check */ + if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected); + + /* decoded size */ + return dstSize; +} + +size_t HUF_decompress1X4_usingDTable( + void* dst, size_t dstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + DTableDesc dtd = HUF_getDTableDesc(DTable); + if (dtd.tableType != 1) return ERROR(GENERIC); + return HUF_decompress1X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable); +} + +size_t HUF_decompress1X4_DCtx (HUF_DTable* DCtx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + const BYTE* ip = (const BYTE*) cSrc; + + size_t const hSize = HUF_readDTableX4 (DCtx, cSrc, cSrcSize); + if (HUF_isError(hSize)) return hSize; + if (hSize >= cSrcSize) return ERROR(srcSize_wrong); + ip += hSize; cSrcSize -= hSize; + + return HUF_decompress1X4_usingDTable_internal (dst, dstSize, ip, cSrcSize, DCtx); +} + +size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_TABLELOG_MAX); + return HUF_decompress1X4_DCtx(DTable, dst, dstSize, cSrc, cSrcSize); +} + +static size_t HUF_decompress4X4_usingDTable_internal( + void* dst, size_t dstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ + + { const BYTE* const istart = (const BYTE*) cSrc; + BYTE* const ostart = (BYTE*) dst; + BYTE* const oend = ostart + dstSize; + const void* const dtPtr = DTable+1; + const HUF_DEltX4* const dt = (const HUF_DEltX4*)dtPtr; + + /* Init */ + BIT_DStream_t bitD1; + BIT_DStream_t bitD2; + BIT_DStream_t bitD3; + BIT_DStream_t bitD4; + size_t const length1 = MEM_readLE16(istart); + size_t const length2 = MEM_readLE16(istart+2); + size_t const length3 = MEM_readLE16(istart+4); + size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6); + const BYTE* const istart1 = istart + 6; /* jumpTable */ + const BYTE* const istart2 = istart1 + length1; + const BYTE* const istart3 = istart2 + length2; + const BYTE* const istart4 = istart3 + length3; + size_t const segmentSize = (dstSize+3) / 4; + BYTE* const opStart2 = ostart + segmentSize; + BYTE* const opStart3 = opStart2 + segmentSize; + BYTE* const opStart4 = opStart3 + segmentSize; + BYTE* op1 = ostart; + BYTE* op2 = opStart2; + BYTE* op3 = opStart3; + BYTE* op4 = opStart4; + U32 endSignal; + DTableDesc const dtd = HUF_getDTableDesc(DTable); + U32 const dtLog = dtd.tableLog; + + if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */ + { size_t const errorCode = BIT_initDStream(&bitD1, istart1, length1); + if (HUF_isError(errorCode)) return errorCode; } + { size_t const errorCode = BIT_initDStream(&bitD2, istart2, length2); + if (HUF_isError(errorCode)) return errorCode; } + { size_t const errorCode = BIT_initDStream(&bitD3, istart3, length3); + if (HUF_isError(errorCode)) return errorCode; } + { size_t const errorCode = BIT_initDStream(&bitD4, istart4, length4); + if (HUF_isError(errorCode)) return errorCode; } + + /* 16-32 symbols per loop (4-8 symbols per stream) */ + endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); + for ( ; (endSignal==BIT_DStream_unfinished) & (op4<(oend-(sizeof(bitD4.bitContainer)-1))) ; ) { + HUF_DECODE_SYMBOLX4_2(op1, &bitD1); + HUF_DECODE_SYMBOLX4_2(op2, &bitD2); + HUF_DECODE_SYMBOLX4_2(op3, &bitD3); + HUF_DECODE_SYMBOLX4_2(op4, &bitD4); + HUF_DECODE_SYMBOLX4_1(op1, &bitD1); + HUF_DECODE_SYMBOLX4_1(op2, &bitD2); + HUF_DECODE_SYMBOLX4_1(op3, &bitD3); + HUF_DECODE_SYMBOLX4_1(op4, &bitD4); + HUF_DECODE_SYMBOLX4_2(op1, &bitD1); + HUF_DECODE_SYMBOLX4_2(op2, &bitD2); + HUF_DECODE_SYMBOLX4_2(op3, &bitD3); + HUF_DECODE_SYMBOLX4_2(op4, &bitD4); + HUF_DECODE_SYMBOLX4_0(op1, &bitD1); + HUF_DECODE_SYMBOLX4_0(op2, &bitD2); + HUF_DECODE_SYMBOLX4_0(op3, &bitD3); + HUF_DECODE_SYMBOLX4_0(op4, &bitD4); + + endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); + } + + /* check corruption */ + if (op1 > opStart2) return ERROR(corruption_detected); + if (op2 > opStart3) return ERROR(corruption_detected); + if (op3 > opStart4) return ERROR(corruption_detected); + /* note : op4 already verified within main loop */ + + /* finish bitStreams one by one */ + HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog); + HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog); + HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog); + HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog); + + /* check */ + { U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); + if (!endCheck) return ERROR(corruption_detected); } + + /* decoded size */ + return dstSize; + } +} + + +size_t HUF_decompress4X4_usingDTable( + void* dst, size_t dstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + DTableDesc dtd = HUF_getDTableDesc(DTable); + if (dtd.tableType != 1) return ERROR(GENERIC); + return HUF_decompress4X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable); +} + + +size_t HUF_decompress4X4_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + const BYTE* ip = (const BYTE*) cSrc; + + size_t hSize = HUF_readDTableX4 (dctx, cSrc, cSrcSize); + if (HUF_isError(hSize)) return hSize; + if (hSize >= cSrcSize) return ERROR(srcSize_wrong); + ip += hSize; cSrcSize -= hSize; + + return HUF_decompress4X4_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx); +} + +size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_TABLELOG_MAX); + return HUF_decompress4X4_DCtx(DTable, dst, dstSize, cSrc, cSrcSize); +} + + +/* ********************************/ +/* Generic decompression selector */ +/* ********************************/ + +size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + DTableDesc const dtd = HUF_getDTableDesc(DTable); + return dtd.tableType ? HUF_decompress1X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable) : + HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable); +} + +size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, + const void* cSrc, size_t cSrcSize, + const HUF_DTable* DTable) +{ + DTableDesc const dtd = HUF_getDTableDesc(DTable); + return dtd.tableType ? HUF_decompress4X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable) : + HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable); +} + + +typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t; +static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] = +{ + /* single, double, quad */ + {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */ + {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */ + {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */ + {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */ + {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */ + {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */ + {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */ + {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */ + {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */ + {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */ + {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */ + {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */ + {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */ + {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */ + {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */ + {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */ +}; + +/** HUF_selectDecoder() : +* Tells which decoder is likely to decode faster, +* based on a set of pre-determined metrics. +* @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 . +* Assumption : 0 < cSrcSize < dstSize <= 128 KB */ +U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize) +{ + /* decoder timing evaluation */ + U32 const Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */ + U32 const D256 = (U32)(dstSize >> 8); + U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256); + U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256); + DTime1 += DTime1 >> 3; /* advantage to algorithm using less memory, for cache eviction */ + + return DTime1 < DTime0; +} + + +typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); + +size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + static const decompressionAlgo decompress[2] = { HUF_decompress4X2, HUF_decompress4X4 }; + + /* validation checks */ + if (dstSize == 0) return ERROR(dstSize_tooSmall); + if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */ + if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */ + if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */ + + { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize); + return decompress[algoNb](dst, dstSize, cSrc, cSrcSize); + } +} + +size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + /* validation checks */ + if (dstSize == 0) return ERROR(dstSize_tooSmall); + if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */ + if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */ + if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */ + + { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize); + return algoNb ? HUF_decompress4X4_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) : + HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ; + } +} + +size_t HUF_decompress4X_hufOnly (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + /* validation checks */ + if (dstSize == 0) return ERROR(dstSize_tooSmall); + if ((cSrcSize >= dstSize) || (cSrcSize <= 1)) return ERROR(corruption_detected); /* invalid */ + + { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize); + return algoNb ? HUF_decompress4X4_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) : + HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ; + } +} + +size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) +{ + /* validation checks */ + if (dstSize == 0) return ERROR(dstSize_tooSmall); + if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */ + if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */ + if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */ + + { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize); + return algoNb ? HUF_decompress1X4_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) : + HUF_decompress1X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ; + } +} diff --git a/thirdparty/zstd/decompress/zstd_decompress.c b/thirdparty/zstd/decompress/zstd_decompress.c new file mode 100644 index 0000000000..910f9ab783 --- /dev/null +++ b/thirdparty/zstd/decompress/zstd_decompress.c @@ -0,0 +1,2376 @@ +/** + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + + +/* *************************************************************** +* Tuning parameters +*****************************************************************/ +/*! + * HEAPMODE : + * Select how default decompression function ZSTD_decompress() will allocate memory, + * in memory stack (0), or in memory heap (1, requires malloc()) + */ +#ifndef ZSTD_HEAPMODE +# define ZSTD_HEAPMODE 1 +#endif + +/*! +* LEGACY_SUPPORT : +* if set to 1, ZSTD_decompress() can decode older formats (v0.1+) +*/ +#ifndef ZSTD_LEGACY_SUPPORT +# define ZSTD_LEGACY_SUPPORT 0 +#endif + +/*! +* MAXWINDOWSIZE_DEFAULT : +* maximum window size accepted by DStream, by default. +* Frames requiring more memory will be rejected. +*/ +#ifndef ZSTD_MAXWINDOWSIZE_DEFAULT +# define ZSTD_MAXWINDOWSIZE_DEFAULT ((1 << ZSTD_WINDOWLOG_MAX) + 1) /* defined within zstd.h */ +#endif + + +/*-******************************************************* +* Dependencies +*********************************************************/ +#include <string.h> /* memcpy, memmove, memset */ +#include "mem.h" /* low level memory routines */ +#define FSE_STATIC_LINKING_ONLY +#include "fse.h" +#define HUF_STATIC_LINKING_ONLY +#include "huf.h" +#include "zstd_internal.h" + +#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) +# include "zstd_legacy.h" +#endif + + +#if defined(_MSC_VER) +# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */ +# define ZSTD_PREFETCH(ptr) _mm_prefetch((const char*)ptr, _MM_HINT_T0) +#elif defined(__GNUC__) +# define ZSTD_PREFETCH(ptr) __builtin_prefetch(ptr, 0, 0) +#else +# define ZSTD_PREFETCH(ptr) /* disabled */ +#endif + +/*-************************************* +* Macros +***************************************/ +#define ZSTD_isError ERR_isError /* for inlining */ +#define FSE_isError ERR_isError +#define HUF_isError ERR_isError + + +/*_******************************************************* +* Memory operations +**********************************************************/ +static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); } + + +/*-************************************************************* +* Context management +***************************************************************/ +typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader, + ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock, + ZSTDds_decompressLastBlock, ZSTDds_checkChecksum, + ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage; + +typedef struct { + FSE_DTable LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)]; + FSE_DTable OFTable[FSE_DTABLE_SIZE_U32(OffFSELog)]; + FSE_DTable MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)]; + HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */ + U32 rep[ZSTD_REP_NUM]; +} ZSTD_entropyTables_t; + +struct ZSTD_DCtx_s +{ + const FSE_DTable* LLTptr; + const FSE_DTable* MLTptr; + const FSE_DTable* OFTptr; + const HUF_DTable* HUFptr; + ZSTD_entropyTables_t entropy; + const void* previousDstEnd; /* detect continuity */ + const void* base; /* start of current segment */ + const void* vBase; /* virtual start of previous segment if it was just before current one */ + const void* dictEnd; /* end of previous segment */ + size_t expected; + ZSTD_frameParams fParams; + blockType_e bType; /* used in ZSTD_decompressContinue(), to transfer blockType between header decoding and block decoding stages */ + ZSTD_dStage stage; + U32 litEntropy; + U32 fseEntropy; + XXH64_state_t xxhState; + size_t headerSize; + U32 dictID; + const BYTE* litPtr; + ZSTD_customMem customMem; + size_t litSize; + size_t rleSize; + BYTE litBuffer[ZSTD_BLOCKSIZE_ABSOLUTEMAX + WILDCOPY_OVERLENGTH]; + BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX]; +}; /* typedef'd to ZSTD_DCtx within "zstd.h" */ + +size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx) { return (dctx==NULL) ? 0 : sizeof(ZSTD_DCtx); } + +size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); } + +size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) +{ + dctx->expected = ZSTD_frameHeaderSize_prefix; + dctx->stage = ZSTDds_getFrameHeaderSize; + dctx->previousDstEnd = NULL; + dctx->base = NULL; + dctx->vBase = NULL; + dctx->dictEnd = NULL; + dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */ + dctx->litEntropy = dctx->fseEntropy = 0; + dctx->dictID = 0; + MEM_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue)); + memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */ + dctx->LLTptr = dctx->entropy.LLTable; + dctx->MLTptr = dctx->entropy.MLTable; + dctx->OFTptr = dctx->entropy.OFTable; + dctx->HUFptr = dctx->entropy.hufTable; + return 0; +} + +ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem) +{ + ZSTD_DCtx* dctx; + + if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem; + if (!customMem.customAlloc || !customMem.customFree) return NULL; + + dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(ZSTD_DCtx), customMem); + if (!dctx) return NULL; + memcpy(&dctx->customMem, &customMem, sizeof(customMem)); + ZSTD_decompressBegin(dctx); + return dctx; +} + +ZSTD_DCtx* ZSTD_createDCtx(void) +{ + return ZSTD_createDCtx_advanced(defaultCustomMem); +} + +size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx) +{ + if (dctx==NULL) return 0; /* support free on NULL */ + ZSTD_free(dctx, dctx->customMem); + return 0; /* reserved as a potential error code in the future */ +} + +void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx) +{ + size_t const workSpaceSize = (ZSTD_BLOCKSIZE_ABSOLUTEMAX+WILDCOPY_OVERLENGTH) + ZSTD_frameHeaderSize_max; + memcpy(dstDCtx, srcDCtx, sizeof(ZSTD_DCtx) - workSpaceSize); /* no need to copy workspace */ +} + +static void ZSTD_refDDict(ZSTD_DCtx* dstDCtx, const ZSTD_DDict* ddict); + + +/*-************************************************************* +* Decompression section +***************************************************************/ + +/*! ZSTD_isFrame() : + * Tells if the content of `buffer` starts with a valid Frame Identifier. + * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. + * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. + * Note 3 : Skippable Frame Identifiers are considered valid. */ +unsigned ZSTD_isFrame(const void* buffer, size_t size) +{ + if (size < 4) return 0; + { U32 const magic = MEM_readLE32(buffer); + if (magic == ZSTD_MAGICNUMBER) return 1; + if ((magic & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) return 1; + } +#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) + if (ZSTD_isLegacy(buffer, size)) return 1; +#endif + return 0; +} + + +/** ZSTD_frameHeaderSize() : +* srcSize must be >= ZSTD_frameHeaderSize_prefix. +* @return : size of the Frame Header */ +static size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize) +{ + if (srcSize < ZSTD_frameHeaderSize_prefix) return ERROR(srcSize_wrong); + { BYTE const fhd = ((const BYTE*)src)[4]; + U32 const dictID= fhd & 3; + U32 const singleSegment = (fhd >> 5) & 1; + U32 const fcsId = fhd >> 6; + return ZSTD_frameHeaderSize_prefix + !singleSegment + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId] + + (singleSegment && !fcsId); + } +} + + +/** ZSTD_getFrameParams() : +* decode Frame Header, or require larger `srcSize`. +* @return : 0, `fparamsPtr` is correctly filled, +* >0, `srcSize` is too small, result is expected `srcSize`, +* or an error code, which can be tested using ZSTD_isError() */ +size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize) +{ + const BYTE* ip = (const BYTE*)src; + + if (srcSize < ZSTD_frameHeaderSize_prefix) return ZSTD_frameHeaderSize_prefix; + if (MEM_readLE32(src) != ZSTD_MAGICNUMBER) { + if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { + if (srcSize < ZSTD_skippableHeaderSize) return ZSTD_skippableHeaderSize; /* magic number + skippable frame length */ + memset(fparamsPtr, 0, sizeof(*fparamsPtr)); + fparamsPtr->frameContentSize = MEM_readLE32((const char *)src + 4); + fparamsPtr->windowSize = 0; /* windowSize==0 means a frame is skippable */ + return 0; + } + return ERROR(prefix_unknown); + } + + /* ensure there is enough `srcSize` to fully read/decode frame header */ + { size_t const fhsize = ZSTD_frameHeaderSize(src, srcSize); + if (srcSize < fhsize) return fhsize; } + + { BYTE const fhdByte = ip[4]; + size_t pos = 5; + U32 const dictIDSizeCode = fhdByte&3; + U32 const checksumFlag = (fhdByte>>2)&1; + U32 const singleSegment = (fhdByte>>5)&1; + U32 const fcsID = fhdByte>>6; + U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; + U32 windowSize = 0; + U32 dictID = 0; + U64 frameContentSize = 0; + if ((fhdByte & 0x08) != 0) return ERROR(frameParameter_unsupported); /* reserved bits, which must be zero */ + if (!singleSegment) { + BYTE const wlByte = ip[pos++]; + U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN; + if (windowLog > ZSTD_WINDOWLOG_MAX) return ERROR(frameParameter_windowTooLarge); /* avoids issue with 1 << windowLog */ + windowSize = (1U << windowLog); + windowSize += (windowSize >> 3) * (wlByte&7); + } + + switch(dictIDSizeCode) + { + default: /* impossible */ + case 0 : break; + case 1 : dictID = ip[pos]; pos++; break; + case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break; + case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break; + } + switch(fcsID) + { + default: /* impossible */ + case 0 : if (singleSegment) frameContentSize = ip[pos]; break; + case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break; + case 2 : frameContentSize = MEM_readLE32(ip+pos); break; + case 3 : frameContentSize = MEM_readLE64(ip+pos); break; + } + if (!windowSize) windowSize = (U32)frameContentSize; + if (windowSize > windowSizeMax) return ERROR(frameParameter_windowTooLarge); + fparamsPtr->frameContentSize = frameContentSize; + fparamsPtr->windowSize = windowSize; + fparamsPtr->dictID = dictID; + fparamsPtr->checksumFlag = checksumFlag; + } + return 0; +} + +/** ZSTD_getFrameContentSize() : +* compatible with legacy mode +* @return : decompressed size of the single frame pointed to be `src` if known, otherwise +* - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined +* - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */ +unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize) +{ +#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) + if (ZSTD_isLegacy(src, srcSize)) { + unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize); + return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret; + } +#endif + { + ZSTD_frameParams fParams; + if (ZSTD_getFrameParams(&fParams, src, srcSize) != 0) return ZSTD_CONTENTSIZE_ERROR; + if (fParams.windowSize == 0) { + /* Either skippable or empty frame, size == 0 either way */ + return 0; + } else if (fParams.frameContentSize != 0) { + return fParams.frameContentSize; + } else { + return ZSTD_CONTENTSIZE_UNKNOWN; + } + } +} + +/** ZSTD_findDecompressedSize() : + * compatible with legacy mode + * `srcSize` must be the exact length of some number of ZSTD compressed and/or + * skippable frames + * @return : decompressed size of the frames contained */ +unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize) +{ + { + unsigned long long totalDstSize = 0; + while (srcSize >= ZSTD_frameHeaderSize_prefix) { + const U32 magicNumber = MEM_readLE32(src); + + if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { + size_t skippableSize; + if (srcSize < ZSTD_skippableHeaderSize) + return ERROR(srcSize_wrong); + skippableSize = MEM_readLE32((const BYTE *)src + 4) + + ZSTD_skippableHeaderSize; + if (srcSize < skippableSize) { + return ZSTD_CONTENTSIZE_ERROR; + } + + src = (const BYTE *)src + skippableSize; + srcSize -= skippableSize; + continue; + } + + { + unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize); + if (ret >= ZSTD_CONTENTSIZE_ERROR) return ret; + + /* check for overflow */ + if (totalDstSize + ret < totalDstSize) return ZSTD_CONTENTSIZE_ERROR; + totalDstSize += ret; + } + { + size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize); + if (ZSTD_isError(frameSrcSize)) { + return ZSTD_CONTENTSIZE_ERROR; + } + + src = (const BYTE *)src + frameSrcSize; + srcSize -= frameSrcSize; + } + } + + if (srcSize) { + return ZSTD_CONTENTSIZE_ERROR; + } + + return totalDstSize; + } +} + +/** ZSTD_getDecompressedSize() : +* compatible with legacy mode +* @return : decompressed size if known, 0 otherwise + note : 0 can mean any of the following : + - decompressed size is not present within frame header + - frame header unknown / not supported + - frame header not complete (`srcSize` too small) */ +unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize) +{ + unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize); + return ret >= ZSTD_CONTENTSIZE_ERROR ? 0 : ret; +} + + +/** ZSTD_decodeFrameHeader() : +* `headerSize` must be the size provided by ZSTD_frameHeaderSize(). +* @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */ +static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize) +{ + size_t const result = ZSTD_getFrameParams(&(dctx->fParams), src, headerSize); + if (ZSTD_isError(result)) return result; /* invalid header */ + if (result>0) return ERROR(srcSize_wrong); /* headerSize too small */ + if (dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID)) return ERROR(dictionary_wrong); + if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0); + return 0; +} + + +typedef struct +{ + blockType_e blockType; + U32 lastBlock; + U32 origSize; +} blockProperties_t; + +/*! ZSTD_getcBlockSize() : +* Provides the size of compressed block from block header `src` */ +size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, + blockProperties_t* bpPtr) +{ + if (srcSize < ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); + { U32 const cBlockHeader = MEM_readLE24(src); + U32 const cSize = cBlockHeader >> 3; + bpPtr->lastBlock = cBlockHeader & 1; + bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3); + bpPtr->origSize = cSize; /* only useful for RLE */ + if (bpPtr->blockType == bt_rle) return 1; + if (bpPtr->blockType == bt_reserved) return ERROR(corruption_detected); + return cSize; + } +} + + +static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity, + const void* src, size_t srcSize) +{ + if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall); + memcpy(dst, src, srcSize); + return srcSize; +} + + +static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + size_t regenSize) +{ + if (srcSize != 1) return ERROR(srcSize_wrong); + if (regenSize > dstCapacity) return ERROR(dstSize_tooSmall); + memset(dst, *(const BYTE*)src, regenSize); + return regenSize; +} + +/*! ZSTD_decodeLiteralsBlock() : + @return : nb of bytes read from src (< srcSize ) */ +size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx, + const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */ +{ + if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected); + + { const BYTE* const istart = (const BYTE*) src; + symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3); + + switch(litEncType) + { + case set_repeat: + if (dctx->litEntropy==0) return ERROR(dictionary_corrupted); + /* fall-through */ + case set_compressed: + if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */ + { size_t lhSize, litSize, litCSize; + U32 singleStream=0; + U32 const lhlCode = (istart[0] >> 2) & 3; + U32 const lhc = MEM_readLE32(istart); + switch(lhlCode) + { + case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */ + /* 2 - 2 - 10 - 10 */ + singleStream = !lhlCode; + lhSize = 3; + litSize = (lhc >> 4) & 0x3FF; + litCSize = (lhc >> 14) & 0x3FF; + break; + case 2: + /* 2 - 2 - 14 - 14 */ + lhSize = 4; + litSize = (lhc >> 4) & 0x3FFF; + litCSize = lhc >> 18; + break; + case 3: + /* 2 - 2 - 18 - 18 */ + lhSize = 5; + litSize = (lhc >> 4) & 0x3FFFF; + litCSize = (lhc >> 22) + (istart[4] << 10); + break; + } + if (litSize > ZSTD_BLOCKSIZE_ABSOLUTEMAX) return ERROR(corruption_detected); + if (litCSize + lhSize > srcSize) return ERROR(corruption_detected); + + if (HUF_isError((litEncType==set_repeat) ? + ( singleStream ? + HUF_decompress1X_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr) : + HUF_decompress4X_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr) ) : + ( singleStream ? + HUF_decompress1X2_DCtx(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize) : + HUF_decompress4X_hufOnly (dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize)) )) + return ERROR(corruption_detected); + + dctx->litPtr = dctx->litBuffer; + dctx->litSize = litSize; + dctx->litEntropy = 1; + if (litEncType==set_compressed) dctx->HUFptr = dctx->entropy.hufTable; + memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH); + return litCSize + lhSize; + } + + case set_basic: + { size_t litSize, lhSize; + U32 const lhlCode = ((istart[0]) >> 2) & 3; + switch(lhlCode) + { + case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */ + lhSize = 1; + litSize = istart[0] >> 3; + break; + case 1: + lhSize = 2; + litSize = MEM_readLE16(istart) >> 4; + break; + case 3: + lhSize = 3; + litSize = MEM_readLE24(istart) >> 4; + break; + } + + if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */ + if (litSize+lhSize > srcSize) return ERROR(corruption_detected); + memcpy(dctx->litBuffer, istart+lhSize, litSize); + dctx->litPtr = dctx->litBuffer; + dctx->litSize = litSize; + memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH); + return lhSize+litSize; + } + /* direct reference into compressed stream */ + dctx->litPtr = istart+lhSize; + dctx->litSize = litSize; + return lhSize+litSize; + } + + case set_rle: + { U32 const lhlCode = ((istart[0]) >> 2) & 3; + size_t litSize, lhSize; + switch(lhlCode) + { + case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */ + lhSize = 1; + litSize = istart[0] >> 3; + break; + case 1: + lhSize = 2; + litSize = MEM_readLE16(istart) >> 4; + break; + case 3: + lhSize = 3; + litSize = MEM_readLE24(istart) >> 4; + if (srcSize<4) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */ + break; + } + if (litSize > ZSTD_BLOCKSIZE_ABSOLUTEMAX) return ERROR(corruption_detected); + memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH); + dctx->litPtr = dctx->litBuffer; + dctx->litSize = litSize; + return lhSize+1; + } + default: + return ERROR(corruption_detected); /* impossible */ + } + } +} + + +typedef union { + FSE_decode_t realData; + U32 alignedBy4; +} FSE_decode_t4; + +/* Default FSE distribution table for Literal Lengths */ +static const FSE_decode_t4 LL_defaultDTable[(1<<LL_DEFAULTNORMLOG)+1] = { + { { LL_DEFAULTNORMLOG, 1, 1 } }, /* header : tableLog, fastMode, fastMode */ + /* base, symbol, bits */ + { { 0, 0, 4 } }, { { 16, 0, 4 } }, { { 32, 1, 5 } }, { { 0, 3, 5 } }, + { { 0, 4, 5 } }, { { 0, 6, 5 } }, { { 0, 7, 5 } }, { { 0, 9, 5 } }, + { { 0, 10, 5 } }, { { 0, 12, 5 } }, { { 0, 14, 6 } }, { { 0, 16, 5 } }, + { { 0, 18, 5 } }, { { 0, 19, 5 } }, { { 0, 21, 5 } }, { { 0, 22, 5 } }, + { { 0, 24, 5 } }, { { 32, 25, 5 } }, { { 0, 26, 5 } }, { { 0, 27, 6 } }, + { { 0, 29, 6 } }, { { 0, 31, 6 } }, { { 32, 0, 4 } }, { { 0, 1, 4 } }, + { { 0, 2, 5 } }, { { 32, 4, 5 } }, { { 0, 5, 5 } }, { { 32, 7, 5 } }, + { { 0, 8, 5 } }, { { 32, 10, 5 } }, { { 0, 11, 5 } }, { { 0, 13, 6 } }, + { { 32, 16, 5 } }, { { 0, 17, 5 } }, { { 32, 19, 5 } }, { { 0, 20, 5 } }, + { { 32, 22, 5 } }, { { 0, 23, 5 } }, { { 0, 25, 4 } }, { { 16, 25, 4 } }, + { { 32, 26, 5 } }, { { 0, 28, 6 } }, { { 0, 30, 6 } }, { { 48, 0, 4 } }, + { { 16, 1, 4 } }, { { 32, 2, 5 } }, { { 32, 3, 5 } }, { { 32, 5, 5 } }, + { { 32, 6, 5 } }, { { 32, 8, 5 } }, { { 32, 9, 5 } }, { { 32, 11, 5 } }, + { { 32, 12, 5 } }, { { 0, 15, 6 } }, { { 32, 17, 5 } }, { { 32, 18, 5 } }, + { { 32, 20, 5 } }, { { 32, 21, 5 } }, { { 32, 23, 5 } }, { { 32, 24, 5 } }, + { { 0, 35, 6 } }, { { 0, 34, 6 } }, { { 0, 33, 6 } }, { { 0, 32, 6 } }, +}; /* LL_defaultDTable */ + +/* Default FSE distribution table for Match Lengths */ +static const FSE_decode_t4 ML_defaultDTable[(1<<ML_DEFAULTNORMLOG)+1] = { + { { ML_DEFAULTNORMLOG, 1, 1 } }, /* header : tableLog, fastMode, fastMode */ + /* base, symbol, bits */ + { { 0, 0, 6 } }, { { 0, 1, 4 } }, { { 32, 2, 5 } }, { { 0, 3, 5 } }, + { { 0, 5, 5 } }, { { 0, 6, 5 } }, { { 0, 8, 5 } }, { { 0, 10, 6 } }, + { { 0, 13, 6 } }, { { 0, 16, 6 } }, { { 0, 19, 6 } }, { { 0, 22, 6 } }, + { { 0, 25, 6 } }, { { 0, 28, 6 } }, { { 0, 31, 6 } }, { { 0, 33, 6 } }, + { { 0, 35, 6 } }, { { 0, 37, 6 } }, { { 0, 39, 6 } }, { { 0, 41, 6 } }, + { { 0, 43, 6 } }, { { 0, 45, 6 } }, { { 16, 1, 4 } }, { { 0, 2, 4 } }, + { { 32, 3, 5 } }, { { 0, 4, 5 } }, { { 32, 6, 5 } }, { { 0, 7, 5 } }, + { { 0, 9, 6 } }, { { 0, 12, 6 } }, { { 0, 15, 6 } }, { { 0, 18, 6 } }, + { { 0, 21, 6 } }, { { 0, 24, 6 } }, { { 0, 27, 6 } }, { { 0, 30, 6 } }, + { { 0, 32, 6 } }, { { 0, 34, 6 } }, { { 0, 36, 6 } }, { { 0, 38, 6 } }, + { { 0, 40, 6 } }, { { 0, 42, 6 } }, { { 0, 44, 6 } }, { { 32, 1, 4 } }, + { { 48, 1, 4 } }, { { 16, 2, 4 } }, { { 32, 4, 5 } }, { { 32, 5, 5 } }, + { { 32, 7, 5 } }, { { 32, 8, 5 } }, { { 0, 11, 6 } }, { { 0, 14, 6 } }, + { { 0, 17, 6 } }, { { 0, 20, 6 } }, { { 0, 23, 6 } }, { { 0, 26, 6 } }, + { { 0, 29, 6 } }, { { 0, 52, 6 } }, { { 0, 51, 6 } }, { { 0, 50, 6 } }, + { { 0, 49, 6 } }, { { 0, 48, 6 } }, { { 0, 47, 6 } }, { { 0, 46, 6 } }, +}; /* ML_defaultDTable */ + +/* Default FSE distribution table for Offset Codes */ +static const FSE_decode_t4 OF_defaultDTable[(1<<OF_DEFAULTNORMLOG)+1] = { + { { OF_DEFAULTNORMLOG, 1, 1 } }, /* header : tableLog, fastMode, fastMode */ + /* base, symbol, bits */ + { { 0, 0, 5 } }, { { 0, 6, 4 } }, + { { 0, 9, 5 } }, { { 0, 15, 5 } }, + { { 0, 21, 5 } }, { { 0, 3, 5 } }, + { { 0, 7, 4 } }, { { 0, 12, 5 } }, + { { 0, 18, 5 } }, { { 0, 23, 5 } }, + { { 0, 5, 5 } }, { { 0, 8, 4 } }, + { { 0, 14, 5 } }, { { 0, 20, 5 } }, + { { 0, 2, 5 } }, { { 16, 7, 4 } }, + { { 0, 11, 5 } }, { { 0, 17, 5 } }, + { { 0, 22, 5 } }, { { 0, 4, 5 } }, + { { 16, 8, 4 } }, { { 0, 13, 5 } }, + { { 0, 19, 5 } }, { { 0, 1, 5 } }, + { { 16, 6, 4 } }, { { 0, 10, 5 } }, + { { 0, 16, 5 } }, { { 0, 28, 5 } }, + { { 0, 27, 5 } }, { { 0, 26, 5 } }, + { { 0, 25, 5 } }, { { 0, 24, 5 } }, +}; /* OF_defaultDTable */ + +/*! ZSTD_buildSeqTable() : + @return : nb bytes read from src, + or an error code if it fails, testable with ZSTD_isError() +*/ +static size_t ZSTD_buildSeqTable(FSE_DTable* DTableSpace, const FSE_DTable** DTablePtr, + symbolEncodingType_e type, U32 max, U32 maxLog, + const void* src, size_t srcSize, + const FSE_decode_t4* defaultTable, U32 flagRepeatTable) +{ + const void* const tmpPtr = defaultTable; /* bypass strict aliasing */ + switch(type) + { + case set_rle : + if (!srcSize) return ERROR(srcSize_wrong); + if ( (*(const BYTE*)src) > max) return ERROR(corruption_detected); + FSE_buildDTable_rle(DTableSpace, *(const BYTE*)src); + *DTablePtr = DTableSpace; + return 1; + case set_basic : + *DTablePtr = (const FSE_DTable*)tmpPtr; + return 0; + case set_repeat: + if (!flagRepeatTable) return ERROR(corruption_detected); + return 0; + default : /* impossible */ + case set_compressed : + { U32 tableLog; + S16 norm[MaxSeq+1]; + size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize); + if (FSE_isError(headerSize)) return ERROR(corruption_detected); + if (tableLog > maxLog) return ERROR(corruption_detected); + FSE_buildDTable(DTableSpace, norm, max, tableLog); + *DTablePtr = DTableSpace; + return headerSize; + } } +} + +size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr, + const void* src, size_t srcSize) +{ + const BYTE* const istart = (const BYTE* const)src; + const BYTE* const iend = istart + srcSize; + const BYTE* ip = istart; + + /* check */ + if (srcSize < MIN_SEQUENCES_SIZE) return ERROR(srcSize_wrong); + + /* SeqHead */ + { int nbSeq = *ip++; + if (!nbSeq) { *nbSeqPtr=0; return 1; } + if (nbSeq > 0x7F) { + if (nbSeq == 0xFF) { + if (ip+2 > iend) return ERROR(srcSize_wrong); + nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2; + } else { + if (ip >= iend) return ERROR(srcSize_wrong); + nbSeq = ((nbSeq-0x80)<<8) + *ip++; + } + } + *nbSeqPtr = nbSeq; + } + + /* FSE table descriptors */ + if (ip+4 > iend) return ERROR(srcSize_wrong); /* minimum possible size */ + { symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6); + symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3); + symbolEncodingType_e const MLtype = (symbolEncodingType_e)((*ip >> 2) & 3); + ip++; + + /* Build DTables */ + { size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr, + LLtype, MaxLL, LLFSELog, + ip, iend-ip, LL_defaultDTable, dctx->fseEntropy); + if (ZSTD_isError(llhSize)) return ERROR(corruption_detected); + ip += llhSize; + } + { size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr, + OFtype, MaxOff, OffFSELog, + ip, iend-ip, OF_defaultDTable, dctx->fseEntropy); + if (ZSTD_isError(ofhSize)) return ERROR(corruption_detected); + ip += ofhSize; + } + { size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr, + MLtype, MaxML, MLFSELog, + ip, iend-ip, ML_defaultDTable, dctx->fseEntropy); + if (ZSTD_isError(mlhSize)) return ERROR(corruption_detected); + ip += mlhSize; + } + } + + return ip-istart; +} + + +typedef struct { + size_t litLength; + size_t matchLength; + size_t offset; + const BYTE* match; +} seq_t; + +typedef struct { + BIT_DStream_t DStream; + FSE_DState_t stateLL; + FSE_DState_t stateOffb; + FSE_DState_t stateML; + size_t prevOffset[ZSTD_REP_NUM]; + const BYTE* base; + size_t pos; + uPtrDiff gotoDict; +} seqState_t; + + +FORCE_NOINLINE +size_t ZSTD_execSequenceLast7(BYTE* op, + BYTE* const oend, seq_t sequence, + const BYTE** litPtr, const BYTE* const litLimit, + const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd) +{ + BYTE* const oLitEnd = op + sequence.litLength; + size_t const sequenceLength = sequence.litLength + sequence.matchLength; + BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ + BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; + const BYTE* const iLitEnd = *litPtr + sequence.litLength; + const BYTE* match = oLitEnd - sequence.offset; + + /* check */ + if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */ + if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */ + if (oLitEnd <= oend_w) return ERROR(GENERIC); /* Precondition */ + + /* copy literals */ + if (op < oend_w) { + ZSTD_wildcopy(op, *litPtr, oend_w - op); + *litPtr += oend_w - op; + op = oend_w; + } + while (op < oLitEnd) *op++ = *(*litPtr)++; + + /* copy Match */ + if (sequence.offset > (size_t)(oLitEnd - base)) { + /* offset beyond prefix */ + if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected); + match = dictEnd - (base-match); + if (match + sequence.matchLength <= dictEnd) { + memmove(oLitEnd, match, sequence.matchLength); + return sequenceLength; + } + /* span extDict & currentPrefixSegment */ + { size_t const length1 = dictEnd - match; + memmove(oLitEnd, match, length1); + op = oLitEnd + length1; + sequence.matchLength -= length1; + match = base; + } } + while (op < oMatchEnd) *op++ = *match++; + return sequenceLength; +} + + +static seq_t ZSTD_decodeSequence(seqState_t* seqState) +{ + seq_t seq; + + U32 const llCode = FSE_peekSymbol(&seqState->stateLL); + U32 const mlCode = FSE_peekSymbol(&seqState->stateML); + U32 const ofCode = FSE_peekSymbol(&seqState->stateOffb); /* <= maxOff, by table construction */ + + U32 const llBits = LL_bits[llCode]; + U32 const mlBits = ML_bits[mlCode]; + U32 const ofBits = ofCode; + U32 const totalBits = llBits+mlBits+ofBits; + + static const U32 LL_base[MaxLL+1] = { + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + 16, 18, 20, 22, 24, 28, 32, 40, + 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, + 0x2000, 0x4000, 0x8000, 0x10000 }; + + static const U32 ML_base[MaxML+1] = { + 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20, 21, 22, 23, 24, 25, 26, + 27, 28, 29, 30, 31, 32, 33, 34, + 35, 37, 39, 41, 43, 47, 51, 59, + 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803, + 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 }; + + static const U32 OF_base[MaxOff+1] = { + 0, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D, + 0xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD, + 0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD, + 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD }; + + /* sequence */ + { size_t offset; + if (!ofCode) + offset = 0; + else { + offset = OF_base[ofCode] + BIT_readBitsFast(&seqState->DStream, ofBits); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */ + if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); + } + + if (ofCode <= 1) { + offset += (llCode==0); + if (offset) { + size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset]; + temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */ + if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1]; + seqState->prevOffset[1] = seqState->prevOffset[0]; + seqState->prevOffset[0] = offset = temp; + } else { + offset = seqState->prevOffset[0]; + } + } else { + seqState->prevOffset[2] = seqState->prevOffset[1]; + seqState->prevOffset[1] = seqState->prevOffset[0]; + seqState->prevOffset[0] = offset; + } + seq.offset = offset; + } + + seq.matchLength = ML_base[mlCode] + ((mlCode>31) ? BIT_readBitsFast(&seqState->DStream, mlBits) : 0); /* <= 16 bits */ + if (MEM_32bits() && (mlBits+llBits>24)) BIT_reloadDStream(&seqState->DStream); + + seq.litLength = LL_base[llCode] + ((llCode>15) ? BIT_readBitsFast(&seqState->DStream, llBits) : 0); /* <= 16 bits */ + if (MEM_32bits() || + (totalBits > 64 - 7 - (LLFSELog+MLFSELog+OffFSELog)) ) BIT_reloadDStream(&seqState->DStream); + + /* ANS state update */ + FSE_updateState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */ + FSE_updateState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */ + if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */ + FSE_updateState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */ + + return seq; +} + + +FORCE_INLINE +size_t ZSTD_execSequence(BYTE* op, + BYTE* const oend, seq_t sequence, + const BYTE** litPtr, const BYTE* const litLimit, + const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd) +{ + BYTE* const oLitEnd = op + sequence.litLength; + size_t const sequenceLength = sequence.litLength + sequence.matchLength; + BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ + BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; + const BYTE* const iLitEnd = *litPtr + sequence.litLength; + const BYTE* match = oLitEnd - sequence.offset; + + /* check */ + if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */ + if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */ + if (oLitEnd>oend_w) return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, base, vBase, dictEnd); + + /* copy Literals */ + ZSTD_copy8(op, *litPtr); + if (sequence.litLength > 8) + ZSTD_wildcopy(op+8, (*litPtr)+8, sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */ + op = oLitEnd; + *litPtr = iLitEnd; /* update for next sequence */ + + /* copy Match */ + if (sequence.offset > (size_t)(oLitEnd - base)) { + /* offset beyond prefix -> go into extDict */ + if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected); + match = dictEnd + (match - base); + if (match + sequence.matchLength <= dictEnd) { + memmove(oLitEnd, match, sequence.matchLength); + return sequenceLength; + } + /* span extDict & currentPrefixSegment */ + { size_t const length1 = dictEnd - match; + memmove(oLitEnd, match, length1); + op = oLitEnd + length1; + sequence.matchLength -= length1; + match = base; + if (op > oend_w || sequence.matchLength < MINMATCH) { + U32 i; + for (i = 0; i < sequence.matchLength; ++i) op[i] = match[i]; + return sequenceLength; + } + } } + /* Requirement: op <= oend_w && sequence.matchLength >= MINMATCH */ + + /* match within prefix */ + if (sequence.offset < 8) { + /* close range match, overlap */ + static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */ + static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */ + int const sub2 = dec64table[sequence.offset]; + op[0] = match[0]; + op[1] = match[1]; + op[2] = match[2]; + op[3] = match[3]; + match += dec32table[sequence.offset]; + ZSTD_copy4(op+4, match); + match -= sub2; + } else { + ZSTD_copy8(op, match); + } + op += 8; match += 8; + + if (oMatchEnd > oend-(16-MINMATCH)) { + if (op < oend_w) { + ZSTD_wildcopy(op, match, oend_w - op); + match += oend_w - op; + op = oend_w; + } + while (op < oMatchEnd) *op++ = *match++; + } else { + ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */ + } + return sequenceLength; +} + + +static size_t ZSTD_decompressSequences( + ZSTD_DCtx* dctx, + void* dst, size_t maxDstSize, + const void* seqStart, size_t seqSize) +{ + const BYTE* ip = (const BYTE*)seqStart; + const BYTE* const iend = ip + seqSize; + BYTE* const ostart = (BYTE* const)dst; + BYTE* const oend = ostart + maxDstSize; + BYTE* op = ostart; + const BYTE* litPtr = dctx->litPtr; + const BYTE* const litEnd = litPtr + dctx->litSize; + const BYTE* const base = (const BYTE*) (dctx->base); + const BYTE* const vBase = (const BYTE*) (dctx->vBase); + const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); + int nbSeq; + + /* Build Decoding Tables */ + { size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, seqSize); + if (ZSTD_isError(seqHSize)) return seqHSize; + ip += seqHSize; + } + + /* Regen sequences */ + if (nbSeq) { + seqState_t seqState; + dctx->fseEntropy = 1; + { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; } + CHECK_E(BIT_initDStream(&seqState.DStream, ip, iend-ip), corruption_detected); + FSE_initDState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr); + FSE_initDState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr); + FSE_initDState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); + + for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) { + nbSeq--; + { seq_t const sequence = ZSTD_decodeSequence(&seqState); + size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd); + if (ZSTD_isError(oneSeqSize)) return oneSeqSize; + op += oneSeqSize; + } } + + /* check if reached exact end */ + if (nbSeq) return ERROR(corruption_detected); + /* save reps for next block */ + { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); } + } + + /* last literal segment */ + { size_t const lastLLSize = litEnd - litPtr; + if (lastLLSize > (size_t)(oend-op)) return ERROR(dstSize_tooSmall); + memcpy(op, litPtr, lastLLSize); + op += lastLLSize; + } + + return op-ostart; +} + + +FORCE_INLINE seq_t ZSTD_decodeSequenceLong_generic(seqState_t* seqState, int const longOffsets) +{ + seq_t seq; + + U32 const llCode = FSE_peekSymbol(&seqState->stateLL); + U32 const mlCode = FSE_peekSymbol(&seqState->stateML); + U32 const ofCode = FSE_peekSymbol(&seqState->stateOffb); /* <= maxOff, by table construction */ + + U32 const llBits = LL_bits[llCode]; + U32 const mlBits = ML_bits[mlCode]; + U32 const ofBits = ofCode; + U32 const totalBits = llBits+mlBits+ofBits; + + static const U32 LL_base[MaxLL+1] = { + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + 16, 18, 20, 22, 24, 28, 32, 40, + 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, + 0x2000, 0x4000, 0x8000, 0x10000 }; + + static const U32 ML_base[MaxML+1] = { + 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20, 21, 22, 23, 24, 25, 26, + 27, 28, 29, 30, 31, 32, 33, 34, + 35, 37, 39, 41, 43, 47, 51, 59, + 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803, + 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 }; + + static const U32 OF_base[MaxOff+1] = { + 0, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D, + 0xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD, + 0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD, + 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD }; + + /* sequence */ + { size_t offset; + if (!ofCode) + offset = 0; + else { + if (longOffsets) { + int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN); + offset = OF_base[ofCode] + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits); + if (MEM_32bits() || extraBits) BIT_reloadDStream(&seqState->DStream); + if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits); + } else { + offset = OF_base[ofCode] + BIT_readBitsFast(&seqState->DStream, ofBits); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */ + if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); + } + } + + if (ofCode <= 1) { + offset += (llCode==0); + if (offset) { + size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset]; + temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */ + if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1]; + seqState->prevOffset[1] = seqState->prevOffset[0]; + seqState->prevOffset[0] = offset = temp; + } else { + offset = seqState->prevOffset[0]; + } + } else { + seqState->prevOffset[2] = seqState->prevOffset[1]; + seqState->prevOffset[1] = seqState->prevOffset[0]; + seqState->prevOffset[0] = offset; + } + seq.offset = offset; + } + + seq.matchLength = ML_base[mlCode] + ((mlCode>31) ? BIT_readBitsFast(&seqState->DStream, mlBits) : 0); /* <= 16 bits */ + if (MEM_32bits() && (mlBits+llBits>24)) BIT_reloadDStream(&seqState->DStream); + + seq.litLength = LL_base[llCode] + ((llCode>15) ? BIT_readBitsFast(&seqState->DStream, llBits) : 0); /* <= 16 bits */ + if (MEM_32bits() || + (totalBits > 64 - 7 - (LLFSELog+MLFSELog+OffFSELog)) ) BIT_reloadDStream(&seqState->DStream); + + { size_t const pos = seqState->pos + seq.litLength; + seq.match = seqState->base + pos - seq.offset; /* single memory segment */ + if (seq.offset > pos) seq.match += seqState->gotoDict; /* separate memory segment */ + seqState->pos = pos + seq.matchLength; + } + + /* ANS state update */ + FSE_updateState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */ + FSE_updateState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */ + if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */ + FSE_updateState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */ + + return seq; +} + +static seq_t ZSTD_decodeSequenceLong(seqState_t* seqState, unsigned const windowSize) { + if (ZSTD_highbit32(windowSize) > STREAM_ACCUMULATOR_MIN) { + return ZSTD_decodeSequenceLong_generic(seqState, 1); + } else { + return ZSTD_decodeSequenceLong_generic(seqState, 0); + } +} + +FORCE_INLINE +size_t ZSTD_execSequenceLong(BYTE* op, + BYTE* const oend, seq_t sequence, + const BYTE** litPtr, const BYTE* const litLimit, + const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd) +{ + BYTE* const oLitEnd = op + sequence.litLength; + size_t const sequenceLength = sequence.litLength + sequence.matchLength; + BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ + BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; + const BYTE* const iLitEnd = *litPtr + sequence.litLength; + const BYTE* match = sequence.match; + + /* check */ +#if 1 + if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */ + if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */ + if (oLitEnd>oend_w) return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, base, vBase, dictEnd); +#endif + + /* copy Literals */ + ZSTD_copy8(op, *litPtr); + if (sequence.litLength > 8) + ZSTD_wildcopy(op+8, (*litPtr)+8, sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */ + op = oLitEnd; + *litPtr = iLitEnd; /* update for next sequence */ + + /* copy Match */ +#if 1 + if (sequence.offset > (size_t)(oLitEnd - base)) { + /* offset beyond prefix */ + if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected); + if (match + sequence.matchLength <= dictEnd) { + memmove(oLitEnd, match, sequence.matchLength); + return sequenceLength; + } + /* span extDict & currentPrefixSegment */ + { size_t const length1 = dictEnd - match; + memmove(oLitEnd, match, length1); + op = oLitEnd + length1; + sequence.matchLength -= length1; + match = base; + if (op > oend_w || sequence.matchLength < MINMATCH) { + U32 i; + for (i = 0; i < sequence.matchLength; ++i) op[i] = match[i]; + return sequenceLength; + } + } } + /* Requirement: op <= oend_w && sequence.matchLength >= MINMATCH */ +#endif + + /* match within prefix */ + if (sequence.offset < 8) { + /* close range match, overlap */ + static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */ + static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */ + int const sub2 = dec64table[sequence.offset]; + op[0] = match[0]; + op[1] = match[1]; + op[2] = match[2]; + op[3] = match[3]; + match += dec32table[sequence.offset]; + ZSTD_copy4(op+4, match); + match -= sub2; + } else { + ZSTD_copy8(op, match); + } + op += 8; match += 8; + + if (oMatchEnd > oend-(16-MINMATCH)) { + if (op < oend_w) { + ZSTD_wildcopy(op, match, oend_w - op); + match += oend_w - op; + op = oend_w; + } + while (op < oMatchEnd) *op++ = *match++; + } else { + ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */ + } + return sequenceLength; +} + +static size_t ZSTD_decompressSequencesLong( + ZSTD_DCtx* dctx, + void* dst, size_t maxDstSize, + const void* seqStart, size_t seqSize) +{ + const BYTE* ip = (const BYTE*)seqStart; + const BYTE* const iend = ip + seqSize; + BYTE* const ostart = (BYTE* const)dst; + BYTE* const oend = ostart + maxDstSize; + BYTE* op = ostart; + const BYTE* litPtr = dctx->litPtr; + const BYTE* const litEnd = litPtr + dctx->litSize; + const BYTE* const base = (const BYTE*) (dctx->base); + const BYTE* const vBase = (const BYTE*) (dctx->vBase); + const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); + unsigned const windowSize = dctx->fParams.windowSize; + int nbSeq; + + /* Build Decoding Tables */ + { size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, seqSize); + if (ZSTD_isError(seqHSize)) return seqHSize; + ip += seqHSize; + } + + /* Regen sequences */ + if (nbSeq) { +#define STORED_SEQS 4 +#define STOSEQ_MASK (STORED_SEQS-1) +#define ADVANCED_SEQS 4 + seq_t sequences[STORED_SEQS]; + int const seqAdvance = MIN(nbSeq, ADVANCED_SEQS); + seqState_t seqState; + int seqNb; + dctx->fseEntropy = 1; + { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; } + seqState.base = base; + seqState.pos = (size_t)(op-base); + seqState.gotoDict = (uPtrDiff)dictEnd - (uPtrDiff)base; /* cast to avoid undefined behaviour */ + CHECK_E(BIT_initDStream(&seqState.DStream, ip, iend-ip), corruption_detected); + FSE_initDState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr); + FSE_initDState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr); + FSE_initDState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); + + /* prepare in advance */ + for (seqNb=0; (BIT_reloadDStream(&seqState.DStream) <= BIT_DStream_completed) && seqNb<seqAdvance; seqNb++) { + sequences[seqNb] = ZSTD_decodeSequenceLong(&seqState, windowSize); + } + if (seqNb<seqAdvance) return ERROR(corruption_detected); + + /* decode and decompress */ + for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && seqNb<nbSeq ; seqNb++) { + seq_t const sequence = ZSTD_decodeSequenceLong(&seqState, windowSize); + size_t const oneSeqSize = ZSTD_execSequenceLong(op, oend, sequences[(seqNb-ADVANCED_SEQS) & STOSEQ_MASK], &litPtr, litEnd, base, vBase, dictEnd); + if (ZSTD_isError(oneSeqSize)) return oneSeqSize; + ZSTD_PREFETCH(sequence.match); + sequences[seqNb&STOSEQ_MASK] = sequence; + op += oneSeqSize; + } + if (seqNb<nbSeq) return ERROR(corruption_detected); + + /* finish queue */ + seqNb -= seqAdvance; + for ( ; seqNb<nbSeq ; seqNb++) { + size_t const oneSeqSize = ZSTD_execSequenceLong(op, oend, sequences[seqNb&STOSEQ_MASK], &litPtr, litEnd, base, vBase, dictEnd); + if (ZSTD_isError(oneSeqSize)) return oneSeqSize; + op += oneSeqSize; + } + + /* save reps for next block */ + { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); } + } + + /* last literal segment */ + { size_t const lastLLSize = litEnd - litPtr; + if (lastLLSize > (size_t)(oend-op)) return ERROR(dstSize_tooSmall); + memcpy(op, litPtr, lastLLSize); + op += lastLLSize; + } + + return op-ostart; +} + + +static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize) +{ /* blockType == blockCompressed */ + const BYTE* ip = (const BYTE*)src; + + if (srcSize >= ZSTD_BLOCKSIZE_ABSOLUTEMAX) return ERROR(srcSize_wrong); + + /* Decode literals section */ + { size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize); + if (ZSTD_isError(litCSize)) return litCSize; + ip += litCSize; + srcSize -= litCSize; + } + if (sizeof(size_t) > 4) /* do not enable prefetching on 32-bits x86, as it's performance detrimental */ + /* likely because of register pressure */ + /* if that's the correct cause, then 32-bits ARM should be affected differently */ + /* it would be good to test this on ARM real hardware, to see if prefetch version improves speed */ + if (dctx->fParams.windowSize > (1<<23)) + return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize); + return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize); +} + + +static void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst) +{ + if (dst != dctx->previousDstEnd) { /* not contiguous */ + dctx->dictEnd = dctx->previousDstEnd; + dctx->vBase = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base)); + dctx->base = dst; + dctx->previousDstEnd = dst; + } +} + +size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize) +{ + size_t dSize; + ZSTD_checkContinuity(dctx, dst); + dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize); + dctx->previousDstEnd = (char*)dst + dSize; + return dSize; +} + + +/** ZSTD_insertBlock() : + insert `src` block into `dctx` history. Useful to track uncompressed blocks. */ +ZSTDLIB_API size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize) +{ + ZSTD_checkContinuity(dctx, blockStart); + dctx->previousDstEnd = (const char*)blockStart + blockSize; + return blockSize; +} + + +size_t ZSTD_generateNxBytes(void* dst, size_t dstCapacity, BYTE byte, size_t length) +{ + if (length > dstCapacity) return ERROR(dstSize_tooSmall); + memset(dst, byte, length); + return length; +} + +/** ZSTD_findFrameCompressedSize() : + * compatible with legacy mode + * `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame + * `srcSize` must be at least as large as the frame contained + * @return : the compressed size of the frame starting at `src` */ +size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize) +{ +#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) + if (ZSTD_isLegacy(src, srcSize)) return ZSTD_findFrameCompressedSizeLegacy(src, srcSize); +#endif + if (srcSize >= ZSTD_skippableHeaderSize && + (MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { + return ZSTD_skippableHeaderSize + MEM_readLE32((const BYTE*)src + 4); + } else { + const BYTE* ip = (const BYTE*)src; + const BYTE* const ipstart = ip; + size_t remainingSize = srcSize; + ZSTD_frameParams fParams; + + size_t const headerSize = ZSTD_frameHeaderSize(ip, remainingSize); + if (ZSTD_isError(headerSize)) return headerSize; + + /* Frame Header */ + { size_t const ret = ZSTD_getFrameParams(&fParams, ip, remainingSize); + if (ZSTD_isError(ret)) return ret; + if (ret > 0) return ERROR(srcSize_wrong); + } + + ip += headerSize; + remainingSize -= headerSize; + + /* Loop on each block */ + while (1) { + blockProperties_t blockProperties; + size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); + if (ZSTD_isError(cBlockSize)) return cBlockSize; + + if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) return ERROR(srcSize_wrong); + + ip += ZSTD_blockHeaderSize + cBlockSize; + remainingSize -= ZSTD_blockHeaderSize + cBlockSize; + + if (blockProperties.lastBlock) break; + } + + if (fParams.checksumFlag) { /* Frame content checksum */ + if (remainingSize < 4) return ERROR(srcSize_wrong); + ip += 4; + remainingSize -= 4; + } + + return ip - ipstart; + } +} + +/*! ZSTD_decompressFrame() : +* @dctx must be properly initialized */ +static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx, + void* dst, size_t dstCapacity, + const void** srcPtr, size_t *srcSizePtr) +{ + const BYTE* ip = (const BYTE*)(*srcPtr); + BYTE* const ostart = (BYTE* const)dst; + BYTE* const oend = ostart + dstCapacity; + BYTE* op = ostart; + size_t remainingSize = *srcSizePtr; + + /* check */ + if (remainingSize < ZSTD_frameHeaderSize_min+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); + + /* Frame Header */ + { size_t const frameHeaderSize = ZSTD_frameHeaderSize(ip, ZSTD_frameHeaderSize_prefix); + if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize; + if (remainingSize < frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); + CHECK_F(ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize)); + ip += frameHeaderSize; remainingSize -= frameHeaderSize; + } + + /* Loop on each block */ + while (1) { + size_t decodedSize; + blockProperties_t blockProperties; + size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); + if (ZSTD_isError(cBlockSize)) return cBlockSize; + + ip += ZSTD_blockHeaderSize; + remainingSize -= ZSTD_blockHeaderSize; + if (cBlockSize > remainingSize) return ERROR(srcSize_wrong); + + switch(blockProperties.blockType) + { + case bt_compressed: + decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize); + break; + case bt_raw : + decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize); + break; + case bt_rle : + decodedSize = ZSTD_generateNxBytes(op, oend-op, *ip, blockProperties.origSize); + break; + case bt_reserved : + default: + return ERROR(corruption_detected); + } + + if (ZSTD_isError(decodedSize)) return decodedSize; + if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, op, decodedSize); + op += decodedSize; + ip += cBlockSize; + remainingSize -= cBlockSize; + if (blockProperties.lastBlock) break; + } + + if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */ + U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState); + U32 checkRead; + if (remainingSize<4) return ERROR(checksum_wrong); + checkRead = MEM_readLE32(ip); + if (checkRead != checkCalc) return ERROR(checksum_wrong); + ip += 4; + remainingSize -= 4; + } + + /* Allow caller to get size read */ + *srcPtr = ip; + *srcSizePtr = remainingSize; + return op-ostart; +} + +static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict); +static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict); + +static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void *dict, size_t dictSize, + const ZSTD_DDict* ddict) +{ + void* const dststart = dst; + + if (ddict) { + if (dict) { + /* programmer error, these two cases should be mutually exclusive */ + return ERROR(GENERIC); + } + + dict = ZSTD_DDictDictContent(ddict); + dictSize = ZSTD_DDictDictSize(ddict); + } + + while (srcSize >= ZSTD_frameHeaderSize_prefix) { + U32 magicNumber; + +#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) + if (ZSTD_isLegacy(src, srcSize)) { + size_t decodedSize; + size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize); + if (ZSTD_isError(frameSize)) return frameSize; + + decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize); + + dst = (BYTE*)dst + decodedSize; + dstCapacity -= decodedSize; + + src = (const BYTE*)src + frameSize; + srcSize -= frameSize; + + continue; + } +#endif + + magicNumber = MEM_readLE32(src); + if (magicNumber != ZSTD_MAGICNUMBER) { + if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { + size_t skippableSize; + if (srcSize < ZSTD_skippableHeaderSize) + return ERROR(srcSize_wrong); + skippableSize = MEM_readLE32((const BYTE *)src + 4) + + ZSTD_skippableHeaderSize; + if (srcSize < skippableSize) { + return ERROR(srcSize_wrong); + } + + src = (const BYTE *)src + skippableSize; + srcSize -= skippableSize; + continue; + } else { + return ERROR(prefix_unknown); + } + } + + if (ddict) { + /* we were called from ZSTD_decompress_usingDDict */ + ZSTD_refDDict(dctx, ddict); + } else { + /* this will initialize correctly with no dict if dict == NULL, so + * use this in all cases but ddict */ + CHECK_F(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize)); + } + ZSTD_checkContinuity(dctx, dst); + + { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity, + &src, &srcSize); + if (ZSTD_isError(res)) return res; + /* don't need to bounds check this, ZSTD_decompressFrame will have + * already */ + dst = (BYTE*)dst + res; + dstCapacity -= res; + } + } + + if (srcSize) return ERROR(srcSize_wrong); /* input not entirely consumed */ + + return (BYTE*)dst - (BYTE*)dststart; +} + +size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict, size_t dictSize) +{ + return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL); +} + + +size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + return ZSTD_decompress_usingDict(dctx, dst, dstCapacity, src, srcSize, NULL, 0); +} + + +size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ +#if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1) + size_t regenSize; + ZSTD_DCtx* const dctx = ZSTD_createDCtx(); + if (dctx==NULL) return ERROR(memory_allocation); + regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize); + ZSTD_freeDCtx(dctx); + return regenSize; +#else /* stack mode */ + ZSTD_DCtx dctx; + return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize); +#endif +} + + +/*-************************************** +* Advanced Streaming Decompression API +* Bufferless and synchronous +****************************************/ +size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; } + +ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) { + switch(dctx->stage) + { + default: /* should not happen */ + case ZSTDds_getFrameHeaderSize: + case ZSTDds_decodeFrameHeader: + return ZSTDnit_frameHeader; + case ZSTDds_decodeBlockHeader: + return ZSTDnit_blockHeader; + case ZSTDds_decompressBlock: + return ZSTDnit_block; + case ZSTDds_decompressLastBlock: + return ZSTDnit_lastBlock; + case ZSTDds_checkChecksum: + return ZSTDnit_checksum; + case ZSTDds_decodeSkippableHeader: + case ZSTDds_skipFrame: + return ZSTDnit_skippableFrame; + } +} + +int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; } /* for zbuff */ + +/** ZSTD_decompressContinue() : +* @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity) +* or an error code, which can be tested using ZSTD_isError() */ +size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + /* Sanity check */ + if (srcSize != dctx->expected) return ERROR(srcSize_wrong); + if (dstCapacity) ZSTD_checkContinuity(dctx, dst); + + switch (dctx->stage) + { + case ZSTDds_getFrameHeaderSize : + if (srcSize != ZSTD_frameHeaderSize_prefix) return ERROR(srcSize_wrong); /* impossible */ + if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ + memcpy(dctx->headerBuffer, src, ZSTD_frameHeaderSize_prefix); + dctx->expected = ZSTD_skippableHeaderSize - ZSTD_frameHeaderSize_prefix; /* magic number + skippable frame length */ + dctx->stage = ZSTDds_decodeSkippableHeader; + return 0; + } + dctx->headerSize = ZSTD_frameHeaderSize(src, ZSTD_frameHeaderSize_prefix); + if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize; + memcpy(dctx->headerBuffer, src, ZSTD_frameHeaderSize_prefix); + if (dctx->headerSize > ZSTD_frameHeaderSize_prefix) { + dctx->expected = dctx->headerSize - ZSTD_frameHeaderSize_prefix; + dctx->stage = ZSTDds_decodeFrameHeader; + return 0; + } + dctx->expected = 0; /* not necessary to copy more */ + + case ZSTDds_decodeFrameHeader: + memcpy(dctx->headerBuffer + ZSTD_frameHeaderSize_prefix, src, dctx->expected); + CHECK_F(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize)); + dctx->expected = ZSTD_blockHeaderSize; + dctx->stage = ZSTDds_decodeBlockHeader; + return 0; + + case ZSTDds_decodeBlockHeader: + { blockProperties_t bp; + size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); + if (ZSTD_isError(cBlockSize)) return cBlockSize; + dctx->expected = cBlockSize; + dctx->bType = bp.blockType; + dctx->rleSize = bp.origSize; + if (cBlockSize) { + dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock; + return 0; + } + /* empty block */ + if (bp.lastBlock) { + if (dctx->fParams.checksumFlag) { + dctx->expected = 4; + dctx->stage = ZSTDds_checkChecksum; + } else { + dctx->expected = 0; /* end of frame */ + dctx->stage = ZSTDds_getFrameHeaderSize; + } + } else { + dctx->expected = 3; /* go directly to next header */ + dctx->stage = ZSTDds_decodeBlockHeader; + } + return 0; + } + case ZSTDds_decompressLastBlock: + case ZSTDds_decompressBlock: + { size_t rSize; + switch(dctx->bType) + { + case bt_compressed: + rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize); + break; + case bt_raw : + rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize); + break; + case bt_rle : + rSize = ZSTD_setRleBlock(dst, dstCapacity, src, srcSize, dctx->rleSize); + break; + case bt_reserved : /* should never happen */ + default: + return ERROR(corruption_detected); + } + if (ZSTD_isError(rSize)) return rSize; + if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize); + + if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */ + if (dctx->fParams.checksumFlag) { /* another round for frame checksum */ + dctx->expected = 4; + dctx->stage = ZSTDds_checkChecksum; + } else { + dctx->expected = 0; /* ends here */ + dctx->stage = ZSTDds_getFrameHeaderSize; + } + } else { + dctx->stage = ZSTDds_decodeBlockHeader; + dctx->expected = ZSTD_blockHeaderSize; + dctx->previousDstEnd = (char*)dst + rSize; + } + return rSize; + } + case ZSTDds_checkChecksum: + { U32 const h32 = (U32)XXH64_digest(&dctx->xxhState); + U32 const check32 = MEM_readLE32(src); /* srcSize == 4, guaranteed by dctx->expected */ + if (check32 != h32) return ERROR(checksum_wrong); + dctx->expected = 0; + dctx->stage = ZSTDds_getFrameHeaderSize; + return 0; + } + case ZSTDds_decodeSkippableHeader: + { memcpy(dctx->headerBuffer + ZSTD_frameHeaderSize_prefix, src, dctx->expected); + dctx->expected = MEM_readLE32(dctx->headerBuffer + 4); + dctx->stage = ZSTDds_skipFrame; + return 0; + } + case ZSTDds_skipFrame: + { dctx->expected = 0; + dctx->stage = ZSTDds_getFrameHeaderSize; + return 0; + } + default: + return ERROR(GENERIC); /* impossible */ + } +} + + +static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) +{ + dctx->dictEnd = dctx->previousDstEnd; + dctx->vBase = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base)); + dctx->base = dict; + dctx->previousDstEnd = (const char*)dict + dictSize; + return 0; +} + +/* ZSTD_loadEntropy() : + * dict : must point at beginning of a valid zstd dictionary + * @return : size of entropy tables read */ +static size_t ZSTD_loadEntropy(ZSTD_entropyTables_t* entropy, const void* const dict, size_t const dictSize) +{ + const BYTE* dictPtr = (const BYTE*)dict; + const BYTE* const dictEnd = dictPtr + dictSize; + + if (dictSize <= 8) return ERROR(dictionary_corrupted); + dictPtr += 8; /* skip header = magic + dictID */ + + + { size_t const hSize = HUF_readDTableX4(entropy->hufTable, dictPtr, dictEnd-dictPtr); + if (HUF_isError(hSize)) return ERROR(dictionary_corrupted); + dictPtr += hSize; + } + + { short offcodeNCount[MaxOff+1]; + U32 offcodeMaxValue = MaxOff, offcodeLog; + size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr); + if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted); + if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted); + CHECK_E(FSE_buildDTable(entropy->OFTable, offcodeNCount, offcodeMaxValue, offcodeLog), dictionary_corrupted); + dictPtr += offcodeHeaderSize; + } + + { short matchlengthNCount[MaxML+1]; + unsigned matchlengthMaxValue = MaxML, matchlengthLog; + size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr); + if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted); + if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted); + CHECK_E(FSE_buildDTable(entropy->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog), dictionary_corrupted); + dictPtr += matchlengthHeaderSize; + } + + { short litlengthNCount[MaxLL+1]; + unsigned litlengthMaxValue = MaxLL, litlengthLog; + size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr); + if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted); + if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted); + CHECK_E(FSE_buildDTable(entropy->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog), dictionary_corrupted); + dictPtr += litlengthHeaderSize; + } + + if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted); + { int i; + size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12)); + for (i=0; i<3; i++) { + U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4; + if (rep==0 || rep >= dictContentSize) return ERROR(dictionary_corrupted); + entropy->rep[i] = rep; + } } + + return dictPtr - (const BYTE*)dict; +} + +static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) +{ + if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize); + { U32 const magic = MEM_readLE32(dict); + if (magic != ZSTD_DICT_MAGIC) { + return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */ + } } + dctx->dictID = MEM_readLE32((const char*)dict + 4); + + /* load entropy tables */ + { size_t const eSize = ZSTD_loadEntropy(&dctx->entropy, dict, dictSize); + if (ZSTD_isError(eSize)) return ERROR(dictionary_corrupted); + dict = (const char*)dict + eSize; + dictSize -= eSize; + } + dctx->litEntropy = dctx->fseEntropy = 1; + + /* reference dictionary content */ + return ZSTD_refDictContent(dctx, dict, dictSize); +} + +size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) +{ + CHECK_F(ZSTD_decompressBegin(dctx)); + if (dict && dictSize) CHECK_E(ZSTD_decompress_insertDictionary(dctx, dict, dictSize), dictionary_corrupted); + return 0; +} + + +/* ====== ZSTD_DDict ====== */ + +struct ZSTD_DDict_s { + void* dictBuffer; + const void* dictContent; + size_t dictSize; + ZSTD_entropyTables_t entropy; + U32 dictID; + U32 entropyPresent; + ZSTD_customMem cMem; +}; /* typedef'd to ZSTD_DDict within "zstd.h" */ + +static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict) +{ + return ddict->dictContent; +} + +static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict) +{ + return ddict->dictSize; +} + +static void ZSTD_refDDict(ZSTD_DCtx* dstDCtx, const ZSTD_DDict* ddict) +{ + ZSTD_decompressBegin(dstDCtx); /* init */ + if (ddict) { /* support refDDict on NULL */ + dstDCtx->dictID = ddict->dictID; + dstDCtx->base = ddict->dictContent; + dstDCtx->vBase = ddict->dictContent; + dstDCtx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize; + dstDCtx->previousDstEnd = dstDCtx->dictEnd; + if (ddict->entropyPresent) { + dstDCtx->litEntropy = 1; + dstDCtx->fseEntropy = 1; + dstDCtx->LLTptr = ddict->entropy.LLTable; + dstDCtx->MLTptr = ddict->entropy.MLTable; + dstDCtx->OFTptr = ddict->entropy.OFTable; + dstDCtx->HUFptr = ddict->entropy.hufTable; + dstDCtx->entropy.rep[0] = ddict->entropy.rep[0]; + dstDCtx->entropy.rep[1] = ddict->entropy.rep[1]; + dstDCtx->entropy.rep[2] = ddict->entropy.rep[2]; + } else { + dstDCtx->litEntropy = 0; + dstDCtx->fseEntropy = 0; + } + } +} + +static size_t ZSTD_loadEntropy_inDDict(ZSTD_DDict* ddict) +{ + ddict->dictID = 0; + ddict->entropyPresent = 0; + if (ddict->dictSize < 8) return 0; + { U32 const magic = MEM_readLE32(ddict->dictContent); + if (magic != ZSTD_DICT_MAGIC) return 0; /* pure content mode */ + } + ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + 4); + + /* load entropy tables */ + CHECK_E( ZSTD_loadEntropy(&ddict->entropy, ddict->dictContent, ddict->dictSize), dictionary_corrupted ); + ddict->entropyPresent = 1; + return 0; +} + + +ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, unsigned byReference, ZSTD_customMem customMem) +{ + if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem; + if (!customMem.customAlloc || !customMem.customFree) return NULL; + + { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem); + if (!ddict) return NULL; + ddict->cMem = customMem; + + if ((byReference) || (!dict) || (!dictSize)) { + ddict->dictBuffer = NULL; + ddict->dictContent = dict; + } else { + void* const internalBuffer = ZSTD_malloc(dictSize, customMem); + if (!internalBuffer) { ZSTD_freeDDict(ddict); return NULL; } + memcpy(internalBuffer, dict, dictSize); + ddict->dictBuffer = internalBuffer; + ddict->dictContent = internalBuffer; + } + ddict->dictSize = dictSize; + ddict->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */ + /* parse dictionary content */ + { size_t const errorCode = ZSTD_loadEntropy_inDDict(ddict); + if (ZSTD_isError(errorCode)) { + ZSTD_freeDDict(ddict); + return NULL; + } } + + return ddict; + } +} + +/*! ZSTD_createDDict() : +* Create a digested dictionary, to start decompression without startup delay. +* `dict` content is copied inside DDict. +* Consequently, `dict` can be released after `ZSTD_DDict` creation */ +ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize) +{ + ZSTD_customMem const allocator = { NULL, NULL, NULL }; + return ZSTD_createDDict_advanced(dict, dictSize, 0, allocator); +} + + +/*! ZSTD_createDDict_byReference() : + * Create a digested dictionary, to start decompression without startup delay. + * Dictionary content is simply referenced, it will be accessed during decompression. + * Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer) */ +ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize) +{ + ZSTD_customMem const allocator = { NULL, NULL, NULL }; + return ZSTD_createDDict_advanced(dictBuffer, dictSize, 1, allocator); +} + + +size_t ZSTD_freeDDict(ZSTD_DDict* ddict) +{ + if (ddict==NULL) return 0; /* support free on NULL */ + { ZSTD_customMem const cMem = ddict->cMem; + ZSTD_free(ddict->dictBuffer, cMem); + ZSTD_free(ddict, cMem); + return 0; + } +} + +size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict) +{ + if (ddict==NULL) return 0; /* support sizeof on NULL */ + return sizeof(*ddict) + (ddict->dictBuffer ? ddict->dictSize : 0) ; +} + +/*! ZSTD_getDictID_fromDict() : + * Provides the dictID stored within dictionary. + * if @return == 0, the dictionary is not conformant with Zstandard specification. + * It can still be loaded, but as a content-only dictionary. */ +unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize) +{ + if (dictSize < 8) return 0; + if (MEM_readLE32(dict) != ZSTD_DICT_MAGIC) return 0; + return MEM_readLE32((const char*)dict + 4); +} + +/*! ZSTD_getDictID_fromDDict() : + * Provides the dictID of the dictionary loaded into `ddict`. + * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. + * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ +unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict) +{ + if (ddict==NULL) return 0; + return ZSTD_getDictID_fromDict(ddict->dictContent, ddict->dictSize); +} + +/*! ZSTD_getDictID_fromFrame() : + * Provides the dictID required to decompresse frame stored within `src`. + * If @return == 0, the dictID could not be decoded. + * This could for one of the following reasons : + * - The frame does not require a dictionary (most common case). + * - The frame was built with dictID intentionally removed. + * Needed dictionary is a hidden information. + * Note : this use case also happens when using a non-conformant dictionary. + * - `srcSize` is too small, and as a result, frame header could not be decoded. + * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`. + * - This is not a Zstandard frame. + * When identifying the exact failure cause, it's possible to use + * ZSTD_getFrameParams(), which will provide a more precise error code. */ +unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize) +{ + ZSTD_frameParams zfp = { 0 , 0 , 0 , 0 }; + size_t const hError = ZSTD_getFrameParams(&zfp, src, srcSize); + if (ZSTD_isError(hError)) return 0; + return zfp.dictID; +} + + +/*! ZSTD_decompress_usingDDict() : +* Decompression using a pre-digested Dictionary +* Use dictionary without significant overhead. */ +size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const ZSTD_DDict* ddict) +{ + /* pass content and size in case legacy frames are encountered */ + return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, + NULL, 0, + ddict); +} + + +/*===================================== +* Streaming decompression +*====================================*/ + +typedef enum { zdss_init, zdss_loadHeader, + zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage; + +/* *** Resource management *** */ +struct ZSTD_DStream_s { + ZSTD_DCtx* dctx; + ZSTD_DDict* ddictLocal; + const ZSTD_DDict* ddict; + ZSTD_frameParams fParams; + ZSTD_dStreamStage stage; + char* inBuff; + size_t inBuffSize; + size_t inPos; + size_t maxWindowSize; + char* outBuff; + size_t outBuffSize; + size_t outStart; + size_t outEnd; + size_t blockSize; + BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX]; /* tmp buffer to store frame header */ + size_t lhSize; + ZSTD_customMem customMem; + void* legacyContext; + U32 previousLegacyVersion; + U32 legacyVersion; + U32 hostageByte; +}; /* typedef'd to ZSTD_DStream within "zstd.h" */ + + +ZSTD_DStream* ZSTD_createDStream(void) +{ + return ZSTD_createDStream_advanced(defaultCustomMem); +} + +ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem) +{ + ZSTD_DStream* zds; + + if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem; + if (!customMem.customAlloc || !customMem.customFree) return NULL; + + zds = (ZSTD_DStream*) ZSTD_malloc(sizeof(ZSTD_DStream), customMem); + if (zds==NULL) return NULL; + memset(zds, 0, sizeof(ZSTD_DStream)); + memcpy(&zds->customMem, &customMem, sizeof(ZSTD_customMem)); + zds->dctx = ZSTD_createDCtx_advanced(customMem); + if (zds->dctx == NULL) { ZSTD_freeDStream(zds); return NULL; } + zds->stage = zdss_init; + zds->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT; + return zds; +} + +size_t ZSTD_freeDStream(ZSTD_DStream* zds) +{ + if (zds==NULL) return 0; /* support free on null */ + { ZSTD_customMem const cMem = zds->customMem; + ZSTD_freeDCtx(zds->dctx); + zds->dctx = NULL; + ZSTD_freeDDict(zds->ddictLocal); + zds->ddictLocal = NULL; + ZSTD_free(zds->inBuff, cMem); + zds->inBuff = NULL; + ZSTD_free(zds->outBuff, cMem); + zds->outBuff = NULL; +#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) + if (zds->legacyContext) + ZSTD_freeLegacyStreamContext(zds->legacyContext, zds->previousLegacyVersion); +#endif + ZSTD_free(zds, cMem); + return 0; + } +} + + +/* *** Initialization *** */ + +size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX + ZSTD_blockHeaderSize; } +size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; } + +size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize) +{ + zds->stage = zdss_loadHeader; + zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0; + ZSTD_freeDDict(zds->ddictLocal); + if (dict && dictSize >= 8) { + zds->ddictLocal = ZSTD_createDDict(dict, dictSize); + if (zds->ddictLocal == NULL) return ERROR(memory_allocation); + } else zds->ddictLocal = NULL; + zds->ddict = zds->ddictLocal; + zds->legacyVersion = 0; + zds->hostageByte = 0; + return ZSTD_frameHeaderSize_prefix; +} + +size_t ZSTD_initDStream(ZSTD_DStream* zds) +{ + return ZSTD_initDStream_usingDict(zds, NULL, 0); +} + +/* ZSTD_initDStream_usingDDict() : + * ddict will just be referenced, and must outlive decompression session */ +size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict) +{ + size_t const initResult = ZSTD_initDStream(zds); + zds->ddict = ddict; + return initResult; +} + +size_t ZSTD_resetDStream(ZSTD_DStream* zds) +{ + zds->stage = zdss_loadHeader; + zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0; + zds->legacyVersion = 0; + zds->hostageByte = 0; + return ZSTD_frameHeaderSize_prefix; +} + +size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, + ZSTD_DStreamParameter_e paramType, unsigned paramValue) +{ + switch(paramType) + { + default : return ERROR(parameter_unknown); + case DStream_p_maxWindowSize : zds->maxWindowSize = paramValue ? paramValue : (U32)(-1); break; + } + return 0; +} + + +size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds) +{ + if (zds==NULL) return 0; /* support sizeof NULL */ + return sizeof(*zds) + + ZSTD_sizeof_DCtx(zds->dctx) + + ZSTD_sizeof_DDict(zds->ddictLocal) + + zds->inBuffSize + zds->outBuffSize; +} + + +/* ***** Decompression ***** */ + +MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize) +{ + size_t const length = MIN(dstCapacity, srcSize); + memcpy(dst, src, length); + return length; +} + + +size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input) +{ + const char* const istart = (const char*)(input->src) + input->pos; + const char* const iend = (const char*)(input->src) + input->size; + const char* ip = istart; + char* const ostart = (char*)(output->dst) + output->pos; + char* const oend = (char*)(output->dst) + output->size; + char* op = ostart; + U32 someMoreWork = 1; + +#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) + if (zds->legacyVersion) + return ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input); +#endif + + while (someMoreWork) { + switch(zds->stage) + { + case zdss_init : + ZSTD_resetDStream(zds); /* transparent reset on starting decoding a new frame */ + /* fall-through */ + + case zdss_loadHeader : + { size_t const hSize = ZSTD_getFrameParams(&zds->fParams, zds->headerBuffer, zds->lhSize); + if (ZSTD_isError(hSize)) +#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) + { U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart); + if (legacyVersion) { + const void* const dict = zds->ddict ? zds->ddict->dictContent : NULL; + size_t const dictSize = zds->ddict ? zds->ddict->dictSize : 0; + CHECK_F(ZSTD_initLegacyStream(&zds->legacyContext, zds->previousLegacyVersion, legacyVersion, + dict, dictSize)); + zds->legacyVersion = zds->previousLegacyVersion = legacyVersion; + return ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input); + } else { + return hSize; /* error */ + } } +#else + return hSize; +#endif + if (hSize != 0) { /* need more input */ + size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */ + if (toLoad > (size_t)(iend-ip)) { /* not enough input to load full header */ + memcpy(zds->headerBuffer + zds->lhSize, ip, iend-ip); + zds->lhSize += iend-ip; + input->pos = input->size; + return (MAX(ZSTD_frameHeaderSize_min, hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */ + } + memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad; + break; + } } + + /* check for single-pass mode opportunity */ + if (zds->fParams.frameContentSize && zds->fParams.windowSize /* skippable frame if == 0 */ + && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) { + size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend-istart); + if (cSize <= (size_t)(iend-istart)) { + size_t const decompressedSize = ZSTD_decompress_usingDDict(zds->dctx, op, oend-op, istart, cSize, zds->ddict); + if (ZSTD_isError(decompressedSize)) return decompressedSize; + ip = istart + cSize; + op += decompressedSize; + zds->dctx->expected = 0; + zds->stage = zdss_init; + someMoreWork = 0; + break; + } } + + /* Consume header */ + ZSTD_refDDict(zds->dctx, zds->ddict); + { size_t const h1Size = ZSTD_nextSrcSizeToDecompress(zds->dctx); /* == ZSTD_frameHeaderSize_prefix */ + CHECK_F(ZSTD_decompressContinue(zds->dctx, NULL, 0, zds->headerBuffer, h1Size)); + { size_t const h2Size = ZSTD_nextSrcSizeToDecompress(zds->dctx); + CHECK_F(ZSTD_decompressContinue(zds->dctx, NULL, 0, zds->headerBuffer+h1Size, h2Size)); + } } + + zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN); + if (zds->fParams.windowSize > zds->maxWindowSize) return ERROR(frameParameter_windowTooLarge); + + /* Adapt buffer sizes to frame header instructions */ + { size_t const blockSize = MIN(zds->fParams.windowSize, ZSTD_BLOCKSIZE_ABSOLUTEMAX); + size_t const neededOutSize = zds->fParams.windowSize + blockSize + WILDCOPY_OVERLENGTH * 2; + zds->blockSize = blockSize; + if (zds->inBuffSize < blockSize) { + ZSTD_free(zds->inBuff, zds->customMem); + zds->inBuffSize = 0; + zds->inBuff = (char*)ZSTD_malloc(blockSize, zds->customMem); + if (zds->inBuff == NULL) return ERROR(memory_allocation); + zds->inBuffSize = blockSize; + } + if (zds->outBuffSize < neededOutSize) { + ZSTD_free(zds->outBuff, zds->customMem); + zds->outBuffSize = 0; + zds->outBuff = (char*)ZSTD_malloc(neededOutSize, zds->customMem); + if (zds->outBuff == NULL) return ERROR(memory_allocation); + zds->outBuffSize = neededOutSize; + } } + zds->stage = zdss_read; + /* pass-through */ + + case zdss_read: + { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds->dctx); + if (neededInSize==0) { /* end of frame */ + zds->stage = zdss_init; + someMoreWork = 0; + break; + } + if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */ + const int isSkipFrame = ZSTD_isSkipFrame(zds->dctx); + size_t const decodedSize = ZSTD_decompressContinue(zds->dctx, + zds->outBuff + zds->outStart, (isSkipFrame ? 0 : zds->outBuffSize - zds->outStart), + ip, neededInSize); + if (ZSTD_isError(decodedSize)) return decodedSize; + ip += neededInSize; + if (!decodedSize && !isSkipFrame) break; /* this was just a header */ + zds->outEnd = zds->outStart + decodedSize; + zds->stage = zdss_flush; + break; + } + if (ip==iend) { someMoreWork = 0; break; } /* no more input */ + zds->stage = zdss_load; + /* pass-through */ + } + + case zdss_load: + { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds->dctx); + size_t const toLoad = neededInSize - zds->inPos; /* should always be <= remaining space within inBuff */ + size_t loadedSize; + if (toLoad > zds->inBuffSize - zds->inPos) return ERROR(corruption_detected); /* should never happen */ + loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip); + ip += loadedSize; + zds->inPos += loadedSize; + if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */ + + /* decode loaded input */ + { const int isSkipFrame = ZSTD_isSkipFrame(zds->dctx); + size_t const decodedSize = ZSTD_decompressContinue(zds->dctx, + zds->outBuff + zds->outStart, zds->outBuffSize - zds->outStart, + zds->inBuff, neededInSize); + if (ZSTD_isError(decodedSize)) return decodedSize; + zds->inPos = 0; /* input is consumed */ + if (!decodedSize && !isSkipFrame) { zds->stage = zdss_read; break; } /* this was just a header */ + zds->outEnd = zds->outStart + decodedSize; + zds->stage = zdss_flush; + /* pass-through */ + } } + + case zdss_flush: + { size_t const toFlushSize = zds->outEnd - zds->outStart; + size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize); + op += flushedSize; + zds->outStart += flushedSize; + if (flushedSize == toFlushSize) { /* flush completed */ + zds->stage = zdss_read; + if (zds->outStart + zds->blockSize > zds->outBuffSize) + zds->outStart = zds->outEnd = 0; + break; + } + /* cannot complete flush */ + someMoreWork = 0; + break; + } + default: return ERROR(GENERIC); /* impossible */ + } } + + /* result */ + input->pos += (size_t)(ip-istart); + output->pos += (size_t)(op-ostart); + { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds->dctx); + if (!nextSrcSizeHint) { /* frame fully decoded */ + if (zds->outEnd == zds->outStart) { /* output fully flushed */ + if (zds->hostageByte) { + if (input->pos >= input->size) { zds->stage = zdss_read; return 1; } /* can't release hostage (not present) */ + input->pos++; /* release hostage */ + } + return 0; + } + if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */ + input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */ + zds->hostageByte=1; + } + return 1; + } + nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds->dctx) == ZSTDnit_block); /* preload header of next block */ + if (zds->inPos > nextSrcSizeHint) return ERROR(GENERIC); /* should never happen */ + nextSrcSizeHint -= zds->inPos; /* already loaded*/ + return nextSrcSizeHint; + } +} diff --git a/thirdparty/zstd/zstd.h b/thirdparty/zstd/zstd.h new file mode 100644 index 0000000000..f8050c1361 --- /dev/null +++ b/thirdparty/zstd/zstd.h @@ -0,0 +1,795 @@ +/* + * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#if defined (__cplusplus) +extern "C" { +#endif + +#ifndef ZSTD_H_235446 +#define ZSTD_H_235446 + +/* ====== Dependency ======*/ +#include <stddef.h> /* size_t */ + + +/* ===== ZSTDLIB_API : control library symbols visibility ===== */ +#if defined(__GNUC__) && (__GNUC__ >= 4) +# define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default"))) +#else +# define ZSTDLIB_VISIBILITY +#endif +#if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) +# define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY +#elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) +# define ZSTDLIB_API __declspec(dllimport) ZSTDLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ +#else +# define ZSTDLIB_API ZSTDLIB_VISIBILITY +#endif + + +/******************************************************************************************************* + Introduction + + zstd, short for Zstandard, is a fast lossless compression algorithm, targeting real-time compression scenarios + at zlib-level and better compression ratios. The zstd compression library provides in-memory compression and + decompression functions. The library supports compression levels from 1 up to ZSTD_maxCLevel() which is 22. + Levels >= 20, labeled `--ultra`, should be used with caution, as they require more memory. + Compression can be done in: + - a single step (described as Simple API) + - a single step, reusing a context (described as Explicit memory management) + - unbounded multiple steps (described as Streaming compression) + The compression ratio achievable on small data can be highly improved using compression with a dictionary in: + - a single step (described as Simple dictionary API) + - a single step, reusing a dictionary (described as Fast dictionary API) + + Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h. + These APIs shall never be used with a dynamic library. + They are not "stable", their definition may change in the future. Only static linking is allowed. +*********************************************************************************************************/ + +/*------ Version ------*/ +#define ZSTD_VERSION_MAJOR 1 +#define ZSTD_VERSION_MINOR 2 +#define ZSTD_VERSION_RELEASE 0 + +#define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE +#define ZSTD_QUOTE(str) #str +#define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str) +#define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION) + +#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) +ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< library version number; to be used when checking dll version */ + + +/*************************************** +* Simple API +***************************************/ +/*! ZSTD_compress() : + * Compresses `src` content as a single zstd compressed frame into already allocated `dst`. + * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. + * @return : compressed size written into `dst` (<= `dstCapacity), + * or an error code if it fails (which can be tested using ZSTD_isError()). */ +ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + int compressionLevel); + +/*! ZSTD_decompress() : + * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. + * `dstCapacity` is an upper bound of originalSize. + * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. + * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), + * or an errorCode if it fails (which can be tested using ZSTD_isError()). */ +ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity, + const void* src, size_t compressedSize); + +/*! ZSTD_getDecompressedSize() : + * NOTE: This function is planned to be obsolete, in favour of ZSTD_getFrameContentSize. + * ZSTD_getFrameContentSize functions the same way, returning the decompressed size of a single + * frame, but distinguishes empty frames from frames with an unknown size, or errors. + * + * Additionally, ZSTD_findDecompressedSize can be used instead. It can handle multiple + * concatenated frames in one buffer, and so is more general. + * As a result however, it requires more computation and entire frames to be passed to it, + * as opposed to ZSTD_getFrameContentSize which requires only a single frame's header. + * + * 'src' is the start of a zstd compressed frame. + * @return : content size to be decompressed, as a 64-bits value _if known_, 0 otherwise. + * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. + * When `return==0`, data to decompress could be any size. + * In which case, it's necessary to use streaming mode to decompress data. + * Optionally, application can still use ZSTD_decompress() while relying on implied limits. + * (For example, data may be necessarily cut into blocks <= 16 KB). + * note 2 : decompressed size is always present when compression is done with ZSTD_compress() + * note 3 : decompressed size can be very large (64-bits value), + * potentially larger than what local system can handle as a single memory segment. + * In which case, it's necessary to use streaming mode to decompress data. + * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. + * Always ensure result fits within application's authorized limits. + * Each application can set its own limits. + * note 5 : when `return==0`, if precise failure cause is needed, use ZSTD_getFrameParams() to know more. */ +ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); + + +/*====== Helper functions ======*/ +ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */ +ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case scenario */ +ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ +ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */ + + +/*************************************** +* Explicit memory management +***************************************/ +/*= Compression context + * When compressing many times, + * it is recommended to allocate a context just once, and re-use it for each successive compression operation. + * This will make workload friendlier for system's memory. + * Use one context per thread for parallel execution in multi-threaded environments. */ +typedef struct ZSTD_CCtx_s ZSTD_CCtx; +ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void); +ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); + +/*! ZSTD_compressCCtx() : + * Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */ +ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel); + +/*= Decompression context + * When decompressing many times, + * it is recommended to allocate a context just once, and re-use it for each successive compression operation. + * This will make workload friendlier for system's memory. + * Use one context per thread for parallel execution in multi-threaded environments. */ +typedef struct ZSTD_DCtx_s ZSTD_DCtx; +ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); +ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); + +/*! ZSTD_decompressDCtx() : + * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()). */ +ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); + + +/************************** +* Simple dictionary API +***************************/ +/*! ZSTD_compress_usingDict() : +* Compression using a predefined Dictionary (see dictBuilder/zdict.h). +* Note : This function loads the dictionary, resulting in significant startup delay. +* Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ +ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict,size_t dictSize, + int compressionLevel); + +/*! ZSTD_decompress_usingDict() : +* Decompression using a predefined Dictionary (see dictBuilder/zdict.h). +* Dictionary must be identical to the one used during compression. +* Note : This function loads the dictionary, resulting in significant startup delay. +* Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ +ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict,size_t dictSize); + + +/**************************** +* Fast dictionary API +****************************/ +typedef struct ZSTD_CDict_s ZSTD_CDict; + +/*! ZSTD_createCDict() : +* When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. +* ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. +* ZSTD_CDict can be created once and used by multiple threads concurrently, as its usage is read-only. +* `dictBuffer` can be released after ZSTD_CDict creation, as its content is copied within CDict */ +ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, int compressionLevel); + +/*! ZSTD_freeCDict() : +* Function frees memory allocated by ZSTD_createCDict(). */ +ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); + +/*! ZSTD_compress_usingCDict() : + * Compression using a digested Dictionary. + * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. + * Note that compression level is decided during dictionary creation. + * Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ +ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const ZSTD_CDict* cdict); + + +typedef struct ZSTD_DDict_s ZSTD_DDict; + +/*! ZSTD_createDDict() : +* Create a digested dictionary, ready to start decompression operation without startup delay. +* dictBuffer can be released after DDict creation, as its content is copied inside DDict */ +ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); + +/*! ZSTD_freeDDict() : +* Function frees memory allocated with ZSTD_createDDict() */ +ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); + +/*! ZSTD_decompress_usingDDict() : +* Decompression using a digested Dictionary. +* Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */ +ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const ZSTD_DDict* ddict); + + +/**************************** +* Streaming +****************************/ + +typedef struct ZSTD_inBuffer_s { + const void* src; /**< start of input buffer */ + size_t size; /**< size of input buffer */ + size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ +} ZSTD_inBuffer; + +typedef struct ZSTD_outBuffer_s { + void* dst; /**< start of output buffer */ + size_t size; /**< size of output buffer */ + size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ +} ZSTD_outBuffer; + + + +/*-*********************************************************************** +* Streaming compression - HowTo +* +* A ZSTD_CStream object is required to track streaming operation. +* Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. +* ZSTD_CStream objects can be reused multiple times on consecutive compression operations. +* It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively, +* since it will play nicer with system's memory, by re-using already allocated memory. +* Use one separate ZSTD_CStream per thread for parallel execution. +* +* Start a new compression by initializing ZSTD_CStream. +* Use ZSTD_initCStream() to start a new compression operation. +* Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section) +* +* Use ZSTD_compressStream() repetitively to consume input stream. +* The function will automatically update both `pos` fields. +* Note that it may not consume the entire input, in which case `pos < size`, +* and it's up to the caller to present again remaining data. +* @return : a size hint, preferred nb of bytes to use as input for next function call +* or an error code, which can be tested using ZSTD_isError(). +* Note 1 : it's just a hint, to help latency a little, any other value will work fine. +* Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize() +* +* At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream(). +* `output->pos` will be updated. +* Note that some content might still be left within internal buffer if `output->size` is too small. +* @return : nb of bytes still present within internal buffer (0 if it's empty) +* or an error code, which can be tested using ZSTD_isError(). +* +* ZSTD_endStream() instructs to finish a frame. +* It will perform a flush and write frame epilogue. +* The epilogue is required for decoders to consider a frame completed. +* Similar to ZSTD_flushStream(), it may not be able to flush the full content if `output->size` is too small. +* In which case, call again ZSTD_endStream() to complete the flush. +* @return : nb of bytes still present within internal buffer (0 if it's empty, hence compression completed) +* or an error code, which can be tested using ZSTD_isError(). +* +* *******************************************************************/ + +typedef struct ZSTD_CStream_s ZSTD_CStream; +/*===== ZSTD_CStream management functions =====*/ +ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); +ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); + +/*===== Streaming compression functions =====*/ +ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); +ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); +ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); +ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); + +ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ +ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances. */ + + + +/*-*************************************************************************** +* Streaming decompression - HowTo +* +* A ZSTD_DStream object is required to track streaming operations. +* Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. +* ZSTD_DStream objects can be re-used multiple times. +* +* Use ZSTD_initDStream() to start a new decompression operation, +* or ZSTD_initDStream_usingDict() if decompression requires a dictionary. +* @return : recommended first input size +* +* Use ZSTD_decompressStream() repetitively to consume your input. +* The function will update both `pos` fields. +* If `input.pos < input.size`, some input has not been consumed. +* It's up to the caller to present again remaining data. +* If `output.pos < output.size`, decoder has flushed everything it could. +* @return : 0 when a frame is completely decoded and fully flushed, +* an error code, which can be tested using ZSTD_isError(), +* any other value > 0, which means there is still some decoding to do to complete current frame. +* The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame. +* *******************************************************************************/ + +typedef struct ZSTD_DStream_s ZSTD_DStream; +/*===== ZSTD_DStream management functions =====*/ +ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); +ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); + +/*===== Streaming decompression functions =====*/ +ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); +ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); + +ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ +ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */ + +#endif /* ZSTD_H_235446 */ + + +#if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) +#define ZSTD_H_ZSTD_STATIC_LINKING_ONLY + +/**************************************************************************************** + * START OF ADVANCED AND EXPERIMENTAL FUNCTIONS + * The definitions in this section are considered experimental. + * They should never be used with a dynamic library, as they may change in the future. + * They are provided for advanced usages. + * Use them only in association with static linking. + * ***************************************************************************************/ + +/* --- Constants ---*/ +#define ZSTD_MAGICNUMBER 0xFD2FB528 /* >= v0.8.0 */ +#define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U + +#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) +#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) + +#define ZSTD_WINDOWLOG_MAX_32 27 +#define ZSTD_WINDOWLOG_MAX_64 27 +#define ZSTD_WINDOWLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64)) +#define ZSTD_WINDOWLOG_MIN 10 +#define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX +#define ZSTD_HASHLOG_MIN 6 +#define ZSTD_CHAINLOG_MAX (ZSTD_WINDOWLOG_MAX+1) +#define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN +#define ZSTD_HASHLOG3_MAX 17 +#define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1) +#define ZSTD_SEARCHLOG_MIN 1 +#define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */ +#define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */ +#define ZSTD_TARGETLENGTH_MIN 4 +#define ZSTD_TARGETLENGTH_MAX 999 + +#define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */ +#define ZSTD_FRAMEHEADERSIZE_MIN 6 +static const size_t ZSTD_frameHeaderSize_prefix = 5; +static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN; +static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX; +static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */ + + +/*--- Advanced types ---*/ +typedef enum { ZSTD_fast, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt, ZSTD_btopt2 } ZSTD_strategy; /* from faster to stronger */ + +typedef struct { + unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */ + unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ + unsigned hashLog; /**< dispatch table : larger == faster, more memory */ + unsigned searchLog; /**< nb of searches : larger == more compression, slower */ + unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */ + unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */ + ZSTD_strategy strategy; +} ZSTD_compressionParameters; + +typedef struct { + unsigned contentSizeFlag; /**< 1: content size will be in frame header (when known) */ + unsigned checksumFlag; /**< 1: generate a 32-bits checksum at end of frame, for error detection */ + unsigned noDictIDFlag; /**< 1: no dictID will be saved into frame header (if dictionary compression) */ +} ZSTD_frameParameters; + +typedef struct { + ZSTD_compressionParameters cParams; + ZSTD_frameParameters fParams; +} ZSTD_parameters; + +/*= Custom memory allocation functions */ +typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); +typedef void (*ZSTD_freeFunction) (void* opaque, void* address); +typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; + +/*************************************** +* Compressed size functions +***************************************/ + +/*! ZSTD_findFrameCompressedSize() : + * `src` should point to the start of a ZSTD encoded frame or skippable frame + * `srcSize` must be at least as large as the frame + * @return : the compressed size of the frame pointed to by `src`, suitable to pass to + * `ZSTD_decompress` or similar, or an error code if given invalid input. */ +ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); + +/*************************************** +* Decompressed size functions +***************************************/ +/*! ZSTD_getFrameContentSize() : +* `src` should point to the start of a ZSTD encoded frame +* `srcSize` must be at least as large as the frame header. A value greater than or equal +* to `ZSTD_frameHeaderSize_max` is guaranteed to be large enough in all cases. +* @return : decompressed size of the frame pointed to be `src` if known, otherwise +* - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined +* - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */ +ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize); + +/*! ZSTD_findDecompressedSize() : +* `src` should point the start of a series of ZSTD encoded and/or skippable frames +* `srcSize` must be the _exact_ size of this series +* (i.e. there should be a frame boundary exactly `srcSize` bytes after `src`) +* @return : the decompressed size of all data in the contained frames, as a 64-bit value _if known_ +* - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN +* - if an error occurred: ZSTD_CONTENTSIZE_ERROR +* +* note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. +* When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. +* In which case, it's necessary to use streaming mode to decompress data. +* Optionally, application can still use ZSTD_decompress() while relying on implied limits. +* (For example, data may be necessarily cut into blocks <= 16 KB). +* note 2 : decompressed size is always present when compression is done with ZSTD_compress() +* note 3 : decompressed size can be very large (64-bits value), +* potentially larger than what local system can handle as a single memory segment. +* In which case, it's necessary to use streaming mode to decompress data. +* note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. +* Always ensure result fits within application's authorized limits. +* Each application can set its own limits. +* note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to +* read each contained frame header. This is efficient as most of the data is skipped, +* however it does mean that all frame data must be present and valid. */ +ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); + + +/*************************************** +* Advanced compression functions +***************************************/ +/*! ZSTD_estimateCCtxSize() : + * Gives the amount of memory allocated for a ZSTD_CCtx given a set of compression parameters. + * `frameContentSize` is an optional parameter, provide `0` if unknown */ +ZSTDLIB_API size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams); + +/*! ZSTD_createCCtx_advanced() : + * Create a ZSTD compression context using external alloc and free functions */ +ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); + +/*! ZSTD_sizeofCCtx() : + * Gives the amount of memory used by a given ZSTD_CCtx */ +ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); + +typedef enum { + ZSTD_p_forceWindow, /* Force back-references to remain < windowSize, even when referencing Dictionary content (default:0) */ + ZSTD_p_forceRawDict /* Force loading dictionary in "content-only" mode (no header analysis) */ +} ZSTD_CCtxParameter; +/*! ZSTD_setCCtxParameter() : + * Set advanced parameters, selected through enum ZSTD_CCtxParameter + * @result : 0, or an error code (which can be tested with ZSTD_isError()) */ +ZSTDLIB_API size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value); + +/*! ZSTD_createCDict_byReference() : + * Create a digested dictionary for compression + * Dictionary content is simply referenced, and therefore stays in dictBuffer. + * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */ +ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); + +/*! ZSTD_createCDict_advanced() : + * Create a ZSTD_CDict using external alloc and free, and customized compression parameters */ +ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, unsigned byReference, + ZSTD_compressionParameters cParams, ZSTD_customMem customMem); + +/*! ZSTD_sizeof_CDict() : + * Gives the amount of memory used by a given ZSTD_sizeof_CDict */ +ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); + +/*! ZSTD_getCParams() : +* @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. +* `estimatedSrcSize` value is optional, select 0 if not known */ +ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); + +/*! ZSTD_getParams() : +* same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. +* All fields of `ZSTD_frameParameters` are set to default (0) */ +ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); + +/*! ZSTD_checkCParams() : +* Ensure param values remain within authorized range */ +ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); + +/*! ZSTD_adjustCParams() : +* optimize params for a given `srcSize` and `dictSize`. +* both values are optional, select `0` if unknown. */ +ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); + +/*! ZSTD_compress_advanced() : +* Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter */ +ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict,size_t dictSize, + ZSTD_parameters params); + +/*! ZSTD_compress_usingCDict_advanced() : +* Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */ +ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const ZSTD_CDict* cdict, ZSTD_frameParameters fParams); + + +/*--- Advanced decompression functions ---*/ + +/*! ZSTD_isFrame() : + * Tells if the content of `buffer` starts with a valid Frame Identifier. + * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. + * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. + * Note 3 : Skippable Frame Identifiers are considered valid. */ +ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size); + +/*! ZSTD_estimateDCtxSize() : + * Gives the potential amount of memory allocated to create a ZSTD_DCtx */ +ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); + +/*! ZSTD_createDCtx_advanced() : + * Create a ZSTD decompression context using external alloc and free functions */ +ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); + +/*! ZSTD_sizeof_DCtx() : + * Gives the amount of memory used by a given ZSTD_DCtx */ +ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); + +/*! ZSTD_createDDict_byReference() : + * Create a digested dictionary, ready to start decompression operation without startup delay. + * Dictionary content is simply referenced, and therefore stays in dictBuffer. + * It is important that dictBuffer outlives DDict, it must remain read accessible throughout the lifetime of DDict */ +ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); + +/*! ZSTD_createDDict_advanced() : + * Create a ZSTD_DDict using external alloc and free, optionally by reference */ +ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, + unsigned byReference, ZSTD_customMem customMem); + +/*! ZSTD_sizeof_DDict() : + * Gives the amount of memory used by a given ZSTD_DDict */ +ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); + +/*! ZSTD_getDictID_fromDict() : + * Provides the dictID stored within dictionary. + * if @return == 0, the dictionary is not conformant with Zstandard specification. + * It can still be loaded, but as a content-only dictionary. */ +ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); + +/*! ZSTD_getDictID_fromDDict() : + * Provides the dictID of the dictionary loaded into `ddict`. + * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. + * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ +ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); + +/*! ZSTD_getDictID_fromFrame() : + * Provides the dictID required to decompressed the frame stored within `src`. + * If @return == 0, the dictID could not be decoded. + * This could for one of the following reasons : + * - The frame does not require a dictionary to be decoded (most common case). + * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. + * Note : this use case also happens when using a non-conformant dictionary. + * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). + * - This is not a Zstandard frame. + * When identifying the exact failure cause, it's possible to use ZSTD_getFrameParams(), which will provide a more precise error code. */ +ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); + + +/******************************************************************** +* Advanced streaming functions +********************************************************************/ + +/*===== Advanced Streaming compression functions =====*/ +ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); +ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); /**< size of CStream is variable, depending primarily on compression level */ +ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct, a size of 0 means unknown. for a frame size of 0 use initCStream_advanced */ +ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< note: a dict will not be used if dict == NULL or dictSize < 8 */ +ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, + ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be 0 (meaning unknown). note: if the contentSizeFlag is set, pledgedSrcSize == 0 means the source size is actually 0 */ +ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); /**< note : cdict will just be referenced, and must outlive compression session */ +ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, unsigned long long pledgedSrcSize, ZSTD_frameParameters fParams); /**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters */ + +/*! ZSTD_resetCStream() : + * start a new compression job, using same parameters from previous job. + * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.. + * Note that zcs must be init at least once before using ZSTD_resetCStream(). + * pledgedSrcSize==0 means "srcSize unknown". + * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. + * @return : 0, or an error code (which can be tested using ZSTD_isError()) */ +ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); + + +/*===== Advanced Streaming decompression functions =====*/ +typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e; +ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); +ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); /**< note: a dict will not be used if dict == NULL or dictSize < 8 */ +ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue); +ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); /**< note : ddict will just be referenced, and must outlive decompression session */ +ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */ +ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); + + +/********************************************************************* +* Buffer-less and synchronous inner streaming functions +* +* This is an advanced API, giving full control over buffer management, for users which need direct control over memory. +* But it's also a complex one, with many restrictions (documented below). +* Prefer using normal streaming API for an easier experience +********************************************************************* */ + +/** + Buffer-less streaming compression (synchronous mode) + + A ZSTD_CCtx object is required to track streaming operations. + Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. + ZSTD_CCtx object can be re-used multiple times within successive compression operations. + + Start by initializing a context. + Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, + or ZSTD_compressBegin_advanced(), for finer parameter control. + It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() + + Then, consume your input using ZSTD_compressContinue(). + There are some important considerations to keep in mind when using this advanced function : + - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only. + - Interface is synchronous : input is consumed entirely and produce 1+ (or more) compressed blocks. + - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. + Worst case evaluation is provided by ZSTD_compressBound(). + ZSTD_compressContinue() doesn't guarantee recover after a failed compression. + - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). + It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) + - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. + In which case, it will "discard" the relevant memory section from its history. + + Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. + It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. + Without last block mark, frames will be considered unfinished (corrupted) by decoders. + + `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress some new frame. +*/ + +/*===== Buffer-less streaming compression functions =====*/ +ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); +ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); +ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be 0 (meaning unknown). note: if the contentSizeFlag is set, pledgedSrcSize == 0 means the source size is actually 0 */ +ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */ +ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); /* compression parameters are already set within cdict. pledgedSrcSize=0 means null-size */ +ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**< note: if pledgedSrcSize can be 0, indicating unknown size. if it is non-zero, it must be accurate. for 0 size frames, use compressBegin_advanced */ + +ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); +ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); + + + +/*- + Buffer-less streaming decompression (synchronous mode) + + A ZSTD_DCtx object is required to track streaming operations. + Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. + A ZSTD_DCtx object can be re-used multiple times. + + First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams(). + It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame, + such as the minimum rolling buffer size to allocate to decompress data (`windowSize`), + and the dictionary ID used. + (Note : content size is optional, it may not be present. 0 means : content size unknown). + Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information. + As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation. + Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB. + Frame parameters are extracted from the beginning of the compressed frame. + Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes. + @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled. + >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. + errorCode, which can be tested using ZSTD_isError(). + + Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict(). + Alternatively, you can copy a prepared context, using ZSTD_copyDCtx(). + + Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. + ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). + ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. + + @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). + It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item. + It can also be an error code, which can be tested with ZSTD_isError(). + + ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`. + They should preferably be located contiguously, prior to current block. + Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters. + ZSTD_decompressContinue() is very sensitive to contiguity, + if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, + or that previous contiguous segment is large enough to properly handle maximum back-reference. + + A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. + Context can then be reset to start a new decompression. + + Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). + This information is not required to properly decode a frame. + + == Special case : skippable frames == + + Skippable frames allow integration of user-defined data into a flow of concatenated frames. + Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frames is as follows : + a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F + b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits + c) Frame Content - any content (User Data) of length equal to Frame Size + For skippable frames ZSTD_decompressContinue() always returns 0. + For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable. + Note : If fparamsPtr->frameContentSize==0, it is ambiguous: the frame might actually be a Zstd encoded frame with no content. + For purposes of decompression, it is valid in both cases to skip the frame using + ZSTD_findFrameCompressedSize to find its size in bytes. + It also returns Frame Size as fparamsPtr->frameContentSize. +*/ + +typedef struct { + unsigned long long frameContentSize; + unsigned windowSize; + unsigned dictID; + unsigned checksumFlag; +} ZSTD_frameParams; + +/*===== Buffer-less streaming decompression functions =====*/ +ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input, see details below */ +ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); +ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); +ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); +ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); +ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); +typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; +ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); + +/** + Block functions + + Block functions produce and decode raw zstd blocks, without frame metadata. + Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes). + User will have to take in charge required information to regenerate data, such as compressed and content sizes. + + A few rules to respect : + - Compressing and decompressing require a context structure + + Use ZSTD_createCCtx() and ZSTD_createDCtx() + - It is necessary to init context before starting + + compression : any ZSTD_compressBegin*() variant, including with dictionary + + decompression : any ZSTD_decompressBegin*() variant, including with dictionary + + copyCCtx() and copyDCtx() can be used too + - Block size is limited, it must be <= ZSTD_getBlockSizeMax() <= ZSTD_BLOCKSIZE_ABSOLUTEMAX + + If input is larger than a block size, it's necessary to split input data into multiple blocks + + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead. + Frame metadata is not that costly, and quickly becomes negligible as source size grows larger. + - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero. + In which case, nothing is produced into `dst`. + + User must test for such outcome and deal directly with uncompressed data + + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!! + + In case of multiple successive blocks, should some of them be uncompressed, + decoder must be informed of their existence in order to follow proper history. + Use ZSTD_insertBlock() for such a case. +*/ + +#define ZSTD_BLOCKSIZE_ABSOLUTEMAX (128 * 1024) /* define, for static allocation */ +/*===== Raw zstd block functions =====*/ +ZSTDLIB_API size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx); +ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); +ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); +ZSTDLIB_API size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); /**< insert block into `dctx` history. Useful for uncompressed blocks */ + + +#endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */ + +#if defined (__cplusplus) +} +#endif |