8 #include "cef_capture.h"
10 #include "ffmpeg_capture.h"
12 #include "image_input.h"
13 #include "input_state.h"
14 #include "lua_utils.h"
18 using namespace movit;
21 bool display(Block *block, lua_State *L, int idx);
23 EffectType current_type(const Block *block)
25 return block->alternatives[block->currently_chosen_alternative]->effect_type;
28 int find_index_of(const Block *block, EffectType val)
30 for (size_t idx = 0; idx < block->alternatives.size(); ++idx) {
31 if (block->alternatives[idx]->effect_type == val) {
38 Scene::Scene(Theme *theme, float aspect_nom, float aspect_denom)
39 : theme(theme), aspect_nom(aspect_nom), aspect_denom(aspect_denom), resource_pool(theme->get_resource_pool()) {}
41 size_t Scene::compute_chain_number(bool is_main_chain) const
43 assert(chains.size() > 0);
44 assert(chains.size() % 2 == 0);
45 bitset<256> disabled = find_disabled_blocks(size_t(-1));
47 size_t chain_number = compute_chain_number_for_block(blocks.size() - 1, disabled);
48 assert(chain_number < chains.size() / 2);
50 chain_number += chains.size() / 2;
55 size_t Scene::compute_chain_number_for_block(size_t block_idx, const bitset<256> &disabled) const
57 Block *block = blocks[block_idx];
60 size_t currently_chosen_alternative;
61 if (disabled.test(block_idx)) {
62 // It doesn't matter, so pick 0 as the canonical choice
63 // (this is the only one that is actually instantiated).
64 currently_chosen_alternative = 0;
66 currently_chosen_alternative = block->currently_chosen_alternative;
68 assert(currently_chosen_alternative < block->alternatives.size());
71 assert(block->cardinality_base == 1);
72 chain_number = currently_chosen_alternative;
74 chain_number = compute_chain_number_for_block(block_idx - 1, disabled) + block->cardinality_base * currently_chosen_alternative;
79 bitset<256> Scene::find_disabled_blocks(size_t chain_idx) const
81 assert(blocks.size() < 256);
84 find_disabled_blocks(chain_idx, blocks.size() - 1, /*currently_disabled=*/false, &ret);
88 void Scene::find_disabled_blocks(size_t chain_idx, size_t block_idx, bool currently_disabled, bitset<256> *disabled) const
90 if (currently_disabled) {
91 disabled->set(block_idx);
93 Block *block = blocks[block_idx];
94 EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
95 for (size_t input_idx = 0; input_idx < block->inputs.size(); ++input_idx) {
96 if (chosen_type == IDENTITY_EFFECT && input_idx > 0) {
97 // Multi-input effect that has been replaced by
98 // IdentityEffect, so every effect but the first are
99 // disabled and will not participate in the chain.
100 find_disabled_blocks(chain_idx, block->inputs[input_idx], /*currently_disabled=*/true, disabled);
102 // Just keep on recursing down.
103 find_disabled_blocks(chain_idx, block->inputs[input_idx], currently_disabled, disabled);
108 bool Scene::is_noncanonical_chain(size_t chain_idx) const
110 bitset<256> disabled = find_disabled_blocks(chain_idx);
111 if (disabled.none()) {
114 assert(blocks.size() < 256);
115 for (size_t block_idx = 0; block_idx < blocks.size(); ++block_idx) {
116 if (disabled.test(block_idx) && blocks[block_idx]->chosen_alternative(chain_idx) != 0) {
123 int Scene::add_input(lua_State* L)
125 assert(lua_gettop(L) == 1 || lua_gettop(L) == 2);
126 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
128 Block *block = new Block;
129 block->idx = scene->blocks.size();
130 if (lua_gettop(L) == 1) {
131 // No parameter given, so a flexible input.
132 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
133 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
134 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
135 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
136 block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
138 // Input of a given type. We'll specialize it here, plus connect the input as given.
139 if (lua_isnumber(L, 2)) {
140 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
141 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
143 } else if (luaL_testudata(L, 2, "HTMLInput")) {
144 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
146 } else if (luaL_testudata(L, 2, "VideoInput")) {
147 FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, 2, "VideoInput");
148 if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
149 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
151 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
152 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
154 } else if (luaL_testudata(L, 2, "ImageInput")) {
155 block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
157 luaL_error(L, "add_input() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
159 bool ok = display(block, L, 2);
162 block->is_input = true;
163 scene->blocks.push_back(block);
165 return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
168 void Scene::find_inputs_for_block(lua_State *L, Scene *scene, Block *block)
170 if (lua_gettop(L) == 2) {
171 // Implicitly the last added effect.
172 assert(!scene->blocks.empty());
173 block->inputs.push_back(scene->blocks.size() - 1);
177 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
178 Block *input_block = nullptr;
179 if (luaL_testudata(L, idx, "Block")) {
180 input_block = *(Block **)luaL_checkudata(L, idx, "Block");
182 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint");
184 // Search through all the blocks to figure out which one contains this effect.
185 for (Block *block : scene->blocks) {
186 if (find(block->alternatives.begin(), block->alternatives.end(), blueprint) != block->alternatives.end()) {
191 if (input_block == nullptr) {
192 luaL_error(L, "Input effect in parameter #%d has not been added to this scene", idx - 1);
195 block->inputs.push_back(input_block->idx);
199 int Scene::add_effect(lua_State* L)
201 assert(lua_gettop(L) >= 2);
202 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
204 Block *block = new Block;
205 block->idx = scene->blocks.size();
207 if (lua_istable(L, 2)) {
208 size_t len = lua_objlen(L, 2);
209 for (size_t i = 0; i < len; ++i) {
210 lua_rawgeti(L, 2, i + 1);
211 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, -1, "EffectBlueprint");
212 block->alternatives.push_back(blueprint);
216 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
217 block->alternatives.push_back(blueprint);
220 find_inputs_for_block(L, scene, block);
221 scene->blocks.push_back(block);
223 return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
226 int Scene::add_optional_effect(lua_State* L)
228 assert(lua_gettop(L) >= 2);
229 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
231 Block *block = new Block;
232 block->idx = scene->blocks.size();
234 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
235 block->alternatives.push_back(blueprint);
237 // An IdentityEffect will be the alternative for when the effect is disabled.
238 block->alternatives.push_back(new EffectBlueprint(IDENTITY_EFFECT));
240 find_inputs_for_block(L, scene, block);
241 scene->blocks.push_back(block);
243 return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
246 Effect *Scene::instantiate_effects(const Block *block, size_t chain_idx, Scene::Instantiation *instantiation)
248 // Find the chosen alternative for this block in this instance.
249 EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
251 vector<Effect *> inputs;
252 for (size_t input_idx : block->inputs) {
253 inputs.push_back(instantiate_effects(blocks[input_idx], chain_idx, instantiation));
255 // As a special case, we allow IdentityEffect to take only one input
256 // even if the other alternative (or alternatives) is multi-input.
257 // Thus, even if there are more than one inputs, instantiate only
259 if (chosen_type == IDENTITY_EFFECT) {
265 switch (chosen_type) {
266 case LIVE_INPUT_YCBCR:
267 case LIVE_INPUT_YCBCR_WITH_DEINTERLACE:
268 case LIVE_INPUT_YCBCR_PLANAR:
269 case LIVE_INPUT_BGRA: {
270 bool deinterlace = (chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
271 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.
272 bmusb::PixelFormat pixel_format;
273 if (chosen_type == LIVE_INPUT_BGRA) {
274 pixel_format = bmusb::PixelFormat_8BitBGRA;
275 } else if (chosen_type == LIVE_INPUT_YCBCR_PLANAR) {
276 pixel_format = bmusb::PixelFormat_8BitYCbCrPlanar;
277 } else if (global_flags.ten_bit_input) {
278 pixel_format = bmusb::PixelFormat_10BitYCbCr;
280 pixel_format = bmusb::PixelFormat_8BitYCbCr;
282 LiveInputWrapper *input = new LiveInputWrapper(theme, instantiation->chain.get(), pixel_format, override_bounce, deinterlace, /*user_connectable=*/true);
283 effect = input->get_effect(); // Adds itself to the chain, so no need to call add_effect().
284 instantiation->inputs.emplace(block->idx, input);
288 ImageInput *input = new ImageInput;
289 instantiation->chain->add_input(input);
290 instantiation->image_inputs.emplace(block->idx, input);
295 effect = instantiate_effect(instantiation->chain.get(), chosen_type);
296 instantiation->chain->add_effect(effect, inputs);
299 instantiation->effects.emplace(block->idx, effect);
303 int Scene::finalize(lua_State* L)
305 bool only_one_mode = false;
306 bool chosen_mode = false;
307 if (lua_gettop(L) == 2) {
308 only_one_mode = true;
309 chosen_mode = checkbool(L, 2);
311 assert(lua_gettop(L) == 1);
313 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
314 Theme *theme = get_theme_updata(L);
317 for (Block *block : scene->blocks) {
318 block->cardinality_base = base;
319 base *= block->alternatives.size();
322 const size_t cardinality = base;
323 size_t real_cardinality = 0;
324 for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
325 if (!scene->is_noncanonical_chain(chain_idx)) {
329 const size_t total_cardinality = real_cardinality * (only_one_mode ? 1 : 2);
330 if (total_cardinality > 200) {
331 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",
335 Block *output_block = scene->blocks.back();
336 for (bool is_main_chain : { false, true }) {
337 for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
338 if ((only_one_mode && is_main_chain != chosen_mode) ||
339 scene->is_noncanonical_chain(chain_idx)) {
340 scene->chains.emplace_back();
344 Scene::Instantiation instantiation;
345 instantiation.chain.reset(new EffectChain(scene->aspect_nom, scene->aspect_denom, theme->get_resource_pool()));
346 scene->instantiate_effects(output_block, chain_idx, &instantiation);
348 add_outputs_and_finalize(instantiation.chain.get(), is_main_chain);
349 scene->chains.emplace_back(move(instantiation));
355 std::pair<movit::EffectChain *, std::function<void()>>
356 Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &input_state)
358 // For video inputs, pick the right interlaced/progressive version
359 // based on the current state of the signals.
360 InputStateInfo info(input_state);
361 for (Block *block : blocks) {
362 if (block->is_input && block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
363 EffectType chosen_type = current_type(block);
364 assert(chosen_type == LIVE_INPUT_YCBCR || chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
365 if (info.last_interlaced[block->signal_to_connect]) {
366 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
368 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);
373 // Pick out the right chain based on the current selections,
374 // and snapshot all the set variables so that we can set them
375 // in the prepare function even if they're being changed by
376 // the Lua code later.
377 bool is_main_chain = (num == 0);
378 size_t chain_idx = compute_chain_number(is_main_chain);
379 const Scene::Instantiation &instantiation = chains[chain_idx];
380 EffectChain *effect_chain = instantiation.chain.get();
382 map<LiveInputWrapper *, int> signals_to_connect;
383 map<ImageInput *, string> images_to_select;
384 map<pair<Effect *, string>, int> int_to_set;
385 map<pair<Effect *, string>, float> float_to_set;
386 map<pair<Effect *, string>, array<float, 3>> vec3_to_set;
387 map<pair<Effect *, string>, array<float, 4>> vec4_to_set;
388 for (const auto &index_and_input : instantiation.inputs) {
389 Block *block = blocks[index_and_input.first];
390 EffectType chosen_type = current_type(block);
391 LiveInputWrapper *input = index_and_input.second;
392 if (chosen_type == LIVE_INPUT_YCBCR ||
393 chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE ||
394 chosen_type == LIVE_INPUT_YCBCR_PLANAR ||
395 chosen_type == LIVE_INPUT_BGRA) {
396 if (block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
397 signals_to_connect.emplace(input, block->signal_to_connect);
399 } else if (block->signal_type_to_connect == Block::CONNECT_CEF) {
400 signals_to_connect.emplace(input, block->cef_to_connect->get_card_index());
402 } else if (block->signal_type_to_connect == Block::CONNECT_VIDEO) {
403 signals_to_connect.emplace(input, block->video_to_connect->get_card_index());
404 } else if (block->signal_type_to_connect == Block::CONNECT_NONE) {
405 luaL_error(L, "An input in a scene was not connected to anything (forgot to call display())");
411 for (const auto &index_and_input : instantiation.image_inputs) {
412 Block *block = blocks[index_and_input.first];
413 ImageInput *input = index_and_input.second;
414 if (current_type(block) == IMAGE_INPUT) {
415 images_to_select.emplace(input, block->pathname);
418 for (const auto &index_and_effect : instantiation.effects) {
419 Block *block = blocks[index_and_effect.first];
420 Effect *effect = index_and_effect.second;
422 // Get the effects currently set on the block.
423 if (current_type(block) != IDENTITY_EFFECT) { // Ignore settings on optional effects.
424 for (const auto &key_and_tuple : block->int_parameters) {
425 int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
427 for (const auto &key_and_tuple : block->float_parameters) {
428 float_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
430 for (const auto &key_and_tuple : block->vec3_parameters) {
431 vec3_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
433 for (const auto &key_and_tuple : block->vec4_parameters) {
434 vec4_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
438 // Parameters set on the blueprint itself override those that are set for the block,
439 // so they are set afterwards.
440 if (!block->alternatives.empty()) {
441 EffectBlueprint *blueprint = block->alternatives[block->currently_chosen_alternative];
442 for (const auto &key_and_tuple : blueprint->int_parameters) {
443 int_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
445 for (const auto &key_and_tuple : blueprint->float_parameters) {
446 float_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
448 for (const auto &key_and_tuple : blueprint->vec3_parameters) {
449 vec3_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
451 for (const auto &key_and_tuple : blueprint->vec4_parameters) {
452 vec4_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
459 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]{
460 lock_guard<mutex> lock(theme->m);
462 // Set up state, including connecting signals.
463 for (const auto &input_and_signal : signals_to_connect) {
464 LiveInputWrapper *input = input_and_signal.first;
465 input->connect_signal_raw(input_and_signal.second, input_state);
467 for (const auto &input_and_filename : images_to_select) {
468 input_and_filename.first->switch_image(input_and_filename.second);
470 for (const auto &effect_and_key_and_value : int_to_set) {
471 Effect *effect = effect_and_key_and_value.first.first;
472 const string &key = effect_and_key_and_value.first.second;
473 const int value = effect_and_key_and_value.second;
474 if (!effect->set_int(key, value)) {
475 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), value);
478 for (const auto &effect_and_key_and_value : float_to_set) {
479 Effect *effect = effect_and_key_and_value.first.first;
480 const string &key = effect_and_key_and_value.first.second;
481 const float value = effect_and_key_and_value.second;
482 if (!effect->set_float(key, value)) {
483 luaL_error(L, "Effect refused set_float(\"%s\", %f) (invalid key?)", key.c_str(), value);
486 for (const auto &effect_and_key_and_value : vec3_to_set) {
487 Effect *effect = effect_and_key_and_value.first.first;
488 const string &key = effect_and_key_and_value.first.second;
489 const float *value = effect_and_key_and_value.second.data();
490 if (!effect->set_vec3(key, value)) {
491 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
492 value[0], value[1], value[2]);
495 for (const auto &effect_and_key_and_value : vec4_to_set) {
496 Effect *effect = effect_and_key_and_value.first.first;
497 const string &key = effect_and_key_and_value.first.second;
498 const float *value = effect_and_key_and_value.second.data();
499 if (!effect->set_vec4(key, value)) {
500 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
501 value[0], value[1], value[2], value[3]);
505 return make_pair(effect_chain, move(setup_chain));
508 bool display(Block *block, lua_State *L, int idx)
510 if (lua_isnumber(L, idx)) {
511 Theme *theme = get_theme_updata(L);
512 int signal_idx = luaL_checknumber(L, idx);
513 block->signal_type_to_connect = Block::CONNECT_SIGNAL;
514 block->signal_to_connect = theme->map_signal(signal_idx);
515 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR); // Will be changed to deinterlaced at get_chain() time if needed.
518 } else if (luaL_testudata(L, idx, "HTMLInput")) {
519 CEFCapture *capture = *(CEFCapture **)luaL_checkudata(L, idx, "HTMLInput");
520 block->signal_type_to_connect = Block::CONNECT_CEF;
521 block->cef_to_connect = capture;
522 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
523 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
526 } else if (luaL_testudata(L, idx, "VideoInput")) {
527 FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, idx, "VideoInput");
528 block->signal_type_to_connect = Block::CONNECT_VIDEO;
529 block->video_to_connect = capture;
530 if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
531 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_PLANAR);
533 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
534 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
537 } else if (luaL_testudata(L, idx, "ImageInput")) {
538 ImageInput *image = *(ImageInput **)luaL_checkudata(L, idx, "ImageInput");
539 block->signal_type_to_connect = Block::CONNECT_NONE;
540 block->currently_chosen_alternative = find_index_of(block, IMAGE_INPUT);
541 block->pathname = image->get_pathname();
548 int Block_display(lua_State* L)
550 assert(lua_gettop(L) == 2);
551 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
552 if (!block->is_input) {
553 luaL_error(L, "display() called on something that isn't an input");
556 bool ok = display(block, L, 2);
558 luaL_error(L, "display() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
561 if (block->currently_chosen_alternative == -1) {
562 luaL_error(L, "display() called on an input whose type was fixed at construction time, with a signal of different type");
568 int Block_choose(lua_State* L)
570 assert(lua_gettop(L) == 2);
571 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
572 int alternative_idx = -1;
573 if (lua_isnumber(L, 2)) {
574 alternative_idx = luaL_checknumber(L, 2);
575 } else if (lua_istable(L, 2)) {
576 // See if it's an Effect metatable (e.g. foo:choose(ResampleEffect))
577 lua_getfield(L, 2, "__effect_type_id");
578 if (lua_isnumber(L, -1)) {
579 EffectType effect_type = EffectType(luaL_checknumber(L, -1));
580 alternative_idx = find_index_of(block, effect_type);
585 if (alternative_idx == -1) {
586 luaL_error(L, "choose() called with something that was not an index or an effect type (e.g. ResampleEffect) that was part of the alternatives");
589 assert(alternative_idx >= 0);
590 assert(size_t(alternative_idx) < block->alternatives.size());
591 block->currently_chosen_alternative = alternative_idx;
593 return wrap_lua_existing_object_nonowned<EffectBlueprint>(L, "EffectBlueprint", block->alternatives[alternative_idx]);
596 int Block_enable(lua_State *L)
598 assert(lua_gettop(L) == 1);
599 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
601 if (block->alternatives.size() != 2 ||
602 block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
603 luaL_error(L, "enable() called on something that wasn't added with add_optional_effect()");
605 block->currently_chosen_alternative = 0; // The actual effect.
609 int Block_enable_if(lua_State *L)
611 assert(lua_gettop(L) == 2);
612 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
614 if (block->alternatives.size() != 2 ||
615 block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
616 luaL_error(L, "enable_if() called on something that wasn't added with add_optional_effect()");
618 bool enabled = checkbool(L, 2);
619 block->currently_chosen_alternative = enabled ? 0 : 1;
623 int Block_disable(lua_State *L)
625 assert(lua_gettop(L) == 1);
626 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
628 block->currently_chosen_alternative = find_index_of(block, IDENTITY_EFFECT);
629 if (block->currently_chosen_alternative == -1) {
630 luaL_error(L, "disable() called on something that didn't have an IdentityEffect fallback (try add_optional_effect())");
632 assert(block->currently_chosen_alternative != -1);
636 int Block_set_int(lua_State *L)
638 assert(lua_gettop(L) == 3);
639 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
640 string key = checkstdstring(L, 2);
641 float value = luaL_checknumber(L, 3);
643 // TODO: check validity already here, if possible?
644 block->int_parameters[key] = value;
649 int Block_set_float(lua_State *L)
651 assert(lua_gettop(L) == 3);
652 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
653 string key = checkstdstring(L, 2);
654 float value = luaL_checknumber(L, 3);
656 // TODO: check validity already here, if possible?
657 block->float_parameters[key] = value;
662 int Block_set_vec3(lua_State *L)
664 assert(lua_gettop(L) == 5);
665 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
666 string key = checkstdstring(L, 2);
668 v[0] = luaL_checknumber(L, 3);
669 v[1] = luaL_checknumber(L, 4);
670 v[2] = luaL_checknumber(L, 5);
672 // TODO: check validity already here, if possible?
673 block->vec3_parameters[key] = v;
678 int Block_set_vec4(lua_State *L)
680 assert(lua_gettop(L) == 6);
681 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
682 string key = checkstdstring(L, 2);
684 v[0] = luaL_checknumber(L, 3);
685 v[1] = luaL_checknumber(L, 4);
686 v[2] = luaL_checknumber(L, 5);
687 v[3] = luaL_checknumber(L, 6);
689 // TODO: check validity already here, if possible?
690 block->vec4_parameters[key] = v;