Ios glTexImage2D在OpenGL ES 2.0中的仅数学着色器中跨单词拆分字节

Ios glTexImage2D在OpenGL ES 2.0中的仅数学着色器中跨单词拆分字节,ios,opengl-es-2.0,Ios,Opengl Es 2.0,我正在写一个数组操作算法,它是在GPU而不是CPU中处理的。我将纹理写入FBO,渲染它,然后读取结果。我遇到的问题是输入的数据与输出的数据不匹配。我使用RGBA将8位字节处理为4字节字 此应用程序适用于iPhone上的IOS,使用OpenGL ES 2.0 头文件: #import <Foundation/Foundation.h> #import <GLKit/GLKit.h> @interface matmanip : UIViewController <G

我正在写一个数组操作算法,它是在GPU而不是CPU中处理的。我将纹理写入FBO,渲染它,然后读取结果。我遇到的问题是输入的数据与输出的数据不匹配。我使用RGBA将8位字节处理为4字节字

此应用程序适用于iPhone上的IOS,使用OpenGL ES 2.0

头文件:

#import <Foundation/Foundation.h>
#import <GLKit/GLKit.h>


@interface matmanip : UIViewController <GLKViewDelegate>
- (void) processmat;
@end
出于测试目的,片段着色器仅在相应的texel处加载32位字-一个简单的传递。我应该把同一个矩阵拿出来,颠倒过来。 片段着色器是:

attribute vec2 position;
attribute vec2 xy;
varying vec2 xyVarying;

void main()
{
    gl_Position = vec4(position.xy, 0.0, 1.0);
    xyVarying = xy;
}
precision mediump float;
uniform sampler2D t_texture;
varying highp vec2 xyVarying;
void main()
{
    gl_FragColor = texture2D(t_texture, xyVarying);

}
我的结果很奇怪

注意,我将整个矩阵设置为零,但第一个字节除外,在本例中为37(25H)

inputMatrix[0] 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
inputMatrix[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
outputMatrix[0] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
outputMatrix[1] 9 0 0 0 7 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
问题是,25小时已被分散为9小时、7小时和2小时。对于任何数字,我都会得到类似的结果,如果我在第二个八位字节中加上一个值,我会在前三个字节的第二个八位字节中得到类似的结果。我做错了什么?我想我是无意中从RGBA转换成了字节或类似的东西

更新:

多亏了雷托·科拉迪的建议,我将min和mag过滤器从

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

并且得到了更接近的结果。然后我意识到我没有使用2的幂来表示宽度,所以我将矩阵从[20,2]改为[16,4],并将ascii表从?至~(63-126)

x和y坐标看起来是加倍/减半的

inputMatrix[0] 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e
inputMatrix[1] 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e
inputMatrix[2] 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e
inputMatrix[3] 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e

outputMatrix[0] 6f 70 71 72 6f 70 71 72 73 74 75 76 73 74 75 76
outputMatrix[1] 6f 70 71 72 6f 70 71 72 73 74 75 76 73 74 75 76
outputMatrix[2] 5f 60 61 62 5f 60 61 62 63 64 65 66 63 64 65 66
outputMatrix[3] 5f 60 61 62 5f 60 61 62 63 64 65 66 63 64 65 66

四个32位字的角在整个矩阵上被拉伸(倒置,但我预期会这样)。

看起来您看到了线性插值的结果,这取决于您指定纹理参数的方式:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
如果要读取单个texel的值,而不是在多个texel之间插值,请将此更改为:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

我找到了。GLKview是一个视网膜显示器和一条线

glViewport(0, 0, _backingWidth, _backingHeight);
在s和t方向上,画布的大小都增加了一倍,因为回退宽度和回退高度是GLKView的像素计数(视网膜像素计数)。因为我可以根据自己的需要调整GLKView的大小(并且从不显示),所以我可以使用实际的宽度和高度

glViewport(0, 0, RGBAWidth, YHeight);
这就解决了问题

inputMatrix[0] 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e
inputMatrix[1] 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e
inputMatrix[2] 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e
inputMatrix[3] 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e

outputMatrix[0] 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e
outputMatrix[1] 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e
outputMatrix[2] 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e
outputMatrix[3] 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e

谢谢你的帮助

输入矩阵[0]4142434444464748494a4b4c4d4e4f50525354输入矩阵[1]61626364666668696b6c6d6e6f70717274outputmatrix[0]61626462626364666666666666666666666666666666666666a6b6c6d6f70727374输出矩阵61 62 63 64 61 62 63 64 65 66 67 68 65 66 67 69 6a 6b 6cI不认为您真的可以做到这一点(将字节存储在着色器浮动中)。不能保证精确到一定程度。浮点数可能在某个阶段被舍入。通过使用像素到矩阵维度的1:1映射和高p浮点数,除非矩阵变得非常大,否则这不重要。精度误差应足够小,以确保最近的像素是正确的。大多数设备将highp舍入到mediump(不支持highp)。任何我认为的精度损失都会把你的字节弄得一团糟。别担心,我看你还是坚持使用iOS。如果在Android上这样做,就不能保证高性能。
glViewport(0, 0, RGBAWidth, YHeight);
inputMatrix[0] 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e
inputMatrix[1] 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e
inputMatrix[2] 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e
inputMatrix[3] 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e

outputMatrix[0] 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e
outputMatrix[1] 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e
outputMatrix[2] 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e
outputMatrix[3] 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e