summaryrefslogtreecommitdiffstats
path: root/src/opencl/qclsampler.cpp
blob: 40b7883a29c8417e1efb9f00b40b38cb6b59f34a (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtOpenCL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qclsampler.h"
#include "qclcontext.h"

QT_BEGIN_NAMESPACE

/*!
    \class QCLSampler
    \brief The QCLSampler class represents an OpenCL sampler object.
    \since 4.7
    \ingroup opencl
*/

class QCLSamplerPrivate
{
public:
    QCLSamplerPrivate()
        : id(0)
        , context(0)
        , normalizedCoordinates(false)
        , addressingMode(QCLSampler::ClampToEdge)
        , filterMode(QCLSampler::Linear)
    {
    }
    QCLSamplerPrivate(const QCLSamplerPrivate *other)
        : id(other->id)
        , context(other->context)
        , normalizedCoordinates(other->normalizedCoordinates)
        , addressingMode(other->addressingMode)
        , filterMode(other->filterMode)
    {
        if (id)
            clRetainSampler(id);
    }

    void assign(const QCLSamplerPrivate *other)
    {
        if (id)
            clReleaseSampler(id);
        id = other->id;
        if (id)
            clRetainSampler(id);
        context = other->context;
        normalizedCoordinates = other->normalizedCoordinates;
        addressingMode = other->addressingMode;
        filterMode = other->filterMode;
    }

    void detach()
    {
        if (id) {
            clReleaseSampler(id);
            id = 0;
        }
    }

    mutable cl_sampler id;
    mutable QCLContext *context;
    bool normalizedCoordinates;
    QCLSampler::AddressingMode addressingMode;
    QCLSampler::FilterMode filterMode;
};

/*!
    Constructs a default OpenCL sampler object, with unnormalized
    co-ordinates, clamp-to-edge addressing, and linear filtering.
*/
QCLSampler::QCLSampler()
    : d_ptr(new QCLSamplerPrivate())
{
}

/*!
    Constructs an OpenCL sampler object from the native identifier \a id.
    This class takes over ownership of \a id and will release it in
    the destructor.  The sampler \a id will be associated with \a context.
*/
QCLSampler::QCLSampler(QCLContext *context, cl_sampler id)
    : d_ptr(new QCLSamplerPrivate())
{
    Q_D(QCLSampler);
    d->id = id;

    // Read the current settings from the OpenCL implementation.
    if (id) {
        cl_bool normalized = CL_FALSE;
        cl_addressing_mode addressing = CL_ADDRESS_CLAMP_TO_EDGE;
        cl_filter_mode filter = CL_FILTER_LINEAR;
        clGetSamplerInfo(id, CL_SAMPLER_NORMALIZED_COORDS,
                         sizeof(normalized), &normalized, 0);
        clGetSamplerInfo(id, CL_SAMPLER_ADDRESSING_MODE,
                         sizeof(addressing), &addressing, 0);
        clGetSamplerInfo(id, CL_SAMPLER_FILTER_MODE,
                         sizeof(filter), &filter, 0);
        d->normalizedCoordinates = (normalized != CL_FALSE);
        d->addressingMode = QCLSampler::AddressingMode(addressing);
        d->filterMode = QCLSampler::FilterMode(filter);
        d->context = context;
    }
}

/*!
    Constructs a copy of \a other.  The \c{clRetainSampler()} function
    will be called to update the reference count on samplerId().
*/
QCLSampler::QCLSampler(const QCLSampler &other)
    : d_ptr(new QCLSamplerPrivate(other.d_ptr.data()))
{
}

/*!
    Releases this OpenCL sampler object by calling \c{clReleaseSampler()}.
*/
QCLSampler::~QCLSampler()
{
    Q_D(QCLSampler);
    d->detach();
}

/*!
    Assigns \a other to this OpenCL sampler object.  The current samplerId()
    will be released with \c{clReleaseSampler()}, and the new samplerId()
    will be retained with \c{clRetainSampler()}.
*/
QCLSampler &QCLSampler::operator=(const QCLSampler &other)
{
    Q_D(QCLSampler);
    if (d != other.d_ptr.data())
        d->assign(other.d_ptr.data());
    return *this;
}

/*!
    \enum QCLSampler::AddressingMode
    This enum specifies how to handle out-of-range image co-ordinates
    when reading from an image in OpenCL.

    \value None No special handling of out-of-range co-ordinates.
    \value ClampToEdge Out-of-range requests clamp to the edge pixel value.
    \value Clamp Out-of-range requests clamp to the image extents.
    \value Repeat Repeats the image in a cycle.
*/

/*!
    \enum QCLSampler::FilterMode
    This enum defines the type of filter to apply when reading from
    an image in OpenCL.

    \value Nearest Use the color of the nearest pixel.
    \value Linear Interpolate linearly between pixel colors to generate
    intermediate pixel colors.
*/

/*!
    Returns true if this sampler is using normalized co-ordinates;
    false otherwise.  The default value is false.

    \sa setNormalizedCoordinates()
*/
bool QCLSampler::normalizedCoordinates() const
{
    Q_D(const QCLSampler);
    return d->normalizedCoordinates;
}

/*!
    Enables or disables normalized co-ordinates according to \a value.

    This function will release samplerId() if \a value is different
    than the previous value.

    \sa normalizedCoordinates()
*/
void QCLSampler::setNormalizedCoordinates(bool value)
{
    Q_D(QCLSampler);
    if (d->normalizedCoordinates != value) {
        d->detach();
        d->normalizedCoordinates = value;
    }
}

/*!
    Returns the addressing mode for out-of-range co-ordinates
    when reading from an image in OpenCL.  The default value is
    ClampToEdge.

    \sa setAddressingMode()
*/
QCLSampler::AddressingMode QCLSampler::addressingMode() const
{
    Q_D(const QCLSampler);
    return d->addressingMode;
}

/*!
    Sets the addressing mode for out-of-range co-ordinates
    when reading from an image in OpenCL to \a value.

    This function will release samplerId() if \a value is different
    than the previous value.

    \sa addressingMode()
*/
void QCLSampler::setAddressingMode(QCLSampler::AddressingMode value)
{
    Q_D(QCLSampler);
    if (d->addressingMode != value) {
        d->detach();
        d->addressingMode = value;
    }
}

/*!
    Returns the type of filter to apply when reading from an image
    in OpenCL.  The default value is Linear.

    \sa setFilterMode()
*/
QCLSampler::FilterMode QCLSampler::filterMode() const
{
    Q_D(const QCLSampler);
    return d->filterMode;
}

/*!
    Sets the type of filter to apply when reading from an image
    in OpenCL to \a value.

    This function will release samplerId() if \a value is different
    than the previous value.

    \sa filterMode()
*/
void QCLSampler::setFilterMode(QCLSampler::FilterMode value)
{
    Q_D(QCLSampler);
    if (d->filterMode != value) {
        d->detach();
        d->filterMode = value;
    }
}

/*!
    Returns the native OpenCL identifier for this sampler; or 0 if
    the identifier has not been created.

    The identifier is created when the sampler is set on a kernel
    as an argument with QCLKernel::setArg().
*/
cl_sampler QCLSampler::samplerId() const
{
    Q_D(const QCLSampler);
    return d->id;
}

/*!
    Returns the OpenCL context that this sampler was created for;
    null if not yet created within a context.
*/
QCLContext *QCLSampler::context() const
{
    Q_D(const QCLSampler);
    return d->context;
}

/*!
    Returns true if this OpenCL sampler has the same parameters
    as \a other; false otherwise.

    \sa operator!=()
*/
bool QCLSampler::operator==(const QCLSampler &other) const
{
    return d_ptr->normalizedCoordinates ==
                other.d_ptr->normalizedCoordinates &&
           d_ptr->addressingMode == other.d_ptr->addressingMode &&
           d_ptr->filterMode == other.d_ptr->filterMode;
}

/*!
    \fn bool QCLSampler::operator!=(const QCLSampler &other) const

    Returns true if this OpenCL sampler does not have the same
    parameters as \a other; false otherwise.

    \sa operator==()
*/

/*!
    \internal
*/
void QCLSampler::setKernelArg
    (QCLContext *context, cl_kernel kernel, int index) const
{
    Q_D(const QCLSampler);
    if (!d->id || d->context != context) {
        // Create a new sampler in the kernel's context.
        if (d->id)
            clReleaseSampler(d->id);    // Was created for another context.
        cl_int error;
        d->id = clCreateSampler
            (context->contextId(),
             d->normalizedCoordinates ? CL_TRUE : CL_FALSE,
             cl_addressing_mode(d->addressingMode),
             cl_filter_mode(d->filterMode), &error);
        context->reportError("QCLKernel::setArg(sampler):", error);
        if (d->id)
            d->context = context;
        else
            d->context = 0;
    }
    clSetKernelArg(kernel, index, sizeof(d->id), &(d->id));
}

QT_END_NAMESPACE