Exemple #1
0
        public Texture2D(GraphicsDevice graphicsDevice, int width, int height, int mipMaps = 1, PixelInternalFormat internalFormat = PixelInternalFormat.Rgba8)
            : base(graphicsDevice, 1, internalFormat)
        {
            Width  = width;
            Height = height;
            Bounds = new Rectangle(0, 0, width, height);
            ThreadingHelper.BlockOnUIThread(() =>
            {
                texture = GL.GenTexture();

                Bind();
                setDefaultTextureParameters();
                bool isDepthTarget = ((int)internalFormat >= (int)PixelInternalFormat.DepthComponent16 && (int)internalFormat <= (int)PixelInternalFormat.DepthComponent32Sgix);
                if (isDepthTarget)
                {
                    GL.TexImage2D(TextureTarget.Texture2D, 0, (OpenTK.Graphics.OpenGL4.PixelInternalFormat)internalFormat, width, height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
                }
                else
                {
                    GL.TexStorage2D(TextureTarget2d.Texture2D, mipMaps, (OpenTK.Graphics.OpenGL4.SizedInternalFormat)internalFormat, width, height);
                }

                //GL.TexImage2D(TextureTarget.Texture2D, 0, (OpenTK.Graphics.OpenGL4.PixelInternalFormat)internalFormat, width, height, 0, (OpenTK.Graphics.OpenGL4.PixelFormat)Format, PixelType.UnsignedByte, IntPtr.Zero);
            });
        }
Exemple #2
0
 public Shader(ShaderType type, string source)
 {
     ThreadingHelper.BlockOnUIThread(() => {
         shader = GL.CreateShader((OpenTK.Graphics.OpenGL4.ShaderType)type);
         GL.ShaderSource(shader, source);
     });
 }
Exemple #3
0
        public void GetData <T>(int level, Nullable <Rectangle> rect, T[] data, int startIndex, int elementCount) where T : struct
        {
            ThreadingHelper.BlockOnUIThread(() =>
            {
                Bind();
                if (rect.HasValue)
                {
                    //TODO: use ? GL.CopyImageSubData(
                    var temp = new T[this.Width * this.Height];
                    GL.GetTexImage(TextureTarget.Texture2D, level, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, temp);
                    int z = 0, w = 0;

                    for (int y = rect.Value.Y; y < rect.Value.Y + rect.Value.Height; ++y)
                    {
                        for (int x = rect.Value.X; x < rect.Value.X + rect.Value.Width; ++x)
                        {
                            data[z * rect.Value.Width + w] = temp[(y * Width) + x];
                            ++w;
                        }
                        ++z;
                        w = 0;
                    }
                }
                else
                {
                    GL.GetTexImage(TextureTarget.Texture2D, level, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, data);
                }
            });
        }
Exemple #4
0
        public void Resize(int vertexCount, bool keepData = false)
        {
            int tempVBO = 0;

            ThreadingHelper.BlockOnUIThread(() =>
            {
                GL.BindVertexArray(0);
                tempVBO = GL.GenBuffer();
                GL.BindBuffer(BufferTarget.ArrayBuffer, tempVBO);
                GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertexCount * VertexDeclaration.VertexStride), IntPtr.Zero, (OpenTK.Graphics.OpenGL4.BufferUsageHint)BufferUsage);
                GraphicsDevice.CheckError();
            });

            ThreadingHelper.BlockOnUIThread(() =>
            {
                this.VertexCount = vertexCount;
                GL.DeleteBuffer(vbo);
                vbo = tempVBO;
                GraphicsDevice.CheckError();
            });    /*
                    * ThreadingHelper.BlockOnUIThread(() =>
                    * {
                    *
                    * }, true);*/
            if (keepData)
            {
                //TODO:
                throw new NotImplementedException();
            }
        }
Exemple #5
0
        public EffectParameterCollection(EffectTechniqueCollection techniques)
        {
            ThreadingHelper.BlockOnUIThread(() => {
                parameters    = new Dictionary <string, EffectParameter> ();
                parameterList = new List <EffectParameter> ();

                foreach (EffectTechnique technique in techniques)
                {
                    foreach (EffectPass pass in technique.Passes)
                    {
                        pass.CacheParameters();

                        foreach (EffectPassParameter param in pass.Parameters)
                        {
                            EffectParameter current = null;
                            if (!parameters.TryGetValue(param.Name, out current))
                            {
                                current = new EffectParameter(param.Name);
                                Add(current);
                            }
                            current.Add(param);
                        }
                    }
                }
            });
        }
Exemple #6
0
        public VertexBuffer(GraphicsDevice graphicsDevice, Type vertexType, int vertexCount, BufferUsageHint usage = BufferUsageHint.StaticDraw)
            : this(graphicsDevice, vertexCount, usage)
        {
            IVertexType tp = Activator.CreateInstance(vertexType) as IVertexType;

            if (tp == null)
            {
                throw new ArgumentException("must be a vertexType");
            }

            this.VertexDeclaration = tp.VertexDeclaration;
            ThreadingHelper.BlockOnUIThread(() =>
            {
                vbo = GL.GenBuffer();
                GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
                GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertexCount * VertexDeclaration.VertexStride), IntPtr.Zero, (OpenTK.Graphics.OpenGL4.BufferUsageHint)BufferUsage);
            });
            ThreadingHelper.BlockOnUIThread(() =>
            {
                vao     = new VertexAttributes();
                vao.vbo = vbo;
                vao.Bind();
                GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
                VertexAttributes.ApplyAttributes(vao, VertexDeclaration);

                GL.BindVertexArray(0);
            }, true);
            GraphicsDevice.CheckError();
        }
        public void DrawUserPrimitives <T>(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int primitiveCount, VertexDeclaration vertexDeclaration)  where T : struct
        {
            VertexBuffer old = VertexBuffer;

            ThreadingHelper.BlockOnUIThread(() =>
            {
                VertexBuffer current;
                if (!userBuffers.TryGetValue(vertexDeclaration, out current))
                {
                    current = new VertexBuffer(this, vertexDeclaration, vertexData.Length);
                    userBuffers.Add(vertexDeclaration, current);
                }
                else if (current.VertexCount < vertexData.Length)
                {
                    if (current != null && !current.IsDisposed)
                    {
                        current.Dispose();
                    }
                    current = new VertexBuffer(this, vertexDeclaration, vertexData.Length);
                    userBuffers[vertexDeclaration] = current;
                }

                current.SetData <T>(vertexData);

                this.VertexBuffer = current;

                DrawPrimitives(primitiveType, vertexOffset, primitiveCount);
            });
            this.VertexBuffer = old;
            CheckError();
        }
        public void DrawUserIndexedPrimitives <T>(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, short[] indexData, int indexOffset, int primitiveCount)
        {
            return;

            IVertexType tp = Activator.CreateInstance <T>() as IVertexType;

            if (tp == null)
            {
                throw new ArgumentException("must be a vertexType");
            }
            VertexBuffer old = VertexBuffer;

            ThreadingHelper.BlockOnUIThread(() =>
            {
                VertexBuffer current = new VertexBuffer(this, tp.VertexDeclaration, vertexData.Length);

                this.VertexBuffer = current;

                VertexBuffer.vao.Bind();
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

                GL.DrawElements((OpenTK.Graphics.OpenGL4.PrimitiveType)primitiveType, primitiveCount * 3, OpenTK.Graphics.OpenGL4.DrawElementsType.UnsignedShort, indexData);

                this.VertexBuffer = old;

                current.Dispose();
            });
            CheckError();
        }
Exemple #9
0
        public void GetData <T>(T[] data) where T : struct//ValueType
        {
            ThreadingHelper.BlockOnUIThread(() =>
            {
                Bind();

                /*if (glFormat == All.CompressedTextureFormats) {
                 *          throw new NotImplementedException ();
                 *  } else {*/
                int level      = 0;
                Rectangle?rect = new Rectangle?();     //new Rectangle? (new Rectangle (0, 0, Width, Height));
                if (rect.HasValue)
                {
                    var temp = new T[this.Width * this.Height];
                    GL.GetTexImage(TextureTarget.Texture2D, level, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, temp);
                    int z = 0, w = 0;

                    for (int y = rect.Value.Y; y < rect.Value.Y + rect.Value.Height; ++y)
                    {
                        for (int x = rect.Value.X; x < rect.Value.X + rect.Value.Width; ++x)
                        {
                            data[z * rect.Value.Width + w] = temp[(y * Width) + x];
                            ++w;
                        }
                        ++z;
                        w = 0;
                    }
                }
                else
                {
                    GL.GetTexImage(TextureTarget.Texture2D, level, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, data);
                }
            });
            //}
        }
        private void createTexture(bool generateMipMaps, int mipMapCount, IntPtr inputData, int width, int height, TextureContentFormat inputFormat, TextureContentFormat outputFormat)
        {
            Width   = width;
            Height  = height;
            Format  = outputFormat;
            MipMaps = new List <TextureContentMipMap>();
            bool hwCompressedInput  = inputFormat == TextureContentFormat.DXT1 || inputFormat == TextureContentFormat.DXT3 || inputFormat == TextureContentFormat.DXT5;
            bool hwCompressedOutput = outputFormat == TextureContentFormat.DXT1 || outputFormat == TextureContentFormat.DXT3 || outputFormat == TextureContentFormat.DXT5;

            ThreadingHelper.BlockOnUIThread(() =>
            {
                texture = GL.GenTexture();

                GL.BindTexture(TextureTarget.Texture2D, texture);
                setDefaultTextureParameters();

                //GL.TexStorage2D(TextureTarget2d.Texture2D,(GenerateMipMaps ? 1 : MipMapCount),SizedInternalFormat.Rgba8,width,height);
                //GL.TexSubImage2D(TextureTarget.Texture2D,0,0,0,width,height,
                GL.TexImage2D(TextureTarget.Texture2D, 0, (hwCompressedOutput ? (OpenTK.Graphics.OpenGL4.PixelInternalFormat)outputFormat : OpenTK.Graphics.OpenGL4.PixelInternalFormat.Rgba), width, height, 0, (hwCompressedInput ? (OpenTK.Graphics.OpenGL4.PixelFormat)inputFormat : OpenTK.Graphics.OpenGL4.PixelFormat.Bgra), PixelType.UnsignedByte, inputData);
                if (!generateMipMaps)
                {
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, mipMapCount);
                    GL.Hint(HintTarget.GenerateMipmapHint, HintMode.Nicest);
                    GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                }
            });

            PreprocessMipMaps();

            ThreadingHelper.BlockOnUIThread(() =>
            {
                GL.DeleteTexture(texture);
            });
        }
 public void Clear(ClearBufferMask mask, System.Drawing.Color color)
 {
     ThreadingHelper.BlockOnUIThread(() =>
     {
         GL.Clear((OpenTK.Graphics.OpenGL4.ClearBufferMask)mask);
         GL.ClearColor(color);
     });
 }
Exemple #12
0
 internal EffectPass(string name)//TODO: content loading
 {
     this.Name = name;
     ThreadingHelper.BlockOnUIThread(() =>
     {
         program = GL.CreateProgram();
     });
 }
Exemple #13
0
 public override void Dispose()
 {
     ThreadingHelper.BlockOnUIThread(() =>
     {
         GL.DeleteFramebuffer(fbo);
     });
     base.Dispose();
 }
 public void Clear(Color color)
 {
     ThreadingHelper.BlockOnUIThread(() =>
     {
         GL.Clear(OpenTK.Graphics.OpenGL4.ClearBufferMask.ColorBufferBit | OpenTK.Graphics.OpenGL4.ClearBufferMask.DepthBufferBit);
         GL.ClearColor(color.R, color.G, color.B, color.A);
     });
     CheckError();
 }
Exemple #15
0
        public override void Dispose()
        {
            ThreadingHelper.BlockOnUIThread(() =>
            {
                GL.DeleteTexture(texture);
            });

            base.Dispose();
        }
Exemple #16
0
 public override void Dispose()
 {
     ThreadingHelper.BlockOnUIThread(() =>
     {
         GL.DeleteBuffer(vbo);
     });
     vao.Dispose();
     base.Dispose();
 }
Exemple #17
0
 internal override void SetSampler(SamplerState state)
 {
     ThreadingHelper.BlockOnUIThread(() =>
     {
         state = state == null ? SamplerState.LinearClamp : state;
         GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)state.AddressU);
         GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)state.AddressV);
     });
 }
Exemple #18
0
 internal void AttachShaders(IEnumerable <Shader> shaders)
 {
     ThreadingHelper.BlockOnUIThread(() =>
     {
         foreach (Shader shader in shaders)
         {
             AttachShader(shader);
         }
     });
 }
        private EffectPassParameter CacheParameter(string name)
        {
            EffectPassParameter param = null;

            ThreadingHelper.BlockOnUIThread(() =>
            {
                int location = pass.GetUniformLocation(name);
                param        = new EffectPassParameter(pass, name, location);
            });
            return(param);
        }
Exemple #20
0
 public void Dispose()
 {
     if (!IsDisposed)
     {
         ThreadingHelper.BlockOnUIThread(() =>
         {
             GL.DeleteVertexArray(vao);
         }, true);
     }
     IsDisposed = true;
 }
Exemple #21
0
 public void SetValue(Quaternion[] values)
 {
     ThreadingHelper.BlockOnUIThread(() =>
     {
         foreach (var param in parameters)
         {
             param.pass.Apply();
             param.SetValue(values);
         }
     });
 }
Exemple #22
0
 public void SetValue(float value)
 {
     ThreadingHelper.BlockOnUIThread(() =>
     {
         foreach (var param in parameters)
         {
             param.pass.Apply();
             param.SetValue(value);
         }
     });
 }
Exemple #23
0
        public void SetData <T>(T[] data) where T : struct
        {
            ThreadingHelper.BlockOnUIThread(() =>
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);

                GCHandle buffer = GCHandle.Alloc(data, GCHandleType.Pinned);
                GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, new IntPtr(data.Length * VertexDeclaration.VertexStride), buffer.AddrOfPinnedObject());    //TODO use bufferusage
                buffer.Free();
            });
            GraphicsDevice.CheckError();
        }
Exemple #24
0
        private void createTexture(bool generateMipMaps, int mipMapCount, IntPtr inputData, int width, int height, TextureContentFormat inputFormat, TextureContentFormat outputFormat)
        {
            Width   = width;
            Height  = height;
            Format  = outputFormat;
            MipMaps = new List <TextureContentMipMap>();
            bool hwCompressedInput  = inputFormat == TextureContentFormat.DXT1 || inputFormat == TextureContentFormat.DXT3 || inputFormat == TextureContentFormat.DXT5;
            bool hwCompressedOutput = outputFormat == TextureContentFormat.DXT1 || outputFormat == TextureContentFormat.DXT3 || outputFormat == TextureContentFormat.DXT5;

            ThreadingHelper.BlockOnUIThread(() =>
            {
                texture = GL.GenTexture();

                GL.BindTexture(TextureTarget.Texture2D, texture);
                bool doGenerate = generateMipMaps && mipMapCount > 1;

                setDefaultTextureParameters();
                //GL.TexStorage2D(TextureTarget2d.Texture2D,(GenerateMipMaps ? 1 : MipMapCount),SizedInternalFormat.Rgba8,width,height);
                //GL.TexSubImage2D(TextureTarget.Texture2D,0,0,0,width,height,
                if (doGenerate)
                {
                    if (graphicsDevice.majorVersion < 3 &&
                        ((graphicsDevice.majorVersion == 1 && graphicsDevice.minorVersion >= 4) ||
                         graphicsDevice.majorVersion > 1))
                    {
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1);
                    }
                    else if (graphicsDevice.majorVersion < 3)
                    {
                        throw new NotSupportedException("Can't generate MipMaps on this Hardware");
                    }
                }
                GL.TexImage2D(TextureTarget.Texture2D, 0, (hwCompressedOutput ? (OpenTK.Graphics.OpenGL4.PixelInternalFormat)outputFormat : OpenTK.Graphics.OpenGL4.PixelInternalFormat.Rgba), width, height, 0, (hwCompressedInput ? (OpenTK.Graphics.OpenGL4.PixelFormat)inputFormat : OpenTK.Graphics.OpenGL4.PixelFormat.Bgra), PixelType.UnsignedByte, inputData);
                if (doGenerate)
                {
                    //TOODO non power of 2 Textures?
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, mipMapCount);
                    GL.Hint(HintTarget.GenerateMipmapHint, HintMode.Nicest);
                    if (graphicsDevice.majorVersion >= 3)
                    {
                        GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                    }
                }
            });

            PreprocessMipMaps();

            ThreadingHelper.BlockOnUIThread(() =>
            {
                GL.DeleteTexture(texture);
            });
        }
Exemple #25
0
        private void PreprocessMipMaps()
        {
            bool hwCompressed = Format == TextureContentFormat.DXT1 || Format == TextureContentFormat.DXT3 || Format == TextureContentFormat.DXT5;
            int  width = Width, height = Height;
            int  realCount = 0;

            for (int i = 0; i < (GenerateMipMaps ? 1 : MipMapCount); i++)
            {
                if (hwCompressed)
                {
                    int    dataSize = 0;
                    byte[] data     = null;
                    ThreadingHelper.BlockOnUIThread(() =>
                    {
                        GL.BindTexture(TextureTarget.Texture2D, texture);
                        GL.GetTexLevelParameter(TextureTarget.Texture2D, i, GetTextureParameter.TextureCompressedImageSize, out dataSize);
                        data = new byte[dataSize];
                        GL.GetCompressedTexImage(TextureTarget.Texture2D, i, data);
                    });
                    MipMaps.Add(new TextureContentMipMap(width, height, Format, data));
                }
                else
                {
                    var bmp = new System.Drawing.Bitmap(width, height);

                    var bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                    ThreadingHelper.BlockOnUIThread(() =>
                    {
                        GL.BindTexture(TextureTarget.Texture2D, texture);
                        GL.GetTexImage(TextureTarget.Texture2D, i, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0);
                    });

                    bmp.UnlockBits(bmpData);

                    MipMaps.Add(new TextureContentMipMap(width, height, Format, bmp));
                }
                width  /= 2;
                height /= 2;
                realCount++;
                if (width == 0 || height == 0)
                {
                    break;
                }
            }
            if (!GenerateMipMaps)
            {
                MipMapCount = realCount;
            }
        }
Exemple #26
0
        public void SetData <T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct
        {
            ThreadingHelper.BlockOnUIThread(() =>
            {
                //vao.Bind();//TODO: verify
                GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);

                GCHandle buffer = GCHandle.Alloc(data, GCHandleType.Pinned);
                GL.BufferSubData(BufferTarget.ArrayBuffer, new IntPtr(offsetInBytes), new IntPtr(elementCount * vertexStride), buffer.AddrOfPinnedObject() + startIndex * vertexStride);

                buffer.Free();
            });
            GraphicsDevice.CheckError();
        }
Exemple #27
0
        public Texture2DArray(GraphicsDevice graphicsDevice, int levels, int width, int height, int layers)
            : base(graphicsDevice)
        {
            ThreadingHelper.BlockOnUIThread(() => {
                texture = GL.GenTexture();

                GL.BindTexture(TextureTarget.Texture2DArray, texture);

                GL.TexStorage3D(TextureTarget3d.Texture2DArray, levels, SizedInternalFormat.Rgba8, width, height, Math.Max(layers, 1));
            });
            Width      = width;
            Height     = height;
            LayerCount = layers;
        }
Exemple #28
0
        internal void Compile()
        {
            ThreadingHelper.BlockOnUIThread(() => {
                GL.CompileShader(shader);

                int compiled;
                GL.GetShader(shader, ShaderParameter.CompileStatus, out compiled);
                if (compiled != 1)
                {
                    string error = GL.GetShaderInfoLog(shader);
                    throw new Exception(error);
                }
            });
        }
Exemple #29
0
 public IndexBuffer(GraphicsDevice graphicsDevice, DrawElementsType indexElementSize, int indexCount, BufferUsageHint usage = BufferUsageHint.StaticDraw)
     : base(graphicsDevice)
 {
     this.IndexElementSize = indexElementSize;
     this.IndexCount       = indexCount;
     this.BufferUsage      = usage;
     ThreadingHelper.BlockOnUIThread(() =>
     {
         ibo = GL.GenBuffer();
         GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo);
         elementSize = (indexElementSize == DrawElementsType.UnsignedByte ? 1 : (indexElementSize == DrawElementsType.UnsignedShort ? 2 : 4));
         GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(indexCount * elementSize), IntPtr.Zero, (OpenTK.Graphics.OpenGL4.BufferUsageHint)BufferUsage);
     });
     GraphicsDevice.CheckError();
     //buffer = new byte[indexCount * (int)IndexElementSize / 8];
 }
Exemple #30
0
        public void SetData <T>(T[] data) where T : struct
        {
            if (data.Length == 0)
            {
                return;
            }



            ThreadingHelper.BlockOnUIThread(() =>
            {
                Bind();
                GL.BufferSubData <T>(BufferTarget.ElementArrayBuffer, IntPtr.Zero, new IntPtr(data.Length * Marshal.SizeOf(default(T))), data);   //TODO:
            });
            GraphicsDevice.CheckError();
            //Buffer.BlockCopy (data, 0, buffer, 0, data.Length);
        }