Render world via an intermediate FrameBuffer.

This commit is contained in:
Paul Chote
2019-11-03 17:57:36 +00:00
committed by reaperrr
parent 0c8a47b5af
commit 327866ffc3
4 changed files with 91 additions and 36 deletions

View File

@@ -683,7 +683,7 @@ namespace OpenRA
// Use worldRenderer.World instead of OrderManager.World to avoid a rendering mismatch while processing orders // Use worldRenderer.World instead of OrderManager.World to avoid a rendering mismatch while processing orders
if (worldRenderer != null && !worldRenderer.World.IsLoadingGameSave) if (worldRenderer != null && !worldRenderer.World.IsLoadingGameSave)
{ {
Renderer.BeginWorld(worldRenderer.Viewport.TopLeft, worldRenderer.Viewport.Zoom); Renderer.BeginWorld(worldRenderer.Viewport.Rectangle);
Sound.SetListenerPosition(worldRenderer.Viewport.CenterPosition); Sound.SetListenerPosition(worldRenderer.Viewport.CenterPosition);
worldRenderer.Draw(); worldRenderer.Draw();
} }
@@ -691,6 +691,10 @@ namespace OpenRA
using (new PerfSample("render_widgets")) using (new PerfSample("render_widgets"))
{ {
Renderer.BeginUI(); Renderer.BeginUI();
if (worldRenderer != null && !worldRenderer.World.IsLoadingGameSave)
worldRenderer.DrawAnnotations();
Ui.Draw(); Ui.Draw();
if (ModData != null && ModData.CursorProvider != null) if (ModData != null && ModData.CursorProvider != null)

View File

@@ -46,6 +46,7 @@ namespace OpenRA.Graphics
public WPos CenterPosition { get { return worldRenderer.ProjectedPosition(CenterLocation); } } public WPos CenterPosition { get { return worldRenderer.ProjectedPosition(CenterLocation); } }
public Rectangle Rectangle { get { return new Rectangle(TopLeft, new Size(viewportSize.X, viewportSize.Y)); } }
public int2 TopLeft { get { return CenterLocation - viewportSize / 2; } } public int2 TopLeft { get { return CenterLocation - viewportSize / 2; } }
public int2 BottomRight { get { return CenterLocation + viewportSize / 2; } } public int2 BottomRight { get { return CenterLocation + viewportSize / 2; } }
int2 viewportSize; int2 viewportSize;
@@ -240,7 +241,6 @@ namespace OpenRA.Graphics
} }
// Rectangle (in viewport coords) that contains things to be drawn // Rectangle (in viewport coords) that contains things to be drawn
static readonly Rectangle ScreenClip = Rectangle.FromLTRB(0, 0, Game.Renderer.Resolution.Width, Game.Renderer.Resolution.Height);
public Rectangle GetScissorBounds(bool insideBounds) public Rectangle GetScissorBounds(bool insideBounds)
{ {
// Visible rectangle in world coordinates (expanded to the corners of the cells) // Visible rectangle in world coordinates (expanded to the corners of the cells)
@@ -250,12 +250,12 @@ namespace OpenRA.Graphics
var cbr = map.CenterOfCell(((MPos)bounds.BottomRight).ToCPos(map)) + new WVec(512, 512, 0); var cbr = map.CenterOfCell(((MPos)bounds.BottomRight).ToCPos(map)) + new WVec(512, 512, 0);
// Convert to screen coordinates // Convert to screen coordinates
var tl = WorldToViewPx(worldRenderer.ScreenPxPosition(ctl - new WVec(0, 0, ctl.Z))).Clamp(ScreenClip); var tl = worldRenderer.ScreenPxPosition(ctl - new WVec(0, 0, ctl.Z)) - TopLeft;
var br = WorldToViewPx(worldRenderer.ScreenPxPosition(cbr - new WVec(0, 0, cbr.Z))).Clamp(ScreenClip); var br = worldRenderer.ScreenPxPosition(cbr - new WVec(0, 0, cbr.Z)) - TopLeft;
// Add an extra one cell fudge in each direction for safety // Add an extra half-cell fudge to avoid clipping isometric tiles
return Rectangle.FromLTRB(tl.X - tileSize.Width, tl.Y - tileSize.Height, return Rectangle.FromLTRB(tl.X - tileSize.Width / 2, tl.Y - tileSize.Height / 2,
br.X + tileSize.Width, br.Y + tileSize.Height); br.X + tileSize.Width / 2, br.Y + tileSize.Height / 2);
} }
ProjectedCellRegion CalculateVisibleCells(bool insideBounds) ProjectedCellRegion CalculateVisibleCells(bool insideBounds)

View File

@@ -248,10 +248,14 @@ namespace OpenRA.Graphics
r.Render(this); r.Render(this);
Game.Renderer.Flush(); Game.Renderer.Flush();
}
public void DrawAnnotations()
{
for (var i = 0; i < preparedAnnotationRenderables.Count; i++) for (var i = 0; i < preparedAnnotationRenderables.Count; i++)
preparedAnnotationRenderables[i].Render(this); preparedAnnotationRenderables[i].Render(this);
// Engine debugging overlays
if (debugVis.Value != null && debugVis.Value.RenderGeometry) if (debugVis.Value != null && debugVis.Value.RenderGeometry)
{ {
for (var i = 0; i < preparedRenderables.Count; i++) for (var i = 0; i < preparedRenderables.Count; i++)
@@ -282,6 +286,7 @@ namespace OpenRA.Graphics
} }
Game.Renderer.Flush(); Game.Renderer.Flush();
preparedRenderables.Clear(); preparedRenderables.Clear();
preparedOverlayRenderables.Clear(); preparedOverlayRenderables.Clear();
preparedAnnotationRenderables.Clear(); preparedAnnotationRenderables.Clear();

View File

@@ -45,15 +45,18 @@ namespace OpenRA
IFrameBuffer screenBuffer; IFrameBuffer screenBuffer;
Sprite screenSprite; Sprite screenSprite;
IFrameBuffer worldBuffer;
Sprite worldSprite;
SheetBuilder fontSheetBuilder; SheetBuilder fontSheetBuilder;
readonly IPlatform platform; readonly IPlatform platform;
float depthScale; float depthMargin;
float depthOffset;
Size lastBufferSize = new Size(-1, -1); Size lastBufferSize = new Size(-1, -1);
int2 lastScroll = new int2(-1, -1);
float lastZoom = -1f; Size lastWorldBufferSize = new Size(-1, -1);
Rectangle lastWorldViewport = Rectangle.Empty;
ITexture currentPaletteTexture; ITexture currentPaletteTexture;
IBatchRenderer currentBatchRenderer; IBatchRenderer currentBatchRenderer;
RenderType renderType = RenderType.None; RenderType renderType = RenderType.None;
@@ -122,10 +125,7 @@ namespace OpenRA
// - a small margin so that tiles rendered partially above the top edge of the screen aren't pushed behind the clip plane // - a small margin so that tiles rendered partially above the top edge of the screen aren't pushed behind the clip plane
// We need an offset of mapGrid.MaximumTerrainHeight * mapGrid.TileSize.Height / 2 to cover the terrain height // We need an offset of mapGrid.MaximumTerrainHeight * mapGrid.TileSize.Height / 2 to cover the terrain height
// and choose to use mapGrid.MaximumTerrainHeight * mapGrid.TileSize.Height / 4 for each of the actor and top-edge cases // and choose to use mapGrid.MaximumTerrainHeight * mapGrid.TileSize.Height / 4 for each of the actor and top-edge cases
depthScale = mapGrid == null || !mapGrid.EnableDepthBuffer ? 0 : depthMargin = mapGrid == null || !mapGrid.EnableDepthBuffer ? 0 : mapGrid.TileSize.Height * mapGrid.MaximumTerrainHeight;
(float)Resolution.Height / (Resolution.Height + mapGrid.TileSize.Height * mapGrid.MaximumTerrainHeight);
depthOffset = depthScale / 2;
} }
void BeginFrame() void BeginFrame()
@@ -153,8 +153,6 @@ namespace OpenRA
screenSprite = new Sprite(screenSheet, screenBounds, TextureChannel.RGBA); screenSprite = new Sprite(screenSheet, screenBounds, TextureChannel.RGBA);
} }
screenBuffer.Bind();
// In HiDPI windows we follow Apple's convention of defining window coordinates as for standard resolution windows // In HiDPI windows we follow Apple's convention of defining window coordinates as for standard resolution windows
// but to have a higher resolution backing surface with more than 1 texture pixel per viewport pixel. // but to have a higher resolution backing surface with more than 1 texture pixel per viewport pixel.
// We must convert the surface buffer size to a viewport size - in general this is NOT just the window size // We must convert the surface buffer size to a viewport size - in general this is NOT just the window size
@@ -168,25 +166,39 @@ namespace OpenRA
} }
} }
public void BeginWorld(int2 scroll, float zoom) public void BeginWorld(Rectangle worldViewport)
{ {
if (renderType != RenderType.None) if (renderType != RenderType.None)
throw new InvalidOperationException("BeginWorld called with renderType = {0}, expected RenderType.None.".F(renderType)); throw new InvalidOperationException("BeginWorld called with renderType = {0}, expected RenderType.None.".F(renderType));
var oldLastBufferSize = lastBufferSize;
BeginFrame(); BeginFrame();
var scale = Window.WindowScale; var worldBufferSize = worldViewport.Size.NextPowerOf2();
var surfaceSize = Window.SurfaceSize; if (worldSprite == null || worldSprite.Sheet.Size != worldBufferSize)
var surfaceBufferSize = surfaceSize.NextPowerOf2();
var bufferSize = new Size((int)(surfaceBufferSize.Width / scale), (int)(surfaceBufferSize.Height / scale));
if (oldLastBufferSize != bufferSize || lastScroll != scroll || lastZoom != zoom)
{ {
WorldSpriteRenderer.SetViewportParams(bufferSize, depthScale, depthOffset, zoom, scroll); if (worldBuffer != null)
WorldModelRenderer.SetViewportParams(bufferSize, zoom, scroll); worldBuffer.Dispose();
lastScroll = scroll; // Render the world into a framebuffer at 1:1 scaling to allow the depth buffer to match the artwork at all zoom levels
lastZoom = zoom; worldBuffer = Context.CreateFrameBuffer(worldBufferSize);
}
if (worldSprite == null || worldViewport.Size != worldSprite.Bounds.Size)
{
var worldSheet = new Sheet(SheetType.BGRA, worldBuffer.Texture);
worldSprite = new Sprite(worldSheet, new Rectangle(int2.Zero, worldViewport.Size), TextureChannel.RGBA);
}
worldBuffer.Bind();
if (worldBufferSize != lastWorldBufferSize || lastWorldViewport != worldViewport)
{
var depthScale = worldBufferSize.Height / (worldBufferSize.Height + depthMargin);
WorldSpriteRenderer.SetViewportParams(worldBufferSize, depthScale, depthScale / 2, 1f, worldViewport.Location);
WorldModelRenderer.SetViewportParams(worldBufferSize, 1f, worldViewport.Location);
lastWorldViewport = worldViewport;
lastWorldBufferSize = worldBufferSize;
} }
renderType = RenderType.World; renderType = RenderType.World;
@@ -194,8 +206,26 @@ namespace OpenRA
public void BeginUI() public void BeginUI()
{ {
if (renderType == RenderType.None) if (renderType == RenderType.World)
{
// Complete world rendering
Flush();
worldBuffer.Unbind();
// Render the world buffer into the UI buffer
screenBuffer.Bind();
var scale = Window.WindowScale;
var bufferSize = new Size((int)(screenSprite.Bounds.Width / scale), (int)(-screenSprite.Bounds.Height / scale));
RgbaSpriteRenderer.DrawSprite(worldSprite, float3.Zero, new float2(bufferSize));
Flush();
}
else
{
// World rendering was skipped
BeginFrame(); BeginFrame();
screenBuffer.Bind();
}
renderType = RenderType.UI; renderType = RenderType.UI;
} }
@@ -222,7 +252,7 @@ namespace OpenRA
screenBuffer.Unbind(); screenBuffer.Unbind();
// Render the compositor buffer to the screen // Render the compositor buffers to the screen
// HACK / PERF: Fudge the coordinates to cover the actual window while keeping the buffer viewport parameters // HACK / PERF: Fudge the coordinates to cover the actual window while keeping the buffer viewport parameters
// This saves us two redundant (and expensive) SetViewportParams each frame // This saves us two redundant (and expensive) SetViewportParams each frame
RgbaSpriteRenderer.DrawSprite(screenSprite, new float3(0, lastBufferSize.Height, 0), new float3(lastBufferSize.Width, -lastBufferSize.Height, 0)); RgbaSpriteRenderer.DrawSprite(screenSprite, new float3(0, lastBufferSize.Height, 0), new float3(lastBufferSize.Width, -lastBufferSize.Height, 0));
@@ -288,7 +318,12 @@ namespace OpenRA
rect = Rectangle.Intersect(rect, scissorState.Peek()); rect = Rectangle.Intersect(rect, scissorState.Peek());
Flush(); Flush();
Context.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height);
if (renderType == RenderType.World)
worldBuffer.EnableScissor(rect);
else
Context.EnableScissor(rect.X, rect.Y, rect.Width, rect.Height);
scissorState.Push(rect); scissorState.Push(rect);
} }
@@ -297,14 +332,25 @@ namespace OpenRA
scissorState.Pop(); scissorState.Pop();
Flush(); Flush();
// Restore previous scissor rect if (renderType == RenderType.World)
if (scissorState.Any())
{ {
var rect = scissorState.Peek(); // Restore previous scissor rect
Context.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height); if (scissorState.Any())
worldBuffer.EnableScissor(scissorState.Peek());
else
worldBuffer.DisableScissor();
} }
else else
Context.DisableScissor(); {
// Restore previous scissor rect
if (scissorState.Any())
{
var rect = scissorState.Peek();
Context.EnableScissor(rect.X, rect.Y, rect.Width, rect.Height);
}
else
Context.DisableScissor();
}
} }
public void EnableDepthBuffer() public void EnableDepthBuffer()