// // Initialize // public void Initialize(int viewWidth, int viewHeight, IntPtr handle, Form form, DXConfigClass DXConfig) { _DXConfig = DXConfig; _handle = handle; _form = form; _viewWidth = viewWidth; _viewHeight = viewHeight; SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(viewWidth, viewHeight, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = handle, SampleDescription = new SampleDescription(DXConfig.MSAA_SampleCount, DXConfig.MSAA_SampleDesc), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; #if DEBUG Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, swapChainDesc, out _device, out _swapChain); #else Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapChainDesc, out _device, out _swapChain); #endif _context = _device.ImmediateContext; // Ignore all windows events Factory factory = _swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(handle, WindowAssociationFlags.IgnoreAll); HandleResize(viewWidth, viewHeight, DXConfig); }
// end of init... and whatever is required to handel window resize public void HandleResize(int viewWidth, int viewHeight, DXConfigClass DXConfig) { // happens when window is minimized if (viewWidth * viewHeight == 0) { return; } _viewWidth = viewWidth; _viewHeight = viewHeight; // dispose whatever needs to be disposed first if (_renderTargetView != null) { _renderTargetView.Dispose(); } if (_depthStencilBuffer != null) { _depthStencilBuffer.Dispose(); } if (_depthStencilView != null) { _depthStencilView.Dispose(); } // Resize the backbuffer _swapChain.ResizeBuffers(1, viewWidth, viewHeight, Format.R8G8B8A8_UNorm, SwapChainFlags.None); // Get the backbuffer from the swapchain var backBuffer = Texture2D.FromSwapChain <Texture2D>(_swapChain, 0); // RenderTargetView on the backbuffer _renderTargetView = new RenderTargetView(_device, backBuffer); _renderTargetView.DebugName = "MainRenderView"; backBuffer.Dispose(); // Create the depth buffer _depthStencilBuffer = new Texture2D(_device, new Texture2DDescription() { Format = Format.D32_Float, ArraySize = 1, MipLevels = 1, Width = viewWidth, Height = viewHeight, SampleDescription = new SampleDescription(DXConfig.MSAA_SampleCount, DXConfig.MSAA_SampleDesc), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }); _depthStencilBuffer.DebugName = "MainDepthBuffer"; _depthStencilView = new DepthStencilView(_device, _depthStencilBuffer); _context.OutputMerger.SetTargets(_depthStencilView, _renderTargetView); _context.Rasterizer.SetViewport(new Viewport(0, 0, viewWidth, viewHeight, 0.0f, 1.0f)); }
public void Initialize(int viewWidth, int viewHeight, IntPtr handle, Form form, DXConfigClass DXConfig) { DestroyAllResources(); _D3D = new D3DClass(); _D3D.Initialize(viewWidth, viewHeight, handle, form, DXConfig); Camera = new DXPerspectiveCamera(_config.FOV, (float)viewWidth, (float)viewHeight, _config.ScreenNear, _config.ScreenDepth); CameraControler = new DXOrbitControler(Camera, (form as ArbaroMainForm).renderCtrl); //CameraControler = new DXArcballControler(Camera, (form as ArbaroMainForm).renderCtrl); }
public void Resize(int viewWidth, int viewHeight, DXConfigClass DXConfig) { if (_D3D != null) { float aspect = (float)viewWidth / (float)viewHeight; Camera.AspectRatio = aspect; // notify everyone that size changed _D3D.HandleResize(viewWidth, viewHeight, DXConfig); } }
static void Main() { bool initialized = false; bool resize = false; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); DXConfig = new DXConfigClass(""); Renderer = new DXRendererClass(DXConfig); form = new ArbaroMainForm(); form.Width = DXConfig.FormDefaultWidth; form.Height = DXConfig.FormDefaultHeight; // Setup handler on resize form (form.renderCtrl as Control).Resize += (sender, args) => { resize = true; }; DXShaderManager = new DXShadersManager(); CS_PreciseTimer preciseTimer = new CS_PreciseTimer(10); DateTime tStart = preciseTimer.Now; RenderLoop.Run(form, () => { if (!initialized) { Rectangle r = form.renderCtrl.ClientRectangle; Renderer.Initialize(r.Width, r.Height, form.renderCtrl.Handle, form, DXConfig); initialized = true; form.RendererInitialized(); } if (resize) { resize = false; Rectangle r = form.renderCtrl.ClientRectangle; Renderer.Resize(r.Width, r.Height, DXConfig); } DateTime tEnd = preciseTimer.Now; float elapsed = (float)(tEnd.Subtract(tStart)).TotalMilliseconds; tStart = tEnd; Renderer.Frame(); }); Renderer.Dispose(); }
public DXRendererClass(DXConfigClass config) { _config = config; }
// // Initialize // public void Initialize(int viewWidth, int viewHeight, IntPtr handle, Form form, DXConfigClass DXConfig) { _DXConfig = DXConfig; _handle = handle; _form = form; _viewWidth = viewWidth; _viewHeight = viewHeight; SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(viewWidth, viewHeight, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = handle, SampleDescription = new SampleDescription(DXConfig.MSAA_SampleCount, DXConfig.MSAA_SampleDesc), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; #if DEBUG Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, swapChainDesc, out _device, out _swapChain); #else Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapChainDesc, out _device, out _swapChain); #endif _context = _device.ImmediateContext; // Ignore all windows events Factory factory = _swapChain.GetParent<Factory>(); factory.MakeWindowAssociation(handle, WindowAssociationFlags.IgnoreAll); HandleResize(viewWidth, viewHeight, DXConfig); }
// end of init... and whatever is required to handel window resize public void HandleResize(int viewWidth, int viewHeight, DXConfigClass DXConfig) { // happens when window is minimized if (viewWidth * viewHeight == 0) return; _viewWidth = viewWidth; _viewHeight = viewHeight; // dispose whatever needs to be disposed first if (_renderTargetView != null) _renderTargetView.Dispose(); if (_depthStencilBuffer != null) _depthStencilBuffer.Dispose(); if (_depthStencilView != null) _depthStencilView.Dispose(); // Resize the backbuffer _swapChain.ResizeBuffers(1, viewWidth, viewHeight, Format.R8G8B8A8_UNorm, SwapChainFlags.None); // Get the backbuffer from the swapchain var backBuffer = Texture2D.FromSwapChain<Texture2D>(_swapChain, 0); // RenderTargetView on the backbuffer _renderTargetView = new RenderTargetView(_device, backBuffer); _renderTargetView.DebugName = "MainRenderView"; backBuffer.Dispose(); // Create the depth buffer _depthStencilBuffer = new Texture2D(_device, new Texture2DDescription() { Format = Format.D32_Float, ArraySize = 1, MipLevels = 1, Width = viewWidth, Height = viewHeight, SampleDescription = new SampleDescription(DXConfig.MSAA_SampleCount, DXConfig.MSAA_SampleDesc), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }); _depthStencilBuffer.DebugName = "MainDepthBuffer"; _depthStencilView = new DepthStencilView(_device, _depthStencilBuffer); _context.OutputMerger.SetTargets(_depthStencilView, _renderTargetView); _context.Rasterizer.SetViewport(new Viewport(0, 0, viewWidth, viewHeight, 0.0f, 1.0f)); }