英文:
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 >> (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]; /* 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论