Optimization for distorted rects + "similar" objects #549
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
.