added shader

This commit is contained in:
Djairo Hougee 2025-01-05 22:48:09 +01:00
parent 126ef4ace8
commit edf4d9fd60
17 changed files with 257 additions and 153 deletions

View File

@ -5,6 +5,8 @@ project ("cuda-raytracer" LANGUAGES CUDA CXX C)
# get the shader files in the build directory # get the shader files in the build directory
file(COPY ${CMAKE_SOURCE_DIR}/src/gui/shaders DESTINATION .) file(COPY ${CMAKE_SOURCE_DIR}/src/gui/shaders DESTINATION .)
set(CMAKE_BUILD_TYPE Debug)
# source files # source files
file(GLOB_RECURSE SOURCE_FILES file(GLOB_RECURSE SOURCE_FILES
${CMAKE_SOURCE_DIR}/src/*.c ${CMAKE_SOURCE_DIR}/src/*.c

View File

@ -3,9 +3,11 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include "Shader.h"
Window::Window(unsigned int w, unsigned int h) { Window::Window(unsigned int w, unsigned int h) {
Window::w = w; this->w = w;
Window::h = h; this->h = h;
} }
void framebuffer_size_callback(GLFWwindow* window, int w, int 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 // init glfw
glfwInit(); glfwInit();
// requesting context version 1.0 makes glfw try to provide the latest version if possible // 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); 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); glfwSetInputMode(this->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetWindowUserPointer(this->window, reinterpret_cast<void*>(this)); glfwSetWindowUserPointer(this->window, reinterpret_cast<void*>(this));
@ -37,20 +39,17 @@ int Window::init() {
glfwMakeContextCurrent(this->window); glfwMakeContextCurrent(this->window);
// init glad(opengl) // init glad(opengl)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD\n"; std::cout << "Failed to initialize GLAD\n";
return -1; return -1;
} }
// init framebuffer // init framebuffer
glViewport(0, 0, this->w, this->h); glViewport(0, 0, this->w, this->h);
if (glfwSetFramebufferSizeCallback(this->window, framebuffer_size_callback) != 0) return -1; if (glfwSetFramebufferSizeCallback(this->window, framebuffer_size_callback) != 0) return -1;
if (init_quad(data)) return -1;
if (init_quad()) return -1;
this->last_frame = std::chrono::steady_clock::now(); this->last_frame = std::chrono::steady_clock::now();
while (!glfwWindowShouldClose(window)) { 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<Quad>(this->w, this->h); this->current_quad = std::make_unique<Quad>(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<Shader>("./shaders/vertshader.glsl", "./shaders/fragshader.glsl");
this->shader->use();
// TODO: default shaders
return 0; return 0;
} }
@ -107,6 +109,7 @@ void Window::tick() {
// render frame // render frame
glBindFramebuffer(GL_FRAMEBUFFER, this->current_quad->fb); glBindFramebuffer(GL_FRAMEBUFFER, this->current_quad->fb);
this->current_quad->render(); this->current_quad->render();
this->shader->use();
glBindVertexArray(this->current_quad->VAO); glBindVertexArray(this->current_quad->VAO);
glBindTexture(GL_TEXTURE_2D, this->current_quad->tex); glBindTexture(GL_TEXTURE_2D, this->current_quad->tex);
glDrawArrays(GL_TRIANGLES, 0, 6); // draw current frame to texture glDrawArrays(GL_TRIANGLES, 0, 6); // draw current frame to texture
@ -116,6 +119,8 @@ void Window::tick() {
glfwSwapBuffers(this->window); glfwSwapBuffers(this->window);
glfwPollEvents(); glfwPollEvents();
std::cout << "done ticking";
} }
void Window::resize(unsigned int w, unsigned int h) { void Window::resize(unsigned int w, unsigned int h) {

View File

@ -2,6 +2,7 @@
#define MAINWINDOW_H #define MAINWINDOW_H
#include "Quad.h" #include "Quad.h"
#include "Shader.h"
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <chrono> #include <chrono>
@ -11,10 +12,11 @@ class Window {
public: public:
unsigned int w; unsigned int w;
unsigned int h; unsigned int h;
float* data; //TODO: dynamic data loading
Window(unsigned int w, unsigned int h); Window(unsigned int w, unsigned int h);
int init(); int init(float* data);
void free(); void free();
void resize(unsigned int w, unsigned int h); void resize(unsigned int w, unsigned int h);
@ -25,8 +27,8 @@ private:
std::chrono::steady_clock::time_point last_frame; std::chrono::steady_clock::time_point last_frame;
void tick(); void tick();
int init_quad(); int init_quad(float* data);
// std::unique_ptr<Shader> shader; std::unique_ptr<Shader> shader;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -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); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
// register the FBO };
void Quad::make_fbo(){
glGenFramebuffers(1, &fb); glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb); glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->tex, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->tex, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
}; }
Quad::~Quad() { Quad::~Quad() {
int res = cudaGraphicsUnregisterResource(CGR); 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); int res = cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone);
if (res) { if (res) {
std::cout << "CUDA error while registering the graphics resource: " << res; std::cout << "CUDA error while registering the graphics resource: " << res;
cudaDeviceReset(); cudaDeviceReset();
exit(1); exit(1);
} }
this->renderer = std::make_unique<Raycaster>(this->CGR, this->w, this->h); this->renderer = std::make_unique<Raycaster>(this->CGR, this->w, this->h, data);
}; };

View File

@ -30,7 +30,8 @@ public:
void render(); void render();
void resize(unsigned int w, unsigned int h); void resize(unsigned int w, unsigned int h);
void cuda_init(); void cuda_init(float* data);
void make_fbo();
}; };
#endif // QUAD_H #endif // QUAD_H

126
src/gui/Shader.h Normal file
View File

@ -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 <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -2,7 +2,9 @@
#include "linalg/linalg.h" #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) { __device__ void FrameBuffer::writePixel(int x, int y, float r, float g, float b) {

View File

@ -8,7 +8,7 @@
class FrameBuffer { class FrameBuffer {
public: public:
uint32_t* buffer; unsigned int* buffer;
std::size_t buffer_size; std::size_t buffer_size;
unsigned int w; unsigned int w;
unsigned int h; unsigned int h;

View File

@ -10,6 +10,7 @@
#include "objs/sphere.h" #include "objs/sphere.h"
// TODO: instead of IMAGEWIDTH and IMAGEHEIGHT this should reflect the windowSize;
__global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer) { __global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer) {
int px = blockIdx.x * blockDim.x + threadIdx.x; int px = blockIdx.x * blockDim.x + threadIdx.x;
int py = blockIdx.y * blockDim.y + threadIdx.y; 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) { auto intersectAxis = [&](float start, float dirVal) {
if (fabsf(dirVal) < epsilon) { if (fabsf(dirVal) < epsilon) {
if (start < 0.f || start > 1.f) { if (start < 0.f || start > 1.f) {
tNear = 1e9f; tNear = 1e9f;
tFar = -1e9f; tFar = -1e9f;
} }
} else { } 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->resources = resources;
this->w = h; this->w = h;
this->w = h; this->w = h;
this->fb = new FrameBuffer(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); // 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<Camera*>(); // d_camera = thrust::device_new<Camera*>();
@ -154,15 +156,14 @@ Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h) {
void Raycaster::render() { void Raycaster::render() {
int res = cudaGraphicsMapresources(1, this->resources); int res = cudaGraphicsMapResources(1, &this->resources);
if (res) { if (res) {
std::cout << "CUDA error while mapping graphic resource: " << res; std::cout << "CUDA error while mapping graphic resource: " << res;
cudaDeviceReset(); cudaDeviceReset();
exit(1); 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), resources);
res = cudaGraphicsResourceGetMappedPointer((void**)(this->fb->buffer), &this->fb->buffer_size, this->resources);
if (res) { if (res) {
std::cout << "CUDA error while fetching resource pointer: " << res; std::cout << "CUDA error while fetching resource pointer: " << res;
cudaDeviceReset(); cudaDeviceReset();
@ -177,13 +178,12 @@ void Raycaster::render() {
dim3 threads(tx, ty); dim3 threads(tx, ty);
// TODO: pass camera info at some point // TODO: pass camera info at some point
// TODO: pass float volume data.
// frame buffer is implicitly copied to the device each frame // frame buffer is implicitly copied to the device each frame
raycastKernel<<<blocks, threads>>> (nullptr, this->fb); raycastKernel<<<blocks, threads>>> (this->data, *this->fb);
res = cudaGetLastError(); cudaError_t err = cudaGetLastError();
if (res) { if (err) {
std::cout << "CUDA error while raycasting: " << res; std::cout << "CUDA error while raycasting: " << cudaGetErrorString(err);
cudaDeviceReset(); cudaDeviceReset();
exit(1); exit(1);
} }

View File

@ -12,22 +12,22 @@ __global__ void raycastKernel(float* volumeData, unsigned char* framebuffer);
struct Raycaster { struct Raycaster {
// thrust::device_ptr<Camera*> d_camera; // thrust::device_ptr<Camera*> d_camera;
// CameraInfo camera_info; // CameraInfo camera_info;
cudaGraphicsResource_t resources; cudaGraphicsResource_t resources;
FrameBuffer* fb; FrameBuffer* fb;
float* data;
int w; int w;
int h; int h;
Raycaster() {};
Raycaster(cudaGraphicsResource_t resources, int nx, int ny);
// ~Raycaster();
void set_camera(Vec3 position, Vec3 forward, Vec3 up); Raycaster(cudaGraphicsResource_t resources, int nx, int ny, float* data);
void render(); // ~Raycaster();
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 #endif // RAYCASTER_H

View File

@ -34,13 +34,13 @@ __device__ unsigned int packUnorm4x8(float r, float g, float b, float a) {
uint out; uint out;
} u; } 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? // 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<float> v{r/len, g/len, b/len, a/len}; u.in[0] = round(r/len * 255.0f);
for (int i = 0; i < v.size(); i++) { u.in[1] = round(g/len * 255.0f);
u.in[i] = round(std::clamp(v[i], 0.0f, 1.0f) * 255.0f); u.in[2] = round(b/len * 255.0f);
} u.in[3] = round(a/len * 255.0f);
return u.out; return u.out;
} }

View File

@ -1,8 +0,0 @@
#include "gui/MainWindow.h"
#include "consts.h"
int main() {
Window window(IMAGE_WIDTH, IMAGE_HEIGHT);
return window.init();
}

View File

@ -1,15 +1,15 @@
#include <iostream>
#include <fstream>
#include <cmath>
#include <cuda_runtime.h>
#include <vector>
#include <algorithm> #include <algorithm>
#include <cmath>
#include "hurricanedata/datareader.h"
#include "linalg/linalg.h"
#include "img/handler.h"
#include "consts.h" #include "consts.h"
#include <cuda_runtime.h>
#include <fstream>
#include "gui/MainWindow.h"
#include "hurricanedata/datareader.h"
#include "illumination/illumination.h" #include "illumination/illumination.h"
#include "img/handler.h"
#include <iostream>
#include "linalg/linalg.h"
#include <vector>
static float* d_volume = nullptr; static float* d_volume = nullptr;
@ -45,69 +45,72 @@ void getSpeed(std::vector<float>& speedData, int idx = 0) {
} }
} }
// TODO: incorporate this main into main.cpp
int unmain(int argc, char** argv) { int main() {
std::vector<float> data; std::vector<float> data;
// getTemperature(data); // getTemperature(data);
getSpeed(data); getSpeed(data);
// TODO: Eveontually remove debug below (i.e., eliminate for-loop etc.) // TODO: Eveontually remove debug below (i.e., eliminate for-loop etc.)
// Generate debug volume data // Generate debug volume data
float* hostVolume = new float[VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH]; float* hostVolume = new float[VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH];
// generateVolume(hostVolume, 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 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) // Discard temperatures above a small star (supposedly, missing temperature values)
hostVolume[i] = data[i]; hostVolume[i] = data[i];
if (data[i] + epsilon >= infty) hostVolume[i] = 0.0f; if (data[i] + epsilon >= infty) hostVolume[i] = 0.0f;
} }
// Min-max normalization // Min-max normalization
float minVal = *std::min_element(hostVolume, hostVolume + VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH); 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); 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++) { for (int i = 0; i < VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; i++) {
hostVolume[i] = (hostVolume[i] - minVal) / (maxVal - minVal); hostVolume[i] = (hostVolume[i] - minVal) / (maxVal - minVal);
} }
// Allocate + copy data to GPU // Allocate + copy data to GPU
size_t volumeSize = sizeof(float) * VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; size_t volumeSize = sizeof(float) * VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH;
cudaMalloc((void**)&d_volume, volumeSize); cudaMalloc((void**)&d_volume, volumeSize);
cudaMemcpy(d_volume, hostVolume, volumeSize, cudaMemcpyHostToDevice); cudaMemcpy(d_volume, hostVolume, volumeSize, cudaMemcpyHostToDevice);
// Allocate framebuffer // Allocate framebuffer
unsigned char* d_framebuffer; // unsigned char* d_framebuffer;
size_t fbSize = IMAGE_WIDTH * IMAGE_HEIGHT * 3 * sizeof(unsigned char); // size_t fbSize = IMAGE_WIDTH * IMAGE_HEIGHT * 3 * sizeof(unsigned char);
cudaMalloc((void**)&d_framebuffer, fbSize); // cudaMalloc((void**)&d_framebuffer, fbSize);
cudaMemset(d_framebuffer, 0, fbSize); // cudaMemset(d_framebuffer, 0, fbSize);
// Copy external constants from consts.h to cuda // Copy external constants from consts.h to cuda
copyConstantsToDevice(); copyConstantsToDevice();
// NOTE: this shold be done within the rayTracer class // NOTE: this is done within the rayTracer class
// // Launch kernel // // Launch kernel
// dim3 blockSize(16, 16); // dim3 blockSize(16, 16);
// dim3 gridSize((IMAGE_WIDTH + blockSize.x - 1)/blockSize.x, // dim3 gridSize((IMAGE_WIDTH + blockSize.x - 1)/blockSize.x,
// (IMAGE_HEIGHT + blockSize.y - 1)/blockSize.y); // (IMAGE_HEIGHT + blockSize.y - 1)/blockSize.y);
// //
// raycastKernel<<<gridSize, blockSize>>>( // raycastKernel<<<gridSize, blockSize>>>(
// d_volume, // d_volume,
// d_framebuffer // d_framebuffer
// ); // );
// cudaDeviceSynchronize(); // cudaDeviceSynchronize();
// Copy framebuffer back to CPU Window window(IMAGE_WIDTH, IMAGE_HEIGHT);
unsigned char* hostFramebuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT * 3]; return window.init(d_volume);
cudaMemcpy(hostFramebuffer, d_framebuffer, fbSize, cudaMemcpyDeviceToHost);
// Export image // // Copy framebuffer back to CPU
saveImage("output.ppm", hostFramebuffer, IMAGE_WIDTH, IMAGE_HEIGHT); // unsigned char* hostFramebuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT * 3];
// cudaMemcpy(hostFramebuffer, d_framebuffer, fbSize, cudaMemcpyDeviceToHost);
// Cleanup //
delete[] hostVolume; // // Export image
delete[] hostFramebuffer; // saveImage("output.ppm", hostFramebuffer, IMAGE_WIDTH, IMAGE_HEIGHT);
cudaFree(d_volume); //
cudaFree(d_framebuffer); // // Cleanup //TODO: cleanup properly
// delete[] hostVolume;
std::cout << "Phong-DVR rendering done. Image saved to output.ppm" << std::endl; // delete[] hostFramebuffer;
return 0; // cudaFree(d_volume);
// cudaFree(d_framebuffer);
//
// std::cout << "Phong-DVR rendering done. Image saved to output.ppm" << std::endl;
// return 0;
} }