Ejemplo n.º 1
0
        protected IndexBuffer(
            GraphicsDevice graphicsDevice,
            IndexElementSize indexElementSize,
            int indexCount,
            BufferUsage usage,
            bool dynamic
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("GraphicsDevice is null");
            }

            GraphicsDevice   = graphicsDevice;
            IndexElementSize = indexElementSize;
            IndexCount       = indexCount;
            BufferUsage      = usage;

            INTERNAL_isDynamic = dynamic;

            Threading.ForceToMainThread(() =>
            {
                Handle = GL.GenBuffer();

                OpenGLDevice.Instance.BindIndexBuffer(Handle);
                GL.BufferData(
                    BufferTarget.ElementArrayBuffer,
                    (IntPtr)(IndexCount * (IndexElementSize == IndexElementSize.SixteenBits ? 2 : 4)),
                    IntPtr.Zero,
                    INTERNAL_isDynamic ? BufferUsageHint.StreamDraw : BufferUsageHint.StaticDraw
                    );
            });
        }
Ejemplo n.º 2
0
        protected void SetDataInternal <T>(
            int offsetInBytes,
            T[] data,
            int startIndex,
            int elementCount,
            SetDataOptions options
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data is null");
            }
            if (data.Length < (startIndex + elementCount))
            {
                throw new InvalidOperationException("The array specified in the data parameter is not the correct size for the amount of data requested.");
            }

            Threading.ForceToMainThread(() =>
                                        OpenGLDevice.Instance.SetIndexBufferData(
                                            Handle,
                                            offsetInBytes,
                                            data,
                                            startIndex,
                                            elementCount,
                                            options
                                            )
                                        );
        }
Ejemplo n.º 3
0
        public void GetData <T>(
            int offsetInBytes,
            T[] data,
            int startIndex,
            int elementCount
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data is null");
            }
            if (data.Length < (startIndex + elementCount))
            {
                throw new InvalidOperationException("The array specified in the data parameter is not the correct size for the amount of data requested.");
            }
            if (BufferUsage == BufferUsage.WriteOnly)
            {
                throw new NotSupportedException(
                          "This IndexBuffer was created with a usage type of BufferUsage.WriteOnly. " +
                          "Calling GetData on a resource that was created with BufferUsage.WriteOnly is not supported."
                          );
            }

            Threading.ForceToMainThread(() =>
                                        OpenGLDevice.Instance.GetIndexBufferData(
                                            Handle,
                                            offsetInBytes,
                                            data,
                                            startIndex,
                                            elementCount
                                            )
                                        );
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RenderTargetCube"/> class.
        /// </summary>
        /// <param name="graphicsDevice">The graphics device.</param>
        /// <param name="size">The width and height of a texture cube face in pixels.</param>
        /// <param name="mipMap">
        /// <see langword="true"/> to generate a full mipmap chain; otherwise <see langword="false"/>.
        /// </param>
        /// <param name="preferredFormat">The preferred format of the surface.</param>
        /// <param name="preferredDepthFormat">The preferred format of the depth-stencil buffer.</param>
        /// <param name="preferredMultiSampleCount">The preferred number of multisample locations.</param>
        /// <param name="usage">The usage mode of the render target.</param>
        public RenderTargetCube(
            GraphicsDevice graphicsDevice,
            int size,
            bool mipMap,
            SurfaceFormat preferredFormat,
            DepthFormat preferredDepthFormat,
            int preferredMultiSampleCount,
            RenderTargetUsage usage
            ) : base(
                graphicsDevice,
                size,
                mipMap,
                preferredFormat
                )
        {
            DepthStencilFormat = preferredDepthFormat;
            MultiSampleCount   = preferredMultiSampleCount;
            RenderTargetUsage  = usage;

            // If we don't need a depth buffer then we're done.
            if (preferredDepthFormat == DepthFormat.None)
            {
                return;
            }

            Threading.ForceToMainThread(() =>
            {
                glDepthStencilBuffer = graphicsDevice.GLDevice.GenRenderbuffer(
                    size,
                    size,
                    preferredDepthFormat
                    );
            });
        }
Ejemplo n.º 5
0
        protected VertexBuffer(
            GraphicsDevice graphicsDevice,
            VertexDeclaration vertexDeclaration,
            int vertexCount,
            BufferUsage bufferUsage,
            bool dynamic
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("GraphicsDevice cannot be null");
            }

            GraphicsDevice    = graphicsDevice;
            VertexDeclaration = vertexDeclaration;
            VertexCount       = vertexCount;
            BufferUsage       = bufferUsage;

            // Make sure the graphics device is assigned in the vertex declaration.
            if (vertexDeclaration.GraphicsDevice != graphicsDevice)
            {
                vertexDeclaration.GraphicsDevice = graphicsDevice;
            }

            Threading.ForceToMainThread(() =>
            {
                Handle = new OpenGLDevice.OpenGLVertexBuffer(
                    GraphicsDevice,
                    dynamic,
                    VertexCount,
                    VertexDeclaration.VertexStride
                    );
            });
        }
Ejemplo n.º 6
0
        protected IndexBuffer(
            GraphicsDevice graphicsDevice,
            IndexElementSize indexElementSize,
            int indexCount,
            BufferUsage usage,
            bool dynamic
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("GraphicsDevice is null");
            }

            GraphicsDevice   = graphicsDevice;
            IndexElementSize = indexElementSize;
            IndexCount       = indexCount;
            BufferUsage      = usage;

            Threading.ForceToMainThread(() =>
            {
                Handle = new OpenGLDevice.OpenGLIndexBuffer(
                    dynamic,
                    IndexCount,
                    IndexElementSize
                    );
            });
        }
Ejemplo n.º 7
0
        public Texture3D(
            GraphicsDevice graphicsDevice,
            int width,
            int height,
            int depth,
            bool mipMap,
            SurfaceFormat format
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice");
            }

            GraphicsDevice = graphicsDevice;
            Width          = width;
            Height         = height;
            Depth          = depth;
            LevelCount     = mipMap ? CalculateMipLevels(width, height) : 1;

            Format = format;
            GetGLSurfaceFormat();

            Threading.ForceToMainThread(() =>
            {
                texture = GraphicsDevice.GLDevice.CreateTexture(
                    typeof(Texture3D),
                    Format,
                    mipMap
                    );

                for (int i = 0; i < LevelCount; i += 1)
                {
                    GraphicsDevice.GLDevice.glTexImage3D(
                        OpenGLDevice.GLenum.GL_TEXTURE_3D,
                        i,
                        (int)glInternalFormat,
                        Math.Max(width >> i, 1),
                        Math.Max(height >> i, 1),
                        depth,
                        0,
                        glFormat,
                        glType,
                        IntPtr.Zero
                        );
                }
            });
        }
Ejemplo n.º 8
0
        protected void SetDataInternal <T>(
            int offsetInBytes,
            T[] data,
            int startIndex,
            int elementCount,
            int vertexStride,
            SetDataOptions options
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data is null");
            }
            if (data.Length < (startIndex + elementCount))
            {
                throw new InvalidOperationException("The array specified in the data parameter is not the correct size for the amount of data requested.");
            }

            int bufferSize = VertexCount * VertexDeclaration.VertexStride;

            if (vertexStride > bufferSize ||
                vertexStride < VertexDeclaration.VertexStride)
            {
                throw new ArgumentOutOfRangeException(
                          "One of the following conditions is true:\n" +
                          "The vertex stride is larger than the vertex buffer.\n" +
                          "The vertex stride is too small for the type of data requested."
                          );
            }

            int elementSizeInBytes = Marshal.SizeOf(typeof(T));

            Threading.ForceToMainThread(() =>
                                        GraphicsDevice.GLDevice.SetVertexBufferData(
                                            Handle,
                                            bufferSize,
                                            elementSizeInBytes,
                                            offsetInBytes,
                                            data,
                                            startIndex,
                                            elementCount,
                                            vertexStride,
                                            options
                                            )
                                        );
        }
Ejemplo n.º 9
0
        public void SetData <T>(
            int level,
            int left,
            int top,
            int right,
            int bottom,
            int front,
            int back,
            T[] data,
            int startIndex,
            int elementCount
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            Threading.ForceToMainThread(() =>
            {
                GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
                try
                {
                    GraphicsDevice.GLDevice.BindTexture(texture);
                    GraphicsDevice.GLDevice.glTexSubImage3D(
                        OpenGLDevice.GLenum.GL_TEXTURE_3D,
                        level,
                        left,
                        top,
                        front,
                        right - left,
                        bottom - top,
                        back - front,
                        glFormat,
                        glType,
                        (IntPtr)(dataHandle.AddrOfPinnedObject().ToInt64() + startIndex * Marshal.SizeOf(typeof(T)))
                        );
                }
                finally
                {
                    dataHandle.Free();
                }
            });
        }
Ejemplo n.º 10
0
        public void GetData <T>(
            int offsetInBytes,
            T[] data,
            int startIndex,
            int elementCount,
            int vertexStride
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException(
                          "data",
                          "This method does not accept null for this parameter."
                          );
            }
            if (data.Length < (startIndex + elementCount))
            {
                throw new ArgumentOutOfRangeException(
                          "elementCount",
                          "This parameter must be a valid index within the array."
                          );
            }
            if (BufferUsage == BufferUsage.WriteOnly)
            {
                throw new NotSupportedException("Calling GetData on a resource that was created with BufferUsage.WriteOnly is not supported.");
            }
            if ((elementCount * vertexStride) > (VertexCount * VertexDeclaration.VertexStride))
            {
                throw new InvalidOperationException("The array is not the correct size for the amount of data requested.");
            }

            Threading.ForceToMainThread(() =>
                                        GraphicsDevice.GLDevice.GetVertexBufferData(
                                            Handle,
                                            offsetInBytes,
                                            data,
                                            startIndex,
                                            elementCount,
                                            vertexStride
                                            )
                                        );
        }
Ejemplo n.º 11
0
        protected VertexBuffer(
            GraphicsDevice graphicsDevice,
            VertexDeclaration vertexDeclaration,
            int vertexCount,
            BufferUsage bufferUsage,
            bool dynamic
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("GraphicsDevice cannot be null");
            }

            GraphicsDevice    = graphicsDevice;
            VertexDeclaration = vertexDeclaration;
            VertexCount       = vertexCount;
            BufferUsage       = bufferUsage;

            // Make sure the graphics device is assigned in the vertex declaration.
            if (vertexDeclaration.GraphicsDevice != graphicsDevice)
            {
                vertexDeclaration.GraphicsDevice = graphicsDevice;
            }

            INTERNAL_isDynamic = dynamic;
            Threading.ForceToMainThread(() =>
            {
                Handle = GL.GenBuffer();

                OpenGLDevice.Instance.BindVertexBuffer(Handle);
                GL.BufferData(
                    BufferTarget.ArrayBuffer,
                    new IntPtr(VertexDeclaration.VertexStride * VertexCount),
                    IntPtr.Zero,
                    INTERNAL_isDynamic ? BufferUsageHint.StreamDraw : BufferUsageHint.StaticDraw
                    );
            });
        }
Ejemplo n.º 12
0
        public void SetData <T>(
            int level,
            Rectangle?rect,
            T[] data,
            int startIndex,
            int elementCount
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            int x, y, w, h;

            if (rect.HasValue)
            {
                x = rect.Value.X;
                y = rect.Value.Y;
                w = rect.Value.Width;
                h = rect.Value.Height;
            }
            else
            {
                x = 0;
                y = 0;
                w = Math.Max(Width >> level, 1);
                h = Math.Max(Height >> level, 1);
            }

            Threading.ForceToMainThread(() =>
            {
                GCHandle dataHandle    = GCHandle.Alloc(data, GCHandleType.Pinned);
                int elementSizeInBytes = Marshal.SizeOf(typeof(T));
                int startByte          = startIndex * elementSizeInBytes;
                IntPtr dataPtr         = (IntPtr)(dataHandle.AddrOfPinnedObject().ToInt64() + startByte);

                try
                {
                    GraphicsDevice.GLDevice.BindTexture(texture);
                    if (glFormat == (PixelFormat)All.CompressedTextureFormats)
                    {
                        int dataLength;
                        if (elementCount > 0)
                        {
                            dataLength = elementCount * elementSizeInBytes;
                        }
                        else
                        {
                            dataLength = data.Length - startByte;
                        }

                        /* Note that we're using glInternalFormat, not glFormat.
                         * In this case, they should actually be the same thing,
                         * but we use glFormat somewhat differently for
                         * compressed textures.
                         * -flibit
                         */
                        GL.CompressedTexSubImage2D(
                            TextureTarget.Texture2D,
                            level,
                            x,
                            y,
                            w,
                            h,
                            (PixelFormat)glInternalFormat,
                            dataLength,
                            dataPtr
                            );
                    }
                    else
                    {
                        // Set pixel alignment to match texel size in bytes
                        int packSize = GetFormatSize();
                        if (packSize != 4)
                        {
                            GL.PixelStore(
                                PixelStoreParameter.UnpackAlignment,
                                packSize
                                );
                        }

                        GL.TexSubImage2D(
                            TextureTarget.Texture2D,
                            level,
                            x,
                            y,
                            w,
                            h,
                            glFormat,
                            glType,
                            dataPtr
                            );

                        // Keep this state sane -flibit
                        if (packSize != 4)
                        {
                            GL.PixelStore(
                                PixelStoreParameter.UnpackAlignment,
                                4
                                );
                        }
                    }
                }
                finally
                {
                    dataHandle.Free();
                }
            });
        }
Ejemplo n.º 13
0
        public Texture2D(
            GraphicsDevice graphicsDevice,
            int width,
            int height,
            bool mipMap,
            SurfaceFormat format
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice");
            }

            GraphicsDevice = graphicsDevice;
            Width          = width;
            Height         = height;
            LevelCount     = mipMap ? CalculateMipLevels(width, height) : 1;

            Format = format;
            GetGLSurfaceFormat();

            Threading.ForceToMainThread(() =>
            {
                texture = GraphicsDevice.GLDevice.CreateTexture(
                    typeof(Texture2D),
                    Format,
                    mipMap
                    );

                if (Format == SurfaceFormat.Dxt1 ||
                    Format == SurfaceFormat.Dxt3 ||
                    Format == SurfaceFormat.Dxt5)
                {
                    for (int i = 0; i < LevelCount; i += 1)
                    {
                        int levelWidth  = Math.Max(Width >> i, 1);
                        int levelHeight = Math.Max(Height >> i, 1);
                        graphicsDevice.GLDevice.glCompressedTexImage2D(
                            OpenGLDevice.GLenum.GL_TEXTURE_2D,
                            i,
                            (int)glInternalFormat,
                            levelWidth,
                            levelHeight,
                            0,
                            ((levelWidth + 3) / 4) * ((levelHeight + 3) / 4) * GetFormatSize(),
                            IntPtr.Zero
                            );
                    }
                }
                else
                {
                    for (int i = 0; i < LevelCount; i += 1)
                    {
                        graphicsDevice.GLDevice.glTexImage2D(
                            OpenGLDevice.GLenum.GL_TEXTURE_2D,
                            i,
                            (int)glInternalFormat,
                            Math.Max(Width >> i, 1),
                            Math.Max(Height >> i, 1),
                            0,
                            glFormat,
                            glType,
                            IntPtr.Zero
                            );
                    }
                }
            });
        }
Ejemplo n.º 14
0
        public static string GetGUIDEXT(PlayerIndex playerIndex)
        {
            IntPtr device = INTERNAL_devices[(int)playerIndex];

            if (device == IntPtr.Zero)
            {
                return(String.Empty);
            }

            if (INTERNAL_isGameController[(int)playerIndex])
            {
                device = SDL.SDL_GameControllerGetJoystick(device);
            }

            StringBuilder result = new StringBuilder();

            Threading.ForceToMainThread(() =>
            {
                byte[] resChar = new byte[33];                 // FIXME: Sort of arbitrary.
                SDL.SDL_JoystickGetGUIDString(
                    SDL.SDL_JoystickGetGUID(device),
                    resChar,
                    resChar.Length
                    );
                if (Game.Instance.Platform.OSVersion.Equals("Linux"))
                {
                    result.Append((char)resChar[8]);
                    result.Append((char)resChar[9]);
                    result.Append((char)resChar[10]);
                    result.Append((char)resChar[11]);
                    result.Append((char)resChar[16]);
                    result.Append((char)resChar[17]);
                    result.Append((char)resChar[18]);
                    result.Append((char)resChar[19]);
                }
                else if (Game.Instance.Platform.OSVersion.Equals("Mac OS X"))
                {
                    result.Append((char)resChar[0]);
                    result.Append((char)resChar[1]);
                    result.Append((char)resChar[2]);
                    result.Append((char)resChar[3]);
                    result.Append((char)resChar[16]);
                    result.Append((char)resChar[17]);
                    result.Append((char)resChar[18]);
                    result.Append((char)resChar[19]);
                }
                else if (Game.Instance.Platform.OSVersion.Equals("Windows"))
                {
                    bool isXInput = true;
                    foreach (byte b in resChar)
                    {
                        if (((char)b) != '0' && b != 0)
                        {
                            isXInput = false;
                            break;
                        }
                    }
                    if (isXInput)
                    {
                        result.Append("xinput");
                    }
                    else
                    {
                        result.Append((char)resChar[0]);
                        result.Append((char)resChar[1]);
                        result.Append((char)resChar[2]);
                        result.Append((char)resChar[3]);
                        result.Append((char)resChar[4]);
                        result.Append((char)resChar[5]);
                        result.Append((char)resChar[6]);
                        result.Append((char)resChar[7]);
                    }
                }
                else
                {
                    throw new Exception("SDL2_GamePad: Platform.OSVersion not handled!");
                }
            });
            return(result.ToString());
        }
Ejemplo n.º 15
0
        public void SetData <T>(
            int level,
            Rectangle?rect,
            T[] data,
            int startIndex,
            int elementCount
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            int x, y, w, h;

            if (rect.HasValue)
            {
                x = rect.Value.X;
                y = rect.Value.Y;
                w = rect.Value.Width;
                h = rect.Value.Height;
            }
            else
            {
                x = 0;
                y = 0;
                w = Math.Max(Width >> level, 1);
                h = Math.Max(Height >> level, 1);
            }

            Threading.ForceToMainThread(() =>
            {
                int elementSizeInBytes = Marshal.SizeOf(typeof(T));
                int startByte          = startIndex * elementSizeInBytes;
#if !JSIL
                GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
                IntPtr dataPtr      = (IntPtr)(dataHandle.AddrOfPinnedObject().ToInt64() + startByte);
#endif
                try
                {
                    GraphicsDevice.GLDevice.BindTexture(texture);
                    if (glFormat == OpenGLDevice.GLenum.GL_COMPRESSED_TEXTURE_FORMATS)
                    {
                        int dataLength;
                        if (elementCount > 0)
                        {
                            dataLength = elementCount * elementSizeInBytes;
                        }
                        else
                        {
                            dataLength = data.Length - startByte;
                        }

                        /* Note that we're using glInternalFormat, not glFormat.
                         * In this case, they should actually be the same thing,
                         * but we use glFormat somewhat differently for
                         * compressed textures.
                         * -flibit
                         */
#if JSIL
                        dynamic Document = Builtins.Global["document"];
                        dynamic Canvas   = Document.getElementById("canvas");
                        dynamic gl       = Canvas.getContext("webgl");
                        gl.compressedTexSubImage2D(
                            OpenGLDevice.GLenum.GL_TEXTURE_2D,
                            level,
                            x,
                            y,
                            w,
                            h,
                            glInternalFormat,
                            data
                            );
#else
                        GraphicsDevice.GLDevice.glCompressedTexSubImage2D(
                            OpenGLDevice.GLenum.GL_TEXTURE_2D,
                            level,
                            x,
                            y,
                            w,
                            h,
                            glInternalFormat,
                            dataLength,
                            dataPtr
                            );
#endif
                    }
                    else
                    {
                        // Set pixel alignment to match texel size in bytes
                        int packSize = GetFormatSize();
                        if (packSize != 4)
                        {
                            GraphicsDevice.GLDevice.glPixelStorei(
                                OpenGLDevice.GLenum.GL_UNPACK_ALIGNMENT,
                                packSize
                                );
                        }

#if JSIL
                        dynamic Document = Builtins.Global["document"];
                        dynamic Canvas   = Document.getElementById("canvas");
                        dynamic gl       = Canvas.getContext("webgl");
                        gl.texSubImage2D(
                            OpenGLDevice.GLenum.GL_TEXTURE_2D,
                            level,
                            x,
                            y,
                            w,
                            h,
                            glFormat,
                            glType,
                            data
                            );
#else
                        GraphicsDevice.GLDevice.glTexSubImage2D(
                            OpenGLDevice.GLenum.GL_TEXTURE_2D,
                            level,
                            x,
                            y,
                            w,
                            h,
                            glFormat,
                            glType,
                            dataPtr
                            );
#endif

                        // Keep this state sane -flibit
                        if (packSize != 4)
                        {
                            GraphicsDevice.GLDevice.glPixelStorei(
                                OpenGLDevice.GLenum.GL_UNPACK_ALIGNMENT,
                                4
                                );
                        }
                    }
                }
                finally
                {
#if !JSIL
                    dataHandle.Free();
#endif
                }
            });
        }
Ejemplo n.º 16
0
        public void GetData <T>(
            int level,
            Rectangle?rect,
            T[] data,
            int startIndex,
            int elementCount
            ) where T : struct
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentException("data cannot be null");
            }
            if (data.Length < startIndex + elementCount)
            {
                throw new ArgumentException(
                          "The data passed has a length of " + data.Length.ToString() +
                          " but " + elementCount.ToString() + " pixels have been requested."
                          );
            }

            Threading.ForceToMainThread(() =>
            {
                if (GraphicsDevice.GLDevice.ReadTargetIfApplicable(texture, level, data, rect))
                {
                    return;
                }

                GraphicsDevice.GLDevice.BindTexture(texture);

                if (glFormat == OpenGLDevice.GLenum.GL_COMPRESSED_TEXTURE_FORMATS)
                {
                    throw new NotImplementedException("GetData, CompressedTexture");
                }
                else if (rect == null)
                {
                    // Just throw the whole texture into the user array.
                    GCHandle ptr = GCHandle.Alloc(data, GCHandleType.Pinned);
                    try
                    {
                        GraphicsDevice.GLDevice.glGetTexImage(
                            OpenGLDevice.GLenum.GL_TEXTURE_2D,
                            0,
                            glFormat,
                            glType,
                            ptr.AddrOfPinnedObject()
                            );
                    }
                    finally
                    {
                        ptr.Free();
                    }
                }
                else
                {
                    // Get the whole texture...
                    T[] texData  = new T[Width * Height];
                    GCHandle ptr = GCHandle.Alloc(texData, GCHandleType.Pinned);
                    try
                    {
                        GraphicsDevice.GLDevice.glGetTexImage(
                            OpenGLDevice.GLenum.GL_TEXTURE_2D,
                            0,
                            glFormat,
                            glType,
                            ptr.AddrOfPinnedObject()
                            );
                    }
                    finally
                    {
                        ptr.Free();
                    }

                    // Now, blit the rect region into the user array.
                    Rectangle region = rect.Value;
                    int curPixel     = -1;
                    for (int row = region.Y; row < region.Y + region.Height; row += 1)
                    {
                        for (int col = region.X; col < region.X + region.Width; col += 1)
                        {
                            curPixel += 1;
                            if (curPixel < startIndex)
                            {
                                // If we're not at the start yet, just keep going...
                                continue;
                            }
                            if (curPixel > elementCount)
                            {
                                // If we're past the end, we're done!
                                return;
                            }
                            data[curPixel - startIndex] = texData[(row * Width) + col];
                        }
                    }
                }
            });
        }
Ejemplo n.º 17
0
        public Texture2D(
            GraphicsDevice graphicsDevice,
            int width,
            int height,
            bool mipmap,
            SurfaceFormat format
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice");
            }

            GraphicsDevice = graphicsDevice;
            Width          = width;
            Height         = height;
            LevelCount     = mipmap ? CalculateMipLevels(width, height) : 1;

            Format = format;
            GetGLSurfaceFormat();

            Threading.ForceToMainThread(() =>
            {
                texture = new OpenGLDevice.OpenGLTexture(
                    TextureTarget.Texture2D,
                    Format,
                    LevelCount > 1
                    );
                if (((Width & (Width - 1)) != 0) || ((Height & (Height - 1)) != 0))
                {
                    texture.WrapS.Set(TextureAddressMode.Clamp);
                    texture.WrapT.Set(TextureAddressMode.Clamp);
                }
                OpenGLDevice.Instance.BindTexture(texture);

                if (Format == SurfaceFormat.Dxt1 ||
                    Format == SurfaceFormat.Dxt3 ||
                    Format == SurfaceFormat.Dxt5)
                {
                    GL.CompressedTexImage2D(
                        TextureTarget.Texture2D,
                        0,
                        glInternalFormat,
                        Width,
                        Height,
                        0,
                        ((Width + 3) / 4) * ((Height + 3) / 4) * GetFormatSize(),
                        IntPtr.Zero
                        );
                }
                else
                {
                    GL.TexImage2D(
                        TextureTarget.Texture2D,
                        0,
                        glInternalFormat,
                        Width,
                        Height,
                        0,
                        glFormat,
                        glType,
                        IntPtr.Zero
                        );
                }
                texture.Flush(true);
            });
        }
Ejemplo n.º 18
0
        public void SetData <T>(
            int level,
            Rectangle?rect,
            T[] data,
            int startIndex,
            int elementCount
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            int x, y, w, h;

            if (rect.HasValue)
            {
                x = rect.Value.X;
                y = rect.Value.Y;
                w = rect.Value.Width;
                h = rect.Value.Height;
            }
            else
            {
                x = 0;
                y = 0;
                w = Math.Max(Width >> level, 1);
                h = Math.Max(Height >> level, 1);
            }

            Threading.ForceToMainThread(() =>
            {
                GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);

                try
                {
                    int elementSizeInBytes = Marshal.SizeOf(typeof(T));
                    int startByte          = startIndex * elementSizeInBytes;
                    IntPtr dataPtr         = (IntPtr)(dataHandle.AddrOfPinnedObject().ToInt64() + startByte);

                    OpenGLDevice.Instance.BindTexture(texture);
                    if (glFormat == (PixelFormat)All.CompressedTextureFormats)
                    {
                        int dataLength;
                        if (elementCount > 0)
                        {
                            dataLength = elementCount * elementSizeInBytes;
                        }
                        else
                        {
                            dataLength = data.Length - startByte;
                        }
                        if (rect.HasValue)
                        {
                            GL.CompressedTexSubImage2D(
                                TextureTarget.Texture2D,
                                level,
                                x,
                                y,
                                w,
                                h,
                                glFormat,
                                dataLength,
                                dataPtr
                                );
                        }
                        else
                        {
                            GL.CompressedTexImage2D(
                                TextureTarget.Texture2D,
                                level,
                                glInternalFormat,
                                w,
                                h,
                                0,
                                dataLength,
                                dataPtr
                                );
                        }
                    }
                    else
                    {
                        // Set pixel alignment to match texel size in bytes
                        GL.PixelStore(
                            PixelStoreParameter.UnpackAlignment,
                            GetFormatSize()
                            );

                        if (rect.HasValue)
                        {
                            GL.TexSubImage2D(
                                TextureTarget.Texture2D,
                                level,
                                x,
                                y,
                                w,
                                h,
                                glFormat,
                                glType,
                                dataPtr
                                );
                        }
                        else
                        {
                            GL.TexImage2D(
                                TextureTarget.Texture2D,
                                level,
                                glInternalFormat,
                                w,
                                h,
                                0,
                                glFormat,
                                glType,
                                dataPtr
                                );
                        }

                        // Return to default pixel alignment
                        GL.PixelStore(
                            PixelStoreParameter.UnpackAlignment,
                            4
                            );
                    }

                    GL.Finish();
                }
                finally
                {
                    dataHandle.Free();
                }
            });
        }
Ejemplo n.º 19
0
        public TextureCube(
            GraphicsDevice graphicsDevice,
            int size,
            bool mipMap,
            SurfaceFormat format
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice");
            }

            GraphicsDevice = graphicsDevice;
            Size           = size;
            LevelCount     = mipMap ? CalculateMipLevels(size) : 1;
            Format         = format;
            GetGLSurfaceFormat();

            Threading.ForceToMainThread(() =>
            {
                texture = new OpenGLDevice.OpenGLTexture(
                    TextureTarget.TextureCubeMap,
                    Format,
                    LevelCount > 1
                    );
                texture.WrapS.Set(TextureAddressMode.Clamp);
                texture.WrapT.Set(TextureAddressMode.Clamp);

                OpenGLDevice.Instance.BindTexture(texture);
                for (int i = 0; i < 6; i += 1)
                {
                    TextureTarget target = GetGLCubeFace((CubeMapFace)i);

                    if (glFormat == (PixelFormat)All.CompressedTextureFormats)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        GL.TexImage2D(
                            target,
                            0,
                            glInternalFormat,
                            size,
                            size,
                            0,
                            glFormat,
                            glType,
                            IntPtr.Zero
                            );
                    }
                }
                texture.Flush(true);

                if (mipMap)
                {
                    GL.TexParameter(
                        TextureTarget.TextureCubeMap,
                        TextureParameterName.GenerateMipmap,
                        1
                        );
                }
            });
        }
Ejemplo n.º 20
0
        public void SetData <T>(
            CubeMapFace cubeMapFace,
            int level,
            Rectangle?rect,
            T[] data,
            int startIndex,
            int elementCount
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            int xOffset, yOffset, width, height;

            if (rect.HasValue)
            {
                xOffset = rect.Value.X;
                yOffset = rect.Value.Y;
                width   = rect.Value.Width;
                height  = rect.Value.Height;
            }
            else
            {
                xOffset = 0;
                yOffset = 0;
                width   = Math.Max(1, Size >> level);
                height  = Math.Max(1, Size >> level);
            }

            Threading.ForceToMainThread(() =>
            {
                GCHandle dataHandle    = GCHandle.Alloc(data, GCHandleType.Pinned);
                int elementSizeInBytes = Marshal.SizeOf(typeof(T));
                int startByte          = startIndex * elementSizeInBytes;
                IntPtr dataPtr         = (IntPtr)(dataHandle.AddrOfPinnedObject().ToInt64() + startByte);

                try
                {
                    GraphicsDevice.GLDevice.BindTexture(texture);
                    if (glFormat == (PixelFormat)All.CompressedTextureFormats)
                    {
                        int dataLength;
                        if (elementCount > 0)
                        {
                            dataLength = elementCount * elementSizeInBytes;
                        }
                        else
                        {
                            dataLength = data.Length - startByte;
                        }

                        /* Note that we're using glInternalFormat, not glFormat.
                         * In this case, they should actually be the same thing,
                         * but we use glFormat somewhat differently for
                         * compressed textures.
                         * -flibit
                         */
                        GL.CompressedTexSubImage2D(
                            GetGLCubeFace(cubeMapFace),
                            level,
                            xOffset,
                            yOffset,
                            width,
                            height,
                            (PixelFormat)glInternalFormat,
                            dataLength,
                            dataPtr
                            );
                    }
                    else
                    {
                        GL.TexSubImage2D(
                            GetGLCubeFace(cubeMapFace),
                            level,
                            xOffset,
                            yOffset,
                            width,
                            height,
                            glFormat,
                            glType,
                            dataPtr
                            );
                    }
                }
                finally
                {
                    dataHandle.Free();
                }
            });
        }
Ejemplo n.º 21
0
        public TextureCube(
            GraphicsDevice graphicsDevice,
            int size,
            bool mipMap,
            SurfaceFormat format
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice");
            }

            GraphicsDevice = graphicsDevice;
            Size           = size;
            LevelCount     = mipMap ? CalculateMipLevels(size) : 1;
            Format         = format;
            GetGLSurfaceFormat();

            Threading.ForceToMainThread(() =>
            {
                texture = GraphicsDevice.GLDevice.CreateTexture(
                    typeof(TextureCube),
                    Format,
                    mipMap
                    );

                if (glFormat == (PixelFormat)All.CompressedTextureFormats)
                {
                    for (int i = 0; i < 6; i += 1)
                    {
                        for (int l = 0; l < LevelCount; l += 1)
                        {
                            int levelSize = Math.Max(size >> l, 1);
                            GL.CompressedTexImage2D(
                                GetGLCubeFace((CubeMapFace)i),
                                l,
                                glInternalFormat,
                                levelSize,
                                levelSize,
                                0,
                                ((levelSize + 3) / 4) * ((levelSize + 3) / 4) * GetFormatSize(),
                                IntPtr.Zero
                                );
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 6; i += 1)
                    {
                        for (int l = 0; l < LevelCount; l += 1)
                        {
                            GL.TexImage2D(
                                GetGLCubeFace((CubeMapFace)i),
                                l,
                                glInternalFormat,
                                size,
                                size,
                                0,
                                glFormat,
                                glType,
                                IntPtr.Zero
                                );
                        }
                    }
                }
            });
        }
Ejemplo n.º 22
0
        public void SetData <T>(
            CubeMapFace face,
            int level,
            Rectangle?rect,
            T[] data,
            int startIndex,
            int elementCount
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            int xOffset, yOffset, width, height;

            if (rect.HasValue)
            {
                xOffset = rect.Value.X;
                yOffset = rect.Value.Y;
                width   = rect.Value.Width;
                height  = rect.Value.Height;
            }
            else
            {
                xOffset = 0;
                yOffset = 0;
                width   = Math.Max(1, Size >> level);
                height  = Math.Max(1, Size >> level);
            }

            Threading.ForceToMainThread(() =>
            {
                GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);

                try
                {
                    OpenGLDevice.Instance.BindTexture(texture);
                    if (glFormat == (PixelFormat)All.CompressedTextureFormats)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        GL.TexSubImage2D(
                            GetGLCubeFace(face),
                            level,
                            xOffset,
                            yOffset,
                            width,
                            height,
                            glFormat,
                            glType,
                            (IntPtr)(dataHandle.AddrOfPinnedObject().ToInt64() + startIndex * Marshal.SizeOf(typeof(T)))
                            );
                    }
                }
                finally
                {
                    dataHandle.Free();
                }
            });
        }
Ejemplo n.º 23
0
        public TextureCube(
            GraphicsDevice graphicsDevice,
            int size,
            bool mipMap,
            SurfaceFormat format
            )
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice");
            }

            GraphicsDevice = graphicsDevice;
            Size           = size;
            LevelCount     = mipMap ? CalculateMipLevels(size) : 1;
            Format         = format;
            GetGLSurfaceFormat();

            Threading.ForceToMainThread(() =>
            {
                texture = GraphicsDevice.GLDevice.CreateTexture(
                    typeof(TextureCube),
                    Format,
                    mipMap
                    );

                if (glFormat == OpenGLDevice.GLenum.GL_COMPRESSED_TEXTURE_FORMATS)
                {
                    for (int i = 0; i < 6; i += 1)
                    {
                        for (int l = 0; l < LevelCount; l += 1)
                        {
                            int levelSize = Math.Max(size >> l, 1);
                            graphicsDevice.GLDevice.glCompressedTexImage2D(
                                OpenGLDevice.GLenum.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
                                l,
                                (int)glInternalFormat,
                                levelSize,
                                levelSize,
                                0,
                                ((levelSize + 3) / 4) * ((levelSize + 3) / 4) * GetFormatSize(),
                                IntPtr.Zero
                                );
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 6; i += 1)
                    {
                        for (int l = 0; l < LevelCount; l += 1)
                        {
                            int levelSize = Math.Max(size >> l, 1);
                            graphicsDevice.GLDevice.glTexImage2D(
                                OpenGLDevice.GLenum.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
                                l,
                                (int)glInternalFormat,
                                levelSize,
                                levelSize,
                                0,
                                glFormat,
                                glType,
                                IntPtr.Zero
                                );
                        }
                    }
                }
            });
        }