Geometry Nodes: make index field more reusable

Some inputs will be the index field implicitly, so we want this
class to be available outside of `node_geo_input_index.cc`.
This commit is contained in:
Jacques Lucke 2021-09-24 16:02:59 +02:00
parent 1a1c546124
commit 95ec6e4dd3
3 changed files with 25 additions and 19 deletions

View File

@ -484,4 +484,13 @@ template<typename T> Field<T> make_constant_field(T value)
GField make_field_constant_if_possible(GField field);
class IndexFieldInput final : public FieldInput {
public:
IndexFieldInput();
const GVArray *get_varray_for_context(const FieldContext &context,
IndexMask mask,
ResourceScope &scope) const final;
};
} // namespace blender::fn

View File

@ -526,6 +526,21 @@ const GVArray *FieldContext::get_varray_for_input(const FieldInput &field_input,
return field_input.get_varray_for_context(*this, mask, scope);
}
IndexFieldInput::IndexFieldInput() : FieldInput(CPPType::get<int>(), "Index")
{
}
const GVArray *IndexFieldInput::get_varray_for_context(const fn::FieldContext &UNUSED(context),
IndexMask mask,
ResourceScope &scope) const
{
/* TODO: Investigate a similar method to IndexRange::as_span() */
auto index_func = [](int i) { return i; };
return &scope.construct<
fn::GVArray_For_EmbeddedVArray<int, VArray_For_Func<int, decltype(index_func)>>>(
mask.min_array_size(), mask.min_array_size(), index_func);
}
/* --------------------------------------------------------------------
* FieldOperation.
*/

View File

@ -23,27 +23,9 @@ static void geo_node_input_index_declare(NodeDeclarationBuilder &b)
b.add_output<decl::Int>("Index").field_source();
}
class IndexFieldInput final : public fn::FieldInput {
public:
IndexFieldInput() : FieldInput(CPPType::get<int>(), "Index")
{
}
const GVArray *get_varray_for_context(const fn::FieldContext &UNUSED(context),
IndexMask mask,
ResourceScope &scope) const final
{
/* TODO: Investigate a similar method to IndexRange::as_span() */
auto index_func = [](int i) { return i; };
return &scope.construct<
fn::GVArray_For_EmbeddedVArray<int, VArray_For_Func<int, decltype(index_func)>>>(
mask.min_array_size(), mask.min_array_size(), index_func);
}
};
static void geo_node_input_index_exec(GeoNodeExecParams params)
{
Field<int> index_field{std::make_shared<IndexFieldInput>()};
Field<int> index_field{std::make_shared<fn::IndexFieldInput>()};
params.set_output("Index", std::move(index_field));
}