aboutsummaryrefslogtreecommitdiff
path: root/src/core/sampler.cpp
blob: 1201327c5a7dc5f31337c9592264ef63c8a90ec5 (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
/*
 * Copyright (c) 2011, Denis Steckelmacher <steckdenis@yahoo.fr>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * \file core/sampler.cpp
 * \brief Sampler
 */

#include "sampler.h"
#include "context.h"
#include "deviceinterface.h"
#include "propertylist.h"

#include <cstring>
#include <cstdlib>

using namespace Coal;

Sampler::Sampler(Context *ctx,
                 cl_bool normalized_coords,
                 cl_addressing_mode addressing_mode,
                 cl_filter_mode filter_mode,
                 cl_int *errcode_ret)
: Object(Object::T_Sampler, ctx), p_bitfield(0)
{
    if (normalized_coords)
        p_bitfield |= CLK_NORMALIZED_COORDS_TRUE;
    else
        p_bitfield |= CLK_NORMALIZED_COORDS_FALSE;

    switch (addressing_mode)
    {
        case CL_ADDRESS_NONE:
            p_bitfield |= CLK_ADDRESS_NONE;
            break;

        case CL_ADDRESS_MIRRORED_REPEAT:
            p_bitfield |= CLK_ADDRESS_MIRRORED_REPEAT;
            break;

        case CL_ADDRESS_REPEAT:
            p_bitfield |= CLK_ADDRESS_REPEAT;
            break;

        case CL_ADDRESS_CLAMP_TO_EDGE:
            p_bitfield |= CLK_ADDRESS_CLAMP_TO_EDGE;
            break;

        case CL_ADDRESS_CLAMP:
            p_bitfield |= CLK_ADDRESS_CLAMP;
            break;

        default:
            *errcode_ret = CL_INVALID_VALUE;
            return;
    }

    switch (filter_mode)
    {
        case CL_FILTER_NEAREST:
            p_bitfield |= CLK_FILTER_NEAREST;
            break;

        case CL_FILTER_LINEAR:
            p_bitfield |= CLK_FILTER_LINEAR;
            break;

        default:
            *errcode_ret = CL_INVALID_VALUE;
            return;
    }

    // Check that images are available on all the devices
    *errcode_ret = checkImageAvailability();
}

Sampler::Sampler(Context *ctx, unsigned int bitfield)
: Object(Object::T_Sampler, ctx), p_bitfield(bitfield)
{
    checkImageAvailability();
}

cl_int Sampler::checkImageAvailability() const
{
    cl_uint num_devices;
    cl_device_id *d_devices;
    cl_int rs;

    rs = ((Context *)parent())->info(CL_CONTEXT_NUM_DEVICES,
                                     sizeof(unsigned int),
                                     &num_devices, 0);

    if (rs != CL_SUCCESS)
        return rs;

    d_devices = (cl_device_id*)std::malloc(num_devices * sizeof(cl_device_id));

    if (!d_devices)
    {
        return CL_OUT_OF_HOST_MEMORY;
    }

    rs = ((Context *)parent())->info(CL_CONTEXT_DEVICES,
                                     num_devices * sizeof(cl_device_id),
                                     d_devices, 0);

    if (rs != CL_SUCCESS)
    {
        std::free((void *)d_devices);
        return rs;
    }

    for (unsigned int i=0; i<num_devices; ++i)
    {
        cl_bool image_support;

        rs = (pobj(d_devices[i]))->info(CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool),
                              &image_support, 0);

        if (rs != CL_SUCCESS)
        {
            std::free((void *)d_devices);
            return rs;
        }

        if (!image_support)
        {
            std::free((void *)d_devices);
            return CL_INVALID_OPERATION;
        }
    }

    std::free((void *)d_devices);

    return CL_SUCCESS;
}

unsigned int Sampler::bitfield() const
{
    return p_bitfield;
}

cl_int Sampler::info(cl_sampler_info param_name,
                     size_t param_value_size,
                     void *param_value,
                     size_t *param_value_size_ret) const
{
    void *value = 0;
    size_t value_length = 0;

    union {
        cl_uint cl_uint_var;
        cl_context cl_context_var;
        cl_bool cl_bool_var;
        cl_addressing_mode cl_addressing_mode_var;
        cl_filter_mode cl_filter_mode_var;
    };

    switch (param_name)
    {
        case CL_SAMPLER_REFERENCE_COUNT:
            SIMPLE_ASSIGN(cl_uint, references());
            break;

        case CL_SAMPLER_CONTEXT:
            SIMPLE_ASSIGN(cl_context, parent());
            break;

        case CL_SAMPLER_NORMALIZED_COORDS:
            if (p_bitfield & CLK_NORMALIZED_COORDS_MASK)
                SIMPLE_ASSIGN(cl_bool, true)
            else
                SIMPLE_ASSIGN(cl_bool, false);
            break;

        case CL_SAMPLER_ADDRESSING_MODE:
            switch (p_bitfield & CLK_ADDRESS_MODE_MASK)
            {
                case CLK_ADDRESS_CLAMP:
                    SIMPLE_ASSIGN(cl_addressing_mode, CL_ADDRESS_CLAMP);
                    break;
                case CLK_ADDRESS_CLAMP_TO_EDGE:
                    SIMPLE_ASSIGN(cl_addressing_mode, CL_ADDRESS_CLAMP_TO_EDGE);
                    break;
                case CLK_ADDRESS_MIRRORED_REPEAT:
                    SIMPLE_ASSIGN(cl_addressing_mode, CL_ADDRESS_MIRRORED_REPEAT);
                    break;
                case CLK_ADDRESS_REPEAT:
                    SIMPLE_ASSIGN(cl_addressing_mode, CL_ADDRESS_REPEAT);
                    break;
                case CLK_ADDRESS_NONE:
                    SIMPLE_ASSIGN(cl_addressing_mode, CL_ADDRESS_NONE);
                    break;
            }
            break;

        case CL_SAMPLER_FILTER_MODE:
            switch (p_bitfield & CLK_FILTER_MASK)
            {
                case CLK_FILTER_LINEAR:
                    SIMPLE_ASSIGN(cl_filter_mode, CL_FILTER_LINEAR);
                    break;
                case CLK_FILTER_NEAREST:
                    SIMPLE_ASSIGN(cl_filter_mode, CL_FILTER_NEAREST);
                    break;
            }

        default:
            return CL_INVALID_VALUE;
    }

    if (param_value && param_value_size < value_length)
        return CL_INVALID_VALUE;

    if (param_value_size_ret)
        *param_value_size_ret = value_length;

    if (param_value)
        std::memcpy(param_value, value, value_length);

    return CL_SUCCESS;
}