跨多个渲染脚本编码功能,避免重复

时间:2019-01-02 22:36:15

标签: android renderscript

我有以下渲染脚本:

#pragma version(1)
#pragma rs java_package_name(some.mypkg)
#pragma rs_fp_relaxed

rs_allocation inputImg;

void root(const uint16_t *v_in, uint16_t *v_out,
          const void *usrData, uint32_t x, uint32_t y) {

    // Read in pixel values from latest frame - YUV color space
    uchar4 in_pixel= rsYuvToRGBA_uchar4(rsGetElementAtYuv_uchar_Y(inputImg, x, y),
                                        rsGetElementAtYuv_uchar_U(inputImg, x, y),
                                        rsGetElementAtYuv_uchar_V(inputImg, x, y));

    //Do some more stuff

    *v_out = newPixel;
}

void doWork( rs_script script, rs_allocation alloc_out) {

    //I cannot refer the input allocation here because I got runtime 
    //errors claiming that the allocations had different size. I think
    //it was caused by the format conversions.
    rsForEach(script, alloc_out, alloc_out);
}

然后我有另一个几乎完全相同的渲染脚本,当从纵向更改为横向时,我将其命名,并且必须将图像旋转90度:

#pragma version(1)
#pragma rs java_package_name(some.mypkg)
#pragma rs_fp_relaxed

rs_allocation inputImg;

void root(const uint16_t *v_in, uint16_t *v_out,
          const void *usrData, uint32_t x, uint32_t y) {

    // Read in pixel values from latest frame - YUV color space
    uchar4 in_pixel= rsYuvToRGBA_uchar4(rsGetElementAtYuv_uchar_Y(inputImg, y, x),
                                        rsGetElementAtYuv_uchar_U(inputImg, y, x),
                                        rsGetElementAtYuv_uchar_V(inputImg, y, x));

    //Do some more stuff

    *v_out = newPixel;
}

void doWork( rs_script script, rs_allocation alloc_out) {

    //I cannot refer the input allocation here because I got runtime 
    //errors claiming that the allocations had different size. I think
    //it was caused by the format conversions.
    rsForEach(script, alloc_out, alloc_out);
}

唯一的区别是将函数{rsGetElementAtYuv_uchar_ $ YUV $(inputImg, x,y )}更改为{rsGetElementAtYuv_uchar_ $ YUV $(inputImg, y,x ) }

实际上,在最终实现中,我应该为每个函数提供四个渲染脚本,因此会有很多代码重复,在“做更多的事情”中有很多事情,但是最糟糕的是在Java中代码我必须放置很多ifs。另一方面,我不想在内核中放置带有条件的标志,因为它会严重影响性能。

有没有一种方法可以使其更清洁而不影响性能?例如。是否可以从doWork调用不同的内核(或创建几个forEach)?是否可以从一个渲染脚本调用另一个渲染脚本中的函数?

0 个答案:

没有答案