mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-19 14:53:50 +02:00
Implement GPU Accelerated VR Overlay (#944)
This commit is contained in:
@@ -10,116 +10,76 @@ using CefSharp.OffScreen;
|
||||
using CefSharp.Structs;
|
||||
using SharpDX.Direct3D11;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using SharpDX.Direct3D;
|
||||
using SharpDX.Mathematics.Interop;
|
||||
using Range = CefSharp.Structs.Range;
|
||||
|
||||
namespace VRCX
|
||||
{
|
||||
public class OffScreenBrowser : ChromiumWebBrowser, IRenderHandler
|
||||
{
|
||||
private readonly ReaderWriterLockSlim _paintBufferLock;
|
||||
private GCHandle _paintBuffer;
|
||||
private int _width;
|
||||
private int _height;
|
||||
private Device _device;
|
||||
private Device1 _device1;
|
||||
private DeviceMultithread _deviceMultithread;
|
||||
private Query _query;
|
||||
private Texture2D _renderTarget;
|
||||
|
||||
public OffScreenBrowser(string address, int width, int height)
|
||||
: base(
|
||||
address,
|
||||
new BrowserSettings()
|
||||
{
|
||||
DefaultEncoding = "UTF-8"
|
||||
}
|
||||
)
|
||||
: base(address, automaticallyCreateBrowser: false)
|
||||
{
|
||||
_paintBufferLock = new ReaderWriterLockSlim();
|
||||
var windowInfo = new WindowInfo();
|
||||
windowInfo.SetAsWindowless(IntPtr.Zero);
|
||||
windowInfo.WindowlessRenderingEnabled = true;
|
||||
windowInfo.SharedTextureEnabled = true;
|
||||
windowInfo.Width = width;
|
||||
windowInfo.Height = height;
|
||||
|
||||
var browserSettings = new BrowserSettings()
|
||||
{
|
||||
DefaultEncoding = "UTF-8",
|
||||
WindowlessFrameRate = 60
|
||||
};
|
||||
|
||||
CreateBrowser(windowInfo, browserSettings);
|
||||
|
||||
Size = new System.Drawing.Size(width, height);
|
||||
RenderHandler = this;
|
||||
|
||||
|
||||
JavascriptBindings.ApplyVrJavascriptBindings(JavascriptObjectRepository);
|
||||
}
|
||||
|
||||
public void UpdateRender(Device device, Texture2D renderTarget)
|
||||
{
|
||||
_device = device;
|
||||
_device1 = _device.QueryInterface<Device1>();
|
||||
|
||||
_deviceMultithread?.Dispose();
|
||||
_deviceMultithread = _device.QueryInterfaceOrNull<DeviceMultithread>();
|
||||
_deviceMultithread?.SetMultithreadProtected(true);
|
||||
|
||||
_renderTarget = renderTarget;
|
||||
|
||||
_query?.Dispose();
|
||||
_query = new Query(_device, new QueryDescription
|
||||
{
|
||||
Type = QueryType.Event,
|
||||
Flags = QueryFlags.None
|
||||
});
|
||||
}
|
||||
|
||||
public new void Dispose()
|
||||
{
|
||||
RenderHandler = null;
|
||||
base.Dispose();
|
||||
|
||||
_paintBufferLock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (_paintBuffer.IsAllocated == true)
|
||||
{
|
||||
_paintBuffer.Free();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_paintBufferLock.ExitWriteLock();
|
||||
}
|
||||
|
||||
_paintBufferLock.Dispose();
|
||||
}
|
||||
|
||||
public void RenderToTexture(Texture2D texture)
|
||||
{
|
||||
// Safeguard against uninitialized texture
|
||||
if (texture == null)
|
||||
return;
|
||||
|
||||
_paintBufferLock.EnterReadLock();
|
||||
try
|
||||
{
|
||||
if (_width > 0 &&
|
||||
_height > 0)
|
||||
{
|
||||
var context = texture.Device.ImmediateContext;
|
||||
var dataBox = context.MapSubresource(
|
||||
texture,
|
||||
0,
|
||||
MapMode.WriteDiscard,
|
||||
MapFlags.None
|
||||
);
|
||||
if (dataBox.IsEmpty == false)
|
||||
{
|
||||
var sourcePtr = _paintBuffer.AddrOfPinnedObject();
|
||||
var destinationPtr = dataBox.DataPointer;
|
||||
var pitch = _width * 4;
|
||||
var rowPitch = dataBox.RowPitch;
|
||||
if (pitch == rowPitch)
|
||||
{
|
||||
WinApi.RtlCopyMemory(
|
||||
destinationPtr,
|
||||
sourcePtr,
|
||||
(uint)(_width * _height * 4)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var y = _height; y > 0; --y)
|
||||
{
|
||||
WinApi.RtlCopyMemory(
|
||||
destinationPtr,
|
||||
sourcePtr,
|
||||
(uint)pitch
|
||||
);
|
||||
sourcePtr += pitch;
|
||||
destinationPtr += rowPitch;
|
||||
}
|
||||
}
|
||||
}
|
||||
context.UnmapSubresource(texture, 0);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_paintBufferLock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
ScreenInfo? IRenderHandler.GetScreenInfo()
|
||||
{
|
||||
return null;
|
||||
return new ScreenInfo
|
||||
{
|
||||
DeviceScaleFactor = 1.0F
|
||||
};
|
||||
}
|
||||
|
||||
bool IRenderHandler.GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY)
|
||||
@@ -136,7 +96,24 @@ namespace VRCX
|
||||
|
||||
void IRenderHandler.OnAcceleratedPaint(PaintElementType type, Rect dirtyRect, AcceleratedPaintInfo paintInfo)
|
||||
{
|
||||
// NOT USED
|
||||
if (type != PaintElementType.View)
|
||||
return;
|
||||
|
||||
if (_device == null)
|
||||
return;
|
||||
|
||||
using Texture2D cefTexture = _device1.OpenSharedResource1<Texture2D>(paintInfo.SharedTextureHandle);
|
||||
_device.ImmediateContext.CopyResource(cefTexture, _renderTarget);
|
||||
_device.ImmediateContext.End(_query);
|
||||
_device.ImmediateContext.Flush();
|
||||
|
||||
RawBool q = _device.ImmediateContext.GetData<RawBool>(_query, AsynchronousFlags.DoNotFlush);
|
||||
|
||||
while (!q)
|
||||
{
|
||||
Thread.Yield();
|
||||
q = _device.ImmediateContext.GetData<RawBool>(_query, AsynchronousFlags.DoNotFlush);
|
||||
}
|
||||
}
|
||||
|
||||
void IRenderHandler.OnCursorChange(IntPtr cursor, CursorType type, CursorInfo customCursorInfo)
|
||||
@@ -149,37 +126,6 @@ namespace VRCX
|
||||
|
||||
void IRenderHandler.OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buffer, int width, int height)
|
||||
{
|
||||
if (type == PaintElementType.View)
|
||||
{
|
||||
_paintBufferLock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (_width != width ||
|
||||
_height != height)
|
||||
{
|
||||
_width = width;
|
||||
_height = height;
|
||||
if (_paintBuffer.IsAllocated == true)
|
||||
{
|
||||
_paintBuffer.Free();
|
||||
}
|
||||
_paintBuffer = GCHandle.Alloc(
|
||||
new byte[_width * _height * 4],
|
||||
GCHandleType.Pinned
|
||||
);
|
||||
}
|
||||
|
||||
WinApi.RtlCopyMemory(
|
||||
_paintBuffer.AddrOfPinnedObject(),
|
||||
buffer,
|
||||
(uint)(width * height * 4)
|
||||
);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_paintBufferLock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IRenderHandler.OnPopupShow(bool show)
|
||||
|
||||
Reference in New Issue
Block a user