2 // Copyright 2012 Francisco Jerez
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
23 #include "core/memory.hpp"
24 #include "core/resource.hpp"
25 #include "util/u_format.h"
27 using namespace clover
;
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 if (flags
& CL_MEM_COPY_HOST_PTR
)
34 data
.append((char *)host_ptr
, size
);
37 memory_obj::~memory_obj() {
38 while (_destroy_notify
.size()) {
39 _destroy_notify
.top()();
40 _destroy_notify
.pop();
45 memory_obj::operator==(const memory_obj
&obj
) const {
50 memory_obj::destroy_notify(std::function
<void ()> f
) {
51 _destroy_notify
.push(f
);
55 memory_obj::flags() const {
60 memory_obj::size() const {
65 memory_obj::host_ptr() const {
69 buffer::buffer(clover::context
&ctx
, cl_mem_flags flags
,
70 size_t size
, void *host_ptr
) :
71 memory_obj(ctx
, flags
, size
, host_ptr
) {
75 buffer::type() const {
76 return CL_MEM_OBJECT_BUFFER
;
79 root_buffer::root_buffer(clover::context
&ctx
, cl_mem_flags flags
,
80 size_t size
, void *host_ptr
) :
81 buffer(ctx
, flags
, size
, host_ptr
) {
85 root_buffer::resource(command_queue
&q
) {
86 // Create a new resource if there's none for this device yet.
87 if (!resources
.count(&q
.device())) {
88 auto r
= (!resources
.empty() ?
89 new root_resource(q
.device(), *this,
90 *resources
.begin()->second
) :
91 new root_resource(q
.device(), *this, q
, data
));
93 resources
.insert(std::make_pair(&q
.device(),
94 std::unique_ptr
<root_resource
>(r
)));
98 return *resources
.find(&q
.device())->second
;
101 sub_buffer::sub_buffer(root_buffer
&parent
, cl_mem_flags flags
,
102 size_t offset
, size_t size
) :
103 buffer(parent
.context(), flags
, size
,
104 (char *)parent
.host_ptr() + offset
),
105 parent(parent
), _offset(offset
) {
109 sub_buffer::resource(command_queue
&q
) {
110 // Create a new resource if there's none for this device yet.
111 if (!resources
.count(&q
.device())) {
112 auto r
= new sub_resource(parent().resource(q
), {{ offset() }});
114 resources
.insert(std::make_pair(&q
.device(),
115 std::unique_ptr
<sub_resource
>(r
)));
118 return *resources
.find(&q
.device())->second
;
122 sub_buffer::offset() const {
126 image::image(clover::context
&ctx
, cl_mem_flags flags
,
127 const cl_image_format
*format
,
128 size_t width
, size_t height
, size_t depth
,
129 size_t row_pitch
, size_t slice_pitch
, size_t size
,
131 memory_obj(ctx
, flags
, size
, host_ptr
),
132 _format(*format
), _width(width
), _height(height
), _depth(depth
),
133 _row_pitch(row_pitch
), _slice_pitch(slice_pitch
) {
137 image::resource(command_queue
&q
) {
138 // Create a new resource if there's none for this device yet.
139 if (!resources
.count(&q
.device())) {
140 auto r
= (!resources
.empty() ?
141 new root_resource(q
.device(), *this,
142 *resources
.begin()->second
) :
143 new root_resource(q
.device(), *this, q
, data
));
145 resources
.insert(std::make_pair(&q
.device(),
146 std::unique_ptr
<root_resource
>(r
)));
150 return *resources
.find(&q
.device())->second
;
154 image::format() const {
159 image::width() const {
164 image::height() const {
169 image::depth() const {
174 image::pixel_size() const {
175 return util_format_get_blocksize(translate_format(_format
));
179 image::row_pitch() const {
184 image::slice_pitch() const {
188 image2d::image2d(clover::context
&ctx
, cl_mem_flags flags
,
189 const cl_image_format
*format
, size_t width
,
190 size_t height
, size_t row_pitch
,
192 image(ctx
, flags
, format
, width
, height
, 1,
193 row_pitch
, 0, height
* row_pitch
, host_ptr
) {
197 image2d::type() const {
198 return CL_MEM_OBJECT_IMAGE2D
;
201 image3d::image3d(clover::context
&ctx
, cl_mem_flags flags
,
202 const cl_image_format
*format
,
203 size_t width
, size_t height
, size_t depth
,
204 size_t row_pitch
, size_t slice_pitch
,
206 image(ctx
, flags
, format
, width
, height
, depth
,
207 row_pitch
, slice_pitch
, depth
* slice_pitch
,
212 image3d::type() const {
213 return CL_MEM_OBJECT_IMAGE3D
;