2015-10-16 85 views
0

我正在寫一個raytracer,並試圖將生成的映像的結果寫入image2d_t,然後通過將其映射回磁盤並將其寫回到主機。OpenCL image2d_t將映射回主機時返回錯誤的值

問題是,我無法讓OpenCL內核寫入image2d_t並在主機上讀取結果。主機上的圖像是帶有一些白點的大部分黑色圖像。

這裏是我的主機代碼的重要組成部分:

cl_image_format rgba_format; 
rgba_format.image_channel_order = CL_RGBA; 
rgba_format.image_channel_data_type = CL_UNSIGNED_INT8; 

outputImage = clCreateImage2D(context, CL_MEM_WRITE_ONLY, 
     &rgba_format, width, height, 0, NULL, &err); 
if(err < 0) fatalError("failed to create OpenCL image"); 

err = clSetKernelArg(sampleKernel, 0, sizeof(outputImage), &outputImage); 
if(err < 0) fatalError("failed to set kernel argument."); 

size_t global_offset[2] = {0, 0}; 
size_t work_size[2] = {height, width}; 

int err = clEnqueueNDRangeKernel(queue, sampleKernel, 2, global_offset, 
     work_size, NULL, 0, NULL, NULL); 
if(err < 0) fatalError("failed to enqueue kernel execution."); 

clFinish(queue); 

// Map the entire output image. 
size_t row_pitch; 
size_t origin[3] = {0, 0, 0}; 
size_t region[3] = {width, height, 1}; 
uint8_t *output = (uint8_t *) clEnqueueMapImage(queue, outputImage, 
     CL_TRUE, CL_MAP_READ, origin, region, &row_pitch, NULL, 0, NULL, 
     NULL, &err); 
if(err < 0) fatalError("failed to map output kernel image."); 

for(int i = 0; i < 5; ++i) 
    printf("%u %u %u %u\n", output[i*4], output[i*4 + 1], output[i*4 + 2], 
      output[i*4 + 3]); 

savePPM(output); 

我的客戶端代碼:

__kernel void sample(__write_only image2d_t out) 
{ 
    int2 coord = (get_global_id(1), get_global_id(0)); 
    uint4 color = (255, 0, 0, 255); 
    write_imageui(out, coord, color); 
} 

的printf()的輸出是:

255 255 255 255 
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 

而生成的圖像是: enter image description here

我知道我可能犯了一個菜鳥的錯誤,但我無法弄清楚問題所在,也找不到任何互聯網上的任何指南來完成這項工作。

我正在使用配備英特爾Iris Pro圖形GPU的MacBook Pro。

回答

1

解決了這個問題。不要忘記在OpenCL矢量上添加類型限定符。

固定內核是:

__kernel void sample(__write_only image2d_t out) 
{ 
    int2 coord = (int2) (get_global_id(1), get_global_id(0)); 
    uint4 color = (uint4) (255, 0, 0, 255); 
    write_imageui(out, coord, color); 
} 
+1

要展開位:無類型限定符,通常的C規則:'(A,B,C,d)'是其評估'A'的表達式, 'b','c'和'd'並返回'd'的結果。考慮接受你自己的答案,從未答覆的隊列中刪除這個問題。 –

+0

我只是在等待2天的時間限制。感謝您的領導和解釋。 – RenatoUtsch