2022-06-06 22:04:36 +02:00
|
|
|
|
|
|
|
#include "mm_material.h"
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
#include "mm_node.h"
|
|
|
|
#include "mm_node_universal_property.h"
|
|
|
|
#include "modules/thread_pool/thread_pool.h"
|
|
|
|
#include "modules/thread_pool/thread_pool_job.h"
|
|
|
|
|
|
|
|
Vector2 MMMaterial::get_image_size() {
|
2022-06-06 22:04:36 +02:00
|
|
|
return image_size;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::set_image_size(const Vector2 &val) {
|
2022-06-06 22:04:36 +02:00
|
|
|
image_size = val;
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:43:42 +02:00
|
|
|
Vector<Variant> MMMaterial::get_nodes() {
|
|
|
|
Vector<Variant> r;
|
|
|
|
for (int i = 0; i < nodes.size(); i++) {
|
|
|
|
r.push_back(nodes[i].get_ref_ptr());
|
|
|
|
}
|
|
|
|
return r;
|
2022-06-06 22:04:36 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 09:43:42 +02:00
|
|
|
void MMMaterial::set_nodes(const Vector<Variant> &val) {
|
|
|
|
nodes.clear();
|
|
|
|
for (int i = 0; i < val.size(); i++) {
|
|
|
|
Ref<MMNode> e = Ref<MMNode>(val[i]);
|
|
|
|
nodes.push_back(e);
|
|
|
|
}
|
2022-06-06 22:04:36 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
bool MMMaterial::get_initialized() const {
|
2022-06-06 22:04:36 +02:00
|
|
|
return initialized;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::set_initialized(const bool val) {
|
2022-06-06 22:04:36 +02:00
|
|
|
initialized = val;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
bool MMMaterial::get_rendering() const {
|
2022-06-06 22:04:36 +02:00
|
|
|
return rendering;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::set_rendering(const bool val) {
|
2022-06-06 22:04:36 +02:00
|
|
|
rendering = val;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
bool MMMaterial::get_queued_render() const {
|
2022-06-06 22:04:36 +02:00
|
|
|
return queued_render;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::set_queued_render(const bool val) {
|
2022-06-06 22:04:36 +02:00
|
|
|
queued_render = val;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::initialize() {
|
2022-06-06 22:04:36 +02:00
|
|
|
if (!initialized) {
|
|
|
|
initialized = true;
|
2022-06-07 03:41:22 +02:00
|
|
|
job->setup(this, "_thread_func");
|
2022-06-06 22:04:36 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
for (int i = 0; i < nodes.size(); ++i) {
|
|
|
|
Ref<MMNode> n = nodes[i];
|
|
|
|
n->init_properties();
|
|
|
|
n->connect("changed", this, "on_node_changed");
|
2022-06-06 22:04:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::add_node(const Ref<MMNode> &node) {
|
|
|
|
nodes.push_back(node);
|
|
|
|
Ref<MMNode> n = node;
|
|
|
|
n->connect("changed", this, "on_node_changed");
|
2022-06-06 22:04:36 +02:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::remove_node(const Ref<MMNode> &node) {
|
|
|
|
if (!node.is_valid()) {
|
2022-06-06 22:04:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
for (int i = 0; i < node->output_properties.size(); ++i) {
|
|
|
|
Ref<MMNodeUniversalProperty> op = node->output_properties[i];
|
|
|
|
|
|
|
|
for (int j = 0; j < nodes.size(); ++j) {
|
|
|
|
Ref<MMNode> n = nodes[j];
|
|
|
|
|
|
|
|
if (n.is_valid()) {
|
|
|
|
for (int k = 0; k < n->input_properties.size(); ++k) {
|
|
|
|
Ref<MMNodeUniversalProperty> ip = n->input_properties[k];
|
|
|
|
|
|
|
|
if (ip->get_input_property() == op) {
|
|
|
|
ip->set_input_property(Ref<MMNodeUniversalProperty>());
|
2022-06-06 22:04:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes.erase(node);
|
2022-06-07 03:41:22 +02:00
|
|
|
|
|
|
|
Ref<MMNode> nn = node;
|
|
|
|
|
|
|
|
nn->disconnect("changed", this, "on_node_changed");
|
2022-06-06 22:04:36 +02:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::render() {
|
2022-06-06 22:04:36 +02:00
|
|
|
initialize();
|
|
|
|
|
|
|
|
if (rendering) {
|
|
|
|
queued_render = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (USE_THREADS) {
|
|
|
|
render_threaded();
|
2022-06-07 09:43:42 +02:00
|
|
|
} else {
|
2022-06-06 22:04:36 +02:00
|
|
|
render_non_threaded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::render_non_threaded() {
|
2022-06-06 22:04:36 +02:00
|
|
|
bool did_render = true;
|
2022-06-07 03:41:22 +02:00
|
|
|
Ref<MMMaterial> self = Ref<MMMaterial>(this);
|
2022-06-06 22:04:36 +02:00
|
|
|
|
|
|
|
while (did_render) {
|
|
|
|
did_render = false;
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
for (int i = 0; i < nodes.size(); ++i) {
|
|
|
|
Ref<MMNode> n = nodes[i];
|
|
|
|
|
|
|
|
if (n.is_valid() && n->render(self)) {
|
2022-06-06 22:04:36 +02:00
|
|
|
did_render = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::render_threaded() {
|
|
|
|
job->set_cancelled(false);
|
2022-06-06 22:04:36 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
if (!ThreadPool::get_singleton()->has_job(job)) {
|
|
|
|
ThreadPool::get_singleton()->add_job(job);
|
2022-06-06 22:04:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::_thread_func() {
|
|
|
|
if (job->get_cancelled()) {
|
2022-06-06 22:04:36 +02:00
|
|
|
rendering = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rendering = true;
|
2022-06-07 03:41:22 +02:00
|
|
|
job->set_cancelled(false);
|
2022-06-06 22:04:36 +02:00
|
|
|
bool did_render = true;
|
2022-06-07 03:41:22 +02:00
|
|
|
Ref<MMMaterial> self = Ref<MMMaterial>(this);
|
2022-06-06 22:04:36 +02:00
|
|
|
|
|
|
|
while (did_render) {
|
|
|
|
did_render = false;
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
for (int i = 0; i < nodes.size(); ++i) {
|
|
|
|
Ref<MMNode> n = nodes[i];
|
|
|
|
|
|
|
|
if (n.is_valid() && n->render(self)) {
|
2022-06-06 22:04:36 +02:00
|
|
|
did_render = true;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
if (job->get_cancelled()) {
|
2022-06-06 22:04:36 +02:00
|
|
|
rendering = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rendering = false;
|
|
|
|
|
|
|
|
if (queued_render) {
|
|
|
|
queued_render = false;
|
|
|
|
_thread_func();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::cancel_render_and_wait() {
|
2022-06-06 22:04:36 +02:00
|
|
|
if (rendering) {
|
2022-06-07 03:41:22 +02:00
|
|
|
ThreadPool::get_singleton()->cancel_job_wait(job);
|
|
|
|
job->set_cancelled(false);
|
2022-06-06 22:04:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::on_node_changed() {
|
2022-06-06 22:04:36 +02:00
|
|
|
emit_changed();
|
|
|
|
call_deferred("render");
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
MMMaterial::MMMaterial() {
|
|
|
|
USE_THREADS = true;
|
2022-06-06 22:04:36 +02:00
|
|
|
image_size = Vector2(128, 128);
|
|
|
|
initialized = false;
|
|
|
|
rendering = false;
|
|
|
|
queued_render = false;
|
2022-06-07 03:41:22 +02:00
|
|
|
job.instance();
|
2022-06-06 22:04:36 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
MMMaterial::~MMMaterial() {
|
2022-06-06 22:04:36 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
void MMMaterial::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_image_size"), &MMMaterial::get_image_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_image_size", "value"), &MMMaterial::set_image_size);
|
2022-06-06 22:04:36 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "image_size"), "set_image_size", "get_image_size");
|
|
|
|
|
2022-06-07 09:43:42 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_nodes"), &MMMaterial::get_nodes);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_nodes", "value"), &MMMaterial::set_nodes);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_NONE, "17/17:MMNode", PROPERTY_USAGE_DEFAULT, "MMNode"), "set_nodes", "get_nodes");
|
2022-06-06 22:04:36 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_initialized"), &MMMaterial::get_initialized);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_initialized", "value"), &MMMaterial::set_initialized);
|
2022-06-07 09:43:42 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "initialized", PROPERTY_HINT_NONE, "", 0), "set_initialized", "get_initialized");
|
2022-06-06 22:04:36 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_rendering"), &MMMaterial::get_rendering);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_rendering", "value"), &MMMaterial::set_rendering);
|
2022-06-07 09:43:42 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rendering", PROPERTY_HINT_NONE, "", 0), "set_rendering", "get_rendering");
|
2022-06-06 22:04:36 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_queued_render"), &MMMaterial::get_queued_render);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_queued_render", "value"), &MMMaterial::set_queued_render);
|
2022-06-07 09:43:42 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "queued_render", PROPERTY_HINT_NONE, "", 0), "set_queued_render", "get_queued_render");
|
2022-06-06 22:04:36 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("initialize"), &MMMaterial::initialize);
|
2022-06-07 09:43:42 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("add_node", "node"), &MMMaterial::add_node);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_node", "node"), &MMMaterial::remove_node);
|
2022-06-07 09:43:42 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("render"), &MMMaterial::render);
|
|
|
|
ClassDB::bind_method(D_METHOD("render_non_threaded"), &MMMaterial::render_non_threaded);
|
|
|
|
ClassDB::bind_method(D_METHOD("render_threaded"), &MMMaterial::render_threaded);
|
2022-06-07 09:43:42 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_thread_func"), &MMMaterial::_thread_func);
|
2022-06-07 09:43:42 +02:00
|
|
|
|
2022-06-07 03:41:22 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("cancel_render_and_wait"), &MMMaterial::cancel_render_and_wait);
|
|
|
|
ClassDB::bind_method(D_METHOD("on_node_changed"), &MMMaterial::on_node_changed);
|
2022-06-06 22:04:36 +02:00
|
|
|
}
|