1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
/*************************************************************************
* Copyright (C) 2010 Tavian Barnes <tavianator@gmail.com> *
* *
* This file is part of The Dimension Library. *
* *
* The Dimension Library is free software; you can redistribute it and/ *
* or modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
* The Dimension Library is distributed in the hope that it will be *
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
*************************************************************************/
#include "dimension_impl.h"
#include <unistd.h> /* For sysconf */
/*
* Boilerplate for multithreading
*/
/* Payload type for passing arguments to worker thread */
typedef struct {
dmnsn_progress *progress;
dmnsn_scene *scene;
dmnsn_kD_splay_tree *kD_splay_tree;
/* For multithreading */
unsigned int index, threads;
} dmnsn_raytrace_payload;
/* Thread callback */
static void *dmnsn_raytrace_scene_thread(void *ptr);
/* Raytrace a scene */
int
dmnsn_raytrace_scene(dmnsn_scene *scene)
{
dmnsn_progress *progress = dmnsn_raytrace_scene_async(scene);
return dmnsn_finish_progress(progress);
}
/* Raytrace a scene in the background */
dmnsn_progress *
dmnsn_raytrace_scene_async(dmnsn_scene *scene)
{
unsigned int i;
dmnsn_object *object;
dmnsn_raytrace_payload *payload;
dmnsn_progress *progress = dmnsn_new_progress();
if (progress) {
payload = malloc(sizeof(dmnsn_raytrace_payload));
if (!payload) {
dmnsn_delete_progress(progress);
return NULL;
}
payload->progress = progress;
payload->scene = scene;
payload->kD_splay_tree = dmnsn_new_kD_splay_tree();
for (i = 0; i < dmnsn_array_size(payload->scene->objects); ++i) {
dmnsn_array_get(payload->scene->objects, i, &object);
dmnsn_kD_splay_insert(payload->kD_splay_tree, object);
}
if (pthread_create(&progress->thread, NULL, &dmnsn_raytrace_scene_thread,
payload) != 0)
{
dmnsn_delete_kD_splay_tree(payload->kD_splay_tree);
free(payload);
dmnsn_delete_progress(progress);
return NULL;
}
}
return progress;
}
/* Start the multi-threaded implementation */
static int dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload);
/* Thread callback */
static void *
dmnsn_raytrace_scene_thread(void *ptr)
{
dmnsn_raytrace_payload *payload = ptr;
int *retval = malloc(sizeof(int));
if (retval) {
*retval = dmnsn_raytrace_scene_multithread(payload);
}
dmnsn_done_progress(payload->progress);
free(payload);
return retval;
}
/* Thread callback */
static void *dmnsn_raytrace_scene_multithread_thread(void *ptr);
/* Set up the multi-threaded engine */
static int
dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload)
{
int i, j, nthreads = payload->scene->nthreads;
void *ptr;
int retval = 0;
dmnsn_raytrace_payload *payloads;
pthread_t *threads;
if (!nthreads) {
/* Find the number of processors/cores running (TODO: do this portably) */
nthreads = sysconf(_SC_NPROCESSORS_ONLN);
if (nthreads < 1) {
nthreads = 1;
}
/* End non-portable section */
}
payloads = malloc(nthreads*sizeof(dmnsn_raytrace_payload));
if (!payloads) {
return 1;
}
threads = malloc(nthreads*sizeof(pthread_t));
if (!threads) {
free(payloads);
return 1;
}
/* Set up the progress object */
dmnsn_new_progress_element(payload->progress,
payload->scene->canvas->y);
/* Create the payloads */
for (i = 0; i < nthreads; ++i) {
payloads[i] = *payload;
payloads[i].index = i;
payloads[i].threads = nthreads;
if (i > 0) {
payloads[i].kD_splay_tree =
dmnsn_kD_splay_copy(payloads[0].kD_splay_tree);
}
}
/* Create the threads */
for (i = 0; i < nthreads; ++i) {
if (pthread_create(&threads[i], NULL,
&dmnsn_raytrace_scene_multithread_thread,
&payloads[i]) != 0)
{
for (j = 0; j < i; ++j) {
if (pthread_join(threads[j], &ptr)) {
dmnsn_error(DMNSN_SEVERITY_MEDIUM,
"Couldn't join worker thread in failed raytrace engine"
" initialization.");
} else {
/* Only free on a successful join - otherwise we might free a pointer
out from under a running thread */
dmnsn_delete_kD_splay_tree(payloads[j].kD_splay_tree);
free(ptr);
}
}
free(payloads);
return 1;
}
}
for (i = 0; i < nthreads; ++i) {
if (pthread_join(threads[i], &ptr)) {
dmnsn_error(DMNSN_SEVERITY_MEDIUM,
"Couldn't join worker thread in raytrace engine.");
} else {
if (retval == 0) {
retval = *(int *)ptr;
}
dmnsn_delete_kD_splay_tree(payloads[i].kD_splay_tree);
free(ptr);
}
}
free(threads);
free(payloads);
return retval;
}
/* Actual raytracing implementation */
static int dmnsn_raytrace_scene_impl(dmnsn_progress *progress,
dmnsn_scene *scene,
dmnsn_kD_splay_tree *kD_splay_tree,
unsigned int index, unsigned int threads);
/* Multi-threading thread callback */
static void *
dmnsn_raytrace_scene_multithread_thread(void *ptr)
{
dmnsn_raytrace_payload *payload = ptr;
int *retval = malloc(sizeof(int));
if (retval) {
*retval = dmnsn_raytrace_scene_impl(payload->progress, payload->scene,
payload->kD_splay_tree,
payload->index, payload->threads);
}
return retval;
}
/*
* Raytracing algorithm
*/
typedef struct dmnsn_raytrace_state {
const dmnsn_scene *scene;
const dmnsn_intersection *intersection;
dmnsn_kD_splay_tree *kD_splay_tree;
unsigned int level;
dmnsn_vector r;
dmnsn_vector viewer;
dmnsn_vector reflected;
dmnsn_color pigment;
} dmnsn_raytrace_state;
/* Main helper for dmnsn_raytrace_scene_impl - shoot a ray */
static dmnsn_color dmnsn_raytrace_shoot(dmnsn_raytrace_state *state,
dmnsn_line ray);
/* Actually raytrace a scene */
static int
dmnsn_raytrace_scene_impl(dmnsn_progress *progress, dmnsn_scene *scene,
dmnsn_kD_splay_tree *kD_splay_tree,
unsigned int index, unsigned int threads)
{
dmnsn_raytrace_state state = {
.scene = scene,
.kD_splay_tree = kD_splay_tree
};
unsigned int width = scene->canvas->x;
unsigned int height = scene->canvas->y;
/* Iterate through each pixel */
unsigned int y;
for (y = index; y < height; y += threads) {
unsigned int x;
for (x = 0; x < width; ++x) {
/* Set the pixel to the background color */
dmnsn_color color = scene->background;
if (scene->quality) {
/* Get the ray corresponding to the (x,y)'th pixel */
dmnsn_line ray = (*scene->camera->ray_fn)(
scene->camera,
((double)x)/(scene->canvas->x - 1),
((double)y)/(scene->canvas->y - 1)
);
/* Shoot a ray */
state.level = scene->limit;
color = dmnsn_raytrace_shoot(&state, ray);
}
dmnsn_set_pixel(scene->canvas, x, y, color);
}
dmnsn_increment_progress(progress);
}
return 0;
}
/* Add epsilon*l.n to l.x0, to avoid self-intersections */
static dmnsn_line
dmnsn_line_add_epsilon(dmnsn_line l)
{
return dmnsn_new_line(
dmnsn_vector_add(
l.x0,
dmnsn_vector_mul(1.0e-9, l.n)
),
l.n
);
}
/* Determine whether a callback may be called */
#define DMNSN_TEXTURE_HAS_CALLBACK(state, telem, fn) \
({ \
const dmnsn_texture *_itexture = state->intersection->texture; \
const dmnsn_texture *_dtexture = state->scene->default_texture; \
(_itexture && _itexture->telem && _itexture->telem->fn) \
? 1 \
: ((_dtexture && _dtexture->telem && _dtexture->telem->fn) ? 1 : 0); \
})
/* Call the appropriate overloaded texture callback */
#define DMNSN_TEXTURE_CALLBACK(state, telem, fn, def, ...) \
({ \
const dmnsn_texture *_itexture = state->intersection->texture; \
const dmnsn_texture *_dtexture = state->scene->default_texture; \
(_itexture && _itexture->telem && _itexture->telem->fn) \
? (*_itexture->telem->fn)(_itexture->telem, ## __VA_ARGS__) \
: ((_dtexture && _dtexture->telem && _dtexture->telem->fn) \
? (*_dtexture->telem->fn)(_dtexture->telem, ## __VA_ARGS__) \
: def); \
})
static dmnsn_color
dmnsn_raytrace_pigment(const dmnsn_raytrace_state *state)
{
return DMNSN_TEXTURE_CALLBACK(state, pigment, pigment_fn, dmnsn_black,
state->r);
}
/* Get the color of a light ray at an intersection point */
static dmnsn_color
dmnsn_raytrace_light_ray(const dmnsn_raytrace_state *state,
const dmnsn_light *light)
{
dmnsn_line shadow_ray = dmnsn_new_line(state->r,
dmnsn_vector_sub(light->x0, state->r));
/* Add epsilon to avoid hitting ourselves with the shadow ray */
shadow_ray = dmnsn_line_add_epsilon(shadow_ray);
/* Check if we're casting a shadow on ourself */
if (dmnsn_vector_dot(shadow_ray.n, state->intersection->normal) < 0.0)
return dmnsn_black;
dmnsn_color color = (*light->light_fn)(light, state->r);
unsigned int level = state->level;
while (level) {
dmnsn_intersection *shadow_caster = dmnsn_kD_splay_search(
state->kD_splay_tree,
shadow_ray
);
if (!shadow_caster || shadow_caster->t > 1.0) {
dmnsn_delete_intersection(shadow_caster);
break;
}
dmnsn_raytrace_state shadow_state = *state;
shadow_state.intersection = shadow_caster;
shadow_state.level = level;
dmnsn_color pigment = dmnsn_raytrace_pigment(&shadow_state);
if (pigment.filter || pigment.trans) {
color = dmnsn_color_filter(color, pigment);
shadow_ray.x0 = dmnsn_line_point(shadow_ray, shadow_caster->t);
shadow_ray.n = dmnsn_vector_sub(light->x0, shadow_ray.x0);
shadow_ray = dmnsn_line_add_epsilon(shadow_ray);
} else {
dmnsn_delete_intersection(shadow_caster);
return dmnsn_black;
}
dmnsn_delete_intersection(shadow_caster);
--level;
}
return color;
}
static dmnsn_color
dmnsn_raytrace_lighting(const dmnsn_raytrace_state *state)
{
/* The illuminated color */
dmnsn_color illum = DMNSN_TEXTURE_CALLBACK(state, finish, ambient_fn,
dmnsn_black, state->pigment);
if (!DMNSN_TEXTURE_HAS_CALLBACK(state, finish, diffuse_fn)
&& !DMNSN_TEXTURE_HAS_CALLBACK(state, finish, specular_fn))
{
return illum;
}
const dmnsn_light *light;
unsigned int i;
/* Iterate over each light */
for (i = 0; i < dmnsn_array_size(state->scene->lights); ++i) {
dmnsn_array_get(state->scene->lights, i, &light);
dmnsn_color light_color = dmnsn_raytrace_light_ray(state, light);
if (!dmnsn_color_is_black(light_color)) {
if (state->scene->quality & DMNSN_RENDER_FINISH) {
dmnsn_vector ray = dmnsn_vector_normalize(
dmnsn_vector_sub(light->x0, state->r)
);
/* Get this light's color contribution to the object */
dmnsn_color diffuse = DMNSN_TEXTURE_CALLBACK(
state, finish, diffuse_fn, dmnsn_black,
light_color, state->pigment, ray, state->intersection->normal
);
dmnsn_color specular = DMNSN_TEXTURE_CALLBACK(
state, finish, specular_fn, dmnsn_black,
light_color, state->pigment, ray, state->intersection->normal,
state->viewer
);
illum = dmnsn_color_add(diffuse, illum);
illum = dmnsn_color_add(specular, illum);
} else {
illum = state->pigment;
}
}
}
return illum;
}
static dmnsn_color
dmnsn_raytrace_reflection(const dmnsn_raytrace_state *state, dmnsn_color color)
{
dmnsn_color refl = color;
if (DMNSN_TEXTURE_HAS_CALLBACK(state, finish, reflection_fn)) {
dmnsn_line refl_ray = dmnsn_new_line(state->r, state->reflected);
refl_ray = dmnsn_line_add_epsilon(refl_ray);
dmnsn_raytrace_state recursive_state = *state;
dmnsn_color rec = dmnsn_raytrace_shoot(&recursive_state, refl_ray);
dmnsn_color contrib = DMNSN_TEXTURE_CALLBACK(
state, finish, reflection_fn, dmnsn_black,
rec, state->pigment, state->reflected, state->intersection->normal
);
refl = dmnsn_color_add(contrib, refl);
}
return refl;
}
static dmnsn_color
dmnsn_raytrace_translucency(const dmnsn_raytrace_state *state,
dmnsn_color color)
{
dmnsn_color trans = color;
if (state->pigment.filter || state->pigment.trans) {
trans = dmnsn_color_mul(1.0 - state->pigment.trans, color);
dmnsn_line trans_ray = dmnsn_new_line(state->r, state->intersection->ray.n);
trans_ray = dmnsn_line_add_epsilon(trans_ray);
dmnsn_raytrace_state recursive_state = *state;
dmnsn_color rec = dmnsn_raytrace_shoot(&recursive_state, trans_ray);
dmnsn_color filtered = dmnsn_color_filter(rec, state->pigment);
trans = dmnsn_color_add(trans, filtered);
}
return trans;
}
/* Shoot a ray, and calculate the color, using `color' as the background */
static dmnsn_color
dmnsn_raytrace_shoot(dmnsn_raytrace_state *state, dmnsn_line ray)
{
if (state->level <= 0)
return dmnsn_black;
--state->level;
dmnsn_intersection *intersection
= dmnsn_kD_splay_search(state->kD_splay_tree, ray);
dmnsn_color color = state->scene->background;
if (intersection) {
state->intersection = intersection;
state->r = dmnsn_line_point(state->intersection->ray,
state->intersection->t);
state->viewer = dmnsn_vector_normalize(
dmnsn_vector_negate(state->intersection->ray.n)
);
state->reflected = dmnsn_vector_sub(
dmnsn_vector_mul(
2*dmnsn_vector_dot(state->viewer, state->intersection->normal),
state->intersection->normal),
state->viewer
);
/* Pigment */
state->pigment = dmnsn_black;
if (state->scene->quality & DMNSN_RENDER_PIGMENT) {
state->pigment = dmnsn_raytrace_pigment(state);
}
color = state->pigment;
/* Finishes and shadows */
dmnsn_color illum = color;
if (state->scene->quality & DMNSN_RENDER_LIGHTS) {
illum = dmnsn_raytrace_lighting(state);
}
color = illum;
/* Reflection */
dmnsn_color refl = illum;
if (state->scene->quality & DMNSN_RENDER_REFLECTION) {
refl = dmnsn_raytrace_reflection(state, refl);
}
color = refl;
/* Translucency */
dmnsn_color trans = color;
trans.filter = 0.0;
trans.trans = 0.0;
if (state->scene->quality & DMNSN_RENDER_TRANSLUCENCY) {
trans = dmnsn_raytrace_translucency(state, trans);
}
color = trans;
dmnsn_delete_intersection(intersection);
}
return color;
}
|