1

我用纯java做了一个小像素排序应用程序,效果很好,但是性能很差。我听说渲染脚本就是为了这个!

我编写了一些代码,但 C99 太新了,所以我知道缺少一些东西。我做了这个小测试脚本。

#pragma version(1)
#pragma rs java_package_name(com.simahero.pixelsort)
#pragma rs_fp_relaxed

float treshhold = 0.f;

static void swap(uchar4 *xp, uchar4 *yp)
{
    uchar4 temp = *xp;
    *xp = *yp;
    *yp = temp;
}

static void selectionSort(uchar4 arr[], int n)
{
    int i, j, min_idx;

    for (i = 0; i < n-1; i++)
    {
        min_idx = i;
        for (j = i+1; j < n; j++)
        if (arr[j].r < arr[min_idx].r)
            min_idx = j;

        swap(&arr[min_idx], &arr[i]);
    }
}

rs_allocation RS_KERNEL invert(rs_allocation in) {

    for (int i = 0; i < rsAllocationGetDimY(in); i++){
        uchar4 row[rsAllocationGetDimX(in)];
        for (int j = 0; j < rsAllocationGetDimX(in); j++){
            uchar4 pixel = rsGetElementAt_uchar4(in, i, j);
            row[j] = pixel;
        }
        selectionSort(row, rsAllocationGetDimX(in));
     }
  return in;
}

void process(rs_allocation inputImage, rs_allocation outputImage) {
   outputImage = invert(inputImage);
}

我在异步任务中简单地调用它,但位图是空的,或者我不知道,因为缺乏调试 rs 的知识。

script.invoke_process(mInAllocation, outputAllocation);
outputAllocation.copyTo(bo);
4

1 回答 1

2

您正在复制图像的每一行,然后对其进行排序,但您永远不会写回结果(在任何地方都没有调用 rsSetElement 方法)。即使你这样做了,它也不认为你会用这种方法获得令人满意的表现。我会通过编写一个在输入分配的所有行上执行的内核来解决这个问题(查看 Renderscirpt 内核的 LaunchOptions),因此它将至少在所有行上并行执行。那看起来像:

rs_allocation allocIn;
rs_allocation allocOut;

void RS_KERNEL sortRows(uchar4 in, int x, int y){
    //with proper launch options, x stays always the same, while this kernel gets called in parallel for all rows of the input allocation ( = different y values)
    for (int currentCollumn = 0; currentCollumn  < rsAllocationGetDimX(allocIn); currentCollumn ++){
       //do your stuff for this row (by using y as index). Use rsGetElementAt and rsSetElementAt calls only (avoid copies for speed)   
    }
 }
于 2020-04-10T20:04:56.900 回答