Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using one shader with multiple sets of uniforms  (Read 1125 times)

0 Members and 1 Guest are viewing this topic.

VolatileParticleDev

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Using one shader with multiple sets of uniforms
« on: March 26, 2023, 12:53:26 pm »
I want to draw multiple drawables with the same fragment shader, but with different uniform values. I see two ways of achieving this:

1. Loading the shader once, and drawing all drawables with the same shader, but setting uniforms between each draw call.

2. Loading multiple instances of the same shader, and drawing every drawable with a separate shader instance, removing the need for constant updating of the uniforms.

Which of these are recommended, or is there another, better way to do it?

From my testing, solution 2 gives better performance overall, but has the disadvantage of causing a lag spike whenever I introduce a new drawable. It seems shader::loadFromFile is (understandably) quite slow, but since Shader instances are non-copyable, I don't see how I can avoid loading from file each time I want a fresh instance.

For context: I expect to draw between 1 and 20 drawables with the same shader.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Using one shader with multiple sets of uniforms
« Reply #1 on: March 27, 2023, 09:06:21 am »
You could keep the shader text in memory, so you don't need a slow roundtrip to the disk. Not sure how much of the lag comes from disk access though.

As for the options, I don't know enough of the inner workings to comment on any :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/