HDK
|
The CVEX library provides a means of calling VEX from C++ code.
This allows users to alter algorithms used by C++ code and makes it easy to access complicated functions (such as noise, texture maps or volume primitives) through the simple VEX interface.
The CVEX_Context class is used to handle the interface between C++ and VEX. The CVEX_Context handles
VEX has a run-time optimizer which optimizes the loaded code based on the parameters passed to the code. This means that if the parameters to the code change, the function needs to be reloaded.
On the other hand, if the parameters are constant, the same CVEX_Context may be re-used to perform evaluation on different input data. For example
VEX functions have parameter declarations. Each of these parameters has a name and a type. Parameters which are flagged as export
are available with in HDK_CVEX_Outputs.
When building a CVEX interface, you have the opportunity to override VEX function parameters with your own values. This allows you to pass information from your code to the user's VEX function.
Prior to calling CVEX_Context::load(), your code must declare all possible C++ parameter names/types that you want to make available to the VEX function. This is done by calling CVEX_Context::addInput().
The easiest way to add an input is to declare the input by name, type and value. For example:
If the user VEX function has a vector P
parameter, then its value will be overridden by the vector array you've passed in.
In some cases, it can be expensive to compute the input parameter values. It's possible to defer the assignment of the value until after the function has been loaded. You still need to declare the variable before calling CVEX_Context::load(), but after the function has been loaded, you can call CVEX_Context::findInput() to see if the input actually exists in the user function.
If you declare a parameter but neglect to assign data to the parameter, CVEX_Context::run() will fail to run.
String parameters require special handling. For these parameters, you need to pass in a UT_StringArray of values. If the array should contain a single string for uniform
parameters, or a value per array entry for varying
parameters.
You can read the value of every parameter flagged as an export
parameter can be read after you call CVEX_Context::run(). However, to do this, you need to set the variable data to a local buffer before running the VEX code.
You can find export parameters by name (CVEX_Context::findOutput()) or you can get a list of all outputs (CVEX_Context::getOutputList()). These methods should be called after the VEX function has been loaded. At this time, you can also query whether the parameter will be uniform
or varying
by calling CVEX_Value::isVarying().
uniform
data buffer to a varying output, this may result in a run-time crash.CVEX will not automatically run VEX code across multiple processors. However, VEX itself is thread-safe within a single context. In order to multi-thread CVEX, you need to have a separate CVEX_Context per-thread.
A very simple example can be seen at CVEX/simple.C (with corresponding VEX code CVEX/simple.vfl).
A more complicated example can be seen at CVEX/cvexsample.C (with corresponding VEX code CVEX/test.vfl).