|
| 1 | +import optix as ox |
| 2 | +import cupy as cp |
| 3 | +import numpy as np |
| 4 | +from PIL import Image, ImageOps |
| 5 | +import logging |
| 6 | +import sys |
| 7 | +logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) |
| 8 | +log = logging.getLogger() |
| 9 | + |
| 10 | +def create_module(ctx, pipeline_opts): |
| 11 | + compile_opts = ox.ModuleCompileOptions(debug_level=ox.CompileDebugLevel.LINEINFO) |
| 12 | + module = ox.Module(ctx, 'cuda/hello.cu', compile_opts, pipeline_opts) |
| 13 | + return module |
| 14 | + |
| 15 | + |
| 16 | +def create_program_groups(ctx, module): |
| 17 | + raygen_grp = ox.ProgramGroup.create_raygen(ctx, module, "__raygen__draw_solid_color") |
| 18 | + miss_grp = ox.ProgramGroup.create_miss(ctx, None, None) |
| 19 | + return raygen_grp, miss_grp |
| 20 | + |
| 21 | + |
| 22 | +def create_pipeline(ctx, program_grps, pipeline_options): |
| 23 | + link_opts = ox.PipelineLinkOptions(max_trace_depth=0, debug_level=ox.CompileDebugLevel.FULL) |
| 24 | + |
| 25 | + pipeline = ox.Pipeline(ctx, compile_options=pipeline_options, link_options=link_opts, |
| 26 | + program_groups=program_grps, max_traversable_graph_depth=1) |
| 27 | + |
| 28 | + pipeline.compute_stack_sizes(0, # max_trace_depth |
| 29 | + 0, # max_cc_depth |
| 30 | + 0) # max_dc_depth |
| 31 | + return pipeline |
| 32 | + |
| 33 | + |
| 34 | +def create_sbt(program_grps): |
| 35 | + raygen_grp, miss_grp = program_grps |
| 36 | + |
| 37 | + raygen_sbt = ox.SbtRecord(raygen_grp, names=('r','g','b'), formats=('f4',)*3) |
| 38 | + raygen_sbt['r'] = 0.462 |
| 39 | + raygen_sbt['g'] = 0.725 |
| 40 | + raygen_sbt['b'] = 0.0 |
| 41 | + |
| 42 | + miss_sbt = ox.SbtRecord(miss_grp) |
| 43 | + |
| 44 | + sbt = ox.ShaderBindingTable(raygen_record=raygen_sbt, miss_records=miss_sbt) |
| 45 | + |
| 46 | + return sbt |
| 47 | + |
| 48 | + |
| 49 | +def launch_pipeline(pipeline : ox.Pipeline, sbt): |
| 50 | + width = 512 |
| 51 | + height = 384 |
| 52 | + |
| 53 | + output_image = cp.empty(shape=(height,width,4), dtype=np.uint8) |
| 54 | + |
| 55 | + params = ox.LaunchParamsRecord(names=('image', 'image_width'), |
| 56 | + formats=('u8', 'u4')) |
| 57 | + params['image'] = output_image.data.ptr |
| 58 | + params['image_width'] = width |
| 59 | + |
| 60 | + stream = cp.cuda.Stream() |
| 61 | + |
| 62 | + pipeline.launch(sbt, dimensions=(width, height), params=params, stream=stream) |
| 63 | + |
| 64 | + stream.synchronize() |
| 65 | + |
| 66 | + return cp.asnumpy(output_image) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + logger = ox.Logger(log) |
| 71 | + ctx = ox.DeviceContext(validation_mode=True, log_callback_function=logger, log_callback_level=4) |
| 72 | + ctx.cache_enabled = False |
| 73 | + |
| 74 | + tgf = ox.TraversableGraphFlags |
| 75 | + pipeline_options = ox.PipelineCompileOptions(traversable_graph_flags=tgf.ALLOW_SINGLE_LEVEL_INSTANCING | tgf.ALLOW_SINGLE_GAS, |
| 76 | + num_payload_values=0, |
| 77 | + num_attribute_values=0, |
| 78 | + exception_flags=ox.ExceptionFlags.NONE, |
| 79 | + pipeline_launch_params_variable_name="params") |
| 80 | + |
| 81 | + module = create_module(ctx, pipeline_options) |
| 82 | + program_grps = create_program_groups(ctx, module) |
| 83 | + pipeline = create_pipeline(ctx, program_grps, pipeline_options) |
| 84 | + sbt = create_sbt(program_grps) |
| 85 | + img = launch_pipeline(pipeline, sbt) |
| 86 | + Image.fromarray(img, 'RGBA').show() |
0 commit comments