Virtual function in OptiX 7.0

Hi developers, I’m a newbie in OptiX and have a simple question.

I implemented simple virtual functions in a class, then derive it from another class like codes below.
It perfectly works in pure CUDA code (specifically, the simplePrintf example in official CUDA examples), but does not work in OptiX code (the optixPathTracer example) with the error:

Caught exception: OPTIX_ERROR_INVALID_PTX: Optix call 'optixModuleCreateFromPTX( state.context, &module_compile_options, &state.pipeline_compile_options, ptx.c_str(), ptx.size(), log, &sizeof_log, &state.ptx_module )' failed: /home/hchoi/NVIDIA-OptiX-SDK-7.0.0-linux64/SDK/optixPathTracer/optixPathTracer.cpp:700) Log: COMPILE ERROR: Invalid PTX input: ptx2llvm-module-001: error: Failed to translate PTX input to LLVM Virtual function calls are not supported in class "Base" (vtable symbol name: "_ZTV4Base"). Virtual function calls are not supported in class "Derived" (vtable symbol name: "_ZTV7Derived").


class Base {
    __device__ virtual ~Base() {}
    virtual void func() {
        printf("Base: %d\n", a);
    int a = 10;

class Derived : public Base {
    __device__ virtual ~Derived() {}
    void func() {
        a = 20;
        printf("Derived: %d\n", a);

extern "C" __global__ void __raygen__rg()
    Base b;

    Derived d;

    Base* b2 = &d;

I just wonder whether it is not supported in OptiX because there is no mention of the virtual function in documents.

Thank you

OptiX doesn’t support virtual function pointers, just as the error message says.

See these related threads.

In OptiX 6 and before, function tables would need to be implemented manually with buffers of bindless callable program IDs

In OptiX 7 that would need to be done with direct callables or continuation callables inside the shader binding table (SBT).

Examples for OptiX 7 direct callable programs can be found here:

1 Like