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
/
Copy pathsampler.go
199 lines (180 loc) · 7.76 KB
/
sampler.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
package cl22
// #include "api.h"
import "C"
import (
"fmt"
"unsafe"
)
// Sampler objects describe how color information from an image is being sampled.
type Sampler uintptr
func (sampler Sampler) handle() C.cl_sampler {
return *(*C.cl_sampler)(unsafe.Pointer(&sampler))
}
// String provides a readable presentation of the sampler identifier.
// It is based on the numerical value of the underlying pointer.
func (sampler Sampler) String() string {
return fmt.Sprintf("0x%X", uintptr(sampler))
}
const (
// SamplerNormalizedCoordsProperty specifies whether the image coordinates are normalized or not.
//
// Use WithNormalizedCoords() for convenience.
//
// Property value type: Bool
SamplerNormalizedCoordsProperty uint64 = C.CL_SAMPLER_NORMALIZED_COORDS
// SamplerAddressingModeProperty specifies how out-of-range image coordinates are handled when reading from
// an image.
//
// Use WithAddressingMode() for convenience.
//
// Property value type: SamplerAddressingMode
SamplerAddressingModeProperty uint64 = C.CL_SAMPLER_ADDRESSING_MODE
// SamplerFilterModeProperty specifies the type of filter that is applied when reading an image.
//
// Use WithFilterMode() for convenience.
//
// Property value type: SamplerFilterMode
SamplerFilterModeProperty uint64 = C.CL_SAMPLER_FILTER_MODE
)
// SamplerProperty is one entry of properties which are taken into account when creating sampler objects.
type SamplerProperty []uint64
// WithNormalizedCoords is a convenience function to create a valid SamplerNormalizedCoordsProperty.
// Use it in combination with CreateSamplerWithProperties().
func WithNormalizedCoords(enabled bool) SamplerProperty {
return SamplerProperty{SamplerNormalizedCoordsProperty, uint64(BoolFrom(enabled))}
}
// SamplerAddressingMode specifies how out-of-range image coordinates are handled when reading from an image.
type SamplerAddressingMode C.cl_addressing_mode
const (
// AddressNoneMode specifies that behavior is undefined for out-of-range image coordinates.
AddressNoneMode SamplerAddressingMode = C.CL_ADDRESS_NONE
// AddressClampToEdgeMode specifies that out-of-range image coordinates are clamped to the edge of the image.
AddressClampToEdgeMode SamplerAddressingMode = C.CL_ADDRESS_CLAMP_TO_EDGE
// AddressClampMode specifies that out-of-range image coordinates are assigned a border color value.
AddressClampMode SamplerAddressingMode = C.CL_ADDRESS_CLAMP
// AddressRepeatMode specifies that out-of-range image coordinates read from the image as-if the image data were
// replicated in all dimensions.
AddressRepeatMode SamplerAddressingMode = C.CL_ADDRESS_REPEAT
// AddressMirroredRepeatMode specifies that out-of-range image coordinates read from the image as-if the image data
// were replicated in all dimensions, mirroring the image contents at the edge of each replication.
//
// Since: 1.1
AddressMirroredRepeatMode SamplerAddressingMode = C.CL_ADDRESS_MIRRORED_REPEAT
)
// WithAddressingMode is a convenience function to create a valid SamplerAddressingModeProperty.
// Use it in combination with CreateSamplerWithProperties().
func WithAddressingMode(mode SamplerAddressingMode) SamplerProperty {
return SamplerProperty{SamplerAddressingModeProperty, uint64(mode)}
}
// SamplerFilterMode specifies the type of filter that is applied when reading an image.
type SamplerFilterMode C.cl_filter_mode
const (
// FilterNearestMode returns the image element nearest to the image coordinate.
FilterNearestMode SamplerFilterMode = C.CL_FILTER_NEAREST
// FilterLinearMode returns a weighted average of the four image elements nearest to the image coordinate.
FilterLinearMode SamplerFilterMode = C.CL_FILTER_LINEAR
)
// WithFilterMode is a convenience function to create a valid SamplerFilterModeProperty.
// Use it in combination with CreateSamplerWithProperties().
func WithFilterMode(mode SamplerFilterMode) SamplerProperty {
return SamplerProperty{SamplerFilterModeProperty, uint64(mode)}
}
// CreateSamplerWithProperties creates a sampler object.
//
// See also: https://registry.khronos.org/OpenCL/sdk/2.2/docs/man/html/clCreateSamplerWithProperties.html
func CreateSamplerWithProperties(context Context, properties ...SamplerProperty) (Sampler, 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
sampler := C.clCreateSamplerWithProperties(
context.handle(),
(*C.cl_sampler_properties)(rawProperties),
&status)
if status != C.CL_SUCCESS {
return 0, StatusError(status)
}
return Sampler(*((*uintptr)(unsafe.Pointer(&sampler)))), nil
}
// RetainSampler increments the sampler reference count.
//
// CreateSamplerWithProperties() and CreateSampler() perform an implicit retain.
//
// See also: https://registry.khronos.org/OpenCL/sdk/2.2/docs/man/html/clRetainSampler.html
func RetainSampler(sampler Sampler) error {
status := C.clRetainSampler(sampler.handle())
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// ReleaseSampler decrements the sampler reference count.
//
// The sampler object is deleted after the reference count becomes zero and commands queued for execution on a
// command-queue(s) that use sampler have finished.
//
// See also: https://registry.khronos.org/OpenCL/sdk/2.2/docs/man/html/clReleaseSampler.html
func ReleaseSampler(sampler Sampler) error {
status := C.clReleaseSampler(sampler.handle())
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// SamplerInfoName identifies properties of a sampler, which can be queried with SamplerInfo().
type SamplerInfoName C.cl_sampler_info
const (
// SamplerReferenceCountInfo returns the sampler reference count.
//
// Note: The reference count returned should be considered immediately stale. It is unsuitable for general use
// in applications. This feature is provided for identifying memory leaks.
//
// Returned type: uint32
SamplerReferenceCountInfo SamplerInfoName = C.CL_SAMPLER_REFERENCE_COUNT
// SamplerContextInfo returns the context specified when the sampler is created.
//
// Returned type: Context
SamplerContextInfo SamplerInfoName = C.CL_SAMPLER_CONTEXT
// SamplerNormalizedCoordsInfo return the normalized coords value associated with sampler.
//
// Returned type: Bool
SamplerNormalizedCoordsInfo SamplerInfoName = C.CL_SAMPLER_NORMALIZED_COORDS
// SamplerAddressingModeInfo returns the addressing mode value associated with sampler.
//
// Returned type: SamplerAddressingMode
SamplerAddressingModeInfo SamplerInfoName = C.CL_SAMPLER_ADDRESSING_MODE
// SamplerFilterModeInfo returns the filter mode value associated with sampler.
//
// Returned type: SamplerFilterMode
SamplerFilterModeInfo SamplerInfoName = C.CL_SAMPLER_FILTER_MODE
)
// SamplerInfo queries information about a sampler.
//
// The provided size need to specify the size of the available space pointed to the provided value in bytes.
//
// The returned number is the required size, in bytes, for the queried information.
// Call the function with a zero size and nil value to request the required size. This helps in determining
// the necessary space for dynamic information, such as arrays.
//
// Raw strings are with a terminating NUL character.
//
// See also: https://registry.khronos.org/OpenCL/sdk/2.2/docs/man/html/clGetSamplerInfo.html
func SamplerInfo(sampler Sampler, paramName ContextInfoName, paramSize uintptr, paramValue unsafe.Pointer) (uintptr, error) {
sizeReturn := C.size_t(0)
status := C.clGetSamplerInfo(
sampler.handle(),
C.cl_sampler_info(paramName),
C.size_t(paramSize),
paramValue,
&sizeReturn)
if status != C.CL_SUCCESS {
return 0, StatusError(status)
}
return uintptr(sizeReturn), nil
}