简体   繁体   English

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

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

So I have this struct with a pointer in Renderscript code: 所以我在Renderscript代码中有一个带有指针的结构:

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

RGB_t *rgb;

This is how I'm allocating and binding memory to this pointer: 这就是我将内存分配和绑定到此指针的方式:

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

This is how I set my initial values: 这是我设置初始值的方式:

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' is a script with a kernel to populate 'RGB_t*', it's exactly like that: “ 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);
}

Running this kernel with: 使用以下命令运行该内核:

function_script.forEach_root(arrayOperational);

doesn't update my ScriptField values at Android Layer. 不会在Android Layer更新我的ScriptField值。 I still have the old values that I've settled before. 我仍然有我以前确定的旧价值观。

So if I call: 因此,如果我致电:

rgbs.get(index)

I'll get exactly my old value, running after 'foreach root' call, of course. 当然,在“ foreach root”调用之后,我将得到我原来的值。

How can I reflect my changes in the Android Layer? 如何在Android Layer中反映我的更改?

Since you're dealing with Bitmap like data (RGB values), you should consider using input and output Allocation objects rather than your own bindings. 由于您正在处理Bitmap例如数据(RGB值),因此应考虑使用输入和输出Allocation对象,而不是您自己的绑定。 Rendescript will efficiently handle that for you rather than having to manually bind fields like this. Rendescript将为您有效地解决此问题,而不必像这样手动绑定字段。

However, if you must do this binding, you'll need to setup the usage of the field to include Allocation.USAGE_SHARED (use a different, overloaded constructor.) With that in place, you should be able to call updateAllocation() on the field object in Java to get the latest data from the script. 但是,如果你必须这样做绑定,你需要安装现场的使用情况,包括Allocation.USAGE_SHARED (使用不同,重载的构造)。通过在地方,你应该能够调用updateAllocation()上Java中的field对象以从脚本中获取最新数据。 If that doesn't work (it depends on the auto-generated code), you could try calling getAllocation() then calling Allocation.syncAll(Allocation.USAGE_SHARED) to get the Allocation updated with any changes from the script. 如果这不起作用(取决于自动生成的代码),则可以尝试调用getAllocation()然后调用Allocation.syncAll(Allocation.USAGE_SHARED)以使用脚本中的任何更改来更新Allocation At that point your field access methods should give the updated values. 此时,您的字段访问方法应提供更新的值。

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

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