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;
73 /// \a true if the argument has been set.
76 /// Storage space required for the referenced object.
77 virtual size_t storage() const;
79 /// Set this argument to some object.
80 virtual void set(size_t size, const void *value) = 0;
82 /// Allocate the necessary resources to bind the specified
83 /// object to this argument, and update \a ctx accordingly.
84 virtual void bind(exec_context &ctx,
85 const clover::module::argument &marg) = 0;
87 /// Free any resources that were allocated in bind().
88 virtual void unbind(exec_context &ctx) = 0;
94 _cl_kernel(clover::program &prog,
95 const std::string &name,
96 const std::vector<clover::module::argument> &margs);
98 void launch(clover::command_queue &q,
99 const std::vector<size_t> &grid_offset,
100 const std::vector<size_t> &grid_size,
101 const std::vector<size_t> &block_size);
103 size_t mem_local() const;
104 size_t mem_private() const;
105 size_t max_block_size() const;
107 const std::string &name() const;
108 std::vector<size_t> block_size() const;
110 clover::program &prog;
111 std::vector<std::unique_ptr<argument>> args;
114 const clover::module &
115 module(const clover::command_queue &q) const;
117 class scalar_argument : public argument {
119 scalar_argument(size_t size);
121 virtual void set(size_t size, const void *value);
122 virtual void bind(exec_context &ctx,
123 const clover::module::argument &marg);
124 virtual void unbind(exec_context &ctx);
128 std::vector<uint8_t> v;
131 class global_argument : public argument {
133 virtual void set(size_t size, const void *value);
134 virtual void bind(exec_context &ctx,
135 const clover::module::argument &marg);
136 virtual void unbind(exec_context &ctx);
142 class local_argument : public argument {
144 virtual size_t storage() const;
146 virtual void set(size_t size, const void *value);
147 virtual void bind(exec_context &ctx,
148 const clover::module::argument &marg);
149 virtual void unbind(exec_context &ctx);
155 class constant_argument : public argument {
157 virtual void set(size_t size, const void *value);
158 virtual void bind(exec_context &ctx,
159 const clover::module::argument &marg);
160 virtual void unbind(exec_context &ctx);
167 class image_rd_argument : public argument {
169 virtual void set(size_t size, const void *value);
170 virtual void bind(exec_context &ctx,
171 const clover::module::argument &marg);
172 virtual void unbind(exec_context &ctx);
176 pipe_sampler_view *st;
179 class image_wr_argument : public argument {
181 virtual void set(size_t size, const void *value);
182 virtual void bind(exec_context &ctx,
183 const clover::module::argument &marg);
184 virtual void unbind(exec_context &ctx);
191 class sampler_argument : public argument {
193 virtual void set(size_t size, const void *value);
194 virtual void bind(exec_context &ctx,
195 const clover::module::argument &marg);
196 virtual void unbind(exec_context &ctx);
199 clover::sampler *obj;