-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use BufferStorage extension for faster glReadPixels.
- Loading branch information
Showing
20 changed files
with
15,078 additions
and
12,227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <OpenGL.h> | ||
#include <Textures.h> | ||
#include <FBOTextureFormats.h> | ||
|
||
#if defined(EGL) || defined(GLESX) | ||
#include <inc/ARB_buffer_storage.h> | ||
#include <EGL/egl.h> | ||
#include <EGL/eglext.h> | ||
#endif | ||
|
||
#include "ColorBufferToRDRAM_BufferStorageExt.h" | ||
|
||
ColorBufferToRDRAM_BufferStorageExt::ColorBufferToRDRAM_BufferStorageExt() | ||
: ColorBufferToRDRAM(), m_curIndex(0) | ||
{ | ||
#ifdef GLESX | ||
glBufferStorage = (PFNGLBUFFERSTORAGEPROC)eglGetProcAddress("glBufferStorageEXT"); | ||
#endif | ||
} | ||
|
||
void ColorBufferToRDRAM_BufferStorageExt::_init() | ||
{ | ||
// Generate Pixel Buffer Objects | ||
glGenBuffers(_numPBO, m_PBO); | ||
} | ||
|
||
void ColorBufferToRDRAM_BufferStorageExt::_destroy() | ||
{ | ||
for (int index = 0; index < _numPBO; ++index) { | ||
if (m_PBOData[index] != nullptr) { | ||
glBindBuffer(GL_PIXEL_PACK_BUFFER, m_PBO[index]); | ||
glUnmapBuffer(GL_PIXEL_PACK_BUFFER); | ||
} | ||
} | ||
|
||
glDeleteBuffers(_numPBO, m_PBO); | ||
|
||
for (int index = 0; index < _numPBO; ++index) { | ||
m_PBO[index] = 0; | ||
} | ||
} | ||
|
||
void ColorBufferToRDRAM_BufferStorageExt::_initBuffers(void) | ||
{ | ||
// Initialize Pixel Buffer Objects | ||
for (int index = 0; index < _numPBO; ++index) { | ||
glBindBuffer(GL_PIXEL_PACK_BUFFER, m_PBO[index]); | ||
m_fence[index] = 0; | ||
glBufferStorage(GL_PIXEL_PACK_BUFFER, m_pTexture->textureBytes, nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT); | ||
m_PBOData[index] = glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, m_pTexture->textureBytes, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT ); | ||
} | ||
|
||
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); | ||
} | ||
|
||
bool ColorBufferToRDRAM_BufferStorageExt::_readPixels(GLint _x0, GLint _y0, GLsizei _width, GLsizei _height, u32 _size, bool _sync) | ||
{ | ||
GLenum colorFormat, colorType, colorFormatBytes; | ||
if (_size > G_IM_SIZ_8b) { | ||
colorFormat = fboFormats.colorFormat; | ||
colorType = fboFormats.colorType; | ||
colorFormatBytes = fboFormats.colorFormatBytes; | ||
} else { | ||
colorFormat = fboFormats.monochromeFormat; | ||
colorType = fboFormats.monochromeType; | ||
colorFormatBytes = fboFormats.monochromeFormatBytes; | ||
} | ||
|
||
glBindBuffer(GL_PIXEL_PACK_BUFFER, m_PBO[m_curIndex]); | ||
glReadPixels(_x0, _y0, m_pTexture->realWidth, _height, colorFormat, colorType, 0); | ||
|
||
//Setup a fence sync object so that we know when glReadPixels completes | ||
glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); | ||
m_fence[m_curIndex] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); | ||
|
||
if (!_sync) { | ||
m_curIndex = (m_curIndex+1)%_numPBO; | ||
} | ||
|
||
//Wait for glReadPixels to complete for the currently selected PBO | ||
if (m_fence[m_curIndex] != 0) { | ||
glClientWaitSync(m_fence[m_curIndex], 0, 1e8); | ||
glDeleteSync(m_fence[m_curIndex]); | ||
} | ||
|
||
GLubyte* pixelData = reinterpret_cast<GLubyte*>(m_PBOData[m_curIndex]); | ||
if (pixelData == nullptr) | ||
return false; | ||
|
||
int widthBytes = _width * colorFormatBytes; | ||
int strideBytes = m_pTexture->realWidth * colorFormatBytes; | ||
|
||
GLubyte* pixelDataAlloc = m_pixelData.data(); | ||
for (unsigned int lnIndex = 0; lnIndex < _height; ++lnIndex) { | ||
memcpy(pixelDataAlloc + lnIndex*widthBytes, pixelData + (lnIndex*strideBytes), widthBytes); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void ColorBufferToRDRAM_BufferStorageExt::_cleanUp() | ||
{ | ||
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "ColorBufferToRDRAM.h" | ||
|
||
#include "inc/ARB_buffer_storage.h" | ||
|
||
|
||
class ColorBufferToRDRAM_BufferStorageExt : public ColorBufferToRDRAM | ||
{ | ||
public: | ||
ColorBufferToRDRAM_BufferStorageExt(); | ||
~ColorBufferToRDRAM_BufferStorageExt() = default; | ||
|
||
private: | ||
void _init() override; | ||
void _destroy() override; | ||
bool _readPixels(GLint _x0, GLint _y0, GLsizei _width, GLsizei _height, u32 _size, bool _sync) override; | ||
void _cleanUp() override; | ||
void _initBuffers(void) override; | ||
static const int _numPBO = 2; | ||
GLuint m_PBO[_numPBO]; | ||
void* m_PBOData[_numPBO]; | ||
u32 m_curIndex; | ||
GLsync m_fence[_numPBO]; | ||
#ifdef GLESX | ||
PFNGLBUFFERSTORAGEPROC glBufferStorage; | ||
#endif | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "ColorBufferToRDRAM.h" | ||
|
||
#include <Textures.h> | ||
#include <FBOTextureFormats.h> | ||
|
||
class ColorBufferToRDRAM_GL : public ColorBufferToRDRAM | ||
{ | ||
public: | ||
ColorBufferToRDRAM_GL(); | ||
~ColorBufferToRDRAM_GL() {}; | ||
|
||
private: | ||
void _init() override; | ||
void _destroy() override; | ||
bool _readPixels(GLint _x0, GLint _y0, GLsizei _width, GLsizei _height, u32 _size, bool _sync) override; | ||
void _cleanUp() override; | ||
void _initBuffers(void) override; | ||
static const int _numPBO = 3; | ||
GLuint m_PBO[_numPBO]; | ||
u32 m_curIndex; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <algorithm> | ||
#include "ColorBufferToRDRAM.h" | ||
|
||
#include <Textures.h> | ||
#include <FBOTextureFormats.h> | ||
|
||
#include <ui/GraphicBuffer.h> | ||
#include <android/native_window.h> | ||
#include <EGL/egl.h> | ||
#include <EGL/eglext.h> | ||
|
||
typedef void (GL_APIENTRY* PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, EGLImageKHR image); | ||
typedef void (GL_APIENTRY* PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, EGLImageKHR image); | ||
|
||
using namespace android; | ||
|
||
class ColorBufferToRDRAM_GLES : public ColorBufferToRDRAM | ||
{ | ||
public: | ||
ColorBufferToRDRAM_GLES(); | ||
~ColorBufferToRDRAM_GLES() = default; | ||
|
||
private: | ||
void _init() override; | ||
void _destroy() override; | ||
bool _readPixels(GLint _x0, GLint _y0, GLsizei _width, GLsizei _height, u32 _size, bool _sync) override; | ||
void _cleanUp() override; | ||
void _initBuffers(void) override; | ||
|
||
GraphicBuffer* m_window; | ||
EGLImageKHR m_image; | ||
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC m_glEGLImageTargetTexture2DOES; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.