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 __CORE_KERNEL_HPP__
24 #define __CORE_KERNEL_HPP__
28 #include "core/base.hpp"
29 #include "core/program.hpp"
30 #include "core/memory.hpp"
31 #include "core/sampler.hpp"
32 #include "pipe/p_state.h"
35 typedef struct _cl_kernel kernel;
39 struct _cl_kernel : public clover::ref_counter {
42 /// Class containing all the state required to execute a compute
46 exec_context(clover::kernel &kern);
49 void *bind(clover::command_queue *q);
53 clover::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;
71 argument(size_t size);
73 /// \a true if the argument has been set.
76 /// Argument size in the input buffer.
79 /// Storage space required for the referenced object.
80 virtual size_t storage() const;
82 /// Set this argument to some object.
83 virtual void set(size_t size, const void *value) = 0;
85 /// Allocate the necessary resources to bind the specified
86 /// object to this argument, and update \a ctx accordingly.
87 virtual void bind(exec_context &ctx) = 0;
89 /// Free any resources that were allocated in bind().
90 virtual void unbind(exec_context &ctx) = 0;
97 _cl_kernel(clover::program &prog,
98 const std::string &name,
99 const std::vector<clover::module::argument> &args);
101 void launch(clover::command_queue &q,
102 const std::vector<size_t> &grid_offset,
103 const std::vector<size_t> &grid_size,
104 const std::vector<size_t> &block_size);
106 size_t mem_local() const;
107 size_t mem_private() const;
108 size_t max_block_size() const;
110 const std::string &name() const;
111 std::vector<size_t> block_size() const;
113 clover::program &prog;
114 std::vector<std::unique_ptr<argument>> args;
117 const clover::module &
118 module(const clover::command_queue &q) const;
120 class scalar_argument : public argument {
122 scalar_argument(size_t size);
124 virtual void set(size_t size, const void *value);
125 virtual void bind(exec_context &ctx);
126 virtual void unbind(exec_context &ctx);
129 std::vector<uint8_t> v;
132 class global_argument : public argument {
134 global_argument(size_t size);
136 virtual void set(size_t size, const void *value);
137 virtual void bind(exec_context &ctx);
138 virtual void unbind(exec_context &ctx);
144 class local_argument : public argument {
148 virtual size_t storage() const;
150 virtual void set(size_t size, const void *value);
151 virtual void bind(exec_context &ctx);
152 virtual void unbind(exec_context &ctx);
158 class constant_argument : public argument {
162 virtual void set(size_t size, const void *value);
163 virtual void bind(exec_context &ctx);
164 virtual void unbind(exec_context &ctx);
171 class image_rd_argument : public argument {
175 virtual void set(size_t size, const void *value);
176 virtual void bind(exec_context &ctx);
177 virtual void unbind(exec_context &ctx);
181 pipe_sampler_view *st;
184 class image_wr_argument : public argument {
188 virtual void set(size_t size, const void *value);
189 virtual void bind(exec_context &ctx);
190 virtual void unbind(exec_context &ctx);
197 class sampler_argument : public argument {
201 virtual void set(size_t size, const void *value);
202 virtual void bind(exec_context &ctx);
203 virtual void unbind(exec_context &ctx);
206 clover::sampler *obj;