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(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 const std::string &name,
100 const std::vector<module::argument> &margs);
102 kernel(const kernel &kern) = delete;
104 operator=(const kernel &kern) = delete;
106 void launch(command_queue &q,
107 const std::vector<size_t> &grid_offset,
108 const std::vector<size_t> &grid_size,
109 const std::vector<size_t> &block_size);
111 size_t mem_local() const;
112 size_t mem_private() const;
113 size_t max_block_size() const;
115 const std::string &name() const;
116 std::vector<size_t> block_size() const;
119 std::vector<std::unique_ptr<argument>> args;
122 const clover::module &
123 module(const command_queue &q) const;
125 class scalar_argument : public argument {
127 scalar_argument(size_t size);
129 virtual void set(size_t size, const void *value);
130 virtual void bind(exec_context &ctx,
131 const module::argument &marg);
132 virtual void unbind(exec_context &ctx);
136 std::vector<uint8_t> v;
139 class global_argument : public argument {
141 virtual void set(size_t size, const void *value);
142 virtual void bind(exec_context &ctx,
143 const module::argument &marg);
144 virtual void unbind(exec_context &ctx);
150 class local_argument : public argument {
152 virtual size_t storage() const;
154 virtual void set(size_t size, const void *value);
155 virtual void bind(exec_context &ctx,
156 const module::argument &marg);
157 virtual void unbind(exec_context &ctx);
163 class constant_argument : public argument {
165 virtual void set(size_t size, const void *value);
166 virtual void bind(exec_context &ctx,
167 const module::argument &marg);
168 virtual void unbind(exec_context &ctx);
175 class image_rd_argument : public argument {
177 virtual void set(size_t size, const void *value);
178 virtual void bind(exec_context &ctx,
179 const module::argument &marg);
180 virtual void unbind(exec_context &ctx);
184 pipe_sampler_view *st;
187 class image_wr_argument : public argument {
189 virtual void set(size_t size, const void *value);
190 virtual void bind(exec_context &ctx,
191 const module::argument &marg);
192 virtual void unbind(exec_context &ctx);
199 class sampler_argument : public argument {
201 virtual void set(size_t size, const void *value);
202 virtual void bind(exec_context &ctx,
203 const module::argument &marg);
204 virtual void unbind(exec_context &ctx);