Add quadIndexBuffer to Renderer

This commit is contained in:
Gustas
2023-08-28 11:14:02 +03:00
committed by Matthias Mailänder
parent 0b90622251
commit 2763e1502b
6 changed files with 29 additions and 21 deletions

View File

@@ -26,7 +26,7 @@ namespace OpenRA
public interface IPlatform 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); ISoundEngine CreateSound(string device);
IFont CreateFont(byte[] data); IFont CreateFont(byte[] data);
} }

View File

@@ -36,7 +36,7 @@ namespace OpenRA.Graphics
{ {
this.renderer = renderer; this.renderer = renderer;
this.shader = shader; this.shader = shader;
vertices = renderer.Context.CreateVertices(renderer.TempBufferSize); vertices = renderer.Context.CreateVertices(renderer.TempVertexBufferSize);
} }
public void Flush() public void Flush()
@@ -65,7 +65,7 @@ namespace OpenRA.Graphics
{ {
renderer.CurrentBatchRenderer = this; renderer.CurrentBatchRenderer = this;
if (s.BlendMode != currentBlend || nv + 6 > renderer.TempBufferSize) if (s.BlendMode != currentBlend || nv + 6 > renderer.TempVertexBufferSize)
Flush(); Flush();
currentBlend = s.BlendMode; currentBlend = s.BlendMode;
@@ -202,7 +202,7 @@ namespace OpenRA.Graphics
{ {
renderer.CurrentBatchRenderer = this; renderer.CurrentBatchRenderer = this;
if (currentBlend != blendMode || nv + v.Length > renderer.TempBufferSize) if (currentBlend != blendMode || nv + v.Length > renderer.TempVertexBufferSize)
Flush(); Flush();
currentBlend = blendMode; currentBlend = blendMode;

View File

@@ -41,9 +41,11 @@ namespace OpenRA
internal IGraphicsContext Context { get; } internal IGraphicsContext Context { get; }
internal int SheetSize { get; } internal int SheetSize { get; }
internal int TempBufferSize { get; } internal int TempVertexBufferSize { get; }
internal int TempIndexBufferSize { get; }
readonly IVertexBuffer<Vertex> tempBuffer; readonly IVertexBuffer<Vertex> tempVertexBuffer;
readonly IIndexBuffer quadIndexBuffer;
readonly Stack<Rectangle> scissorState = new(); readonly Stack<Rectangle> scissorState = new();
IFrameBuffer screenBuffer; IFrameBuffer screenBuffer;
@@ -75,13 +77,15 @@ namespace OpenRA
this.platform = platform; this.platform = platform;
var resolution = GetResolution(graphicSettings); var resolution = GetResolution(graphicSettings);
TempVertexBufferSize = graphicSettings.BatchSize - graphicSettings.BatchSize % 4;
TempIndexBufferSize = TempVertexBufferSize / 4 * 6;
Window = platform.CreateWindow(new Size(resolution.Width, resolution.Height), 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); graphicSettings.VideoDisplay, graphicSettings.GLProfile, !graphicSettings.DisableLegacyGL);
Context = Window.Context; Context = Window.Context;
TempBufferSize = graphicSettings.BatchSize;
SheetSize = graphicSettings.SheetSize; SheetSize = graphicSettings.SheetSize;
WorldSpriteRenderer = new SpriteRenderer(this, Context.CreateShader("combined")); WorldSpriteRenderer = new SpriteRenderer(this, Context.CreateShader("combined"));
@@ -92,7 +96,8 @@ namespace OpenRA
RgbaSpriteRenderer = new RgbaSpriteRenderer(SpriteRenderer); RgbaSpriteRenderer = new RgbaSpriteRenderer(SpriteRenderer);
RgbaColorRenderer = new RgbaColorRenderer(SpriteRenderer); RgbaColorRenderer = new RgbaColorRenderer(SpriteRenderer);
tempBuffer = Context.CreateVertexBuffer(TempBufferSize); tempVertexBuffer = Context.CreateVertexBuffer(TempVertexBufferSize);
quadIndexBuffer = Context.CreateQuadIndexBuffer(TempIndexBufferSize);
} }
static Size GetResolution(GraphicSettings graphicsSettings) static Size GetResolution(GraphicSettings graphicsSettings)
@@ -513,7 +518,8 @@ namespace OpenRA
public void Dispose() public void Dispose()
{ {
WorldModelRenderer.Dispose(); WorldModelRenderer.Dispose();
tempBuffer.Dispose(); tempVertexBuffer.Dispose();
quadIndexBuffer.Dispose();
fontSheetBuilder?.Dispose(); fontSheetBuilder?.Dispose();
if (Fonts != null) if (Fonts != null)
foreach (var font in Fonts.Values) foreach (var font in Fonts.Values)

View File

@@ -16,9 +16,9 @@ namespace OpenRA.Platforms.Default
{ {
public class DefaultPlatform : IPlatform 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) public ISoundEngine CreateSound(string device)

View File

@@ -133,7 +133,7 @@ namespace OpenRA.Platforms.Default
static extern IntPtr XFlush(IntPtr display); static extern IntPtr XFlush(IntPtr display);
public Sdl2PlatformWindow(Size requestEffectiveWindowSize, WindowMode windowMode, 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 the Window/Surface properties until initialization is complete
lock (syncObject) lock (syncObject)
@@ -348,7 +348,7 @@ namespace OpenRA.Platforms.Default
Context = ctx; Context = ctx;
} }
else else
Context = new ThreadedGraphicsContext(new Sdl2GraphicsContext(this), batchSize); Context = new ThreadedGraphicsContext(new Sdl2GraphicsContext(this), vertexBatchSize, indexBatchSize);
Context.SetVSyncEnabled(Game.Settings.Graphics.VSync); Context.SetVSyncEnabled(Game.Settings.Graphics.VSync);

View File

@@ -29,7 +29,8 @@ namespace OpenRA.Platforms.Default
readonly Stack<Message> messagePool = new(); readonly Stack<Message> messagePool = new();
readonly Queue<Message> messages = new(); readonly Queue<Message> messages = new();
public readonly int BatchSize; public readonly int VertexBatchSize;
public readonly int IndexBatchSize;
readonly object syncObject = new(); readonly object syncObject = new();
readonly Thread renderThread; readonly Thread renderThread;
volatile ExceptionDispatchInfo messageException; volatile ExceptionDispatchInfo messageException;
@@ -53,9 +54,10 @@ namespace OpenRA.Platforms.Default
Action<object> doSetBlendMode; Action<object> doSetBlendMode;
Action<object> doSetVSync; Action<object> 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) renderThread = new Thread(RenderThread)
{ {
Name = "ThreadedGraphicsContext RenderThread", Name = "ThreadedGraphicsContext RenderThread",
@@ -151,15 +153,15 @@ namespace OpenRA.Platforms.Default
internal Vertex[] GetVertices(int size) internal Vertex[] GetVertices(int size)
{ {
lock (verticesPool) lock (verticesPool)
if (size <= BatchSize && verticesPool.Count > 0) if (size <= VertexBatchSize && verticesPool.Count > 0)
return verticesPool.Pop(); return verticesPool.Pop();
return new Vertex[size < BatchSize ? BatchSize : size]; return new Vertex[size < VertexBatchSize ? VertexBatchSize : size];
} }
internal void ReturnVertices(Vertex[] vertices) internal void ReturnVertices(Vertex[] vertices)
{ {
if (vertices.Length == BatchSize) if (vertices.Length == VertexBatchSize)
lock (verticesPool) lock (verticesPool)
verticesPool.Push(vertices); verticesPool.Push(vertices);
} }
@@ -562,7 +564,7 @@ namespace OpenRA.Platforms.Default
public void SetData(Vertex[] vertices, int offset, int start, int length) 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. // If we are able to use a buffer without allocation, post a message to avoid blocking.
var buffer = device.GetVertices(length); var buffer = device.GetVertices(length);