ネイティブ画素転送をD2DステージングとMMFの直結へ変更

このコミットが含まれているのは:
routersys committed 2026-07-02 19:28:01 +09:00
1 親 865f5117ea
コミット 8ca66bceb8
6ファイル変更
+285 -132

検索条件に一致するファイルはありません

+31 -37
ファイルの表示
@@ -29,7 +29,6 @@ namespace LuaScript.Engine
private readonly double[] _anchorBuffer = new double[Anchor.AnchorSupport.MaxAnchors * 3];
private int _lastCallbackTagLength = -1;
private string _lastCallbackTagText = string.Empty;
private byte[]? _resultBuffer;
private int _scriptVersion;
private string? _lastScript;
private byte[] _lastScriptBytes = Array.Empty<byte>();
@@ -52,7 +51,7 @@ namespace LuaScript.Engine
string script,
double[] fields,
IReadOnlyDictionary<string, string> stringParameters,
Func<byte[]> loadPixels,
PixelRegionAccess loadPixels,
int width,
int height,
int timeoutMs,
@@ -66,14 +65,12 @@ namespace LuaScript.Engine
Action<string, int, bool, int, double[]> setAnchor,
out bool pixelsDirty,
out bool bufferReplaced,
out byte[]? resultPixels,
out int resultWidth,
out int resultHeight,
out string? error)
{
pixelsDirty = false;
bufferReplaced = false;
resultPixels = null;
resultWidth = width;
resultHeight = height;
error = null;
@@ -114,7 +111,6 @@ namespace LuaScript.Engine
_workEvent!.Set();
byte[]? uploadedPixels = null;
var stopwatch = Stopwatch.StartNew();
int status;
while (true)
@@ -134,8 +130,15 @@ namespace LuaScript.Engine
int callbackKind = view.ReadInt32(NativeProtocol.OffCallbackKind);
if (callbackKind == NativeProtocol.CbKindRequestPixels)
{
uploadedPixels ??= loadPixels();
WritePixelRegion(uploadedPixels, uploadedPixels.Length);
try
{
AccessPixelRegion(loadPixels);
}
catch
{
KillWorker();
throw;
}
}
else if (callbackKind == NativeProtocol.CbKindFlushDraws)
{
@@ -169,26 +172,33 @@ namespace LuaScript.Engine
bufferReplaced = resultWidth != width || resultHeight != height;
pixelsDirty = view.ReadInt32(NativeProtocol.OffPixelsDirty) != 0;
if (pixelsDirty)
if (pixelsDirty && (long)resultWidth * resultHeight * 4 > _allocatedSize - _pixelOffset)
{
int pixelSize = resultWidth * resultHeight * 4;
if (!bufferReplaced && uploadedPixels is not null)
{
ReadPixelRegion(uploadedPixels, pixelSize);
resultPixels = uploadedPixels;
}
else
{
if (_resultBuffer == null || _resultBuffer.Length < pixelSize)
_resultBuffer = new byte[pixelSize];
ReadPixelRegion(_resultBuffer, pixelSize);
resultPixels = _resultBuffer;
}
error = "native worker reported a pixel buffer beyond capacity";
return false;
}
return true;
}
public void ReadPixels(PixelRegionAccess reader) => AccessPixelRegion(reader);
private unsafe void AccessPixelRegion(PixelRegionAccess access)
{
var handle = _view!.SafeMemoryMappedViewHandle;
byte* ptr = null;
handle.AcquirePointer(ref ptr);
try
{
long offset = _view.PointerOffset + _pixelOffset;
access((nint)(ptr + offset), (long)handle.ByteLength - offset);
}
finally
{
handle.ReleasePointer();
}
}
private unsafe void WritePixelRegion(byte[] source, int length)
{
var handle = _view!.SafeMemoryMappedViewHandle;
@@ -205,22 +215,6 @@ namespace LuaScript.Engine
}
}
private unsafe void ReadPixelRegion(byte[] destination, int length)
{
var handle = _view!.SafeMemoryMappedViewHandle;
byte* ptr = null;
handle.AcquirePointer(ref ptr);
try
{
fixed (byte* dst = destination)
Buffer.MemoryCopy(ptr + _view.PointerOffset + _pixelOffset, dst, destination.Length, length);
}
finally
{
handle.ReleasePointer();
}
}
private static int MeasureStringParameters(IReadOnlyDictionary<string, string> stringParameters)
{
int total = 4;
+4
ファイルの表示
@@ -0,0 +1,4 @@
namespace LuaScript.Engine
{
internal delegate void PixelRegionAccess(nint address, long capacity);
}
+60 -15
ファイルの表示
@@ -110,8 +110,14 @@ namespace LuaScript
private bool _hasExplicitDirective;
private ScriptEngineKind _explicitDirectiveKind;
private byte[]? _nativeInputPixels;
private Func<byte[]>? _nativeLoadInput;
private PixelRegionAccess? _nativeUpload;
private PixelRegionAccess? _nativeReadIntoResult;
private PixelRegionAccess? _nativeWriteOutput;
private byte[]? _nativeResultBuffer;
private int _nativeReadLength;
private int _nativeOutputWidth;
private int _nativeOutputHeight;
private bool _nativeUploaded;
private Func<string, int, SceneObjectInfo?>? _nativeResolveObject;
private Func<string, int, double, double, double, (byte[] buffer, int w, int h)>? _nativeLoadFigure;
private Func<string, string, double, bool, bool, int, (byte[] buffer, int w, int h)>? _nativeLoadText;
@@ -848,7 +854,7 @@ namespace LuaScript
{
_nativeWorker ??= new LuaJitWorker(NativeDirectory);
_nativeFields ??= new double[NativeProtocol.FieldCount];
_nativeLoadInput ??= () => _nativeInputPixels ??= LoadPendingInputPixels();
_nativeUpload ??= UploadNativeInputPixels;
_nativeResolveObject ??= (tag, frame) => _context.ResolveObject(tag, frame, out var info) ? info : null;
_nativeLoadFigure ??= NativeLoadFigure;
_nativeLoadText ??= NativeLoadText;
@@ -859,10 +865,10 @@ namespace LuaScript
_nativeSetAnchor ??= ResolveNativeAnchor;
NativeFieldMap.ToFields(ctx, _nativeFields);
_nativeInputPixels = null;
_nativeUploaded = false;
bool ok = _nativeWorker.Execute(
script, _nativeFields, ctx.StringParameters, _nativeLoadInput, imgW, imgH, NativeTimeoutMilliseconds,
script, _nativeFields, ctx.StringParameters, _nativeUpload, imgW, imgH, NativeTimeoutMilliseconds,
_nativeResolveObject,
_nativeLoadFigure,
_nativeLoadText,
@@ -871,7 +877,7 @@ namespace LuaScript
_nativeAddEffect,
_nativeAddDraw,
_nativeSetAnchor,
out bool dirty, out bool bufferReplaced, out byte[]? resultPixels,
out bool dirty, out bool bufferReplaced,
out int resultW, out int resultH, out string? error);
if (!ok)
@@ -885,31 +891,35 @@ namespace LuaScript
bool hasDraws = ctx.DrawCommands.Count > 0;
if (dirty && resultPixels is not null)
if (dirty)
{
int outW = bufferReplaced ? resultW : imgW;
int outH = bufferReplaced ? resultH : imgH;
if (bufferReplaced)
ctx.ReplaceBuffer(resultPixels, outW, outH);
if (hasDraws)
{
ctx.SetResolvedBuffer(resultPixels, outW, outH);
var result = ReadNativeResultPixels(outW, outH);
if (bufferReplaced)
ctx.ReplaceBuffer(result, outW, outH);
else
ctx.SetResolvedBuffer(result, outW, outH);
return true;
}
_pixelManager!.WritePixelsToOutput(resultPixels, outW, outH);
_nativeOutputWidth = outW;
_nativeOutputHeight = outH;
_nativeWorker.ReadPixels(_nativeWriteOutput ??= WriteNativeOutputPixels);
if (bufferReplaced)
effectOutput = _pixelManager.GetTransformOutput(-outW / 2f, -outH / 2f);
effectOutput = _pixelManager!.GetTransformOutput(-outW / 2f, -outH / 2f);
else
effectOutput = _pixelManager.GetTransformOutput(bounds.Left, bounds.Top);
effectOutput = _pixelManager!.GetTransformOutput(bounds.Left, bounds.Top);
return true;
}
if (hasDraws)
{
ctx.SetResolvedBuffer(_nativeLoadInput(), imgW, imgH);
if (_nativeUploaded)
ctx.SetResolvedBuffer(ReadNativeResultPixels(imgW, imgH), imgW, imgH);
return false;
}
@@ -917,6 +927,41 @@ namespace LuaScript
return false;
}
private void UploadNativeInputPixels(nint address, long capacity)
{
_nativeUploaded = true;
_pixelLoaderSemaphore.Wait();
try
{
_pixelManager!.LoadInputPixelsInto(input!, _pixelBounds, _pixelWidth, _pixelHeight, address, capacity);
}
finally
{
_pixelLoaderSemaphore.Release();
}
}
private byte[] ReadNativeResultPixels(int width, int height)
{
int length = width * height * 4;
if (_nativeResultBuffer is null || _nativeResultBuffer.Length < length)
_nativeResultBuffer = new byte[length];
_nativeReadLength = length;
_nativeWorker!.ReadPixels(_nativeReadIntoResult ??= ReadNativePixelsIntoResult);
return _nativeResultBuffer;
}
private unsafe void ReadNativePixelsIntoResult(nint address, long capacity)
{
fixed (byte* destination = _nativeResultBuffer)
Buffer.MemoryCopy((void*)address, destination, _nativeResultBuffer!.Length, Math.Min(_nativeReadLength, capacity));
}
private void WriteNativeOutputPixels(nint address, long capacity)
{
_pixelManager!.WritePixelsToOutput(address, _nativeOutputWidth, _nativeOutputHeight);
}
private void ResolveNativeAnchor(string group, int count, bool is3D, int connection, double[] positions)
{
_context.AddAnchorRequest(new AnchorRequestData(group, count, (AnchorConnection)connection, is3D));
+66 -30
ファイルの表示
@@ -1,7 +1,6 @@
using System;
using Vortice;
using System.Numerics;
using System.Runtime.InteropServices;
using Vortice.DCommon;
using Vortice.Direct2D1;
using Vortice.Direct2D1.Effects;
@@ -29,10 +28,56 @@ namespace LuaScript
_ctx = ctx;
}
public byte[] LoadInputPixels(ID2D1Image input, RawRectF bounds, int width, int height)
public unsafe byte[] LoadInputPixels(ID2D1Image input, RawRectF bounds, int width, int height)
{
EnsureBitmaps(width, height);
RenderToStaging(input, bounds);
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;
@@ -44,37 +89,28 @@ namespace LuaScript
dc.Target = savedTarget;
_stagingBitmap!.CopyFromBitmap(_renderTarget!);
var mapped = _stagingBitmap.Map(MapOptions.Read);
try
{
if (mapped.Pitch == width * 4)
{
Marshal.Copy(mapped.Bits, _pixelBuffer!, 0, width * height * 4);
}
else
{
for (int row = 0; row < height; row++)
Marshal.Copy(
mapped.Bits + mapped.Pitch * row,
_pixelBuffer!,
row * width * 4,
width * 4);
}
}
finally
{
_stagingBitmap.Unmap();
}
return _pixelBuffer!;
}
public unsafe void WritePixelsToOutput(byte[] pixels, int width, int height)
private static unsafe void CopyStagingRows(nint bits, int pitch, byte* destination, long capacity, int width, int height)
{
EnsureBitmaps(width, height);
fixed (byte* ptr = pixels)
_outputBitmap!.CopyFromMemory(new nint(ptr), width * 4);
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)