Using GL_RG_EXT and GL_UNSINGED_BYTE

爱⌒轻易说出口 提交于 2019-12-22 13:01:41

问题


I met some problems while using GL_RG_EXT and GL_UNSIGNED_BYTE. Related code:

class TextureBuffer {
public:
GLuint texture;
GLuint frameBuffer;
GLenum internalformat;
GLenum format;
GLenum type;
int    w,h;

TextureBuffer() : texture(0), frameBuffer(0) {}
void release() {
    if(texture)
    {
        glDeleteTextures(1, &texture);
        texture = 0;
    }
    if(frameBuffer)
    {
        glDeleteFramebuffers(1, &frameBuffer);
        frameBuffer = 0;
    }

}
};  
TextureBuffer _maskTexture;
generateRenderToTexture(GL_RG_EXT, GL_RG_EXT, GL_UNSIGNED_BYTE, _maskTexture, _imageWidth, _imageHeight, false);
void SharpenGPU::generateRenderToTexture(GLint internalformat, GLenum format, GLenum type,
                                     TextureBuffer &tb, int w, int h, bool linearInterp)
{
glGenTextures(1, &tb.texture);
glBindTexture(GL_TEXTURE_2D, tb.texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linearInterp ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linearInterp ? GL_LINEAR : GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, w, h, 0, format, type, NULL);


glGenFramebuffers(1, &tb.frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, tb.frameBuffer);
glClear(_glClearBits);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tb.texture, 0);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
    printf("Framebuffer status: %x", (int)status);

tb.internalformat = internalformat;
tb.format = format;
tb.type = type;
tb.w = w;
tb.h = h;
}

When I use the following code to define _maskTexture,

generateRenderToTexture(GL_RG_EXT, GL_RG_EXT, GL_HALF_FLOAT_OES, _maskTexture, _imageWidth, _imageHeight, false);

the code goes well. But if I use the lines below to define the _maskTexture, an error appears,

generateRenderToTexture(GL_RG_EXT, GL_RG_EXT, GL_UNSIGNED_BYTE, _maskTexture, _imageWidth, _imageHeight, false);

Error:

ProgramInfoLog: Validation Failed: Fragment program failed to compile with current context state.
Validation Failed: Vertex program failed to compile with current context state.

I'm really puzzled about it.

I knew that the error info is caused by compiling the vertex and shader function. Here is the shader function I found to cause the error:

{
const GLchar fShaderText[] = FRAGMENT_SHADER_SOURCE
(
 uniform sampler2D Distance4d; // unsimilar with version 6, patch distance has been prepared.
 uniform sampler2D DistanceCenter;
 varying highp vec2 uv0;

 void main()
 {

     highp float u_dx = 1./imageWH.x;
     highp float v_dy = 1./imageWH.y;
     highp vec2 ItBt = texture2D(DistanceCenter, uv0).yz;
     highp vec2 direcXY;
     highp float remainX = floor(mod(floor(imageWH.x * uv0.x + 0.6),2.) + 0.5); // 0 or 1
     highp float remainY = floor(mod(floor(imageWH.y * uv0.y + 0.6),2.) + 0.5); // 0 or 1;

     {

         //center
         highp float sum0 = texture2D(DistanceCenter, uv0).x;

         highp float sumMin = sum0;
         direcXY = vec2(0.,0.);

         highp vec4 sum4d = texture2D(Distance4d, uv0);
         //left
         if(sum4d.x < sumMin)
         {
             sumMin = sum4d.x;
             direcXY = vec2(-u_dx,0.);
         }

         //up
         if(sum4d.y < sumMin)
         {
             sumMin = sum4d.y;
             direcXY = vec2(0.,v_dy);
         }

         //right

         if(sum4d.z < sumMin)
         {
             sumMin = sum4d.z;
             direcXY = vec2(u_dx,0.);
         }

         //down
         if(sum4d.w < sumMin) // when i disable this line, the error info will disappear
         {
             sumMin = sum4d.w;
             direcXY = vec2(0.,-v_dy);
         }

         direcXY = (sumMin/sum0 > 0.7)? vec2(0.,0.):direcXY;// Section 4.1.1. thresholding. for that center position is preferred


     }

     gl_FragColor = vec4(ItBt.x, ItBt.x - ItBt.y, direcXY.x, direcXY.y);
     //vec4(It, It - Bt, dx, dy);
 }
 );

// Store the progrm, compute uniform locations
ProgramUniforms &pu = (_programs["findP2SpeedUpforS7"] = ProgramUniforms());
pu.program = compileShaders(gVertexShaderText, fShaderText);
pu.uniformMap["mvpMatrix"]    = glGetUniformLocation(pu.program, "mvpMatrix");
pu.uniformMap["Distance4d"] = glGetUniformLocation(pu.program, "Distance4d");
pu.uniformMap["DistanceCenter"] = glGetUniformLocation(pu.program, "DistanceCenter");
pu.uniformMap["imageWH"]      = glGetUniformLocation(pu.program, "imageWH");


}

I have marked out the related line causing the error.

Did someone meet a similar case? Thanks.

来源:https://stackoverflow.com/questions/24380576/using-gl-rg-ext-and-gl-unsinged-byte

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!