Created
January 31, 2018 13:45
-
-
Save zcyemi/3da6d39da4c107af30a6ba65a39e52a7 to your computer and use it in GitHub Desktop.
Cross AppDomain DirectX11(SharpDX) Drawing Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using SharpDX; | |
using SharpDX.Direct3D11; | |
using SharpDX.DXGI; | |
using System.Threading; | |
using Device = SharpDX.Direct3D11.Device; | |
using Buffer = SharpDX.Direct3D11.Buffer; | |
namespace AppDomainDX | |
{ | |
public class DomainDX : MarshalByRefObject | |
{ | |
public Device m_Dx; | |
public SwapChain m_swapchain; | |
public RenderTargetView m_rtv; | |
public Random m_random = new Random(); | |
public void Init(IntPtr devicePtr,IntPtr swapchainPtr,IntPtr rtvPtr) | |
{ | |
m_Dx = new Device(devicePtr); | |
m_swapchain = new SwapChain(swapchainPtr); | |
m_rtv = new RenderTargetView(rtvPtr); | |
} | |
public void Draw() | |
{ | |
Console.WriteLine("call dx draw:"+ Thread.GetDomain().FriendlyName); | |
m_Dx.ImmediateContext.ClearRenderTargetView(m_rtv, new SharpDX.Mathematics.Interop.RawColor4(m_random.NextFloat(0f, 1f), 0, 0, 1.0f)); | |
m_swapchain.Present(0, PresentFlags.None); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using System.Threading; | |
using System.Reflection; | |
using SharpDX; | |
using SharpDX.Direct3D11; | |
using SharpDX.DXGI; | |
using Device = SharpDX.Direct3D11.Device; | |
using Buffer = SharpDX.Direct3D11.Buffer; | |
namespace AppDomainDX | |
{ | |
public class WindowForm : Form | |
{ | |
protected override void WndProc(ref System.Windows.Forms.Message m) | |
{ | |
if(Program.s_ddx != null) | |
{ | |
Program.s_ddx.Draw(); | |
} | |
base.WndProc(ref m); | |
} | |
} | |
static class Program | |
{ | |
private static WindowForm s_form; | |
public static AppDomain s_domainDX; | |
public static DomainDX s_ddx; | |
/// <summary> | |
/// 应用程序的主入口点。 | |
/// </summary> | |
[STAThread] | |
static void Main() | |
{ | |
s_form = new WindowForm(); | |
InitDX(); | |
CreateAppDomain(); | |
s_ddx.Init(s_device.NativePointer, s_dxswapchain.NativePointer, s_rtv.NativePointer); | |
Application.Run(s_form); | |
ReleaseDX(); | |
} | |
public static Device s_device = null; | |
public static SwapChain s_dxswapchain = null; | |
public static DeviceContext s_dxcontext = null; | |
public static RenderTargetView s_rtv = null; | |
public static Texture2D s_buffer = null; | |
public static bool m_inited = false; | |
static void Free<T>(T obj) where T : IDisposable | |
{ | |
if(obj != null) | |
{ | |
obj.Dispose(); | |
} | |
} | |
static void InitDX() | |
{ | |
var scdesc = new SwapChainDescription() | |
{ | |
BufferCount = 1, | |
ModeDescription = new ModeDescription(s_form.ClientSize.Width, s_form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), | |
IsWindowed = true, | |
OutputHandle = s_form.Handle, | |
SampleDescription = new SampleDescription(1, 0), | |
SwapEffect = SwapEffect.Discard, | |
Usage = Usage.RenderTargetOutput | |
}; | |
SharpDX.Direct3D11.Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.Debug, scdesc, out s_device, out s_dxswapchain); | |
s_dxcontext = s_device.ImmediateContext; | |
s_buffer = Texture2D.FromSwapChain<Texture2D>(s_dxswapchain, 0); | |
s_rtv = new RenderTargetView(s_device, s_buffer); | |
s_dxcontext.OutputMerger.SetRenderTargets(s_rtv); | |
s_dxcontext.ClearRenderTargetView(s_rtv, new SharpDX.Mathematics.Interop.RawColor4(1f, 0, 0, 1f)); | |
s_dxswapchain.Present(0, PresentFlags.None); | |
m_inited = true; | |
} | |
static void ReleaseDX() | |
{ | |
Free(s_rtv); | |
Free(s_dxswapchain); | |
Free(s_device); | |
} | |
static void CreateAppDomain() | |
{ | |
string exeAssembly = Assembly.GetEntryAssembly().FullName; | |
AppDomainSetup ads = new AppDomainSetup(); | |
ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; | |
ads.DisallowBindingRedirects = false; | |
ads.DisallowCodeDownload = true; | |
ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; | |
AppDomain s_domainDX = AppDomain.CreateDomain("AD", null, ads); | |
s_ddx = (DomainDX) s_domainDX.CreateInstanceAndUnwrap(exeAssembly, typeof(DomainDX).FullName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment