File: clCreateSampler.c

package info (click to toggle)
pocl 6.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,320 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 175; java: 72; xml: 49
file content (171 lines) | stat: -rw-r--r-- 5,951 bytes parent folder | download | duplicates (2)
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
/* OpenCL runtime library: clCreateSampler()

   Copyright (c) 2012-2017 pocl developers

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#include "devices.h"
#include "pocl_cl.h"
#include "pocl_util.h"

extern unsigned long sampler_c;

CL_API_ENTRY cl_sampler CL_API_CALL
POname(clCreateSampler)(cl_context          context,
                cl_bool             normalized_coords, 
                cl_addressing_mode  addressing_mode, 
                cl_filter_mode      filter_mode,
                cl_int *            errcode_ret)
CL_API_SUFFIX__VERSION_1_0
{
  int errcode = CL_SUCCESS;
  cl_sampler sampler = NULL;

  POCL_GOTO_ERROR_COND ((!IS_CL_OBJECT_VALID (context)), CL_INVALID_CONTEXT);

  /* at least 1 device must support images */
  size_t i, any_device_has_images = 0;
  for (i = 0; i < context->num_devices; i++)
    any_device_has_images += (size_t)context->devices[i]->image_support;
  POCL_GOTO_ERROR_ON ((!any_device_has_images), CL_INVALID_OPERATION,
                      "None of the devices within context support images\n");

  /* check requested sampler validity */
  POCL_GOTO_ERROR_COND (
      ((normalized_coords != CL_TRUE) && (normalized_coords != CL_FALSE)),
      CL_INVALID_VALUE);
  POCL_GOTO_ERROR_COND (((normalized_coords != CL_TRUE)
                         && (addressing_mode == CL_ADDRESS_MIRRORED_REPEAT)),
                        CL_INVALID_VALUE);
  POCL_GOTO_ERROR_COND (((normalized_coords != CL_TRUE)
                         && (addressing_mode == CL_ADDRESS_REPEAT)),
                        CL_INVALID_VALUE);

  sampler = (cl_sampler)calloc (1, sizeof (struct _cl_sampler));
  POCL_GOTO_ERROR_COND ((sampler == NULL), CL_OUT_OF_HOST_MEMORY);

  POCL_INIT_OBJECT (sampler);
  POname (clRetainContext) (context);
  sampler->context = context;
  sampler->normalized_coords = normalized_coords;
  sampler->addressing_mode = addressing_mode;
  sampler->filter_mode = filter_mode;
  sampler->device_data
      = (void **)calloc (POCL_ATOMIC_LOAD (pocl_num_devices), sizeof (void *));

  TP_CREATE_SAMPLER (context->id, sampler->id);

  POCL_ATOMIC_INC (sampler_c);

  for (i = 0; i < context->num_devices; ++i)
    {
      cl_device_id dev = context->devices[i];
      if (*(dev->available) == CL_FALSE)
        continue;
      if (dev->image_support == CL_TRUE && dev->ops->create_sampler)
        dev->ops->create_sampler (dev, sampler, dev->dev_id);
    }

ERROR:
  if(errcode_ret)
  {
    *errcode_ret = errcode;
  }
  return sampler;
}
POsym (clCreateSampler)



CL_API_ENTRY cl_sampler CL_API_CALL
POname (clCreateSamplerWithProperties) (
        cl_context context, const cl_sampler_properties *sampler_properties,
        cl_int *errcode_ret) CL_API_SUFFIX__VERSION_2_0
{

  cl_bool normalized_coords = CL_TRUE;
  cl_addressing_mode addressing_mode = CL_ADDRESS_CLAMP;
  cl_filter_mode filter_mode = CL_FILTER_NEAREST;
  int coords_set = 0, addr_set = 0, filter_set = 0;
  int errcode;

  POCL_GOTO_ERROR_COND ((sampler_properties == NULL), CL_INVALID_VALUE);

  const cl_sampler_properties *p = sampler_properties;
  while (*p != 0)
    {
      switch (*p)
        {
        case CL_SAMPLER_NORMALIZED_COORDS:
          {
            POCL_GOTO_ERROR_ON ((coords_set != 0), CL_INVALID_VALUE,
                                "CL_SAMPLER_NORMALIZED_COORDS property "
                                "has been already set");
            normalized_coords = p[1];
            coords_set = 1;
            break;
          }
        case CL_SAMPLER_ADDRESSING_MODE:
          {
            POCL_GOTO_ERROR_ON ((addr_set != 0), CL_INVALID_VALUE,
                                "CL_SAMPLER_ADDRESSING_MODE property "
                                "has been already set");
            addressing_mode = p[1];
            addr_set = 1;
            break;
          }
        case CL_SAMPLER_FILTER_MODE:
          {
            POCL_GOTO_ERROR_ON ((filter_set != 0), CL_INVALID_VALUE,
                                "CL_SAMPLER_FILTER_MODE property "
                                "has been already set");
            filter_mode = p[1];
            filter_set = 1;
            break;
          }
        default:
          POCL_GOTO_ERROR_ON (1, CL_INVALID_VALUE,
                              "Unknown value in properties: %lu\n",
                              (unsigned long)(*p));
        }
      p += 2;
    }
  unsigned num_props = (p - sampler_properties) + 1; /* include final 0 */
  cl_sampler ret_sam = POname (clCreateSampler) (
      context, normalized_coords, addressing_mode, filter_mode, errcode_ret);
  if (ret_sam == NULL)
    return NULL;

  ret_sam->num_properties = num_props;
  assert (num_props < 10);
  memcpy (ret_sam->properties, sampler_properties,
          num_props * sizeof (cl_sampler_properties));

  return ret_sam;

ERROR:
  if (errcode_ret)
    {
      *errcode_ret = errcode;
    }

  return NULL;
}
POsym (clCreateSamplerWithProperties)