opencl(十二)----图像对象、采样器

图像对象、采样器在主机和设备上使用不同的数据结构。

图像对象

主机: cl_mem   https://www.cnblogs.com/feihu-h/p/12081652.html

设备: image2d_t 、image3d_t

采样器

主机: cl_sampler

设备: sampler_t

 

主机上

// 主机上图像对象
// 图像对象在主机上的  数据结构是,cl_mem
// 使用 clCreateImage2D   clCreateImage3D 创建
// https://www.cnblogs.com/feihu-h/p/12081652.html

cl_mem clCreateImage2D (    
        cl_context context,             //上下文
     cl_mem_flags flags,           // 对象性质标签
     const cl_image_format *image_format,   //输入图像数据格式
     size_t image_width,    //
     size_t image_height,   //
     size_t image_row_pitch, //每一行中的字节数
     void *host_ptr,            // 主机地址
     cl_int *errcode_ret      //错误代码
)

cl_mem clCreateImage3D (
        cl_context context,
     cl_mem_flags flags,
     const cl_image_format *image_format,
     size_t image_width,           //
     size_t image_height,          //
     size_t image_depth,          //
     size_t image_row_pitch,    //每一行字节数
     size_t image_slice_pitch,    //每一层字节数
     void *host_ptr,
     cl_int *errcode_ret        //错误代码
)


typedef struct _cl_image_format
{
     cl_channel_order image_channel_order; //色彩通道,以及各通道顺序
     cl_channel_type image_channel_data_type; // 数据类型

}cl_image_format;

// demo:  创建图像对象
cl_image_format format;
format.image_channel_order = CL_RGBA
format.image_channel_data_type = CL_UNSIGNED_INTS;

image = clCreateImage2D(
     context, 
     CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
     &format, 
     width, 
     height, 
     0, 
     (void*)data, 
     &err
);

// 设置核参数
clSetKernelArg(im_kernl, 0, sizeof(cl_mem), &image);

// 释放可用 clReleaseMemObject
clReleaseMemObject(image)

 

// 主机上的采样器 cl_sampler
cl_sampler clCreateSampler (    
     cl_context context,      // 上下文
     cl_bool normalized_coords, //坐标是否归一化(0.0~1.0)
     cl_addressing_mode addressing_mode, // 内核对超出图像大小范围的坐标的处理方式。
     cl_filter_mode filter_mode, //内核对像素之间颜色取值的插值处理方式
     cl_int *errcode_ret
)

//  cl_addressing_mode  取值  https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/sampler_t.html
CL_ADDRESS_NONE : 坐标超出边界后 取值未定义
CL_ADDRESS_CLAMP: 取边界值,默认为黑
CL_ADDRESS_CLAMP_TO_EDGE : 取边界值
CL_ADDRESS_REPEAT: 范围内的像素不断重复, X mod N
CL_ADDRESS_MIRRORED_REPEAT: 下一个超出范围的坐标被设定为对应的范围内坐标的反射

// demo
cl_sampler ex_sampler = clCreateSampler(ctx, CL_TRUE, CL_ADDRESS_CLAMP_TO_EDGE,
 CL_FILTER_LINEAR);

clSetKernelArg(kernel, 0, sizeof(cl_sampler), &ex_sampler);

 

设备上

//设备上的图像对象  image2d_t   image_3d
//  一些设备将图像保存在特定的设备中,因此有 __read_only  和 __write_only等修饰符。

__kernel void image_proc(read_only image3d_t empg,
 write_only image2d_t ejpg)
// 设备上的采样器 sampler_t
// 主机可以通过 setKernelArg 将 cl_sampler 传递给内核,内核使用sampler_t来接收。

// demo
__kernel void image_proc(__global sampler_t smplr)


// 简单的可以在内核函数内部  直接创建 sampler_t
// const sampler_t sampler_name = sampler_properties
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE |
                               CLK_ADDRESS_CLAMP_TO_EDGE |
                               CLK_FILTER_NEAREST;

posted on 2019-12-27 11:08  feihu_h  阅读(919)  评论(0编辑  收藏  举报

导航