我正在尝试在片元着色器中同时使用samplerBuffer
和sampler2D
。
仅使用samplerBuffer
时,它可以正常工作,代码如下:
#version 460
out vec4 FragColor;
in vec2 texcoord;
uniform sampler2D background;
uniform samplerBuffer OctreeData;
...
void main()
{
Ray ray = initRay();
/*this function does raytraycing in octree and retrieves data
from samplerBuffer using texelFetch function.*/
Contact res = iterativeHitNodes(ray);
if(res.hit)
FragColor = assignNodeColor(res); //sets color of the node
else
FragColor = getBackground(origin, ray);
}
如果我在 main
函数的开头添加 FragColor = texture(background, texcoord.xy); return;
这行代码,它就能成功读取图像并显示出来。(A)
但当在 getBackground
函数中调用 texture
时,会显示灰色屏幕。结果发现,着色器根本没有起作用——即使我有意加载更多的模型,帧率也保持不变。但是没有着色器编译错误。
最大的“问题”是,即使从未调用 texture
,屏幕仍然保持灰色。(B) 例如,当 backgroundType
始终为0且 getBackground
函数如下所示:
vec3 getBackground(vec3 from, vec3 direction)
{
if(backgroundType == 0)
return getSkyBackground(from, direction);
else if(backgroundType == 1)
{
return texture(background, texcoord.xy).xyz;
}
return vec3(0);
}
看起来调用 texture
函数会丢弃片段。
- A 场景下的效果如下:https://imgur.com/5d2WaUT
- B 场景下的效果如下:https://imgur.com/LEDDNht
- 应该的效果以及在 CPU 上的效果如下:https://imgur.com/Gc2BUwa
P.S. 在调试模式下它的工作方式是正确的。由于某种原因,这个问题出现在发布模式中。
P.P.S. 大部分着色器代码在https://pastebin.com/SWCQ9HUw中。
texture
的调用?但是如果所有分支都有对它的调用(可能具有不同的参数),那就没问题了? - BekoiterativeHitNodes
(其中调用了texelFetch
)后停止运行。如果我将其删除,则即使存在分支,texture
似乎也能正常工作。 - Dr.Neo