4bcc3c7689104d2ca83bf4a838c8ef91874decad
[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(command_queue *q);
50 void unbind();
51
52 kernel &kern;
53 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(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 std::vector<size_t> block_size() const;
125
126 argument_range args();
127 const_argument_range args() const;
128
129 program &prog;
130
131 private:
132 const clover::module &module(const command_queue &q) const;
133
134 class scalar_argument : public argument {
135 public:
136 scalar_argument(size_t size);
137
138 virtual void set(size_t size, const void *value);
139 virtual void bind(exec_context &ctx,
140 const module::argument &marg);
141 virtual void unbind(exec_context &ctx);
142
143 private:
144 size_t size;
145 std::vector<uint8_t> v;
146 };
147
148 class global_argument : public argument {
149 public:
150 virtual void set(size_t size, const void *value);
151 virtual void bind(exec_context &ctx,
152 const module::argument &marg);
153 virtual void unbind(exec_context &ctx);
154
155 private:
156 buffer *buf;
157 };
158
159 class local_argument : public argument {
160 public:
161 virtual size_t storage() const;
162
163 virtual void set(size_t size, const void *value);
164 virtual void bind(exec_context &ctx,
165 const module::argument &marg);
166 virtual void unbind(exec_context &ctx);
167
168 private:
169 size_t _storage;
170 };
171
172 class constant_argument : public argument {
173 public:
174 virtual void set(size_t size, const void *value);
175 virtual void bind(exec_context &ctx,
176 const module::argument &marg);
177 virtual void unbind(exec_context &ctx);
178
179 private:
180 buffer *buf;
181 pipe_surface *st;
182 };
183
184 class image_rd_argument : public argument {
185 public:
186 virtual void set(size_t size, const void *value);
187 virtual void bind(exec_context &ctx,
188 const module::argument &marg);
189 virtual void unbind(exec_context &ctx);
190
191 private:
192 image *img;
193 pipe_sampler_view *st;
194 };
195
196 class image_wr_argument : public argument {
197 public:
198 virtual void set(size_t size, const void *value);
199 virtual void bind(exec_context &ctx,
200 const module::argument &marg);
201 virtual void unbind(exec_context &ctx);
202
203 private:
204 image *img;
205 pipe_surface *st;
206 };
207
208 class sampler_argument : public argument {
209 public:
210 virtual void set(size_t size, const void *value);
211 virtual void bind(exec_context &ctx,
212 const module::argument &marg);
213 virtual void unbind(exec_context &ctx);
214
215 private:
216 sampler *s;
217 void *st;
218 };
219
220 std::vector<std::unique_ptr<argument>> _args;
221 std::string _name;
222 exec_context exec;
223 };
224 }
225
226 #endif