clover: Use the dispatch table type from the OpenCL headers
[mesa.git] / src / gallium / state_trackers / clover / core / object.hpp
1 //
2 // Copyright 2013 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 #ifndef CLOVER_CORE_OBJECT_HPP
24 #define CLOVER_CORE_OBJECT_HPP
25
26 #include <cassert>
27 #include <functional>
28 #include <vector>
29
30 #define CL_TARGET_OPENCL_VERSION 220
31 #include "CL/cl.h"
32
33 #include "core/error.hpp"
34 #include "core/property.hpp"
35 #include "api/dispatch.hpp"
36 #include "util/macros.h"
37
38 ///
39 /// Main namespace of the CL state tracker.
40 ///
41 namespace clover {
42 ///
43 /// Class that represents a CL API object.
44 ///
45 template<typename T, typename S>
46 struct descriptor {
47 typedef T object_type;
48 typedef S descriptor_type;
49
50 descriptor() : dispatch(&_dispatch) {
51 static_assert(std::is_standard_layout<descriptor_type>::value,
52 "ICD requires CL API objects to be standard layout.");
53 }
54
55 const cl_icd_dispatch *dispatch;
56 };
57
58 struct default_tag;
59 struct allow_empty_tag;
60 struct wait_list_tag;
61 struct property_list_tag;
62
63 namespace detail {
64 template<typename T, typename D>
65 struct descriptor_traits {
66 typedef T object_type;
67
68 static void
69 validate(D *d) {
70 auto o = static_cast<typename D::object_type *>(d);
71 if (!o || o->dispatch != &_dispatch ||
72 !dynamic_cast<object_type *>(o))
73 throw invalid_object_error<T>();
74 }
75
76 static void
77 validate_list(D * const *ds, size_t n) {
78 if (!ds || !n)
79 throw error(CL_INVALID_VALUE);
80 }
81 };
82
83 template<typename D>
84 struct descriptor_traits<default_tag, D> {
85 typedef typename D::object_type object_type;
86
87 static void
88 validate(D *d) {
89 if (!d || d->dispatch != &_dispatch)
90 throw invalid_object_error<object_type>();
91 }
92
93 static void
94 validate_list(D *const *ds, size_t n) {
95 if (!ds || !n)
96 throw error(CL_INVALID_VALUE);
97 }
98 };
99
100 template<typename D>
101 struct descriptor_traits<allow_empty_tag, D> {
102 typedef typename D::object_type object_type;
103
104 static void
105 validate(D *d) {
106 if (!d || d->dispatch != &_dispatch)
107 throw invalid_object_error<object_type>();
108 }
109
110 static void
111 validate_list(D *const *ds, size_t n) {
112 if (bool(ds) != bool(n))
113 throw error(CL_INVALID_VALUE);
114 }
115 };
116
117 template<typename D>
118 struct descriptor_traits<wait_list_tag, D> {
119 typedef typename D::object_type object_type;
120
121 static void
122 validate(D *d) {
123 if (!d || d->dispatch != &_dispatch)
124 throw invalid_wait_list_error();
125 }
126
127 static void
128 validate_list(D *const *ds, size_t n) {
129 if (bool(ds) != bool(n))
130 throw invalid_wait_list_error();
131 }
132 };
133 }
134
135 ///
136 /// Get a Clover object from an API object performing object
137 /// validation.
138 ///
139 /// \a T can either be the Clover object type to return or a \a tag
140 /// object to select some special validation behavior by means of a
141 /// specialization of the detail::descriptor_traits template. The
142 /// default behavior is to infer the most general Clover object
143 /// type for the given API object.
144 ///
145 template<typename T = default_tag, typename D>
146 typename detail::descriptor_traits<T, D>::object_type &
147 obj(D *d) {
148 detail::descriptor_traits<T, D>::validate(d);
149
150 return static_cast<
151 typename detail::descriptor_traits<T, D>::object_type &>(*d);
152 }
153
154 ///
155 /// Get a pointer to a Clover object from an API object performing
156 /// object validation. Returns \c NULL if its argument is \c NULL.
157 ///
158 /// \sa obj
159 ///
160 template<typename T = default_tag, typename D>
161 typename detail::descriptor_traits<T, D>::object_type *
162 pobj(D *d) {
163 if (d)
164 detail::descriptor_traits<T, D>::validate(d);
165
166 return static_cast<
167 typename detail::descriptor_traits<T, D>::object_type *>(d);
168 }
169
170 ///
171 /// Get an API object from a Clover object.
172 ///
173 template<typename O>
174 typename O::descriptor_type *
175 desc(O &o) {
176 return static_cast<typename O::descriptor_type *>(&o);
177 }
178
179 ///
180 /// Get an API object from a pointer to a Clover object.
181 ///
182 template<typename O>
183 typename O::descriptor_type *
184 desc(O *o) {
185 return static_cast<typename O::descriptor_type *>(o);
186 }
187
188 ///
189 /// Get a range of Clover objects from a range of API objects
190 /// performing object validation.
191 ///
192 /// \sa obj
193 ///
194 template<typename T = default_tag, typename D>
195 ref_vector<typename detail::descriptor_traits<T, D>::object_type>
196 objs(D *const *ds, size_t n) {
197 detail::descriptor_traits<T, D>::validate_list(ds, n);
198 return map(obj<T, D>, range(ds, n));
199 }
200
201 ///
202 /// Get a range of API objects from a range of Clover objects.
203 ///
204 template<typename Os>
205 std::vector<typename Os::value_type::descriptor_type *>
206 descs(const Os &os) {
207 return map([](typename Os::value_type &o) {
208 return desc(o);
209 }, os);
210 }
211 }
212
213 struct _cl_context :
214 public clover::descriptor<clover::context, _cl_context> {};
215
216 struct _cl_device_id :
217 public clover::descriptor<clover::device, _cl_device_id> {};
218
219 struct _cl_event :
220 public clover::descriptor<clover::event, _cl_event> {};
221
222 struct _cl_kernel :
223 public clover::descriptor<clover::kernel, _cl_kernel> {};
224
225 struct _cl_mem :
226 public clover::descriptor<clover::memory_obj, _cl_mem> {};
227
228 struct _cl_platform_id :
229 public clover::descriptor<clover::platform, _cl_platform_id> {};
230
231 struct _cl_program :
232 public clover::descriptor<clover::program, _cl_program> {};
233
234 struct _cl_command_queue :
235 public clover::descriptor<clover::command_queue, _cl_command_queue> {};
236
237 struct _cl_sampler :
238 public clover::descriptor<clover::sampler, _cl_sampler> {};
239
240 #endif