summaryrefslogtreecommitdiff
path: root/scene
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2017-08-18 19:16:32 -0300
committerJuan Linietsky <reduzio@gmail.com>2017-08-18 19:16:32 -0300
commit3f8efdef498c17df4cb6d697fe3f1a5358894ced (patch)
tree879cd9faced95230ee5e48104b37be5ec596a545 /scene
parentcbcf40bd31649d18b24952aec0051cdfbfa78606 (diff)
ProceduralSky now regenerates in a thread (the second time, first does not), closes #9138
Diffstat (limited to 'scene')
-rw-r--r--scene/resources/sky_box.cpp63
-rw-r--r--scene/resources/sky_box.h11
2 files changed, 69 insertions, 5 deletions
diff --git a/scene/resources/sky_box.cpp b/scene/resources/sky_box.cpp
index 5750960845..cc3cfe4dc0 100644
--- a/scene/resources/sky_box.cpp
+++ b/scene/resources/sky_box.cpp
@@ -128,7 +128,7 @@ void ProceduralSky::_radiance_changed() {
VS::get_singleton()->sky_set_texture(sky, texture, size[get_radiance_size()]);
}
-void ProceduralSky::_update_sky() {
+Ref<Image> ProceduralSky::_generate_sky() {
update_queued = false;
@@ -215,9 +215,7 @@ void ProceduralSky::_update_sky() {
image.instance();
image->create(w, h, false, Image::FORMAT_RGBE9995, imgdata);
- VS::get_singleton()->texture_allocate(texture, w, h, Image::FORMAT_RGBE9995, VS::TEXTURE_FLAG_FILTER | VS::TEXTURE_FLAG_REPEAT);
- VS::get_singleton()->texture_set_data(texture, image);
- _radiance_changed();
+ return image;
}
void ProceduralSky::set_sky_top_color(const Color &p_sky_top) {
@@ -385,6 +383,33 @@ RID ProceduralSky::get_rid() const {
return sky;
}
+void ProceduralSky::_update_sky() {
+
+ bool use_thread = true;
+ if (first_time) {
+ use_thread = false;
+ first_time = false;
+ }
+#ifdef NO_THREADS
+ use_thread = false;
+#endif
+ if (use_thread) {
+
+ if (!sky_thread) {
+ sky_thread = Thread::create(_thread_function, this);
+ regen_queued = false;
+ } else {
+ regen_queued = true;
+ }
+
+ } else {
+ Ref<Image> image = _generate_sky();
+ VS::get_singleton()->texture_allocate(texture, image->get_width(), image->get_height(), Image::FORMAT_RGBE9995, VS::TEXTURE_FLAG_FILTER | VS::TEXTURE_FLAG_REPEAT);
+ VS::get_singleton()->texture_set_data(texture, image);
+ _radiance_changed();
+ }
+}
+
void ProceduralSky::_queue_update() {
if (update_queued)
@@ -394,6 +419,26 @@ void ProceduralSky::_queue_update() {
call_deferred("_update_sky");
}
+void ProceduralSky::_thread_done(const Ref<Image> &image) {
+
+ VS::get_singleton()->texture_allocate(texture, image->get_width(), image->get_height(), Image::FORMAT_RGBE9995, VS::TEXTURE_FLAG_FILTER | VS::TEXTURE_FLAG_REPEAT);
+ VS::get_singleton()->texture_set_data(texture, image);
+ _radiance_changed();
+ Thread::wait_to_finish(sky_thread);
+ memdelete(sky_thread);
+ sky_thread = NULL;
+ if (regen_queued) {
+ sky_thread = Thread::create(_thread_function, this);
+ regen_queued = false;
+ }
+}
+
+void ProceduralSky::_thread_function(void *p_ud) {
+
+ ProceduralSky *psky = (ProceduralSky *)p_ud;
+ psky->call_deferred("_thread_done", psky->_generate_sky());
+}
+
void ProceduralSky::_bind_methods() {
ClassDB::bind_method(D_METHOD("_update_sky"), &ProceduralSky::_update_sky);
@@ -446,6 +491,8 @@ void ProceduralSky::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_texture_size", "size"), &ProceduralSky::set_texture_size);
ClassDB::bind_method(D_METHOD("get_texture_size"), &ProceduralSky::get_texture_size);
+ ClassDB::bind_method(D_METHOD("_thread_done", "image"), &ProceduralSky::_thread_done);
+
ADD_GROUP("Sky", "sky_");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sky_top_color"), "set_sky_top_color", "get_sky_top_color");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sky_horizon_color"), "set_sky_horizon_color", "get_sky_horizon_color");
@@ -503,12 +550,20 @@ ProceduralSky::ProceduralSky() {
sun_energy = 16;
texture_size = TEXTURE_SIZE_1024;
+ sky_thread = NULL;
+ regen_queued = false;
+ first_time = true;
_queue_update();
}
ProceduralSky::~ProceduralSky() {
+ if (sky_thread) {
+ Thread::wait_to_finish(sky_thread);
+ memdelete(sky_thread);
+ sky_thread = NULL;
+ }
VS::get_singleton()->free(sky);
VS::get_singleton()->free(texture);
}
diff --git a/scene/resources/sky_box.h b/scene/resources/sky_box.h
index 8298d1b3c0..9bd60cddaf 100644
--- a/scene/resources/sky_box.h
+++ b/scene/resources/sky_box.h
@@ -30,8 +30,8 @@
#ifndef Sky_H
#define Sky_H
+#include "os/thread.h"
#include "scene/resources/texture.h"
-
class Sky : public Resource {
GDCLASS(Sky, Resource);
@@ -97,6 +97,7 @@ public:
};
private:
+ Thread *sky_thread;
Color sky_top_color;
Color sky_horizon_color;
float sky_curve;
@@ -121,12 +122,20 @@ private:
RID texture;
bool update_queued;
+ bool regen_queued;
+
+ bool first_time;
+
+ void _thread_done(const Ref<Image> &p_image);
+ static void _thread_function(void *p_ud);
protected:
static void _bind_methods();
virtual void _radiance_changed();
+ Ref<Image> _generate_sky();
void _update_sky();
+
void _queue_update();
public: