Had a linking problem in the CMakeLists, fixed.

Added a test file to check for cl/gl interop support & supported cl version
Finished CL setup up to the creation of the command queue
master
mitchellhansen 8 years ago
parent bc093ef4e4
commit 49817f94b7

@ -21,8 +21,10 @@ message(STATUS "OpenCL found: ${OPENCL_FOUND}")
find_package( OpenGL REQUIRED) find_package( OpenGL REQUIRED)
message(STATUS "OpenGL found: ${OPENGL_FOUND}") message(STATUS "OpenGL found: ${OPENGL_FOUND}")
# Include the directories for the main programs headers, and SFML's headers # Include the directories for the main program, GL, CL and SFML's headers
include_directories(${SFML_INCLUDE_DIR}) include_directories(${SFML_INCLUDE_DIR})
include_directories(${OpenCL_INCLUDE_DIRS})
include_directories(${OpenGL_INCLUDE_DIRS})
include_directories(include) include_directories(include)
# Set the .cpp sources # Set the .cpp sources
@ -30,9 +32,9 @@ file(GLOB SOURCES "src/*.cpp")
add_executable(${PNAME} ${SOURCES}) add_executable(${PNAME} ${SOURCES})
# Link CL, GL, and SFML # Link CL, GL, and SFML
target_link_libraries(${PNAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES}) target_link_libraries (${PNAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
target_link_libraries (${PNAME} ${OpenCL_LIBRARY}) target_link_libraries (${PNAME} ${OpenCL_LIBRARY})
target_link_libraries (${PNAME} ${OpenGL_LIBRARY}) target_link_libraries (${PNAME} ${OPENGL_LIBRARIES})
# Setup to use C++11 # Setup to use C++11
set_property(TARGET ${PNAME} PROPERTY CXX_STANDARD 11) # Use C++11 set_property(TARGET ${PNAME} PROPERTY CXX_STANDARD 11) # Use C++11

@ -0,0 +1,95 @@
#ifdef linux
#elif defined _WIN32
#elif defined TARGET_OS_MAC
# include <GL/glew.h>
# include <OpenGL/OpenGL.h>
# include <OpenCL/opencl.h>
#endif
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;
}
printf("Warning: Extension not supported %s!\n", support_str);
return 0;
}
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, i;
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);
if (supported) {
// Device supports context sharing with OpenGL
printf("Found GL Sharing Support!\n");
return 1;
}
return -1;
}
int query_platform_devices() {
// From stackoverflow, gets and lists the compute devices
cl_uint num_devices, i;
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);
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);
}
free(devices);
return 1;
}

@ -1,36 +1,37 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <iostream> #include <iostream>
#include <string>
#include <chrono> #include <chrono>
#include "util.hpp" #include "util.hpp"
#include "RayCaster.h" #include "RayCaster.h"
#include <Map.h> #include <Map.h>
#include "Curses.h" #include "Curses.h"
#include <OpenCL/opencl.h> # include <GL/glew.h>
const int WINDOW_X = 150; #ifdef linux
const int WINDOW_Y = 150;
#elif defined _WIN32
#include <CL/cl.h>
#include <CL/opencl.h>
#include <windows.h>
#elif defined TARGET_OS_MAC
# include <OpenGL/OpenGL.h>
# include <OpenCL/opencl.h>
int main(){ #endif
cl_uint num_devices, i;
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
cl_device_id* devices = (cl_device_id*)calloc(sizeof(cl_device_id), num_devices); const int WINDOW_X = 150;
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL); const int WINDOW_Y = 150;
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); int main(){
fprintf(stdout, "%s\n", buf);
}
free(devices); // ===================================================================== //
// ==== Opencl
int error = 0;
// Get the number of platforms // Get the number of platforms
cl_uint platformIdCount = 0; cl_uint platformIdCount = 0;
@ -40,28 +41,68 @@ int main(){
std::vector<cl_platform_id> platformIds (platformIdCount); std::vector<cl_platform_id> platformIds (platformIdCount);
clGetPlatformIDs(platformIdCount, platformIds.data(), nullptr); clGetPlatformIDs(platformIdCount, platformIds.data(), nullptr);
// get the number of devices, fetch them, choose the first one // get the number of devices, fetch them, choose the first one
cl_uint deviceIdCount = 0; cl_uint deviceIdCount = 0;
clGetDeviceIDs (platformIds [0], CL_DEVICE_TYPE_ALL, 0, nullptr,
&deviceIdCount);
std::vector<cl_device_id> deviceIds (deviceIdCount); std::vector<cl_device_id> deviceIds (deviceIdCount);
clGetDeviceIDs (platformIds [0], CL_DEVICE_TYPE_ALL, deviceIdCount,
deviceIds.data (), nullptr);
int error = 0; // Try to get a GPU first
error = clGetDeviceIDs (platformIds [0], CL_DEVICE_TYPE_GPU, 0, nullptr,
&deviceIdCount);
if (deviceIdCount == 0) {
std::cout << "couldn't aquire a GPU, falling back to CPU" << std::endl;
error = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_CPU, 0, nullptr, &deviceIdCount);
error = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_CPU, deviceIdCount, deviceIds.data(), NULL);
} else {
std::cout << "aquired GPU cl target" << std::endl;
clGetDeviceIDs (platformIds[0], CL_DEVICE_TYPE_GPU, deviceIdCount, deviceIds.data (), nullptr);
}
// Set the context's properties // Hurray for standards!
const cl_context_properties contextProperties [] = { // Setup the context properties to grab the current GL context
CL_CONTEXT_PLATFORM, #ifdef linux
reinterpret_cast<cl_context_properties> (platformIds [0]), cl_context_properties context_properties[] = {
0, 0 CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(),
CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(),
CL_CONTEXT_PLATFORM, (cl_context_properties) platform,
0
}; };
cl_context context = clCreateContext ( #elif defined _WIN32
contextProperties, deviceIdCount, cl_context_properties context_properties[] = {
deviceIds.data (), nullptr, CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(),
nullptr, &error); CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(),
CL_CONTEXT_PLATFORM, (cl_context_properties) platform,
0
};
#elif defined TARGET_OS_MAC
CGLContextObj glContext = CGLGetCurrentContext();
CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext);
cl_context_properties context_properties[] = {
CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
(cl_context_properties)shareGroup,
0
};
#endif
// Create our shared context
auto context = clCreateContext(
context_properties,
deviceIdCount,
deviceIds.data(),
nullptr, nullptr,
&error
);
// And the cl command queue
auto commandQueue = clCreateCommandQueue(context, deviceIds[0], 0, NULL);
// At this point the shared GL/CL context is up and running
}; };
@ -108,7 +149,7 @@ void test_ray_reflection(){
return; return;
} }
int main2() { int main0() {
// Initialize the render window // Initialize the render window
Curses curse(sf::Vector2i(5, 5), sf::Vector2i(WINDOW_X, WINDOW_Y)); Curses curse(sf::Vector2i(5, 5), sf::Vector2i(WINDOW_X, WINDOW_Y));
@ -136,7 +177,7 @@ int main2() {
window_sprite.setPosition(0, 0); window_sprite.setPosition(0, 0);
// State values // State values
sf::Vector3i map_dim(50, 50, 50); sf::Vector3i map_dim(100, 100, 100);
sf::Vector2i view_res(WINDOW_X, WINDOW_Y); sf::Vector2i view_res(WINDOW_X, WINDOW_Y);
sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f); sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f);
sf::Vector3f cam_pos(50, 50, 50); sf::Vector3f cam_pos(50, 50, 50);
@ -150,6 +191,7 @@ int main2() {
// Mouse capture // Mouse capture
sf::Vector2i deltas; sf::Vector2i deltas;
sf::Vector2i fixed(window.getSize()); sf::Vector2i fixed(window.getSize());
bool mouse_enabled = true;
while (window.isOpen()) { while (window.isOpen()) {
@ -160,54 +202,11 @@ int main2() {
// If the user tries to exit the application via the GUI // If the user tries to exit the application via the GUI
if (event.type == sf::Event::Closed) if (event.type == sf::Event::Closed)
window.close(); window.close();
if (event.type == sf::Event::KeyPressed) {
// CAMERA DIRECTION
if (event.key.code == sf::Keyboard::Left) {
cam_dir.z -= 0.1f;
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
}
if (event.key.code == sf::Keyboard::Right) {
cam_dir.z += 0.1f;
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
}
if (event.key.code == sf::Keyboard::Down) {
cam_dir.y += 0.1f;
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
}
if (event.key.code == sf::Keyboard::Up) {
cam_dir.y -= 0.1f;
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
} }
//// CAMERA POSITION cam_vec.x = 0;
//if (event.key.code == sf::Keyboard::Q) { cam_vec.y = 0;
// cam_pos.z -= 1; cam_vec.z = 0;
// std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
//}
//if (event.key.code == sf::Keyboard::E) {
// cam_pos.z += 1;
// std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
//}
//if (event.key.code == sf::Keyboard::W) {
// cam_pos.y += 1;
// std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
//}
//if (event.key.code == sf::Keyboard::S) {
// cam_pos.y -= 1;
// std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
//}
//if (event.key.code == sf::Keyboard::A) {
// cam_pos.x += 1;
// std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
//}
//if (event.key.code == sf::Keyboard::D) {
// cam_pos.x -= 1;
// std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
//}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
cam_vec.z = 1; cam_vec.z = 1;
@ -227,67 +226,68 @@ int main2() {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
cam_vec.x = -1; cam_vec.x = -1;
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
cam_dir.z = -0.1f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
cam_vec.z = +0.1f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
cam_vec.y = +0.1f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
cam_vec.y = -0.1f;
}
deltas = fixed - sf::Mouse::getPosition(); deltas = fixed - sf::Mouse::getPosition();
if (deltas != sf::Vector2i(0, 0)) if (deltas != sf::Vector2i(0, 0) && mouse_enabled == true) {
sf::Mouse::setPosition(fixed);
// Mouse movement
sf::Mouse::setPosition(fixed);
cam_dir.y -= deltas.y / 300.0f; cam_dir.y -= deltas.y / 300.0f;
cam_dir.z -= deltas.x / 300.0f; cam_dir.z -= deltas.x / 300.0f;
}
cam_pos.x += cam_vec.x / 10.0; cam_pos.x += cam_vec.x / 1.0;
cam_pos.y += cam_vec.y / 10.0; cam_pos.y += cam_vec.y / 1.0;
cam_pos.z += cam_vec.z / 10.0; cam_pos.z += cam_vec.z / 1.0;
if (cam_vec.x > 0.0f) // if (cam_vec.x > 0.0f)
cam_vec.x -= 0.1; // cam_vec.x -= 0.1;
else if (cam_vec.x < 0.0f) // else if (cam_vec.x < 0.0f)
cam_vec.x += 0.1; // cam_vec.x += 0.1;
//
if (cam_vec.y > 0.0f) // if (cam_vec.y > 0.0f)
cam_vec.y -= 0.1; // cam_vec.y -= 0.1;
else if (cam_vec.y < 0.0f) // else if (cam_vec.y < 0.0f)
cam_vec.y += 0.1; // cam_vec.y += 0.1;
//
if (cam_vec.z > 0.0f) // if (cam_vec.z > 0.0f)
cam_vec.z -= 0.1; // cam_vec.z -= 0.1;
else if (cam_vec.z < 0.0f) // else if (cam_vec.z < 0.0f)
cam_vec.z += 0.1; // cam_vec.z += 0.1;
std::cout << cam_vec.x << " : " << cam_vec.y << " : " << cam_vec.z << std::endl; std::cout << cam_vec.x << " : " << cam_vec.y << " : " << cam_vec.z << std::endl;
// Get the elapsed time from the start of the application
elapsed_time = elap_time();
// Find the time that passed between now and the last frame // Time keeping
elapsed_time = elap_time();
delta_time = elapsed_time - current_time; delta_time = elapsed_time - current_time;
// Set the time for the next frame to use
current_time = elapsed_time; current_time = elapsed_time;
// If the time between the last frame and now was too large (lag)
// cull the time to a more acceptable value. So instead of jumping large
// amounts when lagging, the app only jumps in set increments
if (delta_time > 0.2f) if (delta_time > 0.2f)
delta_time = 0.2f; delta_time = 0.2f;
// Add the frame time to the accumulator, a running total of time we
// need to account for in the application
accumulator_time += delta_time; accumulator_time += delta_time;
// While we have time to step
while ((accumulator_time - step_size) >= step_size) { while ((accumulator_time - step_size) >= step_size) {
// Take away a step from the accumulator
accumulator_time -= step_size; accumulator_time -= step_size;
// Update cycle
curse.Update(delta_time); curse.Update(delta_time);
// And update the application for the amount of time alotted for one step
// Update(step_size);
}
}
// Fps cycle
// map->moveLight(sf::Vector2f(0.3, 0)); // map->moveLight(sf::Vector2f(0.3, 0));
window.clear(sf::Color::Black); window.clear(sf::Color::Black);

Loading…
Cancel
Save