Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
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 341 times)
0 Members and 1 Guest are viewing this topic.
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Posted 2014-01-19 02: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

-D

Hello!
Online theagentd
« Reply #1 - Posted 2014-01-19 03: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.

Myomyomyo.
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #2 - Posted 2014-01-19 05: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

Hello!
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (216 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org 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‑gaming.org
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!