Search code examples
optix

How to rtPrint with Optix Context Wrapper


This question is meant to help beginners of NVIDIA OptiX (much like myself)


What's Happening

When working with the OptiX compiled examples (delivered with the installation of OptiX), I am trying to print to the console from one of the computer kernels and I keep getting these errors:

error: cannot convert ‘optix::Context {aka optix::Handle<optix::ContextObj>}’ to ‘RTcontext’ for argument ‘1’ to ‘RTresult rtContextSetPrintEnabled(RTcontext, int)’
error: cannot convert ‘optix::Context {aka optix::Handle<optix::ContextObj>}’ to ‘RTcontext’ for argument ‘1’ to ‘RTresult rtContextSetPrintBufferSize(RTcontext, RTsize)’

Attempted Solution

Inside the createContext() function, under where the context is created, I added lines of code to turn on debug printing. The lines of code I added are:

rtContextSetPrintEnabled(context, 1);
rtContextSetPrintBufferSize(context, 4096);

These are the two lines that produce the above error. I added these lines of code after the instantiation of the context object, provided by the code below - from the original nvidia example:

context = Context::create();
context->setRayTypeCount( 2 );
context->setEntryPointCount( 1 );
context->setStackSize( 2800 );

So, complete code that breaks looks like this:

// Set up context
context = Context::create();
context->setRayTypeCount( 2 );
context->setEntryPointCount( 1 );
context->setStackSize( 2800 );

// Setup debug printing
rtContextSetPrintEnabled(context, 1);
rtContextSetPrintBufferSize(context, 4096);

Some background information

I am trying to modify the optixWhitted project, working on a CentOS computer using the NSight edition of Eclipse.


Question

While using the optixWhitted code, and trying to maintain the code style and object usage already laid out... How do I solve this?


Solution

  • After looking further at the error and the original instantiation of the context, it turns out that the optixWhitted example uses a wrapper class to handle the rtContext object. The two objects are different classes and in doing some digging around, I discovered that NVIDIA has included the ContextObj class as a convenience wrapper for the underlying rtContext. This ContextObj class has very similar functions to the rtContext and functions outlined in Chapter 3 of the OptiX 5.1 Programming Guide.

    Looking through the ContextObj class, you will find analogous functions for setting the rtPrintf settings: OptiX ContextObj Wrapper Class.

    Specifically you'll find these functions:

    • setPrintEnabled(bool)
    • setPrintBufferSize(uint)

    Final Working Code

    This is the final working code, that uses the ContextObj wrapper class already present and in use inside the optixWhitted tutorial.

    // Set up context
    context = Context::create();
    context->setRayTypeCount( 2 );
    context->setEntryPointCount( 1 );
    context->setStackSize( 2800 );
    
    // Set Output Debugging via rtPrintf
    context->setPrintEnabled(1);
    context->setPrintBufferSize(4096);