This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/functions/FN_multi_function_signature.hh

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

196 lines
5.6 KiB
C++
Raw Normal View History

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
/** \file
* \ingroup fn
*
* The signature of a multi-function contains the functions name and expected parameters. New
2020-07-22 11:46:27 +10:00
* signatures should be build using the #MFSignatureBuilder class.
*/
#include "FN_multi_function_param_type.hh"
#include "BLI_vector.hh"
2020-07-03 14:25:20 +02:00
namespace blender::fn {
struct MFSignature {
/**
* The name should be statically allocated so that it lives longer than this signature. This is
* used instead of an #std::string because of the overhead when many functions are created.
* If the name of the function has to be more dynamic for debugging purposes, override
* #MultiFunction::debug_name() instead. Then the dynamic name will only be computed when it is
* actually needed.
*/
const char *function_name;
Vector<const char *> param_names;
Vector<MFParamType> param_types;
Vector<int> param_data_indices;
bool depends_on_context = false;
int data_index(int param_index) const
{
return param_data_indices[param_index];
}
};
class MFSignatureBuilder {
private:
MFSignature signature_;
int span_count_ = 0;
Functions: refactor virtual array data structures When a function is executed for many elements (e.g. per point) it is often the case that some parameters are different for every element and other parameters are the same (there are some more less common cases). To simplify writing such functions one can use a "virtual array". This is a data structure that has a value for every index, but might not be stored as an actual array internally. Instead, it might be just a single value or is computed on the fly. There are various tradeoffs involved when using this data structure which are mentioned in `BLI_virtual_array.hh`. It is called "virtual", because it uses inheritance and virtual methods. Furthermore, there is a new virtual vector array data structure, which is an array of vectors. Both these types have corresponding generic variants, which can be used when the data type is not known at compile time. This is typically the case when building a somewhat generic execution system. The function system used these virtual data structures before, but now they are more versatile. I've done this refactor in preparation for the attribute processor and other features of geometry nodes. I moved the typed virtual arrays to blenlib, so that they can be used independent of the function system. One open question for me is whether all the generic data structures (and `CPPType`) should be moved to blenlib as well. They are well isolated and don't really contain any business logic. That can be done later if necessary.
2021-03-21 19:31:24 +01:00
int virtual_array_count_ = 0;
int virtual_vector_array_count_ = 0;
int vector_array_count_ = 0;
public:
MFSignatureBuilder(const char *function_name)
{
signature_.function_name = function_name;
}
MFSignature build() const
{
return std::move(signature_);
}
2020-07-22 11:46:27 +10:00
/* Input Parameter Types */
template<typename T> void single_input(const char *name)
{
this->single_input(name, CPPType::get<T>());
}
void single_input(const char *name, const CPPType &type)
{
this->input(name, MFDataType::ForSingle(type));
}
template<typename T> void vector_input(const char *name)
{
this->vector_input(name, CPPType::get<T>());
}
void vector_input(const char *name, const CPPType &base_type)
{
this->input(name, MFDataType::ForVector(base_type));
}
void input(const char *name, MFDataType data_type)
{
signature_.param_names.append(name);
signature_.param_types.append(MFParamType(MFParamType::Input, data_type));
switch (data_type.category()) {
case MFDataType::Single:
signature_.param_data_indices.append(virtual_array_count_++);
break;
case MFDataType::Vector:
signature_.param_data_indices.append(virtual_vector_array_count_++);
break;
}
}
2020-07-22 11:46:27 +10:00
/* Output Parameter Types */
template<typename T> void single_output(const char *name)
{
this->single_output(name, CPPType::get<T>());
}
void single_output(const char *name, const CPPType &type)
{
this->output(name, MFDataType::ForSingle(type));
}
template<typename T> void vector_output(const char *name)
{
this->vector_output(name, CPPType::get<T>());
}
void vector_output(const char *name, const CPPType &base_type)
{
this->output(name, MFDataType::ForVector(base_type));
}
void output(const char *name, MFDataType data_type)
{
signature_.param_names.append(name);
signature_.param_types.append(MFParamType(MFParamType::Output, data_type));
switch (data_type.category()) {
case MFDataType::Single:
signature_.param_data_indices.append(span_count_++);
break;
case MFDataType::Vector:
signature_.param_data_indices.append(vector_array_count_++);
break;
}
}
2020-07-22 11:46:27 +10:00
/* Mutable Parameter Types */
template<typename T> void single_mutable(const char *name)
{
this->single_mutable(name, CPPType::get<T>());
}
void single_mutable(const char *name, const CPPType &type)
{
this->mutable_(name, MFDataType::ForSingle(type));
}
template<typename T> void vector_mutable(const char *name)
{
this->vector_mutable(name, CPPType::get<T>());
}
void vector_mutable(const char *name, const CPPType &base_type)
{
this->mutable_(name, MFDataType::ForVector(base_type));
}
void mutable_(const char *name, MFDataType data_type)
{
signature_.param_names.append(name);
signature_.param_types.append(MFParamType(MFParamType::Mutable, data_type));
switch (data_type.category()) {
case MFDataType::Single:
signature_.param_data_indices.append(span_count_++);
break;
case MFDataType::Vector:
signature_.param_data_indices.append(vector_array_count_++);
break;
}
}
void add(const char *name, const MFParamType &param_type)
{
switch (param_type.interface_type()) {
case MFParamType::Input:
this->input(name, param_type.data_type());
break;
case MFParamType::Mutable:
this->mutable_(name, param_type.data_type());
break;
case MFParamType::Output:
this->output(name, param_type.data_type());
break;
}
}
/* Context */
/** This indicates that the function accesses the context. This disables optimizations that
2020-07-22 11:46:27 +10:00
* depend on the fact that the function always performers the same operation. */
void depends_on_context()
{
signature_.depends_on_context = true;
}
};
2020-07-03 14:25:20 +02:00
} // namespace blender::fn