Merged bartbes/love-experiments/MRTs into default

This commit is contained in:
Alex Szpakowski
2013-03-26 23:41:46 -03:00
10 changed files with 467 additions and 92 deletions
+1
View File
@@ -158,6 +158,7 @@ StringMap<Graphics::Support, Graphics::SUPPORT_MAX_ENUM>::Entry Graphics::suppor
{
{ "canvas", Graphics::SUPPORT_CANVAS },
{ "hdrcanvas", Graphics::SUPPORT_HDR_CANVAS },
{ "multicanvas", Graphics::SUPPORT_MULTI_CANVAS },
{ "shader", Graphics::SUPPORT_SHADER },
{ "npot", Graphics::SUPPORT_NPOT },
{ "subtractive", Graphics::SUPPORT_SUBTRACTIVE },
+1
View File
@@ -87,6 +87,7 @@ public:
{
SUPPORT_CANVAS = 1,
SUPPORT_HDR_CANVAS,
SUPPORT_MULTI_CANVAS,
SUPPORT_SHADER,
SUPPORT_NPOT,
SUPPORT_SUBTRACTIVE,
+186 -9
View File
@@ -59,6 +59,15 @@ struct FramebufferStrategy
*/
virtual void deleteFBO(GLuint, GLuint, GLuint) {}
virtual void bindFBO(GLuint) {}
/// attach additional canvases to the active framebuffer for rendering
/**
* @param[in] canvases List of canvases to attach
**/
virtual void setAttachments(const std::vector<Canvas *> &canvases) {}
/// stop using all additional attached canvases
virtual void setAttachments() {}
};
struct FramebufferStrategyGL3 : public FramebufferStrategy
@@ -127,6 +136,40 @@ struct FramebufferStrategyGL3 : public FramebufferStrategy
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
}
virtual void setAttachments()
{
// set a single render target
glDrawBuffer(GL_COLOR_ATTACHMENT0);
}
virtual void setAttachments(const std::vector<Canvas *> &canvases)
{
if (canvases.size() == 0)
{
setAttachments();
return;
}
std::vector<GLenum> drawbuffers;
drawbuffers.push_back(GL_COLOR_ATTACHMENT0);
// attach the canvas framebuffer textures to the currently bound framebuffer
for (int i = 0; i < canvases.size(); i++)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 + i,
GL_TEXTURE_2D, canvases[i]->getTextureName(), 0);
drawbuffers.push_back(GL_COLOR_ATTACHMENT1 + i);
}
// set up multiple render targets
if (GLEE_VERSION_2_0)
glDrawBuffers(drawbuffers.size(), &drawbuffers[0]);
else if (GLEE_ARB_draw_buffers)
glDrawBuffersARB(drawbuffers.size(), &drawbuffers[0]);
else if (GLEE_ATI_draw_buffers)
glDrawBuffersATI(drawbuffers.size(), &drawbuffers[0]);
}
};
struct FramebufferStrategyPackedEXT : public FramebufferStrategy
@@ -196,6 +239,40 @@ struct FramebufferStrategyPackedEXT : public FramebufferStrategy
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
}
virtual void setAttachments()
{
// set a single render target
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
}
virtual void setAttachments(const std::vector<Canvas *> &canvases)
{
if (canvases.size() == 0)
{
setAttachments();
return;
}
std::vector<GLenum> drawbuffers;
drawbuffers.push_back(GL_COLOR_ATTACHMENT0_EXT);
// attach the canvas framebuffer textures to the currently bound framebuffer
for (int i = 0; i < canvases.size(); i++)
{
glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1_EXT + i,
GL_TEXTURE_2D, canvases[i]->getTextureName(), 0);
drawbuffers.push_back(GL_COLOR_ATTACHMENT1_EXT + i);
}
// set up multiple render targets
if (GLEE_VERSION_2_0)
glDrawBuffers(drawbuffers.size(), &drawbuffers[0]);
else if (GLEE_ARB_draw_buffers)
glDrawBuffersARB(drawbuffers.size(), &drawbuffers[0]);
else if (GLEE_ATI_draw_buffers)
glDrawBuffersATI(drawbuffers.size(), &drawbuffers[0]);
}
};
struct FramebufferStrategyEXT : public FramebufferStrategyPackedEXT
@@ -288,6 +365,9 @@ static void getStrategy()
}
}
static int maxFBOColorAttachments = 0;
static int maxDrawBuffers = 0;
Canvas::Canvas(int width, int height, TextureType texture_type)
: width(width)
, height(height)
@@ -338,18 +418,33 @@ bool Canvas::isSupported()
return (strategy != &strategyNone);
}
bool Canvas::isHdrSupported()
bool Canvas::isHDRSupported()
{
return GLEE_VERSION_3_0 || GLEE_ARB_texture_float;
}
bool Canvas::isMultiCanvasSupported()
{
if (!(isSupported() && (GLEE_VERSION_2_0 || GLEE_ARB_draw_buffers || GLEE_ATI_draw_buffers)))
return false;
if (maxFBOColorAttachments == 0 || maxDrawBuffers == 0)
{
glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxFBOColorAttachments);
glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
}
// system must support at least 4 simultanious active canvases
return maxFBOColorAttachments >= 4 && maxDrawBuffers >= 4;
}
void Canvas::bindDefaultCanvas()
{
if (current != NULL)
current->stopGrab();
}
void Canvas::startGrab()
void Canvas::setupGrab()
{
// already grabbing
if (current == this)
@@ -379,6 +474,63 @@ void Canvas::startGrab()
current = this;
}
void Canvas::startGrab(const std::vector<Canvas *> &canvases)
{
if (canvases.size() > 0)
{
if (!isMultiCanvasSupported())
throw love::Exception("Multi-canvas rendering is not supported on this system.");
if (canvases.size()+1 > maxDrawBuffers || canvases.size()+1 > maxFBOColorAttachments)
throw love::Exception("This system can't simultaniously render to %d canvases.", canvases.size()+1);
}
for (size_t i = 0; i < canvases.size(); i++)
{
if (canvases[i]->getWidth() != width || canvases[i]->getHeight() != height)
throw love::Exception("All canvas arguments must have the same dimensions.");
if (canvases[i]->getTextureType() != texture_type)
throw love::Exception("All canvas arguments must have the same texture type.");
}
setupGrab();
// don't attach anything if there's nothing to attach/detach
if (canvases.size() == 0 && attachedCanvases.size() == 0)
return;
// attach the canvas textures to the active FBO and set up multiple render targets
strategy->setAttachments(canvases);
// retain newly attached canvases
for (size_t i = 0; i < canvases.size(); i++)
canvases[i]->retain();
// release previously attached canvases
for (size_t i = 0; i < attachedCanvases.size(); i++)
attachedCanvases[i]->release();
attachedCanvases = canvases;
}
void Canvas::startGrab()
{
setupGrab();
if (attachedCanvases.size() == 0)
return;
// make sure the FBO is only using a single canvas
strategy->setAttachments();
// release any previously attached canvases
for (size_t i = 0; i < attachedCanvases.size(); i++)
attachedCanvases[i]->release();
attachedCanvases.clear();
}
void Canvas::stopGrab()
{
// i am not grabbing. leave me alone
@@ -393,20 +545,34 @@ void Canvas::stopGrab()
current = NULL;
}
void Canvas::clear(const Color &c)
{
GLuint previous = 0;
if (current != NULL)
previous = current->fbo;
strategy->bindFBO(fbo);
glPushAttrib(GL_COLOR_BUFFER_BIT);
if (current != this)
{
if (current != NULL)
previous = current->fbo;
strategy->bindFBO(fbo);
glPushAttrib(GL_COLOR_BUFFER_BIT);
}
// Make sure only this canvas is cleared when multi-canvas rendering is set
if (attachedCanvases.size() > 0)
strategy->setAttachments();
glClearColor((float)c.r/255.0f, (float)c.g/255.0f, (float)c.b/255.0f, (float)c.a/255.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glPopAttrib();
strategy->bindFBO(previous);
if (attachedCanvases.size() > 0)
strategy->setAttachments(attachedCanvases);
if (current != this)
{
glPopAttrib();
strategy->bindFBO(previous);
}
}
void Canvas::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
@@ -472,6 +638,11 @@ void Canvas::getPixel(unsigned char* pixel_rgba, int x, int y)
strategy->bindFBO( 0 );
}
const std::vector<Canvas *> &Canvas::getAttachedCanvases() const
{
return attachedCanvases;
}
void Canvas::setFilter(const Image::Filter &f)
{
bindTexture(img);
@@ -515,6 +686,12 @@ void Canvas::unloadVolatile()
settings.filter = getFilter();
settings.wrap = getWrap();
strategy->deleteFBO(fbo, depth_stencil, img);
// release attached canvases
for (size_t i = 0; i < attachedCanvases.size(); i++)
attachedCanvases[i]->release();
attachedCanvases.clear();
}
int Canvas::getWidth()
+19 -3
View File
@@ -21,6 +21,7 @@
#ifndef LOVE_GRAPHICS_OPENGL_CANVAS_H
#define LOVE_GRAPHICS_OPENGL_CANVAS_H
// LOVE
#include "graphics/DrawQable.h"
#include "graphics/Volatile.h"
#include "graphics/Image.h"
@@ -31,6 +32,9 @@
#include "common/Matrix.h"
#include "OpenGL.h"
// STL
#include <vector>
namespace love
{
namespace graphics
@@ -50,6 +54,11 @@ public:
Canvas(int width, int height, TextureType texture_type = TYPE_NORMAL);
virtual ~Canvas();
/**
* @param canvases A list of other canvases to temporarily attach to this one,
* to allow drawing to multiple canvases at once.
**/
void startGrab(const std::vector<Canvas *> &canvases);
void startGrab();
void stopGrab();
@@ -66,6 +75,8 @@ public:
void getPixel(unsigned char* pixel_rgba, int x, int y);
const std::vector<Canvas *> &getAttachedCanvases() const;
void setFilter(const Image::Filter &f);
Image::Filter getFilter() const;
@@ -89,20 +100,22 @@ public:
void unloadVolatile();
static bool isSupported();
static bool isHdrSupported();
static bool isHDRSupported();
static bool isMultiCanvasSupported();
static bool getConstant(const char *in, TextureType &out);
static bool getConstant(TextureType in, const char *&out);
static Canvas *current;
static void bindDefaultCanvas();
private:
friend class Shader;
GLuint getTextureName() const
{
return img;
}
private:
friend class Shader;
GLsizei width;
GLsizei height;
GLuint fbo;
@@ -121,6 +134,9 @@ private:
Image::Wrap wrap;
} settings;
std::vector<Canvas *> attachedCanvases;
void setupGrab();
void drawv(const Matrix &t, const vertex *v) const;
static StringMap<TextureType, TYPE_MAX_ENUM>::Entry textureTypeEntries[];
+1 -1
View File
@@ -397,7 +397,7 @@ ParticleSystem *Graphics::newParticleSystem(Image *image, int size)
Canvas *Graphics::newCanvas(int width, int height, Canvas::TextureType texture_type)
{
if (texture_type == Canvas::TYPE_HDR && !Canvas::isHdrSupported())
if (texture_type == Canvas::TYPE_HDR && !Canvas::isHDRSupported())
throw Exception("HDR Canvases are not supported by your OpenGL implementation");
while (GL_NO_ERROR != glGetError())
+8 -1
View File
@@ -47,7 +47,14 @@ int w_Canvas_renderTo(lua_State *L)
if (!lua_isfunction(L, 2))
return luaL_error(L, "Need a function to render to canvas.");
canvas->startGrab();
try
{
canvas->startGrab();
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
lua_settop(L, 2); // make sure the function is on top of the stack
lua_call(L, 0, 0);
canvas->stopGrab();
+81 -4
View File
@@ -943,8 +943,68 @@ int w_setCanvas(lua_State *L)
}
Canvas *canvas = luax_checkcanvas(L, 1);
// this unbinds the previous fbo
canvas->startGrab();
try
{
// this unbinds the previous fbo
canvas->startGrab();
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
return 0;
}
int w_setCanvases(lua_State *L)
{
// discard stencil testing
instance->discardStencil();
// called with none -> reset to default buffer
// nil is an error, to help people with typoes
if (lua_isnone(L,1))
{
Canvas::bindDefaultCanvas();
return 0;
}
bool is_table = lua_istable(L, 1);
std::vector<Canvas *> attachments;
Canvas *canvas = 0;
if (is_table)
{
lua_pushinteger(L, 1);
lua_gettable(L, 1);
canvas = luax_checkcanvas(L, -1);
lua_pop(L, 1);
for (int i = 2; i <= lua_objlen(L, 1); i++)
{
lua_pushinteger(L, i);
lua_gettable(L, 1);
attachments.push_back(luax_checkcanvas(L, -1));
lua_pop(L, 1);
}
}
else
{
canvas = luax_checkcanvas(L, 1);
for (int i = 2; i <= lua_gettop(L); i++)
attachments.push_back(luax_checkcanvas(L, i));
}
try
{
canvas->startGrab(attachments);
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
return 0;
}
@@ -952,14 +1012,25 @@ int w_setCanvas(lua_State *L)
int w_getCanvas(lua_State *L)
{
Canvas *canvas = Canvas::current;
int n = 1;
if (canvas)
{
canvas->retain();
luax_newtype(L, "Canvas", GRAPHICS_CANVAS_T, (void *) canvas);
const std::vector<Canvas *> &attachments = canvas->getAttachedCanvases();
for (size_t i = 0; i < attachments.size(); i++)
{
attachments[i]->retain();
luax_newtype(L, "Canvas", GRAPHICS_CANVAS_T, (void *) attachments[i]);
n++;
}
}
else
lua_pushnil(L);
return 1;
return n;
}
int w_setShader(lua_State *L)
@@ -1009,7 +1080,11 @@ int w_isSupported(lua_State *L)
supported = false;
break;
case Graphics::SUPPORT_HDR_CANVAS:
if (!Canvas::isHdrSupported())
if (!Canvas::isHDRSupported())
supported = false;
break;
case Graphics::SUPPORT_MULTI_CANVAS:
if (!Canvas::isMultiCanvasSupported())
supported = false;
break;
case Graphics::SUPPORT_SHADER:
@@ -1427,7 +1502,9 @@ static const luaL_Reg functions[] =
{ "getMaxPointSize", w_getMaxPointSize },
{ "newScreenshot", w_newScreenshot },
{ "setCanvas", w_setCanvas },
{ "setCanvases", w_setCanvases },
{ "getCanvas", w_getCanvas },
{ "getCanvases", w_getCanvas },
{ "setShader", w_setShader },
{ "getShader", w_getShader },
@@ -90,6 +90,7 @@ int w_getPointStyle(lua_State *L);
int w_getMaxPointSize(lua_State *L);
int w_newScreenshot(lua_State *L);
int w_setCanvas(lua_State *L);
int w_setCanvases(lua_State *L);
int w_getCanvas(lua_State *L);
int w_setShader(lua_State *L);
int w_getShader(lua_State *L);