Jump to content
  • Advertisement
Sign in to follow this  
hudovisk

OpenGL Acessing G-Buffer via PBOs in CPU

This topic is 1419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently implementing a deferred shader and I would like to do some CPU process with the g-buffer.

The most efficient way I found was to use Pixel Buffer Objects for asynchronous read, here is the tutorial I am following http://www.songho.ca/opengl/gl_pbo.html.

 

Here is the way i create my pixel buffer objects:

	//Create the PBO
	glGenBuffers(2, m_pbo);
	glBindBuffer(GL_PIXEL_PACK_BUFFER, m_pbo[0]);
	glBufferData(GL_PIXEL_PACK_BUFFER, m_width * m_height * 3 * sizeof(float), 0, GL_STREAM_READ);

	glBindBuffer(GL_PIXEL_PACK_BUFFER, m_pbo[1]);
	glBufferData(GL_PIXEL_PACK_BUFFER, m_width * m_height * 3 * sizeof(float), 0, GL_STREAM_READ);

and here i create my Frame Buffer Object and attach the 2d textures.

	// Create the FBO
  glGenFramebuffers(1, &m_fbo);
  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);

	// Create the gbuffer textures
  glGenTextures(GBUFFER_NUM_TEXTURES, m_textures);
  glGenTextures(1, &m_depthTexture);

  for (unsigned int i = 0 ; i < GBUFFER_NUM_TEXTURES; i++) {
	glBindTexture(GL_TEXTURE_2D, m_textures[i]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, m_width, m_height, 0, GL_RGB, GL_FLOAT, NULL);
	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, m_textures[i], 0);
  }

  // depth
  glBindTexture(GL_TEXTURE_2D, m_depthTexture);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
	              NULL);
  glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);

  GLenum DrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
  glDrawBuffers(4, DrawBuffers);

here is my render method, i simply render my geometry into the gbuffer and then read the normal buffer and try to write it on the screen:

unsigned int bufferIndex = 0;
unsigned int nextBufferIndex = 0;
void render(Scene& scene)
{
	bufferIndex = (bufferIndex + 1)%2;
 	nextBufferIndex = (bufferIndex + 1)%2;

 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);

 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	renderToGBuffer(scene); 
	 // renderGBuffer();
	
	glBindBuffer(GL_PIXEL_PACK_BUFFER, m_pbo[bufferIndex]);
        glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo);
        glReadBuffer(GL_COLOR_ATTACHMENT0 + GBUFFER_TEXTURE_TYPE_NORMAL);

 	glReadPixels(0, 0, m_width, m_height, GL_RGB, GL_FLOAT, 0);
		
  	glBindBuffer(GL_PIXEL_PACK_BUFFER, m_pbo[nextBufferIndex]);
	m_pixelBuffer = (float*) glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_WRITE);

	if(m_pixelBuffer != nullptr)
	{
		glBindFramebuffer(GL_FRAMEBUFFER, 0);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glDrawPixels(m_width, m_height, GL_RGB, GL_FLOAT, m_pixelBuffer);
		
		glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
	}
	else
	{
		std::cout<<"m_pixelBuffer is NULL"<<std::endl;
	}

	glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
	
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
        SDL_GL_SwapWindow(m_window);
}

And it draws nothing! But it is calling the glDrawPixels because the message in the else statement doesn't show. The method renderGBuffer that is commented works and draws the gbuffer on the screen, so there is something in the buffer.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!