private void UninitializeResources() { if (_renderTarget == null) return; // Unassign back buffer from D3DImage. Lock(); SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero, true); #else SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); Unlock(); // Dispose resources. _query.SafeDispose(); _query = null; _isFrameReady = true; // Set to true while nothing is being rendered. _stagingResource9.SafeDispose(); _stagingResource9 = null; _surface9.SafeDispose(); _surface9 = null; _texture9.SafeDispose(); _texture9 = null; _stagingResource11.SafeDispose(); _stagingResource11 = null; _texture11.SafeDispose(); _texture11 = null; _renderTarget.SafeDispose(); _renderTarget = null; }
public void CreateD3D9TextureFromD3D10Texture(SharpDX.Direct3D10.Texture2D Texture) { DirectXHelpers.SafeDispose(ref shareableTexture9); if (IsShareable(Texture)) { SharpDX.Direct3D9.Format format = TranslateFormat(Texture); if (format == SharpDX.Direct3D9.Format.Unknown) { throw new ArgumentException("Texture format is not compatible with OpenSharedResource"); } IntPtr Handle = GetSharedHandle(Texture); if (Handle == IntPtr.Zero) { throw new ArgumentNullException("Handle"); } shareableTexture9 = new SharpDX.Direct3D9.Texture(sharpDXGraphicsDeviceService9.GraphicsDevice, Texture.Description.Width, Texture.Description.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, format, SharpDX.Direct3D9.Pool.Default, ref Handle); } else { throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared"); } }
/// <summary> /// Load image to the texture. /// </summary> /// <param name="image"></param> public void LoadImage(System.Drawing.Bitmap image) { System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter(); texture = SharpDX.Direct3D9.Texture.FromMemory(device.device, (byte[])converter.ConvertTo(image, typeof(byte[])), (int)(image.Width), (int)(image.Height), 1, SharpDX.Direct3D9.Usage.None, SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.Pool.Default, SharpDX.Direct3D9.Filter.Linear, SharpDX.Direct3D9.Filter.Linear, 0); imageSize = image.Size; }
/// <summary> /// Begin drawing layer. /// </summary> /// <param name="layerIndex"></param> public void BeginLayerDraw(int layerIndex) { try { if (layerIndex >= layers.Count) { SharpDX.Direct3D9.Texture tempTexture; if (layerIndex == 5 && size.Width > padding.Left + padding.Right && size.Height > padding.Top + padding.Bottom) { tempTexture = new SharpDX.Direct3D9.Texture(device, size.Width - padding.Left - padding.Right, size.Height - padding.Top - padding.Bottom , 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.Pool.Default); } else { tempTexture = new SharpDX.Direct3D9.Texture(device, size.Width, size.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.Pool.Default); } layers.Add(new Layer(tempTexture)); } mainSurface = device.GetRenderTarget(0); device.SetRenderTarget(0, layers[layerIndex].surface); device.BeginScene(); } catch (System.Exception ex) { //throw ex; } }
private RenderTarget2D CreateRenderTarget() { var actualWidth = (int)ActualWidth; var actualHeight = (int)ActualHeight; if (actualWidth == 0 || actualHeight == 0) { return(null); } if (GraphicsDevice == null) { return(null); } var renderTarget = new RenderTarget2D(GraphicsDevice, actualWidth, actualHeight, false, SurfaceFormat.Bgra32, DepthFormat.Depth24Stencil8, 1, RenderTargetUsage.PlatformContents, true); var handle = renderTarget.GetSharedHandle(); if (handle == IntPtr.Zero) { throw new ArgumentException("Handle could not be retrieved"); } _renderTargetD3D9 = new SharpDX.Direct3D9.Texture(_graphicsDeviceService.Direct3DDevice, renderTarget.Width, renderTarget.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.Pool.Default, ref handle); using (var surface = _renderTargetD3D9.GetSurfaceLevel(0)) { _direct3DImage.Lock(); _direct3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); _direct3DImage.Unlock(); } return(renderTarget); }
public void EndD3D() { renderTarget.Dispose(); device.Dispose(); context.Dispose(); renderTarget = null; device = null; context = null; }
public Texture2D( IGraphicsDevice graphicsDevice, int width, int height ) { if ( width == 0 ) width = 1; if ( height == 0 ) height = 1; texture = new SharpDX.Direct3D9.Texture ( graphicsDevice.Handle as SharpDX.Direct3D9.Device, width, height, 1, SharpDX.Direct3D9.Usage.AutoGenerateMipMap, SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.Pool.Managed ); texture.FilterTexture ( 0, SharpDX.Direct3D9.Filter.Point ); Size = new Vector2 ( width, height ); }
private void InitializeResources(GraphicsDevice graphicsDevice, int width, int height) { try { Debug.Assert(_renderTarget == null, "Dispose previous back buffer before creating a new back buffer."); // MonoGame var format = _enableAlpha ? SurfaceFormat.Bgra32 : SurfaceFormat.Bgr32; _renderTarget = new RenderTarget2D(graphicsDevice, width, height, false, format, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.DiscardContents); // Direct3D 11 var device11 = (SharpDX.Direct3D11.Device)graphicsDevice.Handle; var formatDXGI = D3D11Helper.ToD3D11(format); _texture11 = D3D11Helper.CreateSharedResource(device11, width, height, formatDXGI); _stagingResource11 = D3D11Helper.CreateStagingResource(device11, _texture11); // Direct3D 9 _texture9 = _d3D9.CreateSharedTexture(_texture11); _surface9 = _texture9.GetSurfaceLevel(0); _stagingResource9 = _d3D9.CreateStagingResource(_texture11); // Direct3D 11 event query. Debug.Assert(_isFrameReady, "_isFrameReady should be true when uninitialized."); var queryDescription = new SharpDX.Direct3D11.QueryDescription { Flags = SharpDX.Direct3D11.QueryFlags.None, Type = SharpDX.Direct3D11.QueryType.Event }; _query = new SharpDX.Direct3D11.Query(device11, queryDescription); // Assign back buffer to D3DImage. // The back buffer is still empty, however we need to set a valid back buffer // for the layout logic. Otherwise, the size of the D3D11Image is (0, 0). Lock(); SetBackBuffer(D3DResourceType.IDirect3DSurface9, _surface9.NativePointer, true); #else SetBackBuffer(D3DResourceType.IDirect3DSurface9, _surface9.NativePointer); Unlock(); if (IsSynchronized) { // Issue a copy of the surface into the staging resource to see when the // resource is no longer used by Direct3D 9. _d3D9.Copy(_surface9, _stagingResource9); _isAvailable9 = _d3D9.TryAccess(_stagingResource9); } } catch { // GPU may run out of memory. UninitializeResources(); throw; } }
/// <summary> /// Initialization, invoke in change directx device or surface size /// </summary> /// <param name="target"></param> public void SetRenderTargetDX11(Texture2D target) { if (this.renderTarget != null) { renderTarget.Dispose(); renderTarget = null; base.Lock(); base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); base.Unlock(); } IsInitilized = target != null; if (target == null) { return; } if (!IsShareable(target)) { throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared"); } var format = TranslateFormat(target); if (format == SharpDX.Direct3D9.Format.Unknown) { throw new ArgumentException("Texture format is not compatible with OpenSharedResource"); } var handle = GetSharedHandle(target); if (handle == IntPtr.Zero) { throw new ArgumentNullException("Handle"); } try { this.renderTarget = new SharpDX.Direct3D9.Texture(device, target.Description.Width, target.Description.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, format, SharpDX.Direct3D9.Pool.Default, ref handle); using (var surface = this.renderTarget.GetSurfaceLevel(0)) { base.Lock(); #if NET40 base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); #else // "enableSoftwareFallback = true" makes Remote Desktop possible. // See: http://msdn.microsoft.com/en-us/library/hh140978%28v=vs.110%29.aspx base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer, true); #endif base.Unlock(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
protected Texture2D( IGraphicsDevice graphicsDevice, int width, int height, SharpDX.Direct3D9.Usage usage, int mipmapLevel = 1 ) { if ( width == 0 ) width = 1; if ( height == 0 ) height = 1; texture = new SharpDX.Direct3D9.Texture ( graphicsDevice.Handle as SharpDX.Direct3D9.Device, width, height, mipmapLevel, usage, SharpDX.Direct3D9.Format.A8R8G8B8, usage.HasFlag ( SharpDX.Direct3D9.Usage.RenderTarget ) ? SharpDX.Direct3D9.Pool.Default : SharpDX.Direct3D9.Pool.Managed ); texture.FilterTexture ( 0, SharpDX.Direct3D9.Filter.Point ); Width = width; Height = height; }
public Polygon(IDrawing.IDevice device) { this.device = (Device)device; line = new SharpDX.Direct3D9.Line(this.device.device); line.Width = lineWidth; line.Antialias = true; fillerLine = new SharpDX.Direct3D9.Line(this.device.device); fillerLine.Width = lineWidth; fillerLine.Antialias = true; sprite = new SharpDX.Direct3D9.Sprite(this.device.device); tex = new SharpDX.Direct3D9.Texture(this.device.device, this.device.size.Width, this.device.size.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.Pool.Default); }
/// <summary> /// Dispose the layer. /// </summary> public void Dispose() { if (surface != null) { surface.Dispose(); surface = null; } if (texture != null) { texture.Dispose(); texture = null; } }
/// <summary> /// If we didn't do this, D3DImage would keep an reference to the backbuffer that causes the device reset below to fail. /// </summary> private void RemoveBackBufferReference() { if (_renderTarget != null) { _renderTarget.Dispose(); _renderTarget = null; } if (_renderTargetD3D9 != null) { _renderTargetD3D9.Dispose(); _renderTargetD3D9 = null; } _d3dImage.Lock(); _d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); _d3dImage.Unlock(); }
/// <summary> /// <p>Adds a sprite to the list of batched sprites.</p> /// </summary> /// <param name="textureRef"><dd> <p>Pointer to an <strong><see cref="SharpDX.Direct3D9.Texture"/></strong> interface that represents the sprite texture.</p> </dd></param> /// <param name="srcRectRef"><dd> <p>Pointer to a <strong><see cref="SharpDX.Rectangle"/></strong> structure that indicates the portion of the source texture to use for the sprite. If this parameter is <strong><c>null</c></strong>, then the entire source image is used for the sprite.</p> </dd></param> /// <param name="centerRef"><dd> <p>Pointer to a <strong><see cref="SharpDX.Vector3"/></strong> vector that identifies the center of the sprite. If this argument is <strong><c>null</c></strong>, the point (0,0,0) is used, which is the upper-left corner.</p> </dd></param> /// <param name="positionRef"><dd> <p>Pointer to a <strong><see cref="SharpDX.Vector3"/></strong> vector that identifies the position of the sprite. If this argument is <strong><c>null</c></strong>, the point (0,0,0) is used, which is the upper-left corner.</p> </dd></param> /// <param name="color"><dd> <p> <strong><see cref="SharpDX.Color4"/></strong> type. The color and alpha channels are modulated by this value. A value of 0xFFFFFFFF maintains the original source color and alpha data. Use the <strong>D3DCOLOR_RGBA</strong> macro to help generate this color.</p> </dd></param> /// <returns><p>If the method succeeds, the return value is <see cref="SharpDX.Result.Ok"/>. If the method fails, the return value can be one of the following: <see cref="SharpDX.Direct3D9.ResultCode.InvalidCall"/>, D3DXERR_INVALIDDATA.</p></returns> /// <remarks> /// <p>To scale, rotate, or translate a sprite, call <strong><see cref="SharpDX.Direct3D9.Sprite.SetTransform"/></strong> with a matrix that contains the scale, rotate, and translate (SRT) values, before calling <see cref="SharpDX.Direct3D9.Sprite.Draw"/>. For information about setting SRT values in a matrix, see Matrix Transforms.</p> /// </remarks> /// <msdn-id>bb174251</msdn-id> /// <unmanaged>HRESULT ID3DXSprite::Draw([In] IDirect3DTexture9* pTexture,[In] const RECT* pSrcRect,[In] const D3DXVECTOR3* pCenter,[In] const D3DXVECTOR3* pPosition,[In] D3DCOLOR Color)</unmanaged> /// <unmanaged-short>ID3DXSprite::Draw</unmanaged-short> public unsafe void Draw(SharpDX.Direct3D9.Texture textureRef, SharpDX.ColorBGRA color, SharpDX.Rectangle?srcRectRef = null, SharpDX.Vector3?centerRef = null, SharpDX.Vector3?positionRef = null) { SharpDX.Rectangle localRect = default(Rectangle); SharpDX.Vector3 localCenter; SharpDX.Vector3 localPosition; if (srcRectRef.HasValue) { localRect = srcRectRef.Value; } if (centerRef.HasValue) { localCenter = centerRef.Value; } if (positionRef.HasValue) { localPosition = positionRef.Value; } Draw(textureRef, srcRectRef.HasValue ? (IntPtr)(void *)&localRect : IntPtr.Zero, centerRef.HasValue ? (IntPtr)(void *)&localCenter : IntPtr.Zero, positionRef.HasValue ? (IntPtr)(void *)&localPosition : IntPtr.Zero, color); }
private void ResetBackBufferReference() { if (DesignerProperties.GetIsInDesignMode(this)) { return; } if (_renderTarget != null) { _renderTarget.Dispose(); _renderTarget = null; } if (_renderTargetD3D9 != null) { _renderTargetD3D9.Dispose(); _renderTargetD3D9 = null; } _direct3DImage.Lock(); _direct3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); _direct3DImage.Unlock(); }
/// <summary> /// Dispose the polygon /// </summary> public void Dispose() { if (line != null) { line.Dispose(); line = null; } if (fillerLine != null) { fillerLine.Dispose(); fillerLine = null; } if (sprite != null) { sprite.Dispose(); sprite = null; } if (tex != null) { tex.Dispose(); tex = null; } }
public Layer(SharpDX.Direct3D9.Texture texture) { this.texture = texture; this.surface = this.texture.GetSurfaceLevel(0); }
/// <summary> /// <p>Adds a sprite to the list of batched sprites.</p> /// </summary> /// <param name="textureRef"><dd> <p>Pointer to an <strong><see cref="SharpDX.Direct3D9.Texture"/></strong> interface that represents the sprite texture.</p> </dd></param> /// <param name="color"><dd> <p> <strong><see cref="RawColor4"/></strong> type. The color and alpha channels are modulated by this value. A value of 0xFFFFFFFF maintains the original source color and alpha data. Use the <strong>D3DCOLOR_RGBA</strong> macro to help generate this color.</p> </dd></param> /// <returns><p>If the method succeeds, the return value is <see cref="SharpDX.Result.Ok"/>. If the method fails, the return value can be one of the following: <see cref="SharpDX.Direct3D9.ResultCode.InvalidCall"/>, D3DXERR_INVALIDDATA.</p></returns> /// <remarks> /// <p>To scale, rotate, or translate a sprite, call <strong><see cref="SharpDX.Direct3D9.Sprite.SetTransform"/></strong> with a matrix that contains the scale, rotate, and translate (SRT) values, before calling <see cref="SharpDX.Direct3D9.Sprite.Draw"/>. For information about setting SRT values in a matrix, see Matrix Transforms.</p> /// </remarks> /// <msdn-id>bb174251</msdn-id> /// <unmanaged>HRESULT ID3DXSprite::Draw([In] IDirect3DTexture9* pTexture,[In] const RECT* pSrcRect,[In] const D3DXVECTOR3* pCenter,[In] const D3DXVECTOR3* pPosition,[In] D3DCOLOR Color)</unmanaged> /// <unmanaged-short>ID3DXSprite::Draw</unmanaged-short> public void Draw(SharpDX.Direct3D9.Texture textureRef, RawColorBGRA color) { Draw(textureRef, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, color); }
public void CreateD3D9TextureFromD3D10Texture(SharpDX.Direct3D10.Texture2D Texture) { DirectXHelpers.SafeDispose(ref shareableTexture9); if (IsShareable(Texture)) { SharpDX.Direct3D9.Format format = TranslateFormat(Texture); if (format == SharpDX.Direct3D9.Format.Unknown) throw new ArgumentException("Texture format is not compatible with OpenSharedResource"); IntPtr Handle = GetSharedHandle(Texture); if (Handle == IntPtr.Zero) throw new ArgumentNullException("Handle"); shareableTexture9 = new SharpDX.Direct3D9.Texture(sharpDXGraphicsDeviceService9.GraphicsDevice, Texture.Description.Width, Texture.Description.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, format, SharpDX.Direct3D9.Pool.Default, ref Handle); } else throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared"); }
public TextureData(SharpDX.Direct3D9.Texture texture) { this.texture = texture; texture.LockRectangle(0, SharpDX.Direct3D9.LockFlags.Discard, out dataStream); }
private void EnsureRenderTarget() { if (_renderTarget == null) { _renderTarget = new RenderTarget2D(GraphicsDevice, (int)ActualWidth, (int)ActualHeight, false, SurfaceFormat.Bgra32, DepthFormat.Depth24Stencil8, 1, RenderTargetUsage.PlatformContents, true); var handle = _renderTarget.GetSharedHandle(); if (handle == IntPtr.Zero) throw new ArgumentException("Handle could not be retrieved"); _renderTargetD3D9 = new SharpDX.Direct3D9.Texture(DeviceService.D3DDevice, _renderTarget.Width, _renderTarget.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.Pool.Default, ref handle); using (SharpDX.Direct3D9.Surface surface = _renderTargetD3D9.GetSurfaceLevel(0)) { _d3DImage.Lock(); _d3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); _d3DImage.Unlock(); } } }
/// <summary> /// If we didn't do this, D3DImage would keep an reference to the backbuffer that causes the device reset below to fail. /// </summary> private void RemoveBackBufferReference() { if (_renderTarget != null) { _renderTarget.Dispose(); _renderTarget = null; } if (_renderTargetD3D9 != null) { _renderTargetD3D9.Dispose(); _renderTargetD3D9 = null; } _d3DImage.Lock(); _d3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); _d3DImage.Unlock(); }
/// <summary> /// Create the RenderTarget. /// </summary> private void CreateRenderTarget() { if (this.renderTarget == null) { try { this.renderTarget = new RenderTarget2D(this.GraphicsDevice, (int)ActualWidth, (int)ActualHeight, false, SurfaceFormat.Bgra32, DepthFormat.Depth24Stencil8, 1, RenderTargetUsage.PlatformContents, true); var handle = this.renderTarget.GetSharedHandle(); if (handle == IntPtr.Zero) throw new ArgumentException("Handle could not be retrieved"); this.renderTargetD3D9 = new DTexture(DeviceService.D3DDevice, this.renderTarget.Width, this.renderTarget.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.Pool.Default, ref handle); using (SharpDX.Direct3D9.Surface surface = this.renderTargetD3D9.GetSurfaceLevel(0)) { this.d3dimage.Lock(); this.d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); this.d3dimage.Unlock(); } } catch (Exception e) { Debug.WriteLine("An error occured while creating the render target"); this.renderTarget = null; } } }
/// <summary> /// Dispose the D3D9's render target. /// </summary> private void DisposeRenderTargetD3D9() { if (this.renderTargetD3D9 != null) { this.renderTargetD3D9.Dispose(); this.renderTargetD3D9 = null; } }
protected virtual void Dispose( bool isDisposing ) { if ( isDisposing ) { texture.Dispose (); texture = null; } }