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").

[optixPathTracer.cu]

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

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

extern "C" __global__ void __raygen__rg()
{
    Base b;
    b.func();

    Derived d;
    d.func();

    Base* b2 = &d;
    b2->func();
 }

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.
https://forums.developer.nvidia.com/t/virtual-methods-problem/32386
https://forums.developer.nvidia.com/t/failed-to-translate-ptx-input-to-llvm/71806

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:
https://forums.developer.nvidia.com/t/optix-advanced-samples-on-github/48410

1 Like