|
|
@ -1,12 +1,24 @@
|
|
|
|
#include "CL_Wrapper.h"
|
|
|
|
#include "CL_Wrapper.h"
|
|
|
|
|
|
|
|
|
|
|
|
CL_Wrapper::CL_Wrapper() {
|
|
|
|
CL_Wrapper::CL_Wrapper() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
acquire_platform_and_device();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (cl_khr_gl_sharing_fallback){
|
|
|
|
|
|
|
|
create_cl_context();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
create_shared_context();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
create_command_queue();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CL_Wrapper::~CL_Wrapper() {
|
|
|
|
CL_Wrapper::~CL_Wrapper() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int CL_Wrapper::acquire_platform_and_device(){
|
|
|
|
int CL_Wrapper::acquire_platform_and_device(){
|
|
|
|
|
|
|
|
|
|
|
|
// Get the number of platforms
|
|
|
|
// Get the number of platforms
|
|
|
@ -88,37 +100,72 @@ int CL_Wrapper::acquire_platform_and_device(){
|
|
|
|
platform_id = current_best_device.platform;
|
|
|
|
platform_id = current_best_device.platform;
|
|
|
|
device_id = current_best_device.id;
|
|
|
|
device_id = current_best_device.id;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test for sharing
|
|
|
|
|
|
|
|
size_t ext_str_size = 1024;
|
|
|
|
|
|
|
|
char *ext_str = new char[ext_str_size];
|
|
|
|
|
|
|
|
clGetDeviceInfo(device_id, CL_DEVICE_EXTENSIONS, ext_str_size, ext_str, &ext_str_size);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (std::string(ext_str).find("cl_khr_gl_sharing") == std::string::npos){
|
|
|
|
|
|
|
|
std::cout << "No support for the cl_khr_gl_sharing extension";
|
|
|
|
|
|
|
|
cl_khr_gl_sharing_fallback = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete ext_str;
|
|
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int CL_Wrapper::create_cl_context() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the device doesn't support sharing then we have to create a regular context
|
|
|
|
|
|
|
|
cl_context_properties context_properties[] = {
|
|
|
|
|
|
|
|
CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context = clCreateContext(
|
|
|
|
|
|
|
|
context_properties,
|
|
|
|
|
|
|
|
1,
|
|
|
|
|
|
|
|
&device_id,
|
|
|
|
|
|
|
|
nullptr, nullptr,
|
|
|
|
|
|
|
|
&error
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (assert(error, "clCreateContext"))
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int CL_Wrapper::create_shared_context() {
|
|
|
|
int CL_Wrapper::create_shared_context() {
|
|
|
|
|
|
|
|
|
|
|
|
// Hurray for standards!
|
|
|
|
// Hurray for standards!
|
|
|
|
// Setup the context properties to grab the current GL context
|
|
|
|
// Setup the context properties to grab the current GL context
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef linux
|
|
|
|
#ifdef linux
|
|
|
|
|
|
|
|
|
|
|
|
cl_context_properties context_properties[] = {
|
|
|
|
cl_context_properties context_properties[] = {
|
|
|
|
CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
|
|
|
|
CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
|
|
|
|
CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
|
|
|
|
CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
|
|
|
|
CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
|
|
|
|
CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id,
|
|
|
|
0
|
|
|
|
0
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#elif defined _WIN32
|
|
|
|
#elif defined _WIN32
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Clean this up next time I'm on a windows machine
|
|
|
|
|
|
|
|
//cl_context_properties context_properties[] = {
|
|
|
|
|
|
|
|
// CL_CONTEXT_PLATFORM, (cl_context_properties) platformIds[0],
|
|
|
|
|
|
|
|
// CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(),
|
|
|
|
|
|
|
|
// CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(),
|
|
|
|
|
|
|
|
// 0
|
|
|
|
|
|
|
|
//};
|
|
|
|
|
|
|
|
HGLRC hGLRC = wglGetCurrentContext();
|
|
|
|
HGLRC hGLRC = wglGetCurrentContext();
|
|
|
|
HDC hDC = wglGetCurrentDC();
|
|
|
|
HDC hDC = wglGetCurrentDC();
|
|
|
|
cl_context_properties context_properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id, CL_GL_CONTEXT_KHR, (cl_context_properties)hGLRC, CL_WGL_HDC_KHR, (cl_context_properties)hDC, 0 };
|
|
|
|
cl_context_properties context_properties[] = {
|
|
|
|
|
|
|
|
CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id,
|
|
|
|
|
|
|
|
CL_GL_CONTEXT_KHR, (cl_context_properties)hGLRC,
|
|
|
|
|
|
|
|
CL_WGL_HDC_KHR, (cl_context_properties)hDC,
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#elif defined TARGET_OS_MAC
|
|
|
|
#elif defined TARGET_OS_MAC
|
|
|
|
|
|
|
|
|
|
|
|
CGLContextObj glContext = CGLGetCurrentContext();
|
|
|
|
CGLContextObj glContext = CGLGetCurrentContext();
|
|
|
|
CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext);
|
|
|
|
CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext);
|
|
|
|
cl_context_properties context_properties[] = {
|
|
|
|
cl_context_properties context_properties[] = {
|
|
|
@ -237,9 +284,9 @@ int CL_Wrapper::set_kernel_arg(
|
|
|
|
|
|
|
|
|
|
|
|
int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags) {
|
|
|
|
int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags) {
|
|
|
|
|
|
|
|
|
|
|
|
cl_mem buff = clCreateBuffer(
|
|
|
|
cl_mem buff = clCreateBuffer(
|
|
|
|
getContext(), flags,
|
|
|
|
getContext(), flags,
|
|
|
|
size, data, &error
|
|
|
|
size, data, &error
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (assert(error, "clCreateBuffer"))
|
|
|
|
if (assert(error, "clCreateBuffer"))
|
|
|
@ -253,9 +300,9 @@ int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data,
|
|
|
|
|
|
|
|
|
|
|
|
int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data) {
|
|
|
|
int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data) {
|
|
|
|
|
|
|
|
|
|
|
|
cl_mem buff = clCreateBuffer(
|
|
|
|
cl_mem buff = clCreateBuffer(
|
|
|
|
getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
|
|
|
|
getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
|
|
|
|
size, data, &error
|
|
|
|
size, data, &error
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (assert(error, "clCreateBuffer"))
|
|
|
|
if (assert(error, "clCreateBuffer"))
|
|
|
@ -278,8 +325,8 @@ int CL_Wrapper::run_kernel(std::string kernel_name, const int work_size){
|
|
|
|
|
|
|
|
|
|
|
|
cl_kernel kernel = kernel_map.at(kernel_name);
|
|
|
|
cl_kernel kernel = kernel_map.at(kernel_name);
|
|
|
|
|
|
|
|
|
|
|
|
error = clEnqueueAcquireGLObjects(getCommandQueue(), 1, &buffer_map.at("image_buffer"), 0, 0, 0);
|
|
|
|
error = clEnqueueAcquireGLObjects(getCommandQueue(), 1, &buffer_map.at("image_buffer"), 0, 0, 0);
|
|
|
|
if (assert(error, "clEnqueueAcquireGLObjects"))
|
|
|
|
if (assert(error, "clEnqueueAcquireGLObjects"))
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
//error = clEnqueueTask(command_queue, kernel, 0, NULL, NULL);
|
|
|
|
//error = clEnqueueTask(command_queue, kernel, 0, NULL, NULL);
|
|
|
@ -294,8 +341,8 @@ int CL_Wrapper::run_kernel(std::string kernel_name, const int work_size){
|
|
|
|
clFinish(getCommandQueue());
|
|
|
|
clFinish(getCommandQueue());
|
|
|
|
|
|
|
|
|
|
|
|
// What if errors out and gl objects are never released?
|
|
|
|
// What if errors out and gl objects are never released?
|
|
|
|
error = clEnqueueReleaseGLObjects(getCommandQueue(), 1, &buffer_map.at("image_buffer"), 0, NULL, NULL);
|
|
|
|
error = clEnqueueReleaseGLObjects(getCommandQueue(), 1, &buffer_map.at("image_buffer"), 0, NULL, NULL);
|
|
|
|
if (assert(error, "clEnqueueReleaseGLObjects"))
|
|
|
|
if (assert(error, "clEnqueueReleaseGLObjects"))
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|