[Beignet] [PATCH 1/2] Add example of using cl_khr_gl_sharing to do gl buffer sharing.
Chuanbo Weng
chuanbo.weng at intel.com
Mon Jan 23 11:18:09 UTC 2017
Signed-off-by: Chuanbo Weng <chuanbo.weng at intel.com>
---
CMakeLists.txt | 34 +++----
examples/CMakeLists.txt | 80 ++++++++++------
examples/gl_buffer_sharing/gl_buffer_sharing.cpp | 115 +++++++++++++++++++++++
kernels/runtime_fill_gl_image.cl | 15 +++
utests/utest_helper.hpp | 2 +
5 files changed, 199 insertions(+), 47 deletions(-)
create mode 100644 examples/gl_buffer_sharing/gl_buffer_sharing.cpp
create mode 100644 kernels/runtime_fill_gl_image.cl
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 59abc45..c13e3b5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -195,22 +195,18 @@ ELSE(XFIXES_FOUND)
ENDIF(XFIXES_FOUND)
ENDIF(X11_FOUND)
-OPTION(ENABLE_GL_SHARING "cl_khr_gl_sharing" OFF)
-
-IF(ENABLE_GL_SHARING)
- pkg_check_modules(OPENGL REQUIRED gl)
- IF(OPENGL_FOUND)
- MESSAGE(STATUS "Looking for OpenGL - found at ${OPENGL_PREFIX}")
- ELSE(OPENGL_FOUND)
- MESSAGE(STATUS "Looking for OpenGL - not found")
- ENDIF(OPENGL_FOUND)
- pkg_check_modules(EGL REQUIRED egl>=11.0.0)
- IF(EGL_FOUND)
- MESSAGE(STATUS "Looking for EGL - found at ${EGL_PREFIX}")
- ELSE(EGL_FOUND)
- MESSAGE(STATUS "Looking for EGL - not found")
- ENDIF(EGL_FOUND)
-ENDIF(ENABLE_GL_SHARING)
+pkg_check_modules(OPENGL QUIET gl>=13.0.0)
+IF(OPENGL_FOUND)
+ MESSAGE(STATUS "Looking for OpenGL - found at ${OPENGL_PREFIX} ${OPENGL_VERSION}")
+ELSE(OPENGL_FOUND)
+ MESSAGE(STATUS "Looking for OpenGL (>=13.0.0) - not found, cl_khr_gl_sharing will be disabled")
+ENDIF(OPENGL_FOUND)
+pkg_check_modules(EGL QUIET egl>=13.0.0)
+IF(EGL_FOUND)
+ MESSAGE(STATUS "Looking for EGL - found at ${EGL_PREFIX} ${EGL_VERSION}")
+ELSE(EGL_FOUND)
+ MESSAGE(STATUS "Looking for EGL (>=13.0.0) - not found, cl_khr_gl_sharing will be disabled")
+ENDIF(EGL_FOUND)
Find_Package(OCLIcd)
IF(OCLIcd_FOUND)
@@ -278,7 +274,6 @@ IF(NOT X11_FOUND)
ENDIF(NOT X11_FOUND)
# libva & libva-x11
-#pkg_check_modules(LIBVA REQUIRED libva>=0.36.0)
pkg_check_modules(LIBVA REQUIRED libva)
pkg_check_modules(LIBVA-X11 REQUIRED libva-x11)
set(LIBVA_BUF_SH_DEP false)
@@ -300,7 +295,12 @@ IF(LIBVA_FOUND AND LIBVA-X11_FOUND)
ELSE(LIBVA_VERSION VERSION_LESS "0.36.0" OR LIBVA-X11_VERSION VERSION_LESS "0.36.0")
set(LIBVA_BUF_SH_DEP true)
ENDIF(LIBVA_VERSION VERSION_LESS "0.36.0" OR LIBVA-X11_VERSION VERSION_LESS "0.36.0")
+ELSE(LIBVA_FOUND AND LIBVA-X11_FOUND)
+ MESSAGE(STATUS "Example libva_buffer_sharing and v4l2_buffer_sharing will not be built")
ENDIF(LIBVA_FOUND AND LIBVA-X11_FOUND)
+IF(NOT (OPENGL_FOUND AND EGL_FOUND AND X11_FOUND))
+ MESSAGE(STATUS "Example gl_buffer_sharing will not be built")
+ENDIF(NOT (OPENGL_FOUND AND EGL_FOUND AND X11_FOUND))
ENDIF(BUILD_EXAMPLES)
ADD_SUBDIRECTORY(include)
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 850b3d9..a9e35c2 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,44 +1,64 @@
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../utests
${CMAKE_CURRENT_SOURCE_DIR}/../include
- ${X11_INCLUDE_DIR})
+ ${X11_INCLUDE_DIR}
+ ${OPENGL_INCLUDE_DIRS}
+ ${EGL_INCLUDE_DIRS})
+
+set (ocl_example_helper_sources
+ ../utests/utest_error.c
+ ../utests/utest_assert.cpp
+ ../utests/utest_file_map.cpp
+ ../utests/utest_helper.cpp)
+
+IF(OPENGL_FOUND AND EGL_FOUND AND X11_FOUND)
+ ADD_DEFINITIONS(-DHAS_GL_EGL_X11)
+ENDIF(OPENGL_FOUND AND EGL_FOUND AND X11_FOUND)
+
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations" )
+
+ADD_LIBRARY(ocl_example_helper SHARED ${ocl_example_helper_sources})
+
+TARGET_LINK_LIBRARIES(ocl_example_helper cl m ${X11_LIBRARIES} ${OPENGL_LIBRARIES} ${EGL_LIBRARIES})
IF(LIBVA_BUF_SH_DEP OR V4L2_BUF_SH_DEP)
-EXECUTE_PROCESS(COMMAND ls "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libva" OUTPUT_VARIABLE LS_RESULT)
-IF ("LS_RESULT" STREQUAL "")
-EXECUTE_PROCESS(COMMAND git submodule init WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..)
-EXECUTE_PROCESS(COMMAND git submodule update WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..)
-EXECUTE_PROCESS(COMMAND git checkout master WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libva)
-ENDIF ("LS_RESULT" STREQUAL "")
+ EXECUTE_PROCESS(COMMAND ls "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libva" OUTPUT_VARIABLE LS_RESULT)
+ IF ("LS_RESULT" STREQUAL "")
+ EXECUTE_PROCESS(COMMAND git submodule init WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..)
+ EXECUTE_PROCESS(COMMAND git submodule update WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..)
+ EXECUTE_PROCESS(COMMAND git checkout master WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libva)
+ ENDIF ("LS_RESULT" STREQUAL "")
-INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libva/va
- ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libva/test/common)
+ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libva/va
+ ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libva/test/common)
-link_directories (${LIBVA_LIBDIR}
- ${LIBVA-X11_LIBDIR})
+ link_directories (${LIBVA_LIBDIR}
+ ${LIBVA-X11_LIBDIR})
-set (va_ocl_basic_sources
- ../utests/utest_error.c
- ../utests/utest_assert.cpp
- ../utests/utest_file_map.cpp
- ../utests/utest_helper.cpp
- ./thirdparty/libva/test/common/va_display.c
- ./thirdparty/libva/test/common/va_display_x11.c)
+ set (va_display_sources
+ ./thirdparty/libva/test/common/va_display.c
+ ./thirdparty/libva/test/common/va_display_x11.c)
-ADD_DEFINITIONS(-DHAVE_VA_X11)
+ ADD_DEFINITIONS(-DHAVE_VA_X11)
-ADD_LIBRARY(va_ocl_basic SHARED ${va_ocl_basic_sources})
+ ADD_LIBRARY(va_display SHARED ${va_display_sources})
-TARGET_LINK_LIBRARIES(va_ocl_basic cl m va va-x11 ${X11_X11_LIB})
+ TARGET_LINK_LIBRARIES(va_display cl m va va-x11 ${X11_LIBRARIES})
-IF(LIBVA_BUF_SH_DEP)
-ADD_DEFINITIONS(-DINPUT_NV12_DEFAULT="${CMAKE_CURRENT_SOURCE_DIR}/libva_buffer_sharing/256_128.nv12")
-ADD_EXECUTABLE(example-libva_buffer_sharing ./libva_buffer_sharing/libva_buffer_sharing.cpp)
-TARGET_LINK_LIBRARIES(example-libva_buffer_sharing va_ocl_basic)
-ENDIF(LIBVA_BUF_SH_DEP)
+ IF(LIBVA_BUF_SH_DEP)
+ ADD_DEFINITIONS(-DINPUT_NV12_DEFAULT="${CMAKE_CURRENT_SOURCE_DIR}/libva_buffer_sharing/256_128.nv12")
+ ADD_EXECUTABLE(example-libva_buffer_sharing ./libva_buffer_sharing/libva_buffer_sharing.cpp)
+ TARGET_LINK_LIBRARIES(example-libva_buffer_sharing ocl_example_helper va_display)
+ ENDIF(LIBVA_BUF_SH_DEP)
-IF(V4L2_BUF_SH_DEP)
-ADD_EXECUTABLE(example-v4l2_buffer_sharing ./v4l2_buffer_sharing/v4l2_buffer_sharing.cpp)
-TARGET_LINK_LIBRARIES(example-v4l2_buffer_sharing va_ocl_basic)
-ENDIF(V4L2_BUF_SH_DEP)
+ IF(V4L2_BUF_SH_DEP)
+ ADD_EXECUTABLE(example-v4l2_buffer_sharing ./v4l2_buffer_sharing/v4l2_buffer_sharing.cpp)
+ TARGET_LINK_LIBRARIES(example-v4l2_buffer_sharing ocl_example_helper va_display)
+ ENDIF(V4L2_BUF_SH_DEP)
ENDIF(LIBVA_BUF_SH_DEP OR V4L2_BUF_SH_DEP)
+
+IF(OPENGL_FOUND AND EGL_FOUND AND X11_FOUND)
+ ADD_EXECUTABLE(example-gl_buffer_sharing ./gl_buffer_sharing/gl_buffer_sharing.cpp)
+ TARGET_LINK_LIBRARIES(example-gl_buffer_sharing ocl_example_helper ${OPENGL_LIBRARIES} ${EGL_LIBRARIES})
+ENDIF(OPENGL_FOUND AND EGL_FOUND AND X11_FOUND)
diff --git a/examples/gl_buffer_sharing/gl_buffer_sharing.cpp b/examples/gl_buffer_sharing/gl_buffer_sharing.cpp
new file mode 100644
index 0000000..2d7e10d
--- /dev/null
+++ b/examples/gl_buffer_sharing/gl_buffer_sharing.cpp
@@ -0,0 +1,115 @@
+/*
+ * 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.1 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/>.
+ *
+ */
+
+#include "utest_helper.hpp"
+#include <stdio.h>
+
+static cl_int cl_status;
+
+const size_t w = EGL_WINDOW_WIDTH;
+const size_t h = EGL_WINDOW_HEIGHT;
+static GLuint tex;
+
+static void draw(){
+ XEvent event;
+
+ float vertices[8] = {-1, 1, 1, 1, 1, -1, -1, -1};
+ float tex_coords[8] = {0, 0, 1, 0, 1, 1, 0, 1};
+ uint32_t color0 = 0x0000ff00;
+
+ for (;;)
+ {
+ XNextEvent(xDisplay, &event);
+
+ if (event.type == Expose)
+ {
+ glClearColor(0.0, 1.0, 0.0, 1.0);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
+ OCL_SET_ARG(1, sizeof(color0), &color0);
+ globals[0] = w;
+ globals[1] = h;
+ locals[0] = 16;
+ locals[1] = 16;
+ glFinish();
+ OCL_ENQUEUE_ACQUIRE_GL_OBJECTS(0);
+ OCL_NDRANGE(2);
+ OCL_ENQUEUE_RELEASE_GL_OBJECTS(0);
+ OCL_FINISH();
+
+ glBindTexture(GL_TEXTURE_2D, tex);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glEnable(GL_TEXTURE_2D);
+ glDisable(GL_BLEND);
+ glVertexPointer(2, GL_FLOAT, sizeof(float) * 2, vertices);
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glClientActiveTexture(GL_TEXTURE0);
+ glTexCoordPointer(2, GL_FLOAT, sizeof(float) * 2, tex_coords);
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ glFinish();
+ eglSwapBuffers(eglDisplay, eglSurface);
+ }
+ if (event.type == KeyPress)
+ break;
+ }
+}
+
+
+static void initialize_ocl_gl(){
+
+ //ocl initialization: basic & create kernel & check extension
+ printf("\n***********************OpenCL info: ***********************\n");
+ if ((cl_status = cl_test_init("runtime_fill_gl_image.cl", "runtime_fill_gl_image", SOURCE)) != 0){
+ fprintf(stderr, "cl_test_init error\n");
+ exit(1);
+ }
+
+ if (eglContext == EGL_NO_CONTEXT) {
+ fprintf(stderr, "There is no valid egl context! Exit!\n");
+ exit(1);
+ }
+
+ XMapWindow(xDisplay, xWindow);
+
+ // Setup kernel and images
+ glGenTextures(1, &tex);
+ glBindTexture(GL_TEXTURE_2D, tex);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
+
+ //Create cl image from miplevel 0
+ OCL_CREATE_GL_IMAGE(buf[0], 0, GL_TEXTURE_2D, 0, tex);
+}
+
+int main(int argc, char *argv[])
+{
+ initialize_ocl_gl();
+
+ draw();
+
+ //destroy resource of cl & gl
+ cl_test_destroy();
+
+ printf("\nExample run successfully!\n");
+}
diff --git a/kernels/runtime_fill_gl_image.cl b/kernels/runtime_fill_gl_image.cl
new file mode 100644
index 0000000..79d4054
--- /dev/null
+++ b/kernels/runtime_fill_gl_image.cl
@@ -0,0 +1,15 @@
+__kernel void
+runtime_fill_gl_image(image2d_t img, int color)
+{
+ int2 coord;
+ float4 color_v4;
+ int lgid_x = get_group_id(0);
+ int lgid_y = get_group_id(1);
+ int num_groups_x = get_num_groups(0);
+ int num_groups_y = get_num_groups(1);
+
+ coord.x = get_global_id(0);
+ coord.y = get_global_id(1);
+ color_v4 = (float4)( lgid_x/(float)num_groups_x, lgid_y/(float)num_groups_y, 1.0, 1.0);
+ write_imagef(img, coord, color_v4);
+}
diff --git a/utests/utest_helper.hpp b/utests/utest_helper.hpp
index a761325..bff7b13 100644
--- a/utests/utest_helper.hpp
+++ b/utests/utest_helper.hpp
@@ -52,6 +52,8 @@
extern EGLDisplay eglDisplay;
extern EGLContext eglContext;
extern EGLSurface eglSurface;
+extern Display *xDisplay;
+extern Window xWindow;
#endif
union uint32_cast {
--
2.7.4
More information about the Beignet
mailing list