This repository has been archived by the owner on Jan 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
318 lines (302 loc) · 10.1 KB
/
buffer.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package cl30
// #include "api.h"
import "C"
import (
"unsafe"
)
// CreateBuffer creates a buffer object.
//
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clCreateBuffer.html
func CreateBuffer(context Context, flags MemFlags, size int, hostPtr unsafe.Pointer) (MemObject, error) {
var status C.cl_int
mem := C.clCreateBuffer(
context.handle(),
C.cl_mem_flags(flags),
C.size_t(size),
hostPtr,
&status)
if status != C.CL_SUCCESS {
return 0, StatusError(status)
}
return MemObject(*((*uintptr)(unsafe.Pointer(&mem)))), nil
}
// CreateBufferWithProperties creates a buffer object.
//
// Since: 3.0
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clCreateBufferWithProperties.html
func CreateBufferWithProperties(context Context, flags MemFlags, size int, hostPtr unsafe.Pointer, properties ...MemProperty) (MemObject, error) {
var rawPropertyList []uint64
for _, property := range properties {
rawPropertyList = append(rawPropertyList, property...)
}
var rawProperties unsafe.Pointer
if len(properties) > 0 {
rawPropertyList = append(rawPropertyList, 0)
rawProperties = unsafe.Pointer(&rawPropertyList[0])
}
var status C.cl_int
mem := C.clCreateBufferWithProperties(
context.handle(),
(*C.cl_mem_properties)(rawProperties),
C.cl_mem_flags(flags),
C.size_t(size),
hostPtr,
&status)
if status != C.CL_SUCCESS {
return 0, StatusError(status)
}
return MemObject(*((*uintptr)(unsafe.Pointer(&mem)))), nil
}
// BufferCreateType determines the kind of sub-buffer object.
type BufferCreateType C.cl_buffer_create_type
const (
// BufferCreateTypeRegion describes a buffer object that represents a specific region in buffer.
//
// Creation data type: BufferRegion
// Since: 1.1
BufferCreateTypeRegion BufferCreateType = C.CL_BUFFER_CREATE_TYPE_REGION
)
// BufferRegionByteSize is the size, in bytes, of the BufferRegion structure.
const BufferRegionByteSize = unsafe.Sizeof(C.cl_buffer_region{})
// BufferRegion describes a subset of a buffer.
//
// Since: 1.1
type BufferRegion struct {
Origin uintptr
Size uintptr
}
// CreateSubBuffer creates a new buffer object (referred to as a sub-buffer object) from an existing buffer object.
//
// The createInfo parameter is dependent on the specified createType.
//
// Since: 1.1
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clCreateSubBuffer.html
func CreateSubBuffer(buffer MemObject, flags MemFlags, createType BufferCreateType, createInfo unsafe.Pointer) (MemObject, error) {
var status C.cl_int
mem := C.clCreateSubBuffer(
buffer.handle(),
C.cl_mem_flags(flags),
C.cl_buffer_create_type(createType),
createInfo,
&status)
if status != C.CL_SUCCESS {
return 0, StatusError(status)
}
return MemObject(*((*uintptr)(unsafe.Pointer(&mem)))), nil
}
// EnqueueMapBuffer enqueues a command to map a region of a buffer object into the host address space and
// returns a pointer to this mapped region.
//
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueMapBuffer.html
func EnqueueMapBuffer(commandQueue CommandQueue,
buffer MemObject, blocking bool, flags MapFlags, offset, size uintptr,
waitList []Event, event *Event) (unsafe.Pointer, error) {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
var status C.cl_int
ptr := C.clEnqueueMapBuffer(
commandQueue.handle(),
buffer.handle(),
C.cl_bool(BoolFrom(blocking)),
C.cl_map_flags(flags),
C.size_t(offset),
C.size_t(size),
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)),
&status)
if status != C.CL_SUCCESS {
return nil, StatusError(status)
}
return ptr, nil
}
// EnqueueReadBuffer enqueues a command to read from a buffer object to host memory.
//
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueReadBuffer.html
func EnqueueReadBuffer(commandQueue CommandQueue, mem MemObject, blockingRead bool, offset, size uintptr, data unsafe.Pointer,
waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueReadBuffer(
commandQueue.handle(),
mem.handle(),
C.cl_bool(BoolFrom(blockingRead)),
C.size_t(offset),
C.size_t(size),
data,
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueReadBufferRect enqueues a command to read from a 2D or 3D rectangular region of a buffer object to
// host memory.
//
// Since: 1.1
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueReadBufferRect.html
func EnqueueReadBufferRect(commandQueue CommandQueue, mem MemObject, blockingRead bool, bufferOrigin, hostOrigin, region [3]uintptr,
bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch uintptr, data unsafe.Pointer, waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueReadBufferRect(
commandQueue.handle(),
mem.handle(),
C.cl_bool(BoolFrom(blockingRead)),
(*C.size_t)(unsafe.Pointer(&bufferOrigin[0])),
(*C.size_t)(unsafe.Pointer(&hostOrigin[0])),
(*C.size_t)(unsafe.Pointer(®ion[0])),
C.size_t(bufferRowPitch),
C.size_t(bufferSlicePitch),
C.size_t(hostRowPitch),
C.size_t(hostSlicePitch),
data,
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueWriteBuffer enqueues a command to write to a buffer object from host memory.
//
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueWriteBuffer.html
func EnqueueWriteBuffer(commandQueue CommandQueue, mem MemObject, blockingRead bool, offset, size uintptr, data unsafe.Pointer,
waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueWriteBuffer(
commandQueue.handle(),
mem.handle(),
C.cl_bool(BoolFrom(blockingRead)),
C.size_t(offset),
C.size_t(size),
data,
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueWriteBufferRect enqueues a command to write to a 2D or 3D rectangular region of a buffer object from
// host memory.
//
// Since: 1.1
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueWriteBufferRect.html
func EnqueueWriteBufferRect(commandQueue CommandQueue, mem MemObject, blockingRead bool, bufferOrigin, hostOrigin, region [3]uintptr,
bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch uintptr, data unsafe.Pointer, waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueWriteBufferRect(
commandQueue.handle(),
mem.handle(),
C.cl_bool(BoolFrom(blockingRead)),
(*C.size_t)(unsafe.Pointer(&bufferOrigin[0])),
(*C.size_t)(unsafe.Pointer(&hostOrigin[0])),
(*C.size_t)(unsafe.Pointer(®ion[0])),
C.size_t(bufferRowPitch),
C.size_t(bufferSlicePitch),
C.size_t(hostRowPitch),
C.size_t(hostSlicePitch),
data,
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueFillBuffer enqueues a command to fill a buffer object with a pattern of a given pattern size.
//
// Since: 1.2
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueFillBuffer.html
func EnqueueFillBuffer(commandQueue CommandQueue, mem MemObject, pattern unsafe.Pointer, patternSize, offset, size uintptr,
waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueFillBuffer(
commandQueue.handle(),
mem.handle(),
pattern,
C.size_t(patternSize),
C.size_t(offset),
C.size_t(size),
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueCopyBuffer enqueues a command to copy from one buffer object to another.
//
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueCopyBuffer.html
func EnqueueCopyBuffer(commandQueue CommandQueue, src, dst MemObject, srcOffset, dstOffset, size uintptr, waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueCopyBuffer(
commandQueue.handle(),
src.handle(),
dst.handle(),
C.size_t(srcOffset),
C.size_t(dstOffset),
C.size_t(size),
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueCopyBufferRect enqueues a command to copy a 2D or 3D rectangular region from a buffer object to another
// buffer object.
//
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueCopyBufferRect.html
func EnqueueCopyBufferRect(commandQueue CommandQueue, src, dst MemObject, srcOrigin, dstOrigin, region [3]uintptr,
srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch uintptr,
waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueCopyBufferRect(
commandQueue.handle(),
src.handle(),
dst.handle(),
(*C.size_t)(unsafe.Pointer(&srcOrigin[0])),
(*C.size_t)(unsafe.Pointer(&dstOrigin[0])),
(*C.size_t)(unsafe.Pointer(®ion[0])),
C.size_t(srcRowPitch),
C.size_t(srcSlicePitch),
C.size_t(dstRowPitch),
C.size_t(dstSlicePitch),
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}