Repository navigation
Expand file tree
/
Copy pathbuffer.h
More file actions
156 lines (136 loc) · 6.7 KB
/
Copy pathbuffer.h
File metadata and controls
156 lines (136 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Deathray2 - An Avisynth plug-in filter for spatial/temporal non-local means de-noising.
*
* version 1.00
*
* Copyright 2015, Jawed Ashraf - Deathray@cupidity.f9.co.uk
*/
#ifndef _BUFFER_H_
#define _BUFFER_H_
#include <CL/cl.h>
enum result;
// Mem
// Abstract class used to track the lifetime of all types of buffer on the device.
class Mem {
public:
Mem();
virtual ~Mem() = 0;
// obj
// Returns the OpenCL memory buffer. Used to manipulate the
// OpenCL Mem object.
cl_mem obj();
// ptr
// Pointer for the memory buffer. Used when setting arguments for
// a kernel call.
cl_mem* ptr();
// valid
// Indicates that the buffer is ready to be used
bool valid() {return valid_;}
protected:
bool valid_ ; // buffer is not usable unless set up correctly
cl_command_queue cq_ ; // buffer is associated with a single device
cl_mem mem_ ; // OpenCL buffer object
};
// Buffer
// Scalar or vector float, int, uint etc. buffer on the device
// Client has to know size in bytes and data movements
// are purely by byte count
class Buffer: public Mem {
public:
// Init
// Set up a buffer based upon required capacity
virtual void Init(
const cl_command_queue &cq, // Specifies the device
const size_t &bytes); // required size in bytes at the time of creation
// CopyTo
// Copies specified byte count data to device buffer from host buffer.
// The copy is blocking, i.e. guaranteed complete on return.
virtual result CopyTo(
const void *host_buffer, // host's buffer as source
const size_t &bytes); // size in bytes to copy
// CopyFrom
// Copies specified byte count data to host buffer from device buffer.
// The copy is blocking, i.e. guaranteed complete on return.
virtual result CopyFrom(
const size_t &bytes, // size in bytes to copy
void *host_buffer); // host's buffer as destination
private:
size_t bytes_; // size in bytes
};
// Plane
// A 2D read/write image buffer of pixels stored in fours
// linearly, i.e. four pixels adjacent on a row.
// Includes padding:
// - final byte4 on each row is padded with zeroes if necessary
// - width and height of the buffer is further expanded and
// zero-padded to work-around an underlying CAL bug
class Plane: public Mem {
public:
// Init
// Set up a plane based upon pixel dimensions.
//
// Width and height are constrained. These constraints
// can be used to ensure that the buffer size is a
// multiple of the access sizes used by a kernel. e.g.
// a kernel that accesses pixels in horizontal strips
// of 4 (uchar4, effectively) can specify a
// width_constraint of 2.
virtual void Init(
const cl_command_queue &cq, // command queue, corresponds with the device holding the buffer
const int &width, // width in pixels
const int &height, // height in pixels
const int &width_constraint, // power of 2 specifier for width of buffer
const int &height_constraint);// power of 2 specifier for height of buffer
// CopyTo
// Copy pixels from host buffer to device buffer
// Method returns immediately, i.e. copy completion
// is not guaranteed upon return
result CopyTo(
const unsigned char &host_buffer, // host's buffer of pixels in row major layout
const int &host_cols, // count of pixels per row to be copied
const int &host_rows, // count of rows to be copied
const int &host_pitch); // size in pixels of each row of host buffer
// CopyToAsynch
// Copy pixels from host buffer to device buffer.
// Method returns immediately, i.e. copy completion
// is not guaranteed upon return. Event can be used
// to discern when copy has finished.
result CopyToAsynch(
const unsigned char &host_buffer, // host's buffer of pixels in row major layout
const int &host_cols, // count of pixels per row to be copied
const int &host_rows, // count of rows to be copied
const int &host_pitch, // size in pixels of each row of host buffer
cl_event *event); // event to track completion of this copy
// CopyFrom
// Copy pixels from device buffer to host buffer.
// Method returns immediately, i.e. copy completion
// is not guaranteed upon return
result CopyFrom(
const int &host_cols, // count of pixels per row to be copied
const int &host_rows, // count of rows to be copied
const int &host_pitch, // size in pixels of each row of host buffer
unsigned char *host_buffer); // host's buffer of floats in row major layout
// CopyFromAsynch
// Copy pixels from device buffer to host buffer.
// Method returns immediately, i.e. copy completion
// is not guaranteed upon return.
//
// This copy will not start until the antecedent event's
// completion. Set to NULL for an immediate copy.
//
// Event can be used to discern when copy has finished.
result CopyFromAsynch(
const int &host_cols, // count of pixels per row to be copied
const int &host_rows, // count of rows to be copied
const int &host_pitch, // size in pixels of each row of host buffer
const cl_event *antecedent, // event that must complete before this copy can start
cl_event *event, // event to track completion of this copy
unsigned char *host_buffer); // host's buffer of floats in row major layout
protected:
int width_; // width of plane buffer in pixels
int height_; // height of plane buffer
};
// GetFormatPixel
// Returns a structure containing the correct settings
// for a 2D buffer of pixels organised in 4s horizontally.
cl_image_format GetFormatPixel();
#endif // _BUFFER_H_