Пример #1
0
 private void PlatformGraphicsDeviceResetting()
 {
     if (!_handle.IsNull)
     {
         GraphicsDevice.DisposeResource(_handle);
         _handle = default;
     }
 }
 /// <summary>
 /// Clear the program cache releasing all shader programs.
 /// </summary>
 public void Clear()
 {
     foreach (var pair in _programCache)
     {
         _graphicsDevice.DisposeResource(pair.Value.Program);
     }
     _programCache.Clear();
 }
Пример #3
0
        /// <summary>
        /// Releases resources associated with this buffer.
        /// </summary>
        /// <param name="disposing"><see langword="true"/> if managed objects should be disposed.</param>
        protected override void Dispose(bool disposing)
        {
            if (!IsDisposed)
            {
                GraphicsDevice.DisposeResource(_glBuffer);
            }

            base.Dispose(disposing);
        }
Пример #4
0
        protected override void Dispose(bool disposing)
        {
            if (!IsDisposed && _handle.IsNull)
            {
                GraphicsDevice.DisposeResource(_handle);
                _handle = default;
            }

            base.Dispose(disposing);
        }
Пример #5
0
 private void DeleteGLTexture()
 {
     GraphicsDevice.DisposeResource(_glTexture);
     _glTexture = default;
 }
Пример #6
0
        private unsafe void PlatformGetData <T>(
            int level, int arraySlice, Rectangle rect, Span <T> destination)
            where T : unmanaged
        {
            if (GL.IsES)
            {
                // TODO: check for for non renderable formats (formats that can't be attached to FBO)
                GL.GenFramebuffers(1, out int framebufferId);
                GL.CheckError();
                var handle = GLHandle.Framebuffer(framebufferId);
                try
                {
                    GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebufferId);
                    GL.CheckError();

                    GL.FramebufferTexture2D(
                        FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D,
                        _glTexture, 0);
                    GL.CheckError();

                    fixed(T *ptr = destination)
                    {
                        GL.ReadPixels(rect.X, rect.Y, rect.Width, rect.Height, _glFormat, _glType, (IntPtr)ptr);
                        GL.CheckError();
                    }
                    GraphicsDevice.DisposeResource(handle);
                }
                catch
                {
                    handle.Free();
                }
            }
            else
            {
                GL.BindTexture(TextureTarget.Texture2D, _glTexture);
                GL.PixelStore(PixelStoreParameter.PackAlignment, Math.Min(sizeof(T), 8));

                // TODO: optimize with stackalloc (will only work on certain sizes)

                int dstSize  = destination.Length * sizeof(T);
                var dstBytes = MemoryMarshal.AsBytes(destination);
                var buffer   = IntPtr.Zero;
                try
                {
                    if (_glFormat == GLPixelFormat.CompressedTextureFormats)
                    {
                        // Note: for compressed format Format.GetSize() returns the size of a 4x4 block
                        int pixelToT    = Format.GetSize() / sizeof(T);
                        int tFullWidth  = Math.Max(Width >> level, 1) / 4 * pixelToT;
                        int bufferBytes = Math.Max(Height >> level, 1) / 4 * tFullWidth * sizeof(T);
                        buffer = Marshal.AllocHGlobal(bufferBytes);
                        var bufferSpan = new ReadOnlySpan <byte>((void *)buffer, bufferBytes);

                        GL.GetCompressedTexImage(TextureTarget.Texture2D, level, buffer);
                        GL.CheckError();

                        int rows       = rect.Height / 4;
                        int tRectWidth = rect.Width / 4 * Format.GetSize() / sizeof(T);

                        for (int row = 0; row < rows; row++)
                        {
                            int bufferStart = rect.X / 4 * pixelToT + (rect.Top / 4 + row) * tFullWidth;
                            int dataStart   = row * tRectWidth;

                            var src = bufferSpan.Slice(bufferStart * sizeof(T), tRectWidth * sizeof(T));
                            var dst = dstBytes.Slice(dataStart * sizeof(T), src.Length);
                            src.CopyTo(dst);
                        }
                    }
                    else
                    {
                        // we need to convert from our format size to the size of T here
                        int tFullWidth  = Math.Max(Width >> level, 1) * Format.GetSize() / sizeof(T);
                        int bufferBytes = Math.Max(Height >> level, 1) * tFullWidth * sizeof(T);
                        buffer = Marshal.AllocHGlobal(bufferBytes);
                        var bufferSpan = new ReadOnlySpan <byte>((void *)buffer, bufferBytes);

                        GL.GetTexImage(TextureTarget.Texture2D, level, _glFormat, _glType, buffer);
                        GL.CheckError();

                        int pixelToT   = Format.GetSize() / sizeof(T);
                        int tRectWidth = rect.Width * pixelToT;

                        for (int row = 0; row < rect.Height; row++)
                        {
                            int bufferStart = rect.X * pixelToT + (row + rect.Top) * tFullWidth;
                            int dataStart   = row * tRectWidth;

                            var src = bufferSpan.Slice(bufferStart * sizeof(T), tRectWidth * sizeof(T));
                            var dst = dstBytes.Slice(dataStart * sizeof(T), src.Length);
                            src.CopyTo(dst);
                        }
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }
        }