我是一名辅助翻译,以下是您需要翻译的内容:我已经阅读了许多有关统一if语句的文章,这些文章处理分支以更改大型着色器“超级着色器”的行为。我开始使用超级着色器(opengl lwjgl),但后来意识到,在片段着色器中添加由uniform设置的if语句进行简单计算的简单操作,与不带uniform if语句的分开的着色器相比,我的fps降低了5个。我没有为我的fps限制设置任何上限,它只是尽可能快地刷新。我将要添加法线映射和视差映射,我可以看到两条路线:
我的好处是流程简单,但使整个程序更加复杂,我面临着丑陋的嵌套if语句。此外,如果我想完全避免if语句,我需要4个单独的着色器来处理每个可能的分支(没有反射的正常情况:有反射的正常情况:没有反射的视差:有反射的视差),仅针对一个特性——反射。
超级顶点着色器:
#version 400 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 textureCoords;
layout(location = 2)in vec3 normal;
**UNIFORM float RenderFlag;**
void main(void){
if(RenderFlag ==0){
//Calculate outVariables for normal mapping to the fragment shader
}
if(RenderFlag ==1){
//Calcuate outVariables for parallax mapping to the fragment shader
}
gl_Position = MVPmatrix *vec4(position,1);
}
优步片段着色器:
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 textureCoords;
layout(location = 2)in vec3 normal;
**UNIFORM float RenderFlag;**
**UNIFORM float reflectionFlag;** // if set either of the 2 render modes
will have some reflection of the skybox added to it, like reflective
surface.
void main(void){
if(RenderFlag ==0){
//display normal mapping
if(reflectionFlag){
vec4 reflectColor = texture(cube_texture, ReflectDirR) ;
//add reflection color to final color and output
}
}
if(RenderFlag ==1){
//display parrallax mapping
if(reflectionFlag){
vec4 reflectColor = texture(cube_texture, ReflectDirR) ;
//add reflection color to final color and output
}
}
gl_Position = MVPmatrix *vec4(position,1);
}
我的好处是流程简单,但使整个程序更加复杂,我面临着丑陋的嵌套if语句。此外,如果我想完全避免if语句,我需要4个单独的着色器来处理每个可能的分支(没有反射的正常情况:有反射的正常情况:没有反射的视差:有反射的视差),仅针对一个特性——反射。
1:GLSL是否执行两个分支和随后的分支并计算BOTH函数,然后输出正确的函数?
2:是否应该删除反射的if语句,改为无论如何都计算反射颜色,并将其添加到最终颜色中,如果它是相对较小的操作,则使用类似以下内容的统一标志:
finalColor = finalColor + reflectionColor * X
where X = a uniform variable, if none X == 0, if Reflection X==some amount.
#define
创建着色器变体的粉丝。相关问题请参见这里。 - jozxyqk