2 // Copyright 2012 Francisco Jerez
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
23 #include "core/kernel.hpp"
24 #include "core/resource.hpp"
25 #include "util/u_math.h"
26 #include "pipe/p_context.h"
28 using namespace clover
;
30 _cl_kernel::_cl_kernel(clover::program
&prog
,
31 const std::string
&name
,
32 const std::vector
<clover::module::argument
> &margs
) :
33 prog(prog
), __name(name
), exec(*this) {
34 for (auto marg
: margs
) {
35 if (marg
.type
== module::argument::scalar
)
36 args
.emplace_back(new scalar_argument(marg
.size
));
37 else if (marg
.type
== module::argument::global
)
38 args
.emplace_back(new global_argument
);
39 else if (marg
.type
== module::argument::local
)
40 args
.emplace_back(new local_argument
);
41 else if (marg
.type
== module::argument::constant
)
42 args
.emplace_back(new constant_argument
);
43 else if (marg
.type
== module::argument::image2d_rd
||
44 marg
.type
== module::argument::image3d_rd
)
45 args
.emplace_back(new image_rd_argument
);
46 else if (marg
.type
== module::argument::image2d_wr
||
47 marg
.type
== module::argument::image3d_wr
)
48 args
.emplace_back(new image_wr_argument
);
49 else if (marg
.type
== module::argument::sampler
)
50 args
.emplace_back(new sampler_argument
);
52 throw error(CL_INVALID_KERNEL_DEFINITION
);
56 template<typename T
, typename V
>
57 static inline std::vector
<T
>
58 pad_vector(clover::command_queue
&q
, const V
&v
, T x
) {
59 std::vector
<T
> w
{ v
.begin(), v
.end() };
60 w
.resize(q
.dev
.max_block_size().size(), x
);
65 _cl_kernel::launch(clover::command_queue
&q
,
66 const std::vector
<size_t> &grid_offset
,
67 const std::vector
<size_t> &grid_size
,
68 const std::vector
<size_t> &block_size
) {
69 void *st
= exec
.bind(&q
);
70 auto g_handles
= map([&](size_t h
) { return (uint32_t *)&exec
.input
[h
]; },
71 exec
.g_handles
.begin(), exec
.g_handles
.end());
73 q
.pipe
->bind_compute_state(q
.pipe
, st
);
74 q
.pipe
->bind_compute_sampler_states(q
.pipe
, 0, exec
.samplers
.size(),
75 exec
.samplers
.data());
76 q
.pipe
->set_compute_sampler_views(q
.pipe
, 0, exec
.sviews
.size(),
78 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(),
79 exec
.resources
.data());
80 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(),
81 exec
.g_buffers
.data(), g_handles
.data());
83 q
.pipe
->launch_grid(q
.pipe
,
84 pad_vector
<uint
>(q
, block_size
, 1).data(),
85 pad_vector
<uint
>(q
, grid_size
, 1).data(),
86 module(q
).sym(__name
).offset
,
89 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(), NULL
, NULL
);
90 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(), NULL
);
91 q
.pipe
->set_compute_sampler_views(q
.pipe
, 0, exec
.sviews
.size(), NULL
);
92 q
.pipe
->bind_compute_sampler_states(q
.pipe
, 0, exec
.samplers
.size(), NULL
);
97 _cl_kernel::mem_local() const {
100 for (auto &arg
: args
) {
101 if (dynamic_cast<local_argument
*>(arg
.get()))
102 sz
+= arg
->storage();
109 _cl_kernel::mem_private() const {
114 _cl_kernel::max_block_size() const {
119 _cl_kernel::name() const {
124 _cl_kernel::block_size() const {
128 const clover::module
&
129 _cl_kernel::module(const clover::command_queue
&q
) const {
130 return prog
.binaries().find(&q
.dev
)->second
;
133 _cl_kernel::exec_context::exec_context(clover::kernel
&kern
) :
134 kern(kern
), q(NULL
), mem_local(0), st(NULL
) {
137 _cl_kernel::exec_context::~exec_context() {
139 q
->pipe
->delete_compute_state(q
->pipe
, st
);
143 _cl_kernel::exec_context::bind(clover::command_queue
*__q
) {
146 // Bind kernel arguments.
147 auto margs
= kern
.module(*q
).sym(kern
.name()).args
;
148 for_each([=](std::unique_ptr
<kernel::argument
> &karg
,
149 const module::argument
&marg
) {
150 karg
->bind(*this, marg
);
151 }, kern
.args
.begin(), kern
.args
.end(), margs
.begin());
153 // Create a new compute state if anything changed.
154 if (!st
|| q
!= __q
||
155 cs
.req_local_mem
!= mem_local
||
156 cs
.req_input_mem
!= input
.size()) {
158 __q
->pipe
->delete_compute_state(__q
->pipe
, st
);
160 cs
.prog
= kern
.module(*q
).sec(module::section::text
).data
.begin();
161 cs
.req_local_mem
= mem_local
;
162 cs
.req_input_mem
= input
.size();
163 st
= q
->pipe
->create_compute_state(q
->pipe
, &cs
);
170 _cl_kernel::exec_context::unbind() {
171 for (auto &arg
: kern
.args
)
187 return { (uint8_t *)&x
, (uint8_t *)&x
+ sizeof(x
) };
191 /// Transform buffer \a v from the native byte order into the byte
192 /// order specified by \a e.
196 byteswap(T
&v
, pipe_endian e
) {
197 if (PIPE_ENDIAN_NATIVE
!= e
)
198 std::reverse(v
.begin(), v
.end());
202 /// Pad buffer \a v to the next multiple of \a n.
206 align(T
&v
, size_t n
) {
207 v
.resize(util_align_npot(v
.size(), n
));
211 msb(const std::vector
<uint8_t> &s
) {
212 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
213 return s
.back() & 0x80;
215 return s
.front() & 0x80;
219 /// Resize buffer \a v to size \a n using sign or zero extension
220 /// according to \a ext.
224 extend(T
&v
, enum clover::module::argument::ext_type ext
, size_t n
) {
225 const size_t m
= std::min(v
.size(), n
);
226 const bool sign_ext
= (ext
== module::argument::sign_ext
);
227 const uint8_t fill
= (sign_ext
&& msb(v
) ? ~0 : 0);
230 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
231 std::copy_n(v
.begin(), m
, w
.begin());
233 std::copy_n(v
.end() - m
, m
, w
.end() - m
);
239 /// Append buffer \a w to \a v.
243 insert(T
&v
, const T
&w
) {
244 v
.insert(v
.end(), w
.begin(), w
.end());
248 /// Append \a n elements to the end of buffer \a v.
252 allocate(T
&v
, size_t n
) {
253 size_t pos
= v
.size();
259 _cl_kernel::argument::argument() : __set(false) {
263 _cl_kernel::argument::set() const {
268 _cl_kernel::argument::storage() const {
272 _cl_kernel::scalar_argument::scalar_argument(size_t size
) : size(size
) {
276 _cl_kernel::scalar_argument::set(size_t size
, const void *value
) {
277 if (size
!= this->size
)
278 throw error(CL_INVALID_ARG_SIZE
);
280 v
= { (uint8_t *)value
, (uint8_t *)value
+ size
};
285 _cl_kernel::scalar_argument::bind(exec_context
&ctx
,
286 const clover::module::argument
&marg
) {
289 extend(w
, marg
.ext_type
, marg
.target_size
);
290 byteswap(w
, ctx
.q
->dev
.endianness());
291 align(ctx
.input
, marg
.target_align
);
292 insert(ctx
.input
, w
);
296 _cl_kernel::scalar_argument::unbind(exec_context
&ctx
) {
300 _cl_kernel::global_argument::set(size_t size
, const void *value
) {
301 if (size
!= sizeof(cl_mem
))
302 throw error(CL_INVALID_ARG_SIZE
);
304 obj
= dynamic_cast<clover::buffer
*>(*(cl_mem
*)value
);
306 throw error(CL_INVALID_MEM_OBJECT
);
312 _cl_kernel::global_argument::bind(exec_context
&ctx
,
313 const clover::module::argument
&marg
) {
314 align(ctx
.input
, marg
.target_align
);
315 ctx
.g_handles
.push_back(allocate(ctx
.input
, marg
.target_size
));
316 ctx
.g_buffers
.push_back(obj
->resource(ctx
.q
).pipe
);
320 _cl_kernel::global_argument::unbind(exec_context
&ctx
) {
324 _cl_kernel::local_argument::storage() const {
329 _cl_kernel::local_argument::set(size_t size
, const void *value
) {
331 throw error(CL_INVALID_ARG_VALUE
);
338 _cl_kernel::local_argument::bind(exec_context
&ctx
,
339 const clover::module::argument
&marg
) {
340 auto v
= bytes(ctx
.mem_local
);
342 extend(v
, module::argument::zero_ext
, marg
.target_size
);
343 byteswap(v
, ctx
.q
->dev
.endianness());
344 align(ctx
.input
, marg
.target_align
);
345 insert(ctx
.input
, v
);
347 ctx
.mem_local
+= __storage
;
351 _cl_kernel::local_argument::unbind(exec_context
&ctx
) {
355 _cl_kernel::constant_argument::set(size_t size
, const void *value
) {
356 if (size
!= sizeof(cl_mem
))
357 throw error(CL_INVALID_ARG_SIZE
);
359 obj
= dynamic_cast<clover::buffer
*>(*(cl_mem
*)value
);
361 throw error(CL_INVALID_MEM_OBJECT
);
367 _cl_kernel::constant_argument::bind(exec_context
&ctx
,
368 const clover::module::argument
&marg
) {
369 auto v
= bytes(ctx
.resources
.size() << 24);
371 extend(v
, module::argument::zero_ext
, marg
.target_size
);
372 byteswap(v
, ctx
.q
->dev
.endianness());
373 align(ctx
.input
, marg
.target_align
);
374 insert(ctx
.input
, v
);
376 st
= obj
->resource(ctx
.q
).bind_surface(*ctx
.q
, false);
377 ctx
.resources
.push_back(st
);
381 _cl_kernel::constant_argument::unbind(exec_context
&ctx
) {
382 obj
->resource(ctx
.q
).unbind_surface(*ctx
.q
, st
);
386 _cl_kernel::image_rd_argument::set(size_t size
, const void *value
) {
387 if (size
!= sizeof(cl_mem
))
388 throw error(CL_INVALID_ARG_SIZE
);
390 obj
= dynamic_cast<clover::image
*>(*(cl_mem
*)value
);
392 throw error(CL_INVALID_MEM_OBJECT
);
398 _cl_kernel::image_rd_argument::bind(exec_context
&ctx
,
399 const clover::module::argument
&marg
) {
400 auto v
= bytes(ctx
.sviews
.size());
402 extend(v
, module::argument::zero_ext
, marg
.target_size
);
403 byteswap(v
, ctx
.q
->dev
.endianness());
404 align(ctx
.input
, marg
.target_align
);
405 insert(ctx
.input
, v
);
407 st
= obj
->resource(ctx
.q
).bind_sampler_view(*ctx
.q
);
408 ctx
.sviews
.push_back(st
);
412 _cl_kernel::image_rd_argument::unbind(exec_context
&ctx
) {
413 obj
->resource(ctx
.q
).unbind_sampler_view(*ctx
.q
, st
);
417 _cl_kernel::image_wr_argument::set(size_t size
, const void *value
) {
418 if (size
!= sizeof(cl_mem
))
419 throw error(CL_INVALID_ARG_SIZE
);
421 obj
= dynamic_cast<clover::image
*>(*(cl_mem
*)value
);
423 throw error(CL_INVALID_MEM_OBJECT
);
429 _cl_kernel::image_wr_argument::bind(exec_context
&ctx
,
430 const clover::module::argument
&marg
) {
431 auto v
= bytes(ctx
.resources
.size());
433 extend(v
, module::argument::zero_ext
, marg
.target_size
);
434 byteswap(v
, ctx
.q
->dev
.endianness());
435 align(ctx
.input
, marg
.target_align
);
436 insert(ctx
.input
, v
);
438 st
= obj
->resource(ctx
.q
).bind_surface(*ctx
.q
, true);
439 ctx
.resources
.push_back(st
);
443 _cl_kernel::image_wr_argument::unbind(exec_context
&ctx
) {
444 obj
->resource(ctx
.q
).unbind_surface(*ctx
.q
, st
);
448 _cl_kernel::sampler_argument::set(size_t size
, const void *value
) {
449 if (size
!= sizeof(cl_sampler
))
450 throw error(CL_INVALID_ARG_SIZE
);
452 obj
= *(cl_sampler
*)value
;
457 _cl_kernel::sampler_argument::bind(exec_context
&ctx
,
458 const clover::module::argument
&marg
) {
459 st
= obj
->bind(*ctx
.q
);
460 ctx
.samplers
.push_back(st
);
464 _cl_kernel::sampler_argument::unbind(exec_context
&ctx
) {
465 obj
->unbind(*ctx
.q
, st
);