Enzo
Loading...
Searching...
No Matches
PointScript.h
1#pragma once
2#include "Engine/Attribute/AttributeHandle.h"
3#include "Engine/Core/Types.h"
4#include "Engine/Daslang/Runtime.h"
5#include <deque>
6#include <memory>
7#include <optional>
8#include <vector>
9
10namespace enzo::geo {
11class Mesh;
12class Primitive;
13}
14
15namespace enzo::script {
16
17class ScriptContext;
18
21{
22 String name;
24 bool written = false;
25};
26
44{
45 public:
48 static std::shared_ptr<PointScript> compile(const String& code, String& error);
49
51 const std::vector<AttributeBinding>& getBindings() const { return bindings_; }
52
55 std::shared_ptr<PointScript> clone() const;
56
59 bool addWrittenAttributes(geo::Primitive& output, String& error) const;
60
70 bool run(
71 const ScriptContext& context,
72 size_t primitiveIndex,
73 geo::Primitive& output,
74 Offset begin,
75 Offset end,
76 String& error
77 );
78
79 private:
80 // Values the script works on for one point, one list per attribute type.
81 template <typename Value> struct BindingValues
82 {
83 std::deque<Value> values;
84 std::vector<std::optional<attr::AttributeHandleRO<Value>>> inputs;
85 std::vector<std::optional<attr::AttributeHandle<Value>>> outputs;
86
87 // Reads a point's values from the input, leaving missing attributes at the default value.
88 void loadFrom(Offset point)
89 {
90 for (size_t valueIndex = 0; valueIndex < values.size(); ++valueIndex)
91 {
92 const auto& input = inputs[valueIndex];
93 values[valueIndex] = input ? input->getValue(point) : Value{};
94 }
95 }
96
97 // Writes a point's values back to the output attributes.
98 void storeTo(Offset point)
99 {
100 for (size_t valueIndex = 0; valueIndex < values.size(); ++valueIndex)
101 {
102 auto& output = outputs[valueIndex];
103 if (output) output->setValue(point, values[valueIndex]);
104 }
105 }
106 };
107
108 PointScript(std::shared_ptr<daslang::CompiledProgram> compiled, std::vector<AttributeBinding> bindings);
109
110 template <typename Action> void visitBindingValues(attr::AttributeType type, Action&& action);
111 template <typename Action> void visitAllBindingValues(Action&& action);
112 bool resolveAttributes(const geo::Primitive& input, geo::Primitive& output, String& error);
113
114 std::shared_ptr<daslang::CompiledProgram> compiled_;
115 std::vector<AttributeBinding> bindings_;
116
117 BindingValues<floatT> floats_;
118 BindingValues<intT> ints_;
119 BindingValues<Vector3> vectors_;
120 BindingValues<boolT> bools_;
121
122 // The point's index and the point count, followed by a pointer to each binding's value.
123 std::vector<daslang::ScriptArgument> arguments_;
124};
125
126} // namespace enzo::script
Basic attribute, parameter, and node types for Enzo.
size_t Offset
enzo::Offset is the internal discontinuous index of an element in a given AttributeOwner.
Definition Types.h:182
AttributeType
Data types available to store attribute values in.
Definition Types.h:37
Base class for all primitive types in the engine.
A compiled script that runs once per point and edits the point's attributes.
Definition PointScript.h:44
const std::vector< AttributeBinding > & getBindings() const
Returns every attribute the code binds, in the order they first appear.
Definition PointScript.h:51
bool run(const ScriptContext &context, size_t primitiveIndex, geo::Primitive &output, Offset begin, Offset end, String &error)
Runs the script on the points in an offset range.
Definition PointScript.cpp:502
static std::shared_ptr< PointScript > compile(const String &code, String &error)
Returns the compiled script for the user's code.
Definition PointScript.cpp:407
std::shared_ptr< PointScript > clone() const
Returns a copy that runs in its own context.
Definition PointScript.cpp:439
bool addWrittenAttributes(geo::Primitive &output, String &error) const
Adds every attribute the script writes to the output, filled with the default value.
Definition PointScript.cpp:446
The world a script reads during one cook of a node.
Definition ScriptContext.h:28
A point attribute the script reads or writes as @name.
Definition PointScript.h:21