Browse Source

Normalizing depth bias across all APIs

Marko Pintera 10 years ago
parent
commit
cba7f9684c

+ 2 - 2
BansheeCore/Include/BsRasterizerState.h

@@ -28,7 +28,7 @@ namespace BansheeEngine
 		PolygonMode polygonMode;
 		CullingMode cullMode;
 
-		int depthBias;
+		float depthBias;
 		float depthBiasClamp;
 		float slopeScaledDepthBias;
 
@@ -65,7 +65,7 @@ namespace BansheeEngine
 		*
 		* @note		This is useful if you want to avoid z fighting for objects at the same or similar depth.
 		*/
-		int getDepthBias() const { return mData.depthBias; }
+		float getDepthBias() const { return mData.depthBias; }
 
 		/**
 		 * @brief	Maximum depth bias value.

+ 3 - 1
BansheeD3D11RenderSystem/Source/BsD3D11RasterizerState.cpp

@@ -19,12 +19,14 @@ namespace BansheeEngine
 
 	void D3D11RasterizerStateCore::initialize()
 	{
+		INT32 scaledDepthBias = Math::floorToInt(-mProperties.getDepthBias() * float((1 << 24))); // Note: Assumes 24-bit depth buffer
+
 		D3D11_RASTERIZER_DESC rasterizerStateDesc;
 		ZeroMemory(&rasterizerStateDesc, sizeof(D3D11_RASTERIZER_DESC));
 
 		rasterizerStateDesc.AntialiasedLineEnable = mProperties.getAntialiasedLineEnable();
 		rasterizerStateDesc.CullMode = D3D11Mappings::get(mProperties.getCullMode());
-		rasterizerStateDesc.DepthBias = mProperties.getDepthBias();
+		rasterizerStateDesc.DepthBias = scaledDepthBias;
 		rasterizerStateDesc.DepthBiasClamp = mProperties.getDepthBiasClamp();
 		rasterizerStateDesc.DepthClipEnable = mProperties.getDepthClipEnable();
 		rasterizerStateDesc.FillMode = D3D11Mappings::get(mProperties.getPolygonMode());

+ 2 - 4
BansheeD3D9RenderSystem/Source/BsD3D9RenderAPI.cpp

@@ -590,7 +590,7 @@ namespace BansheeEngine
 
 		const RasterizerProperties& stateProps = rasterizerState->getProperties();
 
-		setDepthBias((float)stateProps.getDepthBias(), stateProps.getSlopeScaledDepthBias());
+		setDepthBias(stateProps.getDepthBias(), stateProps.getSlopeScaledDepthBias());
 
 		setCullingMode(stateProps.getCullMode());
 
@@ -853,9 +853,7 @@ namespace BansheeEngine
 		if ((mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().RasterCaps & D3DPRASTERCAPS_DEPTHBIAS) != 0)
 		{
 			// Negate bias since D3D is backward
-			// D3D also expresses the constant bias as an absolute value, rather than 
-			// relative to minimum depth unit, so scale to fit
-			constantBias = -constantBias / 250000.0f;
+			constantBias = -constantBias;
 			HRESULT hr = setRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&constantBias));
 			if (FAILED(hr))
 				BS_EXCEPT(RenderingAPIException, "Error setting constant depth bias");

+ 1 - 1
BansheeD3D9RenderSystem/Source/BsD3D9Texture.cpp

@@ -94,7 +94,7 @@ namespace BansheeEngine
 
 	void D3D9TextureCore::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
 	{
-		if (mProperties.getMultisampleCount() > 0)
+		if (mProperties.getMultisampleCount() > 1)
 			BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
 
 		if (mProperties.getUsage() == TU_DEPTHSTENCIL || mProperties.getUsage() == TU_RENDERTARGET) // Render targets cannot be locked normally

+ 1 - 1
BansheeEditor/Source/BsBuiltinEditorResources.cpp

@@ -1751,7 +1751,7 @@ namespace BansheeEngine
 
 		RASTERIZER_STATE_DESC rasterizerDesc;
 		rasterizerDesc.polygonMode = PM_WIREFRAME;
-		rasterizerDesc.depthBias = 10000;
+		rasterizerDesc.depthBias = 0.00001f;
 
 		HRasterizerState rasterizerState = RasterizerState::create(rasterizerDesc);
 

+ 1 - 1
BansheeEditorExec/BsEditorExec.cpp

@@ -65,7 +65,7 @@ int CALLBACK WinMain(
 	InitializeDebugConsole();
 #endif
 
-	EditorApplication::startUp(RenderSystemPlugin::DX11);
+	EditorApplication::startUp(RenderSystemPlugin::OpenGL);
 	EditorApplication::instance().runMainLoop();
 	EditorApplication::shutDown();
 

+ 4 - 2
BansheeGLRenderSystem/Source/BsGLRenderAPI.cpp

@@ -522,7 +522,7 @@ namespace BansheeEngine
 
 		const RasterizerProperties& stateProps = rasterizerState->getProperties();
 
-		setDepthBias((float)stateProps.getDepthBias(), stateProps.getSlopeScaledDepthBias());
+		setDepthBias(stateProps.getDepthBias(), stateProps.getSlopeScaledDepthBias());
 
 		setCullingMode(stateProps.getCullMode());
 
@@ -1148,7 +1148,9 @@ namespace BansheeEngine
 			glEnable(GL_POLYGON_OFFSET_FILL);
 			glEnable(GL_POLYGON_OFFSET_POINT);
 			glEnable(GL_POLYGON_OFFSET_LINE);
-			glPolygonOffset(-slopeScaleBias, -constantBias);
+
+			float scaledConstantBias = -constantBias * float((1 << 24) - 1); // Note: Assumes 24-bit depth buffer
+			glPolygonOffset(slopeScaleBias, scaledConstantBias);
 		}
 		else
 		{