mirror of
https://github.com/GreemDev/Ryujinx.git
synced 2024-12-25 05:35:47 +00:00
a07086c280
* Profiler initial setup * Capture actual timing data * Profiling data dumped to file on close * Support for multiple sessions under the same name * Service profiling * Sort output for easier read * csv output * Split session into 2 seperate values * Refactor name to category * Basic profiling window dummy. Toggle with F1 or set key with config No actual data displayed yet, just a pretty triangle * Simple font rendering * Display some actual timing data * Fix font bearing being ignored * x bearing and advance. Fixed y bearing calc * Different coloured lines to make reading easier * Scrolling * Multiple columns for name * Column titles * display in ms rather than ticks * Bars to display times * Sortable columns * Regex filtering * Better instant timing calculation Fixed minor regex bug * Better filtering Better max value calculation Skip some rendering to reduce profiler weight * Variable update rate * Show/hide inactive button Some other touchups * Add missing project reference * Hide inactive and pause * Fix viewport errors * Update initial window position * Variable name cleanup * Disable timing dump by default * Internal Profile refactor and cleanup * Timing info cleanup * Profile config cleanup * Settings cleanup * Button refactor * Profile refactor * Profile window cleanup * Window manager refactor * Font service cleanup * Fixed bug in profiling method where method was called twice without profiling enabled * Allow update rates of less than 1hz * Stop using window.run because it's apparently not great for performance. Some other performance things, should only draw a new frame when something has changed * Improved time tracking to keep history * Profile window was getting too long so I added regions and split bar rendering out into partial class * Dummy graph view with button to toggle * Realtime graphing initial commit * Display totals on new bar * Simple zooming support with arrow keys * Limit graph zoom and label start and stop * Added support for timing flags * Stop data running away when paused and frame updated * Manual step button * Update at when flag issued (ie every frame) * Removed useless finish profiling call * Enable and disable profiling at compile time. * Better plage for frame swap flag, also kept enough flags to cover larger time spans * No more stopwatches created, uses PerformanceCounter now * public and internal fields to props * Move visible update to update rather than draw as it causes a lockup if called from draw Also added profile window disposal so closing main window closes profiler too * Fixed optimization settings for profiled builds * Appveyer script guess to add profiling builds * Quotes * 1 less quote * Maybe escape space? * Specify config * Different approach * Fix file paths * Fix another path * Better artifact naming * Missing - * test string * Removed for, to test * readd for * moved dashes around so artifacts can begin with letters * quote env vars * martix * Removed configs * Much more efficient capture, ConcurrentDictionary was causing too much overhead * Skip repeating pixels during draw * Stop ram usage getting too high. Compensating for cleanup doing more now * Profile CPU, execute skipped because it's just too much work * Fixed bug with skipping draws. Furthest needed to be reset every loop * Less distracting colour for timing flags * Removed profile method function. It just doesn't play nice with conditional compilation so best to remove it now before it's used a lot * Null check for category, group and item * Forgot to reset instant count/time * Increment line when blank * Fix threading conflict Fixed instant count and time. Now accuratly represents the total time and count in the buffer * Fixed bug in time rendering where times were being trimmed to an int. Also added microsecond/millisecond formatting to reduce the number of decimal places needed * Support for multiple profiling levels * Sometimes it would have to wait a long time for lock to clear so moved it to a tryenter and skip if already locked * Dumb bug regarding clearing of timestamps. Start is already removed so no need to add it to the start * Optimisations in drawing routine: Only calculate bar top and bottom once per bar rather than once per timestamp Pre-calculate the right side of the graph as it was being calculated multiple times per bar Skip rendering timestamps that occupy the same pixel space now uses the raw timestamp to decide. While technically not as accurate it's much easier as the right side of the bar doesn't have to be calculated for a skipped timestamp * Couple alignment changes * Custom equals overload for profile config. The default implpmentation was just too slow * Bump cleanup thread priority. It clears the timer queue so it need to be run frequently * Fixed bug with scrolling caused by recent rendering optimisations. Simply forgot to increment the line index on a skipped line * Stopped blocking memory disposal so much. Also parralised(?) cleanup call * Uses Arial for font. * Enable AA * Inital seperated config support * Fix profile input from keyboard * Check toggle visible key from profiler * Can't use conditional here as _profileWindow doesn't exist it non-profiling build * Removed junk from merge in sln * Fromatting cleanup for review * Fiked small bug caused by race condition * Added multiple flags with colours Added way to set max flags * Fixed flag times Dispays time flags in window * Colors for text frame times * enable and disable flags button added better fix for race crash * Re factored npad out * Explicitly specified type in foreach * Removed extra line * Added s to fix nit * Comment to clarify default time * Another s nit * Ordering nit * Uses Interlocked.Increment over lock * Unindented #if's and #regions * Comment to clarify these are indexes in the list * Uses iequatable over override equals to avoid conversion and checks at runtime * Removed no longer used variable
257 lines
No EOL
8.9 KiB
C#
257 lines
No EOL
8.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using OpenTK;
|
|
using OpenTK.Graphics.OpenGL;
|
|
using SharpFont;
|
|
|
|
namespace Ryujinx.Profiler.UI.SharpFontHelpers
|
|
{
|
|
public class FontService
|
|
{
|
|
private struct CharacterInfo
|
|
{
|
|
public float Left;
|
|
public float Right;
|
|
public float Top;
|
|
public float Bottom;
|
|
|
|
public int Width;
|
|
public float Height;
|
|
|
|
public float AspectRatio;
|
|
|
|
public float BearingX;
|
|
public float BearingY;
|
|
public float Advance;
|
|
}
|
|
|
|
private const int SheetWidth = 1024;
|
|
private const int SheetHeight = 512;
|
|
private int ScreenWidth, ScreenHeight;
|
|
private int CharacterTextureSheet;
|
|
private CharacterInfo[] characters;
|
|
|
|
public Color fontColor { get; set; } = Color.Black;
|
|
|
|
private string GetFontPath()
|
|
{
|
|
string fontFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
|
|
|
|
// Only uses Arial, add more fonts here if wanted
|
|
string path = Path.Combine(fontFolder, "arial.ttf");
|
|
if (File.Exists(path))
|
|
{
|
|
return path;
|
|
}
|
|
|
|
throw new Exception($"Profiler exception. Required font Courier New or Arial not installed to {fontFolder}");
|
|
}
|
|
|
|
public void InitalizeTextures()
|
|
{
|
|
// Create and init some vars
|
|
uint[] rawCharacterSheet = new uint[SheetWidth * SheetHeight];
|
|
int x;
|
|
int y;
|
|
int lineOffset;
|
|
int maxHeight;
|
|
|
|
x = y = lineOffset = maxHeight = 0;
|
|
characters = new CharacterInfo[94];
|
|
|
|
// Get font
|
|
var font = new FontFace(File.OpenRead(GetFontPath()));
|
|
|
|
// Update raw data for each character
|
|
for (int i = 0; i < 94; i++)
|
|
{
|
|
var surface = RenderSurface((char)(i + 33), font, out var xBearing, out var yBearing, out var advance);
|
|
|
|
characters[i] = UpdateTexture(surface, ref rawCharacterSheet, ref x, ref y, ref lineOffset);
|
|
characters[i].BearingX = xBearing;
|
|
characters[i].BearingY = yBearing;
|
|
characters[i].Advance = advance;
|
|
|
|
if (maxHeight < characters[i].Height)
|
|
maxHeight = (int)characters[i].Height;
|
|
}
|
|
|
|
// Fix height for characters shorter than line height
|
|
for (int i = 0; i < 94; i++)
|
|
{
|
|
characters[i].BearingX /= characters[i].Width;
|
|
characters[i].BearingY /= maxHeight;
|
|
characters[i].Advance /= characters[i].Width;
|
|
characters[i].Height /= maxHeight;
|
|
characters[i].AspectRatio = (float)characters[i].Width / maxHeight;
|
|
}
|
|
|
|
// Convert raw data into texture
|
|
CharacterTextureSheet = GL.GenTexture();
|
|
GL.BindTexture(TextureTarget.Texture2D, CharacterTextureSheet);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
|
|
|
|
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, SheetWidth, SheetHeight, 0, PixelFormat.Rgba, PixelType.UnsignedInt8888, rawCharacterSheet);
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, 0);
|
|
}
|
|
|
|
public void UpdateScreenHeight(int height)
|
|
{
|
|
ScreenHeight = height;
|
|
}
|
|
|
|
public float DrawText(string text, float x, float y, float height, bool draw = true)
|
|
{
|
|
float originalX = x;
|
|
|
|
// Skip out of bounds draw
|
|
if (y < height * -2 || y > ScreenHeight + height * 2)
|
|
{
|
|
draw = false;
|
|
}
|
|
|
|
if (draw)
|
|
{
|
|
// Use font map texture
|
|
GL.BindTexture(TextureTarget.Texture2D, CharacterTextureSheet);
|
|
|
|
// Enable blending and textures
|
|
GL.Enable(EnableCap.Texture2D);
|
|
GL.Enable(EnableCap.Blend);
|
|
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
|
|
|
|
// Draw all characters
|
|
GL.Begin(PrimitiveType.Triangles);
|
|
GL.Color4(fontColor);
|
|
}
|
|
|
|
for (int i = 0; i < text.Length; i++)
|
|
{
|
|
if (text[i] == ' ')
|
|
{
|
|
x += height / 4;
|
|
continue;
|
|
}
|
|
|
|
CharacterInfo charInfo = characters[text[i] - 33];
|
|
float width = (charInfo.AspectRatio * height);
|
|
x += (charInfo.BearingX * charInfo.AspectRatio) * width;
|
|
float right = x + width;
|
|
if (draw)
|
|
{
|
|
DrawChar(charInfo, x, right, y + height * (charInfo.Height - charInfo.BearingY), y - height * charInfo.BearingY);
|
|
}
|
|
x = right + charInfo.Advance * charInfo.AspectRatio + 1;
|
|
}
|
|
|
|
if (draw)
|
|
{
|
|
GL.End();
|
|
|
|
// Cleanup for caller
|
|
GL.BindTexture(TextureTarget.Texture2D, 0);
|
|
GL.Disable(EnableCap.Texture2D);
|
|
GL.Disable(EnableCap.Blend);
|
|
}
|
|
|
|
// Return width of rendered text
|
|
return x - originalX;
|
|
}
|
|
|
|
private void DrawChar(CharacterInfo charInfo, float left, float right, float top, float bottom)
|
|
{
|
|
GL.TexCoord2(charInfo.Left, charInfo.Bottom); GL.Vertex2(left, bottom);
|
|
GL.TexCoord2(charInfo.Left, charInfo.Top); GL.Vertex2(left, top);
|
|
GL.TexCoord2(charInfo.Right, charInfo.Top); GL.Vertex2(right, top);
|
|
|
|
GL.TexCoord2(charInfo.Right, charInfo.Top); GL.Vertex2(right, top);
|
|
GL.TexCoord2(charInfo.Right, charInfo.Bottom); GL.Vertex2(right, bottom);
|
|
GL.TexCoord2(charInfo.Left, charInfo.Bottom); GL.Vertex2(left, bottom);
|
|
}
|
|
|
|
public unsafe Surface RenderSurface(char c, FontFace font, out float xBearing, out float yBearing, out float advance)
|
|
{
|
|
var glyph = font.GetGlyph(c, 64);
|
|
xBearing = glyph.HorizontalMetrics.Bearing.X;
|
|
yBearing = glyph.RenderHeight - glyph.HorizontalMetrics.Bearing.Y;
|
|
advance = glyph.HorizontalMetrics.Advance;
|
|
|
|
var surface = new Surface
|
|
{
|
|
Bits = Marshal.AllocHGlobal(glyph.RenderWidth * glyph.RenderHeight),
|
|
Width = glyph.RenderWidth,
|
|
Height = glyph.RenderHeight,
|
|
Pitch = glyph.RenderWidth
|
|
};
|
|
|
|
var stuff = (byte*)surface.Bits;
|
|
for (int i = 0; i < surface.Width * surface.Height; i++)
|
|
*stuff++ = 0;
|
|
|
|
glyph.RenderTo(surface);
|
|
|
|
return surface;
|
|
}
|
|
|
|
private CharacterInfo UpdateTexture(Surface surface, ref uint[] rawCharMap, ref int posX, ref int posY, ref int lineOffset)
|
|
{
|
|
int width = surface.Width;
|
|
int height = surface.Height;
|
|
int len = width * height;
|
|
byte[] data = new byte[len];
|
|
|
|
// Get character bitmap
|
|
Marshal.Copy(surface.Bits, data, 0, len);
|
|
|
|
// Find a slot
|
|
if (posX + width > SheetWidth)
|
|
{
|
|
posX = 0;
|
|
posY += lineOffset;
|
|
lineOffset = 0;
|
|
}
|
|
|
|
// Update lineoffset
|
|
if (lineOffset < height)
|
|
{
|
|
lineOffset = height + 1;
|
|
}
|
|
|
|
// Copy char to sheet
|
|
for (int y = 0; y < height; y++)
|
|
{
|
|
int destOffset = (y + posY) * SheetWidth + posX;
|
|
int sourceOffset = y * width;
|
|
|
|
for (int x = 0; x < width; x++)
|
|
{
|
|
rawCharMap[destOffset + x] = (uint)((0xFFFFFF << 8) | data[sourceOffset + x]);
|
|
}
|
|
}
|
|
|
|
// Generate character info
|
|
CharacterInfo charInfo = new CharacterInfo()
|
|
{
|
|
Left = (float)posX / SheetWidth,
|
|
Right = (float)(posX + width) / SheetWidth,
|
|
Top = (float)(posY - 1) / SheetHeight,
|
|
Bottom = (float)(posY + height) / SheetHeight,
|
|
Width = width,
|
|
Height = height,
|
|
};
|
|
|
|
// Update x
|
|
posX += width + 1;
|
|
|
|
// Give the memory back
|
|
Marshal.FreeHGlobal(surface.Bits);
|
|
return charInfo;
|
|
}
|
|
}
|
|
} |