我想在托管代码中将纹理压缩到pvrtc,所以我找到了一个可以满足我想要的源,但是我得到了一个System.AccessViolationException。这是我到目前为止使用的代码:
[DllImport("x86/pvrtc.dll", CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr CompressTexture(byte[] data, int height, int width, int mipLevels, bool preMultiplied, bool pvrtc4bppCompression, ref IntPtr dataSizes);
public static byte[] Compress(byte[] data, int height, int width, int mipLevels, bool preMultiplied, bool pvrtc4bppCompression)
{
IntPtr dataSizesPtr = Marshal.AllocCoTaskMem(data.Length + 1);
var texDataPtr = CompressTexture(data, height, width, mipLevels, preMultiplied, pvrtc4bppCompression, ref dataSizesPtr);
//Store the size of each mipLevel
var dataSizesArray = new int[mipLevels];
//Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array.
Marshal.Copy(dataSizesPtr, dataSizesArray, 0, dataSizesArray.Length);
var levelSize = 0;
byte[] levelData = new byte[0];
for (int x = 0; x < mipLevels; x++)
{
levelSize = dataSizesArray[x];
levelData = new byte[levelSize];
Marshal.Copy(texDataPtr, levelData, 0, levelSize);
texDataPtr = IntPtr.Add(texDataPtr, levelSize);
}
return levelData;
}
非常感谢任何帮助。