Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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 __CORE_KERNEL_HPP__
  24. #define __CORE_KERNEL_HPP__
  25.  
  26. #include <memory>
  27.  
  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"
  33.  
  34. namespace clover {
  35.    typedef struct _cl_kernel kernel;
  36.    class argument;
  37. }
  38.  
  39. struct _cl_kernel : public clover::ref_counter {
  40. private:
  41.    ///
  42.    /// Class containing all the state required to execute a compute
  43.    /// kernel.
  44.    ///
  45.    struct exec_context {
  46.       exec_context(clover::kernel &kern);
  47.       ~exec_context();
  48.  
  49.       void *bind(clover::command_queue *q);
  50.       void unbind();
  51.  
  52.       clover::kernel &kern;
  53.       clover::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.       /// \a true if the argument has been set.
  74.       bool set() const;
  75.  
  76.       /// Storage space required for the referenced object.
  77.       virtual size_t storage() const;
  78.  
  79.       /// Set this argument to some object.
  80.       virtual void set(size_t size, const void *value) = 0;
  81.  
  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;
  86.  
  87.       /// Free any resources that were allocated in bind().
  88.       virtual void unbind(exec_context &ctx) = 0;
  89.  
  90.    protected:
  91.       bool __set;
  92.    };
  93.  
  94.    _cl_kernel(clover::program &prog,
  95.               const std::string &name,
  96.               const std::vector<clover::module::argument> &margs);
  97.  
  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);
  102.  
  103.    size_t mem_local() const;
  104.    size_t mem_private() const;
  105.    size_t max_block_size() const;
  106.  
  107.    const std::string &name() const;
  108.    std::vector<size_t> block_size() const;
  109.  
  110.    clover::program &prog;
  111.    std::vector<std::unique_ptr<argument>> args;
  112.  
  113. private:
  114.    const clover::module &
  115.    module(const clover::command_queue &q) const;
  116.  
  117.    class scalar_argument : public argument {
  118.    public:
  119.       scalar_argument(size_t size);
  120.  
  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);
  125.  
  126.    private:
  127.       size_t size;
  128.       std::vector<uint8_t> v;
  129.    };
  130.  
  131.    class global_argument : public argument {
  132.    public:
  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);
  137.  
  138.    private:
  139.       clover::buffer *obj;
  140.    };
  141.  
  142.    class local_argument : public argument {
  143.    public:
  144.       virtual size_t storage() const;
  145.  
  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);
  150.  
  151.    private:
  152.       size_t __storage;
  153.    };
  154.  
  155.    class constant_argument : public argument {
  156.    public:
  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);
  161.  
  162.    private:
  163.       clover::buffer *obj;
  164.       pipe_surface *st;
  165.    };
  166.  
  167.    class image_rd_argument : public argument {
  168.    public:
  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);
  173.  
  174.    private:
  175.       clover::image *obj;
  176.       pipe_sampler_view *st;
  177.    };
  178.  
  179.    class image_wr_argument : public argument {
  180.    public:
  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);
  185.  
  186.    private:
  187.       clover::image *obj;
  188.       pipe_surface *st;
  189.    };
  190.  
  191.    class sampler_argument : public argument {
  192.    public:
  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);
  197.  
  198.    private:
  199.       clover::sampler *obj;
  200.       void *st;
  201.    };
  202.  
  203.    std::string __name;
  204.    exec_context exec;
  205. };
  206.  
  207. #endif
  208.