Marko Pintera 13 лет назад
Родитель
Сommit
e846997473

+ 2 - 0
CamelotD3D9Renderer/CamelotD3D9Renderer.vcxproj

@@ -167,6 +167,7 @@
     <ClInclude Include="Include\CmD3D9Prerequisites.h" />
     <ClInclude Include="Include\CmD3D9RenderSystem.h" />
     <ClInclude Include="Include\CmD3D9RenderSystemFactory.h" />
+    <ClInclude Include="Include\CmD3D9RenderTexture.h" />
     <ClInclude Include="Include\CmD3D9RenderWindow.h" />
     <ClInclude Include="Include\CmD3D9RenderWindowManager.h" />
     <ClInclude Include="Include\CmD3D9Resource.h" />
@@ -197,6 +198,7 @@
     <ClCompile Include="Source\CmD3D9MultiRenderTarget.cpp" />
     <ClCompile Include="Source\CmD3D9RenderSystem.cpp" />
     <ClCompile Include="Source\CmD3D9RenderSystemFactory.cpp" />
+    <ClCompile Include="Source\CmD3D9RenderTexture.cpp" />
     <ClCompile Include="Source\CmD3D9RenderWindow.cpp" />
     <ClCompile Include="Source\CmD3D9RenderWindowManager.cpp" />
     <ClCompile Include="Source\CmD3D9Resource.cpp" />

+ 6 - 0
CamelotD3D9Renderer/CamelotD3D9Renderer.vcxproj.filters

@@ -105,6 +105,9 @@
     <ClInclude Include="Include\CmD3D9DepthStencilBuffer.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="Include\CmD3D9RenderTexture.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\CmD3D9Device.cpp">
@@ -191,5 +194,8 @@
     <ClCompile Include="Source\CmD3D9DepthStencilBuffer.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="Source\CmD3D9RenderTexture.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 4 - 1
CamelotD3D9Renderer/Include/CmD3D9DepthStencilBuffer.h

@@ -9,7 +9,10 @@ namespace CamelotEngine
 	{
 	public:
 		D3D9DepthStencilBuffer(UINT32 bitDepth, UINT32 width, UINT32 height, UINT32 fsaa, const String &fsaaHint);
+		~D3D9DepthStencilBuffer();
 
-		// TODO - Add D3D9 specific implementation
+		IDirect3DSurface9* getSurface() const { return mSurface; }
+	protected:
+		IDirect3DSurface9* mSurface;
 	};
 }

+ 1 - 6
CamelotD3D9Renderer/Include/CmD3D9HardwarePixelBuffer.h

@@ -44,7 +44,7 @@ namespace CamelotEngine {
 		~D3D9HardwarePixelBuffer();
 
 		/// Call this to associate a D3D surface or volume with this pixel buffer
-		void bind(IDirect3DDevice9 *dev, IDirect3DSurface9 *mSurface, IDirect3DSurface9* fsaaSurface,
+		void bind(IDirect3DDevice9 *dev, IDirect3DSurface9 *mSurface,
 			bool writeGamma, UINT32 fsaa, const String& srcName, IDirect3DBaseTexture9 *mipTex);
 		void bind(IDirect3DDevice9 *dev, IDirect3DVolume9 *mVolume, IDirect3DBaseTexture9 *mipTex);
 
@@ -66,9 +66,6 @@ namespace CamelotEngine {
 		/// Accessor for surface
 		IDirect3DSurface9 *getSurface(IDirect3DDevice9* d3d9Device);
 
-		/// Accessor for AA surface
-		IDirect3DSurface9 *getFSAASurface(IDirect3DDevice9* d3d9Device);
-
 		/// Release surfaces held by this pixel buffer.
 		void releaseSurfaces(IDirect3DDevice9* d3d9Device);
 
@@ -87,8 +84,6 @@ namespace CamelotEngine {
 		{			
 			/// Surface abstracted by this buffer
 			IDirect3DSurface9* surface;
-			/// AA Surface abstracted by this buffer
-			IDirect3DSurface9* fSAASurface;
 			/// Volume abstracted by this buffer
 			IDirect3DVolume9* volume;
 			/// Temporary surface in main memory if direct locking of mSurface is not possible

+ 1 - 0
CamelotD3D9Renderer/Include/CmD3D9Prerequisites.h

@@ -83,6 +83,7 @@ namespace CamelotEngine
 	class D3D9Resource;
 
 	typedef std::shared_ptr<D3D9RenderWindow> D3D9RenderWindowPtr;
+	typedef std::shared_ptr<D3D9Texture> D3D9TexturePtr;
 
 	enum TypeID_D3D9
 	{

+ 4 - 64
CamelotD3D9Renderer/Include/CmD3D9RenderSystem.h

@@ -118,6 +118,10 @@ namespace CamelotEngine
 		static IDirect3DDevice9* getResourceCreationDevice(UINT index);
 		static IDirect3DDevice9* getActiveD3D9Device();
 
+		/// Take in some requested FSAA settings and output supported D3D settings
+		void determineFSAASettings(IDirect3DDevice9* d3d9Device, UINT32 fsaa, const String& fsaaHint, D3DFORMAT d3dPixelFormat, 
+			bool fullScreen, D3DMULTISAMPLE_TYPE *outMultisampleType, DWORD *outMultisampleQuality);
+
 	private:
 		/// Direct3D
 		IDirect3D9*	 mpD3D;		
@@ -181,7 +185,6 @@ namespace CamelotEngine
 
 		size_t mLastVertexSourceCount;
 
-
         /// Internal method for populating the capabilities structure
 		virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
 		RenderSystemCapabilities* updateRenderSystemCapabilities(D3D9RenderWindow* renderWindow);
@@ -192,46 +195,10 @@ namespace CamelotEngine
         void convertVertexShaderCaps(RenderSystemCapabilities* rsc) const;
         void convertPixelShaderCaps(RenderSystemCapabilities* rsc) const;
 		bool checkVertexTextureFormats(D3D9RenderWindow* renderWindow) const;
-		
-        HashMap<IDirect3DDevice9*, unsigned short> mCurrentLights;
-        /// Saved last view matrix
-        Matrix4 mViewMatrix;
-
-		D3DXMATRIX mDxViewMat, mDxProjMat, mDxWorldMat;
 	
 		typedef vector<D3D9RenderWindow*>::type D3D9RenderWindowList;
 		// List of additional windows after the first (swap chains)
 		D3D9RenderWindowList mRenderWindows;
-		
-		/** Mapping of texture format -> DepthStencil. Used as cache by _getDepthStencilFormatFor
-		*/
-		typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash;
-		DepthStencilHash mDepthStencilHash;
-
-		/** Mapping of depthstencil format -> depthstencil buffer
-			Keep one depthstencil buffer around for every format that is used, it must be large
-			enough to hold the largest rendering target.
-			This is used as cache by _getDepthStencilFor.
-		*/
-		struct ZBufferIdentifier
-		{
-			IDirect3DDevice9* device;
-			D3DFORMAT format;
-			D3DMULTISAMPLE_TYPE multisampleType;
-		};
-		struct ZBufferRef
-		{
-			IDirect3DSurface9 *surface;
-			size_t width, height;
-		};
-		struct ZBufferIdentifierComparator
-		{
-			bool operator()(const ZBufferIdentifier& z0, const ZBufferIdentifier& z1) const;
-		};
-		
-		typedef deque<ZBufferRef>::type ZBufferRefQueue;
-		typedef map<ZBufferIdentifier, ZBufferRefQueue, ZBufferIdentifierComparator>::type ZBufferHash;
-		ZBufferHash mZBufferHash;		
 
 	protected:
 		// I know that's a lot of friends, but I'd rather have friend classes than exposing the needed methods
@@ -256,35 +223,11 @@ namespace CamelotEngine
 
 		HINSTANCE getInstanceHandle() const { return mhInstance; }
 
-		/**
-			Get the matching Z-Buffer identifier for a certain render target
-		*/
-		ZBufferIdentifier getZBufferIdentifier(RenderTarget* rt);
-
-		/** Check which depthStencil formats can be used with a certain pixel format,
-			and return the best suited.
-		*/
-		D3DFORMAT getDepthStencilFormatFor(D3DFORMAT fmt);
-
-		/** Get a depth stencil surface that is compatible with an internal pixel format and
-			multisample type.
-			@returns A directx surface, or 0 if there is no compatible depthstencil possible.
-		*/
-		IDirect3DSurface9* getDepthStencilFor(D3DFORMAT fmt, D3DMULTISAMPLE_TYPE multisample, DWORD multisample_quality, UINT32 width, UINT32 height);
-
-		/** Clear all cached depth stencil surfaces
-		*/
-		void cleanupDepthStencils(IDirect3DDevice9* d3d9Device);
-
         /** Check whether or not filtering is supported for the precise texture format requested
         with the given usage options.
         */
         bool checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage);
 
-		/// Take in some requested FSAA settings and output supported D3D settings
-		void determineFSAASettings(IDirect3DDevice9* d3d9Device, UINT32 fsaa, const String& fsaaHint, D3DFORMAT d3dPixelFormat, 
-			bool fullScreen, D3DMULTISAMPLE_TYPE *outMultisampleType, DWORD *outMultisampleQuality);
-
 		/**
 		 * @brief	Called internally by RenderWindowManager whenever a new window is created.
 		 */
@@ -521,9 +464,6 @@ namespace CamelotEngine
 
 		/// Notify when a device has been reset.
 		void notifyOnDeviceReset(D3D9Device* device);
-		
-		typedef map<RenderTarget*, ZBufferRef>::type TargetDepthStencilMap;
-		TargetDepthStencilMap mCheckedOutTextures;
 	};
 }
 #endif

+ 24 - 0
CamelotD3D9Renderer/Include/CmD3D9RenderTexture.h

@@ -0,0 +1,24 @@
+#pragma once
+
+#include "CmD3D9Prerequisites.h"
+#include "CmRenderTexture.h"
+
+namespace CamelotEngine
+{
+	/// RenderTexture implementation for D3D9
+	class CM_D3D9_EXPORT D3D9RenderTexture : public RenderTexture
+	{
+	public:
+		D3D9RenderTexture();
+		virtual ~D3D9RenderTexture();
+
+		bool requiresTextureFlipping() const { return false; }
+		virtual void getCustomAttribute_internal(const String& name, void* pData);
+
+	protected:
+		IDirect3DSurface9* mColorSurface;
+		IDirect3DSurface9* mDepthStencilSurface;
+
+		void createInternalResourcesImpl();
+	};
+}

+ 14 - 34
CamelotD3D9Renderer/Include/CmD3D9Texture.h

@@ -61,6 +61,20 @@ namespace CamelotEngine {
 		/// Will this texture need to be in the default pool?
 		bool useDefaultPool();
 
+		/** Return hardware pixel buffer for a surface. This buffer can then
+			be used to copy data from and to a particular level of the texture.
+			@param face 	Face number, in case of a cubemap texture. Must be 0
+							for other types of textures.
+                            For cubemaps, this is one of 
+                            +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
+			@param mipmap	Mipmap level. This goes from 0 for the first, largest
+							mipmap level to getNumMipmaps()-1 for the smallest.
+			@returns	A shared pointer to a hardware pixel buffer
+			@remarks	The buffer is invalidated when the resource is unloaded or destroyed.
+						Do not use it after the lifetime of the containing texture.
+		*/
+		HardwarePixelBufferPtr getBuffer(UINT32 face, UINT32 mipmap);
+
 		// Called immediately after the Direct3D device has been created.
 		virtual void notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device);
 
@@ -160,20 +174,6 @@ namespace CamelotEngine {
 		/// internal method, the cube map face name for the spec. face index
 		String _getCubeFaceName(unsigned char face) const
 		{ assert(face < 6); return mCubeFaceNames[face]; }
-		
-		/** Return hardware pixel buffer for a surface. This buffer can then
-			be used to copy data from and to a particular level of the texture.
-			@param face 	Face number, in case of a cubemap texture. Must be 0
-							for other types of textures.
-                            For cubemaps, this is one of 
-                            +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
-			@param mipmap	Mipmap level. This goes from 0 for the first, largest
-							mipmap level to getNumMipmaps()-1 for the smallest.
-			@returns	A shared pointer to a hardware pixel buffer
-			@remarks	The buffer is invalidated when the resource is unloaded or destroyed.
-						Do not use it after the lifetime of the containing texture.
-		*/
-		HardwarePixelBufferPtr getBuffer(UINT32 face, UINT32 mipmap);
 
 		/// internal method, create D3D9HardwarePixelBuffers for every face and
 		/// mipmap level. This method must be called after the D3D texture object was created
@@ -193,26 +193,6 @@ namespace CamelotEngine {
 
 		void determinePool();
     };
-
-	typedef std::shared_ptr<D3D9Texture> D3D9TexturePtr;
-
-    /// RenderTexture implementation for D3D9
-    class CM_D3D9_EXPORT D3D9RenderTexture : public RenderTexture
-    {
-    public:
-		D3D9RenderTexture(const String &name, D3D9HardwarePixelBuffer *buffer, bool writeGamma, UINT32 fsaa);
-        ~D3D9RenderTexture() {}
-
-		virtual void update(bool swap);
-
-		virtual void getCustomAttribute_internal( const String& name, void *pData );
-
-		bool requiresTextureFlipping() const { return false; }
-	protected:
-		D3D9HardwarePixelBuffer* mBuffer;
-
-	};
-
 }
 
 #endif

+ 34 - 2
CamelotD3D9Renderer/Source/CmD3D9DepthStencilBuffer.cpp

@@ -1,8 +1,40 @@
 #include "CmD3D9DepthStencilBuffer.h"
+#include "CmD3D9RenderSystem.h"
 
 namespace CamelotEngine
 {
 	D3D9DepthStencilBuffer::D3D9DepthStencilBuffer(UINT32 bitDepth, UINT32 width, UINT32 height, UINT32 fsaa, const String &fsaaHint)
-		:DepthStencilBuffer(bitDepth, width, height, fsaa, fsaaHint)
-	{ }
+		:DepthStencilBuffer(bitDepth, width, height, fsaa, fsaaHint), mSurface(nullptr)
+	{
+		D3D9RenderSystem* rs = static_cast<D3D9RenderSystem*>(RenderSystem::instancePtr());
+
+		IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
+
+		D3DFORMAT format = D3DFMT_D24S8;
+		D3DMULTISAMPLE_TYPE msType = D3DMULTISAMPLE_NONE;
+		DWORD msQuality = 0;
+		rs->determineFSAASettings(d3d9Device, fsaa, fsaaHint, format, false, &msType, &msQuality);
+
+		/// If not, create the depthstencil surface
+		HRESULT hr = d3d9Device->CreateDepthStencilSurface( 
+			static_cast<UINT>(width), 
+			static_cast<UINT>(height), 
+			format, 
+			msType, 
+			msQuality, 
+			TRUE,  // discard true or false?
+			&mSurface, 
+			NULL);
+
+		if(FAILED(hr))
+		{
+			String msg = DXGetErrorDescription(hr);
+			CM_EXCEPT(InvalidParametersException, "Error CreateDepthStencilSurface : " + msg);
+		}
+	}
+
+	D3D9DepthStencilBuffer::~D3D9DepthStencilBuffer()
+	{
+		SAFE_RELEASE(mSurface);
+	}
 }

+ 0 - 3
CamelotD3D9Renderer/Source/CmD3D9Device.cpp

@@ -343,9 +343,6 @@ namespace CamelotEngine
 		// Notify all listener before device is rested
 		renderSystem->notifyOnDeviceLost(this);
 
-		// Cleanup depth stencils surfaces.
-		renderSystem->cleanupDepthStencils(mpDevice);
-
 		updatePresentationParameters();
 
 		RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();

+ 0 - 15
CamelotD3D9Renderer/Source/CmD3D9HardwarePixelBuffer.cpp

@@ -60,7 +60,6 @@ namespace CamelotEngine
 	}
 	//-----------------------------------------------------------------------------  
 	void D3D9HardwarePixelBuffer::bind(IDirect3DDevice9 *dev, IDirect3DSurface9 *surface, 
-									   IDirect3DSurface9* fsaaSurface,
 									   bool writeGamma, UINT32 fsaa, const String& srcName,
 									   IDirect3DBaseTexture9 *mipTex)
 	{
@@ -79,7 +78,6 @@ namespace CamelotEngine
 		bufferResources->mipTex = mipTex;
 		bufferResources->surface = surface;
 		bufferResources->surface->AddRef();
-		bufferResources->fSAASurface = fsaaSurface;
 
 		D3DSURFACE_DESC desc;
 		if(surface->GetDesc(&desc) != D3D_OK)
@@ -899,17 +897,4 @@ namespace CamelotEngine
 
 		return bufferResources->surface;
 	}
-	//-----------------------------------------------------------------------------   
-	IDirect3DSurface9* D3D9HardwarePixelBuffer::getFSAASurface(IDirect3DDevice9* d3d9Device)
-	{
-		BufferResources* bufferResources = getBufferResources(d3d9Device);
-
-		if (bufferResources	== NULL)
-		{
-			mOwnerTexture->createTextureResources(d3d9Device);
-			bufferResources = getBufferResources(d3d9Device);
-		}
-	
-		return bufferResources->fSAASurface;
-	}
 };

+ 2 - 207
CamelotD3D9Renderer/Source/CmD3D9RenderSystem.cpp

@@ -168,8 +168,6 @@ namespace CamelotEngine
 
 		mLastVertexSourceCount = 0;
 
-		mCurrentLights.clear();	
-
 		RenderWindow* autoWindow = NULL;
 
 		// Init using current settings
@@ -1083,25 +1081,9 @@ namespace CamelotEngine
 
 		IDirect3DSurface9* pDepth = NULL;
 
-		//Check if we saved a depth buffer for this target
-		TargetDepthStencilMap::iterator savedTexture = mCheckedOutTextures.find(target);
-		if (savedTexture != mCheckedOutTextures.end())
-		{
-			pDepth = savedTexture->second.surface;
-		}
-
 		if (!pDepth)
 			target->getCustomAttribute_internal( "D3DZBUFFER", &pDepth );
-		if (!pDepth)
-		{
-			/// No depth buffer provided, use our own
-			/// Request a depth stencil that is compatible with the format, multisample type and
-			/// dimensions of the render target.
-			D3DSURFACE_DESC srfDesc;
-			if(FAILED(pBack[0]->GetDesc(&srfDesc)))
-				return; // ?
-			pDepth = getDepthStencilFor(srfDesc.Format, srfDesc.MultiSampleType, srfDesc.MultiSampleQuality, srfDesc.Width, srfDesc.Height);
-		}
+		
 		// Bind render targets
 		UINT32 count = mCurrentCapabilities->getNumMultiRenderTargets();
 		for(UINT32 x=0; x<count; ++x)
@@ -2713,137 +2695,6 @@ namespace CamelotEngine
 		return mCurrentCapabilities;
 	}
 	//---------------------------------------------------------------------
-	D3DFORMAT D3D9RenderSystem::getDepthStencilFormatFor(D3DFORMAT fmt)
-	{
-		/// Check if result is cached
-		DepthStencilHash::iterator i = mDepthStencilHash.find((unsigned int)fmt);
-		if(i != mDepthStencilHash.end())
-			return i->second;
-		/// If not, probe with CheckDepthStencilMatch
-		D3DFORMAT dsfmt = D3DFMT_UNKNOWN;
-
-		/// Get description of primary render target
-		D3D9Device* activeDevice = mDeviceManager->getActiveDevice();
-		IDirect3DSurface9* mSurface = activeDevice->getPrimaryWindow()->getRenderSurface();
-		D3DSURFACE_DESC srfDesc;
-
-		if(!FAILED(mSurface->GetDesc(&srfDesc)))
-		{
-			/// Probe all depth stencil formats
-			/// Break on first one that matches
-			for(size_t x=0; x<NDSFORMATS; ++x)
-			{
-				// Verify that the depth format exists
-				if (mpD3D->CheckDeviceFormat(
-					activeDevice->getAdapterNumber(),
-					activeDevice->getDeviceType(),
-					srfDesc.Format,
-					D3DUSAGE_DEPTHSTENCIL,
-					D3DRTYPE_SURFACE,
-					ddDepthStencilFormats[x]) != D3D_OK)
-				{
-					continue;
-				}
-				// Verify that the depth format is compatible
-				if(mpD3D->CheckDepthStencilMatch(
-					activeDevice->getAdapterNumber(),
-					activeDevice->getDeviceType(), 
-					srfDesc.Format,
-					fmt, ddDepthStencilFormats[x]) == D3D_OK)
-				{
-					dsfmt = ddDepthStencilFormats[x];
-					break;
-				}
-			}
-		}
-		/// Cache result
-		mDepthStencilHash[(unsigned int)fmt] = dsfmt;
-		return dsfmt;
-	}
-	IDirect3DSurface9* D3D9RenderSystem::getDepthStencilFor(D3DFORMAT fmt, 
-		D3DMULTISAMPLE_TYPE multisample, DWORD multisample_quality, UINT32 width, UINT32 height)
-	{
-		D3DFORMAT dsfmt = getDepthStencilFormatFor(fmt);
-		if(dsfmt == D3DFMT_UNKNOWN)
-			return 0;
-		IDirect3DSurface9 *surface = 0;
-
-		/// Check if result is cached
-		ZBufferIdentifier zBufferIdentifer;
-
-
-		zBufferIdentifer.format = dsfmt;
-		zBufferIdentifer.multisampleType = multisample;
-		zBufferIdentifer.device = getActiveD3D9Device();
-
-		ZBufferRefQueue& zBuffers = mZBufferHash[zBufferIdentifer];
-
-		if(!zBuffers.empty())
-		{
-			const ZBufferRef& zBuffer = zBuffers.front();
-			/// Check if size is larger or equal
-			if(zBuffer.width >= width && zBuffer.height >= height)
-			{
-				surface = zBuffer.surface;
-			} 
-			else
-			{
-				/// If not, destroy current buffer
-				zBuffer.surface->Release();
-				zBuffers.pop_front();
-			}
-		}
-		if(!surface)
-		{
-			/// If not, create the depthstencil surface
-			HRESULT hr = getActiveD3D9Device()->CreateDepthStencilSurface( 
-				static_cast<UINT>(width), 
-				static_cast<UINT>(height), 
-				dsfmt, 
-				multisample, 
-				multisample_quality, 
-				TRUE,  // discard true or false?
-				&surface, 
-				NULL);
-			if(FAILED(hr))
-			{
-				String msg = DXGetErrorDescription(hr);
-				CM_EXCEPT(InvalidParametersException, "Error CreateDepthStencilSurface : " + msg);
-			}
-			/// And cache it
-			ZBufferRef zb;
-			zb.surface = surface;
-			zb.width = width;
-			zb.height = height;
-			zBuffers.push_front(zb);
-		}
-		return surface;
-	}
-
-	//---------------------------------------------------------------------
-	void D3D9RenderSystem::cleanupDepthStencils(IDirect3DDevice9* d3d9Device)
-	{
-		for(ZBufferHash::iterator i = mZBufferHash.begin(); i != mZBufferHash.end();)
-		{
-			/// Release buffer
-			if (i->first.device == d3d9Device)
-			{
-				while (!i->second.empty())
-				{
-					IDirect3DSurface9* surface = i->second.front().surface;
-					surface->Release();
-					i->second.pop_front();
-				}
-				ZBufferHash::iterator deadi = i++;
-				mZBufferHash.erase(deadi);
-			}			
-			else
-			{
-				++i;
-			}
-		}		
-	}
-	//---------------------------------------------------------------------
 	void D3D9RenderSystem::setClipPlanesImpl(const PlaneList& clipPlanes)
 	{
 		size_t i;
@@ -2862,16 +2713,7 @@ namespace CamelotEngine
 			dx9ClipPlane.c = plane.normal.z;
 			dx9ClipPlane.d = plane.d;
 
-			if (mVertexProgramBound)
-			{
-				// programmable clips in clip space (ugh)
-				// must transform worldspace planes by view/proj
-				D3DXMATRIX xform;
-				D3DXMatrixMultiply(&xform, &mDxViewMat, &mDxProjMat);
-				D3DXMatrixInverse(&xform, NULL, &xform);
-				D3DXMatrixTranspose(&xform, &xform);
-				D3DXPlaneTransform(&dx9ClipPlane, &dx9ClipPlane, &xform);
-			}
+			// TODO Low priority - Transform planes to clip space?
 
 			hr = getActiveD3D9Device()->SetClipPlane(static_cast<DWORD>(i), dx9ClipPlane);
 			if (FAILED(hr))
@@ -2889,30 +2731,6 @@ namespace CamelotEngine
 		}
 	}
 	//---------------------------------------------------------------------
-	D3D9RenderSystem::ZBufferIdentifier D3D9RenderSystem::getZBufferIdentifier(RenderTarget* rt)
-	{
-		// Retrieve render surfaces (up to CM_MAX_MULTIPLE_RENDER_TARGETS)
-		IDirect3DSurface9* pBack[CM_MAX_MULTIPLE_RENDER_TARGETS];
-		memset(pBack, 0, sizeof(pBack));
-		rt->getCustomAttribute_internal( "DDBACKBUFFER", &pBack );
-		assert(pBack[0]);
-
-		/// Request a depth stencil that is compatible with the format, multisample type and
-		/// dimensions of the render target.
-		D3DSURFACE_DESC srfDesc;
-		HRESULT hr = pBack[0]->GetDesc(&srfDesc);
-		assert(!(FAILED(hr)));
-		D3DFORMAT dsfmt = getDepthStencilFormatFor(srfDesc.Format);
-		assert(dsfmt != D3DFMT_UNKNOWN);
-
-		/// Build identifier and return
-		ZBufferIdentifier zBufferIdentifier;
-		zBufferIdentifier.format = dsfmt;
-		zBufferIdentifier.multisampleType = srfDesc.MultiSampleType;
-		zBufferIdentifier.device = getActiveD3D9Device();
-		return zBufferIdentifier;
-	}
-	//---------------------------------------------------------------------
 	HRESULT D3D9RenderSystem::__SetRenderState(D3DRENDERSTATETYPE state, DWORD value)
 	{
 		HRESULT hr;
@@ -2966,29 +2784,6 @@ namespace CamelotEngine
 		getActiveD3D9Device()->GetSamplerState(static_cast<DWORD>(unit), D3DSAMP_MAXANISOTROPY, &oldVal);
 		return oldVal;
 	}
-
-	//---------------------------------------------------------------------
-	bool D3D9RenderSystem::ZBufferIdentifierComparator::operator()( const ZBufferIdentifier& z0, const ZBufferIdentifier& z1 ) const
-	{
-		if (z0.device < z1.device)
-			return true;
-
-		if (z0.device == z1.device)
-		{
-			if (z0.format < z1.format)
-				return true;
-
-			if (z0.format == z1.format)
-			{
-				if (z0.multisampleType < z1.multisampleType)
-					return true;
-			}
-		}
-
-		return false;
-	}
-
-
 }
 
 #undef THROW_IF_NOT_RENDER_THREAD

+ 51 - 0
CamelotD3D9Renderer/Source/CmD3D9RenderTexture.cpp

@@ -0,0 +1,51 @@
+#include "CmD3D9RenderTexture.h"
+#include "CmD3D9Texture.h"
+#include "CmD3D9HardwarePixelBuffer.h"
+#include "CmD3D9RenderSystem.h"
+#include "CmD3D9DepthStencilBuffer.h"
+
+namespace CamelotEngine
+{
+	D3D9RenderTexture::D3D9RenderTexture()
+		:mColorSurface(nullptr), mDepthStencilSurface(nullptr)
+	{
+
+	}
+
+	D3D9RenderTexture::~D3D9RenderTexture()
+	{
+
+	}
+
+	void D3D9RenderTexture::getCustomAttribute_internal(const String& name, void* pData)
+	{
+		if(name == "DDBACKBUFFER")
+		{
+			IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
+			*pSurf = mColorSurface;
+			return;
+		}
+		else if(name == "D3DZBUFFER")
+		{
+			IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
+			*pSurf = mDepthStencilSurface;
+			return;
+		}
+		else if(name == "HWND")
+		{
+			HWND *pHwnd = (HWND*)pData;
+			*pHwnd = NULL;
+			return;
+		}
+	}
+
+	void D3D9RenderTexture::createInternalResourcesImpl()
+	{
+		D3D9Texture* d3d9texture = static_cast<D3D9Texture*>(mTexture.get());
+		D3D9HardwarePixelBuffer* pixelBuffer = static_cast<D3D9HardwarePixelBuffer*>(d3d9texture->getBuffer(mFace, mMipLevel).get());
+		mColorSurface = pixelBuffer->getSurface(D3D9RenderSystem::getActiveD3D9Device());
+
+		D3D9DepthStencilBuffer* d3d9DepthStencil = static_cast<D3D9DepthStencilBuffer*>(mDepthStencilBuffer.get());
+		mDepthStencilSurface = d3d9DepthStencil->getSurface();
+	}
+}

+ 124 - 187
CamelotD3D9Renderer/Source/CmD3D9Texture.cpp

@@ -412,6 +412,7 @@ namespace CamelotEngine
 			if (mUsage & TU_RENDERTARGET)
 				mHwGammaWriteSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_TEXTURE, d3dPF, true);
 		}
+
 		// Check FSAA level
 		if (mUsage & TU_RENDERTARGET)
 		{
@@ -448,46 +449,51 @@ namespace CamelotEngine
 		else
 			textureResources = allocateTextureResources(d3d9Device);
 
-
-		// create the texture
-		hr = D3DXCreateTexture(	
-				d3d9Device,								// device
-				static_cast<UINT>(mWidth),				// width
-				static_cast<UINT>(mHeight),				// height
-				numMips,								// number of mip map levels
-				usage,									// usage
-				d3dPF,									// pixel format
-				mD3DPool,
-				&textureResources->pNormTex);			// data pointer
-		// check result and except if failed
-		if (FAILED(hr))
-		{
-			freeInternalResources();
-			CM_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
-		}
-		
-		// set the base texture we'll use in the render system
-		hr = textureResources->pNormTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
-		if (FAILED(hr))
-		{
-			freeInternalResources();
-			CM_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
-		}
-		
-		// set final tex. attributes from tex. description
-		// they may differ from the source image !!!
-		D3DSURFACE_DESC desc;
-		hr = textureResources->pNormTex->GetLevelDesc(0, &desc);
-		if (FAILED(hr))
+		if ((mUsage & TU_RENDERTARGET) == 0)
 		{
-			freeInternalResources();
-			CM_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
-		}
+			// create the texture
+			hr = D3DXCreateTexture(	
+					d3d9Device,								// device
+					static_cast<UINT>(mWidth),				// width
+					static_cast<UINT>(mHeight),				// height
+					numMips,								// number of mip map levels
+					usage,									// usage
+					d3dPF,									// pixel format
+					mD3DPool,
+					&textureResources->pNormTex);			// data pointer
+
+			// check result and except if failed
+			if (FAILED(hr))
+			{
+				freeInternalResources();
+				CM_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
+			}
+
+			// set the base texture we'll use in the render system
+			hr = textureResources->pNormTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
+			if (FAILED(hr))
+			{
+				freeInternalResources();
+				CM_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
+			}
+
+			// set final tex. attributes from tex. description
+			// they may differ from the source image !!!
+			D3DSURFACE_DESC desc;
+			hr = textureResources->pNormTex->GetLevelDesc(0, &desc);
+			if (FAILED(hr))
+			{
+				freeInternalResources();
+				CM_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
+			}
 
-		if (mFSAAType)
+			_setFinalAttributes(d3d9Device, textureResources, 
+				desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
+		}
+		else
 		{
 			// create AA surface
-			HRESULT hr = d3d9Device->CreateRenderTarget(desc.Width, desc.Height, d3dPF, 
+			HRESULT hr = d3d9Device->CreateRenderTarget(mWidth, mHeight, d3dPF, 
 				mFSAAType, 
 				mFSAAQuality,
 				FALSE, // not lockable
@@ -498,10 +504,17 @@ namespace CamelotEngine
 				CM_EXCEPT(RenderingAPIException, "Unable to create AA render target: " + String(DXGetErrorDescription(hr)));
 			}
 
-		}
+			D3DSURFACE_DESC desc;
+			hr = textureResources->pFSAASurface->GetDesc(&desc);
+			if (FAILED(hr))
+			{
+				freeInternalResources();
+				CM_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
+			}
 
-		_setFinalAttributes(d3d9Device, textureResources, 
-			desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
+			_setFinalAttributes(d3d9Device, textureResources, 
+				desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
+		}
 	}
 	/****************************************************************************************/
 	void D3D9Texture::_createCubeTex(IDirect3DDevice9* d3d9Device)
@@ -509,6 +522,11 @@ namespace CamelotEngine
 		// we must have those defined here
 		assert(mWidth > 0 || mHeight > 0);
 
+		if (mUsage & TU_RENDERTARGET)
+		{
+			CM_EXCEPT(RenderingAPIException, "D3D9 Cube texture can not be created as render target !!");
+		}
+
 		// determine wich D3D9 pixel format we'll use
 		HRESULT hr;
 		D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
@@ -540,18 +558,9 @@ namespace CamelotEngine
 			if (mUsage & TU_RENDERTARGET)
 				mHwGammaWriteSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF, true);
 		}
-		// Check FSAA level
-		if (mUsage & TU_RENDERTARGET)
-		{
-			D3D9RenderSystem* rsys = static_cast<D3D9RenderSystem*>(CamelotEngine::RenderSystem::instancePtr());
-			rsys->determineFSAASettings(d3d9Device, mFSAA, mFSAAHint, d3dPF, false, 
-				&mFSAAType, &mFSAAQuality);
-		}
-		else
-		{
-			mFSAAType = D3DMULTISAMPLE_NONE;
-			mFSAAQuality = 0;
-		}
+
+		mFSAAType = D3DMULTISAMPLE_NONE;
+		mFSAAQuality = 0;
 
 		D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
 		const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();			
@@ -575,7 +584,6 @@ namespace CamelotEngine
 		else
 			textureResources = allocateTextureResources(d3d9Device);
 
-
 		// create the texture
 		hr = D3DXCreateCubeTexture(	
 				d3d9Device,									// device
@@ -610,23 +618,8 @@ namespace CamelotEngine
 			CM_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
 		}
 
-		if (mFSAAType)
-		{
-			// create AA surface
-			HRESULT hr = d3d9Device->CreateRenderTarget(desc.Width, desc.Height, d3dPF, 
-				mFSAAType, 
-				mFSAAQuality,
-				FALSE, // not lockable
-				&textureResources->pFSAASurface, NULL);
-
-			if (FAILED(hr))
-			{
-				CM_EXCEPT(RenderingAPIException, "Unable to create AA render target: " + String(DXGetErrorDescription(hr)));
-			}
-		}
-
 		_setFinalAttributes(d3d9Device, textureResources, 
-			desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
+			desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));		
 	}
 	/****************************************************************************************/
 	void D3D9Texture::_createVolumeTex(IDirect3DDevice9* d3d9Device)
@@ -693,7 +686,6 @@ namespace CamelotEngine
 		else
 			textureResources = allocateTextureResources(d3d9Device);
 
-
 		// create the texture
 		hr = D3DXCreateVolumeTexture(	
 				d3d9Device,									// device
@@ -986,68 +978,81 @@ namespace CamelotEngine
 			}
 		}
 
-		
-
-		switch(getTextureType()) {
-		case TEX_TYPE_2D:
-		case TEX_TYPE_1D:
-			assert(textureResources->pNormTex);
-			// For all mipmaps, store surfaces as HardwarePixelBufferPtr
-			for(mip=0; mip<=mNumMipmaps; ++mip)
-			{
-				if(textureResources->pNormTex->GetSurfaceLevel(static_cast<UINT>(mip), &surface) != D3D_OK)
-					CM_EXCEPT(RenderingAPIException, "Get surface level failed");
+		if((mUsage & TU_RENDERTARGET) != 0)
+		{
+			assert(textureResources->pFSAASurface);
+			assert(getTextureType() == TEX_TYPE_2D);
 
-				D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(0, mip);
-								
-				currPixelBuffer->bind(d3d9Device, surface, textureResources->pFSAASurface,
-					mHwGammaWriteSupported, mFSAA, "PortNoName", textureResources->pBaseTex);
+			D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(0, 0);
 
-				// decrement reference count, the GetSurfaceLevel call increments this
-				// this is safe because the pixel buffer keeps a reference as well
-				surface->Release();			
-			}
-			
-			break;
-		case TEX_TYPE_CUBE_MAP:
-			assert(textureResources->pCubeTex);
-			// For all faces and mipmaps, store surfaces as HardwarePixelBufferPtr
-			for(face=0; face<6; ++face)
+			currPixelBuffer->bind(d3d9Device, textureResources->pFSAASurface,
+				mHwGammaWriteSupported, mFSAA, "PortNoName", textureResources->pBaseTex);
+		}
+		else
+		{
+			switch(getTextureType()) 
 			{
+			case TEX_TYPE_2D:
+			case TEX_TYPE_1D:
+				assert(textureResources->pNormTex);
+				// For all mipmaps, store surfaces as HardwarePixelBufferPtr
 				for(mip=0; mip<=mNumMipmaps; ++mip)
 				{
-					if(textureResources->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, static_cast<UINT>(mip), &surface) != D3D_OK)
-						CM_EXCEPT(RenderingAPIException, "Get cubemap surface failed");
+					if(textureResources->pNormTex->GetSurfaceLevel(static_cast<UINT>(mip), &surface) != D3D_OK)
+						CM_EXCEPT(RenderingAPIException, "Get surface level failed");
 
-					D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(face, mip);
+					D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(0, mip);
 
-					currPixelBuffer->bind(d3d9Device, surface, textureResources->pFSAASurface,
-						mHwGammaWriteSupported, mFSAA, "NoNamePort", textureResources->pBaseTex);
+					currPixelBuffer->bind(d3d9Device, surface,
+						mHwGammaWriteSupported, mFSAA, "PortNoName", textureResources->pBaseTex);
 
 					// decrement reference count, the GetSurfaceLevel call increments this
 					// this is safe because the pixel buffer keeps a reference as well
-					surface->Release();				
-				}				
-			}
-			break;
-		case TEX_TYPE_3D:
-			assert(textureResources->pVolumeTex);
-			// For all mipmaps, store surfaces as HardwarePixelBufferPtr
-			for(mip=0; mip<=mNumMipmaps; ++mip)
-			{
-				if(textureResources->pVolumeTex->GetVolumeLevel(static_cast<UINT>(mip), &volume) != D3D_OK)
-					CM_EXCEPT(RenderingAPIException, "Get volume level failed");	
-						
-				D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(0, mip);
+					surface->Release();			
+				}
 
-				currPixelBuffer->bind(d3d9Device, volume, textureResources->pBaseTex);
+				break;
+			case TEX_TYPE_CUBE_MAP:
+				assert(textureResources->pCubeTex);
+				// For all faces and mipmaps, store surfaces as HardwarePixelBufferPtr
+				for(face=0; face<6; ++face)
+				{
+					for(mip=0; mip<=mNumMipmaps; ++mip)
+					{
+						if(textureResources->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, static_cast<UINT>(mip), &surface) != D3D_OK)
+							CM_EXCEPT(RenderingAPIException, "Get cubemap surface failed");
 
-				// decrement reference count, the GetSurfaceLevel call increments this
-				// this is safe because the pixel buffer keeps a reference as well
-				volume->Release();
-			}
-			break;
-		};					
+						D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(face, mip);
+
+						currPixelBuffer->bind(d3d9Device, surface,
+							mHwGammaWriteSupported, mFSAA, "NoNamePort", textureResources->pBaseTex);
+
+						// decrement reference count, the GetSurfaceLevel call increments this
+						// this is safe because the pixel buffer keeps a reference as well
+						surface->Release();				
+					}				
+				}
+				break;
+			case TEX_TYPE_3D:
+				assert(textureResources->pVolumeTex);
+				// For all mipmaps, store surfaces as HardwarePixelBufferPtr
+				for(mip=0; mip<=mNumMipmaps; ++mip)
+				{
+					if(textureResources->pVolumeTex->GetVolumeLevel(static_cast<UINT>(mip), &volume) != D3D_OK)
+						CM_EXCEPT(RenderingAPIException, "Get volume level failed");	
+
+					D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(0, mip);
+
+					currPixelBuffer->bind(d3d9Device, volume, textureResources->pBaseTex);
+
+					// decrement reference count, the GetSurfaceLevel call increments this
+					// this is safe because the pixel buffer keeps a reference as well
+					volume->Release();
+				}
+				break;
+			};		
+		}
+					
 	}
 	#undef GETLEVEL
 	/****************************************************************************************/
@@ -1213,74 +1218,6 @@ namespace CamelotEngine
 
 		return textureResources->pCubeTex;
 	}	
-
-	/****************************************************************************************/
-	D3D9RenderTexture::D3D9RenderTexture(const String &name, 
-		D3D9HardwarePixelBuffer *buffer, 
-		bool writeGamma, 
-		UINT32 fsaa) : mBuffer(buffer)
-	{
-		mName = name;
-		mHwGamma = writeGamma;
-		mFSAA = fsaa;		
-	}
-	//---------------------------------------------------------------------
-    void D3D9RenderTexture::update(bool swap)
-    {
-		D3D9DeviceManager* deviceManager = D3D9RenderSystem::getDeviceManager();       	
-		D3D9Device* currRenderWindowDevice = deviceManager->getActiveRenderTargetDevice();
-
-		if (currRenderWindowDevice != NULL)
-		{
-			if (currRenderWindowDevice->isDeviceLost() == false)
-				RenderTexture::update(swap);
-		}
-		else
-		{
-			for (UINT i=0; i < deviceManager->getDeviceCount(); ++i)
-			{
-				D3D9Device* device = deviceManager->getDevice(i);
-
-				if (device->isDeviceLost() == false)
-				{
-					deviceManager->setActiveRenderTargetDevice(device);
-					RenderTexture::update(swap);
-					deviceManager->setActiveRenderTargetDevice(NULL);
-				}								
-			}
-		}
-	}
-	//---------------------------------------------------------------------
-	void D3D9RenderTexture::getCustomAttribute_internal( const String& name, void *pData )
-	{
-		if(name == "DDBACKBUFFER")
-		{
-			if (mFSAA > 0)
-			{
-				// rendering to AA surface
-				IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
-				*pSurf = static_cast<D3D9HardwarePixelBuffer*>(mBuffer)->getFSAASurface(D3D9RenderSystem::getActiveD3D9Device());
-				return;
-			}
-			else
-			{
-				IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
-				*pSurf = static_cast<D3D9HardwarePixelBuffer*>(mBuffer)->getSurface(D3D9RenderSystem::getActiveD3D9Device());
-				return;
-			}
-		}
-		else if(name == "HWND")
-		{
-			HWND *pHwnd = (HWND*)pData;
-			*pHwnd = NULL;
-			return;
-		}
-		else if(name == "BUFFER")
-		{
-			*static_cast<HardwarePixelBuffer**>(pData) = mBuffer;
-			return;
-		}
-	}    
 }
 
 #undef THROW_IF_NOT_RENDER_THREAD

+ 1 - 1
CamelotGLRenderer/Include/CmGLRenderTexture.h

@@ -47,7 +47,7 @@ namespace CamelotEngine
 		GLRenderTexture();
 		virtual ~GLRenderTexture();
 
-		bool requiresTextureFlipping() const { return false; }
+		bool requiresTextureFlipping() const { return true; }
 		virtual void getCustomAttribute_internal(const String& name, void* pData);
 
 	protected: