Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rayobject_vbvh.cpp « raytrace « intern « render « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0190b971d84765eba333edcc5cebaebc5480d1ae (plain)
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
/**
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2009 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): André Pinto.
 *
 * ***** END GPL LICENSE BLOCK *****
 */
int tot_pushup   = 0;
int tot_pushdown = 0;
int tot_hints    = 0;


#include <assert.h>
#include "rayobject.h"
#include "rayobject_rtbuild.h"
#include "RE_raytrace.h"
#include "BLI_memarena.h"
#include "MEM_guardedalloc.h"
#include "BKE_global.h"
#include "BKE_utildefines.h"
#include "BLI_math.h"

#include "reorganize.h"
#include "bvh.h"
#include "vbvh.h"
#include "svbvh.h"
#include <queue>
#include <algorithm>

#define DFS_STACK_SIZE	256

struct VBVHTree
{
	RayObject rayobj;
	VBVHNode *root;
	MemArena *node_arena;
	float cost;
	RTBuilder *builder;
};

/*
 * Cost to test N childs
 */
struct PackCost
{
	float operator()(int n)
	{
		return n;
	}
};

template<>
void bvh_done<VBVHTree>(VBVHTree *obj)
{
	rtbuild_done(obj->builder, &obj->rayobj.control);
	
	//TODO find a away to exactly calculate the needed memory
	MemArena *arena1 = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
					   BLI_memarena_use_malloc(arena1);
	
	//Build and optimize the tree
	if(1)
	{
		VBVHNode *root = BuildBinaryVBVH<VBVHNode>(arena1,&obj->rayobj.control).transform(obj->builder);
		if(RE_rayobjectcontrol_test_break(&obj->rayobj.control))
		{
			BLI_memarena_free(arena1);
			return;
		}

		reorganize(root);
		remove_useless(root, &root);
		bvh_refit(root);
	
		pushup(root);
		pushdown(root);
		obj->root = root;
	}
	else
	{
/*
	TODO
		MemArena *arena2 = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
						   BLI_memarena_use_malloc(arena2);
						   
		//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>(arena2).transform(obj->builder);			
		VBVH_optimalPackSIMD<OVBVHNode,PackCost>(PackCost()).transform(root);
		obj->root = Reorganize_VBVH<OVBVHNode>(arena1).transform(root);
		
		BLI_memarena_free(arena2);
 */
	}

	//Cleanup
	rtbuild_free( obj->builder );
	obj->builder = NULL;

	obj->node_arena = arena1;
	obj->cost = 1.0;	
}

template<int StackSize>
int intersect(VBVHTree *obj, Isect* isec)
{
	//TODO renable hint support
	if(RE_rayobject_isAligned(obj->root))
		return bvh_node_stack_raycast<VBVHNode,StackSize,false>( obj->root, isec);
	else
		return RE_rayobject_intersect( (RayObject*) obj->root, isec );
}

template<class Tree>
void bvh_hint_bb(Tree *tree, LCTSHint *hint, float *min, float *max)
{
	//TODO renable hint support
	{
	 	hint->size = 0;
	 	hint->stack[hint->size++] = (RayObject*)tree->root;
	}
}

void bfree(VBVHTree *tree)
{
	if(tot_pushup + tot_pushdown + tot_hints + tot_moves)
	{
		if(G.f & G_DEBUG) {
			printf("tot pushups: %d\n", tot_pushup);
			printf("tot pushdowns: %d\n", tot_pushdown);
			printf("tot moves: %d\n", tot_moves);
			printf("tot hints created: %d\n", tot_hints);
		}

		tot_pushup = 0;
		tot_pushdown = 0;
		tot_hints = 0;
		tot_moves = 0;
	}
	bvh_free(tree);
}

/* the cast to pointer function is needed to workarround gcc bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11407 */
template<class Tree, int STACK_SIZE>
RayObjectAPI make_api()
{
	static RayObjectAPI api = 
	{
		(RE_rayobject_raycast_callback) ((int(*)(Tree*,Isect*)) &intersect<STACK_SIZE>),
		(RE_rayobject_add_callback)     ((void(*)(Tree*,RayObject*)) &bvh_add<Tree>),
		(RE_rayobject_done_callback)    ((void(*)(Tree*))       &bvh_done<Tree>),
		(RE_rayobject_free_callback)    ((void(*)(Tree*))       &bvh_free<Tree>),
		(RE_rayobject_merge_bb_callback)((void(*)(Tree*,float*,float*)) &bvh_bb<Tree>),
		(RE_rayobject_cost_callback)	((float(*)(Tree*))      &bvh_cost<Tree>),
		(RE_rayobject_hint_bb_callback)	((void(*)(Tree*,LCTSHint*,float*,float*)) &bvh_hint_bb<Tree>)
	};
	
	return api;
}

template<class Tree>
RayObjectAPI* bvh_get_api(int maxstacksize)
{
	static RayObjectAPI bvh_api256 = make_api<Tree,1024>();
	
	if(maxstacksize <= 1024) return &bvh_api256;
	assert(maxstacksize <= 256);
	return 0;
}

RayObject *RE_rayobject_vbvh_create(int size)
{
	return bvh_create_tree<VBVHTree,DFS_STACK_SIZE>(size);
}