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 1
/
sampler.go
149 lines (135 loc) · 5.75 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
package cl12
// #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))
}
// 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
)
// 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
)
// CreateSampler creates a sampler object.
//
// See also: https://registry.khronos.org/OpenCL/sdk/1.2/docs/man/xhtml/clCreateSampler.html
func CreateSampler(context Context, normalizedCoords bool, addressingMode SamplerAddressingMode, filterMode SamplerFilterMode) (Sampler, error) {
var status C.cl_int
sampler := C.clCreateSampler(
context.handle(),
C.cl_bool(BoolFrom(normalizedCoords)),
C.cl_addressing_mode(addressingMode),
C.cl_filter_mode(filterMode),
&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/1.2/docs/man/xhtml/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/1.2/docs/man/xhtml/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/1.2/docs/man/xhtml/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
}