Skip to content

Commit

Permalink
Halve the renderer's allocation rate (#1621)
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit authored Jan 17, 2024
1 parent d169f4c commit 545e3af
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 249 deletions.
3 changes: 2 additions & 1 deletion OpenDreamClient/DreamClientSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal sealed class DreamClientSystem : EntitySystem {
[Dependency] private readonly IDreamInterfaceManager _interfaceManager = default!;
[Dependency] private readonly IOverlayManager _overlayManager = default!;
[Dependency] private readonly TransformSystem _transformSystem = default!;
[Dependency] private readonly MapSystem _mapSystem = default!;
[Dependency] private readonly EntityLookupSystem _lookupSystem = default!;
[Dependency] private readonly ClientAppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly ClientScreenOverlaySystem _screenOverlaySystem = default!;
Expand All @@ -18,7 +19,7 @@ internal sealed class DreamClientSystem : EntitySystem {
public override void Initialize() {
SubscribeLocalEvent<LocalPlayerAttachedEvent>(OnPlayerAttached);

var mapOverlay = new DreamViewOverlay(_transformSystem, _lookupSystem, _appearanceSystem, _screenOverlaySystem, _clientImagesSystem);
var mapOverlay = new DreamViewOverlay(_transformSystem, _mapSystem, _lookupSystem, _appearanceSystem, _screenOverlaySystem, _clientImagesSystem);
_overlayManager.AddOverlay(mapOverlay);
}

Expand Down
52 changes: 38 additions & 14 deletions OpenDreamClient/Rendering/DreamPlane.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Robust.Client.Graphics;
using OpenDreamShared.Dream;
using Robust.Client.Graphics;
using Robust.Shared.Utility;

namespace OpenDreamClient.Rendering;
Expand All @@ -7,15 +8,13 @@ internal sealed class DreamPlane(IRenderTexture mainRenderTarget) : IDisposable
public IRenderTexture RenderTarget => _temporaryRenderTarget ?? mainRenderTarget;
public RendererMetaData? Master;

public readonly List<Action<Vector2i>> IconDrawActions = new();
public readonly List<Action<Vector2i>> MouseMapDrawActions = new();
public readonly List<RendererMetaData> Sprites = new();

private IRenderTexture? _temporaryRenderTarget;

public void Clear() {
Master = null;
IconDrawActions.Clear();
MouseMapDrawActions.Clear();
Sprites.Clear();
_temporaryRenderTarget = null;
}

Expand Down Expand Up @@ -44,30 +43,55 @@ public void SetTemporaryRenderTarget(IRenderTexture renderTarget) {
/// <summary>
/// Clears this plane's render target, then draws all the plane's icons onto it
/// </summary>
public void Draw(DreamViewOverlay overlay, DrawingHandleWorld handle) {
public void Draw(DreamViewOverlay overlay, DrawingHandleWorld handle, Box2 worldAABB) {
// Draw all icons
handle.RenderInRenderTarget(mainRenderTarget, () => {
foreach (Action<Vector2i> iconAction in IconDrawActions)
iconAction(mainRenderTarget.Size);
foreach (var sprite in Sprites) {
if (sprite.HasRenderSource && overlay.RenderSourceLookup.TryGetValue(sprite.RenderSource!, out var renderSourceTexture)) {
sprite.TextureOverride = renderSourceTexture.Texture;
overlay.DrawIcon(handle, mainRenderTarget.Size, sprite, (-worldAABB.BottomLeft)-(worldAABB.Size/2)+new Vector2(0.5f,0.5f));
} else {
overlay.DrawIcon(handle, mainRenderTarget.Size, sprite, -worldAABB.BottomLeft);
}
}
}, new Color());

if (_temporaryRenderTarget != null) {
// Draw again, but with the color applied
handle.RenderInRenderTarget(_temporaryRenderTarget, () => {
handle.UseShader(overlay.GetBlendAndColorShader(Master, useOverlayMode: true));
handle.SetTransform(overlay.CreateRenderTargetFlipMatrix(_temporaryRenderTarget.Size, Vector2.Zero));
handle.SetTransform(DreamViewOverlay.CreateRenderTargetFlipMatrix(_temporaryRenderTarget.Size, Vector2.Zero));
handle.DrawTextureRect(mainRenderTarget.Texture, new Box2(Vector2.Zero, mainRenderTarget.Size));
handle.SetTransform(Matrix3.Identity);
handle.UseShader(null);
}, new Color());
}
}

/// <summary>
/// Draws this plane's mouse map onto the current render target
/// </summary>
public void DrawMouseMap(Vector2i renderTargetSize) {
foreach (Action<Vector2i> mouseMapAction in MouseMapDrawActions)
mouseMapAction(renderTargetSize);
public void DrawMouseMap(DrawingHandleWorld handle, DreamViewOverlay overlay, Vector2i renderTargetSize, Box2 worldAABB) {
handle.UseShader(overlay.BlockColorInstance);
foreach (var sprite in Sprites) {
if (sprite.MouseOpacity == MouseOpacity.Transparent || sprite.ShouldPassMouse)
continue;

var texture = sprite.Texture;
if (texture == null)
continue;

var pos = (sprite.Position - worldAABB.BottomLeft) * EyeManager.PixelsPerMeter;
if (sprite.TextureOverride != null)
pos -= sprite.TextureOverride.Size / 2 - new Vector2(EyeManager.PixelsPerMeter, EyeManager.PixelsPerMeter) / 2;

int hash = sprite.GetHashCode();
var colorR = (byte)(hash & 0xFF);
var colorG = (byte)((hash >> 8) & 0xFF);
var colorB = (byte)((hash >> 16) & 0xFF);
Color targetColor = new Color(colorR, colorG, colorB); //TODO - this could result in mis-clicks due to hash-collision since we ditch a whole byte.
overlay.MouseMapLookup[targetColor] = sprite;

handle.SetTransform(DreamViewOverlay.CreateRenderTargetFlipMatrix(renderTargetSize, pos));
handle.DrawTextureRect(texture, new Box2(Vector2.Zero, texture.Size), targetColor);
}
}
}
Loading

0 comments on commit 545e3af

Please sign in to comment.