1- import os , sys , enum , logging , collections
1+ import os , sys , enum , copy , logging , collections
22
33import numpy as np
44import cupy as cp
55import optix as ox
66import glfw , imgui
77
8- from optix .sutils .gui import init_ui , display_stats
9- from optix .sutils .gl_display import GLDisplay
10- from optix .sutils .trackball import Trackball , TrackballViewMode
11- from optix .sutils .cuda_output_buffer import CudaOutputBuffer , CudaOutputBufferType , BufferImageFormat
8+ from optix .sutil .gui import init_ui , display_stats
9+ from optix .sutil .gl_display import GLDisplay
10+ from optix .sutil .trackball import Trackball , TrackballViewMode
11+ from optix .sutil .cuda_output_buffer import CudaOutputBuffer , CudaOutputBufferType , BufferImageFormat
1212
1313logging .basicConfig (stream = sys .stdout , level = logging .DEBUG )
1414log = logging .getLogger ()
1515
1616script_dir = os .path .dirname (os .path .abspath (__file__ ))
1717
18+ DEBUG = False
19+
1820#------------------------------------------------------------------------------
1921# Local types
2022#------------------------------------------------------------------------------
@@ -58,7 +60,8 @@ class DynamicGeometryState:
5860 __slots__ = ['params' , 'time' , 'ctx' , 'module' , 'pipeline' , 'pipeline_opts' ,
5961 'raygen_grp' , 'miss_grp' , 'hit_grp' , 'sbt' ,
6062 'generate_vertices_kernel' , 'd_temp_vertices' , 'last_exploding_sphere_rebuild_time' ,
61- 'static_gas' , 'deforming_gas' , 'exploding_gas' , 'ias' ,
63+ 'gas_build_input' , 'static_gas' , 'deforming_gas' , 'exploding_gas' ,
64+ 'ias_build_input' , 'ias' ,
6265 'trackball' , 'camera_changed' , 'mouse_button' , 'resize_dirty' , 'minimized' ]
6366
6467 def __init__ (self ):
@@ -77,7 +80,7 @@ def camera(self):
7780 return self .trackball .camera
7881
7982 @property
80- def dimensions (self ):
83+ def launch_dimensions (self ):
8184 return (int (self .params .width ), int (self .params .height ))
8285
8386class AnimationMode (enum .Enum ):
@@ -203,7 +206,7 @@ def update_state(output_buffer, state):
203206def launch_subframe (output_buffer , state ):
204207 state .params .frame_buffer = output_buffer .map ()
205208
206- state .pipeline .launch (state .sbt , dimensions = state .dimensions ,
209+ state .pipeline .launch (state .sbt , dimensions = state .launch_dimensions ,
207210 params = state .params .handle , stream = output_buffer .stream )
208211
209212 output_buffer .unmap ()
@@ -229,7 +232,7 @@ def init_camera_state(state):
229232
230233def create_context (state ):
231234 logger = ox .Logger (log )
232- ctx = ox .DeviceContext (validation_mode = True , log_callback_function = logger , log_callback_level = 4 )
235+ ctx = ox .DeviceContext (validation_mode = False , log_callback_function = logger , log_callback_level = 4 )
233236 ctx .cache_enabled = False
234237 state .ctx = ctx
235238
@@ -246,8 +249,30 @@ def launch_generate_animated_vertices(state, animation_mode):
246249 generate_animated_vertices (state .d_temp_vertices , animation_mode , state .time , g_tessellation_resolution , g_tessellation_resolution )
247250
248251def update_mesh_accel (state ):
252+ # first sphere is static
253+
254+ # second sphere moves by updating its transform matrix
255+ transform = state .ias_build_input .get_transform_view (1 )
256+ transform [1 ,- 1 ] = np .sin (4 * state .time )
257+
258+ # third sphere deforms
249259 launch_generate_animated_vertices (state , AnimationMode .DEFORM )
250- raise NotImplementedError
260+ state .deforming_gas .update (state .gas_build_input )
261+
262+ # fourth sphere explodes
263+ launch_generate_animated_vertices (state , AnimationMode .EXPLODE )
264+
265+ # we occasionally rebuild the exploding sphere to maintain AS quality
266+ if state .time - state .last_exploding_sphere_rebuild_time > 1 / g_exploding_gas_rebuild_frequency :
267+ state .last_exploding_sphere_rebuild_time = state .time
268+ state .exploding_gas = ox .AccelerationStructure (state .ctx , state .gas_build_input ,
269+ compact = True , allow_update = True , random_vertex_access = True )
270+ state .ias_build_input .instances [3 ].update_traversable (state .exploding_gas )
271+ state .ias_build_input .update_instance (3 )
272+ else :
273+ state .exploding_gas .update (state .gas_build_input )
274+
275+ state .ias .update (state .ias_build_input )
251276
252277def build_vertex_generation_kernel (state ):
253278 cuda_source = os .path .join (script_dir , 'cuda' , 'dynamic_geometry_vertex_generation.cu' )
@@ -277,12 +302,15 @@ def build_mesh_accel(state):
277302
278303 # Build an AS over the triangles.
279304 # We use un-indexed triangles so we can explode the sphere per triangle.
280- build_input = ox .BuildInputTriangleArray (state .d_temp_vertices , flags = [ox .GeometryFlags .NONE ])
281- state .static_gas = ox .AccelerationStructure (state .ctx , build_input ,
305+ state .gas_build_input = ox .BuildInputTriangleArray (state .d_temp_vertices , flags = [ox .GeometryFlags .NONE ])
306+ state .static_gas = ox .AccelerationStructure (state .ctx , state .gas_build_input ,
307+ compact = True , allow_update = False , random_vertex_access = True )
308+
309+ state .deforming_gas = ox .AccelerationStructure (state .ctx , state .gas_build_input ,
282310 compact = True , allow_update = True , random_vertex_access = True )
283311
284- state .deforming_gas = state .static_gas
285- state . exploding_gas = state . static_gas
312+ state .exploding_gas = ox . AccelerationStructure ( state .ctx , state . gas_build_input ,
313+ compact = True , allow_update = True , random_vertex_access = True )
286314
287315 traversables = [state .static_gas , state .static_gas ,
288316 state .deforming_gas , state .exploding_gas ]
@@ -292,20 +320,25 @@ def build_mesh_accel(state):
292320 sbt_offset = i , transform = g_instances [i ])
293321 instances .append (instance )
294322
295- build_input = ox .BuildInputInstanceArray (instances )
296- state .ias = ox .AccelerationStructure (context = state .ctx , build_inputs = build_input ,
297- compact = True , allow_update = True )
323+ state . ias_build_input = ox .BuildInputInstanceArray (instances )
324+ state .ias = ox .AccelerationStructure (context = state .ctx ,
325+ build_inputs = state . ias_build_input , compact = True , allow_update = True )
298326 state .params .trav_handle = state .ias .handle
299327
300328
301329def create_module (state ):
330+ if DEBUG :
331+ exception_flags = ox .ExceptionFlags .DEBUG | ox .ExceptionFlags .TRACE_DEPTH | ox .ExceptionFlags .STACK_OVERFLOW ,
332+ else :
333+ exception_flags = ox .ExceptionFlags .NONE
334+
302335 pipeline_opts = ox .PipelineCompileOptions (
303336 uses_motion_blur = False ,
304337 uses_primitive_type_flags = ox .PrimitiveTypeFlags .TRIANGLE ,
305338 traversable_graph_flags = ox .TraversableGraphFlags .ALLOW_SINGLE_LEVEL_INSTANCING ,
339+ exception_flags = exception_flags ,
306340 num_payload_values = 3 ,
307341 num_attribute_values = 2 ,
308- exception_flags = ox .ExceptionFlags .DEBUG | ox .ExceptionFlags .TRACE_DEPTH | ox .ExceptionFlags .STACK_OVERFLOW ,
309342 pipeline_launch_params_variable_name = "params" )
310343
311344 compile_opts = ox .ModuleCompileOptions (
@@ -328,7 +361,7 @@ def create_pipeline(state):
328361 program_grps = [state .raygen_grp , state .miss_grp , state .hit_grp ]
329362
330363 link_opts = ox .PipelineLinkOptions (max_trace_depth = 1 ,
331- debug_level = ox .CompileDebugLevel .FULL )
364+ debug_level = ox .CompileDebugLevel .LINEINFO )
332365
333366 pipeline = ox .Pipeline (state .ctx ,
334367 compile_options = state .pipeline_opts ,
@@ -410,7 +443,7 @@ def create_sbt(state):
410443
411444 state .time = glfw .get_time () - tstart
412445
413- # update_mesh_accel(state)
446+ update_mesh_accel (state )
414447
415448 update_state (output_buffer , state )
416449
0 commit comments