More error checking, probably way more than I would ever need but oh well, it looks cool

master
MitchellHansen 7 years ago
parent 86f1622090
commit e0cb26a9d1

@ -164,21 +164,21 @@ public:
bool assign_lights(std::vector<PackedData> *data) ; bool assign_lights(std::vector<PackedData> *data) ;
// We take a ptr to the map and create the map, and map_dimensions buffer for the GPU // We take a ptr to the map and create the map, and map_dimensions buffer for the GPU
void assign_map(Old_Map *map) ; bool assign_map(Old_Map *map) ;
// We take a ptr to the camera and create a camera direction and position buffer // We take a ptr to the camera and create a camera direction and position buffer
void assign_camera(Camera *camera) ; bool assign_camera(Camera *camera) ;
// TODO: Hoist this to the base class // TODO: Hoist this to the base class
// Creates 3 buffers relating to the texture atlas: texture_atlas, atlas_dim, and tile_dim // Creates 3 buffers relating to the texture atlas: texture_atlas, atlas_dim, and tile_dim
// With these on the GPU we can texture any quad with an atlas tile // With these on the GPU we can texture any quad with an atlas tile
void create_texture_atlas(sf::Texture *t, sf::Vector2i tile_dim); bool create_texture_atlas(sf::Texture *t, sf::Vector2i tile_dim);
// Check to make sure that the buffers have been initiated and set them as kernel args // Check to make sure that the buffers have been initiated and set them as kernel args
void validate() ; bool validate() ;
// Aquires the GL objects, runs the kernel, releases back the GL objects // Aquires the GL objects, runs the kernel, releases back the GL objects
void compute() ; bool compute() ;
// Take the viewport sprite and draw it to the screen // Take the viewport sprite and draw it to the screen
void draw(sf::RenderWindow* window) ; void draw(sf::RenderWindow* window) ;
@ -202,14 +202,14 @@ private:
bool aquire_hardware(); bool aquire_hardware();
int query_hardware(); bool query_hardware();
// With respect to the individual platforms implementation of sharing // With respect to the individual platforms implementation of sharing
// create a shared cl_gl context // create a shared cl_gl context
int create_shared_context(); int create_shared_context();
// Using the context and the device create a command queue for them // Using the context and the device create a command queue for them
int create_command_queue(); bool create_command_queue();
// Buffer operations // Buffer operations
// All of these functions create and store a buffer in a map with the key representing their name // All of these functions create and store a buffer in a map with the key representing their name

@ -6,6 +6,8 @@ Hardware_Caster::~Hardware_Caster() {}
int Hardware_Caster::init() { int Hardware_Caster::init() {
Logger::log("Initializing the Hardware Caster", Logger::LogLevel::INFO);
if (!aquire_hardware()) { if (!aquire_hardware()) {
Logger::log("Failed to acquire OpenCL hardware", Logger::LogLevel::ERROR, __LINE__, __FILE__); Logger::log("Failed to acquire OpenCL hardware", Logger::LogLevel::ERROR, __LINE__, __FILE__);
return false; return false;
@ -66,26 +68,37 @@ int Hardware_Caster::init() {
} }
void Hardware_Caster::assign_map(Old_Map *map) { bool Hardware_Caster::assign_map(Old_Map *map) {
this->map = map; this->map = map;
auto dimensions = map->getDimensions(); auto dimensions = map->getDimensions();
create_buffer("map", sizeof(char) * dimensions.x * dimensions.y * dimensions.z, map->get_voxel_data()); if (!create_buffer("map", sizeof(char) * dimensions.x * dimensions.y * dimensions.z, map->get_voxel_data()))
create_buffer("map_dimensions", sizeof(int) * 3, &dimensions); return false;
if (!create_buffer("map_dimensions", sizeof(int) * 3, &dimensions))
return false;
return true;
} }
void Hardware_Caster::assign_camera(Camera *camera) { bool Hardware_Caster::assign_camera(Camera *camera) {
this->camera = camera; this->camera = camera;
create_buffer("camera_direction", sizeof(float) * 4, (void*)camera->get_direction_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); if (!create_buffer("camera_direction", sizeof(float) * 4, (void*)camera->get_direction_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR))
create_buffer("camera_position", sizeof(float) * 4, (void*)camera->get_position_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); return false;
if (!create_buffer("camera_position", sizeof(float) * 4, (void*)camera->get_position_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR))
return false;
return true;
} }
void Hardware_Caster::validate() bool Hardware_Caster::validate() {
{
Logger::log("Validating OpenCL kernel args", Logger::LogLevel::INFO);
// Check to make sure everything has been entered; // Check to make sure everything has been entered;
if (camera == nullptr || if (camera == nullptr ||
map == nullptr || map == nullptr ||
@ -93,7 +106,8 @@ void Hardware_Caster::validate()
viewport_matrix == nullptr) { viewport_matrix == nullptr) {
Logger::log("Raycaster.validate() failed, camera, map, or viewport not initialized", Logger::LogLevel::WARN); Logger::log("Raycaster.validate() failed, camera, map, or viewport not initialized", Logger::LogLevel::WARN);
return false;
} else { } else {
// Set all the kernel args // Set all the kernel args
@ -111,26 +125,34 @@ void Hardware_Caster::validate()
set_kernel_arg("raycaster", 11, "atlas_dim"); set_kernel_arg("raycaster", 11, "atlas_dim");
set_kernel_arg("raycaster", 12, "tile_dim"); set_kernel_arg("raycaster", 12, "tile_dim");
return true;
//print_kernel_arguments(); //print_kernel_arguments();
} }
} }
void Hardware_Caster::create_texture_atlas(sf::Texture *t, sf::Vector2i tile_dim) { bool Hardware_Caster::create_texture_atlas(sf::Texture *t, sf::Vector2i tile_dim) {
create_image_buffer("texture_atlas", t->getSize().x * t->getSize().x * 4 * sizeof(float), t, CL_MEM_READ_ONLY); if (!create_image_buffer("texture_atlas", t->getSize().x * t->getSize().x * 4 * sizeof(float), t, CL_MEM_READ_ONLY))
return false;
// create_buffer observes arg 3's // create_buffer observes arg 3's
sf::Vector2u v = t->getSize(); sf::Vector2u v = t->getSize();
create_buffer("atlas_dim", sizeof(sf::Vector2u) , &v);
if (!create_buffer("atlas_dim", sizeof(sf::Vector2u) , &v))
return false;
if (!create_buffer("tile_dim", sizeof(sf::Vector2i), &tile_dim))
return false;
create_buffer("tile_dim", sizeof(sf::Vector2i), &tile_dim); return true;
} }
void Hardware_Caster::compute() { bool Hardware_Caster::compute() {
// correlating work size with texture size? good, bad? // correlating work size with texture size? good, bad?
run_kernel("raycaster", viewport_texture.getSize().x, viewport_texture.getSize().y); return run_kernel("raycaster", viewport_texture.getSize().x, viewport_texture.getSize().y);
} }
// There is a possibility that I would want to move this over to be all inside it's own // There is a possibility that I would want to move this over to be all inside it's own
@ -140,7 +162,8 @@ bool Hardware_Caster::create_viewport(int width, int height, float v_fov, float
// CL needs the screen resolution // CL needs the screen resolution
sf::Vector2i view_res(width, height); sf::Vector2i view_res(width, height);
create_buffer("viewport_resolution", sizeof(int) * 2, &view_res); if (!create_buffer("viewport_resolution", sizeof(int) * 2, &view_res))
return false;
// And an array of vectors describing the way the "lens" of our // And an array of vectors describing the way the "lens" of our
// camera works // camera works
@ -233,74 +256,28 @@ bool Hardware_Caster::assign_lights(std::vector<PackedData> *data) {
if (!create_buffer("light_count", 8, &light_count)) if (!create_buffer("light_count", 8, &light_count))
return false; return false;
return true;
} }
void Hardware_Caster::draw(sf::RenderWindow* window) { void Hardware_Caster::draw(sf::RenderWindow* window) {
window->draw(viewport_sprite); window->draw(viewport_sprite);
} }
bool Hardware_Caster::debug_quick_recompile() bool Hardware_Caster::debug_quick_recompile() {
{
int error = compile_kernel("../kernels/ray_caster_kernel.cl", true, "raycaster"); if (!compile_kernel("../kernels/ray_caster_kernel.cl", true, "raycaster")) {
if (cl_assert(error)) { Logger::log("Failed to recompile kernel", Logger::LogLevel::WARN, __LINE__, __FILE__);
std::cin.get(); // hang the output window so we can read the error std::cin.get(); // hang the output window so we can read the error
return error; return false;
} }
validate();
return validate();
return true;
}
void Hardware_Caster::test_edit_viewport(int width, int height, float v_fov, float h_fov)
{
sf::Vector2i view_res(width, height);
double y_increment_radians = DegreesToRadians(v_fov / view_res.y);
double x_increment_radians = DegreesToRadians(h_fov / view_res.x);
for (int y = -view_res.y / 2; y < view_res.y / 2; y++) {
for (int x = -view_res.x / 2; x < view_res.x / 2; x++) {
// The base ray direction to slew from
sf::Vector3f ray(1, 0, 0);
// Y axis, pitch
ray = sf::Vector3f(
static_cast<float>(ray.z * sin(y_increment_radians * y) + ray.x * cos(y_increment_radians * y)),
static_cast<float>(ray.y),
static_cast<float>(ray.z * cos(y_increment_radians * y) - ray.x * sin(y_increment_radians * y))
);
// Z axis, yaw
ray = sf::Vector3f(
static_cast<float>(ray.x * cos(x_increment_radians * x) - ray.y * sin(x_increment_radians * x)),
static_cast<float>(ray.x * sin(x_increment_radians * x) + ray.y * cos(x_increment_radians * x)),
static_cast<float>(ray.z)
);
// correct for the base ray pointing to (1, 0, 0) as (0, 0). Should equal (1.57, 0)
ray = sf::Vector3f(
static_cast<float>(ray.z * sin(-1.57) + ray.x * cos(-1.57)),
static_cast<float>(ray.y),
static_cast<float>(ray.z * cos(-1.57) - ray.x * sin(-1.57))
);
int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2);
ray = Normalize(ray);
viewport_matrix[index] = sf::Vector4f(
ray.x,
ray.y,
ray.z,
0
);
}
}
} }
bool Hardware_Caster::aquire_hardware() {
bool Hardware_Caster::aquire_hardware() Logger::log("Acquiring OpenCL Hardware", Logger::LogLevel::INFO);
{
// Get the number of platforms // Get the number of platforms
cl_uint platform_count = 0; cl_uint platform_count = 0;
@ -365,6 +342,8 @@ bool Hardware_Caster::aquire_hardware()
void Hardware_Caster::save_config() { void Hardware_Caster::save_config() {
Logger::log("Saving OpenCL hardware config", Logger::LogLevel::INFO);
std::ofstream output_file; std::ofstream output_file;
output_file.open("device_config.bin", std::ofstream::binary | std::ofstream::out | std::ofstream::trunc); output_file.open("device_config.bin", std::ofstream::binary | std::ofstream::out | std::ofstream::trunc);
@ -410,18 +389,31 @@ bool Hardware_Caster::load_config() {
return true; return true;
} }
int Hardware_Caster::query_hardware() { bool Hardware_Caster::query_hardware()
{
Logger::log("Querying OpenCL hardware", Logger::LogLevel::INFO);
// Get the number of platforms // Get the number of platforms
cl_uint plt_cnt = 0; cl_uint plt_cnt = 0;
clGetPlatformIDs(0, nullptr, &plt_cnt); error = clGetPlatformIDs(0, nullptr, &plt_cnt);
if (cl_assert(error)) {
Logger::log("Failed at clGetPlatformIDs() :" + cl_err_lookup(error), Logger::LogLevel::ERROR, __LINE__, __FILE__);
return false;
}
// Fetch the platforms // Fetch the platforms
std::map<cl_platform_id, std::vector<device_info>> plt_ids; std::map<cl_platform_id, std::vector<device_info>> plt_ids;
// buffer before map init // buffer before map init
std::vector<cl_platform_id> plt_buf(plt_cnt); std::vector<cl_platform_id> plt_buf(plt_cnt);
clGetPlatformIDs(plt_cnt, plt_buf.data(), nullptr); error = clGetPlatformIDs(plt_cnt, plt_buf.data(), nullptr);
if (cl_assert(error)) {
Logger::log("Failed at clGetPlatformIDs() :" + cl_err_lookup(error), Logger::LogLevel::ERROR, __LINE__, __FILE__);
return false;
}
// Map init // Map init
for (auto id : plt_buf) { for (auto id : plt_buf) {
@ -437,7 +429,7 @@ int Hardware_Caster::query_hardware() {
// Check to see if we even have OpenCL on this machine // Check to see if we even have OpenCL on this machine
if (deviceIdCount == 0) { if (deviceIdCount == 0) {
Logger::log("No devices supporting OpenCL found", Logger::LogLevel::ERROR, __LINE__, __FILE__); Logger::log("No devices supporting OpenCL found", Logger::LogLevel::ERROR, __LINE__, __FILE__);
return OPENCL_NOT_SUPPORTED; return false;
} }
// Get the device ids // Get the device ids
@ -505,7 +497,7 @@ int Hardware_Caster::query_hardware() {
} }
} }
return 1; return true;
} }
int Hardware_Caster::create_shared_context() { int Hardware_Caster::create_shared_context() {
@ -559,13 +551,13 @@ int Hardware_Caster::create_shared_context() {
if (cl_assert(error)) { if (cl_assert(error)) {
Logger::log("Failed at clCreateContext() :" + cl_err_lookup(error), Logger::LogLevel::ERROR, __LINE__, __FILE__); Logger::log("Failed at clCreateContext() :" + cl_err_lookup(error), Logger::LogLevel::ERROR, __LINE__, __FILE__);
return OPENCL_ERROR; return false;
} }
return 1; return true;
} }
int Hardware_Caster::create_command_queue() { bool Hardware_Caster::create_command_queue() {
// If context and device_id have initialized // If context and device_id have initialized
if (context && device_id) { if (context && device_id) {
@ -574,21 +566,22 @@ int Hardware_Caster::create_command_queue() {
if (cl_assert(error)) { if (cl_assert(error)) {
Logger::log("Failed at clCreateCommandQueue() :" + cl_err_lookup(error), Logger::LogLevel::ERROR, __LINE__, __FILE__); Logger::log("Failed at clCreateCommandQueue() :" + cl_err_lookup(error), Logger::LogLevel::ERROR, __LINE__, __FILE__);
return OPENCL_ERROR; return false;
} }
return 1; } else {
}
else {
Logger::log("Failed creating the command queue. Context or device_id not initialized", Logger::LogLevel::ERROR, __LINE__, __FILE__); Logger::log("Failed creating the command queue. Context or device_id not initialized", Logger::LogLevel::ERROR, __LINE__, __FILE__);
return OPENCL_ERROR; return false;
} }
}
return true;
}
bool Hardware_Caster::compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name) { bool Hardware_Caster::compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name) {
Logger::log("Compiling OpenCL Kernel", Logger::LogLevel::INFO);
const char* source; const char* source;
std::string tmp; std::string tmp;
@ -604,7 +597,6 @@ bool Hardware_Caster::compile_kernel(std::string kernel_source, bool is_path, st
size_t kernel_source_size = strlen(source); size_t kernel_source_size = strlen(source);
// Load the source into CL's data structure // Load the source into CL's data structure
cl_program program = clCreateProgramWithSource( cl_program program = clCreateProgramWithSource(
context, 1, context, 1,
&source, &source,
@ -684,7 +676,6 @@ bool Hardware_Caster::create_image_buffer(std::string buffer_name, cl_uint size,
return false; return false;
} }
int error;
cl_mem buff = clCreateFromGLTexture( cl_mem buff = clCreateFromGLTexture(
getContext(), access_type, GL_TEXTURE_2D, getContext(), access_type, GL_TEXTURE_2D,
0, texture->getNativeHandle(), &error); 0, texture->getNativeHandle(), &error);
@ -718,7 +709,7 @@ bool Hardware_Caster::create_buffer(std::string buffer_name, cl_uint size, void*
if (cl_assert(error)) { if (cl_assert(error)) {
Logger::log("Failed at clCreateBuffer() :" + cl_err_lookup(error), Logger::LogLevel::ERROR, __LINE__, __FILE__); Logger::log("Failed at clCreateBuffer() :" + cl_err_lookup(error), Logger::LogLevel::ERROR, __LINE__, __FILE__);
Logger::log("Buffer name : " + buffer_name, Logger::LogLevel::ERROR, __LINE__, __FILE__); Logger::log("Buffer name : " + buffer_name, Logger::LogLevel::ERROR, __LINE__, __FILE__);
return OPENCL_ERROR; return false;
} }
if (!store_buffer(buff, buffer_name)) if (!store_buffer(buff, buffer_name))

@ -33,7 +33,7 @@ void Logger::log(std::string log_string, LogLevel severity, uint32_t line_number
output << log_string.c_str(); output << log_string.c_str();
if (line_number > 0 && file_name) if (line_number > 0 && file_name)
output << "::" << file_name << ":" << line_number << std::endl; output << " (" << file_name << ":" << line_number << ")" << std::endl;
else else
output << std::endl; output << std::endl;
} }

@ -273,10 +273,17 @@ int main() {
ImGui::NextColumn(); ImGui::NextColumn();
if (ImGui::Button("Recompile kernel")) { if (ImGui::Button("Recompile kernel")) {
while (raycaster->debug_quick_recompile() != 0); while (!raycaster->debug_quick_recompile());
} }
if (ImGui::Button("Pause")) { if (ImGui::Button("Pause")) {
paused = !paused; paused = !paused;
if (paused)
Logger::log("Pausing", Logger::LogLevel::INFO);
else
Logger::log("Unpausing", Logger::LogLevel::INFO);
} }
ImGui::End(); ImGui::End();

Loading…
Cancel
Save