最近正在用pyopencl编写GPU内核程序,但是遇到问题。
以下是内核程序的代码
__kernel void overlay(__global float4 *bg, __global void* ptrs, __global int *img_info, int num_ptrs) {
int x = get_global_id(0);
int y = get_global_id(1);
int bg_index = (y * 1232 + x);
for (int i = 0; i < num_ptrs; i++) {
int left = img_info[i * 4];
int top = img_info[i * 4 + 1];
int width = img_info[i * 4 + 2];
int height = img_info[i * 4 + 3];
if (x >= left && x < left + width && y >= top && y < top + height) {
int img_index = ((y - top) * width + (x - left));
printf("img_index: %d\\n", img_index);
__global float4 *img = (__global float4 *)(((__global char*)ptrs)[i]);
// 打印img
float4 bg_pixel = bg[bg_index];
float4 img_pixel = img[img_index];
printf("img_pixel: %f, %f, %f, %f\\n", img_pixel.x, img_pixel.y, img_pixel.z, img_pixel.w);
float alpha = img_pixel.w / 255.0f;
float inv_alpha = 1.0f - alpha;
bg[bg_index] = (float4)(
bg_pixel.x * inv_alpha + img_pixel.x * alpha,
bg_pixel.y * inv_alpha + img_pixel.y * alpha,
bg_pixel.z * inv_alpha + img_pixel.z * alpha,
bg_pixel.w
);
}
}
}
在内核程序中,我传入了一参数 __global void* ptrs,这个ptrs是一个数组,数组内部包含了100个buffer类型的数据,在此之前,我写了一个程序如下
__kernel void overlay(__global float4 *bg, __global float4 *img, int img_width, int img_height, int bg_w, int bg_h, int blit_x, int blit_y) {
int x = get_global_id(0);
int y = get_global_id(1);
if (x < img_width && y < img_height && x < bg_w && y < bg_h) {
int bg_index = (bg_w * (y + blit_y) + x + blit_x) * 4;
int img_index = (y * img_width + x) * 4;
if (bg_index >= bg_w * bg_h * 4) {
return;
}
if (blit_x + x > bg_w || blit_y + y > bg_h || blit_x + x < 0 || blit_y + y < 0) {
return;
}
float4 bg_pixel = bg[bg_index / 4];
float4 img_pixel = img[img_index / 4];
float alpha = img_pixel.w / 255.0f;
float inv_alpha = 1.0f - alpha;
bg[bg_index / 4] = (float4)(
bg_pixel.x * inv_alpha + img_pixel.x * alpha,
bg_pixel.y * inv_alpha + img_pixel.y * alpha,
bg_pixel.z * inv_alpha + img_pixel.z * alpha,
bg_pixel.w
);
}
}
这两个内核程序都是进行贴图操作,但是第二个程序正常运行,第一个报错。这是因为我在传参的时候第二个程序传入的是单个buffer数据。但是第一段代码,我传入了一个数组的buffer数据,所以导致出错。那么应该怎么传参呢?
就是如何传大量的buffer图片