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(context
&ctx
, cl_mem_flags flags
,
30 size_t size
, void *host_ptr
) :
31 ctx(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(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(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
.dev
)) {
86 auto r
= (!resources
.empty() ?
87 new root_resource(q
.dev
, *this, *resources
.begin()->second
) :
88 new root_resource(q
.dev
, *this, q
, data
));
90 resources
.insert(std::make_pair(&q
.dev
,
91 std::unique_ptr
<root_resource
>(r
)));
95 return *resources
.find(&q
.dev
)->second
;
98 sub_buffer::sub_buffer(root_buffer
&parent
, cl_mem_flags flags
,
99 size_t offset
, size_t size
) :
100 buffer(parent
.ctx
, flags
, size
,
101 (char *)parent
.host_ptr() + offset
),
102 parent(parent
), _offset(offset
) {
106 sub_buffer::resource(command_queue
&q
) {
107 // Create a new resource if there's none for this device yet.
108 if (!resources
.count(&q
.dev
)) {
109 auto r
= new sub_resource(parent
.resource(q
), {{ offset() }});
111 resources
.insert(std::make_pair(&q
.dev
,
112 std::unique_ptr
<sub_resource
>(r
)));
115 return *resources
.find(&q
.dev
)->second
;
119 sub_buffer::offset() const {
123 image::image(context
&ctx
, cl_mem_flags flags
,
124 const cl_image_format
*format
,
125 size_t width
, size_t height
, size_t depth
,
126 size_t row_pitch
, size_t slice_pitch
, size_t size
,
128 memory_obj(ctx
, flags
, size
, host_ptr
),
129 _format(*format
), _width(width
), _height(height
), _depth(depth
),
130 _row_pitch(row_pitch
), _slice_pitch(slice_pitch
) {
134 image::resource(command_queue
&q
) {
135 // Create a new resource if there's none for this device yet.
136 if (!resources
.count(&q
.dev
)) {
137 auto r
= (!resources
.empty() ?
138 new root_resource(q
.dev
, *this, *resources
.begin()->second
) :
139 new root_resource(q
.dev
, *this, q
, data
));
141 resources
.insert(std::make_pair(&q
.dev
,
142 std::unique_ptr
<root_resource
>(r
)));
146 return *resources
.find(&q
.dev
)->second
;
150 image::format() const {
155 image::width() const {
160 image::height() const {
165 image::depth() const {
170 image::pixel_size() const {
171 return util_format_get_blocksize(translate_format(_format
));
175 image::row_pitch() const {
180 image::slice_pitch() const {
184 image2d::image2d(context
&ctx
, cl_mem_flags flags
,
185 const cl_image_format
*format
, size_t width
,
186 size_t height
, size_t row_pitch
,
188 image(ctx
, flags
, format
, width
, height
, 0,
189 row_pitch
, 0, height
* row_pitch
, host_ptr
) {
193 image2d::type() const {
194 return CL_MEM_OBJECT_IMAGE2D
;
197 image3d::image3d(context
&ctx
, cl_mem_flags flags
,
198 const cl_image_format
*format
,
199 size_t width
, size_t height
, size_t depth
,
200 size_t row_pitch
, size_t slice_pitch
,
202 image(ctx
, flags
, format
, width
, height
, depth
,
203 row_pitch
, slice_pitch
, depth
* slice_pitch
,
208 image3d::type() const {
209 return CL_MEM_OBJECT_IMAGE3D
;