clover: Use conversion operator to initialize build log from compat::string.
[mesa.git] / src / gallium / state_trackers / clover / core / memory.cpp
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 #include "core/memory.hpp"
24 #include "core/resource.hpp"
25 #include "util/u_format.h"
26
27 using namespace clover;
28
29 memory_obj::memory_obj(clover::context &ctx, cl_mem_flags flags,
30 size_t size, void *host_ptr) :
31 context(ctx), _flags(flags),
32 _size(size), _host_ptr(host_ptr),
33 _destroy_notify([]{}) {
34 if (flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR))
35 data.append((char *)host_ptr, size);
36 }
37
38 memory_obj::~memory_obj() {
39 _destroy_notify();
40 }
41
42 bool
43 memory_obj::operator==(const memory_obj &obj) const {
44 return this == &obj;
45 }
46
47 void
48 memory_obj::destroy_notify(std::function<void ()> f) {
49 _destroy_notify = f;
50 }
51
52 cl_mem_flags
53 memory_obj::flags() const {
54 return _flags;
55 }
56
57 size_t
58 memory_obj::size() const {
59 return _size;
60 }
61
62 void *
63 memory_obj::host_ptr() const {
64 return _host_ptr;
65 }
66
67 buffer::buffer(clover::context &ctx, cl_mem_flags flags,
68 size_t size, void *host_ptr) :
69 memory_obj(ctx, flags, size, host_ptr) {
70 }
71
72 cl_mem_object_type
73 buffer::type() const {
74 return CL_MEM_OBJECT_BUFFER;
75 }
76
77 root_buffer::root_buffer(clover::context &ctx, cl_mem_flags flags,
78 size_t size, void *host_ptr) :
79 buffer(ctx, flags, size, host_ptr) {
80 }
81
82 resource &
83 root_buffer::resource(command_queue &q) {
84 // Create a new resource if there's none for this device yet.
85 if (!resources.count(&q.device())) {
86 auto r = (!resources.empty() ?
87 new root_resource(q.device(), *this,
88 *resources.begin()->second) :
89 new root_resource(q.device(), *this, q, data));
90
91 resources.insert(std::make_pair(&q.device(),
92 std::unique_ptr<root_resource>(r)));
93 data.clear();
94 }
95
96 return *resources.find(&q.device())->second;
97 }
98
99 sub_buffer::sub_buffer(root_buffer &parent, cl_mem_flags flags,
100 size_t offset, size_t size) :
101 buffer(parent.context(), flags, size,
102 (char *)parent.host_ptr() + offset),
103 parent(parent), _offset(offset) {
104 }
105
106 resource &
107 sub_buffer::resource(command_queue &q) {
108 // Create a new resource if there's none for this device yet.
109 if (!resources.count(&q.device())) {
110 auto r = new sub_resource(parent().resource(q), {{ offset() }});
111
112 resources.insert(std::make_pair(&q.device(),
113 std::unique_ptr<sub_resource>(r)));
114 }
115
116 return *resources.find(&q.device())->second;
117 }
118
119 size_t
120 sub_buffer::offset() const {
121 return _offset;
122 }
123
124 image::image(clover::context &ctx, cl_mem_flags flags,
125 const cl_image_format *format,
126 size_t width, size_t height, size_t depth,
127 size_t row_pitch, size_t slice_pitch, size_t size,
128 void *host_ptr) :
129 memory_obj(ctx, flags, size, host_ptr),
130 _format(*format), _width(width), _height(height), _depth(depth),
131 _row_pitch(row_pitch), _slice_pitch(slice_pitch) {
132 }
133
134 resource &
135 image::resource(command_queue &q) {
136 // Create a new resource if there's none for this device yet.
137 if (!resources.count(&q.device())) {
138 auto r = (!resources.empty() ?
139 new root_resource(q.device(), *this,
140 *resources.begin()->second) :
141 new root_resource(q.device(), *this, q, data));
142
143 resources.insert(std::make_pair(&q.device(),
144 std::unique_ptr<root_resource>(r)));
145 data.clear();
146 }
147
148 return *resources.find(&q.device())->second;
149 }
150
151 cl_image_format
152 image::format() const {
153 return _format;
154 }
155
156 size_t
157 image::width() const {
158 return _width;
159 }
160
161 size_t
162 image::height() const {
163 return _height;
164 }
165
166 size_t
167 image::depth() const {
168 return _depth;
169 }
170
171 size_t
172 image::pixel_size() const {
173 return util_format_get_blocksize(translate_format(_format));
174 }
175
176 size_t
177 image::row_pitch() const {
178 return _row_pitch;
179 }
180
181 size_t
182 image::slice_pitch() const {
183 return _slice_pitch;
184 }
185
186 image2d::image2d(clover::context &ctx, cl_mem_flags flags,
187 const cl_image_format *format, size_t width,
188 size_t height, size_t row_pitch,
189 void *host_ptr) :
190 image(ctx, flags, format, width, height, 0,
191 row_pitch, 0, height * row_pitch, host_ptr) {
192 }
193
194 cl_mem_object_type
195 image2d::type() const {
196 return CL_MEM_OBJECT_IMAGE2D;
197 }
198
199 image3d::image3d(clover::context &ctx, cl_mem_flags flags,
200 const cl_image_format *format,
201 size_t width, size_t height, size_t depth,
202 size_t row_pitch, size_t slice_pitch,
203 void *host_ptr) :
204 image(ctx, flags, format, width, height, depth,
205 row_pitch, slice_pitch, depth * slice_pitch,
206 host_ptr) {
207 }
208
209 cl_mem_object_type
210 image3d::type() const {
211 return CL_MEM_OBJECT_IMAGE3D;
212 }