Why does SCNNode .flattenedClone() either crash or make performance worse?

317 Views Asked by At

I'm attempting to add around 4.5k SCNNode objects to a scene. Their geometries are one of two SCNPlanes, whose materials are rendered by CALayers (but I don't think that matters, the CALayerDelegates are only called once each, so that doesn't seem to be an issue.) Performance is terrible (< 1fps.)

With .showsStatistics enabled I see I'm doing almost 3.5k draw calls, which is my problem according to this WWDC video from 2017. So, as suggested, I add all my nodes to one parent node and call .flattenedClone() on it before adding that to the scene's root node. That gets me graphical corruption and a repeating error on my console:

2019-08-28 14:50:39.122937+0200 Breadboard[867:53252] [SceneKit] Error: C3DProgramHashCodeStoreRegisterProgramForRendererElement - index out of capacity (8192 > 8192)

(The x in "x > 8192" goes up by one every line)

If I split the nodes into two groups and call .flattenedClone() on them before adding the results to the root node the error goes away but my draw call count goes up to almost 5k!

What's going on? Why does calling .flattenedClone() on a node with around 2.5k nodes not... flatten it?

1

There are 1 best solutions below

0
On BEST ANSWER

The problem did turn out to be the CALayers, but it's subtle: if I instead render the CALayer into a UIImage (actually I do it all as a UIGraphicsBeginImageContextWithOptions(...), there's no need for an explicit CALayer), .flattenedClone() does indeed do what I expect it to do and I get close to 60fps with any number of cells. If I attach the CALayer directly to the material, the nodes don't flatten.

Maybe it's something to do with the fact a CALayer is animatable? In any case, with static UIImage it works. This question gave me the hint by mentioning some cases where flattening is automatic, and some where it doesn't happen at all even if you invoke it.