Combining multiple shaders

BlitzMax Forums/OpenGL Module/Combining multiple shaders

Ferret(Posted 2012) [#1]
I'm just starting to understand shaders, i have a phong shader working.
Now i want to add more shaders, but how do i combine them?

I read you can attach multiple shaders to a single program, is this a good idea?

How are multiple shaders usualy renderd?


Kryzon(Posted 2012) [#2]
I may be speaking out of my mouth here, but that would probably give you a lot of wasteful overdraw (having a sequence of fragment shaders to render).

Can't you merge the code into the same source file? (i.e: a shader that handles specular highlights, normal mapping, sub-surface scattering etc. all in the same source. It's the only way I've seen it being done.)


Ferret(Posted 2012) [#3]
Yes, but i also want to assing shaders to a mesh that add effects after the lighting.

Engines i have looked at usualy have a library of shaders, i cant imagine only one shader is used at a time.

Last edited 2012


Kryzon(Posted 2012) [#4]
Could you elaborate on those 'effects after the lighting' you're talking about?
If you need several 'passes' (which is different than having multiple shaders in the same program), then you need to use the cgFX language, from nVidia.
It allows you to define 'passes' and run a block of code exclusive to each pass.

The shader libraries are most likely there to give lots of shader options to the developers: a diffuse only shader; a diffuse+normal shader; a diffuse+normal+specular shader, etc.
Several variations to accommodate different projects or art directions.


Ferret(Posted 2012) [#5]
Effects like bumpmapping or making a single mesh glow.


Kryzon(Posted 2012) [#6]
Bumpmapping is done on the vertex and pixel shaders applied on the characters\objects\environments you want this effect present.
You can do bumpmapping, multi-texturing and specular highlighting all in the same shader.

Mesh glow, on the other hand, is not done through shaders applied to the meshes you want to glow, but rather on a framebuffer texture (a texture that holds a copy of the rendered scene). Mesh glow is a screen post-processing effect.

This is usually done in 4 passes:
1st) Render your scene onto a framebuffer.

2nd) Sample this framebuffer onto a second framebuffer, and only transfer pixels that are brighter than 'n' (essentially you are doing a Bright-Pass filter).

3rd) Blur this second framebuffer (which only contains the brightest spots of the scene).

4th) Compose this blurred framebuffer back onto the original, using Additive Blending (simply add the pixel values and clamp at 1.0, white). Show this composite to the user.

- http://prideout.net/archive/bloom/
- Keywords for searching more technical literature: Bloom, HDR, Glow.


Ferret(Posted 2012) [#7]
Harder then i thought.

I allready had fbo's working but i'm having problems with reading and writing pixels.
Open gl read/write pixel commands are realy slow and give me unexpected results or i'm doing it wrong.

For the shaders i have a default shader now from wich i copy the code to a new file and add the effect i want. This way i can assign different effects to meshes.


Kryzon(Posted 2012) [#8]
Open gl read/write pixel commands are realy slow and give me unexpected results or i'm doing it wrong.

Read\Write pixels is really slow, it's not the way it's usually done.

You write or read pixel values with the fragment shaders themselves.
If you're reading values, you need to include an uniform Sampler that points to the framebuffer color-attachment texture you're going to sample.
Then you can use the Texture2D() method to get the RGBA pixel value, and do whatever you want with it.

If you're writing values, you render a fullscreen quad and set active the framebuffer that holds the color-attachment texture you want the shader to write on. You're essentially printing the texture yourself.

You should really research literature, it explains it all much better than I could ever do.

GPU Gems 1: http://http.developer.nvidia.com/GPUGems/gpugems_part01.html
GPU Gems 2: http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter19.html - (linked to a screen-sampling effect)
GPU Gems 3: http://http.developer.nvidia.com/GPUGems3/gpugems3_part01.html


Ferret(Posted 2012) [#9]
You explained it verry well, i know allot more now ;)