加速嵌套循环绘制

huangapple go评论63阅读模式
英文:

Speeding up nested loop drawing

问题

我有这段代码:

/* 从十六进制颜色中提取单个颜色分量 */
uint8_t ext_col(uint32_t value, int color) {
	return (value >> (color * 8)) & 0xff;
}

glBegin(GL_POINTS);

for (uint16_t y = 0; y < image->height; y+=1) {
    for (uint16_t x = 0; x < image->width; x+=1) {
	uint32_t p = image->data[y * image->width + x]; /* 获取像素数据 */
			
        glColor3ub(
            ext_col(p, 0),  /* 红色 */
            ext_col(p, 1),  /* 绿色 */
            ext_col(p, 2)); /* 蓝色 */
        glVertex2i(x, y);
    }
}

glEnd();

它能正常运行,但速度有点慢,有没有什么方法可以进一步加速,也许可以使用一些汇编技巧?

这是我从上一次优化(速度提高了5倍)以来的最大优化,但仍然无法以每秒60帧的速度渲染750x350像素的图像。

英文:

I have this code:

/* extract single color component from hexadecimal color */
uint8_t ext_col(uint32_t value, int color) {
	return (value &gt;&gt; (color * 8)) &amp; 0xff;
}

glBegin(GL_POINTS);

for (uint16_t y = 0; y &lt; image-&gt;height; y+=1) {
    for (uint16_t x = 0; x &lt; image-&gt;width; x+=1) {
	uint32_t p = image-&gt;data[y * image-&gt;width + x]; /* get pixel data */
			
        glColor3ub(
            ext_col(p, 0),  /* red */
            ext_col(p, 1),  /* green */
            ext_col(p, 2)); /* blue */
        glVertex2i(x, y);
    }
}

glEnd();

and it works fine, but just a bit slow, is there anyway to speed this up further, maybe some assembly tricks?

This is the furthest I've optimised it from my previous instance (5x faster), but it still renders a 750x350 image slower than 60 fps.

答案1

得分: 1

由于ext_col是一个频繁调用的小型函数,我建议将其内联以消除调用函数所带来的开销。请参阅https://stackoverflow.com/a/145841/7218062。将优化设置为-O3可能会自动执行此操作。

英文:

Since ext_col is such a small frequently called function I'd suggest making it inline to get rid of the overhead associated with calling a function. See https://stackoverflow.com/a/145841/7218062. Setting optimization to -O3 will probably do this automatically.

huangapple
  • 本文由 发表于 2023年3月7日 03:58:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定