summaryrefslogtreecommitdiff
path: root/src/cl_mem_gl.c
blob: c77bcb90c05733273b362df7ed1ab11a6ce9703b (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
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
/* 
 * 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: Zhigang Gong <zhigang.gong@intel.com>
 */
#include <GL/gl.h>
#include <GL/glext.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <assert.h>
#include <stdio.h>

#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_platform_id.h"
#include "cl_mem_gl.h"

#include "CL/cl.h"
#include "CL/cl_intel.h"
#include "CL/cl_gl.h"

#ifndef CL_VERSION_1_2
#define CL_INVALID_IMAGE_DESCRIPTOR -65
#endif

static int cl_get_clformat_from_texture(GLint tex_format, cl_image_format * cl_format)
{
  cl_int ret = CL_SUCCESS;

  switch (tex_format) {
  case GL_RGBA8:
  case GL_RGBA:
  case GL_RGBA16:
  case GL_RGBA8I:
  case GL_RGBA16I:
  case GL_RGBA32I:
  case GL_RGBA8UI:
  case GL_RGBA16UI:
  case GL_RGBA32UI:
  case GL_RGBA16F:
  case GL_RGBA32F:
    cl_format->image_channel_order = CL_RGBA;
    break;
  case GL_BGRA:
    cl_format->image_channel_order = CL_BGRA;
    break;
  default:
    ret = CL_INVALID_IMAGE_DESCRIPTOR;
    goto error;
  }

  switch (tex_format) {
  case GL_RGBA8:
  case GL_RGBA:
  case GL_BGRA:
    cl_format->image_channel_data_type = CL_UNORM_INT8;
    break;
  case GL_RGBA16:
    cl_format->image_channel_data_type = CL_UNORM_INT16;
    break;
  case GL_RGBA8I:
    cl_format->image_channel_data_type = CL_SIGNED_INT8;
    break;
  case GL_RGBA16I:
    cl_format->image_channel_data_type = CL_SIGNED_INT16;
    break;
  case GL_RGBA32I:
    cl_format->image_channel_data_type = CL_SIGNED_INT32;
    break;
  case GL_RGBA8UI:
    cl_format->image_channel_data_type = CL_UNSIGNED_INT8;
    break;
  case GL_RGBA16UI:
    cl_format->image_channel_data_type = CL_UNSIGNED_INT16;
    break;
  case GL_RGBA32UI:
    cl_format->image_channel_data_type = CL_UNSIGNED_INT32;
    break;
  case GL_RGBA16F:
    cl_format->image_channel_data_type = CL_HALF_FLOAT;
    break;
  case GL_RGBA32F:
    cl_format->image_channel_order = CL_FLOAT;
    break;
  default:
    ret = CL_INVALID_IMAGE_DESCRIPTOR;
    goto error;
  }

error:
  return ret;
}

static cl_mem_object_type
get_mem_type_from_target(GLenum texture_target, cl_mem_object_type *type)
{
  switch(texture_target) {
  case GL_TEXTURE_1D: *type = CL_MEM_OBJECT_IMAGE1D; break;
  case GL_TEXTURE_2D: *type = CL_MEM_OBJECT_IMAGE2D; break;
  case GL_TEXTURE_3D: *type = CL_MEM_OBJECT_IMAGE3D; break;
  case GL_TEXTURE_1D_ARRAY: *type = CL_MEM_OBJECT_IMAGE1D_ARRAY; break;
  case GL_TEXTURE_2D_ARRAY: *type = CL_MEM_OBJECT_IMAGE2D_ARRAY; break;
  default:
    return -1;
  }
  return 0;
}

LOCAL cl_mem
cl_mem_new_gl_buffer(cl_context ctx,
                     cl_mem_flags flags,
                     GLuint buf_obj,
                     cl_int *errcode_ret)
{
  NOT_IMPLEMENTED;
}

static EGLImageKHR
cl_create_textured_egl_image(cl_context ctx,
                             GLenum texture_target,
                             GLint miplevel,
                             GLuint texture)
{
  struct cl_gl_ext_deps *egl_funcs;
  EGLDisplay egl_display;
  EGLContext egl_context;
  EGLint egl_attribs[] = { EGL_GL_TEXTURE_LEVEL_KHR, miplevel, EGL_NONE};

  assert(ctx->props.gl_type == CL_GL_EGL_DISPLAY);
  egl_funcs =  CL_EXTENSION_GET_FUNCS(ctx, khr_gl_sharing, gl_ext_deps);
  assert(egl_funcs != NULL);
  egl_display = (EGLDisplay)ctx->props.egl_display;
  egl_context = (EGLDisplay)ctx->props.gl_context;
  return egl_funcs->eglCreateImageKHR_func(egl_display, egl_context,
                                           EGL_GL_TEXTURE_2D_KHR,
                                           (EGLClientBuffer)(uintptr_t)texture,
                                           &egl_attribs[0]);
}

LOCAL cl_mem
cl_mem_new_gl_texture(cl_context ctx,
                      cl_mem_flags flags,
                      GLenum texture_target,
                      GLint miplevel,
                      GLuint texture,
                      cl_int *errcode_ret)
{
  cl_int err = CL_SUCCESS;
  cl_mem mem = NULL;
  EGLImageKHR egl_image;
  int w, h, pitch, tiling;
  unsigned int bpp, intel_fmt;
  cl_image_format cl_format;
  unsigned int gl_format;
  /* Check flags consistency */
  if (UNLIKELY(flags & CL_MEM_COPY_HOST_PTR)) {
    err = CL_INVALID_ARG_VALUE;
    goto error;
  }

  mem = cl_mem_allocate(CL_MEM_IMAGE_TYPE, ctx, flags, 0, 0, errcode_ret);
  if (mem == NULL) {
    err = CL_OUT_OF_HOST_MEMORY;
    goto error;
  }

  egl_image = cl_create_textured_egl_image(ctx, texture_target, miplevel, texture);
  if (egl_image == NULL) {
    err = CL_INVALID_GL_OBJECT;
    goto error;
  }
  mem->bo = cl_buffer_alloc_from_eglimage(ctx, (void*)egl_image, &gl_format, &w, &h, &pitch, &tiling);
  if (UNLIKELY(mem->bo == NULL)) {
    err = CL_MEM_OBJECT_ALLOCATION_FAILURE;
    goto error;
  }
  cl_get_clformat_from_texture(gl_format, &cl_format);

  /* XXX Maybe we'd better to check the hw format in driver? */
  intel_fmt = cl_image_get_intel_format(&cl_format);

  if (intel_fmt == INTEL_UNSUPPORTED_FORMAT) {
    err = CL_INVALID_IMAGE_DESCRIPTOR;
    goto error;
  }
  cl_image_byte_per_pixel(&cl_format, &bpp);

  cl_mem_object_type image_type;
  if (get_mem_type_from_target(texture_target, &image_type) != 0) {
    err = CL_INVALID_IMAGE_DESCRIPTOR;
    goto error;
  }

  cl_mem_image_init(cl_mem_image(mem), w, h, image_type, 1, cl_format, intel_fmt, bpp, pitch, 0, tiling);
  cl_mem_image(mem)->egl_image = egl_image;

exit:
  if (errcode_ret)
    *errcode_ret = err;
  return mem;
error:
  cl_mem_delete(mem);
  mem = NULL;
  goto exit;

}

LOCAL void cl_mem_gl_delete(struct _cl_mem_image *image)
{
  struct cl_gl_ext_deps *egl_funcs;
  EGLDisplay egl_display = (EGLDisplay)image->base.ctx->props.egl_display;
  egl_funcs =  CL_EXTENSION_GET_FUNCS(image->base.ctx, khr_gl_sharing, gl_ext_deps);
  egl_funcs->eglDestroyImageKHR_func(egl_display, image->egl_image);
}