Xamarin OpenGL片段着色器使用制服工作时出现奇怪的行为

问题描述:

我目前正在使用的片段着色器有一些非常奇怪的行为,这使得我很疯狂。我有一个我正在渲染的帧缓冲区对象。Xamarin OpenGL片段着色器使用制服工作时出现奇怪的行为

vec4 texelFetch(sampler2D tex, ivec2 size, ivec2 coord) 
{ 
    vec2 texCoord= vec2((2.0*float(coord.x) + 1.0)/(2.0*float(size.x)), (2.0*float(coord.y) + 1.0)/(2.0*float(size.y))); 
    return texture2D(tex, texCoord); 
} 

这意味着我还需要了解大小:在片段着色器我也是从相同的纹理用我自己写的“texelFetch”(因为我使用OpenGL ES,其默认情况下不执行)阅读当我调用此函数时渲染的纹理。纹理的大小为1440×900,其由一个统一的着色器我代表:

uniform vec2 screenImageSize; 

我把我的程序该值,如下所示:

if (imageProgram != null) 
      imageProgram.Use(); 

screenImageSizeUniform = imageProgram.GetUniformIndex("screenImageSize"); 
GL.Uniform2(screenImageSizeUniform, 1440.0f, 900.0f); 


// Draw triangles that represents frame buffer rectangle 
GL.DrawArrays(BeginMode.Triangles, 0, 6); 

奇怪的是,我不当我使用这套制服时会收到正确的输出。但是,当硬编码值时,我确实收到了正确的输出。另外,如果我执行检查,看看有什么值,我看到均匀设置正确:

//Following line does not give me the correct result as it seems to shift pixel values 
//ivec2 test=ivec2(int(screenImageSize.x),int(screenImageSize.y)); 

//Following line does give me the correct result 
ivec2 test=ivec2(1440,900); 

if(test.x==1440 && test.y==900) 
    gl_FragColor=texelFetch(previousFullImage,test,ivec2(gl_FragCoord.x,gl_FragCoord.y)); 
else 
    gl_FragColor=vec4(1.0,0.0,0.0,1.0); 

有人能看到我在做什么错?任何帮助是极大的赞赏

Different rendered images

注:*图像稍微旋转但那是因为我误将截图。

+0

什么是“正确结果”,什么是“错误结果”?你正在从浮动转换为int很多,这取决于默认的精度,这可能会“抵消”东西 –

+0

感谢您的回复!我在文章中包含了这些图片,并使得较低的代码块更加清晰。 据我所见,这不是由于转换。我只做了一次转换,我尝试过: - 对所有值使用highp - 将属性更改为GL.Uniform2(screenImageSizeUniform,1440.5f,900.5f);' 两者似乎都没有效果 –

+0

你是否读写相同的纹理? – solidpixel

根据@ Isogen74提供的信息,我得出结论,它确实可能是GPU堆栈某处的错误。因此,我决定寻找解决方法并更新到opengl es 3.0。已经在那里实现了texelFetch,而不需要纹理的尺寸。现在它就像一个魅力!感谢所有的帮助=)!它真的很感激!