void INativeRenderTarget.GetData <T>(T[] buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int targetIndex, int x, int y, int width, int height) { DefaultOpenTKBackendPlugin.GuardSingleThreadState(); NativeRenderTarget lastRt = BoundRT; Bind(this); { ReadBufferMode oldReadBuffer = (ReadBufferMode)GL.GetInteger(GetPName.ReadBuffer); GL.ReadBuffer((ReadBufferMode)((int)ReadBufferMode.ColorAttachment0 + targetIndex)); GL.ReadPixels(x, y, width, height, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), buffer); GL.ReadBuffer(oldReadBuffer); } Bind(lastRt); }
void INativeTexture.GetData(IntPtr target, ColorDataLayout dataLayout, ColorDataElementType dataElementType) { DefaultOpenTKBackendPlugin.GuardSingleThreadState(); int lastTexId; GL.GetInteger(GetPName.TextureBinding2D, out lastTexId); GL.BindTexture(TextureTarget.Texture2D, this.handle); GL.GetTexImage(TextureTarget.Texture2D, 0, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), target); GL.BindTexture(TextureTarget.Texture2D, lastTexId); }
void INativeRenderTarget.GetData <T>(T[] buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int targetIndex, int x, int y, int width, int height) { DefaultOpenTKBackendPlugin.GuardSingleThreadState(); this.ApplyPostRender(); if (curBound != this) { ApplyGLBind(this); } { GL.Ext.BindFramebuffer(FramebufferTarget.ReadFramebuffer, this.handleMainFBO); GL.ReadBuffer((ReadBufferMode)((int)ReadBufferMode.ColorAttachment0 + targetIndex)); GL.ReadPixels(x, y, width, height, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), buffer); } ApplyGLBind(curBound); }
void INativeRenderTarget.GetData <T>(T[] buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int targetIndex, int x, int y, int width, int height) { // Removed thread guards because of performance //DefaultOpenTKBackendPlugin.GuardSingleThreadState(); // ToDo //this.ApplyPostRender(); //if (curBound != this) ApplyGLBind(this); //{ // GraphicsBackend.GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, this.handleMainFBO); // GraphicsBackend.GL.ReadBuffer((ReadBufferMode)((int)ReadBufferMode.ColorAttachment0 + targetIndex)); // GraphicsBackend.GL.ReadPixels(x, y, width, height, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), buffer); //} //ApplyGLBind(curBound); throw new NotSupportedException(); }
void INativeTexture.GetData <T>(T[] target, ColorDataLayout dataLayout, ColorDataElementType dataElementType) { // ToDo: Add similar code for OpenGL ES throw new NotSupportedException(); // Removed thread guards because of performance //DefaultOpenTKBackendPlugin.GuardSingleThreadState(); // //int lastTexId; //GL.GetInteger(GetPName.TextureBinding2D, out lastTexId); //GL.BindTexture(TextureTarget.Texture2D, this.handle); // // //OpenTK.Graphics.OpenGL.GL.GetTexImage(TextureTarget.Texture2D, 0, // dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), // target); // //GL.BindTexture(TextureTarget.Texture2D, lastTexId); }
/// <summary> /// Uploads the specified pixel data in RGBA format to video memory. A call to <see cref="INativeTexture.SetupEmpty"/> /// is to be considered required for this. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="texture"></param> /// <param name="format">The textures internal format.</param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="data">The block of pixel data to transfer.</param> /// <param name="dataLayout">The color layout of the specified data block.</param> /// <param name="dataElementType">The color element type of the specified data block.</param> public static void LoadData <T>( this INativeTexture texture, TexturePixelFormat format, int width, int height, T[] data, ColorDataLayout dataLayout, ColorDataElementType dataElementType) where T : struct { using (PinnedArrayHandle pinned = new PinnedArrayHandle(data)) { texture.LoadData( format, width, height, pinned.Address, dataLayout, dataElementType); } }
/// <summary> /// Retrieves the main rendering buffer's pixel data from video memory in the Rgba8 format. /// As a storage array type, either byte or <see cref="ColorRgba"/> is recommended. /// </summary> /// <param name="target">The target buffer to store transferred pixel data in.</param> /// <param name="dataLayout">The desired color layout of the specified buffer.</param> /// <param name="dataElementType">The desired color element type of the specified buffer.</param> /// <param name="x">The x position of the rectangular area to read.</param> /// <param name="y">The y position of the rectangular area to read.</param> /// <param name="width">The width of the rectangular area to read. Defaults to the rendering targets width.</param> /// <param name="height">The height of the rectangular area to read. Defaults to the rendering targets height.</param> public static void GetOutputPixelData <T>( this IGraphicsBackend backend, T[] target, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int x, int y, int width, int height) where T : struct { using (PinnedArrayHandle pinned = new PinnedArrayHandle(target)) { backend.GetOutputPixelData( pinned.Address, dataLayout, dataElementType, x, y, width, height); } }
/// <summary> /// Retrieves the rendering targets pixel data from video memory in the Rgba8 format. /// As a storage array type, either byte or <see cref="ColorRgba"/> is recommended. /// </summary> /// <param name="renderTarget"></param> /// <param name="buffer">The target buffer to store transferred pixel data in.</param> /// <param name="dataLayout">The desired color layout of the specified buffer.</param> /// <param name="dataElementType">The desired color element type of the specified buffer.</param> /// <param name="targetIndex">The target texture lists index to read from.</param> /// <param name="x">The x position of the rectangular area to read.</param> /// <param name="y">The y position of the rectangular area to read.</param> /// <param name="width">The width of the rectangular area to read. Defaults to the rendering targets width.</param> /// <param name="height">The height of the rectangular area to read. Defaults to the rendering targets height.</param> public static void GetData <T>( this INativeRenderTarget renderTarget, T[] buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int targetIndex, int x, int y, int width, int height) where T : struct { using (PinnedArrayHandle pinned = new PinnedArrayHandle(buffer)) { renderTarget.GetData( pinned.Address, dataLayout, dataElementType, targetIndex, x, y, width, height); } }
void IGraphicsBackend.GetOutputPixelData <T>(T[] buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int x, int y, int width, int height) { DefaultOpenTKBackendPlugin.GuardSingleThreadState(); NativeRenderTarget lastRt = NativeRenderTarget.BoundRT; NativeRenderTarget.Bind(null); { GL.ReadPixels(x, y, width, height, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), buffer); // The image will be upside-down because of OpenGL's coordinate system. Flip it. int structSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)); T[] switchLine = new T[width * 4 / structSize]; for (int flipY = 0; flipY < height / 2; flipY++) { int lineIndex = flipY * width * 4 / structSize; int lineIndex2 = (height - 1 - flipY) * width * 4 / structSize; // Copy the current line to the switch buffer for (int lineX = 0; lineX < width; lineX++) { switchLine[lineX] = buffer[lineIndex + lineX]; } // Copy the opposite line to the current line for (int lineX = 0; lineX < width; lineX++) { buffer[lineIndex + lineX] = buffer[lineIndex2 + lineX]; } // Copy the switch buffer to the opposite line for (int lineX = 0; lineX < width; lineX++) { buffer[lineIndex2 + lineX] = switchLine[lineX]; } } } NativeRenderTarget.Bind(lastRt); }
void IGraphicsBackend.GetOutputPixelData(IntPtr buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int x, int y, int width, int height) { //DefaultOpenTKBackendPlugin.GuardSingleThreadState(); NativeRenderTarget lastRt = NativeRenderTarget.BoundRT; NativeRenderTarget.Bind(null); { // Use a temporary local buffer, since the image will be upside-down because // of OpenGL's coordinate system and we'll need to flip it before returning. byte[] byteData = new byte[width * height * 4]; // Retrieve pixel data GL.ReadPixels(x, y, width, height, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), byteData); // Flip the retrieved image vertically int bytesPerLine = width * 4; byte[] switchLine = new byte[width * 4]; for (int flipY = 0; flipY < height / 2; flipY++) { int lineIndex = flipY * width * 4; int lineIndex2 = (height - 1 - flipY) * width * 4; // Copy the current line to the switch buffer for (int lineX = 0; lineX < bytesPerLine; lineX++) { switchLine[lineX] = byteData[lineIndex + lineX]; } // Copy the opposite line to the current line for (int lineX = 0; lineX < bytesPerLine; lineX++) { byteData[lineIndex + lineX] = byteData[lineIndex2 + lineX]; } // Copy the switch buffer to the opposite line for (int lineX = 0; lineX < bytesPerLine; lineX++) { byteData[lineIndex2 + lineX] = switchLine[lineX]; } } // Copy the flipped data to the output buffer Marshal.Copy(byteData, 0, buffer, width * height * 4); } NativeRenderTarget.Bind(lastRt); }
void IGraphicsBackend.GetOutputPixelData(IntPtr target, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int x, int y, int width, int height) { }
void IGraphicsBackend.GetOutputPixelData <T>(T[] buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int x, int y, int width, int height) { }
void INativeTexture.LoadData(TexturePixelFormat format, int width, int height, IntPtr data, ColorDataLayout dataLayout, ColorDataElementType dataElementType) { DefaultOpenTKBackendPlugin.GuardSingleThreadState(); int lastTexId; GL.GetInteger(GetPName.TextureBinding2D, out lastTexId); GL.BindTexture(TextureTarget.Texture2D, this.handle); // Load pixel data to video memory GL.TexImage2D(TextureTarget.Texture2D, 0, ToOpenTKPixelFormat(format), width, height, 0, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), data); this.width = width; this.height = height; this.format = format; GL.BindTexture(TextureTarget.Texture2D, lastTexId); }
void INativeTexture.LoadData <T>(TexturePixelFormat format, int width, int height, T[] data, ColorDataLayout dataLayout, ColorDataElementType dataElementType) { // Removed thread guards because of performance //DefaultOpenTKBackendPlugin.GuardSingleThreadState(); int lastTexId; GL.GetInteger(GetPName.TextureBinding2D, out lastTexId); GL.BindTexture(TextureTarget.Texture2D, this.handle); // Load pixel data to video memory GL.TexImage2D(TextureTarget.Texture2D, 0, ToOpenTKPixelInternalFormat(format), width, height, 0, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), data); GL.GenerateMipmap(TextureTarget.Texture2D); this.width = width; this.height = height; this.format = format; GL.BindTexture(TextureTarget.Texture2D, lastTexId); }
void INativeRenderTarget.GetData(IntPtr buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int targetIndex, int x, int y, int width, int height) { }
void INativeTexture.LoadData(TexturePixelFormat format, int width, int height, IntPtr data, ColorDataLayout dataLayout, ColorDataElementType dataElementType) { }
void INativeTexture.GetData(IntPtr target, ColorDataLayout dataLayout, ColorDataElementType dataElementType) { }
unsafe void INativeTexture.LoadData(TexturePixelFormat format, int width, int height, IntPtr data, ColorDataLayout dataLayout, ColorDataElementType dataElementType) { // Removed thread guards because of performance //DefaultOpenTKBackendPlugin.GuardSingleThreadState(); WebGLTexture lastTexId = (WebGLTexture)GraphicsBackend.GL.GetParameter(WebGLRenderingContextBase.TEXTURE_BINDING_2D); GraphicsBackend.GL.BindTexture(WebGLRenderingContextBase.TEXTURE_2D, this.handle); // Load pixel data to video memory GraphicsBackend.GL.TexImage2D(WebGLRenderingContextBase.TEXTURE_2D, 0, ToOpenTKPixelInternalFormat(format), width, height, 0, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), TypedArray <Uint8ClampedArray, byte> .From(new Span <byte>(data.ToPointer(), /*ToDo*/ (width * height * 4)))); GraphicsBackend.GL.GenerateMipmap(WebGLRenderingContextBase.TEXTURE_2D); this.width = width; this.height = height; this.format = format; GraphicsBackend.GL.BindTexture(WebGLRenderingContextBase.TEXTURE_2D, lastTexId); }