From edf4d9fd6034ef99eb08be5c5eab55f8e1e7aa71 Mon Sep 17 00:00:00 2001 From: djairoh Date: Sun, 5 Jan 2025 22:48:09 +0100 Subject: [PATCH] added shader --- CMakeLists.txt | 2 + src/gui/MainWindow.cpp | 27 ++-- src/gui/MainWindow.h | 8 +- src/gui/Quad.cpp | 11 +- src/gui/Quad.h | 3 +- src/gui/Shader.h | 126 +++++++++++++++++ ...rendertype_screen.frag => fragshader.glsl} | 0 src/gui/shaders/rendertype_accumulate.frag | 19 --- src/gui/shaders/rendertype_screen.vert | 11 -- ...ertype_accumulate.vert => vertshader.glsl} | 0 src/illumination/FrameBuffer.cu | 4 +- src/illumination/FrameBuffer.h | 2 +- src/illumination/Raycaster.cu | 20 +-- src/illumination/Raycaster.h | 26 ++-- src/linalg/mat.cu | 10 +- src/main.cpp | 8 -- src/main.cu | 133 +++++++++--------- 17 files changed, 257 insertions(+), 153 deletions(-) create mode 100644 src/gui/Shader.h rename src/gui/shaders/{rendertype_screen.frag => fragshader.glsl} (100%) delete mode 100644 src/gui/shaders/rendertype_accumulate.frag delete mode 100644 src/gui/shaders/rendertype_screen.vert rename src/gui/shaders/{rendertype_accumulate.vert => vertshader.glsl} (100%) delete mode 100644 src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index faa86b6..cacca9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,8 @@ project ("cuda-raytracer" LANGUAGES CUDA CXX C) # get the shader files in the build directory file(COPY ${CMAKE_SOURCE_DIR}/src/gui/shaders DESTINATION .) +set(CMAKE_BUILD_TYPE Debug) + # source files file(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.c diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 2878a25..0a6e8f8 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -3,9 +3,11 @@ #include #include +#include "Shader.h" + Window::Window(unsigned int w, unsigned int h) { - Window::w = w; - Window::h = h; + this->w = w; + this->h = h; } void framebuffer_size_callback(GLFWwindow* window, int w, int h) { @@ -16,7 +18,7 @@ void framebuffer_size_callback(GLFWwindow* window, int w, int h) { } -int Window::init() { +int Window::init(float* data) { // init glfw glfwInit(); // requesting context version 1.0 makes glfw try to provide the latest version if possible @@ -25,7 +27,7 @@ int Window::init() { this->window = glfwCreateWindow(this->w, this->h, "CUDA ray tracing", NULL, NULL); - //hide cursor + //hide cursor // TODO: switch from this style input to something more resembling an actual gui glfwSetInputMode(this->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetWindowUserPointer(this->window, reinterpret_cast(this)); @@ -37,20 +39,17 @@ int Window::init() { glfwMakeContextCurrent(this->window); - // init glad(opengl) if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD\n"; return -1; } - // init framebuffer glViewport(0, 0, this->w, this->h); if (glfwSetFramebufferSizeCallback(this->window, framebuffer_size_callback) != 0) return -1; - - if (init_quad()) return -1; + if (init_quad(data)) return -1; this->last_frame = std::chrono::steady_clock::now(); while (!glfwWindowShouldClose(window)) { @@ -62,11 +61,14 @@ int Window::init() { } -int Window::init_quad() { +int Window::init_quad(float* data) { this->current_quad = std::make_unique(this->w, this->h); - this->current_quad->cuda_init(); + this->current_quad->cuda_init(data); + this->current_quad->make_fbo(); + + this->shader = std::make_unique("./shaders/vertshader.glsl", "./shaders/fragshader.glsl"); + this->shader->use(); - // TODO: default shaders return 0; } @@ -107,6 +109,7 @@ void Window::tick() { // render frame glBindFramebuffer(GL_FRAMEBUFFER, this->current_quad->fb); this->current_quad->render(); + this->shader->use(); glBindVertexArray(this->current_quad->VAO); glBindTexture(GL_TEXTURE_2D, this->current_quad->tex); glDrawArrays(GL_TRIANGLES, 0, 6); // draw current frame to texture @@ -115,6 +118,8 @@ void Window::tick() { // + swap buffers; TODO: check if necessary? glfwSwapBuffers(this->window); glfwPollEvents(); + + std::cout << "done ticking"; } diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index bd7ea79..5d66530 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include "Quad.h" +#include "Shader.h" #include #include #include @@ -11,10 +12,11 @@ class Window { public: unsigned int w; unsigned int h; + float* data; //TODO: dynamic data loading Window(unsigned int w, unsigned int h); - int init(); + int init(float* data); void free(); void resize(unsigned int w, unsigned int h); @@ -25,8 +27,8 @@ private: std::chrono::steady_clock::time_point last_frame; void tick(); - int init_quad(); + int init_quad(float* data); - // std::unique_ptr shader; + std::unique_ptr shader; }; #endif // MAINWINDOW_H diff --git a/src/gui/Quad.cpp b/src/gui/Quad.cpp index 5a3ffd4..0d069e1 100644 --- a/src/gui/Quad.cpp +++ b/src/gui/Quad.cpp @@ -61,13 +61,14 @@ Quad::Quad(unsigned int w, unsigned int h) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); - // register the FBO +}; + +void Quad::make_fbo(){ glGenFramebuffers(1, &fb); glBindFramebuffer(GL_FRAMEBUFFER, fb); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->tex, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); -}; - +} Quad::~Quad() { int res = cudaGraphicsUnregisterResource(CGR); @@ -79,14 +80,14 @@ Quad::~Quad() { }; -void Quad::cuda_init() { +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); } - this->renderer = std::make_unique(this->CGR, this->w, this->h); + this->renderer = std::make_unique(this->CGR, this->w, this->h, data); }; diff --git a/src/gui/Quad.h b/src/gui/Quad.h index 6e6f9ec..e4fa006 100644 --- a/src/gui/Quad.h +++ b/src/gui/Quad.h @@ -30,7 +30,8 @@ public: void render(); void resize(unsigned int w, unsigned int h); - void cuda_init(); + void cuda_init(float* data); + void make_fbo(); }; #endif // QUAD_H diff --git a/src/gui/Shader.h b/src/gui/Shader.h new file mode 100644 index 0000000..12d7648 --- /dev/null +++ b/src/gui/Shader.h @@ -0,0 +1,126 @@ +// Shader class nicked from learnOpengl here: https://learnopengl.com/Getting-started/Shaders +// which is published under the CC BY-NC 4.0 license (and thus fine to use here) + +#ifndef SHADER_H +#define SHADER_H + +#include + +#include +#include +#include +#include + +class Shader +{ +public: + unsigned int ID; + // constructor generates the shader on the fly + // ------------------------------------------------------------------------ + Shader(const char* vertexPath, const char* fragmentPath) + { + // 1. retrieve the vertex/fragment source code from filePath + std::string vertexCode; + std::string fragmentCode; + std::ifstream vShaderFile; + std::ifstream fShaderFile; + // ensure ifstream objects can throw exceptions: + vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); + fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); + try + { + // open files + vShaderFile.open(vertexPath); + fShaderFile.open(fragmentPath); + std::stringstream vShaderStream, fShaderStream; + // read file's buffer contents into streams + vShaderStream << vShaderFile.rdbuf(); + fShaderStream << fShaderFile.rdbuf(); + // close file handlers + vShaderFile.close(); + fShaderFile.close(); + // convert stream into string + vertexCode = vShaderStream.str(); + fragmentCode = fShaderStream.str(); + } + catch (std::ifstream::failure& e) + { + std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl; + } + const char* vShaderCode = vertexCode.c_str(); + const char * fShaderCode = fragmentCode.c_str(); + // 2. compile shaders + unsigned int vertex, fragment; + // vertex shader + vertex = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex, 1, &vShaderCode, NULL); + glCompileShader(vertex); + checkCompileErrors(vertex, "VERTEX"); + // fragment Shader + fragment = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment, 1, &fShaderCode, NULL); + glCompileShader(fragment); + checkCompileErrors(fragment, "FRAGMENT"); + // shader Program + ID = glCreateProgram(); + glAttachShader(ID, vertex); + glAttachShader(ID, fragment); + glLinkProgram(ID); + checkCompileErrors(ID, "PROGRAM"); + // delete the shaders as they're linked into our program now and no longer necessary + glDeleteShader(vertex); + glDeleteShader(fragment); + } + // activate the shader + // ------------------------------------------------------------------------ + void use() + { + glUseProgram(ID); + } + // utility uniform functions + // ------------------------------------------------------------------------ + void setBool(const std::string &name, bool value) const + { + glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value); + } + // ------------------------------------------------------------------------ + void setInt(const std::string &name, int value) const + { + glUniform1i(glGetUniformLocation(ID, name.c_str()), value); + } + // ------------------------------------------------------------------------ + void setFloat(const std::string &name, float value) const + { + glUniform1f(glGetUniformLocation(ID, name.c_str()), value); + } + +private: + // utility function for checking shader compilation/linking errors. + // ------------------------------------------------------------------------ + void checkCompileErrors(unsigned int shader, std::string type) + { + int success; + char infoLog[1024]; + if (type != "PROGRAM") + { + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(shader, 1024, NULL, infoLog); + std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; + } + } + else + { + glGetProgramiv(shader, GL_LINK_STATUS, &success); + if (!success) + { + glGetProgramInfoLog(shader, 1024, NULL, infoLog); + std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; + } + } + } +}; +#endif + + diff --git a/src/gui/shaders/rendertype_screen.frag b/src/gui/shaders/fragshader.glsl similarity index 100% rename from src/gui/shaders/rendertype_screen.frag rename to src/gui/shaders/fragshader.glsl diff --git a/src/gui/shaders/rendertype_accumulate.frag b/src/gui/shaders/rendertype_accumulate.frag deleted file mode 100644 index cce423c..0000000 --- a/src/gui/shaders/rendertype_accumulate.frag +++ /dev/null @@ -1,19 +0,0 @@ -#version 330 core -out vec4 FragColor; - -in vec2 TexCoords; - -uniform sampler2D currentFrameTex; -uniform sampler2D lastFrameTex; -uniform int frameCount; - -#define MAX_FRAMES 500.0f - -void main() -{ - vec3 col = texture(currentFrameTex, TexCoords).rgb; - vec3 col2 = texture(lastFrameTex, TexCoords).rgb; - - col = mix(col, col2, min(frameCount/MAX_FRAMES,1.0f)); - FragColor = vec4(col, 1.0); -} \ No newline at end of file diff --git a/src/gui/shaders/rendertype_screen.vert b/src/gui/shaders/rendertype_screen.vert deleted file mode 100644 index 737bdb3..0000000 --- a/src/gui/shaders/rendertype_screen.vert +++ /dev/null @@ -1,11 +0,0 @@ -#version 330 core -layout(location = 0) in vec2 aPos; -layout(location = 1) in vec2 aTexCoords; - -out vec2 TexCoords; - -void main() -{ - TexCoords = aTexCoords; - gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); -} \ No newline at end of file diff --git a/src/gui/shaders/rendertype_accumulate.vert b/src/gui/shaders/vertshader.glsl similarity index 100% rename from src/gui/shaders/rendertype_accumulate.vert rename to src/gui/shaders/vertshader.glsl diff --git a/src/illumination/FrameBuffer.cu b/src/illumination/FrameBuffer.cu index 0edcc58..e04b865 100644 --- a/src/illumination/FrameBuffer.cu +++ b/src/illumination/FrameBuffer.cu @@ -2,7 +2,9 @@ #include "linalg/linalg.h" -__host__ FrameBuffer::FrameBuffer(unsigned int w, unsigned int h) : w(w), h(h) {} +__host__ FrameBuffer::FrameBuffer(unsigned int w, unsigned int h) : w(w), h(h) { + this->buffer_size = w*h*4; +} __device__ void FrameBuffer::writePixel(int x, int y, float r, float g, float b) { diff --git a/src/illumination/FrameBuffer.h b/src/illumination/FrameBuffer.h index a2577fe..c7e83ad 100644 --- a/src/illumination/FrameBuffer.h +++ b/src/illumination/FrameBuffer.h @@ -8,7 +8,7 @@ class FrameBuffer { public: - uint32_t* buffer; + unsigned int* buffer; std::size_t buffer_size; unsigned int w; unsigned int h; diff --git a/src/illumination/Raycaster.cu b/src/illumination/Raycaster.cu index 4f7b88e..4d6acd3 100644 --- a/src/illumination/Raycaster.cu +++ b/src/illumination/Raycaster.cu @@ -10,6 +10,7 @@ #include "objs/sphere.h" +// TODO: instead of IMAGEWIDTH and IMAGEHEIGHT this should reflect the windowSize; __global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer) { int px = blockIdx.x * blockDim.x + threadIdx.x; int py = blockIdx.y * blockDim.y + threadIdx.y; @@ -41,7 +42,7 @@ __global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer) { auto intersectAxis = [&](float start, float dirVal) { if (fabsf(dirVal) < epsilon) { if (start < 0.f || start > 1.f) { - tNear = 1e9f; + tNear = 1e9f; tFar = -1e9f; } } else { @@ -127,12 +128,13 @@ __global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer) { } -Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h) { +Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h, float* data) { this->resources = resources; this->w = h; this->w = h; this->fb = new FrameBuffer(w, h); + this->data = 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(); @@ -154,15 +156,14 @@ Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h) { void Raycaster::render() { - int res = cudaGraphicsMapresources(1, this->resources); + int res = cudaGraphicsMapResources(1, &this->resources); if (res) { std::cout << "CUDA error while mapping graphic resource: " << res; cudaDeviceReset(); exit(1); } - // check_cuda_errors(cudaGraphicsResourceGetMappedPointer((void**)&(frame_buffer->device_ptr), &(frame_buffer->buffer_size), resources)); - res = cudaGraphicsResourceGetMappedPointer((void**)(this->fb->buffer), &this->fb->buffer_size, this->resources); + res = cudaGraphicsResourceGetMappedPointer((void**)&(this->fb->buffer), &(this->fb->buffer_size), resources); if (res) { std::cout << "CUDA error while fetching resource pointer: " << res; cudaDeviceReset(); @@ -177,13 +178,12 @@ void Raycaster::render() { dim3 threads(tx, ty); // TODO: pass camera info at some point - // TODO: pass float volume data. // frame buffer is implicitly copied to the device each frame - raycastKernel<<>> (nullptr, this->fb); + raycastKernel<<>> (this->data, *this->fb); - res = cudaGetLastError(); - if (res) { - std::cout << "CUDA error while raycasting: " << res; + cudaError_t err = cudaGetLastError(); + if (err) { + std::cout << "CUDA error while raycasting: " << cudaGetErrorString(err); cudaDeviceReset(); exit(1); } diff --git a/src/illumination/Raycaster.h b/src/illumination/Raycaster.h index 170eb41..44ce2a6 100644 --- a/src/illumination/Raycaster.h +++ b/src/illumination/Raycaster.h @@ -12,22 +12,22 @@ __global__ void raycastKernel(float* volumeData, unsigned char* framebuffer); struct Raycaster { - // thrust::device_ptr d_camera; - // CameraInfo camera_info; + // thrust::device_ptr d_camera; + // CameraInfo camera_info; - cudaGraphicsResource_t resources; - FrameBuffer* fb; + cudaGraphicsResource_t resources; + FrameBuffer* fb; + float* data; - int w; - int h; + int w; + int h; + - Raycaster() {}; - Raycaster(cudaGraphicsResource_t resources, int nx, int ny); - // ~Raycaster(); + Raycaster(cudaGraphicsResource_t resources, int nx, int ny, float* data); + // ~Raycaster(); - void set_camera(Vec3 position, Vec3 forward, Vec3 up); - void render(); - void resize(int nx, int ny); - // void raycastKernel(float* volumeData, unsigned char* framebuffer); // TODO: proper framebuffer class + void set_camera(Vec3 position, Vec3 forward, Vec3 up); + void render(); + void resize(int nx, int ny); }; #endif // RAYCASTER_H diff --git a/src/linalg/mat.cu b/src/linalg/mat.cu index d5237e1..c5bdd4a 100644 --- a/src/linalg/mat.cu +++ b/src/linalg/mat.cu @@ -34,13 +34,13 @@ __device__ unsigned int packUnorm4x8(float r, float g, float b, float a) { uint out; } u; - double len = sqrt(r*r + g*g + b*b + a*a); + float len = sqrtf(r*r + g*g + b*b + a*a); // This is a Vec4 but i can't be bothered to make that its own struct/class; FIXME: maybe do that if we need to? - std::vector v{r/len, g/len, b/len, a/len}; - for (int i = 0; i < v.size(); i++) { - u.in[i] = round(std::clamp(v[i], 0.0f, 1.0f) * 255.0f); - } + u.in[0] = round(r/len * 255.0f); + u.in[1] = round(g/len * 255.0f); + u.in[2] = round(b/len * 255.0f); + u.in[3] = round(a/len * 255.0f); return u.out; } diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 60211cb..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "gui/MainWindow.h" -#include "consts.h" - - -int main() { - Window window(IMAGE_WIDTH, IMAGE_HEIGHT); - return window.init(); -} diff --git a/src/main.cu b/src/main.cu index 4a0ceb4..f232b3e 100644 --- a/src/main.cu +++ b/src/main.cu @@ -1,15 +1,15 @@ -#include -#include -#include -#include -#include #include - -#include "hurricanedata/datareader.h" -#include "linalg/linalg.h" -#include "img/handler.h" +#include #include "consts.h" +#include +#include +#include "gui/MainWindow.h" +#include "hurricanedata/datareader.h" #include "illumination/illumination.h" +#include "img/handler.h" +#include +#include "linalg/linalg.h" +#include static float* d_volume = nullptr; @@ -45,69 +45,72 @@ void getSpeed(std::vector& speedData, int idx = 0) { } } -// TODO: incorporate this main into main.cpp -int unmain(int argc, char** argv) { - std::vector data; - // getTemperature(data); - getSpeed(data); + +int main() { + std::vector data; + // getTemperature(data); + getSpeed(data); - // TODO: Eveontually remove debug below (i.e., eliminate for-loop etc.) - // Generate debug volume data - float* hostVolume = new float[VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH]; - // generateVolume(hostVolume, VOLUME_WIDTH, VOLUME_HEIGHT, VOLUME_DEPTH); - for (int i = 0; i < VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; i++) { // TODO: This is technically an unnecessary artifact of the old code taking in a float* instead of a std::vector - // Discard temperatures above a small star (supposedly, missing temperature values) - hostVolume[i] = data[i]; - if (data[i] + epsilon >= infty) hostVolume[i] = 0.0f; - } + // TODO: Eveontually remove debug below (i.e., eliminate for-loop etc.) + // Generate debug volume data + float* hostVolume = new float[VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH]; + // generateVolume(hostVolume, VOLUME_WIDTH, VOLUME_HEIGHT, VOLUME_DEPTH); + for (int i = 0; i < VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; i++) { // TODO: This is technically an unnecessary artifact of the old code taking in a float* instead of a std::vector + // Discard temperatures above a small star (supposedly, missing temperature values) + hostVolume[i] = data[i]; + if (data[i] + epsilon >= infty) hostVolume[i] = 0.0f; + } - // Min-max normalization - float minVal = *std::min_element(hostVolume, hostVolume + VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH); - float maxVal = *std::max_element(hostVolume, hostVolume + VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH); - for (int i = 0; i < VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; i++) { - hostVolume[i] = (hostVolume[i] - minVal) / (maxVal - minVal); - } + // Min-max normalization + float minVal = *std::min_element(hostVolume, hostVolume + VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH); + float maxVal = *std::max_element(hostVolume, hostVolume + VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH); + for (int i = 0; i < VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; i++) { + hostVolume[i] = (hostVolume[i] - minVal) / (maxVal - minVal); + } - // Allocate + copy data to GPU - size_t volumeSize = sizeof(float) * VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; - cudaMalloc((void**)&d_volume, volumeSize); - cudaMemcpy(d_volume, hostVolume, volumeSize, cudaMemcpyHostToDevice); + // Allocate + copy data to GPU + size_t volumeSize = sizeof(float) * VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; + cudaMalloc((void**)&d_volume, volumeSize); + cudaMemcpy(d_volume, hostVolume, volumeSize, cudaMemcpyHostToDevice); - // Allocate framebuffer - unsigned char* d_framebuffer; - size_t fbSize = IMAGE_WIDTH * IMAGE_HEIGHT * 3 * sizeof(unsigned char); - cudaMalloc((void**)&d_framebuffer, fbSize); - cudaMemset(d_framebuffer, 0, fbSize); + // Allocate framebuffer + // unsigned char* d_framebuffer; + // size_t fbSize = IMAGE_WIDTH * IMAGE_HEIGHT * 3 * sizeof(unsigned char); + // cudaMalloc((void**)&d_framebuffer, fbSize); + // cudaMemset(d_framebuffer, 0, fbSize); - // Copy external constants from consts.h to cuda - copyConstantsToDevice(); + // Copy external constants from consts.h to cuda + copyConstantsToDevice(); - // NOTE: this shold be done within the rayTracer class - // // Launch kernel - // dim3 blockSize(16, 16); - // dim3 gridSize((IMAGE_WIDTH + blockSize.x - 1)/blockSize.x, - // (IMAGE_HEIGHT + blockSize.y - 1)/blockSize.y); - // - // raycastKernel<<>>( - // d_volume, - // d_framebuffer - // ); - // cudaDeviceSynchronize(); + // NOTE: this is done within the rayTracer class + // // Launch kernel + // dim3 blockSize(16, 16); + // dim3 gridSize((IMAGE_WIDTH + blockSize.x - 1)/blockSize.x, + // (IMAGE_HEIGHT + blockSize.y - 1)/blockSize.y); + // + // raycastKernel<<>>( + // d_volume, + // d_framebuffer + // ); + // cudaDeviceSynchronize(); - // Copy framebuffer back to CPU - unsigned char* hostFramebuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT * 3]; - cudaMemcpy(hostFramebuffer, d_framebuffer, fbSize, cudaMemcpyDeviceToHost); + Window window(IMAGE_WIDTH, IMAGE_HEIGHT); + return window.init(d_volume); - // Export image - saveImage("output.ppm", hostFramebuffer, IMAGE_WIDTH, IMAGE_HEIGHT); - - // Cleanup - delete[] hostVolume; - delete[] hostFramebuffer; - cudaFree(d_volume); - cudaFree(d_framebuffer); - - std::cout << "Phong-DVR rendering done. Image saved to output.ppm" << std::endl; - return 0; + // // Copy framebuffer back to CPU + // unsigned char* hostFramebuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT * 3]; + // cudaMemcpy(hostFramebuffer, d_framebuffer, fbSize, cudaMemcpyDeviceToHost); + // + // // Export image + // saveImage("output.ppm", hostFramebuffer, IMAGE_WIDTH, IMAGE_HEIGHT); + // + // // Cleanup //TODO: cleanup properly + // delete[] hostVolume; + // delete[] hostFramebuffer; + // cudaFree(d_volume); + // cudaFree(d_framebuffer); + // + // std::cout << "Phong-DVR rendering done. Image saved to output.ppm" << std::endl; + // return 0; }