Incorrect preprocessor directive 错误的 IF 运算符

Incorrect preprocessor directive error of IF operator

我正在使用 #if 预处理器指令编写 GLSL 着色器,但我总是收到错误 incorrect preprocessor directive

下面是我的代码(只是相关部分):

#define thre 20 
float s = get_sample_data(sampling_pos);
#if s > thre
vec4 val = texture(transfer_texture, vec2(s, s));
#endif

预处理是编译步骤之一,发生在运行时之前。它只是根据找到的 # 行转换源代码。它没有关于变量的任何线索,这是运行时概念。此时,变量没有值,预处理器甚至不知道它们。

知道这一点,说你不能在预处理器指令中使用变量值是微不足道的。

您可以将#defined 值与文字常量进行比较:

#define thre 12
#if thre > 15
    float x  = 1.;
#else
    float x  = -1.;
#endif

在 glsl 中,您仍然可以使用条件结构,但它只是 'regular' if.

if(s>thre){
    // do something
}else{
    // do something else
}