warp-lang 1.0.0b2__py3-none-win_amd64.whl → 1.0.0b6__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of warp-lang might be problematic. Click here for more details.

Files changed (271) hide show
  1. docs/conf.py +17 -5
  2. examples/env/env_ant.py +1 -1
  3. examples/env/env_cartpole.py +1 -1
  4. examples/env/env_humanoid.py +1 -1
  5. examples/env/env_usd.py +4 -1
  6. examples/env/environment.py +8 -9
  7. examples/example_dem.py +34 -33
  8. examples/example_diffray.py +364 -337
  9. examples/example_fluid.py +32 -23
  10. examples/example_jacobian_ik.py +97 -93
  11. examples/example_marching_cubes.py +6 -16
  12. examples/example_mesh.py +6 -16
  13. examples/example_mesh_intersect.py +16 -14
  14. examples/example_nvdb.py +14 -16
  15. examples/example_raycast.py +14 -13
  16. examples/example_raymarch.py +16 -23
  17. examples/example_render_opengl.py +19 -10
  18. examples/example_sim_cartpole.py +82 -78
  19. examples/example_sim_cloth.py +45 -48
  20. examples/example_sim_fk_grad.py +51 -44
  21. examples/example_sim_fk_grad_torch.py +47 -40
  22. examples/example_sim_grad_bounce.py +108 -133
  23. examples/example_sim_grad_cloth.py +99 -113
  24. examples/example_sim_granular.py +5 -6
  25. examples/{example_sim_sdf_shape.py → example_sim_granular_collision_sdf.py} +37 -26
  26. examples/example_sim_neo_hookean.py +51 -55
  27. examples/example_sim_particle_chain.py +4 -4
  28. examples/example_sim_quadruped.py +126 -81
  29. examples/example_sim_rigid_chain.py +54 -61
  30. examples/example_sim_rigid_contact.py +66 -70
  31. examples/example_sim_rigid_fem.py +3 -3
  32. examples/example_sim_rigid_force.py +1 -1
  33. examples/example_sim_rigid_gyroscopic.py +3 -4
  34. examples/example_sim_rigid_kinematics.py +28 -39
  35. examples/example_sim_trajopt.py +112 -110
  36. examples/example_sph.py +9 -8
  37. examples/example_wave.py +7 -7
  38. examples/fem/bsr_utils.py +30 -17
  39. examples/fem/example_apic_fluid.py +85 -69
  40. examples/fem/example_convection_diffusion.py +97 -93
  41. examples/fem/example_convection_diffusion_dg.py +142 -149
  42. examples/fem/example_convection_diffusion_dg0.py +141 -136
  43. examples/fem/example_deformed_geometry.py +146 -0
  44. examples/fem/example_diffusion.py +115 -84
  45. examples/fem/example_diffusion_3d.py +116 -86
  46. examples/fem/example_diffusion_mgpu.py +102 -79
  47. examples/fem/example_mixed_elasticity.py +139 -100
  48. examples/fem/example_navier_stokes.py +175 -162
  49. examples/fem/example_stokes.py +143 -111
  50. examples/fem/example_stokes_transfer.py +186 -157
  51. examples/fem/mesh_utils.py +59 -97
  52. examples/fem/plot_utils.py +138 -17
  53. tools/ci/publishing/build_nodes_info.py +54 -0
  54. warp/__init__.py +4 -3
  55. warp/__init__.pyi +1 -0
  56. warp/bin/warp-clang.dll +0 -0
  57. warp/bin/warp.dll +0 -0
  58. warp/build.py +5 -3
  59. warp/build_dll.py +29 -9
  60. warp/builtins.py +836 -492
  61. warp/codegen.py +864 -553
  62. warp/config.py +3 -1
  63. warp/context.py +389 -172
  64. warp/fem/__init__.py +24 -6
  65. warp/fem/cache.py +318 -25
  66. warp/fem/dirichlet.py +7 -3
  67. warp/fem/domain.py +14 -0
  68. warp/fem/field/__init__.py +30 -38
  69. warp/fem/field/field.py +149 -0
  70. warp/fem/field/nodal_field.py +244 -138
  71. warp/fem/field/restriction.py +8 -6
  72. warp/fem/field/test.py +127 -59
  73. warp/fem/field/trial.py +117 -60
  74. warp/fem/geometry/__init__.py +5 -1
  75. warp/fem/geometry/deformed_geometry.py +271 -0
  76. warp/fem/geometry/element.py +24 -1
  77. warp/fem/geometry/geometry.py +86 -14
  78. warp/fem/geometry/grid_2d.py +112 -54
  79. warp/fem/geometry/grid_3d.py +134 -65
  80. warp/fem/geometry/hexmesh.py +953 -0
  81. warp/fem/geometry/partition.py +85 -33
  82. warp/fem/geometry/quadmesh_2d.py +532 -0
  83. warp/fem/geometry/tetmesh.py +451 -115
  84. warp/fem/geometry/trimesh_2d.py +197 -92
  85. warp/fem/integrate.py +534 -268
  86. warp/fem/operator.py +58 -31
  87. warp/fem/polynomial.py +11 -0
  88. warp/fem/quadrature/__init__.py +1 -1
  89. warp/fem/quadrature/pic_quadrature.py +150 -58
  90. warp/fem/quadrature/quadrature.py +209 -57
  91. warp/fem/space/__init__.py +230 -53
  92. warp/fem/space/basis_space.py +489 -0
  93. warp/fem/space/collocated_function_space.py +105 -0
  94. warp/fem/space/dof_mapper.py +49 -2
  95. warp/fem/space/function_space.py +90 -39
  96. warp/fem/space/grid_2d_function_space.py +149 -496
  97. warp/fem/space/grid_3d_function_space.py +173 -538
  98. warp/fem/space/hexmesh_function_space.py +352 -0
  99. warp/fem/space/partition.py +129 -76
  100. warp/fem/space/quadmesh_2d_function_space.py +369 -0
  101. warp/fem/space/restriction.py +46 -34
  102. warp/fem/space/shape/__init__.py +15 -0
  103. warp/fem/space/shape/cube_shape_function.py +738 -0
  104. warp/fem/space/shape/shape_function.py +103 -0
  105. warp/fem/space/shape/square_shape_function.py +611 -0
  106. warp/fem/space/shape/tet_shape_function.py +567 -0
  107. warp/fem/space/shape/triangle_shape_function.py +429 -0
  108. warp/fem/space/tetmesh_function_space.py +132 -1039
  109. warp/fem/space/topology.py +295 -0
  110. warp/fem/space/trimesh_2d_function_space.py +104 -742
  111. warp/fem/types.py +13 -11
  112. warp/fem/utils.py +335 -60
  113. warp/native/array.h +120 -34
  114. warp/native/builtin.h +101 -72
  115. warp/native/bvh.cpp +73 -325
  116. warp/native/bvh.cu +406 -23
  117. warp/native/bvh.h +22 -40
  118. warp/native/clang/clang.cpp +1 -0
  119. warp/native/crt.h +2 -0
  120. warp/native/cuda_util.cpp +8 -3
  121. warp/native/cuda_util.h +1 -0
  122. warp/native/exports.h +1522 -1243
  123. warp/native/intersect.h +19 -4
  124. warp/native/intersect_adj.h +8 -8
  125. warp/native/mat.h +76 -17
  126. warp/native/mesh.cpp +33 -108
  127. warp/native/mesh.cu +114 -18
  128. warp/native/mesh.h +395 -40
  129. warp/native/noise.h +272 -329
  130. warp/native/quat.h +51 -8
  131. warp/native/rand.h +44 -34
  132. warp/native/reduce.cpp +1 -1
  133. warp/native/sparse.cpp +4 -4
  134. warp/native/sparse.cu +163 -155
  135. warp/native/spatial.h +2 -2
  136. warp/native/temp_buffer.h +18 -14
  137. warp/native/vec.h +103 -21
  138. warp/native/warp.cpp +2 -1
  139. warp/native/warp.cu +28 -3
  140. warp/native/warp.h +4 -3
  141. warp/render/render_opengl.py +261 -109
  142. warp/sim/__init__.py +1 -2
  143. warp/sim/articulation.py +385 -185
  144. warp/sim/import_mjcf.py +59 -48
  145. warp/sim/import_urdf.py +15 -15
  146. warp/sim/import_usd.py +174 -102
  147. warp/sim/inertia.py +17 -18
  148. warp/sim/integrator_xpbd.py +4 -3
  149. warp/sim/model.py +330 -250
  150. warp/sim/render.py +1 -1
  151. warp/sparse.py +625 -152
  152. warp/stubs.py +341 -309
  153. warp/tape.py +9 -6
  154. warp/tests/__main__.py +3 -6
  155. warp/tests/assets/curlnoise_golden.npy +0 -0
  156. warp/tests/assets/pnoise_golden.npy +0 -0
  157. warp/tests/{test_class_kernel.py → aux_test_class_kernel.py} +9 -1
  158. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -0
  159. warp/tests/{test_dependent.py → aux_test_dependent.py} +2 -2
  160. warp/tests/{test_reference.py → aux_test_reference.py} +1 -1
  161. warp/tests/aux_test_unresolved_func.py +14 -0
  162. warp/tests/aux_test_unresolved_symbol.py +14 -0
  163. warp/tests/disabled_kinematics.py +239 -0
  164. warp/tests/run_coverage_serial.py +31 -0
  165. warp/tests/test_adam.py +103 -106
  166. warp/tests/test_arithmetic.py +94 -74
  167. warp/tests/test_array.py +82 -101
  168. warp/tests/test_array_reduce.py +57 -23
  169. warp/tests/test_atomic.py +64 -28
  170. warp/tests/test_bool.py +22 -12
  171. warp/tests/test_builtins_resolution.py +1292 -0
  172. warp/tests/test_bvh.py +18 -18
  173. warp/tests/test_closest_point_edge_edge.py +54 -57
  174. warp/tests/test_codegen.py +165 -134
  175. warp/tests/test_compile_consts.py +28 -20
  176. warp/tests/test_conditional.py +108 -24
  177. warp/tests/test_copy.py +10 -12
  178. warp/tests/test_ctypes.py +112 -88
  179. warp/tests/test_dense.py +21 -14
  180. warp/tests/test_devices.py +98 -0
  181. warp/tests/test_dlpack.py +75 -75
  182. warp/tests/test_examples.py +237 -0
  183. warp/tests/test_fabricarray.py +22 -24
  184. warp/tests/test_fast_math.py +15 -11
  185. warp/tests/test_fem.py +1034 -124
  186. warp/tests/test_fp16.py +23 -16
  187. warp/tests/test_func.py +187 -86
  188. warp/tests/test_generics.py +194 -49
  189. warp/tests/test_grad.py +123 -181
  190. warp/tests/test_grad_customs.py +176 -0
  191. warp/tests/test_hash_grid.py +35 -34
  192. warp/tests/test_import.py +10 -23
  193. warp/tests/test_indexedarray.py +24 -25
  194. warp/tests/test_intersect.py +18 -9
  195. warp/tests/test_large.py +141 -0
  196. warp/tests/test_launch.py +14 -41
  197. warp/tests/test_lerp.py +64 -65
  198. warp/tests/test_lvalue.py +493 -0
  199. warp/tests/test_marching_cubes.py +12 -13
  200. warp/tests/test_mat.py +517 -2898
  201. warp/tests/test_mat_lite.py +115 -0
  202. warp/tests/test_mat_scalar_ops.py +2889 -0
  203. warp/tests/test_math.py +103 -9
  204. warp/tests/test_matmul.py +304 -69
  205. warp/tests/test_matmul_lite.py +410 -0
  206. warp/tests/test_mesh.py +60 -22
  207. warp/tests/test_mesh_query_aabb.py +21 -25
  208. warp/tests/test_mesh_query_point.py +111 -22
  209. warp/tests/test_mesh_query_ray.py +12 -24
  210. warp/tests/test_mlp.py +30 -22
  211. warp/tests/test_model.py +92 -89
  212. warp/tests/test_modules_lite.py +39 -0
  213. warp/tests/test_multigpu.py +88 -114
  214. warp/tests/test_noise.py +12 -11
  215. warp/tests/test_operators.py +16 -20
  216. warp/tests/test_options.py +11 -11
  217. warp/tests/test_pinned.py +17 -18
  218. warp/tests/test_print.py +32 -11
  219. warp/tests/test_quat.py +275 -129
  220. warp/tests/test_rand.py +18 -16
  221. warp/tests/test_reload.py +38 -34
  222. warp/tests/test_rounding.py +50 -43
  223. warp/tests/test_runlength_encode.py +168 -20
  224. warp/tests/test_smoothstep.py +9 -11
  225. warp/tests/test_snippet.py +143 -0
  226. warp/tests/test_sparse.py +261 -63
  227. warp/tests/test_spatial.py +276 -243
  228. warp/tests/test_streams.py +110 -85
  229. warp/tests/test_struct.py +268 -63
  230. warp/tests/test_tape.py +39 -21
  231. warp/tests/test_torch.py +90 -86
  232. warp/tests/test_transient_module.py +10 -12
  233. warp/tests/test_types.py +363 -0
  234. warp/tests/test_utils.py +451 -0
  235. warp/tests/test_vec.py +354 -2050
  236. warp/tests/test_vec_lite.py +73 -0
  237. warp/tests/test_vec_scalar_ops.py +2099 -0
  238. warp/tests/test_volume.py +418 -376
  239. warp/tests/test_volume_write.py +124 -134
  240. warp/tests/unittest_serial.py +35 -0
  241. warp/tests/unittest_suites.py +291 -0
  242. warp/tests/unittest_utils.py +342 -0
  243. warp/tests/{test_misc.py → unused_test_misc.py} +13 -5
  244. warp/tests/{test_debug.py → walkthough_debug.py} +3 -17
  245. warp/thirdparty/appdirs.py +36 -45
  246. warp/thirdparty/unittest_parallel.py +589 -0
  247. warp/types.py +622 -211
  248. warp/utils.py +54 -393
  249. warp_lang-1.0.0b6.dist-info/METADATA +238 -0
  250. warp_lang-1.0.0b6.dist-info/RECORD +409 -0
  251. {warp_lang-1.0.0b2.dist-info → warp_lang-1.0.0b6.dist-info}/WHEEL +1 -1
  252. examples/example_cache_management.py +0 -40
  253. examples/example_multigpu.py +0 -54
  254. examples/example_struct.py +0 -65
  255. examples/fem/example_stokes_transfer_3d.py +0 -210
  256. warp/bin/warp-clang.so +0 -0
  257. warp/bin/warp.so +0 -0
  258. warp/fem/field/discrete_field.py +0 -80
  259. warp/fem/space/nodal_function_space.py +0 -233
  260. warp/tests/test_all.py +0 -223
  261. warp/tests/test_array_scan.py +0 -60
  262. warp/tests/test_base.py +0 -208
  263. warp/tests/test_unresolved_func.py +0 -7
  264. warp/tests/test_unresolved_symbol.py +0 -7
  265. warp_lang-1.0.0b2.dist-info/METADATA +0 -26
  266. warp_lang-1.0.0b2.dist-info/RECORD +0 -380
  267. /warp/tests/{test_compile_consts_dummy.py → aux_test_compile_consts_dummy.py} +0 -0
  268. /warp/tests/{test_reference_reference.py → aux_test_reference_reference.py} +0 -0
  269. /warp/tests/{test_square.py → aux_test_square.py} +0 -0
  270. {warp_lang-1.0.0b2.dist-info → warp_lang-1.0.0b6.dist-info}/LICENSE.md +0 -0
  271. {warp_lang-1.0.0b2.dist-info → warp_lang-1.0.0b6.dist-info}/top_level.txt +0 -0
warp/native/bvh.cu CHANGED
@@ -9,6 +9,7 @@
9
9
  #include "warp.h"
10
10
  #include "cuda_util.h"
11
11
  #include "bvh.h"
12
+ #include "sort.h"
12
13
 
13
14
  #include <vector>
14
15
  #include <algorithm>
@@ -16,25 +17,32 @@
16
17
  #include <cuda.h>
17
18
  #include <cuda_runtime_api.h>
18
19
 
20
+ #define THRUST_IGNORE_CUB_VERSION_CHECK
21
+
22
+ #include <cub/cub.cuh>
23
+
24
+
19
25
  namespace wp
20
26
  {
21
27
 
22
- __global__ void bvh_refit_kernel(int n, const int* __restrict__ parents, int* __restrict__ child_count, BVHPackedNodeHalf* __restrict__ lowers, BVHPackedNodeHalf* __restrict__ uppers, const bounds3* bounds)
28
+ __global__ void bvh_refit_kernel(int n, const int* __restrict__ parents, int* __restrict__ child_count, BVHPackedNodeHalf* __restrict__ node_lowers, BVHPackedNodeHalf* __restrict__ node_uppers, const vec3* item_lowers, const vec3* item_uppers)
23
29
  {
24
30
  int index = blockDim.x*blockIdx.x + threadIdx.x;
25
31
 
26
32
  if (index < n)
27
33
  {
28
- bool leaf = lowers[index].b;
34
+ bool leaf = node_lowers[index].b;
29
35
 
30
36
  if (leaf)
31
37
  {
32
38
  // update the leaf node
33
- const int leaf_index = lowers[index].i;
34
- const bounds3& b = bounds[leaf_index];
39
+ const int leaf_index = node_lowers[index].i;
35
40
 
36
- make_node(lowers+index, b.lower, leaf_index, true);
37
- make_node(uppers+index, b.upper, 0, false);
41
+ vec3 lower = item_lowers[leaf_index];
42
+ vec3 upper = item_uppers[leaf_index];
43
+
44
+ make_node(node_lowers+index, lower, leaf_index, true);
45
+ make_node(node_uppers+index, upper, 0, false);
38
46
  }
39
47
  else
40
48
  {
@@ -59,6 +67,214 @@ __global__ void bvh_refit_kernel(int n, const int* __restrict__ parents, int* __
59
67
  // if we have are the last thread (such that the parent node is now complete)
60
68
  // then update its bounds and move onto the the next parent in the hierarchy
61
69
  if (finished == 1)
70
+ {
71
+ const int left_child = node_lowers[parent].i;
72
+ const int right_child = node_uppers[parent].i;
73
+
74
+ vec3 left_lower = vec3(node_lowers[left_child].x,
75
+ node_lowers[left_child].y,
76
+ node_lowers[left_child].z);
77
+
78
+ vec3 left_upper = vec3(node_uppers[left_child].x,
79
+ node_uppers[left_child].y,
80
+ node_uppers[left_child].z);
81
+
82
+ vec3 right_lower = vec3(node_lowers[right_child].x,
83
+ node_lowers[right_child].y,
84
+ node_lowers[right_child].z);
85
+
86
+
87
+ vec3 right_upper = vec3(node_uppers[right_child].x,
88
+ node_uppers[right_child].y,
89
+ node_uppers[right_child].z);
90
+
91
+ // union of child bounds
92
+ vec3 lower = min(left_lower, right_lower);
93
+ vec3 upper = max(left_upper, right_upper);
94
+
95
+ // write new BVH nodes
96
+ make_node(node_lowers+parent, lower, left_child, false);
97
+ make_node(node_uppers+parent, upper, right_child, false);
98
+
99
+ // move onto processing the parent
100
+ index = parent;
101
+ }
102
+ else
103
+ {
104
+ // parent not ready (we are the first child), terminate thread
105
+ break;
106
+ }
107
+ }
108
+ }
109
+ }
110
+
111
+
112
+ void bvh_refit_device(BVH& bvh)
113
+ {
114
+ ContextGuard guard(bvh.context);
115
+
116
+ // clear child counters
117
+ memset_device(WP_CURRENT_CONTEXT, bvh.node_counts, 0, sizeof(int)*bvh.max_nodes);
118
+
119
+ wp_launch_device(WP_CURRENT_CONTEXT, bvh_refit_kernel, bvh.num_items, (bvh.num_items, bvh.node_parents, bvh.node_counts, bvh.node_lowers, bvh.node_uppers, bvh.item_lowers, bvh.item_uppers));
120
+ }
121
+
122
+
123
+ /////////////////////////////////////////////////////////////////////////////////////////////
124
+
125
+ // Create a linear BVH as described in Fast and Simple Agglomerative LBVH construction
126
+ // this is a bottom-up clustering method that outputs one node per-leaf
127
+ //
128
+ class LinearBVHBuilderGPU
129
+ {
130
+ public:
131
+
132
+ LinearBVHBuilderGPU();
133
+ ~LinearBVHBuilderGPU();
134
+
135
+ // takes a bvh (host ref), and pointers to the GPU lower and upper bounds for each triangle
136
+ void build(BVH& bvh, const vec3* item_lowers, const vec3* item_uppers, int num_items, bounds3* total_bounds);
137
+
138
+ private:
139
+
140
+ // temporary data used during building
141
+ int* indices;
142
+ int* keys;
143
+ int* deltas;
144
+ int* range_lefts;
145
+ int* range_rights;
146
+ int* num_children;
147
+
148
+ // bounds data when total item bounds built on GPU
149
+ vec3* total_lower;
150
+ vec3* total_upper;
151
+ vec3* total_inv_edges;
152
+ };
153
+
154
+ ////////////////////////////////////////////////////////
155
+
156
+
157
+
158
+ __global__ void compute_morton_codes(const vec3* __restrict__ item_lowers, const vec3* __restrict__ item_uppers, int n, const vec3* grid_lower, const vec3* grid_inv_edges, int* __restrict__ indices, int* __restrict__ keys)
159
+ {
160
+ const int index = blockDim.x*blockIdx.x + threadIdx.x;
161
+
162
+ if (index < n)
163
+ {
164
+ vec3 lower = item_lowers[index];
165
+ vec3 upper = item_uppers[index];
166
+
167
+ vec3 center = 0.5f*(lower+upper);
168
+
169
+ vec3 local = cw_mul((center-grid_lower[0]), grid_inv_edges[0]);
170
+
171
+ // 10-bit Morton codes stored in lower 30bits (1024^3 effective resolution)
172
+ int key = morton3<1024>(local[0], local[1], local[2]);
173
+
174
+ indices[index] = index;
175
+ keys[index] = key;
176
+ }
177
+ }
178
+
179
+ // calculate the index of the first differing bit between two adjacent Morton keys
180
+ __global__ void compute_key_deltas(const int* __restrict__ keys, int* __restrict__ deltas, int n)
181
+ {
182
+ const int index = blockDim.x*blockIdx.x + threadIdx.x;
183
+
184
+ if (index < n)
185
+ {
186
+ int a = keys[index];
187
+ int b = keys[index+1];
188
+
189
+ int x = a^b;
190
+
191
+ deltas[index] = x;// __clz(x);
192
+ }
193
+ }
194
+
195
+ __global__ void build_leaves(const vec3* __restrict__ item_lowers, const vec3* __restrict__ item_uppers, int n, const int* __restrict__ indices, int* __restrict__ range_lefts, int* __restrict__ range_rights, BVHPackedNodeHalf* __restrict__ lowers, BVHPackedNodeHalf* __restrict__ uppers)
196
+ {
197
+ const int index = blockDim.x*blockIdx.x + threadIdx.x;
198
+
199
+ if (index < n)
200
+ {
201
+ const int item = indices[index];
202
+
203
+ vec3 lower = item_lowers[item];
204
+ vec3 upper = item_uppers[item];
205
+
206
+ // write leaf nodes
207
+ lowers[index] = make_node(lower, item, true);
208
+ uppers[index] = make_node(upper, item, false);
209
+
210
+ // write leaf key ranges
211
+ range_lefts[index] = index;
212
+ range_rights[index] = index;
213
+ }
214
+ }
215
+
216
+ // this bottom-up process assigns left and right children and combines bounds to form internal nodes
217
+ // there is one thread launched per-leaf node, each thread calculates it's parent node and assigns
218
+ // itself to either the left or right parent slot, the last child to complete the parent and moves
219
+ // up the hierarchy
220
+ __global__ void build_hierarchy(int n, int* root, const int* __restrict__ deltas, int* __restrict__ num_children, volatile int* __restrict__ range_lefts, volatile int* __restrict__ range_rights, volatile int* __restrict__ parents, volatile BVHPackedNodeHalf* __restrict__ lowers, volatile BVHPackedNodeHalf* __restrict__ uppers)
221
+ {
222
+ int index = blockDim.x*blockIdx.x + threadIdx.x;
223
+
224
+ if (index < n)
225
+ {
226
+ const int internal_offset = n;
227
+
228
+ for (;;)
229
+ {
230
+ int left = range_lefts[index];
231
+ int right = range_rights[index];
232
+
233
+ // check if we are the root node, if so then store out our index and terminate
234
+ if (left == 0 && right == n-1)
235
+ {
236
+ *root = index;
237
+ parents[index] = -1;
238
+
239
+ break;
240
+ }
241
+
242
+ int childCount = 0;
243
+
244
+ int parent;
245
+
246
+ if (left == 0 || (right != n-1 && deltas[right] < deltas[left-1]))
247
+ {
248
+ parent = right + internal_offset;
249
+
250
+ // set parent left child
251
+ parents[index] = parent;
252
+ lowers[parent].i = index;
253
+ range_lefts[parent] = left;
254
+
255
+ // ensure above writes are visible to all threads
256
+ __threadfence();
257
+
258
+ childCount = atomicAdd(&num_children[parent], 1);
259
+ }
260
+ else
261
+ {
262
+ parent = left + internal_offset - 1;
263
+
264
+ // set parent right child
265
+ parents[index] = parent;
266
+ uppers[parent].i = index;
267
+ range_rights[parent] = right;
268
+
269
+ // ensure above writes are visible to all threads
270
+ __threadfence();
271
+
272
+ childCount = atomicAdd(&num_children[parent], 1);
273
+ }
274
+
275
+ // if we have are the last thread (such that the parent node is now complete)
276
+ // then update its bounds and move onto the the next parent in the hierarchy
277
+ if (childCount == 1)
62
278
  {
63
279
  const int left_child = lowers[parent].i;
64
280
  const int right_child = uppers[parent].i;
@@ -72,15 +288,15 @@ __global__ void bvh_refit_kernel(int n, const int* __restrict__ parents, int* __
72
288
  uppers[left_child].z);
73
289
 
74
290
  vec3 right_lower = vec3(lowers[right_child].x,
75
- lowers[right_child].y,
76
- lowers[right_child].z);
291
+ lowers[right_child].y,
292
+ lowers[right_child].z);
77
293
 
78
294
 
79
295
  vec3 right_upper = vec3(uppers[right_child].x,
80
- uppers[right_child].y,
81
- uppers[right_child].z);
296
+ uppers[right_child].y,
297
+ uppers[right_child].z);
82
298
 
83
- // union of child bounds
299
+ // bounds_union of child bounds
84
300
  vec3 lower = min(left_lower, right_lower);
85
301
  vec3 upper = max(left_upper, right_upper);
86
302
 
@@ -100,30 +316,158 @@ __global__ void bvh_refit_kernel(int n, const int* __restrict__ parents, int* __
100
316
  }
101
317
  }
102
318
 
319
+ CUDA_CALLABLE inline vec3 Vec3Max(const vec3& a, const vec3& b) { return wp::max(a, b); }
320
+ CUDA_CALLABLE inline vec3 Vec3Min(const vec3& a, const vec3& b) { return wp::min(a, b); }
103
321
 
104
- void bvh_refit_device(BVH& bvh, const bounds3* b)
322
+ __global__ void compute_total_bounds(const vec3* item_lowers, const vec3* item_uppers, vec3* total_lower, vec3* total_upper, int num_items)
105
323
  {
106
- ContextGuard guard(bvh.context);
324
+ typedef cub::BlockReduce<vec3, 256> BlockReduce;
107
325
 
108
- // clear child counters
109
- memset_device(WP_CURRENT_CONTEXT, bvh.node_counts, 0, sizeof(int)*bvh.max_nodes);
326
+ __shared__ typename BlockReduce::TempStorage temp_storage;
327
+
328
+ const int blockStart = blockDim.x*blockIdx.x;
329
+ const int numValid = ::min(num_items-blockStart, blockDim.x);
330
+
331
+ const int tid = blockStart + threadIdx.x;
332
+
333
+ if (tid < num_items)
334
+ {
335
+ vec3 lower = item_lowers[tid];
336
+ vec3 upper = item_uppers[tid];
337
+
338
+ vec3 block_upper = BlockReduce(temp_storage).Reduce(upper, Vec3Max, numValid);
339
+
340
+ // sync threads because second reduce uses same temp storage as first
341
+ __syncthreads();
342
+
343
+ vec3 block_lower = BlockReduce(temp_storage).Reduce(lower, Vec3Min, numValid);
110
344
 
111
- wp_launch_device(WP_CURRENT_CONTEXT, bvh_refit_kernel, bvh.max_nodes, (bvh.max_nodes, bvh.node_parents, bvh.node_counts, bvh.node_lowers, bvh.node_uppers, b));
345
+ if (threadIdx.x == 0)
346
+ {
347
+ // write out block results, expanded by the radius
348
+ atomic_max(total_upper, block_upper);
349
+ atomic_min(total_lower, block_lower);
350
+ }
351
+ }
352
+ }
353
+
354
+ // compute inverse edge length, this is just done on the GPU to avoid a CPU->GPU sync point
355
+ __global__ void compute_total_inv_edges(const vec3* total_lower, const vec3* total_upper, vec3* total_inv_edges)
356
+ {
357
+ vec3 edges = (total_upper[0]-total_lower[0]);
358
+ edges += vec3(0.0001f);
359
+
360
+ total_inv_edges[0] = vec3(1.0f/edges[0], 1.0f/edges[1], 1.0f/edges[2]);
112
361
  }
113
362
 
114
- __global__ void set_bounds_from_lowers_and_uppers(int n, bounds3* b, const vec3* lowers, const vec3* uppers)
363
+
364
+
365
+ LinearBVHBuilderGPU::LinearBVHBuilderGPU()
366
+ : indices(NULL)
367
+ , keys(NULL)
368
+ , deltas(NULL)
369
+ , range_lefts(NULL)
370
+ , range_rights(NULL)
371
+ , num_children(NULL)
372
+ , total_lower(NULL)
373
+ , total_upper(NULL)
374
+ , total_inv_edges(NULL)
115
375
  {
116
- const int tid = blockIdx.x*blockDim.x + threadIdx.x;
376
+ total_lower = (vec3*)alloc_temp_device(WP_CURRENT_CONTEXT, sizeof(vec3));
377
+ total_upper = (vec3*)alloc_temp_device(WP_CURRENT_CONTEXT, sizeof(vec3));
378
+ total_inv_edges = (vec3*)alloc_temp_device(WP_CURRENT_CONTEXT, sizeof(vec3));
379
+ }
117
380
 
118
- if (tid < n)
381
+ LinearBVHBuilderGPU::~LinearBVHBuilderGPU()
382
+ {
383
+ free_temp_device(WP_CURRENT_CONTEXT, total_lower);
384
+ free_temp_device(WP_CURRENT_CONTEXT, total_upper);
385
+ free_temp_device(WP_CURRENT_CONTEXT, total_inv_edges);
386
+ }
387
+
388
+
389
+
390
+ void LinearBVHBuilderGPU::build(BVH& bvh, const vec3* item_lowers, const vec3* item_uppers, int num_items, bounds3* total_bounds)
391
+ {
392
+ // allocate temporary memory used during building
393
+ indices = (int*)alloc_temp_device(WP_CURRENT_CONTEXT, sizeof(int)*num_items*2); // *2 for radix sort
394
+ keys = (int*)alloc_temp_device(WP_CURRENT_CONTEXT, sizeof(int)*num_items*2); // *2 for radix sort
395
+ deltas = (int*)alloc_temp_device(WP_CURRENT_CONTEXT, sizeof(int)*num_items); // highest differenting bit between keys for item i and i+1
396
+ range_lefts = (int*)alloc_temp_device(WP_CURRENT_CONTEXT, sizeof(int)*bvh.max_nodes);
397
+ range_rights = (int*)alloc_temp_device(WP_CURRENT_CONTEXT, sizeof(int)*bvh.max_nodes);
398
+ num_children = (int*)alloc_temp_device(WP_CURRENT_CONTEXT, sizeof(int)*bvh.max_nodes);
399
+
400
+ // if total bounds supplied by the host then we just
401
+ // compute our edge length and upload it to the GPU directly
402
+ if (total_bounds)
119
403
  {
120
- b[tid] = bounds3(lowers[tid], uppers[tid]);
404
+ // calculate Morton codes
405
+ vec3 edges = (*total_bounds).edges();
406
+ edges += vec3(0.0001f);
407
+
408
+ vec3 inv_edges = vec3(1.0f/edges[0], 1.0f/edges[1], 1.0f/edges[2]);
409
+
410
+ memcpy_h2d(WP_CURRENT_CONTEXT, total_lower, &total_bounds->lower[0], sizeof(vec3));
411
+ memcpy_h2d(WP_CURRENT_CONTEXT, total_upper, &total_bounds->upper[0], sizeof(vec3));
412
+ memcpy_h2d(WP_CURRENT_CONTEXT, total_inv_edges, &inv_edges[0], sizeof(vec3));
413
+ }
414
+ else
415
+ {
416
+ static vec3 upper(-FLT_MAX);
417
+ static vec3 lower(FLT_MAX);
418
+
419
+ memcpy_h2d(WP_CURRENT_CONTEXT, total_lower, &lower, sizeof(lower));
420
+ memcpy_h2d(WP_CURRENT_CONTEXT, total_upper, &upper, sizeof(upper));
421
+
422
+ // compute the total bounds on the GPU
423
+ wp_launch_device(WP_CURRENT_CONTEXT, compute_total_bounds, num_items, (item_lowers, item_uppers, total_lower, total_upper, num_items));
424
+
425
+ // compute the total edge length
426
+ wp_launch_device(WP_CURRENT_CONTEXT, compute_total_inv_edges, 1, (total_lower, total_upper, total_inv_edges));
121
427
  }
428
+
429
+ // assign 30-bit Morton code based on the centroid of each triangle and bounds for each leaf
430
+ wp_launch_device(WP_CURRENT_CONTEXT, compute_morton_codes, num_items, (item_lowers, item_uppers, num_items, total_lower, total_inv_edges, indices, keys));
431
+
432
+ // sort items based on Morton key (note the 32-bit sort key corresponds to the template parameter to morton3, i.e. 3x9 bit keys combined)
433
+ radix_sort_pairs_device(WP_CURRENT_CONTEXT, keys, indices, num_items);
434
+
435
+ // calculate deltas between adjacent keys
436
+ wp_launch_device(WP_CURRENT_CONTEXT, compute_key_deltas, num_items, (keys, deltas, num_items-1));
437
+
438
+ // initialize leaf nodes
439
+ wp_launch_device(WP_CURRENT_CONTEXT, build_leaves, num_items, (item_lowers, item_uppers, num_items, indices, range_lefts, range_rights, bvh.node_lowers, bvh.node_uppers));
440
+
441
+ // reset children count, this is our atomic counter so we know when an internal node is complete, only used during building
442
+ memset_device(WP_CURRENT_CONTEXT, num_children, 0, sizeof(int)*bvh.max_nodes);
443
+
444
+ // build the tree and internal node bounds
445
+ wp_launch_device(WP_CURRENT_CONTEXT, build_hierarchy, num_items, (num_items, bvh.root, deltas, num_children, range_lefts, range_rights, bvh.node_parents, bvh.node_lowers, bvh.node_uppers));
446
+
447
+ // free temporary memory
448
+ free_temp_device(WP_CURRENT_CONTEXT, indices);
449
+ free_temp_device(WP_CURRENT_CONTEXT, keys);
450
+ free_temp_device(WP_CURRENT_CONTEXT, deltas);
451
+
452
+ free_temp_device(WP_CURRENT_CONTEXT, range_lefts);
453
+ free_temp_device(WP_CURRENT_CONTEXT, range_rights);
454
+ free_temp_device(WP_CURRENT_CONTEXT, num_children);
455
+
456
+ }
457
+
458
+ void bvh_destroy_device(wp::BVH& bvh)
459
+ {
460
+ ContextGuard guard(bvh.context);
461
+
462
+ free_device(WP_CURRENT_CONTEXT, bvh.node_lowers); bvh.node_lowers = NULL;
463
+ free_device(WP_CURRENT_CONTEXT, bvh.node_uppers); bvh.node_uppers = NULL;
464
+ free_device(WP_CURRENT_CONTEXT, bvh.node_parents); bvh.node_parents = NULL;
465
+ free_device(WP_CURRENT_CONTEXT, bvh.node_counts); bvh.node_counts = NULL;
466
+ free_device(WP_CURRENT_CONTEXT, bvh.root); bvh.root = NULL;
122
467
  }
123
468
 
124
469
  } // namespace wp
125
470
 
126
- // refit to data stored in the bvh
127
471
 
128
472
  void bvh_refit_device(uint64_t id)
129
473
  {
@@ -131,12 +475,51 @@ void bvh_refit_device(uint64_t id)
131
475
  if (bvh_get_descriptor(id, bvh))
132
476
  {
133
477
  ContextGuard guard(bvh.context);
134
- wp_launch_device(WP_CURRENT_CONTEXT, wp::set_bounds_from_lowers_and_uppers, bvh.num_bounds, (bvh.num_bounds, bvh.bounds, bvh.lowers, bvh.uppers));
135
478
 
136
- bvh_refit_device(bvh, bvh.bounds);
479
+ bvh_refit_device(bvh);
137
480
  }
481
+ }
138
482
 
483
+ uint64_t bvh_create_device(void* context, wp::vec3* lowers, wp::vec3* uppers, int num_items)
484
+ {
485
+ ContextGuard guard(context);
486
+
487
+ wp::BVH bvh_host;
488
+ bvh_host.num_items = num_items;
489
+ bvh_host.max_nodes = 2*num_items;
490
+ bvh_host.node_lowers = (wp::BVHPackedNodeHalf*)alloc_device(WP_CURRENT_CONTEXT, sizeof(wp::BVHPackedNodeHalf)*bvh_host.max_nodes);
491
+ bvh_host.node_uppers = (wp::BVHPackedNodeHalf*)alloc_device(WP_CURRENT_CONTEXT, sizeof(wp::BVHPackedNodeHalf)*bvh_host.max_nodes);
492
+ bvh_host.node_parents = (int*)alloc_device(WP_CURRENT_CONTEXT, sizeof(int)*bvh_host.max_nodes);
493
+ bvh_host.node_counts = (int*)alloc_device(WP_CURRENT_CONTEXT, sizeof(int)*bvh_host.max_nodes);
494
+ bvh_host.root = (int*)alloc_device(WP_CURRENT_CONTEXT, sizeof(int));
495
+ bvh_host.item_lowers = lowers;
496
+ bvh_host.item_uppers = uppers;
497
+
498
+ bvh_host.context = context ? context : cuda_context_get_current();
499
+
500
+ wp::LinearBVHBuilderGPU builder;
501
+ builder.build(bvh_host, lowers, uppers, num_items, NULL);
502
+
503
+ // create device-side BVH descriptor
504
+ wp::BVH* bvh_device = (wp::BVH*)alloc_device(WP_CURRENT_CONTEXT, sizeof(wp::BVH));
505
+ memcpy_h2d(WP_CURRENT_CONTEXT, bvh_device, &bvh_host, sizeof(wp::BVH));
506
+
507
+ uint64_t bvh_id = (uint64_t)bvh_device;
508
+ wp::bvh_add_descriptor(bvh_id, bvh_host);
509
+
510
+ return bvh_id;
139
511
  }
140
512
 
141
513
 
514
+ void bvh_destroy_device(uint64_t id)
515
+ {
516
+ wp::BVH bvh;
517
+ if (wp::bvh_get_descriptor(id, bvh))
518
+ {
519
+ wp::bvh_destroy_device(bvh);
520
+ wp::bvh_rem_descriptor(id);
142
521
 
522
+ // free descriptor
523
+ free_device(WP_CURRENT_CONTEXT, (void*)id);
524
+ }
525
+ }
warp/native/bvh.h CHANGED
@@ -113,7 +113,7 @@ struct BVHPackedNodeHalf
113
113
  };
114
114
 
115
115
  struct BVH
116
- {
116
+ {
117
117
  BVHPackedNodeHalf* node_lowers;
118
118
  BVHPackedNodeHalf* node_uppers;
119
119
 
@@ -123,33 +123,22 @@ struct BVH
123
123
 
124
124
  int max_depth;
125
125
  int max_nodes;
126
- int num_nodes;
127
-
128
- int root;
126
+ int num_nodes;
127
+
128
+ // pointer (CPU or GPU) to a single integer index in node_lowers, node_uppers
129
+ // representing the root of the tree, this is not always the first node
130
+ // for bottom-up builders
131
+ int* root;
129
132
 
130
- vec3* lowers;
131
- vec3* uppers;
132
- bounds3* bounds;
133
- int num_bounds;
133
+ // item bounds are not owned by the BVH but by the caller
134
+ vec3* item_lowers;
135
+ vec3* item_uppers;
136
+ int num_items;
134
137
 
138
+ // cuda context
135
139
  void* context;
136
140
  };
137
141
 
138
- #if !defined(__CUDA_ARCH__)
139
-
140
- BVH bvh_create(const bounds3* bounds, int num_bounds);
141
-
142
- void bvh_destroy_host(BVH& bvh);
143
- void bvh_destroy_device(BVH& bvh);
144
-
145
- void bvh_refit_host(BVH& bvh, const bounds3* bounds);
146
- void bvh_refit_device(BVH& bvh, const bounds3* bounds);
147
-
148
- // copy host BVH to device
149
- BVH bvh_clone(void* context, const BVH& bvh_host);
150
-
151
- #endif // !__CUDA_ARCH__
152
-
153
142
  CUDA_CALLABLE inline BVHPackedNodeHalf make_node(const vec3& bound, int child, bool leaf)
154
143
  {
155
144
  BVHPackedNodeHalf n;
@@ -162,7 +151,7 @@ CUDA_CALLABLE inline BVHPackedNodeHalf make_node(const vec3& bound, int child, b
162
151
  return n;
163
152
  }
164
153
 
165
- // variation of make_node through volatile pointers used in BuildHierarchy
154
+ // variation of make_node through volatile pointers used in build_hierarchy
166
155
  CUDA_CALLABLE inline void make_node(volatile BVHPackedNodeHalf* n, const vec3& bound, int child, bool leaf)
167
156
  {
168
157
  n->x = bound[0];
@@ -211,7 +200,7 @@ CUDA_CALLABLE inline BVH bvh_get(uint64_t id)
211
200
  CUDA_CALLABLE inline int bvh_get_num_bounds(uint64_t id)
212
201
  {
213
202
  BVH bvh = bvh_get(id);
214
- return bvh.num_bounds;
203
+ return bvh.num_items;
215
204
  }
216
205
 
217
206
 
@@ -257,17 +246,8 @@ CUDA_CALLABLE inline bvh_query_t bvh_query(
257
246
  query.bvh = bvh;
258
247
  query.is_ray = is_ray;
259
248
 
260
-
261
- // if no bvh nodes, return empty query.
262
- if (bvh.num_nodes == 0)
263
- {
264
- query.count = 0;
265
- return query;
266
- }
267
-
268
- // optimization: make the latest
269
-
270
- query.stack[0] = bvh.root;
249
+ // optimization: make the latest
250
+ query.stack[0] = *bvh.root;
271
251
  query.count = 1;
272
252
  query.input_lower = lower;
273
253
  query.input_upper = upper;
@@ -422,17 +402,19 @@ CUDA_CALLABLE inline void adj_bvh_query_next(bvh_query_t& query, int& index, bvh
422
402
 
423
403
  }
424
404
 
425
-
426
-
427
-
428
405
  CUDA_CALLABLE bool bvh_get_descriptor(uint64_t id, BVH& bvh);
429
406
  CUDA_CALLABLE void bvh_add_descriptor(uint64_t id, const BVH& bvh);
430
407
  CUDA_CALLABLE void bvh_rem_descriptor(uint64_t id);
431
408
 
409
+ #if !__CUDA_ARCH__
432
410
 
411
+ void bvh_destroy_host(wp::BVH& bvh);
412
+ void bvh_refit_host(wp::BVH& bvh);
433
413
 
414
+ void bvh_destroy_device(wp::BVH& bvh);
415
+ void bvh_refit_device(uint64_t id);
434
416
 
435
-
417
+ #endif
436
418
 
437
419
  } // namespace wp
438
420
 
@@ -359,6 +359,7 @@ WP_API int load_obj(const char* object_file, const char* module_name)
359
359
  SYMBOL(log10f), SYMBOL_T(log10, double(*)(double)),
360
360
  SYMBOL(expf), SYMBOL_T(exp, double(*)(double)),
361
361
  SYMBOL(sqrtf), SYMBOL_T(sqrt, double(*)(double)),
362
+ SYMBOL(cbrtf), SYMBOL_T(cbrt, double(*)(double)),
362
363
  SYMBOL(powf), SYMBOL_T(pow, double(*)(double, double)),
363
364
  SYMBOL(floorf), SYMBOL_T(floor, double(*)(double)),
364
365
  SYMBOL(ceilf), SYMBOL_T(ceil, double(*)(double)),
warp/native/crt.h CHANGED
@@ -259,6 +259,8 @@ float expf(float);
259
259
  double exp(double);
260
260
  float sqrtf(float);
261
261
  double sqrt(double);
262
+ float cbrtf(float);
263
+ double cbrt(double);
262
264
  float powf(float, float);
263
265
  double pow(double, double);
264
266
  float floorf(float);
warp/native/cuda_util.cpp CHANGED
@@ -89,6 +89,7 @@ static PFN_cuGraphicsResourceGetMappedPointer_v3020 pfn_cuGraphicsResourceGetMap
89
89
  static PFN_cuGraphicsGLRegisterBuffer_v3000 pfn_cuGraphicsGLRegisterBuffer;
90
90
  static PFN_cuGraphicsUnregisterResource_v3000 pfn_cuGraphicsUnregisterResource;
91
91
 
92
+ static bool cuda_driver_initialized = false;
92
93
 
93
94
  bool ContextGuard::always_restore = false;
94
95
 
@@ -196,11 +197,15 @@ bool init_cuda_driver()
196
197
  get_driver_entry_point("cuGraphicsUnregisterResource", &(void*&)pfn_cuGraphicsUnregisterResource);
197
198
 
198
199
  if (pfn_cuInit)
199
- return check_cu(pfn_cuInit(0));
200
- else
201
- return false;
200
+ cuda_driver_initialized = check_cu(pfn_cuInit(0));
201
+
202
+ return cuda_driver_initialized;
202
203
  }
203
204
 
205
+ bool is_cuda_driver_initialized()
206
+ {
207
+ return cuda_driver_initialized;
208
+ }
204
209
 
205
210
  bool check_cuda_result(cudaError_t code, const char* file, int line)
206
211
  {
warp/native/cuda_util.h CHANGED
@@ -83,6 +83,7 @@ CUresult cuGraphicsUnregisterResource_f(CUgraphicsResource resource);
83
83
 
84
84
 
85
85
  bool init_cuda_driver();
86
+ bool is_cuda_driver_initialized();
86
87
 
87
88
  bool check_cuda_result(cudaError_t code, const char* file, int line);
88
89
  inline bool check_cuda_result(uint64_t code, const char* file, int line)