From 772fa6b67853f0e9bf39e6e3faf012c3c761fe24 Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 4 Jul 2026 20:41:01 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B7=E3=83=BC=E3=83=B3=E6=8F=8F=E7=94=BB?= =?UTF-8?q?=E3=81=AE=E8=AA=AD=E3=81=BF=E6=88=BB=E3=81=97=E3=83=AC=E3=83=B3?= =?UTF-8?q?=E3=83=80=E3=83=A9=E3=83=BC=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript/Scene/SceneImageRenderer.cs | 83 +++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 LuaScript/Scene/SceneImageRenderer.cs diff --git a/LuaScript/Scene/SceneImageRenderer.cs b/LuaScript/Scene/SceneImageRenderer.cs new file mode 100644 index 0000000..73cb495 --- /dev/null +++ b/LuaScript/Scene/SceneImageRenderer.cs @@ -0,0 +1,83 @@ +using Vortice; +using YukkuriMovieMaker.Commons; +using YukkuriMovieMaker.Player.Video; + +namespace LuaScript +{ + internal sealed class SceneImageRenderer(IGraphicsDevicesAndContext devices) : IDisposable + { + [ThreadStatic] + private static bool t_rendering; + + private readonly Dictionary _sources = []; + private GraphicsDevicesAndContext? _ctx; + private PixelBufferManager? _pixels; + + public bool TryRender(EffectDescription desc, string name, double time, out byte[] pixels, out int width, out int height) + { + pixels = []; + width = 0; + height = 0; + + if (t_rendering) + return false; + + var info = FindScene(desc, name); + if (info is null) + return false; + + _ctx ??= new GraphicsDevicesAndContext(devices); + if (!_sources.TryGetValue(info.ID, out var source)) + { + if (!info.TryCreateVideoSource(_ctx, out source)) + return false; + _sources[info.ID] = source; + } + + int w = Math.Max(1, info.Width); + int h = Math.Max(1, info.Height); + + t_rendering = true; + try + { + source.Update(TimeSpan.FromSeconds(time), desc.Usage); + _pixels ??= new PixelBufferManager(_ctx); + pixels = _pixels.LoadInputPixels(source.Output, new RawRectF(-w / 2f, -h / 2f, w / 2f, h / 2f), w, h); + } + catch + { + _sources.Remove(info.ID); + source.Dispose(); + pixels = []; + return false; + } + finally + { + t_rendering = false; + } + + width = w; + height = h; + return true; + } + + private static ISceneInfo? FindScene(EffectDescription desc, string name) + { + foreach (var info in desc.Scenes) + { + if (info.ID != desc.SceneId && string.Equals(info.Name, name, StringComparison.Ordinal)) + return info; + } + return null; + } + + public void Dispose() + { + foreach (var source in _sources.Values) + source.Dispose(); + _sources.Clear(); + _pixels?.Dispose(); + _ctx?.Dispose(); + } + } +}