sanaz.dadkhah's picture

PBO in opentk

hi im new to openTK and hoped that i can use PBO in OpenTk.
i read this article i try to write the similar code in opentk.my code is

  class Program1 : GameWindow
    {
        const int SCREEN_WIDTH = 400;
        const int SCREEN_HEIGHT = 300;
        const int IMAGE_WIDTH = 1024;
        const int IMAGE_HEIGHT = 1024;
        const int CHANNEL_COUNT = 4;
        const int DATA_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT * CHANNEL_COUNT;
        uint pboIds;         uint textureId;
        unsafe byte* imageData;
        int screenWidth;        int screenHeight;        bool pboSupported;        int pboMode;        int drawMode = 0;
        Timer timer, t1, t2;        float copyTime, updateTime;
        Bitmap bitmap = new Bitmap("CA-wp2.jpg");
        private bool initSharedMem()
        {
            screenWidth = SCREEN_WIDTH;            screenHeight = SCREEN_HEIGHT;            drawMode = 0; // 0:fill, 1: wireframe, 2:points
 
            // allocate texture buffer
            unsafe
            {
                imageData = (byte*)Unmanaged.NewAndInit<byte>(DATA_SIZE);
            }
            return true;
        }
        protected override void OnUnload(EventArgs e)
        {
            unsafe
            {
                Unmanaged.Free(imageData);
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            initSharedMem();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
            GL.ShadeModel(ShadingModel.Flat);
            GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
            GL.Enable(EnableCap.DepthTest);
            GL.Disable(EnableCap.Lighting);
            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.CullFace);
            GL.ColorMaterial(MaterialFace.FrontAndBack,ColorMaterialParameter.AmbientAndDiffuse);
            GL.Enable(EnableCap.ColorMaterial);
 
            GL.ClearColor(0, 0, 0, 0);                   // background color
            GL.ClearStencil(0);                          // clear stencil buffer
            GL.ClearDepth(1.0f);                         // 0 is near, 1 is far
            GL.DepthFunc(DepthFunction.Lequal);
 
 
            GL.GenTextures(1,out textureId);
            GL.BindTexture(TextureTarget.Texture2D,textureId);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,(int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,(int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS,(int)TextureWrapMode.Clamp);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT,(int)TextureWrapMode.Clamp);
            System.Drawing.Imaging.BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            //GL.TexImage2D(TextureTarget.Texture2D,0,PixelInternalFormat.Rgba8,IMAGE_WIDTH,IMAGE_HEIGHT,0,PixelFormat.Bgra,PixelType.UnsignedByte,data.Scan0);
            bitmap.UnlockBits(data);
            GL.BindTexture(TextureTarget.Texture2D,0);
 
            pboSupported = true;
            pboMode = 1;
 
            if (pboSupported)
            {
                GL.GenBuffers(1,out pboIds);
                GL.BindBuffer(BufferTarget.PixelUnpackBuffer,pboIds);
                GL.BufferData(BufferTarget.PixelUnpackBuffer, (IntPtr)(1024 * 1024 * 4),IntPtr.Zero , BufferUsageHint.StreamDraw);
            }
            IntPtr ptr = GL.MapBuffer(BufferTarget.PixelUnpackBuffer, BufferAccess.WriteOnly);
            unsafe
            {
                byte* VideoMemory = (byte*)ptr.ToPointer();
                byte* a = (byte*)ptr.ToPointer();
                for (int i = 0; i < 1024 * 1024; i++)
                {
                    VideoMemory[i * 4] = 125;
                    VideoMemory[i * 4 + 1] = 238;
                    VideoMemory[i * 4 + 2] = 84;
                    VideoMemory[i * 4 + 3] = 65;
                }
            }
            GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
 
        }
 
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.BindTexture(TextureTarget.Texture2D, textureId);
 
            GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
 
            GL.Begin(BeginMode.Quads);
 
            GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-0.6f, -0.4f);
            GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(0.6f, -0.4f);
            GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(0.6f, 0.4f);
            GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-0.6f, 0.4f);
 
            GL.End();
 
 
            SwapBuffers();
        }
 
        public static void Main(string[] args)
        {
            using (Program1 p = new Program1())
            {
                p.Run(60);
            }
        }
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            if (Keyboard[Key.Escape])
                Exit();
        }
        static unsafe class Unmanaged
        {
            public static void* New<T>(int elementCount)
                where T : struct
            {
                return Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)) *
                            elementCount).ToPointer();
            }
 
            public static void* NewAndInit<T>(int elementCount)
                where T : struct
            {
                int newSizeInBytes = Marshal.SizeOf(typeof(T)) * elementCount;
                byte* newArrayPointer =
                (byte*)Marshal.AllocHGlobal(newSizeInBytes).ToPointer();
 
                for (int i = 0; i < newSizeInBytes; i++)
                    *(newArrayPointer + i) = 0;
 
                return (void*)newArrayPointer;
            }
 
            public static void Free(void* pointerToUnmanagedMemory)
            {
                Marshal.FreeHGlobal(new IntPtr(pointerToUnmanagedMemory));
            }
 
            public static void* Resize<T>(void* oldPointer, int newElementCount)
                where T : struct
            {
                return (Marshal.ReAllocHGlobal(new IntPtr(oldPointer),
                    new IntPtr(Marshal.SizeOf(typeof(T)) * newElementCount))).ToPointer();
            }
        }
 
    }

my problem i gen PixelUnpackBuffer but i can not create texture by TexSubImage2D
please help me