Browse Source

Bugfix: Fixing block compression texture formats

BearishSun 8 years ago
parent
commit
778dc93339

+ 0 - 3
Source/BansheeCore/Image/BsPixelUtil.cpp

@@ -2402,9 +2402,6 @@ namespace bs
 		interimData.allocateInternalBuffer();
 		bulkPixelConversion(src, interimData);
 
-		if(interimFormat != PF_RGBA32F)
-			flipComponentOrder(interimData);
-
 		nvtt::InputOptions io;
 		io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
 		io.setMipmapGeneration(false);

+ 6 - 5
Source/BansheeD3D11RenderAPI/BsD3D11Mappings.cpp

@@ -3,6 +3,7 @@
 #include "BsD3D11Mappings.h"
 #include "Debug/BsDebug.h"
 #include "Error/BsException.h"
+#include "Math/BsMath.h"
 
 namespace bs { namespace ct
 {
@@ -907,21 +908,21 @@ namespace bs { namespace ct
 
 	UINT32 D3D11Mappings::getSizeInBytes(PixelFormat pf, UINT32 width, UINT32 height)
 	{
-		if(width == 0 || height == 0)
-			return 0;
-
 		if(PixelUtil::isCompressed(pf))
 		{
+			UINT32 blockWidth = Math::divideAndRoundUp(width, 4U);
+			UINT32 blockHeight = Math::divideAndRoundUp(height, 4U);
+
 			// D3D wants the width of one row of cells in bytes
 			if (pf == PF_BC1 || pf == PF_BC4)
 			{
 				// 64 bits (8 bytes) per 4x4 block
-				return std::max<UINT32>(1, width / 4) * std::max<UINT32>(1, height / 4) * 8;
+				return blockWidth * blockHeight * 8;
 			}
 			else
 			{
 				// 128 bits (16 bytes) per 4x4 block
-				return std::max<UINT32>(1, width / 4) * std::max<UINT32>(1, height / 4) * 16;
+				return blockWidth * blockHeight * 16;
 			}
 		}
 		else

+ 31 - 7
Source/BansheeD3D11RenderAPI/BsD3D11Texture.cpp

@@ -178,8 +178,21 @@ namespace bs { namespace ct
 		{
 			UINT8* data = (UINT8*)mapstagingbuffer(flags, face, mipLevel, rowPitch, slicePitch);
 			lockedArea.setExternalBuffer(data);
-			lockedArea.setRowPitch(rowPitch);
-			lockedArea.setSlicePitch(slicePitch);
+
+			if (PixelUtil::isCompressed(mProperties.getFormat()))
+			{
+				// Doesn't make sense to provide pitch values in pixels in this case
+				lockedArea.setRowPitch(0);
+				lockedArea.setSlicePitch(0);
+			}
+			else
+			{
+				UINT32 bytesPerPixel = PixelUtil::getNumElemBytes(mProperties.getFormat());
+
+				lockedArea.setRowPitch(rowPitch / bytesPerPixel);
+				lockedArea.setSlicePitch(slicePitch / bytesPerPixel);
+			}
+
 			mLockedForReading = true;
 		}
 		else
@@ -188,8 +201,20 @@ namespace bs { namespace ct
 			{
 				UINT8* data = (UINT8*)map(mTex, flags, face, mipLevel, rowPitch, slicePitch);
 				lockedArea.setExternalBuffer(data);
-				lockedArea.setRowPitch(rowPitch);
-				lockedArea.setSlicePitch(slicePitch);
+
+				if (PixelUtil::isCompressed(mProperties.getFormat()))
+				{
+					// Doesn't make sense to provide pitch values in pixels in this case
+					lockedArea.setRowPitch(0);
+					lockedArea.setSlicePitch(0);
+				}
+				else
+				{
+					UINT32 bytesPerPixel = PixelUtil::getNumElemBytes(mProperties.getFormat());
+
+					lockedArea.setRowPitch(rowPitch / bytesPerPixel);
+					lockedArea.setSlicePitch(slicePitch / bytesPerPixel);
+				}
 			}
 			else
 				lockedArea.setExternalBuffer((UINT8*)mapstaticbuffer(lockedArea, mipLevel, face));
@@ -676,9 +701,8 @@ namespace bs { namespace ct
 			BS_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
 		}
 
-		UINT32 bytesPerPixel = PixelUtil::getNumElemBytes(mProperties.getFormat());
-		rowPitch = pMappedResource.RowPitch / bytesPerPixel;
-		slicePitch = pMappedResource.DepthPitch / bytesPerPixel;
+		rowPitch = pMappedResource.RowPitch;
+		slicePitch = pMappedResource.DepthPitch;
 
 		return pMappedResource.pData;
 	}