diff options
Diffstat (limited to 'source/blender/render/intern/raytrace')
13 files changed, 274 insertions, 412 deletions
diff --git a/source/blender/render/intern/raytrace/bvh.h b/source/blender/render/intern/raytrace/bvh.h index ac86a65ff0b..946b64e98e5 100644 --- a/source/blender/render/intern/raytrace/bvh.h +++ b/source/blender/render/intern/raytrace/bvh.h @@ -88,9 +88,9 @@ static int rayobject_bb_intersect_test(const Isect *isec, const float *_bb) RE_RC_COUNT(isec->raycounter->bb.test); - if(t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return 0; - if(t2x < 0.0 || t2y < 0.0 || t2z < 0.0) return 0; - if(t1x > isec->dist || t1y > isec->dist || t1z > isec->dist) return 0; + if (t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return 0; + if (t2x < 0.0 || t2y < 0.0 || t2z < 0.0) return 0; + if (t1x > isec->dist || t1y > isec->dist || t1z > isec->dist) return 0; RE_RC_COUNT(isec->raycounter->bb.hit); return 1; @@ -113,10 +113,10 @@ template<class Tree> static void bvh_done(Tree *obj); template<class Tree> static void bvh_free(Tree *obj) { - if(obj->builder) + if (obj->builder) rtbuild_free(obj->builder); - if(obj->node_arena) + if (obj->node_arena) BLI_memarena_free(obj->node_arena); MEM_freeN(obj); @@ -125,7 +125,7 @@ static void bvh_free(Tree *obj) template<class Tree> static void bvh_bb(Tree *obj, float *min, float *max) { - if(obj->root) + if (obj->root) bvh_node_merge_bb(obj->root, min, max); } @@ -149,12 +149,10 @@ template<class Node> static inline int bvh_node_hit_test(Node *node, Isect *isec template<class Node> static inline void bvh_node_merge_bb(Node *node, float *min, float *max) { - if(is_leaf(node)) - { + if (is_leaf(node)) { RE_rayobject_merge_bb( (RayObject*)node, min, max); } - else - { + else { DO_MIN(node->bb , min); DO_MAX(node->bb+3, max); } @@ -173,26 +171,22 @@ static int bvh_node_stack_raycast(Node *root, Isect *isec) Node *stack[MAX_STACK_SIZE]; int hit = 0, stack_pos = 0; - if(!TEST_ROOT && !is_leaf(root)) + if (!TEST_ROOT && !is_leaf(root)) bvh_node_push_childs(root, isec, stack, stack_pos); else stack[stack_pos++] = root; - while(stack_pos) - { + while (stack_pos) { Node *node = stack[--stack_pos]; - if(!is_leaf(node)) - { - if(bvh_node_hit_test(node,isec)) - { + if (!is_leaf(node)) { + if (bvh_node_hit_test(node,isec)) { bvh_node_push_childs(node, isec, stack, stack_pos); assert(stack_pos <= MAX_STACK_SIZE); } } - else - { + else { hit |= RE_rayobject_intersect( (RayObject*)node, isec); - if(SHADOW && hit) return hit; + if (SHADOW && hit) return hit; } } return hit; @@ -212,11 +206,9 @@ static int bvh_node_stack_raycast_simd(Node *root, Isect *isec) int hit = 0, stack_pos = 0; - if(!TEST_ROOT) - { - if(!is_leaf(root)) - { - if(!is_leaf(root->child)) + if (!TEST_ROOT) { + if (!is_leaf(root)) { + if (!is_leaf(root->child)) bvh_node_push_childs(root, isec, stack, stack_pos); else return RE_rayobject_intersect( (RayObject*)root->child, isec); @@ -224,19 +216,16 @@ static int bvh_node_stack_raycast_simd(Node *root, Isect *isec) else return RE_rayobject_intersect( (RayObject*)root, isec); } - else - { - if(!is_leaf(root)) + else { + if (!is_leaf(root)) stack[stack_pos++] = root; else return RE_rayobject_intersect( (RayObject*)root, isec); } - while(true) - { + while (true) { //Use SIMD 4 - if(stack_pos >= 4) - { + if (stack_pos >= 4) { __m128 t_bb[6]; Node * t_node[4]; @@ -286,41 +275,33 @@ static int bvh_node_stack_raycast_simd(Node *root, Isect *isec) RE_RC_COUNT(isec->raycounter->simd_bb.test); int res = test_bb_group4( t_bb, isec ); - for(int i=0; i<4; i++) - if(res & (1<<i)) - { + for (int i = 0; i < 4; i++) + if (res & (1 << i)) { RE_RC_COUNT(isec->raycounter->simd_bb.hit); - if(!is_leaf(t_node[i])) - { - for(Node *t=t_node[i]; t; t=t->sibling) - { + if (!is_leaf(t_node[i])) { + for (Node *t = t_node[i]; t; t = t->sibling) { assert(stack_pos < MAX_STACK_SIZE); stack[stack_pos++] = t; } } - else - { + else { hit |= RE_rayobject_intersect( (RayObject*)t_node[i], isec); - if(hit && isec->mode == RE_RAY_SHADOW) return hit; + if (hit && isec->mode == RE_RAY_SHADOW) return hit; } } } - else if(stack_pos > 0) - { + else if (stack_pos > 0) { Node *node = stack[--stack_pos]; assert(!is_leaf(node)); - if(bvh_node_hit_test(node,isec)) - { - if(!is_leaf(node->child)) - { + if (bvh_node_hit_test(node,isec)) { + if (!is_leaf(node->child)) { bvh_node_push_childs(node, isec, stack, stack_pos); assert(stack_pos <= MAX_STACK_SIZE); } - else - { + else { hit |= RE_rayobject_intersect( (RayObject*)node->child, isec); - if(hit && isec->mode == RE_RAY_SHADOW) return hit; + if (hit && isec->mode == RE_RAY_SHADOW) return hit; } } } @@ -338,41 +319,41 @@ template<class Node> static int bvh_node_raycast(Node *node, Isect *isec) { int hit = 0; - if(bvh_test_node(node, isec)) + if (bvh_test_node(node, isec)) { - if(isec->idot_axis[node->split_axis] > 0.0f) + if (isec->idot_axis[node->split_axis] > 0.0f) { int i; for(i=0; i<BVH_NCHILDS; i++) - if(!is_leaf(node->child[i])) + if (!is_leaf(node->child[i])) { - if(node->child[i] == 0) break; + if (node->child[i] == 0) break; hit |= bvh_node_raycast(node->child[i], isec); - if(hit && isec->mode == RE_RAY_SHADOW) return hit; + if (hit && isec->mode == RE_RAY_SHADOW) return hit; } else { hit |= RE_rayobject_intersect( (RayObject*)node->child[i], isec); - if(hit && isec->mode == RE_RAY_SHADOW) return hit; + if (hit && isec->mode == RE_RAY_SHADOW) return hit; } } else { int i; for(i=BVH_NCHILDS-1; i>=0; i--) - if(!is_leaf(node->child[i])) + if (!is_leaf(node->child[i])) { - if(node->child[i]) + if (node->child[i]) { hit |= dfs_raycast(node->child[i], isec); - if(hit && isec->mode == RE_RAY_SHADOW) return hit; + if (hit && isec->mode == RE_RAY_SHADOW) return hit; } } else { hit |= RE_rayobject_intersect( (RayObject*)node->child[i], isec); - if(hit && isec->mode == RE_RAY_SHADOW) return hit; + if (hit && isec->mode == RE_RAY_SHADOW) return hit; } } } @@ -385,28 +366,22 @@ void bvh_dfs_make_hint(Node *node, LCTSHint *hint, int reserve_space, HintObject { assert( hint->size + reserve_space + 1 <= RE_RAY_LCTS_MAX_SIZE ); - if(is_leaf(node)) - { + if (is_leaf(node)) { hint->stack[hint->size++] = (RayObject*)node; } - else - { + else { int childs = count_childs(node); - if(hint->size + reserve_space + childs <= RE_RAY_LCTS_MAX_SIZE) - { + if (hint->size + reserve_space + childs <= RE_RAY_LCTS_MAX_SIZE) { int result = hint_test_bb(hintObject, node->bb, node->bb+3); - if(result == HINT_RECURSE) - { + if (result == HINT_RECURSE) { /* We are 100% sure the ray will be pass inside this node */ bvh_dfs_make_hint_push_siblings(node->child, hint, reserve_space, hintObject); } - else if(result == HINT_ACCEPT) - { + else if (result == HINT_ACCEPT) { hint->stack[hint->size++] = (RayObject*)node; } } - else - { + else { hint->stack[hint->size++] = (RayObject*)node; } } diff --git a/source/blender/render/intern/raytrace/rayobject.cpp b/source/blender/render/intern/raytrace/rayobject.cpp index b2f85e8429d..a2773ba218d 100644 --- a/source/blender/render/intern/raytrace/rayobject.cpp +++ b/source/blender/render/intern/raytrace/rayobject.cpp @@ -60,13 +60,11 @@ MALWAYS_INLINE RayObject* rayface_from_coords(RayFace *rayface, void *ob, void * copy_v3_v3(rayface->v2, v2); copy_v3_v3(rayface->v3, v3); - if (v4) - { + if (v4) { copy_v3_v3(rayface->v4, v4); rayface->quad = 1; } - else - { + else { rayface->quad = 0; } @@ -77,8 +75,7 @@ MALWAYS_INLINE void rayface_from_vlak(RayFace *rayface, ObjectInstanceRen *obi, { rayface_from_coords(rayface, obi, vlr, vlr->v1->co, vlr->v2->co, vlr->v3->co, vlr->v4 ? vlr->v4->co : NULL); - if (obi->transform_primitives) - { + if (obi->transform_primitives) { mul_m4_v3(obi->mat, rayface->v1); mul_m4_v3(obi->mat, rayface->v2); mul_m4_v3(obi->mat, rayface->v3); @@ -295,13 +292,11 @@ MALWAYS_INLINE int intersect_rayface(RayObject *hit_obj, RayFace *face, Isect *i return 0; /* check if we should intersect this face */ - if (is->check == RE_CHECK_VLR_RENDER) - { + if (is->check == RE_CHECK_VLR_RENDER) { if (vlr_check_intersect(is, (ObjectInstanceRen*)face->ob, (VlakRen*)face->face) == 0) return 0; } - else if (is->check == RE_CHECK_VLR_NON_SOLID_MATERIAL) - { + else if (is->check == RE_CHECK_VLR_NON_SOLID_MATERIAL) { if (vlr_check_intersect(is, (ObjectInstanceRen*)face->ob, (VlakRen*)face->face) == 0) return 0; if (vlr_check_intersect_solid(is, (ObjectInstanceRen*)face->ob, (VlakRen*)face->face) == 0) @@ -322,27 +317,25 @@ MALWAYS_INLINE int intersect_rayface(RayObject *hit_obj, RayFace *face, Isect *i /* when a shadow ray leaves a face, it can be little outside the edges * of it, causing intersection to be detected in its neighbor face */ - if (is->skip & RE_SKIP_VLR_NEIGHBOUR) - { - if (dist < 0.1f && is->orig.ob == face->ob) - { + if (is->skip & RE_SKIP_VLR_NEIGHBOUR) { + if (dist < 0.1f && is->orig.ob == face->ob) { VlakRen * a = (VlakRen*)is->orig.face; VlakRen * b = (VlakRen*)face->face; /* so there's a shared edge or vertex, let's intersect ray with * face itself, if that's true we can safely return 1, otherwise * we assume the intersection is invalid, 0 */ - if (a->v1==b->v1 || a->v2==b->v1 || a->v3==b->v1 || a->v4==b->v1 - || a->v1==b->v2 || a->v2==b->v2 || a->v3==b->v2 || a->v4==b->v2 - || a->v1==b->v3 || a->v2==b->v3 || a->v3==b->v3 || a->v4==b->v3 - || (b->v4 && (a->v1==b->v4 || a->v2==b->v4 || a->v3==b->v4 || a->v4==b->v4))) { + if (a->v1==b->v1 || a->v2==b->v1 || a->v3==b->v1 || a->v4==b->v1 || + a->v1==b->v2 || a->v2==b->v2 || a->v3==b->v2 || a->v4==b->v2 || + a->v1==b->v3 || a->v2==b->v3 || a->v3==b->v3 || a->v4==b->v3 || + (b->v4 && (a->v1==b->v4 || a->v2==b->v4 || a->v3==b->v4 || a->v4==b->v4))) + { /* create RayFace from original face, transformed if necessary */ RayFace origface; ObjectInstanceRen *ob= (ObjectInstanceRen*)is->orig.ob; rayface_from_vlak(&origface, ob, (VlakRen*)is->orig.face); - if (!isec_tri_quad_neighbour(is->start, is->dir, &origface)) - { + if (!isec_tri_quad_neighbour(is->start, is->dir, &origface)) { return 0; } } @@ -375,8 +368,7 @@ int RE_rayobject_raycast(RayObject *r, Isect *isec) RE_RC_COUNT(isec->raycounter->raycast.test); /* setup vars used on raycast */ - for (i=0; i<3; i++) - { + for (i=0; i<3; i++) { isec->idot_axis[i] = 1.0f / isec->dir[i]; isec->bv_index[2*i] = isec->idot_axis[i] < 0.0 ? 1 : 0; @@ -388,12 +380,10 @@ int RE_rayobject_raycast(RayObject *r, Isect *isec) #ifdef RT_USE_LAST_HIT /* last hit heuristic */ - if (isec->mode==RE_RAY_SHADOW && isec->last_hit) - { + if (isec->mode==RE_RAY_SHADOW && isec->last_hit) { RE_RC_COUNT(isec->raycounter->rayshadow_last_hit.test); - if (RE_rayobject_intersect(isec->last_hit, isec)) - { + if (RE_rayobject_intersect(isec->last_hit, isec)) { RE_RC_COUNT(isec->raycounter->raycast.hit); RE_RC_COUNT(isec->raycounter->rayshadow_last_hit.hit); return 1; @@ -405,8 +395,7 @@ int RE_rayobject_raycast(RayObject *r, Isect *isec) isec->hit_hint = 0; #endif - if (RE_rayobject_intersect(r, isec)) - { + if (RE_rayobject_intersect(r, isec)) { RE_RC_COUNT(isec->raycounter->raycast.hit); #ifdef RT_USE_HINT @@ -420,12 +409,10 @@ int RE_rayobject_raycast(RayObject *r, Isect *isec) int RE_rayobject_intersect(RayObject *r, Isect *i) { - if (RE_rayobject_isRayFace(r)) - { + if (RE_rayobject_isRayFace(r)) { return intersect_rayface(r, (RayFace*) RE_rayobject_align(r), i); } - else if (RE_rayobject_isVlakPrimitive(r)) - { + else if (RE_rayobject_isVlakPrimitive(r)) { //TODO optimize (useless copy to RayFace to avoid duplicate code) VlakPrimitive *face = (VlakPrimitive*) RE_rayobject_align(r); RayFace nface; @@ -433,8 +420,7 @@ int RE_rayobject_intersect(RayObject *r, Isect *i) return intersect_rayface(r, &nface, i); } - else if (RE_rayobject_isRayAPI(r)) - { + else if (RE_rayobject_isRayAPI(r)) { r = RE_rayobject_align(r); return r->api->raycast(r, i); } @@ -466,12 +452,10 @@ void RE_rayobject_free(RayObject *r) float RE_rayobject_cost(RayObject *r) { - if (RE_rayobject_isRayFace(r) || RE_rayobject_isVlakPrimitive(r)) - { + if (RE_rayobject_isRayFace(r) || RE_rayobject_isVlakPrimitive(r)) { return 1.0f; } - else if (RE_rayobject_isRayAPI(r)) - { + else if (RE_rayobject_isRayAPI(r)) { r = RE_rayobject_align(r); return r->api->cost(r); } @@ -485,8 +469,7 @@ float RE_rayobject_cost(RayObject *r) void RE_rayobject_merge_bb(RayObject *r, float *min, float *max) { - if (RE_rayobject_isRayFace(r)) - { + if (RE_rayobject_isRayFace(r)) { RayFace *face = (RayFace*) RE_rayobject_align(r); DO_MINMAX(face->v1, min, max); @@ -494,8 +477,7 @@ void RE_rayobject_merge_bb(RayObject *r, float *min, float *max) DO_MINMAX(face->v3, min, max); if (RE_rayface_isQuad(face)) DO_MINMAX(face->v4, min, max); } - else if (RE_rayobject_isVlakPrimitive(r)) - { + else if (RE_rayobject_isVlakPrimitive(r)) { VlakPrimitive *face = (VlakPrimitive*) RE_rayobject_align(r); RayFace nface; rayface_from_vlak(&nface, face->ob, face->face); @@ -505,8 +487,7 @@ void RE_rayobject_merge_bb(RayObject *r, float *min, float *max) DO_MINMAX(nface.v3, min, max); if (RE_rayface_isQuad(&nface)) DO_MINMAX(nface.v4, min, max); } - else if (RE_rayobject_isRayAPI(r)) - { + else if (RE_rayobject_isRayAPI(r)) { r = RE_rayobject_align(r); r->api->bb(r, min, max); } @@ -518,12 +499,10 @@ void RE_rayobject_merge_bb(RayObject *r, float *min, float *max) void RE_rayobject_hint_bb(RayObject *r, RayHint *hint, float *min, float *max) { - if (RE_rayobject_isRayFace(r) || RE_rayobject_isVlakPrimitive(r)) - { + if (RE_rayobject_isRayFace(r) || RE_rayobject_isVlakPrimitive(r)) { return; } - else if (RE_rayobject_isRayAPI(r)) - { + else if (RE_rayobject_isRayAPI(r)) { r = RE_rayobject_align(r); return r->api->hint_bb(r, hint, min, max); } @@ -543,8 +522,7 @@ int RE_rayobjectcontrol_test_break(RayObjectControl *control) void RE_rayobject_set_control(RayObject *r, void *data, RE_rayobjectcontrol_test_break_callback test_break) { - if (RE_rayobject_isRayAPI(r)) - { + if (RE_rayobject_isRayAPI(r)) { r = RE_rayobject_align(r); r->control.data = data; r->control.test_break = test_break; diff --git a/source/blender/render/intern/raytrace/rayobject_blibvh.cpp b/source/blender/render/intern/raytrace/rayobject_blibvh.cpp index 165b62cfbe4..d0036fd8556 100644 --- a/source/blender/render/intern/raytrace/rayobject_blibvh.cpp +++ b/source/blender/render/intern/raytrace/rayobject_blibvh.cpp @@ -102,8 +102,7 @@ static void bvh_callback(void *userdata, int index, const BVHTreeRay *UNUSED(ray Isect *isec = data->isec; RayObject *face = data->leafs[index]; - if (RE_rayobject_intersect(face,isec)) - { + if (RE_rayobject_intersect(face, isec)) { hit->index = index; if (isec->mode == RE_RAY_SHADOW) diff --git a/source/blender/render/intern/raytrace/rayobject_hint.h b/source/blender/render/intern/raytrace/rayobject_hint.h index 3689aa8ac17..37d9edb035d 100644 --- a/source/blender/render/intern/raytrace/rayobject_hint.h +++ b/source/blender/render/intern/raytrace/rayobject_hint.h @@ -44,7 +44,7 @@ struct HintBB inline int hint_test_bb(HintBB *obj, float *Nmin, float *Nmax) { - if(bb_fits_inside( Nmin, Nmax, obj->bb, obj->bb+3 ) ) + if (bb_fits_inside( Nmin, Nmax, obj->bb, obj->bb+3 ) ) return HINT_RECURSE; else return HINT_ACCEPT; diff --git a/source/blender/render/intern/raytrace/rayobject_instance.cpp b/source/blender/render/intern/raytrace/rayobject_instance.cpp index 2e803ce0fd3..ce88bac1587 100644 --- a/source/blender/render/intern/raytrace/rayobject_instance.cpp +++ b/source/blender/render/intern/raytrace/rayobject_instance.cpp @@ -99,8 +99,7 @@ static int RE_rayobject_instance_intersect(RayObject *o, Isect *isec) int changed = 0, i, res; // TODO - this is disabling self intersection on instances - if (isec->orig.ob == obj->ob && obj->ob) - { + if (isec->orig.ob == obj->ob && obj->ob) { changed = 1; isec->orig.ob = obj->target_ob; } @@ -117,8 +116,7 @@ static int RE_rayobject_instance_intersect(RayObject *o, Isect *isec) isec->dist *= normalize_v3(isec->dir); // update idot_axis and bv_index - for (i=0; i<3; i++) - { + for (i=0; i<3; i++) { isec->idot_axis[i] = 1.0f / isec->dir[i]; isec->bv_index[2*i] = isec->idot_axis[i] < 0.0 ? 1 : 0; @@ -132,12 +130,10 @@ static int RE_rayobject_instance_intersect(RayObject *o, Isect *isec) res = RE_rayobject_intersect(obj->target, isec); // map dist into original coordinate space - if (res == 0) - { + if (res == 0) { isec->dist = dist; } - else - { + else { // note we don't just multiply dist, because of possible // non-uniform scaling in the transform matrix float vec[3]; @@ -165,8 +161,7 @@ static int RE_rayobject_instance_intersect(RayObject *o, Isect *isec) isec->orig.ob = obj->ob; // restore bv_index - for (i=0; i<3; i++) - { + for (i=0; i<3; i++) { isec->bv_index[2*i] = isec->idot_axis[i] < 0.0 ? 1 : 0; isec->bv_index[2*i+1] = 1 - isec->bv_index[2*i]; @@ -202,8 +197,7 @@ static void RE_rayobject_instance_bb(RayObject *o, float *min, float *max) RE_rayobject_merge_bb(obj->target, m, M); //There must be a faster way than rotating all the 8 vertexs of the BB - for (i=0; i<8; i++) - { + for (i=0; i<8; i++) { for (j=0; j<3; j++) t[j] = i&(1<<j) ? M[j] : m[j]; mul_m4_v3(obj->target2global, t); DO_MINMAX(t, min, max); diff --git a/source/blender/render/intern/raytrace/rayobject_octree.cpp b/source/blender/render/intern/raytrace/rayobject_octree.cpp index ea1d5c2573c..eef2fcc51c9 100644 --- a/source/blender/render/intern/raytrace/rayobject_octree.cpp +++ b/source/blender/render/intern/raytrace/rayobject_octree.cpp @@ -305,7 +305,7 @@ static void ocwrite(Octree *oc, RayFace *face, int quad, short x, short y, short no= (Node *)br->b[oc5]; if (no==NULL) br->b[oc5]= (Branch *)(no= addnode(oc)); - while(no->next) no= no->next; + while (no->next) no = no->next; a= 0; if (no->v[7]) { /* node full */ @@ -313,7 +313,7 @@ static void ocwrite(Octree *oc, RayFace *face, int quad, short x, short y, short no= no->next; } else { - while(no->v[a]!=NULL) a++; + while (no->v[a] != NULL) a++; } no->v[a]= (RayFace*) RE_rayobject_align(face); @@ -383,7 +383,7 @@ static void d2dda(Octree *oc, short b1, short b2, short c1, short c2, char *ocfa x=ocx1; y=ocy1; labda= MIN2(labdax, labday); - while(TRUE) { + while (TRUE) { if (x<0 || y<0 || x>=oc->ocres || y>=oc->ocres); else ocface[oc->ocres*x+y]= 1; @@ -421,7 +421,7 @@ static void filltriangle(Octree *oc, short c1, short c2, char *ocface, short *oc for (y=ocmin[c2];y<=ocmax[c2];y++) { if (ocface[a+y]) { y++; - while(ocface[a+y] && y!=ocmax[c2]) y++; + while (ocface[a+y] && y!=ocmax[c2]) y++; for (y1=ocmax[c2];y1>y;y1--) { if (ocface[a+y1]) { for (y2=y;y2<=y1;y2++) ocface[a+y2]=1; @@ -449,7 +449,7 @@ static void RE_rayobject_octree_free(RayObject *tree) if (oc->adrbranch) { int a= 0; - while(oc->adrbranch[a]) { + while (oc->adrbranch[a]) { MEM_freeN(oc->adrbranch[a]); oc->adrbranch[a]= NULL; a++; @@ -461,7 +461,7 @@ static void RE_rayobject_octree_free(RayObject *tree) if (oc->adrnode) { int a= 0; - while(oc->adrnode[a]) { + while (oc->adrnode[a]) { MEM_freeN(oc->adrnode[a]); oc->adrnode[a]= NULL; a++; @@ -658,8 +658,7 @@ static void RE_rayobject_octree_done(RayObject *tree) oc->ocsize= sqrt(t00*t00+t01*t01+t02*t02); /* global, max size octree */ - for (c=0; c<oc->ro_nodes_used; c++) - { + for (c=0; c<oc->ro_nodes_used; c++) { octree_fill_rayface(oc, oc->ro_nodes[c]); } @@ -683,42 +682,41 @@ static void RE_rayobject_octree_bb(RayObject *tree, float *min, float *max) /* check all faces in this node */ static int testnode(Octree *UNUSED(oc), Isect *is, Node *no, OcVal ocval) { - short nr=0; + short nr = 0; /* return on any first hit */ - if (is->mode==RE_RAY_SHADOW) { + if (is->mode == RE_RAY_SHADOW) { - for (; no; no = no->next) - for (nr=0; nr<8; nr++) - { - RayFace *face = no->v[nr]; - OcVal *ov = no->ov+nr; - - if (!face) break; - - if ( (ov->ocx & ocval.ocx) && (ov->ocy & ocval.ocy) && (ov->ocz & ocval.ocz) ) - { - if ( RE_rayobject_intersect( RE_rayobject_unalignRayFace(face),is) ) - return 1; + for ( ; no; no = no->next) { + for (nr = 0; nr < 8; nr++) { + RayFace *face = no->v[nr]; + OcVal *ov = no->ov + nr; + + if (!face) break; + + if ( (ov->ocx & ocval.ocx) && (ov->ocy & ocval.ocy) && (ov->ocz & ocval.ocz) ) { + if ( RE_rayobject_intersect( RE_rayobject_unalignRayFace(face),is) ) + return 1; + } } } } - else - { /* else mirror or glass or shadowtra, return closest face */ + else { + /* else mirror or glass or shadowtra, return closest face */ int found= 0; - for (; no; no = no->next) - for (nr=0; nr<8; nr++) - { - RayFace *face = no->v[nr]; - OcVal *ov = no->ov+nr; - - if (!face) break; - - if ( (ov->ocx & ocval.ocx) && (ov->ocy & ocval.ocy) && (ov->ocz & ocval.ocz) ) - { - if ( RE_rayobject_intersect( RE_rayobject_unalignRayFace(face),is) ) - found= 1; + for ( ; no; no = no->next) { + for (nr = 0; nr < 8; nr++) { + RayFace *face = no->v[nr]; + OcVal *ov = no->ov + nr; + + if (!face) break; + + if ( (ov->ocx & ocval.ocx) && (ov->ocy & ocval.ocy) && (ov->ocz & ocval.ocz) ) { + if ( RE_rayobject_intersect( RE_rayobject_unalignRayFace(face),is) ) { + found = 1; + } + } } } @@ -1003,7 +1001,7 @@ static int RE_rayobject_octree_intersect(RayObject *tree, Isect *is) /* this loop has been constructed to make sure the first and last node of ray * are always included, even when ddalabda==1.0f or larger */ - while(TRUE) { + while (TRUE) { no= ocread(oc, xo, yo, zo); if (no) { diff --git a/source/blender/render/intern/raytrace/rayobject_qbvh.cpp b/source/blender/render/intern/raytrace/rayobject_qbvh.cpp index 2edf1593e99..2d0abba9a75 100644 --- a/source/blender/render/intern/raytrace/rayobject_qbvh.cpp +++ b/source/blender/render/intern/raytrace/rayobject_qbvh.cpp @@ -71,8 +71,7 @@ void bvh_done<QBVHTree>(QBVHTree *obj) //TODO do this in 1 pass (half memory usage during building) VBVHNode *root = BuildBinaryVBVH<VBVHNode>(arena1, &obj->rayobj.control).transform(obj->builder); - if (RE_rayobjectcontrol_test_break(&obj->rayobj.control)) - { + if (RE_rayobjectcontrol_test_break(&obj->rayobj.control)) { BLI_memarena_free(arena1); BLI_memarena_free(arena2); return; diff --git a/source/blender/render/intern/raytrace/rayobject_rtbuild.cpp b/source/blender/render/intern/raytrace/rayobject_rtbuild.cpp index 54901db8bdd..ad74159fd3b 100644 --- a/source/blender/render/intern/raytrace/rayobject_rtbuild.cpp +++ b/source/blender/render/intern/raytrace/rayobject_rtbuild.cpp @@ -74,8 +74,7 @@ RTBuilder* rtbuild_create(int size) builder->primitives.begin = builder->primitives.end = memblock; builder->primitives.maxsize = size; - for (int i=0; i<3; i++) - { + for (int i=0; i<3; i++) { builder->sorted_begin[i] = (RTBuilder::Object**)MEM_mallocN( sizeof(RTBuilder::Object*)*size,"RTBuilder.sorted_objects"); builder->sorted_end[i] = builder->sorted_begin[i]; } @@ -124,8 +123,7 @@ void rtbuild_add(RTBuilder *b, RayObject *o) b->primitives.end->obj = o; b->primitives.end->cost = RE_rayobject_cost(o); - for (int i=0; i<3; i++) - { + for (int i=0; i<3; i++) { *(b->sorted_end[i]) = b->primitives.end; b->sorted_end[i]++; } @@ -158,8 +156,7 @@ static void object_sort(Item *begin, Item *end, int axis) void rtbuild_done(RTBuilder *b, RayObjectControl* ctrl) { for (int i=0; i<3; i++) - if (b->sorted_begin[i]) - { + if (b->sorted_begin[i]) { if (RE_rayobjectcontrol_test_break(ctrl)) break; object_sort( b->sorted_begin[i], b->sorted_end[i], i ); } @@ -175,13 +172,11 @@ RTBuilder* rtbuild_get_child(RTBuilder *b, int child, RTBuilder *tmp) rtbuild_init( tmp ); for (int i=0; i<3; i++) - if (b->sorted_begin[i]) - { + if (b->sorted_begin[i]) { tmp->sorted_begin[i] = b->sorted_begin[i] + b->child_offset[child ]; tmp->sorted_end [i] = b->sorted_begin[i] + b->child_offset[child+1]; } - else - { + else { tmp->sorted_begin[i] = 0; tmp->sorted_end [i] = 0; } @@ -191,8 +186,7 @@ RTBuilder* rtbuild_get_child(RTBuilder *b, int child, RTBuilder *tmp) void rtbuild_calc_bb(RTBuilder *b) { - if (b->bb[0] == 1.0e30f) - { + if (b->bb[0] == 1.0e30f) { for (RTBuilder::Object **index = b->sorted_begin[0]; index != b->sorted_end[0]; index++) RE_rayobject_merge_bb( (*index)->obj , b->bb, b->bb+3); } @@ -337,30 +331,25 @@ int rtbuild_heuristic_object_split(RTBuilder *b, int nchilds) assert(size > 1); int baxis = -1, boffset = 0; - if (size > nchilds) - { + if (size > nchilds) { float bcost = FLT_MAX; baxis = -1, boffset = size/2; SweepCost *sweep = (SweepCost*)MEM_mallocN( sizeof(SweepCost)*size, "RTBuilder.HeuristicSweep" ); - for (int axis=0; axis<3; axis++) - { + for (int axis=0; axis<3; axis++) { SweepCost sweep_left; RTBuilder::Object **obj = b->sorted_begin[axis]; // float right_cost = 0; - for (int i=size-1; i>=0; i--) - { - if (i == size-1) - { + for (int i=size-1; i>=0; i--) { + if (i == size-1) { copy_v3_v3(sweep[i].bb, obj[i]->bb); copy_v3_v3(sweep[i].bb+3, obj[i]->bb+3); sweep[i].cost = obj[i]->cost; } - else - { + else { sweep[i].bb[0] = MIN2(obj[i]->bb[0], sweep[i+1].bb[0]); sweep[i].bb[1] = MIN2(obj[i]->bb[1], sweep[i+1].bb[1]); sweep[i].bb[2] = MIN2(obj[i]->bb[2], sweep[i+1].bb[2]); @@ -382,8 +371,7 @@ int rtbuild_heuristic_object_split(RTBuilder *b, int nchilds) // right_cost -= obj[0]->cost; if (right_cost < 0) right_cost = 0; - for (int i=1; i<size; i++) - { + for (int i=1; i<size; i++) { //Worst case heuristic (cost of each child is linear) float hcost, left_side, right_side; @@ -401,9 +389,8 @@ int rtbuild_heuristic_object_split(RTBuilder *b, int nchilds) if (left_side > bcost) break; //No way we can find a better heuristic in this axis assert(hcost >= 0); - if ( hcost < bcost - || (hcost == bcost && axis < baxis)) //this makes sure the tree built is the same whatever is the order of the sorting axis - { + // this makes sure the tree built is the same whatever is the order of the sorting axis + if ( hcost < bcost || (hcost == bcost && axis < baxis)) { bcost = hcost; baxis = axis; boffset = i; @@ -423,13 +410,11 @@ int rtbuild_heuristic_object_split(RTBuilder *b, int nchilds) MEM_freeN(sweep); } - else if (size == 2) - { + else if (size == 2) { baxis = 0; boffset = 1; } - else if (size == 1) - { + else if (size == 1) { b->child_offset[0] = 0; b->child_offset[1] = 1; return 1; @@ -500,15 +485,13 @@ int bb_largest_axis(float *min, float *max) sub[0] = max[0]-min[0]; sub[1] = max[1]-min[1]; sub[2] = max[2]-min[2]; - if (sub[0] > sub[1]) - { + if (sub[0] > sub[1]) { if (sub[0] > sub[2]) return 0; else return 2; } - else - { + else { if (sub[1] > sub[2]) return 1; else diff --git a/source/blender/render/intern/raytrace/rayobject_svbvh.cpp b/source/blender/render/intern/raytrace/rayobject_svbvh.cpp index 4c2099eb1e3..cbec02ab798 100644 --- a/source/blender/render/intern/raytrace/rayobject_svbvh.cpp +++ b/source/blender/render/intern/raytrace/rayobject_svbvh.cpp @@ -79,12 +79,10 @@ void bvh_done<SVBVHTree>(SVBVHTree *obj) BLI_memarena_use_align(arena2, 16); //Build and optimize the tree - if (0) - { + if (0) { VBVHNode *root = BuildBinaryVBVH<VBVHNode>(arena1, &obj->rayobj.control).transform(obj->builder); - if (RE_rayobjectcontrol_test_break(&obj->rayobj.control)) - { + if (RE_rayobjectcontrol_test_break(&obj->rayobj.control)) { BLI_memarena_free(arena1); BLI_memarena_free(arena2); return; @@ -100,14 +98,12 @@ void bvh_done<SVBVHTree>(SVBVHTree *obj) obj->root = Reorganize_SVBVH<VBVHNode>(arena2).transform(root); } - else - { + else { //Finds the optimal packing of this tree using a given cost model //TODO this uses quite a lot of memory, find ways to reduce memory usage during building OVBVHNode *root = BuildBinaryVBVH<OVBVHNode>(arena1,&obj->rayobj.control).transform(obj->builder); - if (RE_rayobjectcontrol_test_break(&obj->rayobj.control)) - { + if (RE_rayobjectcontrol_test_break(&obj->rayobj.control)) { BLI_memarena_free(arena1); BLI_memarena_free(arena2); return; diff --git a/source/blender/render/intern/raytrace/rayobject_vbvh.cpp b/source/blender/render/intern/raytrace/rayobject_vbvh.cpp index e82623f1da9..26a99794362 100644 --- a/source/blender/render/intern/raytrace/rayobject_vbvh.cpp +++ b/source/blender/render/intern/raytrace/rayobject_vbvh.cpp @@ -87,11 +87,9 @@ void bvh_done<VBVHTree>(VBVHTree *obj) BLI_memarena_use_malloc(arena1); //Build and optimize the tree - if (1) - { + if (1) { VBVHNode *root = BuildBinaryVBVH<VBVHNode>(arena1,&obj->rayobj.control).transform(obj->builder); - if (RE_rayobjectcontrol_test_break(&obj->rayobj.control)) - { + if (RE_rayobjectcontrol_test_break(&obj->rayobj.control)) { BLI_memarena_free(arena1); return; } @@ -108,8 +106,7 @@ void bvh_done<VBVHTree>(VBVHTree *obj) else obj->root = NULL; } - else - { + else { /* TODO MemArena *arena2 = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "vbvh arena2"); @@ -159,8 +156,7 @@ void bvh_hint_bb(Tree *tree, LCTSHint *hint, float *UNUSED(min), float *UNUSED(m void bfree(VBVHTree *tree) { - if (tot_pushup + tot_pushdown + tot_hints + tot_moves) - { + if (tot_pushup + tot_pushdown + tot_hints + tot_moves) { if (G.debug & G_DEBUG) { printf("tot pushups: %d\n", tot_pushup); printf("tot pushdowns: %d\n", tot_pushdown); diff --git a/source/blender/render/intern/raytrace/reorganize.h b/source/blender/render/intern/raytrace/reorganize.h index 68b2b22ecdd..1930e5bb32b 100644 --- a/source/blender/render/intern/raytrace/reorganize.h +++ b/source/blender/render/intern/raytrace/reorganize.h @@ -66,17 +66,15 @@ void reorganize_find_fittest_parent(Node *tree, Node *node, std::pair<float,Node std::queue<Node*> q; q.push(tree); - while(!q.empty()) - { + while (!q.empty()) { Node *parent = q.front(); q.pop(); - if(parent == node) continue; - if(node_fits_inside(node, parent) && RE_rayobject_isAligned(parent->child) ) - { + if (parent == node) continue; + if (node_fits_inside(node, parent) && RE_rayobject_isAligned(parent->child) ) { float pcost = bb_area(parent->bb, parent->bb+3); cost = std::min( cost, std::make_pair(pcost,parent) ); - for(Node *child = parent->child; child; child = child->sibling) + for (Node *child = parent->child; child; child = child->sibling) q.push(child); } } @@ -89,28 +87,23 @@ void reorganize(Node *root) std::queue<Node*> q; q.push(root); - while(!q.empty()) - { + while (!q.empty()) { Node * node = q.front(); q.pop(); - if( RE_rayobject_isAligned(node->child) ) - { - for(Node **prev = &node->child; *prev; ) - { + if (RE_rayobject_isAligned(node->child)) { + for (Node **prev = &node->child; *prev; ) { assert( RE_rayobject_isAligned(*prev) ); q.push(*prev); std::pair<float,Node*> best(FLT_MAX, root); reorganize_find_fittest_parent( root, *prev, best ); - if(best.second == node) - { + if (best.second == node) { //Already inside the fitnest BB prev = &(*prev)->sibling; } - else - { + else { Node *tmp = *prev; *prev = (*prev)->sibling; @@ -123,8 +116,7 @@ void reorganize(Node *root) } } - if(node != root) - { + if (node != root) { } } } @@ -137,29 +129,24 @@ void reorganize(Node *root) template<class Node> void remove_useless(Node *node, Node **new_node) { - if( RE_rayobject_isAligned(node->child) ) - { + if ( RE_rayobject_isAligned(node->child) ) { - for(Node **prev = &node->child; *prev; ) - { + for (Node **prev = &node->child; *prev; ) { Node *next = (*prev)->sibling; remove_useless(*prev, prev); - if(*prev == NULL) + if (*prev == NULL) *prev = next; - else - { + else { (*prev)->sibling = next; prev = &((*prev)->sibling); } } } - if(node->child) - { - if(RE_rayobject_isAligned(node->child) && node->child->sibling == 0) + if (node->child) { + if (RE_rayobject_isAligned(node->child) && node->child->sibling == 0) *new_node = node->child; } - else if(node->child == NULL) - { + else if (node->child == NULL) { *new_node = NULL; } } @@ -171,18 +158,16 @@ void remove_useless(Node *node, Node **new_node) template<class Node> void pushup(Node *parent) { - if(is_leaf(parent)) return; + if (is_leaf(parent)) return; float p_area = bb_area(parent->bb, parent->bb+3); Node **prev = &parent->child; - for(Node *child = parent->child; RE_rayobject_isAligned(child) && child; ) - { + for (Node *child = parent->child; RE_rayobject_isAligned(child) && child; ) { const float c_area = bb_area(child->bb, child->bb + 3); const int nchilds = count_childs(child); float original_cost = ((p_area != 0.0f)? (c_area / p_area)*nchilds: 1.0f) + 1; float flatten_cost = nchilds; - if(flatten_cost < original_cost && nchilds >= 2) - { + if (flatten_cost < original_cost && nchilds >= 2) { append_sibling(child, child->child); child = child->sibling; *prev = child; @@ -192,15 +177,14 @@ void pushup(Node *parent) // child = *prev; tot_pushup++; } - else - { + else { *prev = child; prev = &(*prev)->sibling; child = *prev; } } - for(Node *child = parent->child; RE_rayobject_isAligned(child) && child; child = child->sibling) + for (Node *child = parent->child; RE_rayobject_isAligned(child) && child; child = child->sibling) pushup(child); } @@ -210,30 +194,27 @@ void pushup(Node *parent) template<class Node, int SSize> void pushup_simd(Node *parent) { - if(is_leaf(parent)) return; + if (is_leaf(parent)) return; int n = count_childs(parent); Node **prev = &parent->child; - for(Node *child = parent->child; RE_rayobject_isAligned(child) && child; ) - { + for (Node *child = parent->child; RE_rayobject_isAligned(child) && child; ) { int cn = count_childs(child); - if(cn-1 <= (SSize - (n%SSize) ) % SSize && RE_rayobject_isAligned(child->child) ) - { + if (cn-1 <= (SSize - (n%SSize) ) % SSize && RE_rayobject_isAligned(child->child) ) { n += (cn - 1); append_sibling(child, child->child); child = child->sibling; *prev = child; } - else - { + else { *prev = child; prev = &(*prev)->sibling; child = *prev; } } - for(Node *child = parent->child; RE_rayobject_isAligned(child) && child; child = child->sibling) + for (Node *child = parent->child; RE_rayobject_isAligned(child) && child; child = child->sibling) pushup_simd<Node,SSize>(child); } @@ -248,19 +229,17 @@ void pushdown(Node *parent) Node **s_child = &parent->child; Node * child = parent->child; - while(child && RE_rayobject_isAligned(child)) - { + while (child && RE_rayobject_isAligned(child)) { Node *next = child->sibling; Node **next_s_child = &child->sibling; //assert(bb_fits_inside(parent->bb, parent->bb+3, child->bb, child->bb+3)); - for(Node *i = parent->child; RE_rayobject_isAligned(i) && i; i = i->sibling) - if(child != i && bb_fits_inside(i->bb, i->bb+3, child->bb, child->bb+3) && RE_rayobject_isAligned(i->child)) - { + for (Node *i = parent->child; RE_rayobject_isAligned(i) && i; i = i->sibling) + if (child != i && bb_fits_inside(i->bb, i->bb+3, child->bb, child->bb+3) && RE_rayobject_isAligned(i->child)) { // todo optimize (should the one with the smallest area?) // float ia = bb_area(i->bb, i->bb+3) -// if(child->i) +// if (child->i) *s_child = child->sibling; child->sibling = i->child; i->child = child; @@ -286,18 +265,17 @@ void pushdown(Node *parent) template<class Node> float bvh_refit(Node *node) { - if(is_leaf(node)) return 0; - if(is_leaf(node->child)) return 0; + if (is_leaf(node)) return 0; + if (is_leaf(node->child)) return 0; float total = 0; - for(Node *child = node->child; child; child = child->sibling) + for (Node *child = node->child; child; child = child->sibling) total += bvh_refit(child); float old_area = bb_area(node->bb, node->bb+3); INIT_MINMAX(node->bb, node->bb+3); - for(Node *child = node->child; child; child = child->sibling) - { + for (Node *child = node->child; child; child = child->sibling) { DO_MIN(child->bb, node->bb); DO_MAX(child->bb+3, node->bb+3); } @@ -347,32 +325,27 @@ struct OVBVHNode int best_cutsize; void set_cut(int cutsize, OVBVHNode ***cut) { - if(cutsize == 1) - { + if (cutsize == 1) { **cut = this; *cut = &(**cut)->sibling; } - else - { - if(cutsize > MAX_CUT_SIZE) - { - for(OVBVHNode *child = this->child; child && RE_rayobject_isAligned(child); child = child->sibling) - { + else { + if (cutsize > MAX_CUT_SIZE) { + for (OVBVHNode *child = this->child; child && RE_rayobject_isAligned(child); child = child->sibling) { child->set_cut( 1, cut ); cutsize--; } assert(cutsize == 0); } else - for(OVBVHNode *child = this->child; child && RE_rayobject_isAligned(child); child = child->sibling) + for (OVBVHNode *child = this->child; child && RE_rayobject_isAligned(child); child = child->sibling) child->set_cut( child->get_cut_size( cutsize ), cut ); } } void optimize() { - if(RE_rayobject_isAligned(this->child)) - { + if (RE_rayobject_isAligned(this->child)) { //Calc new childs { OVBVHNode **cut = &(this->child); @@ -381,7 +354,7 @@ struct OVBVHNode } //Optimize new childs - for(OVBVHNode *child = this->child; child && RE_rayobject_isAligned(child); child = child->sibling) + for (OVBVHNode *child = this->child; child && RE_rayobject_isAligned(child); child = child->sibling) child->optimize(); } } @@ -415,8 +388,7 @@ struct VBVH_optimalPackSIMD //Fetch childs and needed data { float parent_area = bb_area(node->bb, node->bb+3); - for(Node *child = node->child; child && RE_rayobject_isAligned(child); child = child->sibling) - { + for (Node *child = node->child; child && RE_rayobject_isAligned(child); child = child->sibling) { this->child[nchilds] = child; this->child_hit_prob[nchilds] = (parent_area != 0.0f)? bb_area(child->bb, child->bb+3) / parent_area: 1.0f; nchilds++; @@ -427,36 +399,35 @@ struct VBVH_optimalPackSIMD //Build DP table to find minimum cost to represent this node with a given cutsize - int bt [MAX_OPTIMIZE_CHILDS+1][MAX_CUT_SIZE+1]; //backtrace table - float cost[MAX_OPTIMIZE_CHILDS+1][MAX_CUT_SIZE+1]; //cost table (can be reduced to float[2][MAX_CUT_COST]) + int bt [MAX_OPTIMIZE_CHILDS + 1][MAX_CUT_SIZE + 1]; //backtrace table + float cost[MAX_OPTIMIZE_CHILDS + 1][MAX_CUT_SIZE + 1]; //cost table (can be reduced to float[2][MAX_CUT_COST]) - for(int i=0; i<=nchilds; i++) - for(int j=0; j<=MAX_CUT_SIZE; j++) - cost[i][j] = INFINITY; + for (int i = 0; i <= nchilds; i++) { + for (int j = 0; j <= MAX_CUT_SIZE; j++) { + cost[i][j] = INFINITY; + } + } cost[0][0] = 0; - for(int i=1; i<=nchilds; i++) - for(int size=i-1; size/*+(nchilds-i)*/<=MAX_CUT_SIZE; size++) - for(int cut=1; cut+size/*+(nchilds-i)*/<=MAX_CUT_SIZE; cut++) - { - float new_cost = cost[i-1][size] + child_hit_prob[i-1]*child[i-1]->get_cost(cut); - if(new_cost < cost[i][size+cut]) - { - cost[i][size+cut] = new_cost; - bt[i][size+cut] = cut; + for (int i = 1; i<=nchilds; i++) { + for (int size = i - 1; size/*+(nchilds-i)*/<=MAX_CUT_SIZE; size++) { + for (int cut = 1; cut+size/*+(nchilds-i)*/<=MAX_CUT_SIZE; cut++) { + float new_cost = cost[i - 1][size] + child_hit_prob[i - 1] * child[i - 1]->get_cost(cut); + if (new_cost < cost[i][size+cut]) { + cost[i][size+cut] = new_cost; + bt[i][size+cut] = cut; + } + } } } //Save the ways to archieve the minimum cost with a given cutsize - for(int i = nchilds; i <= MAX_CUT_SIZE; i++) - { + for (int i = nchilds; i <= MAX_CUT_SIZE; i++) { node->cut_cost[i-1] = cost[nchilds][i]; - if(cost[nchilds][i] < INFINITY) - { + if (cost[nchilds][i] < INFINITY) { int current_size = i; - for(int j=nchilds; j>0; j--) - { + for (int j=nchilds; j>0; j--) { child[j-1]->cut_size[i-1] = bt[j][current_size]; current_size -= bt[j][current_size]; } @@ -468,26 +439,22 @@ struct VBVH_optimalPackSIMD void calc_costs(Node *node) { - if( RE_rayobject_isAligned(node->child) ) - { + if ( RE_rayobject_isAligned(node->child) ) { int nchilds = 0; - for(Node *child = node->child; child && RE_rayobject_isAligned(child); child = child->sibling) - { + for (Node *child = node->child; child && RE_rayobject_isAligned(child); child = child->sibling) { calc_costs(child); nchilds++; } - for(int i=0; i<MAX_CUT_SIZE; i++) + for (int i=0; i<MAX_CUT_SIZE; i++) node->cut_cost[i] = INFINITY; //We are not allowed to look on nodes with with so many childs - if(nchilds > MAX_CUT_SIZE) - { + if (nchilds > MAX_CUT_SIZE) { float cost = 0; float parent_area = bb_area(node->bb, node->bb+3); - for(Node *child = node->child; child && RE_rayobject_isAligned(child); child = child->sibling) - { + for (Node *child = node->child; child && RE_rayobject_isAligned(child); child = child->sibling) { cost += ((parent_area != 0.0f)? ( bb_area(child->bb, child->bb+3) / parent_area ): 1.0f) * child->get_cost(1); } @@ -495,16 +462,13 @@ struct VBVH_optimalPackSIMD node->cut_cost[0] = cost; node->best_cutsize = nchilds; } - else - { + else { calc_best calc(node); //calc expected cost if we optimaly pack this node - for(int cutsize=nchilds; cutsize<=MAX_CUT_SIZE; cutsize++) - { + for (int cutsize=nchilds; cutsize<=MAX_CUT_SIZE; cutsize++) { float m = node->get_cost(cutsize) + testcost(cutsize); - if(m < node->cut_cost[0]) - { + if (m < node->cut_cost[0]) { node->cut_cost[0] = m; node->best_cutsize = cutsize; } @@ -512,24 +476,22 @@ struct VBVH_optimalPackSIMD } assert(node->cut_cost[0] != INFINITY); } - else - { + else { node->cut_cost[0] = 1.0f; - for(int i=1; i<MAX_CUT_SIZE; i++) + for (int i = 1; i < MAX_CUT_SIZE; i++) node->cut_cost[i] = INFINITY; } } Node *transform(Node *node) { - if(RE_rayobject_isAligned(node->child)) - { + if (RE_rayobject_isAligned(node->child)) { static int num = 0; bool first = false; - if(num == 0) { num++; first = true; } + if (num == 0) { num++; first = true; } calc_costs(node); - if((G.debug & G_DEBUG) && first) printf("expected cost = %f (%d)\n", node->cut_cost[0], node->best_cutsize ); + if ((G.debug & G_DEBUG) && first) printf("expected cost = %f (%d)\n", node->cut_cost[0], node->best_cutsize ); node->optimize(); } return node; diff --git a/source/blender/render/intern/raytrace/svbvh.h b/source/blender/render/intern/raytrace/svbvh.h index e0e96781f36..a4044db8208 100644 --- a/source/blender/render/intern/raytrace/svbvh.h +++ b/source/blender/render/intern/raytrace/svbvh.h @@ -94,9 +94,9 @@ static int svbvh_bb_intersect_test(const Isect *isec, const float *_bb) RE_RC_COUNT(isec->raycounter->bb.test); - if(t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return 0; - if(t2x < 0.0 || t2y < 0.0 || t2z < 0.0) return 0; - if(t1x > isec->dist || t1y > isec->dist || t1z > isec->dist) return 0; + if (t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return 0; + if (t2x < 0.0 || t2y < 0.0 || t2z < 0.0) return 0; + if (t1x > isec->dist || t1y > isec->dist || t1z > isec->dist) return 0; RE_RC_COUNT(isec->raycounter->bb.hit); @@ -116,40 +116,39 @@ static int svbvh_node_stack_raycast(SVBVHNode *root, Isect *isec) stack[stack_pos++] = root; - while(stack_pos) - { + while (stack_pos) { node = stack[--stack_pos]; - if(!svbvh_node_is_leaf(node)) - { + if (!svbvh_node_is_leaf(node)) { int nchilds= node->nchilds; - if(nchilds == 4) { + if (nchilds == 4) { float *child_bb= node->child_bb; int res = svbvh_bb_intersect_test_simd4(isec, ((__m128*) (child_bb))); SVBVHNode **child= node->child; RE_RC_COUNT(isec->raycounter->simd_bb.test); - if(res & 1) { stack[stack_pos++] = child[0]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); } - if(res & 2) { stack[stack_pos++] = child[1]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); } - if(res & 4) { stack[stack_pos++] = child[2]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); } - if(res & 8) { stack[stack_pos++] = child[3]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); } + if (res & 1) { stack[stack_pos++] = child[0]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); } + if (res & 2) { stack[stack_pos++] = child[1]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); } + if (res & 4) { stack[stack_pos++] = child[2]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); } + if (res & 8) { stack[stack_pos++] = child[3]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); } } else { float *child_bb= node->child_bb; SVBVHNode **child= node->child; int i; - for(i=0; i<nchilds; i++) - if(svbvh_bb_intersect_test(isec, (float*)child_bb+6*i)) + for (i = 0; i < nchilds; i++) { + if (svbvh_bb_intersect_test(isec, (float *)child_bb + 6 * i)) { stack[stack_pos++] = child[i]; + } + } } } - else - { + else { hit |= RE_rayobject_intersect((RayObject*)node, isec); - if(SHADOW && hit) break; + if (SHADOW && hit) break; } } @@ -160,25 +159,20 @@ static int svbvh_node_stack_raycast(SVBVHNode *root, Isect *isec) template<> inline void bvh_node_merge_bb<SVBVHNode>(SVBVHNode *node, float *min, float *max) { - if(is_leaf(node)) - { + if (is_leaf(node)) { RE_rayobject_merge_bb((RayObject*)node, min, max); } - else - { + else { int i=0; - while(i+4 <= node->nchilds) - { + while (i+4 <= node->nchilds) { float *res = node->child_bb + 6*i; - for(int j=0; j<3; j++) - { + for (int j = 0; j < 3; j++) { min[j] = MIN2(min[j], res[4*j+0]); min[j] = MIN2(min[j], res[4*j+1]); min[j] = MIN2(min[j], res[4*j+2]); min[j] = MIN2(min[j], res[4*j+3]); } - for(int j=0; j<3; j++) - { + for (int j = 0; j < 3; j++) { max[j] = MAX2(max[j], res[4*(j+3)+0]); max[j] = MAX2(max[j], res[4*(j+3)+1]); max[j] = MAX2(max[j], res[4*(j+3)+2]); @@ -188,10 +182,9 @@ inline void bvh_node_merge_bb<SVBVHNode>(SVBVHNode *node, float *min, float *max i += 4; } - for(; i<node->nchilds; i++) - { - DO_MIN(node->child_bb+6*i , min); - DO_MAX(node->child_bb+3+6*i, max); + for ( ; i < node->nchilds; i++) { + DO_MIN(node->child_bb + 6 * i, min); + DO_MAX(node->child_bb + 3 + 6 * i, max); } } } @@ -218,17 +211,19 @@ struct Reorganize_SVBVH childs_per_node = 0; useless_bb = 0; - for(int i=0; i<16; i++) + for (int i = 0; i < 16; i++) { nodes_with_childs[i] = 0; + } } ~Reorganize_SVBVH() { - if(G.debug & G_DEBUG) { + if (G.debug & G_DEBUG) { printf("%f childs per node\n", childs_per_node / nodes); printf("%d childs BB are useless\n", useless_bb); - for(int i=0; i<16; i++) + for (int i = 0; i < 16; i++) { printf("%i childs per node: %d/%d = %f\n", i, nodes_with_childs[i], nodes, nodes_with_childs[i]/float(nodes)); + } } } @@ -248,14 +243,12 @@ struct Reorganize_SVBVH void prepare_for_simd(SVBVHNode *node) { int i=0; - while(i+4 <= node->nchilds) - { + while (i + 4 <= node->nchilds) { float vec_tmp[4*6]; float *res = node->child_bb+6*i; std::copy(res, res+6*4, vec_tmp); - for(int j=0; j<6; j++) - { + for (int j=0; j<6; j++) { res[4*j+0] = vec_tmp[6*0+j]; res[4*j+1] = vec_tmp[6*1+j]; res[4*j+2] = vec_tmp[6*2+j]; @@ -274,26 +267,25 @@ struct Reorganize_SVBVH SVBVHNode *transform(OldNode *old) { - if(is_leaf(old)) + if (is_leaf(old)) return (SVBVHNode*)old; - if(is_leaf(old->child)) + if (is_leaf(old->child)) return (SVBVHNode*)old->child; int nchilds = count_childs(old); int alloc_childs = nchilds; - if(nchilds % 4 > 2) + if (nchilds % 4 > 2) alloc_childs = padup(nchilds, 4); SVBVHNode *node = create_node(alloc_childs); childs_per_node += nchilds; nodes++; - if(nchilds < 16) + if (nchilds < 16) nodes_with_childs[nchilds]++; useless_bb += alloc_childs-nchilds; - while(alloc_childs > nchilds) - { + while (alloc_childs > nchilds) { const static float def_bb[6] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MIN, FLT_MIN, FLT_MIN }; alloc_childs--; node->child[alloc_childs] = NULL; @@ -301,20 +293,17 @@ struct Reorganize_SVBVH } int i=nchilds; - for(OldNode *o_child = old->child; o_child; o_child = o_child->sibling) - { + for (OldNode *o_child = old->child; o_child; o_child = o_child->sibling) { i--; node->child[i] = transform(o_child); - if(is_leaf(o_child)) - { + if (is_leaf(o_child)) { float bb[6]; INIT_MINMAX(bb, bb+3); RE_rayobject_merge_bb((RayObject*)o_child, bb, bb+3); copy_bb(node->child_bb+i*6, bb); break; } - else - { + else { copy_bb(node->child_bb+i*6, o_child->bb); } } diff --git a/source/blender/render/intern/raytrace/vbvh.h b/source/blender/render/intern/raytrace/vbvh.h index 1c84cd23510..f916dd412f7 100644 --- a/source/blender/render/intern/raytrace/vbvh.h +++ b/source/blender/render/intern/raytrace/vbvh.h @@ -57,17 +57,14 @@ inline static void bvh_node_push_childs(Node *node, Isect *UNUSED(isec), Node ** { Node *child = node->child; - if(is_leaf(child)) - { + if (is_leaf(child)) { stack[stack_pos++] = child; } - else - { - while(child) - { + else { + while (child) { /* Skips BB tests on primitives */ #if 0 - if(is_leaf(child->child)) { + if (is_leaf(child->child)) { stack[stack_pos++] = child->child; } else @@ -86,10 +83,9 @@ template<class Node> int count_childs(Node *parent) { int n = 0; - for(Node *i = parent->child; i; i = i->sibling) - { + for (Node *i = parent->child; i; i = i->sibling) { n++; - if(is_leaf(i)) + if (is_leaf(i)) break; } @@ -100,7 +96,7 @@ int count_childs(Node *parent) template<class Node> void append_sibling(Node *node, Node *sibling) { - while(node->sibling) + while (node->sibling) node = node->sibling; node->sibling = sibling; @@ -118,7 +114,7 @@ struct BuildBinaryVBVH void test_break() { - if(RE_rayobjectcontrol_test_break(control)) + if (RE_rayobjectcontrol_test_break(control)) throw "Stop"; } @@ -160,19 +156,17 @@ struct BuildBinaryVBVH { int size = rtbuild_size(builder); - if(size == 0) { + if (size == 0) { return NULL; } - else if(size == 1) - { + else if (size == 1) { Node *node = create_node(); INIT_MINMAX(node->bb, node->bb+3); rtbuild_merge_bb(builder, node->bb, node->bb+3); node->child = (Node *) rtbuild_get_primitive(builder, 0); return node; } - else - { + else { test_break(); Node *node = create_node(); @@ -183,8 +177,7 @@ struct BuildBinaryVBVH INIT_MINMAX(node->bb, node->bb+3); assert(nc == 2); - for(int i=0; i<nc; i++) - { + for (int i = 0; i < nc; i++) { RTBuilder tmp; rtbuild_get_child(builder, i, &tmp); @@ -224,7 +217,7 @@ struct Reorganize_VBVH VBVHNode *transform(OldNode *old) { - if(is_leaf(old)) + if (is_leaf(old)) return (VBVHNode*)old; VBVHNode *node = create_node(); @@ -237,7 +230,7 @@ struct Reorganize_VBVH { VBVHNode *n_child = transform(o_child); *child_ptr = n_child; - if(is_leaf(n_child)) return node; + if (is_leaf(n_child)) return node; child_ptr = &n_child->sibling; } *child_ptr = 0; |