<分区>
任务
对于我自己的 OpenGL 框架,我正在尝试在平台不支持时解压缩 PVRTC 纹理。为此我下载了 PVRTC-SDK并将 C++ 代码 1:1 翻译成具有非托管代码的 C# 代码。
问题
简而言之:它不起作用,我不知道它的原因。我已经检查了两次代码,它似乎没问题,应该可以工作。 有时我得到一个 AccessViolationException
,有时一个LoaderLock
。AccessViolationException
不再发生,一直是一个错误我的代码的另一部分。 PVRTC 的颜色仍然是错误的。看起来很奇怪,基本笔画是可见的,但是颜色不对。
代码
因为代码比较大所以上传了:
[C#] http://codeviewer.org/view/code:2a65
[C++] http://codeviewer.org/view/code:2a66
也是 C# 方法的调用代码,大小实际上与预期值匹配(因此源数组的图像值恰好是 width * height * 4bit
输出大小是 width * height * 4byte
(因此是压缩图像的 8 倍大小)。
private static unsafe Image ConvertPVRTC_RGBA8888(Image source)
{
Int32 mipmapCount = source.HasMipmaps ? Image.GetMipmapCount(source.Width, source.Height) : 1;
// Create a array with bytes for the target format
Byte[] bytes = new Byte[RGBA8.GetMipMappedSize(0, mipmapCount, source.Width, source.Height)];
fixed (Byte* pTarget = bytes)
{
fixed (Byte* pSource = source.GetData())
{
Int32 sourceOffset = 0;
Int32 targetOffset = 0;
for (Int32 i = 0; i < mipmapCount; i++)
{
// Calculate offsets
Int32 sourceSize = source.Format.GetMipMappedSize(i, 1, source.Width, source.Height);
Int32 targetSize = RGBA8.GetMipMappedSize(i, 1, source.Width, source.Height);
// Decompress mipmapLevel
PVRTC.DecompressPVRTC(pSource + sourceOffset, pTarget + targetOffset, GetMipmapSize(source.Width, i), GetMipmapSize(source.Height, i), source.Format.BitsPerPixel == 2);
// Calculate pointer offset
sourceOffset += sourceSize;
targetOffset += targetSize;
}
}
}
return new Image(bytes, ImageFormat.RGBA8, source.Width, source.Height, source.HasMipmaps, source.IsCubemap);
}
想法
要么原始源代码不正确,要么我在翻译代码时漏掉了一些东西。无论如何,我需要一个解决方案并检查了两次源代码。
(如果有任何 C# 的源代码我会使用它,但我找不到任何)