Sign in to follow this  

OpenGL GL 4.x, Basic question about the Tessellation Control Shader

This topic is 1906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having some trouble understanding the tessellation control shader (TCS). From my understanding, the main function is called once per control point. And it appears OpenGL supplies the shader with the built in variable 'gl_InvocationID' to store the index of the control point (and its attributes) for each invocation. So if your input patch is, for example, a triangle, gl_InvocationID will be 0, 1, and 2 in each subsequent call (though the calls could occur in a different order)

Take a look at the main() function of the TCS shown below from [url="http://ogldev.atspace.co.uk/www/tutorial30/tutorial30.html"]this tutorial[/url]. I included the entire shader just for completeness.

[code]
#version 410 core
// define the number of CPs in the output patch
layout (vertices = 3) out;
uniform vec3 gEyeWorldPos;
// attributes of the input CPs
in vec3 WorldPos_CS_in[];
in vec2 TexCoord_CS_in[];
in vec3 Normal_CS_in[];
// attributes of the output CPs
out vec3 WorldPos_ES_in[];
out vec2 TexCoord_ES_in[];
out vec3 Normal_ES_in[];


void main()
{
// Set the control points of the output patch
TexCoord_ES_in[gl_InvocationID] = TexCoord_CS_in[gl_InvocationID];
Normal_ES_in[gl_InvocationID] = Normal_CS_in[gl_InvocationID];
WorldPos_ES_in[gl_InvocationID] = WorldPos_CS_in[gl_InvocationID];

// Calculate the distance from the camera to the three control points
float EyeToVertexDistance0 = distance(gEyeWorldPos, WorldPos_ES_in[0]);
float EyeToVertexDistance1 = distance(gEyeWorldPos, WorldPos_ES_in[1]);
float EyeToVertexDistance2 = distance(gEyeWorldPos, WorldPos_ES_in[2]);
// Calculate the tessellation levels
gl_TessLevelOuter[0] = GetTessLevel(EyeToVertexDistance1, EyeToVertexDistance2);
gl_TessLevelOuter[1] = GetTessLevel(EyeToVertexDistance2, EyeToVertexDistance0);
gl_TessLevelOuter[2] = GetTessLevel(EyeToVertexDistance0, EyeToVertexDistance1);
gl_TessLevelInner[0] = gl_TessLevelOuter[2];
}

[/code]

Note I removed the intermediate commentary from the code blocks in the tutorial so its possible that some intermediate portions were left out.

My source of confusion is the first 3 lines of code make sense for a 'per-control-point' operation (it uses the gl_InvocationID), but the remaining code seems to be a 'per-patch' operation (it uses all three indices: 0,1,2). The tessellation levels (to my knowledge) correspond to the entire patch. So what I think is happening here, is that the tessellation levels will be improperly calculated twice, and then on the 3rd invocation, (after all the WorldPos_ES_in values are initialized) the correct result will be obtained.

I don't see anything technically incorrect with any of this, but this mixing of per-patch, and per-control-point operations, blurs my understanding of the Tesselation Control Shader as a 'per-control-point' action. Edited by mv348

Share this post


Link to post
Share on other sites
Pity that nobody else answered to this question. That only talks about understanding of tessellation shaders. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
Well, let's back to answering...

[quote name='mv348' timestamp='1351095098' post='4993467']
From my understanding, the main function is called once per control point. [/quote]
Well, not exactly. A tessellation control shader is invoked once per vertex of the output patch.

[quote name='mv348' timestamp='1351095098' post='4993467']
And it appears OpenGL supplies the shader with the built in variable 'gl_InvocationID' to store the index of the control point (and its attributes) for each invocation.[/quote]
That's correct.

[quote name='mv348' timestamp='1351095098' post='4993467']
So if your input patch is, for example, a triangle, gl_InvocationID will be 0, 1, and 2 in each subsequent call (though the calls could occur in a different order)[/quote]
No. The number of invocations is equal to the number of vertices in the output patch. That is defined by [font=Courier][size=1]layout( vertices = ... ) out;[/size][/font] qualifier in the TCS. So, for example, if your input patch contains 3 vertices, and TCS contains the following qualifier: layout (vertices = 4) out; you'll actually get four invocations.


[quote name='mv348' timestamp='1351095098' post='4993467']
My source of confusion is the first 3 lines of code make sense for a 'per-control-point' operation (it uses the gl_InvocationID), but the remaining code seems to be a 'per-patch' operation (it uses all three indices: 0,1,2). The tessellation levels (to my knowledge) correspond to the entire patch. So what I think is happening here, is that the tessellation levels will be improperly calculated twice, and then on the 3rd invocation, (after all the WorldPos_ES_in values are initialized) the correct result will be obtained.[/quote]
The output of the TCS are both per-control-point (i.e. per-vertex) and per-patch variables (for the output patch). Per vertex variable must be set using gl_InvocationID index. Per-patch variables should be set only by one invocation. So, what you have seen in the tutorial is not quite right. It works, but it is not clean, because all invocations do the same job, and overwrites previously written values. I usually use invocation with ID=0 to set per patch output variables.

[code]
if(gl_InvocationID == 0)
{
gl_TessLevelOuter[0] = ...;
gl_TessLevelOuter[1] = ...;
gl_TessLevelOuter[2] = ...;
gl_TessLevelOuter[3] = ...;

gl_TessLevelInner[0] = ...;
gl_TessLevelInner[1] = ...;
//...
[/code]

I hope this "sharpen" your understanding of TCS. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Aks9

Share this post


Link to post
Share on other sites
Awesome! Thanks so much, Aks9! Yeah it seems not too many people are experts on this subject! Lucky we've got you around! :]

I guess my only question is, what if you want to set the tessellation levels as a function of all the control points?

Share this post


Link to post
Share on other sites
[quote name='mv348' timestamp='1351472122' post='4994884']
what if you want to set the tessellation levels as a function of all the control points?
[/quote]
Well, there are several options:
1. If tessellation levels depend only on the input control points, then the above scenario stays. You can access all control points in the single invocation (e.g. ID = 0).
2. If it depends on the output patch (output control points), than you should use built-in GLSL function[b] barrier()[/b] to synchronize all invocations.
3. In most cases you can do it on the CPU and just set levels with
[code]
glPatchParameterfv(GL_PATCH_DEFAULT_INNER_LEVEL, tessLevels);
glPatchParameterfv(GL_PATCH_DEFAULT_OUTER_LEVEL, tessLevels);
[/code]
from the application. This one is my favorite, since in most cases I don't need a tessellation control shader at all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Similar Content

    • By _OskaR
      Hi,
      I have an OpenGL application but without possibility to wite own shaders.
      I need to perform small VS modification - is possible to do it in an alternative way? Do we have apps or driver modifictions which will catch the shader sent to GPU and override it?
    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

  • Popular Now