protected override void CreateSizeDependentResources(D3DApplicationBase app) { base.CreateSizeDependentResources(app); }
protected override void CreateSizeDependentResources(D3DApplicationBase app) { base.CreateSizeDependentResources(app); // Initialize camera Camera.ZNear = 0.5f; Camera.ZFar = 100f; Camera.FOV = (float)Math.PI / 3f; Camera.AspectRatio = Width / (float)Height; // Reinitialize the projection matrix ProjectionMatrix = Matrix.PerspectiveFovRH(Camera.FOV, Camera.AspectRatio, Camera.ZNear, Camera.ZFar); }
void Target_OnSizeChanged(D3DApplicationBase target) { CreateSizeDependentResources(); }
/// <summary> /// Initialize with the provided deviceManager /// </summary> /// <param name="deviceManager"></param> public virtual void Initialize(D3DApplicationBase app) { // If there is a previous device manager, remove event handler if (this.DeviceManager != null) this.DeviceManager.OnInitialize -= DeviceManager_OnInitialize; this.DeviceManager = app.DeviceManager; // Handle OnInitialize event so that device dependent // resources can be reinitialized. this.DeviceManager.OnInitialize += DeviceManager_OnInitialize; // If there is a previous target, remove event handler if (this.Target != null) this.Target.OnSizeChanged -= Target_OnSizeChanged; this.Target = app; // Handle OnSizeChanged event so that size dependent // resources can be reinitialized. this.Target.OnSizeChanged += Target_OnSizeChanged; // If the device is already initialized, then create // any device resources immediately. if (this.DeviceManager.Direct3DDevice != null) { CreateDeviceDependentResources(); } }
protected override void CreateSizeDependentResources(D3DApplicationBase app) { RemoveAndDispose(ref renderTargetSRV); RemoveAndDispose(ref resolvedTargetSRV); RemoveAndDispose(ref altRenderTargetRTV); RemoveAndDispose(ref altRenderTargetSRV); RemoveAndDispose(ref altRenderTargetUAV); RemoveAndDispose(ref altRenderTargetUIntUAV); RemoveAndDispose(ref renderTarget); RemoveAndDispose(ref resolvedTarget); base.CreateSizeDependentResources(app); #region Image Processing var device = this.DeviceManager.Direct3DDevice; renderTarget = ToDispose(RenderTargetView.ResourceAs<Texture2D>()); { renderTarget.DebugName = "Render Target"; renderTargetSRV = ToDispose(new ShaderResourceView(device, renderTarget)); // Initialize a target to resolve multi-sampled render target var resolvedDesc = renderTarget.Description; resolvedDesc.BindFlags = BindFlags.ShaderResource; resolvedDesc.SampleDescription = new SampleDescription(1, 0); resolvedTarget = ToDispose(new Texture2D(device, resolvedDesc)); { resolvedTargetSRV = ToDispose(new ShaderResourceView(device, resolvedTarget)); } // Create two alternative render targets // These are specially configured so they can be bound as SRV, RTV, and UAV // and also as a R32_UInt UAV (read/write to texture resource support) var rtDesc = renderTarget.Description; rtDesc.BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget | BindFlags.UnorderedAccess; rtDesc.SampleDescription = new SampleDescription(1, 0); rtDesc.Format = Format.R8G8B8A8_Typeless; // so it can be bound to the R32_UInt UAV rtDesc.MipLevels = 0; using (var altTarget = new Texture2D(device, rtDesc)) { altRenderTargetRTV = ToDispose(new RenderTargetView(device, altTarget , new RenderTargetViewDescription { Format = Format.R8G8B8A8_UNorm, Dimension = RenderTargetViewDimension.Texture2D } )); altRenderTargetSRV = ToDispose(new ShaderResourceView(device, altTarget , new ShaderResourceViewDescription { Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Format = Format.R8G8B8A8_UNorm, Texture2D = new ShaderResourceViewDescription.Texture2DResource { MipLevels = 1, MostDetailedMip = 0 } } )); altRenderTargetUAV = ToDispose(new UnorderedAccessView(device, altTarget , new UnorderedAccessViewDescription { Dimension = UnorderedAccessViewDimension.Texture2D, Format = Format.R8G8B8A8_UNorm, } )); altRenderTargetUIntUAV = ToDispose(new UnorderedAccessView(device, altTarget , new UnorderedAccessViewDescription { Dimension = UnorderedAccessViewDimension.Texture2D, Format = Format.R32_UInt, } )); } using (var alt2Target = new Texture2D(device, rtDesc)) { alt2RenderTargetRTV = ToDispose(new RenderTargetView(device, alt2Target , new RenderTargetViewDescription { Format = Format.R8G8B8A8_UNorm, Dimension = RenderTargetViewDimension.Texture2D } )); alt2RenderTargetSRV = ToDispose(new ShaderResourceView(device, alt2Target , new ShaderResourceViewDescription { Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Format = Format.R8G8B8A8_UNorm, Texture2D = new ShaderResourceViewDescription.Texture2DResource { MipLevels = 1, MostDetailedMip = 0 } } )); alt2RenderTargetUAV = ToDispose(new UnorderedAccessView(device, alt2Target , new UnorderedAccessViewDescription { Dimension = UnorderedAccessViewDimension.Texture2D, Format = Format.R8G8B8A8_UNorm, } )); alt2RenderTargetUIntUAV = ToDispose(new UnorderedAccessView(device, alt2Target , new UnorderedAccessViewDescription { Dimension = UnorderedAccessViewDimension.Texture2D, Format = Format.R32_UInt, } )); } } #endregion }
protected override void CreateSizeDependentResources(D3DApplicationBase app) { base.CreateSizeDependentResources(app); //// Create a viewport descriptor of the render size. //this.Viewport = new SharpDX.ViewportF( // (float)RenderTargetBounds.X + 100, // (float)RenderTargetBounds.Y + 100, // (float)RenderTargetBounds.Width - 200, // (float)RenderTargetBounds.Height - 200, // 0.0f, // min depth // 1.0f); // max depth //// Set the current viewport for the rasterizer. //this.DeviceManager.Direct3DContext.Rasterizer.SetViewport(Viewport); }
protected override void CreateSizeDependentResources(D3DApplicationBase app) { // Clear the render targets of any deferred contexts before // processing a resize. if (contextList != null) { foreach (var context in contextList) { if (context != null && context.TypeInfo == DeviceContextType.Deferred) { context.OutputMerger.ResetTargets(); context.Dispose(); } } } base.CreateSizeDependentResources(app); }
/// <summary> /// Create size dependent resources, in this case the swap chain and render targets /// </summary> /// <param name="app"></param> protected virtual void CreateSizeDependentResources(D3DApplicationBase app) { // Retrieve references to device and context var device = DeviceManager.Direct3DDevice; var context = DeviceManager.Direct3DContext; // Retrieve Direct2D context (for use with text rendering etc) var d2dContext = DeviceManager.Direct2DContext; // Before the swapchain can resize all the buffers must be released RemoveAndDispose(ref _backBuffer); RemoveAndDispose(ref _renderTargetView); RemoveAndDispose(ref _depthStencilView); RemoveAndDispose(ref _depthBuffer); RemoveAndDispose(ref _bitmapTarget); d2dContext.Target = null; #region Initialize Direct3D swap chain and render target // If the swap chain already exists, resize it. if (_swapChain != null) { _swapChain.ResizeBuffers( _swapChain.Description1.BufferCount, Width, Height, _swapChain.Description.ModeDescription.Format, _swapChain.Description.Flags); } // Otherwise, create a new one. else { // SwapChain description var desc = CreateSwapChainDescription(); // Rather than create a new DXGI Factory we should reuse // the one that has been used internally to create the device // First, retrieve the underlying DXGI Device from the D3D Device. // access the adapter used for that device and then create the swap chain using (var dxgiDevice2 = device.QueryInterface<SharpDX.DXGI.Device2>()) using (var dxgiAdapter = dxgiDevice2.Adapter) using (var dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>()) using (var output = dxgiAdapter.Outputs.First()) { // The CreateSwapChain method is used so we can descend // from this class and implement a swapchain for a desktop // or a Windows 8 AppStore app _swapChain = ToDispose(CreateSwapChain(dxgiFactory2, device, desc)); #if !NETFX_CORE // Retrieve the list of supported display modes DisplayModeList = output.GetDisplayModeList(desc.Format, DisplayModeEnumerationFlags.Scaling); #endif } } // Obtain the backbuffer for this window which will be the final 3D rendertarget. BackBuffer = ToDispose(Texture2D.FromSwapChain<Texture2D>(_swapChain, 0)); // Create a view interface on the rendertarget to use on bind. RenderTargetView = ToDispose(new RenderTargetView(device, BackBuffer)); // Cache the rendertarget dimensions in our helper class for convenient use. var backBufferDesc = BackBuffer.Description; RenderTargetBounds = new SharpDX.Rectangle(0, 0, backBufferDesc.Width, backBufferDesc.Height); // Create a viewport descriptor of the render size. this.Viewport = new SharpDX.ViewportF( (float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, // min depth 1.0f); // max depth // Set the current viewport for the rasterizer. context.Rasterizer.SetViewport(Viewport); // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D texture as the depth/stencil buffer. // Create a DSV to use on bind. this.DepthBuffer = ToDispose(new Texture2D(device, new Texture2DDescription() { Format = SharpDX.DXGI.Format.D32_Float_S8X24_UInt, ArraySize = 1, MipLevels = 1, Width = RenderTargetSize.Width, Height = RenderTargetSize.Height, SampleDescription = SwapChain.Description.SampleDescription, BindFlags = BindFlags.DepthStencil, })); this.DepthStencilView = ToDispose( new DepthStencilView( device, DepthBuffer, new DepthStencilViewDescription() { Dimension = (SwapChain.Description.SampleDescription.Count > 1 || SwapChain.Description.SampleDescription.Quality > 0) ? DepthStencilViewDimension.Texture2DMultisampled : DepthStencilViewDimension.Texture2D })); // Set the OutputMerger targets context.OutputMerger.SetTargets(DepthStencilView, RenderTargetView); #endregion #region Initialize Direct2D render target // Now we set up the Direct2D render target bitmap linked to the swapchain. // Whenever we render to this bitmap, it will be directly rendered to the // swapchain associated with the window. var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1( new SharpDX.Direct2D1.PixelFormat(_swapChain.Description.ModeDescription.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied), DeviceManager.Dpi, DeviceManager.Dpi, SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw); // Direct2D needs the dxgi version of the backbuffer surface pointer. // Get a D2D surface from the DXGI back buffer to use as the D2D render target. using (var dxgiBackBuffer = _swapChain.GetBackBuffer<SharpDX.DXGI.Surface>(0)) BitmapTarget2D = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties)); // So now we can set the Direct2D render target. d2dContext.Target = BitmapTarget2D; // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces. d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale; #endregion }
public override void Initialize(D3DApplicationBase app) { base.Initialize(app); clock = Stopwatch.StartNew(); }