using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using SlimDX; using D3D = SlimDX.Direct3D11; using DXGI = SlimDX.DXGI; using Exocortex.ComponentModel; namespace Exocortex.Rendering.Forms { public partial class RenderSystemControl : UserControl { const int cSampleCount = 4; public RenderSystemControl() { D3D.DriverType driverType = D3D.DriverType.Hardware; D3D.DeviceCreationFlags deviceCreationFlags = D3D.DeviceCreationFlags.None; D3D.FeatureLevel[] requiredFeatureLevels = new D3D.FeatureLevel[] { D3D.FeatureLevel.Level_11_0 }; // requiring DirectX 11 feature level for now. DXGI.SwapChainDescription swapChainDescription = new DXGI.SwapChainDescription() { BufferCount = 1, Usage = SlimDX.DXGI.Usage.RenderTargetOutput, OutputHandle = this.Handle, IsWindowed = true, ModeDescription = new DXGI.ModeDescription(0, 0, new Rational(30, 1), DXGI.Format.R8G8B8A8_UNorm), SampleDescription = new DXGI.SampleDescription(cSampleCount, 0), Flags = DXGI.SwapChainFlags.AllowModeSwitch, SwapEffect = DXGI.SwapEffect.Discard }; DXGI.Adapter defaultAdaptor = new DXGI.Factory().GetAdapter(0); //Debug2.WriteObject("defaultAdaptor.Description", defaultAdaptor.Description); _gpuDescription = defaultAdaptor.Description.Description; _gpuDedicatedVideoMemory = defaultAdaptor.Description.DedicatedVideoMemory; Result result = D3D.Device.CreateWithSwapChain( driverType, deviceCreationFlags, requiredFeatureLevels, swapChainDescription, out _device, out _swapChain); using (DXGI.Factory factory = _swapChain.GetParent()) { factory.SetWindowAssociation(this.Handle, DXGI.WindowAssociationFlags.IgnoreAltEnter); } this.Resize += new EventHandler(RenderControl_Resize); this.SetStyle( ControlStyles.AllPaintingInWmPaint, true ); this.SetStyle( ControlStyles.ResizeRedraw, true ); this.SetStyle(ControlStyles.Opaque, true); this.Paint += new PaintEventHandler(RenderControl_Paint); this.RenderControl_Resize(this, new EventArgs()); _device.ImmediateContext.OutputMerger.SetTargets(_renderTargetView); this.ResizeRedraw = true; } private string _gpuDescription = null; public string GPUDescription { get { return _gpuDescription; } } private long _gpuDedicatedVideoMemory = -1; public long GPUDedicatedVideoMemory { get { return _gpuDedicatedVideoMemory; } } private bool _resourcesLoaded = false; public event EventHandler ResourceLoad; private void OnResourceLoad() { if ( ! _resourcesLoaded) { if (this.ResourceUnload != null) { this.ResourceUnload(this, new EventArgs()); } } _resourcesLoaded = true; } public event EventHandler ResourceUnload; private void OnResourceUnload() { if (_resourcesLoaded) { if (this.ResourceLoad != null) { this.ResourceLoad(this, new EventArgs()); } } _resourcesLoaded = false; } private void DisposeSlimDX() { this.OnResourceUnload(); DisposeHelper.SafeDispose(ref _depthStencilView); DisposeHelper.SafeDispose(ref _renderTargetView); DisposeHelper.SafeDispose(ref _swapChain); DisposeHelper.SafeDispose(ref _device); } private D3D.Device _device = null; public D3D.Device Device { get { return _device; } } private D3D.Viewport _viewport; public D3D.Viewport Viewport { get { return _viewport; } } private DXGI.SwapChain _swapChain = null; public DXGI.SwapChain SwapChain { get { return _swapChain; } } private D3D.RenderTargetView _renderTargetView; public D3D.RenderTargetView RenderTargetView { get { return _renderTargetView; } } private D3D.DepthStencilView _depthStencilView; public D3D.DepthStencilView DepthStencilView { get { return _depthStencilView; } } public event EventHandler Render; private void RenderControl_Paint(object sender, PaintEventArgs e) { this.OnResourceLoad(); if (this.Render != null) { this.Render(this, new EventArgs()); } else { Debug2.WriteLine("RenderSystemControl - no event hooked up on this.Render, defaulting to BackColor."); this.Device.ImmediateContext.ClearRenderTargetView(this.RenderTargetView, new Color4(this.BackColor)); this.SwapChain.Present(0, SlimDX.DXGI.PresentFlags.None); } } private void RenderControl_Resize(object sender, EventArgs e) { if (_device != null) { this.OnResourceUnload(); DisposeHelper.SafeDispose(ref _depthStencilView); DisposeHelper.SafeDispose(ref _renderTargetView); _swapChain.ResizeBuffers(2, 0, 0, SlimDX.DXGI.Format.R8G8B8A8_UNorm, SlimDX.DXGI.SwapChainFlags.AllowModeSwitch); using (D3D.Texture2D resource = D3D.Resource.FromSwapChain(_swapChain, 0)) { _renderTargetView = new D3D.RenderTargetView(_device, resource); } _viewport = new D3D.Viewport(0.0f, 0.0f, this.ClientSize.Width, this.ClientSize.Height); _device.ImmediateContext.Rasterizer.SetViewports(_viewport); D3D.Texture2DDescription depthBufferDesc = new D3D.Texture2DDescription { ArraySize = 1, BindFlags = D3D.BindFlags.DepthStencil, CpuAccessFlags = D3D.CpuAccessFlags.None, Format = DXGI.Format.D32_Float, Height = Math.Max( this.Height, 1 ), Width = Math.Max( this.Width, 1 ), MipLevels = 1, OptionFlags = D3D.ResourceOptionFlags.None, SampleDescription = new DXGI.SampleDescription(cSampleCount, 0), Usage = D3D.ResourceUsage.Default }; using (D3D.Texture2D depthBuffer = new D3D.Texture2D(_device, depthBufferDesc)) { _depthStencilView = new D3D.DepthStencilView(_device, depthBuffer); } this.Device.ImmediateContext.OutputMerger.SetTargets(_depthStencilView, _renderTargetView); this.OnResourceLoad(); } } } }