diff --git a/OpenRA.Game/Graphics/PlatformInterfaces.cs b/OpenRA.Game/Graphics/PlatformInterfaces.cs index a000036bab..f8c897ac6e 100644 --- a/OpenRA.Game/Graphics/PlatformInterfaces.cs +++ b/OpenRA.Game/Graphics/PlatformInterfaces.cs @@ -26,7 +26,7 @@ namespace OpenRA public interface IPlatform { - IPlatformWindow CreateWindow(Size size, WindowMode windowMode, float scaleModifier, int batchSize, int videoDisplay, GLProfile profile, bool enableLegacyGL); + IPlatformWindow CreateWindow(Size size, WindowMode windowMode, float scaleModifier, int vertexBatchSize, int indexBatchSize, int videoDisplay, GLProfile profile, bool enableLegacyGL); ISoundEngine CreateSound(string device); IFont CreateFont(byte[] data); } diff --git a/OpenRA.Game/Graphics/SpriteRenderer.cs b/OpenRA.Game/Graphics/SpriteRenderer.cs index 83977b3a92..4819a5570e 100644 --- a/OpenRA.Game/Graphics/SpriteRenderer.cs +++ b/OpenRA.Game/Graphics/SpriteRenderer.cs @@ -36,7 +36,7 @@ namespace OpenRA.Graphics { this.renderer = renderer; this.shader = shader; - vertices = renderer.Context.CreateVertices(renderer.TempBufferSize); + vertices = renderer.Context.CreateVertices(renderer.TempVertexBufferSize); } public void Flush() @@ -65,7 +65,7 @@ namespace OpenRA.Graphics { renderer.CurrentBatchRenderer = this; - if (s.BlendMode != currentBlend || nv + 6 > renderer.TempBufferSize) + if (s.BlendMode != currentBlend || nv + 6 > renderer.TempVertexBufferSize) Flush(); currentBlend = s.BlendMode; @@ -202,7 +202,7 @@ namespace OpenRA.Graphics { renderer.CurrentBatchRenderer = this; - if (currentBlend != blendMode || nv + v.Length > renderer.TempBufferSize) + if (currentBlend != blendMode || nv + v.Length > renderer.TempVertexBufferSize) Flush(); currentBlend = blendMode; diff --git a/OpenRA.Game/Renderer.cs b/OpenRA.Game/Renderer.cs index 2873261db2..16bf8ba77b 100644 --- a/OpenRA.Game/Renderer.cs +++ b/OpenRA.Game/Renderer.cs @@ -41,9 +41,11 @@ namespace OpenRA internal IGraphicsContext Context { get; } internal int SheetSize { get; } - internal int TempBufferSize { get; } + internal int TempVertexBufferSize { get; } + internal int TempIndexBufferSize { get; } - readonly IVertexBuffer tempBuffer; + readonly IVertexBuffer tempVertexBuffer; + readonly IIndexBuffer quadIndexBuffer; readonly Stack scissorState = new(); IFrameBuffer screenBuffer; @@ -75,13 +77,15 @@ namespace OpenRA this.platform = platform; var resolution = GetResolution(graphicSettings); + TempVertexBufferSize = graphicSettings.BatchSize - graphicSettings.BatchSize % 4; + TempIndexBufferSize = TempVertexBufferSize / 4 * 6; + Window = platform.CreateWindow(new Size(resolution.Width, resolution.Height), - graphicSettings.Mode, graphicSettings.UIScale, graphicSettings.BatchSize, + graphicSettings.Mode, graphicSettings.UIScale, TempVertexBufferSize, TempIndexBufferSize, graphicSettings.VideoDisplay, graphicSettings.GLProfile, !graphicSettings.DisableLegacyGL); Context = Window.Context; - TempBufferSize = graphicSettings.BatchSize; SheetSize = graphicSettings.SheetSize; WorldSpriteRenderer = new SpriteRenderer(this, Context.CreateShader("combined")); @@ -92,7 +96,8 @@ namespace OpenRA RgbaSpriteRenderer = new RgbaSpriteRenderer(SpriteRenderer); RgbaColorRenderer = new RgbaColorRenderer(SpriteRenderer); - tempBuffer = Context.CreateVertexBuffer(TempBufferSize); + tempVertexBuffer = Context.CreateVertexBuffer(TempVertexBufferSize); + quadIndexBuffer = Context.CreateQuadIndexBuffer(TempIndexBufferSize); } static Size GetResolution(GraphicSettings graphicsSettings) @@ -513,7 +518,8 @@ namespace OpenRA public void Dispose() { WorldModelRenderer.Dispose(); - tempBuffer.Dispose(); + tempVertexBuffer.Dispose(); + quadIndexBuffer.Dispose(); fontSheetBuilder?.Dispose(); if (Fonts != null) foreach (var font in Fonts.Values) diff --git a/OpenRA.Platforms.Default/DefaultPlatform.cs b/OpenRA.Platforms.Default/DefaultPlatform.cs index 13264a2eac..472705f24f 100644 --- a/OpenRA.Platforms.Default/DefaultPlatform.cs +++ b/OpenRA.Platforms.Default/DefaultPlatform.cs @@ -16,9 +16,9 @@ namespace OpenRA.Platforms.Default { public class DefaultPlatform : IPlatform { - public IPlatformWindow CreateWindow(Size size, WindowMode windowMode, float scaleModifier, int batchSize, int videoDisplay, GLProfile profile, bool enableLegacyGL) + public IPlatformWindow CreateWindow(Size size, WindowMode windowMode, float scaleModifier, int vertexBatchSize, int indexBatchSize, int videoDisplay, GLProfile profile, bool enableLegacyGL) { - return new Sdl2PlatformWindow(size, windowMode, scaleModifier, batchSize, videoDisplay, profile, enableLegacyGL); + return new Sdl2PlatformWindow(size, windowMode, scaleModifier, vertexBatchSize, indexBatchSize, videoDisplay, profile, enableLegacyGL); } public ISoundEngine CreateSound(string device) diff --git a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs index cb8420668b..99b2fe62a1 100644 --- a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs +++ b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs @@ -133,7 +133,7 @@ namespace OpenRA.Platforms.Default static extern IntPtr XFlush(IntPtr display); public Sdl2PlatformWindow(Size requestEffectiveWindowSize, WindowMode windowMode, - float scaleModifier, int batchSize, int videoDisplay, GLProfile requestProfile, bool enableLegacyGL) + float scaleModifier, int vertexBatchSize, int indexBatchSize, int videoDisplay, GLProfile requestProfile, bool enableLegacyGL) { // Lock the Window/Surface properties until initialization is complete lock (syncObject) @@ -348,7 +348,7 @@ namespace OpenRA.Platforms.Default Context = ctx; } else - Context = new ThreadedGraphicsContext(new Sdl2GraphicsContext(this), batchSize); + Context = new ThreadedGraphicsContext(new Sdl2GraphicsContext(this), vertexBatchSize, indexBatchSize); Context.SetVSyncEnabled(Game.Settings.Graphics.VSync); diff --git a/OpenRA.Platforms.Default/ThreadedGraphicsContext.cs b/OpenRA.Platforms.Default/ThreadedGraphicsContext.cs index a402cd52e6..6d45a7a1ef 100644 --- a/OpenRA.Platforms.Default/ThreadedGraphicsContext.cs +++ b/OpenRA.Platforms.Default/ThreadedGraphicsContext.cs @@ -29,7 +29,8 @@ namespace OpenRA.Platforms.Default readonly Stack messagePool = new(); readonly Queue messages = new(); - public readonly int BatchSize; + public readonly int VertexBatchSize; + public readonly int IndexBatchSize; readonly object syncObject = new(); readonly Thread renderThread; volatile ExceptionDispatchInfo messageException; @@ -53,9 +54,10 @@ namespace OpenRA.Platforms.Default Action doSetBlendMode; Action doSetVSync; - public ThreadedGraphicsContext(Sdl2GraphicsContext context, int batchSize) + public ThreadedGraphicsContext(Sdl2GraphicsContext context, int vertexBatchSize, int indexBatchSize) { - BatchSize = batchSize; + VertexBatchSize = vertexBatchSize; + IndexBatchSize = indexBatchSize; renderThread = new Thread(RenderThread) { Name = "ThreadedGraphicsContext RenderThread", @@ -151,15 +153,15 @@ namespace OpenRA.Platforms.Default internal Vertex[] GetVertices(int size) { lock (verticesPool) - if (size <= BatchSize && verticesPool.Count > 0) + if (size <= VertexBatchSize && verticesPool.Count > 0) return verticesPool.Pop(); - return new Vertex[size < BatchSize ? BatchSize : size]; + return new Vertex[size < VertexBatchSize ? VertexBatchSize : size]; } internal void ReturnVertices(Vertex[] vertices) { - if (vertices.Length == BatchSize) + if (vertices.Length == VertexBatchSize) lock (verticesPool) verticesPool.Push(vertices); } @@ -562,7 +564,7 @@ namespace OpenRA.Platforms.Default public void SetData(Vertex[] vertices, int offset, int start, int length) { - if (length <= device.BatchSize) + if (length <= device.VertexBatchSize) { // If we are able to use a buffer without allocation, post a message to avoid blocking. var buffer = device.GetVertices(length);