2013-05-11 28 views
2

1)我如何訪問forEach_root()除當前元素之外的其他元素?Renderscript,forEach_root,以及從OpenCL移植

在OpenCL中,我們有指向第一個元素的指針,然後可以使用get_global_id(0)來獲取當前索引。但是我們仍然可以訪問所有其他元素。在Renderscript中,我們只有指向當前元素的指針嗎?

2)如何循環遍歷forEach_root()中的分配?

我有一個在java中使用嵌套(雙)循環的代碼。 Renderscript自動化了外層循環,但我找不到任何有關實現內層循環的信息。下面是我最大的努力:

void root(const float3 *v_in, float3 *v_out) { 
    rs_allocation alloc = rsGetAllocation(v_in); 
    uint32_t cnt = rsAllocationGetDimX(alloc); 
    *v_out = 0; 
    for(int i=0; i<cnt; i++) 
    *v_out += v_in[i]; 
} 

但這裏rsGetAllocation()從forEach_root調用時失敗()。

05-11 21:31:29.639: E/RenderScript(17032): ScriptC::ptrToAllocation, failed to find 0x5beb1a40 

萬一我加我的OpenCL代碼在Windows下的偉大工程。我試圖將其移植到Renderscript

typedef float4 wType; 

__kernel void gravity_kernel(__global wType *src,__global wType *dst) 
{ 
    int id = get_global_id(0); 
    int count = get_global_size(0); 
    double4 tmp = 0; 
    for(int i=0;i<count;i++) { 
    float4 diff = src[i] - src[id]; 
    float sq_dist = dot(diff, diff); 
    float4 norm = normalize(diff); 
    if (sq_dist<0.5/60) 
     tmp += convert_double4(norm*sq_dist); 
    else 
     tmp += convert_double4(norm/sq_dist); 
    } 
    dst[id] = convert_float4(tmp); 
} 

回答

2

除了根函數,您可以提供數據。在目前的Android版本(4.2),你可以做以下(這是從圖像處理方案爲例):

的renderScript片段:

#pragma version(1) 
#pragma rs java_package_name(com.example.renderscripttests) 

//Define global variables in your renderscript: 
rs_allocation pixels; 
int width; 
int height; 

// And access these in your root function via rsGetElementAt(pixels, px, py) 
void root(uchar4 *v_out, uint32_t x, uint32_t y) 
{ 
    for(int px = 0; px < width; ++px) 
     for(int py = 0; py < height; ++py) 
     { 
      // unpack a color to a float4 
      float4 f4 = rsUnpackColor8888(*(uchar*)rsGetElementAt(pixels, px, py)); 
      ... 

的Java文件片段

// In your java file, create a renderscript: 
RenderScript renderscript = RenderScript.create(this); 

ScriptC_myscript script = new ScriptC_myscript(renderscript); 

// Create Allocations for in- and output (As input the bitmap 'bitmapIn' should be used): 
Allocation pixelsIn = Allocation.createFromBitmap(renderscript, bitmapIn, 
     Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); 
Allocation pixelsOut = Allocation.createTyped(renderscript, pixelsIn.getType()); 

// Set width, height and pixels in the script: 
script.set_width(640); 
script.set_height(480); 
script.set_pixels(pixelsIn); 

// Call the for each loop: 
script.forEach_root(pixelsOut); 

// Copy Allocation to the bitmap 'bitmapOut': 
pixelsOut.copyTo(bitmapOut); 

您可以看到,在調用forEach_root函數計算'pixelsOut'的值時,輸入'pixelsIn'是之前在renderscript中設置和使用的。預先設置寬度和高度。

+0

謝謝!但假設你的例子中有2個線程,他們會做同樣的工作嗎?兩個線程都會循環顯示相同的像素?另一個問題:是否在你的例子中x&y聲明瞭兩次? – 2013-05-12 09:53:34

+0

我還沒有嘗試過,java端的兩個線程是否會使用相同的腳本進行衝突。在'新ScriptC_myscript(renderscript);'在renderscript方面創建不同的對象我認爲它應該工作。我編輯了兩個循環中的x和y到px和py。 (x和y指的是'v_out'的位置' – stefank 2013-05-12 10:03:22

+0

我明白了,你的意思是線程'在幕後'。對於java端的'pixelsOut'中的每個元素都調用renderscript的root()可以由多個線程同時完成,這樣一個元素在renderscript一側用'v_out'表示,在這個例子中,你可以計算'x'和'y'位置的每個元素'v_out' - 這是自動提供的 - 但訪問整個rs_allocation'像素',如兩個for-loops所示。 – stefank 2013-05-12 10:28:52