From c65af95a662642d925fbd680c284e13138501837 Mon Sep 17 00:00:00 2001 From: Robin Date: Sat, 18 Jan 2025 20:24:15 +0100 Subject: [PATCH 1/2] compiles on robins computer :) --- .gitignore | 1 + CMakeLists.txt | 15 +++++++++++---- default.nix | 21 +++++++++++++++++++++ src/consts.cu | 9 ++++++--- src/gui/MainWindow.cpp | 1 + src/gui/Quad.cpp | 1 + src/gui/input/Widget.cpp | 2 +- src/illumination/Raycaster.cu | 2 ++ src/main.cu | 4 ++-- 9 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 default.nix diff --git a/.gitignore b/.gitignore index 48c406d..698a114 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ data/ # Resulting images *.ppm + diff --git a/CMakeLists.txt b/CMakeLists.txt index 79cf298..192569c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ include_directories(${CMAKE_SOURCE_DIR}/include/imgui) add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES} ${IMGUI_FILES}) # CUDA has specific architectures - set it to the system's architecture if available (or 70 by default) -set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_ARCHITECTURES 70) +set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_ARCHITECTURES 52) set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON @@ -59,8 +59,8 @@ find_package(CUDA REQUIRED) include_directories("${CUDA_INCLUDE_DIRS}") # netcdf -find_package(netCDF REQUIRED) -message(STATUS "Found netcdf in ${GLFW3_INCLUDE_DIR}") +# find_package(netCDF REQUIRED) +# message(STATUS "Found netcdf in ${NETCDF_LIB}") execute_process( COMMAND nc-config --includedir @@ -74,9 +74,16 @@ execute_process( OUTPUT_STRIP_TRAILING_WHITESPACE ) +execute_process( + COMMAND ncxx4-config --prefix + OUTPUT_VARIABLE NETCDFCXX_PREFIX_DIR + OUTPUT_STRIP_TRAILING_WHITESPACE +) + target_include_directories(cuda-raytracer PUBLIC ${netCDF_INCLUDE_DIR}) -find_library(NETCDF_LIB NAMES netcdf-cxx4 netcdf_c++4 PATHS ${NETCDFCXX_LIB_DIR} NO_DEFAULT_PATH) +find_library(NETCDF_LIB NAMES netcdf-cxx4 netcdf_c++4 PATHS ${NETCDFCXX_LIB_DIR} ${NETCDFCXX_PREFIX_DIR}/lib NO_DEFAULT_PATH) +message(STATUS "Found NetCDFCXX in ${NETCDF_LIB}") set(LIBS ${GLFW3_LIBRARY} ${OPENGL_LIBRARY} GLAD ${CMAKE_DL_LIBS} ${CUDA_LIBRARIES} ${NETCDF_LIB}) diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..bcf23d9 --- /dev/null +++ b/default.nix @@ -0,0 +1,21 @@ +with import { config.allowUnfree = true; }; + +stdenv.mkDerivation { + name = "cuda-raytracer"; + src = ./.; + nativeBuildInputs = with cudaPackages; [cmake autoAddDriverRunpath autoPatchelfHook ]; + buildInputs = with pkgs; with cudaPackages; [ libGL glfw netcdf netcdfcxx4 cuda_nvcc cuda_cudart cuda_cccl libcurand]; + + postConfigure = '' + export netCDFCxx_DIR=${netcdfcxx4}/lib/cmake/netCDFCxx + ''; + + installTargets = "preinstall"; + + postInstall = '' + mkdir -p $out/bin + cp cuda-raytracer $out/bin + ''; + + LD_LIBRARY_PATH="/run/opengl-driver/lib/"; +} diff --git a/src/consts.cu b/src/consts.cu index 30e201f..0b953fe 100644 --- a/src/consts.cu +++ b/src/consts.cu @@ -1,9 +1,10 @@ #include "consts.h" +#include "cuda_error.h" // ----------------------- Colour mapping ----------------------- -__constant__ ColorStop d_stopsPythonLike[5]; -__constant__ ColorStop d_stopsGrayscale[2]; -__constant__ ColorStop d_stopsBluePurleRed[3]; +__device__ __constant__ ColorStop d_stopsPythonLike[5]; +__device__ __constant__ ColorStop d_stopsGrayscale[2]; +__device__ __constant__ ColorStop d_stopsBluePurleRed[3]; const ColorStop h_stopsPythonLike[] = { { 0.0f, Color3::init(0.2298057f, 0.29871797f, 0.75368315f) }, // Dark Blue @@ -37,8 +38,10 @@ Vec3 h_cameraUp = Vec3::init(0.0, 1.0, 0.0).normalize(); // Copy the above values to the device void copyConstantsToDevice() { + check_cuda_errors(cudaGetLastError()); // ----------------------- Colour mapping ----------------------- cudaMemcpyToSymbol(d_stopsPythonLike, h_stopsPythonLike, sizeof(h_stopsPythonLike)); + check_cuda_errors(cudaGetLastError()); cudaMemcpyToSymbol(d_stopsGrayscale, h_stopsGrayscale, sizeof(h_stopsGrayscale)); cudaMemcpyToSymbol(d_stopsBluePurleRed, h_stopsBluePurleRed, sizeof(h_stopsBluePurleRed)); diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index dd15872..df722da 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -6,6 +6,7 @@ #include "Shader.h" #include "input/Widget.h" +#include "cuda_error.h" // TODO: Delete diff --git a/src/gui/Quad.cpp b/src/gui/Quad.cpp index d918763..5451a71 100644 --- a/src/gui/Quad.cpp +++ b/src/gui/Quad.cpp @@ -83,6 +83,7 @@ void Quad::cuda_init(float* data) { void Quad::render() { + check_cuda_errors(cudaGetLastError()); glBindTexture(GL_TEXTURE_2D, 0); this->renderer->render(); glBindTexture(GL_TEXTURE_2D, this->tex); diff --git a/src/gui/input/Widget.cpp b/src/gui/input/Widget.cpp index e57d9a1..dc47e8d 100644 --- a/src/gui/input/Widget.cpp +++ b/src/gui/input/Widget.cpp @@ -65,7 +65,7 @@ void Widget::tick(double fps) { this->renderOnce = true; } sprintf(this->fps, "%.3f fps\n", fps); - ImGui::Text(this->fps); + ImGui::Text("%s", this->fps); ImGui::End(); ImGui::Begin("Camera Controls"); diff --git a/src/illumination/Raycaster.cu b/src/illumination/Raycaster.cu index 400cce1..a0c108a 100644 --- a/src/illumination/Raycaster.cu +++ b/src/illumination/Raycaster.cu @@ -156,6 +156,8 @@ Raycaster::Raycaster(cudaGraphicsResource_t resources, int w, int h, float* data void Raycaster::render() { + std::cout << "hello???\n"; + check_cuda_errors(cudaGetLastError()); check_cuda_errors(cudaGraphicsMapResources(1, &this->resources)); check_cuda_errors(cudaGraphicsResourceGetMappedPointer((void**)&(this->fb->buffer), &(this->fb->buffer_size), resources)); diff --git a/src/main.cu b/src/main.cu index 6127861..d471ff2 100644 --- a/src/main.cu +++ b/src/main.cu @@ -21,7 +21,7 @@ static float* d_volume = nullptr; // * time controls - arbitrary skipping to specified point (would require some changes to gpubuffer) (could have) void getTemperature(std::vector& temperatureData, int idx = 0) { - std::string path = "data/trimmed"; + std::string path = "../../../hurricane-sandy-data"; // std::string path = "data"; std::string variable = "T"; DataReader dataReader(path, variable); @@ -31,7 +31,7 @@ void getTemperature(std::vector& temperatureData, int idx = 0) { } void getSpeed(std::vector& speedData, int idx = 0) { - std::string path = "data/trimmed"; + std::string path = "../../../hurricane-sandy-data"; // std::string path = "data"; std::string varU = "U"; std::string varV = "V"; From bae68db6b898e7788e6f34137c8668a751d17985 Mon Sep 17 00:00:00 2001 From: Robin Date: Sun, 19 Jan 2025 22:14:38 +0100 Subject: [PATCH 2/2] improved camera controls --- src/gui/MainWindow.cpp | 6 ++++++ src/gui/input/Widget.cpp | 31 +++++++++++++++++++++++----- src/gui/input/Widget.h | 6 ++++-- src/illumination/Raycaster.cu | 2 +- src/illumination/transferFunction.cu | 3 ++- src/linalg/vec.h | 27 ++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 9ab73a5..8f19b1b 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -92,6 +92,7 @@ void Window::free(float* data) { void Window::tick() { + std::cout << "tick tick\n"; // manually track time diff std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); float diff = (float) std::chrono::duration_cast(now - this->last_frame).count(); @@ -100,10 +101,12 @@ void Window::tick() { // input this->widget->tick(1000.0/diff); + std::cout << "setting tick render\n"; // tick render glBindFramebuffer(GL_FRAMEBUFFER, 0); glDisable(GL_DEPTH_TEST); + std::cout << "blabla\n"; if (!this->widget->paused) this->quad->render(); this->shader->use(); glBindVertexArray(this->quad->VAO); @@ -111,11 +114,14 @@ void Window::tick() { glDrawArrays(GL_TRIANGLES, 0, 6); // draw current frame to texture // render ImGui context + std::cout << "rendering\n"; this->widget->render(); + std::cout << "done rendering\n"; // check for events glfwSwapBuffers(this->window); glfwPollEvents(); + std::cout << "done tick tick\n"; } diff --git a/src/gui/input/Widget.cpp b/src/gui/input/Widget.cpp index c7e6699..b03a795 100644 --- a/src/gui/input/Widget.cpp +++ b/src/gui/input/Widget.cpp @@ -3,6 +3,7 @@ #include "consts.h" #include #include +#include Widget::Widget(GLFWwindow* window) { @@ -13,9 +14,17 @@ Widget::Widget(GLFWwindow* window) { ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init(); - this->cameraPos = Point3::init(64.0f, 42.5f, 250.0f); // Camera for partially trimmed data set + // this->cameraPos = Point3::init(64.0f, 42.5f, 250.0f); // Camera for partially trimmed data set + this->cameraPos = Point3::init(59.0f, 77.5f, -18.0f); // Camera for partially trimmed data set // this->cameraPos = Point3::init(300.0f, 200.0f, -700.0f); // Camera for full data set - this->cameraDir = Point3::init(0.074f, -0.301f, -2.810f); + // this->pitch = -1.7; + // this->yaw = 1.475; + // this->roll = 0; + this->pitch = 0.7; + this->yaw = 4.85; + this->roll = -0.010; + this->cameraDir = Vec3::getDirectionFromEuler(pitch, yaw, roll); + // this->cameraDir = Point3::init(0.074f, -0.301f, -2.810f); // Vec3 h_center = Vec3::init((float)VOLUME_WIDTH/2.0f, (float)VOLUME_HEIGHT/2.0f, (float)VOLUME_DEPTH/2.0f); // this->cameraDir = (h_center - this->cameraPos).normalize(); @@ -119,11 +128,22 @@ void Widget::tick(double fps) { ImGui::DragScalar("X position", ImGuiDataType_Double, &this->cameraPos.x, 0.5f, &min, &max, "%.3f"); ImGui::DragScalar("Y position", ImGuiDataType_Double, &this->cameraPos.z, 0.5f, &min, &max, "%.3f"); ImGui::DragScalar("Z position", ImGuiDataType_Double, &this->cameraPos.y, 0.5f, &min, &max, "%.3f"); - ImGui::DragScalar("X direction", ImGuiDataType_Double, &this->cameraDir.x, 0.005f, &min, &max, "%.3f"); - ImGui::DragScalar("Y direction", ImGuiDataType_Double, &this->cameraDir.z, 0.005f, &min, &max, "%.3f"); - ImGui::DragScalar("Z direction", ImGuiDataType_Double, &this->cameraDir.y, 0.005f, &min, &max, "%.3f"); + // ImGui::DragScalar("X direction", ImGuiDataType_Double, &this->cameraDir.x, 0.005f, &min, &max, "%.3f"); + // ImGui::DragScalar("Y direction", ImGuiDataType_Double, &this->cameraDir.z, 0.005f, &min, &max, "%.3f"); + // ImGui::DragScalar("Z direction", ImGuiDataType_Double, &this->cameraDir.y, 0.005f, &min, &max, "%.3f"); + ImGui::DragScalar("Pitch", ImGuiDataType_Double, &this->pitch, 0.005f, &min, &max, "%.3f"); + ImGui::DragScalar("Yaw", ImGuiDataType_Double, &this->yaw, 0.005f, &min, &max, "%.3f"); + ImGui::DragScalar("Roll", ImGuiDataType_Double, &this->roll, 0.005f, &min, &max, "%.3f"); ImGui::End(); + this->cameraDir.setDirectionFromEuler(pitch, yaw, roll); + + // Calculate upCamera + Vec3 arbitraryVector = Vec3::init(1, 0, 0); + cameraUp = arbitraryVector.cross(cameraDir); + cameraUp.normalize(); + cameraUp.rotateAroundAxis(cameraDir, roll); + copyToDevice(); } @@ -134,6 +154,7 @@ void Widget::render() { void Widget::copyToDevice() { cudaMemcpyToSymbol(&d_cameraPos, &this->cameraPos, sizeof(Point3)); + cudaMemcpyToSymbol(&d_cameraUp, &this->cameraUp, sizeof(Point3)); cudaMemcpyToSymbol(&d_cameraDir, &this->cameraDir, sizeof(Vec3)); cudaMemcpyToSymbol(&d_lightPos, &this->lightPos, sizeof(Point3)); cudaMemcpyToSymbol(&d_backgroundColor, &this->bgColor, sizeof(Color3)); diff --git a/src/gui/input/Widget.h b/src/gui/input/Widget.h index 99a1ef2..34799c4 100644 --- a/src/gui/input/Widget.h +++ b/src/gui/input/Widget.h @@ -9,8 +9,10 @@ class Widget { public: - Point3 cameraDir; - Vec3 cameraPos; + double pitch, yaw, roll; + Vec3 cameraUp; + Vec3 cameraDir; + Point3 cameraPos; Point3 lightPos; Color3 bgColor; // TODO: widget diff --git a/src/illumination/Raycaster.cu b/src/illumination/Raycaster.cu index 94ac55c..8086e38 100644 --- a/src/illumination/Raycaster.cu +++ b/src/illumination/Raycaster.cu @@ -38,7 +38,7 @@ __global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer, const u *= tanHalfFov; v *= tanHalfFov; - // Find ray direction + Vec3 cameraRight = (d_cameraDir.cross(d_cameraUp)).normalize(); d_cameraUp = (cameraRight.cross(d_cameraDir)).normalize(); Vec3 rayDir = (d_cameraDir + cameraRight*u + d_cameraUp*v).normalize(); diff --git a/src/illumination/transferFunction.cu b/src/illumination/transferFunction.cu index a6b9087..6fc6fc8 100644 --- a/src/illumination/transferFunction.cu +++ b/src/illumination/transferFunction.cu @@ -111,7 +111,8 @@ __device__ float4 transferFunction(float density, const Vec3& grad, const Point3 result.x = 0.0f; result.y = 0.0f; result.z = 0.0f; - result.w = alpha; + // result.w = alpha; + result.w = d_opacityConst; } return result; diff --git a/src/linalg/vec.h b/src/linalg/vec.h index a243cb9..a017ba1 100644 --- a/src/linalg/vec.h +++ b/src/linalg/vec.h @@ -31,6 +31,33 @@ struct Vec3 { __host__ __device__ Vec3 cross(const Vec3& b) const { return Vec3::init(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x); } __host__ __device__ Vec3 normalize() const { double len = sqrt(x * x + y * y + z * z); return Vec3::init(x / len, y / len, z / len); } __host__ __device__ double length() const { return sqrt(x * x + y * y + z * z); } + + __host__ __device__ void setDirectionFromEuler(double pitch, double yaw, double roll) { + // Compute the direction vector using the Euler angles in radians + double cosPitch = cos(pitch); + double sinPitch = sin(pitch); + double cosYaw = cos(yaw); + double sinYaw = sin(yaw); + + // Direction vector components + x = cosPitch * cosYaw; + y = cosPitch * sinYaw; + z = sinPitch; + } + + static __host__ __device__ Vec3 getDirectionFromEuler(double pitch, double yaw, double roll) { + Vec3 v = Vec3::init(1,0,0); + v.setDirectionFromEuler(pitch, yaw, roll); + return v; + } + + __host__ __device__ void rotateAroundAxis(const Vec3& axis, double angle) { + double cosA = cos(angle); + double sinA = sin(angle); + + Vec3 rotated = *this * cosA + axis.cross(*this) * sinA + axis * axis.dot(*this) * (1 - cosA); + *this = rotated; + } }; typedef Vec3 Point3;