Skip to content

Commit

Permalink
Introduce Renderer.WorldBufferSnapshot().
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote authored and PunkPun committed Dec 15, 2023
1 parent 6a86a99 commit 6c56ea4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
10 changes: 1 addition & 9 deletions OpenRA.Game/Graphics/WorldRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public sealed class WorldRenderer : IDisposable
readonly List<IRenderable> renderablesBuffer = new();
readonly IRenderer[] renderers;
readonly IRenderPostProcessPass[] postProcessPasses;
readonly ITexture postProcessTexture;

internal WorldRenderer(ModData modData, World world)
{
Expand Down Expand Up @@ -75,8 +74,6 @@ internal WorldRenderer(ModData modData, World world)
debugVis = Exts.Lazy(() => world.WorldActor.TraitOrDefault<DebugVisualizations>());

postProcessPasses = world.WorldActor.TraitsImplementing<IRenderPostProcessPass>().ToArray();
if (postProcessPasses.Length > 0)
postProcessTexture = Game.Renderer.Context.CreateTexture();
}

public void BeginFrame()
Expand Down Expand Up @@ -323,18 +320,13 @@ public void Draw()

void ApplyPostProcessing(PostProcessPassType type)
{
var size = Game.Renderer.WorldFrameBufferSize;
var rect = new Rectangle(0, 0, size.Width, size.Height);
foreach (var pass in postProcessPasses)
{
if (pass.Type != type || !pass.Enabled)
continue;

// Make a copy of the world texture to avoid reading and writing on the same buffer
Game.Renderer.Flush();
postProcessTexture.SetDataFromReadBuffer(rect);
Game.Renderer.Flush();
pass.Draw(this, postProcessTexture);
pass.Draw(this);
}
}

Expand Down
12 changes: 12 additions & 0 deletions OpenRA.Game/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum RenderType { None, World, UI }
readonly IVertexBuffer<Vertex> tempVertexBuffer;
readonly IIndexBuffer quadIndexBuffer;
readonly Stack<Rectangle> scissorState = new();
readonly ITexture worldBufferSnapshot;

IFrameBuffer screenBuffer;
Sprite screenSprite;
Expand All @@ -60,6 +61,15 @@ enum RenderType { None, World, UI }
public Size WorldFrameBufferSize => worldSheet.Size;
public int WorldDownscaleFactor { get; private set; } = 1;

/// <summary>
/// Copies and returns the currently rendered world state as a temporary texture.
/// </summary>
public ITexture WorldBufferSnapshot()
{
worldBufferSnapshot.SetDataFromReadBuffer(new Rectangle(int2.Zero, worldSheet.Size));
return worldBufferSnapshot;
}

SheetBuilder fontSheetBuilder;
readonly IPlatform platform;

Expand Down Expand Up @@ -98,6 +108,7 @@ public Renderer(IPlatform platform, GraphicSettings graphicSettings)

tempVertexBuffer = Context.CreateVertexBuffer<Vertex>(TempVertexBufferSize);
quadIndexBuffer = Context.CreateIndexBuffer(Util.CreateQuadIndices(TempIndexBufferSize / 6));
worldBufferSnapshot = Context.CreateTexture();
}

static Size GetResolution(GraphicSettings graphicsSettings)
Expand Down Expand Up @@ -525,6 +536,7 @@ public void Dispose()
{
worldBuffer.Dispose();
screenBuffer.Dispose();
worldBufferSnapshot.Dispose();
tempVertexBuffer.Dispose();
quadIndexBuffer.Dispose();
fontSheetBuilder?.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Traits/TraitsInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public interface IRenderPostProcessPass
{
PostProcessPassType Type { get; }
bool Enabled { get; }
void Draw(WorldRenderer wr, ITexture worldTexture);
void Draw(WorldRenderer wr);
}

[Flags]
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Cnc/Traits/World/ChronoVortexRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void DrawVortex(float3 pos, int frame)
PostProcessPassType IRenderPostProcessPass.Type => PostProcessPassType.AfterWorld;
bool IRenderPostProcessPass.Enabled => vortices.Count > 0;

void IRenderPostProcessPass.Draw(WorldRenderer wr, ITexture worldTexture)
void IRenderPostProcessPass.Draw(WorldRenderer wr)
{
var scroll = wr.Viewport.TopLeft;
var size = renderer.WorldFrameBufferSize;
Expand All @@ -94,7 +94,7 @@ void IRenderPostProcessPass.Draw(WorldRenderer wr, ITexture worldTexture)
shader.SetVec("Scroll", scroll.X, scroll.Y);
shader.SetVec("p1", width, height);
shader.SetVec("p2", -1, -1);
shader.SetTexture("WorldTexture", worldTexture);
shader.SetTexture("WorldTexture", Game.Renderer.WorldBufferSnapshot());
shader.SetTexture("VortexTexture", vortexSheet.GetTexture());
shader.PrepareRender();
foreach (var (pos, frame) in vortices)
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Traits/World/RenderPostProcessPassBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ protected RenderPostProcessPassBase(string name, PostProcessPassType type)

PostProcessPassType IRenderPostProcessPass.Type => type;
bool IRenderPostProcessPass.Enabled => Enabled;
void IRenderPostProcessPass.Draw(WorldRenderer wr, ITexture worldTexture)
void IRenderPostProcessPass.Draw(WorldRenderer wr)
{
shader.SetTexture("WorldTexture", worldTexture);
shader.SetTexture("WorldTexture", Game.Renderer.WorldBufferSnapshot());
PrepareRender(wr, shader);
shader.PrepareRender();
renderer.DrawBatch(buffer, shader, 0, 6, PrimitiveType.TriangleList);
Expand Down

0 comments on commit 6c56ea4

Please sign in to comment.