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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/*
* Copyright © 2012 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Benjamin Segovia <benjamin.segovia@intel.com>
*/
#include "cl_mem.h"
#include "cl_image.h"
#include "cl_context.h"
#include "cl_utils.h"
#include "cl_alloc.h"
#include "cl_device_id.h"
#include "cl_driver.h"
#include "CL/cl.h"
#include "CL/cl_intel.h"
#include <assert.h>
#include <stdio.h>
static cl_mem
cl_mem_allocate(cl_context ctx,
cl_mem_flags flags,
size_t sz,
cl_int is_tiled,
cl_int *errcode)
{
cl_buffer_mgr bufmgr = NULL;
cl_mem mem = NULL;
cl_int err = CL_SUCCESS;
size_t alignment = 64;
assert(ctx);
FATAL_IF (flags & CL_MEM_ALLOC_HOST_PTR,
"CL_MEM_ALLOC_HOST_PTR unsupported"); /* XXX */
FATAL_IF (flags & CL_MEM_USE_HOST_PTR,
"CL_MEM_USE_HOST_PTR unsupported"); /* XXX */
if (UNLIKELY(sz == 0)) {
err = CL_INVALID_BUFFER_SIZE;
goto error;
}
/* Allocate and inialize the structure itself */
TRY_ALLOC (mem, CALLOC(struct _cl_mem));
mem->ref_n = 1;
mem->magic = CL_MAGIC_MEM_HEADER;
mem->flags = flags;
/* Pinning will require stricter alignment rules */
if ((flags & CL_MEM_PINNABLE) || is_tiled)
alignment = 4096;
/* Allocate space in memory */
bufmgr = cl_context_get_bufmgr(ctx);
assert(bufmgr);
mem->bo = cl_buffer_alloc(bufmgr, "CL memory object", sz, alignment);
if (UNLIKELY(mem->bo == NULL)) {
err = CL_MEM_OBJECT_ALLOCATION_FAILURE;
goto error;
}
/* Append the buffer in the context buffer list */
pthread_mutex_lock(&ctx->buffer_lock);
mem->next = ctx->buffers;
if (ctx->buffers != NULL)
ctx->buffers->prev = mem;
ctx->buffers = mem;
pthread_mutex_unlock(&ctx->buffer_lock);
mem->ctx = ctx;
cl_context_add_ref(ctx);
exit:
if (errcode)
*errcode = err;
return mem;
error:
cl_mem_delete(mem);
mem = NULL;
goto exit;
}
LOCAL cl_mem
cl_mem_new(cl_context ctx,
cl_mem_flags flags,
size_t sz,
void *data,
cl_int *errcode_ret)
{
cl_int err = CL_SUCCESS;
cl_mem mem = NULL;
/* Check flags consistency */
if (UNLIKELY(flags & CL_MEM_COPY_HOST_PTR && data == NULL)) {
err = CL_INVALID_HOST_PTR;
goto error;
}
/* Create the buffer in video memory */
mem = cl_mem_allocate(ctx, flags, sz, CL_FALSE, &err);
if (mem == NULL || err != CL_SUCCESS)
goto error;
/* Copy the data if required */
if (flags & CL_MEM_COPY_HOST_PTR) /* TODO check other flags too */
cl_buffer_subdata(mem->bo, 0, sz, data);
exit:
if (errcode_ret)
*errcode_ret = err;
return mem;
error:
cl_mem_delete(mem);
mem = NULL;
goto exit;
}
static void
cl_mem_copy_data_linear(cl_mem mem,
size_t w,
size_t h,
size_t pitch,
uint32_t bpp,
void *data)
{
size_t x, y, p;
char *dst;
cl_buffer_map(mem->bo, 1);
dst = cl_buffer_get_virtual(mem->bo);
for (y = 0; y < h; ++y) {
char *src = (char*) data + pitch * y;
for (x = 0; x < w; ++x) {
for (p = 0; p < bpp; ++p)
dst[p] = src[p];
dst += bpp;
src += bpp;
}
}
cl_buffer_unmap(mem->bo);
}
static const uint32_t tile_sz = 4096; /* 4KB per tile */
static const uint32_t tilex_w = 512; /* tileX width in bytes */
static const uint32_t tilex_h = 8; /* tileX height in number of rows */
static const uint32_t tiley_w = 128; /* tileY width in bytes */
static const uint32_t tiley_h = 32; /* tileY height in number of rows */
static void
cl_mem_copy_data_tilex(cl_mem mem,
size_t w,
size_t h,
size_t pitch,
uint32_t bpp,
void *data)
{
const size_t tile_w = tilex_w;
const size_t tile_h = tilex_h;
const size_t aligned_pitch = ALIGN(w * bpp, tile_w);
const size_t aligned_height = ALIGN(h, tile_h);
const size_t tilex_n = aligned_pitch / tile_w;
const size_t tiley_n = aligned_height / tile_h;
size_t x, y, tilex, tiley;
char *img = NULL;
char *end = (char*) data + pitch * h;
cl_buffer_map(mem->bo, 1);
img = cl_buffer_get_virtual(mem->bo);
for (tiley = 0; tiley < tiley_n; ++tiley)
for (tilex = 0; tilex < tilex_n; ++tilex) {
char *tile = img + (tilex + tiley * tilex_n) * tile_sz;
for (y = 0; y < tile_h; ++y) {
char *src = (char*) data + (tiley*tile_h+y)*pitch + tilex*tile_w;
char *dst = tile + y*tile_w;
for (x = 0; x < tile_w; ++x, ++dst, ++src) {
if ((uintptr_t) src < (uintptr_t) end)
*dst = *src;
}
}
}
cl_buffer_unmap(mem->bo);
}
static void
cl_mem_copy_data_tiley(cl_mem mem,
size_t w,
size_t h,
size_t pitch,
uint32_t bpp,
void *data)
{
const size_t tile_w = tiley_w;
const size_t tile_h = tiley_h;
const size_t aligned_pitch = ALIGN(w * bpp, tile_w);
const size_t aligned_height = ALIGN(h, tile_h);
const size_t tilex_n = aligned_pitch / tile_w;
const size_t tiley_n = aligned_height / tile_h;
size_t x, y, tilex, tiley, byte;
char *img = NULL;
char *end = (char*) data + pitch * h;
cl_buffer_map(mem->bo, 1);
img = cl_buffer_get_virtual(mem->bo);
for (tiley = 0; tiley < tiley_n; ++tiley)
for (tilex = 0; tilex < tilex_n; ++tilex) {
char *tile = img + (tiley * tilex_n + tilex) * tile_sz;
for (x = 0; x < tile_w; x += 16) {
char *src = (char*) data + tiley*tile_h*pitch + tilex*tile_w+x;
char *dst = tile + x*tile_h;
for (y = 0; y < tile_h; ++y, dst += 16, src += pitch) {
for (byte = 0; byte < 16; ++byte)
if ((uintptr_t) src + byte < (uintptr_t) end)
dst[byte] = src[byte];
}
}
}
cl_buffer_unmap(mem->bo);
}
LOCAL cl_mem
cl_mem_new_image2D(cl_context ctx,
cl_mem_flags flags,
const cl_image_format *fmt,
size_t w,
size_t h,
size_t pitch,
void *data,
cl_int *errcode_ret)
{
cl_int err = CL_SUCCESS;
cl_mem mem = NULL;
uint32_t bpp = 0, intel_fmt = INTEL_UNSUPPORTED_FORMAT;
size_t sz = 0, aligned_pitch = 0, aligned_h;
cl_image_tiling_t tiling = CL_NO_TILE;
/* Check flags consistency */
if (UNLIKELY((flags & CL_MEM_COPY_HOST_PTR) && data == NULL)) {
err = CL_INVALID_HOST_PTR;
goto error;
}
/* Get the size of each pixel */
if (UNLIKELY((err = cl_image_byte_per_pixel(fmt, &bpp)) != CL_SUCCESS))
goto error;
/* Only a sub-set of the formats are supported */
intel_fmt = cl_image_get_intel_format(fmt);
if (UNLIKELY(intel_fmt == INTEL_UNSUPPORTED_FORMAT)) {
err = CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
goto error;
}
/* See if the user parameters match */
#define DO_IMAGE_ERROR \
do { \
err = CL_INVALID_IMAGE_SIZE; \
goto error; \
} while (0);
if (UNLIKELY(w == 0)) DO_IMAGE_ERROR;
if (UNLIKELY(h == 0)) DO_IMAGE_ERROR;
if (UNLIKELY(w > ctx->device->image2d_max_width)) DO_IMAGE_ERROR;
if (UNLIKELY(h > ctx->device->image2d_max_height)) DO_IMAGE_ERROR;
if (UNLIKELY(bpp*w > pitch)) DO_IMAGE_ERROR;
#undef DO_IMAGE_ERROR
/* Pick up tiling mode (we do only linear on SNB) */
if (cl_driver_get_ver(ctx->drv) != 6)
tiling = CL_TILE_Y;
/* Tiling requires to align both pitch and height */
if (tiling == CL_NO_TILE) {
aligned_pitch = w * bpp;
aligned_h = h;
} else if (tiling == CL_TILE_X) {
aligned_pitch = ALIGN(w * bpp, tilex_w);
aligned_h = ALIGN(h, tilex_h);
} else if (tiling == CL_TILE_Y) {
aligned_pitch = ALIGN(w * bpp, tiley_w);
aligned_h = ALIGN(h, tiley_h);
}
sz = aligned_pitch * aligned_h;
mem = cl_mem_allocate(ctx, flags, sz, tiling != CL_NO_TILE, &err);
if (mem == NULL || err != CL_SUCCESS)
goto error;
/* Copy the data if required */
if (flags & CL_MEM_COPY_HOST_PTR) {
if (tiling == CL_NO_TILE)
cl_mem_copy_data_linear(mem, w, h, pitch, bpp, data);
else if (tiling == CL_TILE_X)
cl_mem_copy_data_tilex(mem, w, h, pitch, bpp, data);
else if (tiling == CL_TILE_Y)
cl_mem_copy_data_tiley(mem, w, h, pitch, bpp, data);
}
mem->w = w;
mem->h = h;
mem->fmt = *fmt;
mem->intel_fmt = intel_fmt;
mem->bpp = bpp;
mem->is_image = 1;
mem->pitch = aligned_pitch;
mem->tiling = tiling;
exit:
if (errcode_ret)
*errcode_ret = err;
return mem;
error:
cl_mem_delete(mem);
mem = NULL;
goto exit;
}
LOCAL void
cl_mem_delete(cl_mem mem)
{
if (UNLIKELY(mem == NULL))
return;
if (atomic_dec(&mem->ref_n) > 1)
return;
if (LIKELY(mem->bo != NULL))
cl_buffer_unreference(mem->bo);
/* Remove it from the list */
assert(mem->ctx);
pthread_mutex_lock(&mem->ctx->buffer_lock);
if (mem->prev)
mem->prev->next = mem->next;
if (mem->next)
mem->next->prev = mem->prev;
if (mem->prev == NULL && mem->next == NULL)
mem->ctx->buffers = NULL;
pthread_mutex_unlock(&mem->ctx->buffer_lock);
cl_context_delete(mem->ctx);
cl_free(mem);
}
LOCAL void
cl_mem_add_ref(cl_mem mem)
{
assert(mem);
atomic_inc(&mem->ref_n);
}
LOCAL void*
cl_mem_map(cl_mem mem)
{
cl_buffer_map(mem->bo, 1);
assert(cl_buffer_get_virtual(mem->bo));
return cl_buffer_get_virtual(mem->bo);
}
LOCAL cl_int
cl_mem_unmap(cl_mem mem)
{
cl_buffer_unmap(mem->bo);
return CL_SUCCESS;
}
LOCAL cl_int
cl_mem_pin(cl_mem mem)
{
assert(mem);
if (UNLIKELY((mem->flags & CL_MEM_PINNABLE) == 0))
return CL_INVALID_MEM_OBJECT;
cl_buffer_pin(mem->bo, 4096);
return CL_SUCCESS;
}
LOCAL cl_int
cl_mem_unpin(cl_mem mem)
{
assert(mem);
if (UNLIKELY((mem->flags & CL_MEM_PINNABLE) == 0))
return CL_INVALID_MEM_OBJECT;
cl_buffer_unpin(mem->bo);
return CL_SUCCESS;
}
|