From 8c193acd40289765bbd7c11aa0a1489c4144ac6d Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sat, 23 Sep 2017 13:05:50 -0700 Subject: [PATCH] quick save before the main.cpp refactor --- CMakeLists.txt | 2 +- include/Application.h | 60 +++++++++++++++ include/{Hardware_Caster.h => CLCaster.h} | 6 +- include/LightController.h | 6 +- src/Application.cpp | 16 ++++ src/{Hardware_Caster.cpp => CLCaster.cpp} | 90 +++++++++++------------ src/LightController.cpp | 2 +- src/main.cpp | 25 +------ 8 files changed, 133 insertions(+), 74 deletions(-) create mode 100644 include/Application.h rename include/{Hardware_Caster.h => CLCaster.h} (99%) create mode 100644 src/Application.cpp rename src/{Hardware_Caster.cpp => CLCaster.cpp} (92%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 65314ee..6b66b90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.1) set_property(GLOBAL PROPERTY USE_FOLDERS ON) # Set the project name -set(PNAME Game) +set(PNAME VoxelRaycaster) project(${PNAME}) # Set up variables, and find SFML diff --git a/include/Application.h b/include/Application.h new file mode 100644 index 0000000..45e686e --- /dev/null +++ b/include/Application.h @@ -0,0 +1,60 @@ +#pragma once +#ifdef linux +#include +#include + +#elif defined _WIN32 +// Good lord, windows.h overwrote the std::min() max() definitions +#define NOMINMAX +#include + +// As if hardware is ever going to move away from 1.2 +#define CL_USE_DEPRECATED_OPENCL_1_2_APIS +#include +#include + +#elif defined TARGET_OS_MAC +#include +#include +#include +#include +#include +#endif + +#include "util.hpp" +#include +#include +#include +#include +#include "map/Old_Map.h" +#include "CLCaster.h" +#include "Vector4.hpp" +#include "Camera.h" +#include "Input.h" +#include "Pub_Sub.h" +#include "LightController.h" +#include "LightHandle.h" +#include "imgui/imgui-SFML.h" +#include "imgui/imgui.h" +#include "map/Map.h" + +// Srsly people who macro error codes are the devil +#undef ERROR +#include "Logger.h" + +class Application { + +public: + const int WINDOW_X = 1536; + const int WINDOW_Y = 1024; + + const int MAP_X = 256; + const int MAP_Y = 256; + const int MAP_Z = 256; + + + +private: + + static float elap_time(); +}; \ No newline at end of file diff --git a/include/Hardware_Caster.h b/include/CLCaster.h similarity index 99% rename from include/Hardware_Caster.h rename to include/CLCaster.h index 25bd71a..bba4404 100644 --- a/include/Hardware_Caster.h +++ b/include/CLCaster.h @@ -84,7 +84,7 @@ struct device_info { struct PackedData; -class Hardware_Caster { +class CLCaster { public: @@ -148,8 +148,8 @@ public: * as rendering of the computed cast. * */ - Hardware_Caster(); - virtual ~Hardware_Caster(); + CLCaster(); + virtual ~CLCaster(); // Queries hardware, creates the command queue and context, and compiles kernel diff --git a/include/LightController.h b/include/LightController.h index 4d1c32f..ad745cd 100644 --- a/include/LightController.h +++ b/include/LightController.h @@ -4,7 +4,7 @@ #include #include "util.hpp" #include "Pub_Sub.h" -#include "Hardware_Caster.h" +#include "CLCaster.h" #include "LightHandle.h" @@ -48,12 +48,12 @@ struct PackedData { }; class LightHandle; -class Hardware_Caster; +class CLCaster; class LightController : public VrEventSubscriber { public: - LightController(std::shared_ptr raycaster); + LightController(std::shared_ptr raycaster); ~LightController(); // find a free light 'slot' and create diff --git a/src/Application.cpp b/src/Application.cpp new file mode 100644 index 0000000..feba900 --- /dev/null +++ b/src/Application.cpp @@ -0,0 +1,16 @@ +#include "Application.h" + +float Application::elap_time() { + static std::chrono::time_point start; + static bool started = false; + + if (!started) { + start = std::chrono::system_clock::now(); + started = true; + } + + std::chrono::time_point now = std::chrono::system_clock::now(); + std::chrono::duration elapsed_time = now - start; + return static_cast(elapsed_time.count()); +} + diff --git a/src/Hardware_Caster.cpp b/src/CLCaster.cpp similarity index 92% rename from src/Hardware_Caster.cpp rename to src/CLCaster.cpp index 3e6cf94..43f9685 100644 --- a/src/Hardware_Caster.cpp +++ b/src/CLCaster.cpp @@ -1,10 +1,10 @@ -#include "Hardware_Caster.h" +#include "CLCaster.h" -Hardware_Caster::Hardware_Caster() {} -Hardware_Caster::~Hardware_Caster() {} +CLCaster::CLCaster() {} +CLCaster::~CLCaster() {} -bool Hardware_Caster::init() { +bool CLCaster::init() { Logger::log("Initializing the Hardware Caster", Logger::LogLevel::INFO); @@ -68,7 +68,7 @@ bool Hardware_Caster::init() { } -bool Hardware_Caster::assign_map(std::shared_ptr map) { +bool CLCaster::assign_map(std::shared_ptr map) { this->map = map; auto dimensions = map->getDimensions(); @@ -82,7 +82,7 @@ bool Hardware_Caster::assign_map(std::shared_ptr map) { return true; } -bool Hardware_Caster::release_map() { +bool CLCaster::release_map() { this->map = nullptr; @@ -96,7 +96,7 @@ bool Hardware_Caster::release_map() { } -bool Hardware_Caster::assign_camera(std::shared_ptr camera) { +bool CLCaster::assign_camera(std::shared_ptr camera) { this->camera = camera; @@ -109,7 +109,7 @@ bool Hardware_Caster::assign_camera(std::shared_ptr camera) { return true; } -bool Hardware_Caster::release_camera() { +bool CLCaster::release_camera() { this->camera = nullptr; @@ -122,7 +122,7 @@ bool Hardware_Caster::release_camera() { return true; } -bool Hardware_Caster::validate() { +bool CLCaster::validate() { Logger::log("Validating OpenCL kernel args", Logger::LogLevel::INFO); @@ -163,7 +163,7 @@ bool Hardware_Caster::validate() { } -bool Hardware_Caster::create_texture_atlas(sf::Texture *t, sf::Vector2i tile_dim) { +bool CLCaster::create_texture_atlas(sf::Texture *t, sf::Vector2i tile_dim) { if (!create_image_buffer("texture_atlas", t->getSize().x * t->getSize().x * 4 * sizeof(float), t, CL_MEM_READ_ONLY)) return false; @@ -180,7 +180,7 @@ bool Hardware_Caster::create_texture_atlas(sf::Texture *t, sf::Vector2i tile_dim return true; } -bool Hardware_Caster::compute() { +bool CLCaster::compute() { // correlating work size with texture size? good, bad? return run_kernel("raycaster", viewport_texture.getSize().x, viewport_texture.getSize().y); @@ -189,7 +189,7 @@ bool Hardware_Caster::compute() { // There is a possibility that I would want to move this over to be all inside it's own // container to make it so it can be changed via CL_MEM_USE_HOST_PTR. But I doubt it // would ever be called enough to warrent that -bool Hardware_Caster::create_viewport(int width, int height, float v_fov, float h_fov) { +bool CLCaster::create_viewport(int width, int height, float v_fov, float h_fov) { // CL needs the screen resolution sf::Vector2i view_res(width, height); @@ -272,7 +272,7 @@ bool Hardware_Caster::create_viewport(int width, int height, float v_fov, float } -bool Hardware_Caster::assign_lights(std::vector *data) { +bool CLCaster::assign_lights(std::vector *data) { // Get a pointer to the packed light data this->lights = data; @@ -290,11 +290,11 @@ bool Hardware_Caster::assign_lights(std::vector *data) { return true; } -void Hardware_Caster::draw(sf::RenderWindow* window) { +void CLCaster::draw(sf::RenderWindow* window) { window->draw(viewport_sprite); } -bool Hardware_Caster::debug_quick_recompile() { +bool CLCaster::debug_quick_recompile() { if (!compile_kernel("../kernels/ray_caster_kernel.cl", true, "raycaster")) { Logger::log("Failed to recompile kernel", Logger::LogLevel::WARN, __LINE__, __FILE__); @@ -306,7 +306,7 @@ bool Hardware_Caster::debug_quick_recompile() { } -bool Hardware_Caster::aquire_hardware() { +bool CLCaster::aquire_hardware() { Logger::log("Acquiring OpenCL Hardware", Logger::LogLevel::INFO); @@ -371,7 +371,7 @@ bool Hardware_Caster::aquire_hardware() { return true; } -void Hardware_Caster::save_config() { +void CLCaster::save_config() { Logger::log("Saving OpenCL hardware config", Logger::LogLevel::INFO); @@ -384,7 +384,7 @@ void Hardware_Caster::save_config() { output_file.close(); } -bool Hardware_Caster::load_config() { +bool CLCaster::load_config() { Logger::log("Loading hardware config", Logger::LogLevel::INFO); @@ -420,7 +420,7 @@ bool Hardware_Caster::load_config() { return true; } -bool Hardware_Caster::query_hardware() +bool CLCaster::query_hardware() { Logger::log("Querying OpenCL hardware", Logger::LogLevel::INFO); @@ -531,7 +531,7 @@ bool Hardware_Caster::query_hardware() return true; } -bool Hardware_Caster::create_shared_context() +bool CLCaster::create_shared_context() { // Hurray for standards! @@ -589,7 +589,7 @@ bool Hardware_Caster::create_shared_context() return true; } -bool Hardware_Caster::create_command_queue() { +bool CLCaster::create_command_queue() { // If context and device_id have initialized if (context && device_id) { @@ -610,7 +610,7 @@ bool Hardware_Caster::create_command_queue() { return true; } -bool Hardware_Caster::compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name) { +bool CLCaster::compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name) { Logger::log("Compiling OpenCL Kernel", Logger::LogLevel::INFO); @@ -677,7 +677,7 @@ bool Hardware_Caster::compile_kernel(std::string kernel_source, bool is_path, st return true; } -bool Hardware_Caster::set_kernel_arg( +bool CLCaster::set_kernel_arg( std::string kernel_name, int index, std::string buffer_name) { @@ -698,7 +698,7 @@ bool Hardware_Caster::set_kernel_arg( } -bool Hardware_Caster::create_image_buffer(std::string buffer_name, cl_uint size, sf::Texture* texture, cl_int access_type) { +bool CLCaster::create_image_buffer(std::string buffer_name, cl_uint size, sf::Texture* texture, cl_int access_type) { // I can imagine overwriting buffers will be common, so I think // this is safe to overwrite / release old buffers quietly @@ -723,7 +723,7 @@ bool Hardware_Caster::create_image_buffer(std::string buffer_name, cl_uint size, return true; } -bool Hardware_Caster::create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags) { +bool CLCaster::create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags) { // I can imagine overwriting buffers will be common, so I think // this is safe to overwrite / release old buffers quietly @@ -751,7 +751,7 @@ bool Hardware_Caster::create_buffer(std::string buffer_name, cl_uint size, void* } -bool Hardware_Caster::create_buffer(std::string buffer_name, cl_uint size, void* data) { +bool CLCaster::create_buffer(std::string buffer_name, cl_uint size, void* data) { // I can imagine overwriting buffers will be common, so I think // this is safe to overwrite / release old buffers quietly @@ -779,7 +779,7 @@ bool Hardware_Caster::create_buffer(std::string buffer_name, cl_uint size, void* } -bool Hardware_Caster::release_buffer(std::string buffer_name) { +bool CLCaster::release_buffer(std::string buffer_name) { if (buffer_map.count(buffer_name) > 0) { @@ -802,7 +802,7 @@ bool Hardware_Caster::release_buffer(std::string buffer_name) { } -bool Hardware_Caster::store_buffer(cl_mem buffer, std::string buffer_name) { +bool CLCaster::store_buffer(cl_mem buffer, std::string buffer_name) { if (buffer_map.count(buffer_name) == 0) { buffer_map.emplace(std::make_pair(buffer_name, buffer)); @@ -814,7 +814,7 @@ bool Hardware_Caster::store_buffer(cl_mem buffer, std::string buffer_name) { } -bool Hardware_Caster::run_kernel(std::string kernel_name, const int work_dim_x, const int work_dim_y) { +bool CLCaster::run_kernel(std::string kernel_name, const int work_dim_x, const int work_dim_y) { size_t global_work_size[2] = { static_cast(work_dim_x), static_cast(work_dim_y)}; @@ -857,7 +857,7 @@ bool Hardware_Caster::run_kernel(std::string kernel_name, const int work_dim_x, return true; } -void Hardware_Caster::print_kernel_arguments() +void CLCaster::print_kernel_arguments() { compile_kernel("../kernels/print_arguments.cl", true, "printer"); set_kernel_arg("printer", 0, "map"); @@ -873,13 +873,13 @@ void Hardware_Caster::print_kernel_arguments() run_kernel("printer", 1, 1); } -cl_device_id Hardware_Caster::getDeviceID() { return device_id; }; -cl_platform_id Hardware_Caster::getPlatformID() { return platform_id; }; -cl_context Hardware_Caster::getContext() { return context; }; -cl_kernel Hardware_Caster::getKernel(std::string kernel_name) { return kernel_map.at(kernel_name); }; -cl_command_queue Hardware_Caster::getCommandQueue() { return command_queue; }; +cl_device_id CLCaster::getDeviceID() { return device_id; }; +cl_platform_id CLCaster::getPlatformID() { return platform_id; }; +cl_context CLCaster::getContext() { return context; }; +cl_kernel CLCaster::getKernel(std::string kernel_name) { return kernel_map.at(kernel_name); }; +cl_command_queue CLCaster::getCommandQueue() { return command_queue; }; -bool Hardware_Caster::cl_assert(int error_code) { +bool CLCaster::cl_assert(int error_code) { if (error_code == CL_SUCCESS || error_code == 1) return false; @@ -888,7 +888,7 @@ bool Hardware_Caster::cl_assert(int error_code) { } -std::string Hardware_Caster::cl_err_lookup(int error_code) { +std::string CLCaster::cl_err_lookup(int error_code) { std::string err_msg = ""; @@ -1080,16 +1080,16 @@ std::string Hardware_Caster::cl_err_lookup(int error_code) { case CL_PLATFORM_NOT_FOUND_KHR: err_msg = "CL_PLATFORM_NOT_FOUND_KHR"; break; - case Hardware_Caster::SHARING_NOT_SUPPORTED: + case CLCaster::SHARING_NOT_SUPPORTED: err_msg = "SHARING_NOT_SUPPORTED"; break; - case Hardware_Caster::OPENCL_NOT_SUPPORTED: + case CLCaster::OPENCL_NOT_SUPPORTED: err_msg = "OPENCL_NOT_SUPPORTED"; break; - case Hardware_Caster::OPENCL_ERROR: + case CLCaster::OPENCL_ERROR: err_msg = "OPENCL_ERROR"; break; - case Hardware_Caster::ERR: + case CLCaster::ERR: err_msg = "ERROR"; break; default: @@ -1100,7 +1100,7 @@ std::string Hardware_Caster::cl_err_lookup(int error_code) { } -Hardware_Caster::device::device(cl_device_id device_id, cl_platform_id platform_id) { +CLCaster::device::device(cl_device_id device_id, cl_platform_id platform_id) { this->device_id = device_id; this->platform_id = platform_id; @@ -1135,7 +1135,7 @@ Hardware_Caster::device::device(cl_device_id device_id, cl_platform_id platform_ } -Hardware_Caster::device::device(const device& d) { +CLCaster::device::device(const device& d) { // member values, copy individually device_id = d.device_id; @@ -1148,7 +1148,7 @@ Hardware_Caster::device::device(const device& d) { } -void Hardware_Caster::device::print(std::ostream& stream) const { +void CLCaster::device::print(std::ostream& stream) const { stream << "\n\tDevice ID : " << device_id << std::endl; stream << "\tDevice Name : " << data.device_name << std::endl; @@ -1180,6 +1180,6 @@ void Hardware_Caster::device::print(std::ostream& stream) const { } -void Hardware_Caster::device::print_packed_data(std::ostream& stream) { +void CLCaster::device::print_packed_data(std::ostream& stream) { stream.write(reinterpret_cast(&data), sizeof(data)); } diff --git a/src/LightController.cpp b/src/LightController.cpp index a936888..b6651f4 100644 --- a/src/LightController.cpp +++ b/src/LightController.cpp @@ -1,6 +1,6 @@ #include "LightController.h" -LightController::LightController(std::shared_ptr raycaster) : packed_data_array(reserved_count), open_list(reserved_count) { +LightController::LightController(std::shared_ptr raycaster) : packed_data_array(reserved_count), open_list(reserved_count) { // initialize the open list with numbers 1 -> open_list.size() std::iota(open_list.begin(), open_list.end(), 0); diff --git a/src/main.cpp b/src/main.cpp index f047b87..296f536 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,9 @@ - -// This has to be up here or else glew will complain -//#include "GL_Testing.h" - -#ifdef linux +#ifdef linux #include #include #elif defined _WIN32 -// Good lord, the C++ std overwrote windows.h min() max() definitions +// Good lord, windows.h overwrote the std::min() max() definitions #define NOMINMAX #include @@ -30,7 +26,7 @@ #include #include #include "map/Old_Map.h" -#include "Hardware_Caster.h" +#include "CLCaster.h" #include "Vector4.hpp" #include "Camera.h" #include "Input.h" @@ -45,28 +41,15 @@ #undef ERROR #include "Logger.h" - - const int WINDOW_X = 1536; const int WINDOW_Y = 1024; -const int WORK_SIZE = WINDOW_X * WINDOW_Y; const int MAP_X = 256; const int MAP_Y = 256; const int MAP_Z = 256; float elap_time(){ - static std::chrono::time_point start; - static bool started = false; - - if (!started){ - start = std::chrono::system_clock::now(); - started = true; - } - std::chrono::time_point now = std::chrono::system_clock::now(); - std::chrono::duration elapsed_time = now - start; - return static_cast(elapsed_time.count()); } sf::Sprite window_sprite; @@ -98,7 +81,7 @@ int main() { window.resetGLStates(); // Start up the raycaster - std::shared_ptr raycaster(new Hardware_Caster()); + std::shared_ptr raycaster(new CLCaster()); if (!raycaster->init()) abort();