Search code examples
overloadingrenderscript

Can one overload a RenderScript kernel?


I would like to overload a RenderScript kernel:

/* donothing.rs */

uchar4 __attribute__((kernel, overloadable)) root (uchar4 in) {
  return in;
}

float4 __attribute__((kernel, overloadable)) root (float4 in) {
  return in;
}

However, this generates identically-named Java methods:

// ScriptC_donothing.java:95
public void forEach_root(Allocation ain, Allocation aout, Script.LaunchOptions sc) {
    // check ain
    if (!ain.getType().getElement().isCompatible(__U8_4)) {
        throw new RSRuntimeException("Type mismatch with U8_4!");
    }
    ...

// ScriptC_donothing.java:225
public void forEach_root(Allocation ain, Allocation aout, Script.LaunchOptions sc) {
    // check ain
    if (!ain.getType().getElement().isCompatible(__F32_4)) {
        throw new RSRuntimeException("Type mismatch with F32_4!");
    }
    ...

Is there a way to write the kernels so that overloading works? The usage I expected was:

// DoNothingActivity.java

mInAllocation = Allocation.createFromBitmap(mRS, ...
mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType());

mScript = new ScriptC_donothing(mRS);
mScript.forEach_root(mInAllocation, mOutAllocation);
// calls uchar4 kernel

Solution

  • There's no way to overload kernel names right now. We're investing some ways to associate more type information with an allocation in the future, though; we'll keep this use case in mind.