2013-02-19 45 views
0

我的OpenCL內核有問題。輸入參數傳遞給內核時會損壞。奇怪的是,這個相同的內核在mac osx上完美無瑕地執行。一旦我開始將我的代碼移植到Windows(Windows 8 64位),我開始遇到這個問題。OpenCL損壞的輸入WIN32,在OSX Lion上有效

我提供了一個使用我的相機結構的例子。 x,y,z座標定義爲< 0,0,200>。但是,當它們到達我的內核時,它們顯示爲< 0,-0.00132704,-0.00132704>。

我有一個接受兩個結構的內核。

typedef struct{ 
cl_float d; 
cl_float3 eye; 
cl_float3 lookat; 
cl_float3 u; 
cl_float3 v; 
cl_float3 w; 
cl_float3 up; 
}rt_cl_camera; 

typedef struct { 
float r; 
float g; 
float b; 
} rt_cl_rgb; 

爲了測試的緣故,我縮小了我的內核。在追蹤問題後,我注意到我的輸入參數沒有正確傳遞。但是,我確定我的輸出正確傳回。

__kernel void ray_trace_scene(__global rt_cl_rgb* output, 
           __global rt_cl_camera* camera, 
           const unsigned int pcount) 
{ 
    int pixel = get_global_id(0); 
    if(pixel < pcount){ 
      output[pixel].r = camera->eye.x; 
      output[pixel].g = camera->eye.y; 
      output[pixel].b = camera->eye.z; 
     }// End Pixel computation 
}//End kernel 

我創造我的輸入緩衝器與follwoing:

cl_mem cam_input; 
cl_uint cam_error; 
cam_input = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(rt_cl_camera), NULL, &cam_error); 

我也檢查,以確保我的緩衝已成功創建與

if (cam_error != CL_SUCCESS || !cam_input) { 
    throw std::runtime_error(CLERROR_FAILED_DEVBUFF); 
} 

然後我寫我的數據轉換成我的緩衝區以下。

cl_uint err = 0; 
err = clEnqueueWriteBuffer(commands, cam_input, CL_TRUE, 0, sizeof(rt_cl_camera), cam_ptr, 0, NULL, NULL); 
if (err != CL_SUCCESS) { 
    throw std::runtime_error("Failed to write camera"); 
} 

並最終鏈接我的參數爲適當的命令行插槽。請注意,我的輸出中使用了零插槽。

err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &cam_input); 

,並檢查是否一切成功..

if (err != CL_SUCCESS) { 
     throw std::runtime_error(CLERROR_FAILED_CMDARGS); 
    } 

我不是在過程中的任何一步接收來自OpenCL的任何錯誤消息。有沒有人遇到過這個?任何幫助是極大的讚賞。

備註 - 在每一步我打印出我的局部變量,以確保它們在將它們傳遞給GPU之前是正確和有效的。

回答

2

查找對齊/包裝問題。嘗試在結構中使用float4而不是float3,並在結尾處移動float d。

+0

我甚至從來沒有看過。這確實解決了如何讓它成功傳輸的問題。但是,我仍然有這個使用float3的大量內核,我不認爲ctr-h會解決它。任何建議如何我可以糾正對齊問題,仍然使用float3?無論如何,謝謝你的幫助。 – Freddy 2013-02-19 04:13:04

+2

我一直在主機端發現float3的問題,即使在使用cl_float3時,它們也會導致奇怪的對齊問題。現在我只用float4,並在我的內核中調用.xyz。除非你爲了節省內存而爭先恐後,否則在我看來,這是一個更好的解決方案,而不是爭取標準的結構對齊。 – Thomas 2013-02-19 06:20:13