How to reduce the number of drawcalls when drawing outlines?

Hi everyone.

I’m implementing a new mesh highlight system for my OpenGL application: it works by overlaying an outline drawing on top of the fully rendered scene.


The problem is that the worst-case scenario consists in drawing the same scene twice, the first with lighting shaders and post-processing effects, the second is only a false color drawing for outline extraction.

This means almost doubling the number of draw calls which slows down the rendering time considerably with complex scenes ( > 5.000 individual meshes, no batching, 1 or 2 draw calls per mesh for the scene, 1 draw call per mesh for outlines).

Is there a way to leverage OpenGL features to make this process cheaper?

I was thinking about MRT, with a color output for the fully lit scene and a second one only for later outline detection. However, if I need to outline only a subset of the scene I’d need separate depth testing relative to those color outputs (a mesh may be occluded in the scene but still highlighted with outlines as you can see in the image above).

That’s not viable within the same draw call. A fragment discarded by the depth test either never executes the fragment shader or all of the results of the FS are discarded. Fragments, even when rendering to multiple render targets, are all-or-nothing. Either all render targets are written to or none are.

1 Like

Thanks for your answer.

I know, I also thought about layered rendering, however I’m afraid it would make the whole drawing routine too convoluted.

By the way, Is there anything I could do to speed up this kind of outline detection?

Can you make the base rendering of selected objects cheaper? I’m thinking when the outline is drawn on top you may not need the prettiest shading underneath. It doesn’t reduce the draw call count, but you’d do less “wasted” shading work.