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 _destroy_notify([]{}) {
34 if (flags
& (CL_MEM_COPY_HOST_PTR
| CL_MEM_USE_HOST_PTR
))
35 data
.append((char *)host_ptr
, size
);
38 memory_obj::~memory_obj() {
43 memory_obj::operator==(const memory_obj
&obj
) const {
48 memory_obj::destroy_notify(std::function
<void ()> f
) {
53 memory_obj::flags() const {
58 memory_obj::size() const {
63 memory_obj::host_ptr() const {
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
) {
73 buffer::type() const {
74 return CL_MEM_OBJECT_BUFFER
;
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
) {
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
));
91 resources
.insert(std::make_pair(&q
.device(),
92 std::unique_ptr
<root_resource
>(r
)));
96 return *resources
.find(&q
.device())->second
;
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
) {
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() }});
112 resources
.insert(std::make_pair(&q
.device(),
113 std::unique_ptr
<sub_resource
>(r
)));
116 return *resources
.find(&q
.device())->second
;
120 sub_buffer::offset() const {
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
,
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
) {
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
));
143 resources
.insert(std::make_pair(&q
.device(),
144 std::unique_ptr
<root_resource
>(r
)));
148 return *resources
.find(&q
.device())->second
;
152 image::format() const {
157 image::width() const {
162 image::height() const {
167 image::depth() const {
172 image::pixel_size() const {
173 return util_format_get_blocksize(translate_format(_format
));
177 image::row_pitch() const {
182 image::slice_pitch() const {
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
,
190 image(ctx
, flags
, format
, width
, height
, 0,
191 row_pitch
, 0, height
* row_pitch
, host_ptr
) {
195 image2d::type() const {
196 return CL_MEM_OBJECT_IMAGE2D
;
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
,
204 image(ctx
, flags
, format
, width
, height
, depth
,
205 row_pitch
, slice_pitch
, depth
* slice_pitch
,
210 image3d::type() const {
211 return CL_MEM_OBJECT_IMAGE3D
;