How to pass attributes to a shader and still batch render?

I want to create glow/outline/etc. shaders for Bitmap font labels. I figured out that the best way to pass the outline width to the shader is probably via attributes since uniforms are global and it would affect all labels using the same shader at once.

Unfortunately, I couldn’t find a way how to pass attributes to the shader and still be able to batch draw. CCTrianglesCommand doesn’t seem to support attributes at all.

Is there any way to achieve this?

1 Like

In the current version of cocos2d-x (3.14) it is not possible to do that directly.

However you could try using the probably least known and used class in all of cocos: PrimitiveCommand. PrimitiveCommands allow you to supply custom vertex attribute layout and vertex data for rendering. They don’t support batching though (making them pretty much useless in your case).

(Shameless self advertising start)

I once tackled the same problem myself and mentioned it in the forums.

As a result, I created the cocos2dx-AdvancedRenderer (source here) which basically added a new command called ArbitraryVertexCommand. This new command supports arbitrary vertex attribute layout, batching and is reasonably fast.

(Self advertising end)

Hope this helps.

Regards,
Darian

3 Likes

@Darinex Wow, that advanced renderer looks pretty impressive. Have you tried to submit it as a pull request in the cocos2d-x main? It looks well worth it!

Our game goes well into 20-30k rendered vertices due to many Spine skeletons being onscreen, so according to your documentation we should greatly benefit from your Advanced Renderer.

As far as I understand this, if I want to create two Labels and render two different outline thicknesses via a shader, I’d create one material for each of these labels and set the attributes in the Material, right?
Would it then still batch render those two labels even if they use two different materials?

There’s no license note on the GitHub page, are you fine if we would use this in our game / could you add a license to the Github, so it’s clear? We’re not code stealers :wink: We’d do some benchmarking before of course to see the performance impact.

The documentations says that some vertex calculations can be done on the GPU which sounds great at first look, but it breaks batch once the model view matrix changes. But changes to this matrix are only done in special nodes right? All sprites use the same model view matrix and could benefit from this, right?

Would you be willing to do a quick check of the changes that the Cocos2d-x team did to the renderer in the past year / compare the current version to your renderer to check if it needs any updates? I will also definitely do this if I include it, but you probably know the most about your code. I remember that something on the VAO/VBO handling was changed, but most probably you’re going a completely different route here anyway.

It’s a bit shocking that you analyzed these things in-depth and even created a better version of the renderer but it’s not out there for anybody to use. I wonder how many of these things are out there that are just lost.

1 Like