使用 pthread 的光线追踪器
int main(int a, char *args[]) {
int i;
pthread_t threads[8];
unsigned int* pixmap = malloc(1024*1024*sizeof(int));
v_threadargs.height = 1024.0f;
v_threadargs.width = 1024.0f;
v_threadargs.pixmap = pixmap;
for (i = 0; i < 8; i++) {
v_threadargs.threadid = i;
pthread_create(&threads[i], NULL, render, (void *) &v_threadargs);
}
for (i = 0; i < 8; i++)
pthread_join(threads[i], NULL);
writetga(pixmap, 1024, 1024, "ray8out.tga");
free(pixmap);
pthread_exit(NULL);
return 0;
}
void *render(void *r_threadargs) {
int i,j, threadid, start;
float height, width;
int *pixmap;
struct s_threadargs *p_threadargs;
p_threadargs = (struct s_threadargs *) r_threadargs;
height = p_threadargs -> height;
width = p_threadargs -> width;
pixmap = p_threadargs -> pixmap;
threadid = p_threadargs -> threadid;
stepy = viewplaney0;
deltax = (viewplanex1 - viewplanex0) / width;
deltay = (viewplaney1 - viewplaney0) / height;
stepy += deltay;
float *viewer = (float[3]){0.0f, 0.0f, -7.0f};
if (threadid == 1)
start = 0;
else
start = threadid * height/8;
for (i = start; i < (threadid + 1)*(height/8); i++) {
stepx = viewplanex0;
for (j = 0; j < width; j++) {
float *color = (float[3]){0.0f, 0.0f, 0.0f};
float *raydir = (float[3]){stepx - viewer[0], stepy - viewer[1], 0 - viewer[2]};
float maxdist = 100000.0f;
normalize(raydir);
trace(raydir, viewer, color, 0,maxdist);
int r = (int)(roundf(color[0]*255.0f));
if (r > 255) { r = 255; }
int g = (int)(roundf(color[1]*255.0f));
if (g > 255) { g = 255; }
int b = (int)(roundf(color[2]*255.0f));
if (b > 255) { b = 255; }
pixmap[j+i*(int)width] = (r << 16) | (g << 8) | (b);
stepx += deltax;
}
stepy += deltay;
}
}
我正在尝试使用 8 个线程(使用 gcc 的 pthreads)来实现此 raytracer 程序,但输出图像“ray8out.tga”不正确。程序正确执行,没有任何错误,但逻辑上缺少一些东西。如果有人能帮助我,我会很高兴,错误在哪里?
int main(int a, char *args[]) {
int i;
pthread_t threads[8];
unsigned int* pixmap = malloc(1024*1024*sizeof(int));
v_threadargs.height = 1024.0f;
v_threadargs.width = 1024.0f;
v_threadargs.pixmap = pixmap;
for (i = 0; i < 8; i++) {
v_threadargs.threadid = i;
pthread_create(&threads[i], NULL, render, (void *) &v_threadargs);
}
for (i = 0; i < 8; i++)
pthread_join(threads[i], NULL);
writetga(pixmap, 1024, 1024, "ray8out.tga");
free(pixmap);
pthread_exit(NULL);
return 0;
}
void *render(void *r_threadargs) {
int i,j, threadid, start;
float height, width;
int *pixmap;
struct s_threadargs *p_threadargs;
p_threadargs = (struct s_threadargs *) r_threadargs;
height = p_threadargs -> height;
width = p_threadargs -> width;
pixmap = p_threadargs -> pixmap;
threadid = p_threadargs -> threadid;
stepy = viewplaney0;
deltax = (viewplanex1 - viewplanex0) / width;
deltay = (viewplaney1 - viewplaney0) / height;
stepy += deltay;
float *viewer = (float[3]){0.0f, 0.0f, -7.0f};
if (threadid == 1)
start = 0;
else
start = threadid * height/8;
for (i = start; i < (threadid + 1)*(height/8); i++) {
stepx = viewplanex0;
for (j = 0; j < width; j++) {
float *color = (float[3]){0.0f, 0.0f, 0.0f};
float *raydir = (float[3]){stepx - viewer[0], stepy - viewer[1], 0 - viewer[2]};
float maxdist = 100000.0f;
normalize(raydir);
trace(raydir, viewer, color, 0,maxdist);
int r = (int)(roundf(color[0]*255.0f));
if (r > 255) { r = 255; }
int g = (int)(roundf(color[1]*255.0f));
if (g > 255) { g = 255; }
int b = (int)(roundf(color[2]*255.0f));
if (b > 255) { b = 255; }
pixmap[j+i*(int)width] = (r << 16) | (g << 8) | (b);
stepx += deltax;
}
stepy += deltay;
}
}
I am trying to implement this raytracer program using 8 threads (pthreads using gcc), but output image "ray8out.tga" is not correct. Program is executing correcly without any error but something missing in logic. It will be pleasure if someone please could help me, where is the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将相同的 threadargs 结构传递给每个线程;无法保证它们会在您更改 threadid 值之前开始运行。您应该使用 malloc 为每个线程分配一个新的 threadargs 结构(并从新线程本身中释放它)。
You're passing the same threadargs structure to every thread; there's no guarentee that they'll start running before you change the threadid value. You should allocate a new threadargs structure with malloc for each thread (and free it from within the new thread itself).