+#include <assert.h>
+extern "C" {
+#include <lauxlib.h>
+#include <lua.hpp>
+}
+
+#ifdef HAVE_CEF
+#include "cef_capture.h"
+#endif
+#include "ffmpeg_capture.h"
+#include "flags.h"
+#include "image_input.h"
+#include "input_state.h"
+#include "lua_utils.h"
+#include "scene.h"
+#include "theme.h"
+
+using namespace movit;
+using namespace std;
+
+bool display(Block *block, lua_State *L, int idx);
+
+EffectType current_type(const Block *block)
+{
+ return block->alternatives[block->currently_chosen_alternative]->effect_type;
+}
+
+int find_index_of(const Block *block, EffectType val)
+{
+ for (size_t idx = 0; idx < block->alternatives.size(); ++idx) {
+ if (block->alternatives[idx]->effect_type == val) {
+ return idx;
+ }
+ }
+ return -1;
+}
+
+Scene::Scene(Theme *theme, float aspect_nom, float aspect_denom)
+ : theme(theme), aspect_nom(aspect_nom), aspect_denom(aspect_denom), resource_pool(theme->get_resource_pool()) {}
+
+size_t Scene::compute_chain_number(bool is_main_chain) const
+{
+ assert(chains.size() > 0);
+ assert(chains.size() % 2 == 0);
+ size_t chain_number = compute_chain_number_for_block(blocks.size() - 1);
+ assert(chain_number < chains.size() / 2);
+ if (is_main_chain) {
+ chain_number += chains.size() / 2;
+ }
+ return chain_number;
+}
+
+size_t Scene::compute_chain_number_for_block(size_t block_idx) const
+{
+ Block *block = blocks[block_idx];
+ size_t chain_number;
+ if (block_idx == 0) {
+ assert(block->cardinality_base == 1);
+ chain_number = block->currently_chosen_alternative;
+ } else {
+ chain_number = compute_chain_number_for_block(block_idx - 1) + block->cardinality_base * block->currently_chosen_alternative;
+ }
+ assert(block->currently_chosen_alternative < int(block->alternatives.size()));
+ return chain_number;
+}
+
+int Scene::add_input(lua_State* L)
+{
+ assert(lua_gettop(L) == 1 || lua_gettop(L) == 2);
+ Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
+
+ Block *block = new Block;
+ block->idx = chain->blocks.size();
+ if (lua_gettop(L) == 1) {
+ // No parameter given, so a flexible input.
+ block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
+ block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
+ block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
+ block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
+ block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
+ } else {
+ // Input of a given type. We'll specialize it here, plus connect the input as given.
+ if (lua_isnumber(L, 2)) {
+ block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
+ block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
+#ifdef HAVE_CEF
+ } else if (luaL_testudata(L, 2, "HTMLInput")) {
+ block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
+#endif
+ } else if (luaL_testudata(L, 2, "VideoInput")) {
+ FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, 2, "VideoInput");
+ if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
+ block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
+ } else {
+ assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
+ block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
+ }
+ } else if (luaL_testudata(L, 2, "ImageInput")) {
+ block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
+ } else {
+ luaL_error(L, "add_input() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
+ }
+ bool ok = display(block, L, 2);
+ assert(ok);
+ }
+ block->is_input = true;
+ chain->blocks.push_back(block);
+
+ return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
+}
+
+int Scene::add_effect(lua_State* L)
+{
+ assert(lua_gettop(L) >= 2);
+ Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
+
+ Block *block = new Block;
+ block->idx = chain->blocks.size();
+
+ if (lua_istable(L, 2)) {
+ size_t len = lua_objlen(L, 2);
+ for (size_t i = 0; i < len; ++i) {
+ lua_rawgeti(L, 2, i + 1);
+ EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, -1, "EffectBlueprint");
+ block->alternatives.push_back(blueprint);
+ lua_settop(L, -2);
+ }
+ } else {
+ EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
+ block->alternatives.push_back(blueprint);
+ }
+
+ // Find the inputs.
+ if (lua_gettop(L) == 2) {
+ assert(!chain->blocks.empty());
+ block->inputs.push_back(chain->blocks.size() - 1);
+ } else {
+ for (int idx = 3; idx <= lua_gettop(L); ++idx) {
+ Block *input_block = nullptr;
+ if (luaL_testudata(L, idx, "Block")) {
+ input_block = *(Block **)luaL_checkudata(L, idx, "Block");
+ } else {
+ EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint");
+
+ // Search through all the blocks to figure out which one contains this effect.
+ for (Block *block : chain->blocks) {
+ if (find(block->alternatives.begin(), block->alternatives.end(), blueprint) != block->alternatives.end()) {
+ input_block = block;
+ break;
+ }
+ }
+ if (input_block == nullptr) {
+ luaL_error(L, "Input effect in parameter #%d has not been added to this chain", idx - 1);
+ }
+ }
+ block->inputs.push_back(input_block->idx);
+ }
+ }
+
+ chain->blocks.push_back(block);
+
+ return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
+}
+
+int Scene::add_optional_effect(lua_State* L)
+{
+ assert(lua_gettop(L) >= 2);
+ Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
+
+ // NOTE: We only support effects with a single parent, since that's what IdentityEffect does.
+ Block *block = new Block;
+ block->idx = chain->blocks.size();
+
+ EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
+ block->alternatives.push_back(blueprint);
+
+ // An IdentityEffect will be the alternative for when the effect is disabled.
+ block->alternatives.push_back(new EffectBlueprint(IDENTITY_EFFECT));
+
+ block->inputs.push_back(chain->blocks.size() - 1);
+ chain->blocks.push_back(block);
+
+ return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
+}
+
+Effect *Scene::instantiate_effects(const Block *block, size_t chain_idx, Scene::Instantiation *instantiation)
+{
+ vector<Effect *> inputs;
+ for (size_t input_idx : block->inputs) {
+ inputs.push_back(instantiate_effects(blocks[input_idx], chain_idx, instantiation));
+ }
+
+ // Find the chosen alternative for this block in this instance.
+ size_t chosen_alternative = (chain_idx / block->cardinality_base) % block->alternatives.size();
+ EffectType chosen_type = block->alternatives[chosen_alternative]->effect_type;
+
+ Effect *effect;
+ switch (chosen_type) {
+ case LIVE_INPUT_YCBCR:
+ case LIVE_INPUT_YCBCR_WITH_DEINTERLACE:
+ case LIVE_INPUT_YCBCR_PLANAR:
+ case LIVE_INPUT_BGRA: {
+ bool deinterlace = (chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
+ bool override_bounce = !deinterlace; // For most chains, this will be fine. Reconsider if we see real problems somewhere; it's better than having the user try to understand it.
+ bmusb::PixelFormat pixel_format;
+ if (chosen_type == LIVE_INPUT_BGRA) {
+ pixel_format = bmusb::PixelFormat_8BitBGRA;
+ } else if (chosen_type == LIVE_INPUT_YCBCR_PLANAR) {
+ pixel_format = bmusb::PixelFormat_8BitYCbCrPlanar;
+ } else if (global_flags.ten_bit_input) {
+ pixel_format = bmusb::PixelFormat_10BitYCbCr;
+ } else {
+ pixel_format = bmusb::PixelFormat_8BitYCbCr;
+ }
+ LiveInputWrapper *input = new LiveInputWrapper(theme, instantiation->chain.get(), pixel_format, override_bounce, deinterlace, /*user_connectable=*/true);
+ effect = input->get_effect(); // Adds itself to the chain, so no need to call add_effect().
+ instantiation->inputs.emplace(block->idx, input);
+ break;
+ }
+ case IMAGE_INPUT: {
+ ImageInput *input = new ImageInput;
+ instantiation->chain->add_input(input);
+ instantiation->image_inputs.emplace(block->idx, input);
+ effect = input;
+ break;
+ }
+ default:
+ effect = instantiate_effect(instantiation->chain.get(), block->alternatives[chosen_alternative]->effect_type);
+ instantiation->chain->add_effect(effect, inputs);
+ break;
+ }
+ instantiation->effects.emplace(block->idx, effect);
+ return effect;
+}
+
+int Scene::finalize(lua_State* L)
+{
+ bool only_one_mode = false;
+ bool chosen_mode = false;
+ if (lua_gettop(L) == 2) {
+ only_one_mode = true;
+ chosen_mode = checkbool(L, 2);
+ } else {
+ assert(lua_gettop(L) == 1);
+ }
+ Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
+ Theme *theme = get_theme_updata(L);
+
+ size_t base = 1;
+ for (Block *block : chain->blocks) {
+ block->cardinality_base = base;
+ base *= block->alternatives.size();
+ }
+
+ const size_t cardinality = base;
+ const size_t total_cardinality = cardinality * (only_one_mode ? 1 : 2);
+ if (total_cardinality > 200) {
+ print_warning(L, "The given Scene will instantiate %zu different versions. This will take a lot of time and RAM to compile; see if you could limit some options by e.g. locking the input type in some cases (by giving a fixed input to add_input()).\n",
+ total_cardinality);
+ }
+
+ Block *output_block = chain->blocks.back();
+ for (bool is_main_chain : { false, true }) {
+ for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
+ if (only_one_mode && is_main_chain != chosen_mode) {
+ chain->chains.emplace_back();
+ continue;
+ }
+
+ Scene::Instantiation instantiation;
+ instantiation.chain.reset(new EffectChain(chain->aspect_nom, chain->aspect_denom, theme->get_resource_pool()));
+ chain->instantiate_effects(output_block, chain_idx, &instantiation);
+
+ add_outputs_and_finalize(instantiation.chain.get(), is_main_chain);
+ chain->chains.emplace_back(move(instantiation));
+ }
+ }
+ return 0;
+}
+
+std::pair<movit::EffectChain *, std::function<void()>>
+Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &input_state)
+{
+ // For video inputs, pick the right interlaced/progressive version
+ // based on the current state of the signals.
+ InputStateInfo info(input_state);
+ for (Block *block : blocks) {
+ if (block->is_input && block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
+ EffectType chosen_type = current_type(block);
+ assert(chosen_type == LIVE_INPUT_YCBCR || chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
+ if (info.last_interlaced[block->signal_to_connect]) {
+ block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
+ } else {
+ block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);
+ }
+ }
+ }
+
+ // Pick out the right chain based on the current selections,
+ // and snapshot all the set variables so that we can set them
+ // in the prepare function even if they're being changed by
+ // the Lua code later.
+ bool is_main_chain = (num == 0);
+ size_t chain_idx = compute_chain_number(is_main_chain);
+ const Scene::Instantiation &instantiation = chains[chain_idx];
+ EffectChain *effect_chain = instantiation.chain.get();
+
+ map<LiveInputWrapper *, int> signals_to_connect;
+ map<ImageInput *, string> images_to_select;
+ map<pair<Effect *, string>, int> int_to_set;
+ map<pair<Effect *, string>, float> float_to_set;
+ map<pair<Effect *, string>, array<float, 3>> vec3_to_set;
+ map<pair<Effect *, string>, array<float, 4>> vec4_to_set;
+ for (const auto &index_and_input : instantiation.inputs) {
+ Block *block = blocks[index_and_input.first];
+ EffectType chosen_type = current_type(block);
+ LiveInputWrapper *input = index_and_input.second;
+ if (chosen_type == LIVE_INPUT_YCBCR ||
+ chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE ||
+ chosen_type == LIVE_INPUT_YCBCR_PLANAR ||
+ chosen_type == LIVE_INPUT_BGRA) {
+ if (block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
+ signals_to_connect.emplace(input, block->signal_to_connect);
+#ifdef HAVE_CEF
+ } else if (block->signal_type_to_connect == Block::CONNECT_CEF) {
+ signals_to_connect.emplace(input, block->cef_to_connect->get_card_index());
+#endif
+ } else if (block->signal_type_to_connect == Block::CONNECT_VIDEO) {
+ signals_to_connect.emplace(input, block->video_to_connect->get_card_index());
+ } else {
+ assert(false);
+ }
+ }
+ }
+ for (const auto &index_and_input : instantiation.image_inputs) {
+ Block *block = blocks[index_and_input.first];
+ ImageInput *input = index_and_input.second;
+ if (current_type(block) == IMAGE_INPUT) {
+ images_to_select.emplace(input, block->pathname);
+ }
+ }
+ for (const auto &index_and_effect : instantiation.effects) {
+ Block *block = blocks[index_and_effect.first];
+ Effect *effect = index_and_effect.second;
+
+ // Get the effects currently set on the block.
+ if (current_type(block) != IDENTITY_EFFECT) { // Ignore settings on optional effects.
+ for (const auto &key_and_tuple : block->int_parameters) {
+ int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+ }
+ for (const auto &key_and_tuple : block->float_parameters) {
+ float_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+ }
+ for (const auto &key_and_tuple : block->vec3_parameters) {
+ vec3_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+ }
+ for (const auto &key_and_tuple : block->vec4_parameters) {
+ vec4_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+ }
+ }
+
+ // Parameters set on the blueprint itself override those that are set for the block,
+ // so they are set afterwards.
+ if (!block->alternatives.empty()) {
+ EffectBlueprint *blueprint = block->alternatives[block->currently_chosen_alternative];
+ for (const auto &key_and_tuple : blueprint->int_parameters) {
+ int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+ }
+ for (const auto &key_and_tuple : blueprint->float_parameters) {
+ float_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+ }
+ for (const auto &key_and_tuple : blueprint->vec3_parameters) {
+ vec3_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+ }
+ for (const auto &key_and_tuple : blueprint->vec4_parameters) {
+ vec4_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+ }
+ }
+ }
+
+ lua_pop(L, 1);
+
+ auto setup_chain = [L, theme, signals_to_connect, images_to_select, int_to_set, float_to_set, vec3_to_set, vec4_to_set, input_state]{
+ lock_guard<mutex> lock(theme->m);
+
+ assert(theme->input_state == nullptr);
+ theme->input_state = &input_state;
+
+ // Set up state, including connecting signals.
+ for (const auto &input_and_signal : signals_to_connect) {
+ LiveInputWrapper *input = input_and_signal.first;
+ input->connect_signal_raw(input_and_signal.second, input_state);
+ }
+ for (const auto &input_and_filename : images_to_select) {
+ input_and_filename.first->switch_image(input_and_filename.second);
+ }
+ for (const auto &effect_and_key_and_value : int_to_set) {
+ Effect *effect = effect_and_key_and_value.first.first;
+ const string &key = effect_and_key_and_value.first.second;
+ const int value = effect_and_key_and_value.second;
+ if (!effect->set_int(key, value)) {
+ luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), value);
+ }
+ }
+ for (const auto &effect_and_key_and_value : float_to_set) {
+ Effect *effect = effect_and_key_and_value.first.first;
+ const string &key = effect_and_key_and_value.first.second;
+ const float value = effect_and_key_and_value.second;
+ if (!effect->set_float(key, value)) {
+ luaL_error(L, "Effect refused set_float(\"%s\", %f) (invalid key?)", key.c_str(), value);
+ }
+ }
+ for (const auto &effect_and_key_and_value : vec3_to_set) {
+ Effect *effect = effect_and_key_and_value.first.first;
+ const string &key = effect_and_key_and_value.first.second;
+ const float *value = effect_and_key_and_value.second.data();
+ if (!effect->set_vec3(key, value)) {
+ luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
+ value[0], value[1], value[2]);
+ }
+ }
+ for (const auto &effect_and_key_and_value : vec4_to_set) {
+ Effect *effect = effect_and_key_and_value.first.first;
+ const string &key = effect_and_key_and_value.first.second;
+ const float *value = effect_and_key_and_value.second.data();
+ if (!effect->set_vec4(key, value)) {
+ luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
+ value[0], value[1], value[2], value[3]);
+ }
+ }
+
+ theme->input_state = nullptr;
+ };
+ return make_pair(effect_chain, move(setup_chain));
+}
+
+bool display(Block *block, lua_State *L, int idx)
+{
+ if (lua_isnumber(L, idx)) {
+ Theme *theme = get_theme_updata(L);
+ int signal_idx = luaL_checknumber(L, idx);
+ block->signal_type_to_connect = Block::CONNECT_SIGNAL;
+ block->signal_to_connect = theme->map_signal(signal_idx);
+ block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR); // Will be changed to deinterlaced at get_chain() time if needed.
+ return true;
+#ifdef HAVE_CEF
+ } else if (luaL_testudata(L, idx, "HTMLInput")) {
+ CEFCapture *capture = *(CEFCapture **)luaL_checkudata(L, idx, "HTMLInput");
+ block->signal_type_to_connect = Block::CONNECT_CEF;
+ block->cef_to_connect = capture;
+ block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
+ assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
+ return true;
+#endif
+ } else if (luaL_testudata(L, idx, "VideoInput")) {
+ FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, idx, "VideoInput");
+ block->signal_type_to_connect = Block::CONNECT_VIDEO;
+ block->video_to_connect = capture;
+ if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
+ block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_PLANAR);
+ } else {
+ assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
+ block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
+ }
+ return true;
+ } else if (luaL_testudata(L, idx, "ImageInput")) {
+ ImageInput *image = *(ImageInput **)luaL_checkudata(L, idx, "ImageInput");
+ block->signal_type_to_connect = Block::CONNECT_NONE;
+ block->currently_chosen_alternative = find_index_of(block, IMAGE_INPUT);
+ block->pathname = image->get_pathname();
+ return true;
+ } else {
+ return false;
+ }
+}
+
+int Block_display(lua_State* L)
+{
+ assert(lua_gettop(L) == 2);
+ Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
+ if (!block->is_input) {
+ luaL_error(L, "display() called on something that isn't an input");
+ }
+
+ bool ok = display(block, L, 2);
+ if (!ok) {
+ luaL_error(L, "display() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
+ }
+
+ if (block->currently_chosen_alternative == -1) {
+ luaL_error(L, "display() called on an input whose type was fixed at construction time, with a signal of different type");
+ }
+
+ return 0;
+}
+
+int Block_choose_alternative(lua_State* L)
+{
+ assert(lua_gettop(L) == 2);
+ Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
+ int alternative_idx = luaL_checknumber(L, 2);
+
+ assert(alternative_idx >= 0);
+ assert(size_t(alternative_idx) < block->alternatives.size());
+ block->currently_chosen_alternative = alternative_idx;
+
+ return 0;
+}
+
+int Block_enable(lua_State *L)
+{
+ assert(lua_gettop(L) == 1);
+ Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
+
+ if (block->alternatives.size() != 2 ||
+ block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
+ luaL_error(L, "enable() called on something that wasn't added with add_optional_effect()");
+ }
+ block->currently_chosen_alternative = 0; // The actual effect.
+ return 0;
+}
+
+int Block_disable(lua_State *L)
+{
+ assert(lua_gettop(L) == 1);
+ Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
+
+ if (block->alternatives.size() != 2 ||
+ block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
+ luaL_error(L, "disable() called on something that wasn't added with add_optional_effect()");
+ }
+ block->currently_chosen_alternative = find_index_of(block, IDENTITY_EFFECT);
+ assert(block->currently_chosen_alternative != -1);
+ return 0;
+}
+
+int Block_set_int(lua_State *L)
+{
+ assert(lua_gettop(L) == 3);
+ Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
+ string key = checkstdstring(L, 2);
+ float value = luaL_checknumber(L, 3);
+
+ // TODO: check validity already here, if possible?
+ block->int_parameters[key] = value;
+
+ return 0;
+}
+
+int Block_set_float(lua_State *L)
+{
+ assert(lua_gettop(L) == 3);
+ Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
+ string key = checkstdstring(L, 2);
+ float value = luaL_checknumber(L, 3);
+
+ // TODO: check validity already here, if possible?
+ block->float_parameters[key] = value;
+
+ return 0;
+}
+
+int Block_set_vec3(lua_State *L)
+{
+ assert(lua_gettop(L) == 5);
+ Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
+ string key = checkstdstring(L, 2);
+ array<float, 3> v;
+ v[0] = luaL_checknumber(L, 3);
+ v[1] = luaL_checknumber(L, 4);
+ v[2] = luaL_checknumber(L, 5);
+
+ // TODO: check validity already here, if possible?
+ block->vec3_parameters[key] = v;
+
+ return 0;
+}
+
+int Block_set_vec4(lua_State *L)
+{
+ assert(lua_gettop(L) == 6);
+ Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
+ string key = checkstdstring(L, 2);
+ array<float, 4> v;
+ v[0] = luaL_checknumber(L, 3);
+ v[1] = luaL_checknumber(L, 4);
+ v[2] = luaL_checknumber(L, 5);
+ v[3] = luaL_checknumber(L, 6);
+
+ // TODO: check validity already here, if possible?
+ block->vec4_parameters[key] = v;
+
+ return 0;
+}
+