Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web-GL : Multiple Fragment Shaders per Program

Tags:

glsl

shader

webgl

Does anyone know if it's possible to have multiple fragment shaders run serially in a single Web-GL "program"? I'm trying to replicate some code I have written in WPF using shader Effects. In the WPF program I would wrap an image with multiple borders and each border would have an Effect attached to it (allowing for multiple Effects to run serially on the same image).

like image 610
Goose Avatar asked Jul 08 '12 21:07

Goose


People also ask

Can you use multiple shaders OpenGL?

Multiple shader objects of the same type may not be attached to a single program object. However, a single shader object may be attached to more than one program object. However, even in OpenGL, using multiple shaders of the same type does not work they way you outline it in your question.

How do I use WebGL multiple shaders?

So, if you want to render two objects with two different shaders, you should bind one object (it's buffers actually), use first material ( gl. useProgram(shaderProgramA) ) and dispatch draw. Then bind second object, set second material ( gl. useProgram(shaderProgramB) ) and dispatch another call.

How many times does fragment shader run?

Unless you have early depth testing enabled, a fragment is only discarded after you run the fragment shader. In this case, the frag shader would run once for every fragment in both quads, so 20000 times.

How many time a vertex shader is called?

Your shader is called once per vertex. Each time it's called you are required to set the special global variable, gl_Position to some clip space coordinates. Vertex shaders need data. They can get that data in 3 ways.


1 Answers

I'm afraid you're probably going to have to clarify your question a bit, but I'll take a stab at answering anyway:

WebGL can support, effectively, as many different shaders as you want. (There are of course practical limits like available memory but you'd have to be trying pretty hard to bump into them by creating too many shaders.) In fact, most "real world" WebGL/OpenGL applications will use a combination of many different shaders to produce the final scene rendered to your screen. (A simple example: Water will usually be rendered with a different shader or set of shaders than the rest of the environment around it.)

When dispatching render commands only one shader program may be active at a time. The currently active program is specified by calling gl.useProgram(shaderProgram); after which any geometry drawn will be rendered with that program. If you want to render an effect that requires multiple different shaders you will need to group them by shader and draw each batch separately:

 gl.useProgram(shader1);
 // Setup shader1 uniforms, bind the appropriate buffers, etc.
 gl.drawElements(gl.TRIANGLES, shader1VertexCount, gl.UNSIGNED_SHORT, 0); // Draw geometry that uses shader1

 gl.useProgram(shader2);
 // Setup shader2 uniforms, bind the appropriate buffers, etc.
 gl.drawElements(gl.TRIANGLES, shader2VertexCount, gl.UNSIGNED_SHORT, 0); // Draw geometry that uses shader2

 // And so on...
like image 157
Toji Avatar answered Oct 20 '22 00:10

Toji