Răsfoiți Sursa

Added DX11 texture manager and depth stencil buffer

Marko Pintera 13 ani în urmă
părinte
comite
466d7dba98

+ 4 - 0
CamelotD3D11RenderSystem/CamelotD3D11RenderSystem.vcxproj

@@ -146,6 +146,7 @@
     </Link>
     </Link>
   </ItemDefinitionGroup>
   </ItemDefinitionGroup>
   <ItemGroup>
   <ItemGroup>
+    <ClInclude Include="Include\CmD3D11DepthStencilBuffer.h" />
     <ClInclude Include="Include\CmD3D11HardwareBufferManager.h" />
     <ClInclude Include="Include\CmD3D11HardwareBufferManager.h" />
     <ClInclude Include="Include\CmD3D11HardwareConstantBuffer.h" />
     <ClInclude Include="Include\CmD3D11HardwareConstantBuffer.h" />
     <ClInclude Include="Include\CmD3D11Device.h" />
     <ClInclude Include="Include\CmD3D11Device.h" />
@@ -163,11 +164,13 @@
     <ClInclude Include="Include\CmD3D11RenderTexture.h" />
     <ClInclude Include="Include\CmD3D11RenderTexture.h" />
     <ClInclude Include="Include\CmD3D11RenderWindow.h" />
     <ClInclude Include="Include\CmD3D11RenderWindow.h" />
     <ClInclude Include="Include\CmD3D11Texture.h" />
     <ClInclude Include="Include\CmD3D11Texture.h" />
+    <ClInclude Include="Include\CmD3D11TextureManager.h" />
     <ClInclude Include="Include\CmD3D11VertexDeclaration.h" />
     <ClInclude Include="Include\CmD3D11VertexDeclaration.h" />
     <ClInclude Include="Include\CmD3D11VideoMode.h" />
     <ClInclude Include="Include\CmD3D11VideoMode.h" />
     <ClInclude Include="Include\CmD3D11VideoModeList.h" />
     <ClInclude Include="Include\CmD3D11VideoModeList.h" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
+    <ClCompile Include="Source\CmD3D11DepthStencilBuffer.cpp" />
     <ClCompile Include="Source\CmD3D11Device.cpp" />
     <ClCompile Include="Source\CmD3D11Device.cpp" />
     <ClCompile Include="Source\CmD3D11Driver.cpp" />
     <ClCompile Include="Source\CmD3D11Driver.cpp" />
     <ClCompile Include="Source\CmD3D11DriverList.cpp" />
     <ClCompile Include="Source\CmD3D11DriverList.cpp" />
@@ -184,6 +187,7 @@
     <ClCompile Include="Source\CmD3D11RenderTexture.cpp" />
     <ClCompile Include="Source\CmD3D11RenderTexture.cpp" />
     <ClCompile Include="Source\CmD3D11RenderWindow.cpp" />
     <ClCompile Include="Source\CmD3D11RenderWindow.cpp" />
     <ClCompile Include="Source\CmD3D11Texture.cpp" />
     <ClCompile Include="Source\CmD3D11Texture.cpp" />
+    <ClCompile Include="Source\CmD3D11TextureManager.cpp" />
     <ClCompile Include="Source\CmD3D11VertexDeclaration.cpp" />
     <ClCompile Include="Source\CmD3D11VertexDeclaration.cpp" />
     <ClCompile Include="Source\CmD3D11VideoMode.cpp" />
     <ClCompile Include="Source\CmD3D11VideoMode.cpp" />
     <ClCompile Include="Source\CmD3D11VideoModeList.cpp" />
     <ClCompile Include="Source\CmD3D11VideoModeList.cpp" />

+ 12 - 0
CamelotD3D11RenderSystem/CamelotD3D11RenderSystem.vcxproj.filters

@@ -75,6 +75,12 @@
     <ClInclude Include="Include\CmD3D11RenderTexture.h">
     <ClInclude Include="Include\CmD3D11RenderTexture.h">
       <Filter>Header Files</Filter>
       <Filter>Header Files</Filter>
     </ClInclude>
     </ClInclude>
+    <ClInclude Include="Include\CmD3D11TextureManager.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="Include\CmD3D11DepthStencilBuffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\CmD3D11GpuProgram.cpp">
     <ClCompile Include="Source\CmD3D11GpuProgram.cpp">
@@ -134,5 +140,11 @@
     <ClCompile Include="Source\CmD3D11RenderTexture.cpp">
     <ClCompile Include="Source\CmD3D11RenderTexture.cpp">
       <Filter>Source Files</Filter>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
+    <ClCompile Include="Source\CmD3D11TextureManager.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="Source\CmD3D11DepthStencilBuffer.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 20 - 0
CamelotD3D11RenderSystem/Include/CmD3D11DepthStencilBuffer.h

@@ -0,0 +1,20 @@
+#pragma once
+
+#include "CmD3D11Prerequisites.h"
+#include "CmDepthStencilBuffer.h"
+
+namespace CamelotEngine
+{
+	class CM_D3D11_EXPORT D3D11DepthStencilBuffer : public DepthStencilBuffer
+	{
+	public:
+		D3D11DepthStencilBuffer(UINT32 bitDepth, UINT32 width, UINT32 height, UINT32 fsaa, const String &fsaaHint);
+		~D3D11DepthStencilBuffer();
+
+		bool isCompatible(RenderTarget* renderTarget) const;
+
+	protected:
+		ID3D11Texture2D* mDepthStencil;
+		ID3D11DepthStencilView* mDepthStencilView;
+	};
+}

+ 2 - 1
CamelotD3D11RenderSystem/Include/CmD3D11RenderSystem.h

@@ -11,7 +11,8 @@ namespace CamelotEngine
 		
 		
 		static D3D11Device& getPrimaryDevice();
 		static D3D11Device& getPrimaryDevice();
 
 
-		void determineFSAASettings(UINT32 fsaa, const String& fsaaHint, DXGI_FORMAT format, DXGI_SAMPLE_DESC* outFSAASettings);
+		void determineFSAASettings(UINT32 fsaa, const String& fsaaHint, DXGI_FORMAT format, DXGI_SAMPLE_DESC* outFSAASettings);
+		bool checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage);
 	protected:
 	protected:
 
 
 	private:
 	private:

+ 71 - 71
CamelotD3D11RenderSystem/Include/CmD3D11RenderWindow.h

@@ -5,76 +5,76 @@
 
 
 namespace CamelotEngine
 namespace CamelotEngine
 {
 {
-	class CM_D3D11_EXPORT D3D11RenderWindow : public RenderWindow
-	{
-	public:
-		D3D11RenderWindow(D3D11Device& device, IDXGIFactory* DXGIFactory);
-		~D3D11RenderWindow();
-
-		void initialize(const String& name, unsigned width, unsigned height, bool fullScreen, 
-			const NameValuePairList* miscParams);
-		void destroy(void);
-
-		void reposition(int left, int top);
-		void resize(unsigned int width, unsigned int height);
-
-		void setHidden(bool hidden);
-		void setActive(bool state);
-		void setFullscreen(bool fullScreen, unsigned int width, unsigned int height);
-
-		void swapBuffers(bool waitForVSync = true);
-		void copyContentsToMemory(const PixelData &dst, FrameBuffer buffer);
-
-		void windowMovedOrResized();
-
-		bool isClosed() const									{ return mClosed; }
-		bool isHidden() const									{ return mHidden; }
-
-		void getCustomAttribute( const String& name, void* pData );
-		DXGI_SWAP_CHAIN_DESC* getPresentationParameters(void)	{ return &mSwapChainDesc; }
-		HWND getWindowHandle() const							{ return mHWnd; }
-
-		bool requiresTextureFlipping() const					{ return false; }
-
-	protected:
-		void _createSizeDependedD3DResources();
-		void _destroySizeDependedD3DResources();
-
-		IDXGIDevice* _queryDxgiDevice(); 
-	
-		bool _checkMultiSampleQuality(UINT SampleCount, UINT *outQuality, DXGI_FORMAT format);
-
-		void _createSwapChain();
-		void _resizeSwapChainBuffers(unsigned width, unsigned height);
-
-		bool _getSwitchingFullscreen() const					{ return mSwitchingFullscreen; }
-		void _finishSwitchingFullscreen();
-		
-	protected:
-		D3D11Device&	mDevice;			// D3D11 driver
-		IDXGIFactory*	mDXGIFactory;
-		bool	mIsExternal;
-		bool	mSizing;
-		bool	mClosed;
-		bool	mHidden;
-
-		// -------------------------------------------------------
-		// DirectX-specific
-		// -------------------------------------------------------
-		DXGI_SAMPLE_DESC mFSAAType;
-		UINT mDisplayFrequency;
-		bool mVSync;
-		unsigned int mVSyncInterval;
-
-		// Window size depended resources - must be released before swapchain resize and recreated later
-		ID3D11Texture2D*			mBackBuffer;
-		ID3D11RenderTargetView*		mRenderTargetView;
-		ID3D11DepthStencilView*		mDepthStencilView;
-
-		IDXGISwapChain*				mSwapChain;
-		DXGI_SWAP_CHAIN_DESC		mSwapChainDesc;
-
-		HWND	mHWnd;					// Win32 window handle
-		bool	mSwitchingFullscreen;	// Are we switching from fullscreen to windowed or vice versa
+	class CM_D3D11_EXPORT D3D11RenderWindow : public RenderWindow
+	{
+	public:
+		D3D11RenderWindow(D3D11Device& device, IDXGIFactory* DXGIFactory);
+		~D3D11RenderWindow();
+
+		void initialize(const String& name, unsigned width, unsigned height, bool fullScreen, 
+			const NameValuePairList* miscParams);
+		void destroy(void);
+
+		void reposition(int left, int top);
+		void resize(unsigned int width, unsigned int height);
+
+		void setHidden(bool hidden);
+		void setActive(bool state);
+		void setFullscreen(bool fullScreen, unsigned int width, unsigned int height);
+
+		void swapBuffers(bool waitForVSync = true);
+		void copyContentsToMemory(const PixelData &dst, FrameBuffer buffer);
+
+		void windowMovedOrResized();
+
+		bool isClosed() const									{ return mClosed; }
+		bool isHidden() const									{ return mHidden; }
+
+		void getCustomAttribute( const String& name, void* pData );
+		DXGI_SWAP_CHAIN_DESC* getPresentationParameters(void)	{ return &mSwapChainDesc; }
+		HWND getWindowHandle() const							{ return mHWnd; }
+
+		bool requiresTextureFlipping() const					{ return false; }
+
+	protected:
+		void _createSizeDependedD3DResources();
+		void _destroySizeDependedD3DResources();
+
+		IDXGIDevice* _queryDxgiDevice(); 
+	
+		bool _checkMultiSampleQuality(UINT SampleCount, UINT *outQuality, DXGI_FORMAT format);
+
+		void _createSwapChain();
+		void _resizeSwapChainBuffers(unsigned width, unsigned height);
+
+		bool _getSwitchingFullscreen() const					{ return mSwitchingFullscreen; }
+		void _finishSwitchingFullscreen();
+		
+	protected:
+		D3D11Device&	mDevice;			// D3D11 driver
+		IDXGIFactory*	mDXGIFactory;
+		bool	mIsExternal;
+		bool	mSizing;
+		bool	mClosed;
+		bool	mHidden;
+
+		// -------------------------------------------------------
+		// DirectX-specific
+		// -------------------------------------------------------
+		DXGI_SAMPLE_DESC mFSAAType;
+		UINT mDisplayFrequency;
+		bool mVSync;
+		unsigned int mVSyncInterval;
+
+		// Window size depended resources - must be released before swapchain resize and recreated later
+		ID3D11Texture2D*			mBackBuffer;
+		ID3D11RenderTargetView*		mRenderTargetView;
+		ID3D11DepthStencilView*		mDepthStencilView;
+
+		IDXGISwapChain*				mSwapChain;
+		DXGI_SWAP_CHAIN_DESC		mSwapChainDesc;
+
+		HWND	mHWnd;					// Win32 window handle
+		bool	mSwitchingFullscreen;	// Are we switching from fullscreen to windowed or vice versa
 	};
 	};
 }
 }

+ 2 - 0
CamelotD3D11RenderSystem/Include/CmD3D11Texture.h

@@ -12,6 +12,8 @@ namespace CamelotEngine
 
 
 		ID3D11Resource* getDX11Resource() const { return mTex; }
 		ID3D11Resource* getDX11Resource() const { return mTex; }
 	protected:
 	protected:
+		friend class D3D11TextureManager;
+
 		D3D11Texture();
 		D3D11Texture();
 
 
 		ID3D11Texture1D* m1DTex;
 		ID3D11Texture1D* m1DTex;

+ 30 - 0
CamelotD3D11RenderSystem/Include/CmD3D11TextureManager.h

@@ -0,0 +1,30 @@
+#pragma once
+
+#include "CmD3D11Prerequisites.h"
+#include "CmTextureManager.h"
+
+namespace CamelotEngine 
+{
+	class CM_D3D11_EXPORT D3D11TextureManager : public TextureManager
+	{
+	public:
+		D3D11TextureManager();
+		~D3D11TextureManager();
+
+		/// @copydoc TextureManager::getNativeFormat
+		PixelFormat getNativeFormat(TextureType ttype, PixelFormat format, int usage);
+
+        /// @copydoc TextureManager::isHardwareFilteringSupported
+        bool isHardwareFilteringSupported(TextureType ttype, PixelFormat format, int usage,
+            bool preciseFormatOnly = false);		
+
+		/**
+		 * @copydoc TextureManager::createDepthStencilBuffer()
+		 */
+		DepthStencilBufferPtr createDepthStencilBuffer(UINT32 bitDepth, UINT32 width, 
+			UINT32 height, UINT32 fsaa, const String& fsaaHint);
+
+	protected:		
+		Texture* createImpl();
+	};
+}

+ 64 - 0
CamelotD3D11RenderSystem/Source/CmD3D11DepthStencilBuffer.cpp

@@ -0,0 +1,64 @@
+#include "CmD3D11DepthStencilBuffer.h"
+#include "CmD3D11RenderSystem.h"
+#include "CmD3D11Device.h"
+#include "CmException.h"
+
+namespace CamelotEngine
+{
+	D3D11DepthStencilBuffer::D3D11DepthStencilBuffer(UINT32 bitDepth, UINT32 width, UINT32 height, UINT32 fsaa, const String &fsaaHint)
+		: DepthStencilBuffer(bitDepth, width, height, fsaa, fsaaHint)
+		, mDepthStencil(nullptr)
+		, mDepthStencilView(nullptr)
+	{
+		// Create depth stencil texture
+		D3D11_TEXTURE2D_DESC descDepth;
+
+		descDepth.Width = width;
+		descDepth.Height = height;
+		descDepth.MipLevels = 1;
+		descDepth.ArraySize = 1;
+		descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
+		descDepth.Usage = D3D11_USAGE_DEFAULT;
+		descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
+		descDepth.CPUAccessFlags = 0;
+		descDepth.MiscFlags = 0;
+
+		D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
+		rs->determineFSAASettings(fsaa, fsaaHint, DXGI_FORMAT_D24_UNORM_S8_UINT, &descDepth.SampleDesc);
+
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+		HRESULT hr = device.getD3D11Device()->CreateTexture2D(&descDepth, NULL, &mDepthStencil);
+		if( FAILED(hr) || device.hasError())
+		{
+			String errorDescription = device.getErrorDescription(hr);
+			CM_EXCEPT(RenderingAPIException, "Unable to create depth texture\nError Description:" + errorDescription);
+		}
+
+		// Create the depth stencil view
+		D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
+		ZeroMemory(&descDSV, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
+
+		descDSV.Format =  descDepth.Format;
+		descDSV.ViewDimension = (fsaa != 0) ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D;
+		descDSV.Texture2D.MipSlice = 0;
+		hr = device.getD3D11Device()->CreateDepthStencilView(mDepthStencil, &descDSV, &mDepthStencilView);
+
+		if(FAILED(hr))
+		{
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "Unable to create depth stencil view\nError Description:" + errorDescription);
+		}
+	}
+
+	D3D11DepthStencilBuffer::~D3D11DepthStencilBuffer()
+	{
+		SAFE_RELEASE(mDepthStencilView);
+		SAFE_RELEASE(mDepthStencil);
+	}
+
+	bool D3D11DepthStencilBuffer::isCompatible(RenderTarget* renderTarget) const
+	{
+		// Implement once I have RenderTarget properly implemented
+		CM_EXCEPT(NotImplementedException, "Not implemented");
+	}
+}

+ 5 - 0
CamelotD3D11RenderSystem/Source/CmD3D11RenderSystem.cpp

@@ -117,4 +117,9 @@ namespace CamelotEngine
 		} // while !ok
 		} // while !ok
 		*/
 		*/
 	}
 	}
+
+	bool D3D11RenderSystem::checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage)
+	{
+		return true;
+	}
 }
 }

+ 0 - 50
CamelotD3D11RenderSystem/Source/CmD3D11RenderWindow.cpp

@@ -615,56 +615,6 @@ namespace CamelotEngine
 		}
 		}
 
 
 		mDepthStencilBuffer = TextureManager::instance().createDepthStencilBuffer(32, BBDesc.Width, BBDesc.Height, mFSAA, mFSAAHint);
 		mDepthStencilBuffer = TextureManager::instance().createDepthStencilBuffer(32, BBDesc.Width, BBDesc.Height, mFSAA, mFSAAHint);
-
-		//if(mDepthBufferPoolId != DepthStencilBuffer::POOL_NO_DEPTH)
-		//{
-		//	// Create depth stencil texture
-		//	ID3D11Texture2D* pDepthStencil = NULL;
-		//	D3D11_TEXTURE2D_DESC descDepth;
-
-		//	descDepth.Width = BBDesc.Width;
-		//	descDepth.Height = BBDesc.Height;
-		//	descDepth.MipLevels = 1;
-		//	descDepth.ArraySize = 1;
-		//	descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
-		//	descDepth.SampleDesc.Count = mFSAAType.Count;
-		//	descDepth.SampleDesc.Quality = mFSAAType.Quality;
-		//	descDepth.Usage = D3D11_USAGE_DEFAULT;
-		//	descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
-		//	descDepth.CPUAccessFlags = 0;
-		//	descDepth.MiscFlags = 0;
-
-		//	hr = mDevice.getD3D11Device()->CreateTexture2D(&descDepth, NULL, &pDepthStencil);
-		//	if( FAILED(hr) || mDevice.hasError())
-		//	{
-		//		String errorDescription = mDevice.getErrorDescription(hr);
-		//		CM_EXCEPT(RenderingAPIException, "Unable to create depth texture\nError Description:" + errorDescription);
-		//	}
-
-		//	// Create the depth stencil view
-		//	D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
-		//	ZeroMemory( &descDSV, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC) );
-
-		//	descDSV.Format =  descDepth.Format;
-		//	descDSV.ViewDimension = mFSAA ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D;
-		//	descDSV.Texture2D.MipSlice = 0;
-		//	hr = mDevice.getD3D11Device()->CreateDepthStencilView(pDepthStencil, &descDSV, &mDepthStencilView);
-
-		//	SAFE_RELEASE(pDepthStencil);
-
-		//	if( FAILED(hr) )
-		//	{
-		//		String errorDescription = mDevice.getErrorDescription();
-		//		CM_EXCEPT(RenderingAPIException, 
-		//			"Unable to create depth stencil view\nError Description:" + errorDescription);
-		//	}
-
-		//	D3D11RenderSystem* rsys = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
-		//	DepthStencilBuffer *depthBuf = rsys->_addManualDepthBuffer(mDepthStencilView, mWidth, mHeight, mFSAAType.Count, mFSAAType.Quality);
-
-		//	//Don't forget we want this window to use _this_ depth buffer
-		//	this->attachDepthBuffer(depthBuf);
-		//} 
 	}
 	}
 
 
 	void D3D11RenderWindow::_destroySizeDependedD3DResources()
 	void D3D11RenderWindow::_destroySizeDependedD3DResources()

+ 44 - 0
CamelotD3D11RenderSystem/Source/CmD3D11TextureManager.cpp

@@ -0,0 +1,44 @@
+#include "CmD3D11TextureManager.h"
+#include "CmD3D11Texture.h"
+#include "CmD3D11Mappings.h"
+#include "CmD3D11RenderSystem.h"
+#include "CmD3D11DepthStencilBuffer.h"
+
+namespace CamelotEngine
+{
+	D3D11TextureManager::D3D11TextureManager() 
+		:TextureManager()
+	{ }
+
+	D3D11TextureManager::~D3D11TextureManager()
+	{ }
+
+	Texture* D3D11TextureManager::createImpl()
+	{
+		return new D3D11Texture(); 
+	}
+
+	DepthStencilBufferPtr D3D11TextureManager::createDepthStencilBuffer(UINT32 bitDepth, UINT32 width, 
+		UINT32 height, UINT32 fsaa, const String& fsaaHint)
+	{
+		return DepthStencilBufferPtr(new D3D11DepthStencilBuffer(bitDepth, width, height, fsaa, fsaaHint));
+	}
+
+	PixelFormat D3D11TextureManager::getNativeFormat(TextureType ttype, PixelFormat format, int usage)
+	{
+		// Basic filtering
+		DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(format));
+
+		return D3D11Mappings::_getPF(d3dPF);
+	}
+
+	bool D3D11TextureManager::isHardwareFilteringSupported(TextureType ttype, PixelFormat format, int usage,
+		bool preciseFormatOnly)
+	{
+		if (!preciseFormatOnly)
+			format = getNativeFormat(ttype, format, usage);
+
+		D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
+		return rs->checkTextureFilteringSupported(ttype, format, usage);
+	}
+}