Sprite Auto-Batching breaks?

The following code works ok with auto-batching and uses 2 gl calls.

 Node *root = Node::create();

for (int i=0; i< 100; i++){
    root->addChild(Sprite::create("res/square.png"));
}

root->addChild(LayerColor::create(Color4B::BLUE));
this->addChild(root);

This code also works ok with auto-batching and uses 101 gl calls, 100 for LayerColor, 1 for sprite.

Node *root = Node::create();

for (int i=0; i< 100; i++){
    root->addChild(Sprite::create("res/square.png"));
}
for (int i=0; i< 100; i++){
    root->addChild(LayerColor::create(Color4B::BLUE));
}
this->addChild(root);

return true;

However, the following does not → 200 gl calls but i would expect 101. What is going on???

Node *root = Node::create();

for (int i=0; i< 100; i++){
    root->addChild(Sprite::create("res/square.png"));
    root->addChild(LayerColor::create(Color4B::BLUE));
}

this->addChild(root);

Ok I solved my own problem ) I broke the z-order rule )

Have you considered using a 1x1 pixel texture (I use 16x16, but same thing) to create a Sprite instead of a LayerColor, and scaling it to the size you want, then calling setColor on it? If you create a sprite sheet, add square.png and the 1x1 pixel texture to it, then it may take only 1 draw call all up for as many nodes as you want to draw.

Yes as supporting LayerColor auto-batching would be much harder for me. Just a question R101, did you ever test what is more efficient in case we want to render let’s say for example 100 of 10x10, 100 of 20x20, 100 of 50x50 squares and we use your sprite technique what is best?

  1. Use 1x1 and change content size to meet each requirement.
  2. Use 1x1 and change scale size to meet each requirement.
  3. Use a 50x50 and scale down or change content size down.

No, I haven’t done any tests with the different sizes, mainly because the images I would need to use would be relatively large, so it would have cost more (in terms of memory) to use larger images and scale them down etc. The decision was made to scale up, as it made the most sense.

If you want to save on scaling all the objects, then pick a size out of the sizes you use, and supply that texture. So, for your example, use a 10x10 texture, and scale it up to 20x20 and 50x50, so then you would only be scaling 200 items instead of 300 (going off your example of drawing 100 of each size).

Regardless which you choose, you’ll be saving a lot of draw calls compared to using LayerColor.