Changeset View
Standalone View
source/blender/nodes/NOD_geometry_exec.hh
| Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | |||||
| using fn::GVArray_Span; | using fn::GVArray_Span; | ||||
| using fn::GVArray_Typed; | using fn::GVArray_Typed; | ||||
| using fn::GVArrayPtr; | using fn::GVArrayPtr; | ||||
| using fn::GVMutableArray; | using fn::GVMutableArray; | ||||
| using fn::GVMutableArray_GSpan; | using fn::GVMutableArray_GSpan; | ||||
| using fn::GVMutableArray_Typed; | using fn::GVMutableArray_Typed; | ||||
| using fn::GVMutableArrayPtr; | using fn::GVMutableArrayPtr; | ||||
| /** | |||||
| * This class exists to separate the memory management details of the geometry nodes evaluator from | |||||
| * the node execution functions and related utilities. | |||||
| */ | |||||
| class GeoNodeExecParamsProvider { | |||||
HooglyBoogly: Do you expect there to be more than one implementation of this? I know the compiler might… | |||||
Done Inline ActionsI don't plan to have more than one "good" implementation of this. However, just because something is only used once doesn't mean that separation of concerns is bad in that case. It just reducces the things you have to care about when modifying the code. The compiler probably won't optimize the virtual calls here, but I'm not concerned about the virtual function call overhead here. We might have two evaluator implementations in the future. One is very good and is used in practice all the time. And another one that is very basic and less efficient. The benefit is that this would allow us to check quite easily if a bug is in the evaluator or an individual node. Anyway, I don't plan to have two evaluators any time soon, only when we run into bugs where we don't know what's wrong often enough. JacquesLucke: I don't plan to have more than one "good" implementation of this. However, just because… | |||||
| public: | |||||
| DNode dnode; | |||||
| const PersistentDataHandleMap *handle_map = nullptr; | |||||
| const Object *self_object = nullptr; | |||||
| const ModifierData *modifier = nullptr; | |||||
| Depsgraph *depsgraph = nullptr; | |||||
| /** | |||||
| * Returns true when the node is allowed to get/extract the input value. The identifier is | |||||
| * expected to be valid. This may return false if the input value has been consumed already. | |||||
| */ | |||||
| virtual bool can_get_input(StringRef identifier) const = 0; | |||||
| /** | |||||
| * Returns true when the node is allowed to set the output value. The identifier is expected to | |||||
| * be valid. This may return false if the output value has been set already. | |||||
| */ | |||||
| virtual bool can_set_output(StringRef identifier) const = 0; | |||||
| /** | |||||
| * Take ownership of an input value. The caller is responsible for destructing the value. It does | |||||
| * not have to be freed, because the memory is managed by the geometry nodes evaluator. | |||||
| */ | |||||
| virtual GMutablePointer extract_input(StringRef identifier) = 0; | |||||
| /** | |||||
| * Similar to #extract_input, but has to be used for multi-input sockets. | |||||
| */ | |||||
| virtual Vector<GMutablePointer> extract_multi_input(StringRef identifier) = 0; | |||||
| /** | |||||
| * Get the input value for the identifier without taking ownership of it. | |||||
| */ | |||||
| virtual GPointer get_input(StringRef identifier) const = 0; | |||||
| /** | |||||
| * Prepare a memory buffer for an output value of the node. The returned memory has to be | |||||
| * initialized by the caller. The identifier and type are expected to be correct. | |||||
| */ | |||||
| virtual GMutablePointer alloc_output_value(StringRef identifier, const CPPType &type) = 0; | |||||
| }; | |||||
| class GeoNodeExecParams { | class GeoNodeExecParams { | ||||
| private: | private: | ||||
| const DNode node_; | GeoNodeExecParamsProvider *provider_; | ||||
| GValueMap<StringRef> &input_values_; | |||||
| GValueMap<StringRef> &output_values_; | |||||
| const PersistentDataHandleMap &handle_map_; | |||||
| const Object *self_object_; | |||||
| const ModifierData *modifier_; | |||||
| Depsgraph *depsgraph_; | |||||
| public: | public: | ||||
| GeoNodeExecParams(const DNode node, | GeoNodeExecParams(GeoNodeExecParamsProvider &provider) : provider_(&provider) | ||||
| GValueMap<StringRef> &input_values, | |||||
| GValueMap<StringRef> &output_values, | |||||
| const PersistentDataHandleMap &handle_map, | |||||
| const Object *self_object, | |||||
| const ModifierData *modifier, | |||||
| Depsgraph *depsgraph) | |||||
| : node_(node), | |||||
| input_values_(input_values), | |||||
| output_values_(output_values), | |||||
| handle_map_(handle_map), | |||||
| self_object_(self_object), | |||||
| modifier_(modifier), | |||||
| depsgraph_(depsgraph) | |||||
| { | { | ||||
| } | } | ||||
| /** | /** | ||||
| * Get the input value for the input socket with the given identifier. | * Get the input value for the input socket with the given identifier. | ||||
| * | * | ||||
| * The node calling becomes responsible for destructing the value before it is done | * The node calling becomes responsible for destructing the value before it is done | ||||
| * executing. This method can only be called once for each identifier. | * executing. This method can only be called once for each identifier. | ||||
| */ | */ | ||||
| GMutablePointer extract_input(StringRef identifier) | GMutablePointer extract_input(StringRef identifier) | ||||
| { | { | ||||
| #ifdef DEBUG | #ifdef DEBUG | ||||
| this->check_extract_input(identifier); | this->check_input_access(identifier); | ||||
| #endif | #endif | ||||
| return input_values_.extract(identifier); | return provider_->extract_input(identifier); | ||||
| } | } | ||||
| /** | /** | ||||
| * Get the input value for the input socket with the given identifier. | * Get the input value for the input socket with the given identifier. | ||||
| * | * | ||||
| * This method can only be called once for each identifier. | * This method can only be called once for each identifier. | ||||
| */ | */ | ||||
| template<typename T> T extract_input(StringRef identifier) | template<typename T> T extract_input(StringRef identifier) | ||||
| { | { | ||||
| #ifdef DEBUG | #ifdef DEBUG | ||||
| this->check_extract_input(identifier, &CPPType::get<T>()); | this->check_input_access(identifier, &CPPType::get<T>()); | ||||
| #endif | #endif | ||||
| return input_values_.extract<T>(identifier); | GMutablePointer gvalue = this->extract_input(identifier); | ||||
| return gvalue.relocate_out<T>(); | |||||
| } | } | ||||
| /** | /** | ||||
| * Get input as vector for multi input socket with the given identifier. | * Get input as vector for multi input socket with the given identifier. | ||||
| * | * | ||||
| * This method can only be called once for each identifier. | * This method can only be called once for each identifier. | ||||
| */ | */ | ||||
| template<typename T> Vector<T> extract_multi_input(StringRef identifier) | template<typename T> Vector<T> extract_multi_input(StringRef identifier) | ||||
| { | { | ||||
| Vector<GMutablePointer> gvalues = provider_->extract_multi_input(identifier); | |||||
| Vector<T> values; | Vector<T> values; | ||||
| int index = 0; | for (GMutablePointer gvalue : gvalues) { | ||||
| while (true) { | values.append(gvalue.relocate_out<T>()); | ||||
| std::string sub_identifier = identifier; | |||||
| if (index > 0) { | |||||
| sub_identifier += "[" + std::to_string(index) + "]"; | |||||
| } | |||||
| if (!input_values_.contains(sub_identifier)) { | |||||
| break; | |||||
| } | |||||
| values.append(input_values_.extract<T, StringRef>(sub_identifier)); | |||||
| index++; | |||||
| } | } | ||||
| return values; | return values; | ||||
| } | } | ||||
| /** | /** | ||||
| * Get the input value for the input socket with the given identifier. | * Get the input value for the input socket with the given identifier. | ||||
| */ | */ | ||||
| template<typename T> const T &get_input(StringRef identifier) const | template<typename T> const T &get_input(StringRef identifier) const | ||||
| { | { | ||||
| #ifdef DEBUG | #ifdef DEBUG | ||||
| this->check_extract_input(identifier, &CPPType::get<T>()); | this->check_input_access(identifier, &CPPType::get<T>()); | ||||
| #endif | |||||
| return input_values_.lookup<T>(identifier); | |||||
| } | |||||
| /** | |||||
| * Move-construct a new value based on the given value and store it for the given socket | |||||
| * identifier. | |||||
| */ | |||||
| void set_output_by_move(StringRef identifier, GMutablePointer value) | |||||
| { | |||||
| #ifdef DEBUG | |||||
| BLI_assert(value.type() != nullptr); | |||||
| BLI_assert(value.get() != nullptr); | |||||
| this->check_set_output(identifier, *value.type()); | |||||
| #endif | |||||
| output_values_.add_new_by_move(identifier, value); | |||||
| } | |||||
| void set_output_by_copy(StringRef identifier, GPointer value) | |||||
| { | |||||
| #ifdef DEBUG | |||||
| BLI_assert(value.type() != nullptr); | |||||
| BLI_assert(value.get() != nullptr); | |||||
| this->check_set_output(identifier, *value.type()); | |||||
| #endif | #endif | ||||
| output_values_.add_new_by_copy(identifier, value); | GPointer gvalue = provider_->get_input(identifier); | ||||
| BLI_assert(gvalue.is_type<T>()); | |||||
| return *(const T *)gvalue.get(); | |||||
| } | } | ||||
| /** | /** | ||||
| * Store the output value for the given socket identifier. | * Store the output value for the given socket identifier. | ||||
| */ | */ | ||||
| template<typename T> void set_output(StringRef identifier, T &&value) | template<typename T> void set_output(StringRef identifier, T &&value) | ||||
| { | { | ||||
| using StoredT = std::decay_t<T>; | |||||
| const CPPType &type = CPPType::get<std::decay_t<T>>(); | |||||
| #ifdef DEBUG | #ifdef DEBUG | ||||
| this->check_set_output(identifier, CPPType::get<std::decay_t<T>>()); | this->check_output_access(identifier, type); | ||||
| #endif | #endif | ||||
| output_values_.add_new(identifier, std::forward<T>(value)); | GMutablePointer gvalue = provider_->alloc_output_value(identifier, type); | ||||
| new (gvalue.get()) StoredT(std::forward<T>(value)); | |||||
| } | } | ||||
| /** | /** | ||||
| * Get the node that is currently being executed. | * Get the node that is currently being executed. | ||||
| */ | */ | ||||
| const bNode &node() const | const bNode &node() const | ||||
| { | { | ||||
| return *node_->bnode(); | return *provider_->dnode->bnode(); | ||||
| } | } | ||||
| const PersistentDataHandleMap &handle_map() const | const PersistentDataHandleMap &handle_map() const | ||||
| { | { | ||||
| return handle_map_; | return *provider_->handle_map; | ||||
| } | } | ||||
| const Object *self_object() const | const Object *self_object() const | ||||
| { | { | ||||
| return self_object_; | return provider_->self_object; | ||||
| } | } | ||||
| Depsgraph *depsgraph() const | Depsgraph *depsgraph() const | ||||
| { | { | ||||
| return depsgraph_; | return provider_->depsgraph; | ||||
| } | } | ||||
| /** | /** | ||||
| * Add an error message displayed at the top of the node when displaying the node tree, | * Add an error message displayed at the top of the node when displaying the node tree, | ||||
| * and potentially elsewhere in Blender. | * and potentially elsewhere in Blender. | ||||
| */ | */ | ||||
| void error_message_add(const NodeWarningType type, std::string message) const; | void error_message_add(const NodeWarningType type, std::string message) const; | ||||
| Show All 30 Lines | CustomDataType get_input_attribute_data_type(const StringRef name, | ||||
| const CustomDataType default_type) const; | const CustomDataType default_type) const; | ||||
| AttributeDomain get_highest_priority_input_domain(Span<std::string> names, | AttributeDomain get_highest_priority_input_domain(Span<std::string> names, | ||||
| const GeometryComponent &component, | const GeometryComponent &component, | ||||
| const AttributeDomain default_domain) const; | const AttributeDomain default_domain) const; | ||||
| private: | private: | ||||
| /* Utilities for detecting common errors at when using this class. */ | /* Utilities for detecting common errors at when using this class. */ | ||||
| void check_extract_input(StringRef identifier, const CPPType *requested_type = nullptr) const; | void check_input_access(StringRef identifier, const CPPType *requested_type = nullptr) const; | ||||
| void check_set_output(StringRef identifier, const CPPType &value_type) const; | void check_output_access(StringRef identifier, const CPPType &value_type) const; | ||||
| /* Find the active socket socket with the input name (not the identifier). */ | /* Find the active socket socket with the input name (not the identifier). */ | ||||
| const bNodeSocket *find_available_socket(const StringRef name) const; | const bNodeSocket *find_available_socket(const StringRef name) const; | ||||
| }; | }; | ||||
| } // namespace blender::nodes | } // namespace blender::nodes | ||||
Do you expect there to be more than one implementation of this? I know the compiler might optimize away the virtual calls anyway, but it's just a bit confusing to have this specific thing generalized. Maybe I'm missing something.