Ray *make_ray(Vect orig, Vect dir) { Ray *r; r = make_ray(); VectCopy(r->orig, orig); VectCopy(r->dir, dir); return r; }
/** Generate sample rays for AO * @param state global renderer state * @param sample_buffer sample buffer * @param isect_buffer hit buffer * @param ray_buffer sample ray buffer * @param mask_buffer the mask bitmap to write to * @param n_hits number of hits to work with */ void kernel_ao_gen_rays(state_t state, sample_t* sample_buffer, hit_t* isect_buffer, ray_t* ray_buffer, int* mask_buffer, int i) { //generate the sample if(isect_buffer[i].t == -1) { mask_buffer[i] = 1; } else { mask_buffer[i] = 0; vec3 d = sample_sphere(&sample_buffer[i]); if(dot(d,isect_buffer[i].n) < 0) { d = neg_vec3(d); } ray_buffer[i] = make_ray(isect_buffer[i].h, d); } }
void init_raytracing() { ray_cam = make_camera(); maxlevel = 1; minweight = 0.01; rayeps = 1e-7; eye_ray = make_ray(); image_i = 0; image_j = 0; wrote_image = false; }
__global__ void kernel_ao_gen_rays_gpu(state_t state, sample_t* sample_buffer, hit_t* isect_buffer, ray_t* ray_buffer, int* mask_buffer, int w) { int i = GET_INDEX(); if(isect_buffer[i].t == -1) { mask_buffer[i] = 1; } else { mask_buffer[i] = 0; vec3 d = sample_sphere(&sample_buffer[i]); if(dot(d,isect_buffer[i].n) < 0) { d = neg_vec3(d); } ray_buffer[i] = make_ray(isect_buffer[i].h, d); } }
int main() { const uint32_t samples_per_pixel = 16; const uint32_t width = 1280; const uint32_t height = 720; const vec3 cam_pos(0, 0, 25); const vec3 look_at(0, 0, -1); const vec3 up(0, 1, 0); camera cam(cam_pos, look_at, up, 90, (float)width / height); image img(width, height); sphere s(vec3(0, 0, -0.5), 1); vec3 light_dir = normalize(vec3(-1, -1, -1)); vec3 ambient_color = vec3(1.0, 0, 0); vec3 *pixels = img.pixels; for (uint32_t y = 0; y < height; ++y) { for (uint32_t x = 0; x < width; ++x) { vec3 color(0, 0, 0); for (uint32_t sample = 0; sample < samples_per_pixel; ++sample) { const ray r = make_ray( cam, (x + rand_float(generator)) / width, (height - (y + rand_float(generator))) / height ); vec3 hit; vec3 normal; if (intersect(s, r, &hit, &normal)) { vec3 dir_light_color = fmaxf(0, dot(normal, -light_dir)) * vec3(1.0, 1.0, 1.0); color += saturate(ambient_color + dir_light_color); } } color /= samples_per_pixel; *pixels++ = color; } } save_as_png(img, "test.png"); return 0; }
int main(int argc, char *argv[]) { if(argc < 3) { printf("Usage: %s <cases> <hitcases>\n", argv[0]); exit(0); } int cases = atoi(argv[1]); int hitcases = atoi(argv[2]); printf("AABox double precision benchmark: cases = %d, hitcases = %d, looping %d times\n\n", cases, hitcases, LOOPS); ray *rays = new ray[cases]; aabox *aabbs = new aabox[cases]; // generate the ray-box combinations that intersect int numcases = 0; while(numcases < hitcases) { ray r; aabox b; make_ray(drand(), drand(), drand(), drand(), drand(), drand(), &r); make_aabox(drand(), drand(), drand(), drand(), drand(), drand(), &b); double t; if(standard_div(&r, &b, &t)) { rays[numcases] = r; aabbs[numcases] = b; numcases++; } } // generate the ray-box combinations that don't intersect while(numcases < cases) { ray r; aabox b; make_ray(drand(), drand(), drand(), drand(), drand(), drand(), &r); make_aabox(drand(), drand(), drand(), drand(), drand(), drand(), &b); double t; if(!standard_div(&r, &b, &t)) { rays[numcases] = r; aabbs[numcases] = b; numcases++; } } // verify that all the different algorithms produce similar results for(int i = 0; i < cases; i++) { double hit_t = 0; bool hit = standard_div(&rays[i], &aabbs[i], &hit_t); double t = 0; if(hit != standard_mul(&rays[i], &aabbs[i], &t)) printf("error: standard_mul()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: standard_mul() - t\n"); if(hit != pluecker(&rays[i], &aabbs[i])) printf("error: pluecker()\n"); if(hit != plueckerint_div(&rays[i], &aabbs[i], &t)) printf("error: plueckerint_div()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: plueckerint_div() - t\n"); if(hit != plueckerint_mul(&rays[i], &aabbs[i], &t)) printf("error: plueckerint_mul()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: plueckerint_mul() - t\n"); if(hit != pluecker_cls(&rays[i], &aabbs[i])) printf("error: pluecker_cls()\n"); if(hit != pluecker_cls_cff(&rays[i], &aabbs[i])) printf("error: pluecker_cls_cff()\n"); if(hit != plueckerint_div_cls(&rays[i], &aabbs[i], &t)) printf("error: plueckerint_div_cls()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: plueckerint_div_cls() - t\n"); if(hit != plueckerint_div_cls_cff(&rays[i], &aabbs[i], &t)) printf("error: plueckerint_div_cls_cff()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: plueckerint_div_cls_cff() - t\n"); if(hit != plueckerint_mul_cls(&rays[i], &aabbs[i], &t)) printf("error: plueckerint_mul_cls()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: plueckerint_mul_cls() - t\n"); if(hit != plueckerint_mul_cls_cff(&rays[i], &aabbs[i], &t)) printf("error: plueckerint_mul_cls_cff()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: plueckerint_mul_cls_cff() - t\n"); if(hit != smits_div(&rays[i], &aabbs[i], &t)) printf("error: smits_div()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: smits_div() - t\n"); if(hit != smits_div_cls(&rays[i], &aabbs[i], &t)) printf("error: smits_div_cls()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: smits_div_cls() - t\n"); if(hit != smits_mul(&rays[i], &aabbs[i], &t)) printf("error: smits_mul()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: smits_mul() - t\n"); if(hit != smits_mul_cls(&rays[i], &aabbs[i], &t)) printf("error: smits_mul_cls()\n"); if(hit && (fabs(hit_t - t) > EPSILON)) printf("error: smits_mul_cls() - t\n"); } // benchmark the individual algorithms { clock_t starttime = clock(); int hits = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(pluecker(&rays[i], &aabbs[i])) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("pluecker:\t\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(pluecker_cls(&rays[i], &aabbs[i])) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("pluecker_cls:\t\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(pluecker_cls_cff(&rays[i], &aabbs[i])) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("pluecker_cls_cff:\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } printf("\n"); { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(plueckerint_div(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("plueckerint_div:\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(plueckerint_div_cls(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("plueckerint_div_cls:\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(plueckerint_div_cls_cff(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("plueckerint_div_cls_cff: time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(plueckerint_mul(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("plueckerint_mul:\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(plueckerint_mul_cls(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("plueckerint_mul_cls:\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(plueckerint_mul_cls_cff(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("plueckerint_mul_cls_cff: time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } printf("\n"); { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(standard_div(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("standard_div:\t\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(standard_mul(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("standard_mul:\t\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } printf("\n"); { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(smits_div(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("smits_div:\t\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(smits_div_cls(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("smits_div_cls:\t\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(smits_mul(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("smits_mul:\t\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } { clock_t starttime = clock(); int hits = 0; double t = 0; for(int l = 0; l < LOOPS; l++) for(int i = 0; i < cases; i++) { if(smits_mul_cls(&rays[i], &aabbs[i], &t)) hits++; } clock_t endtime = clock(); if(hits != hitcases * LOOPS) printf("error\n"); printf("smits_mul_cls:\t\t time = %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); } return 0; }