1
votes

I am trying to write a GPGPU program, by just copying contents of a texture to an output texture attached to FBO. however, I see that the input texture data is not being loaded properly/ not binded properly. I couldnt understand whats wrong.

here is my code :

void render()
{

  int i;
  float * result, *in;

  //allocating and filling data in the input texture

  result = (float * ) malloc(4*32*32*sizeof(float));
  in = (float *) malloc(4*32*32*sizeof(float));
  for(i=0;i<32*32*4; i++)
  {
    in[i]=1.0f;
  }


makeBuffers(); //makes the vertex and element buffers
makeTexture(in);  //makes the input textures
makeShaders();    //makes the vertex n pixel shaders
makeProgram();   //makes the program

glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

glBindBuffer(GL_ARRAY_BUFFER, res.vertexBuffer);
glVertexAttribPointer(res.position, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*2, (void*)0);
glEnableVertexAttribArray(res.position);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, res.elementBuffer);
res.texture= glGetUniformLocation(res.shaderProgram, "intexture");

/* bind texture */ makefbo(); // making the offscreen frame buffer object

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, res.texture);
glUniform1i(res.texture, 0);


glDrawElements(GL_TRIANGLE_STRIP,4, GL_UNSIGNED_SHORT, (void*)0);

//    glFlush();   // i think drawing would be enough now. 

glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);   //reading back data from the framebuffer
glReadPixels(0, 0, 32, 32,GL_RGBA,GL_FLOAT,result);

printf(" float values \n");
for(i=0; i< 32*4 ; i++)
  printf(" %f   ", result[i]);
printf(" \n");

}

and here is my maketexture() function

 glGenTextures (1, &res.texture);
glBindTexture(GL_TEXTURE_2D,res.texture);
// set texture parameters
glTexParameteri(GL_TEXTURE_2D,
                GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,
                GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,
                GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,
                GL_TEXTURE_WRAP_T, GL_CLAMP);
// define texture with floating point format
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA32F,
             texSize,texSize,0,GL_RGBA,GL_FLOAT,0);

// transfer data to texture
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,texSize,texSize,
                GL_RGBA,GL_FLOAT,data);

and here is my framebufferoject preparation and output texture biding :

glGenFramebuffers(1, &res.fbo);
glBindFramebuffer(GL_FRAMEBUFFER, res.fbo);
glViewport(0, 0, 32, 32);

glGenTextures(1, &res.tex);
glBindTexture(GL_TEXTURE_2D, res.fbo);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 
         32, 32, 0, GL_RGBA, GL_FLOAT, 0);
 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, 
                      GL_COLOR_ATTACHMENT0, 
                      GL_TEXTURE_2D, res.tex, 0);

the data does not seem to be getting read properly. I tried to attach the input texture to a framebuffer object, and try to read back the texture data immediately, and the data gets displayed fine then. but when I try to render to an offscreen texture, it doesnot give correct output. It gives just a bunch of zeros at the end of pixel shader execution.

pixel shader takes the input texture and copies the data into the output texture : outColor = texture2D (intexture, texcoord );

could somebody see any problem with this?

1
Please don't say that you create the texture, fbo, vao, etc. each time you render a frame. - vallentin
I am not rendering everytime, this is a one time operation. I just need the data to be processed once. - sai pallavi

1 Answers

0
votes

Your code lacks a lot of context, but I have the impression that you bind everything exactly one time and then just leave it that way. That's not how it's meant to be used and will likely not work.

The essential gist is, that if a texture is used as a FBO render target it must not serve as a sampling source. So you have to either unbind textures / FBOs as needed, so that things don't tread on each other's toes, or you're loading shaders which simply make forbidden texture accesses. IMHO unbinding is the simpler option (however with OpenGL-4.4 a bindless texture extension was released, which may become core in 4.5).