Path: blob/master/scene/resources/compressed_texture.h
21136 views
/**************************************************************************/1/* compressed_texture.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/io/resource_loader.h"33#include "scene/resources/texture.h"34#include "servers/rendering/rendering_server.h"3536class BitMap;3738class CompressedTexture2D : public Texture2D {39GDCLASS(CompressedTexture2D, Texture2D);4041public:42enum DataFormat {43DATA_FORMAT_IMAGE,44DATA_FORMAT_PNG,45DATA_FORMAT_WEBP,46DATA_FORMAT_BASIS_UNIVERSAL,47};4849enum {50FORMAT_VERSION = 151};5253enum FormatBits {54FORMAT_BIT_STREAM = 1 << 22,55FORMAT_BIT_HAS_MIPMAPS = 1 << 23,56FORMAT_BIT_DETECT_3D = 1 << 24,57//FORMAT_BIT_DETECT_SRGB = 1 << 25,58FORMAT_BIT_DETECT_NORMAL = 1 << 26,59FORMAT_BIT_DETECT_ROUGNESS = 1 << 27,60};6162private:63String path_to_file;64mutable RID texture;65Image::Format format = Image::FORMAT_L8;66int w = 0;67int h = 0;68mutable Ref<BitMap> alpha_cache;6970Error _load_data(const String &p_path, int &r_width, int &r_height, Ref<Image> &image, bool &r_request_3d, bool &r_request_normal, bool &r_request_roughness, int &mipmap_limit, int p_size_limit = 0);71virtual void reload_from_file() override;7273static void _requested_3d(void *p_ud);74static void _requested_roughness(void *p_ud, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);75static void _requested_normal(void *p_ud);7677protected:78static void _bind_methods();7980public:81static Ref<Image> load_image_from_file(Ref<FileAccess> p_file, int p_size_limit);8283typedef void (*TextureFormatRequestCallback)(const Ref<CompressedTexture2D> &);84typedef void (*TextureFormatRoughnessRequestCallback)(const Ref<CompressedTexture2D> &, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);8586static TextureFormatRequestCallback request_3d_callback;87static TextureFormatRoughnessRequestCallback request_roughness_callback;88static TextureFormatRequestCallback request_normal_callback;8990Image::Format get_format() const;91Error load(const String &p_path);92String get_load_path() const;9394int get_width() const override;95int get_height() const override;96virtual RID get_rid() const override;9798virtual void set_path(const String &p_path, bool p_take_over) override;99100virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;101virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;102virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = true) const override;103104virtual bool has_alpha() const override;105bool is_pixel_opaque(int p_x, int p_y) const override;106107virtual Ref<Image> get_image() const override;108109~CompressedTexture2D();110};111112class ResourceFormatLoaderCompressedTexture2D : public ResourceFormatLoader {113GDSOFTCLASS(ResourceFormatLoaderCompressedTexture2D, ResourceFormatLoader);114115public:116virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;117virtual void get_recognized_extensions(List<String> *p_extensions) const override;118virtual bool handles_type(const String &p_type) const override;119virtual String get_resource_type(const String &p_path) const override;120};121122class CompressedTextureLayered : public TextureLayered {123GDCLASS(CompressedTextureLayered, TextureLayered);124125public:126enum DataFormat {127DATA_FORMAT_IMAGE,128DATA_FORMAT_PNG,129DATA_FORMAT_WEBP,130DATA_FORMAT_BASIS_UNIVERSAL,131};132133enum {134FORMAT_VERSION = 1135};136137enum FormatBits {138FORMAT_BIT_STREAM = 1 << 22,139FORMAT_BIT_HAS_MIPMAPS = 1 << 23,140};141142private:143Error _load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit = 0);144String path_to_file;145mutable RID texture;146Image::Format format = Image::FORMAT_L8;147int w = 0;148int h = 0;149int layers = 0;150bool mipmaps = false;151LayeredType layered_type = LayeredType::LAYERED_TYPE_2D_ARRAY;152153virtual void reload_from_file() override;154155protected:156static void _bind_methods();157158public:159Image::Format get_format() const override;160Error load(const String &p_path);161String get_load_path() const;162virtual LayeredType get_layered_type() const override;163164int get_width() const override;165int get_height() const override;166int get_layers() const override;167virtual bool has_mipmaps() const override;168virtual RID get_rid() const override;169170virtual void set_path(const String &p_path, bool p_take_over) override;171172virtual Ref<Image> get_layer_data(int p_layer) const override;173174CompressedTextureLayered(LayeredType p_layered_type);175~CompressedTextureLayered();176};177178class ResourceFormatLoaderCompressedTextureLayered : public ResourceFormatLoader {179GDSOFTCLASS(ResourceFormatLoaderCompressedTextureLayered, ResourceFormatLoader);180181public:182virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;183virtual void get_recognized_extensions(List<String> *p_extensions) const override;184virtual bool handles_type(const String &p_type) const override;185virtual String get_resource_type(const String &p_path) const override;186};187188class CompressedTexture2DArray : public CompressedTextureLayered {189GDCLASS(CompressedTexture2DArray, CompressedTextureLayered)190public:191CompressedTexture2DArray() :192CompressedTextureLayered(LAYERED_TYPE_2D_ARRAY) {}193};194195class CompressedCubemap : public CompressedTextureLayered {196GDCLASS(CompressedCubemap, CompressedTextureLayered);197198public:199CompressedCubemap() :200CompressedTextureLayered(LAYERED_TYPE_CUBEMAP) {}201};202203class CompressedCubemapArray : public CompressedTextureLayered {204GDCLASS(CompressedCubemapArray, CompressedTextureLayered);205206public:207CompressedCubemapArray() :208CompressedTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}209};210211class CompressedTexture3D : public Texture3D {212GDCLASS(CompressedTexture3D, Texture3D);213214public:215enum DataFormat {216DATA_FORMAT_IMAGE,217DATA_FORMAT_PNG,218DATA_FORMAT_WEBP,219DATA_FORMAT_BASIS_UNIVERSAL,220};221222enum {223FORMAT_VERSION = 1224};225226enum FormatBits {227FORMAT_BIT_STREAM = 1 << 22,228FORMAT_BIT_HAS_MIPMAPS = 1 << 23,229};230231private:232Error _load_data(const String &p_path, Vector<Ref<Image>> &r_data, Image::Format &r_format, int &r_width, int &r_height, int &r_depth, bool &r_mipmaps);233String path_to_file;234mutable RID texture;235Image::Format format = Image::FORMAT_L8;236int w = 0;237int h = 0;238int d = 0;239bool mipmaps = false;240241virtual void reload_from_file() override;242243protected:244static void _bind_methods();245246public:247Image::Format get_format() const override;248Error load(const String &p_path);249String get_load_path() const;250251int get_width() const override;252int get_height() const override;253int get_depth() const override;254virtual bool has_mipmaps() const override;255virtual RID get_rid() const override;256257virtual void set_path(const String &p_path, bool p_take_over) override;258259virtual Vector<Ref<Image>> get_data() const override;260261~CompressedTexture3D();262};263264class ResourceFormatLoaderCompressedTexture3D : public ResourceFormatLoader {265GDSOFTCLASS(ResourceFormatLoaderCompressedTexture3D, ResourceFormatLoader);266267public:268virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;269virtual void get_recognized_extensions(List<String> *p_extensions) const override;270virtual bool handles_type(const String &p_type) const override;271virtual String get_resource_type(const String &p_path) const override;272};273274275