gallium: rename 'state tracker' to 'frontend'
[mesa.git] / src / gallium / frontends / clover / core / kernel.hpp
1 //
2 // Copyright 2012 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_KERNEL_HPP
24 #define CLOVER_CORE_KERNEL_HPP
25
26 #include <memory>
27
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"
33
34 namespace clover {
35 class kernel : public ref_counter, public _cl_kernel {
36 private:
37 ///
38 /// Class containing all the state required to execute a compute
39 /// kernel.
40 ///
41 struct exec_context {
42 exec_context(kernel &kern);
43 ~exec_context();
44
45 exec_context(const exec_context &) = delete;
46 exec_context &
47 operator=(const exec_context &) = delete;
48
49 void *bind(intrusive_ptr<command_queue> _q,
50 const std::vector<size_t> &grid_offset);
51 void unbind();
52
53 kernel &kern;
54 intrusive_ptr<command_queue> q;
55
56 std::vector<uint8_t> input;
57 std::vector<void *> samplers;
58 std::vector<pipe_sampler_view *> sviews;
59 std::vector<pipe_surface *> resources;
60 std::vector<pipe_resource *> g_buffers;
61 std::vector<size_t> g_handles;
62 size_t mem_local;
63
64 private:
65 void *st;
66 pipe_compute_state cs;
67 };
68
69 public:
70 class argument {
71 public:
72 static std::unique_ptr<argument>
73 create(const module::argument &marg);
74
75 argument(const argument &arg) = delete;
76 argument &
77 operator=(const argument &arg) = delete;
78
79 /// \a true if the argument has been set.
80 bool set() const;
81
82 /// Storage space required for the referenced object.
83 virtual size_t storage() const;
84
85 /// Set this argument to some object.
86 virtual void set(size_t size, const void *value) = 0;
87
88 /// Set this argument to an SVM pointer.
89 virtual void set_svm(const void *value) {
90 throw error(CL_INVALID_ARG_INDEX);
91 };
92
93 /// Allocate the necessary resources to bind the specified
94 /// object to this argument, and update \a ctx accordingly.
95 virtual void bind(exec_context &ctx,
96 const module::argument &marg) = 0;
97
98 /// Free any resources that were allocated in bind().
99 virtual void unbind(exec_context &ctx) = 0;
100
101 virtual ~argument() {};
102 protected:
103 argument();
104
105 bool _set;
106 };
107
108 private:
109 typedef adaptor_range<
110 derefs, std::vector<std::unique_ptr<argument>> &
111 > argument_range;
112
113 typedef adaptor_range<
114 derefs, const std::vector<std::unique_ptr<argument>> &
115 > const_argument_range;
116
117 public:
118 kernel(clover::program &prog, const std::string &name,
119 const std::vector<clover::module::argument> &margs);
120
121 kernel(const kernel &kern) = delete;
122 kernel &
123 operator=(const kernel &kern) = delete;
124
125 void launch(command_queue &q,
126 const std::vector<size_t> &grid_offset,
127 const std::vector<size_t> &grid_size,
128 const std::vector<size_t> &block_size);
129
130 size_t mem_local() const;
131 size_t mem_private() const;
132
133 const std::string &name() const;
134
135 std::vector<size_t>
136 optimal_block_size(const command_queue &q,
137 const std::vector<size_t> &grid_size) const;
138 std::vector<size_t>
139 required_block_size() const;
140
141 argument_range args();
142 const_argument_range args() const;
143
144 const intrusive_ref<clover::program> program;
145
146 private:
147 const clover::module &module(const command_queue &q) const;
148
149 class scalar_argument : public argument {
150 public:
151 scalar_argument(size_t size);
152
153 virtual void set(size_t size, const void *value);
154 virtual void bind(exec_context &ctx,
155 const module::argument &marg);
156 virtual void unbind(exec_context &ctx);
157
158 private:
159 size_t size;
160 std::vector<uint8_t> v;
161 };
162
163 class global_argument : public argument {
164 public:
165 virtual void set(size_t size, const void *value);
166 virtual void set_svm(const void *value);
167 virtual void bind(exec_context &ctx,
168 const module::argument &marg);
169 virtual void unbind(exec_context &ctx);
170
171 private:
172 buffer *buf;
173 const void *svm;
174 };
175
176 class local_argument : public argument {
177 public:
178 virtual size_t storage() const;
179
180 virtual void set(size_t size, const void *value);
181 virtual void bind(exec_context &ctx,
182 const module::argument &marg);
183 virtual void unbind(exec_context &ctx);
184
185 private:
186 size_t _storage = 0;
187 };
188
189 class constant_argument : public argument {
190 public:
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);
195
196 private:
197 buffer *buf;
198 pipe_surface *st;
199 };
200
201 class image_argument : public argument {
202 public:
203 const image *get() const {
204 return img;
205 }
206 protected:
207 image *img;
208 };
209
210 class image_rd_argument : public image_argument {
211 public:
212 virtual void set(size_t size, const void *value);
213 virtual void bind(exec_context &ctx,
214 const module::argument &marg);
215 virtual void unbind(exec_context &ctx);
216
217 private:
218 pipe_sampler_view *st;
219 };
220
221 class image_wr_argument : public image_argument {
222 public:
223 virtual void set(size_t size, const void *value);
224 virtual void bind(exec_context &ctx,
225 const module::argument &marg);
226 virtual void unbind(exec_context &ctx);
227
228 private:
229 pipe_surface *st;
230 };
231
232 class sampler_argument : public argument {
233 public:
234 virtual void set(size_t size, const void *value);
235 virtual void bind(exec_context &ctx,
236 const module::argument &marg);
237 virtual void unbind(exec_context &ctx);
238
239 private:
240 sampler *s;
241 void *st;
242 };
243
244 std::vector<std::unique_ptr<argument>> _args;
245 std::string _name;
246 exec_context exec;
247 const ref_holder program_ref;
248 };
249 }
250
251 #endif