翼度科技»论坛 编程开发 .net 查看内容

[WPF] 使用Silk.NET绘制D3D9或OpenGL内容并完美解决空域问题。

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
可扩展渲染控件实现的基本思路(D3D、OpenGL绘制所使用的基类):


 
 
 
首先创建一个抽象类 FramebufferBase,该类主要记录当前控件宽高和图像资源。
  1. public abstract class FramebufferBase : IDisposable
  2. {
  3.     public abstract int FramebufferWidth { get; }
  4.     public abstract int FramebufferHeight { get; }
  5.     public abstract D3DImage D3dImage { get; }
  6.     public abstract void Dispose();
  7. }
复制代码
View Code接下来创建一个基本绘制控件,我这边取名为GameBase。
  1. public abstract class GameBase<TFrame> : Control where TFrame : FramebufferBase
复制代码
当我们在绘制3d内容的时候,总是会先在绘制前做一个准备,比如加载Shader,设置顶点、纹理等等。。。
所以我们应该加入 准备阶段事件 和 绘制事件。
当然如果当前帧绘制完成后,我们也可以做一些操作为下一次渲染做准备。
  1. public abstract event Action Ready;
  2. public abstract event Action<TimeSpan> Render;
  3. public abstract event Action<object, TimeSpan> UpdateFrame;
复制代码
View Code创建三个抽象方法 OnStart、OnDraw、OnSizeChanged
因为D3D和OpenGL创建帧和绘制的方式不太一致,所以需要提出来在继承类中做实现。
  1. protected abstract void OnStart();
  2. protected abstract void OnDraw(DrawingContext drawingContext);
  3. protected abstract void OnSizeChanged(SizeChangedInfo sizeInfo);
复制代码
View Code重载OnRenderSizeChanged、OnRender方法
因为新版本VS加入后了设计时预览,所以我判断了下(DesignerProperties.GetIsInDesignMode)。
  1. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  2. {
  3.     if (!DesignerProperties.GetIsInDesignMode(this))
  4.     {
  5.         OnSizeChanged(sizeInfo);
  6.     }
  7. }
  8. protected override void OnRender(DrawingContext drawingContext)
  9. {
  10.   if (DesignerProperties.GetIsInDesignMode(this))
  11.   {
  12.     DesignTimeHelper.DrawDesign(this, drawingContext);
  13.   }
  14.   else
  15.   {
  16.     if (Framebuffer != null && Framebuffer.D3dImage.IsFrontBufferAvailable)
  17.     {
  18.       OnDraw(drawingContext);
  19.       _stopwatch.Restart();
  20.     }
  21.   }
  22. }
复制代码
View Code创建一个Start方法
CompositionTarget.Rendering事件用于帧绘制并计算帧率。
  1. public void Start()
  2. {
  3.   if (!DesignerProperties.GetIsInDesignMode(this))
  4.   {
  5.     IsVisibleChanged += (_, e) =>
  6.     {
  7.       if ((bool)e.NewValue)
  8.       {
  9.         CompositionTarget.Rendering += CompositionTarget_Rendering;
  10.       }
  11.       else
  12.       {
  13.         CompositionTarget.Rendering -= CompositionTarget_Rendering;
  14.       }
  15.     };
  16.     Loaded += (_, _) => InvalidateVisual();
  17.     OnStart();
  18.   }
  19. }
  20. private void CompositionTarget_Rendering(object sender, EventArgs e)
  21. {
  22.   RenderingEventArgs args = (RenderingEventArgs)e;
  23.   if (_lastRenderTime != args.RenderingTime)
  24.   {
  25.     InvalidateVisual();
  26.     _fpsSample.Add(Convert.ToInt32(1000.0d / (args.RenderingTime.TotalMilliseconds - _lastRenderTime.TotalMilliseconds)));
  27.     // 样本数 30
  28.     if (_fpsSample.Count == 30)
  29.     {
  30.       Fps = Convert.ToInt32(_fpsSample.Average());
  31.       _fpsSample.Clear();
  32.     }
  33.     _lastRenderTime = args.RenderingTime;
  34.   }
  35. }
复制代码
View Code初期阶段,做这些准备就够了
剩下一些变量和依赖属性
  1. public static readonly DependencyProperty FpsProperty = DependencyProperty.Register(nameof(Fps), typeof(int), typeof(GameBase<TFrame>), new PropertyMetadata(0));
  2.    
  3. protected readonly Stopwatch _stopwatch = Stopwatch.StartNew();
  4. private readonly List<int> _fpsSample = new();
  5. protected TimeSpan _lastRenderTime = TimeSpan.FromSeconds(-1);
  6. protected TimeSpan _lastFrameStamp;
  7. protected TFrame Framebuffer { get; set; }
  8. public int Fps
  9. {
  10.     get { return (int)GetValue(FpsProperty); }
  11.     set { SetValue(FpsProperty, value); }
  12. }
复制代码
View CodeOK,基本思路就这样,接下来我将讲解具体实现。
D3D9绘制:

使用库:Silk.NET.Direct3D9
创建RenderContext类,此类主要功能是创建d3d设备及绘制格式。
创建一个d3d9的实例。
  1. IDirect3D9Ex* direct3D9;
复制代码
  1. D3D9.GetApi().Direct3DCreate9Ex(D3D9.SdkVersion, &direct3D9);
复制代码
获取屏幕基本信息。
  1. Displaymodeex pMode = new((uint)sizeof(Displaymodeex));
  2. direct3D9->GetAdapterDisplayModeEx(D3D9.AdapterDefault, ref pMode, null);
复制代码
创建d3d9设备。
重要参数:
BackBufferFormat 这个要与获取的屏幕信息里的格式一致。
  1. PresentParameters presentParameters = new()
  2. {
  3.   Windowed = 1,
  4.   SwapEffect = Swapeffect.Discard,
  5.   HDeviceWindow = 0,
  6.   PresentationInterval = 0,
  7.   BackBufferFormat = pMode.Format,
  8.   BackBufferWidth = 1,
  9.   BackBufferHeight = 1,
  10.   AutoDepthStencilFormat = Format.Unknown,
  11.   BackBufferCount = 1,
  12.   EnableAutoDepthStencil = 0,
  13.   Flags = 0,
  14.   FullScreenRefreshRateInHz = 0,
  15.   MultiSampleQuality = 0,
  16.   MultiSampleType = MultisampleType.MultisampleNone
  17. };
  18. direct3D9->CreateDeviceEx(D3D9.AdapterDefault, Devtype.Hal, 0, D3D9.CreateHardwareVertexprocessing | D3D9.CreateMultithreaded | D3D9.CreatePuredevice, ref presentParameters, (Displaymodeex*)IntPtr.Zero, &device);
复制代码
View Code完整代码:
  1. public unsafe class RenderContext
  2. {
  3.     public IDirect3DDevice9Ex* Device { get; }
  4.     public Format Format { get; }
  5.     public RenderContext()
  6.     {
  7.         IDirect3D9Ex* direct3D9;
  8.         IDirect3DDevice9Ex* device;
  9.         D3D9.GetApi().Direct3DCreate9Ex(D3D9.SdkVersion, &direct3D9);
  10.         Displaymodeex pMode = new((uint)sizeof(Displaymodeex));
  11.         direct3D9->GetAdapterDisplayModeEx(D3D9.AdapterDefault, ref pMode, null);
  12.         PresentParameters presentParameters = new()
  13.         {
  14.             Windowed = 1,
  15.             SwapEffect = Swapeffect.Discard,
  16.             HDeviceWindow = 0,
  17.             PresentationInterval = 0,
  18.             BackBufferFormat = pMode.Format,
  19.             BackBufferWidth = 1,
  20.             BackBufferHeight = 1,
  21.             AutoDepthStencilFormat = Format.Unknown,
  22.             BackBufferCount = 1,
  23.             EnableAutoDepthStencil = 0,
  24.             Flags = 0,
  25.             FullScreenRefreshRateInHz = 0,
  26.             MultiSampleQuality = 0,
  27.             MultiSampleType = MultisampleType.MultisampleNone
  28.         };
  29.         direct3D9->CreateDeviceEx(D3D9.AdapterDefault, Devtype.Hal, 0, D3D9.CreateHardwareVertexprocessing | D3D9.CreateMultithreaded | D3D9.CreatePuredevice, ref presentParameters, (Displaymodeex*)IntPtr.Zero, &device);
  30.         Device = device;
  31.         Format = pMode.Format;
  32.     }
  33. }
复制代码
View Code继承FramebufferBase创建Framebuffer类
这里就是根据传入的宽高创建一个新的Surface并绑定到D3DImage上
  1. public unsafe class Framebuffer : FramebufferBase
  2. {
  3.     public RenderContext Context { get; }
  4.     public override int FramebufferWidth { get; }
  5.     public override int FramebufferHeight { get; }
  6.     public override D3DImage D3dImage { get; }
  7.     public Framebuffer(RenderContext context, int framebufferWidth, int framebufferHeight)
  8.     {
  9.         Context = context;
  10.         FramebufferWidth = framebufferWidth;
  11.         FramebufferHeight = framebufferHeight;
  12.         IDirect3DSurface9* surface;
  13.         context.Device->CreateRenderTarget((uint)FramebufferWidth, (uint)FramebufferHeight, context.Format, MultisampleType.MultisampleNone, 0, 0, &surface, null);
  14.         context.Device->SetRenderTarget(0, surface);
  15.         D3dImage = new D3DImage();
  16.         D3dImage.Lock();
  17.         D3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, (IntPtr)surface);
  18.         D3dImage.Unlock();
  19.     }
  20.     public override void Dispose()
  21.     {
  22.         GC.SuppressFinalize(this);
  23.     }
  24. }
复制代码
View Code创建GameControl,并继承GameBase
  1. public unsafe class GameControl : GameBase<Framebuffer>
复制代码
  1. private RenderContext _context;
  2. public IDirect3DDevice9Ex* Device { get; private set; }
  3. public Format Format { get; private set; }
  4. public override event Action Ready;
  5. public override event Action<TimeSpan> Render;
  6. public override event Action<object, TimeSpan> UpdateFrame;
复制代码
重载OnStart方法
在使用时,OnStart只调用一次并创建RenderContext
  1. protected override void OnStart()
  2. {
  3.   if (_context == null)
  4.   {
  5.     _context = new RenderContext();
  6.     Device = _context.Device;
  7.     Format = _context.Format;
  8.     Ready?.Invoke();
  9.   }
  10. }
复制代码
View Code重载OnSizeChanged方法
每当控件大小方式改变时,将重新创建Framebuffer。
  1. protected override void OnSizeChanged(SizeChangedInfo sizeInfo)
  2. {
  3.   if (_context != null && sizeInfo.NewSize.Width > 0 && sizeInfo.NewSize.Height > 0)
  4.   {
  5.     Framebuffer?.Dispose();
  6.     Framebuffer = new Framebuffer(_context, (int)sizeInfo.NewSize.Width, (int)sizeInfo.NewSize.Height);
  7.   }
  8. }
复制代码
View Code重载OnDraw方法
首先锁定D3dImage,执行Render进行绘制。
绘制完成后,刷新D3dImage并解锁。
将D3dImage资源绘制到控件上。
执行UpdateFrame,告诉使用者,已经绘制完成。
  1. protected override void OnDraw(DrawingContext drawingContext)
  2. {
  3.   Framebuffer.D3dImage.Lock();
  4.   Render?.Invoke(_stopwatch.Elapsed - _lastFrameStamp);
  5.   Framebuffer.D3dImage.AddDirtyRect(new Int32Rect(0, 0, Framebuffer.FramebufferWidth, Framebuffer.FramebufferHeight));
  6.   Framebuffer.D3dImage.Unlock();
  7.   Rect rect = new(0, 0, Framebuffer.D3dImage.Width, Framebuffer.D3dImage.Height);
  8.   drawingContext.DrawImage(Framebuffer.D3dImage, rect);
  9.   UpdateFrame?.Invoke(this, _stopwatch.Elapsed - _lastFrameStamp);
  10. }
复制代码
View Code使用方式:
  1. <UserControl x:Class="SilkWPF.Direct3D9.Sample.MiniTri"
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  4.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.              xmlns:direct3D9="clr-namespace:SilkWPF.Direct3D9"
  7.              mc:Ignorable="d"
  8.              d:DesignHeight="450"
  9.              d:DesignWidth="800">
  10.     <Grid>
  11.         <direct3D9:GameControl x:Name="Game" />
  12.         <TextBlock HorizontalAlignment="Left"
  13.                    VerticalAlignment="Top"
  14.                    Margin="10,5,0,0"
  15.                    FontSize="30"
  16.                    Foreground="Green"
  17.                    Text="{Binding ElementName=Game, Path=Fps}" />
  18.     </Grid>
  19. </UserControl>
复制代码
Xaml
  1. using Silk.NET.Direct3D9;
  2. using Silk.NET.Maths;
  3. using SilkWPF.Common;
  4. using System.Diagnostics;
  5. using System.Numerics;
  6. using System.Runtime.InteropServices;
  7. using System.Windows.Controls;
  8. namespace SilkWPF.Direct3D9.Sample;
  9. /// <summary>
  10. /// MiniTri.xaml 的交互逻辑
  11. /// </summary>
  12. public unsafe partial class MiniTri : UserControl
  13. {
  14.     [StructLayout(LayoutKind.Sequential)]
  15.     struct Vertex
  16.     {
  17.         public Vector4 Position;
  18.         public uint Color;
  19.     }
  20.     private readonly Stopwatch _stopwatch = Stopwatch.StartNew();
  21.     private readonly Vertex[] _vertices =
  22.     {
  23.         new Vertex() { Color = (uint)SilkColor.Red.ToBgra(), Position = new Vector4(400.0f, 100.0f, 0.5f, 1.0f) },
  24.         new Vertex() { Color = (uint)SilkColor.Blue.ToBgra(), Position = new Vector4(650.0f, 500.0f, 0.5f, 1.0f) },
  25.         new Vertex() { Color = (uint)SilkColor.Green.ToBgra(), Position = new Vector4(150.0f, 500.0f, 0.5f, 1.0f) }
  26.     };
  27.     private readonly Vertexelement9[] _vertexelements =
  28.     {
  29.         new Vertexelement9(0, 0, 3, 0, 9, 0),
  30.         new Vertexelement9(0, 16, 4, 0, 10, 0),
  31.         new Vertexelement9(255, 0, 17, 0, 0, 0)
  32.     };
  33.     private IDirect3DVertexBuffer9* _ppVertexBuffer;
  34.     private IDirect3DVertexDeclaration9* _ppDecl;
  35.     public MiniTri()
  36.     {
  37.         InitializeComponent();
  38.         Game.Ready += Game_Ready;
  39.         Game.Render += Game_Render;
  40.         Game.Start();
  41.     }
  42.     private void Game_Ready()
  43.     {
  44.         fixed (Vertex* ptr = &_vertices[0])
  45.         {
  46.             fixed (Vertexelement9* vertexElems = &_vertexelements[0])
  47.             {
  48.                 void* ppbData;
  49.                 Game.Device->CreateVertexBuffer(3 * 20, D3D9.UsageWriteonly, 0, Pool.Default, ref _ppVertexBuffer, null);
  50.                 _ppVertexBuffer->Lock(0, 0, &ppbData, 0);
  51.                 System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(ppbData, ptr, (uint)(sizeof(Vertex) * _vertices.Length));
  52.                 _ppVertexBuffer->Unlock();
  53.                 Game.Device->CreateVertexDeclaration(vertexElems, ref _ppDecl);
  54.             }
  55.         }
  56.     }
  57.     private void Game_Render(TimeSpan obj)
  58.     {
  59.         float hue = (float)_stopwatch.Elapsed.TotalSeconds * 0.15f % 1;
  60.         Vector4 vector = new(1.0f * hue, 1.0f * 0.75f, 1.0f * 0.75f, 1.0f);
  61.         Game.Device->Clear(0, null, D3D9.ClearTarget, (uint)SilkColor.FromHsv(vector).ToBgra(), 1.0f, 0);
  62.         Game.Device->BeginScene();
  63.         Game.Device->SetStreamSource(0, _ppVertexBuffer, 0, 20);
  64.         Game.Device->SetVertexDeclaration(_ppDecl);
  65.         Game.Device->DrawPrimitive(Primitivetype.Trianglelist, 0, 1);
  66.         Game.Device->EndScene();
  67.         Game.Device->Present((Rectangle<int>*)IntPtr.Zero, (Rectangle<int>*)IntPtr.Zero, 1, (RGNData*)IntPtr.Zero);
  68.     }
  69. }
复制代码
C#运行代码,你将得到一个渐变颜色的三角形(amd处理器上对d3d9的支持特别差,使用MediaElement播放视频也卡的不行)。
显示帧数比较低,不用太在意(amd出来背锅)。

 接下来时绘制OpenGL内容:
分割一下 ——————————————————————————————————————————————————————————————————
OpenGL绘制:

实现思路:

使用库:Silk.NET.Direct3D9、OpenTK
可能大家比较奇怪,为什么不用Silk.NET.OpenGL,
目前Silk中的Wgl函数并不完整,我这里需要一些wgl的扩展函数用于关联D3D9设备。
所以我就先使用OpenTK做绘制。
创建一个OpenGL的配置信息类 Settings
  1. public class Settings
  2. {
  3.     public int MajorVersion { get; set; } = 3;
  4.     public int MinorVersion { get; set; } = 3;
  5.     public ContextFlags GraphicsContextFlags { get; set; } = ContextFlags.Default;
  6.     public ContextProfile GraphicsProfile { get; set; } = ContextProfile.Core;
  7.     public IGraphicsContext ContextToUse { get; set; }
  8.     public static bool WouldResultInSameContext([NotNull] Settings a, [NotNull] Settings b)
  9.     {
  10.         if (a.MajorVersion != b.MajorVersion)
  11.         {
  12.             return false;
  13.         }
  14.         if (a.MinorVersion != b.MinorVersion)
  15.         {
  16.             return false;
  17.         }
  18.         if (a.GraphicsProfile != b.GraphicsProfile)
  19.         {
  20.             return false;
  21.         }
  22.         if (a.GraphicsContextFlags != b.GraphicsContextFlags)
  23.         {
  24.             return false;
  25.         }
  26.         return true;
  27.     }
  28. }
复制代码
View Code 创建RenderContext类
具体实现与d3d差不太多,主要是创建设备。
不过要注意GetOrCreateSharedOpenGLContext方法,他是静态的,
我们在初始化wgl时需要一个窗体,所以我在这里让所有绘制控件都使用一个窗体。
  1. public unsafe class RenderContext{    private static IGraphicsContext _sharedContext;    private static Settings _sharedContextSettings;    private static int _sharedContextReferenceCount;    public Format Format { get; }    public IntPtr DxDeviceHandle { get; }    public IntPtr GlDeviceHandle { get; }    public IGraphicsContext GraphicsContext { get; }    public RenderContext(Settings settings)    {        IDirect3D9Ex* direct3D9;        IDirect3DDevice9Ex* device;        D3D9.GetApi().Direct3DCreate9Ex(D3D9.SdkVersion, &direct3D9);        Displaymodeex pMode = new((uint)sizeof(Displaymodeex));        direct3D9->GetAdapterDisplayModeEx(D3D9.AdapterDefault, ref pMode, null);        Format = pMode.Format;        PresentParameters presentParameters = new()        {            Windowed = 1,            SwapEffect = Swapeffect.Discard,            HDeviceWindow = 0,            PresentationInterval = 0,            BackBufferFormat = Format,            BackBufferWidth = 1,            BackBufferHeight = 1,            AutoDepthStencilFormat = Format.Unknown,            BackBufferCount = 1,            EnableAutoDepthStencil = 0,            Flags = 0,            FullScreenRefreshRateInHz = 0,            MultiSampleQuality = 0,            MultiSampleType = MultisampleType.MultisampleNone        };        direct3D9->CreateDeviceEx(D3D9.AdapterDefault, Devtype.Hal, 0, D3D9.CreateHardwareVertexprocessing | D3D9.CreateMultithreaded | D3D9.CreatePuredevice, ref presentParameters, (Displaymodeex*)IntPtr.Zero, &device);        DxDeviceHandle = (IntPtr)device;        GraphicsContext = GetOrCreateSharedOpenGLContext(settings);        GlDeviceHandle = Wgl.DXOpenDeviceNV((IntPtr)device);    }    private static IGraphicsContext GetOrCreateSharedOpenGLContext(Settings settings)    {        if (_sharedContext == null)        {            NativeWindowSettings windowSettings = NativeWindowSettings.Default;            windowSettings.StartFocused = false;            windowSettings.StartVisible = false;            windowSettings.NumberOfSamples = 0;            windowSettings.APIVersion = new Version(settings.MajorVersion, settings.MinorVersion);            windowSettings.Flags = ContextFlags.Offscreen | settings.GraphicsContextFlags;            windowSettings.Profile = settings.GraphicsProfile;            windowSettings.WindowBorder = WindowBorder.Hidden;            windowSettings.WindowState = WindowState.Minimized;            NativeWindow nativeWindow = new(windowSettings);            Wgl.LoadBindings(new GLFWBindingsContext());            _sharedContext = nativeWindow.Context;            _sharedContextSettings = settings;            _sharedContext.MakeCurrent();        }        else        {            if (!Settings.WouldResultInSameContext(settings, _sharedContextSettings))            {                throw new ArgumentException($"The provided {nameof(Settings)} would result" +<UserControl x:Class="SilkWPF.Direct3D9.Sample.MiniTri"
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  4.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.              xmlns:direct3D9="clr-namespace:SilkWPF.Direct3D9"
  7.              mc:Ignorable="d"
  8.              d:DesignHeight="450"
  9.              d:DesignWidth="800">
  10.     <Grid>
  11.         <direct3D9:GameControl x:Name="Game" />
  12.         <TextBlock HorizontalAlignment="Left"
  13.                    VerticalAlignment="Top"
  14.                    Margin="10,5,0,0"
  15.                    FontSize="30"
  16.                    Foreground="Green"
  17.                    Text="{Binding ElementName=Game, Path=Fps}" />
  18.     </Grid>
  19. </UserControl><UserControl x:Class="SilkWPF.Direct3D9.Sample.MiniTri"
  20.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  21.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  22.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  23.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  24.              xmlns:direct3D9="clr-namespace:SilkWPF.Direct3D9"
  25.              mc:Ignorable="d"
  26.              d:DesignHeight="450"
  27.              d:DesignWidth="800">
  28.     <Grid>
  29.         <direct3D9:GameControl x:Name="Game" />
  30.         <TextBlock HorizontalAlignment="Left"
  31.                    VerticalAlignment="Top"
  32.                    Margin="10,5,0,0"
  33.                    FontSize="30"
  34.                    Foreground="Green"
  35.                    Text="{Binding ElementName=Game, Path=Fps}" />
  36.     </Grid>
  37. </UserControl>$"in a different context creation to one previously created. To fix this," +<UserControl x:Class="SilkWPF.Direct3D9.Sample.MiniTri"
  38.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  39.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  40.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  41.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  42.              xmlns:direct3D9="clr-namespace:SilkWPF.Direct3D9"
  43.              mc:Ignorable="d"
  44.              d:DesignHeight="450"
  45.              d:DesignWidth="800">
  46.     <Grid>
  47.         <direct3D9:GameControl x:Name="Game" />
  48.         <TextBlock HorizontalAlignment="Left"
  49.                    VerticalAlignment="Top"
  50.                    Margin="10,5,0,0"
  51.                    FontSize="30"
  52.                    Foreground="Green"
  53.                    Text="{Binding ElementName=Game, Path=Fps}" />
  54.     </Grid>
  55. </UserControl><UserControl x:Class="SilkWPF.Direct3D9.Sample.MiniTri"
  56.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  57.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  58.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  59.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  60.              xmlns:direct3D9="clr-namespace:SilkWPF.Direct3D9"
  61.              mc:Ignorable="d"
  62.              d:DesignHeight="450"
  63.              d:DesignWidth="800">
  64.     <Grid>
  65.         <direct3D9:GameControl x:Name="Game" />
  66.         <TextBlock HorizontalAlignment="Left"
  67.                    VerticalAlignment="Top"
  68.                    Margin="10,5,0,0"
  69.                    FontSize="30"
  70.                    Foreground="Green"
  71.                    Text="{Binding ElementName=Game, Path=Fps}" />
  72.     </Grid>
  73. </UserControl>$" either ensure all of your context settings are identical, or provide an " +<UserControl x:Class="SilkWPF.Direct3D9.Sample.MiniTri"
  74.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  75.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  76.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  77.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  78.              xmlns:direct3D9="clr-namespace:SilkWPF.Direct3D9"
  79.              mc:Ignorable="d"
  80.              d:DesignHeight="450"
  81.              d:DesignWidth="800">
  82.     <Grid>
  83.         <direct3D9:GameControl x:Name="Game" />
  84.         <TextBlock HorizontalAlignment="Left"
  85.                    VerticalAlignment="Top"
  86.                    Margin="10,5,0,0"
  87.                    FontSize="30"
  88.                    Foreground="Green"
  89.                    Text="{Binding ElementName=Game, Path=Fps}" />
  90.     </Grid>
  91. </UserControl><UserControl x:Class="SilkWPF.Direct3D9.Sample.MiniTri"
  92.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  93.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  94.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  95.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  96.              xmlns:direct3D9="clr-namespace:SilkWPF.Direct3D9"
  97.              mc:Ignorable="d"
  98.              d:DesignHeight="450"
  99.              d:DesignWidth="800">
  100.     <Grid>
  101.         <direct3D9:GameControl x:Name="Game" />
  102.         <TextBlock HorizontalAlignment="Left"
  103.                    VerticalAlignment="Top"
  104.                    Margin="10,5,0,0"
  105.                    FontSize="30"
  106.                    Foreground="Green"
  107.                    Text="{Binding ElementName=Game, Path=Fps}" />
  108.     </Grid>
  109. </UserControl>$"external context via the '{nameof(Settings.ContextToUse)}' field.");            }        }        Interlocked.Increment(ref _sharedContextReferenceCount);        return _sharedContext;    }}
复制代码
View Code创建Framebuffer类
这里主要用d3d创建一个Surface,
gl根据Surface生成一个Frame。 
  1. public unsafe class Framebuffer : FramebufferBase
  2. {
  3.     public RenderContext Context { get; }
  4.     public override int FramebufferWidth { get; }
  5.     public override int FramebufferHeight { get; }
  6.     public int GLFramebufferHandle { get; }
  7.     public int GLSharedTextureHandle { get; }
  8.     public int GLDepthRenderBufferHandle { get; }
  9.     public IntPtr DxInteropRegisteredHandle { get; }
  10.     public override D3DImage D3dImage { get; }
  11.     public TranslateTransform TranslateTransform { get; }
  12.     public ScaleTransform FlipYTransform { get; }
  13.     public Framebuffer(RenderContext context, int framebufferWidth, int framebufferHeight)
  14.     {
  15.         Context = context;
  16.         FramebufferWidth = framebufferWidth;
  17.         FramebufferHeight = framebufferHeight;
  18.         IDirect3DDevice9Ex* device = (IDirect3DDevice9Ex*)context.DxDeviceHandle;
  19.         IDirect3DSurface9* surface;
  20.         void* surfacePtr = (void*)IntPtr.Zero;
  21.         device->CreateRenderTarget((uint)FramebufferWidth, (uint)FramebufferHeight, context.Format, MultisampleType.MultisampleNone, 0, 0, &surface, &surfacePtr);
  22.         Wgl.DXSetResourceShareHandleNV((IntPtr)surface, (IntPtr)surfacePtr);
  23.         GLFramebufferHandle = GL.GenFramebuffer();
  24.         GLSharedTextureHandle = GL.GenTexture();
  25.         DxInteropRegisteredHandle = Wgl.DXRegisterObjectNV(context.GlDeviceHandle, (IntPtr)surface, (uint)GLSharedTextureHandle, (uint)TextureTarget.Texture2D, WGL_NV_DX_interop.AccessReadWrite);
  26.         GL.BindFramebuffer(FramebufferTarget.Framebuffer, GLFramebufferHandle);
  27.         GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, GLSharedTextureHandle, 0);
  28.         GLDepthRenderBufferHandle = GL.GenRenderbuffer();
  29.         GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, GLDepthRenderBufferHandle);
  30.         GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.Depth24Stencil8, FramebufferWidth, FramebufferHeight);
  31.         GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, GLDepthRenderBufferHandle);
  32.         GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.StencilAttachment, RenderbufferTarget.Renderbuffer, GLDepthRenderBufferHandle);
  33.         GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
  34.         D3dImage = new D3DImage();
  35.         D3dImage.Lock();
  36.         D3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, (IntPtr)surface);
  37.         D3dImage.Unlock();
  38.         TranslateTransform = new TranslateTransform(0, FramebufferHeight);
  39.         FlipYTransform = new ScaleTransform(1, -1);
  40.     }
  41.     public override void Dispose()
  42.     {
  43.         GL.DeleteFramebuffer(GLFramebufferHandle);
  44.         GL.DeleteRenderbuffer(GLDepthRenderBufferHandle);
  45.         GL.DeleteTexture(GLSharedTextureHandle);
  46.         Wgl.DXUnregisterObjectNV(Context.GlDeviceHandle, DxInteropRegisteredHandle);
  47.         GC.SuppressFinalize(this);
  48.     }
  49. }
复制代码
View Code创建GameControl并继承GameBase
重载OnDraw方法
跟d3d一样,绘制前需要锁定Frame。
绘制完成后进行刷新。
这里有一点不同的是,OpenGL的顶点在左下角(0,0)
所以要对图像进行翻转。
  1. protected override void OnDraw(DrawingContext drawingContext)
  2. {
  3.   Framebuffer.D3dImage.Lock();
  4.   Wgl.DXLockObjectsNV(_context.GlDeviceHandle, 1, new[] { Framebuffer.DxInteropRegisteredHandle });
  5.   GL.BindFramebuffer(FramebufferTarget.Framebuffer, Framebuffer.GLFramebufferHandle);
  6.   GL.Viewport(0, 0, Framebuffer.FramebufferWidth, Framebuffer.FramebufferHeight);
  7.   Render?.Invoke(_stopwatch.Elapsed - _lastFrameStamp);
  8.   GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
  9.   Wgl.DXUnlockObjectsNV(_context.GlDeviceHandle, 1, new[] { Framebuffer.DxInteropRegisteredHandle });
  10.   Framebuffer.D3dImage.AddDirtyRect(new Int32Rect(0, 0, Framebuffer.FramebufferWidth, Framebuffer.FramebufferHeight));
  11.   Framebuffer.D3dImage.Unlock();
  12.   drawingContext.PushTransform(Framebuffer.TranslateTransform);
  13.   drawingContext.PushTransform(Framebuffer.FlipYTransform);
  14.   Rect rect = new(0, 0, Framebuffer.D3dImage.Width, Framebuffer.D3dImage.Height);
  15.   drawingContext.DrawImage(Framebuffer.D3dImage, rect);
  16.   drawingContext.Pop();
  17.   drawingContext.Pop();
  18.   UpdateFrame?.Invoke(this, _stopwatch.Elapsed - _lastFrameStamp);
  19. }
复制代码
View Code其余函数与d3d基本一致,我就不说了(没想到写文章这么累)
完整代码:
  1. public class GameControl : GameBase<Framebuffer>
  2. {
  3.     private RenderContext _context;
  4.     public Settings Setting { get; set; } = new Settings();
  5.     public override event Action Ready;
  6.     public override event Action<TimeSpan> Render;
  7.     public override event Action<object, TimeSpan> UpdateFrame;
  8.     protected override void OnStart()
  9.     {
  10.         if (_context == null)
  11.         {
  12.             _context = new RenderContext(Setting);
  13.             Ready?.Invoke();
  14.         }
  15.     }
  16.     protected override void OnSizeChanged(SizeChangedInfo sizeInfo)
  17.     {
  18.         if (_context != null && sizeInfo.NewSize.Width > 0 && sizeInfo.NewSize.Height > 0)
  19.         {
  20.             Framebuffer?.Dispose();
  21.             Framebuffer = new Framebuffer(_context, (int)sizeInfo.NewSize.Width, (int)sizeInfo.NewSize.Height);
  22.         }
  23.     }
  24.     protected override void OnDraw(DrawingContext drawingContext)
  25.     {
  26.         Framebuffer.D3dImage.Lock();
  27.         Wgl.DXLockObjectsNV(_context.GlDeviceHandle, 1, new[] { Framebuffer.DxInteropRegisteredHandle });
  28.         GL.BindFramebuffer(FramebufferTarget.Framebuffer, Framebuffer.GLFramebufferHandle);
  29.         GL.Viewport(0, 0, Framebuffer.FramebufferWidth, Framebuffer.FramebufferHeight);
  30.         Render?.Invoke(_stopwatch.Elapsed - _lastFrameStamp);
  31.         GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
  32.         Wgl.DXUnlockObjectsNV(_context.GlDeviceHandle, 1, new[] { Framebuffer.DxInteropRegisteredHandle });
  33.         Framebuffer.D3dImage.AddDirtyRect(new Int32Rect(0, 0, Framebuffer.FramebufferWidth, Framebuffer.FramebufferHeight));
  34.         Framebuffer.D3dImage.Unlock();
  35.         drawingContext.PushTransform(Framebuffer.TranslateTransform);
  36.         drawingContext.PushTransform(Framebuffer.FlipYTransform);
  37.         Rect rect = new(0, 0, Framebuffer.D3dImage.Width, Framebuffer.D3dImage.Height);
  38.         drawingContext.DrawImage(Framebuffer.D3dImage, rect);
  39.         drawingContext.Pop();
  40.         drawingContext.Pop();
  41.         UpdateFrame?.Invoke(this, _stopwatch.Elapsed - _lastFrameStamp);
  42.     }
  43. }
复制代码
View Code使用示例:

 

本文使用的代码地址:
qian-o/SilkWPF (github.com)

来源:https://www.cnblogs.com/xymfblogs/archive/2023/02/13/17116581.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具