问题描述
在阅读OpenGL规范时,我始终注意到它提到您可以在一个程序中包含同一类型的多个着色器(即,与glAttachShader相连的多个GL_VERTEX_SHADER).尤其是在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 = ...;
}
然后,您分别编译普通着色器和主着色器.但是,仅对通用着色器进行一次编译.
Then you compile separately common shader and your main shader. But do the compilation of common shader only once.
这篇关于在单个OpenGL程序中附加多个相同类型的着色器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!