A shader program for each UI element?

Started by
4 comments, last by nox_pp 11 years, 11 months ago
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
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.
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.
what
Thank for the responses guys, I will be doing option 2, as that seems to be the better one.

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.
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.

Between Scylla and Charybdis: First Look <-- The game I'm working on

Object-Oriented Programming Sucks <-- The kind of thing I say

This topic is closed to new replies.

Advertisement