简体   繁体   English

Xamarin OpenGL片段着色器使用制服时的行为异常

[英]Xamarin OpenGL fragment shader weird behaviour when working with uniforms

The fragment shader I'm currently working on has some quite weird behaviour which is driving me nuts. 我目前正在使用的片段着色器有一些很奇怪的行为,这使我发疯。 I have a frame buffer object which I'm rendering. 我有一个正在渲染的帧缓冲区对象。 In the fragment shader I also read from the same texture using my own written 'texelFetch' (which is not implemented by default as I'm using OpenGL ES): 在片段着色器中,我还使用自己编写的“ texelFetch”(相同的纹理,因为我使用的是OpenGL ES,因此默认情况下未实现)读取相同的纹理:

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);
}

This means I also need to know the size of the texture I am rendering when I called this function. 这意味着我在调用此函数时还需要知道要渲染的纹理的大小。 The size of the texture is 1440x900 which is represented by an uniform in my shader: 纹理的大小为1440x900,由我的着色器中的制服表示:

uniform vec2 screenImageSize;

I set this value in my program as follows: 我在程序中将此值设置如下:

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);

The weird thing is that I don't receive the correct outputs when I'm using this uniform. 奇怪的是,当我使用这种制服时,我没有收到正确的输出。 I do receive the correct output when I hardcode the values however. 但是,当我对值进行硬编码时,我确实会收到正确的输出。 In addition, if I perform a check to see what the values are, I see that the uniform is set correctly: 另外,如果执行检查以查看值是什么,则会看到统一设置正确:

//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);

Can anyone see what I'm doing wrong? 谁能看到我在做什么错? Any help is greatly appreciated 任何帮助是极大的赞赏

不同的渲染图像

Note: the centre image is slightly rotated but that is due to me making the screenshot incorrectly. 注意:中心图像略微旋转,但这是由于我错误地制作了屏幕截图。

Based on the information that @Isogen74 provided, I concluded that it might indeed be a bug in the GPU stack somewhere. 根据@ Isogen74提供的信息,我得出结论,这确实可能是某处GPU堆栈中的错误。 I therefore decided to look for a workaround and update to opengl es 3.0. 因此,我决定寻找一种解决方法并更新到opengl es 3.0。 texelFetch has been implemented there, without requiring the dimensions of the texture. texelFetch已在此处实现,而无需纹理的尺寸。 Now it works like a charm! 现在它就像一种魅力! Thanks for all the help=)! 感谢您提供的所有帮助=)! Its really appreciated! 非常感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM