Skip to content

Commit 57977bc

Browse files
authored
Merge pull request #5 from mortacious/devel
Update to OptiX 7.4
2 parents d55fc17 + 3d78e8c commit 57977bc

17 files changed

Lines changed: 765 additions & 174 deletions

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python-OptiX
22

3-
Python wrapper for the OptiX 7.3 raytracing engine.
3+
Python wrapper for the OptiX 7 raytracing engine.
44

55
Python-OptiX wraps the OptiX C++ API using Cython and provides a simplified
66
interface to the original C-like API using mainly the
@@ -12,16 +12,18 @@ Only Linux is supported at the moment.
1212

1313
### OptiX Versions
1414

15-
Python-OptiX currently only supports the most recent (7.3.0) release of OptiX
15+
Python-OptiX currently supports the OptiX releases 7.3.0 and 7.4.0
1616

1717
## Installation
1818

1919
### Dependencies
2020

2121
Install a recent version of the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads)
22-
and the [OptiX 7.3.0 SDK](https://developer.nvidia.com/optix/downloads/7.3.0/linux64)
22+
and the [OptiX 7.4.0 SDK](https://developer.nvidia.com/optix/downloads/7.4.0/linux64-x86_64)
2323

24-
Make sure the CUDA header files are installed as well
24+
Note: The older [OptiX 7.3.0 SDK](https://developer.nvidia.com/optix/downloads/7.4.0/linux64-x86_64) version is supported as well.
25+
26+
Make sure the CUDA header files are installed as well.
2527

2628
Add the locations of CUDA and OptiX to the system `PATH` variable if necessary.
2729

examples/compile_with_tasks.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import concurrent.futures
2+
import optix as ox
3+
import argparse
4+
import logging
5+
import sys
6+
from concurrent.futures import ThreadPoolExecutor
7+
import time
8+
9+
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
10+
log = logging.getLogger()
11+
12+
13+
if __name__ == "__main__":
14+
if ox.optix_version()[1] < 4:
15+
raise NotImplementedError("Parallel tasks are not implemented in optix versions < 7.3.")
16+
17+
parser = argparse.ArgumentParser("Compile OptiX modules using parallel tasks")
18+
parser.add_argument('file', nargs=1, help="The input file (.ptx or .cu) to compile")
19+
parser.add_argument('-na', '--num-attributes', type=int, default=2, required=False,
20+
help="Number of attribute values (up to 8, default 2)")
21+
parser.add_argument('-npv', '--num-payload-values', type=int, default=2, required=False,
22+
help=f"Number of payload values (up to {ox.PipelineCompileOptions.DEFAULT_MAX_PAYLOAD_VALUE_COUNT}, default 2)")
23+
parser.add_argument('-npt', '--num-payload-types', type=int, default=1, required=False,
24+
help=f"Number of payload types (up to {ox.ModuleCompileOptions.DEFAULT_MAX_PAYLOAD_TYPE_COUNT}, default 1)")
25+
parser.add_argument('-ni', '--num-iters', type=int, default=1, required=False,
26+
help="Number of iterations to compile. > 1 disables disk cache (default 1)")
27+
parser.add_argument('-dt', '--disable-tasks', action='store_true', required=False,
28+
help="Disable compilation with tasks (default enabled)")
29+
parser.add_argument('-nt', '--num-threads', type=int, default=1, required=False,
30+
help="Number of threads (default 1)")
31+
parser.add_argument('-mt', '--max-num-tasks', type=int, default=2, required=False,
32+
help="Maximum number of additional tasks (default 2)")
33+
34+
args = parser.parse_args()
35+
36+
logger = ox.Logger(log)
37+
ctx = ox.DeviceContext(validation_mode=True, log_callback_function=logger, log_callback_level=3)
38+
39+
if args.num_iters > 1:
40+
ctx.cache_enabled = False
41+
42+
# compile the file content to ptx in case a .cu file is given
43+
ptx = ox.Module.compile_cuda_ptx(args.file[0])
44+
45+
pipeline_options = ox.PipelineCompileOptions(num_payload_values=0,
46+
num_attribute_values=args.num_attributes)
47+
48+
payload_semantics = [ox.PayloadSemantics.DEFAULT] * args.num_payload_values
49+
payload_types = [payload_semantics] * args.num_payload_types
50+
51+
compile_opts = ox.ModuleCompileOptions(payload_types=payload_types)
52+
53+
use_tasks = not args.disable_tasks
54+
55+
if use_tasks:
56+
tic = time.time()
57+
with ThreadPoolExecutor(max_workers=args.num_threads) as executor:
58+
for i in range(args.num_iters):
59+
module, task = ox.Module.create_as_task(ctx, ptx, module_compile_options=compile_opts, pipeline_compile_options=pipeline_options)
60+
task_futures = {executor.submit(task.execute, args.max_num_tasks)}
61+
while task_futures:
62+
done, not_done = concurrent.futures.wait(task_futures, timeout=0.25, return_when=concurrent.futures.FIRST_COMPLETED)
63+
for future in done:
64+
new_tasks = future.result()
65+
if len(new_tasks) > 0:
66+
task_futures.update({executor.submit(t.execute, args.max_num_tasks) for t in new_tasks})
67+
task_futures.remove(future)
68+
69+
# wait for the executor to finish here
70+
print("Overall run time with tasks", time.time()-tic)
71+
else:
72+
tic = time.time()
73+
for i in range(args.num_iters):
74+
module = ox.Module(ctx, ptx, module_compile_options=compile_opts, pipeline_compile_options=pipeline_options)
75+
print("Overall run time without tasks", time.time()-tic)

examples/dynamic_geometry.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ def display_subframe(output_buffer, gl_display, window):
220220
framebuf_res_x, framebuf_res_y,
221221
output_buffer.get_pbo() )
222222

223+
223224
def init_camera_state(state):
224225
camera = state.camera
225226
camera.eye = (0, 1, -20)
@@ -233,12 +234,14 @@ def init_camera_state(state):
233234
trackball.set_reference_frame([1,0,0], [0,0,1], [0,1,0])
234235
trackball.reinitialize_orientation_from_camera()
235236

237+
236238
def create_context(state):
237239
logger = ox.Logger(log)
238240
ctx = ox.DeviceContext(validation_mode=False, log_callback_function=logger, log_callback_level=4)
239241
ctx.cache_enabled = False
240242
state.ctx = ctx
241243

244+
242245
def generate_animated_vertices(out_vertices, animation_mode, time, width, height):
243246
threads_per_block = 128
244247
num_blocks = (width*height + threads_per_block - 1) // threads_per_block
@@ -251,12 +254,13 @@ def generate_animated_vertices(out_vertices, animation_mode, time, width, height
251254
def launch_generate_animated_vertices(state, animation_mode):
252255
generate_animated_vertices(state.d_temp_vertices, animation_mode, state.time, g_tessellation_resolution, g_tessellation_resolution)
253256

257+
254258
def update_mesh_accel(state):
255259
# first sphere is static
256260

257261
# second sphere moves by updating its transform matrix
258-
transform = state.ias_build_input.get_transform_view(1)
259-
transform[1,-1] = np.sin(4*state.time)
262+
transform = state.ias_build_input.view_instance_transform(1)
263+
transform[1, -1] = np.sin(4*state.time)
260264

261265
# third sphere deforms
262266
launch_generate_animated_vertices(state, AnimationMode.DEFORM)
@@ -270,13 +274,14 @@ def update_mesh_accel(state):
270274
state.last_exploding_sphere_rebuild_time = state.time
271275
state.exploding_gas = ox.AccelerationStructure(state.ctx, state.gas_build_input,
272276
compact=True, allow_update=True, random_vertex_access=True)
273-
state.ias_build_input.instances[3].update_traversable(state.exploding_gas)
277+
state.ias_build_input[3].traversable = state.exploding_gas
274278
state.ias_build_input.update_instance(3)
275279
else:
276280
state.exploding_gas.update(state.gas_build_input)
277281

278282
state.ias.update(state.ias_build_input)
279283

284+
280285
def build_vertex_generation_kernel(state):
281286
cuda_source = os.path.join(script_dir, 'cuda', 'dynamic_geometry_vertex_generation.cu')
282287
example_include_path = os.path.dirname(cuda_source)
@@ -289,6 +294,7 @@ def build_vertex_generation_kernel(state):
289294
state.generate_vertices_kernel = cp.RawKernel(code=code, backend='nvrtc',
290295
options=build_flags, name='generate_vertices')
291296

297+
292298
def build_mesh_accel(state):
293299
# Allocate temporary space for vertex generation.
294300
# The same memory space is reused for generating the deformed and exploding vertices before updates.
@@ -335,9 +341,10 @@ def create_module(state):
335341
else:
336342
exception_flags=ox.ExceptionFlags.NONE
337343

344+
print("Triangle value", ox.PrimitiveTypeFlags.TRIANGLE.value)
338345
pipeline_opts = ox.PipelineCompileOptions(
339346
uses_motion_blur=False,
340-
uses_primitive_type_flags = ox.PrimitiveTypeFlags.TRIANGLE,
347+
uses_primitive_type_flags =ox.PrimitiveTypeFlags.TRIANGLE,
341348
traversable_graph_flags=ox.TraversableGraphFlags.ALLOW_SINGLE_LEVEL_INSTANCING,
342349
exception_flags=exception_flags,
343350
num_payload_values=3,
@@ -347,7 +354,7 @@ def create_module(state):
347354
compile_opts = ox.ModuleCompileOptions(
348355
max_register_count=ox.ModuleCompileOptions.DEFAULT_MAX_REGISTER_COUNT,
349356
opt_level=ox.CompileOptimizationLevel.DEFAULT,
350-
debug_level=ox.CompileDebugLevel.LINEINFO)
357+
debug_level=ox.CompileDebugLevel.MODERATE)
351358

352359
cuda_source = os.path.join(script_dir, 'cuda', 'dynamic_geometry.cu')
353360
state.module = ox.Module(state.ctx, cuda_source, compile_opts, pipeline_opts)
@@ -364,7 +371,7 @@ def create_pipeline(state):
364371
program_grps = [state.raygen_grp, state.miss_grp, state.hit_grp]
365372

366373
link_opts = ox.PipelineLinkOptions(max_trace_depth=1,
367-
debug_level=ox.CompileDebugLevel.LINEINFO)
374+
debug_level=ox.CompileDebugLevel.MODERATE)
368375

369376
pipeline = ox.Pipeline(state.ctx,
370377
compile_options=state.pipeline_opts,

examples/dynamic_materials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def create_module(state):
194194
compile_opts = ox.ModuleCompileOptions(
195195
max_register_count=ox.ModuleCompileOptions.DEFAULT_MAX_REGISTER_COUNT,
196196
opt_level=ox.CompileOptimizationLevel.DEFAULT,
197-
debug_level=ox.CompileDebugLevel.LINEINFO)
197+
debug_level=ox.CompileDebugLevel.MODERATE)
198198

199199
source = os.path.join(script_dir, 'cuda', 'dynamic_materials.cu')
200200
state.module = ox.Module(state.ctx, source, compile_opts, pipeline_opts)

examples/hello.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import os, sys, logging
2-
31
import optix as ox
42
import cupy as cp
53
import numpy as np
6-
74
from PIL import Image, ImageOps
8-
9-
script_dir = os.path.dirname(os.path.abspath(__file__))
10-
5+
import logging
6+
import sys
117
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
128
log = logging.getLogger()
139

1410
def create_module(ctx, pipeline_opts):
15-
compile_opts = ox.ModuleCompileOptions(debug_level=ox.CompileDebugLevel.LINEINFO)
16-
source = os.path.join(script_dir, 'cuda', 'hello.cu')
17-
module = ox.Module(ctx, source, compile_opts, pipeline_opts)
11+
compile_opts = ox.ModuleCompileOptions(debug_level=ox.CompileDebugLevel.FULL, opt_level=ox.CompileOptimizationLevel.LEVEL_0)
12+
module = ox.Module(ctx, 'cuda/hello.cu', compile_opts, pipeline_opts)
1813
return module
1914

2015

examples/spheres.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import os, sys, logging
2-
1+
import optix as ox
32
import cupy as cp
43
import numpy as np
5-
import optix as ox
6-
74
from PIL import Image, ImageOps
8-
9-
script_dir = os.path.dirname(os.path.abspath(__file__))
10-
5+
import logging
6+
import sys
117
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
128
log = logging.getLogger()
139
img_size = (1024, 768)
@@ -26,9 +22,8 @@ def create_acceleration_structure(ctx, bboxes):
2622

2723

2824
def create_module(ctx, pipeline_opts):
29-
compile_opts = ox.ModuleCompileOptions(debug_level=ox.CompileDebugLevel.LINEINFO)
30-
source = os.path.join(script_dir, 'cuda', 'spheres.cu')
31-
module = ox.Module(ctx, source, compile_opts, pipeline_opts)
25+
compile_opts = ox.ModuleCompileOptions(debug_level=ox.CompileDebugLevel.FULL, opt_level=ox.CompileOptimizationLevel.LEVEL_0)
26+
module = ox.Module(ctx, 'cuda/spheres.cu', compile_opts, pipeline_opts)
3227
return module
3328

3429

examples/triangle.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import os
2-
1+
import optix as ox
32
import cupy as cp
43
import numpy as np
5-
import optix as ox
6-
74
from PIL import Image, ImageOps
85

9-
script_dir = os.path.dirname(os.path.abspath(__file__))
10-
116
img_size = (1024, 768)
127

138
# use a regular function for logging
@@ -23,9 +18,8 @@ def create_acceleration_structure(ctx, vertices):
2318

2419

2520
def create_module(ctx, pipeline_opts):
26-
compile_opts = ox.ModuleCompileOptions(debug_level=ox.CompileDebugLevel.LINEINFO)
27-
source = os.path.join(script_dir, 'cuda', 'triangle.cu')
28-
module = ox.Module(ctx, source, compile_opts, pipeline_opts)
21+
compile_opts = ox.ModuleCompileOptions(debug_level=ox.CompileDebugLevel.FULL, opt_level=ox.CompileOptimizationLevel.LEVEL_0)
22+
module = ox.Module(ctx, 'cuda/triangle.cu', compile_opts, pipeline_opts)
2923
return module
3024

3125

optix/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from .context import DeviceContext
1+
from .context import DeviceContext, optix_version
22
from .build import *
3-
from .module import Module, ModuleCompileOptions, CompileOptimizationLevel, CompileDebugLevel
3+
from .module import Module, ModuleCompileOptions, CompileOptimizationLevel, CompileDebugLevel, PayloadSemantics, Task
44
from .program_group import ProgramGroup
55
from .struct import SbtRecord, LaunchParamsRecord
66
from .shader_binding_table import ShaderBindingTable

0 commit comments

Comments
 (0)