android – 将数组传递给rsForEach在Renderscript Compute中

android – 将数组传递给rsForEach在Renderscript Compute中,第1张

概述我发现在RenderScript中缺少良好的文档,对于我所知道的,每个RS都是为分配中的每个单独的项执行root(). 我正在为一个图像处理的Renderscript做一个库,作为起点,我达到了这个great answer.但问题是模糊 *** 作是在每个像素和每个像素需要另一个循环(n与模糊宽度)计算.虽然运行在多核,但还是有点太慢了. 我试图修改它以允许(两遍)框过滤器,但这需要在单行或列而不是单元格 我发现在RenderScript中缺少良好的文档,对于我所知道的,每个RS都是为分配中的每个单独的项执行root().

我正在为一个图像处理的Renderscript做一个库,作为起点,我达到了这个great answer.但问题是模糊 *** 作是在每个像素和每个像素需要另一个循环(n与模糊宽度)计算.虽然运行在多核,但还是有点太慢了.

我试图修改它以允许(两遍)框过滤器,但这需要在单行或列而不是单元格上工作.那么有什么办法可以让foreach将数组发送给root()吗?

解决方法 rsForEach只能在分配上 *** 作.

如果要让rsForEach函数调用每个图像行的root(),必须在一个Allocation中传递一个大小与行数相同的长度,然后计算出应该在哪个行上运行root()(类似于在每列上 *** 作).然后,RenderScript应该将工作分配到可用资源上运行(在多核设备上同时处理多行).

您可以通过传递给予图像行中的偏移(在图像数据数组中)的分配来实现. root()中的v_in参数就是行偏移量.由于rsForEach调用正在运行的分配不是映像数据,您无法使用v_out参数写入映像,并且必须单独绑定输出映像.

这里有一些RenderScript显示:

#pragma version(1)#pragma rs java_package_name(com.androID.example.hellocompute)rs_allocation gIn;rs_allocation gOut;rs_script gScript;int mImageWIDth;const uchar4 *gInPixels;uchar4 *gOutPixels;voID init() {}static const int kBlurWIDth = 20;//// This is called per row.// The row indices are passed in as v_in or you Could also use the x argument and multiply it by image wIDth.//voID root(const int32_t *v_in,int32_t *v_out,const voID *usrData,uint32_t x,uint32_t y) {    float3 blur[kBlurWIDth];    float3 cur_colour = {0.0f,0.0f,0.0f};    for ( int i = 0; i < kBlurWIDth; i++) {        float3 init_colour = {0.0f,0.0f};        blur[i] = init_colour;    }    int32_t row_index = *v_in;    int blur_index = 0;    for ( int i = 0; i < mImageWIDth; i++) {        float4 pixel_colour = rsUnpackcolor8888(gInPixels[i + row_index]);        cur_colour -= blur[blur_index];        blur[blur_index] = pixel_colour.rgb;        cur_colour += blur[blur_index];        blur_index += 1;        if ( blur_index >= kBlurWIDth) {            blur_index = 0;        }        gOutPixels[i + row_index] = rsPackcolorTo8888(cur_colour/(float)kBlurWIDth);        //gOutPixels[i + row_index] = rsPackcolorTo8888(pixel_colour);    }}voID filter() {    rsDeBUG("Number of rows:",rsAllocationGetDimX(gIn));    rsForEach(gScript,gIn,gOut,NulL);}

这将使用以下Java进行设置:

mBlurRowScript = new ScriptC_blur_row(mRS,getResources(),R.raw.blur_row);    int row_wIDth = mBitmAPIn.getWIDth();    //    // Create an allocation that indexes each row.    //    int num_rows = mBitmAPIn.getHeight();    int[] row_indices = new int[num_rows];    for ( int i = 0; i < num_rows; i++) {        row_indices[i] = i * row_wIDth;    }    Allocation row_indices_alloc = Allocation.createSized( mRS,Element.I32(mRS),num_rows,Allocation.USAGE_SCRIPT);    row_indices_alloc.copyFrom(row_indices);    //    // The image data has to be bound to the pointers within the RenderScript so it can be accessed    // from the root() function.    //    mBlurRowScript.bind_gInPixels(mInAllocation);    mBlurRowScript.bind_gOutPixels(mOutAllocation);    // Pass in the image wIDth    mBlurRowScript.set_mImageWIDth(row_wIDth);    //    // Pass in the row indices Allocation as the input. It is also passed in as the output though the output is not used.    //    mBlurRowScript.set_gIn(row_indices_alloc);    mBlurRowScript.set_gOut(row_indices_alloc);    mBlurRowScript.set_gScript(mBlurRowScript);    mBlurRowScript.invoke_filter();
总结

以上是内存溢出为你收集整理的android – 将数组传递给rsForEach在Renderscript Compute中全部内容,希望文章能够帮你解决android – 将数组传递给rsForEach在Renderscript Compute中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1133923.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存