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 kernel::kernel(program
&prog
, const std::string
&name
,
31 const std::vector
<module::argument
> &margs
) :
32 prog(prog
), _name(name
), exec(*this) {
33 for (auto &marg
: margs
) {
34 if (marg
.type
== module::argument::scalar
)
35 _args
.emplace_back(new scalar_argument(marg
.size
));
36 else if (marg
.type
== module::argument::global
)
37 _args
.emplace_back(new global_argument
);
38 else if (marg
.type
== module::argument::local
)
39 _args
.emplace_back(new local_argument
);
40 else if (marg
.type
== module::argument::constant
)
41 _args
.emplace_back(new constant_argument
);
42 else if (marg
.type
== module::argument::image2d_rd
||
43 marg
.type
== module::argument::image3d_rd
)
44 _args
.emplace_back(new image_rd_argument
);
45 else if (marg
.type
== module::argument::image2d_wr
||
46 marg
.type
== module::argument::image3d_wr
)
47 _args
.emplace_back(new image_wr_argument
);
48 else if (marg
.type
== module::argument::sampler
)
49 _args
.emplace_back(new sampler_argument
);
51 throw error(CL_INVALID_KERNEL_DEFINITION
);
56 static inline std::vector
<uint
>
57 pad_vector(command_queue
&q
, const V
&v
, uint x
) {
58 std::vector
<uint
> w
{ v
.begin(), v
.end() };
59 w
.resize(q
.dev
.max_block_size().size(), x
);
64 kernel::launch(command_queue
&q
,
65 const std::vector
<size_t> &grid_offset
,
66 const std::vector
<size_t> &grid_size
,
67 const std::vector
<size_t> &block_size
) {
68 const auto m
= prog
.binary(q
.dev
);
69 const auto reduced_grid_size
=
70 map(divides(), grid_size
, block_size
);
71 void *st
= exec
.bind(&q
);
73 // The handles are created during exec_context::bind(), so we need make
74 // sure to call exec_context::bind() before retrieving them.
75 std::vector
<uint32_t *> g_handles
= map([&](size_t h
) {
76 return (uint32_t *)&exec
.input
[h
];
79 q
.pipe
->bind_compute_state(q
.pipe
, st
);
80 q
.pipe
->bind_sampler_states(q
.pipe
, PIPE_SHADER_COMPUTE
,
81 0, exec
.samplers
.size(),
82 exec
.samplers
.data());
84 q
.pipe
->set_compute_sampler_views(q
.pipe
, 0, exec
.sviews
.size(),
86 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(),
87 exec
.resources
.data());
88 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(),
89 exec
.g_buffers
.data(), g_handles
.data());
91 q
.pipe
->launch_grid(q
.pipe
,
92 pad_vector(q
, block_size
, 1).data(),
93 pad_vector(q
, reduced_grid_size
, 1).data(),
94 find(name_equals(_name
), m
.syms
).offset
,
97 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(), NULL
, NULL
);
98 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(), NULL
);
99 q
.pipe
->set_compute_sampler_views(q
.pipe
, 0, exec
.sviews
.size(), NULL
);
100 q
.pipe
->bind_sampler_states(q
.pipe
, PIPE_SHADER_COMPUTE
, 0,
101 exec
.samplers
.size(), NULL
);
106 kernel::mem_local() const {
109 for (auto &arg
: args()) {
110 if (dynamic_cast<local_argument
*>(&arg
))
118 kernel::mem_private() const {
123 kernel::max_block_size() const {
124 return std::numeric_limits
<std::size_t>::max();
128 kernel::name() const {
133 kernel::block_size() const {
137 kernel::argument_range
139 return map(derefs(), _args
);
142 kernel::const_argument_range
143 kernel::args() const {
144 return map(derefs(), _args
);
148 kernel::module(const command_queue
&q
) const {
149 return prog
.binary(q
.dev
);
152 kernel::exec_context::exec_context(kernel
&kern
) :
153 kern(kern
), q(NULL
), mem_local(0), st(NULL
), cs() {
156 kernel::exec_context::~exec_context() {
158 q
->pipe
->delete_compute_state(q
->pipe
, st
);
162 kernel::exec_context::bind(command_queue
*_q
) {
165 // Bind kernel arguments.
166 auto &m
= kern
.prog
.binary(q
->dev
);
167 auto margs
= find(name_equals(kern
.name()), m
.syms
).args
;
168 auto msec
= find(type_equals(module::section::text
), m
.secs
);
170 for_each([=](kernel::argument
&karg
, const module::argument
&marg
) {
171 karg
.bind(*this, marg
);
172 }, kern
.args(), margs
);
174 // Create a new compute state if anything changed.
175 if (!st
|| q
!= _q
||
176 cs
.req_local_mem
!= mem_local
||
177 cs
.req_input_mem
!= input
.size()) {
179 _q
->pipe
->delete_compute_state(_q
->pipe
, st
);
181 cs
.prog
= msec
.data
.begin();
182 cs
.req_local_mem
= mem_local
;
183 cs
.req_input_mem
= input
.size();
184 st
= q
->pipe
->create_compute_state(q
->pipe
, &cs
);
191 kernel::exec_context::unbind() {
192 for (auto &arg
: kern
.args())
208 return { (uint8_t *)&x
, (uint8_t *)&x
+ sizeof(x
) };
212 /// Transform buffer \a v from the native byte order into the byte
213 /// order specified by \a e.
217 byteswap(T
&v
, pipe_endian e
) {
218 if (PIPE_ENDIAN_NATIVE
!= e
)
219 std::reverse(v
.begin(), v
.end());
223 /// Pad buffer \a v to the next multiple of \a n.
227 align(T
&v
, size_t n
) {
228 v
.resize(util_align_npot(v
.size(), n
));
232 msb(const std::vector
<uint8_t> &s
) {
233 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
234 return s
.back() & 0x80;
236 return s
.front() & 0x80;
240 /// Resize buffer \a v to size \a n using sign or zero extension
241 /// according to \a ext.
245 extend(T
&v
, enum module::argument::ext_type ext
, size_t n
) {
246 const size_t m
= std::min(v
.size(), n
);
247 const bool sign_ext
= (ext
== module::argument::sign_ext
);
248 const uint8_t fill
= (sign_ext
&& msb(v
) ? ~0 : 0);
251 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
252 std::copy_n(v
.begin(), m
, w
.begin());
254 std::copy_n(v
.end() - m
, m
, w
.end() - m
);
260 /// Append buffer \a w to \a v.
264 insert(T
&v
, const T
&w
) {
265 v
.insert(v
.end(), w
.begin(), w
.end());
269 /// Append \a n elements to the end of buffer \a v.
273 allocate(T
&v
, size_t n
) {
274 size_t pos
= v
.size();
280 kernel::argument::argument() : _set(false) {
284 kernel::argument::set() const {
289 kernel::argument::storage() const {
293 kernel::scalar_argument::scalar_argument(size_t size
) : size(size
) {
297 kernel::scalar_argument::set(size_t size
, const void *value
) {
298 if (size
!= this->size
)
299 throw error(CL_INVALID_ARG_SIZE
);
301 v
= { (uint8_t *)value
, (uint8_t *)value
+ size
};
306 kernel::scalar_argument::bind(exec_context
&ctx
,
307 const module::argument
&marg
) {
310 extend(w
, marg
.ext_type
, marg
.target_size
);
311 byteswap(w
, ctx
.q
->dev
.endianness());
312 align(ctx
.input
, marg
.target_align
);
313 insert(ctx
.input
, w
);
317 kernel::scalar_argument::unbind(exec_context
&ctx
) {
321 kernel::global_argument::set(size_t size
, const void *value
) {
322 if (size
!= sizeof(cl_mem
))
323 throw error(CL_INVALID_ARG_SIZE
);
325 buf
= &obj
<buffer
>(*(cl_mem
*)value
);
330 kernel::global_argument::bind(exec_context
&ctx
,
331 const module::argument
&marg
) {
332 align(ctx
.input
, marg
.target_align
);
333 ctx
.g_handles
.push_back(allocate(ctx
.input
, marg
.target_size
));
334 ctx
.g_buffers
.push_back(buf
->resource(*ctx
.q
).pipe
);
338 kernel::global_argument::unbind(exec_context
&ctx
) {
342 kernel::local_argument::storage() const {
347 kernel::local_argument::set(size_t size
, const void *value
) {
349 throw error(CL_INVALID_ARG_VALUE
);
356 kernel::local_argument::bind(exec_context
&ctx
,
357 const module::argument
&marg
) {
358 auto v
= bytes(ctx
.mem_local
);
360 extend(v
, module::argument::zero_ext
, marg
.target_size
);
361 byteswap(v
, ctx
.q
->dev
.endianness());
362 align(ctx
.input
, marg
.target_align
);
363 insert(ctx
.input
, v
);
365 ctx
.mem_local
+= _storage
;
369 kernel::local_argument::unbind(exec_context
&ctx
) {
373 kernel::constant_argument::set(size_t size
, const void *value
) {
374 if (size
!= sizeof(cl_mem
))
375 throw error(CL_INVALID_ARG_SIZE
);
377 buf
= &obj
<buffer
>(*(cl_mem
*)value
);
382 kernel::constant_argument::bind(exec_context
&ctx
,
383 const module::argument
&marg
) {
384 auto v
= bytes(ctx
.resources
.size() << 24);
386 extend(v
, module::argument::zero_ext
, marg
.target_size
);
387 byteswap(v
, ctx
.q
->dev
.endianness());
388 align(ctx
.input
, marg
.target_align
);
389 insert(ctx
.input
, v
);
391 st
= buf
->resource(*ctx
.q
).bind_surface(*ctx
.q
, false);
392 ctx
.resources
.push_back(st
);
396 kernel::constant_argument::unbind(exec_context
&ctx
) {
397 buf
->resource(*ctx
.q
).unbind_surface(*ctx
.q
, st
);
401 kernel::image_rd_argument::set(size_t size
, const void *value
) {
402 if (size
!= sizeof(cl_mem
))
403 throw error(CL_INVALID_ARG_SIZE
);
405 img
= &obj
<image
>(*(cl_mem
*)value
);
410 kernel::image_rd_argument::bind(exec_context
&ctx
,
411 const module::argument
&marg
) {
412 auto v
= bytes(ctx
.sviews
.size());
414 extend(v
, module::argument::zero_ext
, marg
.target_size
);
415 byteswap(v
, ctx
.q
->dev
.endianness());
416 align(ctx
.input
, marg
.target_align
);
417 insert(ctx
.input
, v
);
419 st
= img
->resource(*ctx
.q
).bind_sampler_view(*ctx
.q
);
420 ctx
.sviews
.push_back(st
);
424 kernel::image_rd_argument::unbind(exec_context
&ctx
) {
425 img
->resource(*ctx
.q
).unbind_sampler_view(*ctx
.q
, st
);
429 kernel::image_wr_argument::set(size_t size
, const void *value
) {
430 if (size
!= sizeof(cl_mem
))
431 throw error(CL_INVALID_ARG_SIZE
);
433 img
= &obj
<image
>(*(cl_mem
*)value
);
438 kernel::image_wr_argument::bind(exec_context
&ctx
,
439 const module::argument
&marg
) {
440 auto v
= bytes(ctx
.resources
.size());
442 extend(v
, module::argument::zero_ext
, marg
.target_size
);
443 byteswap(v
, ctx
.q
->dev
.endianness());
444 align(ctx
.input
, marg
.target_align
);
445 insert(ctx
.input
, v
);
447 st
= img
->resource(*ctx
.q
).bind_surface(*ctx
.q
, true);
448 ctx
.resources
.push_back(st
);
452 kernel::image_wr_argument::unbind(exec_context
&ctx
) {
453 img
->resource(*ctx
.q
).unbind_surface(*ctx
.q
, st
);
457 kernel::sampler_argument::set(size_t size
, const void *value
) {
458 if (size
!= sizeof(cl_sampler
))
459 throw error(CL_INVALID_ARG_SIZE
);
461 s
= &obj(*(cl_sampler
*)value
);
466 kernel::sampler_argument::bind(exec_context
&ctx
,
467 const module::argument
&marg
) {
468 st
= s
->bind(*ctx
.q
);
469 ctx
.samplers
.push_back(st
);
473 kernel::sampler_argument::unbind(exec_context
&ctx
) {
474 s
->unbind(*ctx
.q
, st
);