Home | History | Annotate | Download | only in core
      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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     18 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
     19 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     20 // SOFTWARE.
     21 //
     22 
     23 #ifndef __CORE_RESOURCE_HPP__
     24 #define __CORE_RESOURCE_HPP__
     25 
     26 #include <list>
     27 
     28 #include "core/base.hpp"
     29 #include "core/memory.hpp"
     30 #include "core/geometry.hpp"
     31 #include "pipe/p_state.h"
     32 
     33 namespace clover {
     34    class mapping;
     35 
     36    ///
     37    /// Class that represents a device-specific instance of some memory
     38    /// object.
     39    ///
     40    class resource {
     41    public:
     42       typedef clover::point<size_t, 3> point;
     43 
     44       resource(const resource &r) = delete;
     45       virtual ~resource();
     46 
     47       void copy(command_queue &q, const point &origin, const point &region,
     48                 resource &src_resource, const point &src_origin);
     49 
     50       void *add_map(command_queue &q, cl_map_flags flags, bool blocking,
     51                     const point &origin, const point &region);
     52       void del_map(void *p);
     53       unsigned map_count() const;
     54 
     55       clover::device &dev;
     56       clover::memory_obj &obj;
     57 
     58       friend class sub_resource;
     59       friend class mapping;
     60       friend struct ::_cl_kernel;
     61 
     62    protected:
     63       resource(clover::device &dev, clover::memory_obj &obj);
     64 
     65       pipe_sampler_view *bind_sampler_view(clover::command_queue &q);
     66       void unbind_sampler_view(clover::command_queue &q,
     67                                pipe_sampler_view *st);
     68 
     69       pipe_surface *bind_surface(clover::command_queue &q, bool rw);
     70       void unbind_surface(clover::command_queue &q, pipe_surface *st);
     71 
     72       pipe_resource *pipe;
     73       point offset;
     74 
     75    private:
     76       std::list<mapping> maps;
     77    };
     78 
     79    ///
     80    /// Resource associated with its own top-level data storage
     81    /// allocated in some device.
     82    ///
     83    class root_resource : public resource {
     84    public:
     85       root_resource(clover::device &dev, clover::memory_obj &obj,
     86                     clover::command_queue &q, const std::string &data);
     87       root_resource(clover::device &dev, clover::memory_obj &obj,
     88                     root_resource &r);
     89       virtual ~root_resource();
     90    };
     91 
     92    ///
     93    /// Resource that reuses a portion of some other resource as data
     94    /// storage.
     95    ///
     96    class sub_resource : public resource {
     97    public:
     98       sub_resource(clover::resource &r, point offset);
     99    };
    100 
    101    ///
    102    /// Class that represents a mapping of some resource into the CPU
    103    /// memory space.
    104    ///
    105    class mapping {
    106    public:
    107       mapping(command_queue &q, resource &r, cl_map_flags flags,
    108               bool blocking, const resource::point &origin,
    109               const resource::point &region);
    110       mapping(const mapping &m) = delete;
    111       mapping(mapping &&m);
    112       ~mapping();
    113 
    114       operator void *() {
    115          return p;
    116       }
    117 
    118       operator char *() {
    119          return (char *)p;
    120       }
    121 
    122    private:
    123       pipe_context *pctx;
    124       pipe_transfer *pxfer;
    125       void *p;
    126    };
    127 }
    128 
    129 #endif
    130