using System; using Vortice; using System.Numerics; using Vortice.DCommon; using Vortice.Direct2D1; using Vortice.Direct2D1.Effects; using Vortice.DXGI; using Vortice.Mathematics; using YukkuriMovieMaker.Commons; namespace LuaScript { internal sealed class PixelBufferManager : IDisposable { private readonly GraphicsDevicesAndContext _ctx; private ID2D1Bitmap1? _renderTarget; private ID2D1Bitmap1? _stagingBitmap; private ID2D1Bitmap1? _outputBitmap; private AffineTransform2D? _transformEffect; private ID2D1Image? _transformOutput; private byte[]? _pixelBuffer; private int _bitmapWidth; private int _bitmapHeight; private RawRectF _cachedBounds; public PixelBufferManager(GraphicsDevicesAndContext ctx) { _ctx = ctx; } public unsafe byte[] LoadInputPixels(ID2D1Image input, RawRectF bounds, int width, int height) { EnsureBitmaps(width, height); RenderToStaging(input, bounds); return ReadStagingPixels(width, height); } public byte[] LoadBrushPixels(ID2D1Brush brush, int width, int height) { EnsureBitmaps(width, height); var dc = _ctx.DeviceContext; var rt = (ID2D1RenderTarget)dc; using var savedTarget = dc.Target; dc.Target = _renderTarget; dc.BeginDraw(); dc.Clear(null); rt.Transform = Matrix3x2.CreateTranslation(width / 2f, height / 2f); rt.FillRectangle(new RawRectF(-width / 2f, -height / 2f, width / 2f, height / 2f), brush); rt.Transform = Matrix3x2.Identity; dc.EndDraw(); dc.Target = savedTarget; _stagingBitmap!.CopyFromBitmap(_renderTarget!); return ReadStagingPixels(width, height); } private unsafe byte[] ReadStagingPixels(int width, int height) { var mapped = _stagingBitmap!.Map(MapOptions.Read); try { fixed (byte* destination = _pixelBuffer) CopyStagingRows(mapped.Bits, mapped.Pitch, destination, _pixelBuffer!.Length, width, height); } finally { _stagingBitmap.Unmap(); } return _pixelBuffer!; } public unsafe void LoadInputPixelsInto(ID2D1Image input, RawRectF bounds, int width, int height, nint destination, long capacity) { EnsureBitmaps(width, height); RenderToStaging(input, bounds); var mapped = _stagingBitmap!.Map(MapOptions.Read); try { CopyStagingRows(mapped.Bits, mapped.Pitch, (byte*)destination, capacity, width, height); } finally { _stagingBitmap.Unmap(); } } public unsafe void WritePixelsToOutput(byte[] pixels, int width, int height) { EnsureBitmaps(width, height); fixed (byte* ptr = pixels) _outputBitmap!.CopyFromMemory(new nint(ptr), width * 4); } public void WritePixelsToOutput(nint source, int width, int height) { EnsureBitmaps(width, height); _outputBitmap!.CopyFromMemory(source, width * 4); } private void RenderToStaging(ID2D1Image input, RawRectF bounds) { var dc = _ctx.DeviceContext; using var savedTarget = dc.Target; dc.Target = _renderTarget; dc.BeginDraw(); dc.Clear(null); dc.DrawImage(input, new Vector2(-bounds.Left, -bounds.Top)); dc.EndDraw(); dc.Target = savedTarget; _stagingBitmap!.CopyFromBitmap(_renderTarget!); } private static unsafe void CopyStagingRows(nint bits, int pitch, byte* destination, long capacity, int width, int height) { long required = (long)width * height * 4; if (required > capacity) throw new InvalidOperationException("Pixel destination is smaller than the mapped region."); byte* source = (byte*)bits; if (pitch == width * 4) { Buffer.MemoryCopy(source, destination, capacity, required); return; } long rowBytes = (long)width * 4; for (int row = 0; row < height; row++) Buffer.MemoryCopy( source + (long)pitch * row, destination + rowBytes * row, capacity - rowBytes * row, rowBytes); } public ID2D1Image GetTransformOutput(float left, float top) { if (_transformEffect is null) { _transformEffect = new AffineTransform2D(_ctx.DeviceContext); _transformEffect.SetInput(0, _outputBitmap, true); _transformOutput = _transformEffect.Output; _cachedBounds = default; } if (_cachedBounds.Left != left || _cachedBounds.Top != top) { _transformEffect.TransformMatrix = Matrix3x2.CreateTranslation(left, top); _cachedBounds = new RawRectF(left, top, left, top); } return _transformOutput!; } private void EnsureBitmaps(int width, int height) { if (_bitmapWidth == width && _bitmapHeight == height) return; _renderTarget?.Dispose(); _stagingBitmap?.Dispose(); _outputBitmap?.Dispose(); _transformOutput?.Dispose(); _transformEffect?.Dispose(); _renderTarget = null; _stagingBitmap = null; _outputBitmap = null; _transformOutput = null; _transformEffect = null; _bitmapWidth = 0; _bitmapHeight = 0; var dc = _ctx.DeviceContext; _renderTarget = dc.CreateEmptyBitmap(width, height, BitmapOptions.Target); var stagingProps = new BitmapProperties1( new PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied), 96f, 96f, BitmapOptions.CpuRead | BitmapOptions.CannotDraw); _stagingBitmap = dc.CreateBitmap( new SizeI(width, height), nint.Zero, width * 4, stagingProps); _outputBitmap = dc.CreateEmptyBitmap(width, height, BitmapOptions.Target); _pixelBuffer = new byte[width * height * 4]; _bitmapWidth = width; _bitmapHeight = height; } public void Dispose() { _renderTarget?.Dispose(); _stagingBitmap?.Dispose(); _outputBitmap?.Dispose(); _transformOutput?.Dispose(); _transformEffect?.Dispose(); _renderTarget = null; _stagingBitmap = null; _outputBitmap = null; _transformOutput = null; _transformEffect = null; } } }