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 CLOVER_CORE_RESOURCE_HPP
24#define CLOVER_CORE_RESOURCE_HPP
25
26#include <list>
27
28#include "core/queue.hpp"
29#include "util/algebra.hpp"
30#include "pipe/p_state.h"
31
32namespace clover {
33   class memory_obj;
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 std::array<size_t, 3> vector;
43
44      virtual ~resource();
45
46      resource(const resource &r) = delete;
47      resource &
48      operator=(const resource &r) = delete;
49
50      void copy(command_queue &q, const vector &origin, const vector &region,
51                resource &src_resource, const vector &src_origin);
52
53      void *add_map(command_queue &q, cl_map_flags flags, bool blocking,
54                    const vector &origin, const vector &region);
55      void del_map(void *p);
56      unsigned map_count() const;
57
58      const intrusive_ref<clover::device> device;
59      memory_obj &obj;
60
61      friend class sub_resource;
62      friend class mapping;
63      friend class kernel;
64
65   protected:
66      resource(clover::device &dev, memory_obj &obj);
67
68      pipe_sampler_view *bind_sampler_view(command_queue &q);
69      void unbind_sampler_view(command_queue &q,
70                               pipe_sampler_view *st);
71
72      pipe_surface *bind_surface(command_queue &q, bool rw);
73      void unbind_surface(command_queue &q, pipe_surface *st);
74
75      pipe_resource *pipe;
76      vector offset;
77
78   private:
79      std::list<mapping> maps;
80   };
81
82   ///
83   /// Resource associated with its own top-level data storage
84   /// allocated in some device.
85   ///
86   class root_resource : public resource {
87   public:
88      root_resource(clover::device &dev, memory_obj &obj,
89                    command_queue &q, const std::string &data);
90      root_resource(clover::device &dev, memory_obj &obj, root_resource &r);
91      virtual ~root_resource();
92   };
93
94   ///
95   /// Resource that reuses a portion of some other resource as data
96   /// storage.
97   ///
98   class sub_resource : public resource {
99   public:
100      sub_resource(resource &r, const vector &offset);
101   };
102
103   ///
104   /// Class that represents a mapping of some resource into the CPU
105   /// memory space.
106   ///
107   class mapping {
108   public:
109      mapping(command_queue &q, resource &r, cl_map_flags flags,
110              bool blocking, const resource::vector &origin,
111              const resource::vector &region);
112      mapping(mapping &&m);
113      ~mapping();
114
115      mapping &
116      operator=(mapping m);
117
118      mapping(const mapping &m) = delete;
119
120      template<typename T>
121      operator T *() const {
122         return (T *)p;
123      }
124
125   private:
126      pipe_context *pctx;
127      pipe_transfer *pxfer;
128      pipe_resource *pres;
129      void *p;
130   };
131}
132
133#endif
134