summaryrefslogtreecommitdiff
path: root/libclapi/cl_sampler.c
blob: bdf5b462ea9ec928798532913e26e135931768f3 (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
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <assert.h>
#include "cl_context.h"
#include "cl_internals.h"
#include "cl_alloc.h"
#include "cl_sampler.h"
#include "cl_mutex.h"
#include "cl_context.h"
#include "cl_device_id.h"
#include "cl_platform_id.h"
#include "cl_driver.h"

static void cl_sampler_delete(cl_sampler sampler)
{
  assert(sampler);
  assert(sampler->ctx);
  CL_MUTEX_LOCK(&sampler->ctx->lock);
  if (sampler->prev)
    sampler->prev->next = sampler->next;
  if (sampler->next)
    sampler->next->prev = sampler->prev;
  if (sampler->ctx->samplers == sampler)
    sampler->ctx->samplers = sampler->next;
  CL_MUTEX_UNLOCK(&sampler->ctx->lock);
  cl_release_context(sampler->ctx);

  if (sampler->ctx->device_num > 1) {
    CL_FREE(sampler->pdata);
  }
  CL_FREE(sampler);
}

static void cl_release_sampler(cl_sampler sampler)
{
  cl_uint i;

  assert(sampler);

  if (cl_ref_dec(&sampler->ref_n) > 1)
    return;

  for (i = 0; i < sampler->ctx->device_num; i++) {
    sampler->ctx->devices[i]->driver->release_sampler(sampler,
        sampler->ctx->devices[i]);
  }

  cl_sampler_delete(sampler);
}

static cl_sampler cl_sampler_new(cl_context ctx, cl_bool normalized_coords,
                                 cl_addressing_mode addressing, cl_filter_mode filter)
{
  cl_sampler sampler = NULL;

  /* Allocate and inialize the structure itself */
  sampler = CL_CALLOC(1, sizeof(_cl_sampler));
  if (sampler == NULL)
    return NULL;

  cl_ref_set_val(&sampler->ref_n, 1);
  sampler->magic = CL_MAGIC_SAMPLER_HEADER;
  sampler->normalized_coords = normalized_coords;
  sampler->address = addressing;
  sampler->filter = filter;
  /* Create the private pointer array if device > 1 */
  if (ctx->device_num > 1) {
    sampler->pdata = CL_CALLOC(ctx->device_num, sizeof(void*));
    if (sampler->pdata == NULL) {
      CL_FREE(sampler);
      return NULL;
    }
  }

  sampler->ctx = ctx;
  cl_retain_context(ctx);
  /* Append the sampler in the context sampler list */
  CL_MUTEX_LOCK(&ctx->lock);
  sampler->next = ctx->samplers;
  if (ctx->samplers != NULL)
    ctx->samplers->prev = sampler;
  ctx->samplers = sampler;
  CL_MUTEX_UNLOCK(&ctx->lock);

  return sampler;
}

static cl_sampler cl_create_sampler(cl_context ctx, cl_bool normalized_coords, cl_addressing_mode addressing,
                                    cl_filter_mode filter, cl_int *errcode_ret)
{
  cl_sampler sampler = NULL;
  cl_int err = CL_SUCCESS;
  cl_uint i;

  sampler = cl_sampler_new(ctx, normalized_coords, addressing, filter);
  if (sampler == NULL) {
    err = CL_OUT_OF_HOST_MEMORY;
    goto exit;
  }

  for (i = 0; i < ctx->device_num; i++) {
    err = ctx->devices[i]->driver->create_sampler(sampler, ctx->devices[i],
          normalized_coords, addressing, filter);
    if (err != CL_SUCCESS)
      goto error;
  }

exit:
  if (errcode_ret)
    *errcode_ret = err;
  return sampler;

error:
  cl_release_sampler(sampler);
  sampler = NULL;
  goto exit;
}

static cl_int cl_retain_sampler(cl_sampler sampler)
{
  assert(sampler);
  int ret = cl_ref_inc_if_positive(&sampler->ref_n);
  if (ret > 0)
    return ret;

  return 0;
}

/**************************************************************************************
 *************************           CL APIs           ********************************
 **************************************************************************************/
cl_sampler
clCreateSampler(cl_context         context,
                cl_bool            normalized,
                cl_addressing_mode addressing,
                cl_filter_mode     filter,
                cl_int*            errcode_ret)
{
  cl_sampler sampler = NULL;
  cl_int err = CL_SUCCESS;
  CHECK_CONTEXT(context);

  sampler = cl_create_sampler(context, normalized, addressing, filter, errcode_ret);

  if (errcode_ret)
    *errcode_ret = err;

error:
  return sampler;
}

cl_int
clReleaseSampler(cl_sampler sampler)
{
  cl_int err = CL_SUCCESS;
  CHECK_SAMPLER(sampler);
  cl_release_sampler(sampler);

error:
  return err;
}

cl_int
clRetainSampler(cl_sampler sampler)
{
  cl_int err = CL_SUCCESS;
  CHECK_SAMPLER(sampler);

  if (cl_retain_sampler(sampler) == 0) {
    err = CL_INVALID_SAMPLER;
  }

error:
  return err;
}