protected override void Dispose(bool disposing) { if (!IsDisposed) { #if DIRECTX if (disposing) { if (_renderTargetView != null) { _renderTargetView.Dispose(); _renderTargetView = null; } if (_depthStencilView != null) { _depthStencilView.Dispose(); _depthStencilView = null; } } #elif OPENGL GraphicsDevice.AddDisposeAction(() => { GL.DeleteRenderbuffers(1, ref this.glDepthStencilBuffer); GraphicsExtensions.CheckGLError(); if (this.glFramebuffer > 0) { GL.DeleteFramebuffers(1, ref this.glFramebuffer); GraphicsExtensions.CheckGLError(); } }); #endif } base.Dispose(disposing); }
public override void Dispose() { #if DIRECTX if (_renderTargetView != null) { _renderTargetView.Dispose(); _renderTargetView = null; } if (_depthStencilView != null) { _depthStencilView.Dispose(); _depthStencilView = null; } #elif OPENGL GL.DeleteRenderbuffers(1, ref this.glDepthStencilBuffer); GraphicsExtensions.CheckGLError(); if (this.glFramebuffer > 0) { GL.DeleteFramebuffers(1, ref this.glFramebuffer); GraphicsExtensions.CheckGLError(); } #endif base.Dispose(); }
public RenderBuffer( IGraphicsDevice graphicsDevice, int width, int height ) : base(graphicsDevice, width, height, 1, true) { targetView = new SharpDX.Direct3D11.RenderTargetView ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, Handle as SharpDX.Direct3D11.Texture2D, new SharpDX.Direct3D11.RenderTargetViewDescription () { Dimension = SharpDX.Direct3D11.RenderTargetViewDimension.Texture2D, Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, Texture2D = new SharpDX.Direct3D11.RenderTargetViewDescription.Texture2DResource () { MipSlice = 0 } } ); depthStencilBuffer = new SharpDX.Direct3D11.Texture2D ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, new SharpDX.Direct3D11.Texture2DDescription () { ArraySize = 1, Width = width, Height = height, Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, MipLevels = 0, BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, } ); depthStencil = new SharpDX.Direct3D11.DepthStencilView ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, depthStencilBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription () { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D, Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, Texture2D = new SharpDX.Direct3D11.DepthStencilViewDescription.Texture2DResource () { MipSlice = 0 } } ); shaderView = new SharpDX.Direct3D11.ShaderResourceView ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, Handle as SharpDX.Direct3D11.Texture2D, new SharpDX.Direct3D11.ShaderResourceViewDescription () { Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource () { MipLevels = 1, MostDetailedMip = 0 } } ); }
public RenderTarget2D (GraphicsDevice graphicsDevice, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage) :base (graphicsDevice, width, height, mipMap, preferredFormat, true) { DepthStencilFormat = preferredDepthFormat; MultiSampleCount = preferredMultiSampleCount; RenderTargetUsage = usage; #if DIRECTX // Create a view interface on the rendertarget to use on bind. _renderTargetView = new SharpDX.Direct3D11.RenderTargetView(graphicsDevice._d3dDevice, _texture); #endif // If we don't need a depth buffer then we're done. if (preferredDepthFormat == DepthFormat.None) return; #if DIRECTX // Setup the multisampling description. var multisampleDesc = new SharpDX.DXGI.SampleDescription(1, 0); if ( preferredMultiSampleCount > 1 ) { multisampleDesc.Count = preferredMultiSampleCount; multisampleDesc.Quality = (int)SharpDX.Direct3D11.StandardMultisampleQualityLevels.StandardMultisamplePattern; } // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D surface as the depth/stencil buffer. // Create a DepthStencil view on this surface to use on bind. using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(graphicsDevice._d3dDevice, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDXHelper.ToFormat(preferredDepthFormat), ArraySize = 1, MipLevels = 1, Width = width, Height = height, SampleDescription = multisampleDesc, BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, })) // Create the view for binding to the device. _depthStencilView = new SharpDX.Direct3D11.DepthStencilView(graphicsDevice._d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Format = SharpDXHelper.ToFormat(preferredDepthFormat), Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D }); #elif OPENGL #if GLES GL.GenRenderbuffers(1, ref glDepthStencilBuffer); #else GL.GenRenderbuffers(1, out glDepthStencilBuffer); #endif GraphicsExtensions.CheckGLError(); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, this.glDepthStencilBuffer); GraphicsExtensions.CheckGLError(); var glDepthStencilFormat = GLDepthComponent16; switch (preferredDepthFormat) { case DepthFormat.Depth16: glDepthStencilFormat = GLDepthComponent16; break; case DepthFormat.Depth24: glDepthStencilFormat = GLDepthComponent24; break; case DepthFormat.Depth24Stencil8: glDepthStencilFormat = GLDepth24Stencil8; break; } GL.RenderbufferStorage(GLRenderbuffer, glDepthStencilFormat, this.width, this.height); GraphicsExtensions.CheckGLError(); #endif }
public RenderTarget2D(GraphicsDevice graphicsDevice, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage) : base(graphicsDevice, width, height, mipMap, preferredFormat, true) { DepthStencilFormat = preferredDepthFormat; MultiSampleCount = preferredMultiSampleCount; RenderTargetUsage = usage; #if DIRECTX // Create a view interface on the rendertarget to use on bind. _renderTargetView = new SharpDX.Direct3D11.RenderTargetView(graphicsDevice._d3dDevice, _texture); #endif // If we don't need a depth buffer then we're done. if (preferredDepthFormat == DepthFormat.None) { return; } #if DIRECTX // Setup the multisampling description. var multisampleDesc = new SharpDX.DXGI.SampleDescription(1, 0); if (preferredMultiSampleCount > 1) { multisampleDesc.Count = preferredMultiSampleCount; multisampleDesc.Quality = (int)SharpDX.Direct3D11.StandardMultisampleQualityLevels.StandardMultisamplePattern; } // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D surface as the depth/stencil buffer. // Create a DepthStencil view on this surface to use on bind. using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(graphicsDevice._d3dDevice, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDXHelper.ToFormat(preferredDepthFormat), ArraySize = 1, MipLevels = 1, Width = width, Height = height, SampleDescription = multisampleDesc, BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, })) // Create the view for binding to the device. _depthStencilView = new SharpDX.Direct3D11.DepthStencilView(graphicsDevice._d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Format = SharpDXHelper.ToFormat(preferredDepthFormat), Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D }); #elif OPENGL #if GLES GL.GenRenderbuffers(1, ref glDepthStencilBuffer); #else GL.GenRenderbuffers(1, out glDepthStencilBuffer); #endif GraphicsExtensions.CheckGLError(); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, this.glDepthStencilBuffer); GraphicsExtensions.CheckGLError(); var glDepthStencilFormat = GLDepthComponent16; switch (preferredDepthFormat) { case DepthFormat.Depth16: glDepthStencilFormat = GLDepthComponent16; break; case DepthFormat.Depth24: glDepthStencilFormat = GLDepthComponent24; break; case DepthFormat.Depth24Stencil8: glDepthStencilFormat = GLDepth24Stencil8; break; } GL.RenderbufferStorage(GLRenderbuffer, glDepthStencilFormat, this.width, this.height); GraphicsExtensions.CheckGLError(); #endif }
protected virtual void CreateSizeDependentResources(TargetBase renderBase) { var d3dDevice = DeviceManager.DeviceDirect3D; var d3dContext = DeviceManager.ContextDirect3D; var d2dContext = DeviceManager.ContextDirect2D; d2dContext.Target = null; Utilities.Dispose(ref renderTargetView); Utilities.Dispose(ref depthStencilView); Utilities.Dispose(ref bitmapTarget); Utilities.Dispose(ref backBuffer); // If the swap chain already exists, resize it. if (swapChain != null) { swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None); } // Otherwise, create a new one. else { // SwapChain description var desc = CreateSwapChainDescription(); // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device // First, retrieve the underlying DXGI Device from the D3D Device. // Creates the swap chain using (var dxgiDevice2 = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>()) using (var dxgiAdapter = dxgiDevice2.Adapter) using (var dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>()) { swapChain = CreateSwapChain(dxgiFactory2, d3dDevice, desc); // Ensure that DXGI does not queue more than one frame at a time. This both reduces // latency and ensures that the application will only render after each VSync, minimizing // power consumption. dxgiDevice2.MaximumFrameLatency = 1; } } // Obtain the backbuffer for this window which will be the final 3D rendertarget. backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(swapChain, 0); { // Create a view interface on the rendertarget to use on bind. renderTargetView = new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer); // Cache the rendertarget dimensions in our helper class for convenient use. var backBufferDesc = BackBuffer.Description; RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height); } // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D surface as the depth/stencil buffer. // Create a DepthStencil view on this surface to use on bind. using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)RenderTargetSize.Width, Height = (int)RenderTargetSize.Height, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, })) depthStencilView = new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D }); // Create a viewport descriptor of the full window size. var viewport = new SharpDX.Mathematics.ViewportF((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f); // Set the current viewport using the descriptor. d3dContext.Rasterizer.SetViewport(viewport); // 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(SharpDX.DXGI.Format.B8G8R8A8_UNorm, 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)) bitmapTarget = 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; }
protected virtual void CreateSizeDependentResources(TargetBase renderBase) { var d3dDevice = DeviceManager.DeviceDirect3D; var d3dContext = DeviceManager.ContextDirect3D; var d2dContext = DeviceManager.ContextDirect2D; d2dContext.Target = null; Utilities.Dispose(ref renderTargetView); Utilities.Dispose(ref depthStencilView); Utilities.Dispose(ref bitmapTarget); Utilities.Dispose(ref backBuffer); // If the swap chain already exists, resize it. if (swapChain != null) { swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None); } // Otherwise, create a new one. else { // SwapChain description var desc = CreateSwapChainDescription(); // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device // First, retrieve the underlying DXGI Device from the D3D Device. // Creates the swap chain using (var dxgiDevice2 = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>()) using (var dxgiAdapter = dxgiDevice2.Adapter) using (var dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>()) { swapChain = CreateSwapChain(dxgiFactory2, d3dDevice, desc); // Ensure that DXGI does not queue more than one frame at a time. This both reduces // latency and ensures that the application will only render after each VSync, minimizing // power consumption. dxgiDevice2.MaximumFrameLatency = 1; } } // Obtain the backbuffer for this window which will be the final 3D rendertarget. backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0); { // Create a view interface on the rendertarget to use on bind. renderTargetView = new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer); // Cache the rendertarget dimensions in our helper class for convenient use. var backBufferDesc = BackBuffer.Description; RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height); } // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D surface as the depth/stencil buffer. // Create a DepthStencil view on this surface to use on bind. using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)RenderTargetSize.Width, Height = (int)RenderTargetSize.Height, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, })) depthStencilView = new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D }); // Create a viewport descriptor of the full window size. var viewport = new SharpDX.Mathematics.ViewportF((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f); // Set the current viewport using the descriptor. d3dContext.Rasterizer.SetViewport(viewport); // 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(SharpDX.DXGI.Format.B8G8R8A8_UNorm, 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)) bitmapTarget = 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; }
private void _Direct3Dを初期化する() { // D3D11デバイスと SwapChain を生成します。 SharpDX.Direct3D11.Device.CreateWithSwapChain( SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_11_1 }, // 機能レベル 11.1 new SharpDX.DXGI.SwapChainDescription { BufferCount = 1, Flags = SharpDX.DXGI.SwapChainFlags.AllowModeSwitch, IsWindowed = true, ModeDescription = new SharpDX.DXGI.ModeDescription { Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Width = this.ClientSize.Width, Height = this.ClientSize.Height, Scaling = SharpDX.DXGI.DisplayModeScaling.Stretched, }, OutputHandle = this.Handle, SampleDescription = new SharpDX.DXGI.SampleDescription(4, 0), // MSAA x4 SwapEffect = SharpDX.DXGI.SwapEffect.Discard, Usage = SharpDX.DXGI.Usage.RenderTargetOutput, }, out this._D3D11Device, out this._DXGISwapChain); // 既定のRenderTarget と 既定のDepthStencil ならびにそのビューを作成します。 this._既定のD3D11RenderTarget = this._DXGISwapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0); this._既定のD3D11DepthStencil = new SharpDX.Direct3D11.Texture2D( this._D3D11Device, new SharpDX.Direct3D11.Texture2DDescription { Width = this._既定のD3D11RenderTarget.Description.Width, Height = this._既定のD3D11RenderTarget.Description.Height, MipLevels = 1, ArraySize = 1, Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, SampleDescription = this._既定のD3D11RenderTarget.Description.SampleDescription, Usage = SharpDX.Direct3D11.ResourceUsage.Default, BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None, }); this._既定のD3D11RenderTargetView = new SharpDX.Direct3D11.RenderTargetView(this._D3D11Device, this._既定のD3D11RenderTarget); this._既定のD3D11DepthStencilView = new SharpDX.Direct3D11.DepthStencilView(this._D3D11Device, this._既定のD3D11DepthStencil); // 加算合成用のブレンドステートを作成します。 var BlendStateAdd = new SharpDX.Direct3D11.BlendStateDescription() { AlphaToCoverageEnable = false, // アルファマスクで透過する(するならZバッファ必須) IndependentBlendEnable = false, // 個別設定。false なら BendStateDescription.RenderTarget[0] だけが有効で、[1~7] は無視される。 }; BlendStateAdd.RenderTarget[0].IsBlendEnabled = true; // true ならブレンディングが有効。 BlendStateAdd.RenderTarget[0].RenderTargetWriteMask = SharpDX.Direct3D11.ColorWriteMaskFlags.All; // RGBA の書き込みマスク。 // アルファ値のブレンディング設定 ... 特になし BlendStateAdd.RenderTarget[0].SourceAlphaBlend = SharpDX.Direct3D11.BlendOption.One; BlendStateAdd.RenderTarget[0].DestinationAlphaBlend = SharpDX.Direct3D11.BlendOption.Zero; BlendStateAdd.RenderTarget[0].AlphaBlendOperation = SharpDX.Direct3D11.BlendOperation.Add; // 色値のブレンディング設定 ... 加算合成 BlendStateAdd.RenderTarget[0].SourceBlend = SharpDX.Direct3D11.BlendOption.SourceAlpha; BlendStateAdd.RenderTarget[0].DestinationBlend = SharpDX.Direct3D11.BlendOption.One; BlendStateAdd.RenderTarget[0].BlendOperation = SharpDX.Direct3D11.BlendOperation.Add; // ブレンドステートを作成する。 this._BlendState加算合成 = new SharpDX.Direct3D11.BlendState(this._D3D11Device, BlendStateAdd); // ブレンドステートと深度ステンシルステートを OM に設定します。 this._D3D11Device.ImmediateContext.OutputMerger.BlendState = this._BlendState加算合成; this._D3D11Device.ImmediateContext.OutputMerger.DepthStencilState = null; // ラスタライザステートを作成します。 this._RasterizerState = new SharpDX.Direct3D11.RasterizerState( this._D3D11Device, new SharpDX.Direct3D11.RasterizerStateDescription { CullMode = SharpDX.Direct3D11.CullMode.Back, FillMode = SharpDX.Direct3D11.FillMode.Solid, }); }