diff --git a/src/cuda_error.cpp b/src/cuda_error.cpp new file mode 100644 index 0000000..86cde92 --- /dev/null +++ b/src/cuda_error.cpp @@ -0,0 +1,13 @@ +#include "cuda_error.h" + +#include "cuda_runtime.h" +#include + + +void check_cuda(cudaError_t res, char const* const func, const char* const file, int const line) { + if (res) { + std::cout << "CUDA encountered an error: " << cudaGetErrorString(res) << " in " << file << ":" << line << std::endl; + cudaDeviceReset(); + exit(1); + } +} diff --git a/src/cuda_error.h b/src/cuda_error.h new file mode 100644 index 0000000..3556a27 --- /dev/null +++ b/src/cuda_error.h @@ -0,0 +1,10 @@ +#ifndef CUDA_ERROR_H +#define CUDA_ERROR_H + +#include "cuda_runtime.h" + +#define check_cuda_errors(val) check_cuda( (val), #val, __FILE__, __LINE__ ) +void check_cuda(cudaError_t res, char const* const func, const char* const file, int const line); + +#endif // CUDA_ERROR_H + diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 0a6e8f8..470a41f 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -119,7 +119,7 @@ void Window::tick() { glfwSwapBuffers(this->window); glfwPollEvents(); - std::cout << "done ticking"; + std::cout << "done ticking\n"; } diff --git a/src/gui/Quad.cpp b/src/gui/Quad.cpp index 0d069e1..c4fd629 100644 --- a/src/gui/Quad.cpp +++ b/src/gui/Quad.cpp @@ -1,5 +1,6 @@ #include "Quad.h" +#include "cuda_error.h" #include #include #include "cuda_runtime.h" @@ -71,22 +72,12 @@ void Quad::make_fbo(){ } Quad::~Quad() { - int res = cudaGraphicsUnregisterResource(CGR); - if (res) { - std::cout << "CUDA error while deregistering the graphics resource: " << res; - cudaDeviceReset(); - exit(1); - } + check_cuda_errors(cudaGraphicsUnregisterResource(CGR)); }; void Quad::cuda_init(float* data) { - int res = cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone); - if (res) { - std::cout << "CUDA error while registering the graphics resource: " << res; - cudaDeviceReset(); - exit(1); - } + check_cuda_errors(cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone)); this->renderer = std::make_unique(this->CGR, this->w, this->h, data); }; @@ -117,21 +108,10 @@ void Quad::resize(unsigned int w, unsigned int h) { glBindFramebuffer(GL_FRAMEBUFFER, 0); if (this->renderer != nullptr) { - // TODO: probably make a function for the cuda error checking - int res = cudaGraphicsUnregisterResource(CGR); - if (res) { - std::cout << "CUDA error while deregistering the graphics resource: " << res; - cudaDeviceReset(); - exit(1); - } + check_cuda_errors(cudaGraphicsUnregisterResource(CGR)); + check_cuda_errors(cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone)); - res = cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone); - if (res) { - std::cout << "CUDA error while registering the graphics resource: " << res; - cudaDeviceReset(); - exit(1); - } - this->renderer->resources = this->CGR; - this->renderer->resize(w, h); + this->renderer->resources = this->CGR; + this->renderer->resize(w, h); } }; diff --git a/src/illumination/Raycaster.cu b/src/illumination/Raycaster.cu index 4d6acd3..b99e293 100644 --- a/src/illumination/Raycaster.cu +++ b/src/illumination/Raycaster.cu @@ -5,6 +5,7 @@ #include "linalg/linalg.h" #include "consts.h" +#include "cuda_error.h" #include "shading.h" #include #include "objs/sphere.h" @@ -130,8 +131,8 @@ __global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer) { Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h, float* data) { this->resources = resources; - this->w = h; - this->w = h; + this->w = w; + this->h = h; this->fb = new FrameBuffer(w, h); this->data = data; @@ -139,68 +140,27 @@ Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h, float* data // camera_info = CameraInfo(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), 90.0f, (float) w, (float) h); // d_camera = thrust::device_new(); - int res = cudaDeviceSynchronize(); - if (res) { - std::cout << "CUDA error while synchronizing device: " << res; - cudaDeviceReset(); - exit(1); - } - - res = cudaDeviceSynchronize(); - if (res) { - std::cout << "CUDA error while synchronizing device: " << res; - cudaDeviceReset(); - exit(1); - } + check_cuda_errors(cudaDeviceSynchronize()); } void Raycaster::render() { - int res = cudaGraphicsMapResources(1, &this->resources); - if (res) { - std::cout << "CUDA error while mapping graphic resource: " << res; - cudaDeviceReset(); - exit(1); - } - - res = cudaGraphicsResourceGetMappedPointer((void**)&(this->fb->buffer), &(this->fb->buffer_size), resources); - if (res) { - std::cout << "CUDA error while fetching resource pointer: " << res; - cudaDeviceReset(); - exit(1); - } + check_cuda_errors(cudaGraphicsMapResources(1, &this->resources)); + check_cuda_errors(cudaGraphicsResourceGetMappedPointer((void**)&(this->fb->buffer), &(this->fb->buffer_size), resources)); // FIXME: might not be the best parallelization configuraiton - int tx = 32; - int ty = 32; - - dim3 blocks(this->w / tx + 1, this->h / ty + 1); - dim3 threads(tx, ty); + int tx = 16; + int ty = 16; + dim3 threadSize(this->w / tx + 1, this->h / ty + 1); + dim3 blockSize(tx, ty); // TODO: pass camera info at some point // frame buffer is implicitly copied to the device each frame - raycastKernel<<>> (this->data, *this->fb); + raycastKernel<<>> (this->data, *this->fb); - cudaError_t err = cudaGetLastError(); - if (err) { - std::cout << "CUDA error while raycasting: " << cudaGetErrorString(err); - cudaDeviceReset(); - exit(1); - } - - res = cudaDeviceSynchronize(); - if (res) { - std::cout << "CUDA error while synchronizing device: " << res; - cudaDeviceReset(); - exit(1); - } - - res = cudaGraphicsUnmapResources(1, &this->resources); - if (res) { - std::cout << "CUDA error while unmapping a resource: " << res; - cudaDeviceReset(); - exit(1); - } + check_cuda_errors(cudaGetLastError()); + check_cuda_errors(cudaDeviceSynchronize()); + check_cuda_errors(cudaGraphicsUnmapResources(1, &this->resources)); } @@ -218,10 +178,5 @@ void Raycaster::resize(int w, int h) { dim3 blocks(w / tx + 1, h / ty + 1); dim3 threads(tx, ty); - int res = cudaDeviceSynchronize(); - if (res != 0) { - std::cout << "CUDA error while synchronizing device: " << res; - cudaDeviceReset(); - exit(1); - } + check_cuda_errors(cudaDeviceSynchronize()); }