Immediate mode Optimization with Polygons

Avatar
  • updated
  • Answered

We are struggling to optimize Shapes performance with immediate mode drawing. In the same script we draw lines, text, rings, triangles, polylines, and filled polygons. 

My understanding of optimizing with Immediate Mode is that we should draw all objects of a single type in sequence, then move to the next type. Even when arranging my script to do so, I seem to run nowhere near as fast as when I just drop the same quantity of component shapes into the scene as game objects and let your system handle the optimization itself. 

The way I have tested performance differences:

1. Run Scene with both game objects SHAPES and Immediate Mode SHAPES script rendering the same thing

2. Turn off all component game objects, check frames

3. Turn off Immediate Mode script, check frames

Doing this yields a huge boost in frames when the Immediate Mode is shut off and almost no boost to frames when component shapes are turned off. Component shapes seem to be super fast, which is awesome! 

In my ImmediateMode script my code flow is: 

override void DrawShapes()

   using (Draw.Command())

      for (0:lines.count)

         Draw.Line()

      for (0:rings.count)

         Draw.Ring()

...etc


Is that the proper way to optimize? There are more calculations that we do inside that Draw.Command, but even when I create the simplest version as a test script I get bad performance compared to just using component shapes on game objects. 


Since you talk so much about Immediate Mode in the docs, I assume I'm doing something wrong lol. Immediate mode drawing is ideal for us because it lets us iterate faster.

Thanks so much :)

Reporting a bug? please specify Unity version:
Reporting a bug? please specify Shapes version:
Reporting a bug? please specify Render Pipeline:
Built-in render pipeline
Pinned replies
Avatar
Freya Holmér creator
  • Answer
  • Answered

first things to check:

1. make sure you have immediate mode instancing support enabled in the Shapes settings

2. make sure you're running the latest version of shapes

3. make sure the frame debugger also shows all your shapes as instanced together

4. make sure you compare performance in builds, not in the editor. Immediate mode has a lot of code in C# land, which means there's a *lot* of debugging overhead that won't show up in builds

now, if none of these things really help to the amount you expect, then there is by necessity going to be some amount of overhead of using immediate mode, mostly because component based shapes are "just" mesh renderers, part of Unity's native render pipeline, while immediate mode drawing is unfortunately more complicated (but instead more flexible) as it is non-persistent and set up in code every frame

also, the topic says "Optimization with Polygons", so if you're talking about the polygon Shape specifically - this one should be faster in immediate mode than in component mode, since polygons and polylines can't instance when using components. In addition, in versions prior to Shapes 3.2.2 polygons and polylines would reconstruct every frame, which they, shouldn't, in later versions :)

Avatar
Freya Holmér creator
  • Answer
  • Answered

first things to check:

1. make sure you have immediate mode instancing support enabled in the Shapes settings

2. make sure you're running the latest version of shapes

3. make sure the frame debugger also shows all your shapes as instanced together

4. make sure you compare performance in builds, not in the editor. Immediate mode has a lot of code in C# land, which means there's a *lot* of debugging overhead that won't show up in builds

now, if none of these things really help to the amount you expect, then there is by necessity going to be some amount of overhead of using immediate mode, mostly because component based shapes are "just" mesh renderers, part of Unity's native render pipeline, while immediate mode drawing is unfortunately more complicated (but instead more flexible) as it is non-persistent and set up in code every frame

also, the topic says "Optimization with Polygons", so if you're talking about the polygon Shape specifically - this one should be faster in immediate mode than in component mode, since polygons and polylines can't instance when using components. In addition, in versions prior to Shapes 3.2.2 polygons and polylines would reconstruct every frame, which they, shouldn't, in later versions :)