Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryImMouse committed Jul 13, 2024
1 parent dfbf30f commit 16edf45
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 137 deletions.
6 changes: 6 additions & 0 deletions Hypercube.Client/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Client.Input.Handler;
using Hypercube.Client.Input.Manager;
using Hypercube.Client.Resources.Caching;
using Hypercube.Client.Runtimes;
using Hypercube.Client.Runtimes.Loop;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.Entities.Realisation.EventBus;
using Hypercube.Shared.Entities.Realisation.Manager;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.Resources.Caching;
using Hypercube.Shared.Resources.Manager;
using Hypercube.Shared.Timing;

Expand Down Expand Up @@ -39,6 +41,10 @@ public static void Register(DependenciesContainer rootContainer)
// Texturing
rootContainer.Register<ITextureManager, TextureManager>();

// Caching
rootContainer.Register<ICacheManager, CacheManager>();
rootContainer.Register<ICacheManagerInternal, CacheManager>();

// Camera
rootContainer.Register<ICameraManager, CameraManager>();

Expand Down
10 changes: 8 additions & 2 deletions Hypercube.Client/Entities/Systems/Sprite/SpriteSystem.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using Hypercube.Client.Graphics.Drawing;
using Hypercube.Client.Graphics.Event;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Resources.Caching;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.Entities.Realisation;
using Hypercube.Shared.Entities.Realisation.Systems;
using Hypercube.Shared.Entities.Systems.Transform;
using Hypercube.Shared.Math.Transform;
using Hypercube.Shared.Math.Vector;
using Hypercube.Shared.Resources.Caching;

namespace Hypercube.Client.Entities.Systems.Sprite;

public sealed class SpriteSystem : EntitySystem
{
[Dependency] private readonly IRenderDrawing _drawing = default!;
[Dependency] private readonly ITextureManager _textureManager = default!;
[Dependency] private readonly ICacheManager _cacheManager = default!;

public override void Initialize()
{
Expand All @@ -34,6 +36,10 @@ private void OnRenderDrawing(ref RenderDrawingEvent ev)

public void Render(Entity<SpriteComponent> entity, Transform2 transform)
{
_drawing.DrawTexture(entity.Component.TextureHandle ??= _textureManager.GetTextureHandle(entity.Component.TexturePath), Vector2.Zero, entity.Component.Color, transform.Matrix * entity.Component.Transform.Matrix);
if (entity.Component.TextureHandle == null)
entity.Component.TextureHandle =
_cacheManager.GetResource<TextureResource>(entity.Component.TexturePath).Texture ?? throw new NullReferenceException();

_drawing.DrawTexture(entity.Component.TextureHandle, Vector2.Zero, entity.Component.Color, transform.Matrix * entity.Component.Transform.Matrix);
}
}
7 changes: 5 additions & 2 deletions Hypercube.Client/Graphics/Drawing/RenderDrawing.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using Hypercube.Client.Graphics.Rendering;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Texturing.TextureSettings;
using Hypercube.Client.Resources.Caching;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.Math;
using Hypercube.Shared.Math.Box;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Math.Vector;
using Hypercube.Shared.Resources.Caching;

namespace Hypercube.Client.Graphics.Drawing;

public sealed class RenderDrawing : IRenderDrawing
{
[Dependency] private readonly ITextureManager _textureManager = default!;
[Dependency] private readonly IRenderer _renderer = default!;
[Dependency] private readonly ICacheManager _cacheManager = default!;

public void DrawTexture(ITexture texture, Vector2 position)
{
Expand All @@ -36,7 +38,8 @@ public void DrawTexture(ITexture texture, Box2 quad, Box2 uv)

public void DrawTexture(ITexture texture, Box2 quad, Box2 uv, Color color)
{
_renderer.DrawTexture(_textureManager.GetTextureHandle(texture, new Texture2DCreationSettings()), quad, uv, color);
var handle = _cacheManager.GetResource<TextureResource>(texture.Path).Texture;
_renderer.DrawTexture(handle, quad, uv, color);
}

public void DrawTexture(ITextureHandle texture, Vector2 position, Color color, Matrix4X4 model)
Expand Down
3 changes: 2 additions & 1 deletion Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hypercube.Client.Graphics.Shading;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Windows;
using Hypercube.Client.Resources.Caching;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Runtimes.Loop.Event;
using OpenToolkit.Graphics.OpenGL4;
Expand Down Expand Up @@ -33,7 +34,7 @@ public sealed partial class Renderer
private void OnLoad()
{
_baseShader = new Shader("/base", _resourceManager);
_baseTexture = _textureManager.GetTextureHandle("/icon.png");
_baseTexture = _cacheManager.GetResource<TextureResource>("/icon.png").Texture;
_baseTexture.Bind(HTexTarget.Texture2D);

_cameraManager.SetMainCamera(_cameraManager.CreateCamera2D(MainWindow.Size));
Expand Down
5 changes: 4 additions & 1 deletion Hypercube.Client/Graphics/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Hypercube.Shared.EventBus;
using Hypercube.Shared.EventBus.Events;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Resources.Caching;
using Hypercube.Shared.Resources.Manager;
using Hypercube.Shared.Runtimes.Event;
using Hypercube.Shared.Runtimes.Loop.Event;
Expand All @@ -26,6 +27,8 @@ public sealed partial class Renderer : IRenderer, IPostInject, IEventSubscriber
[Dependency] private readonly ITiming _timing = default!;
[Dependency] private readonly ICameraManager _cameraManager = default!;
[Dependency] private readonly IResourceManager _resourceManager = default!;
[Dependency] private readonly ICacheManagerInternal _cacheManagerInternal = default!;
[Dependency] private readonly ICacheManager _cacheManager = default!;

private readonly ILogger _logger = LoggingManager.GetLogger("renderer");
private readonly ILogger _loggerOpenGL = LoggingManager.GetLogger("open_gl")!;
Expand Down Expand Up @@ -117,7 +120,7 @@ private void OnStartup(ref RuntimeStartupEvent args)

InitOpenGL();

_textureManager.CacheHandles();
_cacheManagerInternal.PreloadTextures();

OnLoad();
}
Expand Down
1 change: 0 additions & 1 deletion Hypercube.Client/Graphics/Texturing/ITextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Hypercube.Client.Graphics.Texturing;
public interface ITextureManager
{
ITexture GetTexture(ResourcePath path);
void CacheHandles();

ITextureHandle GetTextureHandle(ResourcePath path, ITextureCreationSettings settings);
ITextureHandle GetTextureHandle(ResourcePath path);
Expand Down
132 changes: 2 additions & 130 deletions Hypercube.Client/Graphics/Texturing/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,24 @@
using Hypercube.Client.Graphics.Texturing.Events;
using Hypercube.Client.Graphics.Texturing.TextureSettings;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Resources;
using Hypercube.Shared.Resources.Manager;
using Hypercube.Shared.Runtimes.Event;
using StbImageSharp;

namespace Hypercube.Client.Graphics.Texturing;

public sealed class TextureManager : ITextureManager, IEventSubscriber, IPostInject
public sealed class TextureManager : ITextureManager
{
[Dependency] private readonly IEventBus _eventBus = default!;
[Dependency] private readonly IResourceManager _resourceManager = default!;

private FrozenDictionary<ResourcePath, ITextureHandle> _cachedHandles = FrozenDictionary<ResourcePath, ITextureHandle>.Empty;
private FrozenDictionary<ResourcePath, ITexture> _cachedTextures = FrozenDictionary<ResourcePath, ITexture>.Empty;

private readonly Logger _logger = LoggingManager.GetLogger("texturing");

public TextureManager()
{
StbImage.stbi_set_flip_vertically_on_load(1);
}

public void PostInject()
{
_eventBus.Subscribe<RuntimeInitializationEvent>(this, OnInitialization);
}

private void OnInitialization(ref RuntimeInitializationEvent args)
{
_logger.EngineInfo("Caching textures...");
var st = Stopwatch.StartNew();
var ev = new TexturesPreloadEvent(new HashSet<ResourcePath>());
var textures = new Dictionary<ResourcePath, ITexture>();
_eventBus.Raise(ref ev);

foreach (var texturePath in ev.Textures)
{
if (textures.ContainsKey(texturePath))
continue;

var texture = CreateTexture(texturePath);
textures.Add(texturePath, texture);
}

CacheTextures(textures);
st.Stop();
_logger.EngineInfo($"Cached {_cachedTextures.Count} textures in {st.Elapsed}");
}

public void CacheHandles()
{
_logger.EngineInfo("Caching handles...");
var st = Stopwatch.StartNew();

var ev = new HandlesPreloadEvent(new HashSet<ResourcePath>());
_eventBus.Raise(ref ev);

var handles = new Dictionary<ResourcePath, ITextureHandle>();

foreach (var handlePath in ev.Handles)
{
if (handles.ContainsKey(handlePath))
continue;

ITextureHandle handle;

if (_cachedTextures.TryGetValue(handlePath, out var texture))
{
handle = CreateTextureHandle(texture, new Texture2DCreationSettings());
handles.Add(handlePath, handle);
continue;
}

handle = CreateTextureHandle(handlePath, new Texture2DCreationSettings());
handles.Add(handlePath, handle);
}

CacheHandles(handles);
st.Stop();

_logger.EngineInfo($"Cached {_cachedHandles.Count} handles in {st.Elapsed}");
}

#region PublicAPI

Expand Down Expand Up @@ -122,13 +56,7 @@ public ITextureHandle GetTextureHandle(ITexture texture, ITextureCreationSetting

internal ITexture GetTextureInternal(ResourcePath path)
{
if (_cachedTextures.TryGetValue(path, out var value))
return value;

// fallback to low performance method
var texture = CreateTexture(path);
CacheTexture(texture);

return texture;
}

Expand All @@ -144,26 +72,7 @@ internal ITexture CreateTexture(ResourcePath path)

internal ITextureHandle GetTextureHandleInternal(ResourcePath path, ITextureCreationSettings settings)
{
if (_cachedHandles.TryGetValue(path, out var value))
return value;

ITextureHandle handle;

if (_cachedTextures.TryGetValue(path, out var texture))
{
// low performance method fallback
handle = CreateTextureHandle(texture, settings);
CacheHandle(handle);

return handle;
}

// fallback to low performance method
handle = CreateTextureHandle(path, settings);

CacheHandle(handle);

return handle;
return CreateTextureHandle(path, settings);
}

internal ITextureHandle CreateTextureHandle(ResourcePath path, ITextureCreationSettings settings)
Expand All @@ -178,43 +87,6 @@ internal ITextureHandle CreateTextureHandle(ITexture texture, ITextureCreationSe
{
return new TextureHandle(texture, settings);
}

/// <summary>
/// Extremely high impact on performance, use when you know what you're doing
/// </summary>
internal void CacheTexture(ITexture texture)
{
var cached = _cachedTextures.ToDictionary();
cached.Add(texture.Path, texture);
_cachedTextures = cached.ToFrozenDictionary();
_logger.Warning($"Cached texture with path {texture.Path} in runtime");
}

/// <summary>
/// Extremely high impact on performance, use when you know what you're doing
/// </summary>
internal void CacheHandle(ITextureHandle texture)
{
var cached = _cachedHandles.ToDictionary();
cached.Add(texture.Texture.Path, texture);
_cachedHandles = cached.ToFrozenDictionary();
_logger.Warning($"Cached handle with path {texture.Texture.Path} in runtime");
}

#endregion

#region Preloading

internal void CacheTextures(Dictionary<ResourcePath, ITexture> textures)
{
_cachedTextures = textures.ToFrozenDictionary();
}

internal void CacheHandles(Dictionary<ResourcePath, ITextureHandle> handles)
{
_cachedHandles = handles.ToFrozenDictionary();
}

#endregion

}
35 changes: 35 additions & 0 deletions Hypercube.Client/Resources/Caching/CacheManager.Preload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Diagnostics;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.Logging;

namespace Hypercube.Client.Resources.Caching;

public partial class CacheManager
{
public void PreloadTextures()
{
var logger = LoggingManager.GetLogger("cache.preload");

PreloadTextures(logger);
}
private void PreloadTextures(Logger logger)
{
var container = DependencyManager.GetContainer();
logger.EngineInfo("Preloading textures...");
var st = Stopwatch.StartNew();

var texDict = GetTypeDict<TextureResource>();

var files = _resourceManager.FindContentFiles("/Textures/")
.Where(p => !texDict.ContainsKey(p) && p.Extension == ".png")
.Select(p => new TextureResource() { Path = p});


foreach (var file in files)
{
file.Load(file.Path, container);

texDict[file.Path] = file;
}
}
}
Loading

0 comments on commit 16edf45

Please sign in to comment.