Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (688)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Managing Multiple Shader Programs  (Read 641 times)
0 Members and 1 Guest are viewing this topic.
Offline DavidW

Junior Devvie

Medals: 3
Exp: 7 years

« Posted 2014-01-19 01:04:26 »

Hey all,

I'm getting to the point where I'm starting to have several shaders (so far up to five) and am wondering how some of you start managing these.  So far my system is less than elegant and I'm sure there is a cleaner, more OO way of doing this.  Here are a few of my ideas:

1) My current system is to have one Shader class which takes source code Strings as inputs in the constructor. I then have the main game class take care of compiling, binding, passing uniforms, and everything else.  This is working fine for a small number of shaders but I can see it'll be way too disorganized later.

2) Have the Drawable objects (Drawable is an interface) themselves keep track of which shader to use when being drawn.  This sounds like a nice solution but it'll mean that each drawable object will need references to things like the current camera and projective matrix which doesn't seem like good object oriented practice.

3) Have a ShaderManager object which takes care of uniforms and binding and have a public static method getShader() or something which drawable gives drawable objects access.  Again, doesn't seem like very good OO practice since only Drawable classes actually need access to these shaders.  I could pass a reference to the ShaderManager to each Drawable but I don't really like that either.

4) Something else???

Any advice would be greatly appreciated!  Looking forward to a good discussion.  Smiley


Online theagentd

« JGO Bitwise Duke »

Medals: 597
Projects: 4
Exp: 8 years

« Reply #1 - Posted 2014-01-19 02:01:54 »

The easiest thing to do is simply to use the first one and reduce the number of shaders. For example, my meshes require 4 textures: diffuse, normal, emissive and specular+gloss. These 4 are all optional. In addition to this, I also have both skinned (skeleton animated) meshes and static unanimated meshes, which need different vertex shaders. In total I have 2^5 = 32 unique combinations of fragment and vertex shaders (shader programs) for my meshes. Managing them would be horrific and performance would probably also suffer from to the frequent shader switching. Instead, you can simply do some minor unnecessary work in your shader and avoid the permutations. I only have one fragment shader which assumes that my mesh has all 4 textures. If I don't have a diffuse map, I simply bind a 1x1 white texture. No normal map? Just bind a 1x1 "pass-through" normal map and do the bump mapping calculations anyway since it hardly makes a difference. Skinning however is quite expensive, so I do create two shaders, one for skinned meshes and one for static meshes, for a grand total of 2 shader programs.

Offline DavidW

Junior Devvie

Medals: 3
Exp: 7 years

« Reply #2 - Posted 2014-01-19 04:43:53 »

Thanks for your advice.  I see what you mean about juts sending in pass through data for info which isn't being used.  That could help out.  Right now I have the following shader programs: One for general static meshes. One for drawing billboards, the creation of which was chronicled in my previous thread.  One for drawing the ground which gets special treatment since it can have shadows drawn on it.  One for skinned meshes (which I am working on now).  And one specifically for drawing text and the HUD info, which I think could combine with the billboard shader if I wanted to.  I'm not sure how many shaders I'm going to want to use so far, but I am planning on using them for some other specialized effects.  All in all it seems like the first option is a little too messy once you get more than 2 or 3 shaders going, but perhaps there are ways I can simplify things and use fewer...

Thanks a bunch for your quick response!  Grin

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Dwinin (73 views)
2015-11-07 13:29:08

Rems19 (81 views)
2015-10-31 01:36:56

Rems19 (79 views)
2015-10-31 01:32:37

williamwoles (107 views)
2015-10-23 10:42:59

williamwoles (93 views)
2015-10-23 10:42:45

Jervac_ (111 views)
2015-10-18 23:29:12

DarkCart (137 views)
2015-10-16 00:58:11

KaiHH (118 views)
2015-10-11 14:10:14

KaiHH (158 views)
2015-10-11 13:26:18

BurntPizza (173 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!