什么时候适合使用带有 regl 的主要着色器函数?

When is it appropriate to use the main shader functions with regl?

在构造着色器时,我注意到有时会构造一个名为 main 的 void 函数来定义着色器。迭代属性时,似乎 main 被添加到着色器中。

我想弄清楚何时适合在 main 内定义着色器,而不是仅在着色器的顶级定义 space 内。

来自 the regl tutorial 的示例代码。

var drawTriangle = regl({
  //
  // First we define a vertex shader.  This is a program which tells the GPU
  // where to draw the vertices.
  //
  vert: `
    // This is a simple vertex shader that just passes the position through
    attribute vec2 position;
    void main () {
      gl_Position = vec4(position, 0, 1);
    }
  `,

  //
  // Next, we define a fragment shader to tell the GPU what color to draw.
  //
  frag: `
    // This is program just colors the triangle white
    void main () {
      gl_FragColor = vec4(1, 1, 1, 1);
    }
  `,

  // Finally we need to give the vertices to the GPU
  attributes: {
    position: [
      [1, 0],
      [0, 1],
      [-1, -1]
    ]
  },

  // And also tell it how many vertices to draw
  count: 3
})

我不熟悉 regl,但 GLSL 着色器始终需要 main() 函数。它是着色器的入口点。例如,在顶点着色器的情况下,将为每个顶点执行着色器的 main() 函数。在您的教程代码中,gl_Position 是顶点着色器的内置(而不是用户定义的)输出。 attribute vec2 position; 为顶点着色器定义一个输入变量,它可以访问 position 属性。