Correct ways to declare Variables from Host API Optix5.1

I have very basic question regarding the right way to declare variables form using Host API. For the sake of enquiry lets look into the optixHello sample distributed with Optix5.1 version. This sample is very simple in that it doesn’t really do any raytracing. It uses a program to define the ray_generation_program (draw_color.cu). In this CUDA file I see three variables 1)result_buffer 2)draw_color 3)launch_index. As launch_index is predefined there is to problem understanding it and there is not need to declare it’s value from Host side.

 `result_buffer` is a buffer in which the output of the whole simulation is stored, in optixHello.cpp it is declared using rtContextDeclareVariable(). Where as `draw_color` is declared using rtProgramDeclareVariable(). So my confusion is when is appropriate to use which function? How does all this figure out when I am using C++ wrapper. Like if have do the equivalent of rtProgramDeclareVariable() do I to query the variable from the program handle, some this like this?
prog["draw_color"]->set3fv(...)

The programming guide doesn’t really mention when to use which declaration function.

Thanks :)

You could debug into some of these single lines inside the OptiX SDK examples and you’ll see what C API calls the C++ wrappers do underneath.
Or directly look into the C++ wrapper implementation inside OptiX SDK 5.1.0\include\optixu\optixpp_namespace.h

You declare what variable is what type inside the device code!
The host code must match that declaration or the OptiX validation step will fail during a launch.
(This has been always like this since OptiX 1.0.)

In the C++ wrappers, the operator queries if a variable of that name is declared already and when not, declares it.
The member functions to set values on the VariableObj class, e.g. like “setFloat” have overloads for all possible rtVariableSet*() C API Functions and that defines the type of the variable on host side.
[url]http://raytracing-docs.nvidia.com/optix/guide/index.html#optixpp#10036[/url]

Thanks for clearing up the c++ part of my question. Can also point me in a direction where I can find out the right way to declare a variable, as in when is it appropriate to use which rt*DeclareVariable() function. Like the in the above case where rtProgramDeclareVariable() is used.

There are two things to consider there.

First, which OptiX objects can hold variables at all.
This defines the scope of the variable and which rt*DeclareVariable function you can use.
Please have a look through the OptiX API Reference pdf document in your installation for that.
Or look into the C++ wrappers for “declareVariable”

Then there is an order of scopes in which variables are evaluated in OptiX.
Not all OptiX program domains have access to all variable scopes.
Means you can actually shadow variables with that scope precedence, like defining a default in the Program but the actual value in the Material. Since only one of these is used in the end it’s recommended to define variables only at one scope for performance reasons though.
[url]http://raytracing-docs.nvidia.com/optix/guide/index.html#programs#4130[/url]

If you’re new to OptiX, I would highly recommend to watch my GTC 2018 presentation “An Introduction to NVIDIA OptiX” and work through the source code of the accompanying examples and do a source code diff between each two projects. All links for that can be found in this sticky post:
[url]https://devtalk.nvidia.com/default/topic/998546/optix/optix-advanced-samples-on-github/[/url]