可以使用Script.LaunchOptions
類。
使用此類可以設置內核的執行限制。
例:你想對你的數據(因此,在2個維),起始於X -index(0爲主,含)3
的「矩形」運行的內核,並在X結尾-index(不含)8
和,在ÿ側的限制是11
和22
:
Script.LaunchOptions launchOptions;
launchOptions = new Script.LaunchOptions();
launchOptions.setX(3, 8);
launchOptions.setY(11, 22);
// Kernel run
myScript.forEach_myKernelName(inAlloc, outAlloc, launchOptions);
例:要在一個圖像應用內核,用3個像素寬的邊界(例如直接從FASTExample示例項目):
Script.LaunchOptions fastLaunchOptions;
fastLaunchOptions = new Script.LaunchOptions();
fastLaunchOptions.setX(3, inputImageSize.width - 3);
fastLaunchOptions.setY(3, inputImageSize.height - 3);
// ...
scriptCFast.forEach_fastOptimized(
grayAllocation, fastKpAllocation, fastLaunchOptions);
例:你想通過限制範圍內應用ScriptIntrinsicConvolve3x3
內核:
// Define the convolution
ScriptIntrinsicConvolve3x3 convolve3x3 =
ScriptIntrinsicConvolve3x3.create(mRS, Element.RGBA_8888(mRS));
// Some coefficients
float[] coefficients = {
0.7f, 0, 0.5f,
0, 1.0f, 0,
0.5f, 0, 1.0f
};
convolve3x3.setCoefficients(coefficients);
// Execute the allocation with limits
Script.LaunchOptions launchOptions;
launchOptions = new Script.LaunchOptions();
launchOptions.setX(3, 8);
launchOptions.setY(11, 22);
convolve3x3.setInput(inputAllocation);
convolve3x3.forEach(convolvedAllocation, launchOptions);
注意:這個過程只是在一定範圍內執行內核,但它不會創建一個新的,更小的分配。因此,已經執行了某些限制的內核後,你應該使用複製的內核,就像下面的一個複製它的結果:
// Store the input allocation
rs_allocation inputAllocation;
// Offset indices, which define the start point for
// the copy in the input allocation.
int inputOffsetX;
int inputOffsetY;
uchar4 __attribute__((kernel)) copyAllocation(int x, int y) {
return rsGetElementAt_uchar4(
inputAllocation, x + inputOffsetX, y + inputOffsetY);
}
調用時:
scriptC_main.set_inputAllocation(convolvedAllocation);
scriptC_main.set_inputOffsetX(offsetX);
scriptC_main.set_inputOffsetY(offsetY);
scriptC_main.forEach_copyAllocation(outputAllocation);
編輯:我專門爲此情況創建了example,您可以在其中看到以下過程:
- 在數據集上執行有限卷積。
- 將卷積輸出複製到新的分配。
參考:RenderScript: parallel computing on Android, the easy way
@ cmaster11可以請你幫助? – Shahryar
@LarrySchiefer可以請你幫忙嗎?你是我在回答renderscript問題的stackoverflow中看到的少數幾個人之一。 – Shahryar
感謝您的通知:P – cmaster11