Пример #1
0
        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();
                }
            }
        }
Пример #2
0
    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;
    }
Пример #3
0
 /// <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;
     }
 }
Пример #4
0
 protected override void DisposeResource()
 {
     if (_Surface != null)
     {
         _Surface.Dispose();
         _Surface = null;
     }
     base.DisposeResource();
 }
Пример #5
0
    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;
      }
    }
Пример #6
0
        /// <summary>
        /// Dispose the layer.
        /// </summary>
        public void Dispose()
        {
            if (surface != null)
            {
                surface.Dispose();
                surface = null;
            }

            if (texture != null)
            {
                texture.Dispose();
                texture = null;
            }
        }
Пример #7
0
 /// <summary>
 /// Initialize the device on the control.
 /// </summary>
 /// <param name="control"></param>
 public void Initialize(Control control)
 {
     padding = control.Padding;
     size    = control.Size;
     //! Dispose the layers.
     for (int i = 0; i < layers.Count; i++)
     {
         if (layers[i] != null)
         {
             layers[i].Dispose();
         }
     }
     //! Dispose the sprite.
     if (sprite != null)
     {
         sprite.Dispose();
         sprite = null;
     }
     //! Dispose the main surface.
     if (mainSurface != null)
     {
         mainSurface.Dispose();
         mainSurface = null;
     }
     System.GC.Collect();
     //! if device isn't null then reset the device size.
     if (device != null)
     {
         device.Reset(new SharpDX.Direct3D9.PresentParameters(control.ClientSize.Width, control.ClientSize.Height));
     }
     else
     {
         //! create a new device.
         device = new SharpDX.Direct3D9.Device(new SharpDX.Direct3D9.Direct3D(), 0, SharpDX.Direct3D9.DeviceType.Hardware, control.Handle,
                                               SharpDX.Direct3D9.CreateFlags.HardwareVertexProcessing, new SharpDX.Direct3D9.PresentParameters(control.ClientSize.Width, control.ClientSize.Height));
     }
     //! create a new sprite.
     sprite = new SharpDX.Direct3D9.Sprite(device);
     //! Create layers.
     for (int i = 0; i < layers.Count; i++)
     {
         layers[i] = new Layer(new SharpDX.Direct3D9.Texture(device, size.Width, size.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.Pool.Default));
     }
 }
Пример #8
0
 public Surface(SharpDX.Direct3D9.Surface surface)
 {
     _Surface = surface;
 }