clray
view src/ocl.h @ 62:d9520da6b801
minor readme fix
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 28 Dec 2015 10:31:58 +0200 |
parents | 8047637961a2 |
children |
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 void finish_opencl();
46 CLMemBuffer *create_mem_buffer(int rdwr, size_t sz, const void *buf);
48 CLMemBuffer *create_image_buffer(int rdwr, int xsz, int ysz, const void *pixels = 0);
49 CLMemBuffer *create_image_buffer(int rdwr, unsigned int tex);
51 void destroy_mem_buffer(CLMemBuffer *mbuf);
53 void *map_mem_buffer(CLMemBuffer *mbuf, int rdwr, cl_event *ev = 0);
54 void unmap_mem_buffer(CLMemBuffer *mbuf, cl_event *ev = 0);
56 bool write_mem_buffer(CLMemBuffer *mbuf, size_t sz, const void *src, cl_event *ev = 0);
57 bool read_mem_buffer(CLMemBuffer *mbuf, size_t sz, void *dest, cl_event *ev = 0);
59 bool acquire_gl_object(CLMemBuffer *mbuf, cl_event *ev = 0);
60 bool release_gl_object(CLMemBuffer *mbuf, cl_event *ev = 0);
62 enum {
63 ARGTYPE_NONE,
65 ARGTYPE_INT,
66 ARGTYPE_FLOAT,
67 ARGTYPE_FLOAT4,
68 ARGTYPE_MEM_BUF
69 };
71 struct CLArg {
72 int type;
73 union {
74 int ival;
75 float fval;
76 cl_float4 vval;
77 CLMemBuffer *mbuf;
78 } v;
80 CLArg();
81 };
84 class CLProgram {
85 private:
86 std::string kname;
87 cl_program prog;
88 cl_kernel kernel;
89 std::vector<CLArg> args;
90 bool built;
91 mutable cl_event wait_event;
92 mutable cl_event last_event;
94 public:
95 CLProgram(const char *kname);
96 ~CLProgram();
98 bool load(const char *fname);
100 bool set_argi(int arg, int val);
101 bool set_argf(int arg, float val);
102 bool set_arg_buffer(int arg, int rdwr, size_t sz, const void *buf = 0);
103 bool set_arg_image(int arg, int rdwr, int xsz, int ysz, const void *pix = 0);
104 bool set_arg_texture(int arg, int rdwr, unsigned int tex);
105 CLMemBuffer *get_arg_buffer(int arg);
106 int get_num_args() const;
108 bool build(const char *opt = 0);
110 bool run() const;
111 bool run(int dim, ...) const;
113 // sets an event that has to be completed before running the kernel
114 void set_wait_event(cl_event ev);
116 // gets the last event so that we can wait for it to finish
117 cl_event get_last_event() const;
118 };
120 #endif /* OCL_H_ */