Blink API
|
A Blink Kernel is created from a ProgramSource object. A ProgramSource object is constructed from a string or character array containing the source code for a Blink kernel. For example, the following character array contains the source for a kernel that applies a gain operation to its input image and writes the result to its output:
To construct a ProgramSource object from this source string, the code needed is simply:
Once you have the ProgramSource, this can be used to construct a Blink Kernel.
The constructor for a Kernel takes the following arguments:
A target ComputeDevice must be passed in when constructing a kernel. The static functions Blink::ComputeDevice::GetCurrentCPUDevice() and Blink::ComputeDevice::GetCurrentGPUDevice() give access to the currently-selected CPU device and GPU device respectively. At run-time, whenever a kernel is constructed, Blink will generate the code required to run the kernel on the target device. Since this target code is generated at on-the-fly, the decision on which device to use can also be made at run-time - for example, you might want to expose a parameter in your plug-in's UI that allows the user to choose whether or not to use the GPU for processing. You could do this by having a Bool_knob that stores its value in the local variable _useGPU. You could then choose a ComputeDevice at run-time with the following code:
A std::vector containing the Blink::Images to be used by the Kernel must be passed in when a kernel is constructed. Blink::Images should be added to the vector in the order in which they appear in the kernel. For example, suppose you have images called "gainInput" and "gainOutput" that you wish to use as the "src" and "dst" Images respectively for the gain_kernel above. You could set up a vector of Images as follows:
For more information on constructing Blink::Images, or fetching them from Nuke, see section Getting Data To and From Blink Images.
We are now ready to construct an instance of the Gain kernel, with the following line of code:
If your kernel has any parameters, now is the time to set values for them, after creating the kernel and before you run it. The Gain kernel has a single parameter, "Gain". Suppose the value to be passed in is stored in the local variable _gain, a floating-point value which can be set by a Float_knob in your plug-in's UI. Parameter values are set by passing the external parameter name as the first argument and the value as the second argument to the setParamValue() function:
The external name for a kernel parameter can be set using the defineParam() function inside a kernel, as in the Gain kernel above. The first argument to this function is the internal name for the parameter, the second is the desired external name and the third is the default value it should take. If defineParam() is not called inside the kernel's define() function, the external name for the parameter will be the same as its internal one (this would be "multiply" in the Gain kernel above).
A kernel is run by calling its iterate() function.
This will execute the kernel at every position in the iteration space. These kernel executions happen in parallel for different positions in the iteration space - typically you will get one kernel execution per core running on the CPU at one time, and many hundreds of kernel executions running simultaneously on the GPU.
When the iteration function is called as above, with no parameters, the iteration space will be the bounds of the last image passed in to the kernel. In the example above, this would be the bounds of the gainOutput image (see Setting Up the Images to Process). You can also set a different iteration space by passing in a Blink::IterationController to the iterate function. An IterationController is constructed with a Blink::Rect that defines the bounds for the iteration space.
You can call iterate() as many times as you like on the same kernel. You can also change parameter values in between calls to iterate(). However, if you want to change the images used by the kernel or the ComputeDevice it is run on, you must make a new instance of the kernel and use that instead. This is because the code required to run the kernel on the target device is generated when the kernel is constructed. Different code will be generated for different target devices, and the generated code will also depend on the images passed in - for example, it might differ according to the number of components in each of the images. Of course, you will also need to construct a new instance of the kernel if you want to change what the kernel does (the Program Source - see Loading a Kernel).
You can also get parameter values back from a kernel. You can use this to check the values or, more importantly, to read values that have been calculated by ImageReductionKernels. For example, suppose we have created and set up a Mean kernel as in the Example of a Reduction Kernel. This inherits a parameter, "mean", from the MeanReductionData structure. After the kernel has been run using iterate(), you can read back the value of "mean" using the getParamValue() function.
©2021 The Foundry Visionmongers, Ltd. All Rights Reserved. |