Exemplo n.º 1
0
        /// <summary>
        /// Loads a texture from a stream.
        /// </summary>
        /// <param name="device">The <see cref="Direct3D11.Device"/>.</param>
        /// <param name="stream">The stream to load the texture from.</param>
        /// <param name="flags">Sets the texture flags (for unordered access...etc.)</param>
        /// <param name="usage">Usage of the resource. Default is <see cref="ResourceUsage.Immutable"/> </param>
        /// <returns>A texture</returns>
        public static Texture Load(Direct3D11.Device device, Stream stream, TextureFlags flags = TextureFlags.ShaderResource, ResourceUsage usage = ResourceUsage.Immutable)
        {
            stream.Position = 0;
            var image = Image.Load(stream);

            if (image == null)
            {
                stream.Position = 0;
                return(null);
            }

            try
            {
                switch (image.Description.Dimension)
                {
                case TextureDimension.Texture1D:
                    return(Texture1D.New(device, image, flags, usage));

                case TextureDimension.Texture2D:
                    return(Texture2D.New(device, image, flags, usage));

                case TextureDimension.Texture3D:
                    return(Texture3D.New(device, image, flags, usage));

                case TextureDimension.TextureCube:
                    return(TextureCube.New(device, image, flags, usage));
                }
            }
            finally
            {
                image.Dispose();
                stream.Position = 0;
            }
            throw new InvalidOperationException("Dimension not supported");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new texture with the specified generic texture description.
        /// </summary>
        /// <param name="graphicsDevice">The graphics device.</param>
        /// <param name="description">The description.</param>
        /// <returns>A Texture instance, either a RenderTarget or DepthStencilBuffer or Texture, depending on Binding flags.</returns>
        public static Texture New(Direct3D11.Device graphicsDevice, TextureDescription description)
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice");
            }

            if ((description.BindFlags & BindFlags.RenderTarget) != 0)
            {
                throw new NotSupportedException("RenderTarget is not supported.");
            }
            else if ((description.BindFlags & BindFlags.DepthStencil) != 0)
            {
                throw new NotSupportedException("DepthStencil is not supported.");
            }
            else
            {
                switch (description.Dimension)
                {
                case TextureDimension.Texture1D:
                    return(Texture1D.New(graphicsDevice, description));

                case TextureDimension.Texture2D:
                    return(Texture2D.New(graphicsDevice, description));

                case TextureDimension.Texture3D:
                    return(Texture3D.New(graphicsDevice, description));

                case TextureDimension.TextureCube:
                    return(TextureCube.New(graphicsDevice, description));
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        public static Texture2D Load(GraphicsDevice graphicsDevice, ImageC image, ResourceUsage usage = ResourceUsage.Immutable)
        {
            if (graphicsDevice == null)
            {
                return(null);
            }

            Texture2D result = null;

            image.GetIntPtr((IntPtr data) =>
            {
                Texture2DDescription description;
                description.ArraySize         = 1;
                description.BindFlags         = BindFlags.ShaderResource;
                description.CpuAccessFlags    = CpuAccessFlags.None;
                description.Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm;
                description.Height            = image.Height;
                description.MipLevels         = 1;
                description.OptionFlags       = ResourceOptionFlags.None;
                description.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);
                description.Usage             = usage;
                description.Width             = image.Width;

                //return Texture2D.New(graphicsDevice, description, new DataBox[] { new DataBox(lockData.Scan0, lockData.Stride, 0) }); //Only for the none toolkit version which unfortunately we cannot use currently.
                result = Texture2D.New(graphicsDevice, description.Width, description.Height, description.MipLevels, description.Format,
                                       new[] { new SharpDX.DataBox(data, image.Width * ImageC.PixelSize, 0) }, TextureFlags.ShaderResource, 1, description.Usage);
            });
            return(result);
        }
Exemplo n.º 4
0
        // RefreshAndTransferTexture() updates the texture on the gpu
        // Call this before the texture is used at the rendering stage or else a race condition will occur
        // If the image dimension change the texture must be recreated or else the directx update call will fail when the image is being transfered to the gpu
        public void RefreshAndTransferTexture(GraphicsDevice gd)
        {
            // Lock for thread safety (used in setimage())
            lock (_textureLock)
            {
                if (_imageUpdated && Image != null)
                {
                    // Destroy the existing texture if the new image does not fit the current texture description
                    if (Texture != null && (Texture.Description.Width != Image.Description.Width ||
                                            Texture.Description.Height != Image.Description.Height ||
                                            Texture.Description.ArraySize != Image.Description.ArraySize ||
                                            Texture.Description.Depth != Image.Description.Depth ||
                                            Texture.Description.Dimension != Image.Description.Dimension ||
                                            Texture.Description.Format != Image.Description.Format ||
                                            Texture.Description.MipLevels != Image.Description.MipLevels))
                    {
                        Texture.Dispose();
                        Texture = null;
                    }
                    // (Re)create the texture context as a dynamic texture (cpu update possible) and store the image dimensions
                    if (Texture == null)
                    {
                        Texture = Texture2D.New(gd, Image, TextureFlags.ShaderResource, ResourceUsage.Dynamic);
                        // Update the image plane size to match the image's aspect ratio
                        UpdatePlaneAspectRatio();
                    }
                    else
                    {
                        // Update the texture
                        Texture.SetData(Image);
                    }

                    _imageUpdated = false;
                    FPS.Tick();
                }
            }
        }
Exemplo n.º 5
0
        internal SpriteFont(GraphicsDevice device, SpriteFontData spriteFontData, bool disposeSpriteFontDataResources)
            : base(device)
        {
            drawGlyphDelegate    = InternalDrawGlyph;
            measureGlyphDelegate = MeasureStringGlyph;

            // Read the glyph data.
            globalBaseOffsetY = spriteFontData.BaseOffset;
            glyphs            = spriteFontData.Glyphs;
            characterMap      = new Dictionary <char, int>(glyphs.Length * 2);

            // Prebuild the character map
            var characterList = new List <char>(glyphs.Length);

            for (int i = 0; i < glyphs.Length; i++)
            {
                var charItem = (char)glyphs[i].Character;
                characterMap.Add(charItem, i);
                characterList.Add(charItem);
            }

            // Prepare kernings if they are available.
            var kernings = spriteFontData.Kernings;

            if (kernings != null)
            {
                kerningMap = new Dictionary <int, float>(spriteFontData.Kernings.Length);
                for (int i = 0; i < kernings.Length; i++)
                {
                    int key = (kernings[i].First << 16) | kernings[i].Second;
                    kerningMap.Add(key, kernings[i].Offset);
                }
            }

            Characters = new ReadOnlyCollection <char>(characterList);

            // Read font properties.
            LineSpacing = spriteFontData.LineSpacing;

            if (spriteFontData.DefaultCharacter > 0)
            {
                DefaultCharacter = (char)spriteFontData.DefaultCharacter;
                if (!characterMap.TryGetValue(DefaultCharacter.Value, out defaultGlyphIndex))
                {
                    defaultGlyphIndex = -1;
                }
            }

            // Read the texture data.
            textures = new Texture2D[spriteFontData.Bitmaps.Length];
            for (int i = 0; i < textures.Length; i++)
            {
                var bitmap = spriteFontData.Bitmaps[i];
                if (bitmap.Data is SpriteFontData.BitmapData)
                {
                    var image = (SpriteFontData.BitmapData)bitmap.Data;
                    textures[i] = ToDispose(Texture2D.New(device, image.Width, image.Height, image.PixelFormat, image.Data));
                }
                else if (bitmap.Data is Texture2D)
                {
                    textures[i] = (Texture2D)bitmap.Data;
                    if (disposeSpriteFontDataResources)
                    {
                        ToDispose(textures[i]);
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("SpriteFontData.Bitmap of type [{0}] is not supported. Only SpriteFontData.BitmapData or Texture2D", bitmap == null ? "null" : bitmap.GetType().Name));
                }
            }
        }