ported renderer to OpenTK #2408

Closed
wants to merge 4 commits into
from

Projects

None yet

5 participants

Owner

I started to port the renderer to http://www.opentk.com/ the successor of the Tao Framework. To my knowledge this does not fix any issues, but at least the code is more readable and does not rely on unmaintained software.

Owner

It is also possible use http://www.opentk.com/doc/opentk/gamewindow and http://www.opentk.com/doc/input instead of SDL. This might also make it FreeBSD compatible @ihptru. http://www.opentk.com/node/1382

I am not sure if http://www.opentk.com/project/text is a good replacement for Tao.FreeType though and there is nothing to replace Tao.Cg but it stays compatible with OpenTK.

Owner

http://www.opentk.com/project/QuickFont seems to be the replacement for Tao.FreeType and http://www.opentk.com/project/CgNet is an updated (to latest Nvidia Cg) version of Tao.Cg

Owner

http://cs-sdl.sourceforge.net/ is based on Tao.SDL and looks as if it is maintained. Well, at least the homepage is not 404.

Owner

I also tried to fool around with OpenTKs GameWindow. However it seems to enforce a certain structure to the code: with OnLoad, OnUpdateFrame, OnRenderFrame and OnResize http://www.opentk.com/doc/chapter/0 so this is not a simple search and replace thing therefore I have not managed to make any progress to try it out.

Owner

https://github.com/Robmaister/SharpFont seems to be an enhanced alternative to Tao.FreeType.

Owner

@Mailaender: could you have a go at switching us to SharpFont, and if it
works acceptably on all our targets we'll drop the custom Tao from our
linux packages and just use the system one.

On Fri, Nov 9, 2012 at 1:27 AM, Matthias Mailänder <notifications@github.com

wrote:

https://github.com/Robmaister/SharpFont seems to be an enhanced
alternative to Tao.FreeType.


Reply to this email directly or view it on GitHubhttps://github.com/OpenRA/OpenRA/pull/2408#issuecomment-10186189.

Owner

But no switch to OpenTK, CgNet or CS-SDL?

Owner

Let's get rid of the ugliest problem first (the crazy custom Tao.Freetype)

On Fri, Nov 9, 2012 at 9:19 AM, Matthias Mailänder <notifications@github.com

wrote:

But no switch to OpenTK, CgNet or CS-SDL?


Reply to this email directly or view it on GitHubhttps://github.com/OpenRA/OpenRA/pull/2408#issuecomment-10203235.

Contributor

Hey, I'm the author of SharpFont. As I already mentioned in my readme, you may have some issues running it as a 64-bit process on Windows 64-bit. If you guys can figure out a way to get FreeType2 to compile 64-bit with the long type being 64 bits wide, I can put together a mechanism to load the correct dll on Windows and you won't have to distribute a separate version for Windows 64-bit.

All other 64-bit OSs work fine, though. If you encounter any other bugs, report them and I'll fix them (or if you fix them, submit a pull request and I'll merge it in).

Owner

We don't run 64bit on windows anyway for other reasons (performance being
the big one).

Thanks for making contact, we'll let you know if we hit anything broken,
and hopefully throw good patches in your direction.

On Sat, Nov 10, 2012 at 5:48 AM, Robert Rouhani notifications@github.comwrote:

Hey, I'm the author of SharpFont. As I already mentioned in my readme, you
may have some issues running it as a 64-bit process on Windows 64-bit. If
you guys can figure out a way to get FreeType2 to compile 64-bit with the
long type being 64 bits wide, I can put together a mechanism to load the
correct dll on Windows and you won't have to distribute a separate version
for Windows 64-bit.

All other 64-bit OSs work fine, though. If you encounter any other bugs,
report them and I'll fix them (or if you fix them, submit a pull request
and I'll merge it in).


Reply to this email directly or view it on GitHubhttps://github.com/OpenRA/OpenRA/pull/2408#issuecomment-10236006.

Owner

I tried: https://github.com/Mailaender/OpenRA/compare/sharpfont but it does not work:

Exception of type `System.NullReferenceException`: Object reference not set to an instance of an object
  at OpenRA.Graphics.SpriteFont.CreateGlyph (Pair`2 c) [0x00118] in OpenRA/OpenRA.Game/Graphics/SpriteFont.cs:125 
  at OpenRA.FileFormats.Cache`2[OpenRA.FileFormats.Pair`2[System.Char,System.Drawing.Color],OpenRA.Graphics.GlyphInfo].get_Item (Pair`2 key) [0x00013] in OpenRA/OpenRA.FileFormats/Primitives/Cache.cs:40 
  at OpenRA.Graphics.SpriteFont.PrecacheColor (Color c) [0x00008] in OpenRA/OpenRA.Game/Graphics/SpriteFont.cs:52 
  at OpenRA.Graphics.SpriteFont..ctor (System.String name, Int32 size) [0x0007e] in OpenRA/OpenRA.Game/Graphics/SpriteFont.cs:44 
  at OpenRA.Graphics.Renderer.<InitializeFonts>m__5A (KeyValuePair`2 x) [0x00000] in OpenRA/OpenRA.Game/Graphics/Renderer.cs:71 
  at (wrapper delegate-invoke) <Module>:invoke_SpriteFont__this___KeyValuePair`2<string, Pair`2<string, int>> (System.Collections.Generic.KeyValuePair`2<string, OpenRA.FileFormats.Pair`2<string, int>>)
  at System.Linq.Enumerable.ToDictionary[KeyValuePair`2,String,SpriteFont] (IEnumerable`1 source, System.Func`2 keySelector, System.Func`2 elementSelector, IEqualityComparer`1 comparer) [0x0002f] in /home/abuild/rpmbuild/BUILD/mono-2.10.9/mcs/class/System.Core/System.Linq/Enumerable.cs:2823 
  at System.Linq.Enumerable.ToDictionary[KeyValuePair`2,String,SpriteFont] (IEnumerable`1 source, System.Func`2 keySelector, System.Func`2 elementSelector) [0x00000] in /home/abuild/rpmbuild/BUILD/mono-2.10.9/mcs/class/System.Core/System.Linq/Enumerable.cs:2810 
  at OpenRA.Graphics.Renderer.InitializeFonts (OpenRA.FileFormats.Manifest m) [0x00000] in OpenRA/OpenRA.Game/Graphics/Renderer.cs:71 
  at OpenRA.Game.InitializeWithMods (System.String[] mods) [0x0013a] in OpenRA/OpenRA.Game/Game.cs:293 
  at OpenRA.Game.Initialize (OpenRA.Arguments args) [0x00135] in OpenRA/OpenRA.Game/Game.cs:264 
  at OpenRA.Program.Run (System.String[] args) [0x00000] in OpenRA/OpenRA.Game/Support/Program.cs:89 
  at OpenRA.Program.Main (System.String[] args) [0x0002b] in OpenRA/OpenRA.Game/Support/Program.cs:36

There is also some stuff with pointers in the previous code that I don't understand. I guess the problem is related to that.

Contributor

Looks like you're just forgetting to call face.Glyph.RenderGlyph, the NullReferenceException on line 125 can only happen if p is 0 (when you try to read from memory address 0, you'll get a NullReferenceException). p would only ever be 0 if the glyph hasn't been rendered yet.

EDIT: That reminds me, it looks like I forgot to remove the "Glyph" suffix from RenderGlyph when I was converting method names...

Owner

Works, thanks. @chrisforbes: see pull request #2475

@Mailaender Mailaender closed this Dec 10, 2012
@Mailaender Mailaender reopened this Mar 1, 2014
Owner

There are some problems as we already use OpenTK hidden in https://github.com/flibitijibibo/SDL2-CS which results in conflicts when trying to do a straight port. However OpenTK comes with it's own SDL2 backend http://www.opentk.com/files/doc/namespace_open_t_k_1_1_platform_1_1_s_d_l2.html We should probably use that instead of our own horrible mix of renderer, audio and input DLLs.

Owner

Ah damn, I forgot about the fiddly dummy contexts again and also stumpled upon opentk/opentk#52.

Owner
pchote commented Mar 1, 2014

I don't really see the value in migrating from one legacy API to another legacy API. The 1.1 release gives us definite benefits, but this looks to be using their unsupported legacy release.

@pchote pchote closed this Mar 12, 2014

Hi, I'm the maintainer of OpenTK, pitching in because I saw this issue linked from opentk/opentk#52.

Commit 3e9002b switches from Tao.OpenGl (OpenGL 2.1, unsupported) to OpenTK (OpenGL 4.4, supported). Commit 86879b6 implements the Tao.Sdl - OpenTK bridge. This is a supported configuration for OpenTK, and will already yield most of the performance improvements of OpenTK 1.1.

thefiddler/opentk-perftest demonstrates several ways to use OpenTK with SDL2:

  • using OpenTK standalone (SDL2 backend)
  • using SDL2-CS standalone
  • combining OpenTK with SDL2-CS via an extern alias

Each approach has its own advantages:

  • OpenTK is faster and offers the simplest route to non-desktop platforms (OpenGL ES for Android, iOS, WinRT.) It exposes most, but not all SDL2 features.
  • SDL2-CS exposes all SDL2 features and offers the simplest upgrade path from Tao.Sdl. It is currently limited to OpenGL 3.2 (desktop-only).
  • OpenTK with SDL2-CS combines the advantages of both: up-to-date OpenGL and OpenGL ES bindings, improved performance. However, memory pressure and download sizes will be higher than with plain OpenTK.

See here for a performance comparison of the various approaches.

Feel free to ping me if you encounter any issues or need help while working with OpenTK.

Owner

Ah, interesting. I did not find https://github.com/thefiddler/opentk-perftest and https://github.com/thefiddler/opentk/tree/master/Source/Examples as well as the documentation seems to not have been updated to the new SDL 2 backend so I had trouble getting this working again.

Using the SDL2 backend for more details in OpenTK) is a matter of deploying the native SDL2.dll / .so / .dylib somewhere in the dll path. Using the default options, OpenTK will use SDL2 if available and fall back to a native backend if not (win32, x11, etc).

This is very much on purpose! Existing applications gain portability to *BSD, Wayland, Mir, (soon) WinRT without changing a single line of code - which explains why you didn't see any changes in opentk/Examples.

As far as OpenRA is concerned, you need to pick (a) a platform abstraction and (b) an OpenGL binding. Possible combinations include:

  • (a) Tao.Sdl + (b) Tao.OpenGl
  • (a) Tao.Sdl + (b) OpenTK
  • (a) SDL2-cs + (b) OpenTK
  • (a) OpenTK + (b) OpenTK

More exotic combinations are also possible (e.g. there are people using VTK + OpenTK), but these are probably not such a good fit for OpenRA.

As the developer of both Tao.OpenGl and OpenTK, I'd strongly recommend using OpenTK for (b). It is faster, better designed, more complete and uses less memory to boot. It culminates 8 years of experience in OpenGL interop.

For (a) my advice would be as follows:

  • If you rely on the 2d rendering APIs of SDL2, then use SDL2 directly.
  • Otherwise, OpenTK and SDL2 are surprisingly similar. It's a matter of whether you wish to take a hard or soft dependency on SDL2 (which matters mostly on mobile):
SDL2                OpenTK
SDL_Window          NativeWindow
SDL_GLContext       GraphicsContext
SDL_GLattr          GraphicsMode
SDL_Display         DisplayDevice
SDL_DisplayMode     DisplayResolution
SDL_GameController  GamePad
...

If only SDL2 had existed 7 years ago :)

Owner

Thanks for the advice. We also read about DirectX support via ANGLE http://www.opentk.com/news/opentk-1.1-stable-released which might be interesting because missing OpenGL support is the no. 1 newbie problem. I will definitely look into this again. We will also try to be a good downstream and support you with patches and bug reports if necessary.

Owner
pchote commented Mar 14, 2014

We had discussed moving to (a) OpenTK + (b) OpenTK in our IRC channel back before this was reopened (see also #4540). That was my main motivation behind closing this: I don't see the value in merging a half-way OpenTK port while a full port is on the cards. This should be done in one step.

Owner

Superseded by #5248

@Mailaender Mailaender deleted the Mailaender:opentk branch May 4, 2014
Owner

Gave it a try https://github.com/Mailaender/OpenRA/compare/opentk-renderer-input but there is a lot TODO.

I got OpenTK window creation and rendering to work, but only by accident when I stumbled upon http://www.opentk.com/node/1996 which suggested GameWindow.Visible = true as GameWindow.Run() won't work at all when you already got your own game loop. This seems to be documented nowhere else.

However I can't force OpenTK.Input to do anything yet even after tireless hours. https://github.com/Mailaender/OpenRA/compare/opentk-renderer-input The window never gets focus and does not listen neither mouse nor keyboard events. I also tried to hook up the GameWindow events, but they are never fired, too. You can't even close it without killing it manually.

Owner

I just noticed GameWindow.ProcessEvents() is what I need here.

Owner

https://github.com/Mailaender/OpenRA/compare/opentk-renderer-input is now almost done except:

  • clipboard
  • text input
  • some mouse events are ignored (place units at build palette)
  • adjust Makefile
  • something like SDL_SetWindowGrab
  • mouse cursors seems to lag on low FPS
Owner

(a) OpenTK + (b) OpenTK is now achieved by #6893.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment