0

I intend to process RGB data through RenderScript.

For this I've created Allocations in Java and passing them to RS Kernel function as below:

uchar3 __attribute__((kernel)) invert(uchar3 v_in, uint32_t v_out) {
v_in.r = ...;
v_in.g = ...;
v_in.b = ...;
}

However ideally I would like to work on v_out in similar way i.e. setting values for v_out.r, v_out.g and v_out.b. Currently I can not do this with uint32_t v_out.

Now if I define the above RS kernel as:

uchar3 __attribute__((kernel)) invert(uchar3 v_in, uchar3 v_out) {
...
}

I get below compile time error: error: Unexpected kernel invert() parameter 'v_out' of type 'uchar3 *'

Please suggest how to resolve this.

Compile time:

error: Unexpected kernel invert() parameter 'v_out' of type 'uchar3 *'
Gagan
  • 1,497
  • 1
  • 12
  • 27

2 Answers2

1

you should be defining this as

uchar3 __attribute__((kernel)) invert(uchar3 in);

that function will then be reflected as ScriptC_.forEach_invert(Allocation in, Allocation out). each element in in will be passed to invert, and each value returned from invert will be written to the corresponding location in out.

Tim Murray
  • 2,205
  • 13
  • 13
  • Thanks for your reply. This part works well however I still can't get back this "processed" out data in java. My test code(for 5 elements) looks like this: `mScript.forEach_invert(ain, aout); byte[] output = {0, 0, 0, 0, 0}; aout.copyTo(output);` Am I missing something here? – Gagan Dec 10 '13 at 02:52
  • Yes, I did get the values back in Java. There was a mismatch in byte[] lengths. – Gagan Dec 10 '13 at 05:30
  • Does writing a RS `kernel` function always ensure that the code runs on GPU? How can we check whether a particular RS function (kernel or invokable) runs on GPU or CPU? – Gagan Dec 10 '13 at 19:26
  • No, in fact you can write an RS kernel in such a way that it precludes it from running on the GPU, for example recursive functions. I don't think you can check whether you're running on CPU or GPU beyond obvious performance differences... Or you could attach a profiler and look for any GPU activity? No use in production environment ofc. – chrisvarnz Feb 24 '14 at 13:57
  • some GPUs are capable of recursion. longer term, we're working on better profiler support and more logcat options that spell out why something is running on a particular processor. however, the GPU is not always going to be faster than the CPU or anything like that; the GPU is usually pretty busy with rendering a high-DPI display, CPU and GPU are power/thermal limited, they have the same memory bandwidth, and so on. – Tim Murray Feb 24 '14 at 18:08
1

Define your own local variable of uchar3 type and then populate it before returning it:

uchar3 __attribute__((kernel)) invert(uchar3 in) {
    uchar3 out;
    out.r = ...
    out.g = ...
    out.b = ...
    return out;
}

The compiler is clever enough to notice what you are doing, so there won't be additional copies made of the output items.

Stephen Hines
  • 2,612
  • 1
  • 13
  • 12
  • Thanks for your input. I'm progressing with both these approaches and have updated my finding in comment-section above. – Gagan Dec 10 '13 at 02:53
  • Thanks again for the answer mate. I did get the values back in Java. Now just have to create my custom 24 bit `Element` for my ain and aout allocations. In current implementation, the returned uchar3 data is 32 bit long and I lose r-component of my 2nd pixel and henceforth. – Gagan Dec 10 '13 at 05:29