Implemented duplicate()

master
Marc Gilleron 2020-02-23 02:50:04 +00:00
parent 3a579dfb3e
commit c87dc49046
4 changed files with 67 additions and 4 deletions

View File

@ -242,3 +242,17 @@ void ProgramGraph::debug_print_dot_file(String file_path) const {
f->close();
memdelete(f);
}
void ProgramGraph::copy_from(const ProgramGraph &other) {
clear();
_next_node_id = other._next_node_id;
_nodes.reserve(other._nodes.size());
for (auto it = other._nodes.begin(); it != other._nodes.end(); ++it) {
const Node *other_node = it->second;
Node *node = memnew(Node);
node->id = other_node->id;
node->inputs = other_node->inputs;
node->outputs = other_node->outputs;
_nodes.insert(std::make_pair(node->id, node));
}
}

View File

@ -48,6 +48,8 @@ public:
void find_dependencies(uint32_t end_node_id, std::vector<uint32_t> &order) const;
void find_terminal_nodes(std::vector<uint32_t> &node_ids) const;
void copy_from(const ProgramGraph &other);
void debug_print_dot_file(String file_path) const;
private:

View File

@ -159,9 +159,12 @@ VoxelGeneratorGraph::NodeTypeDB::NodeTypeDB() {
}
VoxelGeneratorGraph::VoxelGeneratorGraph() {
clear();
clear_bounds();
_bounds.min = Vector3i(-128);
_bounds.max = Vector3i(128);
// TODO Remove this, it's for testing
load_waves_preset();
compile();
}
@ -181,7 +184,7 @@ void VoxelGeneratorGraph::clear() {
_graph.clear();
_program.clear();
_memory.clear();
_memory.resize(8, 0);
}
uint32_t VoxelGeneratorGraph::create_node(NodeTypeID type_id) {
@ -743,6 +746,10 @@ float VoxelGeneratorGraph::generate_single(const Vector3i &position) {
break;
}
#ifdef DEBUG_ENABLED
CRASH_COND(_memory.size() == 0);
#endif
ArraySlice<float> memory(_memory, 0, _memory.size() / 2);
memory[0] = position.x;
memory[1] = position.y;
@ -1116,6 +1123,43 @@ void VoxelGeneratorGraph::set_box_bounds(Vector3i min, Vector3i max, float sdf_v
_bounds.sdf_value1 = type_value;
}
Ref<Resource> VoxelGeneratorGraph::duplicate(bool p_subresources) const {
Ref<VoxelGeneratorGraph> d;
d.instance();
d->_channel = _channel;
d->_iso_scale = _iso_scale;
d->_bounds = _bounds;
d->_graph.copy_from(_graph);
// Program not copied, as it may contain pointers to the resources we are duplicating
const uint32_t *key = nullptr;
while ((key = _nodes.next(key))) {
Node *node = _nodes.get(*key);
Node *node_copy = memnew(Node);
node_copy->type = node->type;
node_copy->gui_position = node->gui_position;
node_copy->params = node->params;
if (p_subresources) {
for (auto it = node_copy->params.begin(); it != node_copy->params.end(); ++it) {
Object *obj = *it;
if (obj != nullptr) {
Resource *res = Object::cast_to<Resource>(obj);
if (res != nullptr) {
*it = res->duplicate(p_subresources);
}
}
}
}
d->_nodes.set(*key, node_copy);
}
return d;
}
float VoxelGeneratorGraph::debug_measure_microseconds_per_voxel() {
Vector3i pos(1, 1, 1);
float v;

View File

@ -96,11 +96,14 @@ public:
void set_vertical_bounds(int min_y, int max_y, float bottom_sdf_value, float top_sdf_value, uint64_t bottom_type_value, uint64_t top_type_value);
void set_box_bounds(Vector3i min, Vector3i max, float sdf_value, uint64_t type_value);
Ref<Resource> duplicate(bool p_subresources) const override;
float debug_measure_microseconds_per_voxel();
private:
void compile();
Interval analyze_range(Vector3i min_pos, Vector3i max_pos);
void make_modified();
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
@ -114,9 +117,6 @@ private:
Vector2 gui_position;
};
ProgramGraph _graph;
HashMap<uint32_t, Node *> _nodes;
struct Bounds {
BoundsType type = BOUNDS_NONE;
Vector3i min;
@ -128,6 +128,9 @@ private:
uint64_t type_value1 = 0;
};
ProgramGraph _graph;
HashMap<uint32_t, Node *> _nodes;
std::vector<uint8_t> _program;
std::vector<float> _memory;
VoxelBuffer::ChannelId _channel = VoxelBuffer::CHANNEL_SDF;