【发布时间】:2014-12-19 05:58:05
【问题描述】:
我目前正在尝试在从文件加载后使用 SetData 在 Texture2D 对象上手动设置纹理数据。 Texture2D 是在启用 MipMaps 的情况下创建的。数据已从使用 DXT1 压缩保存的 .dds 文件加载,大小为 512x512。
这是负责创建纹理并将数据加载到其中的代码。
texture = new Texture2D(graphics, (int)ddsHeader.Width, (int)ddsHeader.Height, mipMapCount > 1, sFormat);
for (int i = 0; i < (int)mipMapCount; i++)
{
int pow = (int)Math.Pow(2, i);
int width = (int)ddsHeader.Width / pow;
int height = (int)ddsHeader.Height / pow;
Rectangle? rect = null;
// get image size
int blockSize = GetBlockSize(width, height);
Console.WriteLine(string.Format("Width {0} Height {1}", width, height));
Console.WriteLine("Block size: " + blockSize + " " + (int)ddsHeader.PitchOrLinearSize);
// read the texture
byte[] textureData = reader.ReadBytes(blockSize);
rect = new Rectangle(0, 0, width, height);
// set the color into the appropriate level of the texture
if (sFormat == SurfaceFormat.Color && dxFormat == DXGIFormat.A8B8G8R8)
{
Color[] colors = ProcessUncompressedTexture(textureData, width, height);
texture.SetData<Color>(i, rect, colors, 0, width * height);
}
else
{
texture.SetData<byte>(i, rect, textureData, 0, blockSize);
}
}
它工作正常,数据被正确加载并正确设置到每个 mip 级别 - 直到它到达第 9 级,它会抛出以下内容:ArgumentException was unhandled: The size of the data passed in is too large or too small for this resource.
这是输出。
Width 512 Height 512 // level 0
Block size: 262144 262144
Width 256 Height 256 // level 1
Block size: 65536 262144
Width 128 Height 128 // level 2
Block size: 16384 262144
Width 64 Height 64 // level 3
Block size: 4096 262144
Width 32 Height 32 // level 4
Block size: 1024 262144
Width 16 Height 16 // level 5
Block size: 256 262144
Width 8 Height 8 // level 6
Block size: 64 262144
Width 4 Height 4 // level 7
Block size: 16 262144
Width 2 Height 2 // level 8
Block size: 4 262144
现在,textureData 的计数为 4。此 mip 级别的宽度和高度为 2。矩形的大小也是 2 个单位。我看不出是什么导致了这个错误的发生,因为之前的 8 个级别设置得很好。
有谁知道这里可能发生了什么导致这种情况?
【问题讨论】: