问题描述
在阅读 OpenGL 规范时,我一直注意到它提到您可以在单个程序中包含多个相同类型的着色器(即多个 GL_VERTEX_SHADER 与 glAttachShader 相关联).特别是在 OpenGL 4.2, §2.11.3,程序对象中:同一类型的多个着色器对象可以附加到单个程序对象......".
In reading the OpenGL specs, I have noticed throughout that it mentions that you can include multiple shaders of the same kind in a single program (i.e. more than one GL_VERTEX_SHADER attached with glAttachShader). Specifically in OpenGL 4.2, §2.11.3, Program Objects: "Multiple shader objects of the same type may be attached to a single program object...".
OpenGL 管道程序和子程序可能适用于此,但这是在它们存在之前定义的(实际上它可以追溯到 2.1 规范,§2.15.2)所以我正在寻找这个想法的 GL4 之前的例子.当我做了一些简单的测试时,我发现包含多个 void main()
会导致链接错误.有没有人知道使用它的实际示例?
OpenGL pipeline programs and subroutines might apply here, but this was defined before those existed (in fact it goes back to the 2.1 spec, §2.15.2) so I am looking for a pre-GL4 example of this idea. When I did some simple testing, I found that including more than one void main()
caused linking errors. Is anyone aware of a practical example where this is used?
推荐答案
您可以将常用函数放在单独的着色器中.然后只编译一次,链接到多个程序中.
You can put common functions in separate shader. Then compile it only once, and link in multiple programs.
这与您只编译一次 cpp 文件以获取静态或共享库的方式类似.然后将这个库链接到多个可执行程序中,从而节省编译时间.
It's similar how you compile your cpp files only once to get static or shared library. Then you link this library into multiple executable programs thus saving compilation time.
假设您有复杂的照明功能:
Let's say you have complex lighting function:
vec3 ComputeLighting(vec3 position, vec3 eyeDir)
{
// ...
return vec3(...);
}
然后对于要使用此功能的每个着色器执行以下操作:
Then for each of shader where you want to use this functionality do this:
vec3 ComputeLighting(vec3 position, vec3 eyeDir);
void main()
{
vec3 light = ComputeLighting(arg1, arg2);
gl_FragColor = ...;
}
然后分别编译通用着色器和主着色器.但是普通shader只编译一次.
Then you compile separately common shader and your main shader. But do the compilation of common shader only once.
这篇关于在单个 OpenGL 程序中附加多个相同类型的着色器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!