Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (565)
Games in Android Showcase (151)
games submitted by our members
Games in WIP (606)
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 503 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


Offline theagentd

« JGO Bitwise Duke »

Medals: 409
Projects: 2
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.

theagentd (11 views)
2015-03-27 23:08:20

wxwsk8er (52 views)
2015-03-20 15:39:46

Fairy Tailz (46 views)
2015-03-15 21:52:20

Olo (28 views)
2015-03-13 17:51:59

Olo (30 views)
2015-03-13 17:50:51

Olo (37 views)
2015-03-13 17:50:16

Olo (43 views)
2015-03-13 17:47:07

ClaasJG (57 views)
2015-03-10 11:36:42

ClaasJG (41 views)
2015-03-10 11:33:01

Pippogeek (47 views)
2015-03-05 14:36:23
How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27 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!