Exemplo n.º 1
0
 void INativeTexture.GetData <T>(T[] target, ColorDataLayout dataLayout, ColorDataElementType dataElementType)
 {
     for (int i = 0; i < target.Length; i++)
     {
         target[i] = default(T);
     }
 }
Exemplo n.º 2
0
 void INativeRenderTarget.GetData <T>(T[] buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int targetIndex, int x, int y, int width, int height)
 {
     for (int i = 0; i < buffer.Length; i++)
     {
         buffer[i] = default(T);
     }
 }
Exemplo n.º 3
0
 public static uint ToOpenTK(this ColorDataLayout layout)
 {
     switch (layout)
     {
     default:
     case ColorDataLayout.Rgba: return(WebGLRenderingContextBase.RGBA);
     }
 }
Exemplo n.º 4
0
 public static PixelFormat ToOpenTK(this ColorDataLayout layout)
 {
     switch (layout)
     {
     default:
     case ColorDataLayout.Rgba: return(PixelFormat.Rgba);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Retrieves the textures pixel data from video memory in the Rgba8 format.
 /// As a storage array type, either byte or <see cref="ColorRgba"/> is recommended.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="texture"></param>
 /// <param name="target">The buffer to store pixel values into.</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>
 public static void GetData <T>(
     this INativeTexture texture,
     T[] target,
     ColorDataLayout dataLayout,
     ColorDataElementType dataElementType)
 {
     using (PinnedArrayHandle pinned = new PinnedArrayHandle(target))
     {
         texture.GetData(
             pinned.Address,
             dataLayout,
             dataElementType);
     }
 }
Exemplo n.º 6
0
        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);
            {
                GL.ReadBuffer((ReadBufferMode)((int)ReadBufferMode.ColorAttachment0 + targetIndex));
                GL.ReadPixels(x, y, width, height, dataLayout.ToOpenTK(), dataElementType.ToOpenTK(), buffer);
                GL.ReadBuffer(ReadBufferMode.Back);
            }
            Bind(lastRt);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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();
        }
Exemplo n.º 11
0
 /// <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);
     }
 }
Exemplo n.º 12
0
 /// <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);
     }
 }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
 /// <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);
     }
 }
Exemplo n.º 15
0
        void IGraphicsBackend.GetOutputPixelData <T>(T[] buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int x, int y, int width, int height)
        {
            // Removed thread guards because of performance
            //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 = 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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
 void INativeRenderTarget.GetData(IntPtr buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int targetIndex, int x, int y, int width, int height)
 {
 }
Exemplo n.º 18
0
 void IGraphicsBackend.GetOutputPixelData(IntPtr target, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int x, int y, int width, int height)
 {
 }
Exemplo n.º 19
0
 void INativeTexture.LoadData(TexturePixelFormat format, int width, int height, IntPtr data, ColorDataLayout dataLayout, ColorDataElementType dataElementType)
 {
 }
Exemplo n.º 20
0
 void IGraphicsBackend.GetOutputPixelData <T>(T[] buffer, ColorDataLayout dataLayout, ColorDataElementType dataElementType, int x, int y, int width, int height)
 {
 }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
 void INativeTexture.GetData(IntPtr target, ColorDataLayout dataLayout, ColorDataElementType dataElementType)
 {
 }
Exemplo n.º 23
0
        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);
        }