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