[Opengl4.0] glsl-use subroutines to select a shader

Source: Internet
Author: User

In glsl,SubroutineIt binds a function call to a function definition in a function definition set based on the value of a variable. In many ways, it is similar to the function pointer in C. A global variable acts as a pointer to call a function. You can set the value of this variable in OpenGL to bind it to one of multiple function definitions. The function definition in subroutine does not have to have the same name, but must have the same number of parameters of the same type and the same return type.

Subroutines allows you to select different implementation functions without switching the shader or re-compiling during runtime, or using if to judge. For example, you can implement multipleAlgorithmUsed to render different objects in the scene. When rendering a scenario, we can change the global variable value of subroutines to select an appropriate rendering algorithm instead of switching the shader or using conditional judgment.

Note: As the performance becomes more and more important in the coloring machine, it is necessary to avoid conditional judgment or switching the coloring machine. With subroutines, we can implement the conditional judgment and coloring machine switching function without increasing the computing load.


The following is an example. In this example, the teapot is rendered twice using subroutine. Different Illumination models are used respectively. First useADS (ambient, diffuse, specular) ModelFor rendering, only use for the second timeDiffuse ModelRendering. A subroutine global variable is used to select a rendering algorithm. The following two figures show the comparison of results.

On the left is the ADS model rendering effect, and on the right is the diffuse model rendering effect.


The following describes how to use subroutines.

We define subroutines in vertex shader.

(1) first, define the subroutine type:

Subroutine vec3 shademodeltype (vec4 position, vec3 normal );

The aboveCodeDefines a new subroutine type named shademodeltype. The semantics is similar to that of the function prototype. It defines a name, a parameter list, and a return type. The parameter name is not required.

(2) then we define an uniform variable shademodel of the above Type:

Subroutine uniform shademodeltype shademodel;

This variable acts as a function pointer and is used in OpenGL.ProgramWill be assigned to one of two possible functions.

(3) define two functions as part of subroutine.

They all have a prefix:

Subroutine (shademodeltype)

It means that the function matches the subroutine type. We use this prefix to define two functions:

Subroutine (shademodeltype) vec3 phongmodel (vec4 position, vec3 norm) {vec3 S = normalize (vec3 (light. position-position); vec3 v = normalize (-position. XYZ); vec3 r = reflect (-S, Norm); vec3 ambient = light. la * material. ka; float sdotn = max (dot (S, norm), 0.0); vec3 diffuse = light. LD * material. KD * sdotn; vec3 spec = vec3 (0.0); If (sdotn> 0.0) SPEC = light. ls * material. KS * POW (max (dot (R, v), 0.0), material. shininess); Return ambient + DIFFUSE + spec;} subroutine (shademodeltype) vec3 diffuseonly (vec4 position, vec3 norm) {vec3 S = normalize (vec3 (light. position-position); Return light. LD * material. KD * max (dot (S, norm), 0.0 );}

(4) function call

In the main function of vertex shader, we use the uniform variable shademodel of subroutine to call the two defined functions.

Lightintensity = shademodel (eyeposition, eyenorm );

In this way, according to the value of the uniform variable shademodel, this call will be bound to one of the two functions we have defined above.

(5) assign a value to the subroutine uniform variable.

Two steps are taken.

1. Use the glgetsubroutineindex function to query the indexes of each subroutine function.

 
Gluint adsindex = glgetsubroutineindex (programhandle, gl_vertex_shader, "phongmodel"); gluint diffuseindex = glgetsubroutineindex (programhandle, gl_vertex_shader, "diffuseonly ");

The first parameter is the program handle. The second is the coloring tool. The third is the name of subroutine. The queried index is stored in two gluint values.
2,To select an appropriate subroutine function, we need to assign a value to the subroutine uniform variable shademodel.Use the following function call:

Gluniformsubroutinesuiv (gl_vertex_shader, 1, & adsindex );

This function can set multiple uniform variable values at a time. The first parameter specifies the coloring phase. The second is the number of uniform variables to be set. You only need to set the value of an uniform variable.

If you want to set multiple parameters, the third parameter should be an array. Generally, the I element in the array is assigned to the one referenced as I in subroutine uniform.

You only need to set a value here. If the index is 0, the subroutine uniform variable is set.

Note: In OpenGL, the index of the uniform variable starts from 0 by default. You can use glgetsubroutineuniformlocation () to query multiple variables. 

Then, the rendering results are displayed.


To render another effect, you only need to call the following function:

Gluniformsubroutinesuiv (gl_vertex_shader, 1, & diffuseindex );

Then you can enter the rendering process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.