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"
26 using namespace clover
;
28 _cl_mem::_cl_mem(clover::context
&ctx
, cl_mem_flags flags
,
29 size_t size
, void *host_ptr
) :
30 ctx(ctx
), _flags(flags
),
31 _size(size
), _host_ptr(host_ptr
),
32 _destroy_notify([]{}) {
33 if (flags
& CL_MEM_COPY_HOST_PTR
)
34 data
.append((char *)host_ptr
, size
);
42 _cl_mem::destroy_notify(std::function
<void ()> f
) {
47 _cl_mem::flags() const {
52 _cl_mem::size() const {
57 _cl_mem::host_ptr() const {
61 buffer::buffer(clover::context
&ctx
, cl_mem_flags flags
,
62 size_t size
, void *host_ptr
) :
63 memory_obj(ctx
, flags
, size
, host_ptr
) {
67 buffer::type() const {
68 return CL_MEM_OBJECT_BUFFER
;
71 root_buffer::root_buffer(clover::context
&ctx
, cl_mem_flags flags
,
72 size_t size
, void *host_ptr
) :
73 buffer(ctx
, flags
, size
, host_ptr
) {
77 root_buffer::resource(cl_command_queue q
) {
78 // Create a new resource if there's none for this device yet.
79 if (!resources
.count(&q
->dev
)) {
80 auto r
= (!resources
.empty() ?
81 new root_resource(q
->dev
, *this, *resources
.begin()->second
) :
82 new root_resource(q
->dev
, *this, *q
, data
));
84 resources
.insert(std::make_pair(&q
->dev
,
85 std::unique_ptr
<root_resource
>(r
)));
89 return *resources
.find(&q
->dev
)->second
;
92 sub_buffer::sub_buffer(clover::root_buffer
&parent
, cl_mem_flags flags
,
93 size_t offset
, size_t size
) :
94 buffer(parent
.ctx
, flags
, size
,
95 (char *)parent
.host_ptr() + offset
),
96 parent(parent
), _offset(offset
) {
100 sub_buffer::resource(cl_command_queue q
) {
101 // Create a new resource if there's none for this device yet.
102 if (!resources
.count(&q
->dev
)) {
103 auto r
= new sub_resource(parent
.resource(q
), { offset() });
105 resources
.insert(std::make_pair(&q
->dev
,
106 std::unique_ptr
<sub_resource
>(r
)));
109 return *resources
.find(&q
->dev
)->second
;
113 sub_buffer::offset() const {
117 image::image(clover::context
&ctx
, cl_mem_flags flags
,
118 const cl_image_format
*format
,
119 size_t width
, size_t height
, size_t depth
,
120 size_t row_pitch
, size_t slice_pitch
, size_t size
,
122 memory_obj(ctx
, flags
, size
, host_ptr
),
123 _format(*format
), _width(width
), _height(height
), _depth(depth
),
124 _row_pitch(row_pitch
), _slice_pitch(slice_pitch
) {
128 image::resource(cl_command_queue q
) {
129 // Create a new resource if there's none for this device yet.
130 if (!resources
.count(&q
->dev
)) {
131 auto r
= (!resources
.empty() ?
132 new root_resource(q
->dev
, *this, *resources
.begin()->second
) :
133 new root_resource(q
->dev
, *this, *q
, data
));
135 resources
.insert(std::make_pair(&q
->dev
,
136 std::unique_ptr
<root_resource
>(r
)));
140 return *resources
.find(&q
->dev
)->second
;
144 image::format() const {
149 image::width() const {
154 image::height() const {
159 image::depth() const {
164 image::row_pitch() const {
169 image::slice_pitch() const {
173 image2d::image2d(clover::context
&ctx
, cl_mem_flags flags
,
174 const cl_image_format
*format
, size_t width
,
175 size_t height
, size_t row_pitch
,
177 image(ctx
, flags
, format
, width
, height
, 0,
178 row_pitch
, 0, height
* row_pitch
, host_ptr
) {
182 image2d::type() const {
183 return CL_MEM_OBJECT_IMAGE2D
;
186 image3d::image3d(clover::context
&ctx
, cl_mem_flags flags
,
187 const cl_image_format
*format
,
188 size_t width
, size_t height
, size_t depth
,
189 size_t row_pitch
, size_t slice_pitch
,
191 image(ctx
, flags
, format
, width
, height
, depth
,
192 row_pitch
, slice_pitch
, depth
* slice_pitch
,
197 image3d::type() const {
198 return CL_MEM_OBJECT_IMAGE3D
;