Jump to content
  • Advertisement
Sign in to follow this  
metsfan

A shader program for each UI element?

This topic is 2330 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm building a custom UI engine for my game, and I'm wondering how I want to build the renderer for each control. Each control can have several attributes such as a background color, a background image, as well as an infinite number of subcontrols. I am planning to have controls such as text input fields, labels, buttons, image views, checkboxes, ect, all the usuals.

The way I see it is that there's 2 ways I can go about rendering them:

1) Each control has its own shader program and is completely responsible for rendering itself.
2) There is a main UI renderer class that holds on to a single shader program or a shader program for each type of element, and renders all elements at once in a single shader.

I'm leaning towards option 1 right because it just seems simpler from a programming perspective, but I'm concerned about performance. If there are 50+ UI elements on the screen, all of different types, this means 50+ active shader programs that my graphics API needs to keep track of, on top of the shader programs for each mesh. So my question to the forum, in your experience, which method makes the most sense? Is there another method I'm simply not seeing?

Thanks.

P.S. Don't try to talk me out of making my own UI engine, cause I'm doing it smile.png

Share this post


Link to post
Share on other sites
Advertisement
Your priority should be to reduce state changes and number of draw calls in any given frame. In light of that, I lean more toward option 2. You probably shouldn't be changing shader state for every control or element. UI elements tend to be perfect candidates for batching into a single draw call, since you don't have to worry about culling or view-dependent draw order. UI elements tend to be simple alpha-blended draw anyway, so in most cases a single shader for all UI elements may be sufficient, and all the data (background color, background image, etc...) are just data passed to the shader. If you do need special effects (glows, particles, animations, etc...) these can all be done in a special pass, but the bulk of your elements probably won't need them.

Share this post


Link to post
Share on other sites
1 shader program for every UI element seems a bit over the top to me. Keep in mind that these programs are loaded into RAM and too much will cause problems. UI is pretty much just 2D images, so 1 or 2 shaders for everything seems decent enough.

Share this post


Link to post
Share on other sites
Thank for the responses guys, I will be doing option 2, as that seems to be the better one.

Share this post


Link to post
Share on other sites

1) Each control has its own shader program and is completely responsible for rendering itself.
2) There is a main UI renderer class that holds on to a single shader program or a shader program for each type of element, and renders all elements at once in a single shader.


I would recommend using something along option 2 as well. My solution for this would be to create a shader-factory (which may be the main UI renderer itself) from which the UI-Elements can obtain the shaders. The shader factory has a use-count on the shaders so they can be garbage collected. If a UI-Element wants to have its own very special shader it can use it, if it doesn't, use the default-shader. In my experience UI-Elements tend to have very similar shading methods with only parameter-values being different.

Share this post


Link to post
Share on other sites
Well, I don't know about there being a "main UI renderer class that holds on to a single shader program," but in general, you only need a single shader that can apply to all UI elements...and probably most other 2D objects as well. The key is to make a shader that can be somewhat generically parameterized through its uniforms.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!