2
* Copyright 2011, Blender Foundation.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#define MBVH_OBJECT_SENTINEL 0x76543210
22
#define MBVH_NODE_SIZE 8
23
#define MBVH_STACK_SIZE 1024
24
#define MBVH_RAY_STACK_SIZE 10000
26
typedef struct MBVHTask {
33
typedef struct MVBHRay {
47
__device float3 mbvh_inverse_direction(float3 dir)
49
// Avoid divide by zero (ooeps = exp2f(-80.0f))
50
float ooeps = 0.00000000000000000000000082718061255302767487140869206996285356581211090087890625f;
53
idir.x = 1.0f / (fabsf(dir.x) > ooeps ? dir.x : copysignf(ooeps, dir.x));
54
idir.y = 1.0f / (fabsf(dir.y) > ooeps ? dir.y : copysignf(ooeps, dir.y));
55
idir.z = 1.0f / (fabsf(dir.z) > ooeps ? dir.z : copysignf(ooeps, dir.z));
60
__device void mbvh_instance_push(KernelGlobals *kg, int object, MBVHRay *ray)
62
Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
64
ray->P = transform_point(&tfm, ray->origP);
66
float3 dir = ray->origD;
68
if(ray->t != ray->tmax) dir *= ray->t;
70
dir = transform_direction(&tfm, dir);
71
ray->idir = mbvh_inverse_direction(normalize(dir));
73
if(ray->t != ray->tmax) ray->t = len(dir);
76
__device void mbvh_instance_pop(KernelGlobals *kg, int object, MBVHRay *ray)
78
Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
80
if(ray->t != ray->tmax)
81
ray->t = len(transform_direction(&tfm, (1.0f/(ray->idir)) * (ray->t)));
84
ray->idir = mbvh_inverse_direction(ray->origD);
87
/* Sven Woop's algorithm */
88
__device void mbvh_triangle_intersect(KernelGlobals *kg, MBVHRay *ray, int object, int triAddr)
91
float3 idir = ray->idir;
93
/* compute and check intersection t-value */
94
float4 v00 = kernel_tex_fetch(__tri_woop, triAddr*MBVH_NODE_SIZE+0);
95
float4 v11 = kernel_tex_fetch(__tri_woop, triAddr*MBVH_NODE_SIZE+1);
96
float3 dir = 1.0f/idir;
98
float Oz = v00.w - P.x*v00.x - P.y*v00.y - P.z*v00.z;
99
float invDz = 1.0f/(dir.x*v00.x + dir.y*v00.y + dir.z*v00.z);
100
float t = Oz * invDz;
102
if(t > 0.0f && t < ray->t) {
103
/* compute and check barycentric u */
104
float Ox = v11.w + P.x*v11.x + P.y*v11.y + P.z*v11.z;
105
float Dx = dir.x*v11.x + dir.y*v11.y + dir.z*v11.z;
109
/* compute and check barycentric v */
110
float4 v22 = kernel_tex_fetch(__tri_woop, triAddr*MBVH_NODE_SIZE+2);
111
float Oy = v22.w + P.x*v22.x + P.y*v22.y + P.z*v22.z;
112
float Dy = dir.x*v22.x + dir.y*v22.y + dir.z*v22.z;
115
if(v >= 0.0f && u + v <= 1.0f) {
116
/* record intersection */
117
ray->index = triAddr;
118
ray->object = object;
127
__device void mbvh_node_intersect(KernelGlobals *kg, __m128 *traverseChild,
128
__m128 *tHit, float3 P, float3 idir, float t, int nodeAddr)
131
const __m128 bminx = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*MBVH_NODE_SIZE+0);
132
const __m128 t0x = _mm_mul_ps(_mm_sub_ps(bminx, _mm_set_ps1(P.x)), _mm_set_ps1(idir.x));
133
const __m128 bmaxx = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*MBVH_NODE_SIZE+1);
134
const __m128 t1x = _mm_mul_ps(_mm_sub_ps(bmaxx, _mm_set_ps1(P.x)), _mm_set_ps1(idir.x));
136
__m128 tmin = _mm_max_ps(_mm_min_ps(t0x, t1x), _mm_setzero_ps());
137
__m128 tmax = _mm_min_ps(_mm_max_ps(t0x, t1x), _mm_set_ps1(t));
140
const __m128 bminy = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*MBVH_NODE_SIZE+2);
141
const __m128 t0y = _mm_mul_ps(_mm_sub_ps(bminy, _mm_set_ps1(P.y)), _mm_set_ps1(idir.y));
142
const __m128 bmaxy = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*MBVH_NODE_SIZE+3);
143
const __m128 t1y = _mm_mul_ps(_mm_sub_ps(bmaxy, _mm_set_ps1(P.y)), _mm_set_ps1(idir.y));
145
tmin = _mm_max_ps(_mm_min_ps(t0y, t1y), tmin);
146
tmax = _mm_min_ps(_mm_max_ps(t0y, t1y), tmax);
149
const __m128 bminz = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*MBVH_NODE_SIZE+4);
150
const __m128 t0z = _mm_mul_ps(_mm_sub_ps(bminz, _mm_set_ps1(P.z)), _mm_set_ps1(idir.z));
151
const __m128 bmaxz = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*MBVH_NODE_SIZE+5);
152
const __m128 t1z = _mm_mul_ps(_mm_sub_ps(bmaxz, _mm_set_ps1(P.z)), _mm_set_ps1(idir.z));
154
tmin = _mm_max_ps(_mm_min_ps(t0z, t1z), tmin);
155
tmax = _mm_min_ps(_mm_max_ps(t0z, t1z), tmax);
157
/* compare and get mask */
158
*traverseChild = _mm_cmple_ps(tmin, tmax);
160
/* get distance XXX probably wrong */
164
static void mbvh_sort_by_length(int id[4], float len[4])
166
for(int i = 1; i < 4; i++) {
169
while(j >= 0 && len[j] > len[j+1]) {
170
swap(len[j], len[j+1]);
171
swap(id[j], id[j+1]);
177
__device void scene_intersect(KernelGlobals *kg, MBVHRay *rays, int numrays)
179
/* traversal stacks */
180
MBVHTask task_stack[MBVH_STACK_SIZE];
181
int active_ray_stacks[4][MBVH_RAY_STACK_SIZE];
182
int num_task, num_active[4] = {0, 0, 0, 0};
183
__m128i one_mm = _mm_set1_epi32(1);
185
/* push root node task on stack */
186
task_stack[0].node = kernel_data.bvh.root;
187
task_stack[0].index = 0;
188
task_stack[0].num = numrays;
189
task_stack[0].object = ~0;
192
/* push all rays in first SIMD lane */
193
for(int i = 0; i < numrays; i++)
194
active_ray_stacks[0][i] = i;
195
num_active[0] = numrays;
197
while(num_task >= 1) {
199
MBVHTask task = task_stack[--num_task];
201
if(task.node == MBVH_OBJECT_SENTINEL) {
204
/* pop rays from stack */
205
num_active[task.index] -= task.num;
206
int ray_offset = num_active[task.index];
209
for(int i = 0; i < task.num; i++) {
210
MBVHRay *ray = &rays[active_ray_stacks[task.index][ray_offset + i]];
211
mbvh_instance_pop(kg, task.object, ray);
214
else if(task.node >= 0) {
217
/* pop rays from stack*/
218
num_active[task.index] -= task.num;
219
int ray_offset = num_active[task.index];
221
/* initialze simd values */
222
__m128i num_active_mm = _mm_load_si128((__m128i*)num_active);
223
__m128 len_mm = _mm_set_ps1(0.0f);
225
for(int i = 0; i < task.num; i++) {
226
int rayid = active_ray_stacks[task.index][ray_offset + i];
227
MVBHRay *ray = rays + rayid;
229
/* intersect 4 QBVH node children */
233
mbvh_node_intersect(kg, &result, &thit, ray->P, ray->idir, ray->t, task.node);
235
/* update length for sorting */
236
len_mm = _mm_add_ps(len_mm, _mm_and_ps(thit, result));
238
/* push rays on stack */
239
for(int j = 0; j < 4; j++)
240
active_ray_stacks[j][num_active[j]] = rayid;
242
/* update num active */
243
__m128i resulti = _mm_and_si128(*((__m128i*)&result), one_mm);
244
num_active_mm = _mm_add_epi32(resulti, num_active_mm);
245
_mm_store_si128((__m128i*)num_active, num_active_mm);
248
if(num_active[0] || num_active[1] || num_active[2] || num_active[3]) {
249
/* load child node addresses */
250
float4 cnodes = kernel_tex_fetch(__bvh_nodes, task.node);
252
__float_as_int(cnodes.x),
253
__float_as_int(cnodes.y),
254
__float_as_int(cnodes.z),
255
__float_as_int(cnodes.w)};
257
/* sort nodes by average intersection distance */
258
int ids[4] = {0, 1, 2, 3};
261
_mm_store_ps(len, len_mm);
262
mbvh_sort_by_length(ids, len);
264
/* push new tasks on stack */
265
for(int j = 0; j < 4; j++) {
269
task_stack[num_task].node = child[id];
270
task_stack[num_task].index = id;
271
task_stack[num_task].num = num_active[id];
272
task_stack[num_task].object = task.object;
279
/* fetch leaf node data */
280
float4 leaf = kernel_tex_fetch(__bvh_nodes, (-task.node-1)*MBVH_NODE_SIZE+(MBVH_NODE_SIZE-2));
281
int triAddr = __float_as_int(leaf.x);
282
int triAddr2 = __float_as_int(leaf.y);
284
/* pop rays from stack*/
285
num_active[task.index] -= task.num;
286
int ray_offset = num_active[task.index];
290
int i, numq = (task.num >> 2) << 2;
292
/* SIMD ray leaf intersection */
293
for(i = 0; i < numq; i += 4) {
295
&rays[active_ray_stacks[task.index][ray_offset + i + 0]],
296
&rays[active_ray_stacks[task.index][ray_offset + i + 1]],
297
&rays[active_ray_stacks[task.index][ray_offset + i + 2]],
298
&rays[active_ray_stacks[task.index][ray_offset + i + 3]]};
302
while(triAddr < triAddr2) {
303
mbvh_triangle_intersect(ray4[0], task.object, task.node);
304
mbvh_triangle_intersect(ray4[1], task.object, task.node);
305
mbvh_triangle_intersect(ray4[2], task.object, task.node);
306
mbvh_triangle_intersect(ray4[3], task.object, task.node);
309
/* some shadow ray optim could be done by setting t=0 */
315
/* mono ray leaf intersection */
316
for(; i < task.num; i++) {
317
MBVHRay *ray = &rays[active_ray_stacks[task.index][ray_offset + i]];
319
while(triAddr < triAddr2) {
320
mbvh_triangle_intersect(kg, ray, task.object, task.node);
327
int object = -triAddr-1;
330
/* push instance pop task */
331
task_stack[num_task].node = MBVH_OBJECT_SENTINEL;
332
task_stack[num_task].index = task.index;
333
task_stack[num_task].num = task.num;
334
task_stack[num_task].object = object;
337
num_active[task.index] += task.num;
340
task_stack[num_task].node = node;
341
task_stack[num_task].index = task.index;
342
task_stack[num_task].num = task.num;
343
task_stack[num_task].object = object;
346
for(int i = 0; i < task.num; i++) {
347
int rayid = active_ray_stacks[task.index][ray_offset + i];
349
/* push on stack for last task */
350
active_ray_stacks[task.index][num_active[task.index]] = rayid;
351
num_active[task.index]++;
354
MBVHRay *ray = &rays[rayid];
355
mbvh_instance_push(kg, object, ray);
362
__device void mbvh_set_ray(MBVHRay *rays, int i, Ray *ray, float tmax)
364
MBVHRay *mray = &rays[i];
366
/* ray parameters in registers */
368
mray->idir = mbvh_inverse_direction(ray->D);
372
__device bool mbvh_get_intersection(MVBHRay *rays, int i, Intersection *isect, float tmax)
374
MBVHRay *mray = &rays[i];
382
isect->index = mray->index;
383
isect->object = mray->object;
388
__device bool mbvh_get_shadow(MBVHRay *rays, int i, float tmax)
390
return (rays[i].t == tmax);