使用 pthread 的光线追踪器

发布于 2024-11-13 11:46:47 字数 2211 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2024-11-20 11:46:47

您将相同的 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文