Renderscript, what is the `in` parameter?

446 views Asked by At

I am trying to understand Renderscript. Could somebody have a look at this code, and let me know what the in parameter is ? It is not an Allocation object, so it is an Element? Why is it an array then?

(I got the code from here, and modified it, http://www.jayway.com/2014/02/11/renderscript-on-android-basics/)

#pragma version(1)
#pragma rs java_package_name(foo.bar)

rs_allocation inPixels;
int height;
int width;

void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
    float3 pixel = convert_float4(in[0]).rgb;

    pixel.r = (pixel.r + pixel.g + pixel.b)/3;
    pixel.g = (pixel.r + pixel.g + pixel.b)/3;
    pixel.b = (pixel.r + pixel.g + pixel.b)/3;

    int topRight
    //float4 f4 = rsUnpackColor8888(*(uchar*)rsGetElementAt(inPixels, x+1, y+1));

    out->xyz = convert_uchar3(pixel);
}

What does this line convert_float4(in[0]) do? The index 0 point to what ? The first pixel? So if I want to access the next pixel I should increase that by one?

2

There are 2 answers

1
Larry Schiefer On BEST ANSWER

The uchar4 and float3 types are essentially the same as what you find in OpenCL. They are vector values containing 4 and 3 components, respectively. The convert_float4() and convert_uchar3() functions are provided by Renderscript to do correct, fast conversion between the different types.

The in parameter is essentially a pointer to the current element in which your kernel is operating. The x and y values tell you exactly which Element within your Allocation the in corresponds. You should not attempt to use it as an array and directly access other elements. If you do, you risk touching memory in which your process does not have access. Renderscript is processing your dataset in parallel, which may be done on different threads or a different processor (GPU). It depends on the hardware implementation.

3
Stephen Hines On

Larry's response is a great explanation of how to work with these vector pieces of data.

You could rewrite this to be more clear using our pass-by-value style instead:

uchar4 RS_KERNEL root(uchar4 in, uint32_t x, uint32_t y) {
  // x and y aren't used, so you can remove those from the above signature too.
  uchar4 out;
  float3 pixel = convert_float4(in).rgb;

  pixel.r = (pixel.r + pixel.g + pixel.b)/3;
  // This seems buggy to me below, since pixel.r was just modified.
  // I think you need another temporary variable (assuming you are trying to make this work and getting weird behavior).
  pixel.g = (pixel.r + pixel.g + pixel.b)/3;
  pixel.b = (pixel.r + pixel.g + pixel.b)/3;

  //int topRight
  //float4 f4 = rsUnpackColor8888(*(uchar*)rsGetElementAt(inPixels, x+1, y+1));

  out.xyz = convert_uchar3(pixel);
  return out;
}