changeset 1400:3462663e4d83 MRTs

Fixed up the API: setCanvas (singular) now only takes one canvas, setCanvases must be used for multi-canvas rendering
author Alex Szpakowski <slime73@gmail.com>
date Mon, 25 Mar 2013 21:23:09 -0300
parents fa3eabb3602a
children bee3f7b0dca0
files src/modules/graphics/opengl/Canvas.cpp src/modules/graphics/opengl/Canvas.h src/modules/graphics/opengl/wrap_Canvas.cpp src/modules/graphics/opengl/wrap_Graphics.cpp src/modules/graphics/opengl/wrap_Graphics.h
diffstat 5 files changed, 74 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/modules/graphics/opengl/Canvas.cpp	Sun Mar 24 02:20:15 2013 -0300
+++ b/src/modules/graphics/opengl/Canvas.cpp	Mon Mar 25 21:23:09 2013 -0300
@@ -60,11 +60,14 @@
 	virtual void deleteFBO(GLuint, GLuint, GLuint) {}
 	virtual void bindFBO(GLuint) {}
 
-	/// attach additional canvases to this framebuffer for rendering
+	/// 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
@@ -134,12 +137,17 @@
 		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)
 		{
-			// set a single render target
-			glDrawBuffer(GL_COLOR_ATTACHMENT0);
+			setAttachments();
 			return;
 		}
 
@@ -232,12 +240,17 @@
 		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)
 		{
-			// set a single render target
-			glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
+			setAttachments();
 			return;
 		}
 
@@ -431,7 +444,7 @@
 		current->stopGrab();
 }
 
-void Canvas::startGrab()
+void Canvas::setupGrab()
 {
 	// already grabbing
 	if (current == this)
@@ -476,14 +489,18 @@
 	{
 		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.");
 	}
 
-	startGrab();
+	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 this FBO and set up multiple render targets
 	strategy->setAttachments(canvases);
 
 	// retain newly attached canvases
@@ -497,6 +514,23 @@
 	attachedCanvases = canvases;
 }
 
+void Canvas::startGrab()
+{
+	setupGrab();
+
+	if (attachedCanvases.size() == 0)
+		return;
+
+	// make sure the FBO is only using a single render target
+	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
--- a/src/modules/graphics/opengl/Canvas.h	Sun Mar 24 02:20:15 2013 -0300
+++ b/src/modules/graphics/opengl/Canvas.h	Mon Mar 25 21:23:09 2013 -0300
@@ -59,6 +59,7 @@
 	 * to allow drawing to multiple canvases at once.
 	 **/
 	void startGrab(const std::vector<Canvas *> &canvases);
+	void startGrab();
 	void stopGrab();
 
 	void clear(const Color &c);
@@ -135,7 +136,7 @@
 
 	std::vector<Canvas *> attachedCanvases;
 
-	void startGrab();
+	void setupGrab();
 	void drawv(const Matrix &t, const vertex *v) const;
 
 	static StringMap<TextureType, TYPE_MAX_ENUM>::Entry textureTypeEntries[];
--- a/src/modules/graphics/opengl/wrap_Canvas.cpp	Sun Mar 24 02:20:15 2013 -0300
+++ b/src/modules/graphics/opengl/wrap_Canvas.cpp	Mon Mar 25 21:23:09 2013 -0300
@@ -47,10 +47,9 @@
 	if (!lua_isfunction(L, 2))
 		return luaL_error(L, "Need a function to render to canvas.");
 
-	static std::vector<Canvas *> attachments;
 	try
 	{
-		canvas->startGrab(attachments);
+		canvas->startGrab();
 	}
 	catch (love::Exception &e)
 	{
--- a/src/modules/graphics/opengl/wrap_Graphics.cpp	Sun Mar 24 02:20:15 2013 -0300
+++ b/src/modules/graphics/opengl/wrap_Graphics.cpp	Mon Mar 25 21:23:09 2013 -0300
@@ -942,6 +942,34 @@
 		return 0;
 	}
 
+	Canvas *canvas = luax_checkcanvas(L, 1);
+
+	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;
 
@@ -1474,7 +1502,7 @@
 	{ "getMaxPointSize", w_getMaxPointSize },
 	{ "newScreenshot", w_newScreenshot },
 	{ "setCanvas", w_setCanvas },
-	{ "setCanvases", w_setCanvas },
+	{ "setCanvases", w_setCanvases },
 	{ "getCanvas", w_getCanvas },
 	{ "getCanvases", w_getCanvas },
 
--- a/src/modules/graphics/opengl/wrap_Graphics.h	Sun Mar 24 02:20:15 2013 -0300
+++ b/src/modules/graphics/opengl/wrap_Graphics.h	Mon Mar 25 21:23:09 2013 -0300
@@ -90,6 +90,7 @@
 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);