Path: blob/21.2-virgl/src/gallium/frontends/clover/core/kernel.hpp
4572 views
//1// Copyright 2012 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#ifndef CLOVER_CORE_KERNEL_HPP23#define CLOVER_CORE_KERNEL_HPP2425#include <map>26#include <memory>2728#include "core/object.hpp"29#include "core/printf.hpp"30#include "core/program.hpp"31#include "core/memory.hpp"32#include "core/sampler.hpp"33#include "pipe/p_state.h"3435namespace clover {36class kernel : public ref_counter, public _cl_kernel {37private:38///39/// Class containing all the state required to execute a compute40/// kernel.41///42struct exec_context {43exec_context(kernel &kern);44~exec_context();4546exec_context(const exec_context &) = delete;47exec_context &48operator=(const exec_context &) = delete;4950void *bind(intrusive_ptr<command_queue> _q,51const std::vector<size_t> &grid_offset);52void unbind();5354kernel &kern;55intrusive_ptr<command_queue> q;56std::unique_ptr<printf_handler> print_handler;5758std::vector<uint8_t> input;59std::vector<void *> samplers;60std::vector<pipe_sampler_view *> sviews;61std::vector<pipe_image_view> iviews;62std::vector<pipe_surface *> resources;63std::vector<pipe_resource *> g_buffers;64std::vector<size_t> g_handles;65size_t mem_local;6667private:68void *st;69pipe_compute_state cs;70};7172public:73class argument {74public:75static std::unique_ptr<argument>76create(const module::argument &marg);7778argument(const argument &arg) = delete;79argument &80operator=(const argument &arg) = delete;8182/// \a true if the argument has been set.83bool set() const;8485/// Storage space required for the referenced object.86virtual size_t storage() const;8788/// Set this argument to some object.89virtual void set(size_t size, const void *value) = 0;9091/// Set this argument to an SVM pointer.92virtual void set_svm(const void *value) {93throw error(CL_INVALID_ARG_INDEX);94};9596/// Allocate the necessary resources to bind the specified97/// object to this argument, and update \a ctx accordingly.98virtual void bind(exec_context &ctx,99const module::argument &marg) = 0;100101/// Free any resources that were allocated in bind().102virtual void unbind(exec_context &ctx) = 0;103104virtual ~argument() {};105protected:106argument();107108bool _set;109};110111private:112typedef adaptor_range<113derefs, std::vector<std::unique_ptr<argument>> &114> argument_range;115116typedef adaptor_range<117derefs, const std::vector<std::unique_ptr<argument>> &118> const_argument_range;119120public:121kernel(clover::program &prog, const std::string &name,122const std::vector<clover::module::argument> &margs);123124kernel(const kernel &kern) = delete;125kernel &126operator=(const kernel &kern) = delete;127128void launch(command_queue &q,129const std::vector<size_t> &grid_offset,130const std::vector<size_t> &grid_size,131const std::vector<size_t> &block_size);132133size_t mem_local() const;134size_t mem_private() const;135136const std::string &name() const;137138std::vector<size_t>139optimal_block_size(const command_queue &q,140const std::vector<size_t> &grid_size) const;141std::vector<size_t>142required_block_size() const;143144argument_range args();145const_argument_range args() const;146std::vector<clover::module::arg_info> args_infos();147148const intrusive_ref<clover::program> program;149150private:151const clover::module &module(const command_queue &q) const;152153class scalar_argument : public argument {154public:155scalar_argument(size_t size);156157virtual void set(size_t size, const void *value);158virtual void bind(exec_context &ctx,159const module::argument &marg);160virtual void unbind(exec_context &ctx);161162private:163size_t size;164std::vector<uint8_t> v;165};166167class global_argument : public argument {168public:169global_argument();170171virtual void set(size_t size, const void *value);172virtual void set_svm(const void *value);173virtual void bind(exec_context &ctx,174const module::argument &marg);175virtual void unbind(exec_context &ctx);176177private:178buffer *buf;179const void *svm;180};181182class local_argument : public argument {183public:184virtual size_t storage() const;185186virtual void set(size_t size, const void *value);187virtual void bind(exec_context &ctx,188const module::argument &marg);189virtual void unbind(exec_context &ctx);190191private:192size_t _storage = 0;193};194195class constant_argument : public argument {196public:197constant_argument();198199virtual void set(size_t size, const void *value);200virtual void bind(exec_context &ctx,201const module::argument &marg);202virtual void unbind(exec_context &ctx);203204private:205buffer *buf;206pipe_surface *st;207};208209class image_argument : public argument {210public:211const image *get() const {212return img;213}214protected:215image *img;216};217218class image_rd_argument : public image_argument {219public:220virtual void set(size_t size, const void *value);221virtual void bind(exec_context &ctx,222const module::argument &marg);223virtual void unbind(exec_context &ctx);224225private:226pipe_sampler_view *st;227};228229class image_wr_argument : public image_argument {230public:231virtual void set(size_t size, const void *value);232virtual void bind(exec_context &ctx,233const module::argument &marg);234virtual void unbind(exec_context &ctx);235};236237class sampler_argument : public argument {238public:239sampler_argument();240241virtual void set(size_t size, const void *value);242virtual void bind(exec_context &ctx,243const module::argument &marg);244virtual void unbind(exec_context &ctx);245246private:247sampler *s;248void *st;249};250251std::vector<std::unique_ptr<argument>> _args;252std::map<device *, std::unique_ptr<root_buffer> > _constant_buffers;253std::string _name;254exec_context exec;255const ref_holder program_ref;256};257}258259#endif260261262