1. spencercw
  2. gb_emulator

Commits

spencercw  committed 55390aa

Use OpenGL pixel buffers to improve rendering performance.

  • Participants
  • Parent commits 763a17e
  • Branches default

Comments (0)

Files changed (2)

File gb_emulator/include/gb_emulator/gb_video_opengl.h

View file
  • Ignore whitespace
 	// OpenGL data
 	GLuint program_;
 	GLuint vertexBuffer_;
+	GLuint pixelBuffer_;
 	GLuint texture_;
 
 	// Cg data
 
 	// RGB pixel buffers
 	std::unique_ptr<uint32_t[]> unscaledPixelBuffer_;
-	std::unique_ptr<uint32_t[]> scaledPixelBuffer_;
 	std::unique_ptr<uint32_t[]> textureBuffer_;
 
 	// Redraws the display

File gb_emulator/src/gb_video_opengl.cpp

View file
  • Ignore whitespace
 deviceContext_(nullptr),
 renderContext_(nullptr),
 vertexBuffer_(0),
+pixelBuffer_(0),
 texture_(0),
 cgContext_(nullptr, cgDestroyContext),
 vertexShader_(nullptr, cgDestroyProgram),
 fragmentShader_(nullptr, cgDestroyProgram),
 installDir_(installDir),
 unscaledPixelBuffer_(new uint32_t[WIDTH * HEIGHT]),
-scaledPixelBuffer_(new uint32_t[SCALED_WIDTH * SCALED_HEIGHT]),
 textureBuffer_(new uint32_t[TEXTURE_WIDTH * TEXTURE_HEIGHT])
 {
 }
 		UnregisterClassW(reinterpret_cast<wchar_t *>(windowClass_), GetModuleHandle(nullptr));
 	}
 }
+
 void GbVideoOpenGL::draw(uint16_t *pixelBuffer, bool gbc)
 {
+	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelBuffer_);
+	uint32_t *pbo = static_cast<uint32_t *>(glMapBuffer(GL_PIXEL_UNPACK_BUFFER,
+		GL_WRITE_ONLY));
+
 	// Perform post-processing in software
-	postProcess(pixelBuffer, gbc, unscaledPixelBuffer_.get(), scaledPixelBuffer_.get());
+	postProcess(pixelBuffer, gbc, unscaledPixelBuffer_.get(), pbo);
+	glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
 
 	// Upload the texture into video memory
 	glBindTexture(GL_TEXTURE_2D, texture_);
 	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, SCALED_WIDTH, SCALED_HEIGHT, GL_BGRA, GL_UNSIGNED_BYTE,
-		scaledPixelBuffer_.get());
+		nullptr);
+	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 
 	// Update the display
 	glClearColor(0.f, 0.f, 0.f, 0.f);
 	}
 	cgGLSetTextureParameter(samplerParam, texture_);
 
+	// Create the pixel buffer object that we render into
+	glGenBuffers(1, &pixelBuffer_);
+	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelBuffer_);
+	glBufferData(GL_PIXEL_UNPACK_BUFFER, SCALED_WIDTH * SCALED_HEIGHT * sizeof(uint32_t), nullptr,
+		GL_STREAM_DRAW);
+	glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+
 	// Bind the shaders
 	cgGLSetManageTextureParameters(cgContext_.get(), CG_TRUE);
 	cgGLBindProgram(vertexShader_.get());