From 0e8e71639b7f922e2d020abf0bb387e82360a041 Mon Sep 17 00:00:00 2001 From: Francisco Zurita Date: Sat, 11 Feb 2017 01:29:12 -0500 Subject: [PATCH] Implement generic copy color to RDRAM for GLES2 devices Async copy is not supported for GLES2 devices unless using EGL Image extension which has platform independent implementations --- projects/msvc12/GLideN64.vcxproj | 2 + projects/msvc12/GLideN64.vcxproj.filters | 6 +++ ...opengl_ColorBufferReaderWithReadPixels.cpp | 43 +++++++++++++++++++ .../opengl_ColorBufferReaderWithReadPixels.h | 18 ++++++++ .../OpenGLContext/opengl_ContextImpl.cpp | 8 +++- src/mupen64plus-video-gliden64.mk | 1 + 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/Graphics/OpenGLContext/opengl_ColorBufferReaderWithReadPixels.cpp create mode 100644 src/Graphics/OpenGLContext/opengl_ColorBufferReaderWithReadPixels.h diff --git a/projects/msvc12/GLideN64.vcxproj b/projects/msvc12/GLideN64.vcxproj index 8c850bbb..5e7ccff2 100644 --- a/projects/msvc12/GLideN64.vcxproj +++ b/projects/msvc12/GLideN64.vcxproj @@ -308,6 +308,7 @@ + @@ -441,6 +442,7 @@ + diff --git a/projects/msvc12/GLideN64.vcxproj.filters b/projects/msvc12/GLideN64.vcxproj.filters index 148f9fb2..dd2b31d0 100644 --- a/projects/msvc12/GLideN64.vcxproj.filters +++ b/projects/msvc12/GLideN64.vcxproj.filters @@ -332,6 +332,9 @@ Source Files + + Source Files\Graphics\OpenGL + @@ -622,5 +625,8 @@ Header Files + + Header Files\Graphics\OpenGL + \ No newline at end of file diff --git a/src/Graphics/OpenGLContext/opengl_ColorBufferReaderWithReadPixels.cpp b/src/Graphics/OpenGLContext/opengl_ColorBufferReaderWithReadPixels.cpp new file mode 100644 index 00000000..f4df5816 --- /dev/null +++ b/src/Graphics/OpenGLContext/opengl_ColorBufferReaderWithReadPixels.cpp @@ -0,0 +1,43 @@ +#include +#include "opengl_ColorBufferReaderWithReadPixels.h" +#include + +using namespace graphics; +using namespace opengl; + +ColorBufferReaderWithReadPixels::ColorBufferReaderWithReadPixels(CachedTexture *_pTexture) + : ColorBufferReader(_pTexture) +{ + +} + +u8 * ColorBufferReaderWithReadPixels::readPixels(s32 _x0, s32 _y0, u32 _width, u32 _height, u32 _size, bool _sync) +{ + const graphics::FramebufferTextureFormats & fbTexFormat = gfxContext.getFramebufferTextureFormats(); + GLenum colorFormat, colorType, colorFormatBytes; + if (_size > G_IM_SIZ_8b) { + colorFormat = GLenum(fbTexFormat.colorFormat); + colorType = GLenum(fbTexFormat.colorType); + colorFormatBytes = GLenum(fbTexFormat.colorFormatBytes); + } else { + colorFormat = GLenum(fbTexFormat.monochromeFormat); + colorType = GLenum(fbTexFormat.monochromeType); + colorFormatBytes = GLenum(fbTexFormat.monochromeFormatBytes); + } + + // No async pixel buffer copies are supported in this class, this is a last resort fallback + auto pixelData = std::unique_ptr(new GLubyte[m_pTexture->realWidth * _height * colorFormatBytes]) ; + glReadPixels(_x0, _y0, m_pTexture->realWidth, _height, colorFormat, colorType, pixelData.get()); + + int widthBytes = _width*colorFormatBytes; + int strideBytes = m_pTexture->realWidth * colorFormatBytes; + + for (unsigned int lnIndex = 0; lnIndex < _height; ++lnIndex) { + std::copy_n(pixelData.get() + (lnIndex*strideBytes), widthBytes, m_pixelData.data() + lnIndex*widthBytes); + } + return m_pixelData.data(); +} + +void ColorBufferReaderWithReadPixels::cleanUp() +{ +} diff --git a/src/Graphics/OpenGLContext/opengl_ColorBufferReaderWithReadPixels.h b/src/Graphics/OpenGLContext/opengl_ColorBufferReaderWithReadPixels.h new file mode 100644 index 00000000..841176ab --- /dev/null +++ b/src/Graphics/OpenGLContext/opengl_ColorBufferReaderWithReadPixels.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include "opengl_CachedFunctions.h" + +namespace opengl { + +class ColorBufferReaderWithReadPixels : + public graphics::ColorBufferReader +{ +public: + ColorBufferReaderWithReadPixels(CachedTexture * _pTexture); + ~ColorBufferReaderWithReadPixels() = default; + + u8 * readPixels(s32 _x0, s32 _y0, u32 _width, u32 _height, u32 _size, bool _sync) override; + void cleanUp() override; +}; + +} diff --git a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp index e2292e8e..83c20a6e 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp @@ -7,6 +7,7 @@ #include "opengl_ColorBufferReaderWithPixelBuffer.h" #include "opengl_ColorBufferReaderWithBufferStorage.h" //#include "opengl_ColorBufferReaderWithEGLImage.h" +#include "opengl_ColorBufferReaderWithReadPixels.h" #include "opengl_Utils.h" #include "GLSL/glsl_CombinerProgramBuilder.h" #include "GLSL/glsl_SpecialShadersFactory.h" @@ -297,14 +298,17 @@ graphics::PixelReadBuffer * ContextImpl::createPixelReadBuffer(size_t _sizeInByt graphics::ColorBufferReader * ContextImpl::createColorBufferReader(CachedTexture * _pTexture) { /* -#ifdef EGL +#if defined(EGL) && defined(OS_ANDROID) return new ColorBufferReaderWithEGLImage(_pTexture, m_cachedFunctions->getCachedBindTexture()); #endif*/ if (m_glInfo.bufferStorage) return new ColorBufferReaderWithBufferStorage(_pTexture, m_cachedFunctions->getCachedBindBuffer()); - return new ColorBufferReaderWithPixelBuffer(_pTexture, m_cachedFunctions->getCachedBindBuffer()); + if (!m_glInfo.isGLES2) + return new ColorBufferReaderWithPixelBuffer(_pTexture, m_cachedFunctions->getCachedBindBuffer()); + + return new ColorBufferReaderWithReadPixels(_pTexture); } /*---------------Shaders-------------*/ diff --git a/src/mupen64plus-video-gliden64.mk b/src/mupen64plus-video-gliden64.mk index 415e0b3a..deea65d9 100644 --- a/src/mupen64plus-video-gliden64.mk +++ b/src/mupen64plus-video-gliden64.mk @@ -90,6 +90,7 @@ MY_LOCAL_SRC_FILES := \ $(SRCDIR)/Graphics/OpenGLContext/opengl_CachedFunctions.cpp \ $(SRCDIR)/Graphics/OpenGLContext/opengl_ColorBufferReaderWithBufferStorage.cpp \ $(SRCDIR)/Graphics/OpenGLContext/opengl_ColorBufferReaderWithPixelBuffer.cpp \ + $(SRCDIR)/Graphics/OpenGLContext/opengl_ColorBufferReaderWithReadPixels.cpp \ $(SRCDIR)/Graphics/OpenGLContext/opengl_ContextImpl.cpp \ $(SRCDIR)/Graphics/OpenGLContext/opengl_GLInfo.cpp \ $(SRCDIR)/Graphics/OpenGLContext/opengl_Parameters.cpp \