Opengl TexelFetch和“TexelFetch”的问题;标量Swizzle“;在GLSL中 我以前曾做过一些着色器的工作,但我认为我自己对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)); }

Opengl TexelFetch和“TexelFetch”的问题;标量Swizzle“;在GLSL中 我以前曾做过一些着色器的工作,但我认为我自己对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)); },opengl,glsl,shader,Opengl,Glsl,Shader,当我通过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:

当我通过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是什么(在网上的任何地方都找不到),也不知道为什么会有问题

此配置文件不支持:es

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

另外,如果您使用的是
texelFetch
,为什么您仍在编写类似
gl\u 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<直到桌面GL 3.0版才引入code>Teselfetch。至于你的另一个问题,最好是作为一个恰当的问题问。虽然你可能应该先搜索,因为我相当肯定它以前已经被回答过了。