clEnqueueReadBuffer 不写入缓冲区

时间:2021-06-23 09:22:19

标签: c graphics opencl

我是 OpenCL 的新手,我正在尝试制作 Mandelbrot 集的并行渲染器。 它昨天起作用了,我很确定我没有修改我的代码中的任何内容,但是 clEnqueueReadBuffer 命令似乎没有在那里做任何事情。也欢迎改进我的代码。

int draw_image(t_plan *plan, t_ocl_data *ocl_data)
{
    cl_mem  img_mem;
    size_t  global_size[2];
    size_t  local_size[2];
    cl_int  err;

    img_mem = image_to_cl_buffer(&plan->image, ocl_data->context);
    fill_image(plan->image, 0x6f42f5);
    err = clSetKernelArg(ocl_data->kernel, 0, sizeof(cl_mem), &img_mem);
    global_size[0] = plan->image.res.x;
    global_size[1] = plan->image.res.y;
    local_size[0] = plan->image.bpp;
    local_size[1] = plan->image.bpp;
    err = clEnqueueNDRangeKernel(ocl_data->cqueue, ocl_data->kernel, 2, NULL, global_size, NULL, 0, NULL, NULL);
    if (err != CL_SUCCESS)
    {
        printf("%d\n", err);
        exit(0);
    }
    sleep(1);
    err = clEnqueueReadBuffer(ocl_data->cqueue, img_mem, CL_TRUE, 0, plan->image.total_size, plan->image.img_data, 0, NULL, NULL);
    if (err != CL_SUCCESS)
    {
        printf("%d\n", err);
        exit(0);
    }
    return (0);
}
double lerp(int x, int xmax, double low, double high);

double lerp(int x, int xmax, double low, double high)
{
    return (x * (high - low) / xmax + low);
}

__kernel
void mandelbrot(__global int *img_data)
{
    int x = get_global_id(0);
    int y = get_global_id(1);
    int width = get_global_size(0);
    int height = get_global_size(1);
    int max_iter = 256;
    int i = 0;
    double a = lerp(x, width, -2.5, 1.0);
    double b = lerp(y, height, -1.0, 1.0);
    double na = 0;
    double nb = 0;
    double n_a = 0;
    double n_b = 0;
    while (n_a + n_b <= 4 && ++i < max_iter)
    {
        nb = 2 * nb * na + b;
        na = n_a - n_b + a;
        n_a = na * na;
        n_b = nb * nb;
    }
    img_data[y * width + x] = i | i << 8 | i << 16;
}

0 个答案:

没有答案
相关问题