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 #ifndef CLOVER_CORE_KERNEL_HPP
24 #define CLOVER_CORE_KERNEL_HPP
28 #include "core/object.hpp"
29 #include "core/program.hpp"
30 #include "core/memory.hpp"
31 #include "core/sampler.hpp"
32 #include "pipe/p_state.h"
35 class kernel : public ref_counter, public _cl_kernel {
38 /// Class containing all the state required to execute a compute
42 exec_context(kernel &kern);
45 exec_context(const exec_context &) = delete;
47 operator=(const exec_context &) = delete;
49 void *bind(intrusive_ptr<command_queue> _q);
53 intrusive_ptr<command_queue> q;
55 std::vector<uint8_t> input;
56 std::vector<void *> samplers;
57 std::vector<pipe_sampler_view *> sviews;
58 std::vector<pipe_surface *> resources;
59 std::vector<pipe_resource *> g_buffers;
60 std::vector<size_t> g_handles;
65 pipe_compute_state cs;
73 argument(const argument &arg) = delete;
75 operator=(const argument &arg) = delete;
77 /// \a true if the argument has been set.
80 /// Storage space required for the referenced object.
81 virtual size_t storage() const;
83 /// Set this argument to some object.
84 virtual void set(size_t size, const void *value) = 0;
86 /// Allocate the necessary resources to bind the specified
87 /// object to this argument, and update \a ctx accordingly.
88 virtual void bind(exec_context &ctx,
89 const module::argument &marg) = 0;
91 /// Free any resources that were allocated in bind().
92 virtual void unbind(exec_context &ctx) = 0;
99 typedef adaptor_range<
100 derefs, std::vector<std::unique_ptr<argument>> &
103 typedef adaptor_range<
104 derefs, const std::vector<std::unique_ptr<argument>> &
105 > const_argument_range;
108 kernel(clover::program &prog, const std::string &name,
109 const std::vector<clover::module::argument> &margs);
111 kernel(const kernel &kern) = delete;
113 operator=(const kernel &kern) = delete;
115 void launch(command_queue &q,
116 const std::vector<size_t> &grid_offset,
117 const std::vector<size_t> &grid_size,
118 const std::vector<size_t> &block_size);
120 size_t mem_local() const;
121 size_t mem_private() const;
123 const std::string &name() const;
126 optimal_block_size(const command_queue &q,
127 const std::vector<size_t> &grid_size) const;
129 required_block_size() const;
131 argument_range args();
132 const_argument_range args() const;
134 const intrusive_ref<clover::program> program;
137 const clover::module &module(const command_queue &q) const;
139 class scalar_argument : public argument {
141 scalar_argument(size_t size);
143 virtual void set(size_t size, const void *value);
144 virtual void bind(exec_context &ctx,
145 const module::argument &marg);
146 virtual void unbind(exec_context &ctx);
150 std::vector<uint8_t> v;
153 class global_argument : public argument {
155 virtual void set(size_t size, const void *value);
156 virtual void bind(exec_context &ctx,
157 const module::argument &marg);
158 virtual void unbind(exec_context &ctx);
164 class local_argument : public argument {
166 virtual size_t storage() const;
168 virtual void set(size_t size, const void *value);
169 virtual void bind(exec_context &ctx,
170 const module::argument &marg);
171 virtual void unbind(exec_context &ctx);
177 class constant_argument : public argument {
179 virtual void set(size_t size, const void *value);
180 virtual void bind(exec_context &ctx,
181 const module::argument &marg);
182 virtual void unbind(exec_context &ctx);
189 class image_rd_argument : public argument {
191 virtual void set(size_t size, const void *value);
192 virtual void bind(exec_context &ctx,
193 const module::argument &marg);
194 virtual void unbind(exec_context &ctx);
198 pipe_sampler_view *st;
201 class image_wr_argument : public argument {
203 virtual void set(size_t size, const void *value);
204 virtual void bind(exec_context &ctx,
205 const module::argument &marg);
206 virtual void unbind(exec_context &ctx);
213 class sampler_argument : public argument {
215 virtual void set(size_t size, const void *value);
216 virtual void bind(exec_context &ctx,
217 const module::argument &marg);
218 virtual void unbind(exec_context &ctx);
225 std::vector<std::unique_ptr<argument>> _args;