Skip to content

Commit 5612995

Browse files
committed
Implemented texture streaming
1 parent 895630e commit 5612995

9 files changed

Lines changed: 459 additions & 0 deletions

modules/streaming_texture/SCsub

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
from misc.utility.scons_hints import *
3+
4+
Import("env")
5+
Import("env_modules")
6+
7+
env_st = env_modules.Clone()
8+
9+
# Godot source files
10+
11+
module_obj = []
12+
13+
env_st.add_source_files(module_obj, "*.cpp")
14+
env.modules_sources += module_obj
15+
16+
# Needed to force rebuilding the module files when the thirdparty library is updated.
17+
env.Depends(module_obj, thirdparty_obj)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**************************************************************************/
2+
/* register_types.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include "register_types.h"
32+
33+
#include "streaming_image_loader.h"
34+
35+
static Ref<StreamingImageLoader> streaming_image_loader;
36+
37+
void initialize_streaming_texture_module(ModuleInitializationLevel p_level) {
38+
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
39+
return;
40+
}
41+
42+
streaming_image_loader.instantiate();
43+
ResourceLoader::add_resource_format_loader(streaming_image_loader);
44+
}
45+
46+
void uninitialize_streaming_texture_module(ModuleInitializationLevel p_level) {
47+
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
48+
return;
49+
}
50+
51+
ResourceLoader::remove_resource_format_loader(streaming_image_loader);
52+
streaming_image_loader.unref();
53+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**************************************************************************/
2+
/* register_types.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "modules/register_module_types.h"
34+
35+
void initialize_streaming_texture_module(ModuleInitializationLevel p_level);
36+
void uninitialize_streaming_texture_module(ModuleInitializationLevel p_level);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**************************************************************************/
2+
/* streaming_image.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include "streaming_image.h"
32+
33+
void StreamingImage::_bind_methods() {
34+
35+
}
36+
37+
int StreamingImage::get_mipmap_size(int p_mipmap) const {
38+
return Image::get_image_mipmap_offset(width, height, format, p_mipmap);
39+
}
40+
41+
Error StreamingImage::load_mipmap(int p_mipmap, void *p_buffer, size_t p_buffer_size) {
42+
43+
}
44+
45+
int StreamingImage::get_width() const {
46+
return width;
47+
}
48+
49+
int StreamingImage::get_height() const {
50+
return height;
51+
}
52+
53+
Size2i StreamingImage::get_size() const {
54+
return Size2i(width, height);
55+
}
56+
57+
int StreamingImage::get_mipmap_count() const {
58+
return Image::get_image_required_mipmaps(width, height, format);
59+
}
60+
61+
int StreamingImage::get_current_mipmap() const {
62+
return current_mipmap;
63+
}
64+
65+
Image::Format StreamingImage::get_format() const {
66+
return format;
67+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**************************************************************************/
2+
/* streaming_texture.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "core/io/image.h"
34+
35+
class StreamingImage : public Resource {
36+
GDCLASS(StreamingImage, Resource);
37+
38+
friend class StreamingTextureServer;
39+
40+
public:
41+
enum {
42+
MAX_WIDTH = (1 << 24), // Force a limit somehow.
43+
MAX_HEIGHT = (1 << 24), // Force a limit somehow.
44+
MAX_PIXELS = 268435456 // 16384 ^ 2
45+
};
46+
47+
protected:
48+
static void _bind_methods();
49+
50+
private:
51+
Image::Format format = Image::FORMAT_L8;
52+
int width;
53+
int height;
54+
int current_mipmap;
55+
56+
int get_mipmap_size(int p_mipmap) const;
57+
Error load_mipmap(int p_mipmap, void *p_buffer, size_t p_buffer_size);
58+
59+
public:
60+
int get_width() const;
61+
int get_height() const;
62+
Size2i get_size() const;
63+
int get_mipmap_count() const;
64+
int get_current_mipmap() const;
65+
66+
Image::Format get_format() const;
67+
68+
StreamingImage() = default;
69+
};
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**************************************************************************/
2+
/* streaming_image_loader.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include "streaming_image_loader.h"
32+
#include "core/io/file_access.h"
33+
34+
Ref<Resource> StreamingImageLoader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
35+
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
36+
if (f.is_null()) {
37+
if (r_error) {
38+
*r_error = ERR_CANT_OPEN;
39+
}
40+
return Ref<Resource>();
41+
}
42+
43+
uint8_t header[4] = { 0, 0, 0, 0 };
44+
f->get_buffer(header, 4);
45+
46+
bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'S' || header[3] != 'I';
47+
if (unrecognized) {
48+
if (r_error) {
49+
*r_error = ERR_FILE_UNRECOGNIZED;
50+
}
51+
ERR_FAIL_V(Ref<Resource>());
52+
}
53+
54+
Ref<Image> image;
55+
image.instantiate();
56+
57+
image->create_empty(2, 2, false, Image::FORMAT_RGB8);
58+
image->set_pixel(0, 0, Color(1.0, 0.0, 0.0));
59+
image->set_pixel(1, 0, Color(0.0, 1.0, 0.0));
60+
image->set_pixel(0, 1, Color(0.0, 0.0, 1.0));
61+
image->set_pixel(1, 1, Color(1.0, 1.0, 1.0));
62+
63+
if (r_error) {
64+
*r_error = OK;
65+
}
66+
67+
return image;
68+
}
69+
70+
void StreamingImageLoader::get_recognized_extensions(List<String> *p_extensions) const {
71+
p_extensions->push_back("streaming_image");
72+
}
73+
74+
bool StreamingImageLoader::handles_type(const String &p_type) const {
75+
return p_type == "Image";
76+
}
77+
78+
String StreamingImageLoader::get_resource_type(const String &p_path) const {
79+
return p_path.get_extension().to_lower() == "streaming_image" ? "Image" : String();
80+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**************************************************************************/
2+
/* streaming_image_loader.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "core/io/resource_loader.h"
34+
#include "core/io/image.h"
35+
36+
class StreamingImageLoader : public ResourceFormatLoader {
37+
GDCLASS(StreamingImageLoader, ResourceFormatImporter);
38+
39+
public:
40+
virtual 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);
41+
virtual void get_recognized_extensions(List<String> *p_extensions) const;
42+
virtual bool handles_type(const String &p_type) const;
43+
virtual String get_resource_type(const String &p_path) const;
44+
45+
StreamingImageLoader();
46+
virtual ~StreamingImageLoader();
47+
};

0 commit comments

Comments
 (0)