【问题标题】:Issues with TexelFetch and "Scalar Swizzle" in GLSLGLSL 中的 TexelFetch 和“Scalar Swizzle”问题
【发布时间】:2016-07-30 09:29:22
【问题描述】:

我以前做过一些着色器的工作,但我认为自己对 GLSL 的经验相对缺乏。我正在尝试编写一个使用一系列片段着色器模拟烟雾的流体求解器。其中大部分涉及从纹理中获取离散样本,然后对它们进行一些操作,因此它们都有类似的编译错误。必须评估每个像素,并且只有每个像素,因此我使用 texelFetch 而不是 texture2D 进行采样。以下是此类片段着色器的示例:

uniform sampler2D velocity;
uniform sampler2D pressure;
uniform sampler2D solid;
uniform float numCellsX;
uniform float numCellsY;

void main(void) {
    ivec2 pos = ivec2(gl_FragCoord.xy);
    if (texelFetch(solid, pos.xy, 0).x > 0.0)
        discard;

    float c = texelFetch(pressure, pos.xy, 0).x;
    float up = texelFetchOffset(pressure, pos.xy, 0, ivec2(0, 1)).x;
    float down = texelFetchOffset(pressure, pos.xy, 0, ivec2(0, -1)).x;
    float left = texelFetchOffset(pressure, pos.xy, 0, ivec2(-1, 0)).x;
    float right = texelFetchOffset(pressure, pos.xy, 0, ivec2(1, 0)).x;

    float upS = texelFetchOffset(solid, pos.xy, 0, ivec2(0, 1)).x;
    float downS = texelFetchOffset(solid, pos.xy, 0, ivec2(0, -1)).x;
    float leftS = texelFetchOffset(solid, pos.xy, 0, ivec2(-1, 0)).x;
    float rightS = texelFetchOffset(solid, pos.xy, 0, ivec2(1, 0)).x;

    if (upS > 0.0) up = c;
    if (downS > 0.0) down = c;
    if (leftS > 0.0) left = c;
    if (rightS > 0.0) right = c;

    gl_FragColor = texelFetch(velocity, pos.xy, 0) - vec2(0.5 * numCellsX * (right - left), 0.5 * numCellsY * (up - down));
}

当我通过 glSlangValidator 运行它时,我收到以下错误:

ERROR: 0:14: 'texelFetch' : no matching overloaded function found
ERROR: 0:14: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:17: 'texelFetch' : no matching overloaded function found
ERROR: 0:17: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:18: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:18: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:19: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:19: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:20: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:20: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:21: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:21: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:23: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:23: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:24: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:24: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:25: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:25: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:26: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:26: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:33: 'texelFetch' : no matching overloaded function found
ERROR: 0:33: 'assign' :  cannot convert from 'temp 2-component vector of float'
to 'fragColor mediump 4-component vector of float FragColor'
ERROR: 23 compilation errors.  No code generated.

几乎每一行都有一个错误,我在网上找到的关于这些事情的信息很少。根据 GLSL 参考手册:

gvec4 texelFetch(gsampler2D sampler, ivec2 P, int lod);

我不太明白为什么它不接受我的 texelFetch 调用。我也不知道 scalar swizzle 是什么(在网上任何地方都找不到)或者为什么它是一个问题。

【问题讨论】:

    标签: opengl glsl shader


    【解决方案1】:

    此配置文件不支持:es

    也许您应该使用 GLSL 的桌面版本而不是 GLSL ES(大概是 2.0)来编译它。您应该始终在 GLSL 着色器中使用 #version 声明。

    另外,如果您使用的是texelFetch,为什么还要写入已弃用的输出,例如gl_FragColor

    但是:

    无法从“浮点的临时 2 分量向量”转换

    无论此行的版本如何,都是如此:

    texelFetch(velocity, pos.xy, 0) - vec2(0.5 * numCellsX * (right - left), 0.5 * numCellsY * (up - down))
    

    texelFetch 返回一个 4 元素向量。不能从 4 元素向量中减去 2 元素向量。

    【讨论】:

    • 我想我应该提到,我的目标是 WebGL,所以我认为 ES 是正确的选择。 WebGL 不支持 texelFetch 吗?如果没有,我怎样才能得到每个像素中心在归一化纹理坐标中的确切位置?
    • @JoshuaDotson:“WebGL 不支持 texelFetch 吗?”WebGL 基于 OpenGL ES 2.0,主要基于桌面 GL 2.1。 texelFetch 直到桌面 GL 版本 3.0 才引入。至于您的其他问题,最好将其作为一个适当的问题提出。尽管您可能应该先搜索一下,因为我很确定以前已经回答过了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 2021-11-29
    • 1970-01-01
    • 2013-06-30
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多