cleaned up an unused file, added check for sharing
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
#include <iostream>
|
||||
#include "util.hpp"
|
||||
#include <map>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef linux
|
||||
#include <CL/cl.h>
|
||||
#include <CL/opencl.h>
|
||||
#include <GL/glx.h>
|
||||
|
||||
#elif defined _WIN32
|
||||
#include <CL/cl_gl.h>
|
||||
@@ -35,13 +37,19 @@ public:
|
||||
~CL_Wrapper();
|
||||
|
||||
int acquire_platform_and_device();
|
||||
|
||||
int create_shared_context();
|
||||
int create_cl_context();
|
||||
|
||||
int create_command_queue();
|
||||
|
||||
int compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name);
|
||||
|
||||
int create_buffer(std::string buffer_name, cl_uint size, void* data);
|
||||
int create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags);
|
||||
int set_kernel_arg(std::string kernel_name, int index, std::string buffer_name);
|
||||
int store_buffer(cl_mem, std::string buffer_name);
|
||||
|
||||
int set_kernel_arg(std::string kernel_name, int index, std::string buffer_name);
|
||||
int run_kernel(std::string kernel_name, const int work_size);
|
||||
|
||||
bool assert(int error_code, std::string function_name);
|
||||
@@ -56,7 +64,7 @@ private:
|
||||
|
||||
int error = 0;
|
||||
bool initialized = false;
|
||||
|
||||
bool cl_khr_gl_sharing_fallback = false;
|
||||
|
||||
cl_platform_id platform_id;
|
||||
cl_device_id device_id;
|
||||
|
||||
@@ -2,16 +2,18 @@
|
||||
#include <SFML/System/Vector3.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <cmath>
|
||||
#define _USE_MATH_DEFINES
|
||||
#include <math.h>
|
||||
#include "util.hpp"
|
||||
#include <deque>
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
#include <deque>
|
||||
#define _USE_MATH_DEFINES
|
||||
#include <math.h>
|
||||
|
||||
#define CHUNK_DIM 32
|
||||
|
||||
|
||||
@@ -1,12 +1,24 @@
|
||||
#include "CL_Wrapper.h"
|
||||
|
||||
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() {
|
||||
}
|
||||
|
||||
|
||||
int CL_Wrapper::acquire_platform_and_device(){
|
||||
|
||||
// Get the number of platforms
|
||||
@@ -88,37 +100,72 @@ int CL_Wrapper::acquire_platform_and_device(){
|
||||
platform_id = current_best_device.platform;
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
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() {
|
||||
|
||||
// Hurray for standards!
|
||||
// Setup the context properties to grab the current GL context
|
||||
|
||||
#ifdef linux
|
||||
|
||||
cl_context_properties context_properties[] = {
|
||||
CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
|
||||
CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
|
||||
CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
|
||||
CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id,
|
||||
0
|
||||
};
|
||||
|
||||
#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();
|
||||
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
|
||||
|
||||
CGLContextObj glContext = CGLGetCurrentContext();
|
||||
CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext);
|
||||
cl_context_properties context_properties[] = {
|
||||
|
||||
17
src/Map.cpp
17
src/Map.cpp
@@ -1,9 +1,4 @@
|
||||
#pragma once
|
||||
#include "Map.h"
|
||||
#include <iostream>
|
||||
#include <SFML/System/Vector3.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include "util.hpp"
|
||||
|
||||
Map::Map(sf::Vector3i position) {
|
||||
|
||||
@@ -365,18 +360,6 @@ void Map::setVoxel(sf::Vector3i world_position, int val) {
|
||||
|
||||
}
|
||||
|
||||
void Map::moveLight(sf::Vector2f in) {
|
||||
//
|
||||
// sf::Vector3f light_spherical = CartToSphere(global_light);
|
||||
//
|
||||
// light_spherical.y += in.y;
|
||||
// light_spherical.x += in.x;
|
||||
//
|
||||
// global_light = SphereToCart(light_spherical);
|
||||
//
|
||||
// return;
|
||||
}
|
||||
|
||||
void Chunk::set(int type) {
|
||||
for (int i = 0; i < CHUNK_DIM * CHUNK_DIM * CHUNK_DIM; i++) {
|
||||
voxel_data[i] = 0;
|
||||
|
||||
@@ -1,138 +0,0 @@
|
||||
#pragma once
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#ifdef linux
|
||||
|
||||
#elif defined _WIN32
|
||||
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
|
||||
#include <CL/cl_gl.h>
|
||||
#include <CL/cl.h>
|
||||
#include <CL/opencl.h>
|
||||
|
||||
#elif defined TARGET_OS_MAC
|
||||
# include <OpenGL/OpenGL.h>
|
||||
# include <OpenCL/opencl.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TARGET_OS_MAC
|
||||
int IsExtensionSupported(
|
||||
const char* support_str,
|
||||
const char* ext_string,
|
||||
size_t ext_buffer_size) {
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
const char* space_substr = strnstr(ext_string + offset, " ", ext_buffer_size - offset);
|
||||
|
||||
size_t space_pos = space_substr ? space_substr - ext_string : 0;
|
||||
|
||||
while (space_pos < ext_buffer_size) {
|
||||
|
||||
if( strncmp(support_str, ext_string + offset, space_pos) == 0 ) {
|
||||
// Device supports requested extension!
|
||||
printf("Info: Found extension support ‘%s’!\n", support_str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Keep searching -- skip to next token string
|
||||
offset = space_pos + 1;
|
||||
space_substr = strnstr(ext_string + offset, " ", ext_buffer_size - offset);
|
||||
space_pos = space_substr ? space_substr - ext_string : 0;
|
||||
}
|
||||
|
||||
std::cout << "Warning: Extension not supported " << support_str << std::endl;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
inline int test_for_gl_cl_sharing() {
|
||||
|
||||
|
||||
int err = 0;
|
||||
|
||||
#if defined (__APPLE__) || defined(MACOSX)
|
||||
static const char *CL_GL_SHARING_EXT = "cl_APPLE_gl_sharing";
|
||||
#else
|
||||
static const char* CL_GL_SHARING_EXT = "cl_khr_gl_sharing";
|
||||
#endif
|
||||
|
||||
cl_uint num_devices;
|
||||
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
|
||||
|
||||
cl_device_id *devices = (cl_device_id *) calloc(sizeof(cl_device_id), num_devices);
|
||||
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
|
||||
|
||||
// Get string containing supported device extensions
|
||||
size_t ext_size = 1024;
|
||||
char *ext_string = (char *) malloc(ext_size);
|
||||
err = clGetDeviceInfo(devices[0], CL_DEVICE_EXTENSIONS, ext_size, ext_string, &ext_size);
|
||||
|
||||
free(devices);
|
||||
|
||||
// Search for GL support in extension string (space delimited)
|
||||
//int supported = IsExtensionSupported(CL_GL_SHARING_EXT, ext_string, ext_size);
|
||||
int supported = 0;
|
||||
if (supported) {
|
||||
// Device supports context sharing with OpenGL
|
||||
printf("Found GL Sharing Support!\n");
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
inline int query_platform_devices() {
|
||||
|
||||
int error = 0;
|
||||
// Get the number of platforms
|
||||
cl_uint platform_count = 0;
|
||||
clGetPlatformIDs(0, nullptr, &platform_count);
|
||||
|
||||
// Fetch the platforms
|
||||
std::vector<cl_platform_id> platformIds(platform_count);
|
||||
clGetPlatformIDs(platform_count, platformIds.data(), nullptr);
|
||||
|
||||
|
||||
for (unsigned int q = 0; q < platform_count; q++) {
|
||||
|
||||
// From stackoverflow, gets and lists the compute devices
|
||||
cl_uint num_devices, i;
|
||||
error = clGetDeviceIDs(platformIds[q], CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
|
||||
|
||||
cl_device_id *devices = (cl_device_id *)calloc(sizeof(cl_device_id), num_devices);
|
||||
error = clGetDeviceIDs(platformIds[q], CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
|
||||
|
||||
char buf[128];
|
||||
for (i = 0; i < num_devices; i++) {
|
||||
clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 128, buf, NULL);
|
||||
fprintf(stdout, "Device %s supports ", buf);
|
||||
|
||||
clGetDeviceInfo(devices[i], CL_DEVICE_VERSION, 128, buf, NULL);
|
||||
fprintf(stdout, "%s\n", buf);
|
||||
|
||||
|
||||
//cl_device_type a;
|
||||
//clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, 128, &a, NULL);
|
||||
//std::cout << a << std::endl;
|
||||
|
||||
//cl_uint b;
|
||||
//clGetDeviceInfo(devices[i], CL_DEVICE_MAX_CLOCK_FREQUENCY, 128, &b, NULL);
|
||||
//std::cout << b << std::endl;
|
||||
|
||||
//cl_uint c;
|
||||
//clGetDeviceInfo(devices[i], CL_DEVICE_MAX_COMPUTE_UNITS, 128, &c, NULL);
|
||||
//std::cout << c << std::endl;
|
||||
|
||||
std::cout << devices[i] << std::endl;
|
||||
}
|
||||
|
||||
free(devices);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
14
src/main.cpp
14
src/main.cpp
@@ -25,7 +25,6 @@
|
||||
#include <OpenCL/cl_ext.h>
|
||||
|
||||
#endif
|
||||
#include "TestPlatform.cpp"
|
||||
#include "Map.h"
|
||||
#include "Curses.h"
|
||||
#include "util.hpp"
|
||||
@@ -65,21 +64,14 @@ sf::Texture window_texture;
|
||||
|
||||
int main() {
|
||||
|
||||
CL_Wrapper c;
|
||||
|
||||
|
||||
Map m(sf::Vector3i (50, 50, 50));
|
||||
m.generate_octree();
|
||||
//Map m(sf::Vector3i (50, 50, 50));
|
||||
//m.generate_octree();
|
||||
return 1;
|
||||
|
||||
//sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML");
|
||||
|
||||
//// Setup CL, instantiate and pass in values to the kernel
|
||||
//CL_Wrapper c;
|
||||
//query_platform_devices();
|
||||
//c.acquire_platform_and_device();
|
||||
//c.create_shared_context();
|
||||
//c.create_command_queue();
|
||||
|
||||
//if (c.compile_kernel("../kernels/ray_caster_kernel.cl", true, "min_kern") < 0) {
|
||||
// std::cin.get();
|
||||
// return -1;
|
||||
|
||||
Reference in New Issue
Block a user