-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathMonoRenderer.Texture.cs
More file actions
198 lines (172 loc) · 6.55 KB
/
Copy pathMonoRenderer.Texture.cs
File metadata and controls
198 lines (172 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Intersect.Client.Framework.Graphics;
using Intersect.Core;
using Intersect.Framework.Reflection;
using Microsoft.Extensions.Logging;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Intersect.Client.MonoGame.Graphics;
internal partial class MonoRenderer
{
private readonly ConcurrentDictionary<Texture2D, IGameTexture> _allocatedTextures = [];
private long _allocatedTexturesSize;
private bool AddAllocatedTexture(Texture2D platformTexture, IGameTexture gameTexture)
{
if (!_allocatedTextures.TryAdd(platformTexture, gameTexture))
{
return false;
}
_allocatedTexturesSize += MeasureDataSize(platformTexture);
return true;
}
private bool RemoveAllocatedTexture(Texture2D platformTexture, [NotNullWhen(true)] out IGameTexture? gameTexture)
{
if (!_allocatedTextures.TryRemove(platformTexture, out gameTexture))
{
return false;
}
_allocatedTexturesSize -= MeasureDataSize(platformTexture);
return true;
}
private static long MeasureDataSize(Texture2D platformTexture)
{
var width = platformTexture.Width;
var height = platformTexture.Height;
return width * height * 4;
// We don't currently use mipmaps but this may become relevant in the future:
// internal static int CalculateMipLevels(int width, int height = 0, int depth = 0)
// {
// int mipLevels = 1;
// int num = Math.Max(Math.Max(width, height), depth);
// while (num > 1)
// {
// num /= 2;
// ++mipLevels;
// }
// return mipLevels;
// }
}
private Texture2D? CreatePlatformTextureFromStream(MonoTexture gameTexture, Stream stream)
{
var platformTexture = Texture2D.FromStream(_graphicsDevice, stream);
if (platformTexture is null)
{
return platformTexture;
}
platformTexture.Disposing += Texture2DOnDisposing;
if (!AddAllocatedTexture(platformTexture, gameTexture))
{
throw new InvalidOperationException("Failed to record allocated texture");
}
return platformTexture;
}
private void Texture2DOnDisposing(object? sender, EventArgs args)
{
if (sender is Texture2D platformTexture)
{
OnPlatformTextureDisposal(platformTexture);
return;
}
ApplicationContext.CurrentContext.Logger.LogError(
"Received disposal event but it was not from an instance of {ExpectedType} ({ActualType})",
typeof(Texture2D).GetName(qualified: true),
sender?.GetType().GetName(qualified: true) ?? "null"
);
}
private IGameTexture CreateGameTextureFromPlatformTexture(string assetName, Texture2D platformTexture)
{
var gameTexture = new MonoTexture(this, assetName, platformTexture);
return gameTexture;
}
protected override IGameTexture CreateGameTextureFromAtlasReference(string assetName, AtlasReference atlasReference)
{
var gameTexture = new MonoTexture(this, assetName, atlasReference);
return gameTexture;
}
public override IGameTexture CreateTextureFromStreamFactory(string assetName, Func<Stream> streamFactory)
{
var gameTexture = new MonoTexture(this, assetName, streamFactory);
return gameTexture;
}
private void OnPlatformTextureDisposal(Texture2D platformTexture)
{
if (RemoveAllocatedTexture(platformTexture, out var gameTexture))
{
MarkFreed(gameTexture);
}
else
{
ApplicationContext.CurrentContext.Logger.LogError(
"Failed to remove platform texture from allocations, is it not tracked? '{TextureName}'",
platformTexture.ToString()
);
}
}
private class MonoTexture : GameTexture<Texture2D, MonoRenderer>
{
internal MonoTexture(
MonoRenderer renderer,
string? name,
Texture2D platformTexture
) : base(renderer: renderer, name: name, platformTexture: platformTexture)
{
}
internal MonoTexture(
MonoRenderer renderer,
string name,
Func<Stream> streamFactory,
bool pinned = false
) : base(renderer: renderer, name: name, streamFactory: streamFactory, pinned: pinned)
{
}
internal MonoTexture(
MonoRenderer renderer,
string name,
AtlasReference atlasReference
) : base(renderer: renderer, name: name, atlasReference: atlasReference)
{
}
public override int Width => AtlasReference?.IsRotated == true
? AtlasReference.Bounds.Height
: AtlasReference?.Bounds.Width ?? PlatformTexture?.Width ?? 0;
public override int Height => AtlasReference?.IsRotated == true
? AtlasReference.Bounds.Width
: AtlasReference?.Bounds.Height ?? PlatformTexture?.Height ?? 0;
protected override Texture2D? CreatePlatformTextureFromStream(Stream stream)
{
var platformTexture = Renderer.CreatePlatformTextureFromStream(this, stream);
if (platformTexture is { IsDisposed: false })
{
platformTexture.Disposing += (_, _) => PlatformTexture = null;
}
return platformTexture;
}
public override Color GetPixel(int x, int y)
{
if (PlatformTexture is not { } platformTexture)
{
return Color.White;
}
if (AtlasReference is not null)
{
if (AtlasReference.IsRotated)
{
var z = x;
x = AtlasReference.Bounds.Right - y - AtlasReference.Bounds.Height;
y = AtlasReference.Bounds.Top + z;
}
else
{
x += AtlasReference.Bounds.X;
y += AtlasReference.Bounds.Y;
}
}
var buffer = new Microsoft.Xna.Framework.Color[1];
var pixelBounds = new Rectangle(x, y, 1, 1);
platformTexture.GetData(level: 0, rect: pixelBounds, data: buffer, startIndex: 0, elementCount: 1);
var pixelColor = buffer[0];
return new Color(pixelColor.A, pixelColor.R, pixelColor.G, pixelColor.B);
}
}
}