Visual165Tests.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // --- The Visualizer's GPU layers (core/VisualFx.h scene) and LYNX Dance (core/FruityDance.h) - round 165e ------------
  2. #include "core/FruityDance.h"
  3. TEST (visual165_shader_layers_round_trip)
  4. {
  5. using namespace lynx::visualfx::scene;
  6. // every shader type has effects with unique ids; the 2D types none
  7. for (int t = 0; t < layerTypeCount; ++t)
  8. {
  9. const auto type = (LayerType) t;
  10. CHECK (isShaderType (type) == (effectCount (type) > 0));
  11. std::set<std::string> ids;
  12. for (int e = 0; e < effectCount (type); ++e)
  13. {
  14. CHECK (ids.insert (effectId (type, e)).second);
  15. CHECK (effectIndex (type, effectId (type, e)) == e);
  16. CHECK (std::string (effectName (type, e)).size() > 2 && std::string (effectHint (type, e)).size() > 2);
  17. // the Add menu's layer: that effect, saved and read back unchanged
  18. Scene s;
  19. s.layers.push_back (shaderLayer (type, e));
  20. CHECK (s.layers[0].type == type && s.layers[0].effect == e);
  21. const auto back = parse (write (s));
  22. CHECK (back && *back == s);
  23. }
  24. CHECK (effectIndex (type, "no such effect") == -1);
  25. }
  26. CHECK (readsBelow (LayerType::feedback) && readsBelow (LayerType::postprocess) && ! readsBelow (LayerType::canvas));
  27. // blend modes are saved by id; unknown values keep Normal
  28. Scene s;
  29. for (int b = 0; b < blendCount; ++b)
  30. {
  31. Layer l;
  32. l.type = LayerType::bars;
  33. l.blend = (Blend) b;
  34. s.layers.push_back (l);
  35. }
  36. const auto back = parse (write (s));
  37. CHECK (back && *back == s);
  38. const auto odd = parse ("LYNX visualizer 1\nlayer=postprocess\neffect=nonsense\nblend=weird\n");
  39. CHECK (odd && odd->layers.size() == 1 && odd->layers[0].effect == 0 && odd->layers[0].blend == Blend::normal);
  40. // a 0.1.19 scene (no effect / blend keys) reads as before
  41. const auto old = parse ("LYNX visualizer 1\nlayer=bars\ncount=32\n");
  42. CHECK (old && old->layers[0].blend == Blend::normal && old->layers[0].effect == 0);
  43. // the built-in GPU scenes: each has a shader layer and starts with a background
  44. int gpuScenes = 0;
  45. for (const auto& p : presets())
  46. gpuScenes += std::any_of (p.layers.begin(), p.layers.end(), [] (const Layer& l) { return isShaderType (l.type); });
  47. CHECK (gpuScenes >= 6);
  48. }
  49. TEST (visual165_zgameeditor_gpu_mapping)
  50. {
  51. using namespace lynx::visualfx::scene;
  52. const std::string ini = "[General]\r\nLayerCount=8\r\n"
  53. "[AppSet0]\r\nApp=Background\\SolidColor\r\nFParamValues=0,0.5,1,0.5\r\nEnabled=1\r\n"
  54. "[AppSet1]\r\nApp=Canvas effects\\Electric\r\nEnabled=1\r\n"
  55. "[AppSet2]\r\nApp=Object Arrays\\CubesGrid\r\nEnabled=1\r\n"
  56. "[AppSet3]\r\nApp=Feedback\\WormHoleEclipse\r\nEnabled=1\r\n"
  57. "[AppSet4]\r\nApp=Postprocess\\Youlean Bloom\r\nFParamValues=0.4,0.3,0.6,0,0.9\r\nEnabled=1\r\n"
  58. "[AppSet5]\r\nApp=Postprocess\\Vignette\r\nFParamValues=0.2,0,0,0,0.7\r\nEnabled=1\r\n"
  59. "[AppSet6]\r\nApp=Misc\\Unknown Thing\r\nEnabled=1\r\n"
  60. "[AppSet7]\r\nApp=Terrain\\Perturbed Grid\r\nEnabled=0\r\n";
  61. std::vector<std::string> unmapped;
  62. const auto s = fromZge ({ ini.begin(), ini.end() }, &unmapped);
  63. CHECK (s && s->layers.size() == 6);
  64. if (! s || s->layers.size() != 6)
  65. return;
  66. // the solid colour from its parameters (Alpha, Hue, Saturation, Lightness: hue 0.5 = cyan, value 1 - 0.5)
  67. const auto bg = s->layers[0];
  68. CHECK (bg.type == LayerType::background && (bg.colourA & 0xffffff) == 0x008080);
  69. CHECK (s->layers[1].type == LayerType::canvas && s->layers[1].effect == effectIndex (LayerType::canvas, "electric"));
  70. CHECK (s->layers[2].type == LayerType::object3d && s->layers[2].effect == effectIndex (LayerType::object3d, "cubes"));
  71. CHECK (s->layers[3].type == LayerType::feedback && s->layers[3].effect == effectIndex (LayerType::feedback, "spin"));
  72. const auto bloom = s->layers[4];
  73. CHECK (bloom.type == LayerType::postprocess && bloom.effect == effectIndex (LayerType::postprocess, "bloom"));
  74. CHECK (std::abs (bloom.amount - 0.4f) < 1.0e-4f && std::abs (bloom.size - 0.6f) < 1.0e-4f && std::abs (bloom.opacity - 0.9f) < 1.0e-4f);
  75. const auto vignette = s->layers[5];
  76. CHECK (vignette.effect == effectIndex (LayerType::postprocess, "vignette") && std::abs (vignette.amount - 0.8f) < 1.0e-4f);
  77. CHECK (unmapped == std::vector<std::string> { "Misc\\Unknown Thing" });
  78. if (! realFlpDir.empty())
  79. {
  80. // FL's presets: how many of their layers now have a counterpart (report), and every scene still draws
  81. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / "ZGameEditor Visualizer";
  82. int layers = 0, missing = 0, shader = 0;
  83. std::error_code ec;
  84. for (auto it = std::filesystem::recursive_directory_iterator (dir, ec); ! ec && it != std::filesystem::recursive_directory_iterator(); it.increment (ec))
  85. if (it->path().extension() == ".fst")
  86. {
  87. std::ifstream in (it->path(), std::ios::binary);
  88. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  89. std::vector<std::string> left;
  90. const auto scene = fromZge (bytes, &left);
  91. CHECK (scene.has_value());
  92. if (! scene)
  93. continue;
  94. layers += (int) scene->layers.size();
  95. missing += (int) left.size();
  96. shader += (int) std::count_if (scene->layers.begin(), scene->layers.end(), [] (const Layer& l) { return isShaderType (l.type); });
  97. }
  98. std::printf (" ZGE presets: %d layers mapped (%d GPU layers), %d without a counterpart\n", layers, shader, missing);
  99. CHECK (layers == 0 || shader > 100);
  100. }
  101. }
  102. TEST (dance165_state_keys_and_poses)
  103. {
  104. using namespace lynx::dance;
  105. // LYNX's own state round trip
  106. Settings s;
  107. s.move = hula;
  108. s.mirrored = true;
  109. s.visible = false;
  110. s.scale = 1.5f;
  111. s.speed = 2.0f;
  112. s.x = 900;
  113. s.y = 640;
  114. s.colour = 0xff20c0ff;
  115. const auto back = fromState (toState (s));
  116. CHECK (back && *back == s);
  117. CHECK (read (toState (s)) == s);
  118. CHECK (! fromState ({ 1, 2, 3 }) && read ({}) == Settings {});
  119. // FL's state: int version, then move, mirrored, shown, blend (/256; round 190ba: it is FL's "Blend" slider, not a
  120. // size), speed, x, y (+ display options)
  121. std::vector<std::uint8_t> fl (40, 0);
  122. auto put = [&fl] (int at, std::int32_t v) { std::memcpy (fl.data() + at, &v, 4); };
  123. put (0, 1);
  124. put (4, jumping);
  125. put (8, 1);
  126. put (12, 1);
  127. put (16, 96);
  128. put (20, 0);
  129. put (24, 300);
  130. put (28, 420);
  131. const auto f = fromFl (fl);
  132. CHECK (f && f->move == jumping && f->mirrored && f->visible && f->scale == 1.0f && std::abs (f->blend - 0.375f) < 1.0e-6f && f->speed == 1.0f && f->x == 300 && f->y == 420);
  133. put (20, 96);
  134. CHECK (fromFl (fl) && std::abs (fromFl (fl)->speed - 4.0f) < 1.0e-6f);
  135. put (20, -96);
  136. CHECK (fromFl (fl) && std::abs (fromFl (fl)->speed - 0.25f) < 1.0e-6f);
  137. CHECK (! fromFl (std::vector<std::uint8_t> (39, 0)));
  138. put (0, 0);
  139. CHECK (! fromFl (fl));
  140. // FL's key map (REA_rodada165e §3: the table its TriggerVoice reads)
  141. CHECK (actionForKey (59).kind == KeyAction::Kind::move && actionForKey (59).move == freestyle && ! actionForKey (59).mirrored);
  142. CHECK (actionForKey (61).move == stepping && ! actionForKey (61).mirrored);
  143. CHECK (actionForKey (48).kind == KeyAction::Kind::move && actionForKey (48).move == freestyle && actionForKey (48).mirrored);
  144. CHECK (actionForKey (46).move == stepping && actionForKey (46).mirrored);
  145. CHECK (actionForKey (hideKey).kind == KeyAction::Kind::hide && actionForKey (showKey).kind == KeyAction::Kind::show);
  146. CHECK (actionForKey (100).kind == KeyAction::Kind::none && actionForKey (20).kind == KeyAction::Kind::none);
  147. CHECK (actionForKey (firstMoveKey + moveCount - 1).move == moveCount - 1);
  148. CHECK (actionForKey (firstMoveKey + moveCount).kind == KeyAction::Kind::none);
  149. // poses: deterministic, finite, moving (except the held pose's arms), Freestyle never picks Waiting / Pose
  150. for (int m = 0; m < moveCount; ++m)
  151. {
  152. float travel = 0.0f;
  153. Pose prev = pose (m, 0.0, 7);
  154. for (int k = 1; k <= 64; ++k)
  155. {
  156. const auto p = pose (m, k / 16.0, 7);
  157. CHECK (std::isfinite (p.x) && std::isfinite (p.y) && std::isfinite (p.lean) && std::isfinite (p.shoulder[0]) && std::isfinite (p.knee[1]));
  158. CHECK (std::abs (p.turn) <= 1.0f + 1.0e-6f && p.squash > 0.5f && p.squash <= 1.0f + 1.0e-6f);
  159. travel += std::abs (p.shoulder[0] - prev.shoulder[0]) + std::abs (p.y - prev.y) + std::abs (p.hipShift - prev.hipShift) + std::abs (p.turn - prev.turn);
  160. prev = p;
  161. }
  162. CHECK (travel > 0.01f);
  163. const auto a = pose (m, 3.37, 11), b = pose (m, 3.37, 11);
  164. CHECK (a.shoulder == b.shoulder && a.y == b.y);
  165. }
  166. for (std::int64_t i = 0; i < 200; ++i)
  167. {
  168. const auto m = freestyleMove (i, 3);
  169. CHECK (m >= stepping && m <= spin);
  170. }
  171. // a mix halfway is between the two
  172. const auto p0 = pose (held, 0.0), p1 = pose (zombie, 0.0);
  173. const auto mid = Pose::mix (p0, p1, 0.5f);
  174. CHECK (std::abs (mid.shoulder[0] - 0.5f * (p0.shoulder[0] + p1.shoulder[0])) < 1.0e-5f);
  175. }
  176. TEST (dance165_silent_channel_signals)
  177. {
  178. using namespace lynx;
  179. auto inst = dance::makeInstrument();
  180. auto* sig = dance::signalsOf (inst.get());
  181. CHECK (sig != nullptr);
  182. CHECK (dance::signalsOf (makeInstrument (InstrumentKind::wasp).get()) == nullptr);
  183. if (sig == nullptr)
  184. return;
  185. inst->prepare (48000.0);
  186. std::uint32_t seen = 0;
  187. int keys[16] {};
  188. CHECK (sig->pull (seen, keys, 16) == 0);
  189. inst->noteOn (60, 0.8f);
  190. inst->noteOn (53, 0.8f);
  191. // it makes no sound
  192. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  193. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  194. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  195. CHECK (sig->pull (seen, keys, 16) == 2 && keys[0] == 60 && keys[1] == 53);
  196. CHECK (sig->pull (seen, keys, 16) == 0);
  197. // more than the ring holds between two reads: the newest 16
  198. for (int k = 0; k < 40; ++k)
  199. inst->noteOn (k, 1.0f);
  200. CHECK (sig->pull (seen, keys, 16) == 16 && keys[0] == 24 && keys[15] == 39);
  201. // the made channel of the kind is the dance instrument
  202. CHECK (dance::signalsOf (makeInstrument (InstrumentKind::fruitydance).get()) != nullptr);
  203. }