I’m learning OpenCL and I wrote simple kernel, which copies part (square) of input image and pastes it to different position (in this case (0,0)) of output image. The problem is reading of parameter.
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE |
CLK_ADDRESS_CLAMP_TO_EDGE |
CLK_FILTER_NEAREST;
__kernel void image_part_copy( __read_only image2d_t input,
__constant int* x_coord,
__global float* output,
__write_only image2d_t outputImage)
{
int width = get_image_width( input );
int height = get_image_height( input );
int2 pixelcoord = (int2) (get_global_id(0), get_global_id(1));
int xc = x_coord[get_global_id(0)];
const int top_left = xc; // <--- THE PROBLEM IS HERE
//const int top_left = 1000;
const int width_of_cut = 2500;
const int right_bottom = top_left + width_of_cut;
int a,b;
a = (int)(pixelcoord.x + top_left);
b = (int)(pixelcoord.y + top_left);
if( a < right_bottom && b < right_bottom )
{
float4 first_pixel = read_imagef(input, sampler, (int2)(a,b));
write_imagef(outputImage, (int2)(pixelcoord.x,pixelcoord.y), first_pixel);
}
else
{
float4 copy = read_imagef(input, sampler, pixelcoord);
write_imagef(outputImage, (int2)(pixelcoord.x,pixelcoord.y), copy);
}
output[get_global_id(0)] = (float)(top_left); // just for control
}
If there is const int top_left = 1000; (commented line), it works as I wanted. But if I set parameter __constant int* x_coord in host code and then read it in kernel: int xc = x_coord[get_global_id(0)]; and const int top_left = xc;, it just copies the input image. But the control: output[get_global_id(0)] = (float)(top_left); works. In host code is the output parameter the assigned value (input parameter): __constant int* x_coord.