简体   繁体   English

RenderScript分配。 从分配读取int

[英]RenderScript Allocation. Read int from Allocation

In Java I have this code for creating the Allocation for the intPointer. 在Java中,我有这段代码可为intPointer创建分配。 But after the renderscript computation I can't get the value of the allocation back. 但是在渲染脚本计算之后,我无法获得分配的值。 There is no copyTo(int) method only byte[], short[], int[], float[], bitmap. 没有copyTo(int)方法,只有byte [],short [],int [],float [],位图。

    //Create Allocation
    Allocation intPointer = Allocation.createSized(renderScript, Element.I32(renderScript), 2);

    convolution.bind_intPointer(intPointer);
    convolution.forEach_root(allocationOut);

    [...]
    //read Allocation, not working
    int[] ints = new int[]{0x01234567};
    intPointer.copyTo(ints);             //does not overwrite the ints the line above
    int i = (int)ints[0];
    Log.d("ints", String.valueOf(i));

renderscript: 的renderScript:

#pragma version(1)
#pragma rs java_package_name(package com.ap.wificam)
#pragma rs_fp_imprecise

rs_allocation input;
rs_allocation mask;

int32_t *intPointer = 0;

void root(uchar4* out, uint32_t x, uint32_t y) {

    //do some stuff

    if((sum.x + sum.y + sum.z)/3 > *intPointer)
        *intPointer = (sum.x + sum.y + sum.z)/3;    //wirte data to intPointer

    *out = convert_uchar4(sum);
}

Log.d("ints", String.valueOf(i)); Log.d(“ ints”,String.valueOf(i)); gives me D/ints﹕ 19088743 and rsDebug("rs", *intPointer); 给我D/ints﹕ 19088743和rsDebug(“ rs”,* intPointer); 2045 0x7fd

How can I get my int value from the Allocation? 我如何从分配中获取我的int值?

Your RenderScript setup seems to be correct, but I think you're having problems initializing the data in the allocation correctly. 您的RenderScript设置似乎是正确的,但是我认为您在正确初始化分配中的数据时遇到了问题。

Since you're reading the values of the allocation inside RenderScript when you compare if the sum average is greater than *intPointer , you'll want to make sure the starting value for that is what you're actually expecting. 由于在比较sum是否大于*intPointer ,您正在读取RenderScript中的分配值,因此需要确保该起始值是您实际期望的值。 Otherwise it will have uninitialized data and the > comparison will fail most of the time. 否则,它将具有未初始化的数据,并且>比较将在大多数情况下失败。

I guess that's what you were trying to do with int32_t *intPointer = 0; 我想这就是您要使用int32_t *intPointer = 0; , but that's not really the correct way to initialize the data of pointers in C. The easiest way to do this is to do a data copy from Java right after you create the allocation: ,但这并不是在C中初始化指针数据的正确方法。最简单的方法是在创建分配后立即从Java复制数据:

Allocation intPointer = Allocation.createSized(renderScript, Element.I32(renderScript), 2);
intPointer.copy1DRangeFrom(0, 2, new int[2]);
convolution.bind_intPointer(intPointer);

This will make sure all values are 0 at the start of your RS kernel (since in Java, new int[2] creates a 0-initialized array). 这将确保RS内核开始时所有值均为0(因为在Java中, new int[2]创建了一个0初始化的数组)。

Furthermore, your code might not be entirely thread-safe. 此外,您的代码可能并不完全是线程安全的。 Since you will have many threads reading and writing simultaneously to that single *intPointer address, you might run into synchronization problems, where the final value you get out of that may or may not be the true global maximum from all your threads. 由于您将有许多线程同时读取和写入单个*intPointer地址,因此您可能会遇到同步问题,从中获得的最终值可能是也可能不是所有线程的真实全局最大值。

I would suggest that instead of doing this, you store all (sum.x + sum.y + sum.z)/3 values in an allocation that is as big as your input data, then copy that allocation back out to Java and from there, do a simple loop over its array to find the maximum value. 我建议不要这样做,而是将所有 (sum.x + sum.y + sum.z)/3值存储在与输入数据一样大的分配中,然后将该分配复制回Java并从在那里,对其数组进行简单循环以找到最大值。

Properly parallelizing a maximum algorithm from inside a RenderScript kernel is actually a bit difficult to do (you can read this other answer I posted some time ago for more details if you're interested), so to keep things simple, you might be better off just finding the maximum from Java instead as I suggested above. 从RenderScript内核内部正确并行化一个最大算法实际上是很难做到的(如果您有兴趣的话,您可以阅读我前段时间发布的其他答案,以获取更多详细信息),因此,保持简单可能会更好只是从Java中找到最大值,而不是如上所述。

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

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