Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
clfBuffer.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <Base/Common/BIASpragma.hh>
26 #include <OpenCLFramework/clfBuffer.hh>
27 #include <OpenCLFramework/clfException.hh>
28 #include <Base/Debug/Error.hh>
29 
30 using namespace BIAS;
31 
32 clfBuffer::clfBuffer(cl::Context *context, cl::CommandQueue *queue) : clfMemory(context, queue) {
33  rows_=0;
34  cols_=0;
35  itemsize_=0;
36  stride_=0;
37  channels_ = 1;
38  cache_ = false;
39 }
40 
42  //TODO: Do we need to free memory if allocated???
43 }
44 
45 void clfBuffer::Allocate(unsigned int bufsize, bool readonly, bool writeonly, void *hostptr, bool copy) {
46  int cl_mem_flags = DetermineMemFlags_(readonly, writeonly, hostptr, copy);
47  if (initialized_ && !sharedGL_ && size_ == bufsize) {
48  // already allocated with correct size
49  return;
50  }
51  size_ = bufsize;
52  try {
53  buffer_ = cl::Buffer(*context_, cl_mem_flags, size_, hostptr);
54  } catch(cl::Error &err) {
55  THROW_CL_EXCEPTION(err);
56  }
57  initialized_ = true;
58  sharedGL_ = false;
59 }
60 
61 void clfBuffer::Allocate(unsigned int rows, unsigned int cols, unsigned int channels, unsigned int itemsize, bool readonly, bool writeonly, void *hostptr, bool copy) {
62  int cl_mem_flags = DetermineMemFlags_(readonly, writeonly, hostptr, copy);
63  if (cache_) {
64  if (readonly) {
65  cl_mem_flags = CL_READ_ONLY_CACHE;
66  } else {
67  cl_mem_flags = CL_READ_WRITE_CACHE;
68  }
69  }
70  if (initialized_ && !sharedGL_ && size_ == rows*cols*itemsize) {
71  // already allocated with correct size, just grab new values
72  size_ = rows*cols*itemsize;
73  rows_=rows;
74  cols_=cols;
75  channels_ = channels;
76  itemsize_=itemsize;
77  stride_=cols*itemsize;
78  return;
79  }
80  size_ = rows*cols*itemsize*channels;
81  rows_=rows;
82  cols_=cols;
83  itemsize_=itemsize;
84  stride_=cols*itemsize;
85  channels_=channels;
86 
87  try {
88  buffer_ = cl::Buffer(*context_, cl_mem_flags, size_, hostptr);
89  } catch(cl::Error &err) {
90  THROW_CL_EXCEPTION(err);
91  }
92  initialized_ = true;
93  sharedGL_ = false;
94 }
95 
96 void clfBuffer::AllocateFromVertexBuffer(BIAS::glfVertexBuffer &vbo, bool readonly, bool writeonly) {
97  int cl_mem_flags = DetermineMemFlags_(readonly, writeonly);
98  try {
99  buffer_ = cl::BufferGL(*context_, cl_mem_flags, vbo.GetBufferID());
100  } catch(cl::Error &err) {
101  THROW_CL_EXCEPTION(err);
102  }
104  initialized_ = true;
105  sharedGL_ = true;
106  glid_ = vbo.GetBufferID();
107 }
108 
109 void clfBuffer::SetCached(bool cache) {
110  cache_ = cache;
111 }
112 
113 cl::Buffer& clfBuffer::buffer() {
114  return (cl::Buffer&)buffer_;
115 }
116 
117 void clfBuffer::WriteToBuffer(const void *data, unsigned int offset, unsigned int size) {
118  if (size == 0) {
119  size = size_;
120  }
121  try {
122  queue_->enqueueWriteBuffer(buffer(), CL_TRUE, offset, size, data);
123  } catch (cl::Error &error) {
124  THROW_CL_EXCEPTION(error);
125  }
126 }
127 
128 void clfBuffer::ReadFromBuffer(void *data, unsigned int offset, unsigned int size) {
129  if (size == 0) {
130  size = size_;
131  }
132  try {
133  queue_->enqueueReadBuffer(buffer(), CL_TRUE, offset, size, data);
134  } catch (cl::Error &error) {
135  THROW_CL_EXCEPTION(error);
136  }
137 }
138 
139 void clfBuffer::CopyBuffer(clfBuffer &outputbuffer, unsigned int srcoffset, unsigned int dstoffset, unsigned int size) {
140  if (size == 0) {
141  size = size_;
142  }
143  try {
144  cl::Event event;
145  queue_->enqueueCopyBuffer(buffer(), outputbuffer.buffer(), srcoffset, dstoffset, size, NULL, &event);
146  event.wait();
147  } catch (cl::Error &error) {
148  THROW_CL_EXCEPTION(error);
149  }
150 }
151 
152 void* clfBuffer::MapBuffer(bool write, unsigned int offset, unsigned int size) {
153  try {
154  cl_map_flags mapflags = 0;
155  if (write) {
156  mapflags = CL_MAP_WRITE;
157  } else {
158  mapflags = CL_MAP_READ;
159  }
160  if (size == 0) {
161  size = size_;
162  }
163  return queue_->enqueueMapBuffer(buffer(), CL_TRUE, mapflags, offset, size);
164  } catch (cl::Error &error) {
165  THROW_CL_EXCEPTION(error);
166  }
167  return NULL;
168 }
169 
170 unsigned int clfBuffer::GetRows() const {
171  return rows_;
172 }
173 unsigned int clfBuffer::GetCols() const {
174  return cols_;
175 }
176 unsigned int clfBuffer::GetItemSize() const {
177  return itemsize_;
178 }
179 unsigned int clfBuffer::GetStride() const {
180  return stride_;
181 }
182 
183 unsigned int clfBuffer::GetChannels() const {
184  return channels_;
185 }
186 
cl::Buffer & buffer()
Definition: clfBuffer.cpp:113
unsigned int GetItemSize() const
Definition: clfBuffer.cpp:176
int GetNumVertices() const
Returns the number of vertices in the vertex buffer.
void SetCached(bool cache)
Definition: clfBuffer.cpp:109
void * MapBuffer(bool write=false, unsigned int offset=0, unsigned int size=0)
Definition: clfBuffer.cpp:152
void AllocateFromVertexBuffer(BIAS::glfVertexBuffer &vbo, bool readonly=false, bool writeonly=false)
Allocation of a memory buffer from a GL vertexBuffer (works only on shared context!) ...
Definition: clfBuffer.cpp:96
OpenCL Buffer wrapper.
Definition: clfBuffer.hh:43
unsigned int glid_
Definition: clfMemory.hh:61
unsigned int GetCols() const
Definition: clfBuffer.cpp:173
A vertex buffer contains an array of vertices that can be used for rendering.
unsigned int GetChannels() const
Definition: clfBuffer.cpp:183
unsigned int GetStride() const
Definition: clfBuffer.cpp:179
int DetermineMemFlags_(bool readonly, bool writeonly, const void *hostptr=NULL, bool copy=false)
Definition: clfMemory.cpp:73
cl::Context * context_
Definition: clfMemory.hh:54
cl::CommandQueue * queue_
Definition: clfMemory.hh:55
void ReadFromBuffer(void *data, unsigned int offset=0, unsigned int size=0)
read from buffer object to host memory
Definition: clfBuffer.cpp:128
void Allocate(unsigned int bufsize, bool readonly=false, bool writeonly=false, void *hostptr=NULL, bool copy=false)
Allocation of a memory buffer A memory buffer can be created on device or host, it can be initialized...
Definition: clfBuffer.cpp:45
virtual ~clfBuffer()
Definition: clfBuffer.cpp:41
int GetVertexSize() const
Returns the size in bytes of a single vertex.
unsigned int GetRows() const
Definition: clfBuffer.cpp:170
clfBuffer(cl::Context *context, cl::CommandQueue *queue_)
Definition: clfBuffer.cpp:32
void CopyBuffer(clfBuffer &outputbuffer, unsigned int srcoffset=0, unsigned int dstoffset=0, unsigned int size=0)
copy from one buffer to another
Definition: clfBuffer.cpp:139
void WriteToBuffer(const void *data, unsigned int offset=0, unsigned int size=0)
write from host memory to buffer object
Definition: clfBuffer.cpp:117
cl::Memory buffer_
Definition: clfMemory.hh:56
glfVertexFormat GetFormat() const
unsigned int size_
Definition: clfMemory.hh:58