GLSL问题:一个程序中有多个着色器 [英] GLSL Problem: Multiple shaders in one program

查看:90
本文介绍了GLSL问题:一个程序中有多个着色器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定对着色器有误解:

I must have misunderstood something with shaders:

我认为,由于您可以将多个着色器附加到一个程序,因此您可以简单地附加一个以上的片段着色器,例如:通过颜色调制渲染的板条纹理折射.

I thought that as you can attach multiple shaders to one program, you'd be able to simply attach more than one fragment shader, as an example: A crate texture rendered with a color modulation and refraction.

但是显然不是这样,因为每个程序只能有一个主要功能.

But apparently this is not the case, as you can have only one main function per program.

  • 如何解决主要功能限制,并允许在同一程序中相互调用的多个片段着色器进行动态组合?

推荐答案

您可以预先定义一组入口点.假设您具有有限数量的效果(漫反射,镜面反射,环境等).它们最多都不会被应用一次,因此您只需要创建一个类似的管理着色器即可:

You can have a set of entry points pre-defined. Suppose you have a limited number of effects (diffuse, specular, environment, etc). None of them is applied at most once, so you just need to create a managing shader like that one:

void apply_diffuse();
void apply_specular();
void apply_environment();

void main(){ ...
     apply_diffuse();
     apply_specular();
     apply_environment();
...}

然后,当需要链接着色器程序时,将相应的实现附加为单独的GLSL对象.如果您不想要这种效果,则某些实现可能是虚拟的.这种方法不需要源文本解析,几乎没有性能损失.

Then, when it's time to link the shader program you attach the corresponding implementations as separate GLSL objects. Some implementations may be dummies if you don't want the effect. This approach doesn't require source text parsing and has next to no performance penalty.

这篇关于GLSL问题:一个程序中有多个着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆