Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Buffer" /> class.
        /// </summary>
        /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
        /// <param name="description">The description.</param>
        /// <param name="bufferFlags">Type of the buffer.</param>
        /// <param name="viewFormat">The view format.</param>
        /// <param name="dataPointer">The data pointer.</param>
        protected Buffer(GraphicsDevice device, BufferDescription description, BufferFlags bufferFlags, PixelFormat viewFormat, IntPtr dataPointer)
            : base(device)
        {
            Description = description;
            BufferFlags = bufferFlags;
            ViewFormat  = viewFormat;

#if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
            int  pixelSize;
            bool isCompressed;
            OpenGLConvertExtensions.ConvertPixelFormat(device, viewFormat, out internalFormat, out glPixelFormat, out type, out pixelSize, out isCompressed);
#endif

            Recreate(dataPointer);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Buffer" /> class.
        /// </summary>
        /// <param name="description">The description.</param>
        /// <param name="viewFlags">Type of the buffer.</param>
        /// <param name="viewFormat">The view format.</param>
        /// <param name="dataPointer">The data pointer.</param>
        protected Buffer InitializeFromImpl(BufferDescription description, BufferFlags viewFlags, PixelFormat viewFormat, IntPtr dataPointer)
        {
            bufferDescription = description;
            ViewFlags         = viewFlags;

#if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
            int  pixelSize;
            bool isCompressed;
            OpenGLConvertExtensions.ConvertPixelFormat(GraphicsDevice, ref viewFormat, out internalFormat, out glPixelFormat, out type, out pixelSize, out isCompressed);
#endif
            ViewFormat = viewFormat;

            Recreate(dataPointer);

            return(this);
        }
Ejemplo n.º 3
0
        private void InitializeFromImpl(DataBox[] dataBoxes = null)
        {
            if (ParentTexture != null)
            {
                resourceId = ParentTexture.ResourceId;

                // copy parameters
                InternalFormat = ParentTexture.InternalFormat;
                FormatGl       = ParentTexture.FormatGl;
                Type           = ParentTexture.Type;
                Target         = ParentTexture.Target;
                DepthPitch     = ParentTexture.DepthPitch;
                RowPitch       = ParentTexture.RowPitch;
                IsDepthBuffer  = ParentTexture.IsDepthBuffer;
                HasStencil     = ParentTexture.HasStencil;
                IsRenderbuffer = ParentTexture.IsRenderbuffer;

                resourceIdStencil   = ParentTexture.ResourceIdStencil;
                pixelBufferObjectId = ParentTexture.PixelBufferObjectId;
            }

            if (resourceId == 0)
            {
                switch (Dimension)
                {
                case TextureDimension.Texture1D:
#if !SILICONSTUDIO_PLATFORM_MONO_MOBILE
                    Target = TextureTarget.Texture1D;
                    break;
#endif
                case TextureDimension.Texture2D:
                    Target = TextureTarget.Texture2D;
                    break;

                case TextureDimension.Texture3D:
                    Target = TextureTarget.Texture3D;
                    break;

                case TextureDimension.TextureCube:
                    Target = TextureTarget.TextureCubeMap;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                PixelInternalFormat internalFormat;
                PixelFormatGl       format;
                PixelType           type;
                int  pixelSize;
                bool compressed;
                OpenGLConvertExtensions.ConvertPixelFormat(GraphicsDevice, Description.Format, out internalFormat, out format, out type, out pixelSize, out compressed);

                InternalFormat = internalFormat;
                FormatGl       = format;
                Type           = type;
                DepthPitch     = Description.Width * Description.Height * pixelSize;
                RowPitch       = Description.Width * pixelSize;

                if ((Description.Flags & TextureFlags.DepthStencil) != 0)
                {
                    IsDepthBuffer = true;
                    HasStencil    = InternalHasStencil(Format);
                }
                else
                {
                    IsDepthBuffer = false;
                    HasStencil    = false;
                }

                using (GraphicsDevice.UseOpenGLCreationContext())
                {
                    // Depth texture are render buffer for now
                    // TODO: enable switch
                    if ((Description.Flags & TextureFlags.DepthStencil) != 0 && (Description.Flags & TextureFlags.ShaderResource) == 0)
                    {
                        RenderbufferStorage depth, stencil;
                        ConvertDepthFormat(GraphicsDevice, Description.Format, out depth, out stencil);

                        GL.GenRenderbuffers(1, out resourceId);
                        GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, resourceId);
                        GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, depth, Width, Height);

                        // separate stencil
                        if (stencil != 0)
                        {
                            GL.GenRenderbuffers(1, out resourceIdStencil);
                            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, resourceIdStencil);
                            GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, stencil, Width, Height);
                        }

                        GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);

                        IsRenderbuffer = true;
                        return;
                    }
                    else
                    {
                        GL.GenTextures(1, out resourceId);
                        GL.BindTexture(Target, resourceId);

                        IsRenderbuffer = false;
                    }

                    // No filtering on depth buffer
                    if ((Description.Flags & (TextureFlags.RenderTarget | TextureFlags.DepthStencil)) !=
                        TextureFlags.None)
                    {
                        GL.TexParameter(Target, TextureParameterName.TextureMinFilter,
                                        (int)TextureMinFilter.Nearest);
                        GL.TexParameter(Target, TextureParameterName.TextureMagFilter,
                                        (int)TextureMagFilter.Nearest);
                        GL.TexParameter(Target, TextureParameterName.TextureWrapS,
                                        (int)TextureWrapMode.ClampToEdge);
                        GL.TexParameter(Target, TextureParameterName.TextureWrapT,
                                        (int)TextureWrapMode.ClampToEdge);
                        BoundSamplerState = GraphicsDevice.SamplerStates.PointClamp;
                    }
#if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
                    else if (Description.MipLevels <= 1)
                    {
                        GL.TexParameter(Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                        GL.TexParameter(Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                    }
#endif

#if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
                    if (!GraphicsDevice.IsOpenGLES2)
#endif
                    {
                        GL.TexParameter(Target, TextureParameterName.TextureBaseLevel, 0);
                        GL.TexParameter(Target, TextureParameterName.TextureMaxLevel, Description.MipLevels - 1);
                    }

                    if (Description.MipLevels == 0)
                    {
                        throw new NotImplementedException();
                    }

                    var setSize = TextureSetSize(Target);

                    for (var arrayIndex = 0; arrayIndex < Description.ArraySize; ++arrayIndex)
                    {
                        var offsetArray = arrayIndex * Description.MipLevels;
                        for (int i = 0; i < Description.MipLevels; ++i)
                        {
                            IntPtr data   = IntPtr.Zero;
                            var    width  = CalculateMipSize(Description.Width, i);
                            var    height = CalculateMipSize(Description.Height, i);
                            if (dataBoxes != null && i < dataBoxes.Length)
                            {
                                if (setSize > 1 && !compressed && dataBoxes[i].RowPitch != width * pixelSize)
                                {
                                    throw new NotSupportedException("Can't upload texture with pitch in glTexImage2D.");
                                }
                                // Might be possible, need to check API better.
                                data = dataBoxes[offsetArray + i].DataPointer;
                            }

                            if (setSize == 2)
                            {
                                var dataSetTarget = GetTextureTargetForDataSet2D(Target, arrayIndex);
                                if (compressed)
                                {
                                    GL.CompressedTexImage2D(dataSetTarget, i, (CompressedInternalFormat2D)internalFormat,
                                                            width, height, 0, dataBoxes[offsetArray + i].SlicePitch, data);
                                }
                                else
                                {
                                    GL.TexImage2D(dataSetTarget, i, (TextureComponentCount2D)internalFormat,
                                                  width, height, 0, format, type, data);
                                }
                            }
                            else if (setSize == 3)
                            {
                                var dataSetTarget = GetTextureTargetForDataSet3D(Target);
                                var depth         = Target == TextureTarget.Texture2DArray ? Description.Depth : CalculateMipSize(Description.Depth, i); // no depth mipmaps in Texture2DArray
                                if (compressed)
                                {
                                    GL.CompressedTexImage3D(dataSetTarget, i, (CompressedInternalFormat3D)internalFormat,
                                                            width, height, depth, 0, dataBoxes[offsetArray + i].SlicePitch, data);
                                }
                                else
                                {
                                    GL.TexImage3D(dataSetTarget, i, (TextureComponentCount3D)internalFormat,
                                                  width, height, depth, 0, format, type, data);
                                }
                            }
#if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
                            else if (setSize == 1)
                            {
                                if (compressed)
                                {
                                    GL.CompressedTexImage1D(TextureTarget.Texture1D, i, internalFormat,
                                                            width, 0, dataBoxes[offsetArray + i].SlicePitch, data);
                                }
                                else
                                {
                                    GL.TexImage1D(TextureTarget.Texture1D, i, internalFormat,
                                                  width, 0, format, type, data);
                                }
                            }
#endif
                        }
                    }
                    GL.BindTexture(Target, 0);

                    InitializePixelBufferObject();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Inits this instance with the specified texture datas.
        /// </summary>
        /// <param name="textureDatas">The texture datas.</param>
        protected virtual void Init(DataBox[] textureDatas)
        {
            if (Target != TextureTarget.Texture2D && Target != TextureTarget.TextureCubeMap)
            {
                throw new Exception("incorrect type of Texture2D, it should be TextureTarget.Texture2D or TextureTarget.TextureCubeMap");
            }

            using (var creationContext = GraphicsDevice.UseOpenGLCreationContext())
            {
                // Can we just do a renderbuffer?
                if ((Description.Flags & TextureFlags.ShaderResource) == 0)
                {
                    if ((Description.Flags & TextureFlags.DepthStencil) == TextureFlags.DepthStencil)
                    {
                        RenderbufferStorage depth, stencil;
                        ConvertDepthFormat(GraphicsDevice, Description.Format, out depth, out stencil);

                        // Create depth render buffer (might contain stencil data too)
                        GL.GenRenderbuffers(1, out resourceId);
                        GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, resourceId);
                        GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, depth, Description.Width, Description.Height);
                        if (OpenGLConvertExtensions.GetErrorCode() != ErrorCode.NoError)
                        {
                            throw new InvalidOperationException("Could not create render buffer");
                        }

                        // If stencil buffer is separate, create it as well
                        if (stencil != 0)
                        {
                            GL.GenRenderbuffers(1, out ResouceIdStencil);
                            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, ResouceIdStencil);
                            GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, stencil, Description.Width, Description.Height);
                            if (OpenGLConvertExtensions.GetErrorCode() != ErrorCode.NoError)
                            {
                                throw new InvalidOperationException("Could not create render buffer");
                            }
                        }

                        GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);
                        return;
                    }
                }

                PixelInternalFormat internalFormat;
                PixelFormatGl       format;
                PixelType           type;
                int  pixelSize;
                bool compressed;
                ConvertPixelFormat(GraphicsDevice, Description.Format, out internalFormat, out format, out type, out pixelSize, out compressed);

                InternalFormat = internalFormat;
                FormatGl       = format;
                Type           = type;
                DepthPitch     = Description.Width * Description.Height * pixelSize;
                RowPitch       = Description.Width * pixelSize;

                if (Description.Usage == GraphicsResourceUsage.Staging)
                {
#if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
                    GL.GenBuffers(1, out resourceId);
                    GL.BindBuffer(BufferTarget.PixelPackBuffer, resourceId);
                    GL.BufferData(BufferTarget.PixelPackBuffer, (IntPtr)DepthPitch, IntPtr.Zero,
                                  BufferUsageHint.StreamRead);
                    GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
#else
                    StagingData = Marshal.AllocHGlobal(DepthPitch);
#endif
                }
                else
                {
                    int textureId;

                    // If we're on main context, change internal states so that texture is bound again
                    if (!creationContext.UseDeviceCreationContext)
                    {
                        GraphicsDevice.UseTemporaryFirstTexture();
                    }

                    GL.GenTextures(1, out textureId);
                    GL.BindTexture(TextureTarget.Texture2D, textureId);

                    // No filtering on depth buffer
                    if ((Description.Flags & (TextureFlags.RenderTarget | TextureFlags.DepthStencil)) !=
                        TextureFlags.None)
                    {
                        GL.TexParameter(Target, TextureParameterName.TextureMinFilter,
                                        (int)TextureMinFilter.Nearest);
                        GL.TexParameter(Target, TextureParameterName.TextureMagFilter,
                                        (int)TextureMagFilter.Nearest);
                        GL.TexParameter(Target, TextureParameterName.TextureWrapS,
                                        (int)TextureWrapMode.ClampToEdge);
                        GL.TexParameter(Target, TextureParameterName.TextureWrapT,
                                        (int)TextureWrapMode.ClampToEdge);
                    }
#if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
                    else if (Description.MipLevels <= 1)
                    {
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                    }
#endif

#if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
                    GL.TexParameter(Target, TextureParameterName.TextureBaseLevel, 0);
                    GL.TexParameter(Target, TextureParameterName.TextureMaxLevel, Description.MipLevels - 1);
#endif

                    if (Description.MipLevels == 0)
                    {
                        throw new NotImplementedException();
                    }

                    for (int i = 0; i < Description.MipLevels; ++i)
                    {
                        IntPtr data   = IntPtr.Zero;
                        var    width  = CalculateMipSize(Description.Width, i);
                        var    height = CalculateMipSize(Description.Height, i);
                        if (textureDatas != null && i < textureDatas.Length)
                        {
                            if (!compressed && textureDatas[i].RowPitch != width * pixelSize)
                            {
                                throw new NotSupportedException("Can't upload texture with pitch in glTexImage2D.");
                            }
                            // Might be possible, need to check API better.
                            data = textureDatas[i].DataPointer;
                        }
                        if (compressed)
                        {
#if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES && !SILICONSTUDIO_PLATFORM_MONO_MOBILE
                            throw new NotSupportedException("Can't use compressed textures on desktop OpenGL ES.");
#else
                            GL.CompressedTexImage2D(Target, i, internalFormat,
                                                    width, height, 0, textureDatas[i].SlicePitch, data);
#endif
                        }
                        else
                        {
#if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES && !SILICONSTUDIO_PLATFORM_MONO_MOBILE
                            GL.TexImage2D(TextureTarget2d.Texture2D, i, internalFormat.ToOpenGL(),
                                          width, height, 0, format, type, data);
#else
                            GL.TexImage2D(Target, i, internalFormat,
                                          width, height, 0, format, type, data);
#endif
                        }
                    }
                    GL.BindTexture(Target, 0);

                    resourceId = textureId;

#if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
                    if (!GraphicsDevice.IsOpenGLES2)
#endif
                    {
                        if (Description.Usage == GraphicsResourceUsage.Dynamic)
                        {
                            InitializePixelBufferObject();
                        }
                    }
                }
            }
        }