From 10d73ea1d718b2fdf54b55a3998ddacd62349ba4 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 29 Apr 2017 18:38:23 -0300 Subject: [PATCH] Moved some backend-agnostic code out of the OpenGL backend of love.graphics. --HG-- branch : minor --- src/modules/graphics/Canvas.cpp | 47 ++++++++++++++++++++++++++ src/modules/graphics/Canvas.h | 2 +- src/modules/graphics/opengl/Canvas.cpp | 45 ++---------------------- 3 files changed, 50 insertions(+), 44 deletions(-) diff --git a/src/modules/graphics/Canvas.cpp b/src/modules/graphics/Canvas.cpp index 82fbc46cc..c54253619 100644 --- a/src/modules/graphics/Canvas.cpp +++ b/src/modules/graphics/Canvas.cpp @@ -89,6 +89,53 @@ int Canvas::getRequestedMSAA() const return settings.msaa; } +love::image::ImageData *Canvas::newImageData(love::image::Image *module, int slice, int mipmap, const Rect &r) +{ + if (!isReadable()) + throw love::Exception("Canvas:newImageData cannot be called on non-readable Canvases."); + + if (isPixelFormatDepthStencil(getPixelFormat())) + throw love::Exception("Canvas:newImageData cannot be called on Canvases with depth/stencil pixel formats."); + + if (r.x < 0 || r.y < 0 || r.w <= 0 || r.h <= 0 || (r.x + r.w) > getPixelWidth(mipmap) || (r.y + r.h) > getPixelHeight(mipmap)) + throw love::Exception("Invalid rectangle dimensions."); + + if (slice < 0 || (texType == TEXTURE_VOLUME && slice >= getDepth(mipmap)) + || (texType == TEXTURE_2D_ARRAY && slice >= layers) + || (texType == TEXTURE_CUBE && slice >= 6)) + { + throw love::Exception("Invalid slice index."); + } + + Graphics *gfx = Module::getInstance(Module::M_GRAPHICS); + if (gfx != nullptr && gfx->isCanvasActive(this)) + throw love::Exception("Canvas:newImageData cannot be called while that Canvas is currently active."); + + PixelFormat dataformat; + switch (getPixelFormat()) + { + case PIXELFORMAT_RGB10A2: // FIXME: Conversions aren't supported in GLES + dataformat = PIXELFORMAT_RGBA16; + break; + case PIXELFORMAT_R16F: + case PIXELFORMAT_RG16F: + case PIXELFORMAT_RGBA16F: + case PIXELFORMAT_RG11B10F: // FIXME: Conversions aren't supported in GLES + dataformat = PIXELFORMAT_RGBA16F; + break; + case PIXELFORMAT_R32F: + case PIXELFORMAT_RG32F: + case PIXELFORMAT_RGBA32F: + dataformat = PIXELFORMAT_RGBA32F; + break; + default: + dataformat = PIXELFORMAT_RGBA8; + break; + } + + return module->newImageData(r.w, r.h, dataformat); +} + void Canvas::draw(Graphics *gfx, Quad *q, const Matrix4 &t) { if (gfx->isCanvasActive(this)) diff --git a/src/modules/graphics/Canvas.h b/src/modules/graphics/Canvas.h index 13aaae135..37714a342 100644 --- a/src/modules/graphics/Canvas.h +++ b/src/modules/graphics/Canvas.h @@ -66,7 +66,7 @@ public: MipmapMode getMipmapMode() const; int getRequestedMSAA() const; - virtual love::image::ImageData *newImageData(love::image::Image *module, int slice, int mipmap, const Rect &rect) = 0; + virtual love::image::ImageData *newImageData(love::image::Image *module, int slice, int mipmap, const Rect &rect); virtual void generateMipmaps() = 0; virtual int getMSAA() const = 0; diff --git a/src/modules/graphics/opengl/Canvas.cpp b/src/modules/graphics/opengl/Canvas.cpp index b14d37ca1..05c94265e 100644 --- a/src/modules/graphics/opengl/Canvas.cpp +++ b/src/modules/graphics/opengl/Canvas.cpp @@ -454,49 +454,8 @@ ptrdiff_t Canvas::getHandle() const love::image::ImageData *Canvas::newImageData(love::image::Image *module, int slice, int mipmap, const Rect &r) { - if (!isReadable()) - throw love::Exception("Canvas:newImageData cannot be called on non-readable Canvases."); - - if (isPixelFormatDepthStencil(getPixelFormat())) - throw love::Exception("Canvas:newImageData cannot be called on Canvases with depth/stencil pixel formats."); - - if (r.x < 0 || r.y < 0 || r.w <= 0 || r.h <= 0 || (r.x + r.w) > getPixelWidth(mipmap) || (r.y + r.h) > getPixelHeight(mipmap)) - throw love::Exception("Invalid rectangle dimensions."); - - if (slice < 0 || (texType == TEXTURE_VOLUME && slice >= getDepth(mipmap)) - || (texType == TEXTURE_2D_ARRAY && slice >= layers) - || (texType == TEXTURE_CUBE && slice >= 6)) - { - throw love::Exception("Invalid slice index."); - } - - Graphics *gfx = Module::getInstance(Module::M_GRAPHICS); - if (gfx != nullptr && gfx->isCanvasActive(this)) - throw love::Exception("Canvas:newImageData cannot be called while that Canvas is currently active."); - - PixelFormat dataformat; - switch (getPixelFormat()) - { - case PIXELFORMAT_RGB10A2: // FIXME: Conversions aren't supported in GLES - dataformat = PIXELFORMAT_RGBA16; - break; - case PIXELFORMAT_R16F: - case PIXELFORMAT_RG16F: - case PIXELFORMAT_RGBA16F: - case PIXELFORMAT_RG11B10F: // FIXME: Conversions aren't supported in GLES - dataformat = PIXELFORMAT_RGBA16F; - break; - case PIXELFORMAT_R32F: - case PIXELFORMAT_RG32F: - case PIXELFORMAT_RGBA32F: - dataformat = PIXELFORMAT_RGBA32F; - break; - default: - dataformat = PIXELFORMAT_RGBA8; - break; - } - - love::image::ImageData *imagedata = module->newImageData(r.w, r.h, dataformat); + love::image::ImageData *imagedata = love::graphics::Canvas::newImageData(module, slice, mipmap, r); + PixelFormat dataformat = imagedata->getFormat(); bool isSRGB = false; OpenGL::TextureFormat fmt = gl.convertPixelFormat(dataformat, false, isSRGB);