0

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?

Trt Trt
  • 5,330
  • 13
  • 53
  • 86
  • How many position does 'in' have? How can I know it's size? no matter what number I put it does not throw an out of bound error – Trt Trt Jun 15 '15 at 10:24
  • *in is just a placeholder for a single cell of an Allocation that you created in Java. You cannot use it to index anything other than in[0] or *in (which are equivalent). The same holds for out. – Stephen Hines Jun 17 '15 at 19:40

2 Answers2

2

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.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
2

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;
}
Stephen Hines
  • 2,612
  • 1
  • 13
  • 12
  • Thanks, Stephen! Appreciate the feedback and great re-write with the value argument version of a kennel. – Larry Schiefer Jun 16 '15 at 03:24
  • I tried your code and I am getting this: /home/Projects/android/foo-master/app/src/main/rs/pixelsCalc.rs:8:17: error: expected ';' after top level declarator – Trt Trt Jun 17 '15 at 12:45
  • That is saying you have a typo of some sort on line 8, column 17. The code as I wrote it above works just fine with llvm-rs-cc. – Stephen Hines Jun 17 '15 at 19:38