clover: Migrate a bunch of pointers and references in the object tree to smart refere...
[mesa.git] / src / gallium / state_trackers / 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 void unbind();
51
52 kernel &kern;
53 intrusive_ptr<command_queue> q;
54
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;
61 size_t mem_local;
62
63 private:
64 void *st;
65 pipe_compute_state cs;
66 };
67
68 public:
69 class argument {
70 public:
71 argument();
72
73 argument(const argument &arg) = delete;
74 argument &
75 operator=(const argument &arg) = delete;
76
77 /// \a true if the argument has been set.
78 bool set() const;
79
80 /// Storage space required for the referenced object.
81 virtual size_t storage() const;
82
83 /// Set this argument to some object.
84 virtual void set(size_t size, const void *value) = 0;
85
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;
90
91 /// Free any resources that were allocated in bind().
92 virtual void unbind(exec_context &ctx) = 0;
93
94 protected:
95 bool _set;
96 };
97
98 private:
99 typedef adaptor_range<
100 derefs, std::vector<std::unique_ptr<argument>> &
101 > argument_range;
102
103 typedef adaptor_range<
104 derefs, const std::vector<std::unique_ptr<argument>> &
105 > const_argument_range;
106
107 public:
108 kernel(clover::program &prog, const std::string &name,
109 const std::vector<clover::module::argument> &margs);
110
111 kernel(const kernel &kern) = delete;
112 kernel &
113 operator=(const kernel &kern) = delete;
114
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);
119
120 size_t mem_local() const;
121 size_t mem_private() const;
122
123 const std::string &name() const;
124
125 std::vector<size_t>
126 optimal_block_size(const command_queue &q,
127 const std::vector<size_t> &grid_size) const;
128 std::vector<size_t>
129 required_block_size() const;
130
131 argument_range args();
132 const_argument_range args() const;
133
134 const intrusive_ref<clover::program> program;
135
136 private:
137 const clover::module &module(const command_queue &q) const;
138
139 class scalar_argument : public argument {
140 public:
141 scalar_argument(size_t size);
142
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);
147
148 private:
149 size_t size;
150 std::vector<uint8_t> v;
151 };
152
153 class global_argument : public argument {
154 public:
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);
159
160 private:
161 buffer *buf;
162 };
163
164 class local_argument : public argument {
165 public:
166 virtual size_t storage() const;
167
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);
172
173 private:
174 size_t _storage;
175 };
176
177 class constant_argument : public argument {
178 public:
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);
183
184 private:
185 buffer *buf;
186 pipe_surface *st;
187 };
188
189 class image_rd_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 image *img;
198 pipe_sampler_view *st;
199 };
200
201 class image_wr_argument : public argument {
202 public:
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);
207
208 private:
209 image *img;
210 pipe_surface *st;
211 };
212
213 class sampler_argument : public argument {
214 public:
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);
219
220 private:
221 sampler *s;
222 void *st;
223 };
224
225 std::vector<std::unique_ptr<argument>> _args;
226 std::string _name;
227 exec_context exec;
228 };
229 }
230
231 #endif