繁体   English   中英

如何使Renderscript更新指向struct的指针?

[英]How to make Renderscript update a pointer to struct?

所以我在Renderscript代码中有一个带有指针的结构:

typedef struct __attribute__((packed, aligned(4))) RGB {
    float red;
    float green;
    float blue;
} RGB_t;

RGB_t *rgb;

这就是我将内存分配和绑定到此指针的方式:

ScriptField_RGB rgbs = new ScriptField_RGB(mRS, bitmap.getWidth()*bitmap.getHeight());
function_script.bind_rgb(rgbs);

这是我设置初始值的方式:

int index = 0;
for(int y = 0; y < bitmap.getHeight(); ++y) {
    for(int x = 0; x < bitmap.getWidth(); ++x) {
       int color = bitmap.getPixel(x, y);
       ScriptField_RGB.Item i = new ScriptField_RGB.Item();
       i.red = Color.red(color) / 255.0f;
       i.green = Color.green(color) / 255.0f;
       i.blue = Color.blue(color) / 255.0f;
       rgbs.set(i, index, false);
       index++;
   }
}

rgbs.copyAll();

“ function_script”是一个带有内核的脚本,用于填充“ RGB_t *”,就像这样:

#pragma version(1)
#pragma rs java_package_name(my.package)

#include "rgb.rs"

void __attribute__((kernel)) root(RGB_t pixel, uint32_t x) {
    //rsDebug("before", rgb[x].red,rgb[x].green,rgb[x].blue);
    rgb[x].red = pixel.red * 255.0f;
    rgb[x].green = pixel.green * 255.0f;
    rgb[x].blue = pixel.blue * 255.0f;
    //rsDebug("after", rgb[x].red,rgb[x].green,rgb[x].blue);
}

使用以下命令运行该内核:

function_script.forEach_root(arrayOperational);

不会在Android Layer更新我的ScriptField值。 我仍然有我以前确定的旧价值观。

因此,如果我致电:

rgbs.get(index)

当然,在“ foreach root”调用之后,我将得到我原来的值。

如何在Android Layer中反映我的更改?

由于您正在处理Bitmap例如数据(RGB值),因此应考虑使用输入和输出Allocation对象,而不是您自己的绑定。 Rendescript将为您有效地解决此问题,而不必像这样手动绑定字段。

但是,如果你必须这样做绑定,你需要安装现场的使用情况,包括Allocation.USAGE_SHARED (使用不同,重载的构造)。通过在地方,你应该能够调用updateAllocation()上Java中的field对象以从脚本中获取最新数据。 如果这不起作用(取决于自动生成的代码),则可以尝试调用getAllocation()然后调用Allocation.syncAll(Allocation.USAGE_SHARED)以使用脚本中的任何更改来更新Allocation 此时,您的字段访问方法应提供更新的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM