你如何在OpenCL中表示一个图像

时间:2013-08-02 18:37:01

标签: opencl

我有示例代码,但它完全省略了我的(void *)should_be!

我设置了cl_image_desc,cl_image_format,缓冲区,原点和区域:

cl_image_desc desc;
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
desc.image_width = width;
desc.image_height = height;
desc.image_depth = 0;
desc.image_array_size = 0;
desc.image_row_pitch = 0;
desc.image_slice_pitch = 0;
desc.num_mip_levels = 0;
desc.num_samples = 0;
desc.buffer = NULL;

cl_image_format format;
format.image_channel_order = CL_R;
format.image_channel_data_type = CL_FLOAT;

cl_mem bufferSourceImage = clCreateImage(context, CL_MEM_READ_ONLY, &format, &desc, NULL, NULL);
size_t origin[3] = {0, 0, 0};
size_t region[3] = {width, height,1};

在下一个片段中,sourceImage是指向我图像的void指针。但是我的形象是什么?对于每个像素,存在r,g,b,a,x和y值。

clEnqueueWriteImage(queue, bufferSourceImage, CL_TRUE, origin, region, 0, 0, sourceImage, 0, NULL, NULL);

如何将我的图像(一堆(r,g,b,a,x,y))转换为合适的数组?

这是他们提供的内核:

__kernel void convolution(__read_only image2d_t sourceImage, __write_only image2d_t outputImage, int rows, int cols, __constant float* filter, int filterWidth, sampler_t sampler)
{
    int column = get_global_id(0);
    int row = get_global_id(1);
    int halfWidth = (int)(filterWidth/2);

    float4 sum = {0.0f, 0.0f, 0.0f, 0.0f};

    int filterIdx = 0;
    int2 coords;
    for(int i = -halfWidth; i <= halfWidth; i++)
    {
        coords.y = row + i;
        for(int i2 = -halfWidth; i2 <= halfWidth; i2++) 
        {
            coords.x = column + i2;
            float4 pixel;
            pixel = read_imagef(sourceImage, sampler, coords);
            sum.x += pixel.x * filter[filterIdx++];

        }
    }
    if(myRow < rows && myCol < cols)
    {
        coords.x = column;
        coords.y = row;
        write_imagef(outputImage, coords, sum);
    }
}

1 个答案:

答案 0 :(得分:1)

根据需要设置cl_image_format,然后您只需按照您选择的格式进行操作即可。目前您的通道(R,G,B,A)数据应表示为&#34;单精度浮点值&#34; - image_channel_data_type = CL_FLOAT,您只能使用其中一个频道并将其输入预期的R频道(image_channel_order = CL_R)。

你的内核期望浮动:

float4 pixel;
pixel = read_imagef(sourceImage, sampler, coords);