avogatro's picture

want smooth text movement

opentk 9.9.2b
OS archlinux 32bit 2.6.31
driver nvidia 190 beta

I have problem to move a textprinter with a constant speed.
Vsync is on,
and i use timedelta to control the speed.

long timestamp = Stopwatch.GetTimestamp();

long timedelta = timestamp-last_timestamp;

result is not very good, every sec it make very big movement.
speed is just not constant.

any advise?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

In my experience, large hiccups like this are usually caused by the garbage collector. Try printing the collection count (System.GC.CollectionCount) - does it increase whenever you see a hiccup?

There are two general approaches to optimize GC performance:

  1. Avoid all per-frame heap allocations. Allocate & collect everything at load time or other suitable intervals (e.g. level changes for a game), use memory pooling (dead objects return to the pool instead of being collected) and try to use the stack wherever possible (use structs instead of classes wherever possible).
  2. Allocate freely but keep the reference tree simple and shallow. Allocate arrays instead of individual objects (an array of 1000 objects has a reference complexity of 1. 1000 individual objects have a complexity of 1000). Try to avoid deep reference hierarchies.

You can pick one of the two approaches but not mix them together. Note that Mono does not employ a generational garbage collector at this time, which means that the second approach is difficult to implement - if not impossible. A new GC is expected to land on Mono 2.8 but that is at least half a year away.

In general:

  • Core OpenTK allocates absolutely no memory after startup, except in specific circumstances (e.g. changing resolutions, creating new graphics/audio contexts, windows etc).
  • The TextPrinter allocates memory for dynamic strings (TextPrinterOptions.NoCache). This is unavoidable for two reasons: (a) dynamic strings are (by definition!) dynamic (you allocate memory whenever you call object.ToString()); (b) the TextPrinter tries to work around a number of GDI+ bugs by splitting strings on newline characters. Static strings are cached and don't suffer from this issue.
  • Windows.Forms (and the GLControl) allocate memory whenever they raise an event (mouse movements are the worst offender). This is fine for your typical GUI, but it makes winforms totally unsuitable for realtime graphics (hence the NativeWindow/GameWindow classes in OpenTK).

Even with thisn limitations, I've been able to get a steady 120fps for stereoscopic rendering (where a single hiccup will cause nausea - speaking from experience!) It's not impossible to have a steady framerate - the trick is to work with the limitations in mind.