From 45627e6a85425ef94784ca2a1aefd49837dcd231 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sat, 29 Oct 2016 23:56:50 -0700 Subject: [PATCH] Whoops, forgot to save the files, same commit as before --- include/CL_Wrapper.h | 5 ++++ include/RayCaster.h | 25 +++++++++++----- include/Renderer.cpp | 19 ++++++++++++ include/Renderer.h | 24 ++++++++++++--- include/util.hpp | 7 +++++ src/CL_Wrapper.cpp | 71 ++++++++++++++++++++++++++++++-------------- src/RayCaster.cpp | 10 +++---- 7 files changed, 122 insertions(+), 39 deletions(-) diff --git a/include/CL_Wrapper.h b/include/CL_Wrapper.h index d3b2ac3..08b8f5e 100644 --- a/include/CL_Wrapper.h +++ b/include/CL_Wrapper.h @@ -43,6 +43,8 @@ public: int create_command_queue(); + int check_cl_khr_gl_sharing(); + 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); @@ -60,11 +62,14 @@ public: cl_kernel getKernel(std::string kernel_name); cl_command_queue getCommandQueue(); + bool was_init_valid(); + private: int error = 0; bool initialized = false; bool cl_khr_gl_sharing_fallback = false; + bool cl_supported = false; cl_platform_id platform_id; cl_device_id device_id; diff --git a/include/RayCaster.h b/include/RayCaster.h index 2612d57..eb9dc78 100644 --- a/include/RayCaster.h +++ b/include/RayCaster.h @@ -2,23 +2,32 @@ #include #include #include +#include "Old_map.h" + + +// What about a parent, child relationship between the raycaster and it's two +// different modes? Raycaster -> ClCaster, SoftwareCaster +class Camera; class RayCaster { public: - RayCaster(Map *map, - sf::Vector3 map_dimensions, - sf::Vector2 viewport_resolution); + + RayCaster(); ~RayCaster(); - void setFOV(float fov); - void setResolution(sf::Vector2 resolution); + virtual void assign_map(Old_Map *map) = 0; + virtual void assign_camera(Camera *camera) = 0; + virtual void assign_viewport(int width, int height, float v_fov, float h_fov) = 0; + virtual void assign_light(Light light) = 0; + + // draw will abstract the gl sharing and software rendering + // methods of retrieving the screen buffer + virtual void draw(sf::RenderWindow* window) = 0; - sf::Color* CastRays(sf::Vector3 camera_direction, sf::Vector3 camera_position); - void moveCamera(sf::Vector2f v); private: sf::Vector3 map_dimensions; - Map *map; + Old_Map *map; // The XY resolution of the viewport sf::Vector2 resolution; diff --git a/include/Renderer.cpp b/include/Renderer.cpp index a194f82..0545cc4 100644 --- a/include/Renderer.cpp +++ b/include/Renderer.cpp @@ -1 +1,20 @@ #include "Renderer.h" + +Renderer::Renderer() { + + cl = new CL_Wrapper(); + if (!cl->was_init_valid()) { + delete cl; + rc = new RayCaster(); + } +} + +void Renderer::register_camera(Camera *camera) +{ + +} + +void Renderer::draw() +{ + +} diff --git a/include/Renderer.h b/include/Renderer.h index cda2007..b7fb543 100644 --- a/include/Renderer.h +++ b/include/Renderer.h @@ -4,6 +4,8 @@ #include "SFML/Graphics.hpp" #include "CL_Wrapper.h" #include "Camera.h" +#include "Old_map.h" +#include "RayCaster.h" // Renderer needs to handle the distinction between a few difference circumstances. // A.) The machine supports OpenCL and cl_khr_gl_sharing @@ -18,6 +20,9 @@ // intent of leaving it specialized to only the raycaster. Any further OpenCL // work can use its own class +// Perhaps in the future there will be a container "scene" which will +// hold the current map, camera, and light objects. The renderer will +// then be passed that scene which it will then use to render with class Renderer { @@ -26,9 +31,11 @@ public: // The renderer needs all of the things that are required // by CL in order to render the screen - void register_camera(Camera camera); - - + void register_camera(Camera* camera); + void register_map(Old_Map* map); + void register_lights(); + void create_viewport(float v_fov, float h_fov, int height, int width); + void register_light(light l); void draw(); sf::RenderWindow* get_window(); @@ -36,10 +43,19 @@ public: private: CL_Wrapper *cl; + RayCaster *rc; + bool sharing_supported = false; - sf::Uint8 *drawing_surface; + bool cl_supported = false; + + sf::Uint8 *drawing_surface; sf::RenderWindow* window; + std::vector lights; + Old_Map* map; + Camera* camera; + sf::Uint8 *view_matrix; + }; diff --git a/include/util.hpp b/include/util.hpp index 10ff55d..6ce8208 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -4,10 +4,17 @@ #include #include #include +#include "Vector4.hpp" const double PI = 3.141592653589793238463; const float PI_F = 3.14159265358979f; +struct Light { + sf::Vector4f rgbi; + sf::Vector3f position; + sf::Vector3f direction_cartesian; +}; + struct fps_counter { public: fps_counter(){ diff --git a/src/CL_Wrapper.cpp b/src/CL_Wrapper.cpp index 356aac4..af2cf84 100644 --- a/src/CL_Wrapper.cpp +++ b/src/CL_Wrapper.cpp @@ -2,16 +2,29 @@ CL_Wrapper::CL_Wrapper() { - acquire_platform_and_device(); + // Check to see if acquiring the platform failed out + // This also catches when there are general errors, + // falling back to the known good software renderer + if (acquire_platform_and_device() == -1) { + std::cout << "Falling back to the software renderer" << std::endl; + return; + } + + cl_supported = true; - if (cl_khr_gl_sharing_fallback){ - create_cl_context(); - } else { - create_shared_context(); - } + // update cl_khr_gl_sharing_fallback's value + check_cl_khr_gl_sharing(); - create_command_queue(); + // Whether or not we can share GL contexts with our CL kernels + // Enables on-GPU editing and rendering of the screen buffer + if (cl_khr_gl_sharing_fallback){ + create_cl_context(); + } else { + create_shared_context(); + } + create_command_queue(); + } @@ -43,6 +56,13 @@ int CL_Wrapper::acquire_platform_and_device(){ cl_uint deviceIdCount = 0; error = clGetDeviceIDs(plt_buf[i], CL_DEVICE_TYPE_ALL, 0, nullptr, &deviceIdCount); + // Check to see if we even have opencl on this machine + if (deviceIdCount == 0) { + cl_supported = false; + std::cout << "OpenCL does not appear to be supported on this machine" << std::endl; + return -1; + } + // Get the device ids std::vector deviceIds(deviceIdCount); error = clGetDeviceIDs(plt_buf[i], CL_DEVICE_TYPE_ALL, deviceIdCount, deviceIds.data(), NULL); @@ -100,18 +120,6 @@ 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; }; @@ -134,7 +142,7 @@ int CL_Wrapper::create_cl_context() { if (assert(error, "clCreateContext")) return -1; - return 0; + return 1; } @@ -188,7 +196,7 @@ int CL_Wrapper::create_shared_context() { if (assert(error, "clCreateContext")) return -1; - return 0; + return 1; } int CL_Wrapper::create_command_queue(){ @@ -200,7 +208,7 @@ int CL_Wrapper::create_command_queue(){ if (assert(error, "clCreateCommandQueue")) return -1; - return 0; + return 1; } else { std::cout << "Failed creating the command queue, context or device_id not initialized"; @@ -208,6 +216,21 @@ int CL_Wrapper::create_command_queue(){ } } +int CL_Wrapper::check_cl_khr_gl_sharing() { + + // 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; +} + int CL_Wrapper::compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name) { const char* source; @@ -354,6 +377,10 @@ cl_context CL_Wrapper::getContext(){ return context; }; cl_kernel CL_Wrapper::getKernel(std::string kernel_name ){ return kernel_map.at(kernel_name); }; cl_command_queue CL_Wrapper::getCommandQueue(){ return command_queue; }; +bool CL_Wrapper::was_init_valid() { + return cl_supported; +} + bool CL_Wrapper::assert(int error_code, std::string function_name){ // Just gonna do a little jump table here, just error codes so who cares diff --git a/src/RayCaster.cpp b/src/RayCaster.cpp index 56e6c1e..8d48b62 100644 --- a/src/RayCaster.cpp +++ b/src/RayCaster.cpp @@ -2,12 +2,12 @@ #include #include +RayCaster::RayCaster() { +} -RayCaster::RayCaster( - Map *map, - sf::Vector3 map_dimensions, - sf::Vector2 viewport_resolution ) { - +void RayCaster::assign_map(Old_Map* map) { + this->map = map; +} // Override values //this.map_dimensions = new Vector3 (50, 50, 50); //this.resolution = new Vector2 (200, 200);