Skip to content

Commit 5031ada

Browse files
sync renderengine
1 parent 8fcde99 commit 5031ada

15 files changed

Lines changed: 529 additions & 125 deletions
0 Bytes
Loading
0 Bytes
Loading

RenderEngine/GLResourceManager.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,21 @@ public sealed class GlResourceManager : IDisposable
3737
/// </summary>
3838
private bool _disposed;
3939

40+
/// <summary>
41+
/// Gets or sets a value indicating whether [use matrices].
42+
/// </summary>
43+
/// <value>
44+
/// <c>true</c> if [use matrices]; otherwise, <c>false</c>.
45+
/// </value>
4046
public bool UseMatrices { get; set; } = true;
4147

4248
// --- Textures ---
4349

50+
/// <summary>
51+
/// The fallback texture identifier
52+
/// </summary>
53+
private int _fallbackTextureId = -1;
54+
4455
/// <summary>
4556
/// Gets the texture.
4657
/// Be careful, this can overwrite existing textures.
@@ -62,6 +73,44 @@ public int GetTexture(string filePath)
6273
return texId;
6374
}
6475

76+
/// <summary>
77+
/// Gets the fallback texture.
78+
/// </summary>
79+
/// <returns>Id of Texture, fallback, checkerboard</returns>
80+
public int GetFallbackTexture()
81+
{
82+
if (_fallbackTextureId >= 0) return _fallbackTextureId;
83+
84+
var pixels = new byte[] { 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 255 };
85+
_fallbackTextureId = GL.GenTexture();
86+
GL.BindTexture(TextureTarget.Texture2D, _fallbackTextureId);
87+
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 2, 2, 0,
88+
PixelFormat.Rgba, PixelType.UnsignedByte, pixels);
89+
90+
// 1. CLAMP TO EDGE: Stops the edges from wrapping around and bleeding colors at the borders
91+
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
92+
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
93+
94+
// 2. MIPMAPS: Keep it sharp up close, but blend it in the distance to stop the diagonal zippering
95+
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.NearestMipmapLinear);
96+
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
97+
98+
// 3. ANISOTROPIC FILTERING: The ultimate fix for viewing textures at sharp "roof" angles.
99+
// (Using hex codes safely bypasses any OpenTK version differences for this extension)
100+
GL.GetFloat((GetPName)0x84FF, out float maxAniso); // GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
101+
if (maxAniso > 0.0f)
102+
{
103+
GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)0x84FE, maxAniso); // GL_TEXTURE_MAX_ANISOTROPY_EXT
104+
}
105+
106+
// Generate the smaller mipmap versions so the MinFilter works
107+
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
108+
109+
GL.BindTexture(TextureTarget.Texture2D, 0);
110+
111+
return _fallbackTextureId;
112+
}
113+
65114
/// <summary>
66115
/// Creates the master textures.
67116
/// </summary>
@@ -270,7 +319,13 @@ public void Dispose()
270319
_textureCache.Clear();
271320
_programCache.Clear();
272321

322+
if (_fallbackTextureId >= 0)
323+
{
324+
GL.DeleteTexture(_fallbackTextureId);
325+
_fallbackTextureId = -1;
326+
}
327+
273328
GC.SuppressFinalize(this);
274329
}
275330
}
276-
}
331+
}

RenderEngine/Geometry2DBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,4 @@ internal static (float x, float y, int r, int g, int b, int a)[] BuildCircleOutl
197197
return pts;
198198
}
199199
}
200-
}
200+
}

RenderEngine/ImageToolkit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,4 @@ public int[] CompositeToBits()
187187
}
188188
}
189189
}
190-
}
190+
}

RenderEngine/Interfaces/IImageToolkit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ public interface IImageToolkit
6363
/// <param name="insertAt">The insert at.</param>
6464
void MergeLayers(LayeredImageContainer container, int[] layerIndices, int insertAt);
6565
}
66-
}
66+
}

RenderEngine/LayeredImageContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,4 @@ private static unsafe void AlphaBlend(Span<byte> baseSpan, Span<byte> overlaySpa
236236
}
237237
}
238238
}
239-
}
239+
}

RenderEngine/OpenTKHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,4 @@ internal static bool IsOpenGlCompatible(int requiredMajor = 4, int requiredMinor
295295
return _glVersion >= new Version(requiredMajor, requiredMinor);
296296
}
297297
}
298-
}
298+
}

RenderEngine/RenderBatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,4 @@ public void AddHostAction(Action action)
309309
HostActions.Add(action);
310310
}
311311
}
312-
}
312+
}

RenderEngine/RenderResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ internal static class RenderResource
3838
/// </summary>
3939
internal const string ErrorLayerSizeMismatch = "Layer size mismatch.";
4040
}
41-
}
41+
}

0 commit comments

Comments
 (0)