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

时间:2013-12-22 05:05:53

标签: c++ opengl glsl shader

关于着色器管道如何在每个阶段传递数据方面,我有点困惑。

我要做的是使用glVertexAttrib4fv()通过曲面细分控制着色器,然后是曲面细分评估着色器,传递在顶点阶段加载的颜色数据,以便可以在片段着色器中使用它。我不确定我是否犯了某种概念上的错误(很可能,因为我仍然试图通过固定功能来解决这个问题),但无论如何,只要我尝试通过镶嵌着色器,我的原始人拒绝渲染。在此之前,我的原始渲染,但它只呈现黑色。我的着色器如下:

顶点着色器:

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"
    };

细分控制着色器:

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"
        "}"
    };

细分评估着色器:

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"
        "}"
    };

片段着色器:

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"
    };

1 个答案:

答案 0 :(得分:11)

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

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

或输入/输出块也采用无界数组的形式:

in 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 [];

为了尽可能简化,我将避免使用接口块。

相反,我将介绍每个补丁输入和输出的概念,因为考虑到整个镶嵌表面的颜色是恒定的,它们将进一步简化着色器......

修改的曲面细分控制着色器:

      in  vec4 vs_color [];
patch out vec4 patch_color;

...

  patch_color = vs_color [gl_InvocationID];

修改的曲面细分评估着色器:

patch in  vec4 patch_color;
      out vec4 tes_color;

...

  tes_color = patch_color;

通过这些更改,您应该有一个有效的传递,并且对TCS和TES阶段的工作方式有一点了解。