示例#1
0
        private void GenerateTextures(Dictionary <Bitmap, IEnumerable <GlyphInfo> > glyphData)
        {
            var imageSettings = new GorgonImageInfo(ImageType.Image2D, BufferFormat.R8G8B8A8_UNorm)
            {
                Width  = Info.TextureWidth,
                Height = Info.TextureHeight,
                Depth  = 1
            };
            var textureSettings = new GorgonTexture2DInfo
            {
                Format          = BufferFormat.R8G8B8A8_UNorm,
                Width           = Info.TextureWidth,
                Height          = Info.TextureHeight,
                Usage           = ResourceUsage.Default,
                Binding         = TextureBinding.ShaderResource,
                IsCubeMap       = false,
                MipLevels       = 1,
                MultisampleInfo = GorgonMultisampleInfo.NoMultiSampling
            };

            GorgonImage     image       = null;
            GorgonTexture2D texture     = null;
            int             arrayIndex  = 0;
            int             bitmapCount = glyphData.Count;

            try
            {
                // We copy each bitmap into a texture array index until we've hit the max texture array size, and then
                // we move to a new texture.  This will keep our glyph textures inside of a single texture object until
                // it is absolutely necessary to change and should improve performance when rendering.
                foreach (KeyValuePair <Bitmap, IEnumerable <GlyphInfo> > glyphBitmap in glyphData)
                {
                    if ((image == null) || (arrayIndex >= Graphics.VideoAdapter.MaxTextureArrayCount))
                    {
                        textureSettings.ArrayCount = imageSettings.ArrayCount = bitmapCount.Min(Graphics.VideoAdapter.MaxTextureArrayCount);
                        arrayIndex = 0;

                        image?.Dispose();
                        image = new GorgonImage(imageSettings);

                        texture = image.ToTexture2D(Graphics, new GorgonTexture2DLoadOptions
                        {
                            Name = $"GorgonFont_{Name}_Internal_Texture_{Guid.NewGuid():N}"
                        });
                        _internalTextures.Add(texture);
                    }

                    CopyBitmap(glyphBitmap.Key, image, arrayIndex);

                    // Send to our texture.
                    texture.SetData(image.Buffers[0, arrayIndex], destArrayIndex: arrayIndex);

                    foreach (GlyphInfo info in glyphBitmap.Value)
                    {
                        info.Texture           = texture;
                        info.TextureArrayIndex = arrayIndex;
                    }

                    bitmapCount--;
                    arrayIndex++;
                }
            }
            finally
            {
                image?.Dispose();
            }
        }