corona
corona copied to clipboard
Optimization for distorted rects + "similar" objects
Distorted rects use a slight modification of the fragment shell to do their work. Unfortunately, this means that they count as having a different program than other objects, even if everything else is the same (from the renderer's point of view), so for instance this sequence will break batching at each step:
distorted rect, normal rect, distorted rect, normal rect
Those might all have the same image or use a common sheet, in which case our intuition would be that this all should batch.
The shell's distortion mod is actually a superset of the normal behavior, so after the initial switch, we can in fact just leave it in place so long as the only change in render state would be a "downgrade" of the current program.
For most path-based display objects, q
(v_TexCoordZ
, in the shell) is set to 1, the exception being distorted rects, of course, which add some fractional delta.
Box2D debugging, emitters, and particle systems each zero out their vertices and so will have q
of 0, as far as I can tell.
Dividing by zero is not usually good, so we probably don't want to let these make it to the distortion mod.
As it happens, rects use the triangle strip primitive type, whereas Box2D uses line loops and the others are triangle fans, and the renderer must break a batch to switch types. So we can piggyback on this break that has to happen anyway and demote the distortion mod to avoid issues with zeroes.
With a long enough chain of "normal rect"s, the cost of extra shader instructions will overtake what you would have incurred by switching the program. (Quite a long chain, probably.) I have some comments about a possible solution.
A lower-tech way would be to demote—as with primitive types—whenever we break a batch due to !enoughSpace
.