clray
view src/ocl.h @ 51:9c0867942612
changed the binary target to clray
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 02 Sep 2010 01:41:51 +0100 |
parents | 057b8575a1c1 |
children | 8c858e1a89e8 |
line source
1 #ifndef OCL_H_
2 #define OCL_H_
4 #include <vector>
5 #include <string>
6 #ifndef __APPLE__
7 #include <CL/cl.h>
8 #include <CL/cl_gl.h>
9 #else
10 #include <OpenCL/opencl.h>
11 #endif
13 enum {
14 ARG_RD = CL_MEM_READ_ONLY,
15 ARG_WR = CL_MEM_WRITE_ONLY,
16 ARG_RDWR = CL_MEM_READ_WRITE
17 };
19 enum {
20 MAP_RD = CL_MAP_READ,
21 MAP_WR = CL_MAP_WRITE,
22 MAP_RDWR = CL_MAP_READ | CL_MAP_WRITE
23 };
25 enum {
26 MEM_BUFFER,
27 IMAGE_BUFFER
28 };
30 struct CLMemBuffer {
31 int type;
32 cl_mem mem;
34 size_t size;
35 size_t xsz, ysz;
36 void *ptr;
37 unsigned int tex;
38 };
41 bool init_opencl();
42 void destroy_opencl();
44 CLMemBuffer *create_mem_buffer(int rdwr, size_t sz, const void *buf);
46 CLMemBuffer *create_image_buffer(int rdwr, int xsz, int ysz, const void *pixels = 0);
47 CLMemBuffer *create_image_buffer(int rdwr, unsigned int tex);
49 void destroy_mem_buffer(CLMemBuffer *mbuf);
51 void *map_mem_buffer(CLMemBuffer *mbuf, int rdwr, cl_event *ev = 0);
52 void unmap_mem_buffer(CLMemBuffer *mbuf, cl_event *ev = 0);
54 bool write_mem_buffer(CLMemBuffer *mbuf, size_t sz, const void *src, cl_event *ev = 0);
55 bool read_mem_buffer(CLMemBuffer *mbuf, size_t sz, void *dest, cl_event *ev = 0);
57 bool acquire_gl_object(CLMemBuffer *mbuf, cl_event *ev = 0);
58 bool release_gl_object(CLMemBuffer *mbuf, cl_event *ev = 0);
60 enum {
61 ARGTYPE_NONE,
63 ARGTYPE_INT,
64 ARGTYPE_FLOAT,
65 ARGTYPE_FLOAT4,
66 ARGTYPE_MEM_BUF
67 };
69 struct CLArg {
70 int type;
71 union {
72 int ival;
73 float fval;
74 cl_float4 vval;
75 CLMemBuffer *mbuf;
76 } v;
78 CLArg();
79 };
82 class CLProgram {
83 private:
84 std::string kname;
85 cl_program prog;
86 cl_kernel kernel;
87 std::vector<CLArg> args;
88 bool built;
89 mutable cl_event wait_event;
90 mutable cl_event last_event;
92 public:
93 CLProgram(const char *kname);
94 ~CLProgram();
96 bool load(const char *fname);
98 bool set_argi(int arg, int val);
99 bool set_argf(int arg, float val);
100 bool set_arg_buffer(int arg, int rdwr, size_t sz, const void *buf = 0);
101 bool set_arg_image(int arg, int rdwr, int xsz, int ysz, const void *pix = 0);
102 bool set_arg_texture(int arg, int rdwr, unsigned int tex);
103 CLMemBuffer *get_arg_buffer(int arg);
104 int get_num_args() const;
106 bool build(const char *opt = 0);
108 bool run() const;
109 bool run(int dim, ...) const;
111 // sets an event that has to be completed before running the kernel
112 void set_wait_event(cl_event ev);
114 // gets the last event so that we can wait for it to finish
115 cl_event get_last_event() const;
116 };
118 #endif /* OCL_H_ */