Просмотр исходного кода

Added separate clear methods for clearing active render target and active viewport

Marko Pintera 12 лет назад
Родитель
Сommit
4ddd9e2995

+ 2 - 2
BansheeForwardRenderer/Source/BsForwardRenderer.cpp

@@ -77,12 +77,12 @@ namespace BansheeEngine
 			RenderTargetPtr target = camerasPerTarget.target;
 			RenderTargetPtr target = camerasPerTarget.target;
 			const Vector<HCamera>::type& cameras = camerasPerTarget.cameras;
 			const Vector<HCamera>::type& cameras = camerasPerTarget.cameras;
 
 
-			coreAccessor.setRenderTarget(target);
 			coreAccessor.beginFrame();
 			coreAccessor.beginFrame();
 
 
 			for(auto& camera : cameras)
 			for(auto& camera : cameras)
 			{
 			{
 				ViewportPtr viewport = camera->getViewport();
 				ViewportPtr viewport = camera->getViewport();
+				coreAccessor.setViewport(viewport);
 
 
 				UINT32 clearBuffers = 0;
 				UINT32 clearBuffers = 0;
 				if(viewport->getRequiresColorClear())
 				if(viewport->getRequiresColorClear())
@@ -95,7 +95,7 @@ namespace BansheeEngine
 					clearBuffers |= FBT_STENCIL;
 					clearBuffers |= FBT_STENCIL;
 
 
 				if(clearBuffers != 0)
 				if(clearBuffers != 0)
-					coreAccessor.clear(target, clearBuffers, viewport->getClearColor(), viewport->getClearDepthValue(), viewport->getClearStencilValue(), viewport->getDimensions());
+					coreAccessor.clearViewport(clearBuffers, viewport->getClearColor(), viewport->getClearDepthValue(), viewport->getClearStencilValue());
 
 
 				// Render scene
 				// Render scene
 				render(camera);
 				render(camera);

+ 2 - 2
CamelotClient/CamelotClient.cpp

@@ -36,9 +36,9 @@
 #include "CmRTTIType.h"
 #include "CmRTTIType.h"
 #include "CmCursor.h"
 #include "CmCursor.h"
 
 
-#define DX11
+//#define DX11
 //#define DX9
 //#define DX9
-//#define GL
+#define GL
 
 
 using namespace CamelotFramework;
 using namespace CamelotFramework;
 using namespace BansheeEditor;
 using namespace BansheeEditor;

+ 14 - 4
CamelotCore/Include/CmCoreThreadAccessor.h

@@ -75,7 +75,7 @@ namespace CamelotFramework
 		}
 		}
 
 
 		/** @copydoc RenderSystem::setViewport() */
 		/** @copydoc RenderSystem::setViewport() */
-		void setViewport(ViewportPtr& vp)
+		void setViewport(const ViewportPtr& vp)
 		{
 		{
 			mCommandQueue->queue(boost::bind(&RenderSystem::setViewport, RenderSystem::instancePtr(), vp));
 			mCommandQueue->queue(boost::bind(&RenderSystem::setViewport, RenderSystem::instancePtr(), vp));
 		}
 		}
@@ -173,10 +173,20 @@ namespace CamelotFramework
 			mCommandQueue->queue(boost::bind(&RenderSystem::endFrame, RenderSystem::instancePtr()));
 			mCommandQueue->queue(boost::bind(&RenderSystem::endFrame, RenderSystem::instancePtr()));
 		}
 		}
 
 
-		/** @copydoc RenderSystem::clear() */
-		void clear(RenderTargetPtr target, UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0, const Rect& clearArea = Rect::EMPTY)
+		/**
+		 * @copydoc RenderSystem::clearRenderTarget()
+		 */
+		void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0)
+		{
+			mCommandQueue->queue(boost::bind(&RenderSystem::clearRenderTarget, RenderSystem::instancePtr(), buffers, color, depth, stencil));
+		}
+
+		/**
+		 * @copydoc RenderSystem::clearViewport()
+		 */
+		void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0)
 		{
 		{
-			mCommandQueue->queue(boost::bind(&RenderSystem::clear, RenderSystem::instancePtr(), target, buffers, color, depth, stencil, boost::cref(clearArea)));
+			mCommandQueue->queue(boost::bind(&RenderSystem::clearViewport, RenderSystem::instancePtr(), buffers, color, depth, stencil));
 		}
 		}
 
 
 		/** @copydoc RenderSystem::swapBuffers() */
 		/** @copydoc RenderSystem::swapBuffers() */

+ 23 - 13
CamelotCore/Include/CmRenderSystem.h

@@ -154,7 +154,7 @@ namespace CamelotFramework
 
 
 		@param target Viewport to render to.
 		@param target Viewport to render to.
 		*/
 		*/
-		virtual void setViewport(ViewportPtr& vp) = 0;
+		virtual void setViewport(const ViewportPtr& vp) = 0;
 
 
 		/** Sets the current vertex buffer for the specified source index.   
 		/** Sets the current vertex buffer for the specified source index.   
 		/** @note Set buffer to nullptr to clear the buffer at the specified index.*/
 		/** @note Set buffer to nullptr to clear the buffer at the specified index.*/
@@ -260,18 +260,28 @@ namespace CamelotFramework
 		*/
 		*/
 		virtual void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom) = 0;
 		virtual void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom) = 0;
 
 
-		/** Clears the provided render target to the specified values
-		@param renderTarget Render target to clear. Entire surface will be cleared
-				regardless of viewport or scissor rect.
-		@param buffers Combination of one or more elements of FrameBufferType
-		denoting which buffers are to be cleared
-		@param colour The colour to clear the colour buffer with, if enabled
-		@param depth The value to initialise the depth buffer with, if enabled
-		@param stencil The value to initialise the stencil buffer with, if enabled.
-		*/
-		virtual void clear(RenderTargetPtr renderTarget, UINT32 buffers, 
-			const Color& color = Color::Black, 
-			float depth = 1.0f, UINT16 stencil = 0, const Rect& clearArea = Rect::EMPTY) = 0;
+		/**
+		 * @brief	Clears the currently active render target.
+		 *
+		 * @param	buffers			Combination of one or more elements of FrameBufferType
+		 *							denoting which buffers are to be cleared.
+		 * @param	color			(optional) The color to clear the color buffer with, if enabled.
+		 * @param	depth			(optional) The value to initialise the depth buffer with, if enabled.
+		 * @param	stencil			(optional) The value to initialise the stencil buffer with, if enabled.
+		 */
+		virtual void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0) = 0;
+
+		/**
+		 * @brief	Clears the currently active viewport (i.e. it clears just a sub-area of a render-target that is covered by the viewport,
+		 * 			as opposed to clearRenderTarget which always clears the entire render target).
+		 *
+		 * @param	buffers			Combination of one or more elements of FrameBufferType
+		 *							denoting which buffers are to be cleared.
+		 * @param	color			(optional) The color to clear the color buffer with, if enabled.
+		 * @param	depth			(optional) The value to initialise the depth buffer with, if enabled.
+		 * @param	stencil			(optional) The value to initialise the stencil buffer with, if enabled.
+		 */
+		virtual void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0) = 0;
 
 
 		/**
 		/**
          * Set current render target to target, enabling its device context if needed
          * Set current render target to target, enabling its device context if needed

+ 11 - 2
CamelotD3D11RenderSystem/Include/CmD3D11RenderSystem.h

@@ -30,10 +30,19 @@ namespace CamelotFramework
 
 
 		void beginFrame();
 		void beginFrame();
 		void endFrame();
 		void endFrame();
-		void clear(RenderTargetPtr target, UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0, const Rect& clearArea = Rect::EMPTY);
+
+		/**
+		 * @copydoc RenderSystem::clearRenderTarget()
+		 */
+		void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
+
+		/**
+		 * @copydoc RenderSystem::clearViewport()
+		 */
+		void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
 
 
 		void setRenderTarget(RenderTargetPtr target);
 		void setRenderTarget(RenderTargetPtr target);
-		void setViewport(ViewportPtr& vp);
+		void setViewport(const ViewportPtr& vp);
 		void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom);
 		void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom);
 
 
 		void setVertexBuffer(UINT32 index, const VertexBufferPtr& buffer);
 		void setVertexBuffer(UINT32 index, const VertexBufferPtr& buffer);

+ 1 - 1
CamelotD3D11RenderSystem/Include/CmD3D11RenderUtility.h

@@ -11,7 +11,7 @@ namespace CamelotFramework
 		D3D11RenderUtility(D3D11Device* device);
 		D3D11RenderUtility(D3D11Device* device);
 		~D3D11RenderUtility();
 		~D3D11RenderUtility();
 
 
-		void drawClearQuad(const Rect& screenArea, UINT32 clearBuffers, const Color& color, float depth, UINT16 stencil);
+		void drawClearQuad(UINT32 clearBuffers, const Color& color, float depth, UINT16 stencil);
 
 
 	protected:
 	protected:
 		D3D11Device* mDevice;
 		D3D11Device* mDevice;

+ 54 - 50
CamelotD3D11RenderSystem/Source/CmD3D11RenderSystem.cpp

@@ -302,7 +302,7 @@ namespace CamelotFramework
 		// Not used
 		// Not used
 	}
 	}
 
 
-	void D3D11RenderSystem::setViewport(ViewportPtr& vp)
+	void D3D11RenderSystem::setViewport(const ViewportPtr& vp)
 	{
 	{
 		THROW_IF_NOT_CORE_THREAD;
 		THROW_IF_NOT_CORE_THREAD;
 
 
@@ -576,74 +576,78 @@ namespace CamelotFramework
 		mDevice->getImmediateContext()->RSSetScissorRects(1, &mScissorRect);
 		mDevice->getImmediateContext()->RSSetScissorRects(1, &mScissorRect);
 	}
 	}
 
 
-	void D3D11RenderSystem::clear(RenderTargetPtr target, UINT32 buffers, const Color& color, float depth, UINT16 stencil, const Rect& clearArea)
+	void D3D11RenderSystem::clearViewport(UINT32 buffers, const Color& color, float depth, UINT16 stencil)
 	{
 	{
 		THROW_IF_NOT_CORE_THREAD;
 		THROW_IF_NOT_CORE_THREAD;
 
 
-		bool clearEntireTarget = clearArea.width == 0 || clearArea.height == 0;
-		clearEntireTarget |= (clearArea.x == 0 && clearArea.y == 0 && clearArea.width == target->getWidth() && clearArea.height == target->getHeight());
+		if(mActiveRenderTarget == nullptr)
+			return;
 
 
-		if(!clearEntireTarget)
-		{
-			RenderTargetPtr oldRenderTarget = mActiveRenderTarget;
-			if(target != mActiveRenderTarget)
-				setRenderTarget(target);
+		Rect clearArea((int)mViewport.TopLeftX, (int)mViewport.TopLeftY, (int)mViewport.Width, (int)mViewport.Height);
 
 
-			D3D11RenderUtility::instance().drawClearQuad(clearArea, buffers, color, depth, stencil);
+		bool clearEntireTarget = clearArea.width == 0 || clearArea.height == 0;
+		clearEntireTarget |= (clearArea.x == 0 && clearArea.y == 0 && clearArea.width == mActiveRenderTarget->getWidth() && clearArea.height == mActiveRenderTarget->getHeight());
 
 
-			if(oldRenderTarget != mActiveRenderTarget && oldRenderTarget != nullptr)
-				setRenderTarget(oldRenderTarget);
-		}
+		if(!clearEntireTarget)
+			D3D11RenderUtility::instance().drawClearQuad(buffers, color, depth, stencil);
 		else
 		else
-		{
-			// Clear render surfaces
-			if (buffers & FBT_COLOR)
-			{
-				UINT32 maxRenderTargets = mCurrentCapabilities->getNumMultiRenderTargets();
+			clearRenderTarget(buffers, color, depth, stencil);
+	}
 
 
-				ID3D11RenderTargetView** views = cm_newN<ID3D11RenderTargetView*, ScratchAlloc>(maxRenderTargets);
-				memset(views, 0, sizeof(ID3D11RenderTargetView*) * maxRenderTargets);
+	void D3D11RenderSystem::clearRenderTarget(UINT32 buffers, const Color& color, float depth, UINT16 stencil)
+	{
+		THROW_IF_NOT_CORE_THREAD;
 
 
-				target->getCustomAttribute("RTV", views);
-				if (!views[0])
-				{
-					cm_deleteN<ScratchAlloc>(views, maxRenderTargets);
-					return;
-				}
+		if(mActiveRenderTarget == nullptr)
+			return;
 
 
-				float clearColor[4];
-				clearColor[0] = color.r;
-				clearColor[1] = color.g;
-				clearColor[2] = color.b;
-				clearColor[3] = color.a;
+		// Clear render surfaces
+		if (buffers & FBT_COLOR)
+		{
+			UINT32 maxRenderTargets = mCurrentCapabilities->getNumMultiRenderTargets();
 
 
-				for(UINT32 i = 0; i < maxRenderTargets; i++)
-				{
-					if(views[i] != nullptr)
-						mDevice->getImmediateContext()->ClearRenderTargetView(views[i], clearColor);
-				}
+			ID3D11RenderTargetView** views = cm_newN<ID3D11RenderTargetView*, ScratchAlloc>(maxRenderTargets);
+			memset(views, 0, sizeof(ID3D11RenderTargetView*) * maxRenderTargets);
 
 
+			mActiveRenderTarget->getCustomAttribute("RTV", views);
+			if (!views[0])
+			{
 				cm_deleteN<ScratchAlloc>(views, maxRenderTargets);
 				cm_deleteN<ScratchAlloc>(views, maxRenderTargets);
+				return;
 			}
 			}
 
 
-			// Clear depth stencil
-			if((buffers & FBT_DEPTH) != 0 || (buffers & FBT_STENCIL) != 0)
+			float clearColor[4];
+			clearColor[0] = color.r;
+			clearColor[1] = color.g;
+			clearColor[2] = color.b;
+			clearColor[3] = color.a;
+
+			for(UINT32 i = 0; i < maxRenderTargets; i++)
 			{
 			{
-				ID3D11DepthStencilView* depthStencilView = nullptr;
-				target->getCustomAttribute("DSV", &depthStencilView);
+				if(views[i] != nullptr)
+					mDevice->getImmediateContext()->ClearRenderTargetView(views[i], clearColor);
+			}
+
+			cm_deleteN<ScratchAlloc>(views, maxRenderTargets);
+		}
 
 
-				D3D11_CLEAR_FLAG clearFlag;
+		// Clear depth stencil
+		if((buffers & FBT_DEPTH) != 0 || (buffers & FBT_STENCIL) != 0)
+		{
+			ID3D11DepthStencilView* depthStencilView = nullptr;
+			mActiveRenderTarget->getCustomAttribute("DSV", &depthStencilView);
 
 
-				if((buffers & FBT_DEPTH) != 0 && (buffers & FBT_STENCIL) != 0)
-					clearFlag = (D3D11_CLEAR_FLAG)(D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL);
-				else if((buffers & FBT_STENCIL) != 0)
-					clearFlag = D3D11_CLEAR_STENCIL;
-				else
-					clearFlag = D3D11_CLEAR_DEPTH;
+			D3D11_CLEAR_FLAG clearFlag;
 
 
-				if(depthStencilView != nullptr)
-					mDevice->getImmediateContext()->ClearDepthStencilView(depthStencilView, clearFlag, depth, (UINT8)stencil);
-			}
+			if((buffers & FBT_DEPTH) != 0 && (buffers & FBT_STENCIL) != 0)
+				clearFlag = (D3D11_CLEAR_FLAG)(D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL);
+			else if((buffers & FBT_STENCIL) != 0)
+				clearFlag = D3D11_CLEAR_STENCIL;
+			else
+				clearFlag = D3D11_CLEAR_DEPTH;
+
+			if(depthStencilView != nullptr)
+				mDevice->getImmediateContext()->ClearDepthStencilView(depthStencilView, clearFlag, depth, (UINT8)stencil);
 		}
 		}
 	}
 	}
 
 

+ 1 - 23
CamelotD3D11RenderSystem/Source/CmD3D11RenderUtility.cpp

@@ -31,26 +31,8 @@ namespace CamelotFramework
 		SAFE_RELEASE(mClearQuadVB);
 		SAFE_RELEASE(mClearQuadVB);
 	}
 	}
 
 
-	void D3D11RenderUtility::drawClearQuad(const Rect& screenArea, 
-		UINT32 clearBuffers, const Color& color, float depth, UINT16 stencil)
+	void D3D11RenderUtility::drawClearQuad(UINT32 clearBuffers, const Color& color, float depth, UINT16 stencil)
 	{
 	{
-		// Set viewport
-		UINT32 oldNumViewports = 0;
-		mDevice->getImmediateContext()->RSGetViewports(&oldNumViewports, nullptr);
-
-		D3D11_VIEWPORT* oldViewports = cm_newN<D3D11_VIEWPORT>(oldNumViewports);
-		mDevice->getImmediateContext()->RSGetViewports(&oldNumViewports, oldViewports);
-
-		D3D11_VIEWPORT newViewport;
-		newViewport.TopLeftX = (float)screenArea.x;
-		newViewport.TopLeftY = (float)screenArea.y;
-		newViewport.Width = (float)screenArea.width;
-		newViewport.Height = (float)screenArea.height;
-		newViewport.MinDepth = 0.0f;
-		newViewport.MaxDepth = 1.0f;
-
-		mDevice->getImmediateContext()->RSSetViewports(1, &newViewport);
-
 		// Set states
 		// Set states
 		if((clearBuffers & FBT_COLOR) != 0)
 		if((clearBuffers & FBT_COLOR) != 0)
 		{
 		{
@@ -122,10 +104,6 @@ namespace CamelotFramework
 		mDevice->getImmediateContext()->IASetInputLayout(mClearQuadIL);
 		mDevice->getImmediateContext()->IASetInputLayout(mClearQuadIL);
 
 
 		mDevice->getImmediateContext()->DrawIndexed(6, 0, 0);
 		mDevice->getImmediateContext()->DrawIndexed(6, 0, 0);
-
-		// Restore viewports
-		mDevice->getImmediateContext()->RSSetViewports(oldNumViewports, oldViewports);
-		cm_deleteN(oldViewports, oldNumViewports);
 	}
 	}
 
 
 	void D3D11RenderUtility::initClearQuadResources()
 	void D3D11RenderUtility::initClearQuadResources()

+ 14 - 3
CamelotD3D9Renderer/Include/CmD3D9RenderSystem.h

@@ -126,7 +126,7 @@ namespace CamelotFramework
 		/**
 		/**
 		 * @copydoc RenderSystem::setViewport()
 		 * @copydoc RenderSystem::setViewport()
 		 */
 		 */
-		void setViewport(ViewportPtr& vp);	
+		void setViewport(const ViewportPtr& vp);	
 
 
 		/**
 		/**
 		 * @copydoc RenderSystem::beginFrame()
 		 * @copydoc RenderSystem::beginFrame()
@@ -149,8 +149,16 @@ namespace CamelotFramework
 		void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexCount);
 		void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexCount);
 
 
         void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom);
         void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom);
-        void clear(RenderTargetPtr target, UINT32 buffers, const Color& colour = Color::Black, 
-            float depth = 1.0f, UINT16 stencil = 0, const Rect& clearArea = Rect::EMPTY);
+
+		/**
+		 * @copydoc RenderSystem::clearRenderTarget()
+		 */
+		void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
+
+		/**
+		 * @copydoc RenderSystem::clearViewport()
+		 */
+		void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
 
 
 		void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest, bool forGpuProgram = false);
 		void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest, bool forGpuProgram = false);
 
 
@@ -184,6 +192,7 @@ namespace CamelotFramework
 		/// instance
 		/// instance
 		HINSTANCE mhInstance;
 		HINSTANCE mhInstance;
 
 
+		UINT32 mViewportLeft, mViewportTop, mViewportWidth, mViewportHeight;
 		// Scissor test rectangle
 		// Scissor test rectangle
 		RECT mScissorRect;
 		RECT mScissorRect;
 		/// List of D3D drivers installed (video cards)
 		/// List of D3D drivers installed (video cards)
@@ -517,6 +526,8 @@ namespace CamelotFramework
 		 */
 		 */
 		void setStencilRefValue(UINT32 refValue);
 		void setStencilRefValue(UINT32 refValue);
 
 
+		void clearArea(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0, const Rect& clearArea = Rect::EMPTY);
+
 		/// Notify when a device has been lost.
 		/// Notify when a device has been lost.
 		void notifyOnDeviceLost(D3D9Device* device);
 		void notifyOnDeviceLost(D3D9Device* device);
 
 

+ 36 - 20
CamelotD3D9Renderer/Source/CmD3D9RenderSystem.cpp

@@ -85,6 +85,7 @@ namespace CamelotFramework
 		: mTexStageDesc(nullptr)
 		: mTexStageDesc(nullptr)
 		, mNumTexStages(0)
 		, mNumTexStages(0)
 		, mCurrentDrawOperation(DOT_TRIANGLE_LIST)
 		, mCurrentDrawOperation(DOT_TRIANGLE_LIST)
+		, mViewportLeft(0), mViewportTop(0), mViewportWidth(0), mViewportHeight(0)
 	{
 	{
 		// update singleton access pointer.
 		// update singleton access pointer.
 		msD3D9RenderSystem = this;
 		msD3D9RenderSystem = this;
@@ -1132,7 +1133,7 @@ namespace CamelotFramework
 		}
 		}
 	}
 	}
 	//---------------------------------------------------------------------
 	//---------------------------------------------------------------------
-	void D3D9RenderSystem::setViewport(ViewportPtr& vp)
+	void D3D9RenderSystem::setViewport(const ViewportPtr& vp)
 	{
 	{
 		THROW_IF_NOT_CORE_THREAD;
 		THROW_IF_NOT_CORE_THREAD;
 
 
@@ -1149,6 +1150,11 @@ namespace CamelotFramework
 		setCullingMode( mCullingMode );
 		setCullingMode( mCullingMode );
 
 
 		// set viewport dimensions
 		// set viewport dimensions
+		mViewportWidth = vp->getWidth();
+		mViewportHeight = vp->getHeight();
+		mViewportLeft = vp->getLeft();
+		mViewportTop = vp->getTop();
+
 		d3dvp.X = vp->getLeft();
 		d3dvp.X = vp->getLeft();
 		d3dvp.Y = vp->getTop();
 		d3dvp.Y = vp->getTop();
 		d3dvp.Width = vp->getWidth();
 		d3dvp.Width = vp->getWidth();
@@ -1366,18 +1372,30 @@ namespace CamelotFramework
 			}
 			}
 		}
 		}
 	}
 	}
-	//---------------------------------------------------------------------
-	void D3D9RenderSystem::clear(RenderTargetPtr target, UINT32 buffers, 
-		const Color& color, float depth, UINT16 stencil, const Rect& clearArea)
+
+	void D3D9RenderSystem::clearRenderTarget(UINT32 buffers, const Color& color, float depth, UINT16 stencil)
+	{
+		if(mActiveRenderTarget == nullptr)
+			return;
+
+		Rect clearRect(0, 0, mActiveRenderTarget->getWidth(), mActiveRenderTarget->getHeight());
+
+		clearArea(buffers, color, depth, stencil, clearRect);
+	}
+
+	void D3D9RenderSystem::clearViewport(UINT32 buffers, const Color& color, float depth, UINT16 stencil)
+	{
+		Rect clearRect(mViewportLeft, mViewportTop, mViewportWidth, mViewportHeight);
+
+		clearArea(buffers, color, depth, stencil, clearRect);
+	}
+
+	void D3D9RenderSystem::clearArea(UINT32 buffers, const Color& color, float depth, UINT16 stencil, const Rect& clearRect)
 	{
 	{
 		THROW_IF_NOT_CORE_THREAD;
 		THROW_IF_NOT_CORE_THREAD;
 
 
-		RenderTargetPtr previousRenderTarget = mActiveRenderTarget;
-		if(target != mActiveRenderTarget)
-		{
-			previousRenderTarget = mActiveRenderTarget;
-			setRenderTarget(target);
-		}
+		if(mActiveRenderTarget == nullptr)
+			return;
 
 
 		DWORD flags = 0;
 		DWORD flags = 0;
 		if (buffers & FBT_COLOR)
 		if (buffers & FBT_COLOR)
@@ -1395,14 +1413,17 @@ namespace CamelotFramework
 			flags |= D3DCLEAR_STENCIL;
 			flags |= D3DCLEAR_STENCIL;
 		}
 		}
 
 
-		if(clearArea.width > 0 && clearArea.height > 0)
+		bool clearEntireTarget = clearRect.width == 0 || clearRect.height == 0;
+		clearEntireTarget |= (clearRect.x == 0 && clearRect.y == 0 && clearRect.width == mActiveRenderTarget->getWidth() && clearRect.height == mActiveRenderTarget->getHeight());
+
+		if(!clearEntireTarget)
 		{
 		{
 			D3DRECT clearD3DRect;
 			D3DRECT clearD3DRect;
-			clearD3DRect.x1 = clearArea.x;
-			clearD3DRect.x2 = clearD3DRect.x1 + clearArea.width;
+			clearD3DRect.x1 = clearRect.x;
+			clearD3DRect.x2 = clearD3DRect.x1 + clearRect.width;
 
 
-			clearD3DRect.y1 = clearArea.y;
-			clearD3DRect.y2 = clearD3DRect.y1 + clearArea.height;
+			clearD3DRect.y1 = clearRect.y;
+			clearD3DRect.y2 = clearD3DRect.y1 + clearRect.height;
 
 
 			HRESULT hr;
 			HRESULT hr;
 			if(FAILED( hr = getActiveD3D9Device()->Clear(1, &clearD3DRect, flags, color.getAsBGRA(), depth, stencil)))
 			if(FAILED( hr = getActiveD3D9Device()->Clear(1, &clearD3DRect, flags, color.getAsBGRA(), depth, stencil)))
@@ -1420,11 +1441,6 @@ namespace CamelotFramework
 				CM_EXCEPT(RenderingAPIException, "Error clearing frame buffer : " + msg);
 				CM_EXCEPT(RenderingAPIException, "Error clearing frame buffer : " + msg);
 			}
 			}
 		}
 		}
-
-		if(previousRenderTarget != nullptr && target != previousRenderTarget)
-		{
-			setRenderTarget(previousRenderTarget);
-		}
 	}
 	}
 	//---------------------------------------------------------------------
 	//---------------------------------------------------------------------
 	IDirect3D9*	D3D9RenderSystem::getDirect3D9()
 	IDirect3D9*	D3D9RenderSystem::getDirect3D9()

+ 11 - 6
CamelotGLRenderer/Include/CmGLRenderSystem.h

@@ -114,7 +114,7 @@ namespace CamelotFramework {
 		/**
 		/**
 		 * @copydoc RenderSystem::setViewport()
 		 * @copydoc RenderSystem::setViewport()
 		 */
 		 */
-        void setViewport(ViewportPtr& vp);
+        void setViewport(const ViewportPtr& vp);
 
 
 		/**
 		/**
 		 * @copydoc RenderSystem::bindGpuProgram()
 		 * @copydoc RenderSystem::bindGpuProgram()
@@ -152,11 +152,14 @@ namespace CamelotFramework {
 		void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexCount);
 		void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexCount);
 
 
 		/**
 		/**
-		 * @copydoc RenderSystem::clear()
+		 * @copydoc RenderSystem::clearRenderTarget()
 		 */
 		 */
-        void clear(RenderTargetPtr target, UINT32 buffers, 
-            const Color& colour = Color::Black, 
-            float depth = 1.0f, UINT16 stencil = 0, const Rect& clearArea = Rect::EMPTY);
+		void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
+
+		/**
+		 * @copydoc RenderSystem::clearViewport()
+		 */
+		void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
 
 
         /**
         /**
 		 * @copydoc RenderSystem::getColorVertexElementType()
 		 * @copydoc RenderSystem::getColorVertexElementType()
@@ -241,7 +244,7 @@ namespace CamelotFramework {
         /// Store last depth write state
         /// Store last depth write state
         bool mDepthWrite;
         bool mDepthWrite;
 		/// Store last colour write state
 		/// Store last colour write state
-		bool mColourWrite[4];
+		bool mColorWrite[4];
 
 
         GLint convertCompareFunction(CompareFunction func) const;
         GLint convertCompareFunction(CompareFunction func) const;
         GLint convertStencilOp(StencilOperation op, bool invert = false) const;
         GLint convertStencilOp(StencilOperation op, bool invert = false) const;
@@ -578,6 +581,8 @@ namespace CamelotFramework {
 		 */
 		 */
 		void endDraw();
 		void endDraw();
 
 
+		void clearArea(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0, const Rect& clearArea = Rect::EMPTY);
+
 		void setActiveProgram(GpuProgramType gptype, GLSLGpuProgramPtr program);
 		void setActiveProgram(GpuProgramType gptype, GLSLGpuProgramPtr program);
 		GLSLGpuProgramPtr getActiveProgram(GpuProgramType gptype) const;
 		GLSLGpuProgramPtr getActiveProgram(GpuProgramType gptype) const;
     };
     };

+ 41 - 31
CamelotGLRenderer/Source/CmGLRenderSystem.cpp

@@ -100,7 +100,7 @@ namespace CamelotFramework
 
 
 		mViewMatrix = Matrix4::IDENTITY;
 		mViewMatrix = Matrix4::IDENTITY;
 
 
-		mColourWrite[0] = mColourWrite[1] = mColourWrite[2] = mColourWrite[3] = true;
+		mColorWrite[0] = mColorWrite[1] = mColorWrite[2] = mColorWrite[3] = true;
 
 
 		mCurrentContext = 0;
 		mCurrentContext = 0;
 		mMainContext = 0;
 		mMainContext = 0;
@@ -565,7 +565,7 @@ namespace CamelotFramework
 		setStencilRefValue(stencilRefValue);
 		setStencilRefValue(stencilRefValue);
 	}
 	}
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
-	void GLRenderSystem::setViewport(ViewportPtr& vp)
+	void GLRenderSystem::setViewport(const ViewportPtr& vp)
 	{
 	{
 		THROW_IF_NOT_CORE_THREAD;
 		THROW_IF_NOT_CORE_THREAD;
 
 
@@ -716,7 +716,7 @@ namespace CamelotFramework
 
 
 		endDraw();
 		endDraw();
 	}
 	}
-	//---------------------------------------------------------------------
+
 	void GLRenderSystem::setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
 	void GLRenderSystem::setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
 	{
 	{
 		THROW_IF_NOT_CORE_THREAD;
 		THROW_IF_NOT_CORE_THREAD;
@@ -726,32 +726,44 @@ namespace CamelotFramework
 		mScissorLeft = left;
 		mScissorLeft = left;
 		mScissorRight = right;
 		mScissorRight = right;
 	}
 	}
-	//---------------------------------------------------------------------
-	void GLRenderSystem::clear(RenderTargetPtr target, UINT32 buffers, 
-		const Color& colour, float depth, UINT16 stencil, const Rect& clearArea)
+
+	void GLRenderSystem::clearRenderTarget(UINT32 buffers, const Color& color, float depth, UINT16 stencil)
+	{
+		if(mActiveRenderTarget == nullptr)
+			return;
+
+		Rect clearRect(0, 0, mActiveRenderTarget->getWidth(), mActiveRenderTarget->getHeight());
+
+		clearArea(buffers, color, depth, stencil, clearRect);
+	}
+
+	void GLRenderSystem::clearViewport(UINT32 buffers, const Color& color, float depth, UINT16 stencil)
+	{
+		Rect clearRect(mViewportLeft, mViewportTop, mViewportWidth, mViewportHeight);
+
+		clearArea(buffers, color, depth, stencil, clearRect);
+	}
+
+	void GLRenderSystem::clearArea(UINT32 buffers, const Color& color, float depth, UINT16 stencil, const Rect& clearRect)
 	{
 	{
 		THROW_IF_NOT_CORE_THREAD;
 		THROW_IF_NOT_CORE_THREAD;
 
 
-		RenderTargetPtr previousRenderTarget = mActiveRenderTarget;
-		if(target != mActiveRenderTarget)
-		{
-			previousRenderTarget = mActiveRenderTarget;
-			setRenderTarget(target);
-		}
+		if(mActiveRenderTarget == nullptr)
+			return;
 
 
-		bool colourMask = !mColourWrite[0] || !mColourWrite[1] 
-		|| !mColourWrite[2] || !mColourWrite[3]; 
+		bool colorMask = !mColorWrite[0] || !mColorWrite[1] 
+		|| !mColorWrite[2] || !mColorWrite[3]; 
 
 
 		GLbitfield flags = 0;
 		GLbitfield flags = 0;
 		if (buffers & FBT_COLOR)
 		if (buffers & FBT_COLOR)
 		{
 		{
 			flags |= GL_COLOR_BUFFER_BIT;
 			flags |= GL_COLOR_BUFFER_BIT;
 			// Enable buffer for writing if it isn't
 			// Enable buffer for writing if it isn't
-			if (colourMask)
+			if (colorMask)
 			{
 			{
 				glColorMask(true, true, true, true);
 				glColorMask(true, true, true, true);
 			}
 			}
-			glClearColor(colour.r, colour.g, colour.b, colour.a);
+			glClearColor(color.r, color.g, color.b, color.a);
 		}
 		}
 		if (buffers & FBT_DEPTH)
 		if (buffers & FBT_DEPTH)
 		{
 		{
@@ -784,16 +796,19 @@ namespace CamelotFramework
 			glDisable(GL_SCISSOR_TEST);
 			glDisable(GL_SCISSOR_TEST);
 		}
 		}
 
 
-		if(clearArea.width > 0 && clearArea.height > 0)
+		bool clearEntireTarget = clearRect.width == 0 || clearRect.height == 0;
+		clearEntireTarget |= (clearRect.x == 0 && clearRect.y == 0 && clearRect.width == mActiveRenderTarget->getWidth() && clearRect.height == mActiveRenderTarget->getHeight());
+
+		if(!clearEntireTarget)
 		{
 		{
-			setScissorRect(clearArea.x, clearArea.y, clearArea.width, clearArea.height);
+			setScissorRect(clearRect.x, clearRect.y, clearRect.x + clearRect.width, clearRect.y + clearRect.height);
 			setScissorTestEnable(true);			
 			setScissorTestEnable(true);			
 		}
 		}
 
 
 		// Clear buffers
 		// Clear buffers
 		glClear(flags);
 		glClear(flags);
 
 
-		if(clearArea.width > 0 && clearArea.height > 0)
+		if(!clearEntireTarget)
 		{
 		{
 			setScissorTestEnable(false);	
 			setScissorTestEnable(false);	
 		}
 		}
@@ -814,19 +829,14 @@ namespace CamelotFramework
 		{
 		{
 			glDepthMask( GL_FALSE );
 			glDepthMask( GL_FALSE );
 		}
 		}
-		if (colourMask && (buffers & FBT_COLOR))
+		if (colorMask && (buffers & FBT_COLOR))
 		{
 		{
-			glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
+			glColorMask(mColorWrite[0], mColorWrite[1], mColorWrite[2], mColorWrite[3]);
 		}
 		}
 		if (buffers & FBT_STENCIL)
 		if (buffers & FBT_STENCIL)
 		{
 		{
 			glStencilMask(mStencilWriteMask);
 			glStencilMask(mStencilWriteMask);
 		}
 		}
-
-		if(previousRenderTarget != nullptr && target != previousRenderTarget)
-		{
-			setRenderTarget(previousRenderTarget);
-		}
 	}
 	}
 
 
 	/************************************************************************/
 	/************************************************************************/
@@ -1144,10 +1154,10 @@ namespace CamelotFramework
 	{
 	{
 		glColorMask(red, green, blue, alpha);
 		glColorMask(red, green, blue, alpha);
 		// record this
 		// record this
-		mColourWrite[0] = red;
-		mColourWrite[1] = blue;
-		mColourWrite[2] = green;
-		mColourWrite[3] = alpha;
+		mColorWrite[0] = red;
+		mColorWrite[1] = blue;
+		mColorWrite[2] = green;
+		mColorWrite[3] = alpha;
 	}
 	}
 	//---------------------------------------------------------------------
 	//---------------------------------------------------------------------
 	void GLRenderSystem::setPolygonMode(PolygonMode level)
 	void GLRenderSystem::setPolygonMode(PolygonMode level)
@@ -1379,7 +1389,7 @@ namespace CamelotFramework
 		// clearFrameBuffer would be wrong because the value we are recorded may be
 		// clearFrameBuffer would be wrong because the value we are recorded may be
 		// difference with the really state stored in GL context.
 		// difference with the really state stored in GL context.
 		glDepthMask(mDepthWrite);
 		glDepthMask(mDepthWrite);
-		glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
+		glColorMask(mColorWrite[0], mColorWrite[1], mColorWrite[2], mColorWrite[3]);
 		glStencilMask(mStencilWriteMask);
 		glStencilMask(mStencilWriteMask);
 
 
 	}
 	}