Jump to content

  • Log In with Google      Sign In   
  • Create Account


A shader program for each UI element?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 metsfan   Members   -  Reputation: 654

Like
0Likes
Like

Posted 03 June 2012 - 08:17 AM

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 Posted Image

Sponsor:

#2 JTippetts   Moderators   -  Reputation: 8352

Like
1Likes
Like

Posted 03 June 2012 - 09:26 AM

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.

#3 incertia   Crossbones+   -  Reputation: 777

Like
1Likes
Like

Posted 03 June 2012 - 01:03 PM

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

#4 metsfan   Members   -  Reputation: 654

Like
0Likes
Like

Posted 03 June 2012 - 02:38 PM

Thank for the responses guys, I will be doing option 2, as that seems to be the better one.

#5 doeme   Members   -  Reputation: 696

Like
0Likes
Like

Posted 04 June 2012 - 01:22 AM

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.

#6 nox_pp   Members   -  Reputation: 490

Like
0Likes
Like

Posted 04 June 2012 - 07:56 AM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS