summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/clover/core/memory.cpp
blob: 1bf12e3c36ec3864b1a87114319be0a439d72a00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// Copyright 2012 Francisco Jerez
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

#include "core/memory.hpp"
#include "core/resource.hpp"

using namespace clover;

_cl_mem::_cl_mem(clover::context &ctx, cl_mem_flags flags,
                 size_t size, void *host_ptr) :
   ctx(ctx), __flags(flags),
   __size(size), __host_ptr(host_ptr),
   __destroy_notify([]{}),
   data((char *)host_ptr, (host_ptr ? size : 0)) {
}

_cl_mem::~_cl_mem() {
   __destroy_notify();
}

void
_cl_mem::destroy_notify(std::function<void ()> f) {
   __destroy_notify = f;
}

cl_mem_flags
_cl_mem::flags() const {
   return __flags;
}

size_t
_cl_mem::size() const {
   return __size;
}

void *
_cl_mem::host_ptr() const {
   return __host_ptr;
}

buffer::buffer(clover::context &ctx, cl_mem_flags flags,
               size_t size, void *host_ptr) :
   memory_obj(ctx, flags, size, host_ptr) {
}

cl_mem_object_type
buffer::type() const {
   return CL_MEM_OBJECT_BUFFER;
}

root_buffer::root_buffer(clover::context &ctx, cl_mem_flags flags,
                         size_t size, void *host_ptr) :
   buffer(ctx, flags, size, host_ptr) {
}

clover::resource &
root_buffer::resource(cl_command_queue q) {
   // Create a new resource if there's none for this device yet.
   if (!resources.count(&q->dev)) {
      auto r = (!resources.empty() ?
                new root_resource(q->dev, *this, *resources.begin()->second) :
                new root_resource(q->dev, *this, data));

      resources.insert(std::make_pair(&q->dev,
                                      std::unique_ptr<root_resource>(r)));
      data.clear();
   }

   return *resources.find(&q->dev)->second;
}

sub_buffer::sub_buffer(clover::root_buffer &parent, cl_mem_flags flags,
                       size_t offset, size_t size) :
   buffer(parent.ctx, flags, size,
          (char *)parent.host_ptr() + offset),
   parent(parent), __offset(offset) {
}

clover::resource &
sub_buffer::resource(cl_command_queue q) {
   // Create a new resource if there's none for this device yet.
   if (!resources.count(&q->dev)) {
      auto r = new sub_resource(parent.resource(q), { offset() });

      resources.insert(std::make_pair(&q->dev,
                                      std::unique_ptr<sub_resource>(r)));
   }

   return *resources.find(&q->dev)->second;
}

size_t
sub_buffer::offset() const {
   return __offset;
}

image::image(clover::context &ctx, cl_mem_flags flags,
             const cl_image_format *format,
             size_t width, size_t height, size_t depth,
             size_t row_pitch, size_t slice_pitch, size_t size,
             void *host_ptr) :
   memory_obj(ctx, flags, size, host_ptr),
   __format(*format), __width(width), __height(height), __depth(depth),
   __row_pitch(row_pitch), __slice_pitch(slice_pitch) {
}

clover::resource &
image::resource(cl_command_queue q) {
   // Create a new resource if there's none for this device yet.
   if (!resources.count(&q->dev)) {
      auto r = (!resources.empty() ?
                new root_resource(q->dev, *this, *resources.begin()->second) :
                new root_resource(q->dev, *this, data));

      resources.insert(std::make_pair(&q->dev,
                                      std::unique_ptr<root_resource>(r)));
      data.clear();
   }

   return *resources.find(&q->dev)->second;
}

cl_image_format
image::format() const {
   return __format;
}

size_t
image::width() const {
   return __width;
}

size_t
image::height() const {
   return __height;
}

size_t
image::depth() const {
   return __depth;
}

size_t
image::row_pitch() const {
   return __row_pitch;
}

size_t
image::slice_pitch() const {
   return __slice_pitch;
}

image2d::image2d(clover::context &ctx, cl_mem_flags flags,
                 const cl_image_format *format, size_t width,
                 size_t height, size_t row_pitch,
                 void *host_ptr) :
   image(ctx, flags, format, width, height, 0,
         row_pitch, 0, height * row_pitch, host_ptr) {
}

cl_mem_object_type
image2d::type() const {
   return CL_MEM_OBJECT_IMAGE2D;
}

image3d::image3d(clover::context &ctx, cl_mem_flags flags,
                 const cl_image_format *format,
                 size_t width, size_t height, size_t depth,
                 size_t row_pitch, size_t slice_pitch,
                 void *host_ptr) :
   image(ctx, flags, format, width, height, depth,
         row_pitch, slice_pitch, depth * slice_pitch,
         host_ptr) {
}

cl_mem_object_type
image3d::type() const {
   return CL_MEM_OBJECT_IMAGE3D;
}