Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have multiple pixel (fragment) shaders in the same program?

Tags:

I would like to have two pixel shaders; the first doing one thing, and then the next doing something else. Is this possible, or do I have to pack everything into the one shader?

like image 388
Harry Avatar asked Feb 26 '10 19:02

Harry


People also ask

Can you use multiple shaders OpenGL?

However, even in OpenGL, using multiple shaders of the same type does not work they way you outline it in your question. Only one of the shaders can have a main() . The other shaders typically only contain functions. So your idea of chaining multiple shaders of the same type into a pipeline will not work in this form.

How many times does a fragment shader run?

In this case, your fragment shader runs once for every fragment within that quad. For this program, that means 100 * 100 = 10000 times, once for every pixel on your screen. However, not every fragment rendered in the shader has to be displayed on the screen.

Can there be more than one pixel corresponding to each fragment?

A fragment is not equal to a pixel when multi sample anti-aliasing (MSAA) or any of the other modes that change the ratio of rendered pixels to screen pixels is activated. In the case of 4x MSAA, each screen pixel will be represented by 4 (2x2) fragments in the display buffer.

Can you have multiple vertex shaders?

This allows you to actually have several different pixel and vertex shaders in the same file. The second way to reduce performance impact is to set the shader, render every object that uses that shader, then repeat. In other words, batching your rendering.


2 Answers

You can do do this, e.g. by doing function calls from the main entrypoint to functions that are implemented in the various shader objects.

main() {
    callToShaderObject1()
    callToShaderObject2()
}

each of those callToShaderObject functions can live in different shader objects, but all objects have to be attached and linked in the same program before it can be used.

like image 71
Bahbar Avatar answered Oct 08 '22 21:10

Bahbar


They can't run at the same time, but you are free to use different shaders for different geometry, or to render in multiple passes using different shaders.

like image 43
Michael Daum Avatar answered Oct 08 '22 22:10

Michael Daum