Repository navigation
Expand file tree
/
Copy pathbuffer_map.cpp
More file actions
178 lines (146 loc) · 4.18 KB
/
Copy pathbuffer_map.cpp
File metadata and controls
178 lines (146 loc) · 4.18 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Deathray - An Avisynth plug-in filter for spatial/temporal non-local means de-noising.
*
* version 1.04
*
* Copyright 2013, Jawed Ashraf - Deathray@cupidity.f9.co.uk
*/
#include "buffer_map.h"
#include "CLutil.h"
int buffer_map::NewIndex() {
if (buffer_map_.size() != 0)
return buffer_map_.rbegin()->first + 1;
else
return 1;
}
result buffer_map::Append(mem **new_mem, int *new_index) {
pair<map<int, mem*>::iterator, bool> insertionStatus;
*new_index = NewIndex();
insertionStatus = buffer_map_.insert(pair<int, mem*>(*new_index, *new_mem));
if (insertionStatus.second)
return FILTER_OK;
else
return FILTER_ERROR;
}
result buffer_map::AllocBuffer(
const cl_command_queue &cq,
const size_t &bytes,
int *new_index) {
result status = FILTER_OK;
buffer *new_float_buffer = new buffer;
new_float_buffer->Init(cq, bytes);
if (new_float_buffer->valid()) {
mem *new_mem = new_float_buffer;
status = Append(&new_mem, new_index);
return status;
} else {
return FILTER_BUFFER_ALLOCATION_FAILED;
}
}
result buffer_map::CopyToBuffer(
const int &index,
const void *host_buffer,
const size_t &bytes) {
buffer *destination;
destination = static_cast<buffer*>(buffer_map_[index]);
return destination->CopyTo(host_buffer, bytes);
}
result buffer_map::CopyFromBuffer(
const int &index,
const size_t &bytes,
void *host_buffer) {
buffer *source;
source = static_cast<buffer*>(buffer_map_[index]);
return source->CopyFrom(bytes, host_buffer);
}
result buffer_map::AllocPlane(
const cl_command_queue &cq,
const int &width,
const int &height,
int *new_index) {
result status = FILTER_OK;
plane *new_plane = new plane;
new_plane->Init(cq, width, height, 2, 0);
if (new_plane->valid()) {
mem *new_mem = new_plane;
status = Append(&new_mem, new_index);
return status;
} else {
return FILTER_PLANE_ALLOCATION_FAILED;
}
}
result buffer_map::CopyToPlane(
const int &index,
const byte &host_buffer,
const int &host_cols,
const int &host_rows,
const int &host_pitch) {
plane *destination;
destination = static_cast<plane*>(buffer_map_[index]);
return destination->CopyTo(host_buffer, host_cols, host_rows, host_pitch);
}
result buffer_map::CopyToPlaneAsynch(
const int &index,
const byte &host_buffer,
const int &host_cols,
const int &host_rows,
const int &host_pitch,
cl_event *event) {
plane *destination;
destination = static_cast<plane*>(buffer_map_[index]);
return destination->CopyToAsynch(host_buffer, host_cols, host_rows, host_pitch, event);
}
result buffer_map::CopyFromPlane(
const int &index,
const int &host_cols,
const int &host_rows,
const int &host_pitch,
byte *host_buffer) {
plane *source;
source = static_cast<plane*>(buffer_map_[index]);
return source->CopyFrom(host_cols, host_rows, host_pitch, host_buffer);
}
result buffer_map::CopyFromPlaneAsynch(
const int &index,
const int &host_cols,
const int &host_rows,
const int &host_pitch,
const cl_event *antecedent,
cl_event *event,
byte *host_buffer) {
plane *source;
source = static_cast<plane*>(buffer_map_[index]);
return source->CopyFromAsynch(host_cols, host_rows, host_pitch, antecedent, event, host_buffer);
}
void buffer_map::Destroy(const int &index) {
if (! ValidIndex(index)) return;
// Check reference count
cl_uint reference_count = 0;
clGetMemObjectInfo(buffer_map_[index]->obj(),
CL_MEM_REFERENCE_COUNT,
sizeof(cl_uint),
&reference_count,
0);
// Repeatedly release it to ensure it will be destroyed
for (cl_uint i = 0; i < reference_count; ++i) {
clReleaseMemObject(buffer_map_[index]->obj());
}
buffer_map_.erase(index);
}
void buffer_map::DestroyAll() {
map<int, mem*>::iterator each_buffer;
map<int, mem*>::iterator next_buffer;
each_buffer = buffer_map_.begin();
while (each_buffer != buffer_map_.end()) {
next_buffer = each_buffer;
++next_buffer;
Destroy(each_buffer->first);
each_buffer = next_buffer;
}
buffer_map_.clear();
}
bool buffer_map::ValidIndex(const int &index) {
return buffer_map_.count(index) > 0;
}
cl_mem* buffer_map::ptr(const int &index) {
return buffer_map_[index]->ptr();
}