From 60d125948387695c1fd1acf8b2953c2fb1859ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 10 Jan 2015 12:23:36 +0100 Subject: [PATCH] Use Range2Di in *Framebuffer::read(). Not sure why I chose to have offset and size in these two function, but that's probably because I never used them in real code. The original overloads taking pair of Vector2i are now marked as deprecated and will be removed in future release. --- src/Magnum/AbstractFramebuffer.cpp | 28 ++++++++-------- src/Magnum/AbstractFramebuffer.h | 35 +++++++++++++++----- src/Magnum/Implementation/FramebufferState.h | 2 +- src/Magnum/Test/FramebufferGLTest.cpp | 14 ++++---- src/Magnum/Test/MeshGLTest.cpp | 4 +-- 5 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/Magnum/AbstractFramebuffer.cpp b/src/Magnum/AbstractFramebuffer.cpp index a361fb7eb..a53e23865 100644 --- a/src/Magnum/AbstractFramebuffer.cpp +++ b/src/Magnum/AbstractFramebuffer.cpp @@ -230,30 +230,28 @@ void AbstractFramebuffer::clear(FramebufferClearMask mask) { glClear(GLbitfield(mask)); } -void AbstractFramebuffer::read(const Vector2i& offset, const Vector2i& size, Image2D& image) { - const Implementation::FramebufferState& state = *Context::current()->state().framebuffer; - +void AbstractFramebuffer::read(const Range2Di& rectangle, Image2D& image) { #ifndef MAGNUM_TARGET_GLES2 bindInternal(FramebufferTarget::Read); #else bindInternal(state.readTarget); #endif - const std::size_t dataSize = image.dataSize(size); + const std::size_t dataSize = image.dataSize(rectangle.size()); char* const data = new char[dataSize]; - (state.readImplementation)(offset, size, image.format(), image.type(), dataSize, data); - image.setData(image.format(), image.type(), size, data); + (Context::current()->state().framebuffer->readImplementation)(rectangle, image.format(), image.type(), dataSize, data); + image.setData(image.format(), image.type(), rectangle.size(), data); } #ifndef MAGNUM_TARGET_GLES2 -void AbstractFramebuffer::read(const Vector2i& offset, const Vector2i& size, BufferImage2D& image, BufferUsage usage) { +void AbstractFramebuffer::read(const Range2Di& rectangle, BufferImage2D& image, BufferUsage usage) { bindInternal(FramebufferTarget::Read); /* If the buffer doesn't have sufficient size, resize it */ /** @todo Explicitly reset also when buffer usage changes */ - if(image.size() != size) - image.setData(image.format(), image.type(), size, nullptr, usage); + if(image.size() != rectangle.size()) + image.setData(image.format(), image.type(), rectangle.size(), nullptr, usage); image.buffer().bindInternal(Buffer::TargetHint::PixelPack); - (Context::current()->state().framebuffer->readImplementation)(offset, size, image.format(), image.type(), image.dataSize(size), nullptr); + (Context::current()->state().framebuffer->readImplementation)(rectangle, image.format(), image.type(), image.dataSize(rectangle.size()), nullptr); } #endif @@ -394,15 +392,15 @@ void AbstractFramebuffer::readBufferImplementationDSAEXT(GLenum buffer) { } #endif -void AbstractFramebuffer::readImplementationDefault(const Vector2i& offset, const Vector2i& size, const ColorFormat format, const ColorType type, const std::size_t, GLvoid* const data) { - glReadPixels(offset.x(), offset.y(), size.x(), size.y(), GLenum(format), GLenum(type), data); +void AbstractFramebuffer::readImplementationDefault(const Range2Di& rectangle, const ColorFormat format, const ColorType type, const std::size_t, GLvoid* const data) { + glReadPixels(rectangle.min().x(), rectangle.min().y(), rectangle.sizeX(), rectangle.sizeY(), GLenum(format), GLenum(type), data); } -void AbstractFramebuffer::readImplementationRobustness(const Vector2i& offset, const Vector2i& size, const ColorFormat format, const ColorType type, const std::size_t dataSize, GLvoid* const data) { +void AbstractFramebuffer::readImplementationRobustness(const Range2Di& rectangle, const ColorFormat format, const ColorType type, const std::size_t dataSize, GLvoid* const data) { #ifndef MAGNUM_TARGET_GLES - glReadnPixelsARB(offset.x(), offset.y(), size.x(), size.y(), GLenum(format), GLenum(type), dataSize, data); + glReadnPixelsARB(rectangle.min().x(), rectangle.min().y(), rectangle.sizeX(), rectangle.sizeY(), GLenum(format), GLenum(type), dataSize, data); #elif !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_NACL) - glReadnPixelsEXT(offset.x(), offset.y(), size.x(), size.y(), GLenum(format), GLenum(type), dataSize, data); + glReadnPixelsEXT(rectangle.min().x(), rectangle.min().y(), rectangle.sizeX(), rectangle.sizeY(), GLenum(format), GLenum(type), dataSize, data); #else static_cast(offset); static_cast(size); diff --git a/src/Magnum/AbstractFramebuffer.h b/src/Magnum/AbstractFramebuffer.h index 578b9db94..8c92b0c0d 100644 --- a/src/Magnum/AbstractFramebuffer.h +++ b/src/Magnum/AbstractFramebuffer.h @@ -297,8 +297,7 @@ class MAGNUM_EXPORT AbstractFramebuffer { /** * @brief Read block of pixels from framebuffer to image - * @param offset Offset in the framebuffer - * @param size Image size + * @param rectangle Framebuffer rectangle to read * @param image Image where to put the data * * Image parameters like format and type of pixel data are taken from @@ -309,13 +308,22 @@ class MAGNUM_EXPORT AbstractFramebuffer { * @see @fn_gl{BindFramebuffer}, @fn_gl{ReadPixels} or * @fn_gl_extension{ReadnPixels,ARB,robustness} */ - void read(const Vector2i& offset, const Vector2i& size, Image2D& image); + void read(const Range2Di& rectangle, Image2D& image); + + #ifdef MAGNUM_BUILD_DEPRECATED + /** + * @copybrief read(const Range2Di&, Image2D&) + * @deprecated Use @ref read(const Range2Di&, Image2D&) instead. + */ + CORRADE_DEPRECATED("use read(const Range2Di&, Image2D& instead) instead") void read(const Vector2i& offset, const Vector2i& size, Image2D& image) { + read({offset, size}, image); + } + #endif #ifndef MAGNUM_TARGET_GLES2 /** * @brief Read block of pixels from framebuffer to buffer image - * @param offset Offset in the framebuffer - * @param size Image size + * @param rectangle Framebuffer rectangle to read * @param image Buffer image where to put the data * @param usage Buffer usage * @@ -325,7 +333,18 @@ class MAGNUM_EXPORT AbstractFramebuffer { * @todo Make it more flexible (usable with * @extension{ARB,buffer_storage}, avoiding relocations...) */ - void read(const Vector2i& offset, const Vector2i& size, BufferImage2D& image, BufferUsage usage); + void read(const Range2Di& rectangle, BufferImage2D& image, BufferUsage usage); + + #ifdef MAGNUM_BUILD_DEPRECATED + /** + * @copybrief read(const Range2Di&, BufferImage2D&, BufferUsage) + * @deprecated Use @ref read(const Range2Di&, BufferImage2D&, BufferUsage) + * instead. + */ + CORRADE_DEPRECATED("use read(const Range2Di&, BufferImage2D&, BufferUsage) instead") void read(const Vector2i& offset, const Vector2i& size, BufferImage2D& image, BufferUsage usage) { + read({offset, size}, image, usage); + } + #endif #endif #ifdef DOXYGEN_GENERATING_OUTPUT @@ -381,8 +400,8 @@ class MAGNUM_EXPORT AbstractFramebuffer { void MAGNUM_LOCAL readBufferImplementationDSAEXT(GLenum buffer); #endif - static void MAGNUM_LOCAL readImplementationDefault(const Vector2i& offset, const Vector2i& size, ColorFormat format, ColorType type, std::size_t dataSize, GLvoid* data); - static void MAGNUM_LOCAL readImplementationRobustness(const Vector2i& offset, const Vector2i& size, ColorFormat format, ColorType type, std::size_t dataSize, GLvoid* data); + static void MAGNUM_LOCAL readImplementationDefault(const Range2Di& rectangle, ColorFormat format, ColorType type, std::size_t dataSize, GLvoid* data); + static void MAGNUM_LOCAL readImplementationRobustness(const Range2Di& rectangle, ColorFormat format, ColorType type, std::size_t dataSize, GLvoid* data); void MAGNUM_LOCAL invalidateImplementationNoOp(GLsizei, const GLenum*); void MAGNUM_LOCAL invalidateImplementationDefault(GLsizei count, const GLenum* attachments); diff --git a/src/Magnum/Implementation/FramebufferState.h b/src/Magnum/Implementation/FramebufferState.h index 27d9cb0d2..182ad7017 100644 --- a/src/Magnum/Implementation/FramebufferState.h +++ b/src/Magnum/Implementation/FramebufferState.h @@ -59,7 +59,7 @@ struct FramebufferState { void(Renderbuffer::*renderbufferStorageImplementation)(RenderbufferFormat, const Vector2i&); void(Renderbuffer::*renderbufferStorageMultisampleImplementation)(GLsizei, RenderbufferFormat, const Vector2i&); - void(*readImplementation)(const Vector2i&, const Vector2i&, ColorFormat, ColorType, std::size_t, GLvoid*); + void(*readImplementation)(const Range2Di&, ColorFormat, ColorType, std::size_t, GLvoid*); FramebufferTarget readTarget, drawTarget; diff --git a/src/Magnum/Test/FramebufferGLTest.cpp b/src/Magnum/Test/FramebufferGLTest.cpp index 79cb8c22b..1b5923c5a 100644 --- a/src/Magnum/Test/FramebufferGLTest.cpp +++ b/src/Magnum/Test/FramebufferGLTest.cpp @@ -790,7 +790,7 @@ void FramebufferGLTest::read() { framebuffer.clear(FramebufferClear::Color|FramebufferClear::Depth|FramebufferClear::Stencil); Image2D colorImage(ColorFormat::RGBA, ColorType::UnsignedByte); - framebuffer.read({16, 8}, {8, 16}, colorImage); + framebuffer.read({{16, 8}, {8, 16}}, colorImage); CORRADE_COMPARE(colorImage.size(), Vector2i(8, 16)); MAGNUM_VERIFY_NO_ERROR(); @@ -805,7 +805,7 @@ void FramebufferGLTest::read() { #endif Image2D depthImage(ColorFormat::DepthComponent, ColorType::UnsignedShort); - framebuffer.read({}, Vector2i(1), depthImage); + framebuffer.read({{}, Vector2i{1}}, depthImage); MAGNUM_VERIFY_NO_ERROR(); CORRADE_COMPARE(depthImage.data()[0], 48352); @@ -820,7 +820,7 @@ void FramebufferGLTest::read() { #endif Image2D stencilImage(ColorFormat::StencilIndex, ColorType::UnsignedByte); - framebuffer.read({}, Vector2i(1), stencilImage); + framebuffer.read({{}, Vector2i{1}}, stencilImage); MAGNUM_VERIFY_NO_ERROR(); CORRADE_COMPARE(stencilImage.data()[0], 67); @@ -835,7 +835,7 @@ void FramebufferGLTest::read() { #endif Image2D depthStencilImage(ColorFormat::DepthStencil, ColorType::UnsignedInt248); - framebuffer.read({}, Vector2i(1), depthStencilImage); + framebuffer.read({{}, Vector2i{1}}, depthStencilImage); MAGNUM_VERIFY_NO_ERROR(); /** @todo This will probably fail on different systems */ @@ -870,7 +870,7 @@ void FramebufferGLTest::readBuffer() { framebuffer.clear(FramebufferClear::Color|FramebufferClear::Depth|FramebufferClear::Stencil); BufferImage2D colorImage(ColorFormat::RGBA, ColorType::UnsignedByte); - framebuffer.read({16, 8}, {8, 16}, colorImage, BufferUsage::StaticRead); + framebuffer.read({{16, 8}, {8, 16}}, colorImage, BufferUsage::StaticRead); CORRADE_COMPARE(colorImage.size(), Vector2i(8, 16)); MAGNUM_VERIFY_NO_ERROR(); @@ -918,14 +918,14 @@ void FramebufferGLTest::blit() { /* The framebuffer should be black before */ Image2D image(ColorFormat::RGBA, ColorType::UnsignedByte); - b.read({}, Vector2i(1), image); + b.read({{}, Vector2i{1}}, image); MAGNUM_VERIFY_NO_ERROR(); CORRADE_COMPARE(image.data()[0], Color4ub()); /* And have given color after */ Framebuffer::blit(a, b, a.viewport(), FramebufferBlit::Color); - b.read({}, Vector2i(1), image); + b.read({{}, Vector2i{1}}, image); MAGNUM_VERIFY_NO_ERROR(); CORRADE_COMPARE(image.data()[0], Color4ub(128, 64, 32, 17)); diff --git a/src/Magnum/Test/MeshGLTest.cpp b/src/Magnum/Test/MeshGLTest.cpp index dbea75921..8f9910b63 100644 --- a/src/Magnum/Test/MeshGLTest.cpp +++ b/src/Magnum/Test/MeshGLTest.cpp @@ -457,7 +457,7 @@ Checker::Checker(AbstractShaderProgram&& shader, RenderbufferFormat format, Mesh template T Checker::get(ColorFormat format, ColorType type) { Image2D image(format, type); - framebuffer.read({}, Vector2i(1), image); + framebuffer.read({{}, Vector2i{1}}, image); return image.data()[0]; } #endif @@ -1750,7 +1750,7 @@ MultiChecker::MultiChecker(AbstractShaderProgram&& shader, Mesh& mesh): framebuf template T MultiChecker::get(ColorFormat format, ColorType type) { Image2D image(format, type); - framebuffer.read({}, Vector2i(1), image); + framebuffer.read({{}, Vector2i{1}}, image); return image.data()[0]; } #endif