Пример #1
0
        public override async Task <GLTexture> ReadTextureAsync(CancellationToken cancelationToken)
        {
            if (cancelationToken.IsCancellationRequested)
            {
                return(RaiseTextureReaded(null));
            }

            BitmapData bmdata = _bitmap.LockBits(new Rectangle(0, 0, _width, _height), ImageLockMode.ReadOnly, _format);

            using (DisposableAction unlocker = new DisposableAction(() => _bitmap.UnlockBits(bmdata)))
            {
                GLTexture texture;
                using (GLService.AcquireContext())
                    texture = new GLTexture(GL.GenTexture(), _width, _height, _format);

                using (DisposableAction insurance = new DisposableAction(texture.Dispose))
                {
                    if (cancelationToken.IsCancellationRequested)
                    {
                        return(RaiseTextureReaded(null));
                    }

                    using (GLService.AcquireContext())
                    {
                        GL.BindTexture(TextureTarget.Texture2D, texture.Id);
                        GL.TexImage2D(TextureTarget.Texture2D, 0, _format, bmdata.Width, bmdata.Height, 0, _format, _format, bmdata.Scan0);
                    }

                    //ErrorCode error = GL.GetError();
                    //if (error != ErrorCode.NoError)
                    //    throw new ArgumentException("Error building TexImage. GL Error: " + error);

                    _bitmap.UnlockBits(bmdata);
                    unlocker.Cancel();

                    if (cancelationToken.IsCancellationRequested)
                    {
                        return(RaiseTextureReaded(null));
                    }

                    using (GLService.AcquireContext())
                    {
                        GL.BindTexture(TextureTarget.Texture2D, texture.Id);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
                    }

                    insurance.Cancel();
                }
                return(RaiseTextureReaded(texture));
            }
        }
Пример #2
0
        public byte[] GetManagedPixelsArray(PixelFormatDescriptor format)
        {
            byte[] result = new byte[Width * Height * format.BytesPerPixel];

            using (GLService.AcquireContext())
            {
                GL.BindTexture(TextureTarget.Texture2D, Id);
                GL.GetTexImage(TextureTarget.Texture2D, 0, format, format, result);
                return(result);
            }
        }
Пример #3
0
        public static void SetText(string[] pages)
        {
            lock (PagesLock)
            {
                _pages = pages;
                int pagesCont = Math.Max(1, _pages == null ? 0 : _pages.Length);
                GLService.SetViewportDesiredHeight(pagesCont * GLLocationPageRenderer.ScreenHeight);
            }

            DrawEvent.Set();
            //GLService.InvalidateViewports();
        }
Пример #4
0
        public SafeHGlobalHandle GetUnmanagedPixelsArray(PixelFormatDescriptor format)
        {
            SafeHGlobalHandle result = new SafeHGlobalHandle(Width * Height * format.BytesPerPixel);

            using (GLService.AcquireContext())
                using (DisposableAction insurance = new DisposableAction(result.Dispose))
                {
                    GL.BindTexture(TextureTarget.Texture2D, Id);
                    GL.GetTexImage(TextureTarget.Texture2D, 0, format, format, result.DangerousGetHandle());

                    insurance.Cancel();
                }
            return(result);
        }
Пример #5
0
 public void Write()
 {
     using (Bitmap bmp = new Bitmap(_texture.Width, _texture.Height, _texture.PixelFormat))
     {
         BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, _texture.PixelFormat);
         using (GLService.AcquireContext())
         {
             GL.BindTexture(TextureTarget.Texture2D, _texture.Id);
             GL.GetTexImage(TextureTarget.Texture2D, 0, _texture.PixelFormat, _texture.PixelFormat, data.Scan0);
         }
         bmp.UnlockBits(data);
         bmp.Save(_filePath, _imageFormat);
     }
 }
Пример #6
0
        public void Link()
        {
            using (GLService.AcquireContext())
            {
                GL.LinkProgram(Id);

                int result;
                GL.GetProgram(Id, GetProgramParameterName.LinkStatus, out result);
                if (result != 1)
                {
                    string logInfo;
                    GL.GetProgramInfoLog(Id, out logInfo);
                    throw Exceptions.CreateException("Program {0} log: {1}", Id, logInfo);
                }
            }
        }
Пример #7
0
        public GLPalettedTextureShaderProgram()
        {
            using (DisposableAction insurance = new DisposableAction(Dispose))
            {
                using (GLService.AcquireContext())
                    using (GLShader fs = GLShader.CompileShaderFromSource(FragmentShaderSource, ShaderType.FragmentShader))
                        using (GLShader vs = GLShader.CompileShaderFromSource(VertexShaderSource, ShaderType.VertexShader))
                        {
                            GL.AttachShader(Id, fs.Id);
                            GL.AttachShader(Id, vs.Id);
                            Link();
                        }

                insurance.Cancel();
            }
        }
Пример #8
0
        public static GLShader CompileShaderFromSource(string source, ShaderType type)
        {
            using (GLService.AcquireContext())
            {
                int id = GL.CreateShader(type);
                GL.ShaderSource(id, source);

                string logInfo;
                GL.CompileShader(id);
                GL.GetShaderInfoLog(id, out logInfo);

                if (logInfo.Length > 0 && !logInfo.Contains("hardware"))
                {
                    throw Exceptions.CreateException("Compile shader failed! Log:{0}", logInfo);
                }

                return(new GLShader(id, type));
            }
        }
Пример #9
0
        public IDisposable Use(GLTexture texture, GLTexture palette, int paletteIndex)
        {
            using (GLService.AcquireContext())
            {
                GL.UseProgram(Id);

                GL.Uniform1(GL.GetUniformLocation(Id, "paletteIndex"), paletteIndex);

                GL.Uniform1(GL.GetUniformLocation(Id, "palette"), 1);
                GL.ActiveTexture(TextureUnit.Texture1);
                GL.BindTexture(TextureTarget.Texture2D, palette.Id);

                GL.Uniform1(GL.GetUniformLocation(Id, "texture"), 0);
                GL.ActiveTexture(TextureUnit.Texture0);
                GL.BindTexture(TextureTarget.Texture2D, texture.Id);
            }

            return(UnuseProgramAction);
        }
        public void Write()
        {
            string prefix = Path.ChangeExtension(_generalPath, null) + "_";

            using (Bitmap bmp = new Bitmap(_texture.Width, _texture.Height, _texture.PixelFormat))
            {
                BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, _texture.PixelFormat);
                using (GLService.AcquireContext())
                {
                    GL.BindTexture(TextureTarget.Texture2D, _texture.Id);
                    GL.GetTexImage(TextureTarget.Texture2D, 0, _texture.PixelFormat, _texture.PixelFormat, data.Scan0);
                }
                bmp.UnlockBits(data);

                using (UnmanagedMemoryStream input = data.Scan0.OpenStream(data.Stride * data.Height, FileAccess.Read))
                {
                    for (int i = 0; i < _texture.Height; i++)
                    {
                        string filePath = prefix + i.ToString("D2") + ".act";
                        Write(input, filePath);
                    }
                }
            }
        }
Пример #11
0
 public void Dispose()
 {
     using (GLService.AcquireContext())
         GL.DeleteTexture(Id);
 }
Пример #12
0
 public void Dispose()
 {
     using (GLService.AcquireContext())
         GL.DeleteProgram(Id);
 }
Пример #13
0
 public GLShaderProgram()
 {
     using (GLService.AcquireContext())
         Id = GL.CreateProgram();
 }
Пример #14
0
 public void Dispose()
 {
     using (GLService.AcquireContext())
         GL.DeleteFramebuffer(Id);
 }
Пример #15
0
        public static GLTexture HorizontalJoin(GLTexture left, GLTexture right)
        {
            Exceptions.CheckArgumentNull(left, "left");
            Exceptions.CheckArgumentNull(right, "right");

            if (left.Height != right.Height)
            {
                throw new ArgumentException("height");
            }
            if (!left.PixelFormat.Equals(right.PixelFormat))
            {
                throw new ArgumentException("pixelFormat");
            }

            int height = left.Height;
            PixelFormatDescriptor format = left.PixelFormat;

            GLTexture texture = null;

            try
            {
                using (GLService.AcquireContext())
                {
                    texture = new GLTexture(GL.GenTexture(), left.Width + right.Width, height, format);
                    GL.BindTexture(TextureTarget.Texture2D, texture.Id);
                    GL.TexImage2D(TextureTarget.Texture2D, 0, format, texture.Width, texture.Height, 0, format, format, IntPtr.Zero);

                    if (GLService.CheckVersion(4, 3))
                    {
                        GL.CopyImageSubData(left.Id, ImageTarget.Texture2D, 0, 0, 0, 0, texture.Id, ImageTarget.Texture2D, 0, 0, 0, 0, left.Width, left.Height, 1);
                        GL.CopyImageSubData(right.Id, ImageTarget.Texture2D, 0, 0, 0, 0, texture.Id, ImageTarget.Texture2D, 0, left.Width, 0, 0, right.Width, right.Height, 1);
                    }
                    else
                    {
                        using (GLFramebuffer framebuffer = GLFramebuffer.Create())
                        {
                            GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer.Id);
                            GL.FramebufferTexture2D(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment1, TextureTarget.Texture2D, texture.Id, 0);

                            GL.FramebufferTexture2D(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, left.Id, 0);
                            GL.DrawBuffer(DrawBufferMode.ColorAttachment1);
                            GL.BlitFramebuffer(0, 0, left.Width, height, 0, 0, left.Width, height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear);

                            GL.FramebufferTexture2D(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, right.Id, 0);
                            GL.DrawBuffer(DrawBufferMode.ColorAttachment1);
                            GL.BlitFramebuffer(0, 0, left.Width, height, left.Width, 0, left.Width * 2, height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear);
                        }
                    }

                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
                }
            }
            catch
            {
                texture.SafeDispose();
                throw;
            }

            return(texture);
        }
Пример #16
0
        public static void HorizontalSplit(GLTexture layer, out GLTexture leftTexture, out GLTexture rightTexture)
        {
            Exceptions.CheckArgumentNull(layer, "layer");
            if (layer.Width % 2 != 0)
            {
                throw new ArgumentException("layer.Width");
            }

            int width  = layer.Width / 2;
            int height = layer.Height;
            PixelFormatDescriptor format = layer.PixelFormat;

            leftTexture  = null;
            rightTexture = null;

            try
            {
                using (GLService.AcquireContext())
                {
                    leftTexture  = new GLTexture(GL.GenTexture(), width, height, format);
                    rightTexture = new GLTexture(GL.GenTexture(), width, height, format);

                    foreach (GLTexture texture in new[] { leftTexture, rightTexture })
                    {
                        GL.BindTexture(TextureTarget.Texture2D, texture.Id);
                        GL.TexImage2D(TextureTarget.Texture2D, 0, format, width, height, 0, format, format, IntPtr.Zero);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
                    }

                    if (GLService.CheckVersion(4, 3))
                    {
                        GL.CopyImageSubData(layer.Id, ImageTarget.Texture2D, 0, 0, 0, 0, leftTexture.Id, ImageTarget.Texture2D, 0, 0, 0, 0, width, height, 1);
                        GL.CopyImageSubData(layer.Id, ImageTarget.Texture2D, 0, width, 0, 0, rightTexture.Id, ImageTarget.Texture2D, 0, 0, 0, 0, width, height, 1);
                    }
                    else
                    {
                        using (GLFramebuffer framebuffer = GLFramebuffer.Create())
                        {
                            GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer.Id);
                            GL.FramebufferTexture2D(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, layer.Id, 0);

                            GL.FramebufferTexture2D(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment1, TextureTarget.Texture2D, leftTexture.Id, 0);
                            GL.DrawBuffer(DrawBufferMode.ColorAttachment1);
                            GL.BlitFramebuffer(0, 0, width, height, 0, 0, width, height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear);

                            GL.FramebufferTexture2D(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment1, TextureTarget.Texture2D, rightTexture.Id, 0);
                            GL.DrawBuffer(DrawBufferMode.ColorAttachment1);
                            GL.BlitFramebuffer(width, 0, width * 2, height, 0, 0, width, height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear);
                        }
                    }
                }
            }
            catch
            {
                leftTexture.SafeDispose();
                rightTexture.SafeDispose();
                throw;
            }
        }