clray

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