Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 mv348   Members   -  Reputation: 249

Like
0Likes
Like

Posted 24 October 2012 - 10:11 AM

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 this tutorial. I included the entire shader just for completeness.

#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];
}


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, 24 October 2012 - 10:15 AM.


Sponsor:

#2 Aks9   Members   -  Reputation: 811

Like
0Likes
Like

Posted 27 October 2012 - 10:00 AM

Pity that nobody else answered to this question. That only talks about understanding of tessellation shaders. Posted Image
Well, let's back to answering...

From my understanding, the main function is called once per control point.

Well, not exactly. A tessellation control shader is invoked once per vertex of the output patch.

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.

That's correct.

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)

No. The number of invocations is equal to the number of vertices in the output patch. That is defined by layout( vertices = ... ) out; 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.


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.

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.

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

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

I hope this "sharpen" your understanding of TCS. Posted Image

Edited by Aks9, 27 October 2012 - 10:04 AM.


#3 mv348   Members   -  Reputation: 249

Like
0Likes
Like

Posted 28 October 2012 - 06:55 PM

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?

#4 Aks9   Members   -  Reputation: 811

Like
0Likes
Like

Posted 29 October 2012 - 07:29 AM

what if you want to set the tessellation levels as a function of all the control points?

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 barrier() to synchronize all invocations.
3. In most cases you can do it on the CPU and just set levels with
glPatchParameterfv(GL_PATCH_DEFAULT_INNER_LEVEL, tessLevels);
glPatchParameterfv(GL_PATCH_DEFAULT_OUTER_LEVEL, tessLevels);
from the application. This one is my favorite, since in most cases I don't need a tessellation control shader at all.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS