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

DX9 working fine with new way of texture initialization

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

+ 2 - 2
CamelotClient/CamelotClient.cpp

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

+ 5 - 3
CamelotD3D9Renderer/Include/CmD3D9Mappings.h

@@ -98,12 +98,14 @@ namespace CamelotFramework
         // Convert matrix from D3D style
         static Matrix4 convertD3DXMatrix( const D3DXMATRIX& mat );
 
-		/// utility method, convert D3D9 pixel format to Ogre pixel format
+		/// utility method, convert D3D9 pixel format to engine pixel format
 		static PixelFormat _getPF(D3DFORMAT d3dPF);
-		/// utility method, convert Ogre pixel format to D3D9 pixel format
+		/// utility method, convert engine pixel format to D3D9 pixel format
 		static D3DFORMAT _getPF(PixelFormat ogrePF);
-		/// utility method, find closest Ogre pixel format that D3D9 can support
+
 		static PixelFormat _getClosestSupportedPF(PixelFormat ogrePF);
+		static PixelFormat _getClosestSupportedRenderTargetPF(PixelFormat enginePF);
+		static PixelFormat _getClosestSupportedDepthStencilPF(PixelFormat enginePF);
 	};
 }
 #endif

+ 0 - 1
CamelotD3D9Renderer/Include/CmD3D9RenderSystem.h

@@ -253,7 +253,6 @@ namespace CamelotFramework
 
         void convertVertexShaderCaps(RenderSystemCapabilities* rsc) const;
         void convertPixelShaderCaps(RenderSystemCapabilities* rsc) const;
-		bool checkVertexTextureFormats(D3D9RenderWindow* renderWindow) const;
 
 	protected:
 		// I know that's a lot of friends, but I'd rather have friend classes than exposing the needed methods

+ 31 - 6
CamelotD3D9Renderer/Source/CmD3D9Mappings.cpp

@@ -517,12 +517,8 @@ namespace CamelotFramework
 		// (This is not the case for floating point formats, EXCEPT for green-red one)
 		switch(camelotPf)
 		{
-		case PF_B8G8R8:
-			return D3DFMT_R8G8B8;
 		case PF_B8G8R8A8:
 			return D3DFMT_A8R8G8B8;
-		case PF_R8G8B8A8:
-			return D3DFMT_A8B8G8R8;
 		case PF_R8G8B8X8:
 			return D3DFMT_X8B8G8R8;
 		case PF_B8G8R8X8:
@@ -571,9 +567,11 @@ namespace CamelotFramework
 		switch(enginePF)
 		{
 		case PF_R8:
-			return PF_B8G8R8;
+			return PF_B8G8R8X8;
 		case PF_R8G8:
-			return PF_B8G8R8;
+			return PF_B8G8R8X8;
+		case PF_B8G8R8:
+			return PF_B8G8R8X8;
 		case PF_A8R8G8B8:
 			return PF_B8G8R8A8;
 		case PF_A8B8G8R8:
@@ -592,4 +590,31 @@ namespace CamelotFramework
 		}
 	}
 
+	PixelFormat D3D9Mappings::_getClosestSupportedRenderTargetPF(PixelFormat enginePF)
+	{
+		switch(enginePF)
+		{
+		case PF_B8G8R8A8:
+			return PF_B8G8R8A8;
+		case PF_B8G8R8X8:
+			return PF_B8G8R8X8;
+		default:
+			return PF_B8G8R8A8;
+		}
+	}
+
+	PixelFormat D3D9Mappings::_getClosestSupportedDepthStencilPF(PixelFormat enginePF)
+	{
+		switch(enginePF)
+		{
+		case PF_D24S8:
+			return PF_D24S8;
+		case PF_D32:
+			return PF_D32;
+		case PF_D16:
+			return PF_D16;
+		default:
+			return PF_D24S8;
+		}
+	}
 }

+ 0 - 29
CamelotD3D9Renderer/Source/CmD3D9RenderSystem.cpp

@@ -2251,35 +2251,6 @@ namespace CamelotFramework
 		}
 	}
 	//-----------------------------------------------------------------------
-	bool D3D9RenderSystem::checkVertexTextureFormats(D3D9RenderWindow* renderWindow) const
-	{
-		bool anySupported = false;
-
-		IDirect3DSurface9* bbSurf;
-		renderWindow->getCustomAttribute("DDBACKBUFFER", &bbSurf);
-		D3DSURFACE_DESC bbSurfDesc;
-		bbSurf->GetDesc(&bbSurfDesc);
-
-		for (UINT32 ipf = 1; ipf < static_cast<UINT32>(PF_COUNT); ++ipf)
-		{
-			PixelFormat pf = (PixelFormat)ipf;
-
-			D3DFORMAT fmt = 
-				D3D9Mappings::_getPF(D3D9Mappings::_getClosestSupportedPF(pf));
-
-			if (SUCCEEDED(mpD3D->CheckDeviceFormat(
-				mActiveD3DDriver->getAdapterNumber(), D3DDEVTYPE_HAL, bbSurfDesc.Format, 
-				D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, fmt)))
-			{
-				// cool, at least one supported
-				anySupported = true;
-			}
-		}
-
-		return anySupported;
-
-	}
-	//-----------------------------------------------------------------------
 	void D3D9RenderSystem::initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps)
 	{
 		if (caps->getRenderSystemName() != getName())

+ 8 - 12
CamelotD3D9Renderer/Source/CmD3D9Texture.cpp

@@ -393,9 +393,6 @@ namespace CamelotFramework
 		HRESULT hr;
 		D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
 		
-		// let's D3DX check the corrected pixel format
-		hr = D3DXCheckTextureRequirements(d3d9Device, NULL, NULL, NULL, 0, &d3dPF, mD3DPool);
-		
 		if(mFormat != D3D9Mappings::_getPF(d3dPF))
 		{
 			CM_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
@@ -448,7 +445,7 @@ namespace CamelotFramework
 		const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();			
 
 		// check if mip maps are supported on hardware
-		if (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPMAP) || (mUsage & TU_RENDERTARGET) != 0 || (mUsage & TU_DEPTHSTENCIL) != 0)
+		if (numMips > 1 && (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPMAP) || (mUsage & TU_RENDERTARGET) != 0 || (mUsage & TU_DEPTHSTENCIL) != 0))
 		{
 			CM_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: 0");
 			numMips = 1;
@@ -575,9 +572,6 @@ namespace CamelotFramework
 		HRESULT hr;
 		D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
 
-		// let's D3DX check the corrected pixel format
-		hr = D3DXCheckCubeTextureRequirements(d3d9Device, NULL, NULL, 0, &d3dPF, mD3DPool);
-		
 		if(mFormat != D3D9Mappings::_getPF(d3dPF))
 		{
 			CM_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
@@ -615,7 +609,7 @@ namespace CamelotFramework
 		const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();			
 		
 		// check if mip map cube textures are supported
-		if (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
+		if (numMips > 1 && !(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
 		{
 			CM_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: 0");
 			numMips = 1;
@@ -685,9 +679,6 @@ namespace CamelotFramework
 		HRESULT hr;
 		D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
 
-		// let's D3DX check the corrected pixel format
-		hr = D3DXCheckVolumeTextureRequirements(d3d9Device, NULL, NULL, NULL, NULL, 0, &d3dPF, mD3DPool);
-		
 		if(mFormat != D3D9Mappings::_getPF(d3dPF))
 		{
 			CM_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
@@ -723,7 +714,7 @@ namespace CamelotFramework
 
 
 		// check if mip map volume textures are supported
-		if (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
+		if (numMips > 1 && !(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
 		{
 			CM_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: 0");
 		}
@@ -791,6 +782,11 @@ namespace CamelotFramework
 				"Height: " + toString(height) + "/" + toString(mHeight) +
 				"Depth: " + toString(depth) + "/" + toString(mDepth));
 		}
+
+		if(format != mFormat)
+		{
+			CM_EXCEPT(InternalErrorException, "Wanted and created texture formats don't match! " + toString(format) + "/" + toString(mFormat));
+		}
 		
 		// Create list of subsurfaces for getBuffer()
 		_createSurfaceList(d3d9Device, textureResources);

+ 7 - 34
CamelotD3D9Renderer/Source/CmD3D9TextureManager.cpp

@@ -65,45 +65,18 @@ namespace CamelotFramework
 
 	PixelFormat D3D9TextureManager::getNativeFormat(TextureType ttype, PixelFormat format, int usage)
 	{
-		// Basic filtering
-		D3DFORMAT d3dPF = D3D9Mappings::_getPF(D3D9Mappings::_getClosestSupportedPF(format));
-
-		// Calculate usage
-		DWORD d3dusage = 0;
-		D3DPOOL pool = D3DPOOL_MANAGED;
-		if (usage & TU_RENDERTARGET) 
+		if((usage & TU_RENDERTARGET) != 0)
 		{
-			d3dusage |= D3DUSAGE_RENDERTARGET;
-			pool = D3DPOOL_DEFAULT; 
+			return D3D9Mappings::_getClosestSupportedRenderTargetPF(format);
 		}
-		if(usage & TU_DEPTHSTENCIL)
+		else if((usage & TU_DEPTHSTENCIL) != 0)
 		{
-			d3dusage |= D3DUSAGE_DEPTHSTENCIL;
-			pool = D3DPOOL_DEFAULT;
+			return D3D9Mappings::_getClosestSupportedDepthStencilPF(format);
 		}
-		if (usage & TU_DYNAMIC)
+		else
 		{
-			d3dusage |= D3DUSAGE_DYNAMIC;
-			pool = D3DPOOL_DEFAULT;
+			// Basic filtering
+			return D3D9Mappings::_getClosestSupportedPF(format);
 		}
-
-		IDirect3DDevice9* pCurDevice = D3D9RenderSystem::getActiveD3D9Device();
-
-		// Use D3DX to adjust pixel format
-		switch(ttype)
-		{
-		case TEX_TYPE_1D:
-		case TEX_TYPE_2D:
-			D3DXCheckTextureRequirements(pCurDevice, NULL, NULL, NULL, d3dusage, &d3dPF, pool);
-			break;
-		case TEX_TYPE_3D:
-			D3DXCheckVolumeTextureRequirements(pCurDevice, NULL, NULL, NULL, NULL, d3dusage, &d3dPF, pool);
-			break;
-		case TEX_TYPE_CUBE_MAP:
-			D3DXCheckCubeTextureRequirements(pCurDevice, NULL, NULL, d3dusage, &d3dPF, pool);
-			break;
-		};
-
-		return D3D9Mappings::_getPF(d3dPF);
 	}
 }

+ 2 - 11
Opts.txt

@@ -36,16 +36,7 @@ from a transient mesh by drawing only parts of its buffer. But only do this afte
 
 ---------
 
-Other possible improvements:
- - MeshData always completely overwrites destination mesh sub-meshes, plus it doesn't allow empty spaces or overlapping between the sub-meshes, they are always
-   sequential.
- - When writing to mesh vertex buffer that requires a color flip I need to create a temporary copy of the entire buffer. It would be better to handle this differently. 
-
-SubMesh
- - Needs default submesh for when the user doesn't set any (just returns entire index range). Needs to be added to RTTI as well
- - Remove draw op from MeshData and add it to Mesh constructor
-
  Problem with Texture::allocateSubresourceBuffer
- - It relies on format being accurate, however it is only accurate after texture has been created on the core thread. Which means if I call that before texture is created I will get a buffer of incorrect size.
-   - I should probably ensure that parameters provided to Texture are accurate and don't change while on core thread. Probably deal with them inside TextureManager.
+  - Fix DX9 & DX11
+   - Implement properly TextureManager::getNativeFormat for DX9 and DX11
    - After that is done remove the synchronize() point from FreeImgImporter::import, FontImporter and two texture updates in GUIManager

+ 2 - 1
TODO.txt

@@ -131,7 +131,8 @@ Low priority TODO
    - Material::setParamBlock is commented out
  - onMovedOrResized is still used by Viewport while that same callback is offered by RenderWindowManager. There is no need to have them in both places.
  - Texture "ScaleToFit" will cause the texture to repeat instead of clipping the image. e.g. a 50x20 texture placed on an 50x100 area will repeat 5x
-
+ - When writing to mesh vertex buffer in Mesh::writeSubresource that requires a color flip I need to create a temporary copy of the 
+    entire buffer. It would be better to handle this differently. 
 ----------------------------------------------------------------------------------------------
 Optional:
  - Need better handling for shader techniques. Some Materials are able to run on all renderers yet I can only specify one. This is problematic