【发布时间】:2014-09-25 14:51:16
【问题描述】:
我们有一些不支持非罐纹理的旧设备,并且我们有一个将 ARGB 纹理转换为 2 次幂纹理的功能。问题是它很慢,我们想知道是否有更好的方法来转换这些纹理。
void PotTexture()
{
size_t u2 = 1; while (u2 < imageData.width) u2 *= 2;
size_t v2 = 1; while (v2 < imageData.height) v2 *= 2;
std::vector<unsigned char> pottedImageData;
pottedImageData.resize(u2 * v2 * 4);
size_t y, x, c;
for (y = 0; y < imageData.height; y++)
{
for (x = 0; x < imageData.width; x++)
{
for (c = 0; c < 4; c++)
{
pottedImageData[4 * u2 * y + 4 * x + c] = imageData.convertedData[4 * imageData.width * y + 4 * x + c];
}
}
}
imageData.width = u2;
imageData.height = v2;
std::swap(imageData.convertedData, pottedImageData);
}
在某些设备上,这可以轻松使用 100% 的 CPU,因此任何优化都会令人惊叹。有没有我可以查看的执行此转换的现有函数?
编辑:
我已经稍微优化了上面的循环:
for (y = 0; y < imageData.height; y++)
{
memcpy(
&(pottedImageData[y * u2 * 4]),
&(imageData.convertedData[y * imageData.width * 4]),
imageData.width * 4);
}
【问题讨论】:
-
使用
memcpy进行优化,而不是memmove,因为您知道源缓冲区和目标缓冲区不能重叠。 -
@PaulR 我正要发布同样的内容。在我们的测试中,
memcpy的速度要快得多。 -
好的——在这一点上,我希望你的内存带宽非常有限,所以我认为你在这个级别上没有什么可以做的。但是,看起来您现在在下面的答案中有一个更“全面”的解决方案,所以我猜代码优化现在是多余的。