我們有一些不支持非紋理紋理的舊設備,我們有一個將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