1
\$\begingroup\$

I have coded a game in OpenGL and for rendering I used a Sprite Batching technique to draw many sprites in a single call to glDrawElements(). But as soon as I wanted to use a different shader for a different state of the character, I noticed that I couldn't do it.

As I understood, all the necessary data is entered into the shader at the moment of renderer initialization and when I call Flush function, the renderer doesn't care which shader I use for drawing, all the data is already entered into the main one.

Will I have to give up with Sprite Batching, or is there a way to use several shaders at once?

\$\endgroup\$
3
  • \$\begingroup\$ In some cases, we can create an "ubershader" — a single shader program that can exhibit multiple different behaviours based on the input data — that combines your different rendering effects into one. Then you can draw all your sprites in one batch with the ubershader, using data stored in the vertex data or a parallel instance data buffer to control which sprites trigger which behaviour paths in the shader. To show you how to do that for this case, we'd need more detail about the sprites and effects you're drawing: ideally with image and code samples. \$\endgroup\$ Commented Jul 23, 2024 at 10:48
  • \$\begingroup\$ @DMGregory isn't it easier to rewrite renderer system to support multiple shader programs. I understand with sprite batching to start applying different shaders to different sprites will not work. So in most 2d engines this system is not used? \$\endgroup\$ Commented Jul 23, 2024 at 11:52
  • \$\begingroup\$ We're not dealing with a limitation of the renderer software, but a fundamental aspect of how the GPU hardware works. Each draw call binds a single shader program to use for processing all geometry submitted with that call. Even cutting edge GPU-driven renderers have this restriction, they just reduce the communication with the CPU needed to orchestrate the passes. 2D engines absolutely use sprite batching extensively, they just put work into sorting content to set up the largest batches they can with minimal state changes between them, sometimes using ubershaders as one tool to do so. \$\endgroup\$ Commented Jul 23, 2024 at 12:58

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.