Harry
Harry

Reputation: 3106

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

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?

Upvotes: 24

Views: 14273

Answers (3)

Serdar Sanli
Serdar Sanli

Reputation: 1104

The answer depends on the GL version, you need to check glAttachShader documentation for the version you are using. GLES versions (including webgl) do not allow attaching multiple fragment shaders to a single program and will raise GL_INVALID_OPERATION error when attempted.

glAttachShader - OpenGL 4:

It is permissible to attach multiple shader objects of the same type because each may contain a portion of the complete shader.

glAttachShader - OpenGL ES 3.2:

It is not permissible to attach multiple shader objects of the same type.

Upvotes: 2

Bahbar
Bahbar

Reputation: 18015

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.

Upvotes: 22

Michael Daum
Michael Daum

Reputation: 830

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.

Upvotes: 4

Related Questions