• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 namespace 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 clear(command_queue &q, const vector &origin, const vector &region,
54                  const std::string &data);
55 
56       mapping *add_map(command_queue &q, cl_map_flags flags, bool blocking,
57                        const vector &origin, const vector &region);
58       void del_map(void *p);
59       unsigned map_count() const;
60 
61       const intrusive_ref<clover::device> device;
62       memory_obj &obj;
63 
64       friend class sub_resource;
65       friend class mapping;
66       friend class kernel;
67 
68    protected:
69       resource(clover::device &dev, memory_obj &obj);
70 
71       pipe_sampler_view *bind_sampler_view(command_queue &q);
72       void unbind_sampler_view(command_queue &q,
73                                pipe_sampler_view *st);
74 
75       pipe_surface *bind_surface(command_queue &q, bool rw);
76       void unbind_surface(command_queue &q, pipe_surface *st);
77 
78       pipe_image_view create_image_view(command_queue &q);
79 
80       pipe_resource *pipe;
81       vector offset;
82 
83    private:
84       std::list<mapping> maps;
85    };
86 
87    ///
88    /// Resource associated with its own top-level data storage
89    /// allocated in some device.
90    ///
91    class root_resource : public resource {
92    public:
93       root_resource(clover::device &dev, memory_obj &obj,
94                     command_queue &q, const void *data_ptr);
95       root_resource(clover::device &dev, memory_obj &obj, root_resource &r);
96       virtual ~root_resource();
97    };
98 
99    ///
100    /// Resource that reuses a portion of some other resource as data
101    /// storage.
102    ///
103    class sub_resource : public resource {
104    public:
105       sub_resource(resource &r, const vector &offset);
106    };
107 
108    ///
109    /// Class that represents a mapping of some resource into the CPU
110    /// memory space.
111    ///
112    class mapping {
113    public:
114       mapping(command_queue &q, resource &r, cl_map_flags flags,
115               bool blocking, const resource::vector &origin,
116               const resource::vector &region);
117       mapping(mapping &&m);
118       ~mapping();
119 
120       mapping &
121       operator=(mapping m);
122 
123       mapping(const mapping &m) = delete;
124 
125       template<typename T>
operator T*() const126       operator T *() const {
127          return (T *)p;
128       }
129 
130       resource::vector pitch() const;
131 
132    private:
133       pipe_context *pctx;
134       pipe_transfer *pxfer;
135       pipe_resource *pres;
136       void *p;
137    };
138 }
139 
140 #endif
141