我们有一些不支持非纹理纹理的旧设备,我们有一个将ARGB纹理转换为2纹理下一个幂的功能。问题在于它很慢,我们想知道是否有更好的方法来转换这些纹理。将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'。 – 2014-09-25 15:00:55
@PaulR我正要发布相同的东西。在我们的测试中'memcpy'要快得多。 – Grapes 2014-09-25 15:05:59
好的 - 在这一点上,我希望你的内存带宽有限,所以我认为你不能在这个级别做更多的事情。然而,看起来你现在在下面的答案中有一个更“全面”的解决方案,所以我猜现在代码优化是多余的。 – 2014-09-25 15:32:46