The cuda Lua Table#
Every function and constant a .cu.lua launch script can use. For what a launch script is
and when it runs, refer to The Lua Launch Script.
Accepted source assets: .cu, compiled with NVRTC on load, and the pre-compiled .ptx,
.cubin and .fatbin. Extensions are matched case-insensitively. That is the complete
list.
The CUDA headers are reachable from a .cu source through #include, for example
#include <cuda_fp16.h>. They come from one directory, the CUDA installation SPG finds:
CUDA_PATH or CUDA_HOME if either is set in the environment, otherwise the headers bundled
with the renderer. It is the only include directory the compile is given, so a header sitting
beside the .cu does not resolve. Refer to Compile It Yourself.
Describing Outputs#
Output descriptors state what an output has to be; SPG owns the memory.
Function |
Description |
|---|---|
|
A texture-backed output. Preferred for image data. Note the argument order against height-first shapes. |
|
A buffer-backed output of arbitrary shape, up to 8 dimensions. Use for non-image data. |
|
A buffer filled with zeros, ones, or a stated value. |
cuda.image and cuda.empty take an optional trailing cuda.stateful, which marks the
output persistent so SPG hands the same resource back next frame instead of a fresh one:
TrailKernel.cu.lua, from the runnable stateful node example#function trail(inputs, outputs)
local image = inputs["Image"]
assert(#image.shape == 2, "Input must be a 2D image")
-- The input is HdrColor, linear radiance, so no exact dtype is pinned here.
-- shape is 1-indexed: [1] = height (rows), [2] = width (columns).
local height = image.shape[1]
local width = image.shape[2]
-- The feedback framebuffer, and the only reason the effect exists.
outputs["History"] = cuda.image(width, height, cuda.float4, cuda.stateful)
-- Ordinary outputs: published as AOVs and handed out fresh each frame.
outputs["Live"] = cuda.image(width, height, cuda.uchar4)
outputs["Trail"] = cuda.image(width, height, cuda.uchar4)
return cuda.kernel({
args = {
cuda.int(width), -- -> int width
cuda.int(height), -- -> int height
cuda.float(inputs["decay"]), -- -> float decay
cuda.TextureObject(image), -- -> cudaTextureObject_t inputImage
cuda.SurfaceObject(outputs["History"]), -- -> cudaSurfaceObject_t history
cuda.SurfaceObject(outputs["Live"]), -- -> cudaSurfaceObject_t liveImage
cuda.SurfaceObject(outputs["Trail"]), -- -> cudaSurfaceObject_t trailImage
},
block = { 32, 32 },
grid = { math.ceil(width / 32), math.ceil(height / 32) },
})
end
The Wrappers#
Each wrapper turns a descriptor or a value into the CUDA type the kernel parameter expects.
Function |
Wraps to |
Description |
|---|---|---|
|
|
Read-only, hardware-cached texture access to an input or output resource. |
|
|
Read-write surface access to an output resource. |
|
|
Raw device pointer to an input or output resource. |
|
|
Upload a Lua table of numbers to a GPU device array. |
|
|
Upload a |
|
the matching C scalar |
Scalar kernel argument. |
Every dtype constant can be called as a scalar constructor, for example cuda.half(1.0).
Vector types accept multiple scalar arguments or a single value-input entry, for example
cuda.float3(x, y, z) or cuda.int2(inputs["size"]).
Kernel Launch#
return cuda.kernel({
args = { --[[ ordered kernel arguments ]] },
block = { bx, by },
grid = { gx, gy },
sharedMemSize = 0,
})
Field |
Description |
|---|---|
|
Ordered kernel arguments, matched against the C signature by position. Required. |
|
Threads per block, as |
|
Blocks per launch, in the same form. Optional. |
|
Dynamic shared memory in bytes, the third argument of |
block and grid map directly to the CUDA launch configuration (<<<grid, block>>>).
Left out, they are derived from the first output’s shape: a block of 16 x 16 x 1, or 256 x 1 x 1
for a rank-1 or rank-0 resource, and a grid covering that shape. Value-inputs on the shader prim supply them instead when the script
states neither. Set them explicitly whenever the iteration domain is not the output image, such as
a kernel whose threads walk a buffer. Refer to Control the Launch Geometry.
dtype Constants#
All dtype constants live in the cuda table and can also be called as constructors for
kernel arguments, for example cuda.int(42).
Constant |
C Type |
Size |
|---|---|---|
|
bool |
1 byte |
|
uint8 |
1 byte |
|
uchar4 |
4 bytes |
|
__half, half2, half3, half4 |
2 to 8 bytes |
|
float, float2, float3, float4 |
4 to 16 bytes |
|
int32_t, int2, int3, int4 |
4 to 16 bytes |
|
uint32_t, uint2, uint3, uint4 |
4 to 16 bytes |
|
double, double2, double3, double4 |
8 to 32 bytes |
|
int64_t, uint64_t |
8 bytes |
Cached Computation#
Function |
Description |
|---|---|
|
Call |