Renderscript, what is the `in` parameter?

人盡茶涼 提交于 2019-12-20 07:32:04

问题


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?


回答1:


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.




回答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;
}


来源:https://stackoverflow.com/questions/30832030/renderscript-what-is-the-in-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!