1

我想使用以下 RenderScript 代码计算位图的像素

渲染脚本

文件名:counter.rs

#pragma version(1)
#pragma rs java_package_name(com.mypackage)
#pragma rs_fp_relaxed

uint count; // initialized in Java
void countPixels(uchar4* unused, uint x, uint y) {
  rsAtomicInc(&count);
}

爪哇

Application context = ...; // The application context
RenderScript rs = RenderScript.create(applicationContext);

Bitmap bitmap = ...; // A random bitmap
Allocation allocation = Allocation.createFromBitmap(rs, bitmap);

ScriptC_Counter script = new ScriptC_Counter(rs);
script.set_count(0);
script.forEach_countPixels(allocation);

allocation.syncAll(Allocation.USAGE_SCRIPT);
long count = script.get_count();

错误

这是我收到的错误消息:

错误:找不到计数的地址

问题

  • 为什么我的代码不起作用?
  • 我该如何解决?

链接

4

2 回答 2

1

附带说明一下,除非必须,否则在并行计算中使用原子操作通常不是一个好习惯。RenderScript 实际上为这类应用程序提供了缩减内核。也许你可以试一试。

代码有几个问题:

  1. 变量“count”应该被声明为“volatile”
  2. countPixels 应该是“void RS_KERNEL countPixels(uchar4 in)”
  3. script.get_count() 不会为您提供“count”的最新值,您必须通过分配来取回该值。

如果你必须使用 rsAtomicInc,一个很好的例子实际上是 RenderScript CTS 测试:

AtomicTest.rs

原子测试.java

于 2016-12-23T01:30:56.333 回答
1

这是我的工作解决方案。

渲染脚本

文件名:counter.rs

#pragma version(1)
#pragma rs java_package_name(com.mypackage)
#pragma rs_fp_relaxed

int32_t count = 0;
rs_allocation rsAllocationCount;

void countPixels(uchar4* unused, uint x, uint y) {
  rsAtomicInc(&count);
  rsSetElementAt_int(rsAllocationCount, count, 0);
}

爪哇

Context context = ...;
RenderScript renderScript = RenderScript.create(context);

Bitmap bitmap = ...; // A random bitmap
Allocation allocationBitmap = Allocation.createFromBitmap(renderScript, bitmap);
Allocation allocationCount = Allocation.createTyped(renderScript, Type.createX(renderScript, Element.I32(renderScript), 1));

ScriptC_Counter script = new ScriptC_Counter(renderScript);
script.set_rsAllocationCount(allocationCount);
script.forEach_countPixels(allocationBitmap);

int[] count = new int[1];
allocationBitmap.syncAll(Allocation.USAGE_SCRIPT);
allocationCount.copyTo(count);

// The count can now be accessed via
count[0];
于 2017-01-24T13:16:12.893 回答