且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据通过细分着色器传递到片段着色器

更新时间:2022-10-17 17:46:09

这并不奇怪,TCS输入/输出必须是以下形式:

 在vec4 vs_color []; 
out vec4 tcs_color [];

也采用***数组的形式:

 在CustomVertex {
vec4 color;
} custom_vs [ ;

out CustomVertex {
vec4 color;
} custom_tcs [];

对于一点点上下文,这是一个TCS /几何着色器作为顶点着色器的输出:

  in gl_PerVertex 
{
vec4 gl_Position;
float gl_PointSize;
float gl_ClipDistance [];
} gl_in [];






为了尽可能简单,我将避免使用接口块。相反,我将介绍每补丁输入和输出的概念,因为他们将进一步简化您的着色器,因为考虑到颜色在整个镶嵌表面是恒定的...

p>

修改镶嵌控制着色器:



  in vec4 vs_color []; 
patch out vec4 patch_color;

...

patch_color = VS_color [gl_InvocationID];



修改镶嵌评估着色器:



  patch in vec4 patch_color; 
出vec4 tes_color;

...

tes_color = patch_color;

有了这些变化,你应该有一个工作的传递,和TES阶段工作。


I'm a bit confused about how the shader pipeline works with regards to passing data through each stage.

What I'm trying to do is pass color data that is loaded in the vertex stage using glVertexAttrib4fv() through the tessellation control shader, and then the tessellation evaluation shader, so that it can be used in the fragment shader. I'm not sure if I've made some sort of conceptual mistake (quite possible, since I'm still trying to get my head around this over fixed functions), but either way, as soon as I try and pass anything through the tessellation shaders, my primitives refuse to render at all. Before that, my primitive renders, but it only renders in black. My shaders are as follows:

Vertex Shader:

static const GLchar* vss[] =
    {
        "#version 430 core                                                  \n"
        "                                                                   \n"
        "layout (location = 0) in vec4 offset;                              \n"
        "layout (location = 1) in vec4 color;                               \n"
        "                                                                   \n"
        "out vec4 vs_color;                                                 \n"
        "                                                                   \n"
        "void main(void)                                                    \n"
        "{                                                                  \n"
        "   const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, -0.5, 1.0),   \n"
        "                                  vec4(-0.25, -0.25, -0.5, 1.0),   \n"
        "                                  vec4( 0.25,  0.25, -0.5, 1.0));  \n"
        "                                                                   \n"
        "   gl_Position = vertices[gl_VertexID] + offset;                   \n"
        "   vs_color = color;                                               \n"
        "}                                                                  \n"
    };

Tessellation control shader:

static const GLchar* tc_ss[] =
    {
        "#version 430 core                                                              \n"
        "layout (vertices = 3) out;                                                     \n"
        "in vec4 vs_color;                                                              \n"
        "out vec4 tcs_color;                                                            \n"
        "void main(void)                                                                \n"
        "{                                                                              \n"
        "   if (gl_InvocationID == 0)                                                   \n"
        "   {                                                                           \n"
        "       gl_TessLevelInner[0]    = 10.0;                                         \n"
        "       gl_TessLevelOuter[0]    = 10.0;                                         \n"
        "       gl_TessLevelOuter[1]    = 10.0;                                         \n"
        "       gl_TessLevelOuter[2]    = 10.0;                                         \n"
        "   }                                                                           \n"
        "   gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;   \n"
        "   tcs_color = vs_color;                                                       \n"
        "}"
    };

Tessellation Evaluation shader:

static const GLchar* te_ss[] =
    {
        "#version 430 core                                                                  \n"
        "in vec4 tcs_color;                                                                 \n"
        "out vec4 tes_color;                                                                \n"
        "layout (triangles, equal_spacing, cw) in;                                          \n"
        "void main(void)                                                                    \n"
        "{                                                                                  \n"
        "   gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position +                          \n"
        "                  gl_TessCoord.y * gl_in[1].gl_Position +                          \n"
        "                  gl_TessCoord.z * gl_in[2].gl_Position);                          \n"
        "   tes_color = tcs_color;                                                          \n"
        "}"
    };

Fragment shader:

static const GLchar* fss[] =
    {
        "#version 430 core      \n"
        "in vec4 tes_color;     \n"
        "out vec4 color;        \n"
        "                       \n"
        "void main(void)        \n"
        "{                      \n"
        "    color = tes_color; \n"
        "}                      \n"
    };

This is not surprising, TCS inputs/outputs must be in the form:

in  vec4 vs_color  [];
out vec4 tcs_color [];

or in input/output blocks that also take the form of unbounded arrays:

in CustomVertex {
  vec4 color;
} custom_vs [];

out CustomVertex {
  vec4 color;
} custom_tcs [];

For a little bit of context, this is what a TCS / geometry shader sees as the output from vertex shaders:

in gl_PerVertex
{
  vec4  gl_Position;
  float gl_PointSize;
  float gl_ClipDistance [];
} gl_in [];


To keep things as simple as possible, I will avoid using interface blocks.

Instead, I will introduce the concept of per-patch inputs and outputs, because they will further simplify your shaders considering the color is constant across the entire tessellated surface...

Modified Tessellation Control Shader:

      in  vec4 vs_color [];
patch out vec4 patch_color;

...

  patch_color = vs_color [gl_InvocationID];

Modified Tessellation Evaluation Shader:

patch in  vec4 patch_color;
      out vec4 tes_color;

...

  tes_color = patch_color;

With these changes, you should have a working pass-through and a slightly better understanding of how the TCS and TES stages work.