简体   繁体   English

在glsl着色器中将矩阵作为纹理访问

[英]Accessing matrix as texture in glsl shader

I have a matrix which has 149 rows and 210 cols. 我有一个具有149行和210列的矩阵。 I want to sent it to shader to do some calculation. 我想将其发送到着色器进行一些计算。 Values of the matrix are between 9-128. 矩阵的值在9-128之间。 I've used the following for binding it to the texture: 我使用以下内容将其绑定到纹理:

m_GLTextureNNZ = new QOpenGLTexture(QOpenGLTexture::Target::Target2DArray);
m_GLTextureNNZ->create();
m_GLTextureNNZ->bind(indx);
m_GLTextureNNZ->setMinificationFilter(QOpenGLTexture::Nearest); 
m_GLTextureNNZ->setMagnificationFilter(QOpenGLTexture::Nearest);
m_GLTextureNNZ->setLayers(1);
m_GLTextureNNZ->setFormat(QOpenGLTexture::R8U);
m_GLTextureNNZ->setSize(m_sizeNZ2DCoeff_r,m_sizeNZ2DCoeff_c,1);
m_GLTextureNNZ->setWrapMode(QOpenGLTexture::ClampToEdge);
m_GLTextureNNZ->allocateStorage(QOpenGLTexture::Red, QOpenGLTexture::UInt8);

m_GLTextureNNZ->setData(0, 0, QOpenGLTexture::Red, QOpenGLTexture::UInt8, m_NNZ.data());

and in the shader I want to access a specific row and col of this texture: 在着色器中,我想访问此纹理的特定行和列:

uniform usampler2DArray NNZ;
vec3 getTexCoord(ivec2 indx, ivec3 tsize )
 {
  // ivec3 size = textureSize()
  return vec3( (indx.x+0.5)/tsize.x , (indx.y+0.5)/tsize.y , tsize.z);
 }
 .
 .
 .
 ivec3 patchedTexSize = textureSize(NNZ,0);
 vec3 texCoord = getTexCoord( ivec2(0,0),  ivec3(patchedTexSize.xy,0));

 uint iNNZ =  texture(NNZ, texCoord).r;

So based on my matrix (0,0) should have a value of 1. however I'm not getting this value. 因此,根据我的矩阵(0,0)的值应该为1。但是我没有得到这个值。 Is there something that I'm missing? 有什么我想念的吗?

EDIT: So I tested with all zeros matrix and the value that I get from texture lookup, no matter what the texcoords are is 2^8-1. 编辑:因此,我测试了全零矩阵,并且从纹理查找得到的值,无论texcoords是2 ^ 8-1。

The problem was that my data was int and I was trying to create an unsigned bit texture. 问题是我的数据是int ,我试图创建一个无符号的位纹理。 So I converted my data to uint8_t and it works. 因此,我将数据转换为uint8_t并且可以正常工作。 Also, I needed to add glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 另外,我需要添加glPixelStorei(GL_UNPACK_ALIGNMENT, 1); to be sure the alignment of the pixels are correct. 确保像素对齐正确。

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

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