Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline GL GetData #1564

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion MonoGame.Framework/Graphics/.GL/ConcreteGraphicsDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Platform.Graphics.Utilities;
Expand Down Expand Up @@ -59,7 +60,15 @@ public override void GetBackBufferData<T>(Rectangle? rect, T[] data, int startIn
Rectangle srcRect = rect ?? new Rectangle(0, 0, PresentationParameters.BackBufferWidth, PresentationParameters.BackBufferHeight);
int tSize = ReflectionHelpers.SizeOf<T>();
int flippedY = PresentationParameters.BackBufferHeight - srcRect.Y - srcRect.Height;
GL.ReadPixels(srcRect.X, flippedY, srcRect.Width, srcRect.Height, PixelFormat.Rgba, PixelType.UnsignedByte, data);
GCHandle dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
GL.ReadPixels(srcRect.X, flippedY, srcRect.Width, srcRect.Height, PixelFormat.Rgba, PixelType.UnsignedByte, dataPtr.AddrOfPinnedObject());
}
finally
{
dataPtr.Free();
}

// buffer is returned upside down, so we swap the rows around when copying over
int rowSize = srcRect.Width * PresentationParameters.BackBufferFormat.GetSize() / tSize;
Expand Down
36 changes: 30 additions & 6 deletions MonoGame.Framework/Graphics/.GL/ConcreteTexture2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,16 @@ public void GetData<T>(int level, int arraySlice, Rectangle checkedRect, T[] dat
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, _glTexture, 0);
GL.CheckGLError();

GL.ReadPixels(checkedRect.X, checkedRect.Y, checkedRect.Width, checkedRect.Height, _glFormat, _glType, data);
GL.CheckGLError();
GCHandle dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
GL.ReadPixels(checkedRect.X, checkedRect.Y, checkedRect.Width, checkedRect.Height, _glFormat, _glType, dataPtr.AddrOfPinnedObject());
GL.CheckGLError();
}
finally
{
dataPtr.Free();
}
GL.DeleteFramebuffer(framebufferId);
#else
int tSizeInByte = ReflectionHelpers.SizeOf<T>();
Expand All @@ -199,8 +207,16 @@ public void GetData<T>(int level, int arraySlice, Rectangle checkedRect, T[] dat
int pixelToT = Format.GetSize() / tSizeInByte;
int tFullWidth = Math.Max(this.Width >> level, 1) / 4 * pixelToT;
T[] temp = new T[Math.Max(this.Height >> level, 1) / 4 * tFullWidth];
GL.GetCompressedTexImage(TextureTarget.Texture2D, level, temp);
GL.CheckGLError();
GCHandle pixelsPtr = GCHandle.Alloc(temp, GCHandleType.Pinned);
try
{
GL.GetCompressedTexImage(TextureTarget.Texture2D, level, pixelsPtr.AddrOfPinnedObject());
GL.CheckGLError();
}
finally
{
pixelsPtr.Free();
}

int rowCount = checkedRect.Height / 4;
int tRectWidth = checkedRect.Width / 4 * Format.GetSize() / tSizeInByte;
Expand All @@ -216,8 +232,16 @@ public void GetData<T>(int level, int arraySlice, Rectangle checkedRect, T[] dat
// we need to convert from our format size to the size of T here
int tFullWidth = Math.Max(this.Width >> level, 1) * Format.GetSize() / tSizeInByte;
T[] temp = new T[Math.Max(this.Height >> level, 1) * tFullWidth];
GL.GetTexImage(TextureTarget.Texture2D, level, _glFormat, _glType, temp);
GL.CheckGLError();
GCHandle pixelsPtr = GCHandle.Alloc(temp, GCHandleType.Pinned);
try
{
GL.GetTexImage(TextureTarget.Texture2D, level, _glFormat, _glType, pixelsPtr.AddrOfPinnedObject());
GL.CheckGLError();
}
finally
{
pixelsPtr.Free();
}

int pixelToT = Format.GetSize() / tSizeInByte;
int rowCount = checkedRect.Height;
Expand Down
24 changes: 20 additions & 4 deletions MonoGame.Framework/Graphics/.GL/ConcreteTextureCube.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,16 @@ public void GetData<T>(CubeMapFace face, int level, Rectangle checkedRect, T[] d
int pixelToT = Format.GetSize() / tSizeInByte;
int tFullWidth = Math.Max(this.Size >> level, 1) / 4 * pixelToT;
T[] temp = new T[Math.Max(this.Size >> level, 1) / 4 * tFullWidth];
GL.GetCompressedTexImage(target, level, temp);
GL.CheckGLError();
GCHandle pixelsPtr = GCHandle.Alloc(temp, GCHandleType.Pinned);
try
{
GL.GetCompressedTexImage(target, level, pixelsPtr.AddrOfPinnedObject());
GL.CheckGLError();
}
finally
{
pixelsPtr.Free();
}

int rowCount = checkedRect.Height / 4;
int tRectWidth = checkedRect.Width / 4 * Format.GetSize() / tSizeInByte;
Expand All @@ -126,8 +134,16 @@ public void GetData<T>(CubeMapFace face, int level, Rectangle checkedRect, T[] d
// we need to convert from our format size to the size of T here
int tFullWidth = Math.Max(this.Size >> level, 1) * Format.GetSize() / tSizeInByte;
T[] temp = new T[Math.Max(this.Size >> level, 1) * tFullWidth];
GL.GetTexImage(target, level, _glFormat, _glType, temp);
GL.CheckGLError();
GCHandle pixelsPtr = GCHandle.Alloc(temp, GCHandleType.Pinned);
try
{
GL.GetTexImage(target, level, _glFormat, _glType, pixelsPtr.AddrOfPinnedObject());
GL.CheckGLError();
}
finally
{
pixelsPtr.Free();
}

int pixelToT = Format.GetSize() / tSizeInByte;
int rowCount = checkedRect.Height;
Expand Down
51 changes: 6 additions & 45 deletions MonoGame.Framework/Graphics/.GL/OpenGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ internal unsafe int GenTexture()
[UnmanagedFunctionPointer(callingConvention)]
[MonoNativeFunctionWrapper]
internal delegate void ReadPixelsDelegate(int x, int y, int width, int height, PixelFormat format, PixelType type, IntPtr data);
internal ReadPixelsDelegate ReadPixelsInternal;
internal ReadPixelsDelegate ReadPixels;

[System.Security.SuppressUnmanagedCodeSecurity()]
[UnmanagedFunctionPointer(callingConvention)]
Expand Down Expand Up @@ -1206,13 +1206,13 @@ internal delegate void TexSubImage2DDelegate(TextureTarget target, int level,
[UnmanagedFunctionPointer(callingConvention)]
[MonoNativeFunctionWrapper]
internal delegate void GetTexImageDelegate(TextureTarget target, int level, PixelFormat format, PixelType type, [Out] IntPtr pixels);
internal GetTexImageDelegate GetTexImageInternal;
internal GetTexImageDelegate GetTexImage;

[System.Security.SuppressUnmanagedCodeSecurity()]
[UnmanagedFunctionPointer(callingConvention)]
[MonoNativeFunctionWrapper]
internal delegate void GetCompressedTexImageDelegate(TextureTarget target, int level, [Out] IntPtr pixels);
internal GetCompressedTexImageDelegate GetCompressedTexImageInternal;
internal GetCompressedTexImageDelegate GetCompressedTexImage;

[System.Security.SuppressUnmanagedCodeSecurity()]
[UnmanagedFunctionPointer(callingConvention)]
Expand Down Expand Up @@ -1401,7 +1401,7 @@ private void LoadEntryPoints()
UniformMatrix2fv = LoadFunctionOrNull<UniformMatrix2fvDelegate>("glUniformMatrix2fv");
UniformMatrix3fv = LoadFunctionOrNull<UniformMatrix3fvDelegate>("glUniformMatrix3fv");
UniformMatrix4fv = LoadFunctionOrNull<UniformMatrix4fvDelegate>("glUniformMatrix4fv");
ReadPixelsInternal = LoadFunctionOrNull<ReadPixelsDelegate>("glReadPixels");
ReadPixels = LoadFunctionOrNull<ReadPixelsDelegate>("glReadPixels");
// uniforms OpenGL Version >= 2.1
// ... UniformMatrix 2x3,2x4, 3x2, 3x4, 4x2, 4x3
// uniforms OpenGL Version >= 3.0
Expand Down Expand Up @@ -1476,8 +1476,8 @@ private void LoadEntryPoints()
TexSubImage2D = LoadFunctionOrNull<TexSubImage2DDelegate>("glTexSubImage2D");
PixelStore = LoadFunctionOrNull<PixelStoreDelegate>("glPixelStorei");
Finish = LoadFunctionOrNull<FinishDelegate>("glFinish");
GetTexImageInternal = LoadFunctionOrNull<GetTexImageDelegate>("glGetTexImage");
GetCompressedTexImageInternal = LoadFunctionOrNull<GetCompressedTexImageDelegate>("glGetCompressedTexImage");
GetTexImage = LoadFunctionOrNull<GetTexImageDelegate>("glGetTexImage");
GetCompressedTexImage = LoadFunctionOrNull<GetCompressedTexImageDelegate>("glGetCompressedTexImage");
TexImage3D = LoadFunctionOrNull<TexImage3DDelegate>("glTexImage3D");
TexSubImage3D = LoadFunctionOrNull<TexSubImage3DDelegate>("glTexSubImage3D");
DeleteTextures = LoadFunctionOrNull<DeleteTexturesDelegate>("glDeleteTextures");
Expand Down Expand Up @@ -1761,45 +1761,6 @@ internal void TexParameter(TextureTarget target, TextureParameterName name, int
TexParameteri(target, name, value);
}

internal void GetTexImage<T>(TextureTarget target, int level, PixelFormat format, PixelType type, T[] pixels) where T : struct
{
GCHandle pixelsPtr = GCHandle.Alloc(pixels, GCHandleType.Pinned);
try
{
GetTexImageInternal(target, level, format, type, pixelsPtr.AddrOfPinnedObject());
}
finally
{
pixelsPtr.Free();
}
}

internal void GetCompressedTexImage<T>(TextureTarget target, int level, T[] pixels) where T : struct
{
GCHandle pixelsPtr = GCHandle.Alloc(pixels, GCHandleType.Pinned);
try
{
GetCompressedTexImageInternal(target, level, pixelsPtr.AddrOfPinnedObject());
}
finally
{
pixelsPtr.Free();
}
}

public void ReadPixels<T>(int x, int y, int width, int height, PixelFormat format, PixelType type, T[] data)
{
GCHandle dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
ReadPixelsInternal(x, y, width, height, format, type, dataPtr.AddrOfPinnedObject());
}
finally
{
dataPtr.Free();
}
}


[Conditional("DEBUG")]
[DebuggerHidden]
Expand Down
13 changes: 11 additions & 2 deletions MonoGame.Framework/Media/Android/ConcreteVideoPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Copyright (C)2023 Nick Kastellanos

using System;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
Expand Down Expand Up @@ -172,8 +173,16 @@ public override Texture2D PlatformGetTexture()
GL.CheckGLError();

// Read the pixel data from the framebuffer
GL.ReadPixels(0, 0, this.Video.Width, this.Video.Height, GLPixelFormat.Rgba, PixelType.UnsignedByte, _frameData);
GL.CheckGLError();
GCHandle dataPtr = GCHandle.Alloc(_frameData, GCHandleType.Pinned);
try
{
GL.ReadPixels(0, 0, this.Video.Width, this.Video.Height, GLPixelFormat.Rgba, PixelType.UnsignedByte, dataPtr.AddrOfPinnedObject());
GL.CheckGLError();
}
finally
{
dataPtr.Free();
}

// Dettach framebuffer
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
Expand Down
Loading