clray

annotate src/ocl.h @ 42:1169f3d04135

added CL/GL interop support for macosx (compiles) and windows (untested)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 28 Aug 2010 02:01:16 +0100
parents 1bcbb53b3505
children 8047637961a2
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@39 44 CLMemBuffer *create_mem_buffer(int rdwr, size_t sz, const void *buf);
nuclear@41 45
nuclear@41 46 CLMemBuffer *create_image_buffer(int rdwr, int xsz, int ysz, const void *pixels = 0);
nuclear@41 47 CLMemBuffer *create_image_buffer(int rdwr, unsigned int tex);
nuclear@41 48
nuclear@0 49 void destroy_mem_buffer(CLMemBuffer *mbuf);
nuclear@0 50
nuclear@39 51 void *map_mem_buffer(CLMemBuffer *mbuf, int rdwr, cl_event *ev = 0);
nuclear@39 52 void unmap_mem_buffer(CLMemBuffer *mbuf, cl_event *ev = 0);
nuclear@0 53
nuclear@39 54 bool write_mem_buffer(CLMemBuffer *mbuf, size_t sz, const void *src, cl_event *ev = 0);
nuclear@39 55 bool read_mem_buffer(CLMemBuffer *mbuf, size_t sz, void *dest, cl_event *ev = 0);
nuclear@39 56
nuclear@39 57 bool acquire_gl_object(CLMemBuffer *mbuf, cl_event *ev = 0);
nuclear@39 58 bool release_gl_object(CLMemBuffer *mbuf, cl_event *ev = 0);
nuclear@0 59
nuclear@1 60 enum {
nuclear@1 61 ARGTYPE_NONE,
nuclear@1 62
nuclear@1 63 ARGTYPE_INT,
nuclear@1 64 ARGTYPE_FLOAT,
nuclear@1 65 ARGTYPE_FLOAT4,
nuclear@1 66 ARGTYPE_MEM_BUF
nuclear@1 67 };
nuclear@1 68
nuclear@1 69 struct CLArg {
nuclear@1 70 int type;
nuclear@1 71 union {
nuclear@1 72 int ival;
nuclear@1 73 float fval;
nuclear@1 74 cl_float4 vval;
nuclear@1 75 CLMemBuffer *mbuf;
nuclear@1 76 } v;
John@14 77
John@14 78 CLArg();
nuclear@1 79 };
nuclear@1 80
nuclear@0 81
nuclear@0 82 class CLProgram {
nuclear@0 83 private:
nuclear@0 84 std::string kname;
nuclear@0 85 cl_program prog;
nuclear@0 86 cl_kernel kernel;
nuclear@1 87 std::vector<CLArg> args;
nuclear@0 88 bool built;
nuclear@39 89 mutable cl_event wait_event;
nuclear@39 90 mutable cl_event last_event;
nuclear@0 91
nuclear@0 92 public:
nuclear@0 93 CLProgram(const char *kname);
nuclear@0 94 ~CLProgram();
nuclear@0 95
nuclear@0 96 bool load(const char *fname);
nuclear@0 97
nuclear@1 98 bool set_argi(int arg, int val);
nuclear@1 99 bool set_argf(int arg, float val);
nuclear@28 100 bool set_arg_buffer(int arg, int rdwr, size_t sz, const void *buf = 0);
nuclear@41 101 bool set_arg_image(int arg, int rdwr, int xsz, int ysz, const void *pix = 0);
nuclear@39 102 bool set_arg_texture(int arg, int rdwr, unsigned int tex);
nuclear@0 103 CLMemBuffer *get_arg_buffer(int arg);
John@14 104 int get_num_args() const;
nuclear@0 105
nuclear@0 106 bool build();
nuclear@0 107
nuclear@0 108 bool run() const;
nuclear@0 109 bool run(int dim, ...) const;
nuclear@39 110
nuclear@39 111 // sets an event that has to be completed before running the kernel
nuclear@39 112 void set_wait_event(cl_event ev);
nuclear@39 113
nuclear@39 114 // gets the last event so that we can wait for it to finish
nuclear@39 115 cl_event get_last_event() const;
nuclear@0 116 };
nuclear@0 117
nuclear@0 118 #endif /* OCL_H_ */