Coding functions across multiple renderscripts to avoid duplications

旧巷老猫 提交于 2019-12-11 20:30:15

问题


I have the following 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);
}

Then I have another renderscript that is almost identical, I call it when changing from portrait to landscape and I have to rotate the image by 90 degrees:

#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);
}

The only differences are the functions { rsGetElementAtYuv_uchar_$YUV$(inputImg, x, y) } changed to { rsGetElementAtYuv_uchar_$YUV$(inputImg, y, x) }

Actually with the final implementation I should have four renderscripts for each function, so there would be a lot of code duplication, in "Do some more stuff" there is a lot of stuff, but the worst thing is that in the java code I have to place a lot of ifs. On the other hand I don't want to put a flag with a condition within the kernel because it would hit heavily the performance.

Is there a way to make it cleaner without affecting the performance? E.G. Is it possible to call different kernels from doWork (or creating several forEach)? Is it possible to call from one renderscript a function in another renderscript?

来源:https://stackoverflow.com/questions/54014065/coding-functions-across-multiple-renderscripts-to-avoid-duplications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!