Skip to content

Commit

Permalink
Revert "Run graphics rendering on a dedicated thread."
Browse files Browse the repository at this point in the history
This reverts commit b9be52c.
  • Loading branch information
pchote authored and reaperrr committed Jun 4, 2018
1 parent afc5a54 commit 6c338eb
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 875 deletions.
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/PlatformInterfaces.cs
Expand Up @@ -17,7 +17,7 @@ namespace OpenRA
{
public interface IPlatform
{
IGraphicsDevice CreateGraphics(Size size, WindowMode windowMode, int batchSize);
IGraphicsDevice CreateGraphics(Size size, WindowMode windowMode);
ISoundEngine CreateSound(string device);
}

Expand Down
11 changes: 4 additions & 7 deletions OpenRA.Game/Renderer.cs
Expand Up @@ -51,7 +51,7 @@ public Renderer(IPlatform platform, GraphicSettings graphicSettings)
{
var resolution = GetResolution(graphicSettings);

Device = platform.CreateGraphics(new Size(resolution.Width, resolution.Height), graphicSettings.Mode, graphicSettings.BatchSize);
Device = platform.CreateGraphics(new Size(resolution.Width, resolution.Height), graphicSettings.Mode);

TempBufferSize = graphicSettings.BatchSize;
SheetSize = graphicSettings.SheetSize;
Expand Down Expand Up @@ -92,11 +92,8 @@ public void InitializeFonts(ModData modData)

Device.OnWindowScaleChanged += (before, after) =>
{
Game.RunAfterTick(() =>
{
foreach (var f in Fonts)
f.Value.SetScale(after);
});
foreach (var f in Fonts)
f.Value.SetScale(after);
};
}

Expand Down Expand Up @@ -267,14 +264,14 @@ public void ReleaseWindowMouseFocus()

public void Dispose()
{
Device.Dispose();
WorldModelRenderer.Dispose();
tempBuffer.Dispose();
if (fontSheetBuilder != null)
fontSheetBuilder.Dispose();
if (Fonts != null)
foreach (var font in Fonts.Values)
font.Dispose();
Device.Dispose();
}

public string GetClipboardText()
Expand Down
8 changes: 3 additions & 5 deletions OpenRA.Platforms.Default/DefaultPlatform.cs
Expand Up @@ -10,17 +10,15 @@
#endregion

using System.Drawing;
using OpenRA;

namespace OpenRA.Platforms.Default
{
public class DefaultPlatform : IPlatform
{
public IGraphicsDevice CreateGraphics(Size size, WindowMode windowMode, int batchSize)
public IGraphicsDevice CreateGraphics(Size size, WindowMode windowMode)
{
// Run graphics rendering on a dedicated thread.
// The calling thread will then have more time to process other tasks, since rendering happens in parallel.
// If the calling thread is the main game thread, this means it can run more logic and render ticks.
return new ThreadedGraphicsDevice(new Sdl2GraphicsDevice(size, windowMode), batchSize);
return new Sdl2GraphicsDevice(size, windowMode);
}

public ISoundEngine CreateSound(string device)
Expand Down
13 changes: 9 additions & 4 deletions OpenRA.Platforms.Default/FrameBuffer.cs
Expand Up @@ -18,12 +18,12 @@ namespace OpenRA.Platforms.Default
{
sealed class FrameBuffer : ThreadAffine, IFrameBuffer
{
readonly ITexture texture;
readonly Texture texture;
readonly Size size;
uint framebuffer, depth;
bool disposed;

public FrameBuffer(Size size, ITextureInternal texture)
public FrameBuffer(Size size)
{
this.size = size;
if (!Exts.IsPowerOf2(size.Width) || !Exts.IsPowerOf2(size.Height))
Expand All @@ -35,7 +35,7 @@ public FrameBuffer(Size size, ITextureInternal texture)
OpenGL.CheckGLError();

// Color
this.texture = texture;
texture = new Texture();
texture.SetEmpty(size.Width, size.Height);
OpenGL.glFramebufferTexture2D(OpenGL.FRAMEBUFFER_EXT, OpenGL.COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, texture.ID, 0);
OpenGL.CheckGLError();
Expand Down Expand Up @@ -120,9 +120,14 @@ public ITexture Texture
}
}

~FrameBuffer()
{
Game.RunAfterTick(() => Dispose(false));
}

public void Dispose()
{
Dispose(true);
Game.RunAfterTick(() => Dispose(true));
GC.SuppressFinalize(this);
}

Expand Down
21 changes: 0 additions & 21 deletions OpenRA.Platforms.Default/IGraphicsDeviceInternal.cs

This file was deleted.

19 changes: 0 additions & 19 deletions OpenRA.Platforms.Default/ITextureInternal.cs

This file was deleted.

3 changes: 0 additions & 3 deletions OpenRA.Platforms.Default/OpenRA.Platforms.Default.csproj
Expand Up @@ -48,16 +48,13 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DefaultPlatform.cs" />
<Compile Include="IGraphicsDeviceInternal.cs" />
<Compile Include="ITextureInternal.cs" />
<Compile Include="Sdl2GraphicsDevice.cs" />
<Compile Include="Sdl2Input.cs" />
<Compile Include="Shader.cs" />
<Compile Include="FrameBuffer.cs" />
<Compile Include="MultiTapDetection.cs" />
<Compile Include="Texture.cs" />
<Compile Include="ThreadAffine.cs" />
<Compile Include="ThreadedGraphicsDevice.cs" />
<Compile Include="VertexBuffer.cs" />
<Compile Include="OpenAlSoundEngine.cs" />
<Compile Include="OpenGL.cs" />
Expand Down
33 changes: 13 additions & 20 deletions OpenRA.Platforms.Default/Sdl2GraphicsDevice.cs
Expand Up @@ -18,7 +18,7 @@

namespace OpenRA.Platforms.Default
{
sealed class Sdl2GraphicsDevice : ThreadAffine, IGraphicsDeviceInternal
sealed class Sdl2GraphicsDevice : ThreadAffine, IGraphicsDevice
{
readonly Sdl2Input input;

Expand Down Expand Up @@ -140,14 +140,6 @@ public Sdl2GraphicsDevice(Size windowSize, WindowMode windowMode)
SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
}

SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE);
input = new Sdl2Input();
}

public void InitializeOpenGL()
{
SetThreadAffinity();

context = SDL.SDL_GL_CreateContext(window);
if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window, context) < 0)
throw new InvalidOperationException("Can not create OpenGL context. (Error: {0})".F(SDL.SDL_GetError()));
Expand All @@ -160,6 +152,9 @@ public void InitializeOpenGL()
OpenGL.CheckGLError();
OpenGL.glEnableVertexAttribArray(Shader.TexMetadataAttributeIndex);
OpenGL.CheckGLError();

SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE);
input = new Sdl2Input();
}

public IHardwareCursor CreateHardwareCursor(string name, Size size, byte[] data, int2 hotspot)
Expand Down Expand Up @@ -226,9 +221,7 @@ internal void WindowSizeChanged()
SurfaceSize = new Size(width, height);
WindowScale = width * 1f / WindowSize.Width;

var onWindowScaleChanged = OnWindowScaleChanged;
if (onWindowScaleChanged != null)
onWindowScaleChanged(oldScale, WindowScale);
OnWindowScaleChanged(oldScale, WindowScale);
}
}
}
Expand Down Expand Up @@ -269,9 +262,14 @@ public SDL2HardwareCursor(Size size, byte[] data, int2 hotspot)
}
}

~SDL2HardwareCursor()
{
Game.RunAfterTick(() => Dispose(false));
}

public void Dispose()
{
Dispose(true);
Game.RunAfterTick(() => Dispose(true));
GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -492,7 +490,7 @@ public void Present()
public void PumpInput(IInputHandler inputHandler)
{
VerifyThreadAffinity();
Game.RunAfterTick(() => input.PumpInput(this, inputHandler));
input.PumpInput(this, inputHandler);
}

public string GetClipboardText()
Expand Down Expand Up @@ -526,14 +524,9 @@ public ITexture CreateTexture(Bitmap bitmap)
}

public IFrameBuffer CreateFrameBuffer(Size s)
{
return CreateFrameBuffer(s, new Texture());
}

public IFrameBuffer CreateFrameBuffer(Size s, ITextureInternal texture)
{
VerifyThreadAffinity();
return new FrameBuffer(s, texture);
return new FrameBuffer(s);
}

public IShader CreateShader(string name)
Expand Down
2 changes: 2 additions & 0 deletions OpenRA.Platforms.Default/Sdl2Input.cs
Expand Up @@ -191,6 +191,8 @@ public void PumpInput(Sdl2GraphicsDevice device, IInputHandler inputHandler)
inputHandler.OnMouseInput(pendingMotion.Value);
pendingMotion = null;
}

OpenGL.CheckGLError();
}
}
}
2 changes: 1 addition & 1 deletion OpenRA.Platforms.Default/Shader.cs
Expand Up @@ -139,7 +139,7 @@ public void PrepareRender()
foreach (var kv in textures)
{
OpenGL.glActiveTexture(OpenGL.GL_TEXTURE0 + kv.Key);
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, ((ITextureInternal)kv.Value).ID);
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, ((Texture)kv.Value).ID);
}

OpenGL.CheckGLError();
Expand Down
9 changes: 7 additions & 2 deletions OpenRA.Platforms.Default/Texture.cs
Expand Up @@ -16,7 +16,7 @@

namespace OpenRA.Platforms.Default
{
sealed class Texture : ThreadAffine, ITextureInternal
sealed class Texture : ThreadAffine, ITexture
{
uint texture;
TextureScaleFilter scaleFilter;
Expand Down Expand Up @@ -187,9 +187,14 @@ public void SetEmpty(int width, int height)
OpenGL.CheckGLError();
}

~Texture()
{
Game.RunAfterTick(() => Dispose(false));
}

public void Dispose()
{
Dispose(true);
Game.RunAfterTick(() => Dispose(true));
GC.SuppressFinalize(this);
}

Expand Down
9 changes: 2 additions & 7 deletions OpenRA.Platforms.Default/ThreadAffine.cs
Expand Up @@ -16,22 +16,17 @@ namespace OpenRA.Platforms.Default
{
abstract class ThreadAffine
{
volatile int managedThreadId;
readonly int managedThreadId;

protected ThreadAffine()
{
SetThreadAffinity();
}

protected void SetThreadAffinity()
{
managedThreadId = Thread.CurrentThread.ManagedThreadId;
}

protected void VerifyThreadAffinity()
{
if (managedThreadId != Thread.CurrentThread.ManagedThreadId)
throw new InvalidOperationException("Cross-thread operation not valid: This method must only be called from the thread that owns this object.");
throw new InvalidOperationException("Cross-thread operation not valid: This method must be called from the same thread that created this object.");
}
}
}

0 comments on commit 6c338eb

Please sign in to comment.