Пример #1
0
        public unsafe Font(FontContent font)
        {
            Content = font;
            Sources = new StructuredBuffer <GlyphSource>(BufferTarget.ShaderStorageBuffer, font.Characters.Count, font.Name + " Glyph Sources");

            GL.BindTexture(TextureTarget.Texture2D, Atlas);
            var data = font.Atlas.Pin();

            for (int mipLevel = 0; mipLevel < font.Atlas.MipLevels; ++mipLevel)
            {
                GL.TexImage2D(TextureTarget.Texture2D, mipLevel, PixelInternalFormat.R8Snorm, font.Atlas.Width >> mipLevel, font.Atlas.Height >> mipLevel, 0, PixelFormat.Red, PixelType.Byte, new IntPtr(data + font.Atlas.GetMipStartIndex(mipLevel)));
            }
            font.Atlas.Unpin();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, font.Atlas.MipLevels - 1);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.BindTexture(TextureTarget.Texture2D, 0);

            int nextSourceId = 0;
            var sourcesData  = new GlyphSource[font.Characters.Count];

            foreach (var character in font.Characters)
            {
                sourceIds.Add(character.Key, nextSourceId);
                sourcesData[nextSourceId] = new GlyphSource {
                    Minimum       = new Vector2(character.Value.SourceMinimum.X, character.Value.SourceMinimum.Y),
                    PackedSpan    = character.Value.SourceSpan.X | (character.Value.SourceSpan.Y << 16),
                    DistanceScale = character.Value.DistanceScale
                };
                ++nextSourceId;
            }
            Sources.Update(sourcesData);
        }
Пример #2
0
        public unsafe Font(Device device, DeviceContext context, FontContent font)
        {
            Content = font;
            Sources = new StructuredBuffer <GlyphSource>(device, font.Characters.Count, font.Name + " Glyph Sources");
            Atlas   = new Texture2D(device, new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = SharpDX.DXGI.Format.R8_SNorm,
                Height            = font.Atlas.Height,
                Width             = font.Atlas.Width,
                MipLevels         = font.Atlas.MipLevels,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = ResourceUsage.Default
            });
            Atlas.DebugName    = font.Name + " Atlas";
            AtlasSRV           = new ShaderResourceView(device, Atlas);
            AtlasSRV.DebugName = font.Name + " Atlas SRV";

            var data = font.Atlas.Pin();

            for (int mipLevel = 0; mipLevel < font.Atlas.MipLevels; ++mipLevel)
            {
                var databox = new DataBox(new IntPtr(data + font.Atlas.GetMipStartIndex(mipLevel)), font.Atlas.GetRowPitch(mipLevel), 0);
                context.UpdateSubresource(databox, Atlas, mipLevel);
            }
            font.Atlas.Unpin();

            sourceIds = new Dictionary <char, int>();
            int nextSourceId = 0;
            var sourcesData  = new GlyphSource[font.Characters.Count];

            foreach (var character in font.Characters)
            {
                sourceIds.Add(character.Key, nextSourceId);
                sourcesData[nextSourceId] = new GlyphSource
                {
                    Minimum       = new Vector2(character.Value.SourceMinimum.X, character.Value.SourceMinimum.Y),
                    PackedSpan    = character.Value.SourceSpan.X | (character.Value.SourceSpan.Y << 16),
                    DistanceScale = character.Value.DistanceScale
                };
                ++nextSourceId;
            }
            Sources.Update(context, sourcesData);
        }