forked from Zyko0/go-opencl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurego_library_unix.go
181 lines (174 loc) · 5.08 KB
/
purego_library_unix.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
//go:build !windows && !wasm
package pureCL
import (
"errors"
"github.com/ebitengine/purego"
"runtime"
"unsafe"
)
func getOpenCLPath() ([]string, error) {
if runtime.GOOS == "linux" {
return []string{
"/usr/lib/libOpenCL.so",
"/usr/local/lib/libOpenCL.so",
"/usr/local/lib/libpocl.so",
"/usr/lib64/libOpenCL.so",
"/usr/lib32/libOpenCL.so",
"libOpenCL.so"}, nil
} else if runtime.GOOS == "darwin" {
return []string{
"libOpenCL.so",
"/System/Library/Frameworks/OpenCL.framework/OpenCL"}, nil
} else if runtime.GOOS == "android" {
return []string{
"/system/lib64/libOpenCL.so",
"/system/vendor/lib64/libOpenCL.so",
"/system/vendor/lib64/egl/libGLES_mali.so",
"/system/vendor/lib64/libPVROCL.so",
"/data/data/org.pocl.libs/files/lib64/libpocl.so",
"/system/lib/libOpenCL.so",
"/system/vendor/lib/libOpenCL.so",
"/system/vendor/lib/egl/libGLES_mali.so",
"/system/lib64/egl/libGLES_mali.so",
"/system/vendor/lib/libPVROCL.so",
"/data/data/org.pocl.libs/files/lib/libpocl.so",
"libOpenCL.so"}, nil
}
return nil, errors.New("unknown system paths")
}
func loadLibrary(paths []string) error {
if handle != 0 {
return nil
}
paths2, err := getOpenCLPath()
if err != nil {
return err
}
paths = append(paths, paths2...)
var resultError error
for i := 0; i < len(paths); i++ {
libOpenCl, err0 := purego.Dlopen(paths[i], purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err0 == nil {
handle = libOpenCl
return nil
}
resultError = ErrJoin(resultError, err0)
}
return errors.New("no path has passed:\n" + resultError.Error())
}
func initUnsupported(handle uintptr, errIn error) error {
readImg, err := purego.Dlsym(handle, "clEnqueueReadImage")
if err != nil {
errIn = err
}
mapImg, err := purego.Dlsym(handle, "clEnqueueMapImage")
if err != nil {
errIn = errors.Join(errIn, err)
}
mapBuffer, err := purego.Dlsym(handle, "clEnqueueMapBuffer")
if err != nil {
errIn = errors.Join(errIn, err)
}
writeImg, err := purego.Dlsym(handle, "clEnqueueWriteImage")
if err != nil {
errIn = errors.Join(errIn, err)
}
// Note: Functions with unsupported arguments requiring syscall loading
EnqueueReadImage = func(queue CommandQueue, image Buffer, blockingRead bool, origin, region [3]Size, row_pitch, slice_pitch Size, ptr unsafe.Pointer, numEventsWaitList uint, eventWaitList []Event, event *Event) Status {
block := uintptr(0)
if blockingRead {
block = 1
}
eventList := uintptr(0)
if eventWaitList != nil {
eventList = uintptr(unsafe.Pointer(&eventWaitList[0]))
}
r1, _, _ := purego.SyscallN(readImg,
uintptr(queue),
uintptr(image),
block,
uintptr(unsafe.Pointer(&origin[0])),
uintptr(unsafe.Pointer(®ion[0])),
uintptr(row_pitch),
uintptr(slice_pitch),
uintptr(ptr),
uintptr(numEventsWaitList),
eventList,
uintptr(unsafe.Pointer(event)),
0)
return Status(r1)
}
EnqueueMapImage = func(queue CommandQueue, image Buffer, blockingMap bool, mapFlags MapFlag, origin, region [3]Size, imageRowPitch, imageSlicePitch *Size, numEventsWaitList uint, eventWaitList []Event, event *Event, errCodeRet *Status) uintptr {
block := uintptr(0)
if blockingMap {
block = 1
}
eventList := uintptr(0)
if eventWaitList != nil {
eventList = uintptr(unsafe.Pointer(&eventWaitList[0]))
}
r1, _, _ := purego.SyscallN(mapImg,
uintptr(queue),
uintptr(image),
block,
uintptr(mapFlags),
uintptr(unsafe.Pointer(&origin[:][0])),
uintptr(unsafe.Pointer(®ion[:][0])),
uintptr(unsafe.Pointer(imageRowPitch)),
uintptr(unsafe.Pointer(imageSlicePitch)),
uintptr(numEventsWaitList),
eventList,
uintptr(unsafe.Pointer(event)),
uintptr(unsafe.Pointer(errCodeRet)),
)
return r1
}
EnqueueMapBuffer = func(queue CommandQueue, buffer Buffer, blockingMap bool, mapFlags MapFlag, offset, size Size, numEventsWaitList uint, eventWaitList []Event, event *Event, errCodeRet *Status) uintptr {
block := uintptr(0)
if blockingMap {
block = 1
}
eventList := uintptr(0)
if eventWaitList != nil {
eventList = uintptr(unsafe.Pointer(&eventWaitList[0]))
}
r1, _, _ := purego.SyscallN(mapBuffer,
uintptr(queue),
uintptr(buffer),
uintptr(block),
uintptr(mapFlags),
uintptr(offset),
uintptr(size),
uintptr(numEventsWaitList),
eventList,
uintptr(unsafe.Pointer(event)),
uintptr(unsafe.Pointer(errCodeRet)),
)
return r1
}
EnqueueWriteImage = func(queue CommandQueue, image Buffer, blockingRead bool, origin, region [3]Size, row_pitch, slice_pitch Size, ptr unsafe.Pointer, numEventsWaitList uint, eventWaitList []Event, event *Event) Status {
block := uintptr(0)
if blockingRead {
block = 1
}
eventList := uintptr(0)
if eventWaitList != nil {
eventList = uintptr(unsafe.Pointer(&eventWaitList[0]))
}
r1, _, _ := purego.SyscallN(writeImg,
uintptr(queue),
uintptr(image),
uintptr(block),
uintptr(unsafe.Pointer(&origin[0])),
uintptr(unsafe.Pointer(®ion[0])),
uintptr(row_pitch),
uintptr(slice_pitch),
uintptr(ptr),
uintptr(numEventsWaitList),
eventList,
uintptr(unsafe.Pointer(event)),
)
return Status(r1)
}
return errIn
}