Skip to content

Commit

Permalink
Merge branch 'DComp' of https://github.com/RERASER/OngekiFumenEditor
Browse files Browse the repository at this point in the history
…into DComp

# Conflicts:
#	OngekiFumenEditor/Modules/FumenVisualEditor/Views/FumenVisualEditorView.xaml
  • Loading branch information
MikiraSora committed Oct 27, 2024
2 parents 92a8866 + 83b91d1 commit 98f5822
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 221 deletions.
93 changes: 57 additions & 36 deletions Dependences/GLWpfControl/DComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ public class DComp
private Vortice.Direct3D12.ID3D12Device _device12 = null;
private Vortice.Direct3D12.ID3D12CommandQueue _commandQueue12 = null;

private Vortice.DXGI.IDXGIDevice _DXGIDevice = null;
private Vortice.DXGI.IDXGIFactory2 _DXGIFactory2 = null;
private Vortice.DXGI.IDXGIDevice1 _DXGIDevice1 = null;
private Vortice.DXGI.IDXGIOutput _DXGIOutput = null;
private Vortice.DXGI.IDXGISwapChain1 _DXGISwapChain1 = null;
private Vortice.DXGI.IDXGISurface1 _DXGISwapChainSurface = null;

private Vortice.DirectComposition.IDCompositionDevice _DcompDevice = null;
private Vortice.DirectComposition.IDCompositionTarget _DcompTarget = null;
private Vortice.DirectComposition.IDCompositionVisual _DcompVisual = null;
private Vortice.DirectComposition.IDCompositionSurface _DcompSurface = null;
//private Vortice.DirectComposition.IDCompositionSurface _DcompSurface = null;

private Vortice.Direct2D1.ID2D1Factory1 _factory2D = null;
private Vortice.Direct2D1.ID2D1Device _device2D = null;
private Vortice.Direct2D1.ID2D1DeviceContext _deviceContext2D = null;
private Vortice.Direct2D1.ID2D1RenderTarget _D2D1RenderTarget = null;

private DpiScale currentDpi;
private IntPtr hwndHost;
Expand Down Expand Up @@ -105,42 +109,52 @@ public void InitDirect3D(bool UseDirect3D12)
var hr = Vortice.Direct3D11.D3D11.D3D11CreateDevice(null, Vortice.Direct3D.DriverType.Hardware, Vortice.Direct3D11.DeviceCreationFlags.BgraSupport, [Vortice.Direct3D.FeatureLevel.Level_11_1], out _device);
_deviceContext = _device.CreateDeferredContext();
}
_DXGIDevice = _device.QueryInterface<Vortice.DXGI.IDXGIDevice>();
var adapter = _DXGIDevice.GetAdapter();
_DXGIDevice1 = _device.QueryInterface<Vortice.DXGI.IDXGIDevice1>();
_DXGIDevice1.SetMaximumFrameLatency(1);
var adapter = _DXGIDevice1.GetAdapter();
adapter.EnumOutputs(0, out _DXGIOutput);
adapter.Dispose();
_DXGIFactory2 = Vortice.DXGI.DXGI.CreateDXGIFactory2<Vortice.DXGI.IDXGIFactory2>(true);
//_DXGIFactory2 = _DXGIDevice1.GetParent<Vortice.DXGI.IDXGIFactory2>();
_factory2D = Vortice.Direct2D1.D2D1.D2D1CreateFactory<Vortice.Direct2D1.ID2D1Factory1>(Vortice.Direct2D1.FactoryType.MultiThreaded, Vortice.Direct2D1.DebugLevel.None);
_device2D = _factory2D.CreateDevice(_DXGIDevice);
_device2D = _factory2D.CreateDevice(_DXGIDevice1);
_deviceContext2D = _device2D.CreateDeviceContext(Vortice.Direct2D1.DeviceContextOptions.EnableMultithreadedOptimizations);
var hr2 = Vortice.DirectComposition.DComp.DCompositionCreateDevice2(_device2D, out _DcompDevice);
var hr4 = _DcompDevice.CreateVisual(out _DcompVisual);
_DXGISwapChain1 = _DXGIFactory2.CreateSwapChainForComposition(_device, new(1, 1, Vortice.DXGI.Format.R8G8B8A8_UNorm, swapEffect: Vortice.DXGI.SwapEffect.FlipDiscard, alphaMode: Vortice.DXGI.AlphaMode.Premultiplied, flags: Vortice.DXGI.SwapChainFlags.AllowTearing));
_DXGISwapChain1.BackgroundColor = new(16f / 256f, 16f / 256f, 16f / 256f, 1f);
_DcompVisual.SetContent(_DXGISwapChain1);
_DcompVisual.SetOffsetX(0);
_DcompVisual.SetOffsetY(0);
}

public void CreateRenderResources()
{
_texture2D = _device.CreateTexture2D(Vortice.DXGI.Format.R8G8B8A8_UNorm, (uint)hostWidth, (uint)hostHeight, bindFlags: Vortice.Direct3D11.BindFlags.RenderTarget | Vortice.Direct3D11.BindFlags.ShaderResource);
_texture2DDepth = _device.CreateTexture2D(Vortice.DXGI.Format.D24_UNorm_S8_UInt, (uint)hostWidth, (uint)hostHeight, bindFlags: Vortice.Direct3D11.BindFlags.DepthStencil);
_t2dSurface = _texture2D.QueryInterface<Vortice.DXGI.IDXGISurface1>();
var hr2 = Vortice.DirectComposition.DComp.DCompositionCreateDevice2(_device2D, out _DcompDevice);
_device.ImmediateContext.ClearState();
_device.ImmediateContext.Flush();
var hr = _DXGISwapChain1.ResizeBuffers(0, (uint)hostWidth, (uint)hostHeight,Vortice.DXGI.Format.R8G8B8A8_UNorm,swapChainFlags: Vortice.DXGI.SwapChainFlags.AllowTearing);

_DXGISwapChainSurface = _DXGISwapChain1.GetBuffer<Vortice.DXGI.IDXGISurface1>(0);
_D2D1RenderTarget = _factory2D.CreateDxgiSurfaceRenderTarget(_DXGISwapChainSurface, new() { DpiX = (float)(currentDpi.DpiScaleX * 96d), DpiY = (float)(currentDpi.DpiScaleY * 96d), PixelFormat = new(Vortice.DXGI.Format.R8G8B8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied), Type = Vortice.Direct2D1.RenderTargetType.Hardware, Usage = Vortice.Direct2D1.RenderTargetUsage.None, MinLevel = Vortice.Direct2D1.FeatureLevel.Level_10 });
var hr3 = _DcompDevice.CreateTargetForHwnd(hwndHost, true, out _DcompTarget);
var hr4 = _DcompDevice.CreateVisual(out _DcompVisual);
var hr5 = _DcompDevice.CreateSurface((uint)hostWidth, (uint)hostHeight, Vortice.DXGI.Format.R8G8B8A8_UNorm, Vortice.DXGI.AlphaMode.Premultiplied, out _DcompSurface);
_DcompVisual.SetContent(_DcompSurface);
_DcompVisual.SetOffsetX(0);
_DcompVisual.SetOffsetY(0);
//_DcompVisual.SetTransform(System.Numerics.Matrix3x2.CreateScale(1, -1, new(0f, hostHeight / 2f)));

_t2dBitmap = _D2D1RenderTarget.CreateSharedBitmap(_t2dSurface, new(new(Vortice.DXGI.Format.R8G8B8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied), (float)(currentDpi.DpiScaleX * 96d), (float)(currentDpi.DpiScaleY * 96d)));
_DcompTarget.SetRoot(_DcompVisual);
_DcompDevice.Commit();

}

public void DestoryRenderResources()
{
_DcompSurface?.Dispose();
_DcompSurface = null;
_DcompVisual?.Dispose();
_DcompVisual = null;
_DcompTarget?.Dispose();
_DcompTarget = null;
_DcompDevice?.Dispose();
_DcompDevice = null;
_D2D1RenderTarget?.Dispose();
_D2D1RenderTarget = null;
_DXGISwapChainSurface?.Dispose();
_DXGISwapChainSurface = null;
_t2dBitmap?.Dispose();
_t2dBitmap = null;
_t2dSurface?.Dispose();
Expand All @@ -153,10 +167,20 @@ public void DestoryRenderResources()

public void DestoryDirect3D()
{
_DcompVisual?.Dispose();
_DcompVisual = null;
_DcompTarget?.Dispose();
_DcompTarget = null;
_DcompDevice?.Dispose();
_DcompDevice = null;
_DXGIFactory2?.Dispose();
_DXGIFactory2 = null;
_DXGISwapChain1?.Dispose();
_DXGISwapChain1 = null;
_deviceContext2D?.Dispose();
_device2D?.Dispose();
_factory2D?.Dispose();
_DXGIDevice?.Dispose();
_DXGIDevice1?.Dispose();
_deviceContext?.Dispose();
_device?.Dispose();
_commandQueue12?.Dispose();
Expand All @@ -165,35 +189,32 @@ public void DestoryDirect3D()

public void Draw()
{
var rt = _DcompSurface.BeginDraw<Vortice.Direct2D1.ID2D1DeviceContext>(null, out _);
rt.Transform = System.Numerics.Matrix3x2.CreateScale(1, -1, new(0f, (float)(hostHeight / 2f / currentDpi.DpiScaleY)));
rt.SetDpi((float)(currentDpi.DpiScaleX * 96d), (float)(currentDpi.DpiScaleY * 96d));
if (_t2dBitmap == null)
{
_t2dBitmap = rt.CreateSharedBitmap(_t2dSurface, new(new(Vortice.DXGI.Format.R8G8B8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied), (float)(currentDpi.DpiScaleX * 96d), (float)(currentDpi.DpiScaleY * 96d)));
}
rt.DrawBitmap(_t2dBitmap);
rt.Transform = System.Numerics.Matrix3x2.Identity;
var brush = rt.CreateSolidColorBrush(new Vortice.Mathematics.Color(255, 255, 255, 255));
_D2D1RenderTarget.BeginDraw();
_D2D1RenderTarget.Transform = System.Numerics.Matrix3x2.CreateScale(1, -1, new(0f, (float)(hostHeight / 2f / currentDpi.DpiScaleY)));
//rt.SetDpi((float)(currentDpi.DpiScaleX * 96d), (float)(currentDpi.DpiScaleY * 96d));
_D2D1RenderTarget.DrawBitmap(_t2dBitmap);
_D2D1RenderTarget.Transform = System.Numerics.Matrix3x2.Identity;
//var brush = rt.CreateSolidColorBrush(new Vortice.Mathematics.Color(255, 255, 255, 255));
var queue = DWriteCore.GetCommands(this);
float height = (float)(hostHeight / currentDpi.DpiScaleY);
foreach (var item in queue)
{
item.Invoke(rt, height);
item.Invoke(_D2D1RenderTarget, height);
}
queue.Clear();
_DcompSurface.EndDraw();
rt.Dispose();
_D2D1RenderTarget.EndDraw();
_DXGISwapChain1.Present(0, Vortice.DXGI.PresentFlags.AllowTearing);
//rt.Dispose();
//_t2dBitmap.Dispose();

brush.Dispose();
_DcompDevice.Commit();
//brush.Dispose();
//_DcompDevice.Commit();
}

public void WaitForVBlank()
{
_DcompDevice.WaitForCommitCompletion();
//_DXGIOutput.WaitForVBlank();
//_DcompDevice.WaitForCommitCompletion();
_DXGIOutput.WaitForVBlank();
}
}
}
2 changes: 1 addition & 1 deletion Dependences/GLWpfControl/DCompGL.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OpenTK.Wpf"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
d:DesignHeight="450" d:DesignWidth="800" Background="#0F0F0F">
<Border Name="CompositionHostElement" Background="#0F0F0F"/>
</UserControl>
72 changes: 67 additions & 5 deletions Dependences/GLWpfControl/DCompGL.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vortice.Direct2D1.Effects;
using Vortice.Mathematics;

namespace OpenTK.Wpf
Expand All @@ -40,6 +41,8 @@ public partial class DCompGL : UserControl
public new event System.Windows.Input.MouseEventHandler MouseLeave { add { CompositionHostElement.MouseLeave += value; } remove { CompositionHostElement.MouseLeave -= value; } }
private object disposeLock = new object();
volatile bool loop = true;
private double hostWidthWithDPI;
private double hostHeightWithDPI;

public DCompGL()
{
Expand All @@ -52,10 +55,15 @@ public DCompGL()
private void DCompGL_Unloaded(object sender, RoutedEventArgs e)
{
loop = false;
hwndHost = IntPtr.Zero;
host?.Dispose();
}

private void DCompGL_Loaded(object sender, RoutedEventArgs e)
{
loop = true;
hostWidthWithDPI = RenderSize.Width;
hostHeightWithDPI = RenderSize.Height;
host = new(GetParentSize);
host.Resized += HostResized;
CompositionHostElement.Child = host;
Expand All @@ -73,6 +81,13 @@ private void HostResized(IntPtr hWnd)
hwndHost = hWnd;
LoopThread = new(EntryPoint);
LoopThread.Start();
//host.WMPaint += GLCore.RenderD3D;
host.WMResize += (width, height) =>
{
double dpi = DXInterop.GetDpiForSystem() / 96d;
hostWidthWithDPI = width/ dpi;
hostHeightWithDPI = height/ dpi;
};
}
else
{
Expand All @@ -93,7 +108,7 @@ public void EntryPoint()
{
Dispatcher.Invoke(() =>
{
GLCore.OnRender(DesignMode, RenderSize.Width, RenderSize.Height, hwndHost);
GLCore.OnRender(DesignMode, hostWidthWithDPI, hostHeightWithDPI, hwndHost);
});
GLCore.RenderD3D();
GLCore.WaitForVBlank();
Expand All @@ -105,14 +120,17 @@ public void EntryPoint()
}
Dispatcher.Invoke(() =>
{
GLCore.Dispose();
//GLCore.Dispose();
host.Dispose();
});
}

public void Start(GLWpfControlSettings settings)
{
GLCore.Start(settings);
Dispatcher.Invoke(() =>
{
GLCore.Start(settings);
});
}
}

Expand All @@ -126,6 +144,10 @@ public partial class DCompGLHost : HwndHost

public Action<IntPtr>? Resized;

public Action<uint,uint>? WMResize;

public Action? WMPaint;

public DCompGLHost(Func<(double, double)> GetSize)
{
getSizeFunc = GetSize;
Expand Down Expand Up @@ -169,13 +191,19 @@ enum WindowStyle : int
WS_VSCROLL = 0x00200000,
WS_BORDER = 0x00800000,
}

[Flags]
enum WindowStyleEx : int
{
WS_EX_NOREDIRECTIONBITMAP = 0x00200000,
}
protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
var (width, height) = getSizeFunc();
Resize(width, height);
hwndHost = CreateWindowEx(
0, "STATIC", "",
(int)(WindowStyle.WS_CHILD | WindowStyle.WS_VISIBLE | WindowStyle.WS_CLIPCHILDREN),
(int)(WindowStyleEx.WS_EX_NOREDIRECTIONBITMAP), "STATIC", "",
(int)(WindowStyle.WS_CHILD | WindowStyle.WS_VISIBLE),
0, 0,
hostWidth, hostHeight,
hwndParent.Handle,
Expand All @@ -185,6 +213,37 @@ protected override HandleRef BuildWindowCore(HandleRef hwndParent)
return new HandleRef(this, hwndHost);
}

protected override nint WndProc(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled)
{
switch (msg)
{
case 0x0005:
var width = LOWORD((uint)lParam);
var height = HIWORD((uint)lParam);
WMResize?.Invoke(width, height);
//SetWindowPos(hwnd, 0, 0, 0, (int)width, (int)height, 0x4000 | 0x0200 | 0x0008 | 0x0002 | 0x0004);
handled = true;
return 0;
case 0x000F:
WMPaint?.Invoke();
handled = true;
return 0;
default:
break;
}
return base.WndProc(hwnd, msg, wParam, lParam, ref handled);
}

private static ushort LOWORD(uint value)
{
return (ushort)(value & 0xFFFF);
}

private static ushort HIWORD(uint value)
{
return (ushort)(value >> 16);
}

protected override void DestroyWindowCore(HandleRef hwnd)
{
DestroyWindow(hwndHost);
Expand All @@ -204,5 +263,8 @@ private static extern IntPtr CreateWindowEx(int dwExStyle,

[DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)]
private static extern bool DestroyWindow(IntPtr hwnd);

[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int X, int Y, int cx, int cy, uint flag);
}
}
4 changes: 2 additions & 2 deletions Dependences/GLWpfControl/DWriteCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class DWriteCore

private static DComp CurrentDComp;

private static Dictionary<DComp, Queue<Action<Vortice.Direct2D1.ID2D1DeviceContext, float>>> DcompDWriteCommandQueue = new();
private static Dictionary<DComp, Queue<Action<Vortice.Direct2D1.ID2D1RenderTarget, float>>> DcompDWriteCommandQueue = new();

//private static Dictionary<Vortice.DirectWrite.IDWriteTextFormat> formats;

Expand All @@ -28,7 +28,7 @@ public static void SetCurrent(DComp DComp)
}
}

public static Queue<Action<Vortice.Direct2D1.ID2D1DeviceContext, float>> GetCommands(DComp DComp)
public static Queue<Action<Vortice.Direct2D1.ID2D1RenderTarget, float>> GetCommands(DComp DComp)
{
return DcompDWriteCommandQueue[DComp];
}
Expand Down
Loading

0 comments on commit 98f5822

Please sign in to comment.